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