1 /* 2 * Copyright (C) 2016 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 package android.view; 18 19 import android.annotation.IntDef; 20 import android.annotation.NonNull; 21 import android.annotation.Nullable; 22 import android.annotation.SuppressLint; 23 import android.graphics.Bitmap; 24 import android.graphics.HardwareRenderer; 25 import android.graphics.Rect; 26 import android.os.Handler; 27 import android.view.ViewTreeObserver.OnDrawListener; 28 29 import java.lang.annotation.Retention; 30 import java.lang.annotation.RetentionPolicy; 31 import java.util.concurrent.Executor; 32 import java.util.function.Consumer; 33 34 /** 35 * Provides a mechanisms to issue pixel copy requests to allow for copy 36 * operations from {@link Surface} to {@link Bitmap} 37 */ 38 public final class PixelCopy { 39 40 /** @hide */ 41 @Retention(RetentionPolicy.SOURCE) 42 @IntDef({SUCCESS, ERROR_UNKNOWN, ERROR_TIMEOUT, ERROR_SOURCE_NO_DATA, 43 ERROR_SOURCE_INVALID, ERROR_DESTINATION_INVALID}) 44 public @interface CopyResultStatus {} 45 46 /** The pixel copy request succeeded */ 47 public static final int SUCCESS = 0; 48 49 /** The pixel copy request failed with an unknown error. */ 50 public static final int ERROR_UNKNOWN = 1; 51 52 /** 53 * A timeout occurred while trying to acquire a buffer from the source to 54 * copy from. 55 */ 56 public static final int ERROR_TIMEOUT = 2; 57 58 /** 59 * The source has nothing to copy from. When the source is a {@link Surface} 60 * this means that no buffers have been queued yet. Wait for the source 61 * to produce a frame and try again. 62 */ 63 public static final int ERROR_SOURCE_NO_DATA = 3; 64 65 /** 66 * It is not possible to copy from the source. This can happen if the source 67 * is hardware-protected or destroyed. 68 */ 69 public static final int ERROR_SOURCE_INVALID = 4; 70 71 /** 72 * The destination isn't a valid copy target. If the destination is a bitmap 73 * this can occur if the bitmap is too large for the hardware to copy to. 74 * It can also occur if the destination has been destroyed. 75 */ 76 public static final int ERROR_DESTINATION_INVALID = 5; 77 78 /** 79 * Listener for observing the completion of a PixelCopy request. 80 */ 81 public interface OnPixelCopyFinishedListener { 82 /** 83 * Callback for when a pixel copy request has completed. This will be called 84 * regardless of whether the copy succeeded or failed. 85 * 86 * @param copyResult Contains the resulting status of the copy request. 87 * This will either be {@link PixelCopy#SUCCESS} or one of the 88 * <code>PixelCopy.ERROR_*</code> values. 89 */ onPixelCopyFinished(@opyResultStatus int copyResult)90 void onPixelCopyFinished(@CopyResultStatus int copyResult); 91 } 92 93 /** 94 * Requests for the display content of a {@link SurfaceView} to be copied 95 * into a provided {@link Bitmap}. 96 * 97 * The contents of the source will be scaled to fit exactly inside the bitmap. 98 * The pixel format of the source buffer will be converted, as part of the copy, 99 * to fit the bitmap's {@link Bitmap.Config}. The most recently queued buffer 100 * in the SurfaceView's Surface will be used as the source of the copy. 101 * 102 * @param source The source from which to copy 103 * @param dest The destination of the copy. The source will be scaled to 104 * match the width, height, and format of this bitmap. 105 * @param listener Callback for when the pixel copy request completes 106 * @param listenerThread The callback will be invoked on this Handler when 107 * the copy is finished. 108 */ request(@onNull SurfaceView source, @NonNull Bitmap dest, @NonNull OnPixelCopyFinishedListener listener, @NonNull Handler listenerThread)109 public static void request(@NonNull SurfaceView source, @NonNull Bitmap dest, 110 @NonNull OnPixelCopyFinishedListener listener, @NonNull Handler listenerThread) { 111 request(source.getHolder().getSurface(), dest, listener, listenerThread); 112 } 113 114 /** 115 * Requests for the display content of a {@link SurfaceView} to be copied 116 * into a provided {@link Bitmap}. 117 * 118 * The contents of the source will be scaled to fit exactly inside the bitmap. 119 * The pixel format of the source buffer will be converted, as part of the copy, 120 * to fit the bitmap's {@link Bitmap.Config}. The most recently queued buffer 121 * in the SurfaceView's Surface will be used as the source of the copy. 122 * 123 * @param source The source from which to copy 124 * @param srcRect The area of the source to copy from. If this is null 125 * the copy area will be the entire surface. The rect will be clamped to 126 * the bounds of the Surface. 127 * @param dest The destination of the copy. The source will be scaled to 128 * match the width, height, and format of this bitmap. 129 * @param listener Callback for when the pixel copy request completes 130 * @param listenerThread The callback will be invoked on this Handler when 131 * the copy is finished. 132 */ request(@onNull SurfaceView source, @Nullable Rect srcRect, @NonNull Bitmap dest, @NonNull OnPixelCopyFinishedListener listener, @NonNull Handler listenerThread)133 public static void request(@NonNull SurfaceView source, @Nullable Rect srcRect, 134 @NonNull Bitmap dest, @NonNull OnPixelCopyFinishedListener listener, 135 @NonNull Handler listenerThread) { 136 request(source.getHolder().getSurface(), srcRect, 137 dest, listener, listenerThread); 138 } 139 140 /** 141 * Requests a copy of the pixels from a {@link Surface} to be copied into 142 * a provided {@link Bitmap}. 143 * 144 * The contents of the source will be scaled to fit exactly inside the bitmap. 145 * The pixel format of the source buffer will be converted, as part of the copy, 146 * to fit the bitmap's {@link Bitmap.Config}. The most recently queued buffer 147 * in the Surface will be used as the source of the copy. 148 * 149 * @param source The source from which to copy 150 * @param dest The destination of the copy. The source will be scaled to 151 * match the width, height, and format of this bitmap. 152 * @param listener Callback for when the pixel copy request completes 153 * @param listenerThread The callback will be invoked on this Handler when 154 * the copy is finished. 155 */ request(@onNull Surface source, @NonNull Bitmap dest, @NonNull OnPixelCopyFinishedListener listener, @NonNull Handler listenerThread)156 public static void request(@NonNull Surface source, @NonNull Bitmap dest, 157 @NonNull OnPixelCopyFinishedListener listener, @NonNull Handler listenerThread) { 158 request(source, null, dest, listener, listenerThread); 159 } 160 161 /** 162 * Requests a copy of the pixels at the provided {@link Rect} from 163 * a {@link Surface} to be copied into a provided {@link Bitmap}. 164 * 165 * The contents of the source rect will be scaled to fit exactly inside the bitmap. 166 * The pixel format of the source buffer will be converted, as part of the copy, 167 * to fit the bitmap's {@link Bitmap.Config}. The most recently queued buffer 168 * in the Surface will be used as the source of the copy. 169 * 170 * @param source The source from which to copy 171 * @param srcRect The area of the source to copy from. If this is null 172 * the copy area will be the entire surface. The rect will be clamped to 173 * the bounds of the Surface. 174 * @param dest The destination of the copy. The source will be scaled to 175 * match the width, height, and format of this bitmap. 176 * @param listener Callback for when the pixel copy request completes 177 * @param listenerThread The callback will be invoked on this Handler when 178 * the copy is finished. 179 */ request(@onNull Surface source, @Nullable Rect srcRect, @NonNull Bitmap dest, @NonNull OnPixelCopyFinishedListener listener, @NonNull Handler listenerThread)180 public static void request(@NonNull Surface source, @Nullable Rect srcRect, 181 @NonNull Bitmap dest, @NonNull OnPixelCopyFinishedListener listener, 182 @NonNull Handler listenerThread) { 183 validateBitmapDest(dest); 184 if (!source.isValid()) { 185 throw new IllegalArgumentException("Surface isn't valid, source.isValid() == false"); 186 } 187 if (srcRect != null && srcRect.isEmpty()) { 188 throw new IllegalArgumentException("sourceRect is empty"); 189 } 190 HardwareRenderer.copySurfaceInto(source, new HardwareRenderer.CopyRequest(srcRect, dest) { 191 @Override 192 public void onCopyFinished(int result) { 193 listenerThread.post(() -> listener.onPixelCopyFinished(result)); 194 } 195 }); 196 } 197 198 /** 199 * Requests a copy of the pixels from a {@link Window} to be copied into 200 * a provided {@link Bitmap}. 201 * 202 * The contents of the source will be scaled to fit exactly inside the bitmap. 203 * The pixel format of the source buffer will be converted, as part of the copy, 204 * to fit the bitmap's {@link Bitmap.Config}. The most recently queued buffer 205 * in the Window's Surface will be used as the source of the copy. 206 * 207 * Note: This is limited to being able to copy from Window's with a non-null 208 * DecorView. If {@link Window#peekDecorView()} is null this throws an 209 * {@link IllegalArgumentException}. It will similarly throw an exception 210 * if the DecorView has not yet acquired a backing surface. It is recommended 211 * that {@link OnDrawListener} is used to ensure that at least one draw 212 * has happened before trying to copy from the window, otherwise either 213 * an {@link IllegalArgumentException} will be thrown or an error will 214 * be returned to the {@link OnPixelCopyFinishedListener}. 215 * 216 * @param source The source from which to copy 217 * @param dest The destination of the copy. The source will be scaled to 218 * match the width, height, and format of this bitmap. 219 * @param listener Callback for when the pixel copy request completes 220 * @param listenerThread The callback will be invoked on this Handler when 221 * the copy is finished. 222 */ request(@onNull Window source, @NonNull Bitmap dest, @NonNull OnPixelCopyFinishedListener listener, @NonNull Handler listenerThread)223 public static void request(@NonNull Window source, @NonNull Bitmap dest, 224 @NonNull OnPixelCopyFinishedListener listener, @NonNull Handler listenerThread) { 225 request(source, null, dest, listener, listenerThread); 226 } 227 228 /** 229 * Requests a copy of the pixels at the provided {@link Rect} from 230 * a {@link Window} to be copied into a provided {@link Bitmap}. 231 * 232 * The contents of the source rect will be scaled to fit exactly inside the bitmap. 233 * The pixel format of the source buffer will be converted, as part of the copy, 234 * to fit the bitmap's {@link Bitmap.Config}. The most recently queued buffer 235 * in the Window's Surface will be used as the source of the copy. 236 * 237 * Note: This is limited to being able to copy from Window's with a non-null 238 * DecorView. If {@link Window#peekDecorView()} is null this throws an 239 * {@link IllegalArgumentException}. It will similarly throw an exception 240 * if the DecorView has not yet acquired a backing surface. It is recommended 241 * that {@link OnDrawListener} is used to ensure that at least one draw 242 * has happened before trying to copy from the window, otherwise either 243 * an {@link IllegalArgumentException} will be thrown or an error will 244 * be returned to the {@link OnPixelCopyFinishedListener}. 245 * 246 * @param source The source from which to copy 247 * @param srcRect The area of the source to copy from. If this is null 248 * the copy area will be the entire surface. The rect will be clamped to 249 * the bounds of the Surface. 250 * @param dest The destination of the copy. The source will be scaled to 251 * match the width, height, and format of this bitmap. 252 * @param listener Callback for when the pixel copy request completes 253 * @param listenerThread The callback will be invoked on this Handler when 254 * the copy is finished. 255 */ request(@onNull Window source, @Nullable Rect srcRect, @NonNull Bitmap dest, @NonNull OnPixelCopyFinishedListener listener, @NonNull Handler listenerThread)256 public static void request(@NonNull Window source, @Nullable Rect srcRect, 257 @NonNull Bitmap dest, @NonNull OnPixelCopyFinishedListener listener, 258 @NonNull Handler listenerThread) { 259 validateBitmapDest(dest); 260 final Rect insets = new Rect(); 261 final Surface surface = sourceForWindow(source, insets); 262 request(surface, adjustSourceRectForInsets(insets, srcRect), dest, listener, 263 listenerThread); 264 } 265 validateBitmapDest(Bitmap bitmap)266 private static void validateBitmapDest(Bitmap bitmap) { 267 // TODO: Pre-check max texture dimens if we can 268 if (bitmap == null) { 269 throw new IllegalArgumentException("Bitmap cannot be null"); 270 } 271 if (bitmap.isRecycled()) { 272 throw new IllegalArgumentException("Bitmap is recycled"); 273 } 274 if (!bitmap.isMutable()) { 275 throw new IllegalArgumentException("Bitmap is immutable"); 276 } 277 } 278 sourceForWindow(Window source, Rect outInsets)279 private static Surface sourceForWindow(Window source, Rect outInsets) { 280 if (source == null) { 281 throw new IllegalArgumentException("source is null"); 282 } 283 if (source.peekDecorView() == null) { 284 throw new IllegalArgumentException( 285 "Only able to copy windows with decor views"); 286 } 287 Surface surface = null; 288 final ViewRootImpl root = source.peekDecorView().getViewRootImpl(); 289 if (root != null) { 290 surface = root.mSurface; 291 final Rect surfaceInsets = root.mWindowAttributes.surfaceInsets; 292 outInsets.set(surfaceInsets.left, surfaceInsets.top, 293 root.mWidth + surfaceInsets.left, root.mHeight + surfaceInsets.top); 294 } 295 if (surface == null || !surface.isValid()) { 296 throw new IllegalArgumentException( 297 "Window doesn't have a backing surface!"); 298 } 299 return surface; 300 } 301 adjustSourceRectForInsets(Rect insets, Rect srcRect)302 private static Rect adjustSourceRectForInsets(Rect insets, Rect srcRect) { 303 if (srcRect == null) { 304 return insets; 305 } 306 if (insets != null) { 307 srcRect.offset(insets.left, insets.top); 308 } 309 return srcRect; 310 } 311 312 /** 313 * Contains the result of a PixelCopy request 314 */ 315 public static final class Result { 316 private int mStatus; 317 private Bitmap mBitmap; 318 Result(@opyResultStatus int status, Bitmap bitmap)319 private Result(@CopyResultStatus int status, Bitmap bitmap) { 320 mStatus = status; 321 mBitmap = bitmap; 322 } 323 324 /** 325 * Returns the status of the copy request. 326 */ getStatus()327 public @CopyResultStatus int getStatus() { 328 return mStatus; 329 } 330 validateStatus()331 private void validateStatus() { 332 if (mStatus != SUCCESS) { 333 throw new IllegalStateException("Copy request didn't succeed, status = " + mStatus); 334 } 335 } 336 337 /** 338 * If the PixelCopy {@link Request} was given a destination bitmap with 339 * {@link Request.Builder#setDestinationBitmap(Bitmap)} then the returned bitmap will be 340 * the same as the one given. If no destination bitmap was provided, then this 341 * will contain the automatically allocated Bitmap to hold the result. 342 * 343 * @return the Bitmap the copy request was stored in. 344 * @throws IllegalStateException if {@link #getStatus()} is not SUCCESS 345 */ getBitmap()346 public @NonNull Bitmap getBitmap() { 347 validateStatus(); 348 return mBitmap; 349 } 350 } 351 352 /** 353 * Represents a PixelCopy request. 354 * 355 * To create a copy request, use either of the PixelCopy.Request.ofWindow or 356 * PixelCopy.Request.ofSurface factories to create a {@link Request.Builder} for the 357 * given source content. After setting any optional parameters, such as 358 * {@link Builder#setSourceRect(Rect)}, build the request with {@link Builder#build()} and 359 * then execute it with {@link PixelCopy#request(Request, Executor, Consumer)} 360 */ 361 public static final class Request { 362 private final Surface mSource; 363 private final Rect mSourceInsets; 364 private Rect mSrcRect; 365 private Bitmap mDest; 366 Request(Surface source, Rect sourceInsets)367 private Request(Surface source, Rect sourceInsets) { 368 this.mSource = source; 369 this.mSourceInsets = sourceInsets; 370 } 371 372 /** 373 * A builder to create the complete PixelCopy request, which is then executed by calling 374 * {@link #request(Request, Executor, Consumer)} with the built request returned from 375 * {@link #build()} 376 */ 377 public static final class Builder { 378 private Request mRequest; 379 Builder(Request request)380 private Builder(Request request) { 381 mRequest = request; 382 } 383 384 /** 385 * Creates a PixelCopy Builder for the given {@link Window} 386 * @param source The Window to copy from 387 * @return A {@link Builder} builder to set the optional params & build the request 388 */ 389 @SuppressLint("BuilderSetStyle") ofWindow(@onNull Window source)390 public static @NonNull Builder ofWindow(@NonNull Window source) { 391 final Rect insets = new Rect(); 392 final Surface surface = sourceForWindow(source, insets); 393 return new Builder(new Request(surface, insets)); 394 } 395 396 /** 397 * Creates a PixelCopy Builder for the {@link Window} that the given {@link View} is 398 * attached to. 399 * 400 * Note that this copy request is not cropped to the area the View occupies by default. 401 * If that behavior is desired, use {@link View#getLocationInWindow(int[])} combined 402 * with {@link Builder#setSourceRect(Rect)} to set a crop area to restrict the copy 403 * operation. 404 * 405 * @param source A View that {@link View#isAttachedToWindow() is attached} to a window 406 * that will be used to retrieve the window to copy from. 407 * @return A {@link Builder} builder to set the optional params & build the request 408 */ 409 @SuppressLint("BuilderSetStyle") ofWindow(@onNull View source)410 public static @NonNull Builder ofWindow(@NonNull View source) { 411 if (source == null || !source.isAttachedToWindow()) { 412 throw new IllegalArgumentException( 413 "View must not be null & must be attached to window"); 414 } 415 final Rect insets = new Rect(); 416 Surface surface = null; 417 final ViewRootImpl root = source.getViewRootImpl(); 418 if (root != null) { 419 surface = root.mSurface; 420 insets.set(root.mWindowAttributes.surfaceInsets); 421 } 422 if (surface == null || !surface.isValid()) { 423 throw new IllegalArgumentException( 424 "Window doesn't have a backing surface!"); 425 } 426 return new Builder(new Request(surface, insets)); 427 } 428 429 /** 430 * Creates a PixelCopy Builder for the given {@link Surface} 431 * 432 * @param source The Surface to copy from. Must be {@link Surface#isValid() valid}. 433 * @return A {@link Builder} builder to set the optional params & build the request 434 */ 435 @SuppressLint("BuilderSetStyle") ofSurface(@onNull Surface source)436 public static @NonNull Builder ofSurface(@NonNull Surface source) { 437 if (source == null || !source.isValid()) { 438 throw new IllegalArgumentException("Source must not be null & must be valid"); 439 } 440 return new Builder(new Request(source, null)); 441 } 442 443 /** 444 * Creates a PixelCopy Builder for the {@link Surface} belonging to the 445 * given {@link SurfaceView} 446 * 447 * @param source The SurfaceView to copy from. The backing surface must be 448 * {@link Surface#isValid() valid} 449 * @return A {@link Builder} builder to set the optional params & build the request 450 */ 451 @SuppressLint("BuilderSetStyle") ofSurface(@onNull SurfaceView source)452 public static @NonNull Builder ofSurface(@NonNull SurfaceView source) { 453 return ofSurface(source.getHolder().getSurface()); 454 } 455 requireNotBuilt()456 private void requireNotBuilt() { 457 if (mRequest == null) { 458 throw new IllegalStateException("build() already called on this builder"); 459 } 460 } 461 462 /** 463 * Sets the region of the source to copy from. By default, the entire source is copied 464 * to the output. If only a subset of the source is necessary to be copied, specifying 465 * a srcRect will improve performance by reducing 466 * the amount of data being copied. 467 * 468 * @param srcRect The area of the source to read from. Null or empty will be treated to 469 * mean the entire source 470 * @return this 471 */ setSourceRect(@ullable Rect srcRect)472 public @NonNull Builder setSourceRect(@Nullable Rect srcRect) { 473 requireNotBuilt(); 474 mRequest.mSrcRect = srcRect; 475 return this; 476 } 477 478 /** 479 * Specifies the output bitmap in which to store the result. By default, a Bitmap of 480 * format {@link android.graphics.Bitmap.Config#ARGB_8888} with a width & height 481 * matching that of the {@link #setSourceRect(Rect) source area} will be created to 482 * place the result. 483 * 484 * @param destination The bitmap to store the result, or null to have a bitmap 485 * automatically created of the appropriate size. If not null, must 486 * not be {@link Bitmap#isRecycled() recycled} and must be 487 * {@link Bitmap#isMutable() mutable}. 488 * @return this 489 */ setDestinationBitmap(@ullable Bitmap destination)490 public @NonNull Builder setDestinationBitmap(@Nullable Bitmap destination) { 491 requireNotBuilt(); 492 if (destination != null) { 493 validateBitmapDest(destination); 494 } 495 mRequest.mDest = destination; 496 return this; 497 } 498 499 /** 500 * @return The built {@link PixelCopy.Request} 501 */ build()502 public @NonNull Request build() { 503 requireNotBuilt(); 504 Request ret = mRequest; 505 mRequest = null; 506 return ret; 507 } 508 } 509 510 /** 511 * @return The destination bitmap as set by {@link Builder#setDestinationBitmap(Bitmap)} 512 */ getDestinationBitmap()513 public @Nullable Bitmap getDestinationBitmap() { 514 return mDest; 515 } 516 517 /** 518 * @return The source rect to copy from as set by {@link Builder#setSourceRect(Rect)} 519 */ getSourceRect()520 public @Nullable Rect getSourceRect() { 521 return mSrcRect; 522 } 523 524 /** 525 * @hide 526 */ request(@onNull Executor callbackExecutor, @NonNull Consumer<Result> listener)527 public void request(@NonNull Executor callbackExecutor, 528 @NonNull Consumer<Result> listener) { 529 if (!mSource.isValid()) { 530 callbackExecutor.execute(() -> listener.accept( 531 new Result(ERROR_SOURCE_INVALID, null))); 532 return; 533 } 534 HardwareRenderer.copySurfaceInto(mSource, new HardwareRenderer.CopyRequest( 535 adjustSourceRectForInsets(mSourceInsets, mSrcRect), mDest) { 536 @Override 537 public void onCopyFinished(int result) { 538 callbackExecutor.execute(() -> listener.accept( 539 new Result(result, mDestinationBitmap))); 540 } 541 }); 542 } 543 } 544 545 /** 546 * Executes the pixel copy request 547 * @param request The request to execute 548 * @param callbackExecutor The executor to run the callback on 549 * @param listener The callback for when the copy request is completed 550 */ request(@onNull Request request, @NonNull Executor callbackExecutor, @NonNull Consumer<Result> listener)551 public static void request(@NonNull Request request, @NonNull Executor callbackExecutor, 552 @NonNull Consumer<Result> listener) { 553 request.request(callbackExecutor, listener); 554 } 555 PixelCopy()556 private PixelCopy() {} 557 } 558