The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2007 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 | |
Mathias Agopian | bc72611 | 2009-09-23 15:44:05 -0700 | [diff] [blame] | 17 | #define LOG_TAG "BootAnimation" |
| 18 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 19 | #include <stdint.h> |
| 20 | #include <sys/types.h> |
| 21 | #include <math.h> |
| 22 | #include <fcntl.h> |
| 23 | #include <utils/misc.h> |
Mathias Agopian | b4d5a72 | 2009-09-23 17:05:19 -0700 | [diff] [blame] | 24 | #include <signal.h> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 25 | |
Jason parks | bd9a08d | 2011-01-31 15:04:34 -0600 | [diff] [blame] | 26 | #include <cutils/properties.h> |
| 27 | |
Mathias Agopian | b13b9bd | 2012-02-17 18:27:36 -0800 | [diff] [blame] | 28 | #include <androidfw/AssetManager.h> |
Mathias Agopian | ac31a3b | 2009-05-21 19:59:24 -0700 | [diff] [blame] | 29 | #include <binder/IPCThreadState.h> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 30 | #include <utils/Atomic.h> |
| 31 | #include <utils/Errors.h> |
| 32 | #include <utils/Log.h> |
Mathias Agopian | b13b9bd | 2012-02-17 18:27:36 -0800 | [diff] [blame] | 33 | #include <utils/threads.h> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 34 | |
| 35 | #include <ui/PixelFormat.h> |
| 36 | #include <ui/Rect.h> |
| 37 | #include <ui/Region.h> |
| 38 | #include <ui/DisplayInfo.h> |
Mathias Agopian | dff8e58 | 2009-05-04 14:17:04 -0700 | [diff] [blame] | 39 | #include <ui/FramebufferNativeWindow.h> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 40 | |
Mathias Agopian | 8335f1c | 2012-02-25 18:48:35 -0800 | [diff] [blame] | 41 | #include <gui/Surface.h> |
| 42 | #include <gui/SurfaceComposerClient.h> |
Mathias Agopian | 000479f | 2010-02-09 17:46:37 -0800 | [diff] [blame] | 43 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 44 | #include <core/SkBitmap.h> |
Mathias Agopian | 2b99e55 | 2011-11-10 15:59:07 -0800 | [diff] [blame] | 45 | #include <core/SkStream.h> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 46 | #include <images/SkImageDecoder.h> |
| 47 | |
| 48 | #include <GLES/gl.h> |
| 49 | #include <GLES/glext.h> |
| 50 | #include <EGL/eglext.h> |
| 51 | |
| 52 | #include "BootAnimation.h" |
| 53 | |
Jim Huang | c11f462 | 2010-08-10 03:12:15 +0800 | [diff] [blame] | 54 | #define USER_BOOTANIMATION_FILE "/data/local/bootanimation.zip" |
| 55 | #define SYSTEM_BOOTANIMATION_FILE "/system/media/bootanimation.zip" |
Jason parks | bd9a08d | 2011-01-31 15:04:34 -0600 | [diff] [blame] | 56 | #define SYSTEM_ENCRYPTED_BOOTANIMATION_FILE "/system/media/bootanimation-encrypted.zip" |
Jim Huang | c11f462 | 2010-08-10 03:12:15 +0800 | [diff] [blame] | 57 | |
Mathias Agopian | db7dd2a | 2012-05-12 15:08:21 -0700 | [diff] [blame] | 58 | extern "C" int clock_nanosleep(clockid_t clock_id, int flags, |
| 59 | const struct timespec *request, |
| 60 | struct timespec *remain); |
| 61 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 62 | namespace android { |
| 63 | |
| 64 | // --------------------------------------------------------------------------- |
| 65 | |
Mathias Agopian | 627e7b5 | 2009-05-21 19:21:59 -0700 | [diff] [blame] | 66 | BootAnimation::BootAnimation() : Thread(false) |
Mathias Agopian | a8826d6 | 2009-10-01 03:10:14 -0700 | [diff] [blame] | 67 | { |
Mathias Agopian | 627e7b5 | 2009-05-21 19:21:59 -0700 | [diff] [blame] | 68 | mSession = new SurfaceComposerClient(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | BootAnimation::~BootAnimation() { |
| 72 | } |
| 73 | |
| 74 | void BootAnimation::onFirstRef() { |
Mathias Agopian | bc72611 | 2009-09-23 15:44:05 -0700 | [diff] [blame] | 75 | status_t err = mSession->linkToComposerDeath(this); |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 76 | ALOGE_IF(err, "linkToComposerDeath failed (%s) ", strerror(-err)); |
Mathias Agopian | 8434c53 | 2009-09-23 18:52:49 -0700 | [diff] [blame] | 77 | if (err == NO_ERROR) { |
Mathias Agopian | bc72611 | 2009-09-23 15:44:05 -0700 | [diff] [blame] | 78 | run("BootAnimation", PRIORITY_DISPLAY); |
| 79 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 80 | } |
| 81 | |
Mathias Agopian | bc72611 | 2009-09-23 15:44:05 -0700 | [diff] [blame] | 82 | sp<SurfaceComposerClient> BootAnimation::session() const { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 83 | return mSession; |
| 84 | } |
| 85 | |
Mathias Agopian | bc72611 | 2009-09-23 15:44:05 -0700 | [diff] [blame] | 86 | |
| 87 | void BootAnimation::binderDied(const wp<IBinder>& who) |
| 88 | { |
| 89 | // woah, surfaceflinger died! |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 90 | ALOGD("SurfaceFlinger died, exiting..."); |
Mathias Agopian | bc72611 | 2009-09-23 15:44:05 -0700 | [diff] [blame] | 91 | |
| 92 | // calling requestExit() is not enough here because the Surface code |
| 93 | // might be blocked on a condition variable that will never be updated. |
| 94 | kill( getpid(), SIGKILL ); |
| 95 | requestExit(); |
| 96 | } |
| 97 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 98 | status_t BootAnimation::initTexture(Texture* texture, AssetManager& assets, |
| 99 | const char* name) { |
| 100 | Asset* asset = assets.open(name, Asset::ACCESS_BUFFER); |
| 101 | if (!asset) |
| 102 | return NO_INIT; |
| 103 | SkBitmap bitmap; |
| 104 | SkImageDecoder::DecodeMemory(asset->getBuffer(false), asset->getLength(), |
| 105 | &bitmap, SkBitmap::kNo_Config, SkImageDecoder::kDecodePixels_Mode); |
| 106 | asset->close(); |
| 107 | delete asset; |
| 108 | |
| 109 | // ensure we can call getPixels(). No need to call unlock, since the |
| 110 | // bitmap will go out of scope when we return from this method. |
| 111 | bitmap.lockPixels(); |
| 112 | |
| 113 | const int w = bitmap.width(); |
| 114 | const int h = bitmap.height(); |
| 115 | const void* p = bitmap.getPixels(); |
| 116 | |
| 117 | GLint crop[4] = { 0, h, w, -h }; |
| 118 | texture->w = w; |
| 119 | texture->h = h; |
| 120 | |
| 121 | glGenTextures(1, &texture->name); |
| 122 | glBindTexture(GL_TEXTURE_2D, texture->name); |
| 123 | |
| 124 | switch (bitmap.getConfig()) { |
| 125 | case SkBitmap::kA8_Config: |
| 126 | glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, w, h, 0, GL_ALPHA, |
| 127 | GL_UNSIGNED_BYTE, p); |
| 128 | break; |
| 129 | case SkBitmap::kARGB_4444_Config: |
| 130 | glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA, |
| 131 | GL_UNSIGNED_SHORT_4_4_4_4, p); |
| 132 | break; |
| 133 | case SkBitmap::kARGB_8888_Config: |
| 134 | glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA, |
| 135 | GL_UNSIGNED_BYTE, p); |
| 136 | break; |
| 137 | case SkBitmap::kRGB_565_Config: |
| 138 | glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, w, h, 0, GL_RGB, |
| 139 | GL_UNSIGNED_SHORT_5_6_5, p); |
| 140 | break; |
| 141 | default: |
| 142 | break; |
| 143 | } |
| 144 | |
| 145 | glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_CROP_RECT_OES, crop); |
| 146 | glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); |
| 147 | glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); |
| 148 | glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); |
| 149 | glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); |
| 150 | return NO_ERROR; |
| 151 | } |
| 152 | |
Mathias Agopian | a8826d6 | 2009-10-01 03:10:14 -0700 | [diff] [blame] | 153 | status_t BootAnimation::initTexture(void* buffer, size_t len) |
| 154 | { |
| 155 | //StopWatch watch("blah"); |
| 156 | |
| 157 | SkBitmap bitmap; |
Mathias Agopian | 2b99e55 | 2011-11-10 15:59:07 -0800 | [diff] [blame] | 158 | SkMemoryStream stream(buffer, len); |
| 159 | SkImageDecoder* codec = SkImageDecoder::Factory(&stream); |
| 160 | codec->setDitherImage(false); |
| 161 | if (codec) { |
| 162 | codec->decode(&stream, &bitmap, |
Mathias Agopian | 60691ce | 2012-06-11 14:08:02 -0700 | [diff] [blame^] | 163 | SkBitmap::kARGB_8888_Config, |
Mathias Agopian | 2b99e55 | 2011-11-10 15:59:07 -0800 | [diff] [blame] | 164 | SkImageDecoder::kDecodePixels_Mode); |
| 165 | delete codec; |
| 166 | } |
Mathias Agopian | a8826d6 | 2009-10-01 03:10:14 -0700 | [diff] [blame] | 167 | |
| 168 | // ensure we can call getPixels(). No need to call unlock, since the |
| 169 | // bitmap will go out of scope when we return from this method. |
| 170 | bitmap.lockPixels(); |
| 171 | |
| 172 | const int w = bitmap.width(); |
| 173 | const int h = bitmap.height(); |
| 174 | const void* p = bitmap.getPixels(); |
| 175 | |
| 176 | GLint crop[4] = { 0, h, w, -h }; |
| 177 | int tw = 1 << (31 - __builtin_clz(w)); |
| 178 | int th = 1 << (31 - __builtin_clz(h)); |
| 179 | if (tw < w) tw <<= 1; |
| 180 | if (th < h) th <<= 1; |
| 181 | |
| 182 | switch (bitmap.getConfig()) { |
| 183 | case SkBitmap::kARGB_8888_Config: |
| 184 | if (tw != w || th != h) { |
| 185 | glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, tw, th, 0, GL_RGBA, |
| 186 | GL_UNSIGNED_BYTE, 0); |
| 187 | glTexSubImage2D(GL_TEXTURE_2D, 0, |
| 188 | 0, 0, w, h, GL_RGBA, GL_UNSIGNED_BYTE, p); |
| 189 | } else { |
| 190 | glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, tw, th, 0, GL_RGBA, |
| 191 | GL_UNSIGNED_BYTE, p); |
| 192 | } |
| 193 | break; |
| 194 | |
| 195 | case SkBitmap::kRGB_565_Config: |
| 196 | if (tw != w || th != h) { |
| 197 | glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, tw, th, 0, GL_RGB, |
| 198 | GL_UNSIGNED_SHORT_5_6_5, 0); |
| 199 | glTexSubImage2D(GL_TEXTURE_2D, 0, |
| 200 | 0, 0, w, h, GL_RGB, GL_UNSIGNED_SHORT_5_6_5, p); |
| 201 | } else { |
| 202 | glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, tw, th, 0, GL_RGB, |
| 203 | GL_UNSIGNED_SHORT_5_6_5, p); |
| 204 | } |
| 205 | break; |
| 206 | default: |
| 207 | break; |
| 208 | } |
| 209 | |
| 210 | glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_CROP_RECT_OES, crop); |
| 211 | |
| 212 | return NO_ERROR; |
| 213 | } |
| 214 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 215 | status_t BootAnimation::readyToRun() { |
| 216 | mAssets.addDefaultAssets(); |
| 217 | |
| 218 | DisplayInfo dinfo; |
| 219 | status_t status = session()->getDisplayInfo(0, &dinfo); |
| 220 | if (status) |
| 221 | return -1; |
| 222 | |
| 223 | // create the native surface |
Mathias Agopian | 17f638b | 2009-04-16 20:04:08 -0700 | [diff] [blame] | 224 | sp<SurfaceControl> control = session()->createSurface( |
Mathias Agopian | 9638e5c | 2011-04-20 14:19:32 -0700 | [diff] [blame] | 225 | 0, dinfo.w, dinfo.h, PIXEL_FORMAT_RGB_565); |
Mathias Agopian | 439863f | 2011-06-28 19:09:31 -0700 | [diff] [blame] | 226 | |
| 227 | SurfaceComposerClient::openGlobalTransaction(); |
Mathias Agopian | 17f638b | 2009-04-16 20:04:08 -0700 | [diff] [blame] | 228 | control->setLayer(0x40000000); |
Mathias Agopian | 439863f | 2011-06-28 19:09:31 -0700 | [diff] [blame] | 229 | SurfaceComposerClient::closeGlobalTransaction(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 230 | |
Mathias Agopian | 17f638b | 2009-04-16 20:04:08 -0700 | [diff] [blame] | 231 | sp<Surface> s = control->getSurface(); |
| 232 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 233 | // initialize opengl and egl |
Mathias Agopian | 738b9a4 | 2009-08-06 16:41:02 -0700 | [diff] [blame] | 234 | const EGLint attribs[] = { |
Mathias Agopian | 1b253b7 | 2011-08-15 15:20:22 -0700 | [diff] [blame] | 235 | EGL_RED_SIZE, 8, |
| 236 | EGL_GREEN_SIZE, 8, |
| 237 | EGL_BLUE_SIZE, 8, |
Mathias Agopian | a8826d6 | 2009-10-01 03:10:14 -0700 | [diff] [blame] | 238 | EGL_DEPTH_SIZE, 0, |
| 239 | EGL_NONE |
Mathias Agopian | 738b9a4 | 2009-08-06 16:41:02 -0700 | [diff] [blame] | 240 | }; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 241 | EGLint w, h, dummy; |
| 242 | EGLint numConfigs; |
| 243 | EGLConfig config; |
| 244 | EGLSurface surface; |
| 245 | EGLContext context; |
Mathias Agopian | 627e7b5 | 2009-05-21 19:21:59 -0700 | [diff] [blame] | 246 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 247 | EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY); |
Mathias Agopian | 627e7b5 | 2009-05-21 19:21:59 -0700 | [diff] [blame] | 248 | |
| 249 | eglInitialize(display, 0, 0); |
Mathias Agopian | 1b253b7 | 2011-08-15 15:20:22 -0700 | [diff] [blame] | 250 | eglChooseConfig(display, attribs, &config, 1, &numConfigs); |
Mathias Agopian | 1473f46 | 2009-04-10 14:24:30 -0700 | [diff] [blame] | 251 | surface = eglCreateWindowSurface(display, config, s.get(), NULL); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 252 | context = eglCreateContext(display, config, NULL, NULL); |
| 253 | eglQuerySurface(display, surface, EGL_WIDTH, &w); |
| 254 | eglQuerySurface(display, surface, EGL_HEIGHT, &h); |
Mathias Agopian | a8826d6 | 2009-10-01 03:10:14 -0700 | [diff] [blame] | 255 | |
Mathias Agopian | abac010 | 2009-07-31 14:47:00 -0700 | [diff] [blame] | 256 | if (eglMakeCurrent(display, surface, surface, context) == EGL_FALSE) |
| 257 | return NO_INIT; |
Mathias Agopian | a8826d6 | 2009-10-01 03:10:14 -0700 | [diff] [blame] | 258 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 259 | mDisplay = display; |
| 260 | mContext = context; |
| 261 | mSurface = surface; |
| 262 | mWidth = w; |
| 263 | mHeight = h; |
Mathias Agopian | 17f638b | 2009-04-16 20:04:08 -0700 | [diff] [blame] | 264 | mFlingerSurfaceControl = control; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 265 | mFlingerSurface = s; |
| 266 | |
Jim Huang | c11f462 | 2010-08-10 03:12:15 +0800 | [diff] [blame] | 267 | mAndroidAnimation = true; |
Jason parks | bd9a08d | 2011-01-31 15:04:34 -0600 | [diff] [blame] | 268 | |
| 269 | // If the device has encryption turned on or is in process |
| 270 | // of being encrypted we show the encrypted boot animation. |
| 271 | char decrypt[PROPERTY_VALUE_MAX]; |
| 272 | property_get("vold.decrypt", decrypt, ""); |
| 273 | |
| 274 | bool encryptedAnimation = atoi(decrypt) != 0 || !strcmp("trigger_restart_min_framework", decrypt); |
| 275 | |
| 276 | if ((encryptedAnimation && |
| 277 | (access(SYSTEM_ENCRYPTED_BOOTANIMATION_FILE, R_OK) == 0) && |
| 278 | (mZip.open(SYSTEM_ENCRYPTED_BOOTANIMATION_FILE) == NO_ERROR)) || |
| 279 | |
| 280 | ((access(USER_BOOTANIMATION_FILE, R_OK) == 0) && |
| 281 | (mZip.open(USER_BOOTANIMATION_FILE) == NO_ERROR)) || |
| 282 | |
| 283 | ((access(SYSTEM_BOOTANIMATION_FILE, R_OK) == 0) && |
| 284 | (mZip.open(SYSTEM_BOOTANIMATION_FILE) == NO_ERROR))) { |
Chih-Wei Huang | 2978751 | 2010-11-03 15:33:00 +0800 | [diff] [blame] | 285 | mAndroidAnimation = false; |
Jason parks | bd9a08d | 2011-01-31 15:04:34 -0600 | [diff] [blame] | 286 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 287 | |
| 288 | return NO_ERROR; |
| 289 | } |
| 290 | |
Mathias Agopian | a8826d6 | 2009-10-01 03:10:14 -0700 | [diff] [blame] | 291 | bool BootAnimation::threadLoop() |
| 292 | { |
| 293 | bool r; |
| 294 | if (mAndroidAnimation) { |
| 295 | r = android(); |
| 296 | } else { |
| 297 | r = movie(); |
| 298 | } |
| 299 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 300 | eglMakeCurrent(mDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT); |
| 301 | eglDestroyContext(mDisplay, mContext); |
| 302 | eglDestroySurface(mDisplay, mSurface); |
Mathias Agopian | 6cf0db2 | 2009-04-17 19:36:26 -0700 | [diff] [blame] | 303 | mFlingerSurface.clear(); |
Mathias Agopian | 17f638b | 2009-04-16 20:04:08 -0700 | [diff] [blame] | 304 | mFlingerSurfaceControl.clear(); |
Mathias Agopian | 627e7b5 | 2009-05-21 19:21:59 -0700 | [diff] [blame] | 305 | eglTerminate(mDisplay); |
| 306 | IPCThreadState::self()->stopProcess(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 307 | return r; |
| 308 | } |
| 309 | |
Mathias Agopian | a8826d6 | 2009-10-01 03:10:14 -0700 | [diff] [blame] | 310 | bool BootAnimation::android() |
| 311 | { |
Mathias Agopian | b2cf954 | 2009-03-24 18:34:16 -0700 | [diff] [blame] | 312 | initTexture(&mAndroid[0], mAssets, "images/android-logo-mask.png"); |
| 313 | initTexture(&mAndroid[1], mAssets, "images/android-logo-shine.png"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 314 | |
| 315 | // clear screen |
Mathias Agopian | a8826d6 | 2009-10-01 03:10:14 -0700 | [diff] [blame] | 316 | glShadeModel(GL_FLAT); |
Mathias Agopian | b2cf954 | 2009-03-24 18:34:16 -0700 | [diff] [blame] | 317 | glDisable(GL_DITHER); |
| 318 | glDisable(GL_SCISSOR_TEST); |
Mathias Agopian | 59f19e4 | 2011-05-06 19:22:12 -0700 | [diff] [blame] | 319 | glClearColor(0,0,0,1); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 320 | glClear(GL_COLOR_BUFFER_BIT); |
| 321 | eglSwapBuffers(mDisplay, mSurface); |
| 322 | |
Mathias Agopian | a8826d6 | 2009-10-01 03:10:14 -0700 | [diff] [blame] | 323 | glEnable(GL_TEXTURE_2D); |
| 324 | glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); |
| 325 | |
Mathias Agopian | b2cf954 | 2009-03-24 18:34:16 -0700 | [diff] [blame] | 326 | const GLint xc = (mWidth - mAndroid[0].w) / 2; |
| 327 | const GLint yc = (mHeight - mAndroid[0].h) / 2; |
| 328 | const Rect updateRect(xc, yc, xc + mAndroid[0].w, yc + mAndroid[0].h); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 329 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 330 | glScissor(updateRect.left, mHeight - updateRect.bottom, updateRect.width(), |
| 331 | updateRect.height()); |
| 332 | |
Mathias Agopian | b2cf954 | 2009-03-24 18:34:16 -0700 | [diff] [blame] | 333 | // Blend state |
| 334 | glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); |
| 335 | glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); |
| 336 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 337 | const nsecs_t startTime = systemTime(); |
| 338 | do { |
Mathias Agopian | 1379665 | 2009-03-24 22:49:21 -0700 | [diff] [blame] | 339 | nsecs_t now = systemTime(); |
| 340 | double time = now - startTime; |
Mathias Agopian | b2cf954 | 2009-03-24 18:34:16 -0700 | [diff] [blame] | 341 | float t = 4.0f * float(time / us2ns(16667)) / mAndroid[1].w; |
| 342 | GLint offset = (1 - (t - floorf(t))) * mAndroid[1].w; |
| 343 | GLint x = xc - offset; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 344 | |
Mathias Agopian | 8166864 | 2009-07-28 11:41:30 -0700 | [diff] [blame] | 345 | glDisable(GL_SCISSOR_TEST); |
| 346 | glClear(GL_COLOR_BUFFER_BIT); |
| 347 | |
| 348 | glEnable(GL_SCISSOR_TEST); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 349 | glDisable(GL_BLEND); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 350 | glBindTexture(GL_TEXTURE_2D, mAndroid[1].name); |
Mathias Agopian | b2cf954 | 2009-03-24 18:34:16 -0700 | [diff] [blame] | 351 | glDrawTexiOES(x, yc, 0, mAndroid[1].w, mAndroid[1].h); |
| 352 | glDrawTexiOES(x + mAndroid[1].w, yc, 0, mAndroid[1].w, mAndroid[1].h); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 353 | |
Mathias Agopian | b2cf954 | 2009-03-24 18:34:16 -0700 | [diff] [blame] | 354 | glEnable(GL_BLEND); |
| 355 | glBindTexture(GL_TEXTURE_2D, mAndroid[0].name); |
| 356 | glDrawTexiOES(xc, yc, 0, mAndroid[0].w, mAndroid[0].h); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 357 | |
Mathias Agopian | 627e7b5 | 2009-05-21 19:21:59 -0700 | [diff] [blame] | 358 | EGLBoolean res = eglSwapBuffers(mDisplay, mSurface); |
| 359 | if (res == EGL_FALSE) |
| 360 | break; |
| 361 | |
Mathias Agopian | 1379665 | 2009-03-24 22:49:21 -0700 | [diff] [blame] | 362 | // 12fps: don't animate too fast to preserve CPU |
| 363 | const nsecs_t sleepTime = 83333 - ns2us(systemTime() - now); |
| 364 | if (sleepTime > 0) |
Mathias Agopian | a8826d6 | 2009-10-01 03:10:14 -0700 | [diff] [blame] | 365 | usleep(sleepTime); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 366 | } while (!exitPending()); |
| 367 | |
| 368 | glDeleteTextures(1, &mAndroid[0].name); |
| 369 | glDeleteTextures(1, &mAndroid[1].name); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 370 | return false; |
| 371 | } |
| 372 | |
Mathias Agopian | a8826d6 | 2009-10-01 03:10:14 -0700 | [diff] [blame] | 373 | |
| 374 | bool BootAnimation::movie() |
| 375 | { |
| 376 | ZipFileRO& zip(mZip); |
| 377 | |
| 378 | size_t numEntries = zip.getNumEntries(); |
| 379 | ZipEntryRO desc = zip.findEntryByName("desc.txt"); |
| 380 | FileMap* descMap = zip.createEntryFileMap(desc); |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 381 | ALOGE_IF(!descMap, "descMap is null"); |
Mathias Agopian | a8826d6 | 2009-10-01 03:10:14 -0700 | [diff] [blame] | 382 | if (!descMap) { |
| 383 | return false; |
| 384 | } |
| 385 | |
| 386 | String8 desString((char const*)descMap->getDataPtr(), |
| 387 | descMap->getDataLength()); |
| 388 | char const* s = desString.string(); |
| 389 | |
| 390 | Animation animation; |
| 391 | |
| 392 | // Parse the description file |
| 393 | for (;;) { |
| 394 | const char* endl = strstr(s, "\n"); |
| 395 | if (!endl) break; |
| 396 | String8 line(s, endl - s); |
| 397 | const char* l = line.string(); |
| 398 | int fps, width, height, count, pause; |
| 399 | char path[256]; |
| 400 | if (sscanf(l, "%d %d %d", &width, &height, &fps) == 3) { |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 401 | //ALOGD("> w=%d, h=%d, fps=%d", fps, width, height); |
Mathias Agopian | a8826d6 | 2009-10-01 03:10:14 -0700 | [diff] [blame] | 402 | animation.width = width; |
| 403 | animation.height = height; |
| 404 | animation.fps = fps; |
| 405 | } |
| 406 | if (sscanf(l, "p %d %d %s", &count, &pause, path) == 3) { |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 407 | //ALOGD("> count=%d, pause=%d, path=%s", count, pause, path); |
Mathias Agopian | a8826d6 | 2009-10-01 03:10:14 -0700 | [diff] [blame] | 408 | Animation::Part part; |
| 409 | part.count = count; |
| 410 | part.pause = pause; |
| 411 | part.path = path; |
| 412 | animation.parts.add(part); |
| 413 | } |
| 414 | s = ++endl; |
| 415 | } |
| 416 | |
| 417 | // read all the data structures |
| 418 | const size_t pcount = animation.parts.size(); |
| 419 | for (size_t i=0 ; i<numEntries ; i++) { |
| 420 | char name[256]; |
| 421 | ZipEntryRO entry = zip.findEntryByIndex(i); |
| 422 | if (zip.getEntryFileName(entry, name, 256) == 0) { |
| 423 | const String8 entryName(name); |
| 424 | const String8 path(entryName.getPathDir()); |
| 425 | const String8 leaf(entryName.getPathLeaf()); |
| 426 | if (leaf.size() > 0) { |
| 427 | for (int j=0 ; j<pcount ; j++) { |
| 428 | if (path == animation.parts[j].path) { |
| 429 | int method; |
| 430 | // supports only stored png files |
| 431 | if (zip.getEntryInfo(entry, &method, 0, 0, 0, 0, 0)) { |
| 432 | if (method == ZipFileRO::kCompressStored) { |
| 433 | FileMap* map = zip.createEntryFileMap(entry); |
| 434 | if (map) { |
| 435 | Animation::Frame frame; |
| 436 | frame.name = leaf; |
| 437 | frame.map = map; |
| 438 | Animation::Part& part(animation.parts.editItemAt(j)); |
| 439 | part.frames.add(frame); |
| 440 | } |
| 441 | } |
| 442 | } |
| 443 | } |
| 444 | } |
| 445 | } |
| 446 | } |
| 447 | } |
| 448 | |
| 449 | // clear screen |
| 450 | glShadeModel(GL_FLAT); |
| 451 | glDisable(GL_DITHER); |
| 452 | glDisable(GL_SCISSOR_TEST); |
| 453 | glDisable(GL_BLEND); |
Mathias Agopian | 59f19e4 | 2011-05-06 19:22:12 -0700 | [diff] [blame] | 454 | glClearColor(0,0,0,1); |
Mathias Agopian | a8826d6 | 2009-10-01 03:10:14 -0700 | [diff] [blame] | 455 | glClear(GL_COLOR_BUFFER_BIT); |
| 456 | |
| 457 | eglSwapBuffers(mDisplay, mSurface); |
| 458 | |
| 459 | glBindTexture(GL_TEXTURE_2D, 0); |
| 460 | glEnable(GL_TEXTURE_2D); |
| 461 | glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); |
| 462 | glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); |
| 463 | glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); |
| 464 | glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); |
| 465 | glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); |
| 466 | |
| 467 | const int xc = (mWidth - animation.width) / 2; |
| 468 | const int yc = ((mHeight - animation.height) / 2); |
| 469 | nsecs_t lastFrame = systemTime(); |
| 470 | nsecs_t frameDuration = s2ns(1) / animation.fps; |
| 471 | |
Mathias Agopian | 9f3020d | 2009-11-06 16:30:18 -0800 | [diff] [blame] | 472 | Region clearReg(Rect(mWidth, mHeight)); |
| 473 | clearReg.subtractSelf(Rect(xc, yc, xc+animation.width, yc+animation.height)); |
| 474 | |
Mathias Agopian | a8826d6 | 2009-10-01 03:10:14 -0700 | [diff] [blame] | 475 | for (int i=0 ; i<pcount && !exitPending() ; i++) { |
| 476 | const Animation::Part& part(animation.parts[i]); |
| 477 | const size_t fcount = part.frames.size(); |
| 478 | glBindTexture(GL_TEXTURE_2D, 0); |
| 479 | |
| 480 | for (int r=0 ; !part.count || r<part.count ; r++) { |
| 481 | for (int j=0 ; j<fcount && !exitPending(); j++) { |
| 482 | const Animation::Frame& frame(part.frames[j]); |
Mathias Agopian | db7dd2a | 2012-05-12 15:08:21 -0700 | [diff] [blame] | 483 | nsecs_t lastFrame = systemTime(); |
Mathias Agopian | a8826d6 | 2009-10-01 03:10:14 -0700 | [diff] [blame] | 484 | |
| 485 | if (r > 0) { |
| 486 | glBindTexture(GL_TEXTURE_2D, frame.tid); |
| 487 | } else { |
| 488 | if (part.count != 1) { |
| 489 | glGenTextures(1, &frame.tid); |
| 490 | glBindTexture(GL_TEXTURE_2D, frame.tid); |
| 491 | glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); |
| 492 | glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); |
| 493 | } |
| 494 | initTexture( |
| 495 | frame.map->getDataPtr(), |
| 496 | frame.map->getDataLength()); |
| 497 | } |
| 498 | |
Mathias Agopian | 9f3020d | 2009-11-06 16:30:18 -0800 | [diff] [blame] | 499 | if (!clearReg.isEmpty()) { |
| 500 | Region::const_iterator head(clearReg.begin()); |
| 501 | Region::const_iterator tail(clearReg.end()); |
| 502 | glEnable(GL_SCISSOR_TEST); |
| 503 | while (head != tail) { |
| 504 | const Rect& r(*head++); |
| 505 | glScissor(r.left, mHeight - r.bottom, |
| 506 | r.width(), r.height()); |
| 507 | glClear(GL_COLOR_BUFFER_BIT); |
| 508 | } |
| 509 | glDisable(GL_SCISSOR_TEST); |
| 510 | } |
Mathias Agopian | a8826d6 | 2009-10-01 03:10:14 -0700 | [diff] [blame] | 511 | glDrawTexiOES(xc, yc, 0, animation.width, animation.height); |
| 512 | eglSwapBuffers(mDisplay, mSurface); |
| 513 | |
| 514 | nsecs_t now = systemTime(); |
| 515 | nsecs_t delay = frameDuration - (now - lastFrame); |
Mathias Agopian | db7dd2a | 2012-05-12 15:08:21 -0700 | [diff] [blame] | 516 | //ALOGD("%lld, %lld", ns2ms(now - lastFrame), ns2ms(delay)); |
Mathias Agopian | a8826d6 | 2009-10-01 03:10:14 -0700 | [diff] [blame] | 517 | lastFrame = now; |
Mathias Agopian | db7dd2a | 2012-05-12 15:08:21 -0700 | [diff] [blame] | 518 | |
| 519 | if (delay > 0) { |
| 520 | struct timespec spec; |
| 521 | spec.tv_sec = (now + delay) / 1000000000; |
| 522 | spec.tv_nsec = (now + delay) % 1000000000; |
| 523 | int err; |
| 524 | do { |
| 525 | err = clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &spec, NULL); |
| 526 | } while (err<0 && errno == EINTR); |
| 527 | } |
Mathias Agopian | a8826d6 | 2009-10-01 03:10:14 -0700 | [diff] [blame] | 528 | } |
| 529 | usleep(part.pause * ns2us(frameDuration)); |
| 530 | } |
| 531 | |
| 532 | // free the textures for this part |
| 533 | if (part.count != 1) { |
| 534 | for (int j=0 ; j<fcount ; j++) { |
| 535 | const Animation::Frame& frame(part.frames[j]); |
| 536 | glDeleteTextures(1, &frame.tid); |
| 537 | } |
| 538 | } |
| 539 | } |
| 540 | |
| 541 | return false; |
| 542 | } |
| 543 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 544 | // --------------------------------------------------------------------------- |
| 545 | |
| 546 | } |
| 547 | ; // namespace android |