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; |
| 34 | import android.view.WindowManager; |
Ahan Wu | 67e7f10 | 2019-01-14 20:38:14 +0800 | [diff] [blame] | 35 | |
Ahan Wu | 67e7f10 | 2019-01-14 20:38:14 +0800 | [diff] [blame] | 36 | import com.android.systemui.R; |
| 37 | |
Ahan Wu | 7688424 | 2019-05-22 20:04:23 +0800 | [diff] [blame] | 38 | import java.io.FileDescriptor; |
| 39 | import java.io.PrintWriter; |
| 40 | |
Ahan Wu | 67e7f10 | 2019-01-14 20:38:14 +0800 | [diff] [blame] | 41 | /** |
| 42 | * A GL renderer for image wallpaper. |
| 43 | */ |
Ahan Wu | fa42c51 | 2019-05-15 19:52:51 +0800 | [diff] [blame] | 44 | public class ImageWallpaperRenderer implements GLWallpaperRenderer, |
| 45 | ImageRevealHelper.RevealStateListener { |
Ahan Wu | 67e7f10 | 2019-01-14 20:38:14 +0800 | [diff] [blame] | 46 | private static final String TAG = ImageWallpaperRenderer.class.getSimpleName(); |
Ahan Wu | e16e1fa | 2019-05-29 18:39:33 +0800 | [diff] [blame] | 47 | private static final float SCALE_VIEWPORT_MIN = 1f; |
| 48 | private static final float SCALE_VIEWPORT_MAX = 1.1f; |
Ahan Wu | 3573cdd | 2019-10-04 11:59:10 +0800 | [diff] [blame] | 49 | private static final boolean DEBUG = true; |
Ahan Wu | 67e7f10 | 2019-01-14 20:38:14 +0800 | [diff] [blame] | 50 | |
| 51 | private final WallpaperManager mWallpaperManager; |
| 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 | 48ebbd7 | 2019-03-12 20:59:13 +0800 | [diff] [blame] | 61 | private Bitmap mBitmap; |
Ahan Wu | e16e1fa | 2019-05-29 18:39:33 +0800 | [diff] [blame] | 62 | private boolean mScissorMode; |
| 63 | private float mXOffset; |
| 64 | private float mYOffset; |
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 | 67e7f10 | 2019-01-14 20:38:14 +0800 | [diff] [blame] | 67 | mWallpaperManager = context.getSystemService(WallpaperManager.class); |
| 68 | if (mWallpaperManager == null) { |
| 69 | Log.w(TAG, "WallpaperManager not available"); |
| 70 | } |
| 71 | |
Ahan Wu | e16e1fa | 2019-05-29 18:39:33 +0800 | [diff] [blame] | 72 | DisplayInfo displayInfo = new DisplayInfo(); |
| 73 | WindowManager wm = context.getSystemService(WindowManager.class); |
| 74 | wm.getDefaultDisplay().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 | fa42c51 | 2019-05-15 19:52:51 +0800 | [diff] [blame] | 90 | if (loadBitmap()) { |
Ahan Wu | 330bc60 | 2019-04-25 15:04:39 +0800 | [diff] [blame] | 91 | // Compute threshold of the image, this is an async work. |
| 92 | mImageProcessHelper.start(mBitmap); |
Ahan Wu | 67e7f10 | 2019-01-14 20:38:14 +0800 | [diff] [blame] | 93 | } |
| 94 | } |
| 95 | |
| 96 | @Override |
Ahan Wu | fa42c51 | 2019-05-15 19:52:51 +0800 | [diff] [blame] | 97 | public void onSurfaceCreated() { |
Ahan Wu | 67e7f10 | 2019-01-14 20:38:14 +0800 | [diff] [blame] | 98 | glClearColor(0f, 0f, 0f, 1.0f); |
| 99 | mProgram.useGLProgram( |
| 100 | R.raw.image_wallpaper_vertex_shader, R.raw.image_wallpaper_fragment_shader); |
Ahan Wu | fa42c51 | 2019-05-15 19:52:51 +0800 | [diff] [blame] | 101 | |
| 102 | if (!loadBitmap()) { |
| 103 | Log.w(TAG, "reload bitmap failed!"); |
| 104 | } |
| 105 | |
Ahan Wu | 48ebbd7 | 2019-03-12 20:59:13 +0800 | [diff] [blame] | 106 | mWallpaper.setup(mBitmap); |
| 107 | mBitmap = null; |
Ahan Wu | 67e7f10 | 2019-01-14 20:38:14 +0800 | [diff] [blame] | 108 | } |
| 109 | |
Ahan Wu | fa42c51 | 2019-05-15 19:52:51 +0800 | [diff] [blame] | 110 | private boolean loadBitmap() { |
Ahan Wu | 3573cdd | 2019-10-04 11:59:10 +0800 | [diff] [blame] | 111 | if (DEBUG) { |
| 112 | Log.d(TAG, "loadBitmap: mBitmap=" + mBitmap); |
| 113 | } |
Ahan Wu | fa42c51 | 2019-05-15 19:52:51 +0800 | [diff] [blame] | 114 | if (mWallpaperManager != null && mBitmap == null) { |
| 115 | mBitmap = mWallpaperManager.getBitmap(); |
| 116 | mWallpaperManager.forgetLoadedWallpaper(); |
| 117 | if (mBitmap != null) { |
Ahan Wu | c6e84a1 | 2019-09-06 19:19:05 +0800 | [diff] [blame] | 118 | float scale = (float) mScissor.height() / mBitmap.getHeight(); |
| 119 | int surfaceHeight = Math.max(mScissor.height(), mBitmap.getHeight()); |
| 120 | int surfaceWidth = scale > 1f |
| 121 | ? Math.round(mBitmap.getWidth() * scale) |
| 122 | : mBitmap.getWidth(); |
| 123 | mSurfaceSize.set(0, 0, surfaceWidth, surfaceHeight); |
Ahan Wu | fa42c51 | 2019-05-15 19:52:51 +0800 | [diff] [blame] | 124 | } |
Ahan Wu | 67e7f10 | 2019-01-14 20:38:14 +0800 | [diff] [blame] | 125 | } |
Ahan Wu | 3573cdd | 2019-10-04 11:59:10 +0800 | [diff] [blame] | 126 | if (DEBUG) { |
| 127 | Log.d(TAG, "loadBitmap done, surface size=" + mSurfaceSize); |
| 128 | } |
Ahan Wu | fa42c51 | 2019-05-15 19:52:51 +0800 | [diff] [blame] | 129 | return mBitmap != null; |
Ahan Wu | 67e7f10 | 2019-01-14 20:38:14 +0800 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | @Override |
Ahan Wu | fa42c51 | 2019-05-15 19:52:51 +0800 | [diff] [blame] | 133 | public void onSurfaceChanged(int width, int height) { |
| 134 | glViewport(0, 0, width, height); |
| 135 | } |
| 136 | |
| 137 | @Override |
| 138 | public void onDrawFrame() { |
Ahan Wu | 330bc60 | 2019-04-25 15:04:39 +0800 | [diff] [blame] | 139 | float threshold = mImageProcessHelper.getThreshold(); |
Ahan Wu | 67e7f10 | 2019-01-14 20:38:14 +0800 | [diff] [blame] | 140 | float reveal = mImageRevealHelper.getReveal(); |
| 141 | |
Ahan Wu | 67e7f10 | 2019-01-14 20:38:14 +0800 | [diff] [blame] | 142 | glUniform1f(mWallpaper.getHandle(ImageGLWallpaper.U_AOD2OPACITY), 1); |
Ahan Wu | 330bc60 | 2019-04-25 15:04:39 +0800 | [diff] [blame] | 143 | glUniform1f(mWallpaper.getHandle(ImageGLWallpaper.U_PER85), threshold); |
Ahan Wu | 67e7f10 | 2019-01-14 20:38:14 +0800 | [diff] [blame] | 144 | glUniform1f(mWallpaper.getHandle(ImageGLWallpaper.U_REVEAL), reveal); |
| 145 | |
Ahan Wu | e16e1fa | 2019-05-29 18:39:33 +0800 | [diff] [blame] | 146 | glClear(GL_COLOR_BUFFER_BIT); |
| 147 | // We only need to scale viewport while doing transition. |
| 148 | if (mScissorMode) { |
| 149 | scaleViewport(reveal); |
| 150 | } else { |
| 151 | glViewport(0, 0, mSurfaceSize.width(), mSurfaceSize.height()); |
| 152 | } |
Ahan Wu | 67e7f10 | 2019-01-14 20:38:14 +0800 | [diff] [blame] | 153 | mWallpaper.useTexture(); |
| 154 | mWallpaper.draw(); |
| 155 | } |
| 156 | |
Ahan Wu | fa42c51 | 2019-05-15 19:52:51 +0800 | [diff] [blame] | 157 | @Override |
| 158 | public void updateAmbientMode(boolean inAmbientMode, long duration) { |
| 159 | mImageRevealHelper.updateAwake(!inAmbientMode, duration); |
| 160 | } |
| 161 | |
| 162 | @Override |
Ahan Wu | e16e1fa | 2019-05-29 18:39:33 +0800 | [diff] [blame] | 163 | public void updateOffsets(float xOffset, float yOffset) { |
| 164 | mXOffset = xOffset; |
| 165 | mYOffset = yOffset; |
| 166 | int left = (int) ((mSurfaceSize.width() - mScissor.width()) * xOffset); |
| 167 | int right = left + mScissor.width(); |
| 168 | mScissor.set(left, mScissor.top, right, mScissor.bottom); |
| 169 | } |
| 170 | |
| 171 | @Override |
Ahan Wu | fa42c51 | 2019-05-15 19:52:51 +0800 | [diff] [blame] | 172 | public Size reportSurfaceSize() { |
| 173 | return new Size(mSurfaceSize.width(), mSurfaceSize.height()); |
| 174 | } |
| 175 | |
| 176 | @Override |
| 177 | public void finish() { |
| 178 | mProxy = null; |
| 179 | } |
| 180 | |
Ahan Wu | c836335 | 2019-03-07 17:35:27 +0800 | [diff] [blame] | 181 | private void scaleViewport(float reveal) { |
Ahan Wu | e16e1fa | 2019-05-29 18:39:33 +0800 | [diff] [blame] | 182 | int left = mScissor.left; |
| 183 | int top = mScissor.top; |
| 184 | int width = mScissor.width(); |
| 185 | int height = mScissor.height(); |
Ahan Wu | c836335 | 2019-03-07 17:35:27 +0800 | [diff] [blame] | 186 | // Interpolation between SCALE_VIEWPORT_MAX and SCALE_VIEWPORT_MIN by reveal. |
Ahan Wu | e16e1fa | 2019-05-29 18:39:33 +0800 | [diff] [blame] | 187 | float vpScaled = MathUtils.lerp(SCALE_VIEWPORT_MIN, SCALE_VIEWPORT_MAX, reveal); |
Ahan Wu | c836335 | 2019-03-07 17:35:27 +0800 | [diff] [blame] | 188 | // Calculate the offset amount from the lower left corner. |
Ahan Wu | e16e1fa | 2019-05-29 18:39:33 +0800 | [diff] [blame] | 189 | float offset = (SCALE_VIEWPORT_MIN - vpScaled) / 2; |
Ahan Wu | c836335 | 2019-03-07 17:35:27 +0800 | [diff] [blame] | 190 | // Change the viewport. |
Ahan Wu | e16e1fa | 2019-05-29 18:39:33 +0800 | [diff] [blame] | 191 | mViewport.set((int) (left + width * offset), (int) (top + height * offset), |
Ahan Wu | fa42c51 | 2019-05-15 19:52:51 +0800 | [diff] [blame] | 192 | (int) (width * vpScaled), (int) (height * vpScaled)); |
Ahan Wu | e16e1fa | 2019-05-29 18:39:33 +0800 | [diff] [blame] | 193 | glViewport(mViewport.left, mViewport.top, mViewport.right, mViewport.bottom); |
Ahan Wu | 67e7f10 | 2019-01-14 20:38:14 +0800 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | @Override |
| 197 | public void onRevealStateChanged() { |
Ahan Wu | fa42c51 | 2019-05-15 19:52:51 +0800 | [diff] [blame] | 198 | mProxy.requestRender(); |
Ahan Wu | 67e7f10 | 2019-01-14 20:38:14 +0800 | [diff] [blame] | 199 | } |
| 200 | |
Ahan Wu | fa42c51 | 2019-05-15 19:52:51 +0800 | [diff] [blame] | 201 | @Override |
Ahan Wu | 0e17480 | 2019-07-23 20:41:52 +0800 | [diff] [blame] | 202 | public void onRevealStart(boolean animate) { |
Ahan Wu | 3573cdd | 2019-10-04 11:59:10 +0800 | [diff] [blame] | 203 | if (DEBUG) { |
| 204 | Log.v(TAG, "onRevealStart: start, anim=" + animate); |
| 205 | } |
| 206 | |
Ahan Wu | 0e17480 | 2019-07-23 20:41:52 +0800 | [diff] [blame] | 207 | if (animate) { |
| 208 | mScissorMode = true; |
| 209 | // Use current display area of texture. |
| 210 | mWallpaper.adjustTextureCoordinates(mSurfaceSize, mScissor, mXOffset, mYOffset); |
| 211 | } |
Ahan Wu | fa42c51 | 2019-05-15 19:52:51 +0800 | [diff] [blame] | 212 | mProxy.preRender(); |
Ahan Wu | 3573cdd | 2019-10-04 11:59:10 +0800 | [diff] [blame] | 213 | |
| 214 | if (DEBUG) { |
| 215 | Log.v(TAG, "onRevealStart: done"); |
| 216 | } |
Ahan Wu | fa42c51 | 2019-05-15 19:52:51 +0800 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | @Override |
| 220 | public void onRevealEnd() { |
Ahan Wu | 3573cdd | 2019-10-04 11:59:10 +0800 | [diff] [blame] | 221 | if (DEBUG) { |
| 222 | Log.v(TAG, "onRevealEnd: start, mScissorMode=" + mScissorMode); |
| 223 | } |
| 224 | |
Ahan Wu | 0e17480 | 2019-07-23 20:41:52 +0800 | [diff] [blame] | 225 | if (mScissorMode) { |
| 226 | mScissorMode = false; |
| 227 | // reset texture coordinates to use full texture. |
| 228 | mWallpaper.adjustTextureCoordinates(null, null, 0, 0); |
| 229 | // We need draw full texture back before finishing render. |
| 230 | mProxy.requestRender(); |
| 231 | } |
Ahan Wu | fa42c51 | 2019-05-15 19:52:51 +0800 | [diff] [blame] | 232 | mProxy.postRender(); |
Ahan Wu | 3573cdd | 2019-10-04 11:59:10 +0800 | [diff] [blame] | 233 | |
| 234 | if (DEBUG) { |
| 235 | Log.v(TAG, "onRevealEnd: done"); |
| 236 | } |
Ahan Wu | 67e7f10 | 2019-01-14 20:38:14 +0800 | [diff] [blame] | 237 | } |
Ahan Wu | 7688424 | 2019-05-22 20:04:23 +0800 | [diff] [blame] | 238 | |
| 239 | @Override |
| 240 | public void dump(String prefix, FileDescriptor fd, PrintWriter out, String[] args) { |
| 241 | out.print(prefix); out.print("mProxy="); out.print(mProxy); |
| 242 | out.print(prefix); out.print("mSurfaceSize="); out.print(mSurfaceSize); |
Ahan Wu | e16e1fa | 2019-05-29 18:39:33 +0800 | [diff] [blame] | 243 | out.print(prefix); out.print("mScissor="); out.print(mScissor); |
| 244 | out.print(prefix); out.print("mViewport="); out.print(mViewport); |
| 245 | out.print(prefix); out.print("mScissorMode="); out.print(mScissorMode); |
| 246 | out.print(prefix); out.print("mXOffset="); out.print(mXOffset); |
| 247 | out.print(prefix); out.print("mYOffset="); out.print(mYOffset); |
Ahan Wu | 7688424 | 2019-05-22 20:04:23 +0800 | [diff] [blame] | 248 | out.print(prefix); out.print("threshold="); out.print(mImageProcessHelper.getThreshold()); |
Ahan Wu | 3573cdd | 2019-10-04 11:59:10 +0800 | [diff] [blame] | 249 | out.print(prefix); out.print("mReveal="); out.print(mImageRevealHelper.getReveal()); |
Ahan Wu | e16e1fa | 2019-05-29 18:39:33 +0800 | [diff] [blame] | 250 | mWallpaper.dump(prefix, fd, out, args); |
Ahan Wu | 7688424 | 2019-05-22 20:04:23 +0800 | [diff] [blame] | 251 | } |
Ahan Wu | 67e7f10 | 2019-01-14 20:38:14 +0800 | [diff] [blame] | 252 | } |