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