blob: 391f515af1156266d7a7c64dd8bbfa1769773234 [file] [log] [blame]
Romain Guy8f0095c2011-05-02 17:24:22 -07001/*
2 * Copyright (C) 2011 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#include "jni.h"
18#include <nativehelper/JNIHelp.h>
19#include <android_runtime/AndroidRuntime.h>
Romain Guy035f10102011-06-15 17:57:28 -070020#include <android_runtime/android_graphics_SurfaceTexture.h>
Romain Guy8f0095c2011-05-02 17:24:22 -070021
Romain Guy6be3d552011-07-14 18:08:37 -070022#include <ui/Region.h>
23#include <ui/Rect.h>
24
Andy McFaddend47f7d82012-12-18 09:48:38 -080025#include <gui/GLConsumer.h>
Mathias Agopian52800612013-02-14 17:11:20 -080026#include <gui/Surface.h>
Romain Guy6be3d552011-07-14 18:08:37 -070027
Derek Sollenbergerc287a772019-08-02 13:44:31 -040028#include <android/graphics/canvas.h>
Florin Malita5c3d9272014-05-08 10:35:36 -040029
Andreas Gampe987f79f2014-11-18 17:29:46 -080030#include "core_jni_helpers.h"
31
Romain Guy8f0095c2011-05-02 17:24:22 -070032namespace android {
33
34// ----------------------------------------------------------------------------
Romain Guy6be3d552011-07-14 18:08:37 -070035// JNI Glue
36// ----------------------------------------------------------------------------
37
38static struct {
39 jmethodID set;
40 jfieldID left;
41 jfieldID top;
42 jfieldID right;
43 jfieldID bottom;
44} gRectClassInfo;
45
46static struct {
Romain Guy6be3d552011-07-14 18:08:37 -070047 jfieldID nativeWindow;
48} gTextureViewClassInfo;
49
50#define GET_INT(object, field) \
51 env->GetIntField(object, field)
52
Ashok Bhat36bef0b2014-01-20 20:08:01 +000053#define GET_LONG(object, field) \
54 env->GetLongField(object, field)
55
Romain Guy6be3d552011-07-14 18:08:37 -070056#define SET_INT(object, field, value) \
57 env->SetIntField(object, field, value)
58
Ashok Bhat36bef0b2014-01-20 20:08:01 +000059#define SET_LONG(object, field, value) \
60 env->SetLongField(object, field, value)
61
Romain Guy6be3d552011-07-14 18:08:37 -070062#define INVOKEV(object, method, ...) \
63 env->CallVoidMethod(object, method, __VA_ARGS__)
64
65// ----------------------------------------------------------------------------
Romain Guy8f0095c2011-05-02 17:24:22 -070066// Native layer
67// ----------------------------------------------------------------------------
68
Romain Guy6be3d552011-07-14 18:08:37 -070069/**
70 * This is a private API, and this implementation is also provided in the NDK.
71 * However, the NDK links against android_runtime, which means that using the
72 * NDK implementation would create a circular dependency between the libraries.
73 */
74static int32_t native_window_lock(ANativeWindow* window, ANativeWindow_Buffer* outBuffer,
75 Rect* inOutDirtyBounds) {
76 return window->perform(window, NATIVE_WINDOW_LOCK, outBuffer, inOutDirtyBounds);
77}
78
79static int32_t native_window_unlockAndPost(ANativeWindow* window) {
80 return window->perform(window, NATIVE_WINDOW_UNLOCK_AND_POST);
81}
82
83static void android_view_TextureView_createNativeWindow(JNIEnv* env, jobject textureView,
84 jobject surface) {
85
Mathias Agopian52a9a102013-08-02 01:38:38 -070086 sp<IGraphicBufferProducer> producer(SurfaceTexture_getProducer(env, surface));
Mathias Agopian24f43c92013-08-06 20:16:12 -070087 sp<ANativeWindow> window = new Surface(producer, true);
Romain Guy6be3d552011-07-14 18:08:37 -070088
Mathias Agopianb1d90c82013-03-06 17:45:42 -080089 window->incStrong((void*)android_view_TextureView_createNativeWindow);
Ashok Bhat36bef0b2014-01-20 20:08:01 +000090 SET_LONG(textureView, gTextureViewClassInfo.nativeWindow, jlong(window.get()));
Romain Guy6be3d552011-07-14 18:08:37 -070091}
92
93static void android_view_TextureView_destroyNativeWindow(JNIEnv* env, jobject textureView) {
94
95 ANativeWindow* nativeWindow = (ANativeWindow*)
Ashok Bhat36bef0b2014-01-20 20:08:01 +000096 GET_LONG(textureView, gTextureViewClassInfo.nativeWindow);
Romain Guy6be3d552011-07-14 18:08:37 -070097
98 if (nativeWindow) {
99 sp<ANativeWindow> window(nativeWindow);
Mathias Agopianb1d90c82013-03-06 17:45:42 -0800100 window->decStrong((void*)android_view_TextureView_createNativeWindow);
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000101 SET_LONG(textureView, gTextureViewClassInfo.nativeWindow, 0);
Romain Guy6be3d552011-07-14 18:08:37 -0700102 }
103}
104
Romain Guy53bacf52013-04-30 11:30:10 -0700105static jboolean android_view_TextureView_lockCanvas(JNIEnv* env, jobject,
Derek Sollenbergerc287a772019-08-02 13:44:31 -0400106 jlong nativeWindow, jobject canvasObj, jobject dirtyRect) {
Romain Guy6be3d552011-07-14 18:08:37 -0700107
108 if (!nativeWindow) {
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000109 return JNI_FALSE;
Romain Guy6be3d552011-07-14 18:08:37 -0700110 }
111
Pablo Ceballos82702922015-08-07 17:28:03 -0700112 Rect rect(Rect::EMPTY_RECT);
Romain Guy6be3d552011-07-14 18:08:37 -0700113 if (dirtyRect) {
114 rect.left = GET_INT(dirtyRect, gRectClassInfo.left);
115 rect.top = GET_INT(dirtyRect, gRectClassInfo.top);
116 rect.right = GET_INT(dirtyRect, gRectClassInfo.right);
117 rect.bottom = GET_INT(dirtyRect, gRectClassInfo.bottom);
118 } else {
119 rect.set(Rect(0x3FFF, 0x3FFF));
120 }
121
Derek Sollenbergerc287a772019-08-02 13:44:31 -0400122 ANativeWindow_Buffer outBuffer;
Romain Guy6be3d552011-07-14 18:08:37 -0700123 sp<ANativeWindow> window((ANativeWindow*) nativeWindow);
Derek Sollenbergerc287a772019-08-02 13:44:31 -0400124 int32_t status = native_window_lock(window.get(), &outBuffer, &rect);
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000125 if (status) return JNI_FALSE;
Romain Guy6be3d552011-07-14 18:08:37 -0700126
Derek Sollenberger9ca5bbe2019-08-14 15:50:59 -0400127 graphics::Canvas canvas(env, canvasObj);
128 canvas.setBuffer(&outBuffer, ANativeWindow_getBuffersDataSpace(window.get()));
129 canvas.clipRect({rect.left, rect.top, rect.right, rect.bottom});
Romain Guy6be3d552011-07-14 18:08:37 -0700130
131 if (dirtyRect) {
132 INVOKEV(dirtyRect, gRectClassInfo.set,
133 int(rect.left), int(rect.top), int(rect.right), int(rect.bottom));
134 }
Romain Guy53bacf52013-04-30 11:30:10 -0700135
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000136 return JNI_TRUE;
Romain Guy6be3d552011-07-14 18:08:37 -0700137}
138
139static void android_view_TextureView_unlockCanvasAndPost(JNIEnv* env, jobject,
Derek Sollenbergerc287a772019-08-02 13:44:31 -0400140 jlong nativeWindow, jobject canvasObj) {
Romain Guy6be3d552011-07-14 18:08:37 -0700141
Derek Sollenbergerc287a772019-08-02 13:44:31 -0400142 // release the buffer from the canvas
Derek Sollenberger9ca5bbe2019-08-14 15:50:59 -0400143 graphics::Canvas canvas(env, canvasObj);
144 canvas.setBuffer(nullptr, ADATASPACE_UNKNOWN);
Romain Guy6be3d552011-07-14 18:08:37 -0700145
146 if (nativeWindow) {
147 sp<ANativeWindow> window((ANativeWindow*) nativeWindow);
148 native_window_unlockAndPost(window.get());
149 }
150}
151
Romain Guy8f0095c2011-05-02 17:24:22 -0700152// ----------------------------------------------------------------------------
153// JNI Glue
154// ----------------------------------------------------------------------------
155
156const char* const kClassPathName = "android/view/TextureView";
157
Daniel Micay76f6a862015-09-19 17:31:01 -0400158static const JNINativeMethod gMethods[] = {
Romain Guy6be3d552011-07-14 18:08:37 -0700159 { "nCreateNativeWindow", "(Landroid/graphics/SurfaceTexture;)V",
160 (void*) android_view_TextureView_createNativeWindow },
161 { "nDestroyNativeWindow", "()V",
162 (void*) android_view_TextureView_destroyNativeWindow },
163
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000164 { "nLockCanvas", "(JLandroid/graphics/Canvas;Landroid/graphics/Rect;)Z",
Romain Guy6be3d552011-07-14 18:08:37 -0700165 (void*) android_view_TextureView_lockCanvas },
Ashok Bhat36bef0b2014-01-20 20:08:01 +0000166 { "nUnlockCanvasAndPost", "(JLandroid/graphics/Canvas;)V",
Romain Guy6be3d552011-07-14 18:08:37 -0700167 (void*) android_view_TextureView_unlockCanvasAndPost },
Romain Guy8f0095c2011-05-02 17:24:22 -0700168};
169
170int register_android_view_TextureView(JNIEnv* env) {
Andreas Gampe987f79f2014-11-18 17:29:46 -0800171 jclass clazz = FindClassOrDie(env, "android/graphics/Rect");
172 gRectClassInfo.set = GetMethodIDOrDie(env, clazz, "set", "(IIII)V");
173 gRectClassInfo.left = GetFieldIDOrDie(env, clazz, "left", "I");
174 gRectClassInfo.top = GetFieldIDOrDie(env, clazz, "top", "I");
175 gRectClassInfo.right = GetFieldIDOrDie(env, clazz, "right", "I");
176 gRectClassInfo.bottom = GetFieldIDOrDie(env, clazz, "bottom", "I");
Romain Guy6be3d552011-07-14 18:08:37 -0700177
Andreas Gampe987f79f2014-11-18 17:29:46 -0800178 clazz = FindClassOrDie(env, "android/view/TextureView");
179 gTextureViewClassInfo.nativeWindow = GetFieldIDOrDie(env, clazz, "mNativeWindow", "J");
Romain Guy6be3d552011-07-14 18:08:37 -0700180
Andreas Gampe987f79f2014-11-18 17:29:46 -0800181 return RegisterMethodsOrDie(env, kClassPathName, gMethods, NELEM(gMethods));
Romain Guy8f0095c2011-05-02 17:24:22 -0700182}
183
184};