blob: a893ec8b862b28036d60864d89a9019d44c93717 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
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
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -070017#define LOG_NDEBUG 0
Mathias Agopianbc726112009-09-23 15:44:05 -070018#define LOG_TAG "BootAnimation"
19
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080020#include <stdint.h>
Damien Bargiacchi97480862016-03-29 14:55:55 -070021#include <sys/inotify.h>
22#include <sys/poll.h>
23#include <sys/stat.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080024#include <sys/types.h>
25#include <math.h>
26#include <fcntl.h>
27#include <utils/misc.h>
Mathias Agopianb4d5a722009-09-23 17:05:19 -070028#include <signal.h>
Elliott Hughesbb94f312014-10-21 10:41:33 -070029#include <time.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080030
Jason parksbd9a08d2011-01-31 15:04:34 -060031#include <cutils/properties.h>
32
Mathias Agopianb13b9bd2012-02-17 18:27:36 -080033#include <androidfw/AssetManager.h>
Mathias Agopianac31a3b2009-05-21 19:59:24 -070034#include <binder/IPCThreadState.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080035#include <utils/Atomic.h>
36#include <utils/Errors.h>
37#include <utils/Log.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080038
39#include <ui/PixelFormat.h>
40#include <ui/Rect.h>
41#include <ui/Region.h>
42#include <ui/DisplayInfo.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080043
Jeff Brown0b722fe2012-08-24 22:40:14 -070044#include <gui/ISurfaceComposer.h>
Mathias Agopian8335f1c2012-02-25 18:48:35 -080045#include <gui/Surface.h>
46#include <gui/SurfaceComposerClient.h>
Mathias Agopian000479f2010-02-09 17:46:37 -080047
Andreas Gampecfedceb2014-09-30 21:48:18 -070048// TODO: Fix Skia.
49#pragma GCC diagnostic push
50#pragma GCC diagnostic ignored "-Wunused-parameter"
Derek Sollenbergereece0dd2014-02-27 14:31:29 -050051#include <SkBitmap.h>
52#include <SkStream.h>
53#include <SkImageDecoder.h>
Andreas Gampecfedceb2014-09-30 21:48:18 -070054#pragma GCC diagnostic pop
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080055
56#include <GLES/gl.h>
57#include <GLES/glext.h>
58#include <EGL/eglext.h>
59
60#include "BootAnimation.h"
Geoffrey Pitschd6d9a1d2016-06-08 00:38:58 -070061#include "audioplay.h"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080062
63namespace android {
64
Damien Bargiacchi97480862016-03-29 14:55:55 -070065static const char OEM_BOOTANIMATION_FILE[] = "/oem/media/bootanimation.zip";
66static const char SYSTEM_BOOTANIMATION_FILE[] = "/system/media/bootanimation.zip";
67static const char SYSTEM_ENCRYPTED_BOOTANIMATION_FILE[] = "/system/media/bootanimation-encrypted.zip";
68static const char SYSTEM_DATA_DIR_PATH[] = "/data/system";
69static const char SYSTEM_TIME_DIR_NAME[] = "time";
70static const char SYSTEM_TIME_DIR_PATH[] = "/data/system/time";
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -070071static const char CLOCK_FONT_ASSET[] = "images/clock_font.png";
72static const char CLOCK_FONT_ZIP_NAME[] = "clock_font.png";
Damien Bargiacchi97480862016-03-29 14:55:55 -070073static const char LAST_TIME_CHANGED_FILE_NAME[] = "last_time_change";
74static const char LAST_TIME_CHANGED_FILE_PATH[] = "/data/system/time/last_time_change";
75static const char ACCURATE_TIME_FLAG_FILE_NAME[] = "time_is_accurate";
76static const char ACCURATE_TIME_FLAG_FILE_PATH[] = "/data/system/time/time_is_accurate";
Damien Bargiacchi9071db12016-10-28 17:38:22 -070077static const char TIME_FORMAT_12_HOUR_FLAG_FILE_PATH[] = "/data/system/time/time_format_12_hour";
Damien Bargiacchi96762812016-07-12 15:53:40 -070078// Java timestamp format. Don't show the clock if the date is before 2000-01-01 00:00:00.
79static const long long ACCURATE_TIME_EPOCH = 946684800000;
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -070080static constexpr char FONT_BEGIN_CHAR = ' ';
81static constexpr char FONT_END_CHAR = '~' + 1;
82static constexpr size_t FONT_NUM_CHARS = FONT_END_CHAR - FONT_BEGIN_CHAR + 1;
83static constexpr size_t FONT_NUM_COLS = 16;
84static constexpr size_t FONT_NUM_ROWS = FONT_NUM_CHARS / FONT_NUM_COLS;
85static const int TEXT_CENTER_VALUE = INT_MAX;
86static const int TEXT_MISSING_VALUE = INT_MIN;
Damien Bargiacchi97480862016-03-29 14:55:55 -070087static const char EXIT_PROP_NAME[] = "service.bootanim.exit";
Geoffrey Pitsch30508792016-07-22 17:04:21 -040088static const char PLAY_SOUND_PROP_NAME[] = "persist.sys.bootanim.play_sound";
Narayan Kamathafd31e02013-12-03 13:16:03 +000089static const int ANIM_ENTRY_NAME_MAX = 256;
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -070090static constexpr size_t TEXT_POS_LEN_MAX = 16;
Geoffrey Pitsch290c4352016-08-09 14:35:10 -040091static const char BOOT_COMPLETED_PROP_NAME[] = "sys.boot_completed";
92static const char BOOTREASON_PROP_NAME[] = "ro.boot.bootreason";
93// bootreasons list in "system/core/bootstat/bootstat.cpp".
94static const std::vector<std::string> PLAY_SOUND_BOOTREASON_BLACKLIST {
95 "kernel_panic",
96 "Panic",
97 "Watchdog",
98};
Narayan Kamathafd31e02013-12-03 13:16:03 +000099
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800100// ---------------------------------------------------------------------------
101
Damien Bargiacchi97480862016-03-29 14:55:55 -0700102BootAnimation::BootAnimation() : Thread(false), mClockEnabled(true), mTimeIsAccurate(false),
Damien Bargiacchi9071db12016-10-28 17:38:22 -0700103 mTimeFormat12Hour(false), mTimeCheckThread(NULL) {
Mathias Agopian627e7b52009-05-21 19:21:59 -0700104 mSession = new SurfaceComposerClient();
Geoffrey Pitsch290c4352016-08-09 14:35:10 -0400105
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);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800108}
109
Damien Bargiacchi97480862016-03-29 14:55:55 -0700110BootAnimation::~BootAnimation() {}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800111
112void BootAnimation::onFirstRef() {
Mathias Agopianbc726112009-09-23 15:44:05 -0700113 status_t err = mSession->linkToComposerDeath(this);
Steve Block3762c312012-01-06 19:20:56 +0000114 ALOGE_IF(err, "linkToComposerDeath failed (%s) ", strerror(-err));
Mathias Agopian8434c532009-09-23 18:52:49 -0700115 if (err == NO_ERROR) {
Mathias Agopianbc726112009-09-23 15:44:05 -0700116 run("BootAnimation", PRIORITY_DISPLAY);
117 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800118}
119
Mathias Agopianbc726112009-09-23 15:44:05 -0700120sp<SurfaceComposerClient> BootAnimation::session() const {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800121 return mSession;
122}
123
Mathias Agopianbc726112009-09-23 15:44:05 -0700124
Narayan Kamathafd31e02013-12-03 13:16:03 +0000125void BootAnimation::binderDied(const wp<IBinder>&)
Mathias Agopianbc726112009-09-23 15:44:05 -0700126{
127 // woah, surfaceflinger died!
Steve Block5baa3a62011-12-20 16:23:08 +0000128 ALOGD("SurfaceFlinger died, exiting...");
Mathias Agopianbc726112009-09-23 15:44:05 -0700129
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();
Geoffrey Pitschd6d9a1d2016-06-08 00:38:58 -0700134 audioplay::destroy();
Mathias Agopianbc726112009-09-23 15:44:05 -0700135}
136
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800137status_t BootAnimation::initTexture(Texture* texture, AssetManager& assets,
138 const char* name) {
139 Asset* asset = assets.open(name, Asset::ACCESS_BUFFER);
Andreas Gampecfedceb2014-09-30 21:48:18 -0700140 if (asset == NULL)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800141 return NO_INIT;
142 SkBitmap bitmap;
143 SkImageDecoder::DecodeMemory(asset->getBuffer(false), asset->getLength(),
Mike Reed42a1d082014-07-07 18:06:18 -0400144 &bitmap, kUnknown_SkColorType, SkImageDecoder::kDecodePixels_Mode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800145 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
Mike Reed42a1d082014-07-07 18:06:18 -0400163 switch (bitmap.colorType()) {
164 case kAlpha_8_SkColorType:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800165 glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, w, h, 0, GL_ALPHA,
166 GL_UNSIGNED_BYTE, p);
167 break;
Mike Reed42a1d082014-07-07 18:06:18 -0400168 case kARGB_4444_SkColorType:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800169 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA,
170 GL_UNSIGNED_SHORT_4_4_4_4, p);
171 break;
Mike Reed42a1d082014-07-07 18:06:18 -0400172 case kN32_SkColorType:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800173 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA,
174 GL_UNSIGNED_BYTE, p);
175 break;
Mike Reed42a1d082014-07-07 18:06:18 -0400176 case kRGB_565_SkColorType:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800177 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);
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700189
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800190 return NO_ERROR;
191}
192
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700193status_t BootAnimation::initTexture(FileMap* map, int* width, int* height)
Mathias Agopiana8826d62009-10-01 03:10:14 -0700194{
Mathias Agopiana8826d62009-10-01 03:10:14 -0700195 SkBitmap bitmap;
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700196 SkMemoryStream stream(map->getDataPtr(), map->getDataLength());
Mathias Agopian2b99e552011-11-10 15:59:07 -0800197 SkImageDecoder* codec = SkImageDecoder::Factory(&stream);
Andreas Gampecfedceb2014-09-30 21:48:18 -0700198 if (codec != NULL) {
Elliott Hughesc367d482013-10-29 13:12:55 -0700199 codec->setDitherImage(false);
Mathias Agopian2b99e552011-11-10 15:59:07 -0800200 codec->decode(&stream, &bitmap,
Mike Reed42a1d082014-07-07 18:06:18 -0400201 kN32_SkColorType,
Mathias Agopian2b99e552011-11-10 15:59:07 -0800202 SkImageDecoder::kDecodePixels_Mode);
203 delete codec;
204 }
Mathias Agopiana8826d62009-10-01 03:10:14 -0700205
Mykola Kondratenko0c1eeb32014-04-15 09:35:44 +0200206 // 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.
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700209 delete map;
Mykola Kondratenko0c1eeb32014-04-15 09:35:44 +0200210
Mathias Agopiana8826d62009-10-01 03:10:14 -0700211 // 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
Mike Reed42a1d082014-07-07 18:06:18 -0400225 switch (bitmap.colorType()) {
226 case kN32_SkColorType:
Sai Kiran Korwar27167492015-07-07 20:00:06 +0530227 if (!mUseNpotTextures && (tw != w || th != h)) {
Mathias Agopiana8826d62009-10-01 03:10:14 -0700228 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 {
Sai Kiran Korwar27167492015-07-07 20:00:06 +0530233 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA,
Mathias Agopiana8826d62009-10-01 03:10:14 -0700234 GL_UNSIGNED_BYTE, p);
235 }
236 break;
237
Mike Reed42a1d082014-07-07 18:06:18 -0400238 case kRGB_565_SkColorType:
Sai Kiran Korwar27167492015-07-07 20:00:06 +0530239 if (!mUseNpotTextures && (tw != w || th != h)) {
Mathias Agopiana8826d62009-10-01 03:10:14 -0700240 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 {
Sai Kiran Korwar27167492015-07-07 20:00:06 +0530245 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, w, h, 0, GL_RGB,
Mathias Agopiana8826d62009-10-01 03:10:14 -0700246 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
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700255 *width = w;
256 *height = h;
257
Mathias Agopiana8826d62009-10-01 03:10:14 -0700258 return NO_ERROR;
259}
260
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800261status_t BootAnimation::readyToRun() {
262 mAssets.addDefaultAssets();
263
Jeff Brown0b722fe2012-08-24 22:40:14 -0700264 sp<IBinder> dtoken(SurfaceComposerClient::getBuiltInDisplay(
265 ISurfaceComposer::eDisplayIdMain));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800266 DisplayInfo dinfo;
Jeff Brown0b722fe2012-08-24 22:40:14 -0700267 status_t status = SurfaceComposerClient::getDisplayInfo(dtoken, &dinfo);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800268 if (status)
269 return -1;
270
271 // create the native surface
Jeff Brown0b722fe2012-08-24 22:40:14 -0700272 sp<SurfaceControl> control = session()->createSurface(String8("BootAnimation"),
273 dinfo.w, dinfo.h, PIXEL_FORMAT_RGB_565);
Mathias Agopian439863f2011-06-28 19:09:31 -0700274
275 SurfaceComposerClient::openGlobalTransaction();
Mathias Agopian17f638b2009-04-16 20:04:08 -0700276 control->setLayer(0x40000000);
Mathias Agopian439863f2011-06-28 19:09:31 -0700277 SurfaceComposerClient::closeGlobalTransaction();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800278
Mathias Agopian17f638b2009-04-16 20:04:08 -0700279 sp<Surface> s = control->getSurface();
280
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800281 // initialize opengl and egl
Mathias Agopian738b9a42009-08-06 16:41:02 -0700282 const EGLint attribs[] = {
Mathias Agopian1b253b72011-08-15 15:20:22 -0700283 EGL_RED_SIZE, 8,
284 EGL_GREEN_SIZE, 8,
285 EGL_BLUE_SIZE, 8,
Mathias Agopiana8826d62009-10-01 03:10:14 -0700286 EGL_DEPTH_SIZE, 0,
287 EGL_NONE
Mathias Agopian738b9a42009-08-06 16:41:02 -0700288 };
Andreas Gampecfedceb2014-09-30 21:48:18 -0700289 EGLint w, h;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800290 EGLint numConfigs;
291 EGLConfig config;
292 EGLSurface surface;
293 EGLContext context;
Mathias Agopian627e7b52009-05-21 19:21:59 -0700294
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800295 EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
Mathias Agopian627e7b52009-05-21 19:21:59 -0700296
297 eglInitialize(display, 0, 0);
Mathias Agopian1b253b72011-08-15 15:20:22 -0700298 eglChooseConfig(display, attribs, &config, 1, &numConfigs);
Mathias Agopian1473f462009-04-10 14:24:30 -0700299 surface = eglCreateWindowSurface(display, config, s.get(), NULL);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800300 context = eglCreateContext(display, config, NULL, NULL);
301 eglQuerySurface(display, surface, EGL_WIDTH, &w);
302 eglQuerySurface(display, surface, EGL_HEIGHT, &h);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700303
Mathias Agopianabac0102009-07-31 14:47:00 -0700304 if (eglMakeCurrent(display, surface, surface, context) == EGL_FALSE)
305 return NO_INIT;
Mathias Agopiana8826d62009-10-01 03:10:14 -0700306
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800307 mDisplay = display;
308 mContext = context;
309 mSurface = surface;
310 mWidth = w;
311 mHeight = h;
Mathias Agopian17f638b2009-04-16 20:04:08 -0700312 mFlingerSurfaceControl = control;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800313 mFlingerSurface = s;
314
Elliott Hughesc367d482013-10-29 13:12:55 -0700315 // If the device has encryption turned on or is in process
Jason parksbd9a08d2011-01-31 15:04:34 -0600316 // 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
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700322 if (encryptedAnimation && (access(SYSTEM_ENCRYPTED_BOOTANIMATION_FILE, R_OK) == 0)) {
323 mZipFileName = SYSTEM_ENCRYPTED_BOOTANIMATION_FILE;
Jason parksbd9a08d2011-01-31 15:04:34 -0600324 }
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700325 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 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800331 return NO_ERROR;
332}
333
Mathias Agopiana8826d62009-10-01 03:10:14 -0700334bool BootAnimation::threadLoop()
335{
336 bool r;
Narayan Kamathafd31e02013-12-03 13:16:03 +0000337 // We have no bootanimation file, so we use the stock android logo
338 // animation.
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700339 if (mZipFileName.isEmpty()) {
Mathias Agopiana8826d62009-10-01 03:10:14 -0700340 r = android();
341 } else {
342 r = movie();
343 }
344
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800345 eglMakeCurrent(mDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
346 eglDestroyContext(mDisplay, mContext);
347 eglDestroySurface(mDisplay, mSurface);
Mathias Agopian6cf0db22009-04-17 19:36:26 -0700348 mFlingerSurface.clear();
Mathias Agopian17f638b2009-04-16 20:04:08 -0700349 mFlingerSurfaceControl.clear();
Mathias Agopian627e7b52009-05-21 19:21:59 -0700350 eglTerminate(mDisplay);
351 IPCThreadState::self()->stopProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800352 return r;
353}
354
Mathias Agopiana8826d62009-10-01 03:10:14 -0700355bool BootAnimation::android()
356{
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700357 initTexture(&mAndroid[0], mAssets, "images/android-logo-mask.png");
358 initTexture(&mAndroid[1], mAssets, "images/android-logo-shine.png");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800359
360 // clear screen
Mathias Agopiana8826d62009-10-01 03:10:14 -0700361 glShadeModel(GL_FLAT);
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700362 glDisable(GL_DITHER);
363 glDisable(GL_SCISSOR_TEST);
Mathias Agopian59f19e42011-05-06 19:22:12 -0700364 glClearColor(0,0,0,1);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800365 glClear(GL_COLOR_BUFFER_BIT);
366 eglSwapBuffers(mDisplay, mSurface);
367
Mathias Agopiana8826d62009-10-01 03:10:14 -0700368 glEnable(GL_TEXTURE_2D);
369 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
370
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700371 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);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800374
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800375 glScissor(updateRect.left, mHeight - updateRect.bottom, updateRect.width(),
376 updateRect.height());
377
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700378 // Blend state
379 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
380 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
381
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800382 const nsecs_t startTime = systemTime();
383 do {
Mathias Agopian13796652009-03-24 22:49:21 -0700384 nsecs_t now = systemTime();
385 double time = now - startTime;
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700386 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;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800389
Mathias Agopian81668642009-07-28 11:41:30 -0700390 glDisable(GL_SCISSOR_TEST);
391 glClear(GL_COLOR_BUFFER_BIT);
392
393 glEnable(GL_SCISSOR_TEST);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800394 glDisable(GL_BLEND);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800395 glBindTexture(GL_TEXTURE_2D, mAndroid[1].name);
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700396 glDrawTexiOES(x, yc, 0, mAndroid[1].w, mAndroid[1].h);
397 glDrawTexiOES(x + mAndroid[1].w, yc, 0, mAndroid[1].w, mAndroid[1].h);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800398
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700399 glEnable(GL_BLEND);
400 glBindTexture(GL_TEXTURE_2D, mAndroid[0].name);
401 glDrawTexiOES(xc, yc, 0, mAndroid[0].w, mAndroid[0].h);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800402
Mathias Agopian627e7b52009-05-21 19:21:59 -0700403 EGLBoolean res = eglSwapBuffers(mDisplay, mSurface);
404 if (res == EGL_FALSE)
405 break;
406
Mathias Agopian13796652009-03-24 22:49:21 -0700407 // 12fps: don't animate too fast to preserve CPU
408 const nsecs_t sleepTime = 83333 - ns2us(systemTime() - now);
409 if (sleepTime > 0)
Mathias Agopiana8826d62009-10-01 03:10:14 -0700410 usleep(sleepTime);
Kevin Hesterd3782b22012-04-26 10:38:55 -0700411
412 checkExit();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800413 } while (!exitPending());
414
415 glDeleteTextures(1, &mAndroid[0].name);
416 glDeleteTextures(1, &mAndroid[1].name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800417 return false;
418}
419
Kevin Hesterd3782b22012-04-26 10:38:55 -0700420void 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
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700430bool BootAnimation::validClock(const Animation::Part& part) {
431 return part.clockPosX != TEXT_MISSING_VALUE && part.clockPosY != TEXT_MISSING_VALUE;
432}
433
434bool 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.
450void 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
Jesse Hall083b84c2014-09-22 10:51:09 -0700471// 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.
478static 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
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700496
497static bool readFile(ZipFileRO* zip, const char* name, String8& outString)
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700498{
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700499 ZipEntryRO entry = zip->findEntryByName(name);
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700500 ALOGE_IF(!entry, "couldn't find %s", name);
501 if (!entry) {
502 return false;
503 }
504
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700505 FileMap* entryMap = zip->createEntryFileMap(entry);
506 zip->releaseEntry(entry);
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700507 ALOGE_IF(!entryMap, "entryMap is null");
508 if (!entryMap) {
509 return false;
510 }
511
512 outString.setTo((char const*)entryMap->getDataPtr(), entryMap->getDataLength());
Narayan Kamath688ff4c2015-02-23 15:47:54 +0000513 delete entryMap;
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700514 return true;
515}
516
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700517// 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.
520status_t BootAnimation::initFont(Font* font, const char* fallback) {
521 status_t status = NO_ERROR;
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800522
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700523 if (font->map != nullptr) {
524 glGenTextures(1, &font->texture.name);
525 glBindTexture(GL_TEXTURE_2D, font->texture.name);
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800526
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700527 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;
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800537 }
538
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700539 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
547void 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
Damien Bargiacchi9071db12016-10-28 17:38:22 -0700593// We render 12 or 24 hour time.
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700594void BootAnimation::drawClock(const Font& font, const int xPos, const int yPos) {
Damien Bargiacchi9071db12016-10-28 17:38:22 -0700595 static constexpr char TIME_FORMAT_12[] = "%l:%M";
596 static constexpr char TIME_FORMAT_24[] = "%H:%M";
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700597 static constexpr int TIME_LENGTH = 6;
598
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800599 time_t rawtime;
600 time(&rawtime);
601 struct tm* timeInfo = localtime(&rawtime);
602
603 char timeBuff[TIME_LENGTH];
Damien Bargiacchi9071db12016-10-28 17:38:22 -0700604 const char* timeFormat = mTimeFormat12Hour ? TIME_FORMAT_12 : TIME_FORMAT_24;
605 size_t length = strftime(timeBuff, TIME_LENGTH, timeFormat, timeInfo);
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800606
607 if (length != TIME_LENGTH - 1) {
608 ALOGE("Couldn't format time; abandoning boot animation clock");
609 mClockEnabled = false;
610 return;
611 }
612
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700613 int x = xPos;
614 int y = yPos;
615 drawText(timeBuff, font, false, &x, &y);
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800616}
617
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700618bool BootAnimation::parseAnimationDesc(Animation& animation)
Mathias Agopiana8826d62009-10-01 03:10:14 -0700619{
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700620 String8 desString;
621
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700622 if (!readFile(animation.zip, "desc.txt", desString)) {
Narayan Kamathafd31e02013-12-03 13:16:03 +0000623 return false;
624 }
Mathias Agopiana8826d62009-10-01 03:10:14 -0700625 char const* s = desString.string();
626
Mathias Agopiana8826d62009-10-01 03:10:14 -0700627 // Parse the description file
628 for (;;) {
629 const char* endl = strstr(s, "\n");
Andreas Gampecfedceb2014-09-30 21:48:18 -0700630 if (endl == NULL) break;
Mathias Agopiana8826d62009-10-01 03:10:14 -0700631 String8 line(s, endl - s);
632 const char* l = line.string();
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800633 int fps = 0;
634 int width = 0;
635 int height = 0;
636 int count = 0;
637 int pause = 0;
Narayan Kamathafd31e02013-12-03 13:16:03 +0000638 char path[ANIM_ENTRY_NAME_MAX];
Jesse Hall083b84c2014-09-22 10:51:09 -0700639 char color[7] = "000000"; // default to black if unspecified
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700640 char clockPos1[TEXT_POS_LEN_MAX + 1] = "";
641 char clockPos2[TEXT_POS_LEN_MAX + 1] = "";
Jesse Hall083b84c2014-09-22 10:51:09 -0700642
Kevin Hesterd3782b22012-04-26 10:38:55 -0700643 char pathType;
Mathias Agopiana8826d62009-10-01 03:10:14 -0700644 if (sscanf(l, "%d %d %d", &width, &height, &fps) == 3) {
Jesse Hall083b84c2014-09-22 10:51:09 -0700645 // ALOGD("> w=%d, h=%d, fps=%d", width, height, fps);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700646 animation.width = width;
647 animation.height = height;
648 animation.fps = fps;
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700649 } else if (sscanf(l, " %c %d %d %s #%6s %16s %16s",
650 &pathType, &count, &pause, path, color, clockPos1, clockPos2) >= 4) {
651 //ALOGD("> type=%c, count=%d, pause=%d, path=%s, color=%s, clockPos1=%s, clockPos2=%s",
652 // pathType, count, pause, path, color, clockPos1, clockPos2);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700653 Animation::Part part;
Kevin Hesterd3782b22012-04-26 10:38:55 -0700654 part.playUntilComplete = pathType == 'c';
Mathias Agopiana8826d62009-10-01 03:10:14 -0700655 part.count = count;
656 part.pause = pause;
657 part.path = path;
Geoffrey Pitschd6d9a1d2016-06-08 00:38:58 -0700658 part.audioData = NULL;
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700659 part.animation = NULL;
Jesse Hall083b84c2014-09-22 10:51:09 -0700660 if (!parseColor(color, part.backgroundColor)) {
661 ALOGE("> invalid color '#%s'", color);
662 part.backgroundColor[0] = 0.0f;
663 part.backgroundColor[1] = 0.0f;
664 part.backgroundColor[2] = 0.0f;
665 }
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700666 parsePosition(clockPos1, clockPos2, &part.clockPosX, &part.clockPosY);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700667 animation.parts.add(part);
668 }
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700669 else if (strcmp(l, "$SYSTEM") == 0) {
670 // ALOGD("> SYSTEM");
671 Animation::Part part;
672 part.playUntilComplete = false;
673 part.count = 1;
674 part.pause = 0;
Geoffrey Pitschd6d9a1d2016-06-08 00:38:58 -0700675 part.audioData = NULL;
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700676 part.animation = loadAnimation(String8(SYSTEM_BOOTANIMATION_FILE));
677 if (part.animation != NULL)
678 animation.parts.add(part);
679 }
Mathias Agopiana8826d62009-10-01 03:10:14 -0700680 s = ++endl;
681 }
682
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700683 return true;
684}
685
686bool BootAnimation::preloadZip(Animation& animation)
687{
Mathias Agopiana8826d62009-10-01 03:10:14 -0700688 // read all the data structures
689 const size_t pcount = animation.parts.size();
Narayan Kamathafd31e02013-12-03 13:16:03 +0000690 void *cookie = NULL;
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400691 ZipFileRO* zip = animation.zip;
692 if (!zip->startIteration(&cookie)) {
Narayan Kamathafd31e02013-12-03 13:16:03 +0000693 return false;
694 }
695
Geoffrey Pitscha91a2d72016-07-12 14:46:19 -0400696 Animation::Part* partWithAudio = NULL;
Narayan Kamathafd31e02013-12-03 13:16:03 +0000697 ZipEntryRO entry;
698 char name[ANIM_ENTRY_NAME_MAX];
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400699 while ((entry = zip->nextEntry(cookie)) != NULL) {
700 const int foundEntryName = zip->getEntryFileName(entry, name, ANIM_ENTRY_NAME_MAX);
Narayan Kamathafd31e02013-12-03 13:16:03 +0000701 if (foundEntryName > ANIM_ENTRY_NAME_MAX || foundEntryName == -1) {
702 ALOGE("Error fetching entry file name");
703 continue;
704 }
705
706 const String8 entryName(name);
707 const String8 path(entryName.getPathDir());
708 const String8 leaf(entryName.getPathLeaf());
709 if (leaf.size() > 0) {
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700710 if (entryName == CLOCK_FONT_ZIP_NAME) {
711 FileMap* map = zip->createEntryFileMap(entry);
712 if (map) {
713 animation.clockFont.map = map;
714 }
715 continue;
716 }
717
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400718 for (size_t j = 0; j < pcount; j++) {
Narayan Kamathafd31e02013-12-03 13:16:03 +0000719 if (path == animation.parts[j].path) {
Narayan Kamath4600dd02015-06-16 12:02:57 +0100720 uint16_t method;
Narayan Kamathafd31e02013-12-03 13:16:03 +0000721 // supports only stored png files
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400722 if (zip->getEntryInfo(entry, &method, NULL, NULL, NULL, NULL, NULL)) {
Narayan Kamathafd31e02013-12-03 13:16:03 +0000723 if (method == ZipFileRO::kCompressStored) {
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400724 FileMap* map = zip->createEntryFileMap(entry);
Narayan Kamathafd31e02013-12-03 13:16:03 +0000725 if (map) {
Narayan Kamathafd31e02013-12-03 13:16:03 +0000726 Animation::Part& part(animation.parts.editItemAt(j));
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700727 if (leaf == "audio.wav") {
728 // a part may have at most one audio file
Geoffrey Pitschd6d9a1d2016-06-08 00:38:58 -0700729 part.audioData = (uint8_t *)map->getDataPtr();
730 part.audioLength = map->getDataLength();
Geoffrey Pitscha91a2d72016-07-12 14:46:19 -0400731 partWithAudio = &part;
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400732 } else if (leaf == "trim.txt") {
733 part.trimData.setTo((char const*)map->getDataPtr(),
734 map->getDataLength());
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700735 } else {
736 Animation::Frame frame;
737 frame.name = leaf;
738 frame.map = map;
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400739 frame.trimWidth = animation.width;
740 frame.trimHeight = animation.height;
741 frame.trimX = 0;
742 frame.trimY = 0;
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700743 part.frames.add(frame);
744 }
Mathias Agopiana8826d62009-10-01 03:10:14 -0700745 }
Geoffrey Pitschd6d9a1d2016-06-08 00:38:58 -0700746 } else {
747 ALOGE("bootanimation.zip is compressed; must be only stored");
Mathias Agopiana8826d62009-10-01 03:10:14 -0700748 }
749 }
750 }
751 }
752 }
753 }
754
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400755 // If there is trimData present, override the positioning defaults.
756 for (Animation::Part& part : animation.parts) {
757 const char* trimDataStr = part.trimData.string();
758 for (size_t frameIdx = 0; frameIdx < part.frames.size(); frameIdx++) {
759 const char* endl = strstr(trimDataStr, "\n");
760 // No more trimData for this part.
761 if (endl == NULL) {
762 break;
763 }
764 String8 line(trimDataStr, endl - trimDataStr);
765 const char* lineStr = line.string();
766 trimDataStr = ++endl;
767 int width = 0, height = 0, x = 0, y = 0;
768 if (sscanf(lineStr, "%dx%d+%d+%d", &width, &height, &x, &y) == 4) {
769 Animation::Frame& frame(part.frames.editItemAt(frameIdx));
770 frame.trimWidth = width;
771 frame.trimHeight = height;
772 frame.trimX = x;
773 frame.trimY = y;
774 } else {
775 ALOGE("Error parsing trim.txt, line: %s", lineStr);
776 break;
777 }
778 }
779 }
780
Geoffrey Pitschd6d9a1d2016-06-08 00:38:58 -0700781 // Create and initialize audioplay if there is a wav file in any of the animations.
Geoffrey Pitscha91a2d72016-07-12 14:46:19 -0400782 if (partWithAudio != NULL) {
Geoffrey Pitschd6d9a1d2016-06-08 00:38:58 -0700783 ALOGD("found audio.wav, creating playback engine");
Geoffrey Pitscha91a2d72016-07-12 14:46:19 -0400784 if (!audioplay::create(partWithAudio->audioData, partWithAudio->audioLength)) {
785 return false;
786 }
Geoffrey Pitschd6d9a1d2016-06-08 00:38:58 -0700787 }
788
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400789 zip->endIteration(cookie);
Narayan Kamathafd31e02013-12-03 13:16:03 +0000790
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700791 return true;
792}
793
794bool BootAnimation::movie()
795{
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700796 Animation* animation = loadAnimation(mZipFileName);
797 if (animation == NULL)
798 return false;
799
Damien Bargiacchi97480862016-03-29 14:55:55 -0700800 bool anyPartHasClock = false;
801 for (size_t i=0; i < animation->parts.size(); i++) {
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700802 if(validClock(animation->parts[i])) {
Damien Bargiacchi97480862016-03-29 14:55:55 -0700803 anyPartHasClock = true;
804 break;
805 }
806 }
807 if (!anyPartHasClock) {
808 mClockEnabled = false;
809 }
810
Sai Kiran Korwar27167492015-07-07 20:00:06 +0530811 // Check if npot textures are supported
812 mUseNpotTextures = false;
813 String8 gl_extensions;
814 const char* exts = reinterpret_cast<const char*>(glGetString(GL_EXTENSIONS));
815 if (!exts) {
816 glGetError();
817 } else {
818 gl_extensions.setTo(exts);
819 if ((gl_extensions.find("GL_ARB_texture_non_power_of_two") != -1) ||
820 (gl_extensions.find("GL_OES_texture_npot") != -1)) {
821 mUseNpotTextures = true;
822 }
823 }
824
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800825 // Blend required to draw time on top of animation frames.
826 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700827 glShadeModel(GL_FLAT);
828 glDisable(GL_DITHER);
829 glDisable(GL_SCISSOR_TEST);
830 glDisable(GL_BLEND);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700831
832 glBindTexture(GL_TEXTURE_2D, 0);
833 glEnable(GL_TEXTURE_2D);
834 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
835 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
836 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
837 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
838 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
839
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700840 bool clockFontInitialized = false;
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800841 if (mClockEnabled) {
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700842 clockFontInitialized =
843 (initFont(&animation->clockFont, CLOCK_FONT_ASSET) == NO_ERROR);
844 mClockEnabled = clockFontInitialized;
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800845 }
846
Damien Bargiacchi97480862016-03-29 14:55:55 -0700847 if (mClockEnabled && !updateIsTimeAccurate()) {
848 mTimeCheckThread = new TimeCheckThread(this);
849 mTimeCheckThread->run("BootAnimation::TimeCheckThread", PRIORITY_NORMAL);
850 }
851
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700852 playAnimation(*animation);
Damien Bargiacchi97480862016-03-29 14:55:55 -0700853
854 if (mTimeCheckThread != NULL) {
855 mTimeCheckThread->requestExit();
856 mTimeCheckThread = NULL;
857 }
858
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700859 releaseAnimation(animation);
860
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700861 if (clockFontInitialized) {
862 glDeleteTextures(1, &animation->clockFont.texture.name);
Andriy Naborskyy815e51d2016-03-24 16:43:34 -0700863 }
864
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700865 return false;
866}
867
868bool BootAnimation::playAnimation(const Animation& animation)
869{
870 const size_t pcount = animation.parts.size();
Mathias Agopiana8826d62009-10-01 03:10:14 -0700871 nsecs_t frameDuration = s2ns(1) / animation.fps;
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400872 const int animationX = (mWidth - animation.width) / 2;
873 const int animationY = (mHeight - animation.height) / 2;
Mathias Agopian9f3020d2009-11-06 16:30:18 -0800874
Narayan Kamathafd31e02013-12-03 13:16:03 +0000875 for (size_t i=0 ; i<pcount ; i++) {
Mathias Agopiana8826d62009-10-01 03:10:14 -0700876 const Animation::Part& part(animation.parts[i]);
877 const size_t fcount = part.frames.size();
878 glBindTexture(GL_TEXTURE_2D, 0);
879
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700880 // Handle animation package
881 if (part.animation != NULL) {
882 playAnimation(*part.animation);
883 if (exitPending())
884 break;
885 continue; //to next part
886 }
887
Mathias Agopiana8826d62009-10-01 03:10:14 -0700888 for (int r=0 ; !part.count || r<part.count ; r++) {
Kevin Hesterd3782b22012-04-26 10:38:55 -0700889 // Exit any non playuntil complete parts immediately
890 if(exitPending() && !part.playUntilComplete)
891 break;
892
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700893 // only play audio file the first time we animate the part
Geoffrey Pitsch290c4352016-08-09 14:35:10 -0400894 if (r == 0 && part.audioData && playSoundsAllowed()) {
895 ALOGD("playing clip for part%d, size=%d", (int) i, part.audioLength);
896 audioplay::playClip(part.audioData, part.audioLength);
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700897 }
898
Jesse Hall083b84c2014-09-22 10:51:09 -0700899 glClearColor(
900 part.backgroundColor[0],
901 part.backgroundColor[1],
902 part.backgroundColor[2],
903 1.0f);
904
Narayan Kamathafd31e02013-12-03 13:16:03 +0000905 for (size_t j=0 ; j<fcount && (!exitPending() || part.playUntilComplete) ; j++) {
Mathias Agopiana8826d62009-10-01 03:10:14 -0700906 const Animation::Frame& frame(part.frames[j]);
Mathias Agopiandb7dd2a2012-05-12 15:08:21 -0700907 nsecs_t lastFrame = systemTime();
Mathias Agopiana8826d62009-10-01 03:10:14 -0700908
909 if (r > 0) {
910 glBindTexture(GL_TEXTURE_2D, frame.tid);
911 } else {
912 if (part.count != 1) {
913 glGenTextures(1, &frame.tid);
914 glBindTexture(GL_TEXTURE_2D, frame.tid);
915 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
916 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
917 }
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700918 int w, h;
919 initTexture(frame.map, &w, &h);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700920 }
921
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400922 const int xc = animationX + frame.trimX;
923 const int yc = animationY + frame.trimY;
924 Region clearReg(Rect(mWidth, mHeight));
925 clearReg.subtractSelf(Rect(xc, yc, xc+frame.trimWidth, yc+frame.trimHeight));
Mathias Agopian9f3020d2009-11-06 16:30:18 -0800926 if (!clearReg.isEmpty()) {
927 Region::const_iterator head(clearReg.begin());
928 Region::const_iterator tail(clearReg.end());
929 glEnable(GL_SCISSOR_TEST);
930 while (head != tail) {
Andreas Gampecfedceb2014-09-30 21:48:18 -0700931 const Rect& r2(*head++);
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400932 glScissor(r2.left, mHeight - r2.bottom, r2.width(), r2.height());
Mathias Agopian9f3020d2009-11-06 16:30:18 -0800933 glClear(GL_COLOR_BUFFER_BIT);
934 }
935 glDisable(GL_SCISSOR_TEST);
936 }
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400937 // specify the y center as ceiling((mHeight - frame.trimHeight) / 2)
938 // which is equivalent to mHeight - (yc + frame.trimHeight)
939 glDrawTexiOES(xc, mHeight - (yc + frame.trimHeight),
940 0, frame.trimWidth, frame.trimHeight);
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700941 if (mClockEnabled && mTimeIsAccurate && validClock(part)) {
942 drawClock(animation.clockFont, part.clockPosX, part.clockPosY);
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800943 }
944
Mathias Agopiana8826d62009-10-01 03:10:14 -0700945 eglSwapBuffers(mDisplay, mSurface);
946
947 nsecs_t now = systemTime();
948 nsecs_t delay = frameDuration - (now - lastFrame);
Mathias Agopiandb7dd2a2012-05-12 15:08:21 -0700949 //ALOGD("%lld, %lld", ns2ms(now - lastFrame), ns2ms(delay));
Mathias Agopiana8826d62009-10-01 03:10:14 -0700950 lastFrame = now;
Mathias Agopiandb7dd2a2012-05-12 15:08:21 -0700951
952 if (delay > 0) {
953 struct timespec spec;
954 spec.tv_sec = (now + delay) / 1000000000;
955 spec.tv_nsec = (now + delay) % 1000000000;
956 int err;
957 do {
958 err = clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &spec, NULL);
959 } while (err<0 && errno == EINTR);
960 }
Kevin Hesterd3782b22012-04-26 10:38:55 -0700961
962 checkExit();
Mathias Agopiana8826d62009-10-01 03:10:14 -0700963 }
Kevin Hesterd3782b22012-04-26 10:38:55 -0700964
Mathias Agopiana8826d62009-10-01 03:10:14 -0700965 usleep(part.pause * ns2us(frameDuration));
Kevin Hesterd3782b22012-04-26 10:38:55 -0700966
967 // For infinite parts, we've now played them at least once, so perhaps exit
968 if(exitPending() && !part.count)
969 break;
Mathias Agopiana8826d62009-10-01 03:10:14 -0700970 }
971
Geoffrey Pitsch2fb30fb2016-07-06 16:16:20 -0400972 }
973
974 // Free textures created for looping parts now that the animation is done.
975 for (const Animation::Part& part : animation.parts) {
Mathias Agopiana8826d62009-10-01 03:10:14 -0700976 if (part.count != 1) {
Geoffrey Pitsch2fb30fb2016-07-06 16:16:20 -0400977 const size_t fcount = part.frames.size();
978 for (size_t j = 0; j < fcount; j++) {
Mathias Agopiana8826d62009-10-01 03:10:14 -0700979 const Animation::Frame& frame(part.frames[j]);
980 glDeleteTextures(1, &frame.tid);
981 }
982 }
983 }
Geoffrey Pitschd6d9a1d2016-06-08 00:38:58 -0700984
985 // we've finally played everything we're going to play
986 audioplay::setPlaying(false);
987 audioplay::destroy();
988
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700989 return true;
Mathias Agopiana8826d62009-10-01 03:10:14 -0700990}
991
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700992void BootAnimation::releaseAnimation(Animation* animation) const
993{
994 for (Vector<Animation::Part>::iterator it = animation->parts.begin(),
995 e = animation->parts.end(); it != e; ++it) {
996 if (it->animation)
997 releaseAnimation(it->animation);
998 }
999 if (animation->zip)
1000 delete animation->zip;
1001 delete animation;
1002}
1003
1004BootAnimation::Animation* BootAnimation::loadAnimation(const String8& fn)
1005{
1006 if (mLoadedFiles.indexOf(fn) >= 0) {
1007 ALOGE("File \"%s\" is already loaded. Cyclic ref is not allowed",
1008 fn.string());
1009 return NULL;
1010 }
1011 ZipFileRO *zip = ZipFileRO::open(fn);
1012 if (zip == NULL) {
1013 ALOGE("Failed to open animation zip \"%s\": %s",
1014 fn.string(), strerror(errno));
1015 return NULL;
1016 }
1017
1018 Animation *animation = new Animation;
1019 animation->fileName = fn;
1020 animation->zip = zip;
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -07001021 animation->clockFont.map = nullptr;
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001022 mLoadedFiles.add(animation->fileName);
1023
1024 parseAnimationDesc(*animation);
Geoffrey Pitscha91a2d72016-07-12 14:46:19 -04001025 if (!preloadZip(*animation)) {
1026 return NULL;
1027 }
1028
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001029
1030 mLoadedFiles.remove(fn);
1031 return animation;
1032}
Damien Bargiacchi97480862016-03-29 14:55:55 -07001033
Geoffrey Pitsch290c4352016-08-09 14:35:10 -04001034bool BootAnimation::playSoundsAllowed() const {
1035 // Only play sounds for system boots, not runtime restarts.
1036 if (!mSystemBoot) {
1037 return false;
1038 }
1039
1040 // Read the system property to see if we should play the sound.
1041 // If it's not present, default to allowed.
1042 if (!property_get_bool(PLAY_SOUND_PROP_NAME, 1)) {
1043 return false;
1044 }
1045
1046 // Don't play sounds if this is a reboot due to an error.
1047 char bootreason[PROPERTY_VALUE_MAX];
1048 if (property_get(BOOTREASON_PROP_NAME, bootreason, nullptr) > 0) {
1049 for (const auto& str : PLAY_SOUND_BOOTREASON_BLACKLIST) {
1050 if (strcasecmp(str.c_str(), bootreason) == 0) {
1051 return false;
1052 }
1053 }
1054 }
1055 return true;
1056}
1057
Damien Bargiacchi97480862016-03-29 14:55:55 -07001058bool BootAnimation::updateIsTimeAccurate() {
1059 static constexpr long long MAX_TIME_IN_PAST = 60000LL * 60LL * 24LL * 30LL; // 30 days
1060 static constexpr long long MAX_TIME_IN_FUTURE = 60000LL * 90LL; // 90 minutes
1061
1062 if (mTimeIsAccurate) {
1063 return true;
1064 }
1065
1066 struct stat statResult;
Damien Bargiacchi9071db12016-10-28 17:38:22 -07001067
1068 if(stat(TIME_FORMAT_12_HOUR_FLAG_FILE_PATH, &statResult) == 0) {
1069 mTimeFormat12Hour = true;
1070 }
1071
Damien Bargiacchi97480862016-03-29 14:55:55 -07001072 if(stat(ACCURATE_TIME_FLAG_FILE_PATH, &statResult) == 0) {
1073 mTimeIsAccurate = true;
1074 return true;
1075 }
1076
1077 FILE* file = fopen(LAST_TIME_CHANGED_FILE_PATH, "r");
1078 if (file != NULL) {
1079 long long lastChangedTime = 0;
1080 fscanf(file, "%lld", &lastChangedTime);
1081 fclose(file);
1082 if (lastChangedTime > 0) {
1083 struct timespec now;
1084 clock_gettime(CLOCK_REALTIME, &now);
1085 // Match the Java timestamp format
1086 long long rtcNow = (now.tv_sec * 1000LL) + (now.tv_nsec / 1000000LL);
Damien Bargiacchi96762812016-07-12 15:53:40 -07001087 if (ACCURATE_TIME_EPOCH < rtcNow
1088 && lastChangedTime > (rtcNow - MAX_TIME_IN_PAST)
1089 && lastChangedTime < (rtcNow + MAX_TIME_IN_FUTURE)) {
Damien Bargiacchi97480862016-03-29 14:55:55 -07001090 mTimeIsAccurate = true;
1091 }
1092 }
1093 }
1094
1095 return mTimeIsAccurate;
1096}
1097
1098BootAnimation::TimeCheckThread::TimeCheckThread(BootAnimation* bootAnimation) : Thread(false),
1099 mInotifyFd(-1), mSystemWd(-1), mTimeWd(-1), mBootAnimation(bootAnimation) {}
1100
1101BootAnimation::TimeCheckThread::~TimeCheckThread() {
1102 // mInotifyFd may be -1 but that's ok since we're not at risk of attempting to close a valid FD.
1103 close(mInotifyFd);
1104}
1105
1106bool BootAnimation::TimeCheckThread::threadLoop() {
1107 bool shouldLoop = doThreadLoop() && !mBootAnimation->mTimeIsAccurate
1108 && mBootAnimation->mClockEnabled;
1109 if (!shouldLoop) {
1110 close(mInotifyFd);
1111 mInotifyFd = -1;
1112 }
1113 return shouldLoop;
1114}
1115
1116bool BootAnimation::TimeCheckThread::doThreadLoop() {
1117 static constexpr int BUFF_LEN (10 * (sizeof(struct inotify_event) + NAME_MAX + 1));
1118
1119 // Poll instead of doing a blocking read so the Thread can exit if requested.
1120 struct pollfd pfd = { mInotifyFd, POLLIN, 0 };
1121 ssize_t pollResult = poll(&pfd, 1, 1000);
1122
1123 if (pollResult == 0) {
1124 return true;
1125 } else if (pollResult < 0) {
1126 ALOGE("Could not poll inotify events");
1127 return false;
1128 }
1129
1130 char buff[BUFF_LEN] __attribute__ ((aligned(__alignof__(struct inotify_event))));;
1131 ssize_t length = read(mInotifyFd, buff, BUFF_LEN);
1132 if (length == 0) {
1133 return true;
1134 } else if (length < 0) {
1135 ALOGE("Could not read inotify events");
1136 return false;
1137 }
1138
1139 const struct inotify_event *event;
1140 for (char* ptr = buff; ptr < buff + length; ptr += sizeof(struct inotify_event) + event->len) {
1141 event = (const struct inotify_event *) ptr;
1142 if (event->wd == mSystemWd && strcmp(SYSTEM_TIME_DIR_NAME, event->name) == 0) {
1143 addTimeDirWatch();
1144 } else if (event->wd == mTimeWd && (strcmp(LAST_TIME_CHANGED_FILE_NAME, event->name) == 0
1145 || strcmp(ACCURATE_TIME_FLAG_FILE_NAME, event->name) == 0)) {
1146 return !mBootAnimation->updateIsTimeAccurate();
1147 }
1148 }
1149
1150 return true;
1151}
1152
1153void BootAnimation::TimeCheckThread::addTimeDirWatch() {
1154 mTimeWd = inotify_add_watch(mInotifyFd, SYSTEM_TIME_DIR_PATH,
1155 IN_CLOSE_WRITE | IN_MOVED_TO | IN_ATTRIB);
1156 if (mTimeWd > 0) {
1157 // No need to watch for the time directory to be created if it already exists
1158 inotify_rm_watch(mInotifyFd, mSystemWd);
1159 mSystemWd = -1;
1160 }
1161}
1162
1163status_t BootAnimation::TimeCheckThread::readyToRun() {
1164 mInotifyFd = inotify_init();
1165 if (mInotifyFd < 0) {
1166 ALOGE("Could not initialize inotify fd");
1167 return NO_INIT;
1168 }
1169
1170 mSystemWd = inotify_add_watch(mInotifyFd, SYSTEM_DATA_DIR_PATH, IN_CREATE | IN_ATTRIB);
1171 if (mSystemWd < 0) {
1172 close(mInotifyFd);
1173 mInotifyFd = -1;
1174 ALOGE("Could not add watch for %s", SYSTEM_DATA_DIR_PATH);
1175 return NO_INIT;
1176 }
1177
1178 addTimeDirWatch();
1179
1180 if (mBootAnimation->updateIsTimeAccurate()) {
1181 close(mInotifyFd);
1182 mInotifyFd = -1;
1183 return ALREADY_EXISTS;
1184 }
1185
1186 return NO_ERROR;
1187}
1188
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001189// ---------------------------------------------------------------------------
1190
1191}
1192; // namespace android