Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit ce2a426a authored by John Reck's avatar John Reck Committed by Android (Google) Code Review
Browse files

Merge "Allow PixelCopy for a window from any View"

parents b82bd9b2 4d73cb10
Loading
Loading
Loading
Loading
+15 −0
Original line number Original line Diff line number Diff line
@@ -49229,6 +49229,10 @@ package android.view {
  }
  }
  public final class PixelCopy {
  public final class PixelCopy {
    method @NonNull public static android.view.PixelCopy.Request ofSurface(@NonNull android.view.Surface, @NonNull java.util.concurrent.Executor, @NonNull java.util.function.Consumer<android.view.PixelCopy.CopyResult>);
    method @NonNull public static android.view.PixelCopy.Request ofSurface(@NonNull android.view.SurfaceView, @NonNull java.util.concurrent.Executor, @NonNull java.util.function.Consumer<android.view.PixelCopy.CopyResult>);
    method @NonNull public static android.view.PixelCopy.Request ofWindow(@NonNull android.view.Window, @NonNull java.util.concurrent.Executor, @NonNull java.util.function.Consumer<android.view.PixelCopy.CopyResult>);
    method @NonNull public static android.view.PixelCopy.Request ofWindow(@NonNull android.view.View, @NonNull java.util.concurrent.Executor, @NonNull java.util.function.Consumer<android.view.PixelCopy.CopyResult>);
    method public static void request(@NonNull android.view.SurfaceView, @NonNull android.graphics.Bitmap, @NonNull android.view.PixelCopy.OnPixelCopyFinishedListener, @NonNull android.os.Handler);
    method public static void request(@NonNull android.view.SurfaceView, @NonNull android.graphics.Bitmap, @NonNull android.view.PixelCopy.OnPixelCopyFinishedListener, @NonNull android.os.Handler);
    method public static void request(@NonNull android.view.SurfaceView, @Nullable android.graphics.Rect, @NonNull android.graphics.Bitmap, @NonNull android.view.PixelCopy.OnPixelCopyFinishedListener, @NonNull android.os.Handler);
    method public static void request(@NonNull android.view.SurfaceView, @Nullable android.graphics.Rect, @NonNull android.graphics.Bitmap, @NonNull android.view.PixelCopy.OnPixelCopyFinishedListener, @NonNull android.os.Handler);
    method public static void request(@NonNull android.view.Surface, @NonNull android.graphics.Bitmap, @NonNull android.view.PixelCopy.OnPixelCopyFinishedListener, @NonNull android.os.Handler);
    method public static void request(@NonNull android.view.Surface, @NonNull android.graphics.Bitmap, @NonNull android.view.PixelCopy.OnPixelCopyFinishedListener, @NonNull android.os.Handler);
@@ -49243,10 +49247,21 @@ package android.view {
    field public static final int SUCCESS = 0; // 0x0
    field public static final int SUCCESS = 0; // 0x0
  }
  }
  public static final class PixelCopy.CopyResult {
    method @NonNull public android.graphics.Bitmap getBitmap();
    method public int getStatus();
  }
  public static interface PixelCopy.OnPixelCopyFinishedListener {
  public static interface PixelCopy.OnPixelCopyFinishedListener {
    method public void onPixelCopyFinished(int);
    method public void onPixelCopyFinished(int);
  }
  }
  public static final class PixelCopy.Request {
    method public void request();
    method @NonNull public android.view.PixelCopy.Request setDestinationBitmap(@Nullable android.graphics.Bitmap);
    method @NonNull public android.view.PixelCopy.Request setSourceRect(@Nullable android.graphics.Rect);
  }
  public final class PointerIcon implements android.os.Parcelable {
  public final class PointerIcon implements android.os.Parcelable {
    method @NonNull public static android.view.PointerIcon create(@NonNull android.graphics.Bitmap, float, float);
    method @NonNull public static android.view.PointerIcon create(@NonNull android.graphics.Bitmap, float, float);
    method public int describeContents();
    method public int describeContents();
+34 −9
Original line number Original line Diff line number Diff line
@@ -1046,16 +1046,41 @@ public class HardwareRenderer {
    }
    }


    /** @hide */
    /** @hide */
    public static int copySurfaceInto(Surface surface, Rect srcRect, Bitmap bitmap) {
    public abstract static class CopyRequest {
        if (srcRect == null) {
        protected Bitmap mDestinationBitmap;
            // Empty rect means entire surface
        final Rect mSrcRect;
            return nCopySurfaceInto(surface, 0, 0, 0, 0, bitmap.getNativeInstance());

        protected CopyRequest(Rect srcRect, Bitmap destinationBitmap) {
            mDestinationBitmap = destinationBitmap;
            if (srcRect != null) {
                mSrcRect = srcRect;
            } else {
            } else {
            return nCopySurfaceInto(surface, srcRect.left, srcRect.top,
                mSrcRect = new Rect();
                    srcRect.right, srcRect.bottom, bitmap.getNativeInstance());
            }
            }
        }
        }


        /**
         * Retrieve the bitmap in which to store the result of the copy request
         */
        public long getDestinationBitmap(int srcWidth, int srcHeight) {
            if (mDestinationBitmap == null) {
                mDestinationBitmap =
                        Bitmap.createBitmap(srcWidth, srcHeight, Bitmap.Config.ARGB_8888);
            }
            return mDestinationBitmap.getNativeInstance();
        }

        /** Called when the copy is completed */
        public abstract void onCopyFinished(int result);
    }

    /** @hide */
    public static void copySurfaceInto(Surface surface, CopyRequest copyRequest) {
        final Rect srcRect = copyRequest.mSrcRect;
        nCopySurfaceInto(surface, srcRect.left, srcRect.top, srcRect.right, srcRect.bottom,
                copyRequest);
    }

    /**
    /**
     * Creates a {@link android.graphics.Bitmap.Config#HARDWARE} bitmap from the given
     * Creates a {@link android.graphics.Bitmap.Config#HARDWARE} bitmap from the given
     * RenderNode. Note that the RenderNode should be created as a root node (so x/y of 0,0), and
     * RenderNode. Note that the RenderNode should be created as a root node (so x/y of 0,0), and
@@ -1464,8 +1489,8 @@ public class HardwareRenderer {


    private static native void nRemoveObserver(long nativeProxy, long nativeObserver);
    private static native void nRemoveObserver(long nativeProxy, long nativeObserver);


    private static native int nCopySurfaceInto(Surface surface,
    private static native void nCopySurfaceInto(Surface surface,
            int srcLeft, int srcTop, int srcRight, int srcBottom, long bitmapHandle);
            int srcLeft, int srcTop, int srcRight, int srcBottom, CopyRequest request);


    private static native Bitmap nCreateHardwareBitmap(long renderNode, int width, int height);
    private static native Bitmap nCreateHardwareBitmap(long renderNode, int width, int height);


+231 −20
Original line number Original line Diff line number Diff line
@@ -20,12 +20,15 @@ import android.annotation.IntDef;
import android.annotation.NonNull;
import android.annotation.NonNull;
import android.annotation.Nullable;
import android.annotation.Nullable;
import android.graphics.Bitmap;
import android.graphics.Bitmap;
import android.graphics.HardwareRenderer;
import android.graphics.Rect;
import android.graphics.Rect;
import android.os.Handler;
import android.os.Handler;
import android.view.ViewTreeObserver.OnDrawListener;
import android.view.ViewTreeObserver.OnDrawListener;


import java.lang.annotation.Retention;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.RetentionPolicy;
import java.util.concurrent.Executor;
import java.util.function.Consumer;


/**
/**
 * Provides a mechanisms to issue pixel copy requests to allow for copy
 * Provides a mechanisms to issue pixel copy requests to allow for copy
@@ -183,12 +186,10 @@ public final class PixelCopy {
        if (srcRect != null && srcRect.isEmpty()) {
        if (srcRect != null && srcRect.isEmpty()) {
            throw new IllegalArgumentException("sourceRect is empty");
            throw new IllegalArgumentException("sourceRect is empty");
        }
        }
        // TODO: Make this actually async and fast and cool and stuff
        HardwareRenderer.copySurfaceInto(source, new HardwareRenderer.CopyRequest(srcRect, dest) {
        int result = ThreadedRenderer.copySurfaceInto(source, srcRect, dest);
        listenerThread.post(new Runnable() {
            @Override
            @Override
            public void run() {
            public void onCopyFinished(int result) {
                listener.onPixelCopyFinished(result);
                listenerThread.post(() -> listener.onPixelCopyFinished(result));
            }
            }
        });
        });
    }
    }
@@ -255,6 +256,26 @@ public final class PixelCopy {
            @NonNull Bitmap dest, @NonNull OnPixelCopyFinishedListener listener,
            @NonNull Bitmap dest, @NonNull OnPixelCopyFinishedListener listener,
            @NonNull Handler listenerThread) {
            @NonNull Handler listenerThread) {
        validateBitmapDest(dest);
        validateBitmapDest(dest);
        final Rect insets = new Rect();
        final Surface surface = sourceForWindow(source, insets);
        request(surface, adjustSourceRectForInsets(insets, srcRect), dest, listener,
                listenerThread);
    }

    private static void validateBitmapDest(Bitmap bitmap) {
        // TODO: Pre-check max texture dimens if we can
        if (bitmap == null) {
            throw new IllegalArgumentException("Bitmap cannot be null");
        }
        if (bitmap.isRecycled()) {
            throw new IllegalArgumentException("Bitmap is recycled");
        }
        if (!bitmap.isMutable()) {
            throw new IllegalArgumentException("Bitmap is immutable");
        }
    }

    private static Surface sourceForWindow(Window source, Rect outInsets) {
        if (source == null) {
        if (source == null) {
            throw new IllegalArgumentException("source is null");
            throw new IllegalArgumentException("source is null");
        }
        }
@@ -267,31 +288,221 @@ public final class PixelCopy {
        if (root != null) {
        if (root != null) {
            surface = root.mSurface;
            surface = root.mSurface;
            final Rect surfaceInsets = root.mWindowAttributes.surfaceInsets;
            final Rect surfaceInsets = root.mWindowAttributes.surfaceInsets;
            if (srcRect == null) {
            outInsets.set(surfaceInsets.left, surfaceInsets.top,
                srcRect = new Rect(surfaceInsets.left, surfaceInsets.top,
                    root.mWidth + surfaceInsets.left, root.mHeight + surfaceInsets.top);
                    root.mWidth + surfaceInsets.left, root.mHeight + surfaceInsets.top);
            } else {
                srcRect.offset(surfaceInsets.left, surfaceInsets.top);
            }
        }
        }
        if (surface == null || !surface.isValid()) {
        if (surface == null || !surface.isValid()) {
            throw new IllegalArgumentException(
            throw new IllegalArgumentException(
                    "Window doesn't have a backing surface!");
                    "Window doesn't have a backing surface!");
        }
        }
        request(surface, srcRect, dest, listener, listenerThread);
        return surface;
    }
    }


    private static void validateBitmapDest(Bitmap bitmap) {
    private static Rect adjustSourceRectForInsets(Rect insets, Rect srcRect) {
        // TODO: Pre-check max texture dimens if we can
        if (srcRect == null) {
        if (bitmap == null) {
            return insets;
            throw new IllegalArgumentException("Bitmap cannot be null");
        }
        }
        if (bitmap.isRecycled()) {
        if (insets != null) {
            throw new IllegalArgumentException("Bitmap is recycled");
            srcRect.offset(insets.left, insets.top);
        }
        return srcRect;
    }

    /**
     * Contains the result of a PixelCopy request
     */
    public static final class CopyResult {
        private int mStatus;
        private Bitmap mBitmap;

        private CopyResult(@CopyResultStatus int status, Bitmap bitmap) {
            mStatus = status;
            mBitmap = bitmap;
        }

        /**
         * Returns the {@link CopyResultStatus} of the copy request.
         */
        public @CopyResultStatus int getStatus() {
            return mStatus;
        }

        private void validateStatus() {
            if (mStatus != SUCCESS) {
                throw new IllegalStateException("Copy request didn't succeed, status = " + mStatus);
            }
            }
        if (!bitmap.isMutable()) {
            throw new IllegalArgumentException("Bitmap is immutable");
        }
        }

        /**
         * If the PixelCopy {@link Request} was given a destination bitmap with
         * {@link Request#setDestinationBitmap(Bitmap)} then the returned bitmap will be the same
         * as the one given. If no destination bitmap was provided, then this
         * will contain the automatically allocated Bitmap to hold the result.
         *
         * @return the Bitmap the copy request was stored in.
         * @throws IllegalStateException if {@link #getStatus()} is not SUCCESS
         */
        public @NonNull Bitmap getBitmap() {
            validateStatus();
            return mBitmap;
        }
    }

    /**
     * A builder to create the complete PixelCopy request, which is then executed by calling
     * {@link #request()}
     */
    public static final class Request {
        private Request(Surface source, Rect sourceInsets, Executor listenerThread,
                        Consumer<CopyResult> listener) {
            this.mSource = source;
            this.mSourceInsets = sourceInsets;
            this.mListenerThread = listenerThread;
            this.mListener = listener;
        }

        private final Surface mSource;
        private final Consumer<CopyResult> mListener;
        private final Executor mListenerThread;
        private final Rect mSourceInsets;
        private Rect mSrcRect;
        private Bitmap mDest;

        /**
         * Sets the region of the source to copy from. By default, the entire source is copied to
         * the output. If only a subset of the source is necessary to be copied, specifying a
         * srcRect will improve performance by reducing
         * the amount of data being copied.
         *
         * @param srcRect The area of the source to read from. Null or empty will be treated to
         *                mean the entire source
         * @return this
         */
        public @NonNull Request setSourceRect(@Nullable Rect srcRect) {
            this.mSrcRect = srcRect;
            return this;
        }

        /**
         * Specifies the output bitmap in which to store the result. By default, a Bitmap of format
         * {@link android.graphics.Bitmap.Config#ARGB_8888} with a width & height matching that
         * of the {@link #setSourceRect(Rect) source area} will be created to place the result.
         *
         * @param destination The bitmap to store the result, or null to have a bitmap
         *                    automatically created of the appropriate size. If not null, must not
         *                    be {@link Bitmap#isRecycled() recycled} and must be
         *                    {@link Bitmap#isMutable() mutable}.
         * @return this
         */
        public @NonNull Request setDestinationBitmap(@Nullable Bitmap destination) {
            if (destination != null) {
                validateBitmapDest(destination);
            }
            this.mDest = destination;
            return this;
        }

        /**
         * Executes the request.
         */
        public void request() {
            if (!mSource.isValid()) {
                mListenerThread.execute(() -> mListener.accept(
                        new CopyResult(ERROR_SOURCE_INVALID, null)));
                return;
            }
            HardwareRenderer.copySurfaceInto(mSource, new HardwareRenderer.CopyRequest(
                    adjustSourceRectForInsets(mSourceInsets, mSrcRect), mDest) {
                @Override
                public void onCopyFinished(int result) {
                    mListenerThread.execute(() -> mListener.accept(
                            new CopyResult(result, mDestinationBitmap)));
                }
            });
        }
    }

    /**
     * Creates a PixelCopy request for the given {@link Window}
     * @param source The Window to copy from
     * @param callbackExecutor The executor to run the callback on
     * @param listener The callback for when the copy request is completed
     * @return A {@link Request} builder to set the optional params & execute the request
     */
    public static @NonNull Request ofWindow(@NonNull Window source,
                                            @NonNull Executor callbackExecutor,
                                            @NonNull Consumer<CopyResult> listener) {
        final Rect insets = new Rect();
        final Surface surface = sourceForWindow(source, insets);
        return new Request(surface, insets, callbackExecutor, listener);
    }

    /**
     * Creates a PixelCopy request for the {@link Window} that the given {@link View} is
     * attached to.
     *
     * Note that this copy request is not cropped to the area the View occupies by default. If that
     * behavior is desired, use {@link View#getLocationInWindow(int[])} combined with
     * {@link Request#setSourceRect(Rect)} to set a crop area to restrict the copy operation.
     *
     * @param source A View that {@link View#isAttachedToWindow() is attached} to a window that
     *               will be used to retrieve the window to copy from.
     * @param callbackExecutor The executor to run the callback on
     * @param listener The callback for when the copy request is completed
     * @return A {@link Request} builder to set the optional params & execute the request
     */
    public static @NonNull Request ofWindow(@NonNull View source,
                                            @NonNull Executor callbackExecutor,
                                            @NonNull Consumer<CopyResult> listener) {
        if (source == null || !source.isAttachedToWindow()) {
            throw new IllegalArgumentException(
                    "View must not be null & must be attached to window");
        }
        final Rect insets = new Rect();
        Surface surface = null;
        final ViewRootImpl root = source.getViewRootImpl();
        if (root != null) {
            surface = root.mSurface;
            insets.set(root.mWindowAttributes.surfaceInsets);
        }
        if (surface == null || !surface.isValid()) {
            throw new IllegalArgumentException(
                    "Window doesn't have a backing surface!");
        }
        return new Request(surface, insets, callbackExecutor, listener);
    }

    /**
     * Creates a PixelCopy request for the given {@link Surface}
     *
     * @param source The Surface to copy from. Must be {@link Surface#isValid() valid}.
     * @param callbackExecutor The executor to run the callback on
     * @param listener The callback for when the copy request is completed
     * @return A {@link Request} builder to set the optional params & execute the request
     */
    public static @NonNull Request ofSurface(@NonNull Surface source,
                                             @NonNull Executor callbackExecutor,
                                             @NonNull Consumer<CopyResult> listener) {
        if (source == null || !source.isValid()) {
            throw new IllegalArgumentException("Source must not be null & must be valid");
        }
        return new Request(source, null, callbackExecutor, listener);
    }

    /**
     * Creates a PixelCopy request for the {@link Surface} belonging to the
     * given {@link SurfaceView}
     *
     * @param source The SurfaceView to copy from. The backing surface must be
     *               {@link Surface#isValid() valid}
     * @param callbackExecutor The executor to run the callback on
     * @param listener The callback for when the copy request is completed
     * @return A {@link Request} builder to set the optional params & execute the request
     */
    public static @NonNull Request ofSurface(@NonNull SurfaceView source,
                                             @NonNull Executor callbackExecutor,
                                             @NonNull Consumer<CopyResult> listener) {
        return ofSurface(source.getHolder().getSurface(), callbackExecutor, listener);
    }
    }


    private PixelCopy() {}
    private PixelCopy() {}
+42 −0
Original line number Original line Diff line number Diff line
/*
 * Copyright (C) 2022 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#pragma once

#include "Rect.h"
#include "hwui/Bitmap.h"

namespace android::uirenderer {

// Keep in sync with PixelCopy.java codes
enum class CopyResult {
    Success = 0,
    UnknownError = 1,
    Timeout = 2,
    SourceEmpty = 3,
    SourceInvalid = 4,
    DestinationInvalid = 5,
};

struct CopyRequest {
    Rect srcRect;
    CopyRequest(Rect srcRect) : srcRect(srcRect) {}
    virtual ~CopyRequest() {}
    virtual SkBitmap getDestinationBitmap(int srcWidth, int srcHeight) = 0;
    virtual void onCopyFinished(CopyResult result) = 0;
};

}  // namespace android::uirenderer
+22 −57

File changed.

Preview size limit exceeded, changes collapsed.

Loading