android_view_Surface.cpp revision b215529c720106165041c56142fce85a81929d33
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 <SkRegion.h> 43 44#include <utils/misc.h> 45#include <utils/Log.h> 46 47#include <ScopedUtfChars.h> 48 49// ---------------------------------------------------------------------------- 50 51namespace android { 52 53static const char* const OutOfResourcesException = 54 "android/view/Surface$OutOfResourcesException"; 55 56static struct { 57 jclass clazz; 58 jfieldID mNativeObject; 59 jfieldID mLock; 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.mLock); 97 if (env->MonitorEnter(lock) == JNI_OK) { 98 sur = reinterpret_cast<Surface *>( 99 env->GetLongField(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, true)); 112 if (surface == NULL) { 113 return NULL; 114 } 115 116 jobject surfaceObj = env->NewObject(gSurfaceClassInfo.clazz, 117 gSurfaceClassInfo.ctor, (jlong)surface.get()); 118 if (surfaceObj == NULL) { 119 if (env->ExceptionCheck()) { 120 ALOGE("Could not create instance of Surface from IGraphicBufferProducer."); 121 LOGE_EX(env); 122 env->ExceptionClear(); 123 } 124 return NULL; 125 } 126 surface->incStrong(&sRefBaseOwner); 127 return surfaceObj; 128} 129 130// ---------------------------------------------------------------------------- 131 132static inline bool isSurfaceValid(const sp<Surface>& sur) { 133 return Surface::isValid(sur); 134} 135 136// ---------------------------------------------------------------------------- 137 138static jlong nativeCreateFromSurfaceTexture(JNIEnv* env, jclass clazz, 139 jobject surfaceTextureObj) { 140 sp<IGraphicBufferProducer> producer(SurfaceTexture_getProducer(env, surfaceTextureObj)); 141 if (producer == NULL) { 142 jniThrowException(env, "java/lang/IllegalArgumentException", 143 "SurfaceTexture has already been released"); 144 return 0; 145 } 146 147 sp<Surface> surface(new Surface(producer, true)); 148 if (surface == NULL) { 149 jniThrowException(env, OutOfResourcesException, NULL); 150 return 0; 151 } 152 153 surface->incStrong(&sRefBaseOwner); 154 return jlong(surface.get()); 155} 156 157static void nativeRelease(JNIEnv* env, jclass clazz, jlong nativeObject) { 158 sp<Surface> sur(reinterpret_cast<Surface *>(nativeObject)); 159 sur->decStrong(&sRefBaseOwner); 160} 161 162static jboolean nativeIsValid(JNIEnv* env, jclass clazz, jlong 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, jlong 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_RGB_565: return SkBitmap::kRGB_565_Config; 188 default: return SkBitmap::kNo_Config; 189 } 190} 191 192static inline void swapCanvasPtr(JNIEnv* env, jobject canvasObj, SkCanvas* newCanvas) { 193 jobject canvasFinalizerObj = env->GetObjectField(canvasObj, gCanvasClassInfo.mFinalizer); 194 SkCanvas* previousCanvas = reinterpret_cast<SkCanvas*>( 195 env->GetLongField(canvasObj, gCanvasClassInfo.mNativeCanvas)); 196 env->SetLongField(canvasObj, gCanvasClassInfo.mNativeCanvas, (jlong)newCanvas); 197 env->SetLongField(canvasFinalizerObj, gCanvasFinalizerClassInfo.mNativeCanvas, (jlong)newCanvas); 198 SkSafeUnref(previousCanvas); 199} 200 201static jlong nativeLockCanvas(JNIEnv* env, jclass clazz, 202 jlong nativeObject, jobject canvasObj, jobject dirtyRectObj) { 203 sp<Surface> surface(reinterpret_cast<Surface *>(nativeObject)); 204 205 if (!isSurfaceValid(surface)) { 206 doThrowIAE(env); 207 return 0; 208 } 209 210 Rect dirtyRect; 211 Rect* dirtyRectPtr = NULL; 212 213 if (dirtyRectObj) { 214 dirtyRect.left = env->GetIntField(dirtyRectObj, gRectClassInfo.left); 215 dirtyRect.top = env->GetIntField(dirtyRectObj, gRectClassInfo.top); 216 dirtyRect.right = env->GetIntField(dirtyRectObj, gRectClassInfo.right); 217 dirtyRect.bottom = env->GetIntField(dirtyRectObj, gRectClassInfo.bottom); 218 dirtyRectPtr = &dirtyRect; 219 } 220 221 ANativeWindow_Buffer outBuffer; 222 status_t err = surface->lock(&outBuffer, dirtyRectPtr); 223 if (err < 0) { 224 const char* const exception = (err == NO_MEMORY) ? 225 OutOfResourcesException : 226 "java/lang/IllegalArgumentException"; 227 jniThrowException(env, exception, NULL); 228 return 0; 229 } 230 231 // Associate a SkCanvas object to this surface 232 env->SetIntField(canvasObj, gCanvasClassInfo.mSurfaceFormat, outBuffer.format); 233 234 SkBitmap bitmap; 235 ssize_t bpr = outBuffer.stride * bytesPerPixel(outBuffer.format); 236 bitmap.setConfig(convertPixelFormat(outBuffer.format), outBuffer.width, outBuffer.height, bpr); 237 if (outBuffer.format == PIXEL_FORMAT_RGBX_8888) { 238 bitmap.setIsOpaque(true); 239 } 240 if (outBuffer.width > 0 && outBuffer.height > 0) { 241 bitmap.setPixels(outBuffer.bits); 242 } else { 243 // be safe with an empty bitmap. 244 bitmap.setPixels(NULL); 245 } 246 247 SkCanvas* nativeCanvas = SkNEW_ARGS(SkCanvas, (bitmap)); 248 swapCanvasPtr(env, canvasObj, nativeCanvas); 249 250 if (dirtyRectPtr) { 251 nativeCanvas->clipRect( SkRect::Make(reinterpret_cast<const SkIRect&>(dirtyRect)) ); 252 } 253 254 if (dirtyRectObj) { 255 env->SetIntField(dirtyRectObj, gRectClassInfo.left, dirtyRect.left); 256 env->SetIntField(dirtyRectObj, gRectClassInfo.top, dirtyRect.top); 257 env->SetIntField(dirtyRectObj, gRectClassInfo.right, dirtyRect.right); 258 env->SetIntField(dirtyRectObj, gRectClassInfo.bottom, dirtyRect.bottom); 259 } 260 261 // Create another reference to the surface and return it. This reference 262 // should be passed to nativeUnlockCanvasAndPost in place of mNativeObject, 263 // because the latter could be replaced while the surface is locked. 264 sp<Surface> lockedSurface(surface); 265 lockedSurface->incStrong(&sRefBaseOwner); 266 return (jlong) lockedSurface.get(); 267} 268 269static void nativeUnlockCanvasAndPost(JNIEnv* env, jclass clazz, 270 jlong nativeObject, jobject canvasObj) { 271 sp<Surface> surface(reinterpret_cast<Surface *>(nativeObject)); 272 if (!isSurfaceValid(surface)) { 273 return; 274 } 275 276 // detach the canvas from the surface 277 SkCanvas* nativeCanvas = SkNEW(SkCanvas); 278 swapCanvasPtr(env, canvasObj, nativeCanvas); 279 280 // unlock surface 281 status_t err = surface->unlockAndPost(); 282 if (err < 0) { 283 doThrowIAE(env); 284 } 285} 286 287// ---------------------------------------------------------------------------- 288 289static jlong nativeCreateFromSurfaceControl(JNIEnv* env, jclass clazz, 290 jlong surfaceControlNativeObj) { 291 /* 292 * This is used by the WindowManagerService just after constructing 293 * a Surface and is necessary for returning the Surface reference to 294 * the caller. At this point, we should only have a SurfaceControl. 295 */ 296 297 sp<SurfaceControl> ctrl(reinterpret_cast<SurfaceControl *>(surfaceControlNativeObj)); 298 sp<Surface> surface(ctrl->getSurface()); 299 if (surface != NULL) { 300 surface->incStrong(&sRefBaseOwner); 301 } 302 return reinterpret_cast<jlong>(surface.get()); 303} 304 305static jlong nativeReadFromParcel(JNIEnv* env, jclass clazz, 306 jlong nativeObject, jobject parcelObj) { 307 Parcel* parcel = parcelForJavaObject(env, parcelObj); 308 if (parcel == NULL) { 309 doThrowNPE(env); 310 return 0; 311 } 312 313 sp<Surface> self(reinterpret_cast<Surface *>(nativeObject)); 314 sp<IBinder> binder(parcel->readStrongBinder()); 315 316 // update the Surface only if the underlying IGraphicBufferProducer 317 // has changed. 318 if (self != NULL 319 && (self->getIGraphicBufferProducer()->asBinder() == binder)) { 320 // same IGraphicBufferProducer, return ourselves 321 return jlong(self.get()); 322 } 323 324 sp<Surface> sur; 325 sp<IGraphicBufferProducer> gbp(interface_cast<IGraphicBufferProducer>(binder)); 326 if (gbp != NULL) { 327 // we have a new IGraphicBufferProducer, create a new Surface for it 328 sur = new Surface(gbp, true); 329 // and keep a reference before passing to java 330 sur->incStrong(&sRefBaseOwner); 331 } 332 333 if (self != NULL) { 334 // and loose the java reference to ourselves 335 self->decStrong(&sRefBaseOwner); 336 } 337 338 return jlong(sur.get()); 339} 340 341static void nativeWriteToParcel(JNIEnv* env, jclass clazz, 342 jlong nativeObject, jobject parcelObj) { 343 Parcel* parcel = parcelForJavaObject(env, parcelObj); 344 if (parcel == NULL) { 345 doThrowNPE(env); 346 return; 347 } 348 sp<Surface> self(reinterpret_cast<Surface *>(nativeObject)); 349 parcel->writeStrongBinder( self != 0 ? self->getIGraphicBufferProducer()->asBinder() : NULL); 350} 351 352// ---------------------------------------------------------------------------- 353 354static JNINativeMethod gSurfaceMethods[] = { 355 {"nativeCreateFromSurfaceTexture", "(Landroid/graphics/SurfaceTexture;)J", 356 (void*)nativeCreateFromSurfaceTexture }, 357 {"nativeRelease", "(J)V", 358 (void*)nativeRelease }, 359 {"nativeIsValid", "(J)Z", 360 (void*)nativeIsValid }, 361 {"nativeIsConsumerRunningBehind", "(J)Z", 362 (void*)nativeIsConsumerRunningBehind }, 363 {"nativeLockCanvas", "(JLandroid/graphics/Canvas;Landroid/graphics/Rect;)J", 364 (void*)nativeLockCanvas }, 365 {"nativeUnlockCanvasAndPost", "(JLandroid/graphics/Canvas;)V", 366 (void*)nativeUnlockCanvasAndPost }, 367 {"nativeCreateFromSurfaceControl", "(J)J", 368 (void*)nativeCreateFromSurfaceControl }, 369 {"nativeReadFromParcel", "(JLandroid/os/Parcel;)J", 370 (void*)nativeReadFromParcel }, 371 {"nativeWriteToParcel", "(JLandroid/os/Parcel;)V", 372 (void*)nativeWriteToParcel }, 373}; 374 375int register_android_view_Surface(JNIEnv* env) 376{ 377 int err = AndroidRuntime::registerNativeMethods(env, "android/view/Surface", 378 gSurfaceMethods, NELEM(gSurfaceMethods)); 379 380 jclass clazz = env->FindClass("android/view/Surface"); 381 gSurfaceClassInfo.clazz = jclass(env->NewGlobalRef(clazz)); 382 gSurfaceClassInfo.mNativeObject = 383 env->GetFieldID(gSurfaceClassInfo.clazz, "mNativeObject", "J"); 384 gSurfaceClassInfo.mLock = 385 env->GetFieldID(gSurfaceClassInfo.clazz, "mLock", "Ljava/lang/Object;"); 386 gSurfaceClassInfo.ctor = env->GetMethodID(gSurfaceClassInfo.clazz, "<init>", "(J)V"); 387 388 clazz = env->FindClass("android/graphics/Canvas"); 389 gCanvasClassInfo.mFinalizer = env->GetFieldID(clazz, "mFinalizer", "Landroid/graphics/Canvas$CanvasFinalizer;"); 390 gCanvasClassInfo.mNativeCanvas = env->GetFieldID(clazz, "mNativeCanvas", "J"); 391 gCanvasClassInfo.mSurfaceFormat = env->GetFieldID(clazz, "mSurfaceFormat", "I"); 392 393 clazz = env->FindClass("android/graphics/Canvas$CanvasFinalizer"); 394 gCanvasFinalizerClassInfo.mNativeCanvas = env->GetFieldID(clazz, "mNativeCanvas", "J"); 395 396 clazz = env->FindClass("android/graphics/Rect"); 397 gRectClassInfo.left = env->GetFieldID(clazz, "left", "I"); 398 gRectClassInfo.top = env->GetFieldID(clazz, "top", "I"); 399 gRectClassInfo.right = env->GetFieldID(clazz, "right", "I"); 400 gRectClassInfo.bottom = env->GetFieldID(clazz, "bottom", "I"); 401 402 return err; 403} 404 405}; 406