blob: 520366f518abc1f08284db64c288d99e2d1bedec [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";
Huihong Luod4a217e2020-01-16 15:56:33 -0800109static const char DISPLAYS_PROP_NAME[] = "persist.service.bootanim.displays";
Kyeongkab.Nam35a8a112019-07-17 16:41:48 +0900110static const int ANIM_ENTRY_NAME_MAX = ANIM_PATH_MAX + 1;
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700111static constexpr size_t TEXT_POS_LEN_MAX = 16;
Narayan Kamathafd31e02013-12-03 13:16:03 +0000112
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800113// ---------------------------------------------------------------------------
114
Ed Coyne7464ac92017-06-08 12:26:48 -0700115BootAnimation::BootAnimation(sp<Callbacks> callbacks)
Ed Coyne2c9e94a2017-05-31 10:08:28 -0700116 : Thread(false), mClockEnabled(true), mTimeIsAccurate(false),
Yi Kong911ac232019-03-24 01:49:02 -0700117 mTimeFormat12Hour(false), mTimeCheckThread(nullptr), mCallbacks(callbacks) {
Mathias Agopian627e7b52009-05-21 19:21:59 -0700118 mSession = new SurfaceComposerClient();
Geoffrey Pitsch290c4352016-08-09 14:35:10 -0400119
Keun-young Parkb5938422017-03-23 13:46:24 -0700120 std::string powerCtl = android::base::GetProperty("sys.powerctl", "");
121 if (powerCtl.empty()) {
122 mShuttingDown = false;
123 } else {
124 mShuttingDown = true;
125 }
Huihong Luo50a2f362018-08-11 09:27:57 -0700126 ALOGD("%sAnimationStartTiming start time: %" PRId64 "ms", mShuttingDown ? "Shutdown" : "Boot",
127 elapsedRealtime());
128}
129
130BootAnimation::~BootAnimation() {
131 if (mAnimation != nullptr) {
132 releaseAnimation(mAnimation);
133 mAnimation = nullptr;
134 }
135 ALOGD("%sAnimationStopTiming start time: %" PRId64 "ms", mShuttingDown ? "Shutdown" : "Boot",
136 elapsedRealtime());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800137}
138
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800139void BootAnimation::onFirstRef() {
Mathias Agopianbc726112009-09-23 15:44:05 -0700140 status_t err = mSession->linkToComposerDeath(this);
Wei Wang159a4102018-10-23 23:15:58 -0700141 SLOGE_IF(err, "linkToComposerDeath failed (%s) ", strerror(-err));
Mathias Agopian8434c532009-09-23 18:52:49 -0700142 if (err == NO_ERROR) {
Huihong Luo50a2f362018-08-11 09:27:57 -0700143 // Load the animation content -- this can be slow (eg 200ms)
144 // called before waitForSurfaceFlinger() in main() to avoid wait
145 ALOGD("%sAnimationPreloadTiming start time: %" PRId64 "ms",
146 mShuttingDown ? "Shutdown" : "Boot", elapsedRealtime());
147 preloadAnimation();
148 ALOGD("%sAnimationPreloadStopTiming start time: %" PRId64 "ms",
149 mShuttingDown ? "Shutdown" : "Boot", elapsedRealtime());
Mathias Agopianbc726112009-09-23 15:44:05 -0700150 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800151}
152
Mathias Agopianbc726112009-09-23 15:44:05 -0700153sp<SurfaceComposerClient> BootAnimation::session() const {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800154 return mSession;
155}
156
Narayan Kamathafd31e02013-12-03 13:16:03 +0000157void BootAnimation::binderDied(const wp<IBinder>&)
Mathias Agopianbc726112009-09-23 15:44:05 -0700158{
159 // woah, surfaceflinger died!
Wei Wang159a4102018-10-23 23:15:58 -0700160 SLOGD("SurfaceFlinger died, exiting...");
Mathias Agopianbc726112009-09-23 15:44:05 -0700161
162 // calling requestExit() is not enough here because the Surface code
163 // might be blocked on a condition variable that will never be updated.
164 kill( getpid(), SIGKILL );
165 requestExit();
166}
167
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800168status_t BootAnimation::initTexture(Texture* texture, AssetManager& assets,
169 const char* name) {
170 Asset* asset = assets.open(name, Asset::ACCESS_BUFFER);
Yi Kong911ac232019-03-24 01:49:02 -0700171 if (asset == nullptr)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800172 return NO_INIT;
173 SkBitmap bitmap;
Matt Sarett8898c162016-03-24 16:11:01 -0400174 sk_sp<SkData> data = SkData::MakeWithoutCopy(asset->getBuffer(false),
175 asset->getLength());
176 sk_sp<SkImage> image = SkImage::MakeFromEncoded(data);
177 image->asLegacyBitmap(&bitmap, SkImage::kRO_LegacyBitmapMode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800178 asset->close();
179 delete asset;
180
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800181 const int w = bitmap.width();
182 const int h = bitmap.height();
183 const void* p = bitmap.getPixels();
184
185 GLint crop[4] = { 0, h, w, -h };
186 texture->w = w;
187 texture->h = h;
188
189 glGenTextures(1, &texture->name);
190 glBindTexture(GL_TEXTURE_2D, texture->name);
191
Mike Reed42a1d082014-07-07 18:06:18 -0400192 switch (bitmap.colorType()) {
193 case kAlpha_8_SkColorType:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800194 glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, w, h, 0, GL_ALPHA,
195 GL_UNSIGNED_BYTE, p);
196 break;
Mike Reed42a1d082014-07-07 18:06:18 -0400197 case kARGB_4444_SkColorType:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800198 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA,
199 GL_UNSIGNED_SHORT_4_4_4_4, p);
200 break;
Mike Reed42a1d082014-07-07 18:06:18 -0400201 case kN32_SkColorType:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800202 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA,
203 GL_UNSIGNED_BYTE, p);
204 break;
Mike Reed42a1d082014-07-07 18:06:18 -0400205 case kRGB_565_SkColorType:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800206 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, w, h, 0, GL_RGB,
207 GL_UNSIGNED_SHORT_5_6_5, p);
208 break;
209 default:
210 break;
211 }
212
213 glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_CROP_RECT_OES, crop);
214 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
215 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
216 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
217 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700218
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800219 return NO_ERROR;
220}
221
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700222status_t BootAnimation::initTexture(FileMap* map, int* width, int* height)
Mathias Agopiana8826d62009-10-01 03:10:14 -0700223{
Mathias Agopiana8826d62009-10-01 03:10:14 -0700224 SkBitmap bitmap;
Damien Bargiacchif67b3172016-09-07 20:17:14 -0700225 sk_sp<SkData> data = SkData::MakeWithoutCopy(map->getDataPtr(),
226 map->getDataLength());
Matt Sarett8898c162016-03-24 16:11:01 -0400227 sk_sp<SkImage> image = SkImage::MakeFromEncoded(data);
228 image->asLegacyBitmap(&bitmap, SkImage::kRO_LegacyBitmapMode);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700229
Mykola Kondratenko0c1eeb32014-04-15 09:35:44 +0200230 // FileMap memory is never released until application exit.
231 // Release it now as the texture is already loaded and the memory used for
232 // the packed resource can be released.
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700233 delete map;
Mykola Kondratenko0c1eeb32014-04-15 09:35:44 +0200234
Mathias Agopiana8826d62009-10-01 03:10:14 -0700235 const int w = bitmap.width();
236 const int h = bitmap.height();
237 const void* p = bitmap.getPixels();
238
239 GLint crop[4] = { 0, h, w, -h };
240 int tw = 1 << (31 - __builtin_clz(w));
241 int th = 1 << (31 - __builtin_clz(h));
242 if (tw < w) tw <<= 1;
243 if (th < h) th <<= 1;
244
Mike Reed42a1d082014-07-07 18:06:18 -0400245 switch (bitmap.colorType()) {
246 case kN32_SkColorType:
Sai Kiran Korwar27167492015-07-07 20:00:06 +0530247 if (!mUseNpotTextures && (tw != w || th != h)) {
Mathias Agopiana8826d62009-10-01 03:10:14 -0700248 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, tw, th, 0, GL_RGBA,
Yi Kong911ac232019-03-24 01:49:02 -0700249 GL_UNSIGNED_BYTE, nullptr);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700250 glTexSubImage2D(GL_TEXTURE_2D, 0,
251 0, 0, w, h, GL_RGBA, GL_UNSIGNED_BYTE, p);
252 } else {
Sai Kiran Korwar27167492015-07-07 20:00:06 +0530253 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA,
Mathias Agopiana8826d62009-10-01 03:10:14 -0700254 GL_UNSIGNED_BYTE, p);
255 }
256 break;
257
Mike Reed42a1d082014-07-07 18:06:18 -0400258 case kRGB_565_SkColorType:
Sai Kiran Korwar27167492015-07-07 20:00:06 +0530259 if (!mUseNpotTextures && (tw != w || th != h)) {
Mathias Agopiana8826d62009-10-01 03:10:14 -0700260 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, tw, th, 0, GL_RGB,
Yi Kong911ac232019-03-24 01:49:02 -0700261 GL_UNSIGNED_SHORT_5_6_5, nullptr);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700262 glTexSubImage2D(GL_TEXTURE_2D, 0,
263 0, 0, w, h, GL_RGB, GL_UNSIGNED_SHORT_5_6_5, p);
264 } else {
Sai Kiran Korwar27167492015-07-07 20:00:06 +0530265 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, w, h, 0, GL_RGB,
Mathias Agopiana8826d62009-10-01 03:10:14 -0700266 GL_UNSIGNED_SHORT_5_6_5, p);
267 }
268 break;
269 default:
270 break;
271 }
272
273 glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_CROP_RECT_OES, crop);
274
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700275 *width = w;
276 *height = h;
277
Mathias Agopiana8826d62009-10-01 03:10:14 -0700278 return NO_ERROR;
279}
280
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800281status_t BootAnimation::readyToRun() {
282 mAssets.addDefaultAssets();
283
Dominik Laskowski3316a0a2019-01-25 02:56:41 -0800284 mDisplayToken = SurfaceComposerClient::getInternalDisplayToken();
285 if (mDisplayToken == nullptr)
286 return -1;
287
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800288 DisplayInfo dinfo;
Dominik Laskowski3316a0a2019-01-25 02:56:41 -0800289 status_t status = SurfaceComposerClient::getDisplayInfo(mDisplayToken, &dinfo);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800290 if (status)
291 return -1;
292
293 // create the native surface
Jeff Brown0b722fe2012-08-24 22:40:14 -0700294 sp<SurfaceControl> control = session()->createSurface(String8("BootAnimation"),
295 dinfo.w, dinfo.h, PIXEL_FORMAT_RGB_565);
Mathias Agopian439863f2011-06-28 19:09:31 -0700296
Robert Carre13b58e2017-08-31 14:50:44 -0700297 SurfaceComposerClient::Transaction t;
Huihong Luof97c9e22019-05-29 11:08:01 -0700298
299 // this guest property specifies multi-display IDs to show the boot animation
300 // multiple ids can be set with comma (,) as separator, for example:
Huihong Luod4a217e2020-01-16 15:56:33 -0800301 // setprop persist.boot.animation.displays 19260422155234049,19261083906282754
Huihong Luof97c9e22019-05-29 11:08:01 -0700302 Vector<uint64_t> physicalDisplayIds;
303 char displayValue[PROPERTY_VALUE_MAX] = "";
Huihong Luod4a217e2020-01-16 15:56:33 -0800304 property_get(DISPLAYS_PROP_NAME, displayValue, "");
Huihong Luof97c9e22019-05-29 11:08:01 -0700305 bool isValid = displayValue[0] != '\0';
306 if (isValid) {
307 char *p = displayValue;
308 while (*p) {
309 if (!isdigit(*p) && *p != ',') {
310 isValid = false;
311 break;
312 }
313 p ++;
314 }
315 if (!isValid)
Huihong Luod4a217e2020-01-16 15:56:33 -0800316 SLOGE("Invalid syntax for the value of system prop: %s", DISPLAYS_PROP_NAME);
Huihong Luof97c9e22019-05-29 11:08:01 -0700317 }
318 if (isValid) {
319 std::istringstream stream(displayValue);
320 for (PhysicalDisplayId id; stream >> id; ) {
321 physicalDisplayIds.add(id);
322 if (stream.peek() == ',')
323 stream.ignore();
324 }
325
326 // In the case of multi-display, boot animation shows on the specified displays
327 // in addition to the primary display
328 auto ids = SurfaceComposerClient::getPhysicalDisplayIds();
329 constexpr uint32_t LAYER_STACK = 0;
330 for (auto id : physicalDisplayIds) {
331 if (std::find(ids.begin(), ids.end(), id) != ids.end()) {
332 sp<IBinder> token = SurfaceComposerClient::getPhysicalDisplayToken(id);
333 if (token != nullptr)
334 t.setDisplayLayerStack(token, LAYER_STACK);
335 }
336 }
337 t.setLayerStack(control, LAYER_STACK);
338 }
339
Robert Carre13b58e2017-08-31 14:50:44 -0700340 t.setLayer(control, 0x40000000)
341 .apply();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800342
Mathias Agopian17f638b2009-04-16 20:04:08 -0700343 sp<Surface> s = control->getSurface();
344
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800345 // initialize opengl and egl
Mathias Agopian738b9a42009-08-06 16:41:02 -0700346 const EGLint attribs[] = {
Mathias Agopian1b253b72011-08-15 15:20:22 -0700347 EGL_RED_SIZE, 8,
348 EGL_GREEN_SIZE, 8,
349 EGL_BLUE_SIZE, 8,
Mathias Agopiana8826d62009-10-01 03:10:14 -0700350 EGL_DEPTH_SIZE, 0,
351 EGL_NONE
Mathias Agopian738b9a42009-08-06 16:41:02 -0700352 };
Andreas Gampecfedceb2014-09-30 21:48:18 -0700353 EGLint w, h;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800354 EGLint numConfigs;
355 EGLConfig config;
356 EGLSurface surface;
357 EGLContext context;
Mathias Agopian627e7b52009-05-21 19:21:59 -0700358
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800359 EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
Mathias Agopian627e7b52009-05-21 19:21:59 -0700360
Yi Kong911ac232019-03-24 01:49:02 -0700361 eglInitialize(display, nullptr, nullptr);
Mathias Agopian1b253b72011-08-15 15:20:22 -0700362 eglChooseConfig(display, attribs, &config, 1, &numConfigs);
Yi Kong911ac232019-03-24 01:49:02 -0700363 surface = eglCreateWindowSurface(display, config, s.get(), nullptr);
364 context = eglCreateContext(display, config, nullptr, nullptr);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800365 eglQuerySurface(display, surface, EGL_WIDTH, &w);
366 eglQuerySurface(display, surface, EGL_HEIGHT, &h);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700367
Mathias Agopianabac0102009-07-31 14:47:00 -0700368 if (eglMakeCurrent(display, surface, surface, context) == EGL_FALSE)
369 return NO_INIT;
Mathias Agopiana8826d62009-10-01 03:10:14 -0700370
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800371 mDisplay = display;
372 mContext = context;
373 mSurface = surface;
374 mWidth = w;
375 mHeight = h;
Mathias Agopian17f638b2009-04-16 20:04:08 -0700376 mFlingerSurfaceControl = control;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800377 mFlingerSurface = s;
Adrian Roos9ee5dff2018-08-22 20:19:49 +0200378 mTargetInset = -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800379
Huihong Luo50a2f362018-08-11 09:27:57 -0700380 return NO_ERROR;
381}
382
383bool BootAnimation::preloadAnimation() {
384 findBootAnimationFile();
385 if (!mZipFileName.isEmpty()) {
386 mAnimation = loadAnimation(mZipFileName);
387 return (mAnimation != nullptr);
388 }
389
390 return false;
391}
392
Nikita Ioffe06c986e2020-01-27 17:16:03 +0000393bool BootAnimation::findBootAnimationFileInternal(const std::vector<std::string> &files) {
394 for (const std::string& f : files) {
395 if (access(f.c_str(), R_OK) == 0) {
396 mZipFileName = f.c_str();
397 return true;
398 }
399 }
400 return false;
401}
402
Huihong Luo50a2f362018-08-11 09:27:57 -0700403void BootAnimation::findBootAnimationFile() {
Elliott Hughesc367d482013-10-29 13:12:55 -0700404 // If the device has encryption turned on or is in process
Jason parksbd9a08d2011-01-31 15:04:34 -0600405 // of being encrypted we show the encrypted boot animation.
406 char decrypt[PROPERTY_VALUE_MAX];
407 property_get("vold.decrypt", decrypt, "");
408
Keun-young Parkb5938422017-03-23 13:46:24 -0700409 bool encryptedAnimation = atoi(decrypt) != 0 ||
410 !strcmp("trigger_restart_min_framework", decrypt);
Jason parksbd9a08d2011-01-31 15:04:34 -0600411
Jaekyun Seokc521bb32018-01-30 11:52:47 +0900412 if (!mShuttingDown && encryptedAnimation) {
Nikita Ioffe06c986e2020-01-27 17:16:03 +0000413 static const std::vector<std::string> encryptedBootFiles = {
414 PRODUCT_ENCRYPTED_BOOTANIMATION_FILE, SYSTEM_ENCRYPTED_BOOTANIMATION_FILE,
415 };
416 if (findBootAnimationFileInternal(encryptedBootFiles)) {
417 return;
Jaekyun Seokc521bb32018-01-30 11:52:47 +0900418 }
Jason parksbd9a08d2011-01-31 15:04:34 -0600419 }
Beverly34ffe252019-05-17 11:03:54 -0400420
421 const bool playDarkAnim = android::base::GetIntProperty("ro.boot.theme", 0) == 1;
Nikita Ioffe06c986e2020-01-27 17:16:03 +0000422 static const std::vector<std::string> bootFiles = {
423 APEX_BOOTANIMATION_FILE, playDarkAnim ? PRODUCT_BOOTANIMATION_DARK_FILE : PRODUCT_BOOTANIMATION_FILE,
424 OEM_BOOTANIMATION_FILE, SYSTEM_BOOTANIMATION_FILE
425 };
426 static const std::vector<std::string> shutdownFiles = {
427 PRODUCT_SHUTDOWNANIMATION_FILE, OEM_SHUTDOWNANIMATION_FILE, SYSTEM_SHUTDOWNANIMATION_FILE, ""
428 };
429 static const std::vector<std::string> userspaceRebootFiles = {
430 PRODUCT_USERSPACE_REBOOT_ANIMATION_FILE, OEM_USERSPACE_REBOOT_ANIMATION_FILE,
431 SYSTEM_USERSPACE_REBOOT_ANIMATION_FILE,
432 };
Keun-young Parkb5938422017-03-23 13:46:24 -0700433
Nikita Ioffe06c986e2020-01-27 17:16:03 +0000434 if (android::base::GetBoolProperty("sys.init.userspace_reboot.in_progress", false)) {
435 findBootAnimationFileInternal(userspaceRebootFiles);
436 } else if (mShuttingDown) {
437 findBootAnimationFileInternal(shutdownFiles);
438 } else {
439 findBootAnimationFileInternal(bootFiles);
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700440 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800441}
442
Mathias Agopiana8826d62009-10-01 03:10:14 -0700443bool BootAnimation::threadLoop()
444{
445 bool r;
Narayan Kamathafd31e02013-12-03 13:16:03 +0000446 // We have no bootanimation file, so we use the stock android logo
447 // animation.
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700448 if (mZipFileName.isEmpty()) {
Mathias Agopiana8826d62009-10-01 03:10:14 -0700449 r = android();
450 } else {
451 r = movie();
452 }
453
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800454 eglMakeCurrent(mDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
455 eglDestroyContext(mDisplay, mContext);
456 eglDestroySurface(mDisplay, mSurface);
Mathias Agopian6cf0db22009-04-17 19:36:26 -0700457 mFlingerSurface.clear();
Mathias Agopian17f638b2009-04-16 20:04:08 -0700458 mFlingerSurfaceControl.clear();
Mathias Agopian627e7b52009-05-21 19:21:59 -0700459 eglTerminate(mDisplay);
Sai Kiran Korwar3eee9fb2015-06-16 17:13:35 +0530460 eglReleaseThread();
Mathias Agopian627e7b52009-05-21 19:21:59 -0700461 IPCThreadState::self()->stopProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800462 return r;
463}
464
Mathias Agopiana8826d62009-10-01 03:10:14 -0700465bool BootAnimation::android()
466{
Wei Wang159a4102018-10-23 23:15:58 -0700467 SLOGD("%sAnimationShownTiming start time: %" PRId64 "ms", mShuttingDown ? "Shutdown" : "Boot",
Keun-young Parkd1794cd2017-04-04 12:21:19 -0700468 elapsedRealtime());
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700469 initTexture(&mAndroid[0], mAssets, "images/android-logo-mask.png");
470 initTexture(&mAndroid[1], mAssets, "images/android-logo-shine.png");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800471
Ed Coyne7464ac92017-06-08 12:26:48 -0700472 mCallbacks->init({});
473
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800474 // clear screen
Mathias Agopiana8826d62009-10-01 03:10:14 -0700475 glShadeModel(GL_FLAT);
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700476 glDisable(GL_DITHER);
477 glDisable(GL_SCISSOR_TEST);
Mathias Agopian59f19e42011-05-06 19:22:12 -0700478 glClearColor(0,0,0,1);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800479 glClear(GL_COLOR_BUFFER_BIT);
480 eglSwapBuffers(mDisplay, mSurface);
481
Mathias Agopiana8826d62009-10-01 03:10:14 -0700482 glEnable(GL_TEXTURE_2D);
483 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
484
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700485 const GLint xc = (mWidth - mAndroid[0].w) / 2;
486 const GLint yc = (mHeight - mAndroid[0].h) / 2;
487 const Rect updateRect(xc, yc, xc + mAndroid[0].w, yc + mAndroid[0].h);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800488
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800489 glScissor(updateRect.left, mHeight - updateRect.bottom, updateRect.width(),
490 updateRect.height());
491
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700492 // Blend state
493 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
494 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
495
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800496 const nsecs_t startTime = systemTime();
497 do {
Mathias Agopian13796652009-03-24 22:49:21 -0700498 nsecs_t now = systemTime();
499 double time = now - startTime;
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700500 float t = 4.0f * float(time / us2ns(16667)) / mAndroid[1].w;
501 GLint offset = (1 - (t - floorf(t))) * mAndroid[1].w;
502 GLint x = xc - offset;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800503
Mathias Agopian81668642009-07-28 11:41:30 -0700504 glDisable(GL_SCISSOR_TEST);
505 glClear(GL_COLOR_BUFFER_BIT);
506
507 glEnable(GL_SCISSOR_TEST);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800508 glDisable(GL_BLEND);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800509 glBindTexture(GL_TEXTURE_2D, mAndroid[1].name);
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700510 glDrawTexiOES(x, yc, 0, mAndroid[1].w, mAndroid[1].h);
511 glDrawTexiOES(x + mAndroid[1].w, yc, 0, mAndroid[1].w, mAndroid[1].h);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800512
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700513 glEnable(GL_BLEND);
514 glBindTexture(GL_TEXTURE_2D, mAndroid[0].name);
515 glDrawTexiOES(xc, yc, 0, mAndroid[0].w, mAndroid[0].h);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800516
Mathias Agopian627e7b52009-05-21 19:21:59 -0700517 EGLBoolean res = eglSwapBuffers(mDisplay, mSurface);
518 if (res == EGL_FALSE)
519 break;
520
Mathias Agopian13796652009-03-24 22:49:21 -0700521 // 12fps: don't animate too fast to preserve CPU
522 const nsecs_t sleepTime = 83333 - ns2us(systemTime() - now);
523 if (sleepTime > 0)
Mathias Agopiana8826d62009-10-01 03:10:14 -0700524 usleep(sleepTime);
Kevin Hesterd3782b22012-04-26 10:38:55 -0700525
526 checkExit();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800527 } while (!exitPending());
528
529 glDeleteTextures(1, &mAndroid[0].name);
530 glDeleteTextures(1, &mAndroid[1].name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800531 return false;
532}
533
Kevin Hesterd3782b22012-04-26 10:38:55 -0700534void BootAnimation::checkExit() {
535 // Allow surface flinger to gracefully request shutdown
536 char value[PROPERTY_VALUE_MAX];
537 property_get(EXIT_PROP_NAME, value, "0");
538 int exitnow = atoi(value);
539 if (exitnow) {
540 requestExit();
Ed Coyne7464ac92017-06-08 12:26:48 -0700541 mCallbacks->shutdown();
Kevin Hesterd3782b22012-04-26 10:38:55 -0700542 }
543}
544
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700545bool BootAnimation::validClock(const Animation::Part& part) {
546 return part.clockPosX != TEXT_MISSING_VALUE && part.clockPosY != TEXT_MISSING_VALUE;
547}
548
549bool parseTextCoord(const char* str, int* dest) {
550 if (strcmp("c", str) == 0) {
551 *dest = TEXT_CENTER_VALUE;
552 return true;
553 }
554
555 char* end;
556 int val = (int) strtol(str, &end, 0);
557 if (end == str || *end != '\0' || val == INT_MAX || val == INT_MIN) {
558 return false;
559 }
560 *dest = val;
561 return true;
562}
563
564// Parse two position coordinates. If only string is non-empty, treat it as the y value.
565void parsePosition(const char* str1, const char* str2, int* x, int* y) {
566 bool success = false;
567 if (strlen(str1) == 0) { // No values were specified
568 // success = false
569 } else if (strlen(str2) == 0) { // we have only one value
570 if (parseTextCoord(str1, y)) {
571 *x = TEXT_CENTER_VALUE;
572 success = true;
573 }
574 } else {
575 if (parseTextCoord(str1, x) && parseTextCoord(str2, y)) {
576 success = true;
577 }
578 }
579
580 if (!success) {
581 *x = TEXT_MISSING_VALUE;
582 *y = TEXT_MISSING_VALUE;
583 }
584}
585
Jesse Hall083b84c2014-09-22 10:51:09 -0700586// Parse a color represented as an HTML-style 'RRGGBB' string: each pair of
587// characters in str is a hex number in [0, 255], which are converted to
588// floating point values in the range [0.0, 1.0] and placed in the
589// corresponding elements of color.
590//
591// If the input string isn't valid, parseColor returns false and color is
592// left unchanged.
593static bool parseColor(const char str[7], float color[3]) {
594 float tmpColor[3];
595 for (int i = 0; i < 3; i++) {
596 int val = 0;
597 for (int j = 0; j < 2; j++) {
598 val *= 16;
599 char c = str[2*i + j];
600 if (c >= '0' && c <= '9') val += c - '0';
601 else if (c >= 'A' && c <= 'F') val += (c - 'A') + 10;
602 else if (c >= 'a' && c <= 'f') val += (c - 'a') + 10;
603 else return false;
604 }
605 tmpColor[i] = static_cast<float>(val) / 255.0f;
606 }
607 memcpy(color, tmpColor, sizeof(tmpColor));
608 return true;
609}
610
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700611
612static bool readFile(ZipFileRO* zip, const char* name, String8& outString)
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700613{
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700614 ZipEntryRO entry = zip->findEntryByName(name);
Wei Wang159a4102018-10-23 23:15:58 -0700615 SLOGE_IF(!entry, "couldn't find %s", name);
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700616 if (!entry) {
617 return false;
618 }
619
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700620 FileMap* entryMap = zip->createEntryFileMap(entry);
621 zip->releaseEntry(entry);
Wei Wang159a4102018-10-23 23:15:58 -0700622 SLOGE_IF(!entryMap, "entryMap is null");
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700623 if (!entryMap) {
624 return false;
625 }
626
627 outString.setTo((char const*)entryMap->getDataPtr(), entryMap->getDataLength());
Narayan Kamath688ff4c2015-02-23 15:47:54 +0000628 delete entryMap;
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700629 return true;
630}
631
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700632// The font image should be a 96x2 array of character images. The
633// columns are the printable ASCII characters 0x20 - 0x7f. The
634// top row is regular text; the bottom row is bold.
635status_t BootAnimation::initFont(Font* font, const char* fallback) {
636 status_t status = NO_ERROR;
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800637
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700638 if (font->map != nullptr) {
639 glGenTextures(1, &font->texture.name);
640 glBindTexture(GL_TEXTURE_2D, font->texture.name);
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800641
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700642 status = initTexture(font->map, &font->texture.w, &font->texture.h);
643
644 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
645 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
646 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
647 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
648 } else if (fallback != nullptr) {
649 status = initTexture(&font->texture, mAssets, fallback);
650 } else {
651 return NO_INIT;
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800652 }
653
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700654 if (status == NO_ERROR) {
655 font->char_width = font->texture.w / FONT_NUM_COLS;
656 font->char_height = font->texture.h / FONT_NUM_ROWS / 2; // There are bold and regular rows
657 }
658
659 return status;
660}
661
662void BootAnimation::drawText(const char* str, const Font& font, bool bold, int* x, int* y) {
663 glEnable(GL_BLEND); // Allow us to draw on top of the animation
664 glBindTexture(GL_TEXTURE_2D, font.texture.name);
665
666 const int len = strlen(str);
667 const int strWidth = font.char_width * len;
668
669 if (*x == TEXT_CENTER_VALUE) {
670 *x = (mWidth - strWidth) / 2;
671 } else if (*x < 0) {
672 *x = mWidth + *x - strWidth;
673 }
674 if (*y == TEXT_CENTER_VALUE) {
675 *y = (mHeight - font.char_height) / 2;
676 } else if (*y < 0) {
677 *y = mHeight + *y - font.char_height;
678 }
679
680 int cropRect[4] = { 0, 0, font.char_width, -font.char_height };
681
682 for (int i = 0; i < len; i++) {
683 char c = str[i];
684
685 if (c < FONT_BEGIN_CHAR || c > FONT_END_CHAR) {
686 c = '?';
687 }
688
689 // Crop the texture to only the pixels in the current glyph
690 const int charPos = (c - FONT_BEGIN_CHAR); // Position in the list of valid characters
691 const int row = charPos / FONT_NUM_COLS;
692 const int col = charPos % FONT_NUM_COLS;
693 cropRect[0] = col * font.char_width; // Left of column
694 cropRect[1] = row * font.char_height * 2; // Top of row
695 // Move down to bottom of regular (one char_heigh) or bold (two char_heigh) line
696 cropRect[1] += bold ? 2 * font.char_height : font.char_height;
697 glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_CROP_RECT_OES, cropRect);
698
699 glDrawTexiOES(*x, *y, 0, font.char_width, font.char_height);
700
701 *x += font.char_width;
702 }
703
704 glDisable(GL_BLEND); // Return to the animation's default behaviour
705 glBindTexture(GL_TEXTURE_2D, 0);
706}
707
Damien Bargiacchi9071db12016-10-28 17:38:22 -0700708// We render 12 or 24 hour time.
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700709void BootAnimation::drawClock(const Font& font, const int xPos, const int yPos) {
Damien Bargiacchi9071db12016-10-28 17:38:22 -0700710 static constexpr char TIME_FORMAT_12[] = "%l:%M";
711 static constexpr char TIME_FORMAT_24[] = "%H:%M";
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700712 static constexpr int TIME_LENGTH = 6;
713
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800714 time_t rawtime;
715 time(&rawtime);
716 struct tm* timeInfo = localtime(&rawtime);
717
718 char timeBuff[TIME_LENGTH];
Damien Bargiacchi9071db12016-10-28 17:38:22 -0700719 const char* timeFormat = mTimeFormat12Hour ? TIME_FORMAT_12 : TIME_FORMAT_24;
720 size_t length = strftime(timeBuff, TIME_LENGTH, timeFormat, timeInfo);
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800721
722 if (length != TIME_LENGTH - 1) {
Wei Wang159a4102018-10-23 23:15:58 -0700723 SLOGE("Couldn't format time; abandoning boot animation clock");
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800724 mClockEnabled = false;
725 return;
726 }
727
Damien Bargiacchi45a76442016-12-05 18:02:18 -0800728 char* out = timeBuff[0] == ' ' ? &timeBuff[1] : &timeBuff[0];
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700729 int x = xPos;
730 int y = yPos;
Damien Bargiacchi45a76442016-12-05 18:02:18 -0800731 drawText(out, font, false, &x, &y);
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800732}
733
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700734bool BootAnimation::parseAnimationDesc(Animation& animation)
Mathias Agopiana8826d62009-10-01 03:10:14 -0700735{
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700736 String8 desString;
737
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700738 if (!readFile(animation.zip, "desc.txt", desString)) {
Narayan Kamathafd31e02013-12-03 13:16:03 +0000739 return false;
740 }
Mathias Agopiana8826d62009-10-01 03:10:14 -0700741 char const* s = desString.string();
742
Mathias Agopiana8826d62009-10-01 03:10:14 -0700743 // Parse the description file
744 for (;;) {
745 const char* endl = strstr(s, "\n");
Yi Kong911ac232019-03-24 01:49:02 -0700746 if (endl == nullptr) break;
Mathias Agopiana8826d62009-10-01 03:10:14 -0700747 String8 line(s, endl - s);
748 const char* l = line.string();
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800749 int fps = 0;
750 int width = 0;
751 int height = 0;
752 int count = 0;
753 int pause = 0;
Narayan Kamathafd31e02013-12-03 13:16:03 +0000754 char path[ANIM_ENTRY_NAME_MAX];
Jesse Hall083b84c2014-09-22 10:51:09 -0700755 char color[7] = "000000"; // default to black if unspecified
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700756 char clockPos1[TEXT_POS_LEN_MAX + 1] = "";
757 char clockPos2[TEXT_POS_LEN_MAX + 1] = "";
Jesse Hall083b84c2014-09-22 10:51:09 -0700758
Kevin Hesterd3782b22012-04-26 10:38:55 -0700759 char pathType;
Mathias Agopiana8826d62009-10-01 03:10:14 -0700760 if (sscanf(l, "%d %d %d", &width, &height, &fps) == 3) {
Wei Wang159a4102018-10-23 23:15:58 -0700761 // SLOGD("> w=%d, h=%d, fps=%d", width, height, fps);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700762 animation.width = width;
763 animation.height = height;
764 animation.fps = fps;
Kyeongkab.Nam35a8a112019-07-17 16:41:48 +0900765 } else if (sscanf(l, " %c %d %d %" STRTO(ANIM_PATH_MAX) "s #%6s %16s %16s",
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700766 &pathType, &count, &pause, path, color, clockPos1, clockPos2) >= 4) {
Wei Wang159a4102018-10-23 23:15:58 -0700767 //SLOGD("> type=%c, count=%d, pause=%d, path=%s, color=%s, clockPos1=%s, clockPos2=%s",
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700768 // pathType, count, pause, path, color, clockPos1, clockPos2);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700769 Animation::Part part;
Kevin Hesterd3782b22012-04-26 10:38:55 -0700770 part.playUntilComplete = pathType == 'c';
Mathias Agopiana8826d62009-10-01 03:10:14 -0700771 part.count = count;
772 part.pause = pause;
773 part.path = path;
Yi Kong911ac232019-03-24 01:49:02 -0700774 part.audioData = nullptr;
775 part.animation = nullptr;
Jesse Hall083b84c2014-09-22 10:51:09 -0700776 if (!parseColor(color, part.backgroundColor)) {
Wei Wang159a4102018-10-23 23:15:58 -0700777 SLOGE("> invalid color '#%s'", color);
Jesse Hall083b84c2014-09-22 10:51:09 -0700778 part.backgroundColor[0] = 0.0f;
779 part.backgroundColor[1] = 0.0f;
780 part.backgroundColor[2] = 0.0f;
781 }
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700782 parsePosition(clockPos1, clockPos2, &part.clockPosX, &part.clockPosY);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700783 animation.parts.add(part);
784 }
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700785 else if (strcmp(l, "$SYSTEM") == 0) {
Wei Wang159a4102018-10-23 23:15:58 -0700786 // SLOGD("> SYSTEM");
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700787 Animation::Part part;
788 part.playUntilComplete = false;
789 part.count = 1;
790 part.pause = 0;
Yi Kong911ac232019-03-24 01:49:02 -0700791 part.audioData = nullptr;
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700792 part.animation = loadAnimation(String8(SYSTEM_BOOTANIMATION_FILE));
Yi Kong911ac232019-03-24 01:49:02 -0700793 if (part.animation != nullptr)
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700794 animation.parts.add(part);
795 }
Mathias Agopiana8826d62009-10-01 03:10:14 -0700796 s = ++endl;
797 }
798
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700799 return true;
800}
801
802bool BootAnimation::preloadZip(Animation& animation)
803{
Mathias Agopiana8826d62009-10-01 03:10:14 -0700804 // read all the data structures
805 const size_t pcount = animation.parts.size();
Yi Kong911ac232019-03-24 01:49:02 -0700806 void *cookie = nullptr;
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400807 ZipFileRO* zip = animation.zip;
808 if (!zip->startIteration(&cookie)) {
Narayan Kamathafd31e02013-12-03 13:16:03 +0000809 return false;
810 }
811
812 ZipEntryRO entry;
813 char name[ANIM_ENTRY_NAME_MAX];
Yi Kong911ac232019-03-24 01:49:02 -0700814 while ((entry = zip->nextEntry(cookie)) != nullptr) {
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400815 const int foundEntryName = zip->getEntryFileName(entry, name, ANIM_ENTRY_NAME_MAX);
Narayan Kamathafd31e02013-12-03 13:16:03 +0000816 if (foundEntryName > ANIM_ENTRY_NAME_MAX || foundEntryName == -1) {
Wei Wang159a4102018-10-23 23:15:58 -0700817 SLOGE("Error fetching entry file name");
Narayan Kamathafd31e02013-12-03 13:16:03 +0000818 continue;
819 }
820
821 const String8 entryName(name);
822 const String8 path(entryName.getPathDir());
823 const String8 leaf(entryName.getPathLeaf());
824 if (leaf.size() > 0) {
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700825 if (entryName == CLOCK_FONT_ZIP_NAME) {
826 FileMap* map = zip->createEntryFileMap(entry);
827 if (map) {
828 animation.clockFont.map = map;
829 }
830 continue;
831 }
832
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400833 for (size_t j = 0; j < pcount; j++) {
Narayan Kamathafd31e02013-12-03 13:16:03 +0000834 if (path == animation.parts[j].path) {
Narayan Kamath4600dd02015-06-16 12:02:57 +0100835 uint16_t method;
Narayan Kamathafd31e02013-12-03 13:16:03 +0000836 // supports only stored png files
Yi Kong911ac232019-03-24 01:49:02 -0700837 if (zip->getEntryInfo(entry, &method, nullptr, nullptr, nullptr, nullptr, nullptr)) {
Narayan Kamathafd31e02013-12-03 13:16:03 +0000838 if (method == ZipFileRO::kCompressStored) {
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400839 FileMap* map = zip->createEntryFileMap(entry);
Narayan Kamathafd31e02013-12-03 13:16:03 +0000840 if (map) {
Narayan Kamathafd31e02013-12-03 13:16:03 +0000841 Animation::Part& part(animation.parts.editItemAt(j));
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700842 if (leaf == "audio.wav") {
843 // a part may have at most one audio file
Geoffrey Pitschd6d9a1d2016-06-08 00:38:58 -0700844 part.audioData = (uint8_t *)map->getDataPtr();
845 part.audioLength = map->getDataLength();
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400846 } else if (leaf == "trim.txt") {
847 part.trimData.setTo((char const*)map->getDataPtr(),
848 map->getDataLength());
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700849 } else {
850 Animation::Frame frame;
851 frame.name = leaf;
852 frame.map = map;
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400853 frame.trimWidth = animation.width;
854 frame.trimHeight = animation.height;
855 frame.trimX = 0;
856 frame.trimY = 0;
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700857 part.frames.add(frame);
858 }
Mathias Agopiana8826d62009-10-01 03:10:14 -0700859 }
Geoffrey Pitschd6d9a1d2016-06-08 00:38:58 -0700860 } else {
Wei Wang159a4102018-10-23 23:15:58 -0700861 SLOGE("bootanimation.zip is compressed; must be only stored");
Mathias Agopiana8826d62009-10-01 03:10:14 -0700862 }
863 }
864 }
865 }
866 }
867 }
868
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400869 // If there is trimData present, override the positioning defaults.
870 for (Animation::Part& part : animation.parts) {
871 const char* trimDataStr = part.trimData.string();
872 for (size_t frameIdx = 0; frameIdx < part.frames.size(); frameIdx++) {
873 const char* endl = strstr(trimDataStr, "\n");
874 // No more trimData for this part.
Yi Kong911ac232019-03-24 01:49:02 -0700875 if (endl == nullptr) {
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400876 break;
877 }
878 String8 line(trimDataStr, endl - trimDataStr);
879 const char* lineStr = line.string();
880 trimDataStr = ++endl;
881 int width = 0, height = 0, x = 0, y = 0;
882 if (sscanf(lineStr, "%dx%d+%d+%d", &width, &height, &x, &y) == 4) {
883 Animation::Frame& frame(part.frames.editItemAt(frameIdx));
884 frame.trimWidth = width;
885 frame.trimHeight = height;
886 frame.trimX = x;
887 frame.trimY = y;
888 } else {
Wei Wang159a4102018-10-23 23:15:58 -0700889 SLOGE("Error parsing trim.txt, line: %s", lineStr);
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400890 break;
891 }
892 }
893 }
894
895 zip->endIteration(cookie);
Narayan Kamathafd31e02013-12-03 13:16:03 +0000896
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700897 return true;
898}
899
900bool BootAnimation::movie()
901{
Huihong Luo50a2f362018-08-11 09:27:57 -0700902 if (mAnimation == nullptr) {
903 mAnimation = loadAnimation(mZipFileName);
904 }
905
906 if (mAnimation == nullptr)
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700907 return false;
908
Huihong Luo50a2f362018-08-11 09:27:57 -0700909 // mCallbacks->init() may get called recursively,
910 // this loop is needed to get the same results
911 for (const Animation::Part& part : mAnimation->parts) {
912 if (part.animation != nullptr) {
913 mCallbacks->init(part.animation->parts);
914 }
915 }
916 mCallbacks->init(mAnimation->parts);
917
Damien Bargiacchi97480862016-03-29 14:55:55 -0700918 bool anyPartHasClock = false;
Huihong Luo50a2f362018-08-11 09:27:57 -0700919 for (size_t i=0; i < mAnimation->parts.size(); i++) {
920 if(validClock(mAnimation->parts[i])) {
Damien Bargiacchi97480862016-03-29 14:55:55 -0700921 anyPartHasClock = true;
922 break;
923 }
924 }
925 if (!anyPartHasClock) {
926 mClockEnabled = false;
927 }
928
Sai Kiran Korwar27167492015-07-07 20:00:06 +0530929 // Check if npot textures are supported
930 mUseNpotTextures = false;
931 String8 gl_extensions;
932 const char* exts = reinterpret_cast<const char*>(glGetString(GL_EXTENSIONS));
933 if (!exts) {
934 glGetError();
935 } else {
936 gl_extensions.setTo(exts);
937 if ((gl_extensions.find("GL_ARB_texture_non_power_of_two") != -1) ||
938 (gl_extensions.find("GL_OES_texture_npot") != -1)) {
939 mUseNpotTextures = true;
940 }
941 }
942
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800943 // Blend required to draw time on top of animation frames.
944 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700945 glShadeModel(GL_FLAT);
946 glDisable(GL_DITHER);
947 glDisable(GL_SCISSOR_TEST);
948 glDisable(GL_BLEND);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700949
950 glBindTexture(GL_TEXTURE_2D, 0);
951 glEnable(GL_TEXTURE_2D);
952 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
953 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
954 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
955 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
956 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
957
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700958 bool clockFontInitialized = false;
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800959 if (mClockEnabled) {
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700960 clockFontInitialized =
Huihong Luo50a2f362018-08-11 09:27:57 -0700961 (initFont(&mAnimation->clockFont, CLOCK_FONT_ASSET) == NO_ERROR);
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700962 mClockEnabled = clockFontInitialized;
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800963 }
964
Damien Bargiacchi97480862016-03-29 14:55:55 -0700965 if (mClockEnabled && !updateIsTimeAccurate()) {
966 mTimeCheckThread = new TimeCheckThread(this);
967 mTimeCheckThread->run("BootAnimation::TimeCheckThread", PRIORITY_NORMAL);
968 }
969
Huihong Luo50a2f362018-08-11 09:27:57 -0700970 playAnimation(*mAnimation);
Damien Bargiacchi97480862016-03-29 14:55:55 -0700971
Geoffrey Pitscha9173532016-07-19 14:56:39 -0400972 if (mTimeCheckThread != nullptr) {
Damien Bargiacchi97480862016-03-29 14:55:55 -0700973 mTimeCheckThread->requestExit();
Geoffrey Pitscha9173532016-07-19 14:56:39 -0400974 mTimeCheckThread = nullptr;
975 }
976
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700977 if (clockFontInitialized) {
Huihong Luo50a2f362018-08-11 09:27:57 -0700978 glDeleteTextures(1, &mAnimation->clockFont.texture.name);
Andriy Naborskyy815e51d2016-03-24 16:43:34 -0700979 }
980
Huihong Luo50a2f362018-08-11 09:27:57 -0700981 releaseAnimation(mAnimation);
982 mAnimation = nullptr;
Damien Bargiacchi1a8a2132018-07-24 15:20:26 -0700983
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700984 return false;
985}
986
987bool BootAnimation::playAnimation(const Animation& animation)
988{
989 const size_t pcount = animation.parts.size();
Mathias Agopiana8826d62009-10-01 03:10:14 -0700990 nsecs_t frameDuration = s2ns(1) / animation.fps;
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400991 const int animationX = (mWidth - animation.width) / 2;
992 const int animationY = (mHeight - animation.height) / 2;
Mathias Agopian9f3020d2009-11-06 16:30:18 -0800993
Wei Wang159a4102018-10-23 23:15:58 -0700994 SLOGD("%sAnimationShownTiming start time: %" PRId64 "ms", mShuttingDown ? "Shutdown" : "Boot",
Keun-young Parkd1794cd2017-04-04 12:21:19 -0700995 elapsedRealtime());
Narayan Kamathafd31e02013-12-03 13:16:03 +0000996 for (size_t i=0 ; i<pcount ; i++) {
Mathias Agopiana8826d62009-10-01 03:10:14 -0700997 const Animation::Part& part(animation.parts[i]);
998 const size_t fcount = part.frames.size();
999 glBindTexture(GL_TEXTURE_2D, 0);
1000
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001001 // Handle animation package
Yi Kong911ac232019-03-24 01:49:02 -07001002 if (part.animation != nullptr) {
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001003 playAnimation(*part.animation);
1004 if (exitPending())
1005 break;
1006 continue; //to next part
1007 }
1008
Mathias Agopiana8826d62009-10-01 03:10:14 -07001009 for (int r=0 ; !part.count || r<part.count ; r++) {
Kevin Hesterd3782b22012-04-26 10:38:55 -07001010 // Exit any non playuntil complete parts immediately
1011 if(exitPending() && !part.playUntilComplete)
1012 break;
1013
Ed Coyne7464ac92017-06-08 12:26:48 -07001014 mCallbacks->playPart(i, part, r);
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -07001015
Jesse Hall083b84c2014-09-22 10:51:09 -07001016 glClearColor(
1017 part.backgroundColor[0],
1018 part.backgroundColor[1],
1019 part.backgroundColor[2],
1020 1.0f);
1021
Narayan Kamathafd31e02013-12-03 13:16:03 +00001022 for (size_t j=0 ; j<fcount && (!exitPending() || part.playUntilComplete) ; j++) {
Mathias Agopiana8826d62009-10-01 03:10:14 -07001023 const Animation::Frame& frame(part.frames[j]);
Mathias Agopiandb7dd2a2012-05-12 15:08:21 -07001024 nsecs_t lastFrame = systemTime();
Mathias Agopiana8826d62009-10-01 03:10:14 -07001025
1026 if (r > 0) {
1027 glBindTexture(GL_TEXTURE_2D, frame.tid);
1028 } else {
1029 if (part.count != 1) {
1030 glGenTextures(1, &frame.tid);
1031 glBindTexture(GL_TEXTURE_2D, frame.tid);
1032 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
1033 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
1034 }
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -07001035 int w, h;
1036 initTexture(frame.map, &w, &h);
Mathias Agopiana8826d62009-10-01 03:10:14 -07001037 }
1038
Geoffrey Pitschdd214a72016-06-27 17:14:30 -04001039 const int xc = animationX + frame.trimX;
1040 const int yc = animationY + frame.trimY;
1041 Region clearReg(Rect(mWidth, mHeight));
1042 clearReg.subtractSelf(Rect(xc, yc, xc+frame.trimWidth, yc+frame.trimHeight));
Mathias Agopian9f3020d2009-11-06 16:30:18 -08001043 if (!clearReg.isEmpty()) {
1044 Region::const_iterator head(clearReg.begin());
1045 Region::const_iterator tail(clearReg.end());
1046 glEnable(GL_SCISSOR_TEST);
1047 while (head != tail) {
Andreas Gampecfedceb2014-09-30 21:48:18 -07001048 const Rect& r2(*head++);
Geoffrey Pitschdd214a72016-06-27 17:14:30 -04001049 glScissor(r2.left, mHeight - r2.bottom, r2.width(), r2.height());
Mathias Agopian9f3020d2009-11-06 16:30:18 -08001050 glClear(GL_COLOR_BUFFER_BIT);
1051 }
1052 glDisable(GL_SCISSOR_TEST);
1053 }
Geoffrey Pitschdd214a72016-06-27 17:14:30 -04001054 // specify the y center as ceiling((mHeight - frame.trimHeight) / 2)
1055 // which is equivalent to mHeight - (yc + frame.trimHeight)
1056 glDrawTexiOES(xc, mHeight - (yc + frame.trimHeight),
1057 0, frame.trimWidth, frame.trimHeight);
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -07001058 if (mClockEnabled && mTimeIsAccurate && validClock(part)) {
1059 drawClock(animation.clockFont, part.clockPosX, part.clockPosY);
Damien Bargiacchia704b7d2016-02-16 16:55:49 -08001060 }
Adrian Roos9ee5dff2018-08-22 20:19:49 +02001061 handleViewport(frameDuration);
Damien Bargiacchia704b7d2016-02-16 16:55:49 -08001062
Mathias Agopiana8826d62009-10-01 03:10:14 -07001063 eglSwapBuffers(mDisplay, mSurface);
1064
1065 nsecs_t now = systemTime();
1066 nsecs_t delay = frameDuration - (now - lastFrame);
Wei Wang159a4102018-10-23 23:15:58 -07001067 //SLOGD("%lld, %lld", ns2ms(now - lastFrame), ns2ms(delay));
Mathias Agopiana8826d62009-10-01 03:10:14 -07001068 lastFrame = now;
Mathias Agopiandb7dd2a2012-05-12 15:08:21 -07001069
1070 if (delay > 0) {
1071 struct timespec spec;
1072 spec.tv_sec = (now + delay) / 1000000000;
1073 spec.tv_nsec = (now + delay) % 1000000000;
1074 int err;
1075 do {
Yi Kong911ac232019-03-24 01:49:02 -07001076 err = clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &spec, nullptr);
Mathias Agopiandb7dd2a2012-05-12 15:08:21 -07001077 } while (err<0 && errno == EINTR);
1078 }
Kevin Hesterd3782b22012-04-26 10:38:55 -07001079
1080 checkExit();
Mathias Agopiana8826d62009-10-01 03:10:14 -07001081 }
Kevin Hesterd3782b22012-04-26 10:38:55 -07001082
Mathias Agopiana8826d62009-10-01 03:10:14 -07001083 usleep(part.pause * ns2us(frameDuration));
Kevin Hesterd3782b22012-04-26 10:38:55 -07001084
1085 // For infinite parts, we've now played them at least once, so perhaps exit
Adrian Roos9ee5dff2018-08-22 20:19:49 +02001086 if(exitPending() && !part.count && mCurrentInset >= mTargetInset)
Kevin Hesterd3782b22012-04-26 10:38:55 -07001087 break;
Mathias Agopiana8826d62009-10-01 03:10:14 -07001088 }
1089
Geoffrey Pitsch2fb30fb2016-07-06 16:16:20 -04001090 }
1091
1092 // Free textures created for looping parts now that the animation is done.
1093 for (const Animation::Part& part : animation.parts) {
Mathias Agopiana8826d62009-10-01 03:10:14 -07001094 if (part.count != 1) {
Geoffrey Pitsch2fb30fb2016-07-06 16:16:20 -04001095 const size_t fcount = part.frames.size();
1096 for (size_t j = 0; j < fcount; j++) {
Mathias Agopiana8826d62009-10-01 03:10:14 -07001097 const Animation::Frame& frame(part.frames[j]);
1098 glDeleteTextures(1, &frame.tid);
1099 }
1100 }
1101 }
Geoffrey Pitschd6d9a1d2016-06-08 00:38:58 -07001102
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001103 return true;
Mathias Agopiana8826d62009-10-01 03:10:14 -07001104}
1105
Adrian Roos9ee5dff2018-08-22 20:19:49 +02001106void BootAnimation::handleViewport(nsecs_t timestep) {
1107 if (mShuttingDown || !mFlingerSurfaceControl || mTargetInset == 0) {
1108 return;
1109 }
1110 if (mTargetInset < 0) {
1111 // Poll the amount for the top display inset. This will return -1 until persistent properties
1112 // have been loaded.
1113 mTargetInset = android::base::GetIntProperty("persist.sys.displayinset.top",
1114 -1 /* default */, -1 /* min */, mHeight / 2 /* max */);
1115 }
1116 if (mTargetInset <= 0) {
1117 return;
1118 }
1119
1120 if (mCurrentInset < mTargetInset) {
1121 // After the device boots, the inset will effectively be cropped away. We animate this here.
1122 float fraction = static_cast<float>(mCurrentInset) / mTargetInset;
1123 int interpolatedInset = (cosf((fraction + 1) * M_PI) / 2.0f + 0.5f) * mTargetInset;
1124
1125 SurfaceComposerClient::Transaction()
1126 .setCrop(mFlingerSurfaceControl, Rect(0, interpolatedInset, mWidth, mHeight))
1127 .apply();
1128 } else {
1129 // At the end of the animation, we switch to the viewport that DisplayManager will apply
1130 // later. This changes the coordinate system, and means we must move the surface up by
1131 // the inset amount.
Adrian Roos9ee5dff2018-08-22 20:19:49 +02001132 Rect layerStackRect(0, 0, mWidth, mHeight - mTargetInset);
1133 Rect displayRect(0, mTargetInset, mWidth, mHeight);
1134
1135 SurfaceComposerClient::Transaction t;
1136 t.setPosition(mFlingerSurfaceControl, 0, -mTargetInset)
1137 .setCrop(mFlingerSurfaceControl, Rect(0, mTargetInset, mWidth, mHeight));
Dominik Laskowski2a3d9aa2019-11-18 20:26:39 -08001138 t.setDisplayProjection(mDisplayToken, ui::ROTATION_0, layerStackRect, displayRect);
Adrian Roos9ee5dff2018-08-22 20:19:49 +02001139 t.apply();
1140
1141 mTargetInset = mCurrentInset = 0;
1142 }
1143
1144 int delta = timestep * mTargetInset / ms2ns(200);
1145 mCurrentInset += delta;
1146}
1147
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001148void BootAnimation::releaseAnimation(Animation* animation) const
1149{
1150 for (Vector<Animation::Part>::iterator it = animation->parts.begin(),
1151 e = animation->parts.end(); it != e; ++it) {
1152 if (it->animation)
1153 releaseAnimation(it->animation);
1154 }
1155 if (animation->zip)
1156 delete animation->zip;
1157 delete animation;
1158}
1159
1160BootAnimation::Animation* BootAnimation::loadAnimation(const String8& fn)
1161{
1162 if (mLoadedFiles.indexOf(fn) >= 0) {
Wei Wang159a4102018-10-23 23:15:58 -07001163 SLOGE("File \"%s\" is already loaded. Cyclic ref is not allowed",
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001164 fn.string());
Yi Kong911ac232019-03-24 01:49:02 -07001165 return nullptr;
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001166 }
1167 ZipFileRO *zip = ZipFileRO::open(fn);
Yi Kong911ac232019-03-24 01:49:02 -07001168 if (zip == nullptr) {
Wei Wang159a4102018-10-23 23:15:58 -07001169 SLOGE("Failed to open animation zip \"%s\": %s",
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001170 fn.string(), strerror(errno));
Yi Kong911ac232019-03-24 01:49:02 -07001171 return nullptr;
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001172 }
1173
1174 Animation *animation = new Animation;
1175 animation->fileName = fn;
1176 animation->zip = zip;
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -07001177 animation->clockFont.map = nullptr;
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001178 mLoadedFiles.add(animation->fileName);
1179
1180 parseAnimationDesc(*animation);
Geoffrey Pitscha91a2d72016-07-12 14:46:19 -04001181 if (!preloadZip(*animation)) {
Greg Kaiser7426ec82019-09-11 14:34:27 -07001182 releaseAnimation(animation);
Yi Kong911ac232019-03-24 01:49:02 -07001183 return nullptr;
Geoffrey Pitscha91a2d72016-07-12 14:46:19 -04001184 }
1185
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001186 mLoadedFiles.remove(fn);
1187 return animation;
1188}
Damien Bargiacchi97480862016-03-29 14:55:55 -07001189
1190bool BootAnimation::updateIsTimeAccurate() {
1191 static constexpr long long MAX_TIME_IN_PAST = 60000LL * 60LL * 24LL * 30LL; // 30 days
1192 static constexpr long long MAX_TIME_IN_FUTURE = 60000LL * 90LL; // 90 minutes
1193
1194 if (mTimeIsAccurate) {
1195 return true;
1196 }
Keun-young Parkb5938422017-03-23 13:46:24 -07001197 if (mShuttingDown) return true;
Damien Bargiacchi97480862016-03-29 14:55:55 -07001198 struct stat statResult;
Damien Bargiacchi9071db12016-10-28 17:38:22 -07001199
1200 if(stat(TIME_FORMAT_12_HOUR_FLAG_FILE_PATH, &statResult) == 0) {
1201 mTimeFormat12Hour = true;
1202 }
1203
Damien Bargiacchi97480862016-03-29 14:55:55 -07001204 if(stat(ACCURATE_TIME_FLAG_FILE_PATH, &statResult) == 0) {
1205 mTimeIsAccurate = true;
1206 return true;
1207 }
1208
1209 FILE* file = fopen(LAST_TIME_CHANGED_FILE_PATH, "r");
Yi Kong911ac232019-03-24 01:49:02 -07001210 if (file != nullptr) {
Damien Bargiacchi97480862016-03-29 14:55:55 -07001211 long long lastChangedTime = 0;
1212 fscanf(file, "%lld", &lastChangedTime);
1213 fclose(file);
1214 if (lastChangedTime > 0) {
1215 struct timespec now;
1216 clock_gettime(CLOCK_REALTIME, &now);
1217 // Match the Java timestamp format
1218 long long rtcNow = (now.tv_sec * 1000LL) + (now.tv_nsec / 1000000LL);
Damien Bargiacchi96762812016-07-12 15:53:40 -07001219 if (ACCURATE_TIME_EPOCH < rtcNow
1220 && lastChangedTime > (rtcNow - MAX_TIME_IN_PAST)
1221 && lastChangedTime < (rtcNow + MAX_TIME_IN_FUTURE)) {
Damien Bargiacchi97480862016-03-29 14:55:55 -07001222 mTimeIsAccurate = true;
1223 }
1224 }
1225 }
1226
1227 return mTimeIsAccurate;
1228}
1229
1230BootAnimation::TimeCheckThread::TimeCheckThread(BootAnimation* bootAnimation) : Thread(false),
1231 mInotifyFd(-1), mSystemWd(-1), mTimeWd(-1), mBootAnimation(bootAnimation) {}
1232
1233BootAnimation::TimeCheckThread::~TimeCheckThread() {
1234 // mInotifyFd may be -1 but that's ok since we're not at risk of attempting to close a valid FD.
1235 close(mInotifyFd);
1236}
1237
1238bool BootAnimation::TimeCheckThread::threadLoop() {
1239 bool shouldLoop = doThreadLoop() && !mBootAnimation->mTimeIsAccurate
1240 && mBootAnimation->mClockEnabled;
1241 if (!shouldLoop) {
1242 close(mInotifyFd);
1243 mInotifyFd = -1;
1244 }
1245 return shouldLoop;
1246}
1247
1248bool BootAnimation::TimeCheckThread::doThreadLoop() {
1249 static constexpr int BUFF_LEN (10 * (sizeof(struct inotify_event) + NAME_MAX + 1));
1250
1251 // Poll instead of doing a blocking read so the Thread can exit if requested.
1252 struct pollfd pfd = { mInotifyFd, POLLIN, 0 };
1253 ssize_t pollResult = poll(&pfd, 1, 1000);
1254
1255 if (pollResult == 0) {
1256 return true;
1257 } else if (pollResult < 0) {
Wei Wang159a4102018-10-23 23:15:58 -07001258 SLOGE("Could not poll inotify events");
Damien Bargiacchi97480862016-03-29 14:55:55 -07001259 return false;
1260 }
1261
1262 char buff[BUFF_LEN] __attribute__ ((aligned(__alignof__(struct inotify_event))));;
1263 ssize_t length = read(mInotifyFd, buff, BUFF_LEN);
1264 if (length == 0) {
1265 return true;
1266 } else if (length < 0) {
Wei Wang159a4102018-10-23 23:15:58 -07001267 SLOGE("Could not read inotify events");
Damien Bargiacchi97480862016-03-29 14:55:55 -07001268 return false;
1269 }
1270
1271 const struct inotify_event *event;
1272 for (char* ptr = buff; ptr < buff + length; ptr += sizeof(struct inotify_event) + event->len) {
1273 event = (const struct inotify_event *) ptr;
1274 if (event->wd == mSystemWd && strcmp(SYSTEM_TIME_DIR_NAME, event->name) == 0) {
1275 addTimeDirWatch();
1276 } else if (event->wd == mTimeWd && (strcmp(LAST_TIME_CHANGED_FILE_NAME, event->name) == 0
1277 || strcmp(ACCURATE_TIME_FLAG_FILE_NAME, event->name) == 0)) {
1278 return !mBootAnimation->updateIsTimeAccurate();
1279 }
1280 }
1281
1282 return true;
1283}
1284
1285void BootAnimation::TimeCheckThread::addTimeDirWatch() {
1286 mTimeWd = inotify_add_watch(mInotifyFd, SYSTEM_TIME_DIR_PATH,
1287 IN_CLOSE_WRITE | IN_MOVED_TO | IN_ATTRIB);
1288 if (mTimeWd > 0) {
1289 // No need to watch for the time directory to be created if it already exists
1290 inotify_rm_watch(mInotifyFd, mSystemWd);
1291 mSystemWd = -1;
1292 }
1293}
1294
1295status_t BootAnimation::TimeCheckThread::readyToRun() {
1296 mInotifyFd = inotify_init();
1297 if (mInotifyFd < 0) {
Wei Wang159a4102018-10-23 23:15:58 -07001298 SLOGE("Could not initialize inotify fd");
Damien Bargiacchi97480862016-03-29 14:55:55 -07001299 return NO_INIT;
1300 }
1301
1302 mSystemWd = inotify_add_watch(mInotifyFd, SYSTEM_DATA_DIR_PATH, IN_CREATE | IN_ATTRIB);
1303 if (mSystemWd < 0) {
1304 close(mInotifyFd);
1305 mInotifyFd = -1;
Wei Wang159a4102018-10-23 23:15:58 -07001306 SLOGE("Could not add watch for %s", SYSTEM_DATA_DIR_PATH);
Damien Bargiacchi97480862016-03-29 14:55:55 -07001307 return NO_INIT;
1308 }
1309
1310 addTimeDirWatch();
1311
1312 if (mBootAnimation->updateIsTimeAccurate()) {
1313 close(mInotifyFd);
1314 mInotifyFd = -1;
1315 return ALREADY_EXISTS;
1316 }
1317
1318 return NO_ERROR;
1319}
1320
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001321// ---------------------------------------------------------------------------
1322
1323}
1324; // namespace android