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