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