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; |
Ahan Wu | 67e7f10 | 2019-01-14 20:38:14 +0800 | [diff] [blame] | 22 | import static android.opengl.GLES20.glViewport; |
| 23 | |
| 24 | import android.app.WallpaperManager; |
| 25 | import android.content.Context; |
| 26 | import android.graphics.Bitmap; |
| 27 | import android.graphics.Rect; |
Ahan Wu | 67e7f10 | 2019-01-14 20:38:14 +0800 | [diff] [blame] | 28 | import android.util.Log; |
Ahan Wu | fa42c51 | 2019-05-15 19:52:51 +0800 | [diff] [blame] | 29 | import android.util.Size; |
Ahan Wu | 67e7f10 | 2019-01-14 20:38:14 +0800 | [diff] [blame] | 30 | |
Ahan Wu | 67e7f10 | 2019-01-14 20:38:14 +0800 | [diff] [blame] | 31 | import com.android.systemui.R; |
| 32 | |
Ahan Wu | 7688424 | 2019-05-22 20:04:23 +0800 | [diff] [blame] | 33 | import java.io.FileDescriptor; |
| 34 | import java.io.PrintWriter; |
Ahan Wu | 3222d3f | 2020-03-11 20:24:00 +0800 | [diff] [blame] | 35 | import java.util.concurrent.atomic.AtomicInteger; |
| 36 | import java.util.function.Consumer; |
Ahan Wu | 7688424 | 2019-05-22 20:04:23 +0800 | [diff] [blame] | 37 | |
Ahan Wu | 67e7f10 | 2019-01-14 20:38:14 +0800 | [diff] [blame] | 38 | /** |
| 39 | * A GL renderer for image wallpaper. |
| 40 | */ |
Ahan Wu | 4a29779 | 2020-04-16 03:00:14 +0800 | [diff] [blame] | 41 | public class ImageWallpaperRenderer implements GLWallpaperRenderer { |
Ahan Wu | 67e7f10 | 2019-01-14 20:38:14 +0800 | [diff] [blame] | 42 | private static final String TAG = ImageWallpaperRenderer.class.getSimpleName(); |
Ahan Wu | 4a29779 | 2020-04-16 03:00:14 +0800 | [diff] [blame] | 43 | private static final boolean DEBUG = false; |
Ahan Wu | 67e7f10 | 2019-01-14 20:38:14 +0800 | [diff] [blame] | 44 | |
Ahan Wu | 67e7f10 | 2019-01-14 20:38:14 +0800 | [diff] [blame] | 45 | private final ImageGLProgram mProgram; |
| 46 | private final ImageGLWallpaper mWallpaper; |
Ahan Wu | e16e1fa | 2019-05-29 18:39:33 +0800 | [diff] [blame] | 47 | private final Rect mSurfaceSize = new Rect(); |
Ahan Wu | 3222d3f | 2020-03-11 20:24:00 +0800 | [diff] [blame] | 48 | private final WallpaperTexture mTexture; |
Ahan Wu | 48ebbd7 | 2019-03-12 20:59:13 +0800 | [diff] [blame] | 49 | |
Ahan Wu | 4a29779 | 2020-04-16 03:00:14 +0800 | [diff] [blame] | 50 | public ImageWallpaperRenderer(Context context) { |
Ahan Wu | 3222d3f | 2020-03-11 20:24:00 +0800 | [diff] [blame] | 51 | final WallpaperManager wpm = context.getSystemService(WallpaperManager.class); |
| 52 | if (wpm == null) { |
Ahan Wu | 67e7f10 | 2019-01-14 20:38:14 +0800 | [diff] [blame] | 53 | Log.w(TAG, "WallpaperManager not available"); |
| 54 | } |
| 55 | |
Ahan Wu | 3222d3f | 2020-03-11 20:24:00 +0800 | [diff] [blame] | 56 | mTexture = new WallpaperTexture(wpm); |
Ahan Wu | 67e7f10 | 2019-01-14 20:38:14 +0800 | [diff] [blame] | 57 | mProgram = new ImageGLProgram(context); |
| 58 | mWallpaper = new ImageGLWallpaper(mProgram); |
Ahan Wu | 67e7f10 | 2019-01-14 20:38:14 +0800 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | @Override |
Ahan Wu | 287d828 | 2019-12-17 16:23:17 +0800 | [diff] [blame] | 62 | public boolean isWcgContent() { |
Ahan Wu | 3222d3f | 2020-03-11 20:24:00 +0800 | [diff] [blame] | 63 | return mTexture.isWcgContent(); |
Ahan Wu | 287d828 | 2019-12-17 16:23:17 +0800 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | @Override |
Ahan Wu | fa42c51 | 2019-05-15 19:52:51 +0800 | [diff] [blame] | 67 | public void onSurfaceCreated() { |
Ahan Wu | 67e7f10 | 2019-01-14 20:38:14 +0800 | [diff] [blame] | 68 | glClearColor(0f, 0f, 0f, 1.0f); |
| 69 | mProgram.useGLProgram( |
| 70 | R.raw.image_wallpaper_vertex_shader, R.raw.image_wallpaper_fragment_shader); |
Ahan Wu | fa42c51 | 2019-05-15 19:52:51 +0800 | [diff] [blame] | 71 | |
Ahan Wu | 3222d3f | 2020-03-11 20:24:00 +0800 | [diff] [blame] | 72 | mTexture.use(bitmap -> { |
| 73 | if (bitmap == null) { |
| 74 | Log.w(TAG, "reload texture failed!"); |
Ahan Wu | fa42c51 | 2019-05-15 19:52:51 +0800 | [diff] [blame] | 75 | } |
Ahan Wu | 3222d3f | 2020-03-11 20:24:00 +0800 | [diff] [blame] | 76 | mWallpaper.setup(bitmap); |
| 77 | }); |
Ahan Wu | 67e7f10 | 2019-01-14 20:38:14 +0800 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | @Override |
Ahan Wu | fa42c51 | 2019-05-15 19:52:51 +0800 | [diff] [blame] | 81 | public void onSurfaceChanged(int width, int height) { |
| 82 | glViewport(0, 0, width, height); |
| 83 | } |
| 84 | |
| 85 | @Override |
| 86 | public void onDrawFrame() { |
Ahan Wu | e16e1fa | 2019-05-29 18:39:33 +0800 | [diff] [blame] | 87 | glClear(GL_COLOR_BUFFER_BIT); |
Ahan Wu | 4a29779 | 2020-04-16 03:00:14 +0800 | [diff] [blame] | 88 | glViewport(0, 0, mSurfaceSize.width(), mSurfaceSize.height()); |
Ahan Wu | 67e7f10 | 2019-01-14 20:38:14 +0800 | [diff] [blame] | 89 | mWallpaper.useTexture(); |
| 90 | mWallpaper.draw(); |
| 91 | } |
| 92 | |
Ahan Wu | fa42c51 | 2019-05-15 19:52:51 +0800 | [diff] [blame] | 93 | @Override |
Ahan Wu | fa42c51 | 2019-05-15 19:52:51 +0800 | [diff] [blame] | 94 | public Size reportSurfaceSize() { |
Ahan Wu | 4a29779 | 2020-04-16 03:00:14 +0800 | [diff] [blame] | 95 | mTexture.use(null /* consumer */); |
Ahan Wu | 3222d3f | 2020-03-11 20:24:00 +0800 | [diff] [blame] | 96 | mSurfaceSize.set(mTexture.getTextureDimensions()); |
Ahan Wu | fa42c51 | 2019-05-15 19:52:51 +0800 | [diff] [blame] | 97 | return new Size(mSurfaceSize.width(), mSurfaceSize.height()); |
| 98 | } |
| 99 | |
| 100 | @Override |
| 101 | public void finish() { |
Ahan Wu | 67e7f10 | 2019-01-14 20:38:14 +0800 | [diff] [blame] | 102 | } |
Ahan Wu | 7688424 | 2019-05-22 20:04:23 +0800 | [diff] [blame] | 103 | |
| 104 | @Override |
| 105 | public void dump(String prefix, FileDescriptor fd, PrintWriter out, String[] args) { |
Ahan Wu | 7688424 | 2019-05-22 20:04:23 +0800 | [diff] [blame] | 106 | out.print(prefix); out.print("mSurfaceSize="); out.print(mSurfaceSize); |
Ahan Wu | 3222d3f | 2020-03-11 20:24:00 +0800 | [diff] [blame] | 107 | out.print(prefix); out.print("mWcgContent="); out.print(isWcgContent()); |
Ahan Wu | e16e1fa | 2019-05-29 18:39:33 +0800 | [diff] [blame] | 108 | mWallpaper.dump(prefix, fd, out, args); |
Ahan Wu | 7688424 | 2019-05-22 20:04:23 +0800 | [diff] [blame] | 109 | } |
Ahan Wu | 3222d3f | 2020-03-11 20:24:00 +0800 | [diff] [blame] | 110 | |
| 111 | static class WallpaperTexture { |
| 112 | private final AtomicInteger mRefCount; |
| 113 | private final Rect mDimensions; |
| 114 | private final WallpaperManager mWallpaperManager; |
| 115 | private Bitmap mBitmap; |
| 116 | private boolean mWcgContent; |
| 117 | |
| 118 | private WallpaperTexture(WallpaperManager wallpaperManager) { |
| 119 | mWallpaperManager = wallpaperManager; |
| 120 | mRefCount = new AtomicInteger(); |
| 121 | mDimensions = new Rect(); |
| 122 | } |
| 123 | |
| 124 | public void use(Consumer<Bitmap> consumer) { |
| 125 | mRefCount.incrementAndGet(); |
| 126 | synchronized (mRefCount) { |
| 127 | if (mBitmap == null) { |
| 128 | mBitmap = mWallpaperManager.getBitmap(false /* hardware */); |
| 129 | mWcgContent = mWallpaperManager.wallpaperSupportsWcg( |
| 130 | WallpaperManager.FLAG_SYSTEM); |
| 131 | mWallpaperManager.forgetLoadedWallpaper(); |
| 132 | if (mBitmap != null) { |
| 133 | mDimensions.set(0, 0, mBitmap.getWidth(), mBitmap.getHeight()); |
| 134 | } else { |
| 135 | Log.w(TAG, "Can't get bitmap"); |
| 136 | } |
| 137 | } |
| 138 | } |
| 139 | if (consumer != null) { |
| 140 | consumer.accept(mBitmap); |
| 141 | } |
| 142 | synchronized (mRefCount) { |
| 143 | final int count = mRefCount.decrementAndGet(); |
| 144 | if (count == 0 && mBitmap != null) { |
| 145 | if (DEBUG) { |
| 146 | Log.v(TAG, "WallpaperTexture: release 0x" + getHash() |
| 147 | + ", refCount=" + count); |
| 148 | } |
| 149 | mBitmap.recycle(); |
| 150 | mBitmap = null; |
| 151 | } |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | private boolean isWcgContent() { |
| 156 | return mWcgContent; |
| 157 | } |
| 158 | |
| 159 | private String getHash() { |
| 160 | return mBitmap != null ? Integer.toHexString(mBitmap.hashCode()) : "null"; |
| 161 | } |
| 162 | |
| 163 | private Rect getTextureDimensions() { |
| 164 | return mDimensions; |
| 165 | } |
| 166 | |
| 167 | @Override |
| 168 | public String toString() { |
| 169 | return "{" + getHash() + ", " + mRefCount.get() + "}"; |
| 170 | } |
| 171 | } |
Ahan Wu | 67e7f10 | 2019-01-14 20:38:14 +0800 | [diff] [blame] | 172 | } |