1 /*
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>
20 #include <android_runtime/android_graphics_SurfaceTexture.h>
21
22 #include <ui/Region.h>
23 #include <ui/Rect.h>
24
25 #include <gui/GLConsumer.h>
26 #include <gui/Surface.h>
27
28 #include <android/graphics/canvas.h>
29
30 #include "core_jni_helpers.h"
31
32 namespace android {
33
34 // ----------------------------------------------------------------------------
35 // JNI Glue
36 // ----------------------------------------------------------------------------
37
38 static struct {
39 jmethodID set;
40 jfieldID left;
41 jfieldID top;
42 jfieldID right;
43 jfieldID bottom;
44 } gRectClassInfo;
45
46 static struct {
47 jfieldID nativeWindow;
48 } gTextureViewClassInfo;
49
50 #define GET_INT(object, field) \
51 env->GetIntField(object, field)
52
53 #define GET_LONG(object, field) \
54 env->GetLongField(object, field)
55
56 #define SET_INT(object, field, value) \
57 env->SetIntField(object, field, value)
58
59 #define SET_LONG(object, field, value) \
60 env->SetLongField(object, field, value)
61
62 #define INVOKEV(object, method, ...) \
63 env->CallVoidMethod(object, method, __VA_ARGS__)
64
65 // ----------------------------------------------------------------------------
66 // Native layer
67 // ----------------------------------------------------------------------------
68
69 /**
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 */
native_window_lock(ANativeWindow * window,ANativeWindow_Buffer * outBuffer,Rect * inOutDirtyBounds)74 static 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
native_window_unlockAndPost(ANativeWindow * window)79 static int32_t native_window_unlockAndPost(ANativeWindow* window) {
80 return window->perform(window, NATIVE_WINDOW_UNLOCK_AND_POST);
81 }
82
android_view_TextureView_createNativeWindow(JNIEnv * env,jobject textureView,jobject surface)83 static void android_view_TextureView_createNativeWindow(JNIEnv* env, jobject textureView,
84 jobject surface) {
85
86 sp<IGraphicBufferProducer> producer(SurfaceTexture_getProducer(env, surface));
87 sp<ANativeWindow> window = new Surface(producer, true);
88
89 window->incStrong((void*)android_view_TextureView_createNativeWindow);
90 SET_LONG(textureView, gTextureViewClassInfo.nativeWindow, jlong(window.get()));
91 }
92
android_view_TextureView_destroyNativeWindow(JNIEnv * env,jobject textureView)93 static void android_view_TextureView_destroyNativeWindow(JNIEnv* env, jobject textureView) {
94
95 ANativeWindow* nativeWindow = (ANativeWindow*)
96 GET_LONG(textureView, gTextureViewClassInfo.nativeWindow);
97
98 if (nativeWindow) {
99 sp<ANativeWindow> window(nativeWindow);
100 window->decStrong((void*)android_view_TextureView_createNativeWindow);
101 SET_LONG(textureView, gTextureViewClassInfo.nativeWindow, 0);
102 }
103 }
104
android_view_TextureView_lockCanvas(JNIEnv * env,jobject,jlong nativeWindow,jobject canvasObj,jobject dirtyRect)105 static jboolean android_view_TextureView_lockCanvas(JNIEnv* env, jobject,
106 jlong nativeWindow, jobject canvasObj, jobject dirtyRect) {
107
108 if (!nativeWindow) {
109 return JNI_FALSE;
110 }
111
112 Rect rect(Rect::EMPTY_RECT);
113 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
122 ANativeWindow_Buffer outBuffer;
123 sp<ANativeWindow> window((ANativeWindow*) nativeWindow);
124 int32_t status = native_window_lock(window.get(), &outBuffer, &rect);
125 if (status) return JNI_FALSE;
126
127 graphics::Canvas canvas(env, canvasObj);
128 canvas.setBuffer(&outBuffer, ANativeWindow_getBuffersDataSpace(window.get()));
129 canvas.clipRect({rect.left, rect.top, rect.right, rect.bottom});
130
131 if (dirtyRect) {
132 INVOKEV(dirtyRect, gRectClassInfo.set,
133 int(rect.left), int(rect.top), int(rect.right), int(rect.bottom));
134 }
135
136 return JNI_TRUE;
137 }
138
android_view_TextureView_unlockCanvasAndPost(JNIEnv * env,jobject,jlong nativeWindow,jobject canvasObj)139 static void android_view_TextureView_unlockCanvasAndPost(JNIEnv* env, jobject,
140 jlong nativeWindow, jobject canvasObj) {
141
142 // release the buffer from the canvas
143 graphics::Canvas canvas(env, canvasObj);
144 canvas.setBuffer(nullptr, ADATASPACE_UNKNOWN);
145
146 if (nativeWindow) {
147 sp<ANativeWindow> window((ANativeWindow*) nativeWindow);
148 native_window_unlockAndPost(window.get());
149 }
150 }
151
152 // ----------------------------------------------------------------------------
153 // JNI Glue
154 // ----------------------------------------------------------------------------
155
156 const char* const kClassPathName = "android/view/TextureView";
157
158 static const JNINativeMethod gMethods[] = {
159 { "nCreateNativeWindow", "(Landroid/graphics/SurfaceTexture;)V",
160 (void*) android_view_TextureView_createNativeWindow },
161 { "nDestroyNativeWindow", "()V",
162 (void*) android_view_TextureView_destroyNativeWindow },
163
164 { "nLockCanvas", "(JLandroid/graphics/Canvas;Landroid/graphics/Rect;)Z",
165 (void*) android_view_TextureView_lockCanvas },
166 { "nUnlockCanvasAndPost", "(JLandroid/graphics/Canvas;)V",
167 (void*) android_view_TextureView_unlockCanvasAndPost },
168 };
169
register_android_view_TextureView(JNIEnv * env)170 int register_android_view_TextureView(JNIEnv* env) {
171 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");
177
178 clazz = FindClassOrDie(env, "android/view/TextureView");
179 gTextureViewClassInfo.nativeWindow = GetFieldIDOrDie(env, clazz, "mNativeWindow", "J");
180
181 return RegisterMethodsOrDie(env, kClassPathName, gMethods, NELEM(gMethods));
182 }
183
184 };
185