android_view_Surface.cpp revision 7c116b54b743cc3e92ac42abdbbe324d63b50a81
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 // get dirty region 212 Region dirtyRegion; 213 if (dirtyRectObj) { 214 Rect dirty; 215 dirty.left = env->GetIntField(dirtyRectObj, gRectClassInfo.left); 216 dirty.top = env->GetIntField(dirtyRectObj, gRectClassInfo.top); 217 dirty.right = env->GetIntField(dirtyRectObj, gRectClassInfo.right); 218 dirty.bottom = env->GetIntField(dirtyRectObj, gRectClassInfo.bottom); 219 if (!dirty.isEmpty()) { 220 dirtyRegion.set(dirty); 221 } 222 } else { 223 dirtyRegion.set(Rect(0x3FFF, 0x3FFF)); 224 } 225 226 ANativeWindow_Buffer outBuffer; 227 Rect dirtyBounds(dirtyRegion.getBounds()); 228 status_t err = surface->lock(&outBuffer, &dirtyBounds); 229 dirtyRegion.set(dirtyBounds); 230 if (err < 0) { 231 const char* const exception = (err == NO_MEMORY) ? 232 OutOfResourcesException : 233 "java/lang/IllegalArgumentException"; 234 jniThrowException(env, exception, NULL); 235 return NULL; 236 } 237 238 // Associate a SkCanvas object to this surface 239 jobject canvasObj = env->GetObjectField(surfaceObj, gSurfaceClassInfo.mCanvas); 240 env->SetIntField(canvasObj, gCanvasClassInfo.mSurfaceFormat, outBuffer.format); 241 242 SkBitmap bitmap; 243 ssize_t bpr = outBuffer.stride * bytesPerPixel(outBuffer.format); 244 bitmap.setConfig(convertPixelFormat(outBuffer.format), outBuffer.width, outBuffer.height, bpr); 245 if (outBuffer.format == PIXEL_FORMAT_RGBX_8888) { 246 bitmap.setIsOpaque(true); 247 } 248 if (outBuffer.width > 0 && outBuffer.height > 0) { 249 bitmap.setPixels(outBuffer.bits); 250 } else { 251 // be safe with an empty bitmap. 252 bitmap.setPixels(NULL); 253 } 254 255 SkCanvas* nativeCanvas = SkNEW_ARGS(SkCanvas, (bitmap)); 256 swapCanvasPtr(env, canvasObj, nativeCanvas); 257 258 SkRegion clipReg; 259 if (dirtyRegion.isRect()) { // very common case 260 const Rect b(dirtyRegion.getBounds()); 261 clipReg.setRect(b.left, b.top, b.right, b.bottom); 262 } else { 263 size_t count; 264 Rect const* r = dirtyRegion.getArray(&count); 265 while (count) { 266 clipReg.op(r->left, r->top, r->right, r->bottom, SkRegion::kUnion_Op); 267 r++, count--; 268 } 269 } 270 271 nativeCanvas->clipRegion(clipReg); 272 273 if (dirtyRectObj) { 274 const Rect& bounds(dirtyRegion.getBounds()); 275 env->SetIntField(dirtyRectObj, gRectClassInfo.left, bounds.left); 276 env->SetIntField(dirtyRectObj, gRectClassInfo.top, bounds.top); 277 env->SetIntField(dirtyRectObj, gRectClassInfo.right, bounds.right); 278 env->SetIntField(dirtyRectObj, gRectClassInfo.bottom, bounds.bottom); 279 } 280 281 return canvasObj; 282} 283 284static void nativeUnlockCanvasAndPost(JNIEnv* env, jobject surfaceObj, jint nativeObject, jobject canvasObj) { 285 jobject ownCanvasObj = env->GetObjectField(surfaceObj, gSurfaceClassInfo.mCanvas); 286 if (!env->IsSameObject(ownCanvasObj, canvasObj)) { 287 doThrowIAE(env); 288 return; 289 } 290 291 sp<Surface> surface(reinterpret_cast<Surface *>(nativeObject)); 292 if (!isSurfaceValid(surface)) { 293 return; 294 } 295 296 // detach the canvas from the surface 297 SkCanvas* nativeCanvas = SkNEW(SkCanvas); 298 swapCanvasPtr(env, canvasObj, nativeCanvas); 299 300 // unlock surface 301 status_t err = surface->unlockAndPost(); 302 if (err < 0) { 303 doThrowIAE(env); 304 } 305} 306 307// ---------------------------------------------------------------------------- 308 309static jint nativeCopyFrom(JNIEnv* env, jclass clazz, 310 jint nativeObject, jint surfaceControlNativeObj) { 311 /* 312 * This is used by the WindowManagerService just after constructing 313 * a Surface and is necessary for returning the Surface reference to 314 * the caller. At this point, we should only have a SurfaceControl. 315 */ 316 317 sp<SurfaceControl> ctrl(reinterpret_cast<SurfaceControl *>(surfaceControlNativeObj)); 318 sp<Surface> other(ctrl->getSurface()); 319 if (other != NULL) { 320 other->incStrong(&sRefBaseOwner); 321 } 322 323 sp<Surface> sur(reinterpret_cast<Surface *>(nativeObject)); 324 if (sur != NULL) { 325 sur->decStrong(&sRefBaseOwner); 326 } 327 328 return int(other.get()); 329} 330 331static jint nativeReadFromParcel(JNIEnv* env, jclass clazz, 332 jint nativeObject, jobject parcelObj) { 333 Parcel* parcel = parcelForJavaObject(env, parcelObj); 334 if (parcel == NULL) { 335 doThrowNPE(env); 336 return 0; 337 } 338 339 sp<Surface> self(reinterpret_cast<Surface *>(nativeObject)); 340 sp<IBinder> binder(parcel->readStrongBinder()); 341 342 // update the Surface only if the underlying IGraphicBufferProducer 343 // has changed. 344 if (self != NULL 345 && (self->getIGraphicBufferProducer()->asBinder() == binder)) { 346 // same IGraphicBufferProducer, return ourselves 347 return int(self.get()); 348 } 349 350 sp<Surface> sur; 351 sp<IGraphicBufferProducer> gbp(interface_cast<IGraphicBufferProducer>(binder)); 352 if (gbp != NULL) { 353 // we have a new IGraphicBufferProducer, create a new Surface for it 354 sur = new Surface(gbp); 355 // and keep a reference before passing to java 356 sur->incStrong(&sRefBaseOwner); 357 } 358 359 if (self != NULL) { 360 // and loose the java reference to ourselves 361 self->decStrong(&sRefBaseOwner); 362 } 363 364 return int(sur.get()); 365} 366 367static void nativeWriteToParcel(JNIEnv* env, jclass clazz, 368 jint nativeObject, jobject parcelObj) { 369 Parcel* parcel = parcelForJavaObject(env, parcelObj); 370 if (parcel == NULL) { 371 doThrowNPE(env); 372 return; 373 } 374 sp<Surface> self(reinterpret_cast<Surface *>(nativeObject)); 375 parcel->writeStrongBinder( self != 0 ? self->getIGraphicBufferProducer()->asBinder() : NULL); 376} 377 378// ---------------------------------------------------------------------------- 379 380static JNINativeMethod gSurfaceMethods[] = { 381 {"nativeCreateFromSurfaceTexture", "(Landroid/graphics/SurfaceTexture;)I", 382 (void*)nativeCreateFromSurfaceTexture }, 383 {"nativeRelease", "(I)V", 384 (void*)nativeRelease }, 385 {"nativeIsValid", "(I)Z", 386 (void*)nativeIsValid }, 387 {"nativeIsConsumerRunningBehind", "(I)Z", 388 (void*)nativeIsConsumerRunningBehind }, 389 {"nativeLockCanvas", "(ILandroid/graphics/Rect;)Landroid/graphics/Canvas;", 390 (void*)nativeLockCanvas }, 391 {"nativeUnlockCanvasAndPost", "(ILandroid/graphics/Canvas;)V", 392 (void*)nativeUnlockCanvasAndPost }, 393 {"nativeCopyFrom", "(II)I", 394 (void*)nativeCopyFrom }, 395 {"nativeReadFromParcel", "(ILandroid/os/Parcel;)I", 396 (void*)nativeReadFromParcel }, 397 {"nativeWriteToParcel", "(ILandroid/os/Parcel;)V", 398 (void*)nativeWriteToParcel }, 399}; 400 401int register_android_view_Surface(JNIEnv* env) 402{ 403 int err = AndroidRuntime::registerNativeMethods(env, "android/view/Surface", 404 gSurfaceMethods, NELEM(gSurfaceMethods)); 405 406 jclass clazz = env->FindClass("android/view/Surface"); 407 gSurfaceClassInfo.clazz = jclass(env->NewGlobalRef(clazz)); 408 gSurfaceClassInfo.mNativeObject = 409 env->GetFieldID(gSurfaceClassInfo.clazz, "mNativeObject", "I"); 410 gSurfaceClassInfo.mNativeObjectLock = 411 env->GetFieldID(gSurfaceClassInfo.clazz, "mNativeObjectLock", "Ljava/lang/Object;"); 412 gSurfaceClassInfo.mCanvas = 413 env->GetFieldID(gSurfaceClassInfo.clazz, "mCanvas", "Landroid/graphics/Canvas;"); 414 gSurfaceClassInfo.ctor = env->GetMethodID(gSurfaceClassInfo.clazz, "<init>", "(I)V"); 415 416 clazz = env->FindClass("android/graphics/Canvas"); 417 gCanvasClassInfo.mFinalizer = env->GetFieldID(clazz, "mFinalizer", "Landroid/graphics/Canvas$CanvasFinalizer;"); 418 gCanvasClassInfo.mNativeCanvas = env->GetFieldID(clazz, "mNativeCanvas", "I"); 419 gCanvasClassInfo.mSurfaceFormat = env->GetFieldID(clazz, "mSurfaceFormat", "I"); 420 421 clazz = env->FindClass("android/graphics/Canvas$CanvasFinalizer"); 422 gCanvasFinalizerClassInfo.mNativeCanvas = env->GetFieldID(clazz, "mNativeCanvas", "I"); 423 424 clazz = env->FindClass("android/graphics/Rect"); 425 gRectClassInfo.left = env->GetFieldID(clazz, "left", "I"); 426 gRectClassInfo.top = env->GetFieldID(clazz, "top", "I"); 427 gRectClassInfo.right = env->GetFieldID(clazz, "right", "I"); 428 gRectClassInfo.bottom = env->GetFieldID(clazz, "bottom", "I"); 429 430 return err; 431} 432 433}; 434