OSDN Git Service

[DO NOT MERGE] Check bounds in offsetToPtr am: bb6096d37b -s ours am: 31b54b9803...
[android-x86/frameworks-base.git] / cmds / bootanimation / BootAnimation.cpp
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_NDEBUG 0
18 #define LOG_TAG "BootAnimation"
19
20 #include <stdint.h>
21 #include <sys/inotify.h>
22 #include <sys/poll.h>
23 #include <sys/stat.h>
24 #include <sys/types.h>
25 #include <math.h>
26 #include <fcntl.h>
27 #include <utils/misc.h>
28 #include <signal.h>
29 #include <time.h>
30
31 #include <cutils/properties.h>
32
33 #include <androidfw/AssetManager.h>
34 #include <binder/IPCThreadState.h>
35 #include <utils/Atomic.h>
36 #include <utils/Errors.h>
37 #include <utils/Log.h>
38
39 #include <ui/PixelFormat.h>
40 #include <ui/Rect.h>
41 #include <ui/Region.h>
42 #include <ui/DisplayInfo.h>
43
44 #include <gui/ISurfaceComposer.h>
45 #include <gui/Surface.h>
46 #include <gui/SurfaceComposerClient.h>
47
48 // TODO: Fix Skia.
49 #pragma GCC diagnostic push
50 #pragma GCC diagnostic ignored "-Wunused-parameter"
51 #include <SkBitmap.h>
52 #include <SkStream.h>
53 #include <SkImageDecoder.h>
54 #pragma GCC diagnostic pop
55
56 #include <GLES/gl.h>
57 #include <GLES/glext.h>
58 #include <EGL/eglext.h>
59
60 #include "BootAnimation.h"
61 #include "audioplay.h"
62
63 namespace android {
64
65 static const char OEM_BOOTANIMATION_FILE[] = "/oem/media/bootanimation.zip";
66 static const char SYSTEM_BOOTANIMATION_FILE[] = "/system/media/bootanimation.zip";
67 static const char SYSTEM_ENCRYPTED_BOOTANIMATION_FILE[] = "/system/media/bootanimation-encrypted.zip";
68 static const char SYSTEM_DATA_DIR_PATH[] = "/data/system";
69 static const char SYSTEM_TIME_DIR_NAME[] = "time";
70 static const char SYSTEM_TIME_DIR_PATH[] = "/data/system/time";
71 static const char CLOCK_FONT_ASSET[] = "images/clock_font.png";
72 static const char CLOCK_FONT_ZIP_NAME[] = "clock_font.png";
73 static const char LAST_TIME_CHANGED_FILE_NAME[] = "last_time_change";
74 static const char LAST_TIME_CHANGED_FILE_PATH[] = "/data/system/time/last_time_change";
75 static const char ACCURATE_TIME_FLAG_FILE_NAME[] = "time_is_accurate";
76 static const char ACCURATE_TIME_FLAG_FILE_PATH[] = "/data/system/time/time_is_accurate";
77 static const char TIME_FORMAT_12_HOUR_FLAG_FILE_PATH[] = "/data/system/time/time_format_12_hour";
78 // Java timestamp format. Don't show the clock if the date is before 2000-01-01 00:00:00.
79 static const long long ACCURATE_TIME_EPOCH = 946684800000;
80 static constexpr char FONT_BEGIN_CHAR = ' ';
81 static constexpr char FONT_END_CHAR = '~' + 1;
82 static constexpr size_t FONT_NUM_CHARS = FONT_END_CHAR - FONT_BEGIN_CHAR + 1;
83 static constexpr size_t FONT_NUM_COLS = 16;
84 static constexpr size_t FONT_NUM_ROWS = FONT_NUM_CHARS / FONT_NUM_COLS;
85 static const int TEXT_CENTER_VALUE = INT_MAX;
86 static const int TEXT_MISSING_VALUE = INT_MIN;
87 static const char EXIT_PROP_NAME[] = "service.bootanim.exit";
88 static const char PLAY_SOUND_PROP_NAME[] = "persist.sys.bootanim.play_sound";
89 static const int ANIM_ENTRY_NAME_MAX = 256;
90 static constexpr size_t TEXT_POS_LEN_MAX = 16;
91 static const char BOOT_COMPLETED_PROP_NAME[] = "sys.boot_completed";
92 static const char BOOTREASON_PROP_NAME[] = "ro.boot.bootreason";
93 // bootreasons list in "system/core/bootstat/bootstat.cpp".
94 static const std::vector<std::string> PLAY_SOUND_BOOTREASON_BLACKLIST {
95   "kernel_panic",
96   "Panic",
97   "Watchdog",
98 };
99
100 // ---------------------------------------------------------------------------
101
102 BootAnimation::BootAnimation() : Thread(false), mClockEnabled(true), mTimeIsAccurate(false),
103         mTimeFormat12Hour(false), mTimeCheckThread(NULL) {
104     mSession = new SurfaceComposerClient();
105
106     // If the system has already booted, the animation is not being used for a boot.
107     mSystemBoot = !property_get_bool(BOOT_COMPLETED_PROP_NAME, 0);
108 }
109
110 BootAnimation::~BootAnimation() {}
111
112 void BootAnimation::onFirstRef() {
113     status_t err = mSession->linkToComposerDeath(this);
114     ALOGE_IF(err, "linkToComposerDeath failed (%s) ", strerror(-err));
115     if (err == NO_ERROR) {
116         run("BootAnimation", PRIORITY_DISPLAY);
117     }
118 }
119
120 sp<SurfaceComposerClient> BootAnimation::session() const {
121     return mSession;
122 }
123
124
125 void BootAnimation::binderDied(const wp<IBinder>&)
126 {
127     // woah, surfaceflinger died!
128     ALOGD("SurfaceFlinger died, exiting...");
129
130     // calling requestExit() is not enough here because the Surface code
131     // might be blocked on a condition variable that will never be updated.
132     kill( getpid(), SIGKILL );
133     requestExit();
134     audioplay::destroy();
135 }
136
137 status_t BootAnimation::initTexture(Texture* texture, AssetManager& assets,
138         const char* name) {
139     Asset* asset = assets.open(name, Asset::ACCESS_BUFFER);
140     if (asset == NULL)
141         return NO_INIT;
142     SkBitmap bitmap;
143     SkImageDecoder::DecodeMemory(asset->getBuffer(false), asset->getLength(),
144             &bitmap, kUnknown_SkColorType, SkImageDecoder::kDecodePixels_Mode);
145     asset->close();
146     delete asset;
147
148     // ensure we can call getPixels(). No need to call unlock, since the
149     // bitmap will go out of scope when we return from this method.
150     bitmap.lockPixels();
151
152     const int w = bitmap.width();
153     const int h = bitmap.height();
154     const void* p = bitmap.getPixels();
155
156     GLint crop[4] = { 0, h, w, -h };
157     texture->w = w;
158     texture->h = h;
159
160     glGenTextures(1, &texture->name);
161     glBindTexture(GL_TEXTURE_2D, texture->name);
162
163     switch (bitmap.colorType()) {
164         case kAlpha_8_SkColorType:
165             glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, w, h, 0, GL_ALPHA,
166                     GL_UNSIGNED_BYTE, p);
167             break;
168         case kARGB_4444_SkColorType:
169             glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA,
170                     GL_UNSIGNED_SHORT_4_4_4_4, p);
171             break;
172         case kN32_SkColorType:
173             glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA,
174                     GL_UNSIGNED_BYTE, p);
175             break;
176         case kRGB_565_SkColorType:
177             glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, w, h, 0, GL_RGB,
178                     GL_UNSIGNED_SHORT_5_6_5, p);
179             break;
180         default:
181             break;
182     }
183
184     glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_CROP_RECT_OES, crop);
185     glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
186     glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
187     glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
188     glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
189
190     return NO_ERROR;
191 }
192
193 status_t BootAnimation::initTexture(FileMap* map, int* width, int* height)
194 {
195     SkBitmap bitmap;
196     SkMemoryStream  stream(map->getDataPtr(), map->getDataLength());
197     SkImageDecoder* codec = SkImageDecoder::Factory(&stream);
198     if (codec != NULL) {
199         codec->setDitherImage(false);
200         codec->decode(&stream, &bitmap,
201                 kN32_SkColorType,
202                 SkImageDecoder::kDecodePixels_Mode);
203         delete codec;
204     }
205
206     // FileMap memory is never released until application exit.
207     // Release it now as the texture is already loaded and the memory used for
208     // the packed resource can be released.
209     delete map;
210
211     // ensure we can call getPixels(). No need to call unlock, since the
212     // bitmap will go out of scope when we return from this method.
213     bitmap.lockPixels();
214
215     const int w = bitmap.width();
216     const int h = bitmap.height();
217     const void* p = bitmap.getPixels();
218
219     GLint crop[4] = { 0, h, w, -h };
220     int tw = 1 << (31 - __builtin_clz(w));
221     int th = 1 << (31 - __builtin_clz(h));
222     if (tw < w) tw <<= 1;
223     if (th < h) th <<= 1;
224
225     switch (bitmap.colorType()) {
226         case kN32_SkColorType:
227             if (!mUseNpotTextures && (tw != w || th != h)) {
228                 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, tw, th, 0, GL_RGBA,
229                         GL_UNSIGNED_BYTE, 0);
230                 glTexSubImage2D(GL_TEXTURE_2D, 0,
231                         0, 0, w, h, GL_RGBA, GL_UNSIGNED_BYTE, p);
232             } else {
233                 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA,
234                         GL_UNSIGNED_BYTE, p);
235             }
236             break;
237
238         case kRGB_565_SkColorType:
239             if (!mUseNpotTextures && (tw != w || th != h)) {
240                 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, tw, th, 0, GL_RGB,
241                         GL_UNSIGNED_SHORT_5_6_5, 0);
242                 glTexSubImage2D(GL_TEXTURE_2D, 0,
243                         0, 0, w, h, GL_RGB, GL_UNSIGNED_SHORT_5_6_5, p);
244             } else {
245                 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, w, h, 0, GL_RGB,
246                         GL_UNSIGNED_SHORT_5_6_5, p);
247             }
248             break;
249         default:
250             break;
251     }
252
253     glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_CROP_RECT_OES, crop);
254
255     *width = w;
256     *height = h;
257
258     return NO_ERROR;
259 }
260
261 status_t BootAnimation::readyToRun() {
262     mAssets.addDefaultAssets();
263
264     sp<IBinder> dtoken(SurfaceComposerClient::getBuiltInDisplay(
265             ISurfaceComposer::eDisplayIdMain));
266     DisplayInfo dinfo;
267     status_t status = SurfaceComposerClient::getDisplayInfo(dtoken, &dinfo);
268     if (status)
269         return -1;
270
271     // create the native surface
272     sp<SurfaceControl> control = session()->createSurface(String8("BootAnimation"),
273             dinfo.w, dinfo.h, PIXEL_FORMAT_RGB_565);
274
275     SurfaceComposerClient::openGlobalTransaction();
276     control->setLayer(0x40000000);
277     SurfaceComposerClient::closeGlobalTransaction();
278
279     sp<Surface> s = control->getSurface();
280
281     // initialize opengl and egl
282     const EGLint attribs[] = {
283             EGL_RED_SIZE,   8,
284             EGL_GREEN_SIZE, 8,
285             EGL_BLUE_SIZE,  8,
286             EGL_DEPTH_SIZE, 0,
287             EGL_NONE
288     };
289     EGLint w, h;
290     EGLint numConfigs;
291     EGLConfig config;
292     EGLSurface surface;
293     EGLContext context;
294
295     EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
296
297     eglInitialize(display, 0, 0);
298     eglChooseConfig(display, attribs, &config, 1, &numConfigs);
299     surface = eglCreateWindowSurface(display, config, s.get(), NULL);
300     context = eglCreateContext(display, config, NULL, NULL);
301     eglQuerySurface(display, surface, EGL_WIDTH, &w);
302     eglQuerySurface(display, surface, EGL_HEIGHT, &h);
303
304     if (eglMakeCurrent(display, surface, surface, context) == EGL_FALSE)
305         return NO_INIT;
306
307     mDisplay = display;
308     mContext = context;
309     mSurface = surface;
310     mWidth = w;
311     mHeight = h;
312     mFlingerSurfaceControl = control;
313     mFlingerSurface = s;
314
315     // If the device has encryption turned on or is in process
316     // of being encrypted we show the encrypted boot animation.
317     char decrypt[PROPERTY_VALUE_MAX];
318     property_get("vold.decrypt", decrypt, "");
319
320     bool encryptedAnimation = atoi(decrypt) != 0 || !strcmp("trigger_restart_min_framework", decrypt);
321
322     if (encryptedAnimation && (access(SYSTEM_ENCRYPTED_BOOTANIMATION_FILE, R_OK) == 0)) {
323         mZipFileName = SYSTEM_ENCRYPTED_BOOTANIMATION_FILE;
324     }
325     else if (access(OEM_BOOTANIMATION_FILE, R_OK) == 0) {
326         mZipFileName = OEM_BOOTANIMATION_FILE;
327     }
328     else if (access(SYSTEM_BOOTANIMATION_FILE, R_OK) == 0) {
329         mZipFileName = SYSTEM_BOOTANIMATION_FILE;
330     }
331     return NO_ERROR;
332 }
333
334 bool BootAnimation::threadLoop()
335 {
336     bool r;
337     // We have no bootanimation file, so we use the stock android logo
338     // animation.
339     if (mZipFileName.isEmpty()) {
340         r = android();
341     } else {
342         r = movie();
343     }
344
345     eglMakeCurrent(mDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
346     eglDestroyContext(mDisplay, mContext);
347     eglDestroySurface(mDisplay, mSurface);
348     mFlingerSurface.clear();
349     mFlingerSurfaceControl.clear();
350     eglTerminate(mDisplay);
351     IPCThreadState::self()->stopProcess();
352     return r;
353 }
354
355 bool BootAnimation::android()
356 {
357     initTexture(&mAndroid[0], mAssets, "images/android-logo-mask.png");
358     initTexture(&mAndroid[1], mAssets, "images/android-logo-shine.png");
359
360     // clear screen
361     glShadeModel(GL_FLAT);
362     glDisable(GL_DITHER);
363     glDisable(GL_SCISSOR_TEST);
364     glClearColor(0,0,0,1);
365     glClear(GL_COLOR_BUFFER_BIT);
366     eglSwapBuffers(mDisplay, mSurface);
367
368     glEnable(GL_TEXTURE_2D);
369     glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
370
371     const GLint xc = (mWidth  - mAndroid[0].w) / 2;
372     const GLint yc = (mHeight - mAndroid[0].h) / 2;
373     const Rect updateRect(xc, yc, xc + mAndroid[0].w, yc + mAndroid[0].h);
374
375     glScissor(updateRect.left, mHeight - updateRect.bottom, updateRect.width(),
376             updateRect.height());
377
378     // Blend state
379     glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
380     glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
381
382     const nsecs_t startTime = systemTime();
383     do {
384         nsecs_t now = systemTime();
385         double time = now - startTime;
386         float t = 4.0f * float(time / us2ns(16667)) / mAndroid[1].w;
387         GLint offset = (1 - (t - floorf(t))) * mAndroid[1].w;
388         GLint x = xc - offset;
389
390         glDisable(GL_SCISSOR_TEST);
391         glClear(GL_COLOR_BUFFER_BIT);
392
393         glEnable(GL_SCISSOR_TEST);
394         glDisable(GL_BLEND);
395         glBindTexture(GL_TEXTURE_2D, mAndroid[1].name);
396         glDrawTexiOES(x,                 yc, 0, mAndroid[1].w, mAndroid[1].h);
397         glDrawTexiOES(x + mAndroid[1].w, yc, 0, mAndroid[1].w, mAndroid[1].h);
398
399         glEnable(GL_BLEND);
400         glBindTexture(GL_TEXTURE_2D, mAndroid[0].name);
401         glDrawTexiOES(xc, yc, 0, mAndroid[0].w, mAndroid[0].h);
402
403         EGLBoolean res = eglSwapBuffers(mDisplay, mSurface);
404         if (res == EGL_FALSE)
405             break;
406
407         // 12fps: don't animate too fast to preserve CPU
408         const nsecs_t sleepTime = 83333 - ns2us(systemTime() - now);
409         if (sleepTime > 0)
410             usleep(sleepTime);
411
412         checkExit();
413     } while (!exitPending());
414
415     glDeleteTextures(1, &mAndroid[0].name);
416     glDeleteTextures(1, &mAndroid[1].name);
417     return false;
418 }
419
420 void BootAnimation::checkExit() {
421     // Allow surface flinger to gracefully request shutdown
422     char value[PROPERTY_VALUE_MAX];
423     property_get(EXIT_PROP_NAME, value, "0");
424     int exitnow = atoi(value);
425     if (exitnow) {
426         requestExit();
427     }
428 }
429
430 bool BootAnimation::validClock(const Animation::Part& part) {
431     return part.clockPosX != TEXT_MISSING_VALUE && part.clockPosY != TEXT_MISSING_VALUE;
432 }
433
434 bool parseTextCoord(const char* str, int* dest) {
435     if (strcmp("c", str) == 0) {
436         *dest = TEXT_CENTER_VALUE;
437         return true;
438     }
439
440     char* end;
441     int val = (int) strtol(str, &end, 0);
442     if (end == str || *end != '\0' || val == INT_MAX || val == INT_MIN) {
443         return false;
444     }
445     *dest = val;
446     return true;
447 }
448
449 // Parse two position coordinates. If only string is non-empty, treat it as the y value.
450 void parsePosition(const char* str1, const char* str2, int* x, int* y) {
451     bool success = false;
452     if (strlen(str1) == 0) {  // No values were specified
453         // success = false
454     } else if (strlen(str2) == 0) {  // we have only one value
455         if (parseTextCoord(str1, y)) {
456             *x = TEXT_CENTER_VALUE;
457             success = true;
458         }
459     } else {
460         if (parseTextCoord(str1, x) && parseTextCoord(str2, y)) {
461             success = true;
462         }
463     }
464
465     if (!success) {
466         *x = TEXT_MISSING_VALUE;
467         *y = TEXT_MISSING_VALUE;
468     }
469 }
470
471 // Parse a color represented as an HTML-style 'RRGGBB' string: each pair of
472 // characters in str is a hex number in [0, 255], which are converted to
473 // floating point values in the range [0.0, 1.0] and placed in the
474 // corresponding elements of color.
475 //
476 // If the input string isn't valid, parseColor returns false and color is
477 // left unchanged.
478 static bool parseColor(const char str[7], float color[3]) {
479     float tmpColor[3];
480     for (int i = 0; i < 3; i++) {
481         int val = 0;
482         for (int j = 0; j < 2; j++) {
483             val *= 16;
484             char c = str[2*i + j];
485             if      (c >= '0' && c <= '9') val += c - '0';
486             else if (c >= 'A' && c <= 'F') val += (c - 'A') + 10;
487             else if (c >= 'a' && c <= 'f') val += (c - 'a') + 10;
488             else                           return false;
489         }
490         tmpColor[i] = static_cast<float>(val) / 255.0f;
491     }
492     memcpy(color, tmpColor, sizeof(tmpColor));
493     return true;
494 }
495
496
497 static bool readFile(ZipFileRO* zip, const char* name, String8& outString)
498 {
499     ZipEntryRO entry = zip->findEntryByName(name);
500     ALOGE_IF(!entry, "couldn't find %s", name);
501     if (!entry) {
502         return false;
503     }
504
505     FileMap* entryMap = zip->createEntryFileMap(entry);
506     zip->releaseEntry(entry);
507     ALOGE_IF(!entryMap, "entryMap is null");
508     if (!entryMap) {
509         return false;
510     }
511
512     outString.setTo((char const*)entryMap->getDataPtr(), entryMap->getDataLength());
513     delete entryMap;
514     return true;
515 }
516
517 // The font image should be a 96x2 array of character images.  The
518 // columns are the printable ASCII characters 0x20 - 0x7f.  The
519 // top row is regular text; the bottom row is bold.
520 status_t BootAnimation::initFont(Font* font, const char* fallback) {
521     status_t status = NO_ERROR;
522
523     if (font->map != nullptr) {
524         glGenTextures(1, &font->texture.name);
525         glBindTexture(GL_TEXTURE_2D, font->texture.name);
526
527         status = initTexture(font->map, &font->texture.w, &font->texture.h);
528
529         glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
530         glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
531         glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
532         glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
533     } else if (fallback != nullptr) {
534         status = initTexture(&font->texture, mAssets, fallback);
535     } else {
536         return NO_INIT;
537     }
538
539     if (status == NO_ERROR) {
540         font->char_width = font->texture.w / FONT_NUM_COLS;
541         font->char_height = font->texture.h / FONT_NUM_ROWS / 2;  // There are bold and regular rows
542     }
543
544     return status;
545 }
546
547 void BootAnimation::drawText(const char* str, const Font& font, bool bold, int* x, int* y) {
548     glEnable(GL_BLEND);  // Allow us to draw on top of the animation
549     glBindTexture(GL_TEXTURE_2D, font.texture.name);
550
551     const int len = strlen(str);
552     const int strWidth = font.char_width * len;
553
554     if (*x == TEXT_CENTER_VALUE) {
555         *x = (mWidth - strWidth) / 2;
556     } else if (*x < 0) {
557         *x = mWidth + *x - strWidth;
558     }
559     if (*y == TEXT_CENTER_VALUE) {
560         *y = (mHeight - font.char_height) / 2;
561     } else if (*y < 0) {
562         *y = mHeight + *y - font.char_height;
563     }
564
565     int cropRect[4] = { 0, 0, font.char_width, -font.char_height };
566
567     for (int i = 0; i < len; i++) {
568         char c = str[i];
569
570         if (c < FONT_BEGIN_CHAR || c > FONT_END_CHAR) {
571             c = '?';
572         }
573
574         // Crop the texture to only the pixels in the current glyph
575         const int charPos = (c - FONT_BEGIN_CHAR);  // Position in the list of valid characters
576         const int row = charPos / FONT_NUM_COLS;
577         const int col = charPos % FONT_NUM_COLS;
578         cropRect[0] = col * font.char_width;  // Left of column
579         cropRect[1] = row * font.char_height * 2; // Top of row
580         // Move down to bottom of regular (one char_heigh) or bold (two char_heigh) line
581         cropRect[1] += bold ? 2 * font.char_height : font.char_height;
582         glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_CROP_RECT_OES, cropRect);
583
584         glDrawTexiOES(*x, *y, 0, font.char_width, font.char_height);
585
586         *x += font.char_width;
587     }
588
589     glDisable(GL_BLEND);  // Return to the animation's default behaviour
590     glBindTexture(GL_TEXTURE_2D, 0);
591 }
592
593 // We render 12 or 24 hour time.
594 void BootAnimation::drawClock(const Font& font, const int xPos, const int yPos) {
595     static constexpr char TIME_FORMAT_12[] = "%l:%M";
596     static constexpr char TIME_FORMAT_24[] = "%H:%M";
597     static constexpr int TIME_LENGTH = 6;
598
599     time_t rawtime;
600     time(&rawtime);
601     struct tm* timeInfo = localtime(&rawtime);
602
603     char timeBuff[TIME_LENGTH];
604     const char* timeFormat = mTimeFormat12Hour ? TIME_FORMAT_12 : TIME_FORMAT_24;
605     size_t length = strftime(timeBuff, TIME_LENGTH, timeFormat, timeInfo);
606
607     if (length != TIME_LENGTH - 1) {
608         ALOGE("Couldn't format time; abandoning boot animation clock");
609         mClockEnabled = false;
610         return;
611     }
612
613     char* out = timeBuff[0] == ' ' ? &timeBuff[1] : &timeBuff[0];
614     int x = xPos;
615     int y = yPos;
616     drawText(out, font, false, &x, &y);
617 }
618
619 bool BootAnimation::parseAnimationDesc(Animation& animation)
620 {
621     String8 desString;
622
623     if (!readFile(animation.zip, "desc.txt", desString)) {
624         return false;
625     }
626     char const* s = desString.string();
627
628     // Parse the description file
629     for (;;) {
630         const char* endl = strstr(s, "\n");
631         if (endl == NULL) break;
632         String8 line(s, endl - s);
633         const char* l = line.string();
634         int fps = 0;
635         int width = 0;
636         int height = 0;
637         int count = 0;
638         int pause = 0;
639         char path[ANIM_ENTRY_NAME_MAX];
640         char color[7] = "000000"; // default to black if unspecified
641         char clockPos1[TEXT_POS_LEN_MAX + 1] = "";
642         char clockPos2[TEXT_POS_LEN_MAX + 1] = "";
643
644         char pathType;
645         if (sscanf(l, "%d %d %d", &width, &height, &fps) == 3) {
646             // ALOGD("> w=%d, h=%d, fps=%d", width, height, fps);
647             animation.width = width;
648             animation.height = height;
649             animation.fps = fps;
650         } else if (sscanf(l, " %c %d %d %s #%6s %16s %16s",
651                           &pathType, &count, &pause, path, color, clockPos1, clockPos2) >= 4) {
652             //ALOGD("> type=%c, count=%d, pause=%d, path=%s, color=%s, clockPos1=%s, clockPos2=%s",
653             //    pathType, count, pause, path, color, clockPos1, clockPos2);
654             Animation::Part part;
655             part.playUntilComplete = pathType == 'c';
656             part.count = count;
657             part.pause = pause;
658             part.path = path;
659             part.audioData = NULL;
660             part.animation = NULL;
661             if (!parseColor(color, part.backgroundColor)) {
662                 ALOGE("> invalid color '#%s'", color);
663                 part.backgroundColor[0] = 0.0f;
664                 part.backgroundColor[1] = 0.0f;
665                 part.backgroundColor[2] = 0.0f;
666             }
667             parsePosition(clockPos1, clockPos2, &part.clockPosX, &part.clockPosY);
668             animation.parts.add(part);
669         }
670         else if (strcmp(l, "$SYSTEM") == 0) {
671             // ALOGD("> SYSTEM");
672             Animation::Part part;
673             part.playUntilComplete = false;
674             part.count = 1;
675             part.pause = 0;
676             part.audioData = NULL;
677             part.animation = loadAnimation(String8(SYSTEM_BOOTANIMATION_FILE));
678             if (part.animation != NULL)
679                 animation.parts.add(part);
680         }
681         s = ++endl;
682     }
683
684     return true;
685 }
686
687 bool BootAnimation::preloadZip(Animation& animation)
688 {
689     // read all the data structures
690     const size_t pcount = animation.parts.size();
691     void *cookie = NULL;
692     ZipFileRO* zip = animation.zip;
693     if (!zip->startIteration(&cookie)) {
694         return false;
695     }
696
697     Animation::Part* partWithAudio = NULL;
698     ZipEntryRO entry;
699     char name[ANIM_ENTRY_NAME_MAX];
700     while ((entry = zip->nextEntry(cookie)) != NULL) {
701         const int foundEntryName = zip->getEntryFileName(entry, name, ANIM_ENTRY_NAME_MAX);
702         if (foundEntryName > ANIM_ENTRY_NAME_MAX || foundEntryName == -1) {
703             ALOGE("Error fetching entry file name");
704             continue;
705         }
706
707         const String8 entryName(name);
708         const String8 path(entryName.getPathDir());
709         const String8 leaf(entryName.getPathLeaf());
710         if (leaf.size() > 0) {
711             if (entryName == CLOCK_FONT_ZIP_NAME) {
712                 FileMap* map = zip->createEntryFileMap(entry);
713                 if (map) {
714                     animation.clockFont.map = map;
715                 }
716                 continue;
717             }
718
719             for (size_t j = 0; j < pcount; j++) {
720                 if (path == animation.parts[j].path) {
721                     uint16_t method;
722                     // supports only stored png files
723                     if (zip->getEntryInfo(entry, &method, NULL, NULL, NULL, NULL, NULL)) {
724                         if (method == ZipFileRO::kCompressStored) {
725                             FileMap* map = zip->createEntryFileMap(entry);
726                             if (map) {
727                                 Animation::Part& part(animation.parts.editItemAt(j));
728                                 if (leaf == "audio.wav") {
729                                     // a part may have at most one audio file
730                                     part.audioData = (uint8_t *)map->getDataPtr();
731                                     part.audioLength = map->getDataLength();
732                                     partWithAudio = &part;
733                                 } else if (leaf == "trim.txt") {
734                                     part.trimData.setTo((char const*)map->getDataPtr(),
735                                                         map->getDataLength());
736                                 } else {
737                                     Animation::Frame frame;
738                                     frame.name = leaf;
739                                     frame.map = map;
740                                     frame.trimWidth = animation.width;
741                                     frame.trimHeight = animation.height;
742                                     frame.trimX = 0;
743                                     frame.trimY = 0;
744                                     part.frames.add(frame);
745                                 }
746                             }
747                         } else {
748                             ALOGE("bootanimation.zip is compressed; must be only stored");
749                         }
750                     }
751                 }
752             }
753         }
754     }
755
756     // If there is trimData present, override the positioning defaults.
757     for (Animation::Part& part : animation.parts) {
758         const char* trimDataStr = part.trimData.string();
759         for (size_t frameIdx = 0; frameIdx < part.frames.size(); frameIdx++) {
760             const char* endl = strstr(trimDataStr, "\n");
761             // No more trimData for this part.
762             if (endl == NULL) {
763                 break;
764             }
765             String8 line(trimDataStr, endl - trimDataStr);
766             const char* lineStr = line.string();
767             trimDataStr = ++endl;
768             int width = 0, height = 0, x = 0, y = 0;
769             if (sscanf(lineStr, "%dx%d+%d+%d", &width, &height, &x, &y) == 4) {
770                 Animation::Frame& frame(part.frames.editItemAt(frameIdx));
771                 frame.trimWidth = width;
772                 frame.trimHeight = height;
773                 frame.trimX = x;
774                 frame.trimY = y;
775             } else {
776                 ALOGE("Error parsing trim.txt, line: %s", lineStr);
777                 break;
778             }
779         }
780     }
781
782     // Create and initialize audioplay if there is a wav file in any of the animations.
783     if (partWithAudio != NULL) {
784         ALOGD("found audio.wav, creating playback engine");
785         if (!audioplay::create(partWithAudio->audioData, partWithAudio->audioLength)) {
786             return false;
787         }
788     }
789
790     zip->endIteration(cookie);
791
792     return true;
793 }
794
795 bool BootAnimation::movie()
796 {
797     Animation* animation = loadAnimation(mZipFileName);
798     if (animation == NULL)
799         return false;
800
801     bool anyPartHasClock = false;
802     for (size_t i=0; i < animation->parts.size(); i++) {
803         if(validClock(animation->parts[i])) {
804             anyPartHasClock = true;
805             break;
806         }
807     }
808     if (!anyPartHasClock) {
809         mClockEnabled = false;
810     }
811
812     // Check if npot textures are supported
813     mUseNpotTextures = false;
814     String8 gl_extensions;
815     const char* exts = reinterpret_cast<const char*>(glGetString(GL_EXTENSIONS));
816     if (!exts) {
817         glGetError();
818     } else {
819         gl_extensions.setTo(exts);
820         if ((gl_extensions.find("GL_ARB_texture_non_power_of_two") != -1) ||
821             (gl_extensions.find("GL_OES_texture_npot") != -1)) {
822             mUseNpotTextures = true;
823         }
824     }
825
826     // Blend required to draw time on top of animation frames.
827     glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
828     glShadeModel(GL_FLAT);
829     glDisable(GL_DITHER);
830     glDisable(GL_SCISSOR_TEST);
831     glDisable(GL_BLEND);
832
833     glBindTexture(GL_TEXTURE_2D, 0);
834     glEnable(GL_TEXTURE_2D);
835     glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
836     glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
837     glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
838     glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
839     glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
840
841     bool clockFontInitialized = false;
842     if (mClockEnabled) {
843         clockFontInitialized =
844             (initFont(&animation->clockFont, CLOCK_FONT_ASSET) == NO_ERROR);
845         mClockEnabled = clockFontInitialized;
846     }
847
848     if (mClockEnabled && !updateIsTimeAccurate()) {
849         mTimeCheckThread = new TimeCheckThread(this);
850         mTimeCheckThread->run("BootAnimation::TimeCheckThread", PRIORITY_NORMAL);
851     }
852
853     playAnimation(*animation);
854
855     if (mTimeCheckThread != NULL) {
856         mTimeCheckThread->requestExit();
857         mTimeCheckThread = NULL;
858     }
859
860     releaseAnimation(animation);
861
862     if (clockFontInitialized) {
863         glDeleteTextures(1, &animation->clockFont.texture.name);
864     }
865
866     return false;
867 }
868
869 bool BootAnimation::playAnimation(const Animation& animation)
870 {
871     const size_t pcount = animation.parts.size();
872     nsecs_t frameDuration = s2ns(1) / animation.fps;
873     const int animationX = (mWidth - animation.width) / 2;
874     const int animationY = (mHeight - animation.height) / 2;
875
876     for (size_t i=0 ; i<pcount ; i++) {
877         const Animation::Part& part(animation.parts[i]);
878         const size_t fcount = part.frames.size();
879         glBindTexture(GL_TEXTURE_2D, 0);
880
881         // Handle animation package
882         if (part.animation != NULL) {
883             playAnimation(*part.animation);
884             if (exitPending())
885                 break;
886             continue; //to next part
887         }
888
889         for (int r=0 ; !part.count || r<part.count ; r++) {
890             // Exit any non playuntil complete parts immediately
891             if(exitPending() && !part.playUntilComplete)
892                 break;
893
894             // only play audio file the first time we animate the part
895             if (r == 0 && part.audioData && playSoundsAllowed()) {
896                 ALOGD("playing clip for part%d, size=%d", (int) i, part.audioLength);
897                 audioplay::playClip(part.audioData, part.audioLength);
898             }
899
900             glClearColor(
901                     part.backgroundColor[0],
902                     part.backgroundColor[1],
903                     part.backgroundColor[2],
904                     1.0f);
905
906             for (size_t j=0 ; j<fcount && (!exitPending() || part.playUntilComplete) ; j++) {
907                 const Animation::Frame& frame(part.frames[j]);
908                 nsecs_t lastFrame = systemTime();
909
910                 if (r > 0) {
911                     glBindTexture(GL_TEXTURE_2D, frame.tid);
912                 } else {
913                     if (part.count != 1) {
914                         glGenTextures(1, &frame.tid);
915                         glBindTexture(GL_TEXTURE_2D, frame.tid);
916                         glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
917                         glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
918                     }
919                     int w, h;
920                     initTexture(frame.map, &w, &h);
921                 }
922
923                 const int xc = animationX + frame.trimX;
924                 const int yc = animationY + frame.trimY;
925                 Region clearReg(Rect(mWidth, mHeight));
926                 clearReg.subtractSelf(Rect(xc, yc, xc+frame.trimWidth, yc+frame.trimHeight));
927                 if (!clearReg.isEmpty()) {
928                     Region::const_iterator head(clearReg.begin());
929                     Region::const_iterator tail(clearReg.end());
930                     glEnable(GL_SCISSOR_TEST);
931                     while (head != tail) {
932                         const Rect& r2(*head++);
933                         glScissor(r2.left, mHeight - r2.bottom, r2.width(), r2.height());
934                         glClear(GL_COLOR_BUFFER_BIT);
935                     }
936                     glDisable(GL_SCISSOR_TEST);
937                 }
938                 // specify the y center as ceiling((mHeight - frame.trimHeight) / 2)
939                 // which is equivalent to mHeight - (yc + frame.trimHeight)
940                 glDrawTexiOES(xc, mHeight - (yc + frame.trimHeight),
941                               0, frame.trimWidth, frame.trimHeight);
942                 if (mClockEnabled && mTimeIsAccurate && validClock(part)) {
943                     drawClock(animation.clockFont, part.clockPosX, part.clockPosY);
944                 }
945
946                 eglSwapBuffers(mDisplay, mSurface);
947
948                 nsecs_t now = systemTime();
949                 nsecs_t delay = frameDuration - (now - lastFrame);
950                 //ALOGD("%lld, %lld", ns2ms(now - lastFrame), ns2ms(delay));
951                 lastFrame = now;
952
953                 if (delay > 0) {
954                     struct timespec spec;
955                     spec.tv_sec  = (now + delay) / 1000000000;
956                     spec.tv_nsec = (now + delay) % 1000000000;
957                     int err;
958                     do {
959                         err = clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &spec, NULL);
960                     } while (err<0 && errno == EINTR);
961                 }
962
963                 checkExit();
964             }
965
966             usleep(part.pause * ns2us(frameDuration));
967
968             // For infinite parts, we've now played them at least once, so perhaps exit
969             if(exitPending() && !part.count)
970                 break;
971         }
972
973     }
974
975     // Free textures created for looping parts now that the animation is done.
976     for (const Animation::Part& part : animation.parts) {
977         if (part.count != 1) {
978             const size_t fcount = part.frames.size();
979             for (size_t j = 0; j < fcount; j++) {
980                 const Animation::Frame& frame(part.frames[j]);
981                 glDeleteTextures(1, &frame.tid);
982             }
983         }
984     }
985
986     // we've finally played everything we're going to play
987     audioplay::setPlaying(false);
988     audioplay::destroy();
989
990     return true;
991 }
992
993 void BootAnimation::releaseAnimation(Animation* animation) const
994 {
995     for (Vector<Animation::Part>::iterator it = animation->parts.begin(),
996          e = animation->parts.end(); it != e; ++it) {
997         if (it->animation)
998             releaseAnimation(it->animation);
999     }
1000     if (animation->zip)
1001         delete animation->zip;
1002     delete animation;
1003 }
1004
1005 BootAnimation::Animation* BootAnimation::loadAnimation(const String8& fn)
1006 {
1007     if (mLoadedFiles.indexOf(fn) >= 0) {
1008         ALOGE("File \"%s\" is already loaded. Cyclic ref is not allowed",
1009             fn.string());
1010         return NULL;
1011     }
1012     ZipFileRO *zip = ZipFileRO::open(fn);
1013     if (zip == NULL) {
1014         ALOGE("Failed to open animation zip \"%s\": %s",
1015             fn.string(), strerror(errno));
1016         return NULL;
1017     }
1018
1019     Animation *animation =  new Animation;
1020     animation->fileName = fn;
1021     animation->zip = zip;
1022     animation->clockFont.map = nullptr;
1023     mLoadedFiles.add(animation->fileName);
1024
1025     parseAnimationDesc(*animation);
1026     if (!preloadZip(*animation)) {
1027         return NULL;
1028     }
1029
1030
1031     mLoadedFiles.remove(fn);
1032     return animation;
1033 }
1034
1035 bool BootAnimation::playSoundsAllowed() const {
1036     // Only play sounds for system boots, not runtime restarts.
1037     if (!mSystemBoot) {
1038         return false;
1039     }
1040
1041     // Read the system property to see if we should play the sound.
1042     // If it's not present, default to allowed.
1043     if (!property_get_bool(PLAY_SOUND_PROP_NAME, 1)) {
1044         return false;
1045     }
1046
1047     // Don't play sounds if this is a reboot due to an error.
1048     char bootreason[PROPERTY_VALUE_MAX];
1049     if (property_get(BOOTREASON_PROP_NAME, bootreason, nullptr) > 0) {
1050         for (const auto& str : PLAY_SOUND_BOOTREASON_BLACKLIST) {
1051             if (strcasecmp(str.c_str(), bootreason) == 0) {
1052                 return false;
1053             }
1054         }
1055     }
1056     return true;
1057 }
1058
1059 bool BootAnimation::updateIsTimeAccurate() {
1060     static constexpr long long MAX_TIME_IN_PAST =   60000LL * 60LL * 24LL * 30LL;  // 30 days
1061     static constexpr long long MAX_TIME_IN_FUTURE = 60000LL * 90LL;  // 90 minutes
1062
1063     if (mTimeIsAccurate) {
1064         return true;
1065     }
1066
1067     struct stat statResult;
1068
1069     if(stat(TIME_FORMAT_12_HOUR_FLAG_FILE_PATH, &statResult) == 0) {
1070         mTimeFormat12Hour = true;
1071     }
1072
1073     if(stat(ACCURATE_TIME_FLAG_FILE_PATH, &statResult) == 0) {
1074         mTimeIsAccurate = true;
1075         return true;
1076     }
1077
1078     FILE* file = fopen(LAST_TIME_CHANGED_FILE_PATH, "r");
1079     if (file != NULL) {
1080       long long lastChangedTime = 0;
1081       fscanf(file, "%lld", &lastChangedTime);
1082       fclose(file);
1083       if (lastChangedTime > 0) {
1084         struct timespec now;
1085         clock_gettime(CLOCK_REALTIME, &now);
1086         // Match the Java timestamp format
1087         long long rtcNow = (now.tv_sec * 1000LL) + (now.tv_nsec / 1000000LL);
1088         if (ACCURATE_TIME_EPOCH < rtcNow
1089             && lastChangedTime > (rtcNow - MAX_TIME_IN_PAST)
1090             && lastChangedTime < (rtcNow + MAX_TIME_IN_FUTURE)) {
1091             mTimeIsAccurate = true;
1092         }
1093       }
1094     }
1095
1096     return mTimeIsAccurate;
1097 }
1098
1099 BootAnimation::TimeCheckThread::TimeCheckThread(BootAnimation* bootAnimation) : Thread(false),
1100     mInotifyFd(-1), mSystemWd(-1), mTimeWd(-1), mBootAnimation(bootAnimation) {}
1101
1102 BootAnimation::TimeCheckThread::~TimeCheckThread() {
1103     // mInotifyFd may be -1 but that's ok since we're not at risk of attempting to close a valid FD.
1104     close(mInotifyFd);
1105 }
1106
1107 bool BootAnimation::TimeCheckThread::threadLoop() {
1108     bool shouldLoop = doThreadLoop() && !mBootAnimation->mTimeIsAccurate
1109         && mBootAnimation->mClockEnabled;
1110     if (!shouldLoop) {
1111         close(mInotifyFd);
1112         mInotifyFd = -1;
1113     }
1114     return shouldLoop;
1115 }
1116
1117 bool BootAnimation::TimeCheckThread::doThreadLoop() {
1118     static constexpr int BUFF_LEN (10 * (sizeof(struct inotify_event) + NAME_MAX + 1));
1119
1120     // Poll instead of doing a blocking read so the Thread can exit if requested.
1121     struct pollfd pfd = { mInotifyFd, POLLIN, 0 };
1122     ssize_t pollResult = poll(&pfd, 1, 1000);
1123
1124     if (pollResult == 0) {
1125         return true;
1126     } else if (pollResult < 0) {
1127         ALOGE("Could not poll inotify events");
1128         return false;
1129     }
1130
1131     char buff[BUFF_LEN] __attribute__ ((aligned(__alignof__(struct inotify_event))));;
1132     ssize_t length = read(mInotifyFd, buff, BUFF_LEN);
1133     if (length == 0) {
1134         return true;
1135     } else if (length < 0) {
1136         ALOGE("Could not read inotify events");
1137         return false;
1138     }
1139
1140     const struct inotify_event *event;
1141     for (char* ptr = buff; ptr < buff + length; ptr += sizeof(struct inotify_event) + event->len) {
1142         event = (const struct inotify_event *) ptr;
1143         if (event->wd == mSystemWd && strcmp(SYSTEM_TIME_DIR_NAME, event->name) == 0) {
1144             addTimeDirWatch();
1145         } else if (event->wd == mTimeWd && (strcmp(LAST_TIME_CHANGED_FILE_NAME, event->name) == 0
1146                 || strcmp(ACCURATE_TIME_FLAG_FILE_NAME, event->name) == 0)) {
1147             return !mBootAnimation->updateIsTimeAccurate();
1148         }
1149     }
1150
1151     return true;
1152 }
1153
1154 void BootAnimation::TimeCheckThread::addTimeDirWatch() {
1155         mTimeWd = inotify_add_watch(mInotifyFd, SYSTEM_TIME_DIR_PATH,
1156                 IN_CLOSE_WRITE | IN_MOVED_TO | IN_ATTRIB);
1157         if (mTimeWd > 0) {
1158             // No need to watch for the time directory to be created if it already exists
1159             inotify_rm_watch(mInotifyFd, mSystemWd);
1160             mSystemWd = -1;
1161         }
1162 }
1163
1164 status_t BootAnimation::TimeCheckThread::readyToRun() {
1165     mInotifyFd = inotify_init();
1166     if (mInotifyFd < 0) {
1167         ALOGE("Could not initialize inotify fd");
1168         return NO_INIT;
1169     }
1170
1171     mSystemWd = inotify_add_watch(mInotifyFd, SYSTEM_DATA_DIR_PATH, IN_CREATE | IN_ATTRIB);
1172     if (mSystemWd < 0) {
1173         close(mInotifyFd);
1174         mInotifyFd = -1;
1175         ALOGE("Could not add watch for %s", SYSTEM_DATA_DIR_PATH);
1176         return NO_INIT;
1177     }
1178
1179     addTimeDirWatch();
1180
1181     if (mBootAnimation->updateIsTimeAccurate()) {
1182         close(mInotifyFd);
1183         mInotifyFd = -1;
1184         return ALREADY_EXISTS;
1185     }
1186
1187     return NO_ERROR;
1188 }
1189
1190 // ---------------------------------------------------------------------------
1191
1192 }
1193 ; // namespace android