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