blob: 85db4b9f0f8f4b7aa76be04fc0354f834a0b659d [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
Nikita Ioffe06c986e2020-01-27 17:16:03 +000020#include <vector>
21
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080022#include <stdint.h>
Wei Wanga90c54c2017-02-02 11:35:21 -080023#include <inttypes.h>
Damien Bargiacchi97480862016-03-29 14:55:55 -070024#include <sys/inotify.h>
25#include <sys/poll.h>
26#include <sys/stat.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080027#include <sys/types.h>
28#include <math.h>
29#include <fcntl.h>
30#include <utils/misc.h>
Mathias Agopianb4d5a722009-09-23 17:05:19 -070031#include <signal.h>
Elliott Hughesbb94f312014-10-21 10:41:33 -070032#include <time.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080033
Steven Morelandfb7952f2018-02-23 14:58:50 -080034#include <cutils/atomic.h>
Jason parksbd9a08d2011-01-31 15:04:34 -060035#include <cutils/properties.h>
36
Mathias Agopianb13b9bd2012-02-17 18:27:36 -080037#include <androidfw/AssetManager.h>
Mathias Agopianac31a3b2009-05-21 19:59:24 -070038#include <binder/IPCThreadState.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080039#include <utils/Errors.h>
40#include <utils/Log.h>
Wei Wanga90c54c2017-02-02 11:35:21 -080041#include <utils/SystemClock.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080042
Keun-young Parkb5938422017-03-23 13:46:24 -070043#include <android-base/properties.h>
44
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080045#include <ui/PixelFormat.h>
46#include <ui/Rect.h>
47#include <ui/Region.h>
48#include <ui/DisplayInfo.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080049
Jeff Brown0b722fe2012-08-24 22:40:14 -070050#include <gui/ISurfaceComposer.h>
Mathias Agopian8335f1c2012-02-25 18:48:35 -080051#include <gui/Surface.h>
52#include <gui/SurfaceComposerClient.h>
Mathias Agopian000479f2010-02-09 17:46:37 -080053
Andreas Gampecfedceb2014-09-30 21:48:18 -070054// TODO: Fix Skia.
55#pragma GCC diagnostic push
56#pragma GCC diagnostic ignored "-Wunused-parameter"
Derek Sollenbergereece0dd2014-02-27 14:31:29 -050057#include <SkBitmap.h>
Matt Sarett8898c162016-03-24 16:11:01 -040058#include <SkImage.h>
Derek Sollenbergereece0dd2014-02-27 14:31:29 -050059#include <SkStream.h>
Andreas Gampecfedceb2014-09-30 21:48:18 -070060#pragma GCC diagnostic pop
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080061
62#include <GLES/gl.h>
63#include <GLES/glext.h>
64#include <EGL/eglext.h>
65
66#include "BootAnimation.h"
67
Kyeongkab.Nam35a8a112019-07-17 16:41:48 +090068#define ANIM_PATH_MAX 255
69#define STR(x) #x
70#define STRTO(x) STR(x)
71
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080072namespace android {
73
Damien Bargiacchi97480862016-03-29 14:55:55 -070074static const char OEM_BOOTANIMATION_FILE[] = "/oem/media/bootanimation.zip";
Beverly34ffe252019-05-17 11:03:54 -040075static const char PRODUCT_BOOTANIMATION_DARK_FILE[] = "/product/media/bootanimation-dark.zip";
Jaekyun Seokc521bb32018-01-30 11:52:47 +090076static const char PRODUCT_BOOTANIMATION_FILE[] = "/product/media/bootanimation.zip";
Damien Bargiacchi97480862016-03-29 14:55:55 -070077static const char SYSTEM_BOOTANIMATION_FILE[] = "/system/media/bootanimation.zip";
Anders Fridlunde0b4d232018-11-06 14:23:25 +010078static const char APEX_BOOTANIMATION_FILE[] = "/apex/com.android.bootanimation/etc/bootanimation.zip";
Jaekyun Seokc521bb32018-01-30 11:52:47 +090079static const char PRODUCT_ENCRYPTED_BOOTANIMATION_FILE[] = "/product/media/bootanimation-encrypted.zip";
Damien Bargiacchi97480862016-03-29 14:55:55 -070080static const char SYSTEM_ENCRYPTED_BOOTANIMATION_FILE[] = "/system/media/bootanimation-encrypted.zip";
Keun-young Parkb5938422017-03-23 13:46:24 -070081static const char OEM_SHUTDOWNANIMATION_FILE[] = "/oem/media/shutdownanimation.zip";
Jaekyun Seokc521bb32018-01-30 11:52:47 +090082static const char PRODUCT_SHUTDOWNANIMATION_FILE[] = "/product/media/shutdownanimation.zip";
Keun-young Parkb5938422017-03-23 13:46:24 -070083static const char SYSTEM_SHUTDOWNANIMATION_FILE[] = "/system/media/shutdownanimation.zip";
84
Nikita Ioffe06c986e2020-01-27 17:16:03 +000085static constexpr const char* PRODUCT_USERSPACE_REBOOT_ANIMATION_FILE = "/product/media/userspace-reboot.zip";
86static constexpr const char* OEM_USERSPACE_REBOOT_ANIMATION_FILE = "/oem/media/userspace-reboot.zip";
87static constexpr const char* SYSTEM_USERSPACE_REBOOT_ANIMATION_FILE = "/system/media/userspace-reboot.zip";
88
Damien Bargiacchi97480862016-03-29 14:55:55 -070089static const char SYSTEM_DATA_DIR_PATH[] = "/data/system";
90static const char SYSTEM_TIME_DIR_NAME[] = "time";
91static const char SYSTEM_TIME_DIR_PATH[] = "/data/system/time";
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -070092static const char CLOCK_FONT_ASSET[] = "images/clock_font.png";
93static const char CLOCK_FONT_ZIP_NAME[] = "clock_font.png";
Damien Bargiacchi97480862016-03-29 14:55:55 -070094static const char LAST_TIME_CHANGED_FILE_NAME[] = "last_time_change";
95static const char LAST_TIME_CHANGED_FILE_PATH[] = "/data/system/time/last_time_change";
96static const char ACCURATE_TIME_FLAG_FILE_NAME[] = "time_is_accurate";
97static const char ACCURATE_TIME_FLAG_FILE_PATH[] = "/data/system/time/time_is_accurate";
Damien Bargiacchi9071db12016-10-28 17:38:22 -070098static const char TIME_FORMAT_12_HOUR_FLAG_FILE_PATH[] = "/data/system/time/time_format_12_hour";
Damien Bargiacchi96762812016-07-12 15:53:40 -070099// Java timestamp format. Don't show the clock if the date is before 2000-01-01 00:00:00.
100static const long long ACCURATE_TIME_EPOCH = 946684800000;
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700101static constexpr char FONT_BEGIN_CHAR = ' ';
102static constexpr char FONT_END_CHAR = '~' + 1;
103static constexpr size_t FONT_NUM_CHARS = FONT_END_CHAR - FONT_BEGIN_CHAR + 1;
104static constexpr size_t FONT_NUM_COLS = 16;
105static constexpr size_t FONT_NUM_ROWS = FONT_NUM_CHARS / FONT_NUM_COLS;
106static const int TEXT_CENTER_VALUE = INT_MAX;
107static const int TEXT_MISSING_VALUE = INT_MIN;
Damien Bargiacchi97480862016-03-29 14:55:55 -0700108static const char EXIT_PROP_NAME[] = "service.bootanim.exit";
Kyeongkab.Nam35a8a112019-07-17 16:41:48 +0900109static const int ANIM_ENTRY_NAME_MAX = ANIM_PATH_MAX + 1;
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700110static constexpr size_t TEXT_POS_LEN_MAX = 16;
Narayan Kamathafd31e02013-12-03 13:16:03 +0000111
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800112// ---------------------------------------------------------------------------
113
Ed Coyne7464ac92017-06-08 12:26:48 -0700114BootAnimation::BootAnimation(sp<Callbacks> callbacks)
Ed Coyne2c9e94a2017-05-31 10:08:28 -0700115 : Thread(false), mClockEnabled(true), mTimeIsAccurate(false),
Yi Kong911ac232019-03-24 01:49:02 -0700116 mTimeFormat12Hour(false), mTimeCheckThread(nullptr), mCallbacks(callbacks) {
Mathias Agopian627e7b52009-05-21 19:21:59 -0700117 mSession = new SurfaceComposerClient();
Geoffrey Pitsch290c4352016-08-09 14:35:10 -0400118
Keun-young Parkb5938422017-03-23 13:46:24 -0700119 std::string powerCtl = android::base::GetProperty("sys.powerctl", "");
120 if (powerCtl.empty()) {
121 mShuttingDown = false;
122 } else {
123 mShuttingDown = true;
124 }
Huihong Luo50a2f362018-08-11 09:27:57 -0700125 ALOGD("%sAnimationStartTiming start time: %" PRId64 "ms", mShuttingDown ? "Shutdown" : "Boot",
126 elapsedRealtime());
127}
128
129BootAnimation::~BootAnimation() {
130 if (mAnimation != nullptr) {
131 releaseAnimation(mAnimation);
132 mAnimation = nullptr;
133 }
134 ALOGD("%sAnimationStopTiming start time: %" PRId64 "ms", mShuttingDown ? "Shutdown" : "Boot",
135 elapsedRealtime());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800136}
137
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800138void BootAnimation::onFirstRef() {
Mathias Agopianbc726112009-09-23 15:44:05 -0700139 status_t err = mSession->linkToComposerDeath(this);
Wei Wang159a4102018-10-23 23:15:58 -0700140 SLOGE_IF(err, "linkToComposerDeath failed (%s) ", strerror(-err));
Mathias Agopian8434c532009-09-23 18:52:49 -0700141 if (err == NO_ERROR) {
Huihong Luo50a2f362018-08-11 09:27:57 -0700142 // Load the animation content -- this can be slow (eg 200ms)
143 // called before waitForSurfaceFlinger() in main() to avoid wait
144 ALOGD("%sAnimationPreloadTiming start time: %" PRId64 "ms",
145 mShuttingDown ? "Shutdown" : "Boot", elapsedRealtime());
146 preloadAnimation();
147 ALOGD("%sAnimationPreloadStopTiming start time: %" PRId64 "ms",
148 mShuttingDown ? "Shutdown" : "Boot", elapsedRealtime());
Mathias Agopianbc726112009-09-23 15:44:05 -0700149 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800150}
151
Mathias Agopianbc726112009-09-23 15:44:05 -0700152sp<SurfaceComposerClient> BootAnimation::session() const {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800153 return mSession;
154}
155
Narayan Kamathafd31e02013-12-03 13:16:03 +0000156void BootAnimation::binderDied(const wp<IBinder>&)
Mathias Agopianbc726112009-09-23 15:44:05 -0700157{
158 // woah, surfaceflinger died!
Wei Wang159a4102018-10-23 23:15:58 -0700159 SLOGD("SurfaceFlinger died, exiting...");
Mathias Agopianbc726112009-09-23 15:44:05 -0700160
161 // calling requestExit() is not enough here because the Surface code
162 // might be blocked on a condition variable that will never be updated.
163 kill( getpid(), SIGKILL );
164 requestExit();
165}
166
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800167status_t BootAnimation::initTexture(Texture* texture, AssetManager& assets,
168 const char* name) {
169 Asset* asset = assets.open(name, Asset::ACCESS_BUFFER);
Yi Kong911ac232019-03-24 01:49:02 -0700170 if (asset == nullptr)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800171 return NO_INIT;
172 SkBitmap bitmap;
Matt Sarett8898c162016-03-24 16:11:01 -0400173 sk_sp<SkData> data = SkData::MakeWithoutCopy(asset->getBuffer(false),
174 asset->getLength());
175 sk_sp<SkImage> image = SkImage::MakeFromEncoded(data);
176 image->asLegacyBitmap(&bitmap, SkImage::kRO_LegacyBitmapMode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800177 asset->close();
178 delete asset;
179
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800180 const int w = bitmap.width();
181 const int h = bitmap.height();
182 const void* p = bitmap.getPixels();
183
184 GLint crop[4] = { 0, h, w, -h };
185 texture->w = w;
186 texture->h = h;
187
188 glGenTextures(1, &texture->name);
189 glBindTexture(GL_TEXTURE_2D, texture->name);
190
Mike Reed42a1d082014-07-07 18:06:18 -0400191 switch (bitmap.colorType()) {
192 case kAlpha_8_SkColorType:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800193 glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, w, h, 0, GL_ALPHA,
194 GL_UNSIGNED_BYTE, p);
195 break;
Mike Reed42a1d082014-07-07 18:06:18 -0400196 case kARGB_4444_SkColorType:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800197 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA,
198 GL_UNSIGNED_SHORT_4_4_4_4, p);
199 break;
Mike Reed42a1d082014-07-07 18:06:18 -0400200 case kN32_SkColorType:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800201 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA,
202 GL_UNSIGNED_BYTE, p);
203 break;
Mike Reed42a1d082014-07-07 18:06:18 -0400204 case kRGB_565_SkColorType:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800205 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, w, h, 0, GL_RGB,
206 GL_UNSIGNED_SHORT_5_6_5, p);
207 break;
208 default:
209 break;
210 }
211
212 glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_CROP_RECT_OES, crop);
213 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
214 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
215 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
216 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700217
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800218 return NO_ERROR;
219}
220
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700221status_t BootAnimation::initTexture(FileMap* map, int* width, int* height)
Mathias Agopiana8826d62009-10-01 03:10:14 -0700222{
Mathias Agopiana8826d62009-10-01 03:10:14 -0700223 SkBitmap bitmap;
Damien Bargiacchif67b3172016-09-07 20:17:14 -0700224 sk_sp<SkData> data = SkData::MakeWithoutCopy(map->getDataPtr(),
225 map->getDataLength());
Matt Sarett8898c162016-03-24 16:11:01 -0400226 sk_sp<SkImage> image = SkImage::MakeFromEncoded(data);
227 image->asLegacyBitmap(&bitmap, SkImage::kRO_LegacyBitmapMode);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700228
Mykola Kondratenko0c1eeb32014-04-15 09:35:44 +0200229 // FileMap memory is never released until application exit.
230 // Release it now as the texture is already loaded and the memory used for
231 // the packed resource can be released.
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700232 delete map;
Mykola Kondratenko0c1eeb32014-04-15 09:35:44 +0200233
Mathias Agopiana8826d62009-10-01 03:10:14 -0700234 const int w = bitmap.width();
235 const int h = bitmap.height();
236 const void* p = bitmap.getPixels();
237
238 GLint crop[4] = { 0, h, w, -h };
239 int tw = 1 << (31 - __builtin_clz(w));
240 int th = 1 << (31 - __builtin_clz(h));
241 if (tw < w) tw <<= 1;
242 if (th < h) th <<= 1;
243
Mike Reed42a1d082014-07-07 18:06:18 -0400244 switch (bitmap.colorType()) {
245 case kN32_SkColorType:
Sai Kiran Korwar27167492015-07-07 20:00:06 +0530246 if (!mUseNpotTextures && (tw != w || th != h)) {
Mathias Agopiana8826d62009-10-01 03:10:14 -0700247 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, tw, th, 0, GL_RGBA,
Yi Kong911ac232019-03-24 01:49:02 -0700248 GL_UNSIGNED_BYTE, nullptr);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700249 glTexSubImage2D(GL_TEXTURE_2D, 0,
250 0, 0, w, h, GL_RGBA, GL_UNSIGNED_BYTE, p);
251 } else {
Sai Kiran Korwar27167492015-07-07 20:00:06 +0530252 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA,
Mathias Agopiana8826d62009-10-01 03:10:14 -0700253 GL_UNSIGNED_BYTE, p);
254 }
255 break;
256
Mike Reed42a1d082014-07-07 18:06:18 -0400257 case kRGB_565_SkColorType:
Sai Kiran Korwar27167492015-07-07 20:00:06 +0530258 if (!mUseNpotTextures && (tw != w || th != h)) {
Mathias Agopiana8826d62009-10-01 03:10:14 -0700259 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, tw, th, 0, GL_RGB,
Yi Kong911ac232019-03-24 01:49:02 -0700260 GL_UNSIGNED_SHORT_5_6_5, nullptr);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700261 glTexSubImage2D(GL_TEXTURE_2D, 0,
262 0, 0, w, h, GL_RGB, GL_UNSIGNED_SHORT_5_6_5, p);
263 } else {
Sai Kiran Korwar27167492015-07-07 20:00:06 +0530264 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, w, h, 0, GL_RGB,
Mathias Agopiana8826d62009-10-01 03:10:14 -0700265 GL_UNSIGNED_SHORT_5_6_5, p);
266 }
267 break;
268 default:
269 break;
270 }
271
272 glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_CROP_RECT_OES, crop);
273
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700274 *width = w;
275 *height = h;
276
Mathias Agopiana8826d62009-10-01 03:10:14 -0700277 return NO_ERROR;
278}
279
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800280status_t BootAnimation::readyToRun() {
281 mAssets.addDefaultAssets();
282
Dominik Laskowski3316a0a2019-01-25 02:56:41 -0800283 mDisplayToken = SurfaceComposerClient::getInternalDisplayToken();
284 if (mDisplayToken == nullptr)
285 return -1;
286
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800287 DisplayInfo dinfo;
Dominik Laskowski3316a0a2019-01-25 02:56:41 -0800288 status_t status = SurfaceComposerClient::getDisplayInfo(mDisplayToken, &dinfo);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800289 if (status)
290 return -1;
291
292 // create the native surface
Jeff Brown0b722fe2012-08-24 22:40:14 -0700293 sp<SurfaceControl> control = session()->createSurface(String8("BootAnimation"),
294 dinfo.w, dinfo.h, PIXEL_FORMAT_RGB_565);
Mathias Agopian439863f2011-06-28 19:09:31 -0700295
Robert Carre13b58e2017-08-31 14:50:44 -0700296 SurfaceComposerClient::Transaction t;
297 t.setLayer(control, 0x40000000)
298 .apply();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800299
Mathias Agopian17f638b2009-04-16 20:04:08 -0700300 sp<Surface> s = control->getSurface();
301
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800302 // initialize opengl and egl
Mathias Agopian738b9a42009-08-06 16:41:02 -0700303 const EGLint attribs[] = {
Mathias Agopian1b253b72011-08-15 15:20:22 -0700304 EGL_RED_SIZE, 8,
305 EGL_GREEN_SIZE, 8,
306 EGL_BLUE_SIZE, 8,
Mathias Agopiana8826d62009-10-01 03:10:14 -0700307 EGL_DEPTH_SIZE, 0,
308 EGL_NONE
Mathias Agopian738b9a42009-08-06 16:41:02 -0700309 };
Andreas Gampecfedceb2014-09-30 21:48:18 -0700310 EGLint w, h;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800311 EGLint numConfigs;
312 EGLConfig config;
313 EGLSurface surface;
314 EGLContext context;
Mathias Agopian627e7b52009-05-21 19:21:59 -0700315
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800316 EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
Mathias Agopian627e7b52009-05-21 19:21:59 -0700317
Yi Kong911ac232019-03-24 01:49:02 -0700318 eglInitialize(display, nullptr, nullptr);
Mathias Agopian1b253b72011-08-15 15:20:22 -0700319 eglChooseConfig(display, attribs, &config, 1, &numConfigs);
Yi Kong911ac232019-03-24 01:49:02 -0700320 surface = eglCreateWindowSurface(display, config, s.get(), nullptr);
321 context = eglCreateContext(display, config, nullptr, nullptr);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800322 eglQuerySurface(display, surface, EGL_WIDTH, &w);
323 eglQuerySurface(display, surface, EGL_HEIGHT, &h);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700324
Mathias Agopianabac0102009-07-31 14:47:00 -0700325 if (eglMakeCurrent(display, surface, surface, context) == EGL_FALSE)
326 return NO_INIT;
Mathias Agopiana8826d62009-10-01 03:10:14 -0700327
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800328 mDisplay = display;
329 mContext = context;
330 mSurface = surface;
331 mWidth = w;
332 mHeight = h;
Mathias Agopian17f638b2009-04-16 20:04:08 -0700333 mFlingerSurfaceControl = control;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800334 mFlingerSurface = s;
Adrian Roos9ee5dff2018-08-22 20:19:49 +0200335 mTargetInset = -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800336
Huihong Luo50a2f362018-08-11 09:27:57 -0700337 return NO_ERROR;
338}
339
340bool BootAnimation::preloadAnimation() {
341 findBootAnimationFile();
342 if (!mZipFileName.isEmpty()) {
343 mAnimation = loadAnimation(mZipFileName);
344 return (mAnimation != nullptr);
345 }
346
347 return false;
348}
349
Nikita Ioffe06c986e2020-01-27 17:16:03 +0000350bool BootAnimation::findBootAnimationFileInternal(const std::vector<std::string> &files) {
351 for (const std::string& f : files) {
352 if (access(f.c_str(), R_OK) == 0) {
353 mZipFileName = f.c_str();
354 return true;
355 }
356 }
357 return false;
358}
359
Huihong Luo50a2f362018-08-11 09:27:57 -0700360void BootAnimation::findBootAnimationFile() {
Elliott Hughesc367d482013-10-29 13:12:55 -0700361 // If the device has encryption turned on or is in process
Jason parksbd9a08d2011-01-31 15:04:34 -0600362 // of being encrypted we show the encrypted boot animation.
363 char decrypt[PROPERTY_VALUE_MAX];
364 property_get("vold.decrypt", decrypt, "");
365
Keun-young Parkb5938422017-03-23 13:46:24 -0700366 bool encryptedAnimation = atoi(decrypt) != 0 ||
367 !strcmp("trigger_restart_min_framework", decrypt);
Jason parksbd9a08d2011-01-31 15:04:34 -0600368
Jaekyun Seokc521bb32018-01-30 11:52:47 +0900369 if (!mShuttingDown && encryptedAnimation) {
Nikita Ioffe06c986e2020-01-27 17:16:03 +0000370 static const std::vector<std::string> encryptedBootFiles = {
371 PRODUCT_ENCRYPTED_BOOTANIMATION_FILE, SYSTEM_ENCRYPTED_BOOTANIMATION_FILE,
372 };
373 if (findBootAnimationFileInternal(encryptedBootFiles)) {
374 return;
Jaekyun Seokc521bb32018-01-30 11:52:47 +0900375 }
Jason parksbd9a08d2011-01-31 15:04:34 -0600376 }
Beverly34ffe252019-05-17 11:03:54 -0400377
378 const bool playDarkAnim = android::base::GetIntProperty("ro.boot.theme", 0) == 1;
Nikita Ioffe06c986e2020-01-27 17:16:03 +0000379 static const std::vector<std::string> bootFiles = {
380 APEX_BOOTANIMATION_FILE, playDarkAnim ? PRODUCT_BOOTANIMATION_DARK_FILE : PRODUCT_BOOTANIMATION_FILE,
381 OEM_BOOTANIMATION_FILE, SYSTEM_BOOTANIMATION_FILE
382 };
383 static const std::vector<std::string> shutdownFiles = {
384 PRODUCT_SHUTDOWNANIMATION_FILE, OEM_SHUTDOWNANIMATION_FILE, SYSTEM_SHUTDOWNANIMATION_FILE, ""
385 };
386 static const std::vector<std::string> userspaceRebootFiles = {
387 PRODUCT_USERSPACE_REBOOT_ANIMATION_FILE, OEM_USERSPACE_REBOOT_ANIMATION_FILE,
388 SYSTEM_USERSPACE_REBOOT_ANIMATION_FILE,
389 };
Keun-young Parkb5938422017-03-23 13:46:24 -0700390
Nikita Ioffe06c986e2020-01-27 17:16:03 +0000391 if (android::base::GetBoolProperty("sys.init.userspace_reboot.in_progress", false)) {
392 findBootAnimationFileInternal(userspaceRebootFiles);
393 } else if (mShuttingDown) {
394 findBootAnimationFileInternal(shutdownFiles);
395 } else {
396 findBootAnimationFileInternal(bootFiles);
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700397 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800398}
399
Mathias Agopiana8826d62009-10-01 03:10:14 -0700400bool BootAnimation::threadLoop()
401{
402 bool r;
Narayan Kamathafd31e02013-12-03 13:16:03 +0000403 // We have no bootanimation file, so we use the stock android logo
404 // animation.
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700405 if (mZipFileName.isEmpty()) {
Mathias Agopiana8826d62009-10-01 03:10:14 -0700406 r = android();
407 } else {
408 r = movie();
409 }
410
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800411 eglMakeCurrent(mDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
412 eglDestroyContext(mDisplay, mContext);
413 eglDestroySurface(mDisplay, mSurface);
Mathias Agopian6cf0db22009-04-17 19:36:26 -0700414 mFlingerSurface.clear();
Mathias Agopian17f638b2009-04-16 20:04:08 -0700415 mFlingerSurfaceControl.clear();
Mathias Agopian627e7b52009-05-21 19:21:59 -0700416 eglTerminate(mDisplay);
Sai Kiran Korwar3eee9fb2015-06-16 17:13:35 +0530417 eglReleaseThread();
Mathias Agopian627e7b52009-05-21 19:21:59 -0700418 IPCThreadState::self()->stopProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800419 return r;
420}
421
Mathias Agopiana8826d62009-10-01 03:10:14 -0700422bool BootAnimation::android()
423{
Wei Wang159a4102018-10-23 23:15:58 -0700424 SLOGD("%sAnimationShownTiming start time: %" PRId64 "ms", mShuttingDown ? "Shutdown" : "Boot",
Keun-young Parkd1794cd2017-04-04 12:21:19 -0700425 elapsedRealtime());
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700426 initTexture(&mAndroid[0], mAssets, "images/android-logo-mask.png");
427 initTexture(&mAndroid[1], mAssets, "images/android-logo-shine.png");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800428
Ed Coyne7464ac92017-06-08 12:26:48 -0700429 mCallbacks->init({});
430
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800431 // clear screen
Mathias Agopiana8826d62009-10-01 03:10:14 -0700432 glShadeModel(GL_FLAT);
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700433 glDisable(GL_DITHER);
434 glDisable(GL_SCISSOR_TEST);
Mathias Agopian59f19e42011-05-06 19:22:12 -0700435 glClearColor(0,0,0,1);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800436 glClear(GL_COLOR_BUFFER_BIT);
437 eglSwapBuffers(mDisplay, mSurface);
438
Mathias Agopiana8826d62009-10-01 03:10:14 -0700439 glEnable(GL_TEXTURE_2D);
440 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
441
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700442 const GLint xc = (mWidth - mAndroid[0].w) / 2;
443 const GLint yc = (mHeight - mAndroid[0].h) / 2;
444 const Rect updateRect(xc, yc, xc + mAndroid[0].w, yc + mAndroid[0].h);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800445
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800446 glScissor(updateRect.left, mHeight - updateRect.bottom, updateRect.width(),
447 updateRect.height());
448
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700449 // Blend state
450 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
451 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
452
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800453 const nsecs_t startTime = systemTime();
454 do {
Mathias Agopian13796652009-03-24 22:49:21 -0700455 nsecs_t now = systemTime();
456 double time = now - startTime;
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700457 float t = 4.0f * float(time / us2ns(16667)) / mAndroid[1].w;
458 GLint offset = (1 - (t - floorf(t))) * mAndroid[1].w;
459 GLint x = xc - offset;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800460
Mathias Agopian81668642009-07-28 11:41:30 -0700461 glDisable(GL_SCISSOR_TEST);
462 glClear(GL_COLOR_BUFFER_BIT);
463
464 glEnable(GL_SCISSOR_TEST);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800465 glDisable(GL_BLEND);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800466 glBindTexture(GL_TEXTURE_2D, mAndroid[1].name);
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700467 glDrawTexiOES(x, yc, 0, mAndroid[1].w, mAndroid[1].h);
468 glDrawTexiOES(x + mAndroid[1].w, yc, 0, mAndroid[1].w, mAndroid[1].h);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800469
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700470 glEnable(GL_BLEND);
471 glBindTexture(GL_TEXTURE_2D, mAndroid[0].name);
472 glDrawTexiOES(xc, yc, 0, mAndroid[0].w, mAndroid[0].h);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800473
Mathias Agopian627e7b52009-05-21 19:21:59 -0700474 EGLBoolean res = eglSwapBuffers(mDisplay, mSurface);
475 if (res == EGL_FALSE)
476 break;
477
Mathias Agopian13796652009-03-24 22:49:21 -0700478 // 12fps: don't animate too fast to preserve CPU
479 const nsecs_t sleepTime = 83333 - ns2us(systemTime() - now);
480 if (sleepTime > 0)
Mathias Agopiana8826d62009-10-01 03:10:14 -0700481 usleep(sleepTime);
Kevin Hesterd3782b22012-04-26 10:38:55 -0700482
483 checkExit();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800484 } while (!exitPending());
485
486 glDeleteTextures(1, &mAndroid[0].name);
487 glDeleteTextures(1, &mAndroid[1].name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800488 return false;
489}
490
Kevin Hesterd3782b22012-04-26 10:38:55 -0700491void BootAnimation::checkExit() {
492 // Allow surface flinger to gracefully request shutdown
493 char value[PROPERTY_VALUE_MAX];
494 property_get(EXIT_PROP_NAME, value, "0");
495 int exitnow = atoi(value);
496 if (exitnow) {
497 requestExit();
Ed Coyne7464ac92017-06-08 12:26:48 -0700498 mCallbacks->shutdown();
Kevin Hesterd3782b22012-04-26 10:38:55 -0700499 }
500}
501
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700502bool BootAnimation::validClock(const Animation::Part& part) {
503 return part.clockPosX != TEXT_MISSING_VALUE && part.clockPosY != TEXT_MISSING_VALUE;
504}
505
506bool parseTextCoord(const char* str, int* dest) {
507 if (strcmp("c", str) == 0) {
508 *dest = TEXT_CENTER_VALUE;
509 return true;
510 }
511
512 char* end;
513 int val = (int) strtol(str, &end, 0);
514 if (end == str || *end != '\0' || val == INT_MAX || val == INT_MIN) {
515 return false;
516 }
517 *dest = val;
518 return true;
519}
520
521// Parse two position coordinates. If only string is non-empty, treat it as the y value.
522void parsePosition(const char* str1, const char* str2, int* x, int* y) {
523 bool success = false;
524 if (strlen(str1) == 0) { // No values were specified
525 // success = false
526 } else if (strlen(str2) == 0) { // we have only one value
527 if (parseTextCoord(str1, y)) {
528 *x = TEXT_CENTER_VALUE;
529 success = true;
530 }
531 } else {
532 if (parseTextCoord(str1, x) && parseTextCoord(str2, y)) {
533 success = true;
534 }
535 }
536
537 if (!success) {
538 *x = TEXT_MISSING_VALUE;
539 *y = TEXT_MISSING_VALUE;
540 }
541}
542
Jesse Hall083b84c2014-09-22 10:51:09 -0700543// Parse a color represented as an HTML-style 'RRGGBB' string: each pair of
544// characters in str is a hex number in [0, 255], which are converted to
545// floating point values in the range [0.0, 1.0] and placed in the
546// corresponding elements of color.
547//
548// If the input string isn't valid, parseColor returns false and color is
549// left unchanged.
550static bool parseColor(const char str[7], float color[3]) {
551 float tmpColor[3];
552 for (int i = 0; i < 3; i++) {
553 int val = 0;
554 for (int j = 0; j < 2; j++) {
555 val *= 16;
556 char c = str[2*i + j];
557 if (c >= '0' && c <= '9') val += c - '0';
558 else if (c >= 'A' && c <= 'F') val += (c - 'A') + 10;
559 else if (c >= 'a' && c <= 'f') val += (c - 'a') + 10;
560 else return false;
561 }
562 tmpColor[i] = static_cast<float>(val) / 255.0f;
563 }
564 memcpy(color, tmpColor, sizeof(tmpColor));
565 return true;
566}
567
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700568
569static bool readFile(ZipFileRO* zip, const char* name, String8& outString)
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700570{
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700571 ZipEntryRO entry = zip->findEntryByName(name);
Wei Wang159a4102018-10-23 23:15:58 -0700572 SLOGE_IF(!entry, "couldn't find %s", name);
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700573 if (!entry) {
574 return false;
575 }
576
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700577 FileMap* entryMap = zip->createEntryFileMap(entry);
578 zip->releaseEntry(entry);
Wei Wang159a4102018-10-23 23:15:58 -0700579 SLOGE_IF(!entryMap, "entryMap is null");
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700580 if (!entryMap) {
581 return false;
582 }
583
584 outString.setTo((char const*)entryMap->getDataPtr(), entryMap->getDataLength());
Narayan Kamath688ff4c2015-02-23 15:47:54 +0000585 delete entryMap;
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700586 return true;
587}
588
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700589// The font image should be a 96x2 array of character images. The
590// columns are the printable ASCII characters 0x20 - 0x7f. The
591// top row is regular text; the bottom row is bold.
592status_t BootAnimation::initFont(Font* font, const char* fallback) {
593 status_t status = NO_ERROR;
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800594
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700595 if (font->map != nullptr) {
596 glGenTextures(1, &font->texture.name);
597 glBindTexture(GL_TEXTURE_2D, font->texture.name);
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800598
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700599 status = initTexture(font->map, &font->texture.w, &font->texture.h);
600
601 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
602 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
603 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
604 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
605 } else if (fallback != nullptr) {
606 status = initTexture(&font->texture, mAssets, fallback);
607 } else {
608 return NO_INIT;
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800609 }
610
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700611 if (status == NO_ERROR) {
612 font->char_width = font->texture.w / FONT_NUM_COLS;
613 font->char_height = font->texture.h / FONT_NUM_ROWS / 2; // There are bold and regular rows
614 }
615
616 return status;
617}
618
619void BootAnimation::drawText(const char* str, const Font& font, bool bold, int* x, int* y) {
620 glEnable(GL_BLEND); // Allow us to draw on top of the animation
621 glBindTexture(GL_TEXTURE_2D, font.texture.name);
622
623 const int len = strlen(str);
624 const int strWidth = font.char_width * len;
625
626 if (*x == TEXT_CENTER_VALUE) {
627 *x = (mWidth - strWidth) / 2;
628 } else if (*x < 0) {
629 *x = mWidth + *x - strWidth;
630 }
631 if (*y == TEXT_CENTER_VALUE) {
632 *y = (mHeight - font.char_height) / 2;
633 } else if (*y < 0) {
634 *y = mHeight + *y - font.char_height;
635 }
636
637 int cropRect[4] = { 0, 0, font.char_width, -font.char_height };
638
639 for (int i = 0; i < len; i++) {
640 char c = str[i];
641
642 if (c < FONT_BEGIN_CHAR || c > FONT_END_CHAR) {
643 c = '?';
644 }
645
646 // Crop the texture to only the pixels in the current glyph
647 const int charPos = (c - FONT_BEGIN_CHAR); // Position in the list of valid characters
648 const int row = charPos / FONT_NUM_COLS;
649 const int col = charPos % FONT_NUM_COLS;
650 cropRect[0] = col * font.char_width; // Left of column
651 cropRect[1] = row * font.char_height * 2; // Top of row
652 // Move down to bottom of regular (one char_heigh) or bold (two char_heigh) line
653 cropRect[1] += bold ? 2 * font.char_height : font.char_height;
654 glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_CROP_RECT_OES, cropRect);
655
656 glDrawTexiOES(*x, *y, 0, font.char_width, font.char_height);
657
658 *x += font.char_width;
659 }
660
661 glDisable(GL_BLEND); // Return to the animation's default behaviour
662 glBindTexture(GL_TEXTURE_2D, 0);
663}
664
Damien Bargiacchi9071db12016-10-28 17:38:22 -0700665// We render 12 or 24 hour time.
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700666void BootAnimation::drawClock(const Font& font, const int xPos, const int yPos) {
Damien Bargiacchi9071db12016-10-28 17:38:22 -0700667 static constexpr char TIME_FORMAT_12[] = "%l:%M";
668 static constexpr char TIME_FORMAT_24[] = "%H:%M";
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700669 static constexpr int TIME_LENGTH = 6;
670
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800671 time_t rawtime;
672 time(&rawtime);
673 struct tm* timeInfo = localtime(&rawtime);
674
675 char timeBuff[TIME_LENGTH];
Damien Bargiacchi9071db12016-10-28 17:38:22 -0700676 const char* timeFormat = mTimeFormat12Hour ? TIME_FORMAT_12 : TIME_FORMAT_24;
677 size_t length = strftime(timeBuff, TIME_LENGTH, timeFormat, timeInfo);
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800678
679 if (length != TIME_LENGTH - 1) {
Wei Wang159a4102018-10-23 23:15:58 -0700680 SLOGE("Couldn't format time; abandoning boot animation clock");
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800681 mClockEnabled = false;
682 return;
683 }
684
Damien Bargiacchi45a76442016-12-05 18:02:18 -0800685 char* out = timeBuff[0] == ' ' ? &timeBuff[1] : &timeBuff[0];
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700686 int x = xPos;
687 int y = yPos;
Damien Bargiacchi45a76442016-12-05 18:02:18 -0800688 drawText(out, font, false, &x, &y);
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800689}
690
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700691bool BootAnimation::parseAnimationDesc(Animation& animation)
Mathias Agopiana8826d62009-10-01 03:10:14 -0700692{
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700693 String8 desString;
694
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700695 if (!readFile(animation.zip, "desc.txt", desString)) {
Narayan Kamathafd31e02013-12-03 13:16:03 +0000696 return false;
697 }
Mathias Agopiana8826d62009-10-01 03:10:14 -0700698 char const* s = desString.string();
699
Mathias Agopiana8826d62009-10-01 03:10:14 -0700700 // Parse the description file
701 for (;;) {
702 const char* endl = strstr(s, "\n");
Yi Kong911ac232019-03-24 01:49:02 -0700703 if (endl == nullptr) break;
Mathias Agopiana8826d62009-10-01 03:10:14 -0700704 String8 line(s, endl - s);
705 const char* l = line.string();
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800706 int fps = 0;
707 int width = 0;
708 int height = 0;
709 int count = 0;
710 int pause = 0;
Narayan Kamathafd31e02013-12-03 13:16:03 +0000711 char path[ANIM_ENTRY_NAME_MAX];
Jesse Hall083b84c2014-09-22 10:51:09 -0700712 char color[7] = "000000"; // default to black if unspecified
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700713 char clockPos1[TEXT_POS_LEN_MAX + 1] = "";
714 char clockPos2[TEXT_POS_LEN_MAX + 1] = "";
Jesse Hall083b84c2014-09-22 10:51:09 -0700715
Kevin Hesterd3782b22012-04-26 10:38:55 -0700716 char pathType;
Mathias Agopiana8826d62009-10-01 03:10:14 -0700717 if (sscanf(l, "%d %d %d", &width, &height, &fps) == 3) {
Wei Wang159a4102018-10-23 23:15:58 -0700718 // SLOGD("> w=%d, h=%d, fps=%d", width, height, fps);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700719 animation.width = width;
720 animation.height = height;
721 animation.fps = fps;
Kyeongkab.Nam35a8a112019-07-17 16:41:48 +0900722 } else if (sscanf(l, " %c %d %d %" STRTO(ANIM_PATH_MAX) "s #%6s %16s %16s",
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700723 &pathType, &count, &pause, path, color, clockPos1, clockPos2) >= 4) {
Wei Wang159a4102018-10-23 23:15:58 -0700724 //SLOGD("> type=%c, count=%d, pause=%d, path=%s, color=%s, clockPos1=%s, clockPos2=%s",
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700725 // pathType, count, pause, path, color, clockPos1, clockPos2);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700726 Animation::Part part;
Kevin Hesterd3782b22012-04-26 10:38:55 -0700727 part.playUntilComplete = pathType == 'c';
Mathias Agopiana8826d62009-10-01 03:10:14 -0700728 part.count = count;
729 part.pause = pause;
730 part.path = path;
Yi Kong911ac232019-03-24 01:49:02 -0700731 part.audioData = nullptr;
732 part.animation = nullptr;
Jesse Hall083b84c2014-09-22 10:51:09 -0700733 if (!parseColor(color, part.backgroundColor)) {
Wei Wang159a4102018-10-23 23:15:58 -0700734 SLOGE("> invalid color '#%s'", color);
Jesse Hall083b84c2014-09-22 10:51:09 -0700735 part.backgroundColor[0] = 0.0f;
736 part.backgroundColor[1] = 0.0f;
737 part.backgroundColor[2] = 0.0f;
738 }
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700739 parsePosition(clockPos1, clockPos2, &part.clockPosX, &part.clockPosY);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700740 animation.parts.add(part);
741 }
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700742 else if (strcmp(l, "$SYSTEM") == 0) {
Wei Wang159a4102018-10-23 23:15:58 -0700743 // SLOGD("> SYSTEM");
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700744 Animation::Part part;
745 part.playUntilComplete = false;
746 part.count = 1;
747 part.pause = 0;
Yi Kong911ac232019-03-24 01:49:02 -0700748 part.audioData = nullptr;
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700749 part.animation = loadAnimation(String8(SYSTEM_BOOTANIMATION_FILE));
Yi Kong911ac232019-03-24 01:49:02 -0700750 if (part.animation != nullptr)
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700751 animation.parts.add(part);
752 }
Mathias Agopiana8826d62009-10-01 03:10:14 -0700753 s = ++endl;
754 }
755
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700756 return true;
757}
758
759bool BootAnimation::preloadZip(Animation& animation)
760{
Mathias Agopiana8826d62009-10-01 03:10:14 -0700761 // read all the data structures
762 const size_t pcount = animation.parts.size();
Yi Kong911ac232019-03-24 01:49:02 -0700763 void *cookie = nullptr;
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400764 ZipFileRO* zip = animation.zip;
765 if (!zip->startIteration(&cookie)) {
Narayan Kamathafd31e02013-12-03 13:16:03 +0000766 return false;
767 }
768
769 ZipEntryRO entry;
770 char name[ANIM_ENTRY_NAME_MAX];
Yi Kong911ac232019-03-24 01:49:02 -0700771 while ((entry = zip->nextEntry(cookie)) != nullptr) {
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400772 const int foundEntryName = zip->getEntryFileName(entry, name, ANIM_ENTRY_NAME_MAX);
Narayan Kamathafd31e02013-12-03 13:16:03 +0000773 if (foundEntryName > ANIM_ENTRY_NAME_MAX || foundEntryName == -1) {
Wei Wang159a4102018-10-23 23:15:58 -0700774 SLOGE("Error fetching entry file name");
Narayan Kamathafd31e02013-12-03 13:16:03 +0000775 continue;
776 }
777
778 const String8 entryName(name);
779 const String8 path(entryName.getPathDir());
780 const String8 leaf(entryName.getPathLeaf());
781 if (leaf.size() > 0) {
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700782 if (entryName == CLOCK_FONT_ZIP_NAME) {
783 FileMap* map = zip->createEntryFileMap(entry);
784 if (map) {
785 animation.clockFont.map = map;
786 }
787 continue;
788 }
789
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400790 for (size_t j = 0; j < pcount; j++) {
Narayan Kamathafd31e02013-12-03 13:16:03 +0000791 if (path == animation.parts[j].path) {
Narayan Kamath4600dd02015-06-16 12:02:57 +0100792 uint16_t method;
Narayan Kamathafd31e02013-12-03 13:16:03 +0000793 // supports only stored png files
Yi Kong911ac232019-03-24 01:49:02 -0700794 if (zip->getEntryInfo(entry, &method, nullptr, nullptr, nullptr, nullptr, nullptr)) {
Narayan Kamathafd31e02013-12-03 13:16:03 +0000795 if (method == ZipFileRO::kCompressStored) {
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400796 FileMap* map = zip->createEntryFileMap(entry);
Narayan Kamathafd31e02013-12-03 13:16:03 +0000797 if (map) {
Narayan Kamathafd31e02013-12-03 13:16:03 +0000798 Animation::Part& part(animation.parts.editItemAt(j));
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700799 if (leaf == "audio.wav") {
800 // a part may have at most one audio file
Geoffrey Pitschd6d9a1d2016-06-08 00:38:58 -0700801 part.audioData = (uint8_t *)map->getDataPtr();
802 part.audioLength = map->getDataLength();
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400803 } else if (leaf == "trim.txt") {
804 part.trimData.setTo((char const*)map->getDataPtr(),
805 map->getDataLength());
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700806 } else {
807 Animation::Frame frame;
808 frame.name = leaf;
809 frame.map = map;
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400810 frame.trimWidth = animation.width;
811 frame.trimHeight = animation.height;
812 frame.trimX = 0;
813 frame.trimY = 0;
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700814 part.frames.add(frame);
815 }
Mathias Agopiana8826d62009-10-01 03:10:14 -0700816 }
Geoffrey Pitschd6d9a1d2016-06-08 00:38:58 -0700817 } else {
Wei Wang159a4102018-10-23 23:15:58 -0700818 SLOGE("bootanimation.zip is compressed; must be only stored");
Mathias Agopiana8826d62009-10-01 03:10:14 -0700819 }
820 }
821 }
822 }
823 }
824 }
825
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400826 // If there is trimData present, override the positioning defaults.
827 for (Animation::Part& part : animation.parts) {
828 const char* trimDataStr = part.trimData.string();
829 for (size_t frameIdx = 0; frameIdx < part.frames.size(); frameIdx++) {
830 const char* endl = strstr(trimDataStr, "\n");
831 // No more trimData for this part.
Yi Kong911ac232019-03-24 01:49:02 -0700832 if (endl == nullptr) {
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400833 break;
834 }
835 String8 line(trimDataStr, endl - trimDataStr);
836 const char* lineStr = line.string();
837 trimDataStr = ++endl;
838 int width = 0, height = 0, x = 0, y = 0;
839 if (sscanf(lineStr, "%dx%d+%d+%d", &width, &height, &x, &y) == 4) {
840 Animation::Frame& frame(part.frames.editItemAt(frameIdx));
841 frame.trimWidth = width;
842 frame.trimHeight = height;
843 frame.trimX = x;
844 frame.trimY = y;
845 } else {
Wei Wang159a4102018-10-23 23:15:58 -0700846 SLOGE("Error parsing trim.txt, line: %s", lineStr);
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400847 break;
848 }
849 }
850 }
851
852 zip->endIteration(cookie);
Narayan Kamathafd31e02013-12-03 13:16:03 +0000853
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700854 return true;
855}
856
857bool BootAnimation::movie()
858{
Huihong Luo50a2f362018-08-11 09:27:57 -0700859 if (mAnimation == nullptr) {
860 mAnimation = loadAnimation(mZipFileName);
861 }
862
863 if (mAnimation == nullptr)
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700864 return false;
865
Huihong Luo50a2f362018-08-11 09:27:57 -0700866 // mCallbacks->init() may get called recursively,
867 // this loop is needed to get the same results
868 for (const Animation::Part& part : mAnimation->parts) {
869 if (part.animation != nullptr) {
870 mCallbacks->init(part.animation->parts);
871 }
872 }
873 mCallbacks->init(mAnimation->parts);
874
Damien Bargiacchi97480862016-03-29 14:55:55 -0700875 bool anyPartHasClock = false;
Huihong Luo50a2f362018-08-11 09:27:57 -0700876 for (size_t i=0; i < mAnimation->parts.size(); i++) {
877 if(validClock(mAnimation->parts[i])) {
Damien Bargiacchi97480862016-03-29 14:55:55 -0700878 anyPartHasClock = true;
879 break;
880 }
881 }
882 if (!anyPartHasClock) {
883 mClockEnabled = false;
884 }
885
Sai Kiran Korwar27167492015-07-07 20:00:06 +0530886 // Check if npot textures are supported
887 mUseNpotTextures = false;
888 String8 gl_extensions;
889 const char* exts = reinterpret_cast<const char*>(glGetString(GL_EXTENSIONS));
890 if (!exts) {
891 glGetError();
892 } else {
893 gl_extensions.setTo(exts);
894 if ((gl_extensions.find("GL_ARB_texture_non_power_of_two") != -1) ||
895 (gl_extensions.find("GL_OES_texture_npot") != -1)) {
896 mUseNpotTextures = true;
897 }
898 }
899
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800900 // Blend required to draw time on top of animation frames.
901 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700902 glShadeModel(GL_FLAT);
903 glDisable(GL_DITHER);
904 glDisable(GL_SCISSOR_TEST);
905 glDisable(GL_BLEND);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700906
907 glBindTexture(GL_TEXTURE_2D, 0);
908 glEnable(GL_TEXTURE_2D);
909 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
910 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
911 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
912 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
913 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
914
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700915 bool clockFontInitialized = false;
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800916 if (mClockEnabled) {
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700917 clockFontInitialized =
Huihong Luo50a2f362018-08-11 09:27:57 -0700918 (initFont(&mAnimation->clockFont, CLOCK_FONT_ASSET) == NO_ERROR);
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700919 mClockEnabled = clockFontInitialized;
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800920 }
921
Damien Bargiacchi97480862016-03-29 14:55:55 -0700922 if (mClockEnabled && !updateIsTimeAccurate()) {
923 mTimeCheckThread = new TimeCheckThread(this);
924 mTimeCheckThread->run("BootAnimation::TimeCheckThread", PRIORITY_NORMAL);
925 }
926
Huihong Luo50a2f362018-08-11 09:27:57 -0700927 playAnimation(*mAnimation);
Damien Bargiacchi97480862016-03-29 14:55:55 -0700928
Geoffrey Pitscha9173532016-07-19 14:56:39 -0400929 if (mTimeCheckThread != nullptr) {
Damien Bargiacchi97480862016-03-29 14:55:55 -0700930 mTimeCheckThread->requestExit();
Geoffrey Pitscha9173532016-07-19 14:56:39 -0400931 mTimeCheckThread = nullptr;
932 }
933
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700934 if (clockFontInitialized) {
Huihong Luo50a2f362018-08-11 09:27:57 -0700935 glDeleteTextures(1, &mAnimation->clockFont.texture.name);
Andriy Naborskyy815e51d2016-03-24 16:43:34 -0700936 }
937
Huihong Luo50a2f362018-08-11 09:27:57 -0700938 releaseAnimation(mAnimation);
939 mAnimation = nullptr;
Damien Bargiacchi1a8a2132018-07-24 15:20:26 -0700940
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700941 return false;
942}
943
944bool BootAnimation::playAnimation(const Animation& animation)
945{
946 const size_t pcount = animation.parts.size();
Mathias Agopiana8826d62009-10-01 03:10:14 -0700947 nsecs_t frameDuration = s2ns(1) / animation.fps;
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400948 const int animationX = (mWidth - animation.width) / 2;
949 const int animationY = (mHeight - animation.height) / 2;
Mathias Agopian9f3020d2009-11-06 16:30:18 -0800950
Wei Wang159a4102018-10-23 23:15:58 -0700951 SLOGD("%sAnimationShownTiming start time: %" PRId64 "ms", mShuttingDown ? "Shutdown" : "Boot",
Keun-young Parkd1794cd2017-04-04 12:21:19 -0700952 elapsedRealtime());
Narayan Kamathafd31e02013-12-03 13:16:03 +0000953 for (size_t i=0 ; i<pcount ; i++) {
Mathias Agopiana8826d62009-10-01 03:10:14 -0700954 const Animation::Part& part(animation.parts[i]);
955 const size_t fcount = part.frames.size();
956 glBindTexture(GL_TEXTURE_2D, 0);
957
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700958 // Handle animation package
Yi Kong911ac232019-03-24 01:49:02 -0700959 if (part.animation != nullptr) {
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700960 playAnimation(*part.animation);
961 if (exitPending())
962 break;
963 continue; //to next part
964 }
965
Mathias Agopiana8826d62009-10-01 03:10:14 -0700966 for (int r=0 ; !part.count || r<part.count ; r++) {
Kevin Hesterd3782b22012-04-26 10:38:55 -0700967 // Exit any non playuntil complete parts immediately
968 if(exitPending() && !part.playUntilComplete)
969 break;
970
Ed Coyne7464ac92017-06-08 12:26:48 -0700971 mCallbacks->playPart(i, part, r);
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700972
Jesse Hall083b84c2014-09-22 10:51:09 -0700973 glClearColor(
974 part.backgroundColor[0],
975 part.backgroundColor[1],
976 part.backgroundColor[2],
977 1.0f);
978
Narayan Kamathafd31e02013-12-03 13:16:03 +0000979 for (size_t j=0 ; j<fcount && (!exitPending() || part.playUntilComplete) ; j++) {
Mathias Agopiana8826d62009-10-01 03:10:14 -0700980 const Animation::Frame& frame(part.frames[j]);
Mathias Agopiandb7dd2a2012-05-12 15:08:21 -0700981 nsecs_t lastFrame = systemTime();
Mathias Agopiana8826d62009-10-01 03:10:14 -0700982
983 if (r > 0) {
984 glBindTexture(GL_TEXTURE_2D, frame.tid);
985 } else {
986 if (part.count != 1) {
987 glGenTextures(1, &frame.tid);
988 glBindTexture(GL_TEXTURE_2D, frame.tid);
989 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
990 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
991 }
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700992 int w, h;
993 initTexture(frame.map, &w, &h);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700994 }
995
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400996 const int xc = animationX + frame.trimX;
997 const int yc = animationY + frame.trimY;
998 Region clearReg(Rect(mWidth, mHeight));
999 clearReg.subtractSelf(Rect(xc, yc, xc+frame.trimWidth, yc+frame.trimHeight));
Mathias Agopian9f3020d2009-11-06 16:30:18 -08001000 if (!clearReg.isEmpty()) {
1001 Region::const_iterator head(clearReg.begin());
1002 Region::const_iterator tail(clearReg.end());
1003 glEnable(GL_SCISSOR_TEST);
1004 while (head != tail) {
Andreas Gampecfedceb2014-09-30 21:48:18 -07001005 const Rect& r2(*head++);
Geoffrey Pitschdd214a72016-06-27 17:14:30 -04001006 glScissor(r2.left, mHeight - r2.bottom, r2.width(), r2.height());
Mathias Agopian9f3020d2009-11-06 16:30:18 -08001007 glClear(GL_COLOR_BUFFER_BIT);
1008 }
1009 glDisable(GL_SCISSOR_TEST);
1010 }
Geoffrey Pitschdd214a72016-06-27 17:14:30 -04001011 // specify the y center as ceiling((mHeight - frame.trimHeight) / 2)
1012 // which is equivalent to mHeight - (yc + frame.trimHeight)
1013 glDrawTexiOES(xc, mHeight - (yc + frame.trimHeight),
1014 0, frame.trimWidth, frame.trimHeight);
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -07001015 if (mClockEnabled && mTimeIsAccurate && validClock(part)) {
1016 drawClock(animation.clockFont, part.clockPosX, part.clockPosY);
Damien Bargiacchia704b7d2016-02-16 16:55:49 -08001017 }
Adrian Roos9ee5dff2018-08-22 20:19:49 +02001018 handleViewport(frameDuration);
Damien Bargiacchia704b7d2016-02-16 16:55:49 -08001019
Mathias Agopiana8826d62009-10-01 03:10:14 -07001020 eglSwapBuffers(mDisplay, mSurface);
1021
1022 nsecs_t now = systemTime();
1023 nsecs_t delay = frameDuration - (now - lastFrame);
Wei Wang159a4102018-10-23 23:15:58 -07001024 //SLOGD("%lld, %lld", ns2ms(now - lastFrame), ns2ms(delay));
Mathias Agopiana8826d62009-10-01 03:10:14 -07001025 lastFrame = now;
Mathias Agopiandb7dd2a2012-05-12 15:08:21 -07001026
1027 if (delay > 0) {
1028 struct timespec spec;
1029 spec.tv_sec = (now + delay) / 1000000000;
1030 spec.tv_nsec = (now + delay) % 1000000000;
1031 int err;
1032 do {
Yi Kong911ac232019-03-24 01:49:02 -07001033 err = clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &spec, nullptr);
Mathias Agopiandb7dd2a2012-05-12 15:08:21 -07001034 } while (err<0 && errno == EINTR);
1035 }
Kevin Hesterd3782b22012-04-26 10:38:55 -07001036
1037 checkExit();
Mathias Agopiana8826d62009-10-01 03:10:14 -07001038 }
Kevin Hesterd3782b22012-04-26 10:38:55 -07001039
Mathias Agopiana8826d62009-10-01 03:10:14 -07001040 usleep(part.pause * ns2us(frameDuration));
Kevin Hesterd3782b22012-04-26 10:38:55 -07001041
1042 // For infinite parts, we've now played them at least once, so perhaps exit
Adrian Roos9ee5dff2018-08-22 20:19:49 +02001043 if(exitPending() && !part.count && mCurrentInset >= mTargetInset)
Kevin Hesterd3782b22012-04-26 10:38:55 -07001044 break;
Mathias Agopiana8826d62009-10-01 03:10:14 -07001045 }
1046
Geoffrey Pitsch2fb30fb2016-07-06 16:16:20 -04001047 }
1048
1049 // Free textures created for looping parts now that the animation is done.
1050 for (const Animation::Part& part : animation.parts) {
Mathias Agopiana8826d62009-10-01 03:10:14 -07001051 if (part.count != 1) {
Geoffrey Pitsch2fb30fb2016-07-06 16:16:20 -04001052 const size_t fcount = part.frames.size();
1053 for (size_t j = 0; j < fcount; j++) {
Mathias Agopiana8826d62009-10-01 03:10:14 -07001054 const Animation::Frame& frame(part.frames[j]);
1055 glDeleteTextures(1, &frame.tid);
1056 }
1057 }
1058 }
Geoffrey Pitschd6d9a1d2016-06-08 00:38:58 -07001059
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001060 return true;
Mathias Agopiana8826d62009-10-01 03:10:14 -07001061}
1062
Adrian Roos9ee5dff2018-08-22 20:19:49 +02001063void BootAnimation::handleViewport(nsecs_t timestep) {
1064 if (mShuttingDown || !mFlingerSurfaceControl || mTargetInset == 0) {
1065 return;
1066 }
1067 if (mTargetInset < 0) {
1068 // Poll the amount for the top display inset. This will return -1 until persistent properties
1069 // have been loaded.
1070 mTargetInset = android::base::GetIntProperty("persist.sys.displayinset.top",
1071 -1 /* default */, -1 /* min */, mHeight / 2 /* max */);
1072 }
1073 if (mTargetInset <= 0) {
1074 return;
1075 }
1076
1077 if (mCurrentInset < mTargetInset) {
1078 // After the device boots, the inset will effectively be cropped away. We animate this here.
1079 float fraction = static_cast<float>(mCurrentInset) / mTargetInset;
1080 int interpolatedInset = (cosf((fraction + 1) * M_PI) / 2.0f + 0.5f) * mTargetInset;
1081
1082 SurfaceComposerClient::Transaction()
1083 .setCrop(mFlingerSurfaceControl, Rect(0, interpolatedInset, mWidth, mHeight))
1084 .apply();
1085 } else {
1086 // At the end of the animation, we switch to the viewport that DisplayManager will apply
1087 // later. This changes the coordinate system, and means we must move the surface up by
1088 // the inset amount.
Adrian Roos9ee5dff2018-08-22 20:19:49 +02001089 Rect layerStackRect(0, 0, mWidth, mHeight - mTargetInset);
1090 Rect displayRect(0, mTargetInset, mWidth, mHeight);
1091
1092 SurfaceComposerClient::Transaction t;
1093 t.setPosition(mFlingerSurfaceControl, 0, -mTargetInset)
1094 .setCrop(mFlingerSurfaceControl, Rect(0, mTargetInset, mWidth, mHeight));
Dominik Laskowski3316a0a2019-01-25 02:56:41 -08001095 t.setDisplayProjection(mDisplayToken, 0 /* orientation */, layerStackRect, displayRect);
Adrian Roos9ee5dff2018-08-22 20:19:49 +02001096 t.apply();
1097
1098 mTargetInset = mCurrentInset = 0;
1099 }
1100
1101 int delta = timestep * mTargetInset / ms2ns(200);
1102 mCurrentInset += delta;
1103}
1104
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001105void BootAnimation::releaseAnimation(Animation* animation) const
1106{
1107 for (Vector<Animation::Part>::iterator it = animation->parts.begin(),
1108 e = animation->parts.end(); it != e; ++it) {
1109 if (it->animation)
1110 releaseAnimation(it->animation);
1111 }
1112 if (animation->zip)
1113 delete animation->zip;
1114 delete animation;
1115}
1116
1117BootAnimation::Animation* BootAnimation::loadAnimation(const String8& fn)
1118{
1119 if (mLoadedFiles.indexOf(fn) >= 0) {
Wei Wang159a4102018-10-23 23:15:58 -07001120 SLOGE("File \"%s\" is already loaded. Cyclic ref is not allowed",
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001121 fn.string());
Yi Kong911ac232019-03-24 01:49:02 -07001122 return nullptr;
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001123 }
1124 ZipFileRO *zip = ZipFileRO::open(fn);
Yi Kong911ac232019-03-24 01:49:02 -07001125 if (zip == nullptr) {
Wei Wang159a4102018-10-23 23:15:58 -07001126 SLOGE("Failed to open animation zip \"%s\": %s",
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001127 fn.string(), strerror(errno));
Yi Kong911ac232019-03-24 01:49:02 -07001128 return nullptr;
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001129 }
1130
1131 Animation *animation = new Animation;
1132 animation->fileName = fn;
1133 animation->zip = zip;
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -07001134 animation->clockFont.map = nullptr;
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001135 mLoadedFiles.add(animation->fileName);
1136
1137 parseAnimationDesc(*animation);
Geoffrey Pitscha91a2d72016-07-12 14:46:19 -04001138 if (!preloadZip(*animation)) {
Yi Kong911ac232019-03-24 01:49:02 -07001139 return nullptr;
Geoffrey Pitscha91a2d72016-07-12 14:46:19 -04001140 }
1141
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001142
1143 mLoadedFiles.remove(fn);
1144 return animation;
1145}
Damien Bargiacchi97480862016-03-29 14:55:55 -07001146
1147bool BootAnimation::updateIsTimeAccurate() {
1148 static constexpr long long MAX_TIME_IN_PAST = 60000LL * 60LL * 24LL * 30LL; // 30 days
1149 static constexpr long long MAX_TIME_IN_FUTURE = 60000LL * 90LL; // 90 minutes
1150
1151 if (mTimeIsAccurate) {
1152 return true;
1153 }
Keun-young Parkb5938422017-03-23 13:46:24 -07001154 if (mShuttingDown) return true;
Damien Bargiacchi97480862016-03-29 14:55:55 -07001155 struct stat statResult;
Damien Bargiacchi9071db12016-10-28 17:38:22 -07001156
1157 if(stat(TIME_FORMAT_12_HOUR_FLAG_FILE_PATH, &statResult) == 0) {
1158 mTimeFormat12Hour = true;
1159 }
1160
Damien Bargiacchi97480862016-03-29 14:55:55 -07001161 if(stat(ACCURATE_TIME_FLAG_FILE_PATH, &statResult) == 0) {
1162 mTimeIsAccurate = true;
1163 return true;
1164 }
1165
1166 FILE* file = fopen(LAST_TIME_CHANGED_FILE_PATH, "r");
Yi Kong911ac232019-03-24 01:49:02 -07001167 if (file != nullptr) {
Damien Bargiacchi97480862016-03-29 14:55:55 -07001168 long long lastChangedTime = 0;
1169 fscanf(file, "%lld", &lastChangedTime);
1170 fclose(file);
1171 if (lastChangedTime > 0) {
1172 struct timespec now;
1173 clock_gettime(CLOCK_REALTIME, &now);
1174 // Match the Java timestamp format
1175 long long rtcNow = (now.tv_sec * 1000LL) + (now.tv_nsec / 1000000LL);
Damien Bargiacchi96762812016-07-12 15:53:40 -07001176 if (ACCURATE_TIME_EPOCH < rtcNow
1177 && lastChangedTime > (rtcNow - MAX_TIME_IN_PAST)
1178 && lastChangedTime < (rtcNow + MAX_TIME_IN_FUTURE)) {
Damien Bargiacchi97480862016-03-29 14:55:55 -07001179 mTimeIsAccurate = true;
1180 }
1181 }
1182 }
1183
1184 return mTimeIsAccurate;
1185}
1186
1187BootAnimation::TimeCheckThread::TimeCheckThread(BootAnimation* bootAnimation) : Thread(false),
1188 mInotifyFd(-1), mSystemWd(-1), mTimeWd(-1), mBootAnimation(bootAnimation) {}
1189
1190BootAnimation::TimeCheckThread::~TimeCheckThread() {
1191 // mInotifyFd may be -1 but that's ok since we're not at risk of attempting to close a valid FD.
1192 close(mInotifyFd);
1193}
1194
1195bool BootAnimation::TimeCheckThread::threadLoop() {
1196 bool shouldLoop = doThreadLoop() && !mBootAnimation->mTimeIsAccurate
1197 && mBootAnimation->mClockEnabled;
1198 if (!shouldLoop) {
1199 close(mInotifyFd);
1200 mInotifyFd = -1;
1201 }
1202 return shouldLoop;
1203}
1204
1205bool BootAnimation::TimeCheckThread::doThreadLoop() {
1206 static constexpr int BUFF_LEN (10 * (sizeof(struct inotify_event) + NAME_MAX + 1));
1207
1208 // Poll instead of doing a blocking read so the Thread can exit if requested.
1209 struct pollfd pfd = { mInotifyFd, POLLIN, 0 };
1210 ssize_t pollResult = poll(&pfd, 1, 1000);
1211
1212 if (pollResult == 0) {
1213 return true;
1214 } else if (pollResult < 0) {
Wei Wang159a4102018-10-23 23:15:58 -07001215 SLOGE("Could not poll inotify events");
Damien Bargiacchi97480862016-03-29 14:55:55 -07001216 return false;
1217 }
1218
1219 char buff[BUFF_LEN] __attribute__ ((aligned(__alignof__(struct inotify_event))));;
1220 ssize_t length = read(mInotifyFd, buff, BUFF_LEN);
1221 if (length == 0) {
1222 return true;
1223 } else if (length < 0) {
Wei Wang159a4102018-10-23 23:15:58 -07001224 SLOGE("Could not read inotify events");
Damien Bargiacchi97480862016-03-29 14:55:55 -07001225 return false;
1226 }
1227
1228 const struct inotify_event *event;
1229 for (char* ptr = buff; ptr < buff + length; ptr += sizeof(struct inotify_event) + event->len) {
1230 event = (const struct inotify_event *) ptr;
1231 if (event->wd == mSystemWd && strcmp(SYSTEM_TIME_DIR_NAME, event->name) == 0) {
1232 addTimeDirWatch();
1233 } else if (event->wd == mTimeWd && (strcmp(LAST_TIME_CHANGED_FILE_NAME, event->name) == 0
1234 || strcmp(ACCURATE_TIME_FLAG_FILE_NAME, event->name) == 0)) {
1235 return !mBootAnimation->updateIsTimeAccurate();
1236 }
1237 }
1238
1239 return true;
1240}
1241
1242void BootAnimation::TimeCheckThread::addTimeDirWatch() {
1243 mTimeWd = inotify_add_watch(mInotifyFd, SYSTEM_TIME_DIR_PATH,
1244 IN_CLOSE_WRITE | IN_MOVED_TO | IN_ATTRIB);
1245 if (mTimeWd > 0) {
1246 // No need to watch for the time directory to be created if it already exists
1247 inotify_rm_watch(mInotifyFd, mSystemWd);
1248 mSystemWd = -1;
1249 }
1250}
1251
1252status_t BootAnimation::TimeCheckThread::readyToRun() {
1253 mInotifyFd = inotify_init();
1254 if (mInotifyFd < 0) {
Wei Wang159a4102018-10-23 23:15:58 -07001255 SLOGE("Could not initialize inotify fd");
Damien Bargiacchi97480862016-03-29 14:55:55 -07001256 return NO_INIT;
1257 }
1258
1259 mSystemWd = inotify_add_watch(mInotifyFd, SYSTEM_DATA_DIR_PATH, IN_CREATE | IN_ATTRIB);
1260 if (mSystemWd < 0) {
1261 close(mInotifyFd);
1262 mInotifyFd = -1;
Wei Wang159a4102018-10-23 23:15:58 -07001263 SLOGE("Could not add watch for %s", SYSTEM_DATA_DIR_PATH);
Damien Bargiacchi97480862016-03-29 14:55:55 -07001264 return NO_INIT;
1265 }
1266
1267 addTimeDirWatch();
1268
1269 if (mBootAnimation->updateIsTimeAccurate()) {
1270 close(mInotifyFd);
1271 mInotifyFd = -1;
1272 return ALREADY_EXISTS;
1273 }
1274
1275 return NO_ERROR;
1276}
1277
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001278// ---------------------------------------------------------------------------
1279
1280}
1281; // namespace android