android_view_Surface.cpp revision b251f3d0c619c37cc4e4b8d9f8b95eb377423190
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_RGB_565: return SkBitmap::kRGB_565_Config; 187 default: return SkBitmap::kNo_Config; 188 } 189} 190 191static inline void swapCanvasPtr(JNIEnv* env, jobject canvasObj, SkCanvas* newCanvas) { 192 jobject canvasFinalizerObj = env->GetObjectField(canvasObj, gCanvasClassInfo.mFinalizer); 193 SkCanvas* previousCanvas = reinterpret_cast<SkCanvas*>( 194 env->GetIntField(canvasObj, gCanvasClassInfo.mNativeCanvas)); 195 env->SetIntField(canvasObj, gCanvasClassInfo.mNativeCanvas, (int)newCanvas); 196 env->SetIntField(canvasFinalizerObj, gCanvasFinalizerClassInfo.mNativeCanvas, (int)newCanvas); 197 SkSafeUnref(previousCanvas); 198} 199 200static void nativeLockCanvas(JNIEnv* env, jclass clazz, 201 jint nativeObject, jobject canvasObj, jobject dirtyRectObj) { 202 sp<Surface> surface(reinterpret_cast<Surface *>(nativeObject)); 203 204 if (!isSurfaceValid(surface)) { 205 doThrowIAE(env); 206 return; 207 } 208 209 Rect dirtyRect; 210 Rect* dirtyRectPtr = NULL; 211 212 if (dirtyRectObj) { 213 dirtyRect.left = env->GetIntField(dirtyRectObj, gRectClassInfo.left); 214 dirtyRect.top = env->GetIntField(dirtyRectObj, gRectClassInfo.top); 215 dirtyRect.right = env->GetIntField(dirtyRectObj, gRectClassInfo.right); 216 dirtyRect.bottom = env->GetIntField(dirtyRectObj, gRectClassInfo.bottom); 217 dirtyRectPtr = &dirtyRect; 218 } 219 220 ANativeWindow_Buffer outBuffer; 221 status_t err = surface->lock(&outBuffer, dirtyRectPtr); 222 if (err < 0) { 223 const char* const exception = (err == NO_MEMORY) ? 224 OutOfResourcesException : 225 "java/lang/IllegalArgumentException"; 226 jniThrowException(env, exception, NULL); 227 return; 228 } 229 230 // Associate a SkCanvas object to this surface 231 env->SetIntField(canvasObj, gCanvasClassInfo.mSurfaceFormat, outBuffer.format); 232 233 SkBitmap bitmap; 234 ssize_t bpr = outBuffer.stride * bytesPerPixel(outBuffer.format); 235 bitmap.setConfig(convertPixelFormat(outBuffer.format), outBuffer.width, outBuffer.height, bpr); 236 if (outBuffer.format == PIXEL_FORMAT_RGBX_8888) { 237 bitmap.setIsOpaque(true); 238 } 239 if (outBuffer.width > 0 && outBuffer.height > 0) { 240 bitmap.setPixels(outBuffer.bits); 241 } else { 242 // be safe with an empty bitmap. 243 bitmap.setPixels(NULL); 244 } 245 246 SkCanvas* nativeCanvas = SkNEW_ARGS(SkCanvas, (bitmap)); 247 swapCanvasPtr(env, canvasObj, nativeCanvas); 248 249 if (dirtyRectPtr) { 250 nativeCanvas->clipRect( SkRect::Make(reinterpret_cast<const SkIRect&>(dirtyRect)) ); 251 } 252 253 if (dirtyRectObj) { 254 env->SetIntField(dirtyRectObj, gRectClassInfo.left, dirtyRect.left); 255 env->SetIntField(dirtyRectObj, gRectClassInfo.top, dirtyRect.top); 256 env->SetIntField(dirtyRectObj, gRectClassInfo.right, dirtyRect.right); 257 env->SetIntField(dirtyRectObj, gRectClassInfo.bottom, dirtyRect.bottom); 258 } 259} 260 261static void nativeUnlockCanvasAndPost(JNIEnv* env, jclass clazz, 262 jint nativeObject, jobject canvasObj) { 263 sp<Surface> surface(reinterpret_cast<Surface *>(nativeObject)); 264 if (!isSurfaceValid(surface)) { 265 return; 266 } 267 268 // detach the canvas from the surface 269 SkCanvas* nativeCanvas = SkNEW(SkCanvas); 270 swapCanvasPtr(env, canvasObj, nativeCanvas); 271 272 // unlock surface 273 status_t err = surface->unlockAndPost(); 274 if (err < 0) { 275 doThrowIAE(env); 276 } 277} 278 279// ---------------------------------------------------------------------------- 280 281static jint nativeCreateFromSurfaceControl(JNIEnv* env, jclass clazz, 282 jint surfaceControlNativeObj) { 283 /* 284 * This is used by the WindowManagerService just after constructing 285 * a Surface and is necessary for returning the Surface reference to 286 * the caller. At this point, we should only have a SurfaceControl. 287 */ 288 289 sp<SurfaceControl> ctrl(reinterpret_cast<SurfaceControl *>(surfaceControlNativeObj)); 290 sp<Surface> surface(ctrl->getSurface()); 291 if (surface != NULL) { 292 surface->incStrong(&sRefBaseOwner); 293 } 294 return reinterpret_cast<jint>(surface.get()); 295} 296 297static jint nativeReadFromParcel(JNIEnv* env, jclass clazz, 298 jint nativeObject, jobject parcelObj) { 299 Parcel* parcel = parcelForJavaObject(env, parcelObj); 300 if (parcel == NULL) { 301 doThrowNPE(env); 302 return 0; 303 } 304 305 sp<Surface> self(reinterpret_cast<Surface *>(nativeObject)); 306 sp<IBinder> binder(parcel->readStrongBinder()); 307 308 // update the Surface only if the underlying IGraphicBufferProducer 309 // has changed. 310 if (self != NULL 311 && (self->getIGraphicBufferProducer()->asBinder() == binder)) { 312 // same IGraphicBufferProducer, return ourselves 313 return int(self.get()); 314 } 315 316 sp<Surface> sur; 317 sp<IGraphicBufferProducer> gbp(interface_cast<IGraphicBufferProducer>(binder)); 318 if (gbp != NULL) { 319 // we have a new IGraphicBufferProducer, create a new Surface for it 320 sur = new Surface(gbp, true); 321 // and keep a reference before passing to java 322 sur->incStrong(&sRefBaseOwner); 323 } 324 325 if (self != NULL) { 326 // and loose the java reference to ourselves 327 self->decStrong(&sRefBaseOwner); 328 } 329 330 return int(sur.get()); 331} 332 333static void nativeWriteToParcel(JNIEnv* env, jclass clazz, 334 jint nativeObject, jobject parcelObj) { 335 Parcel* parcel = parcelForJavaObject(env, parcelObj); 336 if (parcel == NULL) { 337 doThrowNPE(env); 338 return; 339 } 340 sp<Surface> self(reinterpret_cast<Surface *>(nativeObject)); 341 parcel->writeStrongBinder( self != 0 ? self->getIGraphicBufferProducer()->asBinder() : NULL); 342} 343 344// ---------------------------------------------------------------------------- 345 346static JNINativeMethod gSurfaceMethods[] = { 347 {"nativeCreateFromSurfaceTexture", "(Landroid/graphics/SurfaceTexture;)I", 348 (void*)nativeCreateFromSurfaceTexture }, 349 {"nativeRelease", "(I)V", 350 (void*)nativeRelease }, 351 {"nativeIsValid", "(I)Z", 352 (void*)nativeIsValid }, 353 {"nativeIsConsumerRunningBehind", "(I)Z", 354 (void*)nativeIsConsumerRunningBehind }, 355 {"nativeLockCanvas", "(ILandroid/graphics/Canvas;Landroid/graphics/Rect;)V", 356 (void*)nativeLockCanvas }, 357 {"nativeUnlockCanvasAndPost", "(ILandroid/graphics/Canvas;)V", 358 (void*)nativeUnlockCanvasAndPost }, 359 {"nativeCreateFromSurfaceControl", "(I)I", 360 (void*)nativeCreateFromSurfaceControl }, 361 {"nativeReadFromParcel", "(ILandroid/os/Parcel;)I", 362 (void*)nativeReadFromParcel }, 363 {"nativeWriteToParcel", "(ILandroid/os/Parcel;)V", 364 (void*)nativeWriteToParcel }, 365}; 366 367int register_android_view_Surface(JNIEnv* env) 368{ 369 int err = AndroidRuntime::registerNativeMethods(env, "android/view/Surface", 370 gSurfaceMethods, NELEM(gSurfaceMethods)); 371 372 jclass clazz = env->FindClass("android/view/Surface"); 373 gSurfaceClassInfo.clazz = jclass(env->NewGlobalRef(clazz)); 374 gSurfaceClassInfo.mNativeObject = 375 env->GetFieldID(gSurfaceClassInfo.clazz, "mNativeObject", "I"); 376 gSurfaceClassInfo.mLock = 377 env->GetFieldID(gSurfaceClassInfo.clazz, "mLock", "Ljava/lang/Object;"); 378 gSurfaceClassInfo.ctor = env->GetMethodID(gSurfaceClassInfo.clazz, "<init>", "(I)V"); 379 380 clazz = env->FindClass("android/graphics/Canvas"); 381 gCanvasClassInfo.mFinalizer = env->GetFieldID(clazz, "mFinalizer", "Landroid/graphics/Canvas$CanvasFinalizer;"); 382 gCanvasClassInfo.mNativeCanvas = env->GetFieldID(clazz, "mNativeCanvas", "I"); 383 gCanvasClassInfo.mSurfaceFormat = env->GetFieldID(clazz, "mSurfaceFormat", "I"); 384 385 clazz = env->FindClass("android/graphics/Canvas$CanvasFinalizer"); 386 gCanvasFinalizerClassInfo.mNativeCanvas = env->GetFieldID(clazz, "mNativeCanvas", "I"); 387 388 clazz = env->FindClass("android/graphics/Rect"); 389 gRectClassInfo.left = env->GetFieldID(clazz, "left", "I"); 390 gRectClassInfo.top = env->GetFieldID(clazz, "top", "I"); 391 gRectClassInfo.right = env->GetFieldID(clazz, "right", "I"); 392 gRectClassInfo.bottom = env->GetFieldID(clazz, "bottom", "I"); 393 394 return err; 395} 396 397}; 398