blob: f08c79ad6954a926a874c477fff85b9ea1745b7a [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
Derek Sollenberger22d0d4c72020-04-08 10:53:46 -040037#include <android/imagedecoder.h>
Mathias Agopianb13b9bd2012-02-17 18:27:36 -080038#include <androidfw/AssetManager.h>
Mathias Agopianac31a3b2009-05-21 19:59:24 -070039#include <binder/IPCThreadState.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080040#include <utils/Errors.h>
41#include <utils/Log.h>
Wei Wanga90c54c2017-02-02 11:35:21 -080042#include <utils/SystemClock.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080043
Keun-young Parkb5938422017-03-23 13:46:24 -070044#include <android-base/properties.h>
45
Dominik Laskowski69b281d2019-11-22 14:13:12 -080046#include <ui/DisplayConfig.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080047#include <ui/PixelFormat.h>
48#include <ui/Rect.h>
49#include <ui/Region.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080050
Jeff Brown0b722fe2012-08-24 22:40:14 -070051#include <gui/ISurfaceComposer.h>
Mathias Agopian8335f1c2012-02-25 18:48:35 -080052#include <gui/Surface.h>
53#include <gui/SurfaceComposerClient.h>
Mathias Agopian000479f2010-02-09 17:46:37 -080054
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080055#include <GLES/gl.h>
56#include <GLES/glext.h>
57#include <EGL/eglext.h>
58
59#include "BootAnimation.h"
60
Kyeongkab.Nam35a8a112019-07-17 16:41:48 +090061#define ANIM_PATH_MAX 255
62#define STR(x) #x
63#define STRTO(x) STR(x)
64
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080065namespace android {
66
Damien Bargiacchi97480862016-03-29 14:55:55 -070067static const char OEM_BOOTANIMATION_FILE[] = "/oem/media/bootanimation.zip";
Beverly34ffe252019-05-17 11:03:54 -040068static const char PRODUCT_BOOTANIMATION_DARK_FILE[] = "/product/media/bootanimation-dark.zip";
Jaekyun Seokc521bb32018-01-30 11:52:47 +090069static const char PRODUCT_BOOTANIMATION_FILE[] = "/product/media/bootanimation.zip";
Damien Bargiacchi97480862016-03-29 14:55:55 -070070static const char SYSTEM_BOOTANIMATION_FILE[] = "/system/media/bootanimation.zip";
Anders Fridlunde0b4d232018-11-06 14:23:25 +010071static const char APEX_BOOTANIMATION_FILE[] = "/apex/com.android.bootanimation/etc/bootanimation.zip";
Jaekyun Seokc521bb32018-01-30 11:52:47 +090072static const char PRODUCT_ENCRYPTED_BOOTANIMATION_FILE[] = "/product/media/bootanimation-encrypted.zip";
Damien Bargiacchi97480862016-03-29 14:55:55 -070073static const char SYSTEM_ENCRYPTED_BOOTANIMATION_FILE[] = "/system/media/bootanimation-encrypted.zip";
Keun-young Parkb5938422017-03-23 13:46:24 -070074static const char OEM_SHUTDOWNANIMATION_FILE[] = "/oem/media/shutdownanimation.zip";
Jaekyun Seokc521bb32018-01-30 11:52:47 +090075static const char PRODUCT_SHUTDOWNANIMATION_FILE[] = "/product/media/shutdownanimation.zip";
Keun-young Parkb5938422017-03-23 13:46:24 -070076static const char SYSTEM_SHUTDOWNANIMATION_FILE[] = "/system/media/shutdownanimation.zip";
77
Nikita Ioffe06c986e2020-01-27 17:16:03 +000078static constexpr const char* PRODUCT_USERSPACE_REBOOT_ANIMATION_FILE = "/product/media/userspace-reboot.zip";
79static constexpr const char* OEM_USERSPACE_REBOOT_ANIMATION_FILE = "/oem/media/userspace-reboot.zip";
80static constexpr const char* SYSTEM_USERSPACE_REBOOT_ANIMATION_FILE = "/system/media/userspace-reboot.zip";
81
Damien Bargiacchi97480862016-03-29 14:55:55 -070082static const char SYSTEM_DATA_DIR_PATH[] = "/data/system";
83static const char SYSTEM_TIME_DIR_NAME[] = "time";
84static const char SYSTEM_TIME_DIR_PATH[] = "/data/system/time";
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -070085static const char CLOCK_FONT_ASSET[] = "images/clock_font.png";
86static const char CLOCK_FONT_ZIP_NAME[] = "clock_font.png";
Damien Bargiacchi97480862016-03-29 14:55:55 -070087static const char LAST_TIME_CHANGED_FILE_NAME[] = "last_time_change";
88static const char LAST_TIME_CHANGED_FILE_PATH[] = "/data/system/time/last_time_change";
89static const char ACCURATE_TIME_FLAG_FILE_NAME[] = "time_is_accurate";
90static const char ACCURATE_TIME_FLAG_FILE_PATH[] = "/data/system/time/time_is_accurate";
Damien Bargiacchi9071db12016-10-28 17:38:22 -070091static const char TIME_FORMAT_12_HOUR_FLAG_FILE_PATH[] = "/data/system/time/time_format_12_hour";
Damien Bargiacchi96762812016-07-12 15:53:40 -070092// Java timestamp format. Don't show the clock if the date is before 2000-01-01 00:00:00.
93static const long long ACCURATE_TIME_EPOCH = 946684800000;
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -070094static constexpr char FONT_BEGIN_CHAR = ' ';
95static constexpr char FONT_END_CHAR = '~' + 1;
96static constexpr size_t FONT_NUM_CHARS = FONT_END_CHAR - FONT_BEGIN_CHAR + 1;
97static constexpr size_t FONT_NUM_COLS = 16;
98static constexpr size_t FONT_NUM_ROWS = FONT_NUM_CHARS / FONT_NUM_COLS;
99static const int TEXT_CENTER_VALUE = INT_MAX;
100static const int TEXT_MISSING_VALUE = INT_MIN;
Damien Bargiacchi97480862016-03-29 14:55:55 -0700101static const char EXIT_PROP_NAME[] = "service.bootanim.exit";
Huihong Luod4a217e2020-01-16 15:56:33 -0800102static const char DISPLAYS_PROP_NAME[] = "persist.service.bootanim.displays";
Kyeongkab.Nam35a8a112019-07-17 16:41:48 +0900103static const int ANIM_ENTRY_NAME_MAX = ANIM_PATH_MAX + 1;
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700104static constexpr size_t TEXT_POS_LEN_MAX = 16;
Narayan Kamathafd31e02013-12-03 13:16:03 +0000105
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800106// ---------------------------------------------------------------------------
107
Ed Coyne7464ac92017-06-08 12:26:48 -0700108BootAnimation::BootAnimation(sp<Callbacks> callbacks)
Ed Coyne2c9e94a2017-05-31 10:08:28 -0700109 : Thread(false), mClockEnabled(true), mTimeIsAccurate(false),
Yi Kong911ac232019-03-24 01:49:02 -0700110 mTimeFormat12Hour(false), mTimeCheckThread(nullptr), mCallbacks(callbacks) {
Mathias Agopian627e7b52009-05-21 19:21:59 -0700111 mSession = new SurfaceComposerClient();
Geoffrey Pitsch290c4352016-08-09 14:35:10 -0400112
Keun-young Parkb5938422017-03-23 13:46:24 -0700113 std::string powerCtl = android::base::GetProperty("sys.powerctl", "");
114 if (powerCtl.empty()) {
115 mShuttingDown = false;
116 } else {
117 mShuttingDown = true;
118 }
Huihong Luo50a2f362018-08-11 09:27:57 -0700119 ALOGD("%sAnimationStartTiming start time: %" PRId64 "ms", mShuttingDown ? "Shutdown" : "Boot",
120 elapsedRealtime());
121}
122
123BootAnimation::~BootAnimation() {
124 if (mAnimation != nullptr) {
125 releaseAnimation(mAnimation);
126 mAnimation = nullptr;
127 }
128 ALOGD("%sAnimationStopTiming start time: %" PRId64 "ms", mShuttingDown ? "Shutdown" : "Boot",
129 elapsedRealtime());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800130}
131
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800132void BootAnimation::onFirstRef() {
Mathias Agopianbc726112009-09-23 15:44:05 -0700133 status_t err = mSession->linkToComposerDeath(this);
Wei Wang159a4102018-10-23 23:15:58 -0700134 SLOGE_IF(err, "linkToComposerDeath failed (%s) ", strerror(-err));
Mathias Agopian8434c532009-09-23 18:52:49 -0700135 if (err == NO_ERROR) {
Huihong Luo50a2f362018-08-11 09:27:57 -0700136 // Load the animation content -- this can be slow (eg 200ms)
137 // called before waitForSurfaceFlinger() in main() to avoid wait
138 ALOGD("%sAnimationPreloadTiming start time: %" PRId64 "ms",
139 mShuttingDown ? "Shutdown" : "Boot", elapsedRealtime());
140 preloadAnimation();
141 ALOGD("%sAnimationPreloadStopTiming start time: %" PRId64 "ms",
142 mShuttingDown ? "Shutdown" : "Boot", elapsedRealtime());
Mathias Agopianbc726112009-09-23 15:44:05 -0700143 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800144}
145
Mathias Agopianbc726112009-09-23 15:44:05 -0700146sp<SurfaceComposerClient> BootAnimation::session() const {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800147 return mSession;
148}
149
Narayan Kamathafd31e02013-12-03 13:16:03 +0000150void BootAnimation::binderDied(const wp<IBinder>&)
Mathias Agopianbc726112009-09-23 15:44:05 -0700151{
152 // woah, surfaceflinger died!
Wei Wang159a4102018-10-23 23:15:58 -0700153 SLOGD("SurfaceFlinger died, exiting...");
Mathias Agopianbc726112009-09-23 15:44:05 -0700154
155 // calling requestExit() is not enough here because the Surface code
156 // might be blocked on a condition variable that will never be updated.
157 kill( getpid(), SIGKILL );
158 requestExit();
159}
160
Derek Sollenberger22d0d4c72020-04-08 10:53:46 -0400161static void* decodeImage(const void* encodedData, size_t dataLength, AndroidBitmapInfo* outInfo) {
162 AImageDecoder* decoder = nullptr;
163 AImageDecoder_createFromBuffer(encodedData, dataLength, &decoder);
164 if (!decoder) {
165 return nullptr;
166 }
167
168 const AImageDecoderHeaderInfo* info = AImageDecoder_getHeaderInfo(decoder);
169 outInfo->width = AImageDecoderHeaderInfo_getWidth(info);
170 outInfo->height = AImageDecoderHeaderInfo_getHeight(info);
171 outInfo->format = AImageDecoderHeaderInfo_getAndroidBitmapFormat(info);
172 outInfo->stride = AImageDecoder_getMinimumStride(decoder);
173 outInfo->flags = 0;
174
175 const size_t size = outInfo->stride * outInfo->height;
176 void* pixels = malloc(size);
177 int result = AImageDecoder_decodeImage(decoder, pixels, outInfo->stride, size);
178 AImageDecoder_delete(decoder);
179
180 if (result != ANDROID_IMAGE_DECODER_SUCCESS) {
181 free(pixels);
182 return nullptr;
183 }
184 return pixels;
185}
186
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800187status_t BootAnimation::initTexture(Texture* texture, AssetManager& assets,
188 const char* name) {
189 Asset* asset = assets.open(name, Asset::ACCESS_BUFFER);
Yi Kong911ac232019-03-24 01:49:02 -0700190 if (asset == nullptr)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800191 return NO_INIT;
Derek Sollenberger22d0d4c72020-04-08 10:53:46 -0400192
193 AndroidBitmapInfo bitmapInfo;
194 void* pixels = decodeImage(asset->getBuffer(false), asset->getLength(), &bitmapInfo);
195 auto pixelDeleter = std::unique_ptr<void, decltype(free)*>{ pixels, free };
196
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800197 asset->close();
198 delete asset;
199
Derek Sollenberger22d0d4c72020-04-08 10:53:46 -0400200 if (!pixels) {
201 return NO_INIT;
202 }
203
204 const int w = bitmapInfo.width;
205 const int h = bitmapInfo.height;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800206
207 GLint crop[4] = { 0, h, w, -h };
208 texture->w = w;
209 texture->h = h;
210
211 glGenTextures(1, &texture->name);
212 glBindTexture(GL_TEXTURE_2D, texture->name);
213
Derek Sollenberger22d0d4c72020-04-08 10:53:46 -0400214 switch (bitmapInfo.format) {
215 case ANDROID_BITMAP_FORMAT_A_8:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800216 glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, w, h, 0, GL_ALPHA,
Derek Sollenberger22d0d4c72020-04-08 10:53:46 -0400217 GL_UNSIGNED_BYTE, pixels);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800218 break;
Derek Sollenberger22d0d4c72020-04-08 10:53:46 -0400219 case ANDROID_BITMAP_FORMAT_RGBA_4444:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800220 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA,
Derek Sollenberger22d0d4c72020-04-08 10:53:46 -0400221 GL_UNSIGNED_SHORT_4_4_4_4, pixels);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800222 break;
Derek Sollenberger22d0d4c72020-04-08 10:53:46 -0400223 case ANDROID_BITMAP_FORMAT_RGBA_8888:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800224 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA,
Derek Sollenberger22d0d4c72020-04-08 10:53:46 -0400225 GL_UNSIGNED_BYTE, pixels);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800226 break;
Derek Sollenberger22d0d4c72020-04-08 10:53:46 -0400227 case ANDROID_BITMAP_FORMAT_RGB_565:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800228 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, w, h, 0, GL_RGB,
Derek Sollenberger22d0d4c72020-04-08 10:53:46 -0400229 GL_UNSIGNED_SHORT_5_6_5, pixels);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800230 break;
231 default:
232 break;
233 }
234
235 glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_CROP_RECT_OES, crop);
236 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
237 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
238 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
239 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700240
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800241 return NO_ERROR;
242}
243
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700244status_t BootAnimation::initTexture(FileMap* map, int* width, int* height)
Mathias Agopiana8826d62009-10-01 03:10:14 -0700245{
Derek Sollenberger22d0d4c72020-04-08 10:53:46 -0400246 AndroidBitmapInfo bitmapInfo;
247 void* pixels = decodeImage(map->getDataPtr(), map->getDataLength(), &bitmapInfo);
248 auto pixelDeleter = std::unique_ptr<void, decltype(free)*>{ pixels, free };
Mathias Agopiana8826d62009-10-01 03:10:14 -0700249
Mykola Kondratenko0c1eeb32014-04-15 09:35:44 +0200250 // FileMap memory is never released until application exit.
251 // Release it now as the texture is already loaded and the memory used for
252 // the packed resource can be released.
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700253 delete map;
Mykola Kondratenko0c1eeb32014-04-15 09:35:44 +0200254
Derek Sollenberger22d0d4c72020-04-08 10:53:46 -0400255 if (!pixels) {
256 return NO_INIT;
257 }
258
259 const int w = bitmapInfo.width;
260 const int h = bitmapInfo.height;
Mathias Agopiana8826d62009-10-01 03:10:14 -0700261
262 GLint crop[4] = { 0, h, w, -h };
263 int tw = 1 << (31 - __builtin_clz(w));
264 int th = 1 << (31 - __builtin_clz(h));
265 if (tw < w) tw <<= 1;
266 if (th < h) th <<= 1;
267
Derek Sollenberger22d0d4c72020-04-08 10:53:46 -0400268 switch (bitmapInfo.format) {
269 case ANDROID_BITMAP_FORMAT_RGBA_8888:
Sai Kiran Korwar27167492015-07-07 20:00:06 +0530270 if (!mUseNpotTextures && (tw != w || th != h)) {
Mathias Agopiana8826d62009-10-01 03:10:14 -0700271 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, tw, th, 0, GL_RGBA,
Yi Kong911ac232019-03-24 01:49:02 -0700272 GL_UNSIGNED_BYTE, nullptr);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700273 glTexSubImage2D(GL_TEXTURE_2D, 0,
Derek Sollenberger22d0d4c72020-04-08 10:53:46 -0400274 0, 0, w, h, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700275 } else {
Sai Kiran Korwar27167492015-07-07 20:00:06 +0530276 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA,
Derek Sollenberger22d0d4c72020-04-08 10:53:46 -0400277 GL_UNSIGNED_BYTE, pixels);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700278 }
279 break;
280
Derek Sollenberger22d0d4c72020-04-08 10:53:46 -0400281 case ANDROID_BITMAP_FORMAT_RGB_565:
Sai Kiran Korwar27167492015-07-07 20:00:06 +0530282 if (!mUseNpotTextures && (tw != w || th != h)) {
Mathias Agopiana8826d62009-10-01 03:10:14 -0700283 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, tw, th, 0, GL_RGB,
Yi Kong911ac232019-03-24 01:49:02 -0700284 GL_UNSIGNED_SHORT_5_6_5, nullptr);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700285 glTexSubImage2D(GL_TEXTURE_2D, 0,
Derek Sollenberger22d0d4c72020-04-08 10:53:46 -0400286 0, 0, w, h, GL_RGB, GL_UNSIGNED_SHORT_5_6_5, pixels);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700287 } else {
Sai Kiran Korwar27167492015-07-07 20:00:06 +0530288 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, w, h, 0, GL_RGB,
Derek Sollenberger22d0d4c72020-04-08 10:53:46 -0400289 GL_UNSIGNED_SHORT_5_6_5, pixels);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700290 }
291 break;
292 default:
293 break;
294 }
295
296 glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_CROP_RECT_OES, crop);
297
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700298 *width = w;
299 *height = h;
300
Mathias Agopiana8826d62009-10-01 03:10:14 -0700301 return NO_ERROR;
302}
303
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800304status_t BootAnimation::readyToRun() {
305 mAssets.addDefaultAssets();
306
Dominik Laskowski3316a0a2019-01-25 02:56:41 -0800307 mDisplayToken = SurfaceComposerClient::getInternalDisplayToken();
308 if (mDisplayToken == nullptr)
Dominik Laskowski69b281d2019-11-22 14:13:12 -0800309 return NAME_NOT_FOUND;
Dominik Laskowski3316a0a2019-01-25 02:56:41 -0800310
Dominik Laskowski69b281d2019-11-22 14:13:12 -0800311 DisplayConfig displayConfig;
312 const status_t error =
313 SurfaceComposerClient::getActiveDisplayConfig(mDisplayToken, &displayConfig);
314 if (error != NO_ERROR)
315 return error;
316
317 const ui::Size& resolution = displayConfig.resolution;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800318
319 // create the native surface
Jeff Brown0b722fe2012-08-24 22:40:14 -0700320 sp<SurfaceControl> control = session()->createSurface(String8("BootAnimation"),
Dominik Laskowski69b281d2019-11-22 14:13:12 -0800321 resolution.getWidth(), resolution.getHeight(), PIXEL_FORMAT_RGB_565);
Mathias Agopian439863f2011-06-28 19:09:31 -0700322
Robert Carre13b58e2017-08-31 14:50:44 -0700323 SurfaceComposerClient::Transaction t;
Huihong Luof97c9e22019-05-29 11:08:01 -0700324
325 // this guest property specifies multi-display IDs to show the boot animation
326 // multiple ids can be set with comma (,) as separator, for example:
Huihong Luod4a217e2020-01-16 15:56:33 -0800327 // setprop persist.boot.animation.displays 19260422155234049,19261083906282754
Huihong Luof97c9e22019-05-29 11:08:01 -0700328 Vector<uint64_t> physicalDisplayIds;
329 char displayValue[PROPERTY_VALUE_MAX] = "";
Huihong Luod4a217e2020-01-16 15:56:33 -0800330 property_get(DISPLAYS_PROP_NAME, displayValue, "");
Huihong Luof97c9e22019-05-29 11:08:01 -0700331 bool isValid = displayValue[0] != '\0';
332 if (isValid) {
333 char *p = displayValue;
334 while (*p) {
335 if (!isdigit(*p) && *p != ',') {
336 isValid = false;
337 break;
338 }
339 p ++;
340 }
341 if (!isValid)
Huihong Luod4a217e2020-01-16 15:56:33 -0800342 SLOGE("Invalid syntax for the value of system prop: %s", DISPLAYS_PROP_NAME);
Huihong Luof97c9e22019-05-29 11:08:01 -0700343 }
344 if (isValid) {
345 std::istringstream stream(displayValue);
346 for (PhysicalDisplayId id; stream >> id; ) {
347 physicalDisplayIds.add(id);
348 if (stream.peek() == ',')
349 stream.ignore();
350 }
351
352 // In the case of multi-display, boot animation shows on the specified displays
353 // in addition to the primary display
354 auto ids = SurfaceComposerClient::getPhysicalDisplayIds();
355 constexpr uint32_t LAYER_STACK = 0;
356 for (auto id : physicalDisplayIds) {
357 if (std::find(ids.begin(), ids.end(), id) != ids.end()) {
358 sp<IBinder> token = SurfaceComposerClient::getPhysicalDisplayToken(id);
359 if (token != nullptr)
360 t.setDisplayLayerStack(token, LAYER_STACK);
361 }
362 }
363 t.setLayerStack(control, LAYER_STACK);
364 }
365
Robert Carre13b58e2017-08-31 14:50:44 -0700366 t.setLayer(control, 0x40000000)
367 .apply();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800368
Mathias Agopian17f638b2009-04-16 20:04:08 -0700369 sp<Surface> s = control->getSurface();
370
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800371 // initialize opengl and egl
Mathias Agopian738b9a42009-08-06 16:41:02 -0700372 const EGLint attribs[] = {
Mathias Agopian1b253b72011-08-15 15:20:22 -0700373 EGL_RED_SIZE, 8,
374 EGL_GREEN_SIZE, 8,
375 EGL_BLUE_SIZE, 8,
Mathias Agopiana8826d62009-10-01 03:10:14 -0700376 EGL_DEPTH_SIZE, 0,
377 EGL_NONE
Mathias Agopian738b9a42009-08-06 16:41:02 -0700378 };
Andreas Gampecfedceb2014-09-30 21:48:18 -0700379 EGLint w, h;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800380 EGLint numConfigs;
381 EGLConfig config;
382 EGLSurface surface;
383 EGLContext context;
Mathias Agopian627e7b52009-05-21 19:21:59 -0700384
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800385 EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
Mathias Agopian627e7b52009-05-21 19:21:59 -0700386
Yi Kong911ac232019-03-24 01:49:02 -0700387 eglInitialize(display, nullptr, nullptr);
Mathias Agopian1b253b72011-08-15 15:20:22 -0700388 eglChooseConfig(display, attribs, &config, 1, &numConfigs);
Yi Kong911ac232019-03-24 01:49:02 -0700389 surface = eglCreateWindowSurface(display, config, s.get(), nullptr);
390 context = eglCreateContext(display, config, nullptr, nullptr);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800391 eglQuerySurface(display, surface, EGL_WIDTH, &w);
392 eglQuerySurface(display, surface, EGL_HEIGHT, &h);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700393
Mathias Agopianabac0102009-07-31 14:47:00 -0700394 if (eglMakeCurrent(display, surface, surface, context) == EGL_FALSE)
395 return NO_INIT;
Mathias Agopiana8826d62009-10-01 03:10:14 -0700396
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800397 mDisplay = display;
398 mContext = context;
399 mSurface = surface;
400 mWidth = w;
401 mHeight = h;
Mathias Agopian17f638b2009-04-16 20:04:08 -0700402 mFlingerSurfaceControl = control;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800403 mFlingerSurface = s;
Adrian Roos9ee5dff2018-08-22 20:19:49 +0200404 mTargetInset = -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800405
Huihong Luo50a2f362018-08-11 09:27:57 -0700406 return NO_ERROR;
407}
408
409bool BootAnimation::preloadAnimation() {
410 findBootAnimationFile();
411 if (!mZipFileName.isEmpty()) {
412 mAnimation = loadAnimation(mZipFileName);
413 return (mAnimation != nullptr);
414 }
415
416 return false;
417}
418
Nikita Ioffe06c986e2020-01-27 17:16:03 +0000419bool BootAnimation::findBootAnimationFileInternal(const std::vector<std::string> &files) {
420 for (const std::string& f : files) {
421 if (access(f.c_str(), R_OK) == 0) {
422 mZipFileName = f.c_str();
423 return true;
424 }
425 }
426 return false;
427}
428
Huihong Luo50a2f362018-08-11 09:27:57 -0700429void BootAnimation::findBootAnimationFile() {
Elliott Hughesc367d482013-10-29 13:12:55 -0700430 // If the device has encryption turned on or is in process
Jason parksbd9a08d2011-01-31 15:04:34 -0600431 // of being encrypted we show the encrypted boot animation.
432 char decrypt[PROPERTY_VALUE_MAX];
433 property_get("vold.decrypt", decrypt, "");
434
Keun-young Parkb5938422017-03-23 13:46:24 -0700435 bool encryptedAnimation = atoi(decrypt) != 0 ||
436 !strcmp("trigger_restart_min_framework", decrypt);
Jason parksbd9a08d2011-01-31 15:04:34 -0600437
Jaekyun Seokc521bb32018-01-30 11:52:47 +0900438 if (!mShuttingDown && encryptedAnimation) {
Nikita Ioffe06c986e2020-01-27 17:16:03 +0000439 static const std::vector<std::string> encryptedBootFiles = {
440 PRODUCT_ENCRYPTED_BOOTANIMATION_FILE, SYSTEM_ENCRYPTED_BOOTANIMATION_FILE,
441 };
442 if (findBootAnimationFileInternal(encryptedBootFiles)) {
443 return;
Jaekyun Seokc521bb32018-01-30 11:52:47 +0900444 }
Jason parksbd9a08d2011-01-31 15:04:34 -0600445 }
Beverly34ffe252019-05-17 11:03:54 -0400446
447 const bool playDarkAnim = android::base::GetIntProperty("ro.boot.theme", 0) == 1;
Nikita Ioffe06c986e2020-01-27 17:16:03 +0000448 static const std::vector<std::string> bootFiles = {
449 APEX_BOOTANIMATION_FILE, playDarkAnim ? PRODUCT_BOOTANIMATION_DARK_FILE : PRODUCT_BOOTANIMATION_FILE,
450 OEM_BOOTANIMATION_FILE, SYSTEM_BOOTANIMATION_FILE
451 };
452 static const std::vector<std::string> shutdownFiles = {
453 PRODUCT_SHUTDOWNANIMATION_FILE, OEM_SHUTDOWNANIMATION_FILE, SYSTEM_SHUTDOWNANIMATION_FILE, ""
454 };
455 static const std::vector<std::string> userspaceRebootFiles = {
456 PRODUCT_USERSPACE_REBOOT_ANIMATION_FILE, OEM_USERSPACE_REBOOT_ANIMATION_FILE,
457 SYSTEM_USERSPACE_REBOOT_ANIMATION_FILE,
458 };
Keun-young Parkb5938422017-03-23 13:46:24 -0700459
Nikita Ioffe06c986e2020-01-27 17:16:03 +0000460 if (android::base::GetBoolProperty("sys.init.userspace_reboot.in_progress", false)) {
461 findBootAnimationFileInternal(userspaceRebootFiles);
462 } else if (mShuttingDown) {
463 findBootAnimationFileInternal(shutdownFiles);
464 } else {
465 findBootAnimationFileInternal(bootFiles);
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700466 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800467}
468
Mathias Agopiana8826d62009-10-01 03:10:14 -0700469bool BootAnimation::threadLoop()
470{
471 bool r;
Narayan Kamathafd31e02013-12-03 13:16:03 +0000472 // We have no bootanimation file, so we use the stock android logo
473 // animation.
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700474 if (mZipFileName.isEmpty()) {
Mathias Agopiana8826d62009-10-01 03:10:14 -0700475 r = android();
476 } else {
477 r = movie();
478 }
479
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800480 eglMakeCurrent(mDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
481 eglDestroyContext(mDisplay, mContext);
482 eglDestroySurface(mDisplay, mSurface);
Mathias Agopian6cf0db22009-04-17 19:36:26 -0700483 mFlingerSurface.clear();
Mathias Agopian17f638b2009-04-16 20:04:08 -0700484 mFlingerSurfaceControl.clear();
Mathias Agopian627e7b52009-05-21 19:21:59 -0700485 eglTerminate(mDisplay);
Sai Kiran Korwar3eee9fb2015-06-16 17:13:35 +0530486 eglReleaseThread();
Mathias Agopian627e7b52009-05-21 19:21:59 -0700487 IPCThreadState::self()->stopProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800488 return r;
489}
490
Mathias Agopiana8826d62009-10-01 03:10:14 -0700491bool BootAnimation::android()
492{
Wei Wang159a4102018-10-23 23:15:58 -0700493 SLOGD("%sAnimationShownTiming start time: %" PRId64 "ms", mShuttingDown ? "Shutdown" : "Boot",
Keun-young Parkd1794cd2017-04-04 12:21:19 -0700494 elapsedRealtime());
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700495 initTexture(&mAndroid[0], mAssets, "images/android-logo-mask.png");
496 initTexture(&mAndroid[1], mAssets, "images/android-logo-shine.png");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800497
Ed Coyne7464ac92017-06-08 12:26:48 -0700498 mCallbacks->init({});
499
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800500 // clear screen
Mathias Agopiana8826d62009-10-01 03:10:14 -0700501 glShadeModel(GL_FLAT);
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700502 glDisable(GL_DITHER);
503 glDisable(GL_SCISSOR_TEST);
Mathias Agopian59f19e42011-05-06 19:22:12 -0700504 glClearColor(0,0,0,1);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800505 glClear(GL_COLOR_BUFFER_BIT);
506 eglSwapBuffers(mDisplay, mSurface);
507
Mathias Agopiana8826d62009-10-01 03:10:14 -0700508 glEnable(GL_TEXTURE_2D);
509 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
510
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700511 const GLint xc = (mWidth - mAndroid[0].w) / 2;
512 const GLint yc = (mHeight - mAndroid[0].h) / 2;
513 const Rect updateRect(xc, yc, xc + mAndroid[0].w, yc + mAndroid[0].h);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800514
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800515 glScissor(updateRect.left, mHeight - updateRect.bottom, updateRect.width(),
516 updateRect.height());
517
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700518 // Blend state
519 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
520 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
521
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800522 const nsecs_t startTime = systemTime();
523 do {
Mathias Agopian13796652009-03-24 22:49:21 -0700524 nsecs_t now = systemTime();
525 double time = now - startTime;
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700526 float t = 4.0f * float(time / us2ns(16667)) / mAndroid[1].w;
527 GLint offset = (1 - (t - floorf(t))) * mAndroid[1].w;
528 GLint x = xc - offset;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800529
Mathias Agopian81668642009-07-28 11:41:30 -0700530 glDisable(GL_SCISSOR_TEST);
531 glClear(GL_COLOR_BUFFER_BIT);
532
533 glEnable(GL_SCISSOR_TEST);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800534 glDisable(GL_BLEND);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800535 glBindTexture(GL_TEXTURE_2D, mAndroid[1].name);
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700536 glDrawTexiOES(x, yc, 0, mAndroid[1].w, mAndroid[1].h);
537 glDrawTexiOES(x + mAndroid[1].w, yc, 0, mAndroid[1].w, mAndroid[1].h);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800538
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700539 glEnable(GL_BLEND);
540 glBindTexture(GL_TEXTURE_2D, mAndroid[0].name);
541 glDrawTexiOES(xc, yc, 0, mAndroid[0].w, mAndroid[0].h);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800542
Mathias Agopian627e7b52009-05-21 19:21:59 -0700543 EGLBoolean res = eglSwapBuffers(mDisplay, mSurface);
544 if (res == EGL_FALSE)
545 break;
546
Mathias Agopian13796652009-03-24 22:49:21 -0700547 // 12fps: don't animate too fast to preserve CPU
548 const nsecs_t sleepTime = 83333 - ns2us(systemTime() - now);
549 if (sleepTime > 0)
Mathias Agopiana8826d62009-10-01 03:10:14 -0700550 usleep(sleepTime);
Kevin Hesterd3782b22012-04-26 10:38:55 -0700551
552 checkExit();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800553 } while (!exitPending());
554
555 glDeleteTextures(1, &mAndroid[0].name);
556 glDeleteTextures(1, &mAndroid[1].name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800557 return false;
558}
559
Kevin Hesterd3782b22012-04-26 10:38:55 -0700560void BootAnimation::checkExit() {
561 // Allow surface flinger to gracefully request shutdown
562 char value[PROPERTY_VALUE_MAX];
563 property_get(EXIT_PROP_NAME, value, "0");
564 int exitnow = atoi(value);
565 if (exitnow) {
566 requestExit();
Ed Coyne7464ac92017-06-08 12:26:48 -0700567 mCallbacks->shutdown();
Kevin Hesterd3782b22012-04-26 10:38:55 -0700568 }
569}
570
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700571bool BootAnimation::validClock(const Animation::Part& part) {
572 return part.clockPosX != TEXT_MISSING_VALUE && part.clockPosY != TEXT_MISSING_VALUE;
573}
574
575bool parseTextCoord(const char* str, int* dest) {
576 if (strcmp("c", str) == 0) {
577 *dest = TEXT_CENTER_VALUE;
578 return true;
579 }
580
581 char* end;
582 int val = (int) strtol(str, &end, 0);
583 if (end == str || *end != '\0' || val == INT_MAX || val == INT_MIN) {
584 return false;
585 }
586 *dest = val;
587 return true;
588}
589
590// Parse two position coordinates. If only string is non-empty, treat it as the y value.
591void parsePosition(const char* str1, const char* str2, int* x, int* y) {
592 bool success = false;
593 if (strlen(str1) == 0) { // No values were specified
594 // success = false
595 } else if (strlen(str2) == 0) { // we have only one value
596 if (parseTextCoord(str1, y)) {
597 *x = TEXT_CENTER_VALUE;
598 success = true;
599 }
600 } else {
601 if (parseTextCoord(str1, x) && parseTextCoord(str2, y)) {
602 success = true;
603 }
604 }
605
606 if (!success) {
607 *x = TEXT_MISSING_VALUE;
608 *y = TEXT_MISSING_VALUE;
609 }
610}
611
Jesse Hall083b84c2014-09-22 10:51:09 -0700612// Parse a color represented as an HTML-style 'RRGGBB' string: each pair of
613// characters in str is a hex number in [0, 255], which are converted to
614// floating point values in the range [0.0, 1.0] and placed in the
615// corresponding elements of color.
616//
617// If the input string isn't valid, parseColor returns false and color is
618// left unchanged.
619static bool parseColor(const char str[7], float color[3]) {
620 float tmpColor[3];
621 for (int i = 0; i < 3; i++) {
622 int val = 0;
623 for (int j = 0; j < 2; j++) {
624 val *= 16;
625 char c = str[2*i + j];
626 if (c >= '0' && c <= '9') val += c - '0';
627 else if (c >= 'A' && c <= 'F') val += (c - 'A') + 10;
628 else if (c >= 'a' && c <= 'f') val += (c - 'a') + 10;
629 else return false;
630 }
631 tmpColor[i] = static_cast<float>(val) / 255.0f;
632 }
633 memcpy(color, tmpColor, sizeof(tmpColor));
634 return true;
635}
636
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700637
638static bool readFile(ZipFileRO* zip, const char* name, String8& outString)
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700639{
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700640 ZipEntryRO entry = zip->findEntryByName(name);
Wei Wang159a4102018-10-23 23:15:58 -0700641 SLOGE_IF(!entry, "couldn't find %s", name);
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700642 if (!entry) {
643 return false;
644 }
645
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700646 FileMap* entryMap = zip->createEntryFileMap(entry);
647 zip->releaseEntry(entry);
Wei Wang159a4102018-10-23 23:15:58 -0700648 SLOGE_IF(!entryMap, "entryMap is null");
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700649 if (!entryMap) {
650 return false;
651 }
652
653 outString.setTo((char const*)entryMap->getDataPtr(), entryMap->getDataLength());
Narayan Kamath688ff4c2015-02-23 15:47:54 +0000654 delete entryMap;
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700655 return true;
656}
657
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700658// The font image should be a 96x2 array of character images. The
659// columns are the printable ASCII characters 0x20 - 0x7f. The
660// top row is regular text; the bottom row is bold.
661status_t BootAnimation::initFont(Font* font, const char* fallback) {
662 status_t status = NO_ERROR;
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800663
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700664 if (font->map != nullptr) {
665 glGenTextures(1, &font->texture.name);
666 glBindTexture(GL_TEXTURE_2D, font->texture.name);
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800667
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700668 status = initTexture(font->map, &font->texture.w, &font->texture.h);
669
670 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
671 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
672 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
673 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
674 } else if (fallback != nullptr) {
675 status = initTexture(&font->texture, mAssets, fallback);
676 } else {
677 return NO_INIT;
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800678 }
679
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700680 if (status == NO_ERROR) {
681 font->char_width = font->texture.w / FONT_NUM_COLS;
682 font->char_height = font->texture.h / FONT_NUM_ROWS / 2; // There are bold and regular rows
683 }
684
685 return status;
686}
687
688void BootAnimation::drawText(const char* str, const Font& font, bool bold, int* x, int* y) {
689 glEnable(GL_BLEND); // Allow us to draw on top of the animation
690 glBindTexture(GL_TEXTURE_2D, font.texture.name);
691
692 const int len = strlen(str);
693 const int strWidth = font.char_width * len;
694
695 if (*x == TEXT_CENTER_VALUE) {
696 *x = (mWidth - strWidth) / 2;
697 } else if (*x < 0) {
698 *x = mWidth + *x - strWidth;
699 }
700 if (*y == TEXT_CENTER_VALUE) {
701 *y = (mHeight - font.char_height) / 2;
702 } else if (*y < 0) {
703 *y = mHeight + *y - font.char_height;
704 }
705
706 int cropRect[4] = { 0, 0, font.char_width, -font.char_height };
707
708 for (int i = 0; i < len; i++) {
709 char c = str[i];
710
711 if (c < FONT_BEGIN_CHAR || c > FONT_END_CHAR) {
712 c = '?';
713 }
714
715 // Crop the texture to only the pixels in the current glyph
716 const int charPos = (c - FONT_BEGIN_CHAR); // Position in the list of valid characters
717 const int row = charPos / FONT_NUM_COLS;
718 const int col = charPos % FONT_NUM_COLS;
719 cropRect[0] = col * font.char_width; // Left of column
720 cropRect[1] = row * font.char_height * 2; // Top of row
721 // Move down to bottom of regular (one char_heigh) or bold (two char_heigh) line
722 cropRect[1] += bold ? 2 * font.char_height : font.char_height;
723 glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_CROP_RECT_OES, cropRect);
724
725 glDrawTexiOES(*x, *y, 0, font.char_width, font.char_height);
726
727 *x += font.char_width;
728 }
729
730 glDisable(GL_BLEND); // Return to the animation's default behaviour
731 glBindTexture(GL_TEXTURE_2D, 0);
732}
733
Damien Bargiacchi9071db12016-10-28 17:38:22 -0700734// We render 12 or 24 hour time.
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700735void BootAnimation::drawClock(const Font& font, const int xPos, const int yPos) {
Damien Bargiacchi9071db12016-10-28 17:38:22 -0700736 static constexpr char TIME_FORMAT_12[] = "%l:%M";
737 static constexpr char TIME_FORMAT_24[] = "%H:%M";
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700738 static constexpr int TIME_LENGTH = 6;
739
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800740 time_t rawtime;
741 time(&rawtime);
742 struct tm* timeInfo = localtime(&rawtime);
743
744 char timeBuff[TIME_LENGTH];
Damien Bargiacchi9071db12016-10-28 17:38:22 -0700745 const char* timeFormat = mTimeFormat12Hour ? TIME_FORMAT_12 : TIME_FORMAT_24;
746 size_t length = strftime(timeBuff, TIME_LENGTH, timeFormat, timeInfo);
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800747
748 if (length != TIME_LENGTH - 1) {
Wei Wang159a4102018-10-23 23:15:58 -0700749 SLOGE("Couldn't format time; abandoning boot animation clock");
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800750 mClockEnabled = false;
751 return;
752 }
753
Damien Bargiacchi45a76442016-12-05 18:02:18 -0800754 char* out = timeBuff[0] == ' ' ? &timeBuff[1] : &timeBuff[0];
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700755 int x = xPos;
756 int y = yPos;
Damien Bargiacchi45a76442016-12-05 18:02:18 -0800757 drawText(out, font, false, &x, &y);
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800758}
759
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700760bool BootAnimation::parseAnimationDesc(Animation& animation)
Mathias Agopiana8826d62009-10-01 03:10:14 -0700761{
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700762 String8 desString;
763
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700764 if (!readFile(animation.zip, "desc.txt", desString)) {
Narayan Kamathafd31e02013-12-03 13:16:03 +0000765 return false;
766 }
Mathias Agopiana8826d62009-10-01 03:10:14 -0700767 char const* s = desString.string();
768
Mathias Agopiana8826d62009-10-01 03:10:14 -0700769 // Parse the description file
770 for (;;) {
771 const char* endl = strstr(s, "\n");
Yi Kong911ac232019-03-24 01:49:02 -0700772 if (endl == nullptr) break;
Mathias Agopiana8826d62009-10-01 03:10:14 -0700773 String8 line(s, endl - s);
774 const char* l = line.string();
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800775 int fps = 0;
776 int width = 0;
777 int height = 0;
778 int count = 0;
779 int pause = 0;
Narayan Kamathafd31e02013-12-03 13:16:03 +0000780 char path[ANIM_ENTRY_NAME_MAX];
Jesse Hall083b84c2014-09-22 10:51:09 -0700781 char color[7] = "000000"; // default to black if unspecified
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700782 char clockPos1[TEXT_POS_LEN_MAX + 1] = "";
783 char clockPos2[TEXT_POS_LEN_MAX + 1] = "";
Jesse Hall083b84c2014-09-22 10:51:09 -0700784
Kevin Hesterd3782b22012-04-26 10:38:55 -0700785 char pathType;
Mathias Agopiana8826d62009-10-01 03:10:14 -0700786 if (sscanf(l, "%d %d %d", &width, &height, &fps) == 3) {
Wei Wang159a4102018-10-23 23:15:58 -0700787 // SLOGD("> w=%d, h=%d, fps=%d", width, height, fps);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700788 animation.width = width;
789 animation.height = height;
790 animation.fps = fps;
Kyeongkab.Nam35a8a112019-07-17 16:41:48 +0900791 } else if (sscanf(l, " %c %d %d %" STRTO(ANIM_PATH_MAX) "s #%6s %16s %16s",
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700792 &pathType, &count, &pause, path, color, clockPos1, clockPos2) >= 4) {
Wei Wang159a4102018-10-23 23:15:58 -0700793 //SLOGD("> type=%c, count=%d, pause=%d, path=%s, color=%s, clockPos1=%s, clockPos2=%s",
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700794 // pathType, count, pause, path, color, clockPos1, clockPos2);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700795 Animation::Part part;
Kevin Hesterd3782b22012-04-26 10:38:55 -0700796 part.playUntilComplete = pathType == 'c';
Mathias Agopiana8826d62009-10-01 03:10:14 -0700797 part.count = count;
798 part.pause = pause;
799 part.path = path;
Yi Kong911ac232019-03-24 01:49:02 -0700800 part.audioData = nullptr;
801 part.animation = nullptr;
Jesse Hall083b84c2014-09-22 10:51:09 -0700802 if (!parseColor(color, part.backgroundColor)) {
Wei Wang159a4102018-10-23 23:15:58 -0700803 SLOGE("> invalid color '#%s'", color);
Jesse Hall083b84c2014-09-22 10:51:09 -0700804 part.backgroundColor[0] = 0.0f;
805 part.backgroundColor[1] = 0.0f;
806 part.backgroundColor[2] = 0.0f;
807 }
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700808 parsePosition(clockPos1, clockPos2, &part.clockPosX, &part.clockPosY);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700809 animation.parts.add(part);
810 }
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700811 else if (strcmp(l, "$SYSTEM") == 0) {
Wei Wang159a4102018-10-23 23:15:58 -0700812 // SLOGD("> SYSTEM");
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700813 Animation::Part part;
814 part.playUntilComplete = false;
815 part.count = 1;
816 part.pause = 0;
Yi Kong911ac232019-03-24 01:49:02 -0700817 part.audioData = nullptr;
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700818 part.animation = loadAnimation(String8(SYSTEM_BOOTANIMATION_FILE));
Yi Kong911ac232019-03-24 01:49:02 -0700819 if (part.animation != nullptr)
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700820 animation.parts.add(part);
821 }
Mathias Agopiana8826d62009-10-01 03:10:14 -0700822 s = ++endl;
823 }
824
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700825 return true;
826}
827
828bool BootAnimation::preloadZip(Animation& animation)
829{
Mathias Agopiana8826d62009-10-01 03:10:14 -0700830 // read all the data structures
831 const size_t pcount = animation.parts.size();
Yi Kong911ac232019-03-24 01:49:02 -0700832 void *cookie = nullptr;
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400833 ZipFileRO* zip = animation.zip;
834 if (!zip->startIteration(&cookie)) {
Narayan Kamathafd31e02013-12-03 13:16:03 +0000835 return false;
836 }
837
838 ZipEntryRO entry;
839 char name[ANIM_ENTRY_NAME_MAX];
Yi Kong911ac232019-03-24 01:49:02 -0700840 while ((entry = zip->nextEntry(cookie)) != nullptr) {
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400841 const int foundEntryName = zip->getEntryFileName(entry, name, ANIM_ENTRY_NAME_MAX);
Narayan Kamathafd31e02013-12-03 13:16:03 +0000842 if (foundEntryName > ANIM_ENTRY_NAME_MAX || foundEntryName == -1) {
Wei Wang159a4102018-10-23 23:15:58 -0700843 SLOGE("Error fetching entry file name");
Narayan Kamathafd31e02013-12-03 13:16:03 +0000844 continue;
845 }
846
847 const String8 entryName(name);
848 const String8 path(entryName.getPathDir());
849 const String8 leaf(entryName.getPathLeaf());
850 if (leaf.size() > 0) {
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700851 if (entryName == CLOCK_FONT_ZIP_NAME) {
852 FileMap* map = zip->createEntryFileMap(entry);
853 if (map) {
854 animation.clockFont.map = map;
855 }
856 continue;
857 }
858
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400859 for (size_t j = 0; j < pcount; j++) {
Narayan Kamathafd31e02013-12-03 13:16:03 +0000860 if (path == animation.parts[j].path) {
Narayan Kamath4600dd02015-06-16 12:02:57 +0100861 uint16_t method;
Narayan Kamathafd31e02013-12-03 13:16:03 +0000862 // supports only stored png files
Yi Kong911ac232019-03-24 01:49:02 -0700863 if (zip->getEntryInfo(entry, &method, nullptr, nullptr, nullptr, nullptr, nullptr)) {
Narayan Kamathafd31e02013-12-03 13:16:03 +0000864 if (method == ZipFileRO::kCompressStored) {
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400865 FileMap* map = zip->createEntryFileMap(entry);
Narayan Kamathafd31e02013-12-03 13:16:03 +0000866 if (map) {
Narayan Kamathafd31e02013-12-03 13:16:03 +0000867 Animation::Part& part(animation.parts.editItemAt(j));
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700868 if (leaf == "audio.wav") {
869 // a part may have at most one audio file
Geoffrey Pitschd6d9a1d2016-06-08 00:38:58 -0700870 part.audioData = (uint8_t *)map->getDataPtr();
871 part.audioLength = map->getDataLength();
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400872 } else if (leaf == "trim.txt") {
873 part.trimData.setTo((char const*)map->getDataPtr(),
874 map->getDataLength());
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700875 } else {
876 Animation::Frame frame;
877 frame.name = leaf;
878 frame.map = map;
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400879 frame.trimWidth = animation.width;
880 frame.trimHeight = animation.height;
881 frame.trimX = 0;
882 frame.trimY = 0;
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700883 part.frames.add(frame);
884 }
Mathias Agopiana8826d62009-10-01 03:10:14 -0700885 }
Geoffrey Pitschd6d9a1d2016-06-08 00:38:58 -0700886 } else {
Wei Wang159a4102018-10-23 23:15:58 -0700887 SLOGE("bootanimation.zip is compressed; must be only stored");
Mathias Agopiana8826d62009-10-01 03:10:14 -0700888 }
889 }
890 }
891 }
892 }
893 }
894
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400895 // If there is trimData present, override the positioning defaults.
896 for (Animation::Part& part : animation.parts) {
897 const char* trimDataStr = part.trimData.string();
898 for (size_t frameIdx = 0; frameIdx < part.frames.size(); frameIdx++) {
899 const char* endl = strstr(trimDataStr, "\n");
900 // No more trimData for this part.
Yi Kong911ac232019-03-24 01:49:02 -0700901 if (endl == nullptr) {
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400902 break;
903 }
904 String8 line(trimDataStr, endl - trimDataStr);
905 const char* lineStr = line.string();
906 trimDataStr = ++endl;
907 int width = 0, height = 0, x = 0, y = 0;
908 if (sscanf(lineStr, "%dx%d+%d+%d", &width, &height, &x, &y) == 4) {
909 Animation::Frame& frame(part.frames.editItemAt(frameIdx));
910 frame.trimWidth = width;
911 frame.trimHeight = height;
912 frame.trimX = x;
913 frame.trimY = y;
914 } else {
Wei Wang159a4102018-10-23 23:15:58 -0700915 SLOGE("Error parsing trim.txt, line: %s", lineStr);
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400916 break;
917 }
918 }
919 }
920
921 zip->endIteration(cookie);
Narayan Kamathafd31e02013-12-03 13:16:03 +0000922
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700923 return true;
924}
925
926bool BootAnimation::movie()
927{
Huihong Luo50a2f362018-08-11 09:27:57 -0700928 if (mAnimation == nullptr) {
929 mAnimation = loadAnimation(mZipFileName);
930 }
931
932 if (mAnimation == nullptr)
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700933 return false;
934
Huihong Luo50a2f362018-08-11 09:27:57 -0700935 // mCallbacks->init() may get called recursively,
936 // this loop is needed to get the same results
937 for (const Animation::Part& part : mAnimation->parts) {
938 if (part.animation != nullptr) {
939 mCallbacks->init(part.animation->parts);
940 }
941 }
942 mCallbacks->init(mAnimation->parts);
943
Damien Bargiacchi97480862016-03-29 14:55:55 -0700944 bool anyPartHasClock = false;
Huihong Luo50a2f362018-08-11 09:27:57 -0700945 for (size_t i=0; i < mAnimation->parts.size(); i++) {
946 if(validClock(mAnimation->parts[i])) {
Damien Bargiacchi97480862016-03-29 14:55:55 -0700947 anyPartHasClock = true;
948 break;
949 }
950 }
951 if (!anyPartHasClock) {
952 mClockEnabled = false;
953 }
954
Sai Kiran Korwar27167492015-07-07 20:00:06 +0530955 // Check if npot textures are supported
956 mUseNpotTextures = false;
957 String8 gl_extensions;
958 const char* exts = reinterpret_cast<const char*>(glGetString(GL_EXTENSIONS));
959 if (!exts) {
960 glGetError();
961 } else {
962 gl_extensions.setTo(exts);
963 if ((gl_extensions.find("GL_ARB_texture_non_power_of_two") != -1) ||
964 (gl_extensions.find("GL_OES_texture_npot") != -1)) {
965 mUseNpotTextures = true;
966 }
967 }
968
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800969 // Blend required to draw time on top of animation frames.
970 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700971 glShadeModel(GL_FLAT);
972 glDisable(GL_DITHER);
973 glDisable(GL_SCISSOR_TEST);
974 glDisable(GL_BLEND);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700975
976 glBindTexture(GL_TEXTURE_2D, 0);
977 glEnable(GL_TEXTURE_2D);
978 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
979 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
980 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
981 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
982 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
983
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700984 bool clockFontInitialized = false;
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800985 if (mClockEnabled) {
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700986 clockFontInitialized =
Huihong Luo50a2f362018-08-11 09:27:57 -0700987 (initFont(&mAnimation->clockFont, CLOCK_FONT_ASSET) == NO_ERROR);
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700988 mClockEnabled = clockFontInitialized;
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800989 }
990
Damien Bargiacchi97480862016-03-29 14:55:55 -0700991 if (mClockEnabled && !updateIsTimeAccurate()) {
992 mTimeCheckThread = new TimeCheckThread(this);
993 mTimeCheckThread->run("BootAnimation::TimeCheckThread", PRIORITY_NORMAL);
994 }
995
Huihong Luo50a2f362018-08-11 09:27:57 -0700996 playAnimation(*mAnimation);
Damien Bargiacchi97480862016-03-29 14:55:55 -0700997
Geoffrey Pitscha9173532016-07-19 14:56:39 -0400998 if (mTimeCheckThread != nullptr) {
Damien Bargiacchi97480862016-03-29 14:55:55 -0700999 mTimeCheckThread->requestExit();
Geoffrey Pitscha9173532016-07-19 14:56:39 -04001000 mTimeCheckThread = nullptr;
1001 }
1002
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -07001003 if (clockFontInitialized) {
Huihong Luo50a2f362018-08-11 09:27:57 -07001004 glDeleteTextures(1, &mAnimation->clockFont.texture.name);
Andriy Naborskyy815e51d2016-03-24 16:43:34 -07001005 }
1006
Huihong Luo50a2f362018-08-11 09:27:57 -07001007 releaseAnimation(mAnimation);
1008 mAnimation = nullptr;
Damien Bargiacchi1a8a2132018-07-24 15:20:26 -07001009
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001010 return false;
1011}
1012
1013bool BootAnimation::playAnimation(const Animation& animation)
1014{
1015 const size_t pcount = animation.parts.size();
Mathias Agopiana8826d62009-10-01 03:10:14 -07001016 nsecs_t frameDuration = s2ns(1) / animation.fps;
Geoffrey Pitschdd214a72016-06-27 17:14:30 -04001017 const int animationX = (mWidth - animation.width) / 2;
1018 const int animationY = (mHeight - animation.height) / 2;
Mathias Agopian9f3020d2009-11-06 16:30:18 -08001019
Wei Wang159a4102018-10-23 23:15:58 -07001020 SLOGD("%sAnimationShownTiming start time: %" PRId64 "ms", mShuttingDown ? "Shutdown" : "Boot",
Keun-young Parkd1794cd2017-04-04 12:21:19 -07001021 elapsedRealtime());
Narayan Kamathafd31e02013-12-03 13:16:03 +00001022 for (size_t i=0 ; i<pcount ; i++) {
Mathias Agopiana8826d62009-10-01 03:10:14 -07001023 const Animation::Part& part(animation.parts[i]);
1024 const size_t fcount = part.frames.size();
1025 glBindTexture(GL_TEXTURE_2D, 0);
1026
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001027 // Handle animation package
Yi Kong911ac232019-03-24 01:49:02 -07001028 if (part.animation != nullptr) {
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001029 playAnimation(*part.animation);
1030 if (exitPending())
1031 break;
1032 continue; //to next part
1033 }
1034
Mathias Agopiana8826d62009-10-01 03:10:14 -07001035 for (int r=0 ; !part.count || r<part.count ; r++) {
Kevin Hesterd3782b22012-04-26 10:38:55 -07001036 // Exit any non playuntil complete parts immediately
1037 if(exitPending() && !part.playUntilComplete)
1038 break;
1039
Ed Coyne7464ac92017-06-08 12:26:48 -07001040 mCallbacks->playPart(i, part, r);
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -07001041
Jesse Hall083b84c2014-09-22 10:51:09 -07001042 glClearColor(
1043 part.backgroundColor[0],
1044 part.backgroundColor[1],
1045 part.backgroundColor[2],
1046 1.0f);
1047
Narayan Kamathafd31e02013-12-03 13:16:03 +00001048 for (size_t j=0 ; j<fcount && (!exitPending() || part.playUntilComplete) ; j++) {
Mathias Agopiana8826d62009-10-01 03:10:14 -07001049 const Animation::Frame& frame(part.frames[j]);
Mathias Agopiandb7dd2a2012-05-12 15:08:21 -07001050 nsecs_t lastFrame = systemTime();
Mathias Agopiana8826d62009-10-01 03:10:14 -07001051
1052 if (r > 0) {
1053 glBindTexture(GL_TEXTURE_2D, frame.tid);
1054 } else {
1055 if (part.count != 1) {
1056 glGenTextures(1, &frame.tid);
1057 glBindTexture(GL_TEXTURE_2D, frame.tid);
1058 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
1059 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
1060 }
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -07001061 int w, h;
1062 initTexture(frame.map, &w, &h);
Mathias Agopiana8826d62009-10-01 03:10:14 -07001063 }
1064
Geoffrey Pitschdd214a72016-06-27 17:14:30 -04001065 const int xc = animationX + frame.trimX;
1066 const int yc = animationY + frame.trimY;
1067 Region clearReg(Rect(mWidth, mHeight));
1068 clearReg.subtractSelf(Rect(xc, yc, xc+frame.trimWidth, yc+frame.trimHeight));
Mathias Agopian9f3020d2009-11-06 16:30:18 -08001069 if (!clearReg.isEmpty()) {
1070 Region::const_iterator head(clearReg.begin());
1071 Region::const_iterator tail(clearReg.end());
1072 glEnable(GL_SCISSOR_TEST);
1073 while (head != tail) {
Andreas Gampecfedceb2014-09-30 21:48:18 -07001074 const Rect& r2(*head++);
Geoffrey Pitschdd214a72016-06-27 17:14:30 -04001075 glScissor(r2.left, mHeight - r2.bottom, r2.width(), r2.height());
Mathias Agopian9f3020d2009-11-06 16:30:18 -08001076 glClear(GL_COLOR_BUFFER_BIT);
1077 }
1078 glDisable(GL_SCISSOR_TEST);
1079 }
Geoffrey Pitschdd214a72016-06-27 17:14:30 -04001080 // specify the y center as ceiling((mHeight - frame.trimHeight) / 2)
1081 // which is equivalent to mHeight - (yc + frame.trimHeight)
1082 glDrawTexiOES(xc, mHeight - (yc + frame.trimHeight),
1083 0, frame.trimWidth, frame.trimHeight);
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -07001084 if (mClockEnabled && mTimeIsAccurate && validClock(part)) {
1085 drawClock(animation.clockFont, part.clockPosX, part.clockPosY);
Damien Bargiacchia704b7d2016-02-16 16:55:49 -08001086 }
Adrian Roos9ee5dff2018-08-22 20:19:49 +02001087 handleViewport(frameDuration);
Damien Bargiacchia704b7d2016-02-16 16:55:49 -08001088
Mathias Agopiana8826d62009-10-01 03:10:14 -07001089 eglSwapBuffers(mDisplay, mSurface);
1090
1091 nsecs_t now = systemTime();
1092 nsecs_t delay = frameDuration - (now - lastFrame);
Wei Wang159a4102018-10-23 23:15:58 -07001093 //SLOGD("%lld, %lld", ns2ms(now - lastFrame), ns2ms(delay));
Mathias Agopiana8826d62009-10-01 03:10:14 -07001094 lastFrame = now;
Mathias Agopiandb7dd2a2012-05-12 15:08:21 -07001095
1096 if (delay > 0) {
1097 struct timespec spec;
1098 spec.tv_sec = (now + delay) / 1000000000;
1099 spec.tv_nsec = (now + delay) % 1000000000;
1100 int err;
1101 do {
Yi Kong911ac232019-03-24 01:49:02 -07001102 err = clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &spec, nullptr);
Mathias Agopiandb7dd2a2012-05-12 15:08:21 -07001103 } while (err<0 && errno == EINTR);
1104 }
Kevin Hesterd3782b22012-04-26 10:38:55 -07001105
1106 checkExit();
Mathias Agopiana8826d62009-10-01 03:10:14 -07001107 }
Kevin Hesterd3782b22012-04-26 10:38:55 -07001108
Mathias Agopiana8826d62009-10-01 03:10:14 -07001109 usleep(part.pause * ns2us(frameDuration));
Kevin Hesterd3782b22012-04-26 10:38:55 -07001110
1111 // For infinite parts, we've now played them at least once, so perhaps exit
Adrian Roos9ee5dff2018-08-22 20:19:49 +02001112 if(exitPending() && !part.count && mCurrentInset >= mTargetInset)
Kevin Hesterd3782b22012-04-26 10:38:55 -07001113 break;
Mathias Agopiana8826d62009-10-01 03:10:14 -07001114 }
1115
Geoffrey Pitsch2fb30fb2016-07-06 16:16:20 -04001116 }
1117
1118 // Free textures created for looping parts now that the animation is done.
1119 for (const Animation::Part& part : animation.parts) {
Mathias Agopiana8826d62009-10-01 03:10:14 -07001120 if (part.count != 1) {
Geoffrey Pitsch2fb30fb2016-07-06 16:16:20 -04001121 const size_t fcount = part.frames.size();
1122 for (size_t j = 0; j < fcount; j++) {
Mathias Agopiana8826d62009-10-01 03:10:14 -07001123 const Animation::Frame& frame(part.frames[j]);
1124 glDeleteTextures(1, &frame.tid);
1125 }
1126 }
1127 }
Geoffrey Pitschd6d9a1d2016-06-08 00:38:58 -07001128
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001129 return true;
Mathias Agopiana8826d62009-10-01 03:10:14 -07001130}
1131
Adrian Roos9ee5dff2018-08-22 20:19:49 +02001132void BootAnimation::handleViewport(nsecs_t timestep) {
1133 if (mShuttingDown || !mFlingerSurfaceControl || mTargetInset == 0) {
1134 return;
1135 }
1136 if (mTargetInset < 0) {
1137 // Poll the amount for the top display inset. This will return -1 until persistent properties
1138 // have been loaded.
1139 mTargetInset = android::base::GetIntProperty("persist.sys.displayinset.top",
1140 -1 /* default */, -1 /* min */, mHeight / 2 /* max */);
1141 }
1142 if (mTargetInset <= 0) {
1143 return;
1144 }
1145
1146 if (mCurrentInset < mTargetInset) {
1147 // After the device boots, the inset will effectively be cropped away. We animate this here.
1148 float fraction = static_cast<float>(mCurrentInset) / mTargetInset;
1149 int interpolatedInset = (cosf((fraction + 1) * M_PI) / 2.0f + 0.5f) * mTargetInset;
1150
1151 SurfaceComposerClient::Transaction()
1152 .setCrop(mFlingerSurfaceControl, Rect(0, interpolatedInset, mWidth, mHeight))
1153 .apply();
1154 } else {
1155 // At the end of the animation, we switch to the viewport that DisplayManager will apply
1156 // later. This changes the coordinate system, and means we must move the surface up by
1157 // the inset amount.
Adrian Roos9ee5dff2018-08-22 20:19:49 +02001158 Rect layerStackRect(0, 0, mWidth, mHeight - mTargetInset);
1159 Rect displayRect(0, mTargetInset, mWidth, mHeight);
1160
1161 SurfaceComposerClient::Transaction t;
1162 t.setPosition(mFlingerSurfaceControl, 0, -mTargetInset)
1163 .setCrop(mFlingerSurfaceControl, Rect(0, mTargetInset, mWidth, mHeight));
Dominik Laskowski2a3d9aa2019-11-18 20:26:39 -08001164 t.setDisplayProjection(mDisplayToken, ui::ROTATION_0, layerStackRect, displayRect);
Adrian Roos9ee5dff2018-08-22 20:19:49 +02001165 t.apply();
1166
1167 mTargetInset = mCurrentInset = 0;
1168 }
1169
1170 int delta = timestep * mTargetInset / ms2ns(200);
1171 mCurrentInset += delta;
1172}
1173
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001174void BootAnimation::releaseAnimation(Animation* animation) const
1175{
1176 for (Vector<Animation::Part>::iterator it = animation->parts.begin(),
1177 e = animation->parts.end(); it != e; ++it) {
1178 if (it->animation)
1179 releaseAnimation(it->animation);
1180 }
1181 if (animation->zip)
1182 delete animation->zip;
1183 delete animation;
1184}
1185
1186BootAnimation::Animation* BootAnimation::loadAnimation(const String8& fn)
1187{
1188 if (mLoadedFiles.indexOf(fn) >= 0) {
Wei Wang159a4102018-10-23 23:15:58 -07001189 SLOGE("File \"%s\" is already loaded. Cyclic ref is not allowed",
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001190 fn.string());
Yi Kong911ac232019-03-24 01:49:02 -07001191 return nullptr;
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001192 }
1193 ZipFileRO *zip = ZipFileRO::open(fn);
Yi Kong911ac232019-03-24 01:49:02 -07001194 if (zip == nullptr) {
Wei Wang159a4102018-10-23 23:15:58 -07001195 SLOGE("Failed to open animation zip \"%s\": %s",
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001196 fn.string(), strerror(errno));
Yi Kong911ac232019-03-24 01:49:02 -07001197 return nullptr;
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001198 }
1199
1200 Animation *animation = new Animation;
1201 animation->fileName = fn;
1202 animation->zip = zip;
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -07001203 animation->clockFont.map = nullptr;
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001204 mLoadedFiles.add(animation->fileName);
1205
1206 parseAnimationDesc(*animation);
Geoffrey Pitscha91a2d72016-07-12 14:46:19 -04001207 if (!preloadZip(*animation)) {
Greg Kaiser7426ec82019-09-11 14:34:27 -07001208 releaseAnimation(animation);
Yi Kong911ac232019-03-24 01:49:02 -07001209 return nullptr;
Geoffrey Pitscha91a2d72016-07-12 14:46:19 -04001210 }
1211
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001212 mLoadedFiles.remove(fn);
1213 return animation;
1214}
Damien Bargiacchi97480862016-03-29 14:55:55 -07001215
1216bool BootAnimation::updateIsTimeAccurate() {
1217 static constexpr long long MAX_TIME_IN_PAST = 60000LL * 60LL * 24LL * 30LL; // 30 days
1218 static constexpr long long MAX_TIME_IN_FUTURE = 60000LL * 90LL; // 90 minutes
1219
1220 if (mTimeIsAccurate) {
1221 return true;
1222 }
Keun-young Parkb5938422017-03-23 13:46:24 -07001223 if (mShuttingDown) return true;
Damien Bargiacchi97480862016-03-29 14:55:55 -07001224 struct stat statResult;
Damien Bargiacchi9071db12016-10-28 17:38:22 -07001225
1226 if(stat(TIME_FORMAT_12_HOUR_FLAG_FILE_PATH, &statResult) == 0) {
1227 mTimeFormat12Hour = true;
1228 }
1229
Damien Bargiacchi97480862016-03-29 14:55:55 -07001230 if(stat(ACCURATE_TIME_FLAG_FILE_PATH, &statResult) == 0) {
1231 mTimeIsAccurate = true;
1232 return true;
1233 }
1234
1235 FILE* file = fopen(LAST_TIME_CHANGED_FILE_PATH, "r");
Yi Kong911ac232019-03-24 01:49:02 -07001236 if (file != nullptr) {
Damien Bargiacchi97480862016-03-29 14:55:55 -07001237 long long lastChangedTime = 0;
1238 fscanf(file, "%lld", &lastChangedTime);
1239 fclose(file);
1240 if (lastChangedTime > 0) {
1241 struct timespec now;
1242 clock_gettime(CLOCK_REALTIME, &now);
1243 // Match the Java timestamp format
1244 long long rtcNow = (now.tv_sec * 1000LL) + (now.tv_nsec / 1000000LL);
Damien Bargiacchi96762812016-07-12 15:53:40 -07001245 if (ACCURATE_TIME_EPOCH < rtcNow
1246 && lastChangedTime > (rtcNow - MAX_TIME_IN_PAST)
1247 && lastChangedTime < (rtcNow + MAX_TIME_IN_FUTURE)) {
Damien Bargiacchi97480862016-03-29 14:55:55 -07001248 mTimeIsAccurate = true;
1249 }
1250 }
1251 }
1252
1253 return mTimeIsAccurate;
1254}
1255
1256BootAnimation::TimeCheckThread::TimeCheckThread(BootAnimation* bootAnimation) : Thread(false),
1257 mInotifyFd(-1), mSystemWd(-1), mTimeWd(-1), mBootAnimation(bootAnimation) {}
1258
1259BootAnimation::TimeCheckThread::~TimeCheckThread() {
1260 // mInotifyFd may be -1 but that's ok since we're not at risk of attempting to close a valid FD.
1261 close(mInotifyFd);
1262}
1263
1264bool BootAnimation::TimeCheckThread::threadLoop() {
1265 bool shouldLoop = doThreadLoop() && !mBootAnimation->mTimeIsAccurate
1266 && mBootAnimation->mClockEnabled;
1267 if (!shouldLoop) {
1268 close(mInotifyFd);
1269 mInotifyFd = -1;
1270 }
1271 return shouldLoop;
1272}
1273
1274bool BootAnimation::TimeCheckThread::doThreadLoop() {
1275 static constexpr int BUFF_LEN (10 * (sizeof(struct inotify_event) + NAME_MAX + 1));
1276
1277 // Poll instead of doing a blocking read so the Thread can exit if requested.
1278 struct pollfd pfd = { mInotifyFd, POLLIN, 0 };
1279 ssize_t pollResult = poll(&pfd, 1, 1000);
1280
1281 if (pollResult == 0) {
1282 return true;
1283 } else if (pollResult < 0) {
Wei Wang159a4102018-10-23 23:15:58 -07001284 SLOGE("Could not poll inotify events");
Damien Bargiacchi97480862016-03-29 14:55:55 -07001285 return false;
1286 }
1287
1288 char buff[BUFF_LEN] __attribute__ ((aligned(__alignof__(struct inotify_event))));;
1289 ssize_t length = read(mInotifyFd, buff, BUFF_LEN);
1290 if (length == 0) {
1291 return true;
1292 } else if (length < 0) {
Wei Wang159a4102018-10-23 23:15:58 -07001293 SLOGE("Could not read inotify events");
Damien Bargiacchi97480862016-03-29 14:55:55 -07001294 return false;
1295 }
1296
1297 const struct inotify_event *event;
1298 for (char* ptr = buff; ptr < buff + length; ptr += sizeof(struct inotify_event) + event->len) {
1299 event = (const struct inotify_event *) ptr;
1300 if (event->wd == mSystemWd && strcmp(SYSTEM_TIME_DIR_NAME, event->name) == 0) {
1301 addTimeDirWatch();
1302 } else if (event->wd == mTimeWd && (strcmp(LAST_TIME_CHANGED_FILE_NAME, event->name) == 0
1303 || strcmp(ACCURATE_TIME_FLAG_FILE_NAME, event->name) == 0)) {
1304 return !mBootAnimation->updateIsTimeAccurate();
1305 }
1306 }
1307
1308 return true;
1309}
1310
1311void BootAnimation::TimeCheckThread::addTimeDirWatch() {
1312 mTimeWd = inotify_add_watch(mInotifyFd, SYSTEM_TIME_DIR_PATH,
1313 IN_CLOSE_WRITE | IN_MOVED_TO | IN_ATTRIB);
1314 if (mTimeWd > 0) {
1315 // No need to watch for the time directory to be created if it already exists
1316 inotify_rm_watch(mInotifyFd, mSystemWd);
1317 mSystemWd = -1;
1318 }
1319}
1320
1321status_t BootAnimation::TimeCheckThread::readyToRun() {
1322 mInotifyFd = inotify_init();
1323 if (mInotifyFd < 0) {
Wei Wang159a4102018-10-23 23:15:58 -07001324 SLOGE("Could not initialize inotify fd");
Damien Bargiacchi97480862016-03-29 14:55:55 -07001325 return NO_INIT;
1326 }
1327
1328 mSystemWd = inotify_add_watch(mInotifyFd, SYSTEM_DATA_DIR_PATH, IN_CREATE | IN_ATTRIB);
1329 if (mSystemWd < 0) {
1330 close(mInotifyFd);
1331 mInotifyFd = -1;
Wei Wang159a4102018-10-23 23:15:58 -07001332 SLOGE("Could not add watch for %s", SYSTEM_DATA_DIR_PATH);
Damien Bargiacchi97480862016-03-29 14:55:55 -07001333 return NO_INIT;
1334 }
1335
1336 addTimeDirWatch();
1337
1338 if (mBootAnimation->updateIsTimeAccurate()) {
1339 close(mInotifyFd);
1340 mInotifyFd = -1;
1341 return ALREADY_EXISTS;
1342 }
1343
1344 return NO_ERROR;
1345}
1346
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001347// ---------------------------------------------------------------------------
1348
1349}
1350; // namespace android