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