android_view_Surface.cpp revision e32632682ca9207bd247ca27012cf670b5c23f54
1/* 2 * Copyright (C) 2007 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#define LOG_TAG "Surface" 18 19#include <stdio.h> 20 21#include "jni.h" 22#include "JNIHelp.h" 23#include "android_os_Parcel.h" 24#include "android/graphics/GraphicsJNI.h" 25 26#include <android_runtime/AndroidRuntime.h> 27#include <android_runtime/android_view_Surface.h> 28#include <android_runtime/android_graphics_SurfaceTexture.h> 29 30#include <binder/Parcel.h> 31 32#include <gui/Surface.h> 33#include <gui/SurfaceControl.h> 34#include <gui/GLConsumer.h> 35 36#include <ui/Rect.h> 37#include <ui/Region.h> 38 39#include <SkCanvas.h> 40#include <SkBitmap.h> 41#include <SkRegion.h> 42 43#include <utils/misc.h> 44#include <utils/Log.h> 45 46#include <ScopedUtfChars.h> 47 48// ---------------------------------------------------------------------------- 49 50namespace android { 51 52static const char* const OutOfResourcesException = 53 "android/view/Surface$OutOfResourcesException"; 54 55static struct { 56 jclass clazz; 57 jfieldID mNativeObject; 58 jfieldID mLock; 59 jmethodID ctor; 60} gSurfaceClassInfo; 61 62static struct { 63 jfieldID left; 64 jfieldID top; 65 jfieldID right; 66 jfieldID bottom; 67} gRectClassInfo; 68 69static struct { 70 jfieldID mFinalizer; 71 jfieldID mNativeCanvas; 72 jfieldID mSurfaceFormat; 73} gCanvasClassInfo; 74 75static struct { 76 jfieldID mNativeCanvas; 77} gCanvasFinalizerClassInfo; 78 79// ---------------------------------------------------------------------------- 80 81// this is just a pointer we use to pass to inc/decStrong 82static const void *sRefBaseOwner; 83 84bool android_view_Surface_isInstanceOf(JNIEnv* env, jobject obj) { 85 return env->IsInstanceOf(obj, gSurfaceClassInfo.clazz); 86} 87 88sp<ANativeWindow> android_view_Surface_getNativeWindow(JNIEnv* env, jobject surfaceObj) { 89 return android_view_Surface_getSurface(env, surfaceObj); 90} 91 92sp<Surface> android_view_Surface_getSurface(JNIEnv* env, jobject surfaceObj) { 93 sp<Surface> sur; 94 jobject lock = env->GetObjectField(surfaceObj, 95 gSurfaceClassInfo.mLock); 96 if (env->MonitorEnter(lock) == JNI_OK) { 97 sur = reinterpret_cast<Surface *>( 98 env->GetIntField(surfaceObj, gSurfaceClassInfo.mNativeObject)); 99 env->MonitorExit(lock); 100 } 101 return sur; 102} 103 104jobject android_view_Surface_createFromIGraphicBufferProducer(JNIEnv* env, 105 const sp<IGraphicBufferProducer>& bufferProducer) { 106 if (bufferProducer == NULL) { 107 return NULL; 108 } 109 110 sp<Surface> surface(new Surface(bufferProducer, true)); 111 if (surface == NULL) { 112 return NULL; 113 } 114 115 jobject surfaceObj = env->NewObject(gSurfaceClassInfo.clazz, gSurfaceClassInfo.ctor, surface.get()); 116 if (surfaceObj == NULL) { 117 if (env->ExceptionCheck()) { 118 ALOGE("Could not create instance of Surface from IGraphicBufferProducer."); 119 LOGE_EX(env); 120 env->ExceptionClear(); 121 } 122 return NULL; 123 } 124 surface->incStrong(&sRefBaseOwner); 125 return surfaceObj; 126} 127 128// ---------------------------------------------------------------------------- 129 130static inline bool isSurfaceValid(const sp<Surface>& sur) { 131 return Surface::isValid(sur); 132} 133 134// ---------------------------------------------------------------------------- 135 136static jint nativeCreateFromSurfaceTexture(JNIEnv* env, jclass clazz, 137 jobject surfaceTextureObj) { 138 sp<GLConsumer> st(SurfaceTexture_getSurfaceTexture(env, surfaceTextureObj)); 139 if (st == NULL) { 140 jniThrowException(env, "java/lang/IllegalArgumentException", 141 "SurfaceTexture has already been released"); 142 return 0; 143 } 144 145 sp<IGraphicBufferProducer> bq = st->getBufferQueue(); 146 sp<Surface> surface(new Surface(bq, true)); 147 if (surface == NULL) { 148 jniThrowException(env, OutOfResourcesException, NULL); 149 return 0; 150 } 151 152 surface->incStrong(&sRefBaseOwner); 153 return int(surface.get()); 154} 155 156static void nativeRelease(JNIEnv* env, jclass clazz, jint nativeObject) { 157 sp<Surface> sur(reinterpret_cast<Surface *>(nativeObject)); 158 sur->decStrong(&sRefBaseOwner); 159} 160 161static jboolean nativeIsValid(JNIEnv* env, jclass clazz, jint nativeObject) { 162 sp<Surface> sur(reinterpret_cast<Surface *>(nativeObject)); 163 return isSurfaceValid(sur) ? JNI_TRUE : JNI_FALSE; 164} 165 166static jboolean nativeIsConsumerRunningBehind(JNIEnv* env, jclass clazz, jint nativeObject) { 167 sp<Surface> sur(reinterpret_cast<Surface *>(nativeObject)); 168 if (!isSurfaceValid(sur)) { 169 doThrowIAE(env); 170 return JNI_FALSE; 171 } 172 int value = 0; 173 ANativeWindow* anw = static_cast<ANativeWindow*>(sur.get()); 174 anw->query(anw, NATIVE_WINDOW_CONSUMER_RUNNING_BEHIND, &value); 175 return value; 176} 177 178static inline SkBitmap::Config convertPixelFormat(PixelFormat format) { 179 /* note: if PIXEL_FORMAT_RGBX_8888 means that all alpha bytes are 0xFF, then 180 we can map to SkBitmap::kARGB_8888_Config, and optionally call 181 bitmap.setIsOpaque(true) on the resulting SkBitmap (as an accelerator) 182 */ 183 switch (format) { 184 case PIXEL_FORMAT_RGBX_8888: return SkBitmap::kARGB_8888_Config; 185 case PIXEL_FORMAT_RGBA_8888: return SkBitmap::kARGB_8888_Config; 186 case PIXEL_FORMAT_RGBA_4444: return SkBitmap::kARGB_4444_Config; 187 case PIXEL_FORMAT_RGB_565: return SkBitmap::kRGB_565_Config; 188 case PIXEL_FORMAT_A_8: return SkBitmap::kA8_Config; 189 default: return SkBitmap::kNo_Config; 190 } 191} 192 193static inline void swapCanvasPtr(JNIEnv* env, jobject canvasObj, SkCanvas* newCanvas) { 194 jobject canvasFinalizerObj = env->GetObjectField(canvasObj, gCanvasClassInfo.mFinalizer); 195 SkCanvas* previousCanvas = reinterpret_cast<SkCanvas*>( 196 env->GetIntField(canvasObj, gCanvasClassInfo.mNativeCanvas)); 197 env->SetIntField(canvasObj, gCanvasClassInfo.mNativeCanvas, (int)newCanvas); 198 env->SetIntField(canvasFinalizerObj, gCanvasFinalizerClassInfo.mNativeCanvas, (int)newCanvas); 199 SkSafeUnref(previousCanvas); 200} 201 202static void nativeLockCanvas(JNIEnv* env, jclass clazz, 203 jint nativeObject, jobject canvasObj, jobject dirtyRectObj) { 204 sp<Surface> surface(reinterpret_cast<Surface *>(nativeObject)); 205 206 if (!isSurfaceValid(surface)) { 207 doThrowIAE(env); 208 return; 209 } 210 211 Rect dirtyRect; 212 Rect* dirtyRectPtr = NULL; 213 214 if (dirtyRectObj) { 215 dirtyRect.left = env->GetIntField(dirtyRectObj, gRectClassInfo.left); 216 dirtyRect.top = env->GetIntField(dirtyRectObj, gRectClassInfo.top); 217 dirtyRect.right = env->GetIntField(dirtyRectObj, gRectClassInfo.right); 218 dirtyRect.bottom = env->GetIntField(dirtyRectObj, gRectClassInfo.bottom); 219 dirtyRectPtr = &dirtyRect; 220 } 221 222 ANativeWindow_Buffer outBuffer; 223 status_t err = surface->lock(&outBuffer, dirtyRectPtr); 224 if (err < 0) { 225 const char* const exception = (err == NO_MEMORY) ? 226 OutOfResourcesException : 227 "java/lang/IllegalArgumentException"; 228 jniThrowException(env, exception, NULL); 229 return; 230 } 231 232 // Associate a SkCanvas object to this surface 233 env->SetIntField(canvasObj, gCanvasClassInfo.mSurfaceFormat, outBuffer.format); 234 235 SkBitmap bitmap; 236 ssize_t bpr = outBuffer.stride * bytesPerPixel(outBuffer.format); 237 bitmap.setConfig(convertPixelFormat(outBuffer.format), outBuffer.width, outBuffer.height, bpr); 238 if (outBuffer.format == PIXEL_FORMAT_RGBX_8888) { 239 bitmap.setIsOpaque(true); 240 } 241 if (outBuffer.width > 0 && outBuffer.height > 0) { 242 bitmap.setPixels(outBuffer.bits); 243 } else { 244 // be safe with an empty bitmap. 245 bitmap.setPixels(NULL); 246 } 247 248 SkCanvas* nativeCanvas = SkNEW_ARGS(SkCanvas, (bitmap)); 249 swapCanvasPtr(env, canvasObj, nativeCanvas); 250 251 if (dirtyRectPtr) { 252 nativeCanvas->clipRect( SkRect::Make(reinterpret_cast<const SkIRect&>(dirtyRect)) ); 253 } 254 255 if (dirtyRectObj) { 256 env->SetIntField(dirtyRectObj, gRectClassInfo.left, dirtyRect.left); 257 env->SetIntField(dirtyRectObj, gRectClassInfo.top, dirtyRect.top); 258 env->SetIntField(dirtyRectObj, gRectClassInfo.right, dirtyRect.right); 259 env->SetIntField(dirtyRectObj, gRectClassInfo.bottom, dirtyRect.bottom); 260 } 261} 262 263static void nativeUnlockCanvasAndPost(JNIEnv* env, jclass clazz, 264 jint nativeObject, jobject canvasObj) { 265 sp<Surface> surface(reinterpret_cast<Surface *>(nativeObject)); 266 if (!isSurfaceValid(surface)) { 267 return; 268 } 269 270 // detach the canvas from the surface 271 SkCanvas* nativeCanvas = SkNEW(SkCanvas); 272 swapCanvasPtr(env, canvasObj, nativeCanvas); 273 274 // unlock surface 275 status_t err = surface->unlockAndPost(); 276 if (err < 0) { 277 doThrowIAE(env); 278 } 279} 280 281// ---------------------------------------------------------------------------- 282 283static jint nativeCreateFromSurfaceControl(JNIEnv* env, jclass clazz, 284 jint surfaceControlNativeObj) { 285 /* 286 * This is used by the WindowManagerService just after constructing 287 * a Surface and is necessary for returning the Surface reference to 288 * the caller. At this point, we should only have a SurfaceControl. 289 */ 290 291 sp<SurfaceControl> ctrl(reinterpret_cast<SurfaceControl *>(surfaceControlNativeObj)); 292 sp<Surface> surface(ctrl->getSurface()); 293 if (surface != NULL) { 294 surface->incStrong(&sRefBaseOwner); 295 } 296 return reinterpret_cast<jint>(surface.get()); 297} 298 299static jint nativeReadFromParcel(JNIEnv* env, jclass clazz, 300 jint nativeObject, jobject parcelObj) { 301 Parcel* parcel = parcelForJavaObject(env, parcelObj); 302 if (parcel == NULL) { 303 doThrowNPE(env); 304 return 0; 305 } 306 307 sp<Surface> self(reinterpret_cast<Surface *>(nativeObject)); 308 sp<IBinder> binder(parcel->readStrongBinder()); 309 310 // update the Surface only if the underlying IGraphicBufferProducer 311 // has changed. 312 if (self != NULL 313 && (self->getIGraphicBufferProducer()->asBinder() == binder)) { 314 // same IGraphicBufferProducer, return ourselves 315 return int(self.get()); 316 } 317 318 sp<Surface> sur; 319 sp<IGraphicBufferProducer> gbp(interface_cast<IGraphicBufferProducer>(binder)); 320 if (gbp != NULL) { 321 // we have a new IGraphicBufferProducer, create a new Surface for it 322 sur = new Surface(gbp, true); 323 // and keep a reference before passing to java 324 sur->incStrong(&sRefBaseOwner); 325 } 326 327 if (self != NULL) { 328 // and loose the java reference to ourselves 329 self->decStrong(&sRefBaseOwner); 330 } 331 332 return int(sur.get()); 333} 334 335static void nativeWriteToParcel(JNIEnv* env, jclass clazz, 336 jint nativeObject, jobject parcelObj) { 337 Parcel* parcel = parcelForJavaObject(env, parcelObj); 338 if (parcel == NULL) { 339 doThrowNPE(env); 340 return; 341 } 342 sp<Surface> self(reinterpret_cast<Surface *>(nativeObject)); 343 parcel->writeStrongBinder( self != 0 ? self->getIGraphicBufferProducer()->asBinder() : NULL); 344} 345 346// ---------------------------------------------------------------------------- 347 348static JNINativeMethod gSurfaceMethods[] = { 349 {"nativeCreateFromSurfaceTexture", "(Landroid/graphics/SurfaceTexture;)I", 350 (void*)nativeCreateFromSurfaceTexture }, 351 {"nativeRelease", "(I)V", 352 (void*)nativeRelease }, 353 {"nativeIsValid", "(I)Z", 354 (void*)nativeIsValid }, 355 {"nativeIsConsumerRunningBehind", "(I)Z", 356 (void*)nativeIsConsumerRunningBehind }, 357 {"nativeLockCanvas", "(ILandroid/graphics/Canvas;Landroid/graphics/Rect;)V", 358 (void*)nativeLockCanvas }, 359 {"nativeUnlockCanvasAndPost", "(ILandroid/graphics/Canvas;)V", 360 (void*)nativeUnlockCanvasAndPost }, 361 {"nativeCreateFromSurfaceControl", "(I)I", 362 (void*)nativeCreateFromSurfaceControl }, 363 {"nativeReadFromParcel", "(ILandroid/os/Parcel;)I", 364 (void*)nativeReadFromParcel }, 365 {"nativeWriteToParcel", "(ILandroid/os/Parcel;)V", 366 (void*)nativeWriteToParcel }, 367}; 368 369int register_android_view_Surface(JNIEnv* env) 370{ 371 int err = AndroidRuntime::registerNativeMethods(env, "android/view/Surface", 372 gSurfaceMethods, NELEM(gSurfaceMethods)); 373 374 jclass clazz = env->FindClass("android/view/Surface"); 375 gSurfaceClassInfo.clazz = jclass(env->NewGlobalRef(clazz)); 376 gSurfaceClassInfo.mNativeObject = 377 env->GetFieldID(gSurfaceClassInfo.clazz, "mNativeObject", "I"); 378 gSurfaceClassInfo.mLock = 379 env->GetFieldID(gSurfaceClassInfo.clazz, "mLock", "Ljava/lang/Object;"); 380 gSurfaceClassInfo.ctor = env->GetMethodID(gSurfaceClassInfo.clazz, "<init>", "(I)V"); 381 382 clazz = env->FindClass("android/graphics/Canvas"); 383 gCanvasClassInfo.mFinalizer = env->GetFieldID(clazz, "mFinalizer", "Landroid/graphics/Canvas$CanvasFinalizer;"); 384 gCanvasClassInfo.mNativeCanvas = env->GetFieldID(clazz, "mNativeCanvas", "I"); 385 gCanvasClassInfo.mSurfaceFormat = env->GetFieldID(clazz, "mSurfaceFormat", "I"); 386 387 clazz = env->FindClass("android/graphics/Canvas$CanvasFinalizer"); 388 gCanvasFinalizerClassInfo.mNativeCanvas = env->GetFieldID(clazz, "mNativeCanvas", "I"); 389 390 clazz = env->FindClass("android/graphics/Rect"); 391 gRectClassInfo.left = env->GetFieldID(clazz, "left", "I"); 392 gRectClassInfo.top = env->GetFieldID(clazz, "top", "I"); 393 gRectClassInfo.right = env->GetFieldID(clazz, "right", "I"); 394 gRectClassInfo.bottom = env->GetFieldID(clazz, "bottom", "I"); 395 396 return err; 397} 398 399}; 400