Ahan Wu | 67e7f10 | 2019-01-14 20:38:14 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2019 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 com.android.systemui.glwallpaper; |
| 18 | |
| 19 | import static android.opengl.GLES20.GL_COLOR_BUFFER_BIT; |
| 20 | import static android.opengl.GLES20.glClear; |
| 21 | import static android.opengl.GLES20.glClearColor; |
| 22 | import static android.opengl.GLES20.glUniform1f; |
| 23 | import static android.opengl.GLES20.glViewport; |
| 24 | |
| 25 | import android.app.WallpaperManager; |
| 26 | import android.content.Context; |
Ahan Wu | 0e17480 | 2019-07-23 20:41:52 +0800 | [diff] [blame] | 27 | import android.content.res.Configuration; |
Ahan Wu | 67e7f10 | 2019-01-14 20:38:14 +0800 | [diff] [blame] | 28 | import android.graphics.Bitmap; |
| 29 | import android.graphics.Rect; |
Ahan Wu | 67e7f10 | 2019-01-14 20:38:14 +0800 | [diff] [blame] | 30 | import android.util.Log; |
Ahan Wu | c836335 | 2019-03-07 17:35:27 +0800 | [diff] [blame] | 31 | import android.util.MathUtils; |
Ahan Wu | fa42c51 | 2019-05-15 19:52:51 +0800 | [diff] [blame] | 32 | import android.util.Size; |
Ahan Wu | e16e1fa | 2019-05-29 18:39:33 +0800 | [diff] [blame] | 33 | import android.view.DisplayInfo; |
Ahan Wu | 67e7f10 | 2019-01-14 20:38:14 +0800 | [diff] [blame] | 34 | |
Ahan Wu | 67e7f10 | 2019-01-14 20:38:14 +0800 | [diff] [blame] | 35 | import com.android.systemui.R; |
| 36 | |
Ahan Wu | 7688424 | 2019-05-22 20:04:23 +0800 | [diff] [blame] | 37 | import java.io.FileDescriptor; |
| 38 | import java.io.PrintWriter; |
Ahan Wu | 3222d3f | 2020-03-11 20:24:00 +0800 | [diff] [blame] | 39 | import java.util.concurrent.atomic.AtomicInteger; |
| 40 | import java.util.function.Consumer; |
Ahan Wu | 7688424 | 2019-05-22 20:04:23 +0800 | [diff] [blame] | 41 | |
Ahan Wu | 67e7f10 | 2019-01-14 20:38:14 +0800 | [diff] [blame] | 42 | /** |
| 43 | * A GL renderer for image wallpaper. |
| 44 | */ |
Ahan Wu | fa42c51 | 2019-05-15 19:52:51 +0800 | [diff] [blame] | 45 | public class ImageWallpaperRenderer implements GLWallpaperRenderer, |
| 46 | ImageRevealHelper.RevealStateListener { |
Ahan Wu | 67e7f10 | 2019-01-14 20:38:14 +0800 | [diff] [blame] | 47 | private static final String TAG = ImageWallpaperRenderer.class.getSimpleName(); |
Ahan Wu | e16e1fa | 2019-05-29 18:39:33 +0800 | [diff] [blame] | 48 | private static final float SCALE_VIEWPORT_MIN = 1f; |
| 49 | private static final float SCALE_VIEWPORT_MAX = 1.1f; |
Ahan Wu | e88b165 | 2019-09-19 22:12:46 +0800 | [diff] [blame] | 50 | private static final boolean DEBUG = true; |
Ahan Wu | 67e7f10 | 2019-01-14 20:38:14 +0800 | [diff] [blame] | 51 | |
Ahan Wu | 67e7f10 | 2019-01-14 20:38:14 +0800 | [diff] [blame] | 52 | private final ImageGLProgram mProgram; |
| 53 | private final ImageGLWallpaper mWallpaper; |
| 54 | private final ImageProcessHelper mImageProcessHelper; |
| 55 | private final ImageRevealHelper mImageRevealHelper; |
Ahan Wu | 67e7f10 | 2019-01-14 20:38:14 +0800 | [diff] [blame] | 56 | |
Ahan Wu | fa42c51 | 2019-05-15 19:52:51 +0800 | [diff] [blame] | 57 | private SurfaceProxy mProxy; |
Ahan Wu | e16e1fa | 2019-05-29 18:39:33 +0800 | [diff] [blame] | 58 | private final Rect mScissor; |
| 59 | private final Rect mSurfaceSize = new Rect(); |
| 60 | private final Rect mViewport = new Rect(); |
Ahan Wu | e16e1fa | 2019-05-29 18:39:33 +0800 | [diff] [blame] | 61 | private boolean mScissorMode; |
| 62 | private float mXOffset; |
| 63 | private float mYOffset; |
Ahan Wu | 3222d3f | 2020-03-11 20:24:00 +0800 | [diff] [blame] | 64 | private final WallpaperTexture mTexture; |
Ahan Wu | 48ebbd7 | 2019-03-12 20:59:13 +0800 | [diff] [blame] | 65 | |
Ahan Wu | fa42c51 | 2019-05-15 19:52:51 +0800 | [diff] [blame] | 66 | public ImageWallpaperRenderer(Context context, SurfaceProxy proxy) { |
Ahan Wu | 3222d3f | 2020-03-11 20:24:00 +0800 | [diff] [blame] | 67 | final WallpaperManager wpm = context.getSystemService(WallpaperManager.class); |
| 68 | if (wpm == null) { |
Ahan Wu | 67e7f10 | 2019-01-14 20:38:14 +0800 | [diff] [blame] | 69 | Log.w(TAG, "WallpaperManager not available"); |
| 70 | } |
| 71 | |
Ahan Wu | 3222d3f | 2020-03-11 20:24:00 +0800 | [diff] [blame] | 72 | mTexture = new WallpaperTexture(wpm); |
Ahan Wu | e16e1fa | 2019-05-29 18:39:33 +0800 | [diff] [blame] | 73 | DisplayInfo displayInfo = new DisplayInfo(); |
Ahan Wu | 4e40440 | 2020-01-27 20:39:57 +0800 | [diff] [blame] | 74 | context.getDisplay().getDisplayInfo(displayInfo); |
Ahan Wu | 0e17480 | 2019-07-23 20:41:52 +0800 | [diff] [blame] | 75 | |
| 76 | // We only do transition in portrait currently, b/137962047. |
| 77 | int orientation = context.getResources().getConfiguration().orientation; |
| 78 | if (orientation == Configuration.ORIENTATION_PORTRAIT) { |
| 79 | mScissor = new Rect(0, 0, displayInfo.logicalWidth, displayInfo.logicalHeight); |
| 80 | } else { |
| 81 | mScissor = new Rect(0, 0, displayInfo.logicalHeight, displayInfo.logicalWidth); |
| 82 | } |
Ahan Wu | e16e1fa | 2019-05-29 18:39:33 +0800 | [diff] [blame] | 83 | |
Ahan Wu | fa42c51 | 2019-05-15 19:52:51 +0800 | [diff] [blame] | 84 | mProxy = proxy; |
Ahan Wu | 67e7f10 | 2019-01-14 20:38:14 +0800 | [diff] [blame] | 85 | mProgram = new ImageGLProgram(context); |
| 86 | mWallpaper = new ImageGLWallpaper(mProgram); |
| 87 | mImageProcessHelper = new ImageProcessHelper(); |
| 88 | mImageRevealHelper = new ImageRevealHelper(this); |
Ahan Wu | 67e7f10 | 2019-01-14 20:38:14 +0800 | [diff] [blame] | 89 | |
Ahan Wu | 4e40440 | 2020-01-27 20:39:57 +0800 | [diff] [blame] | 90 | startProcessingImage(); |
| 91 | } |
| 92 | |
| 93 | protected void startProcessingImage() { |
Ahan Wu | 3222d3f | 2020-03-11 20:24:00 +0800 | [diff] [blame] | 94 | // Compute threshold of the image, this is an async work. |
| 95 | mImageProcessHelper.start(mTexture); |
Ahan Wu | 67e7f10 | 2019-01-14 20:38:14 +0800 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | @Override |
Ahan Wu | 287d828 | 2019-12-17 16:23:17 +0800 | [diff] [blame] | 99 | public boolean isWcgContent() { |
Ahan Wu | 3222d3f | 2020-03-11 20:24:00 +0800 | [diff] [blame] | 100 | return mTexture.isWcgContent(); |
Ahan Wu | 287d828 | 2019-12-17 16:23:17 +0800 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | @Override |
Ahan Wu | fa42c51 | 2019-05-15 19:52:51 +0800 | [diff] [blame] | 104 | public void onSurfaceCreated() { |
Ahan Wu | 67e7f10 | 2019-01-14 20:38:14 +0800 | [diff] [blame] | 105 | glClearColor(0f, 0f, 0f, 1.0f); |
| 106 | mProgram.useGLProgram( |
| 107 | R.raw.image_wallpaper_vertex_shader, R.raw.image_wallpaper_fragment_shader); |
Ahan Wu | fa42c51 | 2019-05-15 19:52:51 +0800 | [diff] [blame] | 108 | |
Ahan Wu | 3222d3f | 2020-03-11 20:24:00 +0800 | [diff] [blame] | 109 | mTexture.use(bitmap -> { |
| 110 | if (bitmap == null) { |
| 111 | Log.w(TAG, "reload texture failed!"); |
Ahan Wu | fa42c51 | 2019-05-15 19:52:51 +0800 | [diff] [blame] | 112 | } |
Ahan Wu | 3222d3f | 2020-03-11 20:24:00 +0800 | [diff] [blame] | 113 | mWallpaper.setup(bitmap); |
| 114 | }); |
Ahan Wu | 67e7f10 | 2019-01-14 20:38:14 +0800 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | @Override |
Ahan Wu | fa42c51 | 2019-05-15 19:52:51 +0800 | [diff] [blame] | 118 | public void onSurfaceChanged(int width, int height) { |
| 119 | glViewport(0, 0, width, height); |
| 120 | } |
| 121 | |
| 122 | @Override |
| 123 | public void onDrawFrame() { |
Ahan Wu | 330bc60 | 2019-04-25 15:04:39 +0800 | [diff] [blame] | 124 | float threshold = mImageProcessHelper.getThreshold(); |
Ahan Wu | 67e7f10 | 2019-01-14 20:38:14 +0800 | [diff] [blame] | 125 | float reveal = mImageRevealHelper.getReveal(); |
| 126 | |
Ahan Wu | 67e7f10 | 2019-01-14 20:38:14 +0800 | [diff] [blame] | 127 | glUniform1f(mWallpaper.getHandle(ImageGLWallpaper.U_AOD2OPACITY), 1); |
Ahan Wu | 330bc60 | 2019-04-25 15:04:39 +0800 | [diff] [blame] | 128 | glUniform1f(mWallpaper.getHandle(ImageGLWallpaper.U_PER85), threshold); |
Ahan Wu | 67e7f10 | 2019-01-14 20:38:14 +0800 | [diff] [blame] | 129 | glUniform1f(mWallpaper.getHandle(ImageGLWallpaper.U_REVEAL), reveal); |
| 130 | |
Ahan Wu | e16e1fa | 2019-05-29 18:39:33 +0800 | [diff] [blame] | 131 | glClear(GL_COLOR_BUFFER_BIT); |
| 132 | // We only need to scale viewport while doing transition. |
| 133 | if (mScissorMode) { |
| 134 | scaleViewport(reveal); |
| 135 | } else { |
| 136 | glViewport(0, 0, mSurfaceSize.width(), mSurfaceSize.height()); |
| 137 | } |
Ahan Wu | 67e7f10 | 2019-01-14 20:38:14 +0800 | [diff] [blame] | 138 | mWallpaper.useTexture(); |
| 139 | mWallpaper.draw(); |
| 140 | } |
| 141 | |
Ahan Wu | fa42c51 | 2019-05-15 19:52:51 +0800 | [diff] [blame] | 142 | @Override |
| 143 | public void updateAmbientMode(boolean inAmbientMode, long duration) { |
| 144 | mImageRevealHelper.updateAwake(!inAmbientMode, duration); |
| 145 | } |
| 146 | |
| 147 | @Override |
Ahan Wu | e16e1fa | 2019-05-29 18:39:33 +0800 | [diff] [blame] | 148 | public void updateOffsets(float xOffset, float yOffset) { |
| 149 | mXOffset = xOffset; |
| 150 | mYOffset = yOffset; |
| 151 | int left = (int) ((mSurfaceSize.width() - mScissor.width()) * xOffset); |
| 152 | int right = left + mScissor.width(); |
| 153 | mScissor.set(left, mScissor.top, right, mScissor.bottom); |
| 154 | } |
| 155 | |
| 156 | @Override |
Ahan Wu | fa42c51 | 2019-05-15 19:52:51 +0800 | [diff] [blame] | 157 | public Size reportSurfaceSize() { |
Ahan Wu | 3222d3f | 2020-03-11 20:24:00 +0800 | [diff] [blame] | 158 | mTexture.use(null); |
| 159 | mSurfaceSize.set(mTexture.getTextureDimensions()); |
Ahan Wu | fa42c51 | 2019-05-15 19:52:51 +0800 | [diff] [blame] | 160 | return new Size(mSurfaceSize.width(), mSurfaceSize.height()); |
| 161 | } |
| 162 | |
| 163 | @Override |
| 164 | public void finish() { |
| 165 | mProxy = null; |
| 166 | } |
| 167 | |
Ahan Wu | c836335 | 2019-03-07 17:35:27 +0800 | [diff] [blame] | 168 | private void scaleViewport(float reveal) { |
Ahan Wu | e16e1fa | 2019-05-29 18:39:33 +0800 | [diff] [blame] | 169 | int left = mScissor.left; |
| 170 | int top = mScissor.top; |
| 171 | int width = mScissor.width(); |
| 172 | int height = mScissor.height(); |
Ahan Wu | c836335 | 2019-03-07 17:35:27 +0800 | [diff] [blame] | 173 | // Interpolation between SCALE_VIEWPORT_MAX and SCALE_VIEWPORT_MIN by reveal. |
Ahan Wu | e16e1fa | 2019-05-29 18:39:33 +0800 | [diff] [blame] | 174 | float vpScaled = MathUtils.lerp(SCALE_VIEWPORT_MIN, SCALE_VIEWPORT_MAX, reveal); |
Ahan Wu | c836335 | 2019-03-07 17:35:27 +0800 | [diff] [blame] | 175 | // Calculate the offset amount from the lower left corner. |
Ahan Wu | e16e1fa | 2019-05-29 18:39:33 +0800 | [diff] [blame] | 176 | float offset = (SCALE_VIEWPORT_MIN - vpScaled) / 2; |
Ahan Wu | c836335 | 2019-03-07 17:35:27 +0800 | [diff] [blame] | 177 | // Change the viewport. |
Ahan Wu | e16e1fa | 2019-05-29 18:39:33 +0800 | [diff] [blame] | 178 | mViewport.set((int) (left + width * offset), (int) (top + height * offset), |
Ahan Wu | fa42c51 | 2019-05-15 19:52:51 +0800 | [diff] [blame] | 179 | (int) (width * vpScaled), (int) (height * vpScaled)); |
Ahan Wu | e16e1fa | 2019-05-29 18:39:33 +0800 | [diff] [blame] | 180 | glViewport(mViewport.left, mViewport.top, mViewport.right, mViewport.bottom); |
Ahan Wu | 67e7f10 | 2019-01-14 20:38:14 +0800 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | @Override |
| 184 | public void onRevealStateChanged() { |
Ahan Wu | fa42c51 | 2019-05-15 19:52:51 +0800 | [diff] [blame] | 185 | mProxy.requestRender(); |
Ahan Wu | 67e7f10 | 2019-01-14 20:38:14 +0800 | [diff] [blame] | 186 | } |
| 187 | |
Ahan Wu | fa42c51 | 2019-05-15 19:52:51 +0800 | [diff] [blame] | 188 | @Override |
Ahan Wu | 0e17480 | 2019-07-23 20:41:52 +0800 | [diff] [blame] | 189 | public void onRevealStart(boolean animate) { |
| 190 | if (animate) { |
| 191 | mScissorMode = true; |
| 192 | // Use current display area of texture. |
| 193 | mWallpaper.adjustTextureCoordinates(mSurfaceSize, mScissor, mXOffset, mYOffset); |
| 194 | } |
Ahan Wu | fa42c51 | 2019-05-15 19:52:51 +0800 | [diff] [blame] | 195 | mProxy.preRender(); |
| 196 | } |
| 197 | |
| 198 | @Override |
| 199 | public void onRevealEnd() { |
Ahan Wu | 0e17480 | 2019-07-23 20:41:52 +0800 | [diff] [blame] | 200 | if (mScissorMode) { |
| 201 | mScissorMode = false; |
| 202 | // reset texture coordinates to use full texture. |
| 203 | mWallpaper.adjustTextureCoordinates(null, null, 0, 0); |
| 204 | // We need draw full texture back before finishing render. |
| 205 | mProxy.requestRender(); |
| 206 | } |
Ahan Wu | fa42c51 | 2019-05-15 19:52:51 +0800 | [diff] [blame] | 207 | mProxy.postRender(); |
Ahan Wu | 67e7f10 | 2019-01-14 20:38:14 +0800 | [diff] [blame] | 208 | } |
Ahan Wu | 7688424 | 2019-05-22 20:04:23 +0800 | [diff] [blame] | 209 | |
| 210 | @Override |
| 211 | public void dump(String prefix, FileDescriptor fd, PrintWriter out, String[] args) { |
| 212 | out.print(prefix); out.print("mProxy="); out.print(mProxy); |
| 213 | out.print(prefix); out.print("mSurfaceSize="); out.print(mSurfaceSize); |
Ahan Wu | e16e1fa | 2019-05-29 18:39:33 +0800 | [diff] [blame] | 214 | out.print(prefix); out.print("mScissor="); out.print(mScissor); |
| 215 | out.print(prefix); out.print("mViewport="); out.print(mViewport); |
| 216 | out.print(prefix); out.print("mScissorMode="); out.print(mScissorMode); |
| 217 | out.print(prefix); out.print("mXOffset="); out.print(mXOffset); |
| 218 | out.print(prefix); out.print("mYOffset="); out.print(mYOffset); |
Ahan Wu | 7688424 | 2019-05-22 20:04:23 +0800 | [diff] [blame] | 219 | out.print(prefix); out.print("threshold="); out.print(mImageProcessHelper.getThreshold()); |
Ahan Wu | e88b165 | 2019-09-19 22:12:46 +0800 | [diff] [blame] | 220 | out.print(prefix); out.print("mReveal="); out.print(mImageRevealHelper.getReveal()); |
Ahan Wu | 3222d3f | 2020-03-11 20:24:00 +0800 | [diff] [blame] | 221 | out.print(prefix); out.print("mWcgContent="); out.print(isWcgContent()); |
Ahan Wu | e16e1fa | 2019-05-29 18:39:33 +0800 | [diff] [blame] | 222 | mWallpaper.dump(prefix, fd, out, args); |
Ahan Wu | 7688424 | 2019-05-22 20:04:23 +0800 | [diff] [blame] | 223 | } |
Ahan Wu | 3222d3f | 2020-03-11 20:24:00 +0800 | [diff] [blame] | 224 | |
| 225 | static class WallpaperTexture { |
| 226 | private final AtomicInteger mRefCount; |
| 227 | private final Rect mDimensions; |
| 228 | private final WallpaperManager mWallpaperManager; |
| 229 | private Bitmap mBitmap; |
| 230 | private boolean mWcgContent; |
| 231 | |
| 232 | private WallpaperTexture(WallpaperManager wallpaperManager) { |
| 233 | mWallpaperManager = wallpaperManager; |
| 234 | mRefCount = new AtomicInteger(); |
| 235 | mDimensions = new Rect(); |
| 236 | } |
| 237 | |
| 238 | public void use(Consumer<Bitmap> consumer) { |
| 239 | mRefCount.incrementAndGet(); |
| 240 | synchronized (mRefCount) { |
| 241 | if (mBitmap == null) { |
| 242 | mBitmap = mWallpaperManager.getBitmap(false /* hardware */); |
| 243 | mWcgContent = mWallpaperManager.wallpaperSupportsWcg( |
| 244 | WallpaperManager.FLAG_SYSTEM); |
| 245 | mWallpaperManager.forgetLoadedWallpaper(); |
| 246 | if (mBitmap != null) { |
| 247 | mDimensions.set(0, 0, mBitmap.getWidth(), mBitmap.getHeight()); |
| 248 | } else { |
| 249 | Log.w(TAG, "Can't get bitmap"); |
| 250 | } |
| 251 | } |
| 252 | } |
| 253 | if (consumer != null) { |
| 254 | consumer.accept(mBitmap); |
| 255 | } |
| 256 | synchronized (mRefCount) { |
| 257 | final int count = mRefCount.decrementAndGet(); |
| 258 | if (count == 0 && mBitmap != null) { |
| 259 | if (DEBUG) { |
| 260 | Log.v(TAG, "WallpaperTexture: release 0x" + getHash() |
| 261 | + ", refCount=" + count); |
| 262 | } |
| 263 | mBitmap.recycle(); |
| 264 | mBitmap = null; |
| 265 | } |
| 266 | } |
| 267 | } |
| 268 | |
| 269 | private boolean isWcgContent() { |
| 270 | return mWcgContent; |
| 271 | } |
| 272 | |
| 273 | private String getHash() { |
| 274 | return mBitmap != null ? Integer.toHexString(mBitmap.hashCode()) : "null"; |
| 275 | } |
| 276 | |
| 277 | private Rect getTextureDimensions() { |
| 278 | return mDimensions; |
| 279 | } |
| 280 | |
| 281 | @Override |
| 282 | public String toString() { |
| 283 | return "{" + getHash() + ", " + mRefCount.get() + "}"; |
| 284 | } |
| 285 | } |
Ahan Wu | 67e7f10 | 2019-01-14 20:38:14 +0800 | [diff] [blame] | 286 | } |