Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2009 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 | |
Dianne Hackborn | ba39839 | 2011-08-01 16:11:57 -0700 | [diff] [blame] | 17 | package com.android.systemui; |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 18 | |
John Spurlock | de84f0e | 2013-06-12 12:41:00 -0400 | [diff] [blame] | 19 | import static android.opengl.GLES20.*; |
Winson | c0d7058 | 2016-01-29 10:24:39 -0800 | [diff] [blame] | 20 | |
John Spurlock | de84f0e | 2013-06-12 12:41:00 -0400 | [diff] [blame] | 21 | import static javax.microedition.khronos.egl.EGL10.*; |
| 22 | |
Romain Guy | 407ec78 | 2011-08-24 17:06:58 -0700 | [diff] [blame] | 23 | import android.app.ActivityManager; |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 24 | import android.app.WallpaperManager; |
Chet Haase | c61d70e | 2012-10-10 15:41:57 -0700 | [diff] [blame] | 25 | import android.content.ComponentCallbacks2; |
Romain Guy | 407ec78 | 2011-08-24 17:06:58 -0700 | [diff] [blame] | 26 | import android.graphics.Bitmap; |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 27 | import android.graphics.Canvas; |
Dianne Hackborn | 759a39e | 2009-08-09 17:20:27 -0700 | [diff] [blame] | 28 | import android.graphics.Rect; |
Michael Jurka | 824a4b5 | 2013-12-18 17:10:16 +0100 | [diff] [blame] | 29 | import android.graphics.RectF; |
Mathias Agopian | e2d034c | 2009-09-23 21:06:17 -0700 | [diff] [blame] | 30 | import android.graphics.Region.Op; |
Romain Guy | 407ec78 | 2011-08-24 17:06:58 -0700 | [diff] [blame] | 31 | import android.opengl.GLUtils; |
Romain Guy | 043a6b1 | 2011-09-27 15:37:54 -0700 | [diff] [blame] | 32 | import android.os.SystemProperties; |
Romain Guy | 407ec78 | 2011-08-24 17:06:58 -0700 | [diff] [blame] | 33 | import android.renderscript.Matrix4f; |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 34 | import android.service.wallpaper.WallpaperService; |
Dianne Hackborn | c9dbbe2 | 2009-11-11 22:50:37 -0800 | [diff] [blame] | 35 | import android.util.Log; |
Michael Jurka | 824a4b5 | 2013-12-18 17:10:16 +0100 | [diff] [blame] | 36 | import android.view.Display; |
Adrian Roos | 9b9947d | 2015-06-09 16:46:05 -0700 | [diff] [blame] | 37 | import android.view.DisplayInfo; |
Dianne Hackborn | 8df8b2b | 2009-08-17 15:15:18 -0700 | [diff] [blame] | 38 | import android.view.MotionEvent; |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 39 | import android.view.SurfaceHolder; |
Chet Haase | 2f20081 | 2012-10-11 12:49:08 -0700 | [diff] [blame] | 40 | import android.view.WindowManager; |
Romain Guy | 407ec78 | 2011-08-24 17:06:58 -0700 | [diff] [blame] | 41 | |
Adrian Roos | ab6edbf | 2015-07-07 17:29:15 -0700 | [diff] [blame] | 42 | import java.io.FileDescriptor; |
Romain Guy | 407ec78 | 2011-08-24 17:06:58 -0700 | [diff] [blame] | 43 | import java.io.IOException; |
Adrian Roos | ab6edbf | 2015-07-07 17:29:15 -0700 | [diff] [blame] | 44 | import java.io.PrintWriter; |
Romain Guy | 407ec78 | 2011-08-24 17:06:58 -0700 | [diff] [blame] | 45 | import java.nio.ByteBuffer; |
| 46 | import java.nio.ByteOrder; |
| 47 | import java.nio.FloatBuffer; |
| 48 | |
John Spurlock | de84f0e | 2013-06-12 12:41:00 -0400 | [diff] [blame] | 49 | import javax.microedition.khronos.egl.EGL10; |
| 50 | import javax.microedition.khronos.egl.EGLConfig; |
| 51 | import javax.microedition.khronos.egl.EGLContext; |
| 52 | import javax.microedition.khronos.egl.EGLDisplay; |
| 53 | import javax.microedition.khronos.egl.EGLSurface; |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 54 | |
| 55 | /** |
| 56 | * Default built-in wallpaper that simply shows a static image. |
| 57 | */ |
Romain Guy | 407ec78 | 2011-08-24 17:06:58 -0700 | [diff] [blame] | 58 | @SuppressWarnings({"UnusedDeclaration"}) |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 59 | public class ImageWallpaper extends WallpaperService { |
Jeff Brown | fa2e504 | 2011-01-23 13:14:23 -0800 | [diff] [blame] | 60 | private static final String TAG = "ImageWallpaper"; |
Romain Guy | 407ec78 | 2011-08-24 17:06:58 -0700 | [diff] [blame] | 61 | private static final String GL_LOG_TAG = "ImageWallpaperGL"; |
Jeff Brown | fa2e504 | 2011-01-23 13:14:23 -0800 | [diff] [blame] | 62 | private static final boolean DEBUG = false; |
Romain Guy | 043a6b1 | 2011-09-27 15:37:54 -0700 | [diff] [blame] | 63 | private static final String PROPERTY_KERNEL_QEMU = "ro.kernel.qemu"; |
Jeff Brown | fa2e504 | 2011-01-23 13:14:23 -0800 | [diff] [blame] | 64 | |
Dianne Hackborn | ba39839 | 2011-08-01 16:11:57 -0700 | [diff] [blame] | 65 | static final boolean FIXED_SIZED_SURFACE = true; |
Erik Gilling | 881fb20 | 2011-08-25 10:27:52 -0700 | [diff] [blame] | 66 | static final boolean USE_OPENGL = true; |
Dianne Hackborn | ba39839 | 2011-08-01 16:11:57 -0700 | [diff] [blame] | 67 | |
Romain Guy | ef654bd | 2009-08-11 19:12:17 -0700 | [diff] [blame] | 68 | WallpaperManager mWallpaperManager; |
Romain Guy | 407ec78 | 2011-08-24 17:06:58 -0700 | [diff] [blame] | 69 | |
Chet Haase | c61d70e | 2012-10-10 15:41:57 -0700 | [diff] [blame] | 70 | DrawableEngine mEngine; |
| 71 | |
Romain Guy | 407ec78 | 2011-08-24 17:06:58 -0700 | [diff] [blame] | 72 | boolean mIsHwAccelerated; |
Dianne Hackborn | 759a39e | 2009-08-09 17:20:27 -0700 | [diff] [blame] | 73 | |
Romain Guy | ef654bd | 2009-08-11 19:12:17 -0700 | [diff] [blame] | 74 | @Override |
| 75 | public void onCreate() { |
| 76 | super.onCreate(); |
| 77 | mWallpaperManager = (WallpaperManager) getSystemService(WALLPAPER_SERVICE); |
Romain Guy | 407ec78 | 2011-08-24 17:06:58 -0700 | [diff] [blame] | 78 | |
| 79 | //noinspection PointlessBooleanExpression,ConstantConditions |
| 80 | if (FIXED_SIZED_SURFACE && USE_OPENGL) { |
Romain Guy | 043a6b1 | 2011-09-27 15:37:54 -0700 | [diff] [blame] | 81 | if (!isEmulator()) { |
Jeff Brown | 98365d7 | 2012-08-19 20:30:52 -0700 | [diff] [blame] | 82 | mIsHwAccelerated = ActivityManager.isHighEndGfx(); |
Romain Guy | 043a6b1 | 2011-09-27 15:37:54 -0700 | [diff] [blame] | 83 | } |
Romain Guy | 407ec78 | 2011-08-24 17:06:58 -0700 | [diff] [blame] | 84 | } |
Romain Guy | ef654bd | 2009-08-11 19:12:17 -0700 | [diff] [blame] | 85 | } |
| 86 | |
Chet Haase | c61d70e | 2012-10-10 15:41:57 -0700 | [diff] [blame] | 87 | @Override |
| 88 | public void onTrimMemory(int level) { |
| 89 | if (mEngine != null) { |
| 90 | mEngine.trimMemory(level); |
| 91 | } |
| 92 | } |
| 93 | |
Romain Guy | 043a6b1 | 2011-09-27 15:37:54 -0700 | [diff] [blame] | 94 | private static boolean isEmulator() { |
| 95 | return "1".equals(SystemProperties.get(PROPERTY_KERNEL_QEMU, "0")); |
| 96 | } |
| 97 | |
Craig Mautner | b1ef369 | 2012-11-16 17:31:04 -0800 | [diff] [blame] | 98 | @Override |
Romain Guy | ef654bd | 2009-08-11 19:12:17 -0700 | [diff] [blame] | 99 | public Engine onCreateEngine() { |
Chet Haase | c61d70e | 2012-10-10 15:41:57 -0700 | [diff] [blame] | 100 | mEngine = new DrawableEngine(); |
| 101 | return mEngine; |
Romain Guy | ef654bd | 2009-08-11 19:12:17 -0700 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | class DrawableEngine extends Engine { |
Romain Guy | 407ec78 | 2011-08-24 17:06:58 -0700 | [diff] [blame] | 105 | static final int EGL_CONTEXT_CLIENT_VERSION = 0x3098; |
| 106 | static final int EGL_OPENGL_ES2_BIT = 4; |
| 107 | |
Romain Guy | 407ec78 | 2011-08-24 17:06:58 -0700 | [diff] [blame] | 108 | Bitmap mBackground; |
Michael Jurka | 824a4b5 | 2013-12-18 17:10:16 +0100 | [diff] [blame] | 109 | int mBackgroundWidth = -1, mBackgroundHeight = -1; |
Chet Haase | 5f0d976 | 2012-10-18 12:01:34 -0700 | [diff] [blame] | 110 | int mLastSurfaceWidth = -1, mLastSurfaceHeight = -1; |
Chet Haase | 2f20081 | 2012-10-11 12:49:08 -0700 | [diff] [blame] | 111 | int mLastRotation = -1; |
Michael Jurka | 824a4b5 | 2013-12-18 17:10:16 +0100 | [diff] [blame] | 112 | float mXOffset = 0.5f; |
| 113 | float mYOffset = 0.5f; |
| 114 | float mScale = 1f; |
Romain Guy | ef654bd | 2009-08-11 19:12:17 -0700 | [diff] [blame] | 115 | |
Adrian Roos | 9b9947d | 2015-06-09 16:46:05 -0700 | [diff] [blame] | 116 | private Display mDefaultDisplay; |
| 117 | private final DisplayInfo mTmpDisplayInfo = new DisplayInfo(); |
| 118 | |
Jeff Brown | fa2e504 | 2011-01-23 13:14:23 -0800 | [diff] [blame] | 119 | boolean mVisible = true; |
Jeff Brown | fa2e504 | 2011-01-23 13:14:23 -0800 | [diff] [blame] | 120 | boolean mOffsetsChanged; |
| 121 | int mLastXTranslation; |
| 122 | int mLastYTranslation; |
| 123 | |
Romain Guy | 407ec78 | 2011-08-24 17:06:58 -0700 | [diff] [blame] | 124 | private EGL10 mEgl; |
| 125 | private EGLDisplay mEglDisplay; |
| 126 | private EGLConfig mEglConfig; |
| 127 | private EGLContext mEglContext; |
| 128 | private EGLSurface mEglSurface; |
Romain Guy | 407ec78 | 2011-08-24 17:06:58 -0700 | [diff] [blame] | 129 | |
| 130 | private static final String sSimpleVS = |
| 131 | "attribute vec4 position;\n" + |
| 132 | "attribute vec2 texCoords;\n" + |
| 133 | "varying vec2 outTexCoords;\n" + |
| 134 | "uniform mat4 projection;\n" + |
| 135 | "\nvoid main(void) {\n" + |
| 136 | " outTexCoords = texCoords;\n" + |
| 137 | " gl_Position = projection * position;\n" + |
| 138 | "}\n\n"; |
| 139 | private static final String sSimpleFS = |
| 140 | "precision mediump float;\n\n" + |
| 141 | "varying vec2 outTexCoords;\n" + |
| 142 | "uniform sampler2D texture;\n" + |
| 143 | "\nvoid main(void) {\n" + |
| 144 | " gl_FragColor = texture2D(texture, outTexCoords);\n" + |
| 145 | "}\n\n"; |
John Spurlock | 209bede | 2013-07-17 12:23:27 -0400 | [diff] [blame] | 146 | |
Romain Guy | 407ec78 | 2011-08-24 17:06:58 -0700 | [diff] [blame] | 147 | private static final int FLOAT_SIZE_BYTES = 4; |
| 148 | private static final int TRIANGLE_VERTICES_DATA_STRIDE_BYTES = 5 * FLOAT_SIZE_BYTES; |
| 149 | private static final int TRIANGLE_VERTICES_DATA_POS_OFFSET = 0; |
| 150 | private static final int TRIANGLE_VERTICES_DATA_UV_OFFSET = 3; |
| 151 | |
Adrian Roos | ab6edbf | 2015-07-07 17:29:15 -0700 | [diff] [blame] | 152 | private int mRotationAtLastSurfaceSizeUpdate = -1; |
| 153 | private int mDisplayWidthAtLastSurfaceSizeUpdate = -1; |
| 154 | private int mDisplayHeightAtLastSurfaceSizeUpdate = -1; |
| 155 | |
Adrian Roos | 30a7dfa | 2015-07-17 12:29:53 -0700 | [diff] [blame] | 156 | private int mLastRequestedWidth = -1; |
| 157 | private int mLastRequestedHeight = -1; |
| 158 | |
Jeff Sharkey | 35be756 | 2012-04-18 19:16:15 -0700 | [diff] [blame] | 159 | public DrawableEngine() { |
| 160 | super(); |
| 161 | setFixedSizeAllowed(true); |
| 162 | } |
| 163 | |
Chet Haase | c61d70e | 2012-10-10 15:41:57 -0700 | [diff] [blame] | 164 | public void trimMemory(int level) { |
| 165 | if (level >= ComponentCallbacks2.TRIM_MEMORY_RUNNING_LOW && |
Adrian Roos | 2acaeda | 2014-11-12 18:02:42 +0100 | [diff] [blame] | 166 | mBackground != null) { |
Chet Haase | 5f0d976 | 2012-10-18 12:01:34 -0700 | [diff] [blame] | 167 | if (DEBUG) { |
| 168 | Log.d(TAG, "trimMemory"); |
| 169 | } |
Chet Haase | c61d70e | 2012-10-10 15:41:57 -0700 | [diff] [blame] | 170 | mBackground.recycle(); |
| 171 | mBackground = null; |
Michael Jurka | 824a4b5 | 2013-12-18 17:10:16 +0100 | [diff] [blame] | 172 | mBackgroundWidth = -1; |
| 173 | mBackgroundHeight = -1; |
Chet Haase | c61d70e | 2012-10-10 15:41:57 -0700 | [diff] [blame] | 174 | mWallpaperManager.forgetLoadedWallpaper(); |
| 175 | } |
| 176 | } |
| 177 | |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 178 | @Override |
Dianne Hackborn | 759a39e | 2009-08-09 17:20:27 -0700 | [diff] [blame] | 179 | public void onCreate(SurfaceHolder surfaceHolder) { |
Jeff Brown | 30bc34f | 2011-01-25 12:56:56 -0800 | [diff] [blame] | 180 | if (DEBUG) { |
| 181 | Log.d(TAG, "onCreate"); |
| 182 | } |
| 183 | |
Dianne Hackborn | 759a39e | 2009-08-09 17:20:27 -0700 | [diff] [blame] | 184 | super.onCreate(surfaceHolder); |
John Spurlock | 209bede | 2013-07-17 12:23:27 -0400 | [diff] [blame] | 185 | |
Adrian Roos | 9b9947d | 2015-06-09 16:46:05 -0700 | [diff] [blame] | 186 | mDefaultDisplay = getSystemService(WindowManager.class).getDefaultDisplay(); |
| 187 | |
| 188 | updateSurfaceSize(surfaceHolder, getDefaultDisplayInfo()); |
Chet Haase | a8e5a2b | 2011-10-28 13:18:16 -0700 | [diff] [blame] | 189 | |
| 190 | setOffsetNotificationsEnabled(false); |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | @Override |
Dianne Hackborn | 284ac93 | 2009-08-28 10:34:25 -0700 | [diff] [blame] | 194 | public void onDestroy() { |
| 195 | super.onDestroy(); |
jshe32X | 7670b2b | 2013-10-24 12:57:09 +0800 | [diff] [blame] | 196 | mBackground = null; |
Adrian Roos | 2acaeda | 2014-11-12 18:02:42 +0100 | [diff] [blame] | 197 | mWallpaperManager.forgetLoadedWallpaper(); |
Dianne Hackborn | 284ac93 | 2009-08-28 10:34:25 -0700 | [diff] [blame] | 198 | } |
| 199 | |
Adrian Roos | 9b9947d | 2015-06-09 16:46:05 -0700 | [diff] [blame] | 200 | void updateSurfaceSize(SurfaceHolder surfaceHolder, DisplayInfo displayInfo) { |
Michael Jurka | 824a4b5 | 2013-12-18 17:10:16 +0100 | [diff] [blame] | 201 | // Load background image dimensions, if we haven't saved them yet |
| 202 | if (mBackgroundWidth <= 0 || mBackgroundHeight <= 0) { |
| 203 | // Need to load the image to get dimensions |
| 204 | mWallpaperManager.forgetLoadedWallpaper(); |
| 205 | updateWallpaperLocked(); |
| 206 | if (mBackgroundWidth <= 0 || mBackgroundHeight <= 0) { |
| 207 | // Default to the display size if we can't find the dimensions |
Adrian Roos | 9b9947d | 2015-06-09 16:46:05 -0700 | [diff] [blame] | 208 | mBackgroundWidth = displayInfo.logicalWidth; |
| 209 | mBackgroundHeight = displayInfo.logicalHeight; |
Michael Jurka | 824a4b5 | 2013-12-18 17:10:16 +0100 | [diff] [blame] | 210 | } |
| 211 | } |
| 212 | |
| 213 | // Force the wallpaper to cover the screen in both dimensions |
Adrian Roos | 9b9947d | 2015-06-09 16:46:05 -0700 | [diff] [blame] | 214 | int surfaceWidth = Math.max(displayInfo.logicalWidth, mBackgroundWidth); |
| 215 | int surfaceHeight = Math.max(displayInfo.logicalHeight, mBackgroundHeight); |
Michael Jurka | 824a4b5 | 2013-12-18 17:10:16 +0100 | [diff] [blame] | 216 | |
Dianne Hackborn | ba39839 | 2011-08-01 16:11:57 -0700 | [diff] [blame] | 217 | if (FIXED_SIZED_SURFACE) { |
| 218 | // Used a fixed size surface, because we are special. We can do |
| 219 | // this because we know the current design of window animations doesn't |
| 220 | // cause this to break. |
Michael Jurka | 824a4b5 | 2013-12-18 17:10:16 +0100 | [diff] [blame] | 221 | surfaceHolder.setFixedSize(surfaceWidth, surfaceHeight); |
Adrian Roos | 30a7dfa | 2015-07-17 12:29:53 -0700 | [diff] [blame] | 222 | mLastRequestedWidth = surfaceWidth; |
| 223 | mLastRequestedHeight = surfaceHeight; |
Dianne Hackborn | ba39839 | 2011-08-01 16:11:57 -0700 | [diff] [blame] | 224 | } else { |
| 225 | surfaceHolder.setSizeFromLayout(); |
| 226 | } |
Dianne Hackborn | ac1471a | 2011-02-03 13:46:06 -0800 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | @Override |
Dianne Hackborn | 759a39e | 2009-08-09 17:20:27 -0700 | [diff] [blame] | 230 | public void onVisibilityChanged(boolean visible) { |
Jeff Brown | 30bc34f | 2011-01-25 12:56:56 -0800 | [diff] [blame] | 231 | if (DEBUG) { |
Chet Haase | 2f20081 | 2012-10-11 12:49:08 -0700 | [diff] [blame] | 232 | Log.d(TAG, "onVisibilityChanged: mVisible, visible=" + mVisible + ", " + visible); |
Jeff Brown | 30bc34f | 2011-01-25 12:56:56 -0800 | [diff] [blame] | 233 | } |
| 234 | |
Craig Mautner | b1ef369 | 2012-11-16 17:31:04 -0800 | [diff] [blame] | 235 | if (mVisible != visible) { |
| 236 | if (DEBUG) { |
| 237 | Log.d(TAG, "Visibility changed to visible=" + visible); |
Jeff Brown | 30bc34f | 2011-01-25 12:56:56 -0800 | [diff] [blame] | 238 | } |
Craig Mautner | b1ef369 | 2012-11-16 17:31:04 -0800 | [diff] [blame] | 239 | mVisible = visible; |
longyu.huang | 6b53fd2 | 2015-05-06 01:46:23 -0700 | [diff] [blame] | 240 | if (visible) { |
| 241 | drawFrame(); |
| 242 | } |
Jeff Brown | fa2e504 | 2011-01-23 13:14:23 -0800 | [diff] [blame] | 243 | } |
Dianne Hackborn | 759a39e | 2009-08-09 17:20:27 -0700 | [diff] [blame] | 244 | } |
Jeff Brown | fa2e504 | 2011-01-23 13:14:23 -0800 | [diff] [blame] | 245 | |
Dianne Hackborn | 759a39e | 2009-08-09 17:20:27 -0700 | [diff] [blame] | 246 | @Override |
Dianne Hackborn | 8df8b2b | 2009-08-17 15:15:18 -0700 | [diff] [blame] | 247 | public void onTouchEvent(MotionEvent event) { |
| 248 | super.onTouchEvent(event); |
Dianne Hackborn | 8df8b2b | 2009-08-17 15:15:18 -0700 | [diff] [blame] | 249 | } |
| 250 | |
| 251 | @Override |
Dianne Hackborn | 72c82ab | 2009-08-11 21:13:54 -0700 | [diff] [blame] | 252 | public void onOffsetsChanged(float xOffset, float yOffset, |
Marco Nelissen | bf6956b | 2009-11-09 15:21:13 -0800 | [diff] [blame] | 253 | float xOffsetStep, float yOffsetStep, |
Dianne Hackborn | 72c82ab | 2009-08-11 21:13:54 -0700 | [diff] [blame] | 254 | int xPixels, int yPixels) { |
Jeff Brown | 30bc34f | 2011-01-25 12:56:56 -0800 | [diff] [blame] | 255 | if (DEBUG) { |
| 256 | Log.d(TAG, "onOffsetsChanged: xOffset=" + xOffset + ", yOffset=" + yOffset |
| 257 | + ", xOffsetStep=" + xOffsetStep + ", yOffsetStep=" + yOffsetStep |
| 258 | + ", xPixels=" + xPixels + ", yPixels=" + yPixels); |
| 259 | } |
| 260 | |
Craig Mautner | b1ef369 | 2012-11-16 17:31:04 -0800 | [diff] [blame] | 261 | if (mXOffset != xOffset || mYOffset != yOffset) { |
| 262 | if (DEBUG) { |
| 263 | Log.d(TAG, "Offsets changed to (" + xOffset + "," + yOffset + ")."); |
Jeff Brown | fa2e504 | 2011-01-23 13:14:23 -0800 | [diff] [blame] | 264 | } |
Craig Mautner | b1ef369 | 2012-11-16 17:31:04 -0800 | [diff] [blame] | 265 | mXOffset = xOffset; |
| 266 | mYOffset = yOffset; |
| 267 | mOffsetsChanged = true; |
Jeff Brown | fa2e504 | 2011-01-23 13:14:23 -0800 | [diff] [blame] | 268 | } |
Craig Mautner | b1ef369 | 2012-11-16 17:31:04 -0800 | [diff] [blame] | 269 | drawFrame(); |
Dianne Hackborn | 72c82ab | 2009-08-11 21:13:54 -0700 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | @Override |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 273 | public void onSurfaceChanged(SurfaceHolder holder, int format, int width, int height) { |
Jeff Brown | 30bc34f | 2011-01-25 12:56:56 -0800 | [diff] [blame] | 274 | if (DEBUG) { |
| 275 | Log.d(TAG, "onSurfaceChanged: width=" + width + ", height=" + height); |
| 276 | } |
| 277 | |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 278 | super.onSurfaceChanged(holder, format, width, height); |
Jeff Brown | 30bc34f | 2011-01-25 12:56:56 -0800 | [diff] [blame] | 279 | |
Craig Mautner | b1ef369 | 2012-11-16 17:31:04 -0800 | [diff] [blame] | 280 | drawFrame(); |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 281 | } |
| 282 | |
Craig Mautner | fb729c7 | 2012-10-01 09:39:43 -0700 | [diff] [blame] | 283 | @Override |
Chet Haase | 2f20081 | 2012-10-11 12:49:08 -0700 | [diff] [blame] | 284 | public void onSurfaceDestroyed(SurfaceHolder holder) { |
| 285 | super.onSurfaceDestroyed(holder); |
Chet Haase | 5f0d976 | 2012-10-18 12:01:34 -0700 | [diff] [blame] | 286 | mLastSurfaceWidth = mLastSurfaceHeight = -1; |
Chet Haase | 2f20081 | 2012-10-11 12:49:08 -0700 | [diff] [blame] | 287 | } |
| 288 | |
| 289 | @Override |
| 290 | public void onSurfaceCreated(SurfaceHolder holder) { |
| 291 | super.onSurfaceCreated(holder); |
Chet Haase | 5f0d976 | 2012-10-18 12:01:34 -0700 | [diff] [blame] | 292 | mLastSurfaceWidth = mLastSurfaceHeight = -1; |
Chet Haase | 2f20081 | 2012-10-11 12:49:08 -0700 | [diff] [blame] | 293 | } |
| 294 | |
| 295 | @Override |
Craig Mautner | fb729c7 | 2012-10-01 09:39:43 -0700 | [diff] [blame] | 296 | public void onSurfaceRedrawNeeded(SurfaceHolder holder) { |
| 297 | if (DEBUG) { |
Chet Haase | 2f20081 | 2012-10-11 12:49:08 -0700 | [diff] [blame] | 298 | Log.d(TAG, "onSurfaceRedrawNeeded"); |
Craig Mautner | fb729c7 | 2012-10-01 09:39:43 -0700 | [diff] [blame] | 299 | } |
| 300 | super.onSurfaceRedrawNeeded(holder); |
| 301 | |
Craig Mautner | b1ef369 | 2012-11-16 17:31:04 -0800 | [diff] [blame] | 302 | drawFrame(); |
Craig Mautner | fb729c7 | 2012-10-01 09:39:43 -0700 | [diff] [blame] | 303 | } |
| 304 | |
Adrian Roos | 9b9947d | 2015-06-09 16:46:05 -0700 | [diff] [blame] | 305 | private DisplayInfo getDefaultDisplayInfo() { |
| 306 | mDefaultDisplay.getDisplayInfo(mTmpDisplayInfo); |
| 307 | return mTmpDisplayInfo; |
Michael Jurka | 824a4b5 | 2013-12-18 17:10:16 +0100 | [diff] [blame] | 308 | } |
| 309 | |
Craig Mautner | b1ef369 | 2012-11-16 17:31:04 -0800 | [diff] [blame] | 310 | void drawFrame() { |
Adrian Roos | 2acaeda | 2014-11-12 18:02:42 +0100 | [diff] [blame] | 311 | try { |
Adrian Roos | 9b9947d | 2015-06-09 16:46:05 -0700 | [diff] [blame] | 312 | DisplayInfo displayInfo = getDefaultDisplayInfo(); |
| 313 | int newRotation = displayInfo.rotation; |
Michael Jurka | 824a4b5 | 2013-12-18 17:10:16 +0100 | [diff] [blame] | 314 | |
Adrian Roos | 2acaeda | 2014-11-12 18:02:42 +0100 | [diff] [blame] | 315 | // Sometimes a wallpaper is not large enough to cover the screen in one dimension. |
| 316 | // Call updateSurfaceSize -- it will only actually do the update if the dimensions |
| 317 | // should change |
| 318 | if (newRotation != mLastRotation) { |
| 319 | // Update surface size (if necessary) |
Adrian Roos | 9b9947d | 2015-06-09 16:46:05 -0700 | [diff] [blame] | 320 | updateSurfaceSize(getSurfaceHolder(), displayInfo); |
Adrian Roos | ab6edbf | 2015-07-07 17:29:15 -0700 | [diff] [blame] | 321 | mRotationAtLastSurfaceSizeUpdate = newRotation; |
| 322 | mDisplayWidthAtLastSurfaceSizeUpdate = displayInfo.logicalWidth; |
| 323 | mDisplayHeightAtLastSurfaceSizeUpdate = displayInfo.logicalHeight; |
Chet Haase | 2f20081 | 2012-10-11 12:49:08 -0700 | [diff] [blame] | 324 | } |
Adrian Roos | 2acaeda | 2014-11-12 18:02:42 +0100 | [diff] [blame] | 325 | SurfaceHolder sh = getSurfaceHolder(); |
| 326 | final Rect frame = sh.getSurfaceFrame(); |
| 327 | final int dw = frame.width(); |
| 328 | final int dh = frame.height(); |
| 329 | boolean surfaceDimensionsChanged = dw != mLastSurfaceWidth |
| 330 | || dh != mLastSurfaceHeight; |
Chet Haase | 2f20081 | 2012-10-11 12:49:08 -0700 | [diff] [blame] | 331 | |
Adrian Roos | 2acaeda | 2014-11-12 18:02:42 +0100 | [diff] [blame] | 332 | boolean redrawNeeded = surfaceDimensionsChanged || newRotation != mLastRotation; |
| 333 | if (!redrawNeeded && !mOffsetsChanged) { |
Chet Haase | 5f0d976 | 2012-10-18 12:01:34 -0700 | [diff] [blame] | 334 | if (DEBUG) { |
Adrian Roos | 2acaeda | 2014-11-12 18:02:42 +0100 | [diff] [blame] | 335 | Log.d(TAG, "Suppressed drawFrame since redraw is not needed " |
| 336 | + "and offsets have not changed."); |
Chet Haase | 5f0d976 | 2012-10-18 12:01:34 -0700 | [diff] [blame] | 337 | } |
| 338 | return; |
| 339 | } |
Adrian Roos | 2acaeda | 2014-11-12 18:02:42 +0100 | [diff] [blame] | 340 | mLastRotation = newRotation; |
| 341 | |
| 342 | // Load bitmap if it is not yet loaded or if it was loaded at a different size |
| 343 | if (mBackground == null || surfaceDimensionsChanged) { |
| 344 | if (DEBUG) { |
| 345 | Log.d(TAG, "Reloading bitmap: mBackground, bgw, bgh, dw, dh = " + |
| 346 | mBackground + ", " + |
| 347 | ((mBackground == null) ? 0 : mBackground.getWidth()) + ", " + |
| 348 | ((mBackground == null) ? 0 : mBackground.getHeight()) + ", " + |
| 349 | dw + ", " + dh); |
| 350 | } |
| 351 | mWallpaperManager.forgetLoadedWallpaper(); |
| 352 | updateWallpaperLocked(); |
| 353 | if (mBackground == null) { |
| 354 | if (DEBUG) { |
| 355 | Log.d(TAG, "Unable to load bitmap"); |
| 356 | } |
| 357 | return; |
| 358 | } |
| 359 | if (DEBUG) { |
| 360 | if (dw != mBackground.getWidth() || dh != mBackground.getHeight()) { |
| 361 | Log.d(TAG, "Surface != bitmap dimensions: surface w/h, bitmap w/h: " + |
| 362 | dw + ", " + dh + ", " + mBackground.getWidth() + ", " + |
| 363 | mBackground.getHeight()); |
| 364 | } |
Chet Haase | 5f0d976 | 2012-10-18 12:01:34 -0700 | [diff] [blame] | 365 | } |
| 366 | } |
Chet Haase | 2f20081 | 2012-10-11 12:49:08 -0700 | [diff] [blame] | 367 | |
Adrian Roos | 2acaeda | 2014-11-12 18:02:42 +0100 | [diff] [blame] | 368 | // Center the scaled image |
| 369 | mScale = Math.max(1f, Math.max(dw / (float) mBackground.getWidth(), |
| 370 | dh / (float) mBackground.getHeight())); |
| 371 | final int availw = dw - (int) (mBackground.getWidth() * mScale); |
| 372 | final int availh = dh - (int) (mBackground.getHeight() * mScale); |
| 373 | int xPixels = availw / 2; |
| 374 | int yPixels = availh / 2; |
Michael Jurka | 824a4b5 | 2013-12-18 17:10:16 +0100 | [diff] [blame] | 375 | |
Adrian Roos | 2acaeda | 2014-11-12 18:02:42 +0100 | [diff] [blame] | 376 | // Adjust the image for xOffset/yOffset values. If window manager is handling offsets, |
| 377 | // mXOffset and mYOffset are set to 0.5f by default and therefore xPixels and yPixels |
| 378 | // will remain unchanged |
| 379 | final int availwUnscaled = dw - mBackground.getWidth(); |
| 380 | final int availhUnscaled = dh - mBackground.getHeight(); |
| 381 | if (availwUnscaled < 0) |
| 382 | xPixels += (int) (availwUnscaled * (mXOffset - .5f) + .5f); |
| 383 | if (availhUnscaled < 0) |
| 384 | yPixels += (int) (availhUnscaled * (mYOffset - .5f) + .5f); |
Jeff Brown | 033f63a | 2011-01-23 22:01:49 -0800 | [diff] [blame] | 385 | |
Adrian Roos | 2acaeda | 2014-11-12 18:02:42 +0100 | [diff] [blame] | 386 | mOffsetsChanged = false; |
Adrian Roos | 2acaeda | 2014-11-12 18:02:42 +0100 | [diff] [blame] | 387 | if (surfaceDimensionsChanged) { |
| 388 | mLastSurfaceWidth = dw; |
| 389 | mLastSurfaceHeight = dh; |
Jeff Brown | 033f63a | 2011-01-23 22:01:49 -0800 | [diff] [blame] | 390 | } |
Adrian Roos | 2acaeda | 2014-11-12 18:02:42 +0100 | [diff] [blame] | 391 | if (!redrawNeeded && xPixels == mLastXTranslation && yPixels == mLastYTranslation) { |
| 392 | if (DEBUG) { |
| 393 | Log.d(TAG, "Suppressed drawFrame since the image has not " |
| 394 | + "actually moved an integral number of pixels."); |
| 395 | } |
| 396 | return; |
| 397 | } |
| 398 | mLastXTranslation = xPixels; |
| 399 | mLastYTranslation = yPixels; |
Jeff Brown | 033f63a | 2011-01-23 22:01:49 -0800 | [diff] [blame] | 400 | |
Adrian Roos | 2acaeda | 2014-11-12 18:02:42 +0100 | [diff] [blame] | 401 | if (DEBUG) { |
| 402 | Log.d(TAG, "Redrawing wallpaper"); |
| 403 | } |
Wim Vander Schelden | 9549c06 | 2013-02-07 15:51:51 +0000 | [diff] [blame] | 404 | |
Adrian Roos | 2acaeda | 2014-11-12 18:02:42 +0100 | [diff] [blame] | 405 | if (mIsHwAccelerated) { |
| 406 | if (!drawWallpaperWithOpenGL(sh, availw, availh, xPixels, yPixels)) { |
| 407 | drawWallpaperWithCanvas(sh, availw, availh, xPixels, yPixels); |
| 408 | } |
| 409 | } else { |
Romain Guy | f929629 | 2011-08-25 17:00:39 -0700 | [diff] [blame] | 410 | drawWallpaperWithCanvas(sh, availw, availh, xPixels, yPixels); |
| 411 | } |
Adrian Roos | 2acaeda | 2014-11-12 18:02:42 +0100 | [diff] [blame] | 412 | } finally { |
| 413 | if (FIXED_SIZED_SURFACE && !mIsHwAccelerated) { |
Chet Haase | c61d70e | 2012-10-10 15:41:57 -0700 | [diff] [blame] | 414 | // If the surface is fixed-size, we should only need to |
| 415 | // draw it once and then we'll let the window manager |
| 416 | // position it appropriately. As such, we no longer needed |
| 417 | // the loaded bitmap. Yay! |
Adrian Roos | 2acaeda | 2014-11-12 18:02:42 +0100 | [diff] [blame] | 418 | // hw-accelerated renderer retains bitmap for faster rotation |
Chet Haase | c61d70e | 2012-10-10 15:41:57 -0700 | [diff] [blame] | 419 | mBackground = null; |
| 420 | mWallpaperManager.forgetLoadedWallpaper(); |
| 421 | } |
Dianne Hackborn | 759a39e | 2009-08-09 17:20:27 -0700 | [diff] [blame] | 422 | } |
Dianne Hackborn | 759a39e | 2009-08-09 17:20:27 -0700 | [diff] [blame] | 423 | } |
Romain Guy | ef654bd | 2009-08-11 19:12:17 -0700 | [diff] [blame] | 424 | |
Chet Haase | 5f0d976 | 2012-10-18 12:01:34 -0700 | [diff] [blame] | 425 | private void updateWallpaperLocked() { |
Jeff Brown | fa2e504 | 2011-01-23 13:14:23 -0800 | [diff] [blame] | 426 | Throwable exception = null; |
| 427 | try { |
Chet Haase | 589a6af | 2012-10-24 17:37:00 -0700 | [diff] [blame] | 428 | mBackground = null; |
Michael Jurka | 824a4b5 | 2013-12-18 17:10:16 +0100 | [diff] [blame] | 429 | mBackgroundWidth = -1; |
| 430 | mBackgroundHeight = -1; |
Romain Guy | 407ec78 | 2011-08-24 17:06:58 -0700 | [diff] [blame] | 431 | mBackground = mWallpaperManager.getBitmap(); |
Michael Jurka | 824a4b5 | 2013-12-18 17:10:16 +0100 | [diff] [blame] | 432 | mBackgroundWidth = mBackground.getWidth(); |
| 433 | mBackgroundHeight = mBackground.getHeight(); |
Jeff Brown | fa2e504 | 2011-01-23 13:14:23 -0800 | [diff] [blame] | 434 | } catch (RuntimeException e) { |
| 435 | exception = e; |
| 436 | } catch (OutOfMemoryError e) { |
| 437 | exception = e; |
| 438 | } |
Romain Guy | 407ec78 | 2011-08-24 17:06:58 -0700 | [diff] [blame] | 439 | |
Jeff Brown | fa2e504 | 2011-01-23 13:14:23 -0800 | [diff] [blame] | 440 | if (exception != null) { |
| 441 | mBackground = null; |
Michael Jurka | 824a4b5 | 2013-12-18 17:10:16 +0100 | [diff] [blame] | 442 | mBackgroundWidth = -1; |
| 443 | mBackgroundHeight = -1; |
Jeff Brown | fa2e504 | 2011-01-23 13:14:23 -0800 | [diff] [blame] | 444 | // Note that if we do fail at this, and the default wallpaper can't |
| 445 | // be loaded, we will go into a cycle. Don't do a build where the |
| 446 | // default wallpaper can't be loaded. |
| 447 | Log.w(TAG, "Unable to load wallpaper!", exception); |
Dianne Hackborn | c9dbbe2 | 2009-11-11 22:50:37 -0800 | [diff] [blame] | 448 | try { |
Jeff Brown | fa2e504 | 2011-01-23 13:14:23 -0800 | [diff] [blame] | 449 | mWallpaperManager.clear(); |
| 450 | } catch (IOException ex) { |
| 451 | // now we're really screwed. |
| 452 | Log.w(TAG, "Unable reset to default wallpaper!", ex); |
Dianne Hackborn | c9dbbe2 | 2009-11-11 22:50:37 -0800 | [diff] [blame] | 453 | } |
Romain Guy | ef654bd | 2009-08-11 19:12:17 -0700 | [diff] [blame] | 454 | } |
Romain Guy | 407ec78 | 2011-08-24 17:06:58 -0700 | [diff] [blame] | 455 | } |
| 456 | |
Adrian Roos | ab6edbf | 2015-07-07 17:29:15 -0700 | [diff] [blame] | 457 | @Override |
| 458 | protected void dump(String prefix, FileDescriptor fd, PrintWriter out, String[] args) { |
| 459 | super.dump(prefix, fd, out, args); |
| 460 | |
| 461 | out.print(prefix); out.println("ImageWallpaper.DrawableEngine:"); |
| 462 | out.print(prefix); out.print(" mBackground="); out.print(mBackground); |
| 463 | out.print(" mBackgroundWidth="); out.print(mBackgroundWidth); |
| 464 | out.print(" mBackgroundHeight="); out.println(mBackgroundHeight); |
| 465 | |
| 466 | out.print(prefix); out.print(" mLastRotation="); out.print(mLastRotation); |
| 467 | out.print(" mLastSurfaceWidth="); out.print(mLastSurfaceWidth); |
| 468 | out.print(" mLastSurfaceHeight="); out.println(mLastSurfaceHeight); |
| 469 | |
| 470 | out.print(prefix); out.print(" mXOffset="); out.print(mXOffset); |
| 471 | out.print(" mYOffset="); out.println(mYOffset); |
| 472 | |
| 473 | out.print(prefix); out.print(" mVisible="); out.print(mVisible); |
Adrian Roos | ab6edbf | 2015-07-07 17:29:15 -0700 | [diff] [blame] | 474 | out.print(" mOffsetsChanged="); out.println(mOffsetsChanged); |
| 475 | |
| 476 | out.print(prefix); out.print(" mLastXTranslation="); out.print(mLastXTranslation); |
| 477 | out.print(" mLastYTranslation="); out.print(mLastYTranslation); |
| 478 | out.print(" mScale="); out.println(mScale); |
| 479 | |
Adrian Roos | 30a7dfa | 2015-07-17 12:29:53 -0700 | [diff] [blame] | 480 | out.print(prefix); out.print(" mLastRequestedWidth="); out.print(mLastRequestedWidth); |
| 481 | out.print(" mLastRequestedHeight="); out.println(mLastRequestedHeight); |
| 482 | |
Adrian Roos | ab6edbf | 2015-07-07 17:29:15 -0700 | [diff] [blame] | 483 | out.print(prefix); out.println(" DisplayInfo at last updateSurfaceSize:"); |
| 484 | out.print(prefix); |
| 485 | out.print(" rotation="); out.print(mRotationAtLastSurfaceSizeUpdate); |
| 486 | out.print(" width="); out.print(mDisplayWidthAtLastSurfaceSizeUpdate); |
| 487 | out.print(" height="); out.println(mDisplayHeightAtLastSurfaceSizeUpdate); |
| 488 | } |
| 489 | |
Michael Jurka | 824a4b5 | 2013-12-18 17:10:16 +0100 | [diff] [blame] | 490 | private void drawWallpaperWithCanvas(SurfaceHolder sh, int w, int h, int left, int top) { |
Romain Guy | 407ec78 | 2011-08-24 17:06:58 -0700 | [diff] [blame] | 491 | Canvas c = sh.lockCanvas(); |
| 492 | if (c != null) { |
| 493 | try { |
| 494 | if (DEBUG) { |
Michael Jurka | 824a4b5 | 2013-12-18 17:10:16 +0100 | [diff] [blame] | 495 | Log.d(TAG, "Redrawing: left=" + left + ", top=" + top); |
Romain Guy | 407ec78 | 2011-08-24 17:06:58 -0700 | [diff] [blame] | 496 | } |
| 497 | |
Michael Jurka | 824a4b5 | 2013-12-18 17:10:16 +0100 | [diff] [blame] | 498 | final float right = left + mBackground.getWidth() * mScale; |
| 499 | final float bottom = top + mBackground.getHeight() * mScale; |
Romain Guy | 407ec78 | 2011-08-24 17:06:58 -0700 | [diff] [blame] | 500 | if (w < 0 || h < 0) { |
| 501 | c.save(Canvas.CLIP_SAVE_FLAG); |
Michael Jurka | 824a4b5 | 2013-12-18 17:10:16 +0100 | [diff] [blame] | 502 | c.clipRect(left, top, right, bottom, |
Chet Haase | 5f0d976 | 2012-10-18 12:01:34 -0700 | [diff] [blame] | 503 | Op.DIFFERENCE); |
Romain Guy | 407ec78 | 2011-08-24 17:06:58 -0700 | [diff] [blame] | 504 | c.drawColor(0xff000000); |
| 505 | c.restore(); |
| 506 | } |
| 507 | if (mBackground != null) { |
Michael Jurka | 824a4b5 | 2013-12-18 17:10:16 +0100 | [diff] [blame] | 508 | RectF dest = new RectF(left, top, right, bottom); |
| 509 | // add a filter bitmap? |
| 510 | c.drawBitmap(mBackground, null, dest, null); |
Romain Guy | 407ec78 | 2011-08-24 17:06:58 -0700 | [diff] [blame] | 511 | } |
| 512 | } finally { |
| 513 | sh.unlockCanvasAndPost(c); |
| 514 | } |
| 515 | } |
| 516 | } |
| 517 | |
Romain Guy | f929629 | 2011-08-25 17:00:39 -0700 | [diff] [blame] | 518 | private boolean drawWallpaperWithOpenGL(SurfaceHolder sh, int w, int h, int left, int top) { |
| 519 | if (!initGL(sh)) return false; |
Romain Guy | 407ec78 | 2011-08-24 17:06:58 -0700 | [diff] [blame] | 520 | |
Michael Jurka | 824a4b5 | 2013-12-18 17:10:16 +0100 | [diff] [blame] | 521 | final float right = left + mBackground.getWidth() * mScale; |
| 522 | final float bottom = top + mBackground.getHeight() * mScale; |
Romain Guy | 407ec78 | 2011-08-24 17:06:58 -0700 | [diff] [blame] | 523 | |
| 524 | final Rect frame = sh.getSurfaceFrame(); |
Romain Guy | 407ec78 | 2011-08-24 17:06:58 -0700 | [diff] [blame] | 525 | final Matrix4f ortho = new Matrix4f(); |
| 526 | ortho.loadOrtho(0.0f, frame.width(), frame.height(), 0.0f, -1.0f, 1.0f); |
| 527 | |
| 528 | final FloatBuffer triangleVertices = createMesh(left, top, right, bottom); |
| 529 | |
| 530 | final int texture = loadTexture(mBackground); |
| 531 | final int program = buildProgram(sSimpleVS, sSimpleFS); |
Chet Haase | 2f20081 | 2012-10-11 12:49:08 -0700 | [diff] [blame] | 532 | |
Romain Guy | 407ec78 | 2011-08-24 17:06:58 -0700 | [diff] [blame] | 533 | final int attribPosition = glGetAttribLocation(program, "position"); |
| 534 | final int attribTexCoords = glGetAttribLocation(program, "texCoords"); |
| 535 | final int uniformTexture = glGetUniformLocation(program, "texture"); |
| 536 | final int uniformProjection = glGetUniformLocation(program, "projection"); |
| 537 | |
| 538 | checkGlError(); |
| 539 | |
| 540 | glViewport(0, 0, frame.width(), frame.height()); |
| 541 | glBindTexture(GL_TEXTURE_2D, texture); |
| 542 | |
| 543 | glUseProgram(program); |
| 544 | glEnableVertexAttribArray(attribPosition); |
| 545 | glEnableVertexAttribArray(attribTexCoords); |
| 546 | glUniform1i(uniformTexture, 0); |
| 547 | glUniformMatrix4fv(uniformProjection, 1, false, ortho.getArray(), 0); |
| 548 | |
| 549 | checkGlError(); |
| 550 | |
Michael Wright | 6762a44 | 2013-10-15 14:43:32 -0700 | [diff] [blame] | 551 | if (w > 0 || h > 0) { |
Romain Guy | 407ec78 | 2011-08-24 17:06:58 -0700 | [diff] [blame] | 552 | glClearColor(0.0f, 0.0f, 0.0f, 0.0f); |
| 553 | glClear(GL_COLOR_BUFFER_BIT); |
| 554 | } |
Chet Haase | 2f20081 | 2012-10-11 12:49:08 -0700 | [diff] [blame] | 555 | |
Romain Guy | 407ec78 | 2011-08-24 17:06:58 -0700 | [diff] [blame] | 556 | // drawQuad |
| 557 | triangleVertices.position(TRIANGLE_VERTICES_DATA_POS_OFFSET); |
| 558 | glVertexAttribPointer(attribPosition, 3, GL_FLOAT, false, |
| 559 | TRIANGLE_VERTICES_DATA_STRIDE_BYTES, triangleVertices); |
| 560 | |
| 561 | triangleVertices.position(TRIANGLE_VERTICES_DATA_UV_OFFSET); |
| 562 | glVertexAttribPointer(attribTexCoords, 3, GL_FLOAT, false, |
| 563 | TRIANGLE_VERTICES_DATA_STRIDE_BYTES, triangleVertices); |
| 564 | |
| 565 | glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); |
Chet Haase | 2f20081 | 2012-10-11 12:49:08 -0700 | [diff] [blame] | 566 | |
Romain Guy | c8d983f | 2013-02-20 10:05:36 -0800 | [diff] [blame] | 567 | boolean status = mEgl.eglSwapBuffers(mEglDisplay, mEglSurface); |
Romain Guy | 407ec78 | 2011-08-24 17:06:58 -0700 | [diff] [blame] | 568 | checkEglError(); |
Chet Haase | 2f20081 | 2012-10-11 12:49:08 -0700 | [diff] [blame] | 569 | |
Michael Wright | 2ed0513 | 2015-01-08 14:30:47 -0800 | [diff] [blame] | 570 | finishGL(texture, program); |
Romain Guy | f929629 | 2011-08-25 17:00:39 -0700 | [diff] [blame] | 571 | |
Romain Guy | c8d983f | 2013-02-20 10:05:36 -0800 | [diff] [blame] | 572 | return status; |
Romain Guy | 407ec78 | 2011-08-24 17:06:58 -0700 | [diff] [blame] | 573 | } |
| 574 | |
| 575 | private FloatBuffer createMesh(int left, int top, float right, float bottom) { |
| 576 | final float[] verticesData = { |
| 577 | // X, Y, Z, U, V |
| 578 | left, bottom, 0.0f, 0.0f, 1.0f, |
| 579 | right, bottom, 0.0f, 1.0f, 1.0f, |
| 580 | left, top, 0.0f, 0.0f, 0.0f, |
| 581 | right, top, 0.0f, 1.0f, 0.0f, |
| 582 | }; |
| 583 | |
| 584 | final int bytes = verticesData.length * FLOAT_SIZE_BYTES; |
| 585 | final FloatBuffer triangleVertices = ByteBuffer.allocateDirect(bytes).order( |
| 586 | ByteOrder.nativeOrder()).asFloatBuffer(); |
| 587 | triangleVertices.put(verticesData).position(0); |
| 588 | return triangleVertices; |
| 589 | } |
| 590 | |
| 591 | private int loadTexture(Bitmap bitmap) { |
| 592 | int[] textures = new int[1]; |
John Spurlock | 209bede | 2013-07-17 12:23:27 -0400 | [diff] [blame] | 593 | |
Romain Guy | 407ec78 | 2011-08-24 17:06:58 -0700 | [diff] [blame] | 594 | glActiveTexture(GL_TEXTURE0); |
| 595 | glGenTextures(1, textures, 0); |
| 596 | checkGlError(); |
John Spurlock | 209bede | 2013-07-17 12:23:27 -0400 | [diff] [blame] | 597 | |
Romain Guy | 407ec78 | 2011-08-24 17:06:58 -0700 | [diff] [blame] | 598 | int texture = textures[0]; |
| 599 | glBindTexture(GL_TEXTURE_2D, texture); |
| 600 | checkGlError(); |
John Spurlock | 209bede | 2013-07-17 12:23:27 -0400 | [diff] [blame] | 601 | |
Romain Guy | 407ec78 | 2011-08-24 17:06:58 -0700 | [diff] [blame] | 602 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); |
| 603 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); |
John Spurlock | 209bede | 2013-07-17 12:23:27 -0400 | [diff] [blame] | 604 | |
Romain Guy | 407ec78 | 2011-08-24 17:06:58 -0700 | [diff] [blame] | 605 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); |
| 606 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); |
John Spurlock | 209bede | 2013-07-17 12:23:27 -0400 | [diff] [blame] | 607 | |
Romain Guy | 407ec78 | 2011-08-24 17:06:58 -0700 | [diff] [blame] | 608 | GLUtils.texImage2D(GL_TEXTURE_2D, 0, GL_RGBA, bitmap, GL_UNSIGNED_BYTE, 0); |
| 609 | checkGlError(); |
| 610 | |
Romain Guy | 407ec78 | 2011-08-24 17:06:58 -0700 | [diff] [blame] | 611 | return texture; |
| 612 | } |
John Spurlock | 209bede | 2013-07-17 12:23:27 -0400 | [diff] [blame] | 613 | |
Romain Guy | 407ec78 | 2011-08-24 17:06:58 -0700 | [diff] [blame] | 614 | private int buildProgram(String vertex, String fragment) { |
| 615 | int vertexShader = buildShader(vertex, GL_VERTEX_SHADER); |
| 616 | if (vertexShader == 0) return 0; |
John Spurlock | 209bede | 2013-07-17 12:23:27 -0400 | [diff] [blame] | 617 | |
Romain Guy | 407ec78 | 2011-08-24 17:06:58 -0700 | [diff] [blame] | 618 | int fragmentShader = buildShader(fragment, GL_FRAGMENT_SHADER); |
| 619 | if (fragmentShader == 0) return 0; |
John Spurlock | 209bede | 2013-07-17 12:23:27 -0400 | [diff] [blame] | 620 | |
Romain Guy | 407ec78 | 2011-08-24 17:06:58 -0700 | [diff] [blame] | 621 | int program = glCreateProgram(); |
| 622 | glAttachShader(program, vertexShader); |
Romain Guy | 407ec78 | 2011-08-24 17:06:58 -0700 | [diff] [blame] | 623 | glAttachShader(program, fragmentShader); |
Romain Guy | 407ec78 | 2011-08-24 17:06:58 -0700 | [diff] [blame] | 624 | glLinkProgram(program); |
| 625 | checkGlError(); |
John Spurlock | 209bede | 2013-07-17 12:23:27 -0400 | [diff] [blame] | 626 | |
Michael Wright | 2ed0513 | 2015-01-08 14:30:47 -0800 | [diff] [blame] | 627 | glDeleteShader(vertexShader); |
| 628 | glDeleteShader(fragmentShader); |
| 629 | |
Romain Guy | 407ec78 | 2011-08-24 17:06:58 -0700 | [diff] [blame] | 630 | int[] status = new int[1]; |
| 631 | glGetProgramiv(program, GL_LINK_STATUS, status, 0); |
| 632 | if (status[0] != GL_TRUE) { |
| 633 | String error = glGetProgramInfoLog(program); |
| 634 | Log.d(GL_LOG_TAG, "Error while linking program:\n" + error); |
Romain Guy | 407ec78 | 2011-08-24 17:06:58 -0700 | [diff] [blame] | 635 | glDeleteProgram(program); |
| 636 | return 0; |
| 637 | } |
John Spurlock | 209bede | 2013-07-17 12:23:27 -0400 | [diff] [blame] | 638 | |
Romain Guy | 407ec78 | 2011-08-24 17:06:58 -0700 | [diff] [blame] | 639 | return program; |
| 640 | } |
Romain Guy | 3696779b | 2013-01-28 14:04:07 -0800 | [diff] [blame] | 641 | |
Romain Guy | 407ec78 | 2011-08-24 17:06:58 -0700 | [diff] [blame] | 642 | private int buildShader(String source, int type) { |
| 643 | int shader = glCreateShader(type); |
John Spurlock | 209bede | 2013-07-17 12:23:27 -0400 | [diff] [blame] | 644 | |
Romain Guy | 407ec78 | 2011-08-24 17:06:58 -0700 | [diff] [blame] | 645 | glShaderSource(shader, source); |
| 646 | checkGlError(); |
John Spurlock | 209bede | 2013-07-17 12:23:27 -0400 | [diff] [blame] | 647 | |
Romain Guy | 407ec78 | 2011-08-24 17:06:58 -0700 | [diff] [blame] | 648 | glCompileShader(shader); |
| 649 | checkGlError(); |
John Spurlock | 209bede | 2013-07-17 12:23:27 -0400 | [diff] [blame] | 650 | |
Romain Guy | 407ec78 | 2011-08-24 17:06:58 -0700 | [diff] [blame] | 651 | int[] status = new int[1]; |
| 652 | glGetShaderiv(shader, GL_COMPILE_STATUS, status, 0); |
| 653 | if (status[0] != GL_TRUE) { |
| 654 | String error = glGetShaderInfoLog(shader); |
| 655 | Log.d(GL_LOG_TAG, "Error while compiling shader:\n" + error); |
| 656 | glDeleteShader(shader); |
| 657 | return 0; |
| 658 | } |
John Spurlock | 209bede | 2013-07-17 12:23:27 -0400 | [diff] [blame] | 659 | |
Romain Guy | 407ec78 | 2011-08-24 17:06:58 -0700 | [diff] [blame] | 660 | return shader; |
| 661 | } |
Romain Guy | 3696779b | 2013-01-28 14:04:07 -0800 | [diff] [blame] | 662 | |
Romain Guy | 407ec78 | 2011-08-24 17:06:58 -0700 | [diff] [blame] | 663 | private void checkEglError() { |
| 664 | int error = mEgl.eglGetError(); |
| 665 | if (error != EGL_SUCCESS) { |
| 666 | Log.w(GL_LOG_TAG, "EGL error = " + GLUtils.getEGLErrorString(error)); |
| 667 | } |
| 668 | } |
Romain Guy | 3696779b | 2013-01-28 14:04:07 -0800 | [diff] [blame] | 669 | |
Romain Guy | 407ec78 | 2011-08-24 17:06:58 -0700 | [diff] [blame] | 670 | private void checkGlError() { |
| 671 | int error = glGetError(); |
| 672 | if (error != GL_NO_ERROR) { |
| 673 | Log.w(GL_LOG_TAG, "GL error = 0x" + Integer.toHexString(error), new Throwable()); |
| 674 | } |
| 675 | } |
Romain Guy | 3696779b | 2013-01-28 14:04:07 -0800 | [diff] [blame] | 676 | |
Michael Wright | 2ed0513 | 2015-01-08 14:30:47 -0800 | [diff] [blame] | 677 | private void finishGL(int texture, int program) { |
| 678 | int[] textures = new int[1]; |
| 679 | textures[0] = texture; |
| 680 | glDeleteTextures(1, textures, 0); |
| 681 | glDeleteProgram(program); |
Romain Guy | f929629 | 2011-08-25 17:00:39 -0700 | [diff] [blame] | 682 | mEgl.eglMakeCurrent(mEglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT); |
Romain Guy | 407ec78 | 2011-08-24 17:06:58 -0700 | [diff] [blame] | 683 | mEgl.eglDestroySurface(mEglDisplay, mEglSurface); |
Romain Guy | f929629 | 2011-08-25 17:00:39 -0700 | [diff] [blame] | 684 | mEgl.eglDestroyContext(mEglDisplay, mEglContext); |
Romain Guy | 3696779b | 2013-01-28 14:04:07 -0800 | [diff] [blame] | 685 | mEgl.eglTerminate(mEglDisplay); |
Romain Guy | 407ec78 | 2011-08-24 17:06:58 -0700 | [diff] [blame] | 686 | } |
Romain Guy | 3696779b | 2013-01-28 14:04:07 -0800 | [diff] [blame] | 687 | |
Romain Guy | f929629 | 2011-08-25 17:00:39 -0700 | [diff] [blame] | 688 | private boolean initGL(SurfaceHolder surfaceHolder) { |
Romain Guy | 407ec78 | 2011-08-24 17:06:58 -0700 | [diff] [blame] | 689 | mEgl = (EGL10) EGLContext.getEGL(); |
John Spurlock | 209bede | 2013-07-17 12:23:27 -0400 | [diff] [blame] | 690 | |
Romain Guy | 407ec78 | 2011-08-24 17:06:58 -0700 | [diff] [blame] | 691 | mEglDisplay = mEgl.eglGetDisplay(EGL_DEFAULT_DISPLAY); |
| 692 | if (mEglDisplay == EGL_NO_DISPLAY) { |
| 693 | throw new RuntimeException("eglGetDisplay failed " + |
| 694 | GLUtils.getEGLErrorString(mEgl.eglGetError())); |
| 695 | } |
John Spurlock | 209bede | 2013-07-17 12:23:27 -0400 | [diff] [blame] | 696 | |
Romain Guy | 407ec78 | 2011-08-24 17:06:58 -0700 | [diff] [blame] | 697 | int[] version = new int[2]; |
| 698 | if (!mEgl.eglInitialize(mEglDisplay, version)) { |
| 699 | throw new RuntimeException("eglInitialize failed " + |
| 700 | GLUtils.getEGLErrorString(mEgl.eglGetError())); |
| 701 | } |
John Spurlock | 209bede | 2013-07-17 12:23:27 -0400 | [diff] [blame] | 702 | |
Romain Guy | 407ec78 | 2011-08-24 17:06:58 -0700 | [diff] [blame] | 703 | mEglConfig = chooseEglConfig(); |
| 704 | if (mEglConfig == null) { |
| 705 | throw new RuntimeException("eglConfig not initialized"); |
| 706 | } |
John Spurlock | 209bede | 2013-07-17 12:23:27 -0400 | [diff] [blame] | 707 | |
Romain Guy | 407ec78 | 2011-08-24 17:06:58 -0700 | [diff] [blame] | 708 | mEglContext = createContext(mEgl, mEglDisplay, mEglConfig); |
Chet Haase | 187e1e2 | 2013-03-13 18:04:00 -0700 | [diff] [blame] | 709 | if (mEglContext == EGL_NO_CONTEXT) { |
| 710 | throw new RuntimeException("createContext failed " + |
| 711 | GLUtils.getEGLErrorString(mEgl.eglGetError())); |
Wim Vander Schelden | 9549c06 | 2013-02-07 15:51:51 +0000 | [diff] [blame] | 712 | } |
Chet Haase | 187e1e2 | 2013-03-13 18:04:00 -0700 | [diff] [blame] | 713 | |
Chet Haase | 2e417be | 2013-03-14 09:28:42 -0700 | [diff] [blame] | 714 | int attribs[] = { |
| 715 | EGL_WIDTH, 1, |
| 716 | EGL_HEIGHT, 1, |
| 717 | EGL_NONE |
| 718 | }; |
| 719 | EGLSurface tmpSurface = mEgl.eglCreatePbufferSurface(mEglDisplay, mEglConfig, attribs); |
| 720 | mEgl.eglMakeCurrent(mEglDisplay, tmpSurface, tmpSurface, mEglContext); |
| 721 | |
| 722 | int[] maxSize = new int[1]; |
| 723 | Rect frame = surfaceHolder.getSurfaceFrame(); |
| 724 | glGetIntegerv(GL_MAX_TEXTURE_SIZE, maxSize, 0); |
| 725 | |
| 726 | mEgl.eglMakeCurrent(mEglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT); |
| 727 | mEgl.eglDestroySurface(mEglDisplay, tmpSurface); |
| 728 | |
| 729 | if(frame.width() > maxSize[0] || frame.height() > maxSize[0]) { |
| 730 | mEgl.eglDestroyContext(mEglDisplay, mEglContext); |
| 731 | mEgl.eglTerminate(mEglDisplay); |
| 732 | Log.e(GL_LOG_TAG, "requested texture size " + |
| 733 | frame.width() + "x" + frame.height() + " exceeds the support maximum of " + |
| 734 | maxSize[0] + "x" + maxSize[0]); |
| 735 | return false; |
| 736 | } |
| 737 | |
Romain Guy | 407ec78 | 2011-08-24 17:06:58 -0700 | [diff] [blame] | 738 | mEglSurface = mEgl.eglCreateWindowSurface(mEglDisplay, mEglConfig, surfaceHolder, null); |
Romain Guy | 407ec78 | 2011-08-24 17:06:58 -0700 | [diff] [blame] | 739 | if (mEglSurface == null || mEglSurface == EGL_NO_SURFACE) { |
| 740 | int error = mEgl.eglGetError(); |
Wim Vander Schelden | 9549c06 | 2013-02-07 15:51:51 +0000 | [diff] [blame] | 741 | if (error == EGL_BAD_NATIVE_WINDOW || error == EGL_BAD_ALLOC) { |
| 742 | Log.e(GL_LOG_TAG, "createWindowSurface returned " + |
| 743 | GLUtils.getEGLErrorString(error) + "."); |
Romain Guy | f929629 | 2011-08-25 17:00:39 -0700 | [diff] [blame] | 744 | return false; |
Romain Guy | 407ec78 | 2011-08-24 17:06:58 -0700 | [diff] [blame] | 745 | } |
| 746 | throw new RuntimeException("createWindowSurface failed " + |
| 747 | GLUtils.getEGLErrorString(error)); |
| 748 | } |
John Spurlock | 209bede | 2013-07-17 12:23:27 -0400 | [diff] [blame] | 749 | |
Romain Guy | 407ec78 | 2011-08-24 17:06:58 -0700 | [diff] [blame] | 750 | if (!mEgl.eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface, mEglContext)) { |
| 751 | throw new RuntimeException("eglMakeCurrent failed " + |
| 752 | GLUtils.getEGLErrorString(mEgl.eglGetError())); |
| 753 | } |
Romain Guy | f929629 | 2011-08-25 17:00:39 -0700 | [diff] [blame] | 754 | |
| 755 | return true; |
Romain Guy | 407ec78 | 2011-08-24 17:06:58 -0700 | [diff] [blame] | 756 | } |
John Spurlock | 209bede | 2013-07-17 12:23:27 -0400 | [diff] [blame] | 757 | |
| 758 | |
Romain Guy | 407ec78 | 2011-08-24 17:06:58 -0700 | [diff] [blame] | 759 | EGLContext createContext(EGL10 egl, EGLDisplay eglDisplay, EGLConfig eglConfig) { |
| 760 | int[] attrib_list = { EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE }; |
John Spurlock | 209bede | 2013-07-17 12:23:27 -0400 | [diff] [blame] | 761 | return egl.eglCreateContext(eglDisplay, eglConfig, EGL_NO_CONTEXT, attrib_list); |
Romain Guy | 407ec78 | 2011-08-24 17:06:58 -0700 | [diff] [blame] | 762 | } |
John Spurlock | 209bede | 2013-07-17 12:23:27 -0400 | [diff] [blame] | 763 | |
Romain Guy | 407ec78 | 2011-08-24 17:06:58 -0700 | [diff] [blame] | 764 | private EGLConfig chooseEglConfig() { |
| 765 | int[] configsCount = new int[1]; |
| 766 | EGLConfig[] configs = new EGLConfig[1]; |
| 767 | int[] configSpec = getConfig(); |
| 768 | if (!mEgl.eglChooseConfig(mEglDisplay, configSpec, configs, 1, configsCount)) { |
| 769 | throw new IllegalArgumentException("eglChooseConfig failed " + |
| 770 | GLUtils.getEGLErrorString(mEgl.eglGetError())); |
| 771 | } else if (configsCount[0] > 0) { |
| 772 | return configs[0]; |
| 773 | } |
| 774 | return null; |
| 775 | } |
| 776 | |
| 777 | private int[] getConfig() { |
| 778 | return new int[] { |
| 779 | EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT, |
| 780 | EGL_RED_SIZE, 8, |
| 781 | EGL_GREEN_SIZE, 8, |
| 782 | EGL_BLUE_SIZE, 8, |
| 783 | EGL_ALPHA_SIZE, 0, |
| 784 | EGL_DEPTH_SIZE, 0, |
| 785 | EGL_STENCIL_SIZE, 0, |
Romain Guy | 8efca54 | 2012-10-15 18:09:49 -0700 | [diff] [blame] | 786 | EGL_CONFIG_CAVEAT, EGL_NONE, |
Romain Guy | 407ec78 | 2011-08-24 17:06:58 -0700 | [diff] [blame] | 787 | EGL_NONE |
| 788 | }; |
Romain Guy | ef654bd | 2009-08-11 19:12:17 -0700 | [diff] [blame] | 789 | } |
Dianne Hackborn | 4c62fc0 | 2009-08-08 20:40:27 -0700 | [diff] [blame] | 790 | } |
| 791 | } |