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