blob: a9002945ff91c411e0d3e42e12e06c496b1990fd [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
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
Mathias Agopianfae5cb22010-06-04 18:26:32 -070017#define LOG_TAG "Surface"
18
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080019#include <stdio.h>
20
Mathias Agopian3866f0d2013-02-11 22:08:48 -080021#include "jni.h"
Steven Moreland2279b252017-07-19 09:50:45 -070022#include <nativehelper/JNIHelp.h>
Jeff Brown64a55af2012-08-26 02:47:39 -070023#include "android_os_Parcel.h"
Robert Carra35e5de2017-03-03 17:04:54 -080024#include "android/graphics/GraphicBuffer.h"
Dianne Hackborna1111872010-11-23 20:55:11 -080025#include "android/graphics/GraphicsJNI.h"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080026
Andreas Gampeed6b9df2014-11-20 22:02:20 -080027#include "core_jni_helpers.h"
Mathias Agopian3866f0d2013-02-11 22:08:48 -080028#include <android_runtime/android_view_Surface.h>
29#include <android_runtime/android_graphics_SurfaceTexture.h>
Ruben Brunk87eac992013-09-09 17:44:59 -070030#include <android_runtime/Log.h>
Mathias Agopian8335f1c2012-02-25 18:48:35 -080031
Mathias Agopian4a05f432013-03-12 18:43:34 -070032#include <binder/Parcel.h>
33
Mathias Agopian8335f1c2012-02-25 18:48:35 -080034#include <gui/Surface.h>
Mathias Agopian7a1e28d2017-02-08 17:07:13 -080035#include <gui/view/Surface.h>
Mathias Agopian52800612013-02-14 17:11:20 -080036#include <gui/SurfaceControl.h>
Andy McFaddend47f7d82012-12-18 09:48:38 -080037#include <gui/GLConsumer.h>
Mathias Agopian8335f1c2012-02-25 18:48:35 -080038
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080039#include <ui/Rect.h>
Mathias Agopian8335f1c2012-02-25 18:48:35 -080040#include <ui/Region.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080041
42#include <SkCanvas.h>
43#include <SkBitmap.h>
Leon Scroggins III8790be62013-12-03 16:26:51 -050044#include <SkImage.h>
Mathias Agopian6158b1b2009-05-11 00:03:41 -070045#include <SkRegion.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080046
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080047#include <utils/misc.h>
Jeff Brown9e316a12012-10-08 19:17:06 -070048#include <utils/Log.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080049
Steven Moreland2279b252017-07-19 09:50:45 -070050#include <nativehelper/ScopedUtfChars.h>
Jeff Brown64a55af2012-08-26 02:47:39 -070051
John Reckbb2d0cc2014-10-21 10:09:36 -070052#include <AnimationContext.h>
John Reckba6adf62015-02-19 14:36:50 -080053#include <FrameInfo.h>
John Reckbb2d0cc2014-10-21 10:09:36 -070054#include <RenderNode.h>
55#include <renderthread/RenderProxy.h>
56
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080057// ----------------------------------------------------------------------------
58
59namespace android {
60
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080061static const char* const OutOfResourcesException =
62 "android/view/Surface$OutOfResourcesException";
63
Jeff Brown64a55af2012-08-26 02:47:39 -070064static struct {
65 jclass clazz;
Mathias Agopian3866f0d2013-02-11 22:08:48 -080066 jfieldID mNativeObject;
Jeff Brownfc0ebd72013-04-30 16:33:00 -070067 jfieldID mLock;
Jeff Browncbad9762012-09-04 21:57:59 -070068 jmethodID ctor;
Jeff Brown64a55af2012-08-26 02:47:39 -070069} gSurfaceClassInfo;
tedbo05031612011-06-06 16:02:47 -070070
Jeff Brown64a55af2012-08-26 02:47:39 -070071static struct {
72 jfieldID left;
73 jfieldID top;
74 jfieldID right;
75 jfieldID bottom;
76} gRectClassInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080077
Jeff Brown64a55af2012-08-26 02:47:39 -070078// ----------------------------------------------------------------------------
79
Mathias Agopianb1d90c82013-03-06 17:45:42 -080080// this is just a pointer we use to pass to inc/decStrong
81static const void *sRefBaseOwner;
82
Jeff Brown64a55af2012-08-26 02:47:39 -070083bool android_view_Surface_isInstanceOf(JNIEnv* env, jobject obj) {
84 return env->IsInstanceOf(obj, gSurfaceClassInfo.clazz);
85}
86
Mathias Agopian3866f0d2013-02-11 22:08:48 -080087sp<ANativeWindow> android_view_Surface_getNativeWindow(JNIEnv* env, jobject surfaceObj) {
88 return android_view_Surface_getSurface(env, surfaceObj);
89}
90
Jeff Brown64a55af2012-08-26 02:47:39 -070091sp<Surface> android_view_Surface_getSurface(JNIEnv* env, jobject surfaceObj) {
Mathias Agopian7c116b52013-03-18 20:27:02 -070092 sp<Surface> sur;
93 jobject lock = env->GetObjectField(surfaceObj,
Jeff Brownfc0ebd72013-04-30 16:33:00 -070094 gSurfaceClassInfo.mLock);
Mathias Agopian7c116b52013-03-18 20:27:02 -070095 if (env->MonitorEnter(lock) == JNI_OK) {
96 sur = reinterpret_cast<Surface *>(
Ashok Bhat36bef0b2014-01-20 20:08:01 +000097 env->GetLongField(surfaceObj, gSurfaceClassInfo.mNativeObject));
Mathias Agopian7c116b52013-03-18 20:27:02 -070098 env->MonitorExit(lock);
99 }
Andy McFadden5ad3ab82014-06-11 15:22:32 -0700100 env->DeleteLocalRef(lock);
Mathias Agopian7c116b52013-03-18 20:27:02 -0700101 return sur;
Jeff Browncbad9762012-09-04 21:57:59 -0700102}
103
Jiwen 'Steve' Caid28e8282017-03-31 11:19:50 -0700104jobject android_view_Surface_createFromSurface(JNIEnv* env, const sp<Surface>& surface) {
Chong Zhangaf608e82014-01-29 12:52:15 -0800105 jobject surfaceObj = env->NewObject(gSurfaceClassInfo.clazz,
106 gSurfaceClassInfo.ctor, (jlong)surface.get());
Jeff Browncbad9762012-09-04 21:57:59 -0700107 if (surfaceObj == NULL) {
108 if (env->ExceptionCheck()) {
Andy McFaddend47f7d82012-12-18 09:48:38 -0800109 ALOGE("Could not create instance of Surface from IGraphicBufferProducer.");
Jeff Browncbad9762012-09-04 21:57:59 -0700110 LOGE_EX(env);
111 env->ExceptionClear();
112 }
113 return NULL;
114 }
Mathias Agopianb1d90c82013-03-06 17:45:42 -0800115 surface->incStrong(&sRefBaseOwner);
Jeff Browncbad9762012-09-04 21:57:59 -0700116 return surfaceObj;
117}
118
Jiwen 'Steve' Caid28e8282017-03-31 11:19:50 -0700119jobject 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
Eino-Ville Talvala805f3c92015-02-26 10:57:55 -0800129int android_view_Surface_mapPublicFormatToHalFormat(PublicFormat f) {
Jooyung Han63f5f922019-02-21 14:43:45 +0900130 return mapPublicFormatToHalFormat(f);
Eino-Ville Talvala805f3c92015-02-26 10:57:55 -0800131}
132
133android_dataspace android_view_Surface_mapPublicFormatToHalDataspace(
134 PublicFormat f) {
Jooyung Han63f5f922019-02-21 14:43:45 +0900135 return mapPublicFormatToHalDataspace(f);
Eino-Ville Talvala805f3c92015-02-26 10:57:55 -0800136}
137
138PublicFormat android_view_Surface_mapHalFormatDataspaceToPublicFormat(
139 int format, android_dataspace dataSpace) {
Jooyung Han63f5f922019-02-21 14:43:45 +0900140 return mapHalFormatDataspaceToPublicFormat(format, dataSpace);
Eino-Ville Talvala805f3c92015-02-26 10:57:55 -0800141}
Jeff Brown64a55af2012-08-26 02:47:39 -0700142// ----------------------------------------------------------------------------
143
Mathias Agopianf3d3b4d2013-02-19 18:26:15 -0800144static inline bool isSurfaceValid(const sp<Surface>& sur) {
145 return Surface::isValid(sur);
Mathias Agopian52800612013-02-14 17:11:20 -0800146}
147
148// ----------------------------------------------------------------------------
149
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000150static jlong nativeCreateFromSurfaceTexture(JNIEnv* env, jclass clazz,
Jeff Brown64a55af2012-08-26 02:47:39 -0700151 jobject surfaceTextureObj) {
Mathias Agopian52a9a102013-08-02 01:38:38 -0700152 sp<IGraphicBufferProducer> producer(SurfaceTexture_getProducer(env, surfaceTextureObj));
153 if (producer == NULL) {
Jeff Brown64a55af2012-08-26 02:47:39 -0700154 jniThrowException(env, "java/lang/IllegalArgumentException",
155 "SurfaceTexture has already been released");
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800156 return 0;
Jeff Brown64a55af2012-08-26 02:47:39 -0700157 }
158
Mathias Agopian52a9a102013-08-02 01:38:38 -0700159 sp<Surface> surface(new Surface(producer, true));
Jeff Brown64a55af2012-08-26 02:47:39 -0700160 if (surface == NULL) {
161 jniThrowException(env, OutOfResourcesException, NULL);
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800162 return 0;
Jeff Brown64a55af2012-08-26 02:47:39 -0700163 }
164
Mathias Agopianb1d90c82013-03-06 17:45:42 -0800165 surface->incStrong(&sRefBaseOwner);
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000166 return jlong(surface.get());
Jeff Brown64a55af2012-08-26 02:47:39 -0700167}
168
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000169static void nativeRelease(JNIEnv* env, jclass clazz, jlong nativeObject) {
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800170 sp<Surface> sur(reinterpret_cast<Surface *>(nativeObject));
Mathias Agopianb1d90c82013-03-06 17:45:42 -0800171 sur->decStrong(&sRefBaseOwner);
Jeff Brown64a55af2012-08-26 02:47:39 -0700172}
173
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000174static jboolean nativeIsValid(JNIEnv* env, jclass clazz, jlong nativeObject) {
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800175 sp<Surface> sur(reinterpret_cast<Surface *>(nativeObject));
Mathias Agopian52800612013-02-14 17:11:20 -0800176 return isSurfaceValid(sur) ? JNI_TRUE : JNI_FALSE;
Jeff Brown64a55af2012-08-26 02:47:39 -0700177}
178
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000179static jboolean nativeIsConsumerRunningBehind(JNIEnv* env, jclass clazz, jlong nativeObject) {
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800180 sp<Surface> sur(reinterpret_cast<Surface *>(nativeObject));
Mathias Agopian52800612013-02-14 17:11:20 -0800181 if (!isSurfaceValid(sur)) {
Jeff Brown64a55af2012-08-26 02:47:39 -0700182 doThrowIAE(env);
183 return JNI_FALSE;
184 }
Jeff Brown64a55af2012-08-26 02:47:39 -0700185 int value = 0;
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800186 ANativeWindow* anw = static_cast<ANativeWindow*>(sur.get());
Jeff Brown64a55af2012-08-26 02:47:39 -0700187 anw->query(anw, NATIVE_WINDOW_CONSUMER_RUNNING_BEHIND, &value);
188 return value;
189}
190
Mike Reedb9330552014-06-16 17:31:48 -0400191static inline SkColorType convertPixelFormat(PixelFormat format) {
Jeff Brown64a55af2012-08-26 02:47:39 -0700192 /* note: if PIXEL_FORMAT_RGBX_8888 means that all alpha bytes are 0xFF, then
Mike Reedb9330552014-06-16 17:31:48 -0400193 we can map to kN32_SkColorType, and optionally call
Leon Scroggins III8790be62013-12-03 16:26:51 -0500194 bitmap.setAlphaType(kOpaque_SkAlphaType) on the resulting SkBitmap
195 (as an accelerator)
Jeff Brown64a55af2012-08-26 02:47:39 -0700196 */
197 switch (format) {
Mike Reedb9330552014-06-16 17:31:48 -0400198 case PIXEL_FORMAT_RGBX_8888: return kN32_SkColorType;
199 case PIXEL_FORMAT_RGBA_8888: return kN32_SkColorType;
Romain Guy9505a652016-12-14 09:43:50 -0800200 case PIXEL_FORMAT_RGBA_FP16: return kRGBA_F16_SkColorType;
Mike Reedb9330552014-06-16 17:31:48 -0400201 case PIXEL_FORMAT_RGB_565: return kRGB_565_SkColorType;
202 default: return kUnknown_SkColorType;
Jeff Brown64a55af2012-08-26 02:47:39 -0700203 }
204}
205
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000206static jlong nativeLockCanvas(JNIEnv* env, jclass clazz,
207 jlong nativeObject, jobject canvasObj, jobject dirtyRectObj) {
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800208 sp<Surface> surface(reinterpret_cast<Surface *>(nativeObject));
209
Mathias Agopian52800612013-02-14 17:11:20 -0800210 if (!isSurfaceValid(surface)) {
Jeff Brown64a55af2012-08-26 02:47:39 -0700211 doThrowIAE(env);
Andy McFaddened55c8d2013-08-20 10:05:51 -0700212 return 0;
Jeff Brown64a55af2012-08-26 02:47:39 -0700213 }
214
Pablo Ceballos82702922015-08-07 17:28:03 -0700215 Rect dirtyRect(Rect::EMPTY_RECT);
Mathias Agopian667809e2013-04-17 14:57:41 -0700216 Rect* dirtyRectPtr = NULL;
217
Jeff Brown64a55af2012-08-26 02:47:39 -0700218 if (dirtyRectObj) {
Mathias Agopian667809e2013-04-17 14:57:41 -0700219 dirtyRect.left = env->GetIntField(dirtyRectObj, gRectClassInfo.left);
220 dirtyRect.top = env->GetIntField(dirtyRectObj, gRectClassInfo.top);
221 dirtyRect.right = env->GetIntField(dirtyRectObj, gRectClassInfo.right);
222 dirtyRect.bottom = env->GetIntField(dirtyRectObj, gRectClassInfo.bottom);
223 dirtyRectPtr = &dirtyRect;
Jeff Brown64a55af2012-08-26 02:47:39 -0700224 }
225
Mathias Agopian52800612013-02-14 17:11:20 -0800226 ANativeWindow_Buffer outBuffer;
Mathias Agopian667809e2013-04-17 14:57:41 -0700227 status_t err = surface->lock(&outBuffer, dirtyRectPtr);
Jeff Brown64a55af2012-08-26 02:47:39 -0700228 if (err < 0) {
229 const char* const exception = (err == NO_MEMORY) ?
230 OutOfResourcesException :
231 "java/lang/IllegalArgumentException";
232 jniThrowException(env, exception, NULL);
Andy McFaddened55c8d2013-08-20 10:05:51 -0700233 return 0;
Jeff Brown64a55af2012-08-26 02:47:39 -0700234 }
235
Mike Reedb9330552014-06-16 17:31:48 -0400236 SkImageInfo info = SkImageInfo::Make(outBuffer.width, outBuffer.height,
237 convertPixelFormat(outBuffer.format),
Romain Guy253f2c22016-09-28 17:34:42 -0700238 outBuffer.format == PIXEL_FORMAT_RGBX_8888
Derek Sollenbergerb1002312018-08-02 10:15:09 -0400239 ? kOpaque_SkAlphaType : kPremul_SkAlphaType);
Mike Reedb9330552014-06-16 17:31:48 -0400240
Jeff Brown64a55af2012-08-26 02:47:39 -0700241 SkBitmap bitmap;
Mathias Agopian52800612013-02-14 17:11:20 -0800242 ssize_t bpr = outBuffer.stride * bytesPerPixel(outBuffer.format);
Mike Reedb9330552014-06-16 17:31:48 -0400243 bitmap.setInfo(info, bpr);
Mathias Agopian52800612013-02-14 17:11:20 -0800244 if (outBuffer.width > 0 && outBuffer.height > 0) {
245 bitmap.setPixels(outBuffer.bits);
Jeff Brown64a55af2012-08-26 02:47:39 -0700246 } else {
247 // be safe with an empty bitmap.
248 bitmap.setPixels(NULL);
249 }
Derek Sollenbergerfc615a02012-12-20 14:39:57 -0500250
John Reckc1b33d62015-04-22 09:04:45 -0700251 Canvas* nativeCanvas = GraphicsJNI::getNativeCanvas(env, canvasObj);
252 nativeCanvas->setBitmap(bitmap);
Jeff Brown64a55af2012-08-26 02:47:39 -0700253
Mathias Agopian667809e2013-04-17 14:57:41 -0700254 if (dirtyRectPtr) {
John Reckc1b33d62015-04-22 09:04:45 -0700255 nativeCanvas->clipRect(dirtyRect.left, dirtyRect.top,
Mike Reed6c67f1d2016-12-14 10:29:54 -0500256 dirtyRect.right, dirtyRect.bottom, SkClipOp::kIntersect);
Jeff Brown64a55af2012-08-26 02:47:39 -0700257 }
258
Jeff Brown64a55af2012-08-26 02:47:39 -0700259 if (dirtyRectObj) {
Mathias Agopian667809e2013-04-17 14:57:41 -0700260 env->SetIntField(dirtyRectObj, gRectClassInfo.left, dirtyRect.left);
261 env->SetIntField(dirtyRectObj, gRectClassInfo.top, dirtyRect.top);
262 env->SetIntField(dirtyRectObj, gRectClassInfo.right, dirtyRect.right);
263 env->SetIntField(dirtyRectObj, gRectClassInfo.bottom, dirtyRect.bottom);
Jeff Brown64a55af2012-08-26 02:47:39 -0700264 }
Andy McFaddened55c8d2013-08-20 10:05:51 -0700265
266 // Create another reference to the surface and return it. This reference
267 // should be passed to nativeUnlockCanvasAndPost in place of mNativeObject,
268 // because the latter could be replaced while the surface is locked.
269 sp<Surface> lockedSurface(surface);
270 lockedSurface->incStrong(&sRefBaseOwner);
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000271 return (jlong) lockedSurface.get();
Jeff Brown64a55af2012-08-26 02:47:39 -0700272}
273
Jeff Brownfc0ebd72013-04-30 16:33:00 -0700274static void nativeUnlockCanvasAndPost(JNIEnv* env, jclass clazz,
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000275 jlong nativeObject, jobject canvasObj) {
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800276 sp<Surface> surface(reinterpret_cast<Surface *>(nativeObject));
Mathias Agopian52800612013-02-14 17:11:20 -0800277 if (!isSurfaceValid(surface)) {
Jeff Brown64a55af2012-08-26 02:47:39 -0700278 return;
279 }
280
281 // detach the canvas from the surface
John Reckc1b33d62015-04-22 09:04:45 -0700282 Canvas* nativeCanvas = GraphicsJNI::getNativeCanvas(env, canvasObj);
283 nativeCanvas->setBitmap(SkBitmap());
Jeff Brown64a55af2012-08-26 02:47:39 -0700284
285 // unlock surface
286 status_t err = surface->unlockAndPost();
287 if (err < 0) {
288 doThrowIAE(env);
289 }
290}
291
Dan Stoza5795d642014-06-20 13:01:36 -0700292static void nativeAllocateBuffers(JNIEnv* /* env */ , jclass /* clazz */,
293 jlong nativeObject) {
294 sp<Surface> surface(reinterpret_cast<Surface *>(nativeObject));
295 if (!isSurfaceValid(surface)) {
296 return;
297 }
298
299 surface->allocateBuffers();
300}
301
Mathias Agopian17f638b2009-04-16 20:04:08 -0700302// ----------------------------------------------------------------------------
303
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000304static jlong nativeCreateFromSurfaceControl(JNIEnv* env, jclass clazz,
305 jlong surfaceControlNativeObj) {
Bryce Lee02949f12017-06-16 07:20:34 -0700306 sp<SurfaceControl> ctrl(reinterpret_cast<SurfaceControl *>(surfaceControlNativeObj));
307 sp<Surface> surface(ctrl->createSurface());
308 if (surface != NULL) {
309 surface->incStrong(&sRefBaseOwner);
310 }
311 return reinterpret_cast<jlong>(surface.get());
312}
313
314static jlong nativeGetFromSurfaceControl(JNIEnv* env, jclass clazz,
Robert Carrd5686722019-01-25 14:30:52 -0800315 jlong nativeObject,
Bryce Lee02949f12017-06-16 07:20:34 -0700316 jlong surfaceControlNativeObj) {
Robert Carrd5686722019-01-25 14:30:52 -0800317 Surface* self(reinterpret_cast<Surface *>(nativeObject));
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800318 sp<SurfaceControl> ctrl(reinterpret_cast<SurfaceControl *>(surfaceControlNativeObj));
Robert Carrd5686722019-01-25 14:30:52 -0800319
320 // If the underlying IGBP's are the same, we don't need to do anything.
321 if (self != nullptr &&
322 IInterface::asBinder(self->getIGraphicBufferProducer()) ==
323 IInterface::asBinder(ctrl->getIGraphicBufferProducer())) {
324 return nativeObject;
325 }
326
Jeff Brownfc0ebd72013-04-30 16:33:00 -0700327 sp<Surface> surface(ctrl->getSurface());
328 if (surface != NULL) {
329 surface->incStrong(&sRefBaseOwner);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800330 }
Robert Carrd5686722019-01-25 14:30:52 -0800331
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000332 return reinterpret_cast<jlong>(surface.get());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800333}
334
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000335static jlong nativeReadFromParcel(JNIEnv* env, jclass clazz,
336 jlong nativeObject, jobject parcelObj) {
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800337 Parcel* parcel = parcelForJavaObject(env, parcelObj);
338 if (parcel == NULL) {
339 doThrowNPE(env);
340 return 0;
341 }
Mathias Agopianc3b9cd62013-03-13 16:07:57 -0700342
Eino-Ville Talvala5d2d7782015-12-17 16:50:50 -0800343 android::view::Surface surfaceShim;
344
345 // Calling code in Surface.java has already read the name of the Surface
346 // from the Parcel
347 surfaceShim.readFromParcel(parcel, /*nameAlreadyRead*/true);
348
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800349 sp<Surface> self(reinterpret_cast<Surface *>(nativeObject));
Mathias Agopianc3b9cd62013-03-13 16:07:57 -0700350
351 // update the Surface only if the underlying IGraphicBufferProducer
352 // has changed.
Eino-Ville Talvala5d2d7782015-12-17 16:50:50 -0800353 if (self != nullptr
354 && (IInterface::asBinder(self->getIGraphicBufferProducer()) ==
355 IInterface::asBinder(surfaceShim.graphicBufferProducer))) {
Mathias Agopianc3b9cd62013-03-13 16:07:57 -0700356 // same IGraphicBufferProducer, return ourselves
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000357 return jlong(self.get());
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800358 }
Mathias Agopian4a05f432013-03-12 18:43:34 -0700359
360 sp<Surface> sur;
Eino-Ville Talvala5d2d7782015-12-17 16:50:50 -0800361 if (surfaceShim.graphicBufferProducer != nullptr) {
Mathias Agopianc3b9cd62013-03-13 16:07:57 -0700362 // we have a new IGraphicBufferProducer, create a new Surface for it
Eino-Ville Talvala5d2d7782015-12-17 16:50:50 -0800363 sur = new Surface(surfaceShim.graphicBufferProducer, true);
Mathias Agopianc3b9cd62013-03-13 16:07:57 -0700364 // and keep a reference before passing to java
Mathias Agopianb1d90c82013-03-06 17:45:42 -0800365 sur->incStrong(&sRefBaseOwner);
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800366 }
Mathias Agopian4a05f432013-03-12 18:43:34 -0700367
Mathias Agopianc3b9cd62013-03-13 16:07:57 -0700368 if (self != NULL) {
369 // and loose the java reference to ourselves
370 self->decStrong(&sRefBaseOwner);
371 }
372
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000373 return jlong(sur.get());
Dianne Hackborn61566cc2011-12-02 23:31:52 -0800374}
375
Mathias Agopian29479eb2013-02-14 14:36:04 -0800376static void nativeWriteToParcel(JNIEnv* env, jclass clazz,
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000377 jlong nativeObject, jobject parcelObj) {
Jeff Brown64a55af2012-08-26 02:47:39 -0700378 Parcel* parcel = parcelForJavaObject(env, parcelObj);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800379 if (parcel == NULL) {
Elliott Hughes8451b252011-04-07 19:17:57 -0700380 doThrowNPE(env);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800381 return;
382 }
Mathias Agopian3866f0d2013-02-11 22:08:48 -0800383 sp<Surface> self(reinterpret_cast<Surface *>(nativeObject));
Eino-Ville Talvala5d2d7782015-12-17 16:50:50 -0800384 android::view::Surface surfaceShim;
385 if (self != nullptr) {
386 surfaceShim.graphicBufferProducer = self->getIGraphicBufferProducer();
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);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800391}
392
John Reckb35c9602014-11-13 16:15:08 -0800393static 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
401static 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}
Robert Carrcd9a18c2015-12-16 18:16:21 -0800408
Rob Carr64e516f2015-10-29 00:20:45 +0000409static jlong nativeGetNextFrameNumber(JNIEnv *env, jclass clazz, jlong nativeObject) {
410 Surface* surface = reinterpret_cast<Surface*>(nativeObject);
411 return surface->getNextFrameNumber();
412}
John Reckb35c9602014-11-13 16:15:08 -0800413
Robert Carrcd9a18c2015-12-16 18:16:21 -0800414static jint nativeSetScalingMode(JNIEnv *env, jclass clazz, jlong nativeObject, jint scalingMode) {
415 Surface* surface = reinterpret_cast<Surface*>(nativeObject);
416 return surface->setScalingMode(scalingMode);
417}
418
Robert Carr387838b2016-09-07 14:12:44 -0700419static 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
Robert Carra35e5de2017-03-03 17:04:54 -0800424static jint nativeAttachAndQueueBuffer(JNIEnv *env, jclass clazz, jlong nativeObject,
425 jobject graphicBuffer) {
426 Surface* surface = reinterpret_cast<Surface*>(nativeObject);
427 sp<GraphicBuffer> bp = graphicBufferForJavaObject(env, graphicBuffer);
Chavi Weingartend7ec64c2017-11-30 01:52:01 +0000428 int err = Surface::attachAndQueueBuffer(surface, bp);
Robert Carra35e5de2017-03-03 17:04:54 -0800429 return err;
430}
431
Romain Guy0bbf0b42017-07-20 08:57:07 -0700432static jint nativeSetSharedBufferModeEnabled(JNIEnv* env, jclass clazz, jlong nativeObject,
433 jboolean enabled) {
434 Surface* surface = reinterpret_cast<Surface*>(nativeObject);
Adrian Roosc43dfdf2018-01-29 17:55:44 +0100435 ANativeWindow* anw = static_cast<ANativeWindow*>(surface);
436 return anw->perform(surface, NATIVE_WINDOW_SET_SHARED_BUFFER_MODE, int(enabled));
Romain Guy0bbf0b42017-07-20 08:57:07 -0700437}
438
439static jint nativeSetAutoRefreshEnabled(JNIEnv* env, jclass clazz, jlong nativeObject,
440 jboolean enabled) {
441 Surface* surface = reinterpret_cast<Surface*>(nativeObject);
Adrian Roosc43dfdf2018-01-29 17:55:44 +0100442 ANativeWindow* anw = static_cast<ANativeWindow*>(surface);
443 return anw->perform(surface, NATIVE_WINDOW_SET_AUTO_REFRESH, int(enabled));
Romain Guy0bbf0b42017-07-20 08:57:07 -0700444}
445
John Reckbb2d0cc2014-10-21 10:09:36 -0700446namespace uirenderer {
447
448using namespace android::uirenderer::renderthread;
449
450class ContextFactory : public IContextFactory {
451public:
452 virtual AnimationContext* createAnimationContext(renderthread::TimeLord& clock) {
453 return new AnimationContext(clock);
454 }
455};
456
Peiyong Lind8b68712018-03-06 18:34:21 -0800457static jlong create(JNIEnv* env, jclass clazz, jlong rootNodePtr, jlong surfacePtr,
458 jboolean isWideColorGamut) {
John Reckbb2d0cc2014-10-21 10:09:36 -0700459 RenderNode* rootNode = reinterpret_cast<RenderNode*>(rootNodePtr);
460 sp<Surface> surface(reinterpret_cast<Surface*>(surfacePtr));
461 ContextFactory factory;
462 RenderProxy* proxy = new RenderProxy(false, rootNode, &factory);
463 proxy->loadSystemProperties();
Peiyong Lind8b68712018-03-06 18:34:21 -0800464 if (isWideColorGamut) {
465 proxy->setWideGamut(true);
466 }
Stan Iliev768e3932016-07-08 21:34:52 -0400467 proxy->setSwapBehavior(SwapBehavior::kSwap_discardBuffer);
John Reck8785ceb2018-10-29 16:45:58 -0700468 proxy->setSurface(surface);
John Reckbb2d0cc2014-10-21 10:09:36 -0700469 // Shadows can't be used via this interface, so just set the light source
John Reckab1080c2016-06-21 16:24:20 -0700470 // to all 0s.
John Reck8785ceb2018-10-29 16:45:58 -0700471 proxy->setLightAlpha(0, 0);
472 proxy->setLightGeometry((Vector3){0, 0, 0}, 0);
John Reckbb2d0cc2014-10-21 10:09:36 -0700473 return (jlong) proxy;
474}
475
476static void setSurface(JNIEnv* env, jclass clazz, jlong rendererPtr, jlong surfacePtr) {
477 RenderProxy* proxy = reinterpret_cast<RenderProxy*>(rendererPtr);
478 sp<Surface> surface(reinterpret_cast<Surface*>(surfacePtr));
John Reck8785ceb2018-10-29 16:45:58 -0700479 proxy->setSurface(surface);
John Reckbb2d0cc2014-10-21 10:09:36 -0700480}
481
482static void draw(JNIEnv* env, jclass clazz, jlong rendererPtr) {
483 RenderProxy* proxy = reinterpret_cast<RenderProxy*>(rendererPtr);
John Reckba6adf62015-02-19 14:36:50 -0800484 nsecs_t vsync = systemTime(CLOCK_MONOTONIC);
485 UiFrameInfoBuilder(proxy->frameInfo())
486 .setVsync(vsync, vsync)
Chris Craik1b54fb22015-06-02 17:40:58 -0700487 .addFlag(FrameInfoFlags::SurfaceCanvas);
John Reck2de950d2017-01-25 10:58:30 -0800488 proxy->syncAndDrawFrame();
John Reckbb2d0cc2014-10-21 10:09:36 -0700489}
490
491static void destroy(JNIEnv* env, jclass clazz, jlong rendererPtr) {
492 RenderProxy* proxy = reinterpret_cast<RenderProxy*>(rendererPtr);
493 delete proxy;
494}
495
496} // uirenderer
497
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800498// ----------------------------------------------------------------------------
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800499
John Reckbb2d0cc2014-10-21 10:09:36 -0700500namespace hwui = android::uirenderer;
501
Daniel Micay76f6a862015-09-19 17:31:01 -0400502static const JNINativeMethod gSurfaceMethods[] = {
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000503 {"nativeCreateFromSurfaceTexture", "(Landroid/graphics/SurfaceTexture;)J",
Jeff Brown64a55af2012-08-26 02:47:39 -0700504 (void*)nativeCreateFromSurfaceTexture },
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000505 {"nativeRelease", "(J)V",
Jeff Brown64a55af2012-08-26 02:47:39 -0700506 (void*)nativeRelease },
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000507 {"nativeIsValid", "(J)Z",
Jeff Brown64a55af2012-08-26 02:47:39 -0700508 (void*)nativeIsValid },
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000509 {"nativeIsConsumerRunningBehind", "(J)Z",
Jeff Brown64a55af2012-08-26 02:47:39 -0700510 (void*)nativeIsConsumerRunningBehind },
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000511 {"nativeLockCanvas", "(JLandroid/graphics/Canvas;Landroid/graphics/Rect;)J",
Jeff Brown64a55af2012-08-26 02:47:39 -0700512 (void*)nativeLockCanvas },
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000513 {"nativeUnlockCanvasAndPost", "(JLandroid/graphics/Canvas;)V",
Jeff Brown64a55af2012-08-26 02:47:39 -0700514 (void*)nativeUnlockCanvasAndPost },
Dan Stoza5795d642014-06-20 13:01:36 -0700515 {"nativeAllocateBuffers", "(J)V",
516 (void*)nativeAllocateBuffers },
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000517 {"nativeCreateFromSurfaceControl", "(J)J",
Jeff Brownfc0ebd72013-04-30 16:33:00 -0700518 (void*)nativeCreateFromSurfaceControl },
Robert Carrd5686722019-01-25 14:30:52 -0800519 {"nativeGetFromSurfaceControl", "(JJ)J",
Bryce Lee02949f12017-06-16 07:20:34 -0700520 (void*)nativeGetFromSurfaceControl },
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000521 {"nativeReadFromParcel", "(JLandroid/os/Parcel;)J",
Jeff Brown64a55af2012-08-26 02:47:39 -0700522 (void*)nativeReadFromParcel },
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000523 {"nativeWriteToParcel", "(JLandroid/os/Parcel;)V",
Jeff Brown64a55af2012-08-26 02:47:39 -0700524 (void*)nativeWriteToParcel },
John Reckb35c9602014-11-13 16:15:08 -0800525 {"nativeGetWidth", "(J)I", (void*)nativeGetWidth },
526 {"nativeGetHeight", "(J)I", (void*)nativeGetHeight },
Rob Carr64e516f2015-10-29 00:20:45 +0000527 {"nativeGetNextFrameNumber", "(J)J", (void*)nativeGetNextFrameNumber },
Robert Carrcd9a18c2015-12-16 18:16:21 -0800528 {"nativeSetScalingMode", "(JI)I", (void*)nativeSetScalingMode },
Robert Carr387838b2016-09-07 14:12:44 -0700529 {"nativeForceScopedDisconnect", "(J)I", (void*)nativeForceScopedDisconnect},
Robert Carra35e5de2017-03-03 17:04:54 -0800530 {"nativeAttachAndQueueBuffer", "(JLandroid/graphics/GraphicBuffer;)I", (void*)nativeAttachAndQueueBuffer},
Romain Guy0bbf0b42017-07-20 08:57:07 -0700531 {"nativeSetSharedBufferModeEnabled", "(JZ)I", (void*)nativeSetSharedBufferModeEnabled},
532 {"nativeSetAutoRefreshEnabled", "(JZ)I", (void*)nativeSetAutoRefreshEnabled},
John Reckbb2d0cc2014-10-21 10:09:36 -0700533
534 // HWUI context
Peiyong Lind8b68712018-03-06 18:34:21 -0800535 {"nHwuiCreate", "(JJZ)J", (void*) hwui::create },
John Reckbb2d0cc2014-10-21 10:09:36 -0700536 {"nHwuiSetSurface", "(JJ)V", (void*) hwui::setSurface },
537 {"nHwuiDraw", "(J)V", (void*) hwui::draw },
538 {"nHwuiDestroy", "(J)V", (void*) hwui::destroy },
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800539};
540
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800541int register_android_view_Surface(JNIEnv* env)
542{
Andreas Gampeed6b9df2014-11-20 22:02:20 -0800543 int err = RegisterMethodsOrDie(env, "android/view/Surface",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800544 gSurfaceMethods, NELEM(gSurfaceMethods));
Jeff Brown64a55af2012-08-26 02:47:39 -0700545
Andreas Gampeed6b9df2014-11-20 22:02:20 -0800546 jclass clazz = FindClassOrDie(env, "android/view/Surface");
547 gSurfaceClassInfo.clazz = MakeGlobalRefOrDie(env, clazz);
548 gSurfaceClassInfo.mNativeObject = GetFieldIDOrDie(env,
549 gSurfaceClassInfo.clazz, "mNativeObject", "J");
550 gSurfaceClassInfo.mLock = GetFieldIDOrDie(env,
551 gSurfaceClassInfo.clazz, "mLock", "Ljava/lang/Object;");
552 gSurfaceClassInfo.ctor = GetMethodIDOrDie(env, gSurfaceClassInfo.clazz, "<init>", "(J)V");
Jeff Brown64a55af2012-08-26 02:47:39 -0700553
Andreas Gampeed6b9df2014-11-20 22:02:20 -0800554 clazz = FindClassOrDie(env, "android/graphics/Rect");
555 gRectClassInfo.left = GetFieldIDOrDie(env, clazz, "left", "I");
556 gRectClassInfo.top = GetFieldIDOrDie(env, clazz, "top", "I");
557 gRectClassInfo.right = GetFieldIDOrDie(env, clazz, "right", "I");
558 gRectClassInfo.bottom = GetFieldIDOrDie(env, clazz, "bottom", "I");
Jeff Brown64a55af2012-08-26 02:47:39 -0700559
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800560 return err;
561}
562
563};