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