android_view_Surface.cpp revision ef4e817ad642303509da1cb28ce5a265e1ac3e17
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 "android_util_Binder.h" 22#include "android/graphics/GraphicsJNI.h" 23 24#include <binder/IMemory.h> 25 26#include <gui/Surface.h> 27#include <gui/SurfaceComposerClient.h> 28#include <gui/SurfaceTexture.h> 29 30#include <ui/Rect.h> 31#include <ui/Region.h> 32 33#include <EGL/egl.h> 34 35#include <SkCanvas.h> 36#include <SkBitmap.h> 37#include <SkRegion.h> 38#include <SkPixelRef.h> 39 40#include "jni.h" 41#include "JNIHelp.h" 42#include <android_runtime/AndroidRuntime.h> 43#include <android_runtime/android_view_Surface.h> 44#include <android_runtime/android_graphics_SurfaceTexture.h> 45#include <utils/misc.h> 46 47 48// ---------------------------------------------------------------------------- 49 50namespace android { 51 52enum { 53 // should match Parcelable.java 54 PARCELABLE_WRITE_RETURN_VALUE = 0x0001 55}; 56 57// ---------------------------------------------------------------------------- 58 59static const char* const OutOfResourcesException = 60 "android/view/Surface$OutOfResourcesException"; 61 62const char* const kSurfaceSessionClassPathName = "android/view/SurfaceSession"; 63const char* const kSurfaceClassPathName = "android/view/Surface"; 64 65struct sso_t { 66 jfieldID client; 67}; 68static sso_t sso; 69 70struct so_t { 71 jfieldID surfaceControl; 72 jfieldID surfaceGenerationId; 73 jfieldID surface; 74 jfieldID saveCount; 75 jfieldID canvas; 76}; 77static so_t so; 78 79struct ro_t { 80 jfieldID l; 81 jfieldID t; 82 jfieldID r; 83 jfieldID b; 84}; 85static ro_t ro; 86 87struct po_t { 88 jfieldID x; 89 jfieldID y; 90}; 91static po_t po; 92 93struct co_t { 94 jfieldID surfaceFormat; 95}; 96static co_t co; 97 98struct no_t { 99 jfieldID native_canvas; 100 jfieldID native_region; 101 jfieldID native_parcel; 102}; 103static no_t no; 104 105 106// ---------------------------------------------------------------------------- 107// ---------------------------------------------------------------------------- 108// ---------------------------------------------------------------------------- 109 110static void SurfaceSession_init(JNIEnv* env, jobject clazz) 111{ 112 sp<SurfaceComposerClient> client = new SurfaceComposerClient; 113 client->incStrong(clazz); 114 env->SetIntField(clazz, sso.client, (int)client.get()); 115} 116 117static void SurfaceSession_destroy(JNIEnv* env, jobject clazz) 118{ 119 SurfaceComposerClient* client = 120 (SurfaceComposerClient*)env->GetIntField(clazz, sso.client); 121 if (client != 0) { 122 client->decStrong(clazz); 123 env->SetIntField(clazz, sso.client, 0); 124 } 125} 126 127static void SurfaceSession_kill(JNIEnv* env, jobject clazz) 128{ 129 SurfaceComposerClient* client = 130 (SurfaceComposerClient*)env->GetIntField(clazz, sso.client); 131 if (client != 0) { 132 client->dispose(); 133 client->decStrong(clazz); 134 env->SetIntField(clazz, sso.client, 0); 135 } 136} 137 138// ---------------------------------------------------------------------------- 139 140static sp<SurfaceControl> getSurfaceControl(JNIEnv* env, jobject clazz) 141{ 142 SurfaceControl* const p = 143 (SurfaceControl*)env->GetIntField(clazz, so.surfaceControl); 144 return sp<SurfaceControl>(p); 145} 146 147static void setSurfaceControl(JNIEnv* env, jobject clazz, 148 const sp<SurfaceControl>& surface) 149{ 150 SurfaceControl* const p = 151 (SurfaceControl*)env->GetIntField(clazz, so.surfaceControl); 152 if (surface.get()) { 153 surface->incStrong(clazz); 154 } 155 if (p) { 156 p->decStrong(clazz); 157 } 158 env->SetIntField(clazz, so.surfaceControl, (int)surface.get()); 159} 160 161static sp<Surface> getSurface(JNIEnv* env, jobject clazz) 162{ 163 sp<Surface> result(Surface_getSurface(env, clazz)); 164 if (result == 0) { 165 /* 166 * if this method is called from the WindowManager's process, it means 167 * the client is is not remote, and therefore is allowed to have 168 * a Surface (data), so we create it here. 169 * If we don't have a SurfaceControl, it means we're in a different 170 * process. 171 */ 172 173 SurfaceControl* const control = 174 (SurfaceControl*)env->GetIntField(clazz, so.surfaceControl); 175 if (control) { 176 result = control->getSurface(); 177 if (result != 0) { 178 result->incStrong(clazz); 179 env->SetIntField(clazz, so.surface, (int)result.get()); 180 } 181 } 182 } 183 return result; 184} 185 186sp<ANativeWindow> android_Surface_getNativeWindow( 187 JNIEnv* env, jobject clazz) { 188 return getSurface(env, clazz); 189} 190 191bool android_Surface_isInstanceOf(JNIEnv* env, jobject obj) { 192 jclass surfaceClass = env->FindClass(kSurfaceClassPathName); 193 return env->IsInstanceOf(obj, surfaceClass); 194} 195 196sp<Surface> Surface_getSurface(JNIEnv* env, jobject clazz) { 197 sp<Surface> surface((Surface*)env->GetIntField(clazz, so.surface)); 198 return surface; 199} 200 201void setSurface(JNIEnv* env, jobject clazz, const sp<Surface>& surface) 202{ 203 Surface* const p = (Surface*)env->GetIntField(clazz, so.surface); 204 if (surface.get()) { 205 surface->incStrong(clazz); 206 } 207 if (p) { 208 p->decStrong(clazz); 209 } 210 env->SetIntField(clazz, so.surface, (int)surface.get()); 211 // This test is conservative and it would be better to compare the ISurfaces 212 if (p && p != surface.get()) { 213 jint generationId = env->GetIntField(clazz, so.surfaceGenerationId); 214 generationId++; 215 env->SetIntField(clazz, so.surfaceGenerationId, generationId); 216 } 217} 218 219// ---------------------------------------------------------------------------- 220 221static void Surface_init( 222 JNIEnv* env, jobject clazz, 223 jobject session, 224 jint, jstring jname, jint dpy, jint w, jint h, jint format, jint flags) 225{ 226 if (session == NULL) { 227 doThrowNPE(env); 228 return; 229 } 230 231 SurfaceComposerClient* client = 232 (SurfaceComposerClient*)env->GetIntField(session, sso.client); 233 234 sp<SurfaceControl> surface; 235 if (jname == NULL) { 236 surface = client->createSurface(dpy, w, h, format, flags); 237 } else { 238 const jchar* str = env->GetStringCritical(jname, 0); 239 const String8 name(str, env->GetStringLength(jname)); 240 env->ReleaseStringCritical(jname, str); 241 surface = client->createSurface(name, dpy, w, h, format, flags); 242 } 243 244 if (surface == 0) { 245 jniThrowException(env, OutOfResourcesException, NULL); 246 return; 247 } 248 setSurfaceControl(env, clazz, surface); 249} 250 251static void Surface_initFromSurfaceTexture( 252 JNIEnv* env, jobject clazz, jobject jst) 253{ 254 sp<SurfaceTexture> st(SurfaceTexture_getSurfaceTexture(env, jst)); 255 256 if (st == NULL) { 257 jniThrowException(env, "java/lang/IllegalArgumentException", 258 "SurfaceTexture has already been released"); 259 return; 260 } 261 sp<ISurfaceTexture> bq = st->getBufferQueue(); 262 263 sp<Surface> surface(new Surface(bq)); 264 if (surface == NULL) { 265 jniThrowException(env, OutOfResourcesException, NULL); 266 return; 267 } 268 setSurfaceControl(env, clazz, NULL); 269 setSurface(env, clazz, surface); 270} 271 272static void Surface_initParcel(JNIEnv* env, jobject clazz, jobject argParcel) 273{ 274 Parcel* parcel = (Parcel*)env->GetIntField(argParcel, no.native_parcel); 275 if (parcel == NULL) { 276 doThrowNPE(env); 277 return; 278 } 279 280 sp<Surface> sur(Surface::readFromParcel(*parcel)); 281 setSurface(env, clazz, sur); 282} 283 284static jint Surface_getIdentity(JNIEnv* env, jobject clazz) 285{ 286 const sp<SurfaceControl>& control(getSurfaceControl(env, clazz)); 287 if (control != 0) return (jint) control->getIdentity(); 288 const sp<Surface>& surface(getSurface(env, clazz)); 289 if (surface != 0) return (jint) surface->getIdentity(); 290 return -1; 291} 292 293static void Surface_destroy(JNIEnv* env, jobject clazz, uintptr_t *ostack) 294{ 295 const sp<SurfaceControl>& surface(getSurfaceControl(env, clazz)); 296 if (SurfaceControl::isValid(surface)) { 297 surface->clear(); 298 } 299 setSurfaceControl(env, clazz, 0); 300 setSurface(env, clazz, 0); 301} 302 303static void Surface_release(JNIEnv* env, jobject clazz, uintptr_t *ostack) 304{ 305 setSurfaceControl(env, clazz, 0); 306 setSurface(env, clazz, 0); 307} 308 309static jboolean Surface_isValid(JNIEnv* env, jobject clazz) 310{ 311 const sp<SurfaceControl>& surfaceControl(getSurfaceControl(env, clazz)); 312 if (surfaceControl != 0) { 313 return SurfaceControl::isValid(surfaceControl) ? JNI_TRUE : JNI_FALSE; 314 } 315 const sp<Surface>& surface(getSurface(env, clazz)); 316 return Surface::isValid(surface) ? JNI_TRUE : JNI_FALSE; 317} 318 319static jboolean Surface_isConsumerRunningBehind(JNIEnv* env, jobject clazz) 320{ 321 int value = 0; 322 const sp<Surface>& surface(getSurface(env, clazz)); 323 if (!Surface::isValid(surface)) { 324 doThrowIAE(env); 325 return 0; 326 } 327 ANativeWindow* anw = static_cast<ANativeWindow *>(surface.get()); 328 anw->query(anw, NATIVE_WINDOW_CONSUMER_RUNNING_BEHIND, &value); 329 return (jboolean)value; 330} 331 332static inline SkBitmap::Config convertPixelFormat(PixelFormat format) 333{ 334 /* note: if PIXEL_FORMAT_RGBX_8888 means that all alpha bytes are 0xFF, then 335 we can map to SkBitmap::kARGB_8888_Config, and optionally call 336 bitmap.setIsOpaque(true) on the resulting SkBitmap (as an accelerator) 337 */ 338 switch (format) { 339 case PIXEL_FORMAT_RGBX_8888: return SkBitmap::kARGB_8888_Config; 340 case PIXEL_FORMAT_RGBA_8888: return SkBitmap::kARGB_8888_Config; 341 case PIXEL_FORMAT_RGBA_4444: return SkBitmap::kARGB_4444_Config; 342 case PIXEL_FORMAT_RGB_565: return SkBitmap::kRGB_565_Config; 343 case PIXEL_FORMAT_A_8: return SkBitmap::kA8_Config; 344 default: return SkBitmap::kNo_Config; 345 } 346} 347 348static jobject Surface_lockCanvas(JNIEnv* env, jobject clazz, jobject dirtyRect) 349{ 350 const sp<Surface>& surface(getSurface(env, clazz)); 351 if (!Surface::isValid(surface)) { 352 doThrowIAE(env); 353 return 0; 354 } 355 356 // get dirty region 357 Region dirtyRegion; 358 if (dirtyRect) { 359 Rect dirty; 360 dirty.left = env->GetIntField(dirtyRect, ro.l); 361 dirty.top = env->GetIntField(dirtyRect, ro.t); 362 dirty.right = env->GetIntField(dirtyRect, ro.r); 363 dirty.bottom= env->GetIntField(dirtyRect, ro.b); 364 if (!dirty.isEmpty()) { 365 dirtyRegion.set(dirty); 366 } 367 } else { 368 dirtyRegion.set(Rect(0x3FFF,0x3FFF)); 369 } 370 371 Surface::SurfaceInfo info; 372 status_t err = surface->lock(&info, &dirtyRegion); 373 if (err < 0) { 374 const char* const exception = (err == NO_MEMORY) ? 375 OutOfResourcesException : 376 "java/lang/IllegalArgumentException"; 377 jniThrowException(env, exception, NULL); 378 return 0; 379 } 380 381 // Associate a SkCanvas object to this surface 382 jobject canvas = env->GetObjectField(clazz, so.canvas); 383 env->SetIntField(canvas, co.surfaceFormat, info.format); 384 385 SkCanvas* nativeCanvas = (SkCanvas*)env->GetIntField(canvas, no.native_canvas); 386 SkBitmap bitmap; 387 ssize_t bpr = info.s * bytesPerPixel(info.format); 388 bitmap.setConfig(convertPixelFormat(info.format), info.w, info.h, bpr); 389 if (info.format == PIXEL_FORMAT_RGBX_8888) { 390 bitmap.setIsOpaque(true); 391 } 392 if (info.w > 0 && info.h > 0) { 393 bitmap.setPixels(info.bits); 394 } else { 395 // be safe with an empty bitmap. 396 bitmap.setPixels(NULL); 397 } 398 nativeCanvas->setBitmapDevice(bitmap); 399 400 SkRegion clipReg; 401 if (dirtyRegion.isRect()) { // very common case 402 const Rect b(dirtyRegion.getBounds()); 403 clipReg.setRect(b.left, b.top, b.right, b.bottom); 404 } else { 405 size_t count; 406 Rect const* r = dirtyRegion.getArray(&count); 407 while (count) { 408 clipReg.op(r->left, r->top, r->right, r->bottom, SkRegion::kUnion_Op); 409 r++, count--; 410 } 411 } 412 413 nativeCanvas->clipRegion(clipReg); 414 415 int saveCount = nativeCanvas->save(); 416 env->SetIntField(clazz, so.saveCount, saveCount); 417 418 if (dirtyRect) { 419 const Rect& bounds(dirtyRegion.getBounds()); 420 env->SetIntField(dirtyRect, ro.l, bounds.left); 421 env->SetIntField(dirtyRect, ro.t, bounds.top); 422 env->SetIntField(dirtyRect, ro.r, bounds.right); 423 env->SetIntField(dirtyRect, ro.b, bounds.bottom); 424 } 425 426 return canvas; 427} 428 429static void Surface_unlockCanvasAndPost( 430 JNIEnv* env, jobject clazz, jobject argCanvas) 431{ 432 jobject canvas = env->GetObjectField(clazz, so.canvas); 433 if (env->IsSameObject(canvas, argCanvas) == JNI_FALSE) { 434 doThrowIAE(env); 435 return; 436 } 437 438 const sp<Surface>& surface(getSurface(env, clazz)); 439 if (!Surface::isValid(surface)) 440 return; 441 442 // detach the canvas from the surface 443 SkCanvas* nativeCanvas = (SkCanvas*)env->GetIntField(canvas, no.native_canvas); 444 int saveCount = env->GetIntField(clazz, so.saveCount); 445 nativeCanvas->restoreToCount(saveCount); 446 nativeCanvas->setBitmapDevice(SkBitmap()); 447 env->SetIntField(clazz, so.saveCount, 0); 448 449 // unlock surface 450 status_t err = surface->unlockAndPost(); 451 if (err < 0) { 452 doThrowIAE(env); 453 } 454} 455 456static void Surface_unlockCanvas( 457 JNIEnv* env, jobject clazz, jobject argCanvas) 458{ 459 // XXX: this API has been removed 460 doThrowIAE(env); 461} 462 463static void Surface_openTransaction( 464 JNIEnv* env, jobject clazz) 465{ 466 SurfaceComposerClient::openGlobalTransaction(); 467} 468 469static void Surface_closeTransaction( 470 JNIEnv* env, jobject clazz) 471{ 472 SurfaceComposerClient::closeGlobalTransaction(); 473} 474 475static void Surface_setOrientation( 476 JNIEnv* env, jobject clazz, jint display, jint orientation, jint flags) 477{ 478 int err = SurfaceComposerClient::setOrientation(display, orientation, flags); 479 if (err < 0) { 480 doThrowIAE(env); 481 } 482} 483 484class ScreenshotPixelRef : public SkPixelRef { 485public: 486 ScreenshotPixelRef(SkColorTable* ctable) { 487 fCTable = ctable; 488 SkSafeRef(ctable); 489 setImmutable(); 490 } 491 virtual ~ScreenshotPixelRef() { 492 SkSafeUnref(fCTable); 493 } 494 495 status_t update(int width, int height, int minLayer, int maxLayer, bool allLayers) { 496 status_t res = (width > 0 && height > 0) 497 ? (allLayers 498 ? mScreenshot.update(width, height) 499 : mScreenshot.update(width, height, minLayer, maxLayer)) 500 : mScreenshot.update(); 501 if (res != NO_ERROR) { 502 return res; 503 } 504 505 return NO_ERROR; 506 } 507 508 uint32_t getWidth() const { 509 return mScreenshot.getWidth(); 510 } 511 512 uint32_t getHeight() const { 513 return mScreenshot.getHeight(); 514 } 515 516 uint32_t getStride() const { 517 return mScreenshot.getStride(); 518 } 519 520 uint32_t getFormat() const { 521 return mScreenshot.getFormat(); 522 } 523 524protected: 525 // overrides from SkPixelRef 526 virtual void* onLockPixels(SkColorTable** ct) { 527 *ct = fCTable; 528 return (void*)mScreenshot.getPixels(); 529 } 530 531 virtual void onUnlockPixels() { 532 } 533 534private: 535 ScreenshotClient mScreenshot; 536 SkColorTable* fCTable; 537 538 typedef SkPixelRef INHERITED; 539}; 540 541static jobject doScreenshot(JNIEnv* env, jobject clazz, jint width, jint height, 542 jint minLayer, jint maxLayer, bool allLayers) 543{ 544 ScreenshotPixelRef* pixels = new ScreenshotPixelRef(NULL); 545 if (pixels->update(width, height, minLayer, maxLayer, allLayers) != NO_ERROR) { 546 delete pixels; 547 return 0; 548 } 549 550 uint32_t w = pixels->getWidth(); 551 uint32_t h = pixels->getHeight(); 552 uint32_t s = pixels->getStride(); 553 uint32_t f = pixels->getFormat(); 554 ssize_t bpr = s * android::bytesPerPixel(f); 555 556 SkBitmap* bitmap = new SkBitmap(); 557 bitmap->setConfig(convertPixelFormat(f), w, h, bpr); 558 if (f == PIXEL_FORMAT_RGBX_8888) { 559 bitmap->setIsOpaque(true); 560 } 561 562 if (w > 0 && h > 0) { 563 bitmap->setPixelRef(pixels)->unref(); 564 bitmap->lockPixels(); 565 } else { 566 // be safe with an empty bitmap. 567 delete pixels; 568 bitmap->setPixels(NULL); 569 } 570 571 return GraphicsJNI::createBitmap(env, bitmap, false, NULL); 572} 573 574static jobject Surface_screenshotAll(JNIEnv* env, jobject clazz, jint width, jint height) 575{ 576 return doScreenshot(env, clazz, width, height, 0, 0, true); 577} 578 579static jobject Surface_screenshot(JNIEnv* env, jobject clazz, jint width, jint height, 580 jint minLayer, jint maxLayer) 581{ 582 return doScreenshot(env, clazz, width, height, minLayer, maxLayer, false); 583} 584 585static void Surface_setLayer( 586 JNIEnv* env, jobject clazz, jint zorder) 587{ 588 const sp<SurfaceControl>& surface(getSurfaceControl(env, clazz)); 589 if (surface == 0) return; 590 status_t err = surface->setLayer(zorder); 591 if (err<0 && err!=NO_INIT) { 592 doThrowIAE(env); 593 } 594} 595 596static void Surface_setPosition( 597 JNIEnv* env, jobject clazz, jfloat x, jfloat y) 598{ 599 const sp<SurfaceControl>& surface(getSurfaceControl(env, clazz)); 600 if (surface == 0) return; 601 status_t err = surface->setPosition(x, y); 602 if (err<0 && err!=NO_INIT) { 603 doThrowIAE(env); 604 } 605} 606 607static void Surface_setSize( 608 JNIEnv* env, jobject clazz, jint w, jint h) 609{ 610 const sp<SurfaceControl>& surface(getSurfaceControl(env, clazz)); 611 if (surface == 0) return; 612 status_t err = surface->setSize(w, h); 613 if (err<0 && err!=NO_INIT) { 614 doThrowIAE(env); 615 } 616} 617 618static void Surface_hide( 619 JNIEnv* env, jobject clazz) 620{ 621 const sp<SurfaceControl>& surface(getSurfaceControl(env, clazz)); 622 if (surface == 0) return; 623 status_t err = surface->hide(); 624 if (err<0 && err!=NO_INIT) { 625 doThrowIAE(env); 626 } 627} 628 629static void Surface_show( 630 JNIEnv* env, jobject clazz) 631{ 632 const sp<SurfaceControl>& surface(getSurfaceControl(env, clazz)); 633 if (surface == 0) return; 634 status_t err = surface->show(); 635 if (err<0 && err!=NO_INIT) { 636 doThrowIAE(env); 637 } 638} 639 640static void Surface_setFlags( 641 JNIEnv* env, jobject clazz, jint flags, jint mask) 642{ 643 const sp<SurfaceControl>& surface(getSurfaceControl(env, clazz)); 644 if (surface == 0) return; 645 status_t err = surface->setFlags(flags, mask); 646 if (err<0 && err!=NO_INIT) { 647 doThrowIAE(env); 648 } 649} 650 651static void Surface_setTransparentRegion( 652 JNIEnv* env, jobject clazz, jobject argRegion) 653{ 654 const sp<SurfaceControl>& surface(getSurfaceControl(env, clazz)); 655 if (surface == 0) return; 656 SkRegion* nativeRegion = (SkRegion*)env->GetIntField(argRegion, no.native_region); 657 658 const SkIRect& b(nativeRegion->getBounds()); 659 Region reg(Rect(b.fLeft, b.fTop, b.fRight, b.fBottom)); 660 if (nativeRegion->isComplex()) { 661 SkRegion::Iterator it(*nativeRegion); 662 while (!it.done()) { 663 const SkIRect& r(it.rect()); 664 reg.addRectUnchecked(r.fLeft, r.fTop, r.fRight, r.fBottom); 665 it.next(); 666 } 667 } 668 669 status_t err = surface->setTransparentRegionHint(reg); 670 if (err<0 && err!=NO_INIT) { 671 doThrowIAE(env); 672 } 673} 674 675static void Surface_setAlpha( 676 JNIEnv* env, jobject clazz, jfloat alpha) 677{ 678 const sp<SurfaceControl>& surface(getSurfaceControl(env, clazz)); 679 if (surface == 0) return; 680 status_t err = surface->setAlpha(alpha); 681 if (err<0 && err!=NO_INIT) { 682 doThrowIAE(env); 683 } 684} 685 686static void Surface_setMatrix( 687 JNIEnv* env, jobject clazz, 688 jfloat dsdx, jfloat dtdx, jfloat dsdy, jfloat dtdy) 689{ 690 const sp<SurfaceControl>& surface(getSurfaceControl(env, clazz)); 691 if (surface == 0) return; 692 status_t err = surface->setMatrix(dsdx, dtdx, dsdy, dtdy); 693 if (err<0 && err!=NO_INIT) { 694 doThrowIAE(env); 695 } 696} 697 698static void Surface_setWindowCrop(JNIEnv* env, jobject thiz, jobject crop) 699{ 700 const sp<SurfaceControl>& surface(getSurfaceControl(env, thiz)); 701 if (surface == 0) return; 702 703 Rect nativeCrop; 704 if (crop) { 705 nativeCrop.left = env->GetIntField(crop, ro.l); 706 nativeCrop.top = env->GetIntField(crop, ro.t); 707 nativeCrop.right = env->GetIntField(crop, ro.r); 708 nativeCrop.bottom= env->GetIntField(crop, ro.b); 709 } else { 710 nativeCrop.left = nativeCrop.top = nativeCrop.right = 711 nativeCrop.bottom = 0; 712 } 713 714 status_t err = surface->setCrop(nativeCrop); 715 if (err<0 && err!=NO_INIT) { 716 doThrowIAE(env); 717 } 718} 719 720// ---------------------------------------------------------------------------- 721 722static void Surface_copyFrom( 723 JNIEnv* env, jobject clazz, jobject other) 724{ 725 if (clazz == other) 726 return; 727 728 if (other == NULL) { 729 doThrowNPE(env); 730 return; 731 } 732 733 /* 734 * This is used by the WindowManagerService just after constructing 735 * a Surface and is necessary for returning the Surface reference to 736 * the caller. At this point, we should only have a SurfaceControl. 737 */ 738 739 const sp<SurfaceControl>& surface = getSurfaceControl(env, clazz); 740 const sp<SurfaceControl>& rhs = getSurfaceControl(env, other); 741 if (!SurfaceControl::isSameSurface(surface, rhs)) { 742 // we reassign the surface only if it's a different one 743 // otherwise we would loose our client-side state. 744 setSurfaceControl(env, clazz, rhs); 745 } 746} 747 748static void Surface_transferFrom( 749 JNIEnv* env, jobject clazz, jobject other) 750{ 751 if (clazz == other) 752 return; 753 754 if (other == NULL) { 755 doThrowNPE(env); 756 return; 757 } 758 759 sp<SurfaceControl> control(getSurfaceControl(env, other)); 760 sp<Surface> surface(Surface_getSurface(env, other)); 761 setSurfaceControl(env, clazz, control); 762 setSurface(env, clazz, surface); 763 setSurfaceControl(env, other, 0); 764 setSurface(env, other, 0); 765} 766 767static void Surface_readFromParcel( 768 JNIEnv* env, jobject clazz, jobject argParcel) 769{ 770 Parcel* parcel = (Parcel*)env->GetIntField( argParcel, no.native_parcel); 771 if (parcel == NULL) { 772 doThrowNPE(env); 773 return; 774 } 775 776 sp<Surface> sur(Surface::readFromParcel(*parcel)); 777 setSurface(env, clazz, sur); 778} 779 780static void Surface_writeToParcel( 781 JNIEnv* env, jobject clazz, jobject argParcel, jint flags) 782{ 783 Parcel* parcel = (Parcel*)env->GetIntField( 784 argParcel, no.native_parcel); 785 786 if (parcel == NULL) { 787 doThrowNPE(env); 788 return; 789 } 790 791 // The Java instance may have a SurfaceControl (in the case of the 792 // WindowManager or a system app). In that case, we defer to the 793 // SurfaceControl to send its ISurface. Otherwise, if the Surface is 794 // available we let it parcel itself. Finally, if the Surface is also 795 // NULL we fall back to using the SurfaceControl path which sends an 796 // empty surface; this matches legacy behavior. 797 const sp<SurfaceControl>& control(getSurfaceControl(env, clazz)); 798 if (control != NULL) { 799 SurfaceControl::writeSurfaceToParcel(control, parcel); 800 } else { 801 sp<Surface> surface(Surface_getSurface(env, clazz)); 802 if (surface != NULL) { 803 Surface::writeToParcel(surface, parcel); 804 } else { 805 SurfaceControl::writeSurfaceToParcel(NULL, parcel); 806 } 807 } 808 if (flags & PARCELABLE_WRITE_RETURN_VALUE) { 809 setSurfaceControl(env, clazz, NULL); 810 setSurface(env, clazz, NULL); 811 } 812} 813 814// ---------------------------------------------------------------------------- 815// ---------------------------------------------------------------------------- 816// ---------------------------------------------------------------------------- 817 818static void nativeClassInit(JNIEnv* env, jclass clazz); 819 820static JNINativeMethod gSurfaceSessionMethods[] = { 821 {"init", "()V", (void*)SurfaceSession_init }, 822 {"destroy", "()V", (void*)SurfaceSession_destroy }, 823 {"kill", "()V", (void*)SurfaceSession_kill }, 824}; 825 826static JNINativeMethod gSurfaceMethods[] = { 827 {"nativeClassInit", "()V", (void*)nativeClassInit }, 828 {"init", "(Landroid/view/SurfaceSession;ILjava/lang/String;IIIII)V", (void*)Surface_init }, 829 {"init", "(Landroid/os/Parcel;)V", (void*)Surface_initParcel }, 830 {"initFromSurfaceTexture", "(Landroid/graphics/SurfaceTexture;)V", (void*)Surface_initFromSurfaceTexture }, 831 {"getIdentity", "()I", (void*)Surface_getIdentity }, 832 {"destroy", "()V", (void*)Surface_destroy }, 833 {"release", "()V", (void*)Surface_release }, 834 {"copyFrom", "(Landroid/view/Surface;)V", (void*)Surface_copyFrom }, 835 {"transferFrom", "(Landroid/view/Surface;)V", (void*)Surface_transferFrom }, 836 {"isValid", "()Z", (void*)Surface_isValid }, 837 {"lockCanvasNative", "(Landroid/graphics/Rect;)Landroid/graphics/Canvas;", (void*)Surface_lockCanvas }, 838 {"unlockCanvasAndPost", "(Landroid/graphics/Canvas;)V", (void*)Surface_unlockCanvasAndPost }, 839 {"unlockCanvas", "(Landroid/graphics/Canvas;)V", (void*)Surface_unlockCanvas }, 840 {"openTransaction", "()V", (void*)Surface_openTransaction }, 841 {"closeTransaction", "()V", (void*)Surface_closeTransaction }, 842 {"setOrientation", "(III)V", (void*)Surface_setOrientation }, 843 {"screenshot", "(II)Landroid/graphics/Bitmap;", (void*)Surface_screenshotAll }, 844 {"screenshot", "(IIII)Landroid/graphics/Bitmap;", (void*)Surface_screenshot }, 845 {"setLayer", "(I)V", (void*)Surface_setLayer }, 846 {"setPosition", "(FF)V",(void*)Surface_setPosition }, 847 {"setSize", "(II)V",(void*)Surface_setSize }, 848 {"hide", "()V", (void*)Surface_hide }, 849 {"show", "()V", (void*)Surface_show }, 850 {"setFlags", "(II)V",(void*)Surface_setFlags }, 851 {"setTransparentRegionHint","(Landroid/graphics/Region;)V", (void*)Surface_setTransparentRegion }, 852 {"setAlpha", "(F)V", (void*)Surface_setAlpha }, 853 {"setMatrix", "(FFFF)V", (void*)Surface_setMatrix }, 854 {"readFromParcel", "(Landroid/os/Parcel;)V", (void*)Surface_readFromParcel }, 855 {"writeToParcel", "(Landroid/os/Parcel;I)V", (void*)Surface_writeToParcel }, 856 {"isConsumerRunningBehind", "()Z", (void*)Surface_isConsumerRunningBehind }, 857 {"setWindowCrop", "(Landroid/graphics/Rect;)V", (void*)Surface_setWindowCrop }, 858}; 859 860void nativeClassInit(JNIEnv* env, jclass clazz) 861{ 862 so.surface = env->GetFieldID(clazz, ANDROID_VIEW_SURFACE_JNI_ID, "I"); 863 so.surfaceGenerationId = env->GetFieldID(clazz, "mSurfaceGenerationId", "I"); 864 so.surfaceControl = env->GetFieldID(clazz, "mSurfaceControl", "I"); 865 so.saveCount = env->GetFieldID(clazz, "mSaveCount", "I"); 866 so.canvas = env->GetFieldID(clazz, "mCanvas", "Landroid/graphics/Canvas;"); 867 868 jclass surfaceSession = env->FindClass("android/view/SurfaceSession"); 869 sso.client = env->GetFieldID(surfaceSession, "mClient", "I"); 870 871 jclass canvas = env->FindClass("android/graphics/Canvas"); 872 no.native_canvas = env->GetFieldID(canvas, "mNativeCanvas", "I"); 873 co.surfaceFormat = env->GetFieldID(canvas, "mSurfaceFormat", "I"); 874 875 jclass region = env->FindClass("android/graphics/Region"); 876 no.native_region = env->GetFieldID(region, "mNativeRegion", "I"); 877 878 jclass parcel = env->FindClass("android/os/Parcel"); 879 no.native_parcel = env->GetFieldID(parcel, "mNativePtr", "I"); 880 881 jclass rect = env->FindClass("android/graphics/Rect"); 882 ro.l = env->GetFieldID(rect, "left", "I"); 883 ro.t = env->GetFieldID(rect, "top", "I"); 884 ro.r = env->GetFieldID(rect, "right", "I"); 885 ro.b = env->GetFieldID(rect, "bottom", "I"); 886 887 jclass point = env->FindClass("android/graphics/Point"); 888 po.x = env->GetFieldID(point, "x", "I"); 889 po.y = env->GetFieldID(point, "y", "I"); 890} 891 892int register_android_view_Surface(JNIEnv* env) 893{ 894 int err; 895 err = AndroidRuntime::registerNativeMethods(env, kSurfaceSessionClassPathName, 896 gSurfaceSessionMethods, NELEM(gSurfaceSessionMethods)); 897 898 err |= AndroidRuntime::registerNativeMethods(env, kSurfaceClassPathName, 899 gSurfaceMethods, NELEM(gSurfaceMethods)); 900 return err; 901} 902 903}; 904