• 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 
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 <SkBitmap.h>
29 #include <SkCanvas.h>
30 #include <SkImage.h>
31 
32 #include "android/graphics/GraphicsJNI.h"
33 
34 #include "core_jni_helpers.h"
35 
36 namespace android {
37 
38 // ----------------------------------------------------------------------------
39 // JNI Glue
40 // ----------------------------------------------------------------------------
41 
42 static struct {
43     jmethodID set;
44     jfieldID left;
45     jfieldID top;
46     jfieldID right;
47     jfieldID bottom;
48 } gRectClassInfo;
49 
50 static struct {
51     jfieldID nativeWindow;
52 } gTextureViewClassInfo;
53 
54 #define GET_INT(object, field) \
55     env->GetIntField(object, field)
56 
57 #define GET_LONG(object, field) \
58     env->GetLongField(object, field)
59 
60 #define SET_INT(object, field, value) \
61     env->SetIntField(object, field, value)
62 
63 #define SET_LONG(object, field, value) \
64     env->SetLongField(object, field, value)
65 
66 #define INVOKEV(object, method, ...) \
67     env->CallVoidMethod(object, method, __VA_ARGS__)
68 
69 // ----------------------------------------------------------------------------
70 // Native layer
71 // ----------------------------------------------------------------------------
72 
73 // FIXME: consider exporting this to share (e.g. android_view_Surface.cpp)
convertPixelFormat(const ANativeWindow_Buffer & buffer)74 static inline SkImageInfo convertPixelFormat(const ANativeWindow_Buffer& buffer) {
75     SkColorType colorType = kUnknown_SkColorType;
76     SkAlphaType alphaType = kOpaque_SkAlphaType;
77     switch (buffer.format) {
78         case WINDOW_FORMAT_RGBA_8888:
79             colorType = kN32_SkColorType;
80             alphaType = kPremul_SkAlphaType;
81             break;
82         case WINDOW_FORMAT_RGBX_8888:
83             colorType = kN32_SkColorType;
84             alphaType = kOpaque_SkAlphaType;
85             break;
86         case AHARDWAREBUFFER_FORMAT_R16G16B16A16_FLOAT:
87             colorType = kRGBA_F16_SkColorType;
88             alphaType = kPremul_SkAlphaType;
89             break;
90         case WINDOW_FORMAT_RGB_565:
91             colorType = kRGB_565_SkColorType;
92             alphaType = kOpaque_SkAlphaType;
93             break;
94         default:
95             break;
96     }
97     return SkImageInfo::Make(buffer.width, buffer.height, colorType, alphaType,
98             GraphicsJNI::defaultColorSpace());
99 }
100 
101 /**
102  * This is a private API, and this implementation is also provided in the NDK.
103  * However, the NDK links against android_runtime, which means that using the
104  * NDK implementation would create a circular dependency between the libraries.
105  */
native_window_lock(ANativeWindow * window,ANativeWindow_Buffer * outBuffer,Rect * inOutDirtyBounds)106 static int32_t native_window_lock(ANativeWindow* window, ANativeWindow_Buffer* outBuffer,
107         Rect* inOutDirtyBounds) {
108     return window->perform(window, NATIVE_WINDOW_LOCK, outBuffer, inOutDirtyBounds);
109 }
110 
native_window_unlockAndPost(ANativeWindow * window)111 static int32_t native_window_unlockAndPost(ANativeWindow* window) {
112     return window->perform(window, NATIVE_WINDOW_UNLOCK_AND_POST);
113 }
114 
android_view_TextureView_createNativeWindow(JNIEnv * env,jobject textureView,jobject surface)115 static void android_view_TextureView_createNativeWindow(JNIEnv* env, jobject textureView,
116         jobject surface) {
117 
118     sp<IGraphicBufferProducer> producer(SurfaceTexture_getProducer(env, surface));
119     sp<ANativeWindow> window = new Surface(producer, true);
120 
121     window->incStrong((void*)android_view_TextureView_createNativeWindow);
122     SET_LONG(textureView, gTextureViewClassInfo.nativeWindow, jlong(window.get()));
123 }
124 
android_view_TextureView_destroyNativeWindow(JNIEnv * env,jobject textureView)125 static void android_view_TextureView_destroyNativeWindow(JNIEnv* env, jobject textureView) {
126 
127     ANativeWindow* nativeWindow = (ANativeWindow*)
128             GET_LONG(textureView, gTextureViewClassInfo.nativeWindow);
129 
130     if (nativeWindow) {
131         sp<ANativeWindow> window(nativeWindow);
132             window->decStrong((void*)android_view_TextureView_createNativeWindow);
133         SET_LONG(textureView, gTextureViewClassInfo.nativeWindow, 0);
134     }
135 }
136 
android_view_TextureView_lockCanvas(JNIEnv * env,jobject,jlong nativeWindow,jobject canvas,jobject dirtyRect)137 static jboolean android_view_TextureView_lockCanvas(JNIEnv* env, jobject,
138         jlong nativeWindow, jobject canvas, jobject dirtyRect) {
139 
140     if (!nativeWindow) {
141         return JNI_FALSE;
142     }
143 
144     ANativeWindow_Buffer buffer;
145 
146     Rect rect(Rect::EMPTY_RECT);
147     if (dirtyRect) {
148         rect.left = GET_INT(dirtyRect, gRectClassInfo.left);
149         rect.top = GET_INT(dirtyRect, gRectClassInfo.top);
150         rect.right = GET_INT(dirtyRect, gRectClassInfo.right);
151         rect.bottom = GET_INT(dirtyRect, gRectClassInfo.bottom);
152     } else {
153         rect.set(Rect(0x3FFF, 0x3FFF));
154     }
155 
156     sp<ANativeWindow> window((ANativeWindow*) nativeWindow);
157     int32_t status = native_window_lock(window.get(), &buffer, &rect);
158     if (status) return JNI_FALSE;
159 
160     ssize_t bytesCount = buffer.stride * bytesPerPixel(buffer.format);
161 
162     SkBitmap bitmap;
163     bitmap.setInfo(convertPixelFormat(buffer), bytesCount);
164 
165     if (buffer.width > 0 && buffer.height > 0) {
166         bitmap.setPixels(buffer.bits);
167     } else {
168         bitmap.setPixels(NULL);
169     }
170 
171     Canvas* nativeCanvas = GraphicsJNI::getNativeCanvas(env, canvas);
172     nativeCanvas->setBitmap(bitmap);
173     nativeCanvas->clipRect(rect.left, rect.top, rect.right, rect.bottom,
174             SkClipOp::kIntersect);
175 
176     if (dirtyRect) {
177         INVOKEV(dirtyRect, gRectClassInfo.set,
178                 int(rect.left), int(rect.top), int(rect.right), int(rect.bottom));
179     }
180 
181     return JNI_TRUE;
182 }
183 
android_view_TextureView_unlockCanvasAndPost(JNIEnv * env,jobject,jlong nativeWindow,jobject canvas)184 static void android_view_TextureView_unlockCanvasAndPost(JNIEnv* env, jobject,
185         jlong nativeWindow, jobject canvas) {
186 
187     Canvas* nativeCanvas = GraphicsJNI::getNativeCanvas(env, canvas);
188     nativeCanvas->setBitmap(SkBitmap());
189 
190     if (nativeWindow) {
191         sp<ANativeWindow> window((ANativeWindow*) nativeWindow);
192         native_window_unlockAndPost(window.get());
193     }
194 }
195 
196 // ----------------------------------------------------------------------------
197 // JNI Glue
198 // ----------------------------------------------------------------------------
199 
200 const char* const kClassPathName = "android/view/TextureView";
201 
202 static const JNINativeMethod gMethods[] = {
203     {   "nCreateNativeWindow", "(Landroid/graphics/SurfaceTexture;)V",
204             (void*) android_view_TextureView_createNativeWindow },
205     {   "nDestroyNativeWindow", "()V",
206             (void*) android_view_TextureView_destroyNativeWindow },
207 
208     {   "nLockCanvas", "(JLandroid/graphics/Canvas;Landroid/graphics/Rect;)Z",
209             (void*) android_view_TextureView_lockCanvas },
210     {   "nUnlockCanvasAndPost", "(JLandroid/graphics/Canvas;)V",
211             (void*) android_view_TextureView_unlockCanvasAndPost },
212 };
213 
register_android_view_TextureView(JNIEnv * env)214 int register_android_view_TextureView(JNIEnv* env) {
215     jclass clazz = FindClassOrDie(env, "android/graphics/Rect");
216     gRectClassInfo.set = GetMethodIDOrDie(env, clazz, "set", "(IIII)V");
217     gRectClassInfo.left = GetFieldIDOrDie(env, clazz, "left", "I");
218     gRectClassInfo.top = GetFieldIDOrDie(env, clazz, "top", "I");
219     gRectClassInfo.right = GetFieldIDOrDie(env, clazz, "right", "I");
220     gRectClassInfo.bottom = GetFieldIDOrDie(env, clazz, "bottom", "I");
221 
222     clazz = FindClassOrDie(env, "android/view/TextureView");
223     gTextureViewClassInfo.nativeWindow = GetFieldIDOrDie(env, clazz, "mNativeWindow", "J");
224 
225     return RegisterMethodsOrDie(env, kClassPathName, gMethods, NELEM(gMethods));
226 }
227 
228 };
229