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