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