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