Ahan Wu | 9a8e260 | 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.glUniform1i; |
| 24 | import static android.opengl.GLES20.glViewport; |
| 25 | |
| 26 | import android.app.WallpaperManager; |
| 27 | import android.content.Context; |
| 28 | import android.graphics.Bitmap; |
| 29 | import android.graphics.Rect; |
| 30 | import android.opengl.GLSurfaceView; |
| 31 | import android.util.Log; |
| 32 | |
| 33 | import com.android.systemui.ImageWallpaper; |
| 34 | import com.android.systemui.ImageWallpaper.ImageGLView; |
| 35 | import com.android.systemui.R; |
| 36 | |
| 37 | import javax.microedition.khronos.egl.EGLConfig; |
| 38 | import javax.microedition.khronos.opengles.GL10; |
| 39 | |
| 40 | /** |
| 41 | * A GL renderer for image wallpaper. |
| 42 | */ |
| 43 | public class ImageWallpaperRenderer implements GLSurfaceView.Renderer, |
| 44 | ImageWallpaper.SensorEventListener, ImageWallpaper.WallpaperStatusListener, |
| 45 | ImageRevealHelper.RevealStateListener { |
| 46 | private static final String TAG = ImageWallpaperRenderer.class.getSimpleName(); |
| 47 | |
| 48 | private final WallpaperManager mWallpaperManager; |
| 49 | private final ImageGLProgram mProgram; |
| 50 | private final ImageGLWallpaper mWallpaper; |
| 51 | private final ImageProcessHelper mImageProcessHelper; |
| 52 | private final ImageRevealHelper mImageRevealHelper; |
| 53 | private final ImageGLView mGLView; |
| 54 | private boolean mIsInAmbientMode; |
| 55 | private float mXOffset = 0f; |
| 56 | private float mYOffset = 0f; |
| 57 | |
| 58 | public ImageWallpaperRenderer(Context context, ImageGLView glView) { |
| 59 | mWallpaperManager = context.getSystemService(WallpaperManager.class); |
| 60 | if (mWallpaperManager == null) { |
| 61 | Log.w(TAG, "WallpaperManager not available"); |
| 62 | } |
| 63 | |
| 64 | mProgram = new ImageGLProgram(context); |
| 65 | mWallpaper = new ImageGLWallpaper(mProgram); |
| 66 | mImageProcessHelper = new ImageProcessHelper(); |
| 67 | mImageRevealHelper = new ImageRevealHelper(this); |
| 68 | mGLView = glView; |
| 69 | |
| 70 | if (mWallpaperManager != null) { |
| 71 | // Compute per85 as transition threshold, this is an async work. |
| 72 | mImageProcessHelper.startComputingPercentile85(mWallpaperManager.getBitmap()); |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | @Override |
| 77 | public void onSurfaceCreated(GL10 gl, EGLConfig config) { |
| 78 | glClearColor(0f, 0f, 0f, 1.0f); |
| 79 | mProgram.useGLProgram( |
| 80 | R.raw.image_wallpaper_vertex_shader, R.raw.image_wallpaper_fragment_shader); |
| 81 | mWallpaper.setup(); |
| 82 | mWallpaper.setupTexture(mWallpaperManager.getBitmap()); |
| 83 | } |
| 84 | |
| 85 | @Override |
| 86 | public void onSurfaceChanged(GL10 gl, int width, int height) { |
| 87 | glViewport(0, 0, width, height); |
| 88 | mWallpaper.adjustTextureCoordinates(mWallpaperManager.getBitmap(), |
| 89 | width, height, mXOffset, mYOffset); |
| 90 | } |
| 91 | |
| 92 | @Override |
| 93 | public void onDrawFrame(GL10 gl) { |
| 94 | float threshold = mImageProcessHelper.getPercentile85(); |
| 95 | float reveal = mImageRevealHelper.getReveal(); |
| 96 | |
| 97 | glClear(GL_COLOR_BUFFER_BIT); |
| 98 | |
| 99 | glUniform1f(mWallpaper.getHandle(ImageGLWallpaper.U_AOD2OPACITY), .25f); |
| 100 | glUniform1f(mWallpaper.getHandle(ImageGLWallpaper.U_CENTER_REVEAL), threshold); |
| 101 | glUniform1f(mWallpaper.getHandle(ImageGLWallpaper.U_REVEAL), reveal); |
| 102 | glUniform1i(mWallpaper.getHandle(ImageGLWallpaper.U_AOD_MODE), mIsInAmbientMode ? 1 : 0); |
| 103 | |
| 104 | mWallpaper.useTexture(); |
| 105 | mWallpaper.draw(); |
| 106 | } |
| 107 | |
| 108 | @Override |
| 109 | public void onSensorEvent(boolean awake) { |
| 110 | mImageRevealHelper.updateAwake(awake); |
| 111 | } |
| 112 | |
| 113 | @Override |
| 114 | public void onAmbientModeChanged(boolean inAmbientMode) { |
| 115 | mIsInAmbientMode = inAmbientMode; |
| 116 | if (inAmbientMode) { |
| 117 | mImageRevealHelper.sleep(); |
| 118 | } |
| 119 | requestRender(); |
| 120 | } |
| 121 | |
| 122 | @Override |
| 123 | public void onOffsetsChanged(float xOffset, float yOffset, Rect frame) { |
| 124 | if (frame == null || mWallpaperManager == null |
| 125 | || (xOffset == mXOffset && yOffset == mYOffset)) { |
| 126 | return; |
| 127 | } |
| 128 | |
| 129 | Bitmap bitmap = mWallpaperManager.getBitmap(); |
| 130 | if (bitmap == null) { |
| 131 | return; |
| 132 | } |
| 133 | |
| 134 | int width = frame.width(); |
| 135 | int height = frame.height(); |
| 136 | mXOffset = xOffset; |
| 137 | mYOffset = yOffset; |
| 138 | |
| 139 | mWallpaper.adjustTextureCoordinates(bitmap, width, height, mXOffset, mYOffset); |
| 140 | requestRender(); |
| 141 | } |
| 142 | |
| 143 | @Override |
| 144 | public void onRevealStateChanged() { |
| 145 | requestRender(); |
| 146 | } |
| 147 | |
| 148 | private void requestRender() { |
| 149 | if (mGLView != null) { |
| 150 | mGLView.render(); |
| 151 | } |
| 152 | } |
| 153 | } |