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