android_view_TextureView.cpp revision c677675e9c465dc1de21ecf2e0421835c7eb55b4
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 mSurfaceFormat; 48 jmethodID safeCanvasSwap; 49} gCanvasClassInfo; 50 51static struct { 52 jfieldID nativeWindow; 53} gTextureViewClassInfo; 54 55#define GET_INT(object, field) \ 56 env->GetIntField(object, field) 57 58#define GET_LONG(object, field) \ 59 env->GetLongField(object, field) 60 61#define SET_INT(object, field, value) \ 62 env->SetIntField(object, field, value) 63 64#define SET_LONG(object, field, value) \ 65 env->SetLongField(object, field, value) 66 67#define INVOKEV(object, method, ...) \ 68 env->CallVoidMethod(object, method, __VA_ARGS__) 69 70// ---------------------------------------------------------------------------- 71// Native layer 72// ---------------------------------------------------------------------------- 73 74static inline SkBitmap::Config convertPixelFormat(int32_t format) { 75 switch (format) { 76 case WINDOW_FORMAT_RGBA_8888: 77 return SkBitmap::kARGB_8888_Config; 78 case WINDOW_FORMAT_RGBX_8888: 79 return SkBitmap::kARGB_8888_Config; 80 case WINDOW_FORMAT_RGB_565: 81 return SkBitmap::kRGB_565_Config; 82 default: 83 return SkBitmap::kNo_Config; 84 } 85} 86 87/** 88 * This is a private API, and this implementation is also provided in the NDK. 89 * However, the NDK links against android_runtime, which means that using the 90 * NDK implementation would create a circular dependency between the libraries. 91 */ 92static int32_t native_window_lock(ANativeWindow* window, ANativeWindow_Buffer* outBuffer, 93 Rect* inOutDirtyBounds) { 94 return window->perform(window, NATIVE_WINDOW_LOCK, outBuffer, inOutDirtyBounds); 95} 96 97static int32_t native_window_unlockAndPost(ANativeWindow* window) { 98 return window->perform(window, NATIVE_WINDOW_UNLOCK_AND_POST); 99} 100 101static void android_view_TextureView_createNativeWindow(JNIEnv* env, jobject textureView, 102 jobject surface) { 103 104 sp<IGraphicBufferProducer> producer(SurfaceTexture_getProducer(env, surface)); 105 sp<ANativeWindow> window = new Surface(producer, true); 106 107 window->incStrong((void*)android_view_TextureView_createNativeWindow); 108 SET_LONG(textureView, gTextureViewClassInfo.nativeWindow, jlong(window.get())); 109} 110 111static void android_view_TextureView_destroyNativeWindow(JNIEnv* env, jobject textureView) { 112 113 ANativeWindow* nativeWindow = (ANativeWindow*) 114 GET_LONG(textureView, gTextureViewClassInfo.nativeWindow); 115 116 if (nativeWindow) { 117 sp<ANativeWindow> window(nativeWindow); 118 window->decStrong((void*)android_view_TextureView_createNativeWindow); 119 SET_LONG(textureView, gTextureViewClassInfo.nativeWindow, 0); 120 } 121} 122 123static jboolean android_view_TextureView_lockCanvas(JNIEnv* env, jobject, 124 jlong nativeWindow, jobject canvas, jobject dirtyRect) { 125 126 if (!nativeWindow) { 127 return JNI_FALSE; 128 } 129 130 ANativeWindow_Buffer buffer; 131 132 Rect rect; 133 if (dirtyRect) { 134 rect.left = GET_INT(dirtyRect, gRectClassInfo.left); 135 rect.top = GET_INT(dirtyRect, gRectClassInfo.top); 136 rect.right = GET_INT(dirtyRect, gRectClassInfo.right); 137 rect.bottom = GET_INT(dirtyRect, gRectClassInfo.bottom); 138 } else { 139 rect.set(Rect(0x3FFF, 0x3FFF)); 140 } 141 142 sp<ANativeWindow> window((ANativeWindow*) nativeWindow); 143 int32_t status = native_window_lock(window.get(), &buffer, &rect); 144 if (status) return JNI_FALSE; 145 146 ssize_t bytesCount = buffer.stride * bytesPerPixel(buffer.format); 147 148 SkBitmap bitmap; 149 bitmap.setConfig(convertPixelFormat(buffer.format), buffer.width, buffer.height, bytesCount); 150 151 if (buffer.format == WINDOW_FORMAT_RGBX_8888) { 152 bitmap.setAlphaType(kOpaque_SkAlphaType); 153 } 154 155 if (buffer.width > 0 && buffer.height > 0) { 156 bitmap.setPixels(buffer.bits); 157 } else { 158 bitmap.setPixels(NULL); 159 } 160 161 SET_INT(canvas, gCanvasClassInfo.mSurfaceFormat, buffer.format); 162 163 SkCanvas* nativeCanvas = SkNEW_ARGS(SkCanvas, (bitmap)); 164 INVOKEV(canvas, gCanvasClassInfo.safeCanvasSwap, (jlong)nativeCanvas, false); 165 166 SkRect clipRect; 167 clipRect.set(rect.left, rect.top, rect.right, rect.bottom); 168 nativeCanvas->clipRect(clipRect); 169 170 if (dirtyRect) { 171 INVOKEV(dirtyRect, gRectClassInfo.set, 172 int(rect.left), int(rect.top), int(rect.right), int(rect.bottom)); 173 } 174 175 return JNI_TRUE; 176} 177 178static void android_view_TextureView_unlockCanvasAndPost(JNIEnv* env, jobject, 179 jlong nativeWindow, jobject canvas) { 180 181 SkCanvas* nativeCanvas = SkNEW(SkCanvas); 182 INVOKEV(canvas, gCanvasClassInfo.safeCanvasSwap, (jlong)nativeCanvas, false); 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.safeCanvasSwap, clazz, "safeCanvasSwap", "(JZ)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