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 52 namespace android { 53 54 static const char* const OutOfResourcesException = 55 "android/view/Surface$OutOfResourcesException"; 56 57 static struct { 58 jclass clazz; 59 jfieldID mNativeObject; 60 jfieldID mLock; 61 jmethodID ctor; 62 } gSurfaceClassInfo; 63 64 static struct { 65 jfieldID left; 66 jfieldID top; 67 jfieldID right; 68 jfieldID bottom; 69 } gRectClassInfo; 70 71 static 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 79 static const void *sRefBaseOwner; 80 81 bool android_view_Surface_isInstanceOf(JNIEnv* env, jobject obj) { 82 return env->IsInstanceOf(obj, gSurfaceClassInfo.clazz); 83 } 84 85 sp<ANativeWindow> android_view_Surface_getNativeWindow(JNIEnv* env, jobject surfaceObj) { 86 return android_view_Surface_getSurface(env, surfaceObj); 87 } 88 89 sp<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 102 jobject 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 129 static inline bool isSurfaceValid(const sp<Surface>& sur) { 130 return Surface::isValid(sur); 131 } 132 133 // ---------------------------------------------------------------------------- 134 135 static 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 154 static void nativeRelease(JNIEnv* env, jclass clazz, jlong nativeObject) { 155 sp<Surface> sur(reinterpret_cast<Surface *>(nativeObject)); 156 sur->decStrong(&sRefBaseOwner); 157 } 158 159 static 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 164 static 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 176 static inline SkColorType convertPixelFormat(PixelFormat format) { 177 /* note: if PIXEL_FORMAT_RGBX_8888 means that all alpha bytes are 0xFF, then 178 we can map to kN32_SkColorType, 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 kN32_SkColorType; 184 case PIXEL_FORMAT_RGBA_8888: return kN32_SkColorType; 185 case PIXEL_FORMAT_RGB_565: return kRGB_565_SkColorType; 186 default: return kUnknown_SkColorType; 187 } 188 } 189 190 static 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 SkImageInfo info = SkImageInfo::Make(outBuffer.width, outBuffer.height, 224 convertPixelFormat(outBuffer.format), 225 kPremul_SkAlphaType); 226 if (outBuffer.format == PIXEL_FORMAT_RGBX_8888) { 227 info.fAlphaType = kOpaque_SkAlphaType; 228 } 229 230 SkBitmap bitmap; 231 ssize_t bpr = outBuffer.stride * bytesPerPixel(outBuffer.format); 232 bitmap.setInfo(info, bpr); 233 if (outBuffer.width > 0 && outBuffer.height > 0) { 234 bitmap.setPixels(outBuffer.bits); 235 } else { 236 // be safe with an empty bitmap. 237 bitmap.setPixels(NULL); 238 } 239 240 env->CallVoidMethod(canvasObj, gCanvasClassInfo.setNativeBitmap, 241 reinterpret_cast<jlong>(&bitmap)); 242 243 if (dirtyRectPtr) { 244 SkCanvas* nativeCanvas = GraphicsJNI::getNativeCanvas(env, canvasObj); 245 nativeCanvas->clipRect( SkRect::Make(reinterpret_cast<const SkIRect&>(dirtyRect)) ); 246 } 247 248 if (dirtyRectObj) { 249 env->SetIntField(dirtyRectObj, gRectClassInfo.left, dirtyRect.left); 250 env->SetIntField(dirtyRectObj, gRectClassInfo.top, dirtyRect.top); 251 env->SetIntField(dirtyRectObj, gRectClassInfo.right, dirtyRect.right); 252 env->SetIntField(dirtyRectObj, gRectClassInfo.bottom, dirtyRect.bottom); 253 } 254 255 // Create another reference to the surface and return it. This reference 256 // should be passed to nativeUnlockCanvasAndPost in place of mNativeObject, 257 // because the latter could be replaced while the surface is locked. 258 sp<Surface> lockedSurface(surface); 259 lockedSurface->incStrong(&sRefBaseOwner); 260 return (jlong) lockedSurface.get(); 261 } 262 263 static void nativeUnlockCanvasAndPost(JNIEnv* env, jclass clazz, 264 jlong nativeObject, jobject canvasObj) { 265 sp<Surface> surface(reinterpret_cast<Surface *>(nativeObject)); 266 if (!isSurfaceValid(surface)) { 267 return; 268 } 269 270 // detach the canvas from the surface 271 env->CallVoidMethod(canvasObj, gCanvasClassInfo.setNativeBitmap, (jlong)0); 272 273 // unlock surface 274 status_t err = surface->unlockAndPost(); 275 if (err < 0) { 276 doThrowIAE(env); 277 } 278 } 279 280 static void nativeAllocateBuffers(JNIEnv* /* env */ , jclass /* clazz */, 281 jlong nativeObject) { 282 sp<Surface> surface(reinterpret_cast<Surface *>(nativeObject)); 283 if (!isSurfaceValid(surface)) { 284 return; 285 } 286 287 surface->allocateBuffers(); 288 } 289 290 // ---------------------------------------------------------------------------- 291 292 static jlong nativeCreateFromSurfaceControl(JNIEnv* env, jclass clazz, 293 jlong surfaceControlNativeObj) { 294 /* 295 * This is used by the WindowManagerService just after constructing 296 * a Surface and is necessary for returning the Surface reference to 297 * the caller. At this point, we should only have a SurfaceControl. 298 */ 299 300 sp<SurfaceControl> ctrl(reinterpret_cast<SurfaceControl *>(surfaceControlNativeObj)); 301 sp<Surface> surface(ctrl->getSurface()); 302 if (surface != NULL) { 303 surface->incStrong(&sRefBaseOwner); 304 } 305 return reinterpret_cast<jlong>(surface.get()); 306 } 307 308 static jlong nativeReadFromParcel(JNIEnv* env, jclass clazz, 309 jlong nativeObject, jobject parcelObj) { 310 Parcel* parcel = parcelForJavaObject(env, parcelObj); 311 if (parcel == NULL) { 312 doThrowNPE(env); 313 return 0; 314 } 315 316 sp<Surface> self(reinterpret_cast<Surface *>(nativeObject)); 317 sp<IBinder> binder(parcel->readStrongBinder()); 318 319 // update the Surface only if the underlying IGraphicBufferProducer 320 // has changed. 321 if (self != NULL 322 && (self->getIGraphicBufferProducer()->asBinder() == binder)) { 323 // same IGraphicBufferProducer, return ourselves 324 return jlong(self.get()); 325 } 326 327 sp<Surface> sur; 328 sp<IGraphicBufferProducer> gbp(interface_cast<IGraphicBufferProducer>(binder)); 329 if (gbp != NULL) { 330 // we have a new IGraphicBufferProducer, create a new Surface for it 331 sur = new Surface(gbp, true); 332 // and keep a reference before passing to java 333 sur->incStrong(&sRefBaseOwner); 334 } 335 336 if (self != NULL) { 337 // and loose the java reference to ourselves 338 self->decStrong(&sRefBaseOwner); 339 } 340 341 return jlong(sur.get()); 342 } 343 344 static void nativeWriteToParcel(JNIEnv* env, jclass clazz, 345 jlong nativeObject, jobject parcelObj) { 346 Parcel* parcel = parcelForJavaObject(env, parcelObj); 347 if (parcel == NULL) { 348 doThrowNPE(env); 349 return; 350 } 351 sp<Surface> self(reinterpret_cast<Surface *>(nativeObject)); 352 parcel->writeStrongBinder( self != 0 ? self->getIGraphicBufferProducer()->asBinder() : NULL); 353 } 354 355 // ---------------------------------------------------------------------------- 356 357 static JNINativeMethod gSurfaceMethods[] = { 358 {"nativeCreateFromSurfaceTexture", "(Landroid/graphics/SurfaceTexture;)J", 359 (void*)nativeCreateFromSurfaceTexture }, 360 {"nativeRelease", "(J)V", 361 (void*)nativeRelease }, 362 {"nativeIsValid", "(J)Z", 363 (void*)nativeIsValid }, 364 {"nativeIsConsumerRunningBehind", "(J)Z", 365 (void*)nativeIsConsumerRunningBehind }, 366 {"nativeLockCanvas", "(JLandroid/graphics/Canvas;Landroid/graphics/Rect;)J", 367 (void*)nativeLockCanvas }, 368 {"nativeUnlockCanvasAndPost", "(JLandroid/graphics/Canvas;)V", 369 (void*)nativeUnlockCanvasAndPost }, 370 {"nativeAllocateBuffers", "(J)V", 371 (void*)nativeAllocateBuffers }, 372 {"nativeCreateFromSurfaceControl", "(J)J", 373 (void*)nativeCreateFromSurfaceControl }, 374 {"nativeReadFromParcel", "(JLandroid/os/Parcel;)J", 375 (void*)nativeReadFromParcel }, 376 {"nativeWriteToParcel", "(JLandroid/os/Parcel;)V", 377 (void*)nativeWriteToParcel }, 378 }; 379 380 int register_android_view_Surface(JNIEnv* env) 381 { 382 int err = AndroidRuntime::registerNativeMethods(env, "android/view/Surface", 383 gSurfaceMethods, NELEM(gSurfaceMethods)); 384 385 jclass clazz = env->FindClass("android/view/Surface"); 386 gSurfaceClassInfo.clazz = jclass(env->NewGlobalRef(clazz)); 387 gSurfaceClassInfo.mNativeObject = 388 env->GetFieldID(gSurfaceClassInfo.clazz, "mNativeObject", "J"); 389 gSurfaceClassInfo.mLock = 390 env->GetFieldID(gSurfaceClassInfo.clazz, "mLock", "Ljava/lang/Object;"); 391 gSurfaceClassInfo.ctor = env->GetMethodID(gSurfaceClassInfo.clazz, "<init>", "(J)V"); 392 393 clazz = env->FindClass("android/graphics/Canvas"); 394 gCanvasClassInfo.mSurfaceFormat = env->GetFieldID(clazz, "mSurfaceFormat", "I"); 395 gCanvasClassInfo.setNativeBitmap = env->GetMethodID(clazz, "setNativeBitmap", "(J)V"); 396 397 clazz = env->FindClass("android/graphics/Rect"); 398 gRectClassInfo.left = env->GetFieldID(clazz, "left", "I"); 399 gRectClassInfo.top = env->GetFieldID(clazz, "top", "I"); 400 gRectClassInfo.right = env->GetFieldID(clazz, "right", "I"); 401 gRectClassInfo.bottom = env->GetFieldID(clazz, "bottom", "I"); 402 403 return err; 404 } 405 406 }; 407