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