2 * Copyright (C) 2007 The Android Open Source Project
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #define LOG_TAG "Surface"
23 #include "android_os_Parcel.h"
24 #include "android/graphics/GraphicsJNI.h"
26 #include <android_runtime/AndroidRuntime.h>
27 #include <android_runtime/android_view_Surface.h>
28 #include <android_runtime/android_graphics_SurfaceTexture.h>
29 #include <android_runtime/Log.h>
31 #include <binder/Parcel.h>
33 #include <gui/Surface.h>
34 #include <gui/SurfaceControl.h>
35 #include <gui/GLConsumer.h>
38 #include <ui/Region.h>
45 #include <utils/misc.h>
46 #include <utils/Log.h>
48 #include <ScopedUtfChars.h>
50 #include <AnimationContext.h>
51 #include <DisplayListRenderer.h>
52 #include <FrameInfo.h>
53 #include <RenderNode.h>
54 #include <renderthread/RenderProxy.h>
56 // ----------------------------------------------------------------------------
60 static const char* const OutOfResourcesException =
61 "android/view/Surface$OutOfResourcesException";
65 jfieldID mNativeObject;
78 jfieldID mSurfaceFormat;
79 jmethodID setNativeBitmap;
82 // ----------------------------------------------------------------------------
84 // this is just a pointer we use to pass to inc/decStrong
85 static const void *sRefBaseOwner;
87 bool android_view_Surface_isInstanceOf(JNIEnv* env, jobject obj) {
88 return env->IsInstanceOf(obj, gSurfaceClassInfo.clazz);
91 sp<ANativeWindow> android_view_Surface_getNativeWindow(JNIEnv* env, jobject surfaceObj) {
92 return android_view_Surface_getSurface(env, surfaceObj);
95 sp<Surface> android_view_Surface_getSurface(JNIEnv* env, jobject surfaceObj) {
97 jobject lock = env->GetObjectField(surfaceObj,
98 gSurfaceClassInfo.mLock);
99 if (env->MonitorEnter(lock) == JNI_OK) {
100 sur = reinterpret_cast<Surface *>(
101 env->GetLongField(surfaceObj, gSurfaceClassInfo.mNativeObject));
102 env->MonitorExit(lock);
104 env->DeleteLocalRef(lock);
108 jobject android_view_Surface_createFromIGraphicBufferProducer(JNIEnv* env,
109 const sp<IGraphicBufferProducer>& bufferProducer) {
110 if (bufferProducer == NULL) {
114 sp<Surface> surface(new Surface(bufferProducer, true));
115 if (surface == NULL) {
119 jobject surfaceObj = env->NewObject(gSurfaceClassInfo.clazz,
120 gSurfaceClassInfo.ctor, (jlong)surface.get());
121 if (surfaceObj == NULL) {
122 if (env->ExceptionCheck()) {
123 ALOGE("Could not create instance of Surface from IGraphicBufferProducer.");
125 env->ExceptionClear();
129 surface->incStrong(&sRefBaseOwner);
133 // ----------------------------------------------------------------------------
135 static inline bool isSurfaceValid(const sp<Surface>& sur) {
136 return Surface::isValid(sur);
139 // ----------------------------------------------------------------------------
141 static jlong nativeCreateFromSurfaceTexture(JNIEnv* env, jclass clazz,
142 jobject surfaceTextureObj) {
143 sp<IGraphicBufferProducer> producer(SurfaceTexture_getProducer(env, surfaceTextureObj));
144 if (producer == NULL) {
145 jniThrowException(env, "java/lang/IllegalArgumentException",
146 "SurfaceTexture has already been released");
150 sp<Surface> surface(new Surface(producer, true));
151 if (surface == NULL) {
152 jniThrowException(env, OutOfResourcesException, NULL);
156 surface->incStrong(&sRefBaseOwner);
157 return jlong(surface.get());
160 static void nativeRelease(JNIEnv* env, jclass clazz, jlong nativeObject) {
161 sp<Surface> sur(reinterpret_cast<Surface *>(nativeObject));
162 sur->decStrong(&sRefBaseOwner);
165 static jboolean nativeIsValid(JNIEnv* env, jclass clazz, jlong nativeObject) {
166 sp<Surface> sur(reinterpret_cast<Surface *>(nativeObject));
167 return isSurfaceValid(sur) ? JNI_TRUE : JNI_FALSE;
170 static jboolean nativeIsConsumerRunningBehind(JNIEnv* env, jclass clazz, jlong nativeObject) {
171 sp<Surface> sur(reinterpret_cast<Surface *>(nativeObject));
172 if (!isSurfaceValid(sur)) {
177 ANativeWindow* anw = static_cast<ANativeWindow*>(sur.get());
178 anw->query(anw, NATIVE_WINDOW_CONSUMER_RUNNING_BEHIND, &value);
182 static inline SkColorType convertPixelFormat(PixelFormat format) {
183 /* note: if PIXEL_FORMAT_RGBX_8888 means that all alpha bytes are 0xFF, then
184 we can map to kN32_SkColorType, and optionally call
185 bitmap.setAlphaType(kOpaque_SkAlphaType) on the resulting SkBitmap
189 case PIXEL_FORMAT_RGBX_8888: return kN32_SkColorType;
190 case PIXEL_FORMAT_RGBA_8888: return kN32_SkColorType;
191 case PIXEL_FORMAT_RGB_565: return kRGB_565_SkColorType;
192 default: return kUnknown_SkColorType;
196 static jlong nativeLockCanvas(JNIEnv* env, jclass clazz,
197 jlong nativeObject, jobject canvasObj, jobject dirtyRectObj) {
198 sp<Surface> surface(reinterpret_cast<Surface *>(nativeObject));
200 if (!isSurfaceValid(surface)) {
206 Rect* dirtyRectPtr = NULL;
209 dirtyRect.left = env->GetIntField(dirtyRectObj, gRectClassInfo.left);
210 dirtyRect.top = env->GetIntField(dirtyRectObj, gRectClassInfo.top);
211 dirtyRect.right = env->GetIntField(dirtyRectObj, gRectClassInfo.right);
212 dirtyRect.bottom = env->GetIntField(dirtyRectObj, gRectClassInfo.bottom);
213 dirtyRectPtr = &dirtyRect;
216 ANativeWindow_Buffer outBuffer;
217 status_t err = surface->lock(&outBuffer, dirtyRectPtr);
219 const char* const exception = (err == NO_MEMORY) ?
220 OutOfResourcesException :
221 "java/lang/IllegalArgumentException";
222 jniThrowException(env, exception, NULL);
226 // Associate a SkCanvas object to this surface
227 env->SetIntField(canvasObj, gCanvasClassInfo.mSurfaceFormat, outBuffer.format);
229 SkImageInfo info = SkImageInfo::Make(outBuffer.width, outBuffer.height,
230 convertPixelFormat(outBuffer.format),
231 kPremul_SkAlphaType);
232 if (outBuffer.format == PIXEL_FORMAT_RGBX_8888) {
233 info.fAlphaType = kOpaque_SkAlphaType;
237 ssize_t bpr = outBuffer.stride * bytesPerPixel(outBuffer.format);
238 bitmap.setInfo(info, bpr);
239 if (outBuffer.width > 0 && outBuffer.height > 0) {
240 bitmap.setPixels(outBuffer.bits);
242 // be safe with an empty bitmap.
243 bitmap.setPixels(NULL);
246 env->CallVoidMethod(canvasObj, gCanvasClassInfo.setNativeBitmap,
247 reinterpret_cast<jlong>(&bitmap));
250 SkCanvas* nativeCanvas = GraphicsJNI::getNativeCanvas(env, canvasObj);
251 nativeCanvas->clipRect( SkRect::Make(reinterpret_cast<const SkIRect&>(dirtyRect)) );
255 env->SetIntField(dirtyRectObj, gRectClassInfo.left, dirtyRect.left);
256 env->SetIntField(dirtyRectObj, gRectClassInfo.top, dirtyRect.top);
257 env->SetIntField(dirtyRectObj, gRectClassInfo.right, dirtyRect.right);
258 env->SetIntField(dirtyRectObj, gRectClassInfo.bottom, dirtyRect.bottom);
261 // Create another reference to the surface and return it. This reference
262 // should be passed to nativeUnlockCanvasAndPost in place of mNativeObject,
263 // because the latter could be replaced while the surface is locked.
264 sp<Surface> lockedSurface(surface);
265 lockedSurface->incStrong(&sRefBaseOwner);
266 return (jlong) lockedSurface.get();
269 static void nativeUnlockCanvasAndPost(JNIEnv* env, jclass clazz,
270 jlong nativeObject, jobject canvasObj) {
271 sp<Surface> surface(reinterpret_cast<Surface *>(nativeObject));
272 if (!isSurfaceValid(surface)) {
276 // detach the canvas from the surface
277 env->CallVoidMethod(canvasObj, gCanvasClassInfo.setNativeBitmap, (jlong)0);
280 status_t err = surface->unlockAndPost();
286 static void nativeAllocateBuffers(JNIEnv* /* env */ , jclass /* clazz */,
287 jlong nativeObject) {
288 sp<Surface> surface(reinterpret_cast<Surface *>(nativeObject));
289 if (!isSurfaceValid(surface)) {
293 surface->allocateBuffers();
296 // ----------------------------------------------------------------------------
298 static jlong nativeCreateFromSurfaceControl(JNIEnv* env, jclass clazz,
299 jlong surfaceControlNativeObj) {
301 * This is used by the WindowManagerService just after constructing
302 * a Surface and is necessary for returning the Surface reference to
303 * the caller. At this point, we should only have a SurfaceControl.
306 sp<SurfaceControl> ctrl(reinterpret_cast<SurfaceControl *>(surfaceControlNativeObj));
307 sp<Surface> surface(ctrl->getSurface());
308 if (surface != NULL) {
309 surface->incStrong(&sRefBaseOwner);
311 return reinterpret_cast<jlong>(surface.get());
314 static jlong nativeReadFromParcel(JNIEnv* env, jclass clazz,
315 jlong nativeObject, jobject parcelObj) {
316 Parcel* parcel = parcelForJavaObject(env, parcelObj);
317 if (parcel == NULL) {
322 sp<Surface> self(reinterpret_cast<Surface *>(nativeObject));
323 sp<IBinder> binder(parcel->readStrongBinder());
325 // update the Surface only if the underlying IGraphicBufferProducer
328 && (self->getIGraphicBufferProducer()->asBinder() == binder)) {
329 // same IGraphicBufferProducer, return ourselves
330 return jlong(self.get());
334 sp<IGraphicBufferProducer> gbp(interface_cast<IGraphicBufferProducer>(binder));
336 // we have a new IGraphicBufferProducer, create a new Surface for it
337 sur = new Surface(gbp, true);
338 // and keep a reference before passing to java
339 sur->incStrong(&sRefBaseOwner);
343 // and loose the java reference to ourselves
344 self->decStrong(&sRefBaseOwner);
347 return jlong(sur.get());
350 static void nativeWriteToParcel(JNIEnv* env, jclass clazz,
351 jlong nativeObject, jobject parcelObj) {
352 Parcel* parcel = parcelForJavaObject(env, parcelObj);
353 if (parcel == NULL) {
357 sp<Surface> self(reinterpret_cast<Surface *>(nativeObject));
358 parcel->writeStrongBinder( self != 0 ? self->getIGraphicBufferProducer()->asBinder() : NULL);
361 static jint nativeGetWidth(JNIEnv* env, jclass clazz, jlong nativeObject) {
362 Surface* surface = reinterpret_cast<Surface*>(nativeObject);
363 ANativeWindow* anw = static_cast<ANativeWindow*>(surface);
365 anw->query(anw, NATIVE_WINDOW_WIDTH, &value);
369 static jint nativeGetHeight(JNIEnv* env, jclass clazz, jlong nativeObject) {
370 Surface* surface = reinterpret_cast<Surface*>(nativeObject);
371 ANativeWindow* anw = static_cast<ANativeWindow*>(surface);
373 anw->query(anw, NATIVE_WINDOW_HEIGHT, &value);
377 namespace uirenderer {
379 using namespace android::uirenderer::renderthread;
381 class ContextFactory : public IContextFactory {
383 virtual AnimationContext* createAnimationContext(renderthread::TimeLord& clock) {
384 return new AnimationContext(clock);
388 static jlong create(JNIEnv* env, jclass clazz, jlong rootNodePtr, jlong surfacePtr) {
389 RenderNode* rootNode = reinterpret_cast<RenderNode*>(rootNodePtr);
390 sp<Surface> surface(reinterpret_cast<Surface*>(surfacePtr));
391 ContextFactory factory;
392 RenderProxy* proxy = new RenderProxy(false, rootNode, &factory);
393 proxy->loadSystemProperties();
394 proxy->setSwapBehavior(kSwap_discardBuffer);
395 proxy->initialize(surface);
396 // Shadows can't be used via this interface, so just set the light source
397 // to all 0s. (and width & height are unused, TODO remove them)
398 proxy->setup(0, 0, (Vector3){0, 0, 0}, 0, 0, 0, 1.0f);
399 return (jlong) proxy;
402 static void setSurface(JNIEnv* env, jclass clazz, jlong rendererPtr, jlong surfacePtr) {
403 RenderProxy* proxy = reinterpret_cast<RenderProxy*>(rendererPtr);
404 sp<Surface> surface(reinterpret_cast<Surface*>(surfacePtr));
405 proxy->updateSurface(surface);
408 static void draw(JNIEnv* env, jclass clazz, jlong rendererPtr) {
409 RenderProxy* proxy = reinterpret_cast<RenderProxy*>(rendererPtr);
410 nsecs_t vsync = systemTime(CLOCK_MONOTONIC);
411 UiFrameInfoBuilder(proxy->frameInfo())
412 .setVsync(vsync, vsync)
413 .addFlag(FrameInfoFlags::kSurfaceCanvas);
414 proxy->syncAndDrawFrame();
417 static void destroy(JNIEnv* env, jclass clazz, jlong rendererPtr) {
418 RenderProxy* proxy = reinterpret_cast<RenderProxy*>(rendererPtr);
424 // ----------------------------------------------------------------------------
426 namespace hwui = android::uirenderer;
428 static JNINativeMethod gSurfaceMethods[] = {
429 {"nativeCreateFromSurfaceTexture", "(Landroid/graphics/SurfaceTexture;)J",
430 (void*)nativeCreateFromSurfaceTexture },
431 {"nativeRelease", "(J)V",
432 (void*)nativeRelease },
433 {"nativeIsValid", "(J)Z",
434 (void*)nativeIsValid },
435 {"nativeIsConsumerRunningBehind", "(J)Z",
436 (void*)nativeIsConsumerRunningBehind },
437 {"nativeLockCanvas", "(JLandroid/graphics/Canvas;Landroid/graphics/Rect;)J",
438 (void*)nativeLockCanvas },
439 {"nativeUnlockCanvasAndPost", "(JLandroid/graphics/Canvas;)V",
440 (void*)nativeUnlockCanvasAndPost },
441 {"nativeAllocateBuffers", "(J)V",
442 (void*)nativeAllocateBuffers },
443 {"nativeCreateFromSurfaceControl", "(J)J",
444 (void*)nativeCreateFromSurfaceControl },
445 {"nativeReadFromParcel", "(JLandroid/os/Parcel;)J",
446 (void*)nativeReadFromParcel },
447 {"nativeWriteToParcel", "(JLandroid/os/Parcel;)V",
448 (void*)nativeWriteToParcel },
449 {"nativeGetWidth", "(J)I", (void*)nativeGetWidth },
450 {"nativeGetHeight", "(J)I", (void*)nativeGetHeight },
453 {"nHwuiCreate", "(JJ)J", (void*) hwui::create },
454 {"nHwuiSetSurface", "(JJ)V", (void*) hwui::setSurface },
455 {"nHwuiDraw", "(J)V", (void*) hwui::draw },
456 {"nHwuiDestroy", "(J)V", (void*) hwui::destroy },
459 int register_android_view_Surface(JNIEnv* env)
461 int err = AndroidRuntime::registerNativeMethods(env, "android/view/Surface",
462 gSurfaceMethods, NELEM(gSurfaceMethods));
464 jclass clazz = env->FindClass("android/view/Surface");
465 gSurfaceClassInfo.clazz = jclass(env->NewGlobalRef(clazz));
466 gSurfaceClassInfo.mNativeObject =
467 env->GetFieldID(gSurfaceClassInfo.clazz, "mNativeObject", "J");
468 gSurfaceClassInfo.mLock =
469 env->GetFieldID(gSurfaceClassInfo.clazz, "mLock", "Ljava/lang/Object;");
470 gSurfaceClassInfo.ctor = env->GetMethodID(gSurfaceClassInfo.clazz, "<init>", "(J)V");
472 clazz = env->FindClass("android/graphics/Canvas");
473 gCanvasClassInfo.mSurfaceFormat = env->GetFieldID(clazz, "mSurfaceFormat", "I");
474 gCanvasClassInfo.setNativeBitmap = env->GetMethodID(clazz, "setNativeBitmap", "(J)V");
476 clazz = env->FindClass("android/graphics/Rect");
477 gRectClassInfo.left = env->GetFieldID(clazz, "left", "I");
478 gRectClassInfo.top = env->GetFieldID(clazz, "top", "I");
479 gRectClassInfo.right = env->GetFieldID(clazz, "right", "I");
480 gRectClassInfo.bottom = env->GetFieldID(clazz, "bottom", "I");