| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.view; |
| 18 |
|
| 19 |
import static android.system.OsConstants.EINVAL; |
| 20 |
|
| 21 |
import android.annotation.FloatRange; |
| 22 |
import android.annotation.IntDef; |
| 23 |
import android.annotation.NonNull; |
| 24 |
import android.compat.annotation.UnsupportedAppUsage; |
| 25 |
import android.content.pm.ActivityInfo; |
| 26 |
import android.content.res.CompatibilityInfo.Translator; |
| 27 |
import android.graphics.BLASTBufferQueue; |
| 28 |
import android.graphics.Canvas; |
| 29 |
import android.graphics.ColorSpace; |
| 30 |
import android.graphics.HardwareRenderer; |
| 31 |
import android.graphics.Matrix; |
| 32 |
import android.graphics.Point; |
| 33 |
import android.graphics.RecordingCanvas; |
| 34 |
import android.graphics.Rect; |
| 35 |
import android.graphics.RenderNode; |
| 36 |
import android.graphics.SurfaceTexture; |
| 37 |
import android.hardware.HardwareBuffer; |
| 38 |
import android.os.Build; |
| 39 |
import android.os.Parcel; |
| 40 |
import android.os.Parcelable; |
| 41 |
import android.util.Log; |
| 42 |
|
| 43 |
import dalvik.system.CloseGuard; |
| 44 |
|
| 45 |
import java.lang.annotation.Retention; |
| 46 |
import java.lang.annotation.RetentionPolicy; |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
public class Surface implements Parcelable { |
| 64 |
private static final String TAG = "Surface"; |
| 65 |
|
| 66 |
private static native long nativeCreateFromSurfaceTexture(SurfaceTexture surfaceTexture) |
| 67 |
throws OutOfResourcesException; |
| 68 |
|
| 69 |
private static native long nativeCreateFromSurfaceControl(long surfaceControlNativeObject); |
| 70 |
private static native long nativeGetFromSurfaceControl(long surfaceObject, |
| 71 |
long surfaceControlNativeObject); |
| 72 |
private static native long nativeGetFromBlastBufferQueue(long surfaceObject, |
| 73 |
long blastBufferQueueNativeObject); |
| 74 |
|
| 75 |
private static native long nativeLockCanvas(long nativeObject, Canvas canvas, Rect dirty) |
| 76 |
throws OutOfResourcesException; |
| 77 |
private static native void nativeUnlockCanvasAndPost(long nativeObject, Canvas canvas); |
| 78 |
|
| 79 |
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) |
| 80 |
private static native void nativeRelease(long nativeObject); |
| 81 |
private static native boolean nativeIsValid(long nativeObject); |
| 82 |
private static native boolean nativeIsConsumerRunningBehind(long nativeObject); |
| 83 |
private static native long nativeReadFromParcel(long nativeObject, Parcel source); |
| 84 |
private static native void nativeWriteToParcel(long nativeObject, Parcel dest); |
| 85 |
|
| 86 |
private static native void nativeAllocateBuffers(long nativeObject); |
| 87 |
|
| 88 |
private static native int nativeGetWidth(long nativeObject); |
| 89 |
private static native int nativeGetHeight(long nativeObject); |
| 90 |
|
| 91 |
private static native long nativeGetNextFrameNumber(long nativeObject); |
| 92 |
private static native int nativeSetScalingMode(long nativeObject, int scalingMode); |
| 93 |
private static native int nativeForceScopedDisconnect(long nativeObject); |
| 94 |
private static native int nativeAttachAndQueueBufferWithColorSpace(long nativeObject, |
| 95 |
HardwareBuffer buffer, int colorSpaceId); |
| 96 |
|
| 97 |
private static native int nativeSetSharedBufferModeEnabled(long nativeObject, boolean enabled); |
| 98 |
private static native int nativeSetAutoRefreshEnabled(long nativeObject, boolean enabled); |
| 99 |
|
| 100 |
private static native int nativeSetFrameRate( |
| 101 |
long nativeObject, float frameRate, int compatibility, int changeFrameRateStrategy); |
| 102 |
private static native void nativeDestroy(long nativeObject); |
| 103 |
|
| 104 |
public static final @android.annotation.NonNull Parcelable.Creator<Surface> CREATOR = |
| 105 |
new Parcelable.Creator<Surface>() { |
| 106 |
@Override |
| 107 |
public Surface createFromParcel(Parcel source) { |
| 108 |
try { |
| 109 |
Surface s = new Surface(); |
| 110 |
s.readFromParcel(source); |
| 111 |
return s; |
| 112 |
} catch (Exception e) { |
| 113 |
Log.e(TAG, "Exception creating surface from parcel", e); |
| 114 |
return null; |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
@Override |
| 119 |
public Surface[] newArray(int size) { |
| 120 |
return new Surface[size]; |
| 121 |
} |
| 122 |
}; |
| 123 |
|
| 124 |
private final CloseGuard mCloseGuard = CloseGuard.get(); |
| 125 |
|
| 126 |
|
| 127 |
@UnsupportedAppUsage |
| 128 |
final Object mLock = new Object(); |
| 129 |
@UnsupportedAppUsage |
| 130 |
private String mName; |
| 131 |
@UnsupportedAppUsage |
| 132 |
long mNativeObject; |
| 133 |
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) |
| 134 |
private long mLockedObject; |
| 135 |
private int mGenerationId; |
| 136 |
private final Canvas mCanvas = new CompatibleCanvas(); |
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
private Matrix mCompatibleMatrix; |
| 141 |
|
| 142 |
private HwuiContext mHwuiContext; |
| 143 |
|
| 144 |
private boolean mIsSingleBuffered; |
| 145 |
private boolean mIsSharedBufferModeEnabled; |
| 146 |
private boolean mIsAutoRefreshEnabled; |
| 147 |
|
| 148 |
|
| 149 |
@Retention(RetentionPolicy.SOURCE) |
| 150 |
@IntDef(prefix = { "SCALING_MODE_" }, value = { |
| 151 |
SCALING_MODE_FREEZE, |
| 152 |
SCALING_MODE_SCALE_TO_WINDOW, |
| 153 |
SCALING_MODE_SCALE_CROP, |
| 154 |
SCALING_MODE_NO_SCALE_CROP |
| 155 |
}) |
| 156 |
public @interface ScalingMode {} |
| 157 |
|
| 158 |
|
| 159 |
public static final int SCALING_MODE_FREEZE = 0; |
| 160 |
|
| 161 |
public static final int SCALING_MODE_SCALE_TO_WINDOW = 1; |
| 162 |
|
| 163 |
public static final int SCALING_MODE_SCALE_CROP = 2; |
| 164 |
|
| 165 |
public static final int SCALING_MODE_NO_SCALE_CROP = 3; |
| 166 |
|
| 167 |
|
| 168 |
@IntDef(prefix = { "ROTATION_" }, value = { |
| 169 |
ROTATION_0, |
| 170 |
ROTATION_90, |
| 171 |
ROTATION_180, |
| 172 |
ROTATION_270 |
| 173 |
}) |
| 174 |
@Retention(RetentionPolicy.SOURCE) |
| 175 |
public @interface Rotation {} |
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
public static final int ROTATION_0 = 0; |
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
public static final int ROTATION_90 = 1; |
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
public static final int ROTATION_180 = 2; |
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
public static final int ROTATION_270 = 3; |
| 196 |
|
| 197 |
|
| 198 |
@Retention(RetentionPolicy.SOURCE) |
| 199 |
@IntDef(prefix = {"FRAME_RATE_COMPATIBILITY_"}, |
| 200 |
value = {FRAME_RATE_COMPATIBILITY_DEFAULT, FRAME_RATE_COMPATIBILITY_FIXED_SOURCE}) |
| 201 |
public @interface FrameRateCompatibility {} |
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
public static final int FRAME_RATE_COMPATIBILITY_DEFAULT = 0; |
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
public static final int FRAME_RATE_COMPATIBILITY_FIXED_SOURCE = 1; |
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
public static final int FRAME_RATE_COMPATIBILITY_EXACT = 100; |
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
@Retention(RetentionPolicy.SOURCE) |
| 234 |
@IntDef(prefix = {"CHANGE_FRAME_RATE_"}, |
| 235 |
value = {CHANGE_FRAME_RATE_ONLY_IF_SEAMLESS, CHANGE_FRAME_RATE_ALWAYS}) |
| 236 |
public @interface ChangeFrameRateStrategy {} |
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
public static final int CHANGE_FRAME_RATE_ONLY_IF_SEAMLESS = 0; |
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
public static final int CHANGE_FRAME_RATE_ALWAYS = 1; |
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
@UnsupportedAppUsage |
| 256 |
public Surface() { |
| 257 |
} |
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
public Surface(@NonNull SurfaceControl from) { |
| 268 |
copyFrom(from); |
| 269 |
} |
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
public Surface(SurfaceTexture surfaceTexture) { |
| 288 |
if (surfaceTexture == null) { |
| 289 |
throw new IllegalArgumentException("surfaceTexture must not be null"); |
| 290 |
} |
| 291 |
mIsSingleBuffered = surfaceTexture.isSingleBuffered(); |
| 292 |
synchronized (mLock) { |
| 293 |
mName = surfaceTexture.toString(); |
| 294 |
setNativeObjectLocked(nativeCreateFromSurfaceTexture(surfaceTexture)); |
| 295 |
} |
| 296 |
} |
| 297 |
|
| 298 |
|
| 299 |
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) |
| 300 |
private Surface(long nativeObject) { |
| 301 |
synchronized (mLock) { |
| 302 |
setNativeObjectLocked(nativeObject); |
| 303 |
} |
| 304 |
} |
| 305 |
|
| 306 |
@Override |
| 307 |
protected void finalize() throws Throwable { |
| 308 |
try { |
| 309 |
if (mCloseGuard != null) { |
| 310 |
mCloseGuard.warnIfOpen(); |
| 311 |
} |
| 312 |
release(); |
| 313 |
} finally { |
| 314 |
super.finalize(); |
| 315 |
} |
| 316 |
} |
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
public void release() { |
| 324 |
synchronized (mLock) { |
| 325 |
if (mHwuiContext != null) { |
| 326 |
mHwuiContext.destroy(); |
| 327 |
mHwuiContext = null; |
| 328 |
} |
| 329 |
if (mNativeObject != 0) { |
| 330 |
nativeRelease(mNativeObject); |
| 331 |
setNativeObjectLocked(0); |
| 332 |
} |
| 333 |
} |
| 334 |
} |
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
@UnsupportedAppUsage |
| 343 |
public void destroy() { |
| 344 |
synchronized (mLock) { |
| 345 |
if (mNativeObject != 0) { |
| 346 |
nativeDestroy(mNativeObject); |
| 347 |
} |
| 348 |
release(); |
| 349 |
} |
| 350 |
} |
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
public void hwuiDestroy() { |
| 358 |
if (mHwuiContext != null) { |
| 359 |
mHwuiContext.destroy(); |
| 360 |
mHwuiContext = null; |
| 361 |
} |
| 362 |
} |
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
public boolean isValid() { |
| 371 |
synchronized (mLock) { |
| 372 |
if (mNativeObject == 0) return false; |
| 373 |
return nativeIsValid(mNativeObject); |
| 374 |
} |
| 375 |
} |
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
public int getGenerationId() { |
| 385 |
synchronized (mLock) { |
| 386 |
return mGenerationId; |
| 387 |
} |
| 388 |
} |
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
@UnsupportedAppUsage |
| 397 |
public long getNextFrameNumber() { |
| 398 |
synchronized (mLock) { |
| 399 |
checkNotReleasedLocked(); |
| 400 |
return nativeGetNextFrameNumber(mNativeObject); |
| 401 |
} |
| 402 |
} |
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
public boolean isConsumerRunningBehind() { |
| 411 |
synchronized (mLock) { |
| 412 |
checkNotReleasedLocked(); |
| 413 |
return nativeIsConsumerRunningBehind(mNativeObject); |
| 414 |
} |
| 415 |
} |
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
@NonNull |
| 424 |
public Point getDefaultSize() { |
| 425 |
synchronized (mLock) { |
| 426 |
checkNotReleasedLocked(); |
| 427 |
return new Point(nativeGetWidth(mNativeObject), nativeGetHeight(mNativeObject)); |
| 428 |
} |
| 429 |
} |
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
public Canvas lockCanvas(Rect inOutDirty) |
| 450 |
throws Surface.OutOfResourcesException, IllegalArgumentException { |
| 451 |
synchronized (mLock) { |
| 452 |
checkNotReleasedLocked(); |
| 453 |
if (mLockedObject != 0) { |
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
|
| 458 |
throw new IllegalArgumentException("Surface was already locked"); |
| 459 |
} |
| 460 |
mLockedObject = nativeLockCanvas(mNativeObject, mCanvas, inOutDirty); |
| 461 |
return mCanvas; |
| 462 |
} |
| 463 |
} |
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
public void unlockCanvasAndPost(Canvas canvas) { |
| 472 |
synchronized (mLock) { |
| 473 |
checkNotReleasedLocked(); |
| 474 |
|
| 475 |
if (mHwuiContext != null) { |
| 476 |
mHwuiContext.unlockAndPost(canvas); |
| 477 |
} else { |
| 478 |
unlockSwCanvasAndPost(canvas); |
| 479 |
} |
| 480 |
} |
| 481 |
} |
| 482 |
|
| 483 |
private void unlockSwCanvasAndPost(Canvas canvas) { |
| 484 |
if (canvas != mCanvas) { |
| 485 |
throw new IllegalArgumentException("canvas object must be the same instance that " |
| 486 |
+ "was previously returned by lockCanvas"); |
| 487 |
} |
| 488 |
if (mNativeObject != mLockedObject) { |
| 489 |
Log.w(TAG, "WARNING: Surface's mNativeObject (0x" + |
| 490 |
Long.toHexString(mNativeObject) + ") != mLockedObject (0x" + |
| 491 |
Long.toHexString(mLockedObject) +")"); |
| 492 |
} |
| 493 |
if (mLockedObject == 0) { |
| 494 |
throw new IllegalStateException("Surface was not locked"); |
| 495 |
} |
| 496 |
try { |
| 497 |
nativeUnlockCanvasAndPost(mLockedObject, canvas); |
| 498 |
} finally { |
| 499 |
nativeRelease(mLockedObject); |
| 500 |
mLockedObject = 0; |
| 501 |
} |
| 502 |
} |
| 503 |
|
| 504 |
|
| 505 |
|
| 506 |
|
| 507 |
|
| 508 |
|
| 509 |
|
| 510 |
|
| 511 |
|
| 512 |
|
| 513 |
|
| 514 |
|
| 515 |
|
| 516 |
|
| 517 |
|
| 518 |
|
| 519 |
|
| 520 |
|
| 521 |
|
| 522 |
public Canvas lockHardwareCanvas() { |
| 523 |
synchronized (mLock) { |
| 524 |
checkNotReleasedLocked(); |
| 525 |
if (mHwuiContext == null) { |
| 526 |
mHwuiContext = new HwuiContext(false); |
| 527 |
} |
| 528 |
return mHwuiContext.lockCanvas( |
| 529 |
nativeGetWidth(mNativeObject), |
| 530 |
nativeGetHeight(mNativeObject)); |
| 531 |
} |
| 532 |
} |
| 533 |
|
| 534 |
|
| 535 |
|
| 536 |
|
| 537 |
|
| 538 |
|
| 539 |
|
| 540 |
|
| 541 |
|
| 542 |
|
| 543 |
|
| 544 |
|
| 545 |
|
| 546 |
|
| 547 |
|
| 548 |
|
| 549 |
|
| 550 |
|
| 551 |
|
| 552 |
|
| 553 |
|
| 554 |
|
| 555 |
public Canvas lockHardwareWideColorGamutCanvas() { |
| 556 |
synchronized (mLock) { |
| 557 |
checkNotReleasedLocked(); |
| 558 |
if (mHwuiContext != null && !mHwuiContext.isWideColorGamut()) { |
| 559 |
mHwuiContext.destroy(); |
| 560 |
mHwuiContext = null; |
| 561 |
} |
| 562 |
if (mHwuiContext == null) { |
| 563 |
mHwuiContext = new HwuiContext(true); |
| 564 |
} |
| 565 |
return mHwuiContext.lockCanvas( |
| 566 |
nativeGetWidth(mNativeObject), |
| 567 |
nativeGetHeight(mNativeObject)); |
| 568 |
} |
| 569 |
} |
| 570 |
|
| 571 |
|
| 572 |
|
| 573 |
|
| 574 |
@Deprecated |
| 575 |
public void unlockCanvas(Canvas canvas) { |
| 576 |
throw new UnsupportedOperationException(); |
| 577 |
} |
| 578 |
|
| 579 |
|
| 580 |
|
| 581 |
|
| 582 |
|
| 583 |
void setCompatibilityTranslator(Translator translator) { |
| 584 |
if (translator != null) { |
| 585 |
float appScale = translator.applicationScale; |
| 586 |
mCompatibleMatrix = new Matrix(); |
| 587 |
mCompatibleMatrix.setScale(appScale, appScale); |
| 588 |
} |
| 589 |
} |
| 590 |
|
| 591 |
private void updateNativeObject(long newNativeObject) { |
| 592 |
synchronized (mLock) { |
| 593 |
if (newNativeObject == mNativeObject) { |
| 594 |
return; |
| 595 |
} |
| 596 |
if (mNativeObject != 0) { |
| 597 |
nativeRelease(mNativeObject); |
| 598 |
} |
| 599 |
setNativeObjectLocked(newNativeObject); |
| 600 |
} |
| 601 |
} |
| 602 |
|
| 603 |
|
| 604 |
|
| 605 |
|
| 606 |
|
| 607 |
|
| 608 |
|
| 609 |
|
| 610 |
|
| 611 |
|
| 612 |
|
| 613 |
|
| 614 |
@UnsupportedAppUsage |
| 615 |
public void copyFrom(SurfaceControl other) { |
| 616 |
if (other == null) { |
| 617 |
throw new IllegalArgumentException("other must not be null"); |
| 618 |
} |
| 619 |
|
| 620 |
long surfaceControlPtr = other.mNativeObject; |
| 621 |
if (surfaceControlPtr == 0) { |
| 622 |
throw new NullPointerException( |
| 623 |
"null SurfaceControl native object. Are you using a released SurfaceControl?"); |
| 624 |
} |
| 625 |
long newNativeObject = nativeGetFromSurfaceControl(mNativeObject, surfaceControlPtr); |
| 626 |
updateNativeObject(newNativeObject); |
| 627 |
} |
| 628 |
|
| 629 |
|
| 630 |
|
| 631 |
|
| 632 |
|
| 633 |
|
| 634 |
|
| 635 |
|
| 636 |
public void copyFrom(BLASTBufferQueue queue) { |
| 637 |
if (queue == null) { |
| 638 |
throw new IllegalArgumentException("queue must not be null"); |
| 639 |
} |
| 640 |
|
| 641 |
long blastBufferQueuePtr = queue.mNativeObject; |
| 642 |
if (blastBufferQueuePtr == 0) { |
| 643 |
throw new NullPointerException("Null BLASTBufferQueue native object"); |
| 644 |
} |
| 645 |
long newNativeObject = nativeGetFromBlastBufferQueue(mNativeObject, blastBufferQueuePtr); |
| 646 |
updateNativeObject(newNativeObject); |
| 647 |
} |
| 648 |
|
| 649 |
|
| 650 |
|
| 651 |
|
| 652 |
|
| 653 |
|
| 654 |
|
| 655 |
|
| 656 |
|
| 657 |
|
| 658 |
|
| 659 |
|
| 660 |
|
| 661 |
public void createFrom(SurfaceControl other) { |
| 662 |
if (other == null) { |
| 663 |
throw new IllegalArgumentException("other must not be null"); |
| 664 |
} |
| 665 |
|
| 666 |
long surfaceControlPtr = other.mNativeObject; |
| 667 |
if (surfaceControlPtr == 0) { |
| 668 |
throw new NullPointerException( |
| 669 |
"null SurfaceControl native object. Are you using a released SurfaceControl?"); |
| 670 |
} |
| 671 |
long newNativeObject = nativeCreateFromSurfaceControl(surfaceControlPtr); |
| 672 |
|
| 673 |
synchronized (mLock) { |
| 674 |
if (mNativeObject != 0) { |
| 675 |
nativeRelease(mNativeObject); |
| 676 |
} |
| 677 |
setNativeObjectLocked(newNativeObject); |
| 678 |
} |
| 679 |
} |
| 680 |
|
| 681 |
|
| 682 |
|
| 683 |
|
| 684 |
|
| 685 |
|
| 686 |
|
| 687 |
@Deprecated |
| 688 |
@UnsupportedAppUsage |
| 689 |
public void transferFrom(Surface other) { |
| 690 |
if (other == null) { |
| 691 |
throw new IllegalArgumentException("other must not be null"); |
| 692 |
} |
| 693 |
if (other != this) { |
| 694 |
final long newPtr; |
| 695 |
synchronized (other.mLock) { |
| 696 |
newPtr = other.mNativeObject; |
| 697 |
other.setNativeObjectLocked(0); |
| 698 |
} |
| 699 |
|
| 700 |
synchronized (mLock) { |
| 701 |
if (mNativeObject != 0) { |
| 702 |
nativeRelease(mNativeObject); |
| 703 |
} |
| 704 |
setNativeObjectLocked(newPtr); |
| 705 |
} |
| 706 |
} |
| 707 |
} |
| 708 |
|
| 709 |
@Override |
| 710 |
public int describeContents() { |
| 711 |
return 0; |
| 712 |
} |
| 713 |
|
| 714 |
public void readFromParcel(Parcel source) { |
| 715 |
if (source == null) { |
| 716 |
throw new IllegalArgumentException("source must not be null"); |
| 717 |
} |
| 718 |
|
| 719 |
synchronized (mLock) { |
| 720 |
|
| 721 |
|
| 722 |
|
| 723 |
|
| 724 |
|
| 725 |
|
| 726 |
mName = source.readString(); |
| 727 |
mIsSingleBuffered = source.readInt() != 0; |
| 728 |
setNativeObjectLocked(nativeReadFromParcel(mNativeObject, source)); |
| 729 |
} |
| 730 |
} |
| 731 |
|
| 732 |
@Override |
| 733 |
public void writeToParcel(Parcel dest, int flags) { |
| 734 |
if (dest == null) { |
| 735 |
throw new IllegalArgumentException("dest must not be null"); |
| 736 |
} |
| 737 |
synchronized (mLock) { |
| 738 |
|
| 739 |
|
| 740 |
dest.writeString(mName); |
| 741 |
dest.writeInt(mIsSingleBuffered ? 1 : 0); |
| 742 |
nativeWriteToParcel(mNativeObject, dest); |
| 743 |
} |
| 744 |
if ((flags & Parcelable.PARCELABLE_WRITE_RETURN_VALUE) != 0) { |
| 745 |
release(); |
| 746 |
} |
| 747 |
} |
| 748 |
|
| 749 |
@Override |
| 750 |
public String toString() { |
| 751 |
synchronized (mLock) { |
| 752 |
return "Surface(name=" + mName + ")/@0x" + |
| 753 |
Integer.toHexString(System.identityHashCode(this)); |
| 754 |
} |
| 755 |
} |
| 756 |
|
| 757 |
private void setNativeObjectLocked(long ptr) { |
| 758 |
if (mNativeObject != ptr) { |
| 759 |
if (mNativeObject == 0 && ptr != 0) { |
| 760 |
mCloseGuard.open("Surface.release"); |
| 761 |
} else if (mNativeObject != 0 && ptr == 0) { |
| 762 |
mCloseGuard.close(); |
| 763 |
} |
| 764 |
mNativeObject = ptr; |
| 765 |
mGenerationId += 1; |
| 766 |
if (mHwuiContext != null) { |
| 767 |
mHwuiContext.updateSurface(); |
| 768 |
} |
| 769 |
} |
| 770 |
} |
| 771 |
|
| 772 |
private void checkNotReleasedLocked() { |
| 773 |
if (mNativeObject == 0) { |
| 774 |
throw new IllegalStateException("Surface has already been released."); |
| 775 |
} |
| 776 |
} |
| 777 |
|
| 778 |
|
| 779 |
|
| 780 |
|
| 781 |
|
| 782 |
public void allocateBuffers() { |
| 783 |
synchronized (mLock) { |
| 784 |
checkNotReleasedLocked(); |
| 785 |
nativeAllocateBuffers(mNativeObject); |
| 786 |
} |
| 787 |
} |
| 788 |
|
| 789 |
|
| 790 |
|
| 791 |
|
| 792 |
|
| 793 |
public void setScalingMode(@ScalingMode int scalingMode) { |
| 794 |
synchronized (mLock) { |
| 795 |
checkNotReleasedLocked(); |
| 796 |
int err = nativeSetScalingMode(mNativeObject, scalingMode); |
| 797 |
if (err != 0) { |
| 798 |
throw new IllegalArgumentException("Invalid scaling mode: " + scalingMode); |
| 799 |
} |
| 800 |
} |
| 801 |
} |
| 802 |
|
| 803 |
void forceScopedDisconnect() { |
| 804 |
synchronized (mLock) { |
| 805 |
checkNotReleasedLocked(); |
| 806 |
int err = nativeForceScopedDisconnect(mNativeObject); |
| 807 |
if (err != 0) { |
| 808 |
throw new RuntimeException("Failed to disconnect Surface instance (bad object?)"); |
| 809 |
} |
| 810 |
} |
| 811 |
} |
| 812 |
|
| 813 |
|
| 814 |
|
| 815 |
|
| 816 |
|
| 817 |
|
| 818 |
|
| 819 |
public void attachAndQueueBufferWithColorSpace(HardwareBuffer buffer, ColorSpace colorSpace) { |
| 820 |
synchronized (mLock) { |
| 821 |
checkNotReleasedLocked(); |
| 822 |
if (colorSpace == null) { |
| 823 |
colorSpace = ColorSpace.get(ColorSpace.Named.SRGB); |
| 824 |
} |
| 825 |
int err = nativeAttachAndQueueBufferWithColorSpace(mNativeObject, buffer, |
| 826 |
colorSpace.getId()); |
| 827 |
if (err != 0) { |
| 828 |
throw new RuntimeException( |
| 829 |
"Failed to attach and queue buffer to Surface (bad object?), " |
| 830 |
+ "native error: " + err); |
| 831 |
} |
| 832 |
} |
| 833 |
} |
| 834 |
|
| 835 |
|
| 836 |
|
| 837 |
|
| 838 |
|
| 839 |
public boolean isSingleBuffered() { |
| 840 |
return mIsSingleBuffered; |
| 841 |
} |
| 842 |
|
| 843 |
|
| 844 |
|
| 845 |
|
| 846 |
|
| 847 |
|
| 848 |
|
| 849 |
|
| 850 |
|
| 851 |
|
| 852 |
|
| 853 |
|
| 854 |
|
| 855 |
|
| 856 |
|
| 857 |
|
| 858 |
|
| 859 |
|
| 860 |
|
| 861 |
|
| 862 |
|
| 863 |
|
| 864 |
|
| 865 |
|
| 866 |
|
| 867 |
|
| 868 |
|
| 869 |
|
| 870 |
|
| 871 |
|
| 872 |
|
| 873 |
|
| 874 |
|
| 875 |
|
| 876 |
|
| 877 |
|
| 878 |
public void setSharedBufferModeEnabled(boolean enabled) { |
| 879 |
if (mIsSharedBufferModeEnabled != enabled) { |
| 880 |
int error = nativeSetSharedBufferModeEnabled(mNativeObject, enabled); |
| 881 |
if (error != 0) { |
| 882 |
throw new RuntimeException( |
| 883 |
"Failed to set shared buffer mode on Surface (bad object?)"); |
| 884 |
} else { |
| 885 |
mIsSharedBufferModeEnabled = enabled; |
| 886 |
} |
| 887 |
} |
| 888 |
} |
| 889 |
|
| 890 |
|
| 891 |
|
| 892 |
|
| 893 |
|
| 894 |
|
| 895 |
|
| 896 |
|
| 897 |
public boolean isSharedBufferModeEnabled() { |
| 898 |
return mIsSharedBufferModeEnabled; |
| 899 |
} |
| 900 |
|
| 901 |
|
| 902 |
|
| 903 |
|
| 904 |
|
| 905 |
|
| 906 |
|
| 907 |
|
| 908 |
|
| 909 |
|
| 910 |
|
| 911 |
|
| 912 |
|
| 913 |
|
| 914 |
|
| 915 |
|
| 916 |
|
| 917 |
|
| 918 |
|
| 919 |
|
| 920 |
|
| 921 |
|
| 922 |
public void setAutoRefreshEnabled(boolean enabled) { |
| 923 |
if (mIsAutoRefreshEnabled != enabled) { |
| 924 |
int error = nativeSetAutoRefreshEnabled(mNativeObject, enabled); |
| 925 |
if (error != 0) { |
| 926 |
throw new RuntimeException("Failed to set auto refresh on Surface (bad object?)"); |
| 927 |
} else { |
| 928 |
mIsAutoRefreshEnabled = enabled; |
| 929 |
} |
| 930 |
} |
| 931 |
} |
| 932 |
|
| 933 |
|
| 934 |
|
| 935 |
|
| 936 |
|
| 937 |
|
| 938 |
public boolean isAutoRefreshEnabled() { |
| 939 |
return mIsAutoRefreshEnabled; |
| 940 |
} |
| 941 |
|
| 942 |
|
| 943 |
|
| 944 |
|
| 945 |
|
| 946 |
|
| 947 |
|
| 948 |
|
| 949 |
|
| 950 |
|
| 951 |
|
| 952 |
|
| 953 |
|
| 954 |
|
| 955 |
|
| 956 |
|
| 957 |
|
| 958 |
|
| 959 |
|
| 960 |
|
| 961 |
|
| 962 |
|
| 963 |
|
| 964 |
|
| 965 |
|
| 966 |
|
| 967 |
|
| 968 |
|
| 969 |
|
| 970 |
|
| 971 |
|
| 972 |
|
| 973 |
|
| 974 |
|
| 975 |
|
| 976 |
public void setFrameRate(@FloatRange(from = 0.0) float frameRate, |
| 977 |
@FrameRateCompatibility int compatibility, |
| 978 |
@ChangeFrameRateStrategy int changeFrameRateStrategy) { |
| 979 |
synchronized (mLock) { |
| 980 |
checkNotReleasedLocked(); |
| 981 |
int error = nativeSetFrameRate(mNativeObject, frameRate, compatibility, |
| 982 |
changeFrameRateStrategy); |
| 983 |
if (error == -EINVAL) { |
| 984 |
throw new IllegalArgumentException("Invalid argument to Surface.setFrameRate()"); |
| 985 |
} else if (error != 0) { |
| 986 |
throw new RuntimeException("Failed to set frame rate on Surface"); |
| 987 |
} |
| 988 |
} |
| 989 |
} |
| 990 |
|
| 991 |
|
| 992 |
|
| 993 |
|
| 994 |
|
| 995 |
|
| 996 |
|
| 997 |
public void setFrameRate( |
| 998 |
@FloatRange(from = 0.0) float frameRate, @FrameRateCompatibility int compatibility) { |
| 999 |
setFrameRate(frameRate, compatibility, CHANGE_FRAME_RATE_ONLY_IF_SEAMLESS); |
| 1000 |
} |
| 1001 |
|
| 1002 |
|
| 1003 |
|
| 1004 |
|
| 1005 |
|
| 1006 |
@SuppressWarnings("serial") |
| 1007 |
public static class OutOfResourcesException extends RuntimeException { |
| 1008 |
public OutOfResourcesException() { |
| 1009 |
} |
| 1010 |
public OutOfResourcesException(String name) { |
| 1011 |
super(name); |
| 1012 |
} |
| 1013 |
} |
| 1014 |
|
| 1015 |
|
| 1016 |
|
| 1017 |
|
| 1018 |
|
| 1019 |
|
| 1020 |
|
| 1021 |
|
| 1022 |
|
| 1023 |
public static String rotationToString(int rotation) { |
| 1024 |
switch (rotation) { |
| 1025 |
case Surface.ROTATION_0: { |
| 1026 |
return "ROTATION_0"; |
| 1027 |
} |
| 1028 |
case Surface.ROTATION_90: { |
| 1029 |
return "ROTATION_90"; |
| 1030 |
} |
| 1031 |
case Surface.ROTATION_180: { |
| 1032 |
return "ROTATION_180"; |
| 1033 |
} |
| 1034 |
case Surface.ROTATION_270: { |
| 1035 |
return "ROTATION_270"; |
| 1036 |
} |
| 1037 |
default: { |
| 1038 |
return Integer.toString(rotation); |
| 1039 |
} |
| 1040 |
} |
| 1041 |
} |
| 1042 |
|
| 1043 |
|
| 1044 |
|
| 1045 |
|
| 1046 |
|
| 1047 |
|
| 1048 |
|
| 1049 |
|
| 1050 |
|
| 1051 |
|
| 1052 |
|
| 1053 |
|
| 1054 |
|
| 1055 |
|
| 1056 |
|
| 1057 |
|
| 1058 |
private final class CompatibleCanvas extends Canvas { |
| 1059 |
|
| 1060 |
private Matrix mOrigMatrix = null; |
| 1061 |
|
| 1062 |
@Override |
| 1063 |
public void setMatrix(Matrix matrix) { |
| 1064 |
if (mCompatibleMatrix == null || mOrigMatrix == null || mOrigMatrix.equals(matrix)) { |
| 1065 |
|
| 1066 |
|
| 1067 |
super.setMatrix(matrix); |
| 1068 |
} else { |
| 1069 |
Matrix m = new Matrix(mCompatibleMatrix); |
| 1070 |
m.preConcat(matrix); |
| 1071 |
super.setMatrix(m); |
| 1072 |
} |
| 1073 |
} |
| 1074 |
|
| 1075 |
@SuppressWarnings("deprecation") |
| 1076 |
@Override |
| 1077 |
public void getMatrix(Matrix m) { |
| 1078 |
super.getMatrix(m); |
| 1079 |
if (mOrigMatrix == null) { |
| 1080 |
mOrigMatrix = new Matrix(); |
| 1081 |
} |
| 1082 |
mOrigMatrix.set(m); |
| 1083 |
} |
| 1084 |
} |
| 1085 |
|
| 1086 |
private final class HwuiContext { |
| 1087 |
private final RenderNode mRenderNode; |
| 1088 |
private HardwareRenderer mHardwareRenderer; |
| 1089 |
private RecordingCanvas mCanvas; |
| 1090 |
private final boolean mIsWideColorGamut; |
| 1091 |
|
| 1092 |
HwuiContext(boolean isWideColorGamut) { |
| 1093 |
mRenderNode = RenderNode.create("HwuiCanvas", null); |
| 1094 |
mRenderNode.setClipToBounds(false); |
| 1095 |
mRenderNode.setForceDarkAllowed(false); |
| 1096 |
mIsWideColorGamut = isWideColorGamut; |
| 1097 |
|
| 1098 |
mHardwareRenderer = new HardwareRenderer(); |
| 1099 |
mHardwareRenderer.setContentRoot(mRenderNode); |
| 1100 |
mHardwareRenderer.setSurface(Surface.this, true); |
| 1101 |
mHardwareRenderer.setColorMode( |
| 1102 |
isWideColorGamut |
| 1103 |
? ActivityInfo.COLOR_MODE_WIDE_COLOR_GAMUT |
| 1104 |
: ActivityInfo.COLOR_MODE_DEFAULT); |
| 1105 |
mHardwareRenderer.setLightSourceAlpha(0.0f, 0.0f); |
| 1106 |
mHardwareRenderer.setLightSourceGeometry(0.0f, 0.0f, 0.0f, 0.0f); |
| 1107 |
} |
| 1108 |
|
| 1109 |
Canvas lockCanvas(int width, int height) { |
| 1110 |
if (mCanvas != null) { |
| 1111 |
throw new IllegalStateException("Surface was already locked!"); |
| 1112 |
} |
| 1113 |
mCanvas = mRenderNode.beginRecording(width, height); |
| 1114 |
return mCanvas; |
| 1115 |
} |
| 1116 |
|
| 1117 |
void unlockAndPost(Canvas canvas) { |
| 1118 |
if (canvas != mCanvas) { |
| 1119 |
throw new IllegalArgumentException("canvas object must be the same instance that " |
| 1120 |
+ "was previously returned by lockCanvas"); |
| 1121 |
} |
| 1122 |
mRenderNode.endRecording(); |
| 1123 |
mCanvas = null; |
| 1124 |
mHardwareRenderer.createRenderRequest() |
| 1125 |
.setVsyncTime(System.nanoTime()) |
| 1126 |
.syncAndDraw(); |
| 1127 |
} |
| 1128 |
|
| 1129 |
void updateSurface() { |
| 1130 |
mHardwareRenderer.setSurface(Surface.this, true); |
| 1131 |
} |
| 1132 |
|
| 1133 |
void destroy() { |
| 1134 |
mHardwareRenderer.destroy(); |
| 1135 |
} |
| 1136 |
|
| 1137 |
boolean isWideColorGamut() { |
| 1138 |
return mIsWideColorGamut; |
| 1139 |
} |
| 1140 |
} |
| 1141 |
} |