• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #undef ANDROID_UTILS_REF_BASE_DISABLE_IMPLICIT_CONSTRUCTION // TODO:remove this and fix code
17 
18 #include "jni.h"
19 #include <nativehelper/JNIHelp.h>
20 #include <android_runtime/AndroidRuntime.h>
21 #include <android_runtime/android_graphics_SurfaceTexture.h>
22 
23 #include <ui/Region.h>
24 #include <ui/Rect.h>
25 
26 #include <gui/GLConsumer.h>
27 #include <gui/Surface.h>
28 
29 #include <android/graphics/canvas.h>
30 
31 #include "core_jni_helpers.h"
32 
33 namespace android {
34 
35 // ----------------------------------------------------------------------------
36 // JNI Glue
37 // ----------------------------------------------------------------------------
38 
39 static struct {
40     jmethodID set;
41     jfieldID left;
42     jfieldID top;
43     jfieldID right;
44     jfieldID bottom;
45 } gRectClassInfo;
46 
47 static struct {
48     jfieldID nativeWindow;
49 } gTextureViewClassInfo;
50 
51 #define GET_INT(object, field) \
52     env->GetIntField(object, field)
53 
54 #define GET_LONG(object, field) \
55     env->GetLongField(object, field)
56 
57 #define SET_INT(object, field, value) \
58     env->SetIntField(object, field, value)
59 
60 #define SET_LONG(object, field, value) \
61     env->SetLongField(object, field, value)
62 
63 #define INVOKEV(object, method, ...) \
64     env->CallVoidMethod(object, method, __VA_ARGS__)
65 
66 // ----------------------------------------------------------------------------
67 // Native layer
68 // ----------------------------------------------------------------------------
69 
70 /**
71  * This is a private API, and this implementation is also provided in the NDK.
72  * However, the NDK links against android_runtime, which means that using the
73  * NDK implementation would create a circular dependency between the libraries.
74  */
native_window_lock(ANativeWindow * window,ANativeWindow_Buffer * outBuffer,Rect * inOutDirtyBounds)75 static int32_t native_window_lock(ANativeWindow* window, ANativeWindow_Buffer* outBuffer,
76         Rect* inOutDirtyBounds) {
77     return window->perform(window, NATIVE_WINDOW_LOCK, outBuffer, inOutDirtyBounds);
78 }
79 
native_window_unlockAndPost(ANativeWindow * window)80 static int32_t native_window_unlockAndPost(ANativeWindow* window) {
81     return window->perform(window, NATIVE_WINDOW_UNLOCK_AND_POST);
82 }
83 
android_view_TextureView_createNativeWindow(JNIEnv * env,jobject textureView,jobject surface)84 static void android_view_TextureView_createNativeWindow(JNIEnv* env, jobject textureView,
85         jobject surface) {
86 
87     sp<IGraphicBufferProducer> producer(SurfaceTexture_getProducer(env, surface));
88     sp<ANativeWindow> window = sp<Surface>::make(producer, true);
89 
90     window->incStrong((void*)android_view_TextureView_createNativeWindow);
91     SET_LONG(textureView, gTextureViewClassInfo.nativeWindow, jlong(window.get()));
92 }
93 
android_view_TextureView_destroyNativeWindow(JNIEnv * env,jobject textureView)94 static void android_view_TextureView_destroyNativeWindow(JNIEnv* env, jobject textureView) {
95 
96     ANativeWindow* nativeWindow = (ANativeWindow*)
97             GET_LONG(textureView, gTextureViewClassInfo.nativeWindow);
98 
99     if (nativeWindow) {
100         sp<ANativeWindow> window(nativeWindow);
101             window->decStrong((void*)android_view_TextureView_createNativeWindow);
102         SET_LONG(textureView, gTextureViewClassInfo.nativeWindow, 0);
103     }
104 }
105 
android_view_TextureView_lockCanvas(JNIEnv * env,jobject,jlong nativeWindow,jobject canvasObj,jobject dirtyRect)106 static jboolean android_view_TextureView_lockCanvas(JNIEnv* env, jobject,
107         jlong nativeWindow, jobject canvasObj, jobject dirtyRect) {
108 
109     if (!nativeWindow) {
110         return JNI_FALSE;
111     }
112 
113     Rect rect(Rect::EMPTY_RECT);
114     if (dirtyRect) {
115         rect.left = GET_INT(dirtyRect, gRectClassInfo.left);
116         rect.top = GET_INT(dirtyRect, gRectClassInfo.top);
117         rect.right = GET_INT(dirtyRect, gRectClassInfo.right);
118         rect.bottom = GET_INT(dirtyRect, gRectClassInfo.bottom);
119     } else {
120         rect.set(Rect(0x3FFF, 0x3FFF));
121     }
122 
123     ANativeWindow_Buffer outBuffer;
124     sp<ANativeWindow> window((ANativeWindow*) nativeWindow);
125     int32_t status = native_window_lock(window.get(), &outBuffer, &rect);
126     if (status) return JNI_FALSE;
127 
128     graphics::Canvas canvas(env, canvasObj);
129     canvas.setBuffer(&outBuffer, ANativeWindow_getBuffersDataSpace(window.get()));
130     canvas.clipRect({rect.left, rect.top, rect.right, rect.bottom});
131 
132     if (dirtyRect) {
133         INVOKEV(dirtyRect, gRectClassInfo.set,
134                 int(rect.left), int(rect.top), int(rect.right), int(rect.bottom));
135     }
136 
137     return JNI_TRUE;
138 }
139 
android_view_TextureView_unlockCanvasAndPost(JNIEnv * env,jobject,jlong nativeWindow,jobject canvasObj)140 static void android_view_TextureView_unlockCanvasAndPost(JNIEnv* env, jobject,
141         jlong nativeWindow, jobject canvasObj) {
142 
143     // release the buffer from the canvas
144     graphics::Canvas canvas(env, canvasObj);
145     canvas.setBuffer(nullptr, ADATASPACE_UNKNOWN);
146 
147     if (nativeWindow) {
148         sp<ANativeWindow> window((ANativeWindow*) nativeWindow);
149         native_window_unlockAndPost(window.get());
150     }
151 }
152 
153 // ----------------------------------------------------------------------------
154 // JNI Glue
155 // ----------------------------------------------------------------------------
156 
157 const char* const kClassPathName = "android/view/TextureView";
158 
159 static const JNINativeMethod gMethods[] = {
nCreateNativeWindow(Landroid/graphics/SurfaceTexture;)160     {   "nCreateNativeWindow", "(Landroid/graphics/SurfaceTexture;)V",
161             (void*) android_view_TextureView_createNativeWindow },
nDestroyNativeWindow()162     {   "nDestroyNativeWindow", "()V",
163             (void*) android_view_TextureView_destroyNativeWindow },
164 
nLockCanvas(JLandroid/graphics/Canvas;Landroid/graphics/Rect;)165     {   "nLockCanvas", "(JLandroid/graphics/Canvas;Landroid/graphics/Rect;)Z",
166             (void*) android_view_TextureView_lockCanvas },
nUnlockCanvasAndPost(JLandroid/graphics/Canvas;)167     {   "nUnlockCanvasAndPost", "(JLandroid/graphics/Canvas;)V",
168             (void*) android_view_TextureView_unlockCanvasAndPost },
169 };
170 
register_android_view_TextureView(JNIEnv * env)171 int register_android_view_TextureView(JNIEnv* env) {
172     jclass clazz = FindClassOrDie(env, "android/graphics/Rect");
173     gRectClassInfo.set = GetMethodIDOrDie(env, clazz, "set", "(IIII)V");
174     gRectClassInfo.left = GetFieldIDOrDie(env, clazz, "left", "I");
175     gRectClassInfo.top = GetFieldIDOrDie(env, clazz, "top", "I");
176     gRectClassInfo.right = GetFieldIDOrDie(env, clazz, "right", "I");
177     gRectClassInfo.bottom = GetFieldIDOrDie(env, clazz, "bottom", "I");
178 
179     clazz = FindClassOrDie(env, "android/view/TextureView");
180     gTextureViewClassInfo.nativeWindow = GetFieldIDOrDie(env, clazz, "mNativeWindow", "J");
181 
182     return RegisterMethodsOrDie(env, kClassPathName, gMethods, NELEM(gMethods));
183 }
184 
185 };
186