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 <nativehelper/JNIHelp.h>
23 #include "android_os_Parcel.h"
24 #include "android/graphics/GraphicBuffer.h"
25 #include "android/graphics/GraphicsJNI.h"
26 
27 #include "core_jni_helpers.h"
28 #include <android_runtime/android_view_Surface.h>
29 #include <android_runtime/android_graphics_SurfaceTexture.h>
30 #include <android_runtime/Log.h>
31 
32 #include <binder/Parcel.h>
33 
34 #include <gui/Surface.h>
35 #include <gui/view/Surface.h>
36 #include <gui/SurfaceControl.h>
37 #include <gui/GLConsumer.h>
38 
39 #include <ui/Rect.h>
40 #include <ui/Region.h>
41 
42 #include <SkCanvas.h>
43 #include <SkBitmap.h>
44 #include <SkImage.h>
45 #include <SkRegion.h>
46 
47 #include <utils/misc.h>
48 #include <utils/Log.h>
49 
50 #include <nativehelper/ScopedUtfChars.h>
51 
52 #include <AnimationContext.h>
53 #include <FrameInfo.h>
54 #include <RenderNode.h>
55 #include <renderthread/RenderProxy.h>
56 
57 // ----------------------------------------------------------------------------
58 
59 namespace android {
60 
61 static const char* const OutOfResourcesException =
62     "android/view/Surface$OutOfResourcesException";
63 
64 static struct {
65     jclass clazz;
66     jfieldID mNativeObject;
67     jfieldID mLock;
68     jmethodID ctor;
69 } gSurfaceClassInfo;
70 
71 static struct {
72     jfieldID left;
73     jfieldID top;
74     jfieldID right;
75     jfieldID bottom;
76 } gRectClassInfo;
77 
78 class JNamedColorSpace {
79 public:
80     // ColorSpace.Named.SRGB.ordinal() = 0;
81     static constexpr jint SRGB = 0;
82 
83     // ColorSpace.Named.DISPLAY_P3.ordinal() = 7;
84     static constexpr jint DISPLAY_P3 = 7;
85 };
86 
fromNamedColorSpaceValueToDataspace(const jint colorSpace)87 constexpr ui::Dataspace fromNamedColorSpaceValueToDataspace(const jint colorSpace) {
88     switch (colorSpace) {
89         case JNamedColorSpace::DISPLAY_P3:
90             return ui::Dataspace::DISPLAY_P3;
91         default:
92             return ui::Dataspace::V0_SRGB;
93     }
94 }
95 
96 // ----------------------------------------------------------------------------
97 
98 // this is just a pointer we use to pass to inc/decStrong
99 static const void *sRefBaseOwner;
100 
android_view_Surface_isInstanceOf(JNIEnv * env,jobject obj)101 bool android_view_Surface_isInstanceOf(JNIEnv* env, jobject obj) {
102     return env->IsInstanceOf(obj, gSurfaceClassInfo.clazz);
103 }
104 
android_view_Surface_getNativeWindow(JNIEnv * env,jobject surfaceObj)105 sp<ANativeWindow> android_view_Surface_getNativeWindow(JNIEnv* env, jobject surfaceObj) {
106     return android_view_Surface_getSurface(env, surfaceObj);
107 }
108 
android_view_Surface_getSurface(JNIEnv * env,jobject surfaceObj)109 sp<Surface> android_view_Surface_getSurface(JNIEnv* env, jobject surfaceObj) {
110     sp<Surface> sur;
111     jobject lock = env->GetObjectField(surfaceObj,
112             gSurfaceClassInfo.mLock);
113     if (env->MonitorEnter(lock) == JNI_OK) {
114         sur = reinterpret_cast<Surface *>(
115                 env->GetLongField(surfaceObj, gSurfaceClassInfo.mNativeObject));
116         env->MonitorExit(lock);
117     }
118     env->DeleteLocalRef(lock);
119     return sur;
120 }
121 
android_view_Surface_createFromSurface(JNIEnv * env,const sp<Surface> & surface)122 jobject android_view_Surface_createFromSurface(JNIEnv* env, const sp<Surface>& surface) {
123     jobject surfaceObj = env->NewObject(gSurfaceClassInfo.clazz,
124             gSurfaceClassInfo.ctor, (jlong)surface.get());
125     if (surfaceObj == NULL) {
126         if (env->ExceptionCheck()) {
127             ALOGE("Could not create instance of Surface from IGraphicBufferProducer.");
128             LOGE_EX(env);
129             env->ExceptionClear();
130         }
131         return NULL;
132     }
133     surface->incStrong(&sRefBaseOwner);
134     return surfaceObj;
135 }
136 
android_view_Surface_createFromIGraphicBufferProducer(JNIEnv * env,const sp<IGraphicBufferProducer> & bufferProducer)137 jobject android_view_Surface_createFromIGraphicBufferProducer(JNIEnv* env,
138         const sp<IGraphicBufferProducer>& bufferProducer) {
139     if (bufferProducer == NULL) {
140         return NULL;
141     }
142 
143     sp<Surface> surface(new Surface(bufferProducer, true));
144     return android_view_Surface_createFromSurface(env, surface);
145 }
146 
android_view_Surface_mapPublicFormatToHalFormat(PublicFormat f)147 int android_view_Surface_mapPublicFormatToHalFormat(PublicFormat f) {
148     return mapPublicFormatToHalFormat(f);
149 }
150 
android_view_Surface_mapPublicFormatToHalDataspace(PublicFormat f)151 android_dataspace android_view_Surface_mapPublicFormatToHalDataspace(
152         PublicFormat f) {
153     return mapPublicFormatToHalDataspace(f);
154 }
155 
android_view_Surface_mapHalFormatDataspaceToPublicFormat(int format,android_dataspace dataSpace)156 PublicFormat android_view_Surface_mapHalFormatDataspaceToPublicFormat(
157         int format, android_dataspace dataSpace) {
158     return mapHalFormatDataspaceToPublicFormat(format, dataSpace);
159 }
160 // ----------------------------------------------------------------------------
161 
isSurfaceValid(const sp<Surface> & sur)162 static inline bool isSurfaceValid(const sp<Surface>& sur) {
163     return Surface::isValid(sur);
164 }
165 
166 // ----------------------------------------------------------------------------
167 
nativeCreateFromSurfaceTexture(JNIEnv * env,jclass clazz,jobject surfaceTextureObj)168 static jlong nativeCreateFromSurfaceTexture(JNIEnv* env, jclass clazz,
169         jobject surfaceTextureObj) {
170     sp<IGraphicBufferProducer> producer(SurfaceTexture_getProducer(env, surfaceTextureObj));
171     if (producer == NULL) {
172         jniThrowException(env, "java/lang/IllegalArgumentException",
173                 "SurfaceTexture has already been released");
174         return 0;
175     }
176 
177     sp<Surface> surface(new Surface(producer, true));
178     if (surface == NULL) {
179         jniThrowException(env, OutOfResourcesException, NULL);
180         return 0;
181     }
182 
183     surface->incStrong(&sRefBaseOwner);
184     return jlong(surface.get());
185 }
186 
nativeRelease(JNIEnv * env,jclass clazz,jlong nativeObject)187 static void nativeRelease(JNIEnv* env, jclass clazz, jlong nativeObject) {
188     sp<Surface> sur(reinterpret_cast<Surface *>(nativeObject));
189     sur->decStrong(&sRefBaseOwner);
190 }
191 
nativeIsValid(JNIEnv * env,jclass clazz,jlong nativeObject)192 static jboolean nativeIsValid(JNIEnv* env, jclass clazz, jlong nativeObject) {
193     sp<Surface> sur(reinterpret_cast<Surface *>(nativeObject));
194     return isSurfaceValid(sur) ? JNI_TRUE : JNI_FALSE;
195 }
196 
nativeIsConsumerRunningBehind(JNIEnv * env,jclass clazz,jlong nativeObject)197 static jboolean nativeIsConsumerRunningBehind(JNIEnv* env, jclass clazz, jlong nativeObject) {
198     sp<Surface> sur(reinterpret_cast<Surface *>(nativeObject));
199     if (!isSurfaceValid(sur)) {
200         doThrowIAE(env);
201         return JNI_FALSE;
202     }
203     int value = 0;
204     ANativeWindow* anw = static_cast<ANativeWindow*>(sur.get());
205     anw->query(anw, NATIVE_WINDOW_CONSUMER_RUNNING_BEHIND, &value);
206     return value;
207 }
208 
convertPixelFormat(PixelFormat format)209 static inline SkColorType convertPixelFormat(PixelFormat format) {
210     /* note: if PIXEL_FORMAT_RGBX_8888 means that all alpha bytes are 0xFF, then
211         we can map to kN32_SkColorType, and optionally call
212         bitmap.setAlphaType(kOpaque_SkAlphaType) on the resulting SkBitmap
213         (as an accelerator)
214     */
215     switch (format) {
216     case PIXEL_FORMAT_RGBX_8888:    return kN32_SkColorType;
217     case PIXEL_FORMAT_RGBA_8888:    return kN32_SkColorType;
218     case PIXEL_FORMAT_RGBA_FP16:    return kRGBA_F16_SkColorType;
219     case PIXEL_FORMAT_RGB_565:      return kRGB_565_SkColorType;
220     default:                        return kUnknown_SkColorType;
221     }
222 }
223 
nativeLockCanvas(JNIEnv * env,jclass clazz,jlong nativeObject,jobject canvasObj,jobject dirtyRectObj)224 static jlong nativeLockCanvas(JNIEnv* env, jclass clazz,
225         jlong nativeObject, jobject canvasObj, jobject dirtyRectObj) {
226     sp<Surface> surface(reinterpret_cast<Surface *>(nativeObject));
227 
228     if (!isSurfaceValid(surface)) {
229         doThrowIAE(env);
230         return 0;
231     }
232 
233     if (convertPixelFormat(ANativeWindow_getFormat(surface.get())) == kUnknown_SkColorType) {
234         native_window_set_buffers_format(surface.get(), PIXEL_FORMAT_RGBA_8888);
235     }
236 
237     Rect dirtyRect(Rect::EMPTY_RECT);
238     Rect* dirtyRectPtr = NULL;
239 
240     if (dirtyRectObj) {
241         dirtyRect.left   = env->GetIntField(dirtyRectObj, gRectClassInfo.left);
242         dirtyRect.top    = env->GetIntField(dirtyRectObj, gRectClassInfo.top);
243         dirtyRect.right  = env->GetIntField(dirtyRectObj, gRectClassInfo.right);
244         dirtyRect.bottom = env->GetIntField(dirtyRectObj, gRectClassInfo.bottom);
245         dirtyRectPtr = &dirtyRect;
246     }
247 
248     ANativeWindow_Buffer outBuffer;
249     status_t err = surface->lock(&outBuffer, dirtyRectPtr);
250     if (err < 0) {
251         const char* const exception = (err == NO_MEMORY) ?
252                 OutOfResourcesException :
253                 "java/lang/IllegalArgumentException";
254         jniThrowException(env, exception, NULL);
255         return 0;
256     }
257 
258     SkImageInfo info = SkImageInfo::Make(outBuffer.width, outBuffer.height,
259                                          convertPixelFormat(outBuffer.format),
260                                          outBuffer.format == PIXEL_FORMAT_RGBX_8888
261                                                  ? kOpaque_SkAlphaType : kPremul_SkAlphaType);
262 
263     SkBitmap bitmap;
264     ssize_t bpr = outBuffer.stride * bytesPerPixel(outBuffer.format);
265     bitmap.setInfo(info, bpr);
266     if (outBuffer.width > 0 && outBuffer.height > 0) {
267         bitmap.setPixels(outBuffer.bits);
268     } else {
269         // be safe with an empty bitmap.
270         bitmap.setPixels(NULL);
271     }
272 
273     Canvas* nativeCanvas = GraphicsJNI::getNativeCanvas(env, canvasObj);
274     nativeCanvas->setBitmap(bitmap);
275 
276     if (dirtyRectPtr) {
277         nativeCanvas->clipRect(dirtyRect.left, dirtyRect.top,
278                 dirtyRect.right, dirtyRect.bottom, SkClipOp::kIntersect);
279     }
280 
281     if (dirtyRectObj) {
282         env->SetIntField(dirtyRectObj, gRectClassInfo.left,   dirtyRect.left);
283         env->SetIntField(dirtyRectObj, gRectClassInfo.top,    dirtyRect.top);
284         env->SetIntField(dirtyRectObj, gRectClassInfo.right,  dirtyRect.right);
285         env->SetIntField(dirtyRectObj, gRectClassInfo.bottom, dirtyRect.bottom);
286     }
287 
288     // Create another reference to the surface and return it.  This reference
289     // should be passed to nativeUnlockCanvasAndPost in place of mNativeObject,
290     // because the latter could be replaced while the surface is locked.
291     sp<Surface> lockedSurface(surface);
292     lockedSurface->incStrong(&sRefBaseOwner);
293     return (jlong) lockedSurface.get();
294 }
295 
nativeUnlockCanvasAndPost(JNIEnv * env,jclass clazz,jlong nativeObject,jobject canvasObj)296 static void nativeUnlockCanvasAndPost(JNIEnv* env, jclass clazz,
297         jlong nativeObject, jobject canvasObj) {
298     sp<Surface> surface(reinterpret_cast<Surface *>(nativeObject));
299     if (!isSurfaceValid(surface)) {
300         return;
301     }
302 
303     // detach the canvas from the surface
304     Canvas* nativeCanvas = GraphicsJNI::getNativeCanvas(env, canvasObj);
305     nativeCanvas->setBitmap(SkBitmap());
306 
307     // unlock surface
308     status_t err = surface->unlockAndPost();
309     if (err < 0) {
310         doThrowIAE(env);
311     }
312 }
313 
nativeAllocateBuffers(JNIEnv *,jclass,jlong nativeObject)314 static void nativeAllocateBuffers(JNIEnv* /* env */ , jclass /* clazz */,
315         jlong nativeObject) {
316     sp<Surface> surface(reinterpret_cast<Surface *>(nativeObject));
317     if (!isSurfaceValid(surface)) {
318         return;
319     }
320 
321     surface->allocateBuffers();
322 }
323 
324 // ----------------------------------------------------------------------------
325 
nativeCreateFromSurfaceControl(JNIEnv * env,jclass clazz,jlong surfaceControlNativeObj)326 static jlong nativeCreateFromSurfaceControl(JNIEnv* env, jclass clazz,
327         jlong surfaceControlNativeObj) {
328     sp<SurfaceControl> ctrl(reinterpret_cast<SurfaceControl *>(surfaceControlNativeObj));
329     sp<Surface> surface(ctrl->createSurface());
330     if (surface != NULL) {
331         surface->incStrong(&sRefBaseOwner);
332     }
333     return reinterpret_cast<jlong>(surface.get());
334 }
335 
nativeGetFromSurfaceControl(JNIEnv * env,jclass clazz,jlong nativeObject,jlong surfaceControlNativeObj)336 static jlong nativeGetFromSurfaceControl(JNIEnv* env, jclass clazz,
337         jlong nativeObject,
338         jlong surfaceControlNativeObj) {
339     Surface* self(reinterpret_cast<Surface *>(nativeObject));
340     sp<SurfaceControl> ctrl(reinterpret_cast<SurfaceControl *>(surfaceControlNativeObj));
341 
342     // If the underlying IGBP's are the same, we don't need to do anything.
343     if (self != nullptr &&
344             IInterface::asBinder(self->getIGraphicBufferProducer()) ==
345             IInterface::asBinder(ctrl->getIGraphicBufferProducer())) {
346         return nativeObject;
347     }
348 
349     sp<Surface> surface(ctrl->getSurface());
350     if (surface != NULL) {
351         surface->incStrong(&sRefBaseOwner);
352     }
353 
354     return reinterpret_cast<jlong>(surface.get());
355 }
356 
nativeReadFromParcel(JNIEnv * env,jclass clazz,jlong nativeObject,jobject parcelObj)357 static jlong nativeReadFromParcel(JNIEnv* env, jclass clazz,
358         jlong nativeObject, jobject parcelObj) {
359     Parcel* parcel = parcelForJavaObject(env, parcelObj);
360     if (parcel == NULL) {
361         doThrowNPE(env);
362         return 0;
363     }
364 
365     android::view::Surface surfaceShim;
366 
367     // Calling code in Surface.java has already read the name of the Surface
368     // from the Parcel
369     surfaceShim.readFromParcel(parcel, /*nameAlreadyRead*/true);
370 
371     sp<Surface> self(reinterpret_cast<Surface *>(nativeObject));
372 
373     // update the Surface only if the underlying IGraphicBufferProducer
374     // has changed.
375     if (self != nullptr
376             && (IInterface::asBinder(self->getIGraphicBufferProducer()) ==
377                     IInterface::asBinder(surfaceShim.graphicBufferProducer))) {
378         // same IGraphicBufferProducer, return ourselves
379         return jlong(self.get());
380     }
381 
382     sp<Surface> sur;
383     if (surfaceShim.graphicBufferProducer != nullptr) {
384         // we have a new IGraphicBufferProducer, create a new Surface for it
385         sur = new Surface(surfaceShim.graphicBufferProducer, true);
386         // and keep a reference before passing to java
387         sur->incStrong(&sRefBaseOwner);
388     }
389 
390     if (self != NULL) {
391         // and loose the java reference to ourselves
392         self->decStrong(&sRefBaseOwner);
393     }
394 
395     return jlong(sur.get());
396 }
397 
nativeWriteToParcel(JNIEnv * env,jclass clazz,jlong nativeObject,jobject parcelObj)398 static void nativeWriteToParcel(JNIEnv* env, jclass clazz,
399         jlong nativeObject, jobject parcelObj) {
400     Parcel* parcel = parcelForJavaObject(env, parcelObj);
401     if (parcel == NULL) {
402         doThrowNPE(env);
403         return;
404     }
405     sp<Surface> self(reinterpret_cast<Surface *>(nativeObject));
406     android::view::Surface surfaceShim;
407     if (self != nullptr) {
408         surfaceShim.graphicBufferProducer = self->getIGraphicBufferProducer();
409     }
410     // Calling code in Surface.java has already written the name of the Surface
411     // to the Parcel
412     surfaceShim.writeToParcel(parcel, /*nameAlreadyWritten*/true);
413 }
414 
nativeGetWidth(JNIEnv * env,jclass clazz,jlong nativeObject)415 static jint nativeGetWidth(JNIEnv* env, jclass clazz, jlong nativeObject) {
416     Surface* surface = reinterpret_cast<Surface*>(nativeObject);
417     ANativeWindow* anw = static_cast<ANativeWindow*>(surface);
418     int value = 0;
419     anw->query(anw, NATIVE_WINDOW_WIDTH, &value);
420     return value;
421 }
422 
nativeGetHeight(JNIEnv * env,jclass clazz,jlong nativeObject)423 static jint nativeGetHeight(JNIEnv* env, jclass clazz, jlong nativeObject) {
424     Surface* surface = reinterpret_cast<Surface*>(nativeObject);
425     ANativeWindow* anw = static_cast<ANativeWindow*>(surface);
426     int value = 0;
427     anw->query(anw, NATIVE_WINDOW_HEIGHT, &value);
428     return value;
429 }
430 
nativeGetNextFrameNumber(JNIEnv * env,jclass clazz,jlong nativeObject)431 static jlong nativeGetNextFrameNumber(JNIEnv *env, jclass clazz, jlong nativeObject) {
432     Surface* surface = reinterpret_cast<Surface*>(nativeObject);
433     return surface->getNextFrameNumber();
434 }
435 
nativeSetScalingMode(JNIEnv * env,jclass clazz,jlong nativeObject,jint scalingMode)436 static jint nativeSetScalingMode(JNIEnv *env, jclass clazz, jlong nativeObject, jint scalingMode) {
437     Surface* surface = reinterpret_cast<Surface*>(nativeObject);
438     return surface->setScalingMode(scalingMode);
439 }
440 
nativeForceScopedDisconnect(JNIEnv * env,jclass clazz,jlong nativeObject)441 static jint nativeForceScopedDisconnect(JNIEnv *env, jclass clazz, jlong nativeObject) {
442     Surface* surface = reinterpret_cast<Surface*>(nativeObject);
443     return surface->disconnect(-1, IGraphicBufferProducer::DisconnectMode::AllLocal);
444 }
445 
nativeAttachAndQueueBufferWithColorSpace(JNIEnv * env,jclass clazz,jlong nativeObject,jobject graphicBuffer,jint colorSpaceId)446 static jint nativeAttachAndQueueBufferWithColorSpace(JNIEnv *env, jclass clazz, jlong nativeObject,
447         jobject graphicBuffer, jint colorSpaceId) {
448     Surface* surface = reinterpret_cast<Surface*>(nativeObject);
449     sp<GraphicBuffer> bp = graphicBufferForJavaObject(env, graphicBuffer);
450     int err = Surface::attachAndQueueBufferWithDataspace(surface, bp,
451             fromNamedColorSpaceValueToDataspace(colorSpaceId));
452     return err;
453 }
454 
nativeSetSharedBufferModeEnabled(JNIEnv * env,jclass clazz,jlong nativeObject,jboolean enabled)455 static jint nativeSetSharedBufferModeEnabled(JNIEnv* env, jclass clazz, jlong nativeObject,
456         jboolean enabled) {
457     Surface* surface = reinterpret_cast<Surface*>(nativeObject);
458     ANativeWindow* anw = static_cast<ANativeWindow*>(surface);
459     return anw->perform(surface, NATIVE_WINDOW_SET_SHARED_BUFFER_MODE, int(enabled));
460 }
461 
nativeSetAutoRefreshEnabled(JNIEnv * env,jclass clazz,jlong nativeObject,jboolean enabled)462 static jint nativeSetAutoRefreshEnabled(JNIEnv* env, jclass clazz, jlong nativeObject,
463         jboolean enabled) {
464     Surface* surface = reinterpret_cast<Surface*>(nativeObject);
465     ANativeWindow* anw = static_cast<ANativeWindow*>(surface);
466     return anw->perform(surface, NATIVE_WINDOW_SET_AUTO_REFRESH, int(enabled));
467 }
468 
469 namespace uirenderer {
470 
471 using namespace android::uirenderer::renderthread;
472 
473 class ContextFactory : public IContextFactory {
474 public:
createAnimationContext(renderthread::TimeLord & clock)475     virtual AnimationContext* createAnimationContext(renderthread::TimeLord& clock) {
476         return new AnimationContext(clock);
477     }
478 };
479 
create(JNIEnv * env,jclass clazz,jlong rootNodePtr,jlong surfacePtr,jboolean isWideColorGamut)480 static jlong create(JNIEnv* env, jclass clazz, jlong rootNodePtr, jlong surfacePtr,
481         jboolean isWideColorGamut) {
482     RenderNode* rootNode = reinterpret_cast<RenderNode*>(rootNodePtr);
483     sp<Surface> surface(reinterpret_cast<Surface*>(surfacePtr));
484     ContextFactory factory;
485     RenderProxy* proxy = new RenderProxy(false, rootNode, &factory);
486     proxy->loadSystemProperties();
487     if (isWideColorGamut) {
488         proxy->setWideGamut(true);
489     }
490     proxy->setSwapBehavior(SwapBehavior::kSwap_discardBuffer);
491     proxy->setSurface(surface, false);
492     // Shadows can't be used via this interface, so just set the light source
493     // to all 0s.
494     proxy->setLightAlpha(0, 0);
495     proxy->setLightGeometry((Vector3){0, 0, 0}, 0);
496     return (jlong) proxy;
497 }
498 
setSurface(JNIEnv * env,jclass clazz,jlong rendererPtr,jlong surfacePtr)499 static void setSurface(JNIEnv* env, jclass clazz, jlong rendererPtr, jlong surfacePtr) {
500     RenderProxy* proxy = reinterpret_cast<RenderProxy*>(rendererPtr);
501     sp<Surface> surface(reinterpret_cast<Surface*>(surfacePtr));
502     proxy->setSurface(surface);
503 }
504 
draw(JNIEnv * env,jclass clazz,jlong rendererPtr)505 static void draw(JNIEnv* env, jclass clazz, jlong rendererPtr) {
506     RenderProxy* proxy = reinterpret_cast<RenderProxy*>(rendererPtr);
507     nsecs_t vsync = systemTime(CLOCK_MONOTONIC);
508     UiFrameInfoBuilder(proxy->frameInfo())
509             .setVsync(vsync, vsync)
510             .addFlag(FrameInfoFlags::SurfaceCanvas);
511     proxy->syncAndDrawFrame();
512 }
513 
destroy(JNIEnv * env,jclass clazz,jlong rendererPtr)514 static void destroy(JNIEnv* env, jclass clazz, jlong rendererPtr) {
515     RenderProxy* proxy = reinterpret_cast<RenderProxy*>(rendererPtr);
516     delete proxy;
517 }
518 
519 } // uirenderer
520 
521 // ----------------------------------------------------------------------------
522 
523 namespace hwui = android::uirenderer;
524 
525 static const JNINativeMethod gSurfaceMethods[] = {
526     {"nativeCreateFromSurfaceTexture", "(Landroid/graphics/SurfaceTexture;)J",
527             (void*)nativeCreateFromSurfaceTexture },
528     {"nativeRelease", "(J)V",
529             (void*)nativeRelease },
530     {"nativeIsValid", "(J)Z",
531             (void*)nativeIsValid },
532     {"nativeIsConsumerRunningBehind", "(J)Z",
533             (void*)nativeIsConsumerRunningBehind },
534     {"nativeLockCanvas", "(JLandroid/graphics/Canvas;Landroid/graphics/Rect;)J",
535             (void*)nativeLockCanvas },
536     {"nativeUnlockCanvasAndPost", "(JLandroid/graphics/Canvas;)V",
537             (void*)nativeUnlockCanvasAndPost },
538     {"nativeAllocateBuffers", "(J)V",
539             (void*)nativeAllocateBuffers },
540     {"nativeCreateFromSurfaceControl", "(J)J",
541             (void*)nativeCreateFromSurfaceControl },
542     {"nativeGetFromSurfaceControl", "(JJ)J",
543             (void*)nativeGetFromSurfaceControl },
544     {"nativeReadFromParcel", "(JLandroid/os/Parcel;)J",
545             (void*)nativeReadFromParcel },
546     {"nativeWriteToParcel", "(JLandroid/os/Parcel;)V",
547             (void*)nativeWriteToParcel },
548     {"nativeGetWidth", "(J)I", (void*)nativeGetWidth },
549     {"nativeGetHeight", "(J)I", (void*)nativeGetHeight },
550     {"nativeGetNextFrameNumber", "(J)J", (void*)nativeGetNextFrameNumber },
551     {"nativeSetScalingMode", "(JI)I", (void*)nativeSetScalingMode },
552     {"nativeForceScopedDisconnect", "(J)I", (void*)nativeForceScopedDisconnect},
553     {"nativeAttachAndQueueBufferWithColorSpace", "(JLandroid/graphics/GraphicBuffer;I)I",
554             (void*)nativeAttachAndQueueBufferWithColorSpace},
555     {"nativeSetSharedBufferModeEnabled", "(JZ)I", (void*)nativeSetSharedBufferModeEnabled},
556     {"nativeSetAutoRefreshEnabled", "(JZ)I", (void*)nativeSetAutoRefreshEnabled},
557 
558     // HWUI context
559     {"nHwuiCreate", "(JJZ)J", (void*) hwui::create },
560     {"nHwuiSetSurface", "(JJ)V", (void*) hwui::setSurface },
561     {"nHwuiDraw", "(J)V", (void*) hwui::draw },
562     {"nHwuiDestroy", "(J)V", (void*) hwui::destroy },
563 };
564 
register_android_view_Surface(JNIEnv * env)565 int register_android_view_Surface(JNIEnv* env)
566 {
567     int err = RegisterMethodsOrDie(env, "android/view/Surface",
568             gSurfaceMethods, NELEM(gSurfaceMethods));
569 
570     jclass clazz = FindClassOrDie(env, "android/view/Surface");
571     gSurfaceClassInfo.clazz = MakeGlobalRefOrDie(env, clazz);
572     gSurfaceClassInfo.mNativeObject = GetFieldIDOrDie(env,
573             gSurfaceClassInfo.clazz, "mNativeObject", "J");
574     gSurfaceClassInfo.mLock = GetFieldIDOrDie(env,
575             gSurfaceClassInfo.clazz, "mLock", "Ljava/lang/Object;");
576     gSurfaceClassInfo.ctor = GetMethodIDOrDie(env, gSurfaceClassInfo.clazz, "<init>", "(J)V");
577 
578     clazz = FindClassOrDie(env, "android/graphics/Rect");
579     gRectClassInfo.left = GetFieldIDOrDie(env, clazz, "left", "I");
580     gRectClassInfo.top = GetFieldIDOrDie(env, clazz, "top", "I");
581     gRectClassInfo.right = GetFieldIDOrDie(env, clazz, "right", "I");
582     gRectClassInfo.bottom = GetFieldIDOrDie(env, clazz, "bottom", "I");
583 
584     return err;
585 }
586 
587 };
588