android_view_Surface.cpp revision 3866f0d581ceaa165710feeee9f37fe1b0d7067d
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 <gui/Surface.h> 31#include <gui/GLConsumer.h> 32 33#include <ui/Rect.h> 34#include <ui/Region.h> 35 36#include <SkCanvas.h> 37#include <SkBitmap.h> 38#include <SkRegion.h> 39 40#include <utils/misc.h> 41#include <utils/Log.h> 42 43#include <ScopedUtfChars.h> 44 45// ---------------------------------------------------------------------------- 46 47namespace android { 48 49static const char* const OutOfResourcesException = 50 "android/view/Surface$OutOfResourcesException"; 51 52static struct { 53 jclass clazz; 54 jfieldID mNativeObject; 55 jfieldID mGenerationId; 56 jfieldID mCanvas; 57 jfieldID mCanvasSaveCount; 58 jmethodID ctor; 59} gSurfaceClassInfo; 60 61static struct { 62 jfieldID left; 63 jfieldID top; 64 jfieldID right; 65 jfieldID bottom; 66} gRectClassInfo; 67 68static struct { 69 jfieldID mNativeCanvas; 70 jfieldID mSurfaceFormat; 71} gCanvasClassInfo; 72 73// ---------------------------------------------------------------------------- 74 75bool android_view_Surface_isInstanceOf(JNIEnv* env, jobject obj) { 76 return env->IsInstanceOf(obj, gSurfaceClassInfo.clazz); 77} 78 79sp<ANativeWindow> android_view_Surface_getNativeWindow(JNIEnv* env, jobject surfaceObj) { 80 return android_view_Surface_getSurface(env, surfaceObj); 81} 82 83sp<Surface> android_view_Surface_getSurface(JNIEnv* env, jobject surfaceObj) { 84 return reinterpret_cast<Surface *>( 85 env->GetIntField(surfaceObj, gSurfaceClassInfo.mNativeObject)); 86} 87 88jobject android_view_Surface_createFromISurfaceTexture(JNIEnv* env, 89 const sp<IGraphicBufferProducer>& bufferProducer) { 90 if (bufferProducer == NULL) { 91 return NULL; 92 } 93 94 sp<Surface> surface(new Surface(bufferProducer)); 95 if (surface == NULL) { 96 return NULL; 97 } 98 99 jobject surfaceObj = env->NewObject(gSurfaceClassInfo.clazz, gSurfaceClassInfo.ctor, surface.get()); 100 if (surfaceObj == NULL) { 101 if (env->ExceptionCheck()) { 102 ALOGE("Could not create instance of Surface from IGraphicBufferProducer."); 103 LOGE_EX(env); 104 env->ExceptionClear(); 105 } 106 return NULL; 107 } 108 surface->incStrong(surfaceObj); 109 return surfaceObj; 110} 111 112// ---------------------------------------------------------------------------- 113 114static jint nativeCreateFromSurfaceTexture(JNIEnv* env, jobject surfaceObj, 115 jobject surfaceTextureObj) { 116 sp<GLConsumer> st(SurfaceTexture_getSurfaceTexture(env, surfaceTextureObj)); 117 if (st == NULL) { 118 jniThrowException(env, "java/lang/IllegalArgumentException", 119 "SurfaceTexture has already been released"); 120 return 0; 121 } 122 123 sp<IGraphicBufferProducer> bq = st->getBufferQueue(); 124 sp<Surface> surface(new Surface(bq)); 125 if (surface == NULL) { 126 jniThrowException(env, OutOfResourcesException, NULL); 127 return 0; 128 } 129 130 surface->incStrong(surfaceObj); 131 return int(surface.get()); 132} 133 134static void nativeRelease(JNIEnv* env, jobject surfaceObj, jint nativeObject) { 135 sp<Surface> sur(reinterpret_cast<Surface *>(nativeObject)); 136 sur->decStrong(surfaceObj); 137} 138 139static void nativeDestroy(JNIEnv* env, jobject surfaceObj, jint nativeObject) { 140 sp<Surface> sur(reinterpret_cast<Surface *>(nativeObject)); 141 sur->decStrong(surfaceObj); 142} 143 144static jboolean nativeIsValid(JNIEnv* env, jobject surfaceObj, jint nativeObject) { 145 sp<Surface> sur(reinterpret_cast<Surface *>(nativeObject)); 146 return Surface::isValid(sur) ? JNI_TRUE : JNI_FALSE; 147} 148 149static jboolean nativeIsConsumerRunningBehind(JNIEnv* env, jobject surfaceObj, jint nativeObject) { 150 sp<Surface> sur(reinterpret_cast<Surface *>(nativeObject)); 151 if (!Surface::isValid(sur)) { 152 doThrowIAE(env); 153 return JNI_FALSE; 154 } 155 int value = 0; 156 ANativeWindow* anw = static_cast<ANativeWindow*>(sur.get()); 157 anw->query(anw, NATIVE_WINDOW_CONSUMER_RUNNING_BEHIND, &value); 158 return value; 159} 160 161static inline SkBitmap::Config convertPixelFormat(PixelFormat format) { 162 /* note: if PIXEL_FORMAT_RGBX_8888 means that all alpha bytes are 0xFF, then 163 we can map to SkBitmap::kARGB_8888_Config, and optionally call 164 bitmap.setIsOpaque(true) on the resulting SkBitmap (as an accelerator) 165 */ 166 switch (format) { 167 case PIXEL_FORMAT_RGBX_8888: return SkBitmap::kARGB_8888_Config; 168 case PIXEL_FORMAT_RGBA_8888: return SkBitmap::kARGB_8888_Config; 169 case PIXEL_FORMAT_RGBA_4444: return SkBitmap::kARGB_4444_Config; 170 case PIXEL_FORMAT_RGB_565: return SkBitmap::kRGB_565_Config; 171 case PIXEL_FORMAT_A_8: return SkBitmap::kA8_Config; 172 default: return SkBitmap::kNo_Config; 173 } 174} 175 176static jobject nativeLockCanvas(JNIEnv* env, jobject surfaceObj, jint nativeObject, jobject dirtyRectObj) { 177 sp<Surface> surface(reinterpret_cast<Surface *>(nativeObject)); 178 179 if (!Surface::isValid(surface)) { 180 doThrowIAE(env); 181 return NULL; 182 } 183 184 // get dirty region 185 Region dirtyRegion; 186 if (dirtyRectObj) { 187 Rect dirty; 188 dirty.left = env->GetIntField(dirtyRectObj, gRectClassInfo.left); 189 dirty.top = env->GetIntField(dirtyRectObj, gRectClassInfo.top); 190 dirty.right = env->GetIntField(dirtyRectObj, gRectClassInfo.right); 191 dirty.bottom = env->GetIntField(dirtyRectObj, gRectClassInfo.bottom); 192 if (!dirty.isEmpty()) { 193 dirtyRegion.set(dirty); 194 } 195 } else { 196 dirtyRegion.set(Rect(0x3FFF, 0x3FFF)); 197 } 198 199 Surface::SurfaceInfo info; 200 status_t err = surface->lock(&info, &dirtyRegion); 201 if (err < 0) { 202 const char* const exception = (err == NO_MEMORY) ? 203 OutOfResourcesException : 204 "java/lang/IllegalArgumentException"; 205 jniThrowException(env, exception, NULL); 206 return NULL; 207 } 208 209 // Associate a SkCanvas object to this surface 210 jobject canvasObj = env->GetObjectField(surfaceObj, gSurfaceClassInfo.mCanvas); 211 env->SetIntField(canvasObj, gCanvasClassInfo.mSurfaceFormat, info.format); 212 213 SkCanvas* nativeCanvas = reinterpret_cast<SkCanvas*>( 214 env->GetIntField(canvasObj, gCanvasClassInfo.mNativeCanvas)); 215 SkBitmap bitmap; 216 ssize_t bpr = info.s * bytesPerPixel(info.format); 217 bitmap.setConfig(convertPixelFormat(info.format), info.w, info.h, bpr); 218 if (info.format == PIXEL_FORMAT_RGBX_8888) { 219 bitmap.setIsOpaque(true); 220 } 221 if (info.w > 0 && info.h > 0) { 222 bitmap.setPixels(info.bits); 223 } else { 224 // be safe with an empty bitmap. 225 bitmap.setPixels(NULL); 226 } 227 nativeCanvas->setBitmapDevice(bitmap); 228 229 SkRegion clipReg; 230 if (dirtyRegion.isRect()) { // very common case 231 const Rect b(dirtyRegion.getBounds()); 232 clipReg.setRect(b.left, b.top, b.right, b.bottom); 233 } else { 234 size_t count; 235 Rect const* r = dirtyRegion.getArray(&count); 236 while (count) { 237 clipReg.op(r->left, r->top, r->right, r->bottom, SkRegion::kUnion_Op); 238 r++, count--; 239 } 240 } 241 242 nativeCanvas->clipRegion(clipReg); 243 244 int saveCount = nativeCanvas->save(); 245 env->SetIntField(surfaceObj, gSurfaceClassInfo.mCanvasSaveCount, saveCount); 246 247 if (dirtyRectObj) { 248 const Rect& bounds(dirtyRegion.getBounds()); 249 env->SetIntField(dirtyRectObj, gRectClassInfo.left, bounds.left); 250 env->SetIntField(dirtyRectObj, gRectClassInfo.top, bounds.top); 251 env->SetIntField(dirtyRectObj, gRectClassInfo.right, bounds.right); 252 env->SetIntField(dirtyRectObj, gRectClassInfo.bottom, bounds.bottom); 253 } 254 255 return canvasObj; 256} 257 258static void nativeUnlockCanvasAndPost(JNIEnv* env, jobject surfaceObj, jint nativeObject, jobject canvasObj) { 259 jobject ownCanvasObj = env->GetObjectField(surfaceObj, gSurfaceClassInfo.mCanvas); 260 if (!env->IsSameObject(ownCanvasObj, canvasObj)) { 261 doThrowIAE(env); 262 return; 263 } 264 265 sp<Surface> surface(reinterpret_cast<Surface *>(nativeObject)); 266 if (!Surface::isValid(surface)) { 267 return; 268 } 269 270 // detach the canvas from the surface 271 SkCanvas* nativeCanvas = reinterpret_cast<SkCanvas*>( 272 env->GetIntField(canvasObj, gCanvasClassInfo.mNativeCanvas)); 273 int saveCount = env->GetIntField(surfaceObj, gSurfaceClassInfo.mCanvasSaveCount); 274 nativeCanvas->restoreToCount(saveCount); 275 nativeCanvas->setBitmapDevice(SkBitmap()); 276 env->SetIntField(surfaceObj, gSurfaceClassInfo.mCanvasSaveCount, 0); 277 278 // unlock surface 279 status_t err = surface->unlockAndPost(); 280 if (err < 0) { 281 doThrowIAE(env); 282 } 283} 284 285// ---------------------------------------------------------------------------- 286 287static jint nativeCopyFrom(JNIEnv* env, jobject surfaceObj, 288 jint nativeObject, jint surfaceControlNativeObj) { 289 /* 290 * This is used by the WindowManagerService just after constructing 291 * a Surface and is necessary for returning the Surface reference to 292 * the caller. At this point, we should only have a SurfaceControl. 293 */ 294 295 sp<SurfaceControl> ctrl(reinterpret_cast<SurfaceControl *>(surfaceControlNativeObj)); 296 sp<Surface> other(ctrl->getSurface()); 297 if (other != NULL) { 298 other->incStrong(surfaceObj); 299 } 300 301 sp<Surface> sur(reinterpret_cast<Surface *>(nativeObject)); 302 if (sur != NULL) { 303 sur->decStrong(surfaceObj); 304 } 305 306 return int(other.get()); 307} 308 309static jint nativeReadFromParcel(JNIEnv* env, jobject surfaceObj, 310 jint nativeObject, jobject parcelObj) { 311 Parcel* parcel = parcelForJavaObject(env, parcelObj); 312 if (parcel == NULL) { 313 doThrowNPE(env); 314 return 0; 315 } 316 sp<Surface> self(reinterpret_cast<Surface *>(nativeObject)); 317 if (self != NULL) { 318 self->decStrong(surfaceObj); 319 } 320 sp<Surface> sur(Surface::readFromParcel(*parcel)); 321 if (sur != NULL) { 322 sur->incStrong(surfaceObj); 323 } 324 return int(sur.get()); 325} 326 327static void nativeWriteToParcel(JNIEnv* env, jobject surfaceObj, 328 jint nativeObject, jobject parcelObj) { 329 Parcel* parcel = parcelForJavaObject(env, parcelObj); 330 if (parcel == NULL) { 331 doThrowNPE(env); 332 return; 333 } 334 sp<Surface> self(reinterpret_cast<Surface *>(nativeObject)); 335 Surface::writeToParcel(self, parcel); 336} 337 338// ---------------------------------------------------------------------------- 339 340static JNINativeMethod gSurfaceMethods[] = { 341 {"nativeCreateFromSurfaceTexture", "(Landroid/graphics/SurfaceTexture;)I", 342 (void*)nativeCreateFromSurfaceTexture }, 343 {"nativeRelease", "(I)V", 344 (void*)nativeRelease }, 345 {"nativeDestroy", "(I)V", 346 (void*)nativeDestroy }, 347 {"nativeIsValid", "(I)Z", 348 (void*)nativeIsValid }, 349 {"nativeIsConsumerRunningBehind", "(I)Z", 350 (void*)nativeIsConsumerRunningBehind }, 351 {"nativeLockCanvas", "(ILandroid/graphics/Rect;)Landroid/graphics/Canvas;", 352 (void*)nativeLockCanvas }, 353 {"nativeUnlockCanvasAndPost", "(ILandroid/graphics/Canvas;)V", 354 (void*)nativeUnlockCanvasAndPost }, 355 {"nativeCopyFrom", "(II)I", 356 (void*)nativeCopyFrom }, 357 {"nativeReadFromParcel", "(ILandroid/os/Parcel;)I", 358 (void*)nativeReadFromParcel }, 359 {"nativeWriteToParcel", "(ILandroid/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", "I"); 372 gSurfaceClassInfo.mGenerationId = 373 env->GetFieldID(gSurfaceClassInfo.clazz, "mGenerationId", "I"); 374 gSurfaceClassInfo.mCanvas = 375 env->GetFieldID(gSurfaceClassInfo.clazz, "mCanvas", "Landroid/graphics/Canvas;"); 376 gSurfaceClassInfo.mCanvasSaveCount = 377 env->GetFieldID(gSurfaceClassInfo.clazz, "mCanvasSaveCount", "I"); 378 gSurfaceClassInfo.ctor = env->GetMethodID(gSurfaceClassInfo.clazz, "<init>", "(I)V"); 379 380 clazz = env->FindClass("android/graphics/Canvas"); 381 gCanvasClassInfo.mNativeCanvas = env->GetFieldID(clazz, "mNativeCanvas", "I"); 382 gCanvasClassInfo.mSurfaceFormat = env->GetFieldID(clazz, "mSurfaceFormat", "I"); 383 384 clazz = env->FindClass("android/graphics/Rect"); 385 gRectClassInfo.left = env->GetFieldID(clazz, "left", "I"); 386 gRectClassInfo.top = env->GetFieldID(clazz, "top", "I"); 387 gRectClassInfo.right = env->GetFieldID(clazz, "right", "I"); 388 gRectClassInfo.bottom = env->GetFieldID(clazz, "bottom", "I"); 389 390 return err; 391} 392 393}; 394