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 34#include "core_jni_helpers.h" 35 36namespace android { 37 38// ---------------------------------------------------------------------------- 39// JNI Glue 40// ---------------------------------------------------------------------------- 41 42static struct { 43 jmethodID set; 44 jfieldID left; 45 jfieldID top; 46 jfieldID right; 47 jfieldID bottom; 48} gRectClassInfo; 49 50static struct { 51 jfieldID nativeWindow; 52} gTextureViewClassInfo; 53 54#define GET_INT(object, field) \ 55 env->GetIntField(object, field) 56 57#define GET_LONG(object, field) \ 58 env->GetLongField(object, field) 59 60#define SET_INT(object, field, value) \ 61 env->SetIntField(object, field, value) 62 63#define SET_LONG(object, field, value) \ 64 env->SetLongField(object, field, value) 65 66#define INVOKEV(object, method, ...) \ 67 env->CallVoidMethod(object, method, __VA_ARGS__) 68 69// ---------------------------------------------------------------------------- 70// Native layer 71// ---------------------------------------------------------------------------- 72 73// FIXME: consider exporting this to share (e.g. android_view_Surface.cpp) 74static inline SkImageInfo convertPixelFormat(const ANativeWindow_Buffer& buffer) { 75 SkImageInfo info; 76 info.fWidth = buffer.width; 77 info.fHeight = buffer.height; 78 switch (buffer.format) { 79 case WINDOW_FORMAT_RGBA_8888: 80 info.fColorType = kN32_SkColorType; 81 info.fAlphaType = kPremul_SkAlphaType; 82 break; 83 case WINDOW_FORMAT_RGBX_8888: 84 info.fColorType = kN32_SkColorType; 85 info.fAlphaType = kOpaque_SkAlphaType; 86 break; 87 case WINDOW_FORMAT_RGB_565: 88 info.fColorType = kRGB_565_SkColorType; 89 info.fAlphaType = kOpaque_SkAlphaType; 90 break; 91 default: 92 info.fColorType = kUnknown_SkColorType; 93 // switch to kUnknown_SkAlphaType when its in skia 94 info.fAlphaType = kOpaque_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 Canvas* nativeCanvas = GraphicsJNI::getNativeCanvas(env, canvas); 171 nativeCanvas->setBitmap(bitmap); 172 nativeCanvas->clipRect(rect.left, rect.top, rect.right, rect.bottom); 173 174 if (dirtyRect) { 175 INVOKEV(dirtyRect, gRectClassInfo.set, 176 int(rect.left), int(rect.top), int(rect.right), int(rect.bottom)); 177 } 178 179 return JNI_TRUE; 180} 181 182static void android_view_TextureView_unlockCanvasAndPost(JNIEnv* env, jobject, 183 jlong nativeWindow, jobject canvas) { 184 185 Canvas* nativeCanvas = GraphicsJNI::getNativeCanvas(env, canvas); 186 nativeCanvas->setBitmap(SkBitmap()); 187 188 if (nativeWindow) { 189 sp<ANativeWindow> window((ANativeWindow*) nativeWindow); 190 native_window_unlockAndPost(window.get()); 191 } 192} 193 194// ---------------------------------------------------------------------------- 195// JNI Glue 196// ---------------------------------------------------------------------------- 197 198const char* const kClassPathName = "android/view/TextureView"; 199 200static JNINativeMethod gMethods[] = { 201 { "nCreateNativeWindow", "(Landroid/graphics/SurfaceTexture;)V", 202 (void*) android_view_TextureView_createNativeWindow }, 203 { "nDestroyNativeWindow", "()V", 204 (void*) android_view_TextureView_destroyNativeWindow }, 205 206 { "nLockCanvas", "(JLandroid/graphics/Canvas;Landroid/graphics/Rect;)Z", 207 (void*) android_view_TextureView_lockCanvas }, 208 { "nUnlockCanvasAndPost", "(JLandroid/graphics/Canvas;)V", 209 (void*) android_view_TextureView_unlockCanvasAndPost }, 210}; 211 212int register_android_view_TextureView(JNIEnv* env) { 213 jclass clazz = FindClassOrDie(env, "android/graphics/Rect"); 214 gRectClassInfo.set = GetMethodIDOrDie(env, clazz, "set", "(IIII)V"); 215 gRectClassInfo.left = GetFieldIDOrDie(env, clazz, "left", "I"); 216 gRectClassInfo.top = GetFieldIDOrDie(env, clazz, "top", "I"); 217 gRectClassInfo.right = GetFieldIDOrDie(env, clazz, "right", "I"); 218 gRectClassInfo.bottom = GetFieldIDOrDie(env, clazz, "bottom", "I"); 219 220 clazz = FindClassOrDie(env, "android/view/TextureView"); 221 gTextureViewClassInfo.nativeWindow = GetFieldIDOrDie(env, clazz, "mNativeWindow", "J"); 222 223 return RegisterMethodsOrDie(env, kClassPathName, gMethods, NELEM(gMethods)); 224} 225 226}; 227