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