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