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