John Reck | e94cbc7 | 2016-04-25 13:03:44 -0700 | [diff] [blame] | 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; |
John Reck | 9580146 | 2016-09-01 09:44:09 -0700 | [diff] [blame] | 21 | import android.annotation.Nullable; |
John Reck | 056e5dc | 2022-11-18 17:19:35 -0500 | [diff] [blame] | 22 | import android.annotation.SuppressLint; |
John Reck | e94cbc7 | 2016-04-25 13:03:44 -0700 | [diff] [blame] | 23 | import android.graphics.Bitmap; |
John Reck | 4d73cb1 | 2022-07-27 10:32:52 -0400 | [diff] [blame] | 24 | import android.graphics.HardwareRenderer; |
John Reck | 9580146 | 2016-09-01 09:44:09 -0700 | [diff] [blame] | 25 | import android.graphics.Rect; |
John Reck | e94cbc7 | 2016-04-25 13:03:44 -0700 | [diff] [blame] | 26 | import android.os.Handler; |
John Reck | 9580146 | 2016-09-01 09:44:09 -0700 | [diff] [blame] | 27 | import android.view.ViewTreeObserver.OnDrawListener; |
John Reck | e94cbc7 | 2016-04-25 13:03:44 -0700 | [diff] [blame] | 28 | |
| 29 | import java.lang.annotation.Retention; |
| 30 | import java.lang.annotation.RetentionPolicy; |
John Reck | 4d73cb1 | 2022-07-27 10:32:52 -0400 | [diff] [blame] | 31 | import java.util.concurrent.Executor; |
| 32 | import java.util.function.Consumer; |
John Reck | e94cbc7 | 2016-04-25 13:03:44 -0700 | [diff] [blame] | 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 | */ |
| 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 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 | */ |
| 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 | /** |
John Reck | 9580146 | 2016-09-01 09:44:09 -0700 | [diff] [blame] | 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 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 | */ |
| 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 | /** |
John Reck | e94cbc7 | 2016-04-25 13:03:44 -0700 | [diff] [blame] | 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 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 | */ |
| 156 | public static void request(@NonNull Surface source, @NonNull Bitmap dest, |
| 157 | @NonNull OnPixelCopyFinishedListener listener, @NonNull Handler listenerThread) { |
John Reck | 9580146 | 2016-09-01 09:44:09 -0700 | [diff] [blame] | 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 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 | */ |
| 180 | public static void request(@NonNull Surface source, @Nullable Rect srcRect, |
| 181 | @NonNull Bitmap dest, @NonNull OnPixelCopyFinishedListener listener, |
| 182 | @NonNull Handler listenerThread) { |
John Reck | e94cbc7 | 2016-04-25 13:03:44 -0700 | [diff] [blame] | 183 | validateBitmapDest(dest); |
John Reck | f3a51d6 | 2016-04-27 15:23:51 -0700 | [diff] [blame] | 184 | if (!source.isValid()) { |
| 185 | throw new IllegalArgumentException("Surface isn't valid, source.isValid() == false"); |
| 186 | } |
John Reck | 9580146 | 2016-09-01 09:44:09 -0700 | [diff] [blame] | 187 | if (srcRect != null && srcRect.isEmpty()) { |
| 188 | throw new IllegalArgumentException("sourceRect is empty"); |
| 189 | } |
John Reck | 4d73cb1 | 2022-07-27 10:32:52 -0400 | [diff] [blame] | 190 | HardwareRenderer.copySurfaceInto(source, new HardwareRenderer.CopyRequest(srcRect, dest) { |
John Reck | e94cbc7 | 2016-04-25 13:03:44 -0700 | [diff] [blame] | 191 | @Override |
John Reck | 4d73cb1 | 2022-07-27 10:32:52 -0400 | [diff] [blame] | 192 | public void onCopyFinished(int result) { |
| 193 | listenerThread.post(() -> listener.onPixelCopyFinished(result)); |
John Reck | e94cbc7 | 2016-04-25 13:03:44 -0700 | [diff] [blame] | 194 | } |
| 195 | }); |
| 196 | } |
| 197 | |
John Reck | 9580146 | 2016-09-01 09:44:09 -0700 | [diff] [blame] | 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 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 | */ |
| 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 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 | */ |
| 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); |
John Reck | 4d73cb1 | 2022-07-27 10:32:52 -0400 | [diff] [blame] | 260 | final Rect insets = new Rect(); |
| 261 | final Surface surface = sourceForWindow(source, insets); |
| 262 | request(surface, adjustSourceRectForInsets(insets, srcRect), dest, listener, |
| 263 | listenerThread); |
John Reck | 9580146 | 2016-09-01 09:44:09 -0700 | [diff] [blame] | 264 | } |
| 265 | |
John Reck | e94cbc7 | 2016-04-25 13:03:44 -0700 | [diff] [blame] | 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 | |
John Reck | 4d73cb1 | 2022-07-27 10:32:52 -0400 | [diff] [blame] | 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 | |
| 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 | */ |
John Reck | 03256dd | 2022-10-27 15:32:51 -0400 | [diff] [blame] | 315 | public static final class Result { |
John Reck | 4d73cb1 | 2022-07-27 10:32:52 -0400 | [diff] [blame] | 316 | private int mStatus; |
| 317 | private Bitmap mBitmap; |
| 318 | |
John Reck | 03256dd | 2022-10-27 15:32:51 -0400 | [diff] [blame] | 319 | private Result(@CopyResultStatus int status, Bitmap bitmap) { |
John Reck | 4d73cb1 | 2022-07-27 10:32:52 -0400 | [diff] [blame] | 320 | mStatus = status; |
| 321 | mBitmap = bitmap; |
| 322 | } |
| 323 | |
| 324 | /** |
| 325 | * Returns the {@link CopyResultStatus} of the copy request. |
| 326 | */ |
| 327 | public @CopyResultStatus int getStatus() { |
| 328 | return mStatus; |
| 329 | } |
| 330 | |
| 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 |
John Reck | 03256dd | 2022-10-27 15:32:51 -0400 | [diff] [blame] | 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 |
John Reck | 4d73cb1 | 2022-07-27 10:32:52 -0400 | [diff] [blame] | 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 | */ |
| 346 | public @NonNull Bitmap getBitmap() { |
| 347 | validateStatus(); |
| 348 | return mBitmap; |
| 349 | } |
| 350 | } |
| 351 | |
| 352 | /** |
John Reck | 03256dd | 2022-10-27 15:32:51 -0400 | [diff] [blame] | 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 |
John Reck | 056e5dc | 2022-11-18 17:19:35 -0500 | [diff] [blame] | 359 | * then execute it with {@link PixelCopy#request(Request, Executor, Consumer)} |
John Reck | 4d73cb1 | 2022-07-27 10:32:52 -0400 | [diff] [blame] | 360 | */ |
| 361 | public static final class Request { |
John Reck | 03256dd | 2022-10-27 15:32:51 -0400 | [diff] [blame] | 362 | private final Surface mSource; |
John Reck | 03256dd | 2022-10-27 15:32:51 -0400 | [diff] [blame] | 363 | private final Rect mSourceInsets; |
| 364 | private Rect mSrcRect; |
| 365 | private Bitmap mDest; |
| 366 | |
John Reck | 056e5dc | 2022-11-18 17:19:35 -0500 | [diff] [blame] | 367 | private Request(Surface source, Rect sourceInsets) { |
John Reck | 4d73cb1 | 2022-07-27 10:32:52 -0400 | [diff] [blame] | 368 | this.mSource = source; |
| 369 | this.mSourceInsets = sourceInsets; |
John Reck | 4d73cb1 | 2022-07-27 10:32:52 -0400 | [diff] [blame] | 370 | } |
| 371 | |
John Reck | 4d73cb1 | 2022-07-27 10:32:52 -0400 | [diff] [blame] | 372 | /** |
John Reck | 03256dd | 2022-10-27 15:32:51 -0400 | [diff] [blame] | 373 | * A builder to create the complete PixelCopy request, which is then executed by calling |
John Reck | 056e5dc | 2022-11-18 17:19:35 -0500 | [diff] [blame] | 374 | * {@link #request(Request, Executor, Consumer)} with the built request returned from |
| 375 | * {@link #build()} |
John Reck | 4d73cb1 | 2022-07-27 10:32:52 -0400 | [diff] [blame] | 376 | */ |
John Reck | 03256dd | 2022-10-27 15:32:51 -0400 | [diff] [blame] | 377 | public static final class Builder { |
| 378 | private Request mRequest; |
John Reck | 4d73cb1 | 2022-07-27 10:32:52 -0400 | [diff] [blame] | 379 | |
John Reck | 03256dd | 2022-10-27 15:32:51 -0400 | [diff] [blame] | 380 | private Builder(Request request) { |
| 381 | mRequest = request; |
John Reck | 4d73cb1 | 2022-07-27 10:32:52 -0400 | [diff] [blame] | 382 | } |
John Reck | 03256dd | 2022-10-27 15:32:51 -0400 | [diff] [blame] | 383 | |
John Reck | 056e5dc | 2022-11-18 17:19:35 -0500 | [diff] [blame] | 384 | /** |
John Reck | d688419 | 2022-11-30 14:08:58 -0500 | [diff] [blame] | 385 | * Creates a PixelCopy Builder for the given {@link Window} |
John Reck | 056e5dc | 2022-11-18 17:19:35 -0500 | [diff] [blame] | 386 | * @param source The Window to copy from |
John Reck | d688419 | 2022-11-30 14:08:58 -0500 | [diff] [blame] | 387 | * @return A {@link Builder} builder to set the optional params & build the request |
John Reck | 056e5dc | 2022-11-18 17:19:35 -0500 | [diff] [blame] | 388 | */ |
| 389 | @SuppressLint("BuilderSetStyle") |
| 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 | /** |
John Reck | d688419 | 2022-11-30 14:08:58 -0500 | [diff] [blame] | 397 | * Creates a PixelCopy Builder for the {@link Window} that the given {@link View} is |
John Reck | 056e5dc | 2022-11-18 17:19:35 -0500 | [diff] [blame] | 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. |
John Reck | d688419 | 2022-11-30 14:08:58 -0500 | [diff] [blame] | 407 | * @return A {@link Builder} builder to set the optional params & build the request |
John Reck | 056e5dc | 2022-11-18 17:19:35 -0500 | [diff] [blame] | 408 | */ |
| 409 | @SuppressLint("BuilderSetStyle") |
| 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 | /** |
John Reck | d688419 | 2022-11-30 14:08:58 -0500 | [diff] [blame] | 430 | * Creates a PixelCopy Builder for the given {@link Surface} |
John Reck | 056e5dc | 2022-11-18 17:19:35 -0500 | [diff] [blame] | 431 | * |
| 432 | * @param source The Surface to copy from. Must be {@link Surface#isValid() valid}. |
John Reck | d688419 | 2022-11-30 14:08:58 -0500 | [diff] [blame] | 433 | * @return A {@link Builder} builder to set the optional params & build the request |
John Reck | 056e5dc | 2022-11-18 17:19:35 -0500 | [diff] [blame] | 434 | */ |
| 435 | @SuppressLint("BuilderSetStyle") |
| 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 | /** |
John Reck | d688419 | 2022-11-30 14:08:58 -0500 | [diff] [blame] | 444 | * Creates a PixelCopy Builder for the {@link Surface} belonging to the |
John Reck | 056e5dc | 2022-11-18 17:19:35 -0500 | [diff] [blame] | 445 | * given {@link SurfaceView} |
| 446 | * |
| 447 | * @param source The SurfaceView to copy from. The backing surface must be |
| 448 | * {@link Surface#isValid() valid} |
John Reck | d688419 | 2022-11-30 14:08:58 -0500 | [diff] [blame] | 449 | * @return A {@link Builder} builder to set the optional params & build the request |
John Reck | 056e5dc | 2022-11-18 17:19:35 -0500 | [diff] [blame] | 450 | */ |
| 451 | @SuppressLint("BuilderSetStyle") |
| 452 | public static @NonNull Builder ofSurface(@NonNull SurfaceView source) { |
| 453 | return ofSurface(source.getHolder().getSurface()); |
| 454 | } |
| 455 | |
John Reck | 03256dd | 2022-10-27 15:32:51 -0400 | [diff] [blame] | 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 | */ |
| 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 | */ |
| 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 | */ |
| 502 | public @NonNull Request build() { |
| 503 | requireNotBuilt(); |
| 504 | Request ret = mRequest; |
| 505 | mRequest = null; |
| 506 | return ret; |
| 507 | } |
John Reck | 4d73cb1 | 2022-07-27 10:32:52 -0400 | [diff] [blame] | 508 | } |
| 509 | |
| 510 | /** |
John Reck | 03256dd | 2022-10-27 15:32:51 -0400 | [diff] [blame] | 511 | * @return The destination bitmap as set by {@link Builder#setDestinationBitmap(Bitmap)} |
| 512 | */ |
| 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 | */ |
| 520 | public @Nullable Rect getSourceRect() { |
| 521 | return mSrcRect; |
| 522 | } |
| 523 | |
| 524 | /** |
| 525 | * @hide |
John Reck | 4d73cb1 | 2022-07-27 10:32:52 -0400 | [diff] [blame] | 526 | */ |
John Reck | 056e5dc | 2022-11-18 17:19:35 -0500 | [diff] [blame] | 527 | public void request(@NonNull Executor callbackExecutor, |
| 528 | @NonNull Consumer<Result> listener) { |
John Reck | 4d73cb1 | 2022-07-27 10:32:52 -0400 | [diff] [blame] | 529 | if (!mSource.isValid()) { |
John Reck | 056e5dc | 2022-11-18 17:19:35 -0500 | [diff] [blame] | 530 | callbackExecutor.execute(() -> listener.accept( |
John Reck | 03256dd | 2022-10-27 15:32:51 -0400 | [diff] [blame] | 531 | new Result(ERROR_SOURCE_INVALID, null))); |
John Reck | 4d73cb1 | 2022-07-27 10:32:52 -0400 | [diff] [blame] | 532 | return; |
| 533 | } |
| 534 | HardwareRenderer.copySurfaceInto(mSource, new HardwareRenderer.CopyRequest( |
| 535 | adjustSourceRectForInsets(mSourceInsets, mSrcRect), mDest) { |
| 536 | @Override |
| 537 | public void onCopyFinished(int result) { |
John Reck | 056e5dc | 2022-11-18 17:19:35 -0500 | [diff] [blame] | 538 | callbackExecutor.execute(() -> listener.accept( |
John Reck | 03256dd | 2022-10-27 15:32:51 -0400 | [diff] [blame] | 539 | new Result(result, mDestinationBitmap))); |
John Reck | 4d73cb1 | 2022-07-27 10:32:52 -0400 | [diff] [blame] | 540 | } |
| 541 | }); |
| 542 | } |
| 543 | } |
| 544 | |
| 545 | /** |
John Reck | 03256dd | 2022-10-27 15:32:51 -0400 | [diff] [blame] | 546 | * Executes the pixel copy request |
| 547 | * @param request The request to execute |
John Reck | 056e5dc | 2022-11-18 17:19:35 -0500 | [diff] [blame] | 548 | * @param callbackExecutor The executor to run the callback on |
| 549 | * @param listener The callback for when the copy request is completed |
John Reck | 4d73cb1 | 2022-07-27 10:32:52 -0400 | [diff] [blame] | 550 | */ |
John Reck | 056e5dc | 2022-11-18 17:19:35 -0500 | [diff] [blame] | 551 | public static void request(@NonNull Request request, @NonNull Executor callbackExecutor, |
| 552 | @NonNull Consumer<Result> listener) { |
| 553 | request.request(callbackExecutor, listener); |
John Reck | 4d73cb1 | 2022-07-27 10:32:52 -0400 | [diff] [blame] | 554 | } |
| 555 | |
John Reck | e94cbc7 | 2016-04-25 13:03:44 -0700 | [diff] [blame] | 556 | private PixelCopy() {} |
| 557 | } |