From 9ec6c84f208ab5d9d5419d99891bea2379d63b4e Mon Sep 17 00:00:00 2001 From: Jay Aliomer Date: Tue, 30 Nov 2021 14:01:02 -0500 Subject: [PATCH 1/2] Update wallpaper window pages only at processing Fixes: 207597806 Test: manually create pages Change-Id: I8138d42d6a2066531ec332e3a510edbd95151699 --- .../service/wallpaper/WallpaperService.java | 134 ++---------------- 1 file changed, 15 insertions(+), 119 deletions(-) diff --git a/core/java/android/service/wallpaper/WallpaperService.java b/core/java/android/service/wallpaper/WallpaperService.java index 7b8410bba6ec0..b68717a9b4576 100644 --- a/core/java/android/service/wallpaper/WallpaperService.java +++ b/core/java/android/service/wallpaper/WallpaperService.java @@ -196,7 +196,7 @@ public abstract class WallpaperService extends Service { EngineWindowPage[] mWindowPages = new EngineWindowPage[1]; Bitmap mLastScreenshot; int mLastWindowPage = -1; - float mLastPageOffset = 0; + private boolean mResetWindowPages; // Copies from mIWallpaperEngine. HandlerCaller mCaller; @@ -787,7 +787,7 @@ public abstract class WallpaperService extends Service { Log.w(TAG, "Can't notify system because wallpaper connection " + "was not established."); } - resetWindowPages(); + mResetWindowPages = true; processLocalColors(mPendingXOffset, mPendingXOffsetStep); } catch (RemoteException e) { Log.w(TAG, "Can't notify system because wallpaper connection was lost.", e); @@ -1515,6 +1515,7 @@ public abstract class WallpaperService extends Service { float finalXOffsetStep = xOffsetStep; float finalXOffset = xOffset; mHandler.post(() -> { + resetWindowPages(); int xPage = xCurrentPage; EngineWindowPage current; if (mWindowPages.length == 0 || (mWindowPages.length != xPages)) { @@ -1699,15 +1700,15 @@ public abstract class WallpaperService extends Service { private void resetWindowPages() { if (supportsLocalColorExtraction()) return; + if (!mResetWindowPages) return; + mResetWindowPages = false; mLastWindowPage = -1; - mHandler.post(() -> { - for (int i = 0; i < mWindowPages.length; i++) { - EngineWindowPage page = mWindowPages[i]; - if (page != null) { - page.setLastUpdateTime(0L); - } + for (int i = 0; i < mWindowPages.length; i++) { + EngineWindowPage page = mWindowPages[i]; + if (page != null) { + page.setLastUpdateTime(0L); } - }); + } } private int getRectFPage(RectF area, float step) { @@ -1730,40 +1731,8 @@ public abstract class WallpaperService extends Service { if (DEBUG) { Log.d(TAG, "addLocalColorsAreas adding local color areas " + regions); } - - List colors = getLocalWallpaperColors(regions); mHandler.post(() -> { - float step = mPendingXOffsetStep; - if (!validStep(step)) { - step = 0; - } - for (int i = 0; i < regions.size(); i++) { - RectF area = regions.get(i); - if (!isValid(area)) continue; - int pageInx = getRectFPage(area, step); - // no page should be null - EngineWindowPage page = mWindowPages[pageInx]; - - if (page != null) { - mLocalColorAreas.add(area); - page.addArea(area); - WallpaperColors color = colors.get(i); - if (color != null && !color.equals(page.getColors(area))) { - page.addWallpaperColors(area, color); - } - } else { - mLocalColorsToAdd.add(area); - } - } - for (int i = 0; i < colors.size() && colors.get(i) != null; i++) { - try { - mConnection.onLocalWallpaperColorsChanged(regions.get(i), colors.get(i), - mDisplayContext.getDisplayId()); - } catch (RemoteException e) { - Log.e(TAG, "Error calling Connection.onLocalWallpaperColorsChanged", e); - return; - } - } + mLocalColorsToAdd.addAll(regions); processLocalColors(mPendingXOffset, mPendingYOffset); }); @@ -1784,88 +1753,15 @@ public abstract class WallpaperService extends Service { if (!validStep(step)) { return; } - for (int i = 0; i < regions.size(); i++) { - RectF area = regions.get(i); - if (!isValid(area)) continue; - int pageInx = getRectFPage(area, step); - // no page should be null - EngineWindowPage page = mWindowPages[pageInx]; - if (page != null) { - page.removeArea(area); + for (int i = 0; i < mWindowPages.length; i++) { + for (int j = 0; j < regions.size(); j++) { + EngineWindowPage page = mWindowPages[i]; + if (page != null) page.removeArea(regions.get(j)); } } }); } - private @NonNull List getLocalWallpaperColors(@NonNull List areas) { - ArrayList colors = new ArrayList<>(areas.size()); - float step = mPendingXOffsetStep; - if (!validStep(step)) { - if (DEBUG) Log.d(TAG, "invalid step size " + step); - step = 1.0f; - } - for (int i = 0; i < areas.size(); i++) { - RectF currentArea = areas.get(i); - if (currentArea == null || !isValid(currentArea)) { - Log.wtf(TAG, "invalid local area " + currentArea); - continue; - } - EngineWindowPage page; - RectF area; - int pageIndx; - synchronized (mLock) { - pageIndx = getRectFPage(currentArea, step); - if (mWindowPages.length == 0 || pageIndx < 0 - || pageIndx > mWindowPages.length || !isValid(currentArea)) { - colors.add(null); - continue; - } - area = generateSubRect(currentArea, pageIndx, mWindowPages.length); - page = mWindowPages[pageIndx]; - } - if (page == null) { - colors.add(null); - continue; - } - float finalStep = step; - int finalPageIndx = pageIndx; - Bitmap screenShot = page.getBitmap(); - if (screenShot == null) screenShot = mLastScreenshot; - if (screenShot == null || screenShot.isRecycled()) { - if (DEBUG) { - Log.d(TAG, "invalid bitmap " + screenShot - + " for page " + finalPageIndx); - } - page.setLastUpdateTime(0); - colors.add(null); - continue; - } - Bitmap b = screenShot; - Rect subImage = new Rect( - Math.round(area.left * b.getWidth() / finalStep), - Math.round(area.top * b.getHeight()), - Math.round(area.right * b.getWidth() / finalStep), - Math.round(area.bottom * b.getHeight()) - ); - subImage = fixRect(b, subImage); - if (DEBUG) { - Log.d(TAG, "getting subbitmap of " + subImage.toString() - + " for RectF " + area.toString() - + " screenshot width " + screenShot.getWidth() + " height " - + screenShot.getHeight()); - } - Bitmap colorImg = Bitmap.createBitmap(screenShot, - subImage.left, subImage.top, subImage.width(), subImage.height()); - if (DEBUG) { - Log.d(TAG, "created bitmap " + colorImg.getWidth() + ", " - + colorImg.getHeight()); - } - WallpaperColors color = WallpaperColors.fromBitmap(colorImg); - colors.add(color); - } - return colors; - } - // fix the rect to be included within the bounds of the bitmap private Rect fixRect(Bitmap b, Rect r) { r.left = r.left >= r.right || r.left >= b.getWidth() || r.left > 0 From 53ca29c94f935dbaf5910f102bae428b510fe3ad Mon Sep 17 00:00:00 2001 From: Jay Aliomer Date: Tue, 30 Nov 2021 22:59:40 -0500 Subject: [PATCH 2/2] Eliminate Null checks for local color pages Test: manual Change-Id: Ica07166ffa9f3966cda57b27ff153eb74d4d232d --- .../service/wallpaper/WallpaperService.java | 29 ++++--------------- 1 file changed, 5 insertions(+), 24 deletions(-) diff --git a/core/java/android/service/wallpaper/WallpaperService.java b/core/java/android/service/wallpaper/WallpaperService.java index b68717a9b4576..b862ec15f3d98 100644 --- a/core/java/android/service/wallpaper/WallpaperService.java +++ b/core/java/android/service/wallpaper/WallpaperService.java @@ -193,7 +193,7 @@ public abstract class WallpaperService extends Service { final ArraySet mLocalColorsToAdd = new ArraySet<>(4); // 2D matrix [x][y] to represent a page of a portion of a window - EngineWindowPage[] mWindowPages = new EngineWindowPage[1]; + EngineWindowPage[] mWindowPages = new EngineWindowPage[0]; Bitmap mLastScreenshot; int mLastWindowPage = -1; private boolean mResetWindowPages; @@ -1528,14 +1528,8 @@ public abstract class WallpaperService extends Service { mLocalColorAreas.add(colorArea); int colorPage = getRectFPage(colorArea, finalXOffsetStep); EngineWindowPage currentPage = mWindowPages[colorPage]; - if (currentPage == null) { - currentPage = new EngineWindowPage(); - currentPage.addArea(colorArea); - mWindowPages[colorPage] = currentPage; - } else { - currentPage.setLastUpdateTime(0); - currentPage.removeColor(colorArea); - } + currentPage.setLastUpdateTime(0); + currentPage.removeColor(colorArea); } mLocalColorsToAdd.clear(); } @@ -1550,15 +1544,6 @@ public abstract class WallpaperService extends Service { xPage = mWindowPages.length - 1; } current = mWindowPages[xPage]; - if (current == null) { - if (DEBUG) Log.d(TAG, "making page " + xPage + " out of " + xPages); - if (DEBUG) { - Log.d(TAG, "xOffsetStep " + finalXOffsetStep + " xOffset " - + finalXOffset); - } - current = new EngineWindowPage(); - mWindowPages[xPage] = current; - } updatePage(current, xPage, xPages, finalXOffsetStep); }); } @@ -1704,10 +1689,7 @@ public abstract class WallpaperService extends Service { mResetWindowPages = false; mLastWindowPage = -1; for (int i = 0; i < mWindowPages.length; i++) { - EngineWindowPage page = mWindowPages[i]; - if (page != null) { - page.setLastUpdateTime(0L); - } + mWindowPages[i].setLastUpdateTime(0L); } } @@ -1755,8 +1737,7 @@ public abstract class WallpaperService extends Service { } for (int i = 0; i < mWindowPages.length; i++) { for (int j = 0; j < regions.size(); j++) { - EngineWindowPage page = mWindowPages[i]; - if (page != null) page.removeArea(regions.get(j)); + mWindowPages[i].removeArea(regions.get(j)); } } });