android_view_Surface.cpp revision 667809ef5d2d1fe3796cdc9bdd09503a70d2ba6c
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 mNativeObjectLock; 59 jfieldID mCanvas; 60 jmethodID ctor; 61} gSurfaceClassInfo; 62 63static struct { 64 jfieldID left; 65 jfieldID top; 66 jfieldID right; 67 jfieldID bottom; 68} gRectClassInfo; 69 70static struct { 71 jfieldID mFinalizer; 72 jfieldID mNativeCanvas; 73 jfieldID mSurfaceFormat; 74} gCanvasClassInfo; 75 76static struct { 77 jfieldID mNativeCanvas; 78} gCanvasFinalizerClassInfo; 79 80// ---------------------------------------------------------------------------- 81 82// this is just a pointer we use to pass to inc/decStrong 83static const void *sRefBaseOwner; 84 85bool android_view_Surface_isInstanceOf(JNIEnv* env, jobject obj) { 86 return env->IsInstanceOf(obj, gSurfaceClassInfo.clazz); 87} 88 89sp<ANativeWindow> android_view_Surface_getNativeWindow(JNIEnv* env, jobject surfaceObj) { 90 return android_view_Surface_getSurface(env, surfaceObj); 91} 92 93sp<Surface> android_view_Surface_getSurface(JNIEnv* env, jobject surfaceObj) { 94 sp<Surface> sur; 95 jobject lock = env->GetObjectField(surfaceObj, 96 gSurfaceClassInfo.mNativeObjectLock); 97 if (env->MonitorEnter(lock) == JNI_OK) { 98 sur = reinterpret_cast<Surface *>( 99 env->GetIntField(surfaceObj, gSurfaceClassInfo.mNativeObject)); 100 env->MonitorExit(lock); 101 } 102 return sur; 103} 104 105jobject android_view_Surface_createFromIGraphicBufferProducer(JNIEnv* env, 106 const sp<IGraphicBufferProducer>& bufferProducer) { 107 if (bufferProducer == NULL) { 108 return NULL; 109 } 110 111 sp<Surface> surface(new Surface(bufferProducer)); 112 if (surface == NULL) { 113 return NULL; 114 } 115 116 jobject surfaceObj = env->NewObject(gSurfaceClassInfo.clazz, gSurfaceClassInfo.ctor, surface.get()); 117 if (surfaceObj == NULL) { 118 if (env->ExceptionCheck()) { 119 ALOGE("Could not create instance of Surface from IGraphicBufferProducer."); 120 LOGE_EX(env); 121 env->ExceptionClear(); 122 } 123 return NULL; 124 } 125 surface->incStrong(&sRefBaseOwner); 126 return surfaceObj; 127} 128 129// ---------------------------------------------------------------------------- 130 131static inline bool isSurfaceValid(const sp<Surface>& sur) { 132 return Surface::isValid(sur); 133} 134 135// ---------------------------------------------------------------------------- 136 137static jint nativeCreateFromSurfaceTexture(JNIEnv* env, jclass clazz, 138 jobject surfaceTextureObj) { 139 sp<GLConsumer> st(SurfaceTexture_getSurfaceTexture(env, surfaceTextureObj)); 140 if (st == NULL) { 141 jniThrowException(env, "java/lang/IllegalArgumentException", 142 "SurfaceTexture has already been released"); 143 return 0; 144 } 145 146 sp<IGraphicBufferProducer> bq = st->getBufferQueue(); 147 sp<Surface> surface(new Surface(bq)); 148 if (surface == NULL) { 149 jniThrowException(env, OutOfResourcesException, NULL); 150 return 0; 151 } 152 153 surface->incStrong(&sRefBaseOwner); 154 return int(surface.get()); 155} 156 157static void nativeRelease(JNIEnv* env, jclass clazz, jint nativeObject) { 158 sp<Surface> sur(reinterpret_cast<Surface *>(nativeObject)); 159 sur->decStrong(&sRefBaseOwner); 160} 161 162static jboolean nativeIsValid(JNIEnv* env, jclass clazz, jint nativeObject) { 163 sp<Surface> sur(reinterpret_cast<Surface *>(nativeObject)); 164 return isSurfaceValid(sur) ? JNI_TRUE : JNI_FALSE; 165} 166 167static jboolean nativeIsConsumerRunningBehind(JNIEnv* env, jclass clazz, jint nativeObject) { 168 sp<Surface> sur(reinterpret_cast<Surface *>(nativeObject)); 169 if (!isSurfaceValid(sur)) { 170 doThrowIAE(env); 171 return JNI_FALSE; 172 } 173 int value = 0; 174 ANativeWindow* anw = static_cast<ANativeWindow*>(sur.get()); 175 anw->query(anw, NATIVE_WINDOW_CONSUMER_RUNNING_BEHIND, &value); 176 return value; 177} 178 179static inline SkBitmap::Config convertPixelFormat(PixelFormat format) { 180 /* note: if PIXEL_FORMAT_RGBX_8888 means that all alpha bytes are 0xFF, then 181 we can map to SkBitmap::kARGB_8888_Config, and optionally call 182 bitmap.setIsOpaque(true) on the resulting SkBitmap (as an accelerator) 183 */ 184 switch (format) { 185 case PIXEL_FORMAT_RGBX_8888: return SkBitmap::kARGB_8888_Config; 186 case PIXEL_FORMAT_RGBA_8888: return SkBitmap::kARGB_8888_Config; 187 case PIXEL_FORMAT_RGBA_4444: return SkBitmap::kARGB_4444_Config; 188 case PIXEL_FORMAT_RGB_565: return SkBitmap::kRGB_565_Config; 189 case PIXEL_FORMAT_A_8: return SkBitmap::kA8_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->GetIntField(canvasObj, gCanvasClassInfo.mNativeCanvas)); 198 env->SetIntField(canvasObj, gCanvasClassInfo.mNativeCanvas, (int)newCanvas); 199 env->SetIntField(canvasFinalizerObj, gCanvasFinalizerClassInfo.mNativeCanvas, (int)newCanvas); 200 SkSafeUnref(previousCanvas); 201} 202 203static jobject nativeLockCanvas(JNIEnv* env, jobject surfaceObj, jint nativeObject, jobject dirtyRectObj) { 204 sp<Surface> surface(reinterpret_cast<Surface *>(nativeObject)); 205 206 if (!isSurfaceValid(surface)) { 207 doThrowIAE(env); 208 return NULL; 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 NULL; 230 } 231 232 // Associate a SkCanvas object to this surface 233 jobject canvasObj = env->GetObjectField(surfaceObj, gSurfaceClassInfo.mCanvas); 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.setIsOpaque(true); 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 return canvasObj; 264} 265 266static void nativeUnlockCanvasAndPost(JNIEnv* env, jobject surfaceObj, jint nativeObject, jobject canvasObj) { 267 jobject ownCanvasObj = env->GetObjectField(surfaceObj, gSurfaceClassInfo.mCanvas); 268 if (!env->IsSameObject(ownCanvasObj, canvasObj)) { 269 doThrowIAE(env); 270 return; 271 } 272 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 jint nativeCopyFrom(JNIEnv* env, jclass clazz, 292 jint nativeObject, jint 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> other(ctrl->getSurface()); 301 if (other != NULL) { 302 other->incStrong(&sRefBaseOwner); 303 } 304 305 sp<Surface> sur(reinterpret_cast<Surface *>(nativeObject)); 306 if (sur != NULL) { 307 sur->decStrong(&sRefBaseOwner); 308 } 309 310 return int(other.get()); 311} 312 313static jint nativeReadFromParcel(JNIEnv* env, jclass clazz, 314 jint nativeObject, jobject parcelObj) { 315 Parcel* parcel = parcelForJavaObject(env, parcelObj); 316 if (parcel == NULL) { 317 doThrowNPE(env); 318 return 0; 319 } 320 321 sp<Surface> self(reinterpret_cast<Surface *>(nativeObject)); 322 sp<IBinder> binder(parcel->readStrongBinder()); 323 324 // update the Surface only if the underlying IGraphicBufferProducer 325 // has changed. 326 if (self != NULL 327 && (self->getIGraphicBufferProducer()->asBinder() == binder)) { 328 // same IGraphicBufferProducer, return ourselves 329 return int(self.get()); 330 } 331 332 sp<Surface> sur; 333 sp<IGraphicBufferProducer> gbp(interface_cast<IGraphicBufferProducer>(binder)); 334 if (gbp != NULL) { 335 // we have a new IGraphicBufferProducer, create a new Surface for it 336 sur = new Surface(gbp); 337 // and keep a reference before passing to java 338 sur->incStrong(&sRefBaseOwner); 339 } 340 341 if (self != NULL) { 342 // and loose the java reference to ourselves 343 self->decStrong(&sRefBaseOwner); 344 } 345 346 return int(sur.get()); 347} 348 349static void nativeWriteToParcel(JNIEnv* env, jclass clazz, 350 jint nativeObject, jobject parcelObj) { 351 Parcel* parcel = parcelForJavaObject(env, parcelObj); 352 if (parcel == NULL) { 353 doThrowNPE(env); 354 return; 355 } 356 sp<Surface> self(reinterpret_cast<Surface *>(nativeObject)); 357 parcel->writeStrongBinder( self != 0 ? self->getIGraphicBufferProducer()->asBinder() : NULL); 358} 359 360// ---------------------------------------------------------------------------- 361 362static JNINativeMethod gSurfaceMethods[] = { 363 {"nativeCreateFromSurfaceTexture", "(Landroid/graphics/SurfaceTexture;)I", 364 (void*)nativeCreateFromSurfaceTexture }, 365 {"nativeRelease", "(I)V", 366 (void*)nativeRelease }, 367 {"nativeIsValid", "(I)Z", 368 (void*)nativeIsValid }, 369 {"nativeIsConsumerRunningBehind", "(I)Z", 370 (void*)nativeIsConsumerRunningBehind }, 371 {"nativeLockCanvas", "(ILandroid/graphics/Rect;)Landroid/graphics/Canvas;", 372 (void*)nativeLockCanvas }, 373 {"nativeUnlockCanvasAndPost", "(ILandroid/graphics/Canvas;)V", 374 (void*)nativeUnlockCanvasAndPost }, 375 {"nativeCopyFrom", "(II)I", 376 (void*)nativeCopyFrom }, 377 {"nativeReadFromParcel", "(ILandroid/os/Parcel;)I", 378 (void*)nativeReadFromParcel }, 379 {"nativeWriteToParcel", "(ILandroid/os/Parcel;)V", 380 (void*)nativeWriteToParcel }, 381}; 382 383int register_android_view_Surface(JNIEnv* env) 384{ 385 int err = AndroidRuntime::registerNativeMethods(env, "android/view/Surface", 386 gSurfaceMethods, NELEM(gSurfaceMethods)); 387 388 jclass clazz = env->FindClass("android/view/Surface"); 389 gSurfaceClassInfo.clazz = jclass(env->NewGlobalRef(clazz)); 390 gSurfaceClassInfo.mNativeObject = 391 env->GetFieldID(gSurfaceClassInfo.clazz, "mNativeObject", "I"); 392 gSurfaceClassInfo.mNativeObjectLock = 393 env->GetFieldID(gSurfaceClassInfo.clazz, "mNativeObjectLock", "Ljava/lang/Object;"); 394 gSurfaceClassInfo.mCanvas = 395 env->GetFieldID(gSurfaceClassInfo.clazz, "mCanvas", "Landroid/graphics/Canvas;"); 396 gSurfaceClassInfo.ctor = env->GetMethodID(gSurfaceClassInfo.clazz, "<init>", "(I)V"); 397 398 clazz = env->FindClass("android/graphics/Canvas"); 399 gCanvasClassInfo.mFinalizer = env->GetFieldID(clazz, "mFinalizer", "Landroid/graphics/Canvas$CanvasFinalizer;"); 400 gCanvasClassInfo.mNativeCanvas = env->GetFieldID(clazz, "mNativeCanvas", "I"); 401 gCanvasClassInfo.mSurfaceFormat = env->GetFieldID(clazz, "mSurfaceFormat", "I"); 402 403 clazz = env->FindClass("android/graphics/Canvas$CanvasFinalizer"); 404 gCanvasFinalizerClassInfo.mNativeCanvas = env->GetFieldID(clazz, "mNativeCanvas", "I"); 405 406 clazz = env->FindClass("android/graphics/Rect"); 407 gRectClassInfo.left = env->GetFieldID(clazz, "left", "I"); 408 gRectClassInfo.top = env->GetFieldID(clazz, "top", "I"); 409 gRectClassInfo.right = env->GetFieldID(clazz, "right", "I"); 410 gRectClassInfo.bottom = env->GetFieldID(clazz, "bottom", "I"); 411 412 return err; 413} 414 415}; 416