android_view_TextureView.cpp revision 5c3d927e17e98e8fd4a9f3c86f7f4def0bcfa816
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 76static inline SkBitmap::Config convertPixelFormat(int32_t format) { 77 switch (format) { 78 case WINDOW_FORMAT_RGBA_8888: 79 return SkBitmap::kARGB_8888_Config; 80 case WINDOW_FORMAT_RGBX_8888: 81 return SkBitmap::kARGB_8888_Config; 82 case WINDOW_FORMAT_RGB_565: 83 return SkBitmap::kRGB_565_Config; 84 default: 85 return SkBitmap::kNo_Config; 86 } 87} 88 89/** 90 * This is a private API, and this implementation is also provided in the NDK. 91 * However, the NDK links against android_runtime, which means that using the 92 * NDK implementation would create a circular dependency between the libraries. 93 */ 94static int32_t native_window_lock(ANativeWindow* window, ANativeWindow_Buffer* outBuffer, 95 Rect* inOutDirtyBounds) { 96 return window->perform(window, NATIVE_WINDOW_LOCK, outBuffer, inOutDirtyBounds); 97} 98 99static int32_t native_window_unlockAndPost(ANativeWindow* window) { 100 return window->perform(window, NATIVE_WINDOW_UNLOCK_AND_POST); 101} 102 103static void android_view_TextureView_createNativeWindow(JNIEnv* env, jobject textureView, 104 jobject surface) { 105 106 sp<IGraphicBufferProducer> producer(SurfaceTexture_getProducer(env, surface)); 107 sp<ANativeWindow> window = new Surface(producer, true); 108 109 window->incStrong((void*)android_view_TextureView_createNativeWindow); 110 SET_LONG(textureView, gTextureViewClassInfo.nativeWindow, jlong(window.get())); 111} 112 113static void android_view_TextureView_destroyNativeWindow(JNIEnv* env, jobject textureView) { 114 115 ANativeWindow* nativeWindow = (ANativeWindow*) 116 GET_LONG(textureView, gTextureViewClassInfo.nativeWindow); 117 118 if (nativeWindow) { 119 sp<ANativeWindow> window(nativeWindow); 120 window->decStrong((void*)android_view_TextureView_createNativeWindow); 121 SET_LONG(textureView, gTextureViewClassInfo.nativeWindow, 0); 122 } 123} 124 125static jboolean android_view_TextureView_lockCanvas(JNIEnv* env, jobject, 126 jlong nativeWindow, jobject canvas, jobject dirtyRect) { 127 128 if (!nativeWindow) { 129 return JNI_FALSE; 130 } 131 132 ANativeWindow_Buffer buffer; 133 134 Rect rect; 135 if (dirtyRect) { 136 rect.left = GET_INT(dirtyRect, gRectClassInfo.left); 137 rect.top = GET_INT(dirtyRect, gRectClassInfo.top); 138 rect.right = GET_INT(dirtyRect, gRectClassInfo.right); 139 rect.bottom = GET_INT(dirtyRect, gRectClassInfo.bottom); 140 } else { 141 rect.set(Rect(0x3FFF, 0x3FFF)); 142 } 143 144 sp<ANativeWindow> window((ANativeWindow*) nativeWindow); 145 int32_t status = native_window_lock(window.get(), &buffer, &rect); 146 if (status) return JNI_FALSE; 147 148 ssize_t bytesCount = buffer.stride * bytesPerPixel(buffer.format); 149 150 SkBitmap bitmap; 151 bitmap.setConfig(convertPixelFormat(buffer.format), buffer.width, buffer.height, bytesCount); 152 153 if (buffer.format == WINDOW_FORMAT_RGBX_8888) { 154 bitmap.setAlphaType(kOpaque_SkAlphaType); 155 } 156 157 if (buffer.width > 0 && buffer.height > 0) { 158 bitmap.setPixels(buffer.bits); 159 } else { 160 bitmap.setPixels(NULL); 161 } 162 163 SET_INT(canvas, gCanvasClassInfo.mSurfaceFormat, buffer.format); 164 INVOKEV(canvas, gCanvasClassInfo.setNativeBitmap, reinterpret_cast<jlong>(&bitmap)); 165 166 SkRect clipRect; 167 clipRect.set(rect.left, rect.top, rect.right, rect.bottom); 168 SkCanvas* nativeCanvas = GraphicsJNI::getNativeCanvas(env, canvas); 169 nativeCanvas->clipRect(clipRect); 170 171 if (dirtyRect) { 172 INVOKEV(dirtyRect, gRectClassInfo.set, 173 int(rect.left), int(rect.top), int(rect.right), int(rect.bottom)); 174 } 175 176 return JNI_TRUE; 177} 178 179static void android_view_TextureView_unlockCanvasAndPost(JNIEnv* env, jobject, 180 jlong nativeWindow, jobject canvas) { 181 182 INVOKEV(canvas, gCanvasClassInfo.setNativeBitmap, (jlong)0); 183 184 if (nativeWindow) { 185 sp<ANativeWindow> window((ANativeWindow*) nativeWindow); 186 native_window_unlockAndPost(window.get()); 187 } 188} 189 190// ---------------------------------------------------------------------------- 191// JNI Glue 192// ---------------------------------------------------------------------------- 193 194const char* const kClassPathName = "android/view/TextureView"; 195 196static JNINativeMethod gMethods[] = { 197 { "nCreateNativeWindow", "(Landroid/graphics/SurfaceTexture;)V", 198 (void*) android_view_TextureView_createNativeWindow }, 199 { "nDestroyNativeWindow", "()V", 200 (void*) android_view_TextureView_destroyNativeWindow }, 201 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 }, 206}; 207 208#define FIND_CLASS(var, className) \ 209 var = env->FindClass(className); \ 210 LOG_FATAL_IF(!var, "Unable to find class " className); 211 212#define GET_METHOD_ID(var, clazz, methodName, methodDescriptor) \ 213 var = env->GetMethodID(clazz, methodName, methodDescriptor); \ 214 LOG_FATAL_IF(!var, "Unable to find method " methodName); 215 216#define GET_FIELD_ID(var, clazz, fieldName, fieldDescriptor) \ 217 var = env->GetFieldID(clazz, fieldName, fieldDescriptor); \ 218 LOG_FATAL_IF(!var, "Unable to find field" fieldName); 219 220int register_android_view_TextureView(JNIEnv* env) { 221 jclass clazz; 222 FIND_CLASS(clazz, "android/graphics/Rect"); 223 GET_METHOD_ID(gRectClassInfo.set, clazz, "set", "(IIII)V"); 224 GET_FIELD_ID(gRectClassInfo.left, clazz, "left", "I"); 225 GET_FIELD_ID(gRectClassInfo.top, clazz, "top", "I"); 226 GET_FIELD_ID(gRectClassInfo.right, clazz, "right", "I"); 227 GET_FIELD_ID(gRectClassInfo.bottom, clazz, "bottom", "I"); 228 229 FIND_CLASS(clazz, "android/graphics/Canvas"); 230 GET_FIELD_ID(gCanvasClassInfo.mSurfaceFormat, clazz, "mSurfaceFormat", "I"); 231 GET_METHOD_ID(gCanvasClassInfo.setNativeBitmap, clazz, "setNativeBitmap", "(J)V"); 232 233 FIND_CLASS(clazz, "android/view/TextureView"); 234 GET_FIELD_ID(gTextureViewClassInfo.nativeWindow, clazz, "mNativeWindow", "J"); 235 236 return AndroidRuntime::registerNativeMethods(env, kClassPathName, gMethods, NELEM(gMethods)); 237} 238 239}; 240