android_view_Surface.cpp revision 1125d1fa92ab9f3b8315bbfb72e038b62dfd454b
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#include <AnimationContext.h> 51#include <DisplayListRenderer.h> 52#include <RenderNode.h> 53#include <renderthread/RenderProxy.h> 54 55// ---------------------------------------------------------------------------- 56 57namespace android { 58 59static const char* const OutOfResourcesException = 60 "android/view/Surface$OutOfResourcesException"; 61 62static struct { 63 jclass clazz; 64 jfieldID mNativeObject; 65 jfieldID mLock; 66 jmethodID ctor; 67} gSurfaceClassInfo; 68 69static struct { 70 jfieldID left; 71 jfieldID top; 72 jfieldID right; 73 jfieldID bottom; 74} gRectClassInfo; 75 76static struct { 77 jfieldID mSurfaceFormat; 78 jmethodID setNativeBitmap; 79} gCanvasClassInfo; 80 81// ---------------------------------------------------------------------------- 82 83// this is just a pointer we use to pass to inc/decStrong 84static const void *sRefBaseOwner; 85 86bool android_view_Surface_isInstanceOf(JNIEnv* env, jobject obj) { 87 return env->IsInstanceOf(obj, gSurfaceClassInfo.clazz); 88} 89 90sp<ANativeWindow> android_view_Surface_getNativeWindow(JNIEnv* env, jobject surfaceObj) { 91 return android_view_Surface_getSurface(env, surfaceObj); 92} 93 94sp<Surface> android_view_Surface_getSurface(JNIEnv* env, jobject surfaceObj) { 95 sp<Surface> sur; 96 jobject lock = env->GetObjectField(surfaceObj, 97 gSurfaceClassInfo.mLock); 98 if (env->MonitorEnter(lock) == JNI_OK) { 99 sur = reinterpret_cast<Surface *>( 100 env->GetLongField(surfaceObj, gSurfaceClassInfo.mNativeObject)); 101 env->MonitorExit(lock); 102 } 103 env->DeleteLocalRef(lock); 104 return sur; 105} 106 107jobject android_view_Surface_createFromIGraphicBufferProducer(JNIEnv* env, 108 const sp<IGraphicBufferProducer>& bufferProducer) { 109 if (bufferProducer == NULL) { 110 return NULL; 111 } 112 113 sp<Surface> surface(new Surface(bufferProducer, true)); 114 if (surface == NULL) { 115 return NULL; 116 } 117 118 jobject surfaceObj = env->NewObject(gSurfaceClassInfo.clazz, 119 gSurfaceClassInfo.ctor, (jlong)surface.get()); 120 if (surfaceObj == NULL) { 121 if (env->ExceptionCheck()) { 122 ALOGE("Could not create instance of Surface from IGraphicBufferProducer."); 123 LOGE_EX(env); 124 env->ExceptionClear(); 125 } 126 return NULL; 127 } 128 surface->incStrong(&sRefBaseOwner); 129 return surfaceObj; 130} 131 132// ---------------------------------------------------------------------------- 133 134static inline bool isSurfaceValid(const sp<Surface>& sur) { 135 return Surface::isValid(sur); 136} 137 138// ---------------------------------------------------------------------------- 139 140static jlong nativeCreateFromSurfaceTexture(JNIEnv* env, jclass clazz, 141 jobject surfaceTextureObj) { 142 sp<IGraphicBufferProducer> producer(SurfaceTexture_getProducer(env, surfaceTextureObj)); 143 if (producer == NULL) { 144 jniThrowException(env, "java/lang/IllegalArgumentException", 145 "SurfaceTexture has already been released"); 146 return 0; 147 } 148 149 sp<Surface> surface(new Surface(producer, true)); 150 if (surface == NULL) { 151 jniThrowException(env, OutOfResourcesException, NULL); 152 return 0; 153 } 154 155 surface->incStrong(&sRefBaseOwner); 156 return jlong(surface.get()); 157} 158 159static void nativeRelease(JNIEnv* env, jclass clazz, jlong nativeObject) { 160 sp<Surface> sur(reinterpret_cast<Surface *>(nativeObject)); 161 sur->decStrong(&sRefBaseOwner); 162} 163 164static jboolean nativeIsValid(JNIEnv* env, jclass clazz, jlong nativeObject) { 165 sp<Surface> sur(reinterpret_cast<Surface *>(nativeObject)); 166 return isSurfaceValid(sur) ? JNI_TRUE : JNI_FALSE; 167} 168 169static jboolean nativeIsConsumerRunningBehind(JNIEnv* env, jclass clazz, jlong nativeObject) { 170 sp<Surface> sur(reinterpret_cast<Surface *>(nativeObject)); 171 if (!isSurfaceValid(sur)) { 172 doThrowIAE(env); 173 return JNI_FALSE; 174 } 175 int value = 0; 176 ANativeWindow* anw = static_cast<ANativeWindow*>(sur.get()); 177 anw->query(anw, NATIVE_WINDOW_CONSUMER_RUNNING_BEHIND, &value); 178 return value; 179} 180 181static inline SkColorType convertPixelFormat(PixelFormat format) { 182 /* note: if PIXEL_FORMAT_RGBX_8888 means that all alpha bytes are 0xFF, then 183 we can map to kN32_SkColorType, and optionally call 184 bitmap.setAlphaType(kOpaque_SkAlphaType) on the resulting SkBitmap 185 (as an accelerator) 186 */ 187 switch (format) { 188 case PIXEL_FORMAT_RGBX_8888: return kN32_SkColorType; 189 case PIXEL_FORMAT_RGBA_8888: return kN32_SkColorType; 190 case PIXEL_FORMAT_RGB_565: return kRGB_565_SkColorType; 191 default: return kUnknown_SkColorType; 192 } 193} 194 195static jlong nativeLockCanvas(JNIEnv* env, jclass clazz, 196 jlong nativeObject, jobject canvasObj, jobject dirtyRectObj) { 197 sp<Surface> surface(reinterpret_cast<Surface *>(nativeObject)); 198 199 if (!isSurfaceValid(surface)) { 200 doThrowIAE(env); 201 return 0; 202 } 203 204 Rect dirtyRect; 205 Rect* dirtyRectPtr = NULL; 206 207 if (dirtyRectObj) { 208 dirtyRect.left = env->GetIntField(dirtyRectObj, gRectClassInfo.left); 209 dirtyRect.top = env->GetIntField(dirtyRectObj, gRectClassInfo.top); 210 dirtyRect.right = env->GetIntField(dirtyRectObj, gRectClassInfo.right); 211 dirtyRect.bottom = env->GetIntField(dirtyRectObj, gRectClassInfo.bottom); 212 dirtyRectPtr = &dirtyRect; 213 } 214 215 ANativeWindow_Buffer outBuffer; 216 status_t err = surface->lock(&outBuffer, dirtyRectPtr); 217 if (err < 0) { 218 const char* const exception = (err == NO_MEMORY) ? 219 OutOfResourcesException : 220 "java/lang/IllegalArgumentException"; 221 jniThrowException(env, exception, NULL); 222 return 0; 223 } 224 225 // Associate a SkCanvas object to this surface 226 env->SetIntField(canvasObj, gCanvasClassInfo.mSurfaceFormat, outBuffer.format); 227 228 SkImageInfo info = SkImageInfo::Make(outBuffer.width, outBuffer.height, 229 convertPixelFormat(outBuffer.format), 230 kPremul_SkAlphaType); 231 if (outBuffer.format == PIXEL_FORMAT_RGBX_8888) { 232 info.fAlphaType = kOpaque_SkAlphaType; 233 } 234 235 SkBitmap bitmap; 236 ssize_t bpr = outBuffer.stride * bytesPerPixel(outBuffer.format); 237 bitmap.setInfo(info, bpr); 238 if (outBuffer.width > 0 && outBuffer.height > 0) { 239 bitmap.setPixels(outBuffer.bits); 240 } else { 241 // be safe with an empty bitmap. 242 bitmap.setPixels(NULL); 243 } 244 245 env->CallVoidMethod(canvasObj, gCanvasClassInfo.setNativeBitmap, 246 reinterpret_cast<jlong>(&bitmap)); 247 248 if (dirtyRectPtr) { 249 SkCanvas* nativeCanvas = GraphicsJNI::getNativeCanvas(env, canvasObj); 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 // Create another reference to the surface and return it. This reference 261 // should be passed to nativeUnlockCanvasAndPost in place of mNativeObject, 262 // because the latter could be replaced while the surface is locked. 263 sp<Surface> lockedSurface(surface); 264 lockedSurface->incStrong(&sRefBaseOwner); 265 return (jlong) lockedSurface.get(); 266} 267 268static void nativeUnlockCanvasAndPost(JNIEnv* env, jclass clazz, 269 jlong nativeObject, jobject canvasObj) { 270 sp<Surface> surface(reinterpret_cast<Surface *>(nativeObject)); 271 if (!isSurfaceValid(surface)) { 272 return; 273 } 274 275 // detach the canvas from the surface 276 env->CallVoidMethod(canvasObj, gCanvasClassInfo.setNativeBitmap, (jlong)0); 277 278 // unlock surface 279 status_t err = surface->unlockAndPost(); 280 if (err < 0) { 281 doThrowIAE(env); 282 } 283} 284 285static void nativeAllocateBuffers(JNIEnv* /* env */ , jclass /* clazz */, 286 jlong nativeObject) { 287 sp<Surface> surface(reinterpret_cast<Surface *>(nativeObject)); 288 if (!isSurfaceValid(surface)) { 289 return; 290 } 291 292 surface->allocateBuffers(); 293} 294 295// ---------------------------------------------------------------------------- 296 297static jlong nativeCreateFromSurfaceControl(JNIEnv* env, jclass clazz, 298 jlong surfaceControlNativeObj) { 299 /* 300 * This is used by the WindowManagerService just after constructing 301 * a Surface and is necessary for returning the Surface reference to 302 * the caller. At this point, we should only have a SurfaceControl. 303 */ 304 305 sp<SurfaceControl> ctrl(reinterpret_cast<SurfaceControl *>(surfaceControlNativeObj)); 306 sp<Surface> surface(ctrl->getSurface()); 307 if (surface != NULL) { 308 surface->incStrong(&sRefBaseOwner); 309 } 310 return reinterpret_cast<jlong>(surface.get()); 311} 312 313static jlong nativeReadFromParcel(JNIEnv* env, jclass clazz, 314 jlong nativeObject, jobject parcelObj) { 315 Parcel* parcel = parcelForJavaObject(env, parcelObj); 316 if (parcel == NULL) { 317 doThrowNPE(env); 318 return 0; 319 } 320 321 sp<Surface> self(reinterpret_cast<Surface *>(nativeObject)); 322 sp<IBinder> binder(parcel->readStrongBinder()); 323 324 // update the Surface only if the underlying IGraphicBufferProducer 325 // has changed. 326 if (self != NULL 327 && (self->getIGraphicBufferProducer()->asBinder() == binder)) { 328 // same IGraphicBufferProducer, return ourselves 329 return jlong(self.get()); 330 } 331 332 sp<Surface> sur; 333 sp<IGraphicBufferProducer> gbp(interface_cast<IGraphicBufferProducer>(binder)); 334 if (gbp != NULL) { 335 // we have a new IGraphicBufferProducer, create a new Surface for it 336 sur = new Surface(gbp, true); 337 // and keep a reference before passing to java 338 sur->incStrong(&sRefBaseOwner); 339 } 340 341 if (self != NULL) { 342 // and loose the java reference to ourselves 343 self->decStrong(&sRefBaseOwner); 344 } 345 346 return jlong(sur.get()); 347} 348 349static void nativeWriteToParcel(JNIEnv* env, jclass clazz, 350 jlong nativeObject, jobject parcelObj) { 351 Parcel* parcel = parcelForJavaObject(env, parcelObj); 352 if (parcel == NULL) { 353 doThrowNPE(env); 354 return; 355 } 356 sp<Surface> self(reinterpret_cast<Surface *>(nativeObject)); 357 parcel->writeStrongBinder( self != 0 ? self->getIGraphicBufferProducer()->asBinder() : NULL); 358} 359 360namespace uirenderer { 361 362using namespace android::uirenderer::renderthread; 363 364class ContextFactory : public IContextFactory { 365public: 366 virtual AnimationContext* createAnimationContext(renderthread::TimeLord& clock) { 367 return new AnimationContext(clock); 368 } 369}; 370 371static jlong create(JNIEnv* env, jclass clazz, jlong rootNodePtr, jlong surfacePtr) { 372 RenderNode* rootNode = reinterpret_cast<RenderNode*>(rootNodePtr); 373 sp<Surface> surface(reinterpret_cast<Surface*>(surfacePtr)); 374 ContextFactory factory; 375 RenderProxy* proxy = new RenderProxy(false, rootNode, &factory); 376 proxy->loadSystemProperties(); 377 proxy->setSwapBehavior(kSwap_discardBuffer); 378 proxy->initialize(surface); 379 // Shadows can't be used via this interface, so just set the light source 380 // to all 0s. (and width & height are unused, TODO remove them) 381 proxy->setup(0, 0, (Vector3){0, 0, 0}, 0, 0, 0); 382 return (jlong) proxy; 383} 384 385static void setSurface(JNIEnv* env, jclass clazz, jlong rendererPtr, jlong surfacePtr) { 386 RenderProxy* proxy = reinterpret_cast<RenderProxy*>(rendererPtr); 387 sp<Surface> surface(reinterpret_cast<Surface*>(surfacePtr)); 388 proxy->updateSurface(surface); 389} 390 391static void draw(JNIEnv* env, jclass clazz, jlong rendererPtr) { 392 RenderProxy* proxy = reinterpret_cast<RenderProxy*>(rendererPtr); 393 nsecs_t frameTimeNs = systemTime(CLOCK_MONOTONIC); 394 proxy->syncAndDrawFrame(frameTimeNs, 0, 1.0f); 395} 396 397static void destroy(JNIEnv* env, jclass clazz, jlong rendererPtr) { 398 RenderProxy* proxy = reinterpret_cast<RenderProxy*>(rendererPtr); 399 delete proxy; 400} 401 402} // uirenderer 403 404// ---------------------------------------------------------------------------- 405 406namespace hwui = android::uirenderer; 407 408static JNINativeMethod gSurfaceMethods[] = { 409 {"nativeCreateFromSurfaceTexture", "(Landroid/graphics/SurfaceTexture;)J", 410 (void*)nativeCreateFromSurfaceTexture }, 411 {"nativeRelease", "(J)V", 412 (void*)nativeRelease }, 413 {"nativeIsValid", "(J)Z", 414 (void*)nativeIsValid }, 415 {"nativeIsConsumerRunningBehind", "(J)Z", 416 (void*)nativeIsConsumerRunningBehind }, 417 {"nativeLockCanvas", "(JLandroid/graphics/Canvas;Landroid/graphics/Rect;)J", 418 (void*)nativeLockCanvas }, 419 {"nativeUnlockCanvasAndPost", "(JLandroid/graphics/Canvas;)V", 420 (void*)nativeUnlockCanvasAndPost }, 421 {"nativeAllocateBuffers", "(J)V", 422 (void*)nativeAllocateBuffers }, 423 {"nativeCreateFromSurfaceControl", "(J)J", 424 (void*)nativeCreateFromSurfaceControl }, 425 {"nativeReadFromParcel", "(JLandroid/os/Parcel;)J", 426 (void*)nativeReadFromParcel }, 427 {"nativeWriteToParcel", "(JLandroid/os/Parcel;)V", 428 (void*)nativeWriteToParcel }, 429 430 // HWUI context 431 {"nHwuiCreate", "(JJ)J", (void*) hwui::create }, 432 {"nHwuiSetSurface", "(JJ)V", (void*) hwui::setSurface }, 433 {"nHwuiDraw", "(J)V", (void*) hwui::draw }, 434 {"nHwuiDestroy", "(J)V", (void*) hwui::destroy }, 435}; 436 437int register_android_view_Surface(JNIEnv* env) 438{ 439 int err = AndroidRuntime::registerNativeMethods(env, "android/view/Surface", 440 gSurfaceMethods, NELEM(gSurfaceMethods)); 441 442 jclass clazz = env->FindClass("android/view/Surface"); 443 gSurfaceClassInfo.clazz = jclass(env->NewGlobalRef(clazz)); 444 gSurfaceClassInfo.mNativeObject = 445 env->GetFieldID(gSurfaceClassInfo.clazz, "mNativeObject", "J"); 446 gSurfaceClassInfo.mLock = 447 env->GetFieldID(gSurfaceClassInfo.clazz, "mLock", "Ljava/lang/Object;"); 448 gSurfaceClassInfo.ctor = env->GetMethodID(gSurfaceClassInfo.clazz, "<init>", "(J)V"); 449 450 clazz = env->FindClass("android/graphics/Canvas"); 451 gCanvasClassInfo.mSurfaceFormat = env->GetFieldID(clazz, "mSurfaceFormat", "I"); 452 gCanvasClassInfo.setNativeBitmap = env->GetMethodID(clazz, "setNativeBitmap", "(J)V"); 453 454 clazz = env->FindClass("android/graphics/Rect"); 455 gRectClassInfo.left = env->GetFieldID(clazz, "left", "I"); 456 gRectClassInfo.top = env->GetFieldID(clazz, "top", "I"); 457 gRectClassInfo.right = env->GetFieldID(clazz, "right", "I"); 458 gRectClassInfo.bottom = env->GetFieldID(clazz, "bottom", "I"); 459 460 return err; 461} 462 463}; 464