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

Commit 73c0925b authored by Pierre Barbier de Reuille's avatar Pierre Barbier de Reuille Committed by Automerger Merge Worker
Browse files

Merge "Correcting the image area computation." into sc-dev am: 3ca312a2

Original change: https://googleplex-android-review.googlesource.com/c/platform/frameworks/base/+/14250533

Change-Id: Iee4e896348a0bb7c34e551f415c98373d5230b2c
parents cb539af7 3ca312a2
Loading
Loading
Loading
Loading
+44 −32
Original line number Original line Diff line number Diff line
@@ -30,6 +30,7 @@ import android.util.Log;
import android.util.MathUtils;
import android.util.MathUtils;
import android.util.Size;
import android.util.Size;
import android.view.Choreographer;
import android.view.Choreographer;
import android.view.DisplayInfo;
import android.view.SurfaceHolder;
import android.view.SurfaceHolder;
import android.view.WindowManager;
import android.view.WindowManager;


@@ -62,8 +63,7 @@ public class ImageWallpaper extends WallpaperService {
    private final StatusBarStateController mStatusBarStateController;
    private final StatusBarStateController mStatusBarStateController;
    private final ArrayList<RectF> mLocalColorsToAdd = new ArrayList<>();
    private final ArrayList<RectF> mLocalColorsToAdd = new ArrayList<>();
    private final ArraySet<RectF> mColorAreas = new ArraySet<>();
    private final ArraySet<RectF> mColorAreas = new ArraySet<>();
    private float mShift;
    private volatile int mPages = 1;
    private volatile int mPages;
    private HandlerThread mWorker;
    private HandlerThread mWorker;
    // scaled down version
    // scaled down version
    private Bitmap mMiniBitmap;
    private Bitmap mMiniBitmap;
@@ -100,9 +100,9 @@ public class ImageWallpaper extends WallpaperService {
        // Surface is rejected if size below a threshold on some devices (ie. 8px on elfin)
        // Surface is rejected if size below a threshold on some devices (ie. 8px on elfin)
        // set min to 64 px (CTS covers this), please refer to ag/4867989 for detail.
        // set min to 64 px (CTS covers this), please refer to ag/4867989 for detail.
        @VisibleForTesting
        @VisibleForTesting
        static final int MIN_SURFACE_WIDTH = 64;
        static final int MIN_SURFACE_WIDTH = 128;
        @VisibleForTesting
        @VisibleForTesting
        static final int MIN_SURFACE_HEIGHT = 64;
        static final int MIN_SURFACE_HEIGHT = 128;


        private ImageWallpaperRenderer mRenderer;
        private ImageWallpaperRenderer mRenderer;
        private EglHelper mEglHelper;
        private EglHelper mEglHelper;
@@ -112,6 +112,8 @@ public class ImageWallpaper extends WallpaperService {
        private int mHeight = 1;
        private int mHeight = 1;
        private int mImgWidth = 1;
        private int mImgWidth = 1;
        private int mImgHeight = 1;
        private int mImgHeight = 1;
        private float mPageWidth = 1.f;
        private float mPageOffset = 1.f;
        private volatile float mDozeAmount;
        private volatile float mDozeAmount;
        private volatile boolean mNewDozeValue = false;
        private volatile boolean mNewDozeValue = false;
        private volatile boolean mShouldScheduleFrame = false;
        private volatile boolean mShouldScheduleFrame = false;
@@ -158,15 +160,15 @@ public class ImageWallpaper extends WallpaperService {
        public void onOffsetsChanged(float xOffset, float yOffset,
        public void onOffsetsChanged(float xOffset, float yOffset,
                float xOffsetStep, float yOffsetStep,
                float xOffsetStep, float yOffsetStep,
                int xPixelOffset, int yPixelOffset) {
                int xPixelOffset, int yPixelOffset) {
            if (mMiniBitmap == null || mMiniBitmap.isRecycled()) return;
            final int pages;
            final int pages;
            if (xOffsetStep > 0 && xOffsetStep <= 1) {
            if (xOffsetStep > 0 && xOffsetStep <= 1) {
                pages = (int) (1 / xOffsetStep + 1);
                pages = (int) Math.round(1 / xOffsetStep) + 1;
            } else {
            } else {
                pages = 1;
                pages = 1;
            }
            }
            if (pages == mPages) return;
            if (pages == mPages) return;
            mPages = pages;
            mPages = pages;
            if (mMiniBitmap == null || mMiniBitmap.isRecycled()) return;
            updateShift();
            updateShift();
            mWorker.getThreadHandler().post(() ->
            mWorker.getThreadHandler().post(() ->
                    computeAndNotifyLocalColors(new ArrayList<>(mColorAreas), mMiniBitmap));
                    computeAndNotifyLocalColors(new ArrayList<>(mColorAreas), mMiniBitmap));
@@ -174,19 +176,17 @@ public class ImageWallpaper extends WallpaperService {


        private void updateShift() {
        private void updateShift() {
            if (mImgHeight == 0) {
            if (mImgHeight == 0) {
                mShift = 0;
                mPageOffset = 0;
                mPageWidth = 1;
                return;
                return;
            }
            }
            // calculate shift
            // calculate shift
            float imgWidth = (float) mImgWidth / (float) mImgHeight;
            DisplayInfo displayInfo = new DisplayInfo();
            float displayWidth =
            getDisplayContext().getDisplay().getDisplayInfo(displayInfo);
                    (float) mWidth / (float) mHeight;
            int screenWidth = displayInfo.getNaturalWidth();
            // if need to shift
            float imgWidth = Math.min(mImgWidth > 0 ? screenWidth / (float) mImgWidth : 1.f, 1.f);
            if (imgWidth > displayWidth) {
            mPageWidth = imgWidth;
                mShift = imgWidth / imgWidth - displayWidth / imgWidth;
            mPageOffset = (1 - imgWidth) / (float) (mPages - 1);
            } else {
                mShift = 0;
            }
        }
        }


        private void updateMiniBitmap() {
        private void updateMiniBitmap() {
@@ -198,8 +198,8 @@ public class ImageWallpaper extends WallpaperService {
                }
                }
                mImgHeight = b.getHeight();
                mImgHeight = b.getHeight();
                mImgWidth = b.getWidth();
                mImgWidth = b.getWidth();
                mMiniBitmap = Bitmap.createScaledBitmap(b, (int) Math.ceil(scale * b.getWidth()),
                mMiniBitmap = Bitmap.createScaledBitmap(b,  (int) Math.max(scale * b.getWidth(), 1),
                        (int) Math.ceil(scale * b.getHeight()), false);
                        (int) Math.max(scale * b.getHeight(), 1), false);
                computeAndNotifyLocalColors(mLocalColorsToAdd, mMiniBitmap);
                computeAndNotifyLocalColors(mLocalColorsToAdd, mMiniBitmap);
                mLocalColorsToAdd.clear();
                mLocalColorsToAdd.clear();
            });
            });
@@ -274,27 +274,39 @@ public class ImageWallpaper extends WallpaperService {
            });
            });
        }
        }


        /**
         * Transform the logical coordinates into wallpaper coordinates.
         *
         * Logical coordinates are organised such that the various pages are non-overlapping. So,
         * if there are n pages, the first page will have its X coordinate on the range [0-1/n].
         *
         * The real pages are overlapping. If the Wallpaper are a width Ww and the screen a width
         * Ws, the relative width of a page Wr is Ws/Ww. This does not change if the number of
         * pages increase.
         * If there are n pages, the page k starts at the offset k * (1 - Wr) / (n - 1), as the
         * last page is at position (1-Wr) and the others are regularly spread on the range [0-
         * (1-Wr)].
         */
        private RectF pageToImgRect(RectF area) {
        private RectF pageToImgRect(RectF area) {
            float pageWidth = 1f / (float) mPages;
            // Width of a page for the caller of this API.
            if (pageWidth < 1 && pageWidth >= 0) pageWidth = 1;
            float virtualPageWidth = 1f / (float) mPages;
            float imgWidth = (float) mImgWidth / (float) mImgHeight;
            float leftPosOnPage = (area.left % virtualPageWidth) / virtualPageWidth;
            float displayWidth =
            float rightPosOnPage = (area.right % virtualPageWidth) / virtualPageWidth;
                    (float) mWidth / (float) mHeight;
            int currentPage = (int) Math.floor(area.centerX() / virtualPageWidth);
            float expansion = imgWidth > displayWidth ? displayWidth / imgWidth : 1;

            int page = (int) Math.floor(area.centerX() / pageWidth);
            float shiftWidth = mShift * page * pageWidth;
            RectF imgArea = new RectF();
            RectF imgArea = new RectF();
            imgArea.bottom = area.bottom;
            imgArea.bottom = area.bottom;
            imgArea.top = area.top;
            imgArea.top = area.top;
            imgArea.left = MathUtils.constrain(area.left % pageWidth, 0, 1)
            imgArea.left = MathUtils.constrain(
                    * expansion + shiftWidth;
                    leftPosOnPage * mPageWidth + currentPage * mPageOffset, 0, 1);
            imgArea.right = MathUtils.constrain(area.right % pageWidth, 0, 1)
            imgArea.right = MathUtils.constrain(
                    * expansion + shiftWidth;
                    rightPosOnPage * mPageWidth + currentPage * mPageOffset, 0, 1);
            if (imgArea.left > imgArea.right) {
            if (imgArea.left > imgArea.right) {
                // take full page
                // take full page
                imgArea.left = shiftWidth;
                imgArea.left = 0;
                imgArea.right = 1 - (mShift - shiftWidth);
                imgArea.right = 1;
            }
            }

            return imgArea;
            return imgArea;
        }
        }