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