2 * Copyright (C) 2011 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include <nativehelper/JNIHelp.h>
19 #include <android_runtime/AndroidRuntime.h>
20 #include <android_runtime/android_graphics_SurfaceTexture.h>
22 #include <ui/Region.h>
25 #include <gui/GLConsumer.h>
26 #include <gui/Surface.h>
32 #include "android/graphics/GraphicsJNI.h"
34 #include "core_jni_helpers.h"
38 // ----------------------------------------------------------------------------
40 // ----------------------------------------------------------------------------
51 jfieldID nativeWindow;
52 } gTextureViewClassInfo;
54 #define GET_INT(object, field) \
55 env->GetIntField(object, field)
57 #define GET_LONG(object, field) \
58 env->GetLongField(object, field)
60 #define SET_INT(object, field, value) \
61 env->SetIntField(object, field, value)
63 #define SET_LONG(object, field, value) \
64 env->SetLongField(object, field, value)
66 #define INVOKEV(object, method, ...) \
67 env->CallVoidMethod(object, method, __VA_ARGS__)
69 // ----------------------------------------------------------------------------
71 // ----------------------------------------------------------------------------
73 // FIXME: consider exporting this to share (e.g. android_view_Surface.cpp)
74 static inline SkImageInfo convertPixelFormat(const ANativeWindow_Buffer& buffer) {
75 SkColorType ct = kUnknown_SkColorType;
76 SkAlphaType at = kOpaque_SkAlphaType;
77 switch (buffer.format) {
78 case WINDOW_FORMAT_RGBA_8888:
79 ct = kN32_SkColorType;
80 at = kPremul_SkAlphaType;
82 case WINDOW_FORMAT_RGBX_8888:
83 ct = kN32_SkColorType;
84 at = kOpaque_SkAlphaType;
86 case WINDOW_FORMAT_RGB_565:
87 ct = kRGB_565_SkColorType;
88 at = kOpaque_SkAlphaType;
93 return SkImageInfo::Make(buffer.width, buffer.height, ct, at);
97 * This is a private API, and this implementation is also provided in the NDK.
98 * However, the NDK links against android_runtime, which means that using the
99 * NDK implementation would create a circular dependency between the libraries.
101 static int32_t native_window_lock(ANativeWindow* window, ANativeWindow_Buffer* outBuffer,
102 Rect* inOutDirtyBounds) {
103 return window->perform(window, NATIVE_WINDOW_LOCK, outBuffer, inOutDirtyBounds);
106 static int32_t native_window_unlockAndPost(ANativeWindow* window) {
107 return window->perform(window, NATIVE_WINDOW_UNLOCK_AND_POST);
110 static void android_view_TextureView_createNativeWindow(JNIEnv* env, jobject textureView,
113 sp<IGraphicBufferProducer> producer(SurfaceTexture_getProducer(env, surface));
114 sp<ANativeWindow> window = new Surface(producer, true);
116 window->incStrong((void*)android_view_TextureView_createNativeWindow);
117 SET_LONG(textureView, gTextureViewClassInfo.nativeWindow, jlong(window.get()));
120 static void android_view_TextureView_destroyNativeWindow(JNIEnv* env, jobject textureView) {
122 ANativeWindow* nativeWindow = (ANativeWindow*)
123 GET_LONG(textureView, gTextureViewClassInfo.nativeWindow);
126 sp<ANativeWindow> window(nativeWindow);
127 window->decStrong((void*)android_view_TextureView_createNativeWindow);
128 SET_LONG(textureView, gTextureViewClassInfo.nativeWindow, 0);
132 static jboolean android_view_TextureView_lockCanvas(JNIEnv* env, jobject,
133 jlong nativeWindow, jobject canvas, jobject dirtyRect) {
139 ANativeWindow_Buffer buffer;
141 Rect rect(Rect::EMPTY_RECT);
143 rect.left = GET_INT(dirtyRect, gRectClassInfo.left);
144 rect.top = GET_INT(dirtyRect, gRectClassInfo.top);
145 rect.right = GET_INT(dirtyRect, gRectClassInfo.right);
146 rect.bottom = GET_INT(dirtyRect, gRectClassInfo.bottom);
148 rect.set(Rect(0x3FFF, 0x3FFF));
151 sp<ANativeWindow> window((ANativeWindow*) nativeWindow);
152 int32_t status = native_window_lock(window.get(), &buffer, &rect);
153 if (status) return JNI_FALSE;
155 ssize_t bytesCount = buffer.stride * bytesPerPixel(buffer.format);
158 bitmap.setInfo(convertPixelFormat(buffer), bytesCount);
160 if (buffer.width > 0 && buffer.height > 0) {
161 bitmap.setPixels(buffer.bits);
163 bitmap.setPixels(NULL);
166 Canvas* nativeCanvas = GraphicsJNI::getNativeCanvas(env, canvas);
167 nativeCanvas->setBitmap(bitmap);
168 nativeCanvas->clipRect(rect.left, rect.top, rect.right, rect.bottom);
171 INVOKEV(dirtyRect, gRectClassInfo.set,
172 int(rect.left), int(rect.top), int(rect.right), int(rect.bottom));
178 static void android_view_TextureView_unlockCanvasAndPost(JNIEnv* env, jobject,
179 jlong nativeWindow, jobject canvas) {
181 Canvas* nativeCanvas = GraphicsJNI::getNativeCanvas(env, canvas);
182 nativeCanvas->setBitmap(SkBitmap());
185 sp<ANativeWindow> window((ANativeWindow*) nativeWindow);
186 native_window_unlockAndPost(window.get());
190 // ----------------------------------------------------------------------------
192 // ----------------------------------------------------------------------------
194 const char* const kClassPathName = "android/view/TextureView";
196 static const JNINativeMethod gMethods[] = {
197 { "nCreateNativeWindow", "(Landroid/graphics/SurfaceTexture;)V",
198 (void*) android_view_TextureView_createNativeWindow },
199 { "nDestroyNativeWindow", "()V",
200 (void*) android_view_TextureView_destroyNativeWindow },
202 { "nLockCanvas", "(JLandroid/graphics/Canvas;Landroid/graphics/Rect;)Z",
203 (void*) android_view_TextureView_lockCanvas },
204 { "nUnlockCanvasAndPost", "(JLandroid/graphics/Canvas;)V",
205 (void*) android_view_TextureView_unlockCanvasAndPost },
208 int register_android_view_TextureView(JNIEnv* env) {
209 jclass clazz = FindClassOrDie(env, "android/graphics/Rect");
210 gRectClassInfo.set = GetMethodIDOrDie(env, clazz, "set", "(IIII)V");
211 gRectClassInfo.left = GetFieldIDOrDie(env, clazz, "left", "I");
212 gRectClassInfo.top = GetFieldIDOrDie(env, clazz, "top", "I");
213 gRectClassInfo.right = GetFieldIDOrDie(env, clazz, "right", "I");
214 gRectClassInfo.bottom = GetFieldIDOrDie(env, clazz, "bottom", "I");
216 clazz = FindClassOrDie(env, "android/view/TextureView");
217 gTextureViewClassInfo.nativeWindow = GetFieldIDOrDie(env, clazz, "mNativeWindow", "J");
219 return RegisterMethodsOrDie(env, kClassPathName, gMethods, NELEM(gMethods));