BootAnimation.cpp revision 60691ce1600cb79ec9bb9957eee29be7a6a5e5d2
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 58extern "C" int clock_nanosleep(clockid_t clock_id, int flags, 59 const struct timespec *request, 60 struct timespec *remain); 61 62namespace android { 63 64// --------------------------------------------------------------------------- 65 66BootAnimation::BootAnimation() : Thread(false) 67{ 68 mSession = new SurfaceComposerClient(); 69} 70 71BootAnimation::~BootAnimation() { 72} 73 74void BootAnimation::onFirstRef() { 75 status_t err = mSession->linkToComposerDeath(this); 76 ALOGE_IF(err, "linkToComposerDeath failed (%s) ", strerror(-err)); 77 if (err == NO_ERROR) { 78 run("BootAnimation", PRIORITY_DISPLAY); 79 } 80} 81 82sp<SurfaceComposerClient> BootAnimation::session() const { 83 return mSession; 84} 85 86 87void BootAnimation::binderDied(const wp<IBinder>& who) 88{ 89 // woah, surfaceflinger died! 90 ALOGD("SurfaceFlinger died, exiting..."); 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 98status_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 153status_t BootAnimation::initTexture(void* buffer, size_t len) 154{ 155 //StopWatch watch("blah"); 156 157 SkBitmap bitmap; 158 SkMemoryStream stream(buffer, len); 159 SkImageDecoder* codec = SkImageDecoder::Factory(&stream); 160 codec->setDitherImage(false); 161 if (codec) { 162 codec->decode(&stream, &bitmap, 163 SkBitmap::kARGB_8888_Config, 164 SkImageDecoder::kDecodePixels_Mode); 165 delete codec; 166 } 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 215status_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 224 sp<SurfaceControl> control = session()->createSurface( 225 0, dinfo.w, dinfo.h, PIXEL_FORMAT_RGB_565); 226 227 SurfaceComposerClient::openGlobalTransaction(); 228 control->setLayer(0x40000000); 229 SurfaceComposerClient::closeGlobalTransaction(); 230 231 sp<Surface> s = control->getSurface(); 232 233 // initialize opengl and egl 234 const EGLint attribs[] = { 235 EGL_RED_SIZE, 8, 236 EGL_GREEN_SIZE, 8, 237 EGL_BLUE_SIZE, 8, 238 EGL_DEPTH_SIZE, 0, 239 EGL_NONE 240 }; 241 EGLint w, h, dummy; 242 EGLint numConfigs; 243 EGLConfig config; 244 EGLSurface surface; 245 EGLContext context; 246 247 EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY); 248 249 eglInitialize(display, 0, 0); 250 eglChooseConfig(display, attribs, &config, 1, &numConfigs); 251 surface = eglCreateWindowSurface(display, config, s.get(), NULL); 252 context = eglCreateContext(display, config, NULL, NULL); 253 eglQuerySurface(display, surface, EGL_WIDTH, &w); 254 eglQuerySurface(display, surface, EGL_HEIGHT, &h); 255 256 if (eglMakeCurrent(display, surface, surface, context) == EGL_FALSE) 257 return NO_INIT; 258 259 mDisplay = display; 260 mContext = context; 261 mSurface = surface; 262 mWidth = w; 263 mHeight = h; 264 mFlingerSurfaceControl = control; 265 mFlingerSurface = s; 266 267 mAndroidAnimation = true; 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))) { 285 mAndroidAnimation = false; 286 } 287 288 return NO_ERROR; 289} 290 291bool BootAnimation::threadLoop() 292{ 293 bool r; 294 if (mAndroidAnimation) { 295 r = android(); 296 } else { 297 r = movie(); 298 } 299 300 eglMakeCurrent(mDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT); 301 eglDestroyContext(mDisplay, mContext); 302 eglDestroySurface(mDisplay, mSurface); 303 mFlingerSurface.clear(); 304 mFlingerSurfaceControl.clear(); 305 eglTerminate(mDisplay); 306 IPCThreadState::self()->stopProcess(); 307 return r; 308} 309 310bool BootAnimation::android() 311{ 312 initTexture(&mAndroid[0], mAssets, "images/android-logo-mask.png"); 313 initTexture(&mAndroid[1], mAssets, "images/android-logo-shine.png"); 314 315 // clear screen 316 glShadeModel(GL_FLAT); 317 glDisable(GL_DITHER); 318 glDisable(GL_SCISSOR_TEST); 319 glClearColor(0,0,0,1); 320 glClear(GL_COLOR_BUFFER_BIT); 321 eglSwapBuffers(mDisplay, mSurface); 322 323 glEnable(GL_TEXTURE_2D); 324 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); 325 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); 329 330 glScissor(updateRect.left, mHeight - updateRect.bottom, updateRect.width(), 331 updateRect.height()); 332 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 337 const nsecs_t startTime = systemTime(); 338 do { 339 nsecs_t now = systemTime(); 340 double time = now - startTime; 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; 344 345 glDisable(GL_SCISSOR_TEST); 346 glClear(GL_COLOR_BUFFER_BIT); 347 348 glEnable(GL_SCISSOR_TEST); 349 glDisable(GL_BLEND); 350 glBindTexture(GL_TEXTURE_2D, mAndroid[1].name); 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); 353 354 glEnable(GL_BLEND); 355 glBindTexture(GL_TEXTURE_2D, mAndroid[0].name); 356 glDrawTexiOES(xc, yc, 0, mAndroid[0].w, mAndroid[0].h); 357 358 EGLBoolean res = eglSwapBuffers(mDisplay, mSurface); 359 if (res == EGL_FALSE) 360 break; 361 362 // 12fps: don't animate too fast to preserve CPU 363 const nsecs_t sleepTime = 83333 - ns2us(systemTime() - now); 364 if (sleepTime > 0) 365 usleep(sleepTime); 366 } while (!exitPending()); 367 368 glDeleteTextures(1, &mAndroid[0].name); 369 glDeleteTextures(1, &mAndroid[1].name); 370 return false; 371} 372 373 374bool 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); 381 ALOGE_IF(!descMap, "descMap is null"); 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) { 401 //ALOGD("> w=%d, h=%d, fps=%d", fps, width, height); 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) { 407 //ALOGD("> count=%d, pause=%d, path=%s", count, pause, path); 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); 454 glClearColor(0,0,0,1); 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 472 Region clearReg(Rect(mWidth, mHeight)); 473 clearReg.subtractSelf(Rect(xc, yc, xc+animation.width, yc+animation.height)); 474 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]); 483 nsecs_t lastFrame = systemTime(); 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 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 } 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); 516 //ALOGD("%lld, %lld", ns2ms(now - lastFrame), ns2ms(delay)); 517 lastFrame = now; 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 } 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 544// --------------------------------------------------------------------------- 545 546} 547; // namespace android 548