• 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 #undef ANDROID_UTILS_REF_BASE_DISABLE_IMPLICIT_CONSTRUCTION // TODO:remove this and fix code
17 
18 #define LOG_TAG "Surface"
19 
20 #include <stdio.h>
21 
22 #include "jni.h"
23 #include <nativehelper/JNIHelp.h>
24 #include "core_jni_helpers.h"
25 
26 #include <android/graphics/canvas.h>
27 #include <android_runtime/android_hardware_HardwareBuffer.h>
28 #include <android_runtime/android_graphics_SurfaceTexture.h>
29 #include <android_runtime/android_view_Surface.h>
30 #include <android_runtime/Log.h>
31 #ifdef __ANDROID__
32 #include <private/android/AHardwareBufferHelpers.h>
33 
34 #include "android_os_Parcel.h"
35 #include <binder/Parcel.h>
36 
37 #include <gui/BLASTBufferQueue.h>
38 #endif
39 #include <gui/Surface.h>
40 #ifdef __ANDROID__
41 #include <gui/SurfaceControl.h>
42 #include <gui/view/Surface.h>
43 #endif
44 
45 #include <ui/GraphicBuffer.h>
46 #include <ui/Rect.h>
47 #include <ui/Region.h>
48 
49 #include <utils/misc.h>
50 #include <utils/Log.h>
51 
52 #include <nativehelper/ScopedUtfChars.h>
53 
54 // ----------------------------------------------------------------------------
55 
56 namespace android {
57 
58 static const char* const IllegalArgumentException = "java/lang/IllegalArgumentException";
59 static const char* const OutOfResourcesException = "android/view/Surface$OutOfResourcesException";
60 
61 static struct {
62     jclass clazz;
63     jfieldID mNativeObject;
64     jfieldID mLock;
65     jmethodID ctor;
66 } gSurfaceClassInfo;
67 
68 static struct {
69     jfieldID left;
70     jfieldID top;
71     jfieldID right;
72     jfieldID bottom;
73 } gRectClassInfo;
74 
75 #ifdef __ANDROID__
76 class JNamedColorSpace {
77 public:
78     // ColorSpace.Named.SRGB.ordinal() = 0;
79     static constexpr jint SRGB = 0;
80 
81     // ColorSpace.Named.DISPLAY_P3.ordinal() = 7;
82     static constexpr jint DISPLAY_P3 = 7;
83 };
84 
fromNamedColorSpaceValueToDataspace(const jint colorSpace)85 constexpr ui::Dataspace fromNamedColorSpaceValueToDataspace(const jint colorSpace) {
86     switch (colorSpace) {
87         case JNamedColorSpace::DISPLAY_P3:
88             return ui::Dataspace::DISPLAY_P3;
89         default:
90             return ui::Dataspace::V0_SRGB;
91     }
92 }
93 #endif
94 
95 // ----------------------------------------------------------------------------
96 
97 // this is just a pointer we use to pass to inc/decStrong
98 static const void *sRefBaseOwner;
99 
android_view_Surface_isInstanceOf(JNIEnv * env,jobject obj)100 bool android_view_Surface_isInstanceOf(JNIEnv* env, jobject obj) {
101     return env->IsInstanceOf(obj, gSurfaceClassInfo.clazz);
102 }
103 
android_view_Surface_getNativeWindow(JNIEnv * env,jobject surfaceObj)104 sp<ANativeWindow> android_view_Surface_getNativeWindow(JNIEnv* env, jobject surfaceObj) {
105     return android_view_Surface_getSurface(env, surfaceObj);
106 }
107 
android_view_Surface_getSurface(JNIEnv * env,jobject surfaceObj)108 sp<Surface> android_view_Surface_getSurface(JNIEnv* env, jobject surfaceObj) {
109     sp<Surface> sur;
110     jobject lock = env->GetObjectField(surfaceObj,
111             gSurfaceClassInfo.mLock);
112     if (env->MonitorEnter(lock) == JNI_OK) {
113         sur = reinterpret_cast<Surface *>(
114                 env->GetLongField(surfaceObj, gSurfaceClassInfo.mNativeObject));
115         env->MonitorExit(lock);
116     }
117     env->DeleteLocalRef(lock);
118     return sur;
119 }
120 
android_view_Surface_createFromSurface(JNIEnv * env,const sp<Surface> & surface)121 jobject android_view_Surface_createFromSurface(JNIEnv* env, const sp<Surface>& surface) {
122     jobject surfaceObj = env->NewObject(gSurfaceClassInfo.clazz,
123             gSurfaceClassInfo.ctor, (jlong)surface.get());
124     if (surfaceObj == NULL) {
125         if (env->ExceptionCheck()) {
126             ALOGE("Could not create instance of Surface from IGraphicBufferProducer.");
127             LOGE_EX(env);
128             env->ExceptionClear();
129         }
130         return NULL;
131     }
132     surface->incStrong(&sRefBaseOwner);
133     return surfaceObj;
134 }
135 
android_view_Surface_createFromIGraphicBufferProducer(JNIEnv * env,const sp<IGraphicBufferProducer> & bufferProducer)136 jobject android_view_Surface_createFromIGraphicBufferProducer(JNIEnv* env,
137         const sp<IGraphicBufferProducer>& bufferProducer) {
138     if (bufferProducer == NULL) {
139         return NULL;
140     }
141 
142     sp<Surface> surface = sp<Surface>::make(bufferProducer, true);
143     return android_view_Surface_createFromSurface(env, surface);
144 }
145 
146 // ----------------------------------------------------------------------------
147 
isSurfaceValid(const sp<Surface> & sur)148 static inline bool isSurfaceValid(const sp<Surface>& sur) {
149     return Surface::isValid(sur);
150 }
151 
152 // ----------------------------------------------------------------------------
153 
154 #ifdef __ANDROID__
nativeCreateFromSurfaceTexture(JNIEnv * env,jclass clazz,jobject surfaceTextureObj)155 static jlong nativeCreateFromSurfaceTexture(JNIEnv* env, jclass clazz,
156         jobject surfaceTextureObj) {
157     sp<IGraphicBufferProducer> producer(SurfaceTexture_getProducer(env, surfaceTextureObj));
158     if (producer == NULL) {
159         jniThrowException(env, IllegalArgumentException,
160                 "SurfaceTexture has already been released");
161         return 0;
162     }
163 
164     sp<Surface> surface = sp<Surface>::make(producer, true);
165     if (surface == NULL) {
166         jniThrowException(env, OutOfResourcesException, NULL);
167         return 0;
168     }
169 
170     surface->incStrong(&sRefBaseOwner);
171     return jlong(surface.get());
172 }
173 #endif
174 
nativeRelease(JNIEnv * env,jclass clazz,jlong nativeObject)175 static void nativeRelease(JNIEnv* env, jclass clazz, jlong nativeObject) {
176     sp<Surface> sur(reinterpret_cast<Surface *>(nativeObject));
177     sur->decStrong(&sRefBaseOwner);
178 }
179 
nativeIsValid(JNIEnv * env,jclass clazz,jlong nativeObject)180 static jboolean nativeIsValid(JNIEnv* env, jclass clazz, jlong nativeObject) {
181     sp<Surface> sur(reinterpret_cast<Surface *>(nativeObject));
182     return isSurfaceValid(sur) ? JNI_TRUE : JNI_FALSE;
183 }
184 
nativeIsConsumerRunningBehind(JNIEnv * env,jclass clazz,jlong nativeObject)185 static jboolean nativeIsConsumerRunningBehind(JNIEnv* env, jclass clazz, jlong nativeObject) {
186     sp<Surface> sur(reinterpret_cast<Surface *>(nativeObject));
187     if (!isSurfaceValid(sur)) {
188         jniThrowException(env, IllegalArgumentException, NULL);
189         return JNI_FALSE;
190     }
191     int value = 0;
192     ANativeWindow* anw = static_cast<ANativeWindow*>(sur.get());
193     anw->query(anw, NATIVE_WINDOW_CONSUMER_RUNNING_BEHIND, &value);
194     return value;
195 }
196 
nativeLockCanvas(JNIEnv * env,jclass clazz,jlong nativeObject,jobject canvasObj,jobject dirtyRectObj)197 static jlong nativeLockCanvas(JNIEnv* env, jclass clazz,
198         jlong nativeObject, jobject canvasObj, jobject dirtyRectObj) {
199     sp<Surface> surface(reinterpret_cast<Surface *>(nativeObject));
200 
201     if (!isSurfaceValid(surface)) {
202         jniThrowException(env, IllegalArgumentException, NULL);
203         return 0;
204     }
205 
206     if (!ACanvas_isSupportedPixelFormat(ANativeWindow_getFormat(surface.get()))) {
207         native_window_set_buffers_format(surface.get(), PIXEL_FORMAT_RGBA_8888);
208     }
209 
210     Rect dirtyRect(Rect::EMPTY_RECT);
211     Rect* dirtyRectPtr = NULL;
212 
213     if (dirtyRectObj) {
214         dirtyRect.left   = env->GetIntField(dirtyRectObj, gRectClassInfo.left);
215         dirtyRect.top    = env->GetIntField(dirtyRectObj, gRectClassInfo.top);
216         dirtyRect.right  = env->GetIntField(dirtyRectObj, gRectClassInfo.right);
217         dirtyRect.bottom = env->GetIntField(dirtyRectObj, gRectClassInfo.bottom);
218         dirtyRectPtr = &dirtyRect;
219     }
220 
221     ANativeWindow_Buffer buffer;
222     status_t err = surface->lock(&buffer, dirtyRectPtr);
223     if (err < 0) {
224         const char* const exception = (err == NO_MEMORY) ?
225                 OutOfResourcesException : IllegalArgumentException;
226         jniThrowException(env, exception, NULL);
227         return 0;
228     }
229 
230     graphics::Canvas canvas(env, canvasObj);
231     canvas.setBuffer(&buffer, static_cast<int32_t>(surface->getBuffersDataSpace()));
232 
233     if (dirtyRectPtr) {
234         canvas.clipRect({dirtyRect.left, dirtyRect.top, dirtyRect.right, dirtyRect.bottom});
235     }
236 
237     if (dirtyRectObj) {
238         env->SetIntField(dirtyRectObj, gRectClassInfo.left,   dirtyRect.left);
239         env->SetIntField(dirtyRectObj, gRectClassInfo.top,    dirtyRect.top);
240         env->SetIntField(dirtyRectObj, gRectClassInfo.right,  dirtyRect.right);
241         env->SetIntField(dirtyRectObj, gRectClassInfo.bottom, dirtyRect.bottom);
242     }
243 
244     // Create another reference to the surface and return it.  This reference
245     // should be passed to nativeUnlockCanvasAndPost in place of mNativeObject,
246     // because the latter could be replaced while the surface is locked.
247     sp<Surface> lockedSurface(surface);
248     lockedSurface->incStrong(&sRefBaseOwner);
249     return (jlong) lockedSurface.get();
250 }
251 
nativeUnlockCanvasAndPost(JNIEnv * env,jclass clazz,jlong nativeObject,jobject canvasObj)252 static void nativeUnlockCanvasAndPost(JNIEnv* env, jclass clazz,
253         jlong nativeObject, jobject canvasObj) {
254     sp<Surface> surface(reinterpret_cast<Surface *>(nativeObject));
255     if (!isSurfaceValid(surface)) {
256         return;
257     }
258 
259     // detach the canvas from the surface
260     graphics::Canvas canvas(env, canvasObj);
261     canvas.setBuffer(nullptr, ADATASPACE_UNKNOWN);
262 
263     // unlock surface
264     status_t err = surface->unlockAndPost();
265     if (err < 0) {
266         jniThrowException(env, IllegalArgumentException, NULL);
267     }
268 }
269 
nativeAllocateBuffers(JNIEnv *,jclass,jlong nativeObject)270 static void nativeAllocateBuffers(JNIEnv* /* env */ , jclass /* clazz */,
271         jlong nativeObject) {
272     sp<Surface> surface(reinterpret_cast<Surface *>(nativeObject));
273     if (!isSurfaceValid(surface)) {
274         return;
275     }
276 
277     surface->allocateBuffers();
278 }
279 
280 // ----------------------------------------------------------------------------
281 #ifdef __ANDROID__
nativeCreateFromSurfaceControl(JNIEnv * env,jclass clazz,jlong surfaceControlNativeObj)282 static jlong nativeCreateFromSurfaceControl(JNIEnv* env, jclass clazz,
283         jlong surfaceControlNativeObj) {
284     sp<SurfaceControl> ctrl(reinterpret_cast<SurfaceControl *>(surfaceControlNativeObj));
285     sp<Surface> surface(ctrl->createSurface());
286     if (surface != NULL) {
287         surface->incStrong(&sRefBaseOwner);
288     }
289     return reinterpret_cast<jlong>(surface.get());
290 }
291 
nativeGetFromSurfaceControl(JNIEnv * env,jclass clazz,jlong nativeObject,jlong surfaceControlNativeObj)292 static jlong nativeGetFromSurfaceControl(JNIEnv* env, jclass clazz,
293         jlong nativeObject,
294         jlong surfaceControlNativeObj) {
295     Surface* self(reinterpret_cast<Surface *>(nativeObject));
296     sp<SurfaceControl> ctrl(reinterpret_cast<SurfaceControl *>(surfaceControlNativeObj));
297 
298     // If the underlying IGBP's are the same, we don't need to do anything.
299     if (self != nullptr &&
300             IInterface::asBinder(self->getIGraphicBufferProducer()) ==
301             IInterface::asBinder(ctrl->getIGraphicBufferProducer())) {
302         return nativeObject;
303     }
304 
305     sp<Surface> surface(ctrl->getSurface());
306     if (surface != NULL) {
307         surface->incStrong(&sRefBaseOwner);
308     }
309 
310     return reinterpret_cast<jlong>(surface.get());
311 }
312 
nativeGetFromBlastBufferQueue(JNIEnv * env,jclass clazz,jlong nativeObject,jlong blastBufferQueueNativeObj)313 static jlong nativeGetFromBlastBufferQueue(JNIEnv* env, jclass clazz, jlong nativeObject,
314                                            jlong blastBufferQueueNativeObj) {
315     Surface* self(reinterpret_cast<Surface*>(nativeObject));
316     sp<BLASTBufferQueue> queue = reinterpret_cast<BLASTBufferQueue*>(blastBufferQueueNativeObj);
317     const sp<IGraphicBufferProducer>& bufferProducer = queue->getIGraphicBufferProducer();
318     // If the underlying IGBP's are the same, we don't need to do anything.
319     if (self != nullptr &&
320         IInterface::asBinder(self->getIGraphicBufferProducer()) ==
321                 IInterface::asBinder(bufferProducer)) {
322         return nativeObject;
323     }
324 
325     sp<Surface> surface = queue->getSurface(true /* includeSurfaceControlHandle */);
326     if (surface != NULL) {
327         surface->incStrong(&sRefBaseOwner);
328     }
329 
330     return reinterpret_cast<jlong>(surface.get());
331 }
332 
nativeReadFromParcel(JNIEnv * env,jclass clazz,jlong nativeObject,jobject parcelObj)333 static jlong nativeReadFromParcel(JNIEnv* env, jclass clazz,
334         jlong nativeObject, jobject parcelObj) {
335     Parcel* parcel = parcelForJavaObject(env, parcelObj);
336     if (parcel == NULL) {
337         jniThrowNullPointerException(env, NULL);
338         return 0;
339     }
340 
341     android::view::Surface surfaceShim;
342 
343     // Calling code in Surface.java has already read the name of the Surface
344     // from the Parcel
345     surfaceShim.readFromParcel(parcel, /*nameAlreadyRead*/true);
346 
347     sp<Surface> self(reinterpret_cast<Surface *>(nativeObject));
348 
349     // update the Surface only if the underlying IGraphicBufferProducer
350     // has changed.
351     if (self != nullptr
352             && (IInterface::asBinder(self->getIGraphicBufferProducer()) ==
353                     IInterface::asBinder(surfaceShim.graphicBufferProducer))) {
354         // same IGraphicBufferProducer, return ourselves
355         return jlong(self.get());
356     }
357 
358     sp<Surface> sur;
359     if (surfaceShim.graphicBufferProducer != nullptr) {
360         // we have a new IGraphicBufferProducer, create a new Surface for it
361         sur = sp<Surface>::make(surfaceShim.graphicBufferProducer, true,
362                                 surfaceShim.surfaceControlHandle);
363         // and keep a reference before passing to java
364         sur->incStrong(&sRefBaseOwner);
365     }
366 
367     if (self != NULL) {
368         // and loose the java reference to ourselves
369         self->decStrong(&sRefBaseOwner);
370     }
371 
372     return jlong(sur.get());
373 }
374 
nativeWriteToParcel(JNIEnv * env,jclass clazz,jlong nativeObject,jobject parcelObj)375 static void nativeWriteToParcel(JNIEnv* env, jclass clazz,
376         jlong nativeObject, jobject parcelObj) {
377     Parcel* parcel = parcelForJavaObject(env, parcelObj);
378     if (parcel == NULL) {
379         jniThrowNullPointerException(env, NULL);
380         return;
381     }
382     sp<Surface> self(reinterpret_cast<Surface *>(nativeObject));
383     android::view::Surface surfaceShim;
384     if (self != nullptr) {
385         surfaceShim.graphicBufferProducer = self->getIGraphicBufferProducer();
386         surfaceShim.surfaceControlHandle = self->getSurfaceControlHandle();
387     }
388     // Calling code in Surface.java has already written the name of the Surface
389     // to the Parcel
390     surfaceShim.writeToParcel(parcel, /*nameAlreadyWritten*/true);
391 }
392 #endif
393 
nativeGetWidth(JNIEnv * env,jclass clazz,jlong nativeObject)394 static jint nativeGetWidth(JNIEnv* env, jclass clazz, jlong nativeObject) {
395     Surface* surface = reinterpret_cast<Surface*>(nativeObject);
396     ANativeWindow* anw = static_cast<ANativeWindow*>(surface);
397     int value = 0;
398     anw->query(anw, NATIVE_WINDOW_WIDTH, &value);
399     return value;
400 }
401 
nativeGetHeight(JNIEnv * env,jclass clazz,jlong nativeObject)402 static jint nativeGetHeight(JNIEnv* env, jclass clazz, jlong nativeObject) {
403     Surface* surface = reinterpret_cast<Surface*>(nativeObject);
404     ANativeWindow* anw = static_cast<ANativeWindow*>(surface);
405     int value = 0;
406     anw->query(anw, NATIVE_WINDOW_HEIGHT, &value);
407     return value;
408 }
409 
nativeGetNextFrameNumber(JNIEnv * env,jclass clazz,jlong nativeObject)410 static jlong nativeGetNextFrameNumber(JNIEnv *env, jclass clazz, jlong nativeObject) {
411     Surface* surface = reinterpret_cast<Surface*>(nativeObject);
412     return surface->getNextFrameNumber();
413 }
414 
nativeSetScalingMode(JNIEnv * env,jclass clazz,jlong nativeObject,jint scalingMode)415 static jint nativeSetScalingMode(JNIEnv *env, jclass clazz, jlong nativeObject, jint scalingMode) {
416     Surface* surface = reinterpret_cast<Surface*>(nativeObject);
417     return surface->setScalingMode(scalingMode);
418 }
419 
nativeForceScopedDisconnect(JNIEnv * env,jclass clazz,jlong nativeObject)420 static jint nativeForceScopedDisconnect(JNIEnv *env, jclass clazz, jlong nativeObject) {
421     Surface* surface = reinterpret_cast<Surface*>(nativeObject);
422     return surface->disconnect(-1, IGraphicBufferProducer::DisconnectMode::AllLocal);
423 }
424 
425 #ifdef __ANDROID__
nativeAttachAndQueueBufferWithColorSpace(JNIEnv * env,jclass clazz,jlong nativeObject,jobject hardwareBuffer,jint colorSpaceId)426 static jint nativeAttachAndQueueBufferWithColorSpace(JNIEnv* env, jclass clazz, jlong nativeObject,
427                                                      jobject hardwareBuffer, jint colorSpaceId) {
428     Surface* surface = reinterpret_cast<Surface*>(nativeObject);
429     AHardwareBuffer* ahb =
430             android_hardware_HardwareBuffer_getNativeHardwareBuffer(env, hardwareBuffer);
431     GraphicBuffer* gb = AHardwareBuffer_to_GraphicBuffer(ahb);
432     int err = Surface::attachAndQueueBufferWithDataspace(surface, gb,
433             fromNamedColorSpaceValueToDataspace(colorSpaceId));
434     return err;
435 }
436 #endif
437 
nativeSetSharedBufferModeEnabled(JNIEnv * env,jclass clazz,jlong nativeObject,jboolean enabled)438 static jint nativeSetSharedBufferModeEnabled(JNIEnv* env, jclass clazz, jlong nativeObject,
439         jboolean enabled) {
440     Surface* surface = reinterpret_cast<Surface*>(nativeObject);
441     ANativeWindow* anw = static_cast<ANativeWindow*>(surface);
442     return anw->perform(surface, NATIVE_WINDOW_SET_SHARED_BUFFER_MODE, int(enabled));
443 }
444 
nativeSetAutoRefreshEnabled(JNIEnv * env,jclass clazz,jlong nativeObject,jboolean enabled)445 static jint nativeSetAutoRefreshEnabled(JNIEnv* env, jclass clazz, jlong nativeObject,
446         jboolean enabled) {
447     Surface* surface = reinterpret_cast<Surface*>(nativeObject);
448     ANativeWindow* anw = static_cast<ANativeWindow*>(surface);
449     return anw->perform(surface, NATIVE_WINDOW_SET_AUTO_REFRESH, int(enabled));
450 }
451 
nativeSetFrameRate(JNIEnv * env,jclass clazz,jlong nativeObject,jfloat frameRate,jint compatibility,jint changeFrameRateStrategy)452 static jint nativeSetFrameRate(JNIEnv* env, jclass clazz, jlong nativeObject, jfloat frameRate,
453                                jint compatibility, jint changeFrameRateStrategy) {
454     Surface* surface = reinterpret_cast<Surface*>(nativeObject);
455     ANativeWindow* anw = static_cast<ANativeWindow*>(surface);
456     // Our compatibility is a Surface.FRAME_RATE_COMPATIBILITY_* value, and
457     // NATIVE_WINDOW_SET_FRAME_RATE takes an
458     // ANATIVEWINDOW_FRAME_RATE_COMPATIBILITY_* value. The values are identical
459     // though, so no need to explicitly convert.
460     return anw->perform(surface, NATIVE_WINDOW_SET_FRAME_RATE, double(frameRate), compatibility,
461                         int(changeFrameRateStrategy));
462 }
463 
nativeDestroy(JNIEnv * env,jclass clazz,jlong nativeObject)464 static void nativeDestroy(JNIEnv* env, jclass clazz, jlong nativeObject) {
465     sp<Surface> surface(reinterpret_cast<Surface*>(nativeObject));
466     surface->destroy();
467 }
468 
469 // ----------------------------------------------------------------------------
470 
471 static const JNINativeMethod gSurfaceMethods[] = {
472 #ifdef __ANDROID__
nativeCreateFromSurfaceTexture(Landroid/graphics/SurfaceTexture;)473         {"nativeCreateFromSurfaceTexture", "(Landroid/graphics/SurfaceTexture;)J",
474          (void*)nativeCreateFromSurfaceTexture},
475 #endif
nativeRelease(J)476         {"nativeRelease", "(J)V", (void*)nativeRelease},
nativeIsValid(J)477         {"nativeIsValid", "(J)Z", (void*)nativeIsValid},
nativeIsConsumerRunningBehind(J)478         {"nativeIsConsumerRunningBehind", "(J)Z", (void*)nativeIsConsumerRunningBehind},
nativeLockCanvas(JLandroid/graphics/Canvas;Landroid/graphics/Rect;)479         {"nativeLockCanvas", "(JLandroid/graphics/Canvas;Landroid/graphics/Rect;)J",
480          (void*)nativeLockCanvas},
nativeUnlockCanvasAndPost(JLandroid/graphics/Canvas;)481         {"nativeUnlockCanvasAndPost", "(JLandroid/graphics/Canvas;)V",
482          (void*)nativeUnlockCanvasAndPost},
nativeAllocateBuffers(J)483         {"nativeAllocateBuffers", "(J)V", (void*)nativeAllocateBuffers},
484 #ifdef __ANDROID__
nativeCreateFromSurfaceControl(J)485         {"nativeCreateFromSurfaceControl", "(J)J", (void*)nativeCreateFromSurfaceControl},
nativeGetFromSurfaceControl(JJ)486         {"nativeGetFromSurfaceControl", "(JJ)J", (void*)nativeGetFromSurfaceControl},
nativeReadFromParcel(JLandroid/os/Parcel;)487         {"nativeReadFromParcel", "(JLandroid/os/Parcel;)J", (void*)nativeReadFromParcel},
nativeWriteToParcel(JLandroid/os/Parcel;)488         {"nativeWriteToParcel", "(JLandroid/os/Parcel;)V", (void*)nativeWriteToParcel},
489 #endif
nativeGetWidth(J)490         {"nativeGetWidth", "(J)I", (void*)nativeGetWidth},
nativeGetHeight(J)491         {"nativeGetHeight", "(J)I", (void*)nativeGetHeight},
nativeGetNextFrameNumber(J)492         {"nativeGetNextFrameNumber", "(J)J", (void*)nativeGetNextFrameNumber},
nativeSetScalingMode(JI)493         {"nativeSetScalingMode", "(JI)I", (void*)nativeSetScalingMode},
nativeForceScopedDisconnect(J)494         {"nativeForceScopedDisconnect", "(J)I", (void*)nativeForceScopedDisconnect},
495 #ifdef __ANDROID__
nativeAttachAndQueueBufferWithColorSpace(JLandroid/hardware/HardwareBuffer;I)496         {"nativeAttachAndQueueBufferWithColorSpace", "(JLandroid/hardware/HardwareBuffer;I)I",
497          (void*)nativeAttachAndQueueBufferWithColorSpace},
498 #endif
nativeSetSharedBufferModeEnabled(JZ)499         {"nativeSetSharedBufferModeEnabled", "(JZ)I", (void*)nativeSetSharedBufferModeEnabled},
nativeSetAutoRefreshEnabled(JZ)500         {"nativeSetAutoRefreshEnabled", "(JZ)I", (void*)nativeSetAutoRefreshEnabled},
nativeSetFrameRate(JFII)501         {"nativeSetFrameRate", "(JFII)I", (void*)nativeSetFrameRate},
502 #ifdef __ANDROID__
nativeGetFromBlastBufferQueue(JJ)503         {"nativeGetFromBlastBufferQueue", "(JJ)J", (void*)nativeGetFromBlastBufferQueue},
504 #endif
nativeDestroy(J)505         {"nativeDestroy", "(J)V", (void*)nativeDestroy},
506 };
507 
register_android_view_Surface(JNIEnv * env)508 int register_android_view_Surface(JNIEnv* env)
509 {
510     int err = RegisterMethodsOrDie(env, "android/view/Surface",
511             gSurfaceMethods, NELEM(gSurfaceMethods));
512 
513     jclass clazz = FindClassOrDie(env, "android/view/Surface");
514     gSurfaceClassInfo.clazz = MakeGlobalRefOrDie(env, clazz);
515     gSurfaceClassInfo.mNativeObject = GetFieldIDOrDie(env,
516             gSurfaceClassInfo.clazz, "mNativeObject", "J");
517     gSurfaceClassInfo.mLock = GetFieldIDOrDie(env,
518             gSurfaceClassInfo.clazz, "mLock", "Ljava/lang/Object;");
519     gSurfaceClassInfo.ctor = GetMethodIDOrDie(env, gSurfaceClassInfo.clazz, "<init>", "(J)V");
520 
521     clazz = FindClassOrDie(env, "android/graphics/Rect");
522     gRectClassInfo.left = GetFieldIDOrDie(env, clazz, "left", "I");
523     gRectClassInfo.top = GetFieldIDOrDie(env, clazz, "top", "I");
524     gRectClassInfo.right = GetFieldIDOrDie(env, clazz, "right", "I");
525     gRectClassInfo.bottom = GetFieldIDOrDie(env, clazz, "bottom", "I");
526 
527     return err;
528 }
529 
530 };
531