• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2007 The Android Open Source Project
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 
17 #define LOG_TAG "Surface"
18 
19 #include <stdio.h>
20 
21 #include "jni.h"
22 #include "JNIHelp.h"
23 #include "android_os_Parcel.h"
24 #include "android/graphics/GraphicsJNI.h"
25 
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>
30 
31 #include <binder/Parcel.h>
32 
33 #include <gui/Surface.h>
34 #include <gui/SurfaceControl.h>
35 #include <gui/GLConsumer.h>
36 
37 #include <ui/Rect.h>
38 #include <ui/Region.h>
39 
40 #include <SkCanvas.h>
41 #include <SkBitmap.h>
42 #include <SkImage.h>
43 #include <SkRegion.h>
44 
45 #include <utils/misc.h>
46 #include <utils/Log.h>
47 
48 #include <ScopedUtfChars.h>
49 
50 #include <AnimationContext.h>
51 #include <DisplayListRenderer.h>
52 #include <RenderNode.h>
53 #include <renderthread/RenderProxy.h>
54 
55 // ----------------------------------------------------------------------------
56 
57 namespace android {
58 
59 static const char* const OutOfResourcesException =
60     "android/view/Surface$OutOfResourcesException";
61 
62 static struct {
63     jclass clazz;
64     jfieldID mNativeObject;
65     jfieldID mLock;
66     jmethodID ctor;
67 } gSurfaceClassInfo;
68 
69 static struct {
70     jfieldID left;
71     jfieldID top;
72     jfieldID right;
73     jfieldID bottom;
74 } gRectClassInfo;
75 
76 static struct {
77     jfieldID mSurfaceFormat;
78     jmethodID setNativeBitmap;
79 } gCanvasClassInfo;
80 
81 // ----------------------------------------------------------------------------
82 
83 // this is just a pointer we use to pass to inc/decStrong
84 static const void *sRefBaseOwner;
85 
android_view_Surface_isInstanceOf(JNIEnv * env,jobject obj)86 bool android_view_Surface_isInstanceOf(JNIEnv* env, jobject obj) {
87     return env->IsInstanceOf(obj, gSurfaceClassInfo.clazz);
88 }
89 
android_view_Surface_getNativeWindow(JNIEnv * env,jobject surfaceObj)90 sp<ANativeWindow> android_view_Surface_getNativeWindow(JNIEnv* env, jobject surfaceObj) {
91     return android_view_Surface_getSurface(env, surfaceObj);
92 }
93 
android_view_Surface_getSurface(JNIEnv * env,jobject surfaceObj)94 sp<Surface> android_view_Surface_getSurface(JNIEnv* env, jobject surfaceObj) {
95     sp<Surface> sur;
96     jobject lock = env->GetObjectField(surfaceObj,
97             gSurfaceClassInfo.mLock);
98     if (env->MonitorEnter(lock) == JNI_OK) {
99         sur = reinterpret_cast<Surface *>(
100                 env->GetLongField(surfaceObj, gSurfaceClassInfo.mNativeObject));
101         env->MonitorExit(lock);
102     }
103     env->DeleteLocalRef(lock);
104     return sur;
105 }
106 
android_view_Surface_createFromIGraphicBufferProducer(JNIEnv * env,const sp<IGraphicBufferProducer> & bufferProducer)107 jobject android_view_Surface_createFromIGraphicBufferProducer(JNIEnv* env,
108         const sp<IGraphicBufferProducer>& bufferProducer) {
109     if (bufferProducer == NULL) {
110         return NULL;
111     }
112 
113     sp<Surface> surface(new Surface(bufferProducer, true));
114     if (surface == NULL) {
115         return NULL;
116     }
117 
118     jobject surfaceObj = env->NewObject(gSurfaceClassInfo.clazz,
119             gSurfaceClassInfo.ctor, (jlong)surface.get());
120     if (surfaceObj == NULL) {
121         if (env->ExceptionCheck()) {
122             ALOGE("Could not create instance of Surface from IGraphicBufferProducer.");
123             LOGE_EX(env);
124             env->ExceptionClear();
125         }
126         return NULL;
127     }
128     surface->incStrong(&sRefBaseOwner);
129     return surfaceObj;
130 }
131 
132 // ----------------------------------------------------------------------------
133 
isSurfaceValid(const sp<Surface> & sur)134 static inline bool isSurfaceValid(const sp<Surface>& sur) {
135     return Surface::isValid(sur);
136 }
137 
138 // ----------------------------------------------------------------------------
139 
nativeCreateFromSurfaceTexture(JNIEnv * env,jclass clazz,jobject surfaceTextureObj)140 static jlong nativeCreateFromSurfaceTexture(JNIEnv* env, jclass clazz,
141         jobject surfaceTextureObj) {
142     sp<IGraphicBufferProducer> producer(SurfaceTexture_getProducer(env, surfaceTextureObj));
143     if (producer == NULL) {
144         jniThrowException(env, "java/lang/IllegalArgumentException",
145                 "SurfaceTexture has already been released");
146         return 0;
147     }
148 
149     sp<Surface> surface(new Surface(producer, true));
150     if (surface == NULL) {
151         jniThrowException(env, OutOfResourcesException, NULL);
152         return 0;
153     }
154 
155     surface->incStrong(&sRefBaseOwner);
156     return jlong(surface.get());
157 }
158 
nativeRelease(JNIEnv * env,jclass clazz,jlong nativeObject)159 static void nativeRelease(JNIEnv* env, jclass clazz, jlong nativeObject) {
160     sp<Surface> sur(reinterpret_cast<Surface *>(nativeObject));
161     sur->decStrong(&sRefBaseOwner);
162 }
163 
nativeIsValid(JNIEnv * env,jclass clazz,jlong nativeObject)164 static jboolean nativeIsValid(JNIEnv* env, jclass clazz, jlong nativeObject) {
165     sp<Surface> sur(reinterpret_cast<Surface *>(nativeObject));
166     return isSurfaceValid(sur) ? JNI_TRUE : JNI_FALSE;
167 }
168 
nativeIsConsumerRunningBehind(JNIEnv * env,jclass clazz,jlong nativeObject)169 static jboolean nativeIsConsumerRunningBehind(JNIEnv* env, jclass clazz, jlong nativeObject) {
170     sp<Surface> sur(reinterpret_cast<Surface *>(nativeObject));
171     if (!isSurfaceValid(sur)) {
172         doThrowIAE(env);
173         return JNI_FALSE;
174     }
175     int value = 0;
176     ANativeWindow* anw = static_cast<ANativeWindow*>(sur.get());
177     anw->query(anw, NATIVE_WINDOW_CONSUMER_RUNNING_BEHIND, &value);
178     return value;
179 }
180 
convertPixelFormat(PixelFormat format)181 static inline SkColorType convertPixelFormat(PixelFormat format) {
182     /* note: if PIXEL_FORMAT_RGBX_8888 means that all alpha bytes are 0xFF, then
183         we can map to kN32_SkColorType, and optionally call
184         bitmap.setAlphaType(kOpaque_SkAlphaType) on the resulting SkBitmap
185         (as an accelerator)
186     */
187     switch (format) {
188     case PIXEL_FORMAT_RGBX_8888:    return kN32_SkColorType;
189     case PIXEL_FORMAT_RGBA_8888:    return kN32_SkColorType;
190     case PIXEL_FORMAT_RGB_565:      return kRGB_565_SkColorType;
191     default:                        return kUnknown_SkColorType;
192     }
193 }
194 
nativeLockCanvas(JNIEnv * env,jclass clazz,jlong nativeObject,jobject canvasObj,jobject dirtyRectObj)195 static jlong nativeLockCanvas(JNIEnv* env, jclass clazz,
196         jlong nativeObject, jobject canvasObj, jobject dirtyRectObj) {
197     sp<Surface> surface(reinterpret_cast<Surface *>(nativeObject));
198 
199     if (!isSurfaceValid(surface)) {
200         doThrowIAE(env);
201         return 0;
202     }
203 
204     Rect dirtyRect;
205     Rect* dirtyRectPtr = NULL;
206 
207     if (dirtyRectObj) {
208         dirtyRect.left   = env->GetIntField(dirtyRectObj, gRectClassInfo.left);
209         dirtyRect.top    = env->GetIntField(dirtyRectObj, gRectClassInfo.top);
210         dirtyRect.right  = env->GetIntField(dirtyRectObj, gRectClassInfo.right);
211         dirtyRect.bottom = env->GetIntField(dirtyRectObj, gRectClassInfo.bottom);
212         dirtyRectPtr = &dirtyRect;
213     }
214 
215     ANativeWindow_Buffer outBuffer;
216     status_t err = surface->lock(&outBuffer, dirtyRectPtr);
217     if (err < 0) {
218         const char* const exception = (err == NO_MEMORY) ?
219                 OutOfResourcesException :
220                 "java/lang/IllegalArgumentException";
221         jniThrowException(env, exception, NULL);
222         return 0;
223     }
224 
225     // Associate a SkCanvas object to this surface
226     env->SetIntField(canvasObj, gCanvasClassInfo.mSurfaceFormat, outBuffer.format);
227 
228     SkImageInfo info = SkImageInfo::Make(outBuffer.width, outBuffer.height,
229                                          convertPixelFormat(outBuffer.format),
230                                          kPremul_SkAlphaType);
231     if (outBuffer.format == PIXEL_FORMAT_RGBX_8888) {
232         info.fAlphaType = kOpaque_SkAlphaType;
233     }
234 
235     SkBitmap bitmap;
236     ssize_t bpr = outBuffer.stride * bytesPerPixel(outBuffer.format);
237     bitmap.setInfo(info, bpr);
238     if (outBuffer.width > 0 && outBuffer.height > 0) {
239         bitmap.setPixels(outBuffer.bits);
240     } else {
241         // be safe with an empty bitmap.
242         bitmap.setPixels(NULL);
243     }
244 
245     env->CallVoidMethod(canvasObj, gCanvasClassInfo.setNativeBitmap,
246                         reinterpret_cast<jlong>(&bitmap));
247 
248     if (dirtyRectPtr) {
249         SkCanvas* nativeCanvas = GraphicsJNI::getNativeCanvas(env, canvasObj);
250         nativeCanvas->clipRect( SkRect::Make(reinterpret_cast<const SkIRect&>(dirtyRect)) );
251     }
252 
253     if (dirtyRectObj) {
254         env->SetIntField(dirtyRectObj, gRectClassInfo.left,   dirtyRect.left);
255         env->SetIntField(dirtyRectObj, gRectClassInfo.top,    dirtyRect.top);
256         env->SetIntField(dirtyRectObj, gRectClassInfo.right,  dirtyRect.right);
257         env->SetIntField(dirtyRectObj, gRectClassInfo.bottom, dirtyRect.bottom);
258     }
259 
260     // Create another reference to the surface and return it.  This reference
261     // should be passed to nativeUnlockCanvasAndPost in place of mNativeObject,
262     // because the latter could be replaced while the surface is locked.
263     sp<Surface> lockedSurface(surface);
264     lockedSurface->incStrong(&sRefBaseOwner);
265     return (jlong) lockedSurface.get();
266 }
267 
nativeUnlockCanvasAndPost(JNIEnv * env,jclass clazz,jlong nativeObject,jobject canvasObj)268 static void nativeUnlockCanvasAndPost(JNIEnv* env, jclass clazz,
269         jlong nativeObject, jobject canvasObj) {
270     sp<Surface> surface(reinterpret_cast<Surface *>(nativeObject));
271     if (!isSurfaceValid(surface)) {
272         return;
273     }
274 
275     // detach the canvas from the surface
276     env->CallVoidMethod(canvasObj, gCanvasClassInfo.setNativeBitmap, (jlong)0);
277 
278     // unlock surface
279     status_t err = surface->unlockAndPost();
280     if (err < 0) {
281         doThrowIAE(env);
282     }
283 }
284 
nativeAllocateBuffers(JNIEnv *,jclass,jlong nativeObject)285 static void nativeAllocateBuffers(JNIEnv* /* env */ , jclass /* clazz */,
286         jlong nativeObject) {
287     sp<Surface> surface(reinterpret_cast<Surface *>(nativeObject));
288     if (!isSurfaceValid(surface)) {
289         return;
290     }
291 
292     surface->allocateBuffers();
293 }
294 
295 // ----------------------------------------------------------------------------
296 
nativeCreateFromSurfaceControl(JNIEnv * env,jclass clazz,jlong surfaceControlNativeObj)297 static jlong nativeCreateFromSurfaceControl(JNIEnv* env, jclass clazz,
298         jlong surfaceControlNativeObj) {
299     /*
300      * This is used by the WindowManagerService just after constructing
301      * a Surface and is necessary for returning the Surface reference to
302      * the caller. At this point, we should only have a SurfaceControl.
303      */
304 
305     sp<SurfaceControl> ctrl(reinterpret_cast<SurfaceControl *>(surfaceControlNativeObj));
306     sp<Surface> surface(ctrl->getSurface());
307     if (surface != NULL) {
308         surface->incStrong(&sRefBaseOwner);
309     }
310     return reinterpret_cast<jlong>(surface.get());
311 }
312 
nativeReadFromParcel(JNIEnv * env,jclass clazz,jlong nativeObject,jobject parcelObj)313 static jlong nativeReadFromParcel(JNIEnv* env, jclass clazz,
314         jlong nativeObject, jobject parcelObj) {
315     Parcel* parcel = parcelForJavaObject(env, parcelObj);
316     if (parcel == NULL) {
317         doThrowNPE(env);
318         return 0;
319     }
320 
321     sp<Surface> self(reinterpret_cast<Surface *>(nativeObject));
322     sp<IBinder> binder(parcel->readStrongBinder());
323 
324     // update the Surface only if the underlying IGraphicBufferProducer
325     // has changed.
326     if (self != NULL
327             && (self->getIGraphicBufferProducer()->asBinder() == binder)) {
328         // same IGraphicBufferProducer, return ourselves
329         return jlong(self.get());
330     }
331 
332     sp<Surface> sur;
333     sp<IGraphicBufferProducer> gbp(interface_cast<IGraphicBufferProducer>(binder));
334     if (gbp != NULL) {
335         // we have a new IGraphicBufferProducer, create a new Surface for it
336         sur = new Surface(gbp, true);
337         // and keep a reference before passing to java
338         sur->incStrong(&sRefBaseOwner);
339     }
340 
341     if (self != NULL) {
342         // and loose the java reference to ourselves
343         self->decStrong(&sRefBaseOwner);
344     }
345 
346     return jlong(sur.get());
347 }
348 
nativeWriteToParcel(JNIEnv * env,jclass clazz,jlong nativeObject,jobject parcelObj)349 static void nativeWriteToParcel(JNIEnv* env, jclass clazz,
350         jlong nativeObject, jobject parcelObj) {
351     Parcel* parcel = parcelForJavaObject(env, parcelObj);
352     if (parcel == NULL) {
353         doThrowNPE(env);
354         return;
355     }
356     sp<Surface> self(reinterpret_cast<Surface *>(nativeObject));
357     parcel->writeStrongBinder( self != 0 ? self->getIGraphicBufferProducer()->asBinder() : NULL);
358 }
359 
nativeGetWidth(JNIEnv * env,jclass clazz,jlong nativeObject)360 static jint nativeGetWidth(JNIEnv* env, jclass clazz, jlong nativeObject) {
361     Surface* surface = reinterpret_cast<Surface*>(nativeObject);
362     ANativeWindow* anw = static_cast<ANativeWindow*>(surface);
363     int value = 0;
364     anw->query(anw, NATIVE_WINDOW_WIDTH, &value);
365     return value;
366 }
367 
nativeGetHeight(JNIEnv * env,jclass clazz,jlong nativeObject)368 static jint nativeGetHeight(JNIEnv* env, jclass clazz, jlong nativeObject) {
369     Surface* surface = reinterpret_cast<Surface*>(nativeObject);
370     ANativeWindow* anw = static_cast<ANativeWindow*>(surface);
371     int value = 0;
372     anw->query(anw, NATIVE_WINDOW_HEIGHT, &value);
373     return value;
374 }
375 
376 namespace uirenderer {
377 
378 using namespace android::uirenderer::renderthread;
379 
380 class ContextFactory : public IContextFactory {
381 public:
createAnimationContext(renderthread::TimeLord & clock)382     virtual AnimationContext* createAnimationContext(renderthread::TimeLord& clock) {
383         return new AnimationContext(clock);
384     }
385 };
386 
create(JNIEnv * env,jclass clazz,jlong rootNodePtr,jlong surfacePtr)387 static jlong create(JNIEnv* env, jclass clazz, jlong rootNodePtr, jlong surfacePtr) {
388     RenderNode* rootNode = reinterpret_cast<RenderNode*>(rootNodePtr);
389     sp<Surface> surface(reinterpret_cast<Surface*>(surfacePtr));
390     ContextFactory factory;
391     RenderProxy* proxy = new RenderProxy(false, rootNode, &factory);
392     proxy->loadSystemProperties();
393     proxy->setSwapBehavior(kSwap_discardBuffer);
394     proxy->initialize(surface);
395     // Shadows can't be used via this interface, so just set the light source
396     // to all 0s. (and width & height are unused, TODO remove them)
397     proxy->setup(0, 0, (Vector3){0, 0, 0}, 0, 0, 0);
398     return (jlong) proxy;
399 }
400 
setSurface(JNIEnv * env,jclass clazz,jlong rendererPtr,jlong surfacePtr)401 static void setSurface(JNIEnv* env, jclass clazz, jlong rendererPtr, jlong surfacePtr) {
402     RenderProxy* proxy = reinterpret_cast<RenderProxy*>(rendererPtr);
403     sp<Surface> surface(reinterpret_cast<Surface*>(surfacePtr));
404     proxy->updateSurface(surface);
405 }
406 
draw(JNIEnv * env,jclass clazz,jlong rendererPtr)407 static void draw(JNIEnv* env, jclass clazz, jlong rendererPtr) {
408     RenderProxy* proxy = reinterpret_cast<RenderProxy*>(rendererPtr);
409     nsecs_t frameTimeNs = systemTime(CLOCK_MONOTONIC);
410     proxy->syncAndDrawFrame(frameTimeNs, 0, 1.0f);
411 }
412 
destroy(JNIEnv * env,jclass clazz,jlong rendererPtr)413 static void destroy(JNIEnv* env, jclass clazz, jlong rendererPtr) {
414     RenderProxy* proxy = reinterpret_cast<RenderProxy*>(rendererPtr);
415     delete proxy;
416 }
417 
418 } // uirenderer
419 
420 // ----------------------------------------------------------------------------
421 
422 namespace hwui = android::uirenderer;
423 
424 static JNINativeMethod gSurfaceMethods[] = {
425     {"nativeCreateFromSurfaceTexture", "(Landroid/graphics/SurfaceTexture;)J",
426             (void*)nativeCreateFromSurfaceTexture },
427     {"nativeRelease", "(J)V",
428             (void*)nativeRelease },
429     {"nativeIsValid", "(J)Z",
430             (void*)nativeIsValid },
431     {"nativeIsConsumerRunningBehind", "(J)Z",
432             (void*)nativeIsConsumerRunningBehind },
433     {"nativeLockCanvas", "(JLandroid/graphics/Canvas;Landroid/graphics/Rect;)J",
434             (void*)nativeLockCanvas },
435     {"nativeUnlockCanvasAndPost", "(JLandroid/graphics/Canvas;)V",
436             (void*)nativeUnlockCanvasAndPost },
437     {"nativeAllocateBuffers", "(J)V",
438             (void*)nativeAllocateBuffers },
439     {"nativeCreateFromSurfaceControl", "(J)J",
440             (void*)nativeCreateFromSurfaceControl },
441     {"nativeReadFromParcel", "(JLandroid/os/Parcel;)J",
442             (void*)nativeReadFromParcel },
443     {"nativeWriteToParcel", "(JLandroid/os/Parcel;)V",
444             (void*)nativeWriteToParcel },
445     {"nativeGetWidth", "(J)I", (void*)nativeGetWidth },
446     {"nativeGetHeight", "(J)I", (void*)nativeGetHeight },
447 
448     // HWUI context
449     {"nHwuiCreate", "(JJ)J", (void*) hwui::create },
450     {"nHwuiSetSurface", "(JJ)V", (void*) hwui::setSurface },
451     {"nHwuiDraw", "(J)V", (void*) hwui::draw },
452     {"nHwuiDestroy", "(J)V", (void*) hwui::destroy },
453 };
454 
register_android_view_Surface(JNIEnv * env)455 int register_android_view_Surface(JNIEnv* env)
456 {
457     int err = AndroidRuntime::registerNativeMethods(env, "android/view/Surface",
458             gSurfaceMethods, NELEM(gSurfaceMethods));
459 
460     jclass clazz = env->FindClass("android/view/Surface");
461     gSurfaceClassInfo.clazz = jclass(env->NewGlobalRef(clazz));
462     gSurfaceClassInfo.mNativeObject =
463             env->GetFieldID(gSurfaceClassInfo.clazz, "mNativeObject", "J");
464     gSurfaceClassInfo.mLock =
465             env->GetFieldID(gSurfaceClassInfo.clazz, "mLock", "Ljava/lang/Object;");
466     gSurfaceClassInfo.ctor = env->GetMethodID(gSurfaceClassInfo.clazz, "<init>", "(J)V");
467 
468     clazz = env->FindClass("android/graphics/Canvas");
469     gCanvasClassInfo.mSurfaceFormat = env->GetFieldID(clazz, "mSurfaceFormat", "I");
470     gCanvasClassInfo.setNativeBitmap = env->GetMethodID(clazz, "setNativeBitmap", "(J)V");
471 
472     clazz = env->FindClass("android/graphics/Rect");
473     gRectClassInfo.left = env->GetFieldID(clazz, "left", "I");
474     gRectClassInfo.top = env->GetFieldID(clazz, "top", "I");
475     gRectClassInfo.right = env->GetFieldID(clazz, "right", "I");
476     gRectClassInfo.bottom = env->GetFieldID(clazz, "bottom", "I");
477 
478     return err;
479 }
480 
481 };
482