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 | 67e7f10 | 2019-01-14 20:38:14 +0800 | [diff] [blame] | 49 | |
| 50 | private final WallpaperManager mWallpaperManager; |
| 51 | private final ImageGLProgram mProgram; |
| 52 | private final ImageGLWallpaper mWallpaper; |
| 53 | private final ImageProcessHelper mImageProcessHelper; |
| 54 | private final ImageRevealHelper mImageRevealHelper; |
Ahan Wu | 67e7f10 | 2019-01-14 20:38:14 +0800 | [diff] [blame] | 55 | |
Ahan Wu | fa42c51 | 2019-05-15 19:52:51 +0800 | [diff] [blame] | 56 | private SurfaceProxy mProxy; |
Ahan Wu | e16e1fa | 2019-05-29 18:39:33 +0800 | [diff] [blame] | 57 | private final Rect mScissor; |
| 58 | private final Rect mSurfaceSize = new Rect(); |
| 59 | private final Rect mViewport = new Rect(); |
Ahan Wu | 48ebbd7 | 2019-03-12 20:59:13 +0800 | [diff] [blame] | 60 | private Bitmap mBitmap; |
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 | 48ebbd7 | 2019-03-12 20:59:13 +0800 | [diff] [blame] | 64 | |
Ahan Wu | fa42c51 | 2019-05-15 19:52:51 +0800 | [diff] [blame] | 65 | public ImageWallpaperRenderer(Context context, SurfaceProxy proxy) { |
Ahan Wu | 67e7f10 | 2019-01-14 20:38:14 +0800 | [diff] [blame] | 66 | mWallpaperManager = context.getSystemService(WallpaperManager.class); |
| 67 | if (mWallpaperManager == null) { |
| 68 | Log.w(TAG, "WallpaperManager not available"); |
| 69 | } |
| 70 | |
Ahan Wu | e16e1fa | 2019-05-29 18:39:33 +0800 | [diff] [blame] | 71 | DisplayInfo displayInfo = new DisplayInfo(); |
| 72 | WindowManager wm = context.getSystemService(WindowManager.class); |
| 73 | wm.getDefaultDisplay().getDisplayInfo(displayInfo); |
Ahan Wu | 0e17480 | 2019-07-23 20:41:52 +0800 | [diff] [blame] | 74 | |
| 75 | // We only do transition in portrait currently, b/137962047. |
| 76 | int orientation = context.getResources().getConfiguration().orientation; |
| 77 | if (orientation == Configuration.ORIENTATION_PORTRAIT) { |
| 78 | mScissor = new Rect(0, 0, displayInfo.logicalWidth, displayInfo.logicalHeight); |
| 79 | } else { |
| 80 | mScissor = new Rect(0, 0, displayInfo.logicalHeight, displayInfo.logicalWidth); |
| 81 | } |
Ahan Wu | e16e1fa | 2019-05-29 18:39:33 +0800 | [diff] [blame] | 82 | |
Ahan Wu | fa42c51 | 2019-05-15 19:52:51 +0800 | [diff] [blame] | 83 | mProxy = proxy; |
Ahan Wu | 67e7f10 | 2019-01-14 20:38:14 +0800 | [diff] [blame] | 84 | mProgram = new ImageGLProgram(context); |
| 85 | mWallpaper = new ImageGLWallpaper(mProgram); |
| 86 | mImageProcessHelper = new ImageProcessHelper(); |
| 87 | mImageRevealHelper = new ImageRevealHelper(this); |
Ahan Wu | 67e7f10 | 2019-01-14 20:38:14 +0800 | [diff] [blame] | 88 | |
Ahan Wu | fa42c51 | 2019-05-15 19:52:51 +0800 | [diff] [blame] | 89 | if (loadBitmap()) { |
Ahan Wu | 330bc60 | 2019-04-25 15:04:39 +0800 | [diff] [blame] | 90 | // Compute threshold of the image, this is an async work. |
| 91 | mImageProcessHelper.start(mBitmap); |
Ahan Wu | 67e7f10 | 2019-01-14 20:38:14 +0800 | [diff] [blame] | 92 | } |
| 93 | } |
| 94 | |
| 95 | @Override |
Ahan Wu | fa42c51 | 2019-05-15 19:52:51 +0800 | [diff] [blame] | 96 | public void onSurfaceCreated() { |
Ahan Wu | 67e7f10 | 2019-01-14 20:38:14 +0800 | [diff] [blame] | 97 | glClearColor(0f, 0f, 0f, 1.0f); |
| 98 | mProgram.useGLProgram( |
| 99 | R.raw.image_wallpaper_vertex_shader, R.raw.image_wallpaper_fragment_shader); |
Ahan Wu | fa42c51 | 2019-05-15 19:52:51 +0800 | [diff] [blame] | 100 | |
| 101 | if (!loadBitmap()) { |
| 102 | Log.w(TAG, "reload bitmap failed!"); |
| 103 | } |
| 104 | |
Ahan Wu | 48ebbd7 | 2019-03-12 20:59:13 +0800 | [diff] [blame] | 105 | mWallpaper.setup(mBitmap); |
| 106 | mBitmap = null; |
Ahan Wu | 67e7f10 | 2019-01-14 20:38:14 +0800 | [diff] [blame] | 107 | } |
| 108 | |
Ahan Wu | fa42c51 | 2019-05-15 19:52:51 +0800 | [diff] [blame] | 109 | private boolean loadBitmap() { |
| 110 | if (mWallpaperManager != null && mBitmap == null) { |
| 111 | mBitmap = mWallpaperManager.getBitmap(); |
| 112 | mWallpaperManager.forgetLoadedWallpaper(); |
| 113 | if (mBitmap != null) { |
Ahan Wu | c6e84a1 | 2019-09-06 19:19:05 +0800 | [diff] [blame] | 114 | float scale = (float) mScissor.height() / mBitmap.getHeight(); |
| 115 | int surfaceHeight = Math.max(mScissor.height(), mBitmap.getHeight()); |
| 116 | int surfaceWidth = scale > 1f |
| 117 | ? Math.round(mBitmap.getWidth() * scale) |
| 118 | : mBitmap.getWidth(); |
| 119 | mSurfaceSize.set(0, 0, surfaceWidth, surfaceHeight); |
Ahan Wu | fa42c51 | 2019-05-15 19:52:51 +0800 | [diff] [blame] | 120 | } |
Ahan Wu | 67e7f10 | 2019-01-14 20:38:14 +0800 | [diff] [blame] | 121 | } |
Ahan Wu | fa42c51 | 2019-05-15 19:52:51 +0800 | [diff] [blame] | 122 | return mBitmap != null; |
Ahan Wu | 67e7f10 | 2019-01-14 20:38:14 +0800 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | @Override |
Ahan Wu | fa42c51 | 2019-05-15 19:52:51 +0800 | [diff] [blame] | 126 | public void onSurfaceChanged(int width, int height) { |
| 127 | glViewport(0, 0, width, height); |
| 128 | } |
| 129 | |
| 130 | @Override |
| 131 | public void onDrawFrame() { |
Ahan Wu | 330bc60 | 2019-04-25 15:04:39 +0800 | [diff] [blame] | 132 | float threshold = mImageProcessHelper.getThreshold(); |
Ahan Wu | 67e7f10 | 2019-01-14 20:38:14 +0800 | [diff] [blame] | 133 | float reveal = mImageRevealHelper.getReveal(); |
| 134 | |
Ahan Wu | 67e7f10 | 2019-01-14 20:38:14 +0800 | [diff] [blame] | 135 | glUniform1f(mWallpaper.getHandle(ImageGLWallpaper.U_AOD2OPACITY), 1); |
Ahan Wu | 330bc60 | 2019-04-25 15:04:39 +0800 | [diff] [blame] | 136 | glUniform1f(mWallpaper.getHandle(ImageGLWallpaper.U_PER85), threshold); |
Ahan Wu | 67e7f10 | 2019-01-14 20:38:14 +0800 | [diff] [blame] | 137 | glUniform1f(mWallpaper.getHandle(ImageGLWallpaper.U_REVEAL), reveal); |
| 138 | |
Ahan Wu | e16e1fa | 2019-05-29 18:39:33 +0800 | [diff] [blame] | 139 | glClear(GL_COLOR_BUFFER_BIT); |
| 140 | // We only need to scale viewport while doing transition. |
| 141 | if (mScissorMode) { |
| 142 | scaleViewport(reveal); |
| 143 | } else { |
| 144 | glViewport(0, 0, mSurfaceSize.width(), mSurfaceSize.height()); |
| 145 | } |
Ahan Wu | 67e7f10 | 2019-01-14 20:38:14 +0800 | [diff] [blame] | 146 | mWallpaper.useTexture(); |
| 147 | mWallpaper.draw(); |
| 148 | } |
| 149 | |
Ahan Wu | fa42c51 | 2019-05-15 19:52:51 +0800 | [diff] [blame] | 150 | @Override |
| 151 | public void updateAmbientMode(boolean inAmbientMode, long duration) { |
| 152 | mImageRevealHelper.updateAwake(!inAmbientMode, duration); |
| 153 | } |
| 154 | |
| 155 | @Override |
Ahan Wu | e16e1fa | 2019-05-29 18:39:33 +0800 | [diff] [blame] | 156 | public void updateOffsets(float xOffset, float yOffset) { |
| 157 | mXOffset = xOffset; |
| 158 | mYOffset = yOffset; |
| 159 | int left = (int) ((mSurfaceSize.width() - mScissor.width()) * xOffset); |
| 160 | int right = left + mScissor.width(); |
| 161 | mScissor.set(left, mScissor.top, right, mScissor.bottom); |
| 162 | } |
| 163 | |
| 164 | @Override |
Ahan Wu | fa42c51 | 2019-05-15 19:52:51 +0800 | [diff] [blame] | 165 | public Size reportSurfaceSize() { |
| 166 | return new Size(mSurfaceSize.width(), mSurfaceSize.height()); |
| 167 | } |
| 168 | |
| 169 | @Override |
| 170 | public void finish() { |
| 171 | mProxy = null; |
| 172 | } |
| 173 | |
Ahan Wu | c836335 | 2019-03-07 17:35:27 +0800 | [diff] [blame] | 174 | private void scaleViewport(float reveal) { |
Ahan Wu | e16e1fa | 2019-05-29 18:39:33 +0800 | [diff] [blame] | 175 | int left = mScissor.left; |
| 176 | int top = mScissor.top; |
| 177 | int width = mScissor.width(); |
| 178 | int height = mScissor.height(); |
Ahan Wu | c836335 | 2019-03-07 17:35:27 +0800 | [diff] [blame] | 179 | // Interpolation between SCALE_VIEWPORT_MAX and SCALE_VIEWPORT_MIN by reveal. |
Ahan Wu | e16e1fa | 2019-05-29 18:39:33 +0800 | [diff] [blame] | 180 | float vpScaled = MathUtils.lerp(SCALE_VIEWPORT_MIN, SCALE_VIEWPORT_MAX, reveal); |
Ahan Wu | c836335 | 2019-03-07 17:35:27 +0800 | [diff] [blame] | 181 | // Calculate the offset amount from the lower left corner. |
Ahan Wu | e16e1fa | 2019-05-29 18:39:33 +0800 | [diff] [blame] | 182 | float offset = (SCALE_VIEWPORT_MIN - vpScaled) / 2; |
Ahan Wu | c836335 | 2019-03-07 17:35:27 +0800 | [diff] [blame] | 183 | // Change the viewport. |
Ahan Wu | e16e1fa | 2019-05-29 18:39:33 +0800 | [diff] [blame] | 184 | mViewport.set((int) (left + width * offset), (int) (top + height * offset), |
Ahan Wu | fa42c51 | 2019-05-15 19:52:51 +0800 | [diff] [blame] | 185 | (int) (width * vpScaled), (int) (height * vpScaled)); |
Ahan Wu | e16e1fa | 2019-05-29 18:39:33 +0800 | [diff] [blame] | 186 | glViewport(mViewport.left, mViewport.top, mViewport.right, mViewport.bottom); |
Ahan Wu | 67e7f10 | 2019-01-14 20:38:14 +0800 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | @Override |
| 190 | public void onRevealStateChanged() { |
Ahan Wu | fa42c51 | 2019-05-15 19:52:51 +0800 | [diff] [blame] | 191 | mProxy.requestRender(); |
Ahan Wu | 67e7f10 | 2019-01-14 20:38:14 +0800 | [diff] [blame] | 192 | } |
| 193 | |
Ahan Wu | fa42c51 | 2019-05-15 19:52:51 +0800 | [diff] [blame] | 194 | @Override |
Ahan Wu | 0e17480 | 2019-07-23 20:41:52 +0800 | [diff] [blame] | 195 | public void onRevealStart(boolean animate) { |
| 196 | if (animate) { |
| 197 | mScissorMode = true; |
| 198 | // Use current display area of texture. |
| 199 | mWallpaper.adjustTextureCoordinates(mSurfaceSize, mScissor, mXOffset, mYOffset); |
| 200 | } |
Ahan Wu | fa42c51 | 2019-05-15 19:52:51 +0800 | [diff] [blame] | 201 | mProxy.preRender(); |
| 202 | } |
| 203 | |
| 204 | @Override |
| 205 | public void onRevealEnd() { |
Ahan Wu | 0e17480 | 2019-07-23 20:41:52 +0800 | [diff] [blame] | 206 | if (mScissorMode) { |
| 207 | mScissorMode = false; |
| 208 | // reset texture coordinates to use full texture. |
| 209 | mWallpaper.adjustTextureCoordinates(null, null, 0, 0); |
| 210 | // We need draw full texture back before finishing render. |
| 211 | mProxy.requestRender(); |
| 212 | } |
Ahan Wu | fa42c51 | 2019-05-15 19:52:51 +0800 | [diff] [blame] | 213 | mProxy.postRender(); |
Ahan Wu | 67e7f10 | 2019-01-14 20:38:14 +0800 | [diff] [blame] | 214 | } |
Ahan Wu | 7688424 | 2019-05-22 20:04:23 +0800 | [diff] [blame] | 215 | |
| 216 | @Override |
| 217 | public void dump(String prefix, FileDescriptor fd, PrintWriter out, String[] args) { |
| 218 | out.print(prefix); out.print("mProxy="); out.print(mProxy); |
| 219 | out.print(prefix); out.print("mSurfaceSize="); out.print(mSurfaceSize); |
Ahan Wu | e16e1fa | 2019-05-29 18:39:33 +0800 | [diff] [blame] | 220 | out.print(prefix); out.print("mScissor="); out.print(mScissor); |
| 221 | out.print(prefix); out.print("mViewport="); out.print(mViewport); |
| 222 | out.print(prefix); out.print("mScissorMode="); out.print(mScissorMode); |
| 223 | out.print(prefix); out.print("mXOffset="); out.print(mXOffset); |
| 224 | out.print(prefix); out.print("mYOffset="); out.print(mYOffset); |
Ahan Wu | 7688424 | 2019-05-22 20:04:23 +0800 | [diff] [blame] | 225 | out.print(prefix); out.print("threshold="); out.print(mImageProcessHelper.getThreshold()); |
Ahan Wu | e16e1fa | 2019-05-29 18:39:33 +0800 | [diff] [blame] | 226 | mWallpaper.dump(prefix, fd, out, args); |
Ahan Wu | 7688424 | 2019-05-22 20:04:23 +0800 | [diff] [blame] | 227 | } |
Ahan Wu | 67e7f10 | 2019-01-14 20:38:14 +0800 | [diff] [blame] | 228 | } |