blob: db384baff4d72d48d89ec2dfeed719f93f7c1189 [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>
Wei Wanga90c54c2017-02-02 11:35:21 -080021#include <inttypes.h>
Damien Bargiacchi97480862016-03-29 14:55:55 -070022#include <sys/inotify.h>
23#include <sys/poll.h>
24#include <sys/stat.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080025#include <sys/types.h>
26#include <math.h>
27#include <fcntl.h>
28#include <utils/misc.h>
Mathias Agopianb4d5a722009-09-23 17:05:19 -070029#include <signal.h>
Elliott Hughesbb94f312014-10-21 10:41:33 -070030#include <time.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080031
Steven Morelandfb7952f2018-02-23 14:58:50 -080032#include <cutils/atomic.h>
Jason parksbd9a08d2011-01-31 15:04:34 -060033#include <cutils/properties.h>
34
Mathias Agopianb13b9bd2012-02-17 18:27:36 -080035#include <androidfw/AssetManager.h>
Mathias Agopianac31a3b2009-05-21 19:59:24 -070036#include <binder/IPCThreadState.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080037#include <utils/Errors.h>
38#include <utils/Log.h>
Wei Wanga90c54c2017-02-02 11:35:21 -080039#include <utils/SystemClock.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080040
Keun-young Parkb5938422017-03-23 13:46:24 -070041#include <android-base/properties.h>
42
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080043#include <ui/PixelFormat.h>
44#include <ui/Rect.h>
45#include <ui/Region.h>
46#include <ui/DisplayInfo.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080047
Jeff Brown0b722fe2012-08-24 22:40:14 -070048#include <gui/ISurfaceComposer.h>
Mathias Agopian8335f1c2012-02-25 18:48:35 -080049#include <gui/Surface.h>
50#include <gui/SurfaceComposerClient.h>
Mathias Agopian000479f2010-02-09 17:46:37 -080051
Andreas Gampecfedceb2014-09-30 21:48:18 -070052// TODO: Fix Skia.
53#pragma GCC diagnostic push
54#pragma GCC diagnostic ignored "-Wunused-parameter"
Derek Sollenbergereece0dd2014-02-27 14:31:29 -050055#include <SkBitmap.h>
Matt Sarett8898c162016-03-24 16:11:01 -040056#include <SkImage.h>
Derek Sollenbergereece0dd2014-02-27 14:31:29 -050057#include <SkStream.h>
Andreas Gampecfedceb2014-09-30 21:48:18 -070058#pragma GCC diagnostic pop
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080059
60#include <GLES/gl.h>
61#include <GLES/glext.h>
62#include <EGL/eglext.h>
63
64#include "BootAnimation.h"
65
Kyeongkab.Nam35a8a112019-07-17 16:41:48 +090066#define ANIM_PATH_MAX 255
67#define STR(x) #x
68#define STRTO(x) STR(x)
69
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080070namespace android {
71
Damien Bargiacchi97480862016-03-29 14:55:55 -070072static const char OEM_BOOTANIMATION_FILE[] = "/oem/media/bootanimation.zip";
Beverly34ffe252019-05-17 11:03:54 -040073static const char PRODUCT_BOOTANIMATION_DARK_FILE[] = "/product/media/bootanimation-dark.zip";
Jaekyun Seokc521bb32018-01-30 11:52:47 +090074static const char PRODUCT_BOOTANIMATION_FILE[] = "/product/media/bootanimation.zip";
Damien Bargiacchi97480862016-03-29 14:55:55 -070075static const char SYSTEM_BOOTANIMATION_FILE[] = "/system/media/bootanimation.zip";
Anders Fridlunde0b4d232018-11-06 14:23:25 +010076static const char APEX_BOOTANIMATION_FILE[] = "/apex/com.android.bootanimation/etc/bootanimation.zip";
Jaekyun Seokc521bb32018-01-30 11:52:47 +090077static const char PRODUCT_ENCRYPTED_BOOTANIMATION_FILE[] = "/product/media/bootanimation-encrypted.zip";
Damien Bargiacchi97480862016-03-29 14:55:55 -070078static const char SYSTEM_ENCRYPTED_BOOTANIMATION_FILE[] = "/system/media/bootanimation-encrypted.zip";
Keun-young Parkb5938422017-03-23 13:46:24 -070079static const char OEM_SHUTDOWNANIMATION_FILE[] = "/oem/media/shutdownanimation.zip";
Jaekyun Seokc521bb32018-01-30 11:52:47 +090080static const char PRODUCT_SHUTDOWNANIMATION_FILE[] = "/product/media/shutdownanimation.zip";
Keun-young Parkb5938422017-03-23 13:46:24 -070081static const char SYSTEM_SHUTDOWNANIMATION_FILE[] = "/system/media/shutdownanimation.zip";
82
Damien Bargiacchi97480862016-03-29 14:55:55 -070083static const char SYSTEM_DATA_DIR_PATH[] = "/data/system";
84static const char SYSTEM_TIME_DIR_NAME[] = "time";
85static const char SYSTEM_TIME_DIR_PATH[] = "/data/system/time";
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -070086static const char CLOCK_FONT_ASSET[] = "images/clock_font.png";
87static const char CLOCK_FONT_ZIP_NAME[] = "clock_font.png";
Damien Bargiacchi97480862016-03-29 14:55:55 -070088static const char LAST_TIME_CHANGED_FILE_NAME[] = "last_time_change";
89static const char LAST_TIME_CHANGED_FILE_PATH[] = "/data/system/time/last_time_change";
90static const char ACCURATE_TIME_FLAG_FILE_NAME[] = "time_is_accurate";
91static const char ACCURATE_TIME_FLAG_FILE_PATH[] = "/data/system/time/time_is_accurate";
Damien Bargiacchi9071db12016-10-28 17:38:22 -070092static const char TIME_FORMAT_12_HOUR_FLAG_FILE_PATH[] = "/data/system/time/time_format_12_hour";
Damien Bargiacchi96762812016-07-12 15:53:40 -070093// Java timestamp format. Don't show the clock if the date is before 2000-01-01 00:00:00.
94static const long long ACCURATE_TIME_EPOCH = 946684800000;
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -070095static constexpr char FONT_BEGIN_CHAR = ' ';
96static constexpr char FONT_END_CHAR = '~' + 1;
97static constexpr size_t FONT_NUM_CHARS = FONT_END_CHAR - FONT_BEGIN_CHAR + 1;
98static constexpr size_t FONT_NUM_COLS = 16;
99static constexpr size_t FONT_NUM_ROWS = FONT_NUM_CHARS / FONT_NUM_COLS;
100static const int TEXT_CENTER_VALUE = INT_MAX;
101static const int TEXT_MISSING_VALUE = INT_MIN;
Damien Bargiacchi97480862016-03-29 14:55:55 -0700102static const char EXIT_PROP_NAME[] = "service.bootanim.exit";
Kyeongkab.Nam35a8a112019-07-17 16:41:48 +0900103static const int ANIM_ENTRY_NAME_MAX = ANIM_PATH_MAX + 1;
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700104static constexpr size_t TEXT_POS_LEN_MAX = 16;
Narayan Kamathafd31e02013-12-03 13:16:03 +0000105
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800106// ---------------------------------------------------------------------------
107
Ed Coyne7464ac92017-06-08 12:26:48 -0700108BootAnimation::BootAnimation(sp<Callbacks> callbacks)
Ed Coyne2c9e94a2017-05-31 10:08:28 -0700109 : Thread(false), mClockEnabled(true), mTimeIsAccurate(false),
Yi Kong911ac232019-03-24 01:49:02 -0700110 mTimeFormat12Hour(false), mTimeCheckThread(nullptr), mCallbacks(callbacks) {
Mathias Agopian627e7b52009-05-21 19:21:59 -0700111 mSession = new SurfaceComposerClient();
Geoffrey Pitsch290c4352016-08-09 14:35:10 -0400112
Keun-young Parkb5938422017-03-23 13:46:24 -0700113 std::string powerCtl = android::base::GetProperty("sys.powerctl", "");
114 if (powerCtl.empty()) {
115 mShuttingDown = false;
116 } else {
117 mShuttingDown = true;
118 }
Huihong Luo50a2f362018-08-11 09:27:57 -0700119 ALOGD("%sAnimationStartTiming start time: %" PRId64 "ms", mShuttingDown ? "Shutdown" : "Boot",
120 elapsedRealtime());
121}
122
123BootAnimation::~BootAnimation() {
124 if (mAnimation != nullptr) {
125 releaseAnimation(mAnimation);
126 mAnimation = nullptr;
127 }
128 ALOGD("%sAnimationStopTiming start time: %" PRId64 "ms", mShuttingDown ? "Shutdown" : "Boot",
129 elapsedRealtime());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800130}
131
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800132void BootAnimation::onFirstRef() {
Mathias Agopianbc726112009-09-23 15:44:05 -0700133 status_t err = mSession->linkToComposerDeath(this);
Wei Wang159a4102018-10-23 23:15:58 -0700134 SLOGE_IF(err, "linkToComposerDeath failed (%s) ", strerror(-err));
Mathias Agopian8434c532009-09-23 18:52:49 -0700135 if (err == NO_ERROR) {
Huihong Luo50a2f362018-08-11 09:27:57 -0700136 // Load the animation content -- this can be slow (eg 200ms)
137 // called before waitForSurfaceFlinger() in main() to avoid wait
138 ALOGD("%sAnimationPreloadTiming start time: %" PRId64 "ms",
139 mShuttingDown ? "Shutdown" : "Boot", elapsedRealtime());
140 preloadAnimation();
141 ALOGD("%sAnimationPreloadStopTiming start time: %" PRId64 "ms",
142 mShuttingDown ? "Shutdown" : "Boot", elapsedRealtime());
Mathias Agopianbc726112009-09-23 15:44:05 -0700143 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800144}
145
Mathias Agopianbc726112009-09-23 15:44:05 -0700146sp<SurfaceComposerClient> BootAnimation::session() const {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800147 return mSession;
148}
149
Narayan Kamathafd31e02013-12-03 13:16:03 +0000150void BootAnimation::binderDied(const wp<IBinder>&)
Mathias Agopianbc726112009-09-23 15:44:05 -0700151{
152 // woah, surfaceflinger died!
Wei Wang159a4102018-10-23 23:15:58 -0700153 SLOGD("SurfaceFlinger died, exiting...");
Mathias Agopianbc726112009-09-23 15:44:05 -0700154
155 // calling requestExit() is not enough here because the Surface code
156 // might be blocked on a condition variable that will never be updated.
157 kill( getpid(), SIGKILL );
158 requestExit();
159}
160
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800161status_t BootAnimation::initTexture(Texture* texture, AssetManager& assets,
162 const char* name) {
163 Asset* asset = assets.open(name, Asset::ACCESS_BUFFER);
Yi Kong911ac232019-03-24 01:49:02 -0700164 if (asset == nullptr)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800165 return NO_INIT;
166 SkBitmap bitmap;
Matt Sarett8898c162016-03-24 16:11:01 -0400167 sk_sp<SkData> data = SkData::MakeWithoutCopy(asset->getBuffer(false),
168 asset->getLength());
169 sk_sp<SkImage> image = SkImage::MakeFromEncoded(data);
170 image->asLegacyBitmap(&bitmap, SkImage::kRO_LegacyBitmapMode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800171 asset->close();
172 delete asset;
173
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800174 const int w = bitmap.width();
175 const int h = bitmap.height();
176 const void* p = bitmap.getPixels();
177
178 GLint crop[4] = { 0, h, w, -h };
179 texture->w = w;
180 texture->h = h;
181
182 glGenTextures(1, &texture->name);
183 glBindTexture(GL_TEXTURE_2D, texture->name);
184
Mike Reed42a1d082014-07-07 18:06:18 -0400185 switch (bitmap.colorType()) {
186 case kAlpha_8_SkColorType:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800187 glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, w, h, 0, GL_ALPHA,
188 GL_UNSIGNED_BYTE, p);
189 break;
Mike Reed42a1d082014-07-07 18:06:18 -0400190 case kARGB_4444_SkColorType:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800191 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA,
192 GL_UNSIGNED_SHORT_4_4_4_4, p);
193 break;
Mike Reed42a1d082014-07-07 18:06:18 -0400194 case kN32_SkColorType:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800195 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA,
196 GL_UNSIGNED_BYTE, p);
197 break;
Mike Reed42a1d082014-07-07 18:06:18 -0400198 case kRGB_565_SkColorType:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800199 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, w, h, 0, GL_RGB,
200 GL_UNSIGNED_SHORT_5_6_5, p);
201 break;
202 default:
203 break;
204 }
205
206 glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_CROP_RECT_OES, crop);
207 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
208 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
209 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
210 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700211
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800212 return NO_ERROR;
213}
214
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700215status_t BootAnimation::initTexture(FileMap* map, int* width, int* height)
Mathias Agopiana8826d62009-10-01 03:10:14 -0700216{
Mathias Agopiana8826d62009-10-01 03:10:14 -0700217 SkBitmap bitmap;
Damien Bargiacchif67b3172016-09-07 20:17:14 -0700218 sk_sp<SkData> data = SkData::MakeWithoutCopy(map->getDataPtr(),
219 map->getDataLength());
Matt Sarett8898c162016-03-24 16:11:01 -0400220 sk_sp<SkImage> image = SkImage::MakeFromEncoded(data);
221 image->asLegacyBitmap(&bitmap, SkImage::kRO_LegacyBitmapMode);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700222
Mykola Kondratenko0c1eeb32014-04-15 09:35:44 +0200223 // FileMap memory is never released until application exit.
224 // Release it now as the texture is already loaded and the memory used for
225 // the packed resource can be released.
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700226 delete map;
Mykola Kondratenko0c1eeb32014-04-15 09:35:44 +0200227
Mathias Agopiana8826d62009-10-01 03:10:14 -0700228 const int w = bitmap.width();
229 const int h = bitmap.height();
230 const void* p = bitmap.getPixels();
231
232 GLint crop[4] = { 0, h, w, -h };
233 int tw = 1 << (31 - __builtin_clz(w));
234 int th = 1 << (31 - __builtin_clz(h));
235 if (tw < w) tw <<= 1;
236 if (th < h) th <<= 1;
237
Mike Reed42a1d082014-07-07 18:06:18 -0400238 switch (bitmap.colorType()) {
239 case kN32_SkColorType:
Sai Kiran Korwar27167492015-07-07 20:00:06 +0530240 if (!mUseNpotTextures && (tw != w || th != h)) {
Mathias Agopiana8826d62009-10-01 03:10:14 -0700241 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, tw, th, 0, GL_RGBA,
Yi Kong911ac232019-03-24 01:49:02 -0700242 GL_UNSIGNED_BYTE, nullptr);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700243 glTexSubImage2D(GL_TEXTURE_2D, 0,
244 0, 0, w, h, GL_RGBA, GL_UNSIGNED_BYTE, p);
245 } else {
Sai Kiran Korwar27167492015-07-07 20:00:06 +0530246 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA,
Mathias Agopiana8826d62009-10-01 03:10:14 -0700247 GL_UNSIGNED_BYTE, p);
248 }
249 break;
250
Mike Reed42a1d082014-07-07 18:06:18 -0400251 case kRGB_565_SkColorType:
Sai Kiran Korwar27167492015-07-07 20:00:06 +0530252 if (!mUseNpotTextures && (tw != w || th != h)) {
Mathias Agopiana8826d62009-10-01 03:10:14 -0700253 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, tw, th, 0, GL_RGB,
Yi Kong911ac232019-03-24 01:49:02 -0700254 GL_UNSIGNED_SHORT_5_6_5, nullptr);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700255 glTexSubImage2D(GL_TEXTURE_2D, 0,
256 0, 0, w, h, GL_RGB, GL_UNSIGNED_SHORT_5_6_5, p);
257 } else {
Sai Kiran Korwar27167492015-07-07 20:00:06 +0530258 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, w, h, 0, GL_RGB,
Mathias Agopiana8826d62009-10-01 03:10:14 -0700259 GL_UNSIGNED_SHORT_5_6_5, p);
260 }
261 break;
262 default:
263 break;
264 }
265
266 glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_CROP_RECT_OES, crop);
267
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700268 *width = w;
269 *height = h;
270
Mathias Agopiana8826d62009-10-01 03:10:14 -0700271 return NO_ERROR;
272}
273
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800274status_t BootAnimation::readyToRun() {
275 mAssets.addDefaultAssets();
276
Dominik Laskowski3316a0a2019-01-25 02:56:41 -0800277 mDisplayToken = SurfaceComposerClient::getInternalDisplayToken();
278 if (mDisplayToken == nullptr)
279 return -1;
280
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800281 DisplayInfo dinfo;
Dominik Laskowski3316a0a2019-01-25 02:56:41 -0800282 status_t status = SurfaceComposerClient::getDisplayInfo(mDisplayToken, &dinfo);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800283 if (status)
284 return -1;
285
286 // create the native surface
Jeff Brown0b722fe2012-08-24 22:40:14 -0700287 sp<SurfaceControl> control = session()->createSurface(String8("BootAnimation"),
288 dinfo.w, dinfo.h, PIXEL_FORMAT_RGB_565);
Mathias Agopian439863f2011-06-28 19:09:31 -0700289
Robert Carre13b58e2017-08-31 14:50:44 -0700290 SurfaceComposerClient::Transaction t;
291 t.setLayer(control, 0x40000000)
292 .apply();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800293
Mathias Agopian17f638b2009-04-16 20:04:08 -0700294 sp<Surface> s = control->getSurface();
295
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800296 // initialize opengl and egl
Mathias Agopian738b9a42009-08-06 16:41:02 -0700297 const EGLint attribs[] = {
Mathias Agopian1b253b72011-08-15 15:20:22 -0700298 EGL_RED_SIZE, 8,
299 EGL_GREEN_SIZE, 8,
300 EGL_BLUE_SIZE, 8,
Mathias Agopiana8826d62009-10-01 03:10:14 -0700301 EGL_DEPTH_SIZE, 0,
302 EGL_NONE
Mathias Agopian738b9a42009-08-06 16:41:02 -0700303 };
Andreas Gampecfedceb2014-09-30 21:48:18 -0700304 EGLint w, h;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800305 EGLint numConfigs;
306 EGLConfig config;
307 EGLSurface surface;
308 EGLContext context;
Mathias Agopian627e7b52009-05-21 19:21:59 -0700309
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800310 EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
Mathias Agopian627e7b52009-05-21 19:21:59 -0700311
Yi Kong911ac232019-03-24 01:49:02 -0700312 eglInitialize(display, nullptr, nullptr);
Mathias Agopian1b253b72011-08-15 15:20:22 -0700313 eglChooseConfig(display, attribs, &config, 1, &numConfigs);
Yi Kong911ac232019-03-24 01:49:02 -0700314 surface = eglCreateWindowSurface(display, config, s.get(), nullptr);
315 context = eglCreateContext(display, config, nullptr, nullptr);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800316 eglQuerySurface(display, surface, EGL_WIDTH, &w);
317 eglQuerySurface(display, surface, EGL_HEIGHT, &h);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700318
Mathias Agopianabac0102009-07-31 14:47:00 -0700319 if (eglMakeCurrent(display, surface, surface, context) == EGL_FALSE)
320 return NO_INIT;
Mathias Agopiana8826d62009-10-01 03:10:14 -0700321
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800322 mDisplay = display;
323 mContext = context;
324 mSurface = surface;
325 mWidth = w;
326 mHeight = h;
Mathias Agopian17f638b2009-04-16 20:04:08 -0700327 mFlingerSurfaceControl = control;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800328 mFlingerSurface = s;
Adrian Roos9ee5dff2018-08-22 20:19:49 +0200329 mTargetInset = -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800330
Huihong Luo50a2f362018-08-11 09:27:57 -0700331 return NO_ERROR;
332}
333
334bool BootAnimation::preloadAnimation() {
335 findBootAnimationFile();
336 if (!mZipFileName.isEmpty()) {
337 mAnimation = loadAnimation(mZipFileName);
338 return (mAnimation != nullptr);
339 }
340
341 return false;
342}
343
344void BootAnimation::findBootAnimationFile() {
Elliott Hughesc367d482013-10-29 13:12:55 -0700345 // If the device has encryption turned on or is in process
Jason parksbd9a08d2011-01-31 15:04:34 -0600346 // of being encrypted we show the encrypted boot animation.
347 char decrypt[PROPERTY_VALUE_MAX];
348 property_get("vold.decrypt", decrypt, "");
349
Keun-young Parkb5938422017-03-23 13:46:24 -0700350 bool encryptedAnimation = atoi(decrypt) != 0 ||
351 !strcmp("trigger_restart_min_framework", decrypt);
Jason parksbd9a08d2011-01-31 15:04:34 -0600352
Jaekyun Seokc521bb32018-01-30 11:52:47 +0900353 if (!mShuttingDown && encryptedAnimation) {
354 static const char* encryptedBootFiles[] =
355 {PRODUCT_ENCRYPTED_BOOTANIMATION_FILE, SYSTEM_ENCRYPTED_BOOTANIMATION_FILE};
356 for (const char* f : encryptedBootFiles) {
357 if (access(f, R_OK) == 0) {
358 mZipFileName = f;
Huihong Luo50a2f362018-08-11 09:27:57 -0700359 return;
Jaekyun Seokc521bb32018-01-30 11:52:47 +0900360 }
361 }
Jason parksbd9a08d2011-01-31 15:04:34 -0600362 }
Beverly34ffe252019-05-17 11:03:54 -0400363
364 const bool playDarkAnim = android::base::GetIntProperty("ro.boot.theme", 0) == 1;
Jaekyun Seokc521bb32018-01-30 11:52:47 +0900365 static const char* bootFiles[] =
Anders Fridlunde0b4d232018-11-06 14:23:25 +0100366 {APEX_BOOTANIMATION_FILE, playDarkAnim ? PRODUCT_BOOTANIMATION_DARK_FILE : PRODUCT_BOOTANIMATION_FILE,
Beverly34ffe252019-05-17 11:03:54 -0400367 OEM_BOOTANIMATION_FILE, SYSTEM_BOOTANIMATION_FILE};
Keun-young Parkb5938422017-03-23 13:46:24 -0700368 static const char* shutdownFiles[] =
Anders Fridlunde0b4d232018-11-06 14:23:25 +0100369 {PRODUCT_SHUTDOWNANIMATION_FILE, OEM_SHUTDOWNANIMATION_FILE, SYSTEM_SHUTDOWNANIMATION_FILE, ""};
Keun-young Parkb5938422017-03-23 13:46:24 -0700370
371 for (const char* f : (!mShuttingDown ? bootFiles : shutdownFiles)) {
372 if (access(f, R_OK) == 0) {
373 mZipFileName = f;
Huihong Luo50a2f362018-08-11 09:27:57 -0700374 return;
Keun-young Parkb5938422017-03-23 13:46:24 -0700375 }
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700376 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800377}
378
Mathias Agopiana8826d62009-10-01 03:10:14 -0700379bool BootAnimation::threadLoop()
380{
381 bool r;
Narayan Kamathafd31e02013-12-03 13:16:03 +0000382 // We have no bootanimation file, so we use the stock android logo
383 // animation.
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700384 if (mZipFileName.isEmpty()) {
Mathias Agopiana8826d62009-10-01 03:10:14 -0700385 r = android();
386 } else {
387 r = movie();
388 }
389
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800390 eglMakeCurrent(mDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
391 eglDestroyContext(mDisplay, mContext);
392 eglDestroySurface(mDisplay, mSurface);
Mathias Agopian6cf0db22009-04-17 19:36:26 -0700393 mFlingerSurface.clear();
Mathias Agopian17f638b2009-04-16 20:04:08 -0700394 mFlingerSurfaceControl.clear();
Mathias Agopian627e7b52009-05-21 19:21:59 -0700395 eglTerminate(mDisplay);
Sai Kiran Korwar3eee9fb2015-06-16 17:13:35 +0530396 eglReleaseThread();
Mathias Agopian627e7b52009-05-21 19:21:59 -0700397 IPCThreadState::self()->stopProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800398 return r;
399}
400
Mathias Agopiana8826d62009-10-01 03:10:14 -0700401bool BootAnimation::android()
402{
Wei Wang159a4102018-10-23 23:15:58 -0700403 SLOGD("%sAnimationShownTiming start time: %" PRId64 "ms", mShuttingDown ? "Shutdown" : "Boot",
Keun-young Parkd1794cd2017-04-04 12:21:19 -0700404 elapsedRealtime());
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700405 initTexture(&mAndroid[0], mAssets, "images/android-logo-mask.png");
406 initTexture(&mAndroid[1], mAssets, "images/android-logo-shine.png");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800407
Ed Coyne7464ac92017-06-08 12:26:48 -0700408 mCallbacks->init({});
409
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800410 // clear screen
Mathias Agopiana8826d62009-10-01 03:10:14 -0700411 glShadeModel(GL_FLAT);
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700412 glDisable(GL_DITHER);
413 glDisable(GL_SCISSOR_TEST);
Mathias Agopian59f19e42011-05-06 19:22:12 -0700414 glClearColor(0,0,0,1);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800415 glClear(GL_COLOR_BUFFER_BIT);
416 eglSwapBuffers(mDisplay, mSurface);
417
Mathias Agopiana8826d62009-10-01 03:10:14 -0700418 glEnable(GL_TEXTURE_2D);
419 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
420
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700421 const GLint xc = (mWidth - mAndroid[0].w) / 2;
422 const GLint yc = (mHeight - mAndroid[0].h) / 2;
423 const Rect updateRect(xc, yc, xc + mAndroid[0].w, yc + mAndroid[0].h);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800424
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800425 glScissor(updateRect.left, mHeight - updateRect.bottom, updateRect.width(),
426 updateRect.height());
427
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700428 // Blend state
429 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
430 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
431
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800432 const nsecs_t startTime = systemTime();
433 do {
Mathias Agopian13796652009-03-24 22:49:21 -0700434 nsecs_t now = systemTime();
435 double time = now - startTime;
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700436 float t = 4.0f * float(time / us2ns(16667)) / mAndroid[1].w;
437 GLint offset = (1 - (t - floorf(t))) * mAndroid[1].w;
438 GLint x = xc - offset;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800439
Mathias Agopian81668642009-07-28 11:41:30 -0700440 glDisable(GL_SCISSOR_TEST);
441 glClear(GL_COLOR_BUFFER_BIT);
442
443 glEnable(GL_SCISSOR_TEST);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800444 glDisable(GL_BLEND);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800445 glBindTexture(GL_TEXTURE_2D, mAndroid[1].name);
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700446 glDrawTexiOES(x, yc, 0, mAndroid[1].w, mAndroid[1].h);
447 glDrawTexiOES(x + mAndroid[1].w, yc, 0, mAndroid[1].w, mAndroid[1].h);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800448
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700449 glEnable(GL_BLEND);
450 glBindTexture(GL_TEXTURE_2D, mAndroid[0].name);
451 glDrawTexiOES(xc, yc, 0, mAndroid[0].w, mAndroid[0].h);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800452
Mathias Agopian627e7b52009-05-21 19:21:59 -0700453 EGLBoolean res = eglSwapBuffers(mDisplay, mSurface);
454 if (res == EGL_FALSE)
455 break;
456
Mathias Agopian13796652009-03-24 22:49:21 -0700457 // 12fps: don't animate too fast to preserve CPU
458 const nsecs_t sleepTime = 83333 - ns2us(systemTime() - now);
459 if (sleepTime > 0)
Mathias Agopiana8826d62009-10-01 03:10:14 -0700460 usleep(sleepTime);
Kevin Hesterd3782b22012-04-26 10:38:55 -0700461
462 checkExit();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800463 } while (!exitPending());
464
465 glDeleteTextures(1, &mAndroid[0].name);
466 glDeleteTextures(1, &mAndroid[1].name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800467 return false;
468}
469
Kevin Hesterd3782b22012-04-26 10:38:55 -0700470void BootAnimation::checkExit() {
471 // Allow surface flinger to gracefully request shutdown
472 char value[PROPERTY_VALUE_MAX];
473 property_get(EXIT_PROP_NAME, value, "0");
474 int exitnow = atoi(value);
475 if (exitnow) {
476 requestExit();
Ed Coyne7464ac92017-06-08 12:26:48 -0700477 mCallbacks->shutdown();
Kevin Hesterd3782b22012-04-26 10:38:55 -0700478 }
479}
480
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700481bool BootAnimation::validClock(const Animation::Part& part) {
482 return part.clockPosX != TEXT_MISSING_VALUE && part.clockPosY != TEXT_MISSING_VALUE;
483}
484
485bool parseTextCoord(const char* str, int* dest) {
486 if (strcmp("c", str) == 0) {
487 *dest = TEXT_CENTER_VALUE;
488 return true;
489 }
490
491 char* end;
492 int val = (int) strtol(str, &end, 0);
493 if (end == str || *end != '\0' || val == INT_MAX || val == INT_MIN) {
494 return false;
495 }
496 *dest = val;
497 return true;
498}
499
500// Parse two position coordinates. If only string is non-empty, treat it as the y value.
501void parsePosition(const char* str1, const char* str2, int* x, int* y) {
502 bool success = false;
503 if (strlen(str1) == 0) { // No values were specified
504 // success = false
505 } else if (strlen(str2) == 0) { // we have only one value
506 if (parseTextCoord(str1, y)) {
507 *x = TEXT_CENTER_VALUE;
508 success = true;
509 }
510 } else {
511 if (parseTextCoord(str1, x) && parseTextCoord(str2, y)) {
512 success = true;
513 }
514 }
515
516 if (!success) {
517 *x = TEXT_MISSING_VALUE;
518 *y = TEXT_MISSING_VALUE;
519 }
520}
521
Jesse Hall083b84c2014-09-22 10:51:09 -0700522// Parse a color represented as an HTML-style 'RRGGBB' string: each pair of
523// characters in str is a hex number in [0, 255], which are converted to
524// floating point values in the range [0.0, 1.0] and placed in the
525// corresponding elements of color.
526//
527// If the input string isn't valid, parseColor returns false and color is
528// left unchanged.
529static bool parseColor(const char str[7], float color[3]) {
530 float tmpColor[3];
531 for (int i = 0; i < 3; i++) {
532 int val = 0;
533 for (int j = 0; j < 2; j++) {
534 val *= 16;
535 char c = str[2*i + j];
536 if (c >= '0' && c <= '9') val += c - '0';
537 else if (c >= 'A' && c <= 'F') val += (c - 'A') + 10;
538 else if (c >= 'a' && c <= 'f') val += (c - 'a') + 10;
539 else return false;
540 }
541 tmpColor[i] = static_cast<float>(val) / 255.0f;
542 }
543 memcpy(color, tmpColor, sizeof(tmpColor));
544 return true;
545}
546
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700547
548static bool readFile(ZipFileRO* zip, const char* name, String8& outString)
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700549{
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700550 ZipEntryRO entry = zip->findEntryByName(name);
Wei Wang159a4102018-10-23 23:15:58 -0700551 SLOGE_IF(!entry, "couldn't find %s", name);
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700552 if (!entry) {
553 return false;
554 }
555
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700556 FileMap* entryMap = zip->createEntryFileMap(entry);
557 zip->releaseEntry(entry);
Wei Wang159a4102018-10-23 23:15:58 -0700558 SLOGE_IF(!entryMap, "entryMap is null");
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700559 if (!entryMap) {
560 return false;
561 }
562
563 outString.setTo((char const*)entryMap->getDataPtr(), entryMap->getDataLength());
Narayan Kamath688ff4c2015-02-23 15:47:54 +0000564 delete entryMap;
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700565 return true;
566}
567
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700568// The font image should be a 96x2 array of character images. The
569// columns are the printable ASCII characters 0x20 - 0x7f. The
570// top row is regular text; the bottom row is bold.
571status_t BootAnimation::initFont(Font* font, const char* fallback) {
572 status_t status = NO_ERROR;
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800573
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700574 if (font->map != nullptr) {
575 glGenTextures(1, &font->texture.name);
576 glBindTexture(GL_TEXTURE_2D, font->texture.name);
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800577
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700578 status = initTexture(font->map, &font->texture.w, &font->texture.h);
579
580 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
581 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
582 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
583 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
584 } else if (fallback != nullptr) {
585 status = initTexture(&font->texture, mAssets, fallback);
586 } else {
587 return NO_INIT;
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800588 }
589
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700590 if (status == NO_ERROR) {
591 font->char_width = font->texture.w / FONT_NUM_COLS;
592 font->char_height = font->texture.h / FONT_NUM_ROWS / 2; // There are bold and regular rows
593 }
594
595 return status;
596}
597
598void BootAnimation::drawText(const char* str, const Font& font, bool bold, int* x, int* y) {
599 glEnable(GL_BLEND); // Allow us to draw on top of the animation
600 glBindTexture(GL_TEXTURE_2D, font.texture.name);
601
602 const int len = strlen(str);
603 const int strWidth = font.char_width * len;
604
605 if (*x == TEXT_CENTER_VALUE) {
606 *x = (mWidth - strWidth) / 2;
607 } else if (*x < 0) {
608 *x = mWidth + *x - strWidth;
609 }
610 if (*y == TEXT_CENTER_VALUE) {
611 *y = (mHeight - font.char_height) / 2;
612 } else if (*y < 0) {
613 *y = mHeight + *y - font.char_height;
614 }
615
616 int cropRect[4] = { 0, 0, font.char_width, -font.char_height };
617
618 for (int i = 0; i < len; i++) {
619 char c = str[i];
620
621 if (c < FONT_BEGIN_CHAR || c > FONT_END_CHAR) {
622 c = '?';
623 }
624
625 // Crop the texture to only the pixels in the current glyph
626 const int charPos = (c - FONT_BEGIN_CHAR); // Position in the list of valid characters
627 const int row = charPos / FONT_NUM_COLS;
628 const int col = charPos % FONT_NUM_COLS;
629 cropRect[0] = col * font.char_width; // Left of column
630 cropRect[1] = row * font.char_height * 2; // Top of row
631 // Move down to bottom of regular (one char_heigh) or bold (two char_heigh) line
632 cropRect[1] += bold ? 2 * font.char_height : font.char_height;
633 glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_CROP_RECT_OES, cropRect);
634
635 glDrawTexiOES(*x, *y, 0, font.char_width, font.char_height);
636
637 *x += font.char_width;
638 }
639
640 glDisable(GL_BLEND); // Return to the animation's default behaviour
641 glBindTexture(GL_TEXTURE_2D, 0);
642}
643
Damien Bargiacchi9071db12016-10-28 17:38:22 -0700644// We render 12 or 24 hour time.
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700645void BootAnimation::drawClock(const Font& font, const int xPos, const int yPos) {
Damien Bargiacchi9071db12016-10-28 17:38:22 -0700646 static constexpr char TIME_FORMAT_12[] = "%l:%M";
647 static constexpr char TIME_FORMAT_24[] = "%H:%M";
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700648 static constexpr int TIME_LENGTH = 6;
649
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800650 time_t rawtime;
651 time(&rawtime);
652 struct tm* timeInfo = localtime(&rawtime);
653
654 char timeBuff[TIME_LENGTH];
Damien Bargiacchi9071db12016-10-28 17:38:22 -0700655 const char* timeFormat = mTimeFormat12Hour ? TIME_FORMAT_12 : TIME_FORMAT_24;
656 size_t length = strftime(timeBuff, TIME_LENGTH, timeFormat, timeInfo);
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800657
658 if (length != TIME_LENGTH - 1) {
Wei Wang159a4102018-10-23 23:15:58 -0700659 SLOGE("Couldn't format time; abandoning boot animation clock");
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800660 mClockEnabled = false;
661 return;
662 }
663
Damien Bargiacchi45a76442016-12-05 18:02:18 -0800664 char* out = timeBuff[0] == ' ' ? &timeBuff[1] : &timeBuff[0];
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700665 int x = xPos;
666 int y = yPos;
Damien Bargiacchi45a76442016-12-05 18:02:18 -0800667 drawText(out, font, false, &x, &y);
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800668}
669
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700670bool BootAnimation::parseAnimationDesc(Animation& animation)
Mathias Agopiana8826d62009-10-01 03:10:14 -0700671{
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700672 String8 desString;
673
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700674 if (!readFile(animation.zip, "desc.txt", desString)) {
Narayan Kamathafd31e02013-12-03 13:16:03 +0000675 return false;
676 }
Mathias Agopiana8826d62009-10-01 03:10:14 -0700677 char const* s = desString.string();
678
Mathias Agopiana8826d62009-10-01 03:10:14 -0700679 // Parse the description file
680 for (;;) {
681 const char* endl = strstr(s, "\n");
Yi Kong911ac232019-03-24 01:49:02 -0700682 if (endl == nullptr) break;
Mathias Agopiana8826d62009-10-01 03:10:14 -0700683 String8 line(s, endl - s);
684 const char* l = line.string();
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800685 int fps = 0;
686 int width = 0;
687 int height = 0;
688 int count = 0;
689 int pause = 0;
Narayan Kamathafd31e02013-12-03 13:16:03 +0000690 char path[ANIM_ENTRY_NAME_MAX];
Jesse Hall083b84c2014-09-22 10:51:09 -0700691 char color[7] = "000000"; // default to black if unspecified
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700692 char clockPos1[TEXT_POS_LEN_MAX + 1] = "";
693 char clockPos2[TEXT_POS_LEN_MAX + 1] = "";
Jesse Hall083b84c2014-09-22 10:51:09 -0700694
Kevin Hesterd3782b22012-04-26 10:38:55 -0700695 char pathType;
Mathias Agopiana8826d62009-10-01 03:10:14 -0700696 if (sscanf(l, "%d %d %d", &width, &height, &fps) == 3) {
Wei Wang159a4102018-10-23 23:15:58 -0700697 // SLOGD("> w=%d, h=%d, fps=%d", width, height, fps);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700698 animation.width = width;
699 animation.height = height;
700 animation.fps = fps;
Kyeongkab.Nam35a8a112019-07-17 16:41:48 +0900701 } else if (sscanf(l, " %c %d %d %" STRTO(ANIM_PATH_MAX) "s #%6s %16s %16s",
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700702 &pathType, &count, &pause, path, color, clockPos1, clockPos2) >= 4) {
Wei Wang159a4102018-10-23 23:15:58 -0700703 //SLOGD("> type=%c, count=%d, pause=%d, path=%s, color=%s, clockPos1=%s, clockPos2=%s",
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700704 // pathType, count, pause, path, color, clockPos1, clockPos2);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700705 Animation::Part part;
Kevin Hesterd3782b22012-04-26 10:38:55 -0700706 part.playUntilComplete = pathType == 'c';
Mathias Agopiana8826d62009-10-01 03:10:14 -0700707 part.count = count;
708 part.pause = pause;
709 part.path = path;
Yi Kong911ac232019-03-24 01:49:02 -0700710 part.audioData = nullptr;
711 part.animation = nullptr;
Jesse Hall083b84c2014-09-22 10:51:09 -0700712 if (!parseColor(color, part.backgroundColor)) {
Wei Wang159a4102018-10-23 23:15:58 -0700713 SLOGE("> invalid color '#%s'", color);
Jesse Hall083b84c2014-09-22 10:51:09 -0700714 part.backgroundColor[0] = 0.0f;
715 part.backgroundColor[1] = 0.0f;
716 part.backgroundColor[2] = 0.0f;
717 }
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700718 parsePosition(clockPos1, clockPos2, &part.clockPosX, &part.clockPosY);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700719 animation.parts.add(part);
720 }
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700721 else if (strcmp(l, "$SYSTEM") == 0) {
Wei Wang159a4102018-10-23 23:15:58 -0700722 // SLOGD("> SYSTEM");
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700723 Animation::Part part;
724 part.playUntilComplete = false;
725 part.count = 1;
726 part.pause = 0;
Yi Kong911ac232019-03-24 01:49:02 -0700727 part.audioData = nullptr;
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700728 part.animation = loadAnimation(String8(SYSTEM_BOOTANIMATION_FILE));
Yi Kong911ac232019-03-24 01:49:02 -0700729 if (part.animation != nullptr)
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700730 animation.parts.add(part);
731 }
Mathias Agopiana8826d62009-10-01 03:10:14 -0700732 s = ++endl;
733 }
734
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700735 return true;
736}
737
738bool BootAnimation::preloadZip(Animation& animation)
739{
Mathias Agopiana8826d62009-10-01 03:10:14 -0700740 // read all the data structures
741 const size_t pcount = animation.parts.size();
Yi Kong911ac232019-03-24 01:49:02 -0700742 void *cookie = nullptr;
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400743 ZipFileRO* zip = animation.zip;
744 if (!zip->startIteration(&cookie)) {
Narayan Kamathafd31e02013-12-03 13:16:03 +0000745 return false;
746 }
747
748 ZipEntryRO entry;
749 char name[ANIM_ENTRY_NAME_MAX];
Yi Kong911ac232019-03-24 01:49:02 -0700750 while ((entry = zip->nextEntry(cookie)) != nullptr) {
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400751 const int foundEntryName = zip->getEntryFileName(entry, name, ANIM_ENTRY_NAME_MAX);
Narayan Kamathafd31e02013-12-03 13:16:03 +0000752 if (foundEntryName > ANIM_ENTRY_NAME_MAX || foundEntryName == -1) {
Wei Wang159a4102018-10-23 23:15:58 -0700753 SLOGE("Error fetching entry file name");
Narayan Kamathafd31e02013-12-03 13:16:03 +0000754 continue;
755 }
756
757 const String8 entryName(name);
758 const String8 path(entryName.getPathDir());
759 const String8 leaf(entryName.getPathLeaf());
760 if (leaf.size() > 0) {
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700761 if (entryName == CLOCK_FONT_ZIP_NAME) {
762 FileMap* map = zip->createEntryFileMap(entry);
763 if (map) {
764 animation.clockFont.map = map;
765 }
766 continue;
767 }
768
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400769 for (size_t j = 0; j < pcount; j++) {
Narayan Kamathafd31e02013-12-03 13:16:03 +0000770 if (path == animation.parts[j].path) {
Narayan Kamath4600dd02015-06-16 12:02:57 +0100771 uint16_t method;
Narayan Kamathafd31e02013-12-03 13:16:03 +0000772 // supports only stored png files
Yi Kong911ac232019-03-24 01:49:02 -0700773 if (zip->getEntryInfo(entry, &method, nullptr, nullptr, nullptr, nullptr, nullptr)) {
Narayan Kamathafd31e02013-12-03 13:16:03 +0000774 if (method == ZipFileRO::kCompressStored) {
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400775 FileMap* map = zip->createEntryFileMap(entry);
Narayan Kamathafd31e02013-12-03 13:16:03 +0000776 if (map) {
Narayan Kamathafd31e02013-12-03 13:16:03 +0000777 Animation::Part& part(animation.parts.editItemAt(j));
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700778 if (leaf == "audio.wav") {
779 // a part may have at most one audio file
Geoffrey Pitschd6d9a1d2016-06-08 00:38:58 -0700780 part.audioData = (uint8_t *)map->getDataPtr();
781 part.audioLength = map->getDataLength();
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400782 } else if (leaf == "trim.txt") {
783 part.trimData.setTo((char const*)map->getDataPtr(),
784 map->getDataLength());
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700785 } else {
786 Animation::Frame frame;
787 frame.name = leaf;
788 frame.map = map;
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400789 frame.trimWidth = animation.width;
790 frame.trimHeight = animation.height;
791 frame.trimX = 0;
792 frame.trimY = 0;
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700793 part.frames.add(frame);
794 }
Mathias Agopiana8826d62009-10-01 03:10:14 -0700795 }
Geoffrey Pitschd6d9a1d2016-06-08 00:38:58 -0700796 } else {
Wei Wang159a4102018-10-23 23:15:58 -0700797 SLOGE("bootanimation.zip is compressed; must be only stored");
Mathias Agopiana8826d62009-10-01 03:10:14 -0700798 }
799 }
800 }
801 }
802 }
803 }
804
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400805 // If there is trimData present, override the positioning defaults.
806 for (Animation::Part& part : animation.parts) {
807 const char* trimDataStr = part.trimData.string();
808 for (size_t frameIdx = 0; frameIdx < part.frames.size(); frameIdx++) {
809 const char* endl = strstr(trimDataStr, "\n");
810 // No more trimData for this part.
Yi Kong911ac232019-03-24 01:49:02 -0700811 if (endl == nullptr) {
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400812 break;
813 }
814 String8 line(trimDataStr, endl - trimDataStr);
815 const char* lineStr = line.string();
816 trimDataStr = ++endl;
817 int width = 0, height = 0, x = 0, y = 0;
818 if (sscanf(lineStr, "%dx%d+%d+%d", &width, &height, &x, &y) == 4) {
819 Animation::Frame& frame(part.frames.editItemAt(frameIdx));
820 frame.trimWidth = width;
821 frame.trimHeight = height;
822 frame.trimX = x;
823 frame.trimY = y;
824 } else {
Wei Wang159a4102018-10-23 23:15:58 -0700825 SLOGE("Error parsing trim.txt, line: %s", lineStr);
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400826 break;
827 }
828 }
829 }
830
831 zip->endIteration(cookie);
Narayan Kamathafd31e02013-12-03 13:16:03 +0000832
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700833 return true;
834}
835
836bool BootAnimation::movie()
837{
Huihong Luo50a2f362018-08-11 09:27:57 -0700838 if (mAnimation == nullptr) {
839 mAnimation = loadAnimation(mZipFileName);
840 }
841
842 if (mAnimation == nullptr)
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700843 return false;
844
Huihong Luo50a2f362018-08-11 09:27:57 -0700845 // mCallbacks->init() may get called recursively,
846 // this loop is needed to get the same results
847 for (const Animation::Part& part : mAnimation->parts) {
848 if (part.animation != nullptr) {
849 mCallbacks->init(part.animation->parts);
850 }
851 }
852 mCallbacks->init(mAnimation->parts);
853
Damien Bargiacchi97480862016-03-29 14:55:55 -0700854 bool anyPartHasClock = false;
Huihong Luo50a2f362018-08-11 09:27:57 -0700855 for (size_t i=0; i < mAnimation->parts.size(); i++) {
856 if(validClock(mAnimation->parts[i])) {
Damien Bargiacchi97480862016-03-29 14:55:55 -0700857 anyPartHasClock = true;
858 break;
859 }
860 }
861 if (!anyPartHasClock) {
862 mClockEnabled = false;
863 }
864
Sai Kiran Korwar27167492015-07-07 20:00:06 +0530865 // Check if npot textures are supported
866 mUseNpotTextures = false;
867 String8 gl_extensions;
868 const char* exts = reinterpret_cast<const char*>(glGetString(GL_EXTENSIONS));
869 if (!exts) {
870 glGetError();
871 } else {
872 gl_extensions.setTo(exts);
873 if ((gl_extensions.find("GL_ARB_texture_non_power_of_two") != -1) ||
874 (gl_extensions.find("GL_OES_texture_npot") != -1)) {
875 mUseNpotTextures = true;
876 }
877 }
878
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800879 // Blend required to draw time on top of animation frames.
880 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700881 glShadeModel(GL_FLAT);
882 glDisable(GL_DITHER);
883 glDisable(GL_SCISSOR_TEST);
884 glDisable(GL_BLEND);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700885
886 glBindTexture(GL_TEXTURE_2D, 0);
887 glEnable(GL_TEXTURE_2D);
888 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
889 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
890 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
891 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
892 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
893
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700894 bool clockFontInitialized = false;
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800895 if (mClockEnabled) {
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700896 clockFontInitialized =
Huihong Luo50a2f362018-08-11 09:27:57 -0700897 (initFont(&mAnimation->clockFont, CLOCK_FONT_ASSET) == NO_ERROR);
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700898 mClockEnabled = clockFontInitialized;
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800899 }
900
Damien Bargiacchi97480862016-03-29 14:55:55 -0700901 if (mClockEnabled && !updateIsTimeAccurate()) {
902 mTimeCheckThread = new TimeCheckThread(this);
903 mTimeCheckThread->run("BootAnimation::TimeCheckThread", PRIORITY_NORMAL);
904 }
905
Huihong Luo50a2f362018-08-11 09:27:57 -0700906 playAnimation(*mAnimation);
Damien Bargiacchi97480862016-03-29 14:55:55 -0700907
Geoffrey Pitscha9173532016-07-19 14:56:39 -0400908 if (mTimeCheckThread != nullptr) {
Damien Bargiacchi97480862016-03-29 14:55:55 -0700909 mTimeCheckThread->requestExit();
Geoffrey Pitscha9173532016-07-19 14:56:39 -0400910 mTimeCheckThread = nullptr;
911 }
912
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700913 if (clockFontInitialized) {
Huihong Luo50a2f362018-08-11 09:27:57 -0700914 glDeleteTextures(1, &mAnimation->clockFont.texture.name);
Andriy Naborskyy815e51d2016-03-24 16:43:34 -0700915 }
916
Huihong Luo50a2f362018-08-11 09:27:57 -0700917 releaseAnimation(mAnimation);
918 mAnimation = nullptr;
Damien Bargiacchi1a8a2132018-07-24 15:20:26 -0700919
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700920 return false;
921}
922
923bool BootAnimation::playAnimation(const Animation& animation)
924{
925 const size_t pcount = animation.parts.size();
Mathias Agopiana8826d62009-10-01 03:10:14 -0700926 nsecs_t frameDuration = s2ns(1) / animation.fps;
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400927 const int animationX = (mWidth - animation.width) / 2;
928 const int animationY = (mHeight - animation.height) / 2;
Mathias Agopian9f3020d2009-11-06 16:30:18 -0800929
Wei Wang159a4102018-10-23 23:15:58 -0700930 SLOGD("%sAnimationShownTiming start time: %" PRId64 "ms", mShuttingDown ? "Shutdown" : "Boot",
Keun-young Parkd1794cd2017-04-04 12:21:19 -0700931 elapsedRealtime());
Narayan Kamathafd31e02013-12-03 13:16:03 +0000932 for (size_t i=0 ; i<pcount ; i++) {
Mathias Agopiana8826d62009-10-01 03:10:14 -0700933 const Animation::Part& part(animation.parts[i]);
934 const size_t fcount = part.frames.size();
935 glBindTexture(GL_TEXTURE_2D, 0);
936
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700937 // Handle animation package
Yi Kong911ac232019-03-24 01:49:02 -0700938 if (part.animation != nullptr) {
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700939 playAnimation(*part.animation);
940 if (exitPending())
941 break;
942 continue; //to next part
943 }
944
Mathias Agopiana8826d62009-10-01 03:10:14 -0700945 for (int r=0 ; !part.count || r<part.count ; r++) {
Kevin Hesterd3782b22012-04-26 10:38:55 -0700946 // Exit any non playuntil complete parts immediately
947 if(exitPending() && !part.playUntilComplete)
948 break;
949
Ed Coyne7464ac92017-06-08 12:26:48 -0700950 mCallbacks->playPart(i, part, r);
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700951
Jesse Hall083b84c2014-09-22 10:51:09 -0700952 glClearColor(
953 part.backgroundColor[0],
954 part.backgroundColor[1],
955 part.backgroundColor[2],
956 1.0f);
957
Narayan Kamathafd31e02013-12-03 13:16:03 +0000958 for (size_t j=0 ; j<fcount && (!exitPending() || part.playUntilComplete) ; j++) {
Mathias Agopiana8826d62009-10-01 03:10:14 -0700959 const Animation::Frame& frame(part.frames[j]);
Mathias Agopiandb7dd2a2012-05-12 15:08:21 -0700960 nsecs_t lastFrame = systemTime();
Mathias Agopiana8826d62009-10-01 03:10:14 -0700961
962 if (r > 0) {
963 glBindTexture(GL_TEXTURE_2D, frame.tid);
964 } else {
965 if (part.count != 1) {
966 glGenTextures(1, &frame.tid);
967 glBindTexture(GL_TEXTURE_2D, frame.tid);
968 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
969 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
970 }
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700971 int w, h;
972 initTexture(frame.map, &w, &h);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700973 }
974
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400975 const int xc = animationX + frame.trimX;
976 const int yc = animationY + frame.trimY;
977 Region clearReg(Rect(mWidth, mHeight));
978 clearReg.subtractSelf(Rect(xc, yc, xc+frame.trimWidth, yc+frame.trimHeight));
Mathias Agopian9f3020d2009-11-06 16:30:18 -0800979 if (!clearReg.isEmpty()) {
980 Region::const_iterator head(clearReg.begin());
981 Region::const_iterator tail(clearReg.end());
982 glEnable(GL_SCISSOR_TEST);
983 while (head != tail) {
Andreas Gampecfedceb2014-09-30 21:48:18 -0700984 const Rect& r2(*head++);
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400985 glScissor(r2.left, mHeight - r2.bottom, r2.width(), r2.height());
Mathias Agopian9f3020d2009-11-06 16:30:18 -0800986 glClear(GL_COLOR_BUFFER_BIT);
987 }
988 glDisable(GL_SCISSOR_TEST);
989 }
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400990 // specify the y center as ceiling((mHeight - frame.trimHeight) / 2)
991 // which is equivalent to mHeight - (yc + frame.trimHeight)
992 glDrawTexiOES(xc, mHeight - (yc + frame.trimHeight),
993 0, frame.trimWidth, frame.trimHeight);
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700994 if (mClockEnabled && mTimeIsAccurate && validClock(part)) {
995 drawClock(animation.clockFont, part.clockPosX, part.clockPosY);
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800996 }
Adrian Roos9ee5dff2018-08-22 20:19:49 +0200997 handleViewport(frameDuration);
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800998
Mathias Agopiana8826d62009-10-01 03:10:14 -0700999 eglSwapBuffers(mDisplay, mSurface);
1000
1001 nsecs_t now = systemTime();
1002 nsecs_t delay = frameDuration - (now - lastFrame);
Wei Wang159a4102018-10-23 23:15:58 -07001003 //SLOGD("%lld, %lld", ns2ms(now - lastFrame), ns2ms(delay));
Mathias Agopiana8826d62009-10-01 03:10:14 -07001004 lastFrame = now;
Mathias Agopiandb7dd2a2012-05-12 15:08:21 -07001005
1006 if (delay > 0) {
1007 struct timespec spec;
1008 spec.tv_sec = (now + delay) / 1000000000;
1009 spec.tv_nsec = (now + delay) % 1000000000;
1010 int err;
1011 do {
Yi Kong911ac232019-03-24 01:49:02 -07001012 err = clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &spec, nullptr);
Mathias Agopiandb7dd2a2012-05-12 15:08:21 -07001013 } while (err<0 && errno == EINTR);
1014 }
Kevin Hesterd3782b22012-04-26 10:38:55 -07001015
1016 checkExit();
Mathias Agopiana8826d62009-10-01 03:10:14 -07001017 }
Kevin Hesterd3782b22012-04-26 10:38:55 -07001018
Mathias Agopiana8826d62009-10-01 03:10:14 -07001019 usleep(part.pause * ns2us(frameDuration));
Kevin Hesterd3782b22012-04-26 10:38:55 -07001020
1021 // For infinite parts, we've now played them at least once, so perhaps exit
Adrian Roos9ee5dff2018-08-22 20:19:49 +02001022 if(exitPending() && !part.count && mCurrentInset >= mTargetInset)
Kevin Hesterd3782b22012-04-26 10:38:55 -07001023 break;
Mathias Agopiana8826d62009-10-01 03:10:14 -07001024 }
1025
Geoffrey Pitsch2fb30fb2016-07-06 16:16:20 -04001026 }
1027
1028 // Free textures created for looping parts now that the animation is done.
1029 for (const Animation::Part& part : animation.parts) {
Mathias Agopiana8826d62009-10-01 03:10:14 -07001030 if (part.count != 1) {
Geoffrey Pitsch2fb30fb2016-07-06 16:16:20 -04001031 const size_t fcount = part.frames.size();
1032 for (size_t j = 0; j < fcount; j++) {
Mathias Agopiana8826d62009-10-01 03:10:14 -07001033 const Animation::Frame& frame(part.frames[j]);
1034 glDeleteTextures(1, &frame.tid);
1035 }
1036 }
1037 }
Geoffrey Pitschd6d9a1d2016-06-08 00:38:58 -07001038
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001039 return true;
Mathias Agopiana8826d62009-10-01 03:10:14 -07001040}
1041
Adrian Roos9ee5dff2018-08-22 20:19:49 +02001042void BootAnimation::handleViewport(nsecs_t timestep) {
1043 if (mShuttingDown || !mFlingerSurfaceControl || mTargetInset == 0) {
1044 return;
1045 }
1046 if (mTargetInset < 0) {
1047 // Poll the amount for the top display inset. This will return -1 until persistent properties
1048 // have been loaded.
1049 mTargetInset = android::base::GetIntProperty("persist.sys.displayinset.top",
1050 -1 /* default */, -1 /* min */, mHeight / 2 /* max */);
1051 }
1052 if (mTargetInset <= 0) {
1053 return;
1054 }
1055
1056 if (mCurrentInset < mTargetInset) {
1057 // After the device boots, the inset will effectively be cropped away. We animate this here.
1058 float fraction = static_cast<float>(mCurrentInset) / mTargetInset;
1059 int interpolatedInset = (cosf((fraction + 1) * M_PI) / 2.0f + 0.5f) * mTargetInset;
1060
1061 SurfaceComposerClient::Transaction()
1062 .setCrop(mFlingerSurfaceControl, Rect(0, interpolatedInset, mWidth, mHeight))
1063 .apply();
1064 } else {
1065 // At the end of the animation, we switch to the viewport that DisplayManager will apply
1066 // later. This changes the coordinate system, and means we must move the surface up by
1067 // the inset amount.
Adrian Roos9ee5dff2018-08-22 20:19:49 +02001068 Rect layerStackRect(0, 0, mWidth, mHeight - mTargetInset);
1069 Rect displayRect(0, mTargetInset, mWidth, mHeight);
1070
1071 SurfaceComposerClient::Transaction t;
1072 t.setPosition(mFlingerSurfaceControl, 0, -mTargetInset)
1073 .setCrop(mFlingerSurfaceControl, Rect(0, mTargetInset, mWidth, mHeight));
Dominik Laskowski3316a0a2019-01-25 02:56:41 -08001074 t.setDisplayProjection(mDisplayToken, 0 /* orientation */, layerStackRect, displayRect);
Adrian Roos9ee5dff2018-08-22 20:19:49 +02001075 t.apply();
1076
1077 mTargetInset = mCurrentInset = 0;
1078 }
1079
1080 int delta = timestep * mTargetInset / ms2ns(200);
1081 mCurrentInset += delta;
1082}
1083
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001084void BootAnimation::releaseAnimation(Animation* animation) const
1085{
1086 for (Vector<Animation::Part>::iterator it = animation->parts.begin(),
1087 e = animation->parts.end(); it != e; ++it) {
1088 if (it->animation)
1089 releaseAnimation(it->animation);
1090 }
1091 if (animation->zip)
1092 delete animation->zip;
1093 delete animation;
1094}
1095
1096BootAnimation::Animation* BootAnimation::loadAnimation(const String8& fn)
1097{
1098 if (mLoadedFiles.indexOf(fn) >= 0) {
Wei Wang159a4102018-10-23 23:15:58 -07001099 SLOGE("File \"%s\" is already loaded. Cyclic ref is not allowed",
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001100 fn.string());
Yi Kong911ac232019-03-24 01:49:02 -07001101 return nullptr;
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001102 }
1103 ZipFileRO *zip = ZipFileRO::open(fn);
Yi Kong911ac232019-03-24 01:49:02 -07001104 if (zip == nullptr) {
Wei Wang159a4102018-10-23 23:15:58 -07001105 SLOGE("Failed to open animation zip \"%s\": %s",
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001106 fn.string(), strerror(errno));
Yi Kong911ac232019-03-24 01:49:02 -07001107 return nullptr;
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001108 }
1109
1110 Animation *animation = new Animation;
1111 animation->fileName = fn;
1112 animation->zip = zip;
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -07001113 animation->clockFont.map = nullptr;
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001114 mLoadedFiles.add(animation->fileName);
1115
1116 parseAnimationDesc(*animation);
Geoffrey Pitscha91a2d72016-07-12 14:46:19 -04001117 if (!preloadZip(*animation)) {
Yi Kong911ac232019-03-24 01:49:02 -07001118 return nullptr;
Geoffrey Pitscha91a2d72016-07-12 14:46:19 -04001119 }
1120
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001121
1122 mLoadedFiles.remove(fn);
1123 return animation;
1124}
Damien Bargiacchi97480862016-03-29 14:55:55 -07001125
1126bool BootAnimation::updateIsTimeAccurate() {
1127 static constexpr long long MAX_TIME_IN_PAST = 60000LL * 60LL * 24LL * 30LL; // 30 days
1128 static constexpr long long MAX_TIME_IN_FUTURE = 60000LL * 90LL; // 90 minutes
1129
1130 if (mTimeIsAccurate) {
1131 return true;
1132 }
Keun-young Parkb5938422017-03-23 13:46:24 -07001133 if (mShuttingDown) return true;
Damien Bargiacchi97480862016-03-29 14:55:55 -07001134 struct stat statResult;
Damien Bargiacchi9071db12016-10-28 17:38:22 -07001135
1136 if(stat(TIME_FORMAT_12_HOUR_FLAG_FILE_PATH, &statResult) == 0) {
1137 mTimeFormat12Hour = true;
1138 }
1139
Damien Bargiacchi97480862016-03-29 14:55:55 -07001140 if(stat(ACCURATE_TIME_FLAG_FILE_PATH, &statResult) == 0) {
1141 mTimeIsAccurate = true;
1142 return true;
1143 }
1144
1145 FILE* file = fopen(LAST_TIME_CHANGED_FILE_PATH, "r");
Yi Kong911ac232019-03-24 01:49:02 -07001146 if (file != nullptr) {
Damien Bargiacchi97480862016-03-29 14:55:55 -07001147 long long lastChangedTime = 0;
1148 fscanf(file, "%lld", &lastChangedTime);
1149 fclose(file);
1150 if (lastChangedTime > 0) {
1151 struct timespec now;
1152 clock_gettime(CLOCK_REALTIME, &now);
1153 // Match the Java timestamp format
1154 long long rtcNow = (now.tv_sec * 1000LL) + (now.tv_nsec / 1000000LL);
Damien Bargiacchi96762812016-07-12 15:53:40 -07001155 if (ACCURATE_TIME_EPOCH < rtcNow
1156 && lastChangedTime > (rtcNow - MAX_TIME_IN_PAST)
1157 && lastChangedTime < (rtcNow + MAX_TIME_IN_FUTURE)) {
Damien Bargiacchi97480862016-03-29 14:55:55 -07001158 mTimeIsAccurate = true;
1159 }
1160 }
1161 }
1162
1163 return mTimeIsAccurate;
1164}
1165
1166BootAnimation::TimeCheckThread::TimeCheckThread(BootAnimation* bootAnimation) : Thread(false),
1167 mInotifyFd(-1), mSystemWd(-1), mTimeWd(-1), mBootAnimation(bootAnimation) {}
1168
1169BootAnimation::TimeCheckThread::~TimeCheckThread() {
1170 // mInotifyFd may be -1 but that's ok since we're not at risk of attempting to close a valid FD.
1171 close(mInotifyFd);
1172}
1173
1174bool BootAnimation::TimeCheckThread::threadLoop() {
1175 bool shouldLoop = doThreadLoop() && !mBootAnimation->mTimeIsAccurate
1176 && mBootAnimation->mClockEnabled;
1177 if (!shouldLoop) {
1178 close(mInotifyFd);
1179 mInotifyFd = -1;
1180 }
1181 return shouldLoop;
1182}
1183
1184bool BootAnimation::TimeCheckThread::doThreadLoop() {
1185 static constexpr int BUFF_LEN (10 * (sizeof(struct inotify_event) + NAME_MAX + 1));
1186
1187 // Poll instead of doing a blocking read so the Thread can exit if requested.
1188 struct pollfd pfd = { mInotifyFd, POLLIN, 0 };
1189 ssize_t pollResult = poll(&pfd, 1, 1000);
1190
1191 if (pollResult == 0) {
1192 return true;
1193 } else if (pollResult < 0) {
Wei Wang159a4102018-10-23 23:15:58 -07001194 SLOGE("Could not poll inotify events");
Damien Bargiacchi97480862016-03-29 14:55:55 -07001195 return false;
1196 }
1197
1198 char buff[BUFF_LEN] __attribute__ ((aligned(__alignof__(struct inotify_event))));;
1199 ssize_t length = read(mInotifyFd, buff, BUFF_LEN);
1200 if (length == 0) {
1201 return true;
1202 } else if (length < 0) {
Wei Wang159a4102018-10-23 23:15:58 -07001203 SLOGE("Could not read inotify events");
Damien Bargiacchi97480862016-03-29 14:55:55 -07001204 return false;
1205 }
1206
1207 const struct inotify_event *event;
1208 for (char* ptr = buff; ptr < buff + length; ptr += sizeof(struct inotify_event) + event->len) {
1209 event = (const struct inotify_event *) ptr;
1210 if (event->wd == mSystemWd && strcmp(SYSTEM_TIME_DIR_NAME, event->name) == 0) {
1211 addTimeDirWatch();
1212 } else if (event->wd == mTimeWd && (strcmp(LAST_TIME_CHANGED_FILE_NAME, event->name) == 0
1213 || strcmp(ACCURATE_TIME_FLAG_FILE_NAME, event->name) == 0)) {
1214 return !mBootAnimation->updateIsTimeAccurate();
1215 }
1216 }
1217
1218 return true;
1219}
1220
1221void BootAnimation::TimeCheckThread::addTimeDirWatch() {
1222 mTimeWd = inotify_add_watch(mInotifyFd, SYSTEM_TIME_DIR_PATH,
1223 IN_CLOSE_WRITE | IN_MOVED_TO | IN_ATTRIB);
1224 if (mTimeWd > 0) {
1225 // No need to watch for the time directory to be created if it already exists
1226 inotify_rm_watch(mInotifyFd, mSystemWd);
1227 mSystemWd = -1;
1228 }
1229}
1230
1231status_t BootAnimation::TimeCheckThread::readyToRun() {
1232 mInotifyFd = inotify_init();
1233 if (mInotifyFd < 0) {
Wei Wang159a4102018-10-23 23:15:58 -07001234 SLOGE("Could not initialize inotify fd");
Damien Bargiacchi97480862016-03-29 14:55:55 -07001235 return NO_INIT;
1236 }
1237
1238 mSystemWd = inotify_add_watch(mInotifyFd, SYSTEM_DATA_DIR_PATH, IN_CREATE | IN_ATTRIB);
1239 if (mSystemWd < 0) {
1240 close(mInotifyFd);
1241 mInotifyFd = -1;
Wei Wang159a4102018-10-23 23:15:58 -07001242 SLOGE("Could not add watch for %s", SYSTEM_DATA_DIR_PATH);
Damien Bargiacchi97480862016-03-29 14:55:55 -07001243 return NO_INIT;
1244 }
1245
1246 addTimeDirWatch();
1247
1248 if (mBootAnimation->updateIsTimeAccurate()) {
1249 close(mInotifyFd);
1250 mInotifyFd = -1;
1251 return ALREADY_EXISTS;
1252 }
1253
1254 return NO_ERROR;
1255}
1256
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001257// ---------------------------------------------------------------------------
1258
1259}
1260; // namespace android