blob: 6e27aff5ae7b886626f4fb6360a4290bf1c82521 [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
Marin Shalamanovcb783ca2021-01-29 21:26:07 +010046#include <ui/DisplayMode.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>
Marin Shalamanov9070c052020-05-18 19:05:17 +020052#include <gui/DisplayEventReceiver.h>
Mathias Agopian8335f1c2012-02-25 18:48:35 -080053#include <gui/Surface.h>
54#include <gui/SurfaceComposerClient.h>
Lucas Dupin8eedc022021-07-29 21:49:07 +000055#include <GLES2/gl2.h>
56#include <GLES2/gl2ext.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080057#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
Marin Shalamanovcb783ca2021-01-29 21:26:07 +010067using ui::DisplayMode;
68
Damien Bargiacchi97480862016-03-29 14:55:55 -070069static const char OEM_BOOTANIMATION_FILE[] = "/oem/media/bootanimation.zip";
Beverly34ffe252019-05-17 11:03:54 -040070static const char PRODUCT_BOOTANIMATION_DARK_FILE[] = "/product/media/bootanimation-dark.zip";
Jaekyun Seokc521bb32018-01-30 11:52:47 +090071static const char PRODUCT_BOOTANIMATION_FILE[] = "/product/media/bootanimation.zip";
Damien Bargiacchi97480862016-03-29 14:55:55 -070072static const char SYSTEM_BOOTANIMATION_FILE[] = "/system/media/bootanimation.zip";
Anders Fridlunde0b4d232018-11-06 14:23:25 +010073static const char APEX_BOOTANIMATION_FILE[] = "/apex/com.android.bootanimation/etc/bootanimation.zip";
Jaekyun Seokc521bb32018-01-30 11:52:47 +090074static const char PRODUCT_ENCRYPTED_BOOTANIMATION_FILE[] = "/product/media/bootanimation-encrypted.zip";
Damien Bargiacchi97480862016-03-29 14:55:55 -070075static const char SYSTEM_ENCRYPTED_BOOTANIMATION_FILE[] = "/system/media/bootanimation-encrypted.zip";
Keun-young Parkb5938422017-03-23 13:46:24 -070076static const char OEM_SHUTDOWNANIMATION_FILE[] = "/oem/media/shutdownanimation.zip";
Jaekyun Seokc521bb32018-01-30 11:52:47 +090077static const char PRODUCT_SHUTDOWNANIMATION_FILE[] = "/product/media/shutdownanimation.zip";
Keun-young Parkb5938422017-03-23 13:46:24 -070078static const char SYSTEM_SHUTDOWNANIMATION_FILE[] = "/system/media/shutdownanimation.zip";
79
Nikita Ioffe06c986e2020-01-27 17:16:03 +000080static constexpr const char* PRODUCT_USERSPACE_REBOOT_ANIMATION_FILE = "/product/media/userspace-reboot.zip";
81static constexpr const char* OEM_USERSPACE_REBOOT_ANIMATION_FILE = "/oem/media/userspace-reboot.zip";
82static constexpr const char* SYSTEM_USERSPACE_REBOOT_ANIMATION_FILE = "/system/media/userspace-reboot.zip";
83
Damien Bargiacchi97480862016-03-29 14:55:55 -070084static const char SYSTEM_DATA_DIR_PATH[] = "/data/system";
85static const char SYSTEM_TIME_DIR_NAME[] = "time";
86static const char SYSTEM_TIME_DIR_PATH[] = "/data/system/time";
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -070087static const char CLOCK_FONT_ASSET[] = "images/clock_font.png";
88static const char CLOCK_FONT_ZIP_NAME[] = "clock_font.png";
Nicolas Geoffray41fffb42020-12-15 10:50:29 +000089static const char PROGRESS_FONT_ASSET[] = "images/progress_font.png";
90static const char PROGRESS_FONT_ZIP_NAME[] = "progress_font.png";
Damien Bargiacchi97480862016-03-29 14:55:55 -070091static const char LAST_TIME_CHANGED_FILE_NAME[] = "last_time_change";
92static const char LAST_TIME_CHANGED_FILE_PATH[] = "/data/system/time/last_time_change";
93static const char ACCURATE_TIME_FLAG_FILE_NAME[] = "time_is_accurate";
94static const char ACCURATE_TIME_FLAG_FILE_PATH[] = "/data/system/time/time_is_accurate";
Damien Bargiacchi9071db12016-10-28 17:38:22 -070095static const char TIME_FORMAT_12_HOUR_FLAG_FILE_PATH[] = "/data/system/time/time_format_12_hour";
Damien Bargiacchi96762812016-07-12 15:53:40 -070096// Java timestamp format. Don't show the clock if the date is before 2000-01-01 00:00:00.
97static const long long ACCURATE_TIME_EPOCH = 946684800000;
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -070098static constexpr char FONT_BEGIN_CHAR = ' ';
99static constexpr char FONT_END_CHAR = '~' + 1;
100static constexpr size_t FONT_NUM_CHARS = FONT_END_CHAR - FONT_BEGIN_CHAR + 1;
101static constexpr size_t FONT_NUM_COLS = 16;
102static constexpr size_t FONT_NUM_ROWS = FONT_NUM_CHARS / FONT_NUM_COLS;
103static const int TEXT_CENTER_VALUE = INT_MAX;
104static const int TEXT_MISSING_VALUE = INT_MIN;
Damien Bargiacchi97480862016-03-29 14:55:55 -0700105static const char EXIT_PROP_NAME[] = "service.bootanim.exit";
Nicolas Geoffray41fffb42020-12-15 10:50:29 +0000106static const char PROGRESS_PROP_NAME[] = "service.bootanim.progress";
Huihong Luod4a217e2020-01-16 15:56:33 -0800107static const char DISPLAYS_PROP_NAME[] = "persist.service.bootanim.displays";
Kyeongkab.Nam35a8a112019-07-17 16:41:48 +0900108static const int ANIM_ENTRY_NAME_MAX = ANIM_PATH_MAX + 1;
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700109static constexpr size_t TEXT_POS_LEN_MAX = 16;
Lucas Dupin8eedc022021-07-29 21:49:07 +0000110static const char U_TEXTURE[] = "uTexture";
111static const char U_FADE[] = "uFade";
112static const char U_CROP_AREA[] = "uCropArea";
113static const char A_UV[] = "aUv";
114static const char A_POSITION[] = "aPosition";
115static const char VERTEX_SHADER_SOURCE[] = R"(
116 precision mediump float;
117 attribute vec4 aPosition;
118 attribute highp vec2 aUv;
119 varying highp vec2 vUv;
120 void main() {
121 gl_Position = aPosition;
122 vUv = aUv;
123 })";
124static const char IMAGE_FRAG_SHADER_SOURCE[] = R"(
125 precision mediump float;
126 uniform sampler2D uTexture;
127 uniform float uFade;
128 varying highp vec2 vUv;
129 void main() {
130 vec4 color = texture2D(uTexture, vUv);
131 gl_FragColor = vec4(color.x, color.y, color.z, 1.0 - uFade);
132 })";
133static const char TEXT_FRAG_SHADER_SOURCE[] = R"(
134 precision mediump float;
135 uniform sampler2D uTexture;
136 uniform vec4 uCropArea;
137 varying highp vec2 vUv;
138 void main() {
139 vec2 uv = vec2(mix(uCropArea.x, uCropArea.z, vUv.x),
140 mix(uCropArea.y, uCropArea.w, vUv.y));
141 gl_FragColor = texture2D(uTexture, uv);
142 })";
143
144static GLfloat quadPositions[] = {
145 -0.5f, -0.5f,
146 +0.5f, -0.5f,
147 +0.5f, +0.5f,
148 +0.5f, +0.5f,
149 -0.5f, +0.5f,
150 -0.5f, -0.5f
151};
152static GLfloat quadUVs[] = {
153 0.0f, 1.0f,
154 1.0f, 1.0f,
155 1.0f, 0.0f,
156 1.0f, 0.0f,
157 0.0f, 0.0f,
158 0.0f, 1.0f
159};
Narayan Kamathafd31e02013-12-03 13:16:03 +0000160
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800161// ---------------------------------------------------------------------------
162
Ed Coyne7464ac92017-06-08 12:26:48 -0700163BootAnimation::BootAnimation(sp<Callbacks> callbacks)
Marin Shalamanov9070c052020-05-18 19:05:17 +0200164 : Thread(false), mLooper(new Looper(false)), mClockEnabled(true), mTimeIsAccurate(false),
Yi Kong911ac232019-03-24 01:49:02 -0700165 mTimeFormat12Hour(false), mTimeCheckThread(nullptr), mCallbacks(callbacks) {
Mathias Agopian627e7b52009-05-21 19:21:59 -0700166 mSession = new SurfaceComposerClient();
Geoffrey Pitsch290c4352016-08-09 14:35:10 -0400167
Keun-young Parkb5938422017-03-23 13:46:24 -0700168 std::string powerCtl = android::base::GetProperty("sys.powerctl", "");
169 if (powerCtl.empty()) {
170 mShuttingDown = false;
171 } else {
172 mShuttingDown = true;
173 }
Huihong Luo50a2f362018-08-11 09:27:57 -0700174 ALOGD("%sAnimationStartTiming start time: %" PRId64 "ms", mShuttingDown ? "Shutdown" : "Boot",
175 elapsedRealtime());
176}
177
178BootAnimation::~BootAnimation() {
179 if (mAnimation != nullptr) {
180 releaseAnimation(mAnimation);
181 mAnimation = nullptr;
182 }
183 ALOGD("%sAnimationStopTiming start time: %" PRId64 "ms", mShuttingDown ? "Shutdown" : "Boot",
184 elapsedRealtime());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800185}
186
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800187void BootAnimation::onFirstRef() {
Mathias Agopianbc726112009-09-23 15:44:05 -0700188 status_t err = mSession->linkToComposerDeath(this);
Wei Wang159a4102018-10-23 23:15:58 -0700189 SLOGE_IF(err, "linkToComposerDeath failed (%s) ", strerror(-err));
Mathias Agopian8434c532009-09-23 18:52:49 -0700190 if (err == NO_ERROR) {
Huihong Luo50a2f362018-08-11 09:27:57 -0700191 // Load the animation content -- this can be slow (eg 200ms)
192 // called before waitForSurfaceFlinger() in main() to avoid wait
193 ALOGD("%sAnimationPreloadTiming start time: %" PRId64 "ms",
194 mShuttingDown ? "Shutdown" : "Boot", elapsedRealtime());
195 preloadAnimation();
196 ALOGD("%sAnimationPreloadStopTiming start time: %" PRId64 "ms",
197 mShuttingDown ? "Shutdown" : "Boot", elapsedRealtime());
Mathias Agopianbc726112009-09-23 15:44:05 -0700198 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800199}
200
Mathias Agopianbc726112009-09-23 15:44:05 -0700201sp<SurfaceComposerClient> BootAnimation::session() const {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800202 return mSession;
203}
204
Marin Shalamanov9070c052020-05-18 19:05:17 +0200205void BootAnimation::binderDied(const wp<IBinder>&) {
Mathias Agopianbc726112009-09-23 15:44:05 -0700206 // woah, surfaceflinger died!
Wei Wang159a4102018-10-23 23:15:58 -0700207 SLOGD("SurfaceFlinger died, exiting...");
Mathias Agopianbc726112009-09-23 15:44:05 -0700208
209 // calling requestExit() is not enough here because the Surface code
210 // might be blocked on a condition variable that will never be updated.
211 kill( getpid(), SIGKILL );
212 requestExit();
213}
214
Derek Sollenberger22d0d4c72020-04-08 10:53:46 -0400215static void* decodeImage(const void* encodedData, size_t dataLength, AndroidBitmapInfo* outInfo) {
216 AImageDecoder* decoder = nullptr;
217 AImageDecoder_createFromBuffer(encodedData, dataLength, &decoder);
218 if (!decoder) {
219 return nullptr;
220 }
221
222 const AImageDecoderHeaderInfo* info = AImageDecoder_getHeaderInfo(decoder);
223 outInfo->width = AImageDecoderHeaderInfo_getWidth(info);
224 outInfo->height = AImageDecoderHeaderInfo_getHeight(info);
225 outInfo->format = AImageDecoderHeaderInfo_getAndroidBitmapFormat(info);
226 outInfo->stride = AImageDecoder_getMinimumStride(decoder);
227 outInfo->flags = 0;
228
229 const size_t size = outInfo->stride * outInfo->height;
230 void* pixels = malloc(size);
231 int result = AImageDecoder_decodeImage(decoder, pixels, outInfo->stride, size);
232 AImageDecoder_delete(decoder);
233
234 if (result != ANDROID_IMAGE_DECODER_SUCCESS) {
235 free(pixels);
236 return nullptr;
237 }
238 return pixels;
239}
240
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800241status_t BootAnimation::initTexture(Texture* texture, AssetManager& assets,
242 const char* name) {
243 Asset* asset = assets.open(name, Asset::ACCESS_BUFFER);
Yi Kong911ac232019-03-24 01:49:02 -0700244 if (asset == nullptr)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800245 return NO_INIT;
Derek Sollenberger22d0d4c72020-04-08 10:53:46 -0400246
247 AndroidBitmapInfo bitmapInfo;
248 void* pixels = decodeImage(asset->getBuffer(false), asset->getLength(), &bitmapInfo);
249 auto pixelDeleter = std::unique_ptr<void, decltype(free)*>{ pixels, free };
250
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800251 asset->close();
252 delete asset;
253
Derek Sollenberger22d0d4c72020-04-08 10:53:46 -0400254 if (!pixels) {
255 return NO_INIT;
256 }
257
258 const int w = bitmapInfo.width;
259 const int h = bitmapInfo.height;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800260
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800261 texture->w = w;
262 texture->h = h;
263
264 glGenTextures(1, &texture->name);
265 glBindTexture(GL_TEXTURE_2D, texture->name);
266
Derek Sollenberger22d0d4c72020-04-08 10:53:46 -0400267 switch (bitmapInfo.format) {
268 case ANDROID_BITMAP_FORMAT_A_8:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800269 glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, w, h, 0, GL_ALPHA,
Derek Sollenberger22d0d4c72020-04-08 10:53:46 -0400270 GL_UNSIGNED_BYTE, pixels);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800271 break;
Derek Sollenberger22d0d4c72020-04-08 10:53:46 -0400272 case ANDROID_BITMAP_FORMAT_RGBA_4444:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800273 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA,
Derek Sollenberger22d0d4c72020-04-08 10:53:46 -0400274 GL_UNSIGNED_SHORT_4_4_4_4, pixels);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800275 break;
Derek Sollenberger22d0d4c72020-04-08 10:53:46 -0400276 case ANDROID_BITMAP_FORMAT_RGBA_8888:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800277 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA,
Derek Sollenberger22d0d4c72020-04-08 10:53:46 -0400278 GL_UNSIGNED_BYTE, pixels);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800279 break;
Derek Sollenberger22d0d4c72020-04-08 10:53:46 -0400280 case ANDROID_BITMAP_FORMAT_RGB_565:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800281 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, w, h, 0, GL_RGB,
Derek Sollenberger22d0d4c72020-04-08 10:53:46 -0400282 GL_UNSIGNED_SHORT_5_6_5, pixels);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800283 break;
284 default:
285 break;
286 }
287
Lucas Dupin8eedc022021-07-29 21:49:07 +0000288 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
289 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
290 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
291 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700292
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800293 return NO_ERROR;
294}
295
Marin Shalamanov9070c052020-05-18 19:05:17 +0200296status_t BootAnimation::initTexture(FileMap* map, int* width, int* height) {
Derek Sollenberger22d0d4c72020-04-08 10:53:46 -0400297 AndroidBitmapInfo bitmapInfo;
298 void* pixels = decodeImage(map->getDataPtr(), map->getDataLength(), &bitmapInfo);
299 auto pixelDeleter = std::unique_ptr<void, decltype(free)*>{ pixels, free };
Mathias Agopiana8826d62009-10-01 03:10:14 -0700300
Mykola Kondratenko0c1eeb32014-04-15 09:35:44 +0200301 // FileMap memory is never released until application exit.
302 // Release it now as the texture is already loaded and the memory used for
303 // the packed resource can be released.
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700304 delete map;
Mykola Kondratenko0c1eeb32014-04-15 09:35:44 +0200305
Derek Sollenberger22d0d4c72020-04-08 10:53:46 -0400306 if (!pixels) {
307 return NO_INIT;
308 }
309
310 const int w = bitmapInfo.width;
311 const int h = bitmapInfo.height;
Mathias Agopiana8826d62009-10-01 03:10:14 -0700312
Mathias Agopiana8826d62009-10-01 03:10:14 -0700313 int tw = 1 << (31 - __builtin_clz(w));
314 int th = 1 << (31 - __builtin_clz(h));
315 if (tw < w) tw <<= 1;
316 if (th < h) th <<= 1;
317
Derek Sollenberger22d0d4c72020-04-08 10:53:46 -0400318 switch (bitmapInfo.format) {
319 case ANDROID_BITMAP_FORMAT_RGBA_8888:
Sai Kiran Korwar27167492015-07-07 20:00:06 +0530320 if (!mUseNpotTextures && (tw != w || th != h)) {
Mathias Agopiana8826d62009-10-01 03:10:14 -0700321 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, tw, th, 0, GL_RGBA,
Yi Kong911ac232019-03-24 01:49:02 -0700322 GL_UNSIGNED_BYTE, nullptr);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700323 glTexSubImage2D(GL_TEXTURE_2D, 0,
Derek Sollenberger22d0d4c72020-04-08 10:53:46 -0400324 0, 0, w, h, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700325 } else {
Sai Kiran Korwar27167492015-07-07 20:00:06 +0530326 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA,
Derek Sollenberger22d0d4c72020-04-08 10:53:46 -0400327 GL_UNSIGNED_BYTE, pixels);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700328 }
329 break;
330
Derek Sollenberger22d0d4c72020-04-08 10:53:46 -0400331 case ANDROID_BITMAP_FORMAT_RGB_565:
Sai Kiran Korwar27167492015-07-07 20:00:06 +0530332 if (!mUseNpotTextures && (tw != w || th != h)) {
Mathias Agopiana8826d62009-10-01 03:10:14 -0700333 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, tw, th, 0, GL_RGB,
Yi Kong911ac232019-03-24 01:49:02 -0700334 GL_UNSIGNED_SHORT_5_6_5, nullptr);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700335 glTexSubImage2D(GL_TEXTURE_2D, 0,
Derek Sollenberger22d0d4c72020-04-08 10:53:46 -0400336 0, 0, w, h, GL_RGB, GL_UNSIGNED_SHORT_5_6_5, pixels);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700337 } else {
Sai Kiran Korwar27167492015-07-07 20:00:06 +0530338 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, w, h, 0, GL_RGB,
Derek Sollenberger22d0d4c72020-04-08 10:53:46 -0400339 GL_UNSIGNED_SHORT_5_6_5, pixels);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700340 }
341 break;
342 default:
343 break;
344 }
345
Lucas Dupin8eedc022021-07-29 21:49:07 +0000346 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
347 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
348 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
349 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700350
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700351 *width = w;
352 *height = h;
353
Mathias Agopiana8826d62009-10-01 03:10:14 -0700354 return NO_ERROR;
355}
356
Marin Shalamanov9070c052020-05-18 19:05:17 +0200357class BootAnimation::DisplayEventCallback : public LooperCallback {
358 BootAnimation* mBootAnimation;
359
360public:
361 DisplayEventCallback(BootAnimation* bootAnimation) {
362 mBootAnimation = bootAnimation;
363 }
364
365 int handleEvent(int /* fd */, int events, void* /* data */) {
366 if (events & (Looper::EVENT_ERROR | Looper::EVENT_HANGUP)) {
367 ALOGE("Display event receiver pipe was closed or an error occurred. events=0x%x",
368 events);
369 return 0; // remove the callback
370 }
371
372 if (!(events & Looper::EVENT_INPUT)) {
373 ALOGW("Received spurious callback for unhandled poll event. events=0x%x", events);
374 return 1; // keep the callback
375 }
376
377 constexpr int kBufferSize = 100;
378 DisplayEventReceiver::Event buffer[kBufferSize];
379 ssize_t numEvents;
380 do {
381 numEvents = mBootAnimation->mDisplayEventReceiver->getEvents(buffer, kBufferSize);
382 for (size_t i = 0; i < static_cast<size_t>(numEvents); i++) {
383 const auto& event = buffer[i];
384 if (event.header.type == DisplayEventReceiver::DISPLAY_EVENT_HOTPLUG) {
385 SLOGV("Hotplug received");
386
387 if (!event.hotplug.connected) {
388 // ignore hotplug disconnect
389 continue;
390 }
391 auto token = SurfaceComposerClient::getPhysicalDisplayToken(
392 event.header.displayId);
393
394 if (token != mBootAnimation->mDisplayToken) {
395 // ignore hotplug of a secondary display
396 continue;
397 }
398
Marin Shalamanovcb783ca2021-01-29 21:26:07 +0100399 DisplayMode displayMode;
400 const status_t error = SurfaceComposerClient::getActiveDisplayMode(
401 mBootAnimation->mDisplayToken, &displayMode);
Marin Shalamanov9070c052020-05-18 19:05:17 +0200402 if (error != NO_ERROR) {
Marin Shalamanovcb783ca2021-01-29 21:26:07 +0100403 SLOGE("Can't get active display mode.");
Marin Shalamanov9070c052020-05-18 19:05:17 +0200404 }
Marin Shalamanovcb783ca2021-01-29 21:26:07 +0100405 mBootAnimation->resizeSurface(displayMode.resolution.getWidth(),
406 displayMode.resolution.getHeight());
Marin Shalamanov9070c052020-05-18 19:05:17 +0200407 }
408 }
409 } while (numEvents > 0);
410
411 return 1; // keep the callback
412 }
413};
414
415EGLConfig BootAnimation::getEglConfig(const EGLDisplay& display) {
416 const EGLint attribs[] = {
417 EGL_RED_SIZE, 8,
418 EGL_GREEN_SIZE, 8,
419 EGL_BLUE_SIZE, 8,
420 EGL_DEPTH_SIZE, 0,
421 EGL_NONE
422 };
423 EGLint numConfigs;
424 EGLConfig config;
425 eglChooseConfig(display, attribs, &config, 1, &numConfigs);
426 return config;
427}
428
Marin Shalamanov047802d2020-05-19 23:55:12 +0200429ui::Size BootAnimation::limitSurfaceSize(int width, int height) const {
430 ui::Size limited(width, height);
431 bool wasLimited = false;
432 const float aspectRatio = float(width) / float(height);
433 if (mMaxWidth != 0 && width > mMaxWidth) {
434 limited.height = mMaxWidth / aspectRatio;
435 limited.width = mMaxWidth;
436 wasLimited = true;
437 }
438 if (mMaxHeight != 0 && limited.height > mMaxHeight) {
439 limited.height = mMaxHeight;
440 limited.width = mMaxHeight * aspectRatio;
441 wasLimited = true;
442 }
443 SLOGV_IF(wasLimited, "Surface size has been limited to [%dx%d] from [%dx%d]",
444 limited.width, limited.height, width, height);
445 return limited;
446}
447
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800448status_t BootAnimation::readyToRun() {
449 mAssets.addDefaultAssets();
450
Dominik Laskowski3316a0a2019-01-25 02:56:41 -0800451 mDisplayToken = SurfaceComposerClient::getInternalDisplayToken();
452 if (mDisplayToken == nullptr)
Dominik Laskowski69b281d2019-11-22 14:13:12 -0800453 return NAME_NOT_FOUND;
Dominik Laskowski3316a0a2019-01-25 02:56:41 -0800454
Marin Shalamanovcb783ca2021-01-29 21:26:07 +0100455 DisplayMode displayMode;
Dominik Laskowski69b281d2019-11-22 14:13:12 -0800456 const status_t error =
Marin Shalamanovcb783ca2021-01-29 21:26:07 +0100457 SurfaceComposerClient::getActiveDisplayMode(mDisplayToken, &displayMode);
Dominik Laskowski69b281d2019-11-22 14:13:12 -0800458 if (error != NO_ERROR)
459 return error;
460
Marin Shalamanov047802d2020-05-19 23:55:12 +0200461 mMaxWidth = android::base::GetIntProperty("ro.surface_flinger.max_graphics_width", 0);
462 mMaxHeight = android::base::GetIntProperty("ro.surface_flinger.max_graphics_height", 0);
Marin Shalamanovcb783ca2021-01-29 21:26:07 +0100463 ui::Size resolution = displayMode.resolution;
Marin Shalamanov047802d2020-05-19 23:55:12 +0200464 resolution = limitSurfaceSize(resolution.width, resolution.height);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800465 // create the native surface
Jeff Brown0b722fe2012-08-24 22:40:14 -0700466 sp<SurfaceControl> control = session()->createSurface(String8("BootAnimation"),
Dominik Laskowski69b281d2019-11-22 14:13:12 -0800467 resolution.getWidth(), resolution.getHeight(), PIXEL_FORMAT_RGB_565);
Mathias Agopian439863f2011-06-28 19:09:31 -0700468
Robert Carre13b58e2017-08-31 14:50:44 -0700469 SurfaceComposerClient::Transaction t;
Huihong Luof97c9e22019-05-29 11:08:01 -0700470
471 // this guest property specifies multi-display IDs to show the boot animation
472 // multiple ids can be set with comma (,) as separator, for example:
Huihong Luod4a217e2020-01-16 15:56:33 -0800473 // setprop persist.boot.animation.displays 19260422155234049,19261083906282754
Marin Shalamanov11599ee2020-07-27 21:31:48 +0200474 Vector<PhysicalDisplayId> physicalDisplayIds;
Huihong Luof97c9e22019-05-29 11:08:01 -0700475 char displayValue[PROPERTY_VALUE_MAX] = "";
Huihong Luod4a217e2020-01-16 15:56:33 -0800476 property_get(DISPLAYS_PROP_NAME, displayValue, "");
Huihong Luof97c9e22019-05-29 11:08:01 -0700477 bool isValid = displayValue[0] != '\0';
478 if (isValid) {
479 char *p = displayValue;
480 while (*p) {
481 if (!isdigit(*p) && *p != ',') {
482 isValid = false;
483 break;
484 }
485 p ++;
486 }
487 if (!isValid)
Huihong Luod4a217e2020-01-16 15:56:33 -0800488 SLOGE("Invalid syntax for the value of system prop: %s", DISPLAYS_PROP_NAME);
Huihong Luof97c9e22019-05-29 11:08:01 -0700489 }
490 if (isValid) {
491 std::istringstream stream(displayValue);
Marin Shalamanov11599ee2020-07-27 21:31:48 +0200492 for (PhysicalDisplayId id; stream >> id.value; ) {
Huihong Luof97c9e22019-05-29 11:08:01 -0700493 physicalDisplayIds.add(id);
494 if (stream.peek() == ',')
495 stream.ignore();
496 }
497
498 // In the case of multi-display, boot animation shows on the specified displays
499 // in addition to the primary display
500 auto ids = SurfaceComposerClient::getPhysicalDisplayIds();
501 constexpr uint32_t LAYER_STACK = 0;
502 for (auto id : physicalDisplayIds) {
503 if (std::find(ids.begin(), ids.end(), id) != ids.end()) {
504 sp<IBinder> token = SurfaceComposerClient::getPhysicalDisplayToken(id);
505 if (token != nullptr)
506 t.setDisplayLayerStack(token, LAYER_STACK);
507 }
508 }
509 t.setLayerStack(control, LAYER_STACK);
510 }
511
Robert Carre13b58e2017-08-31 14:50:44 -0700512 t.setLayer(control, 0x40000000)
513 .apply();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800514
Mathias Agopian17f638b2009-04-16 20:04:08 -0700515 sp<Surface> s = control->getSurface();
516
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800517 // initialize opengl and egl
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800518 EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
Yi Kong911ac232019-03-24 01:49:02 -0700519 eglInitialize(display, nullptr, nullptr);
Marin Shalamanov9070c052020-05-18 19:05:17 +0200520 EGLConfig config = getEglConfig(display);
521 EGLSurface surface = eglCreateWindowSurface(display, config, s.get(), nullptr);
Lucas Dupin8eedc022021-07-29 21:49:07 +0000522 // Initialize egl context with client version number 2.0.
523 EGLint contextAttributes[] = {EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE};
524 EGLContext context = eglCreateContext(display, config, nullptr, contextAttributes);
Marin Shalamanov9070c052020-05-18 19:05:17 +0200525 EGLint w, h;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800526 eglQuerySurface(display, surface, EGL_WIDTH, &w);
527 eglQuerySurface(display, surface, EGL_HEIGHT, &h);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700528
Mathias Agopianabac0102009-07-31 14:47:00 -0700529 if (eglMakeCurrent(display, surface, surface, context) == EGL_FALSE)
530 return NO_INIT;
Mathias Agopiana8826d62009-10-01 03:10:14 -0700531
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800532 mDisplay = display;
533 mContext = context;
534 mSurface = surface;
535 mWidth = w;
536 mHeight = h;
Mathias Agopian17f638b2009-04-16 20:04:08 -0700537 mFlingerSurfaceControl = control;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800538 mFlingerSurface = s;
Adrian Roos9ee5dff2018-08-22 20:19:49 +0200539 mTargetInset = -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800540
Yegor Malyshev6925e1b2020-09-10 16:42:20 +0200541 projectSceneToWindow();
542
Marin Shalamanov9070c052020-05-18 19:05:17 +0200543 // Register a display event receiver
544 mDisplayEventReceiver = std::make_unique<DisplayEventReceiver>();
545 status_t status = mDisplayEventReceiver->initCheck();
546 SLOGE_IF(status != NO_ERROR, "Initialization of DisplayEventReceiver failed with status: %d",
547 status);
548 mLooper->addFd(mDisplayEventReceiver->getFd(), 0, Looper::EVENT_INPUT,
549 new DisplayEventCallback(this), nullptr);
550
Huihong Luo50a2f362018-08-11 09:27:57 -0700551 return NO_ERROR;
552}
553
Yegor Malyshev6925e1b2020-09-10 16:42:20 +0200554void BootAnimation::projectSceneToWindow() {
555 glViewport(0, 0, mWidth, mHeight);
556 glScissor(0, 0, mWidth, mHeight);
Yegor Malyshev6925e1b2020-09-10 16:42:20 +0200557}
558
Marin Shalamanov9070c052020-05-18 19:05:17 +0200559void BootAnimation::resizeSurface(int newWidth, int newHeight) {
560 // We assume this function is called on the animation thread.
561 if (newWidth == mWidth && newHeight == mHeight) {
562 return;
563 }
564 SLOGV("Resizing the boot animation surface to %d %d", newWidth, newHeight);
565
566 eglMakeCurrent(mDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
567 eglDestroySurface(mDisplay, mSurface);
568
Marin Shalamanov047802d2020-05-19 23:55:12 +0200569 const auto limitedSize = limitSurfaceSize(newWidth, newHeight);
570 mWidth = limitedSize.width;
571 mHeight = limitedSize.height;
Marin Shalamanov9070c052020-05-18 19:05:17 +0200572
573 SurfaceComposerClient::Transaction t;
574 t.setSize(mFlingerSurfaceControl, mWidth, mHeight);
575 t.apply();
576
577 EGLConfig config = getEglConfig(mDisplay);
578 EGLSurface surface = eglCreateWindowSurface(mDisplay, config, mFlingerSurface.get(), nullptr);
579 if (eglMakeCurrent(mDisplay, surface, surface, mContext) == EGL_FALSE) {
580 SLOGE("Can't make the new surface current. Error %d", eglGetError());
581 return;
582 }
Yegor Malyshev6925e1b2020-09-10 16:42:20 +0200583
584 projectSceneToWindow();
Marin Shalamanov9070c052020-05-18 19:05:17 +0200585
586 mSurface = surface;
587}
588
Huihong Luo50a2f362018-08-11 09:27:57 -0700589bool BootAnimation::preloadAnimation() {
590 findBootAnimationFile();
591 if (!mZipFileName.isEmpty()) {
592 mAnimation = loadAnimation(mZipFileName);
593 return (mAnimation != nullptr);
594 }
595
596 return false;
597}
598
Nikita Ioffe06c986e2020-01-27 17:16:03 +0000599bool BootAnimation::findBootAnimationFileInternal(const std::vector<std::string> &files) {
600 for (const std::string& f : files) {
601 if (access(f.c_str(), R_OK) == 0) {
602 mZipFileName = f.c_str();
603 return true;
604 }
605 }
606 return false;
607}
608
Huihong Luo50a2f362018-08-11 09:27:57 -0700609void BootAnimation::findBootAnimationFile() {
Elliott Hughesc367d482013-10-29 13:12:55 -0700610 // If the device has encryption turned on or is in process
Jason parksbd9a08d2011-01-31 15:04:34 -0600611 // of being encrypted we show the encrypted boot animation.
612 char decrypt[PROPERTY_VALUE_MAX];
613 property_get("vold.decrypt", decrypt, "");
614
Keun-young Parkb5938422017-03-23 13:46:24 -0700615 bool encryptedAnimation = atoi(decrypt) != 0 ||
616 !strcmp("trigger_restart_min_framework", decrypt);
Jason parksbd9a08d2011-01-31 15:04:34 -0600617
Jaekyun Seokc521bb32018-01-30 11:52:47 +0900618 if (!mShuttingDown && encryptedAnimation) {
Nikita Ioffe06c986e2020-01-27 17:16:03 +0000619 static const std::vector<std::string> encryptedBootFiles = {
620 PRODUCT_ENCRYPTED_BOOTANIMATION_FILE, SYSTEM_ENCRYPTED_BOOTANIMATION_FILE,
621 };
622 if (findBootAnimationFileInternal(encryptedBootFiles)) {
623 return;
Jaekyun Seokc521bb32018-01-30 11:52:47 +0900624 }
Jason parksbd9a08d2011-01-31 15:04:34 -0600625 }
Beverly34ffe252019-05-17 11:03:54 -0400626
627 const bool playDarkAnim = android::base::GetIntProperty("ro.boot.theme", 0) == 1;
Nikita Ioffe06c986e2020-01-27 17:16:03 +0000628 static const std::vector<std::string> bootFiles = {
629 APEX_BOOTANIMATION_FILE, playDarkAnim ? PRODUCT_BOOTANIMATION_DARK_FILE : PRODUCT_BOOTANIMATION_FILE,
630 OEM_BOOTANIMATION_FILE, SYSTEM_BOOTANIMATION_FILE
631 };
632 static const std::vector<std::string> shutdownFiles = {
633 PRODUCT_SHUTDOWNANIMATION_FILE, OEM_SHUTDOWNANIMATION_FILE, SYSTEM_SHUTDOWNANIMATION_FILE, ""
634 };
635 static const std::vector<std::string> userspaceRebootFiles = {
636 PRODUCT_USERSPACE_REBOOT_ANIMATION_FILE, OEM_USERSPACE_REBOOT_ANIMATION_FILE,
637 SYSTEM_USERSPACE_REBOOT_ANIMATION_FILE,
638 };
Keun-young Parkb5938422017-03-23 13:46:24 -0700639
Nikita Ioffe06c986e2020-01-27 17:16:03 +0000640 if (android::base::GetBoolProperty("sys.init.userspace_reboot.in_progress", false)) {
641 findBootAnimationFileInternal(userspaceRebootFiles);
642 } else if (mShuttingDown) {
643 findBootAnimationFileInternal(shutdownFiles);
644 } else {
645 findBootAnimationFileInternal(bootFiles);
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700646 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800647}
648
Lucas Dupin8eedc022021-07-29 21:49:07 +0000649GLuint compileShader(GLenum shaderType, const GLchar *source) {
650 GLuint shader = glCreateShader(shaderType);
651 glShaderSource(shader, 1, &source, 0);
652 glCompileShader(shader);
653 GLint isCompiled = 0;
654 glGetShaderiv(shader, GL_COMPILE_STATUS, &isCompiled);
655 if (isCompiled == GL_FALSE) {
656 SLOGE("Compile shader failed. Shader type: %d", shaderType);
657 return 0;
658 }
659 return shader;
660}
661
662GLuint linkShader(GLuint vertexShader, GLuint fragmentShader) {
663 GLuint program = glCreateProgram();
664 glAttachShader(program, vertexShader);
665 glAttachShader(program, fragmentShader);
666 glLinkProgram(program);
667 GLint isLinked = 0;
668 glGetProgramiv(program, GL_LINK_STATUS, (int *)&isLinked);
669 if (isLinked == GL_FALSE) {
670 SLOGE("Linking shader failed. Shader handles: vert %d, frag %d",
671 vertexShader, fragmentShader);
672 return 0;
673 }
674 return program;
675}
676
677void BootAnimation::initShaders() {
678 GLuint vertexShader = compileShader(GL_VERTEX_SHADER, (const GLchar *)VERTEX_SHADER_SOURCE);
679 GLuint imageFragmentShader =
680 compileShader(GL_FRAGMENT_SHADER, (const GLchar *)IMAGE_FRAG_SHADER_SOURCE);
681 GLuint textFragmentShader =
682 compileShader(GL_FRAGMENT_SHADER, (const GLchar *)TEXT_FRAG_SHADER_SOURCE);
683
684 // Initialize image shader.
685 mImageShader = linkShader(vertexShader, imageFragmentShader);
686 GLint positionLocation = glGetAttribLocation(mImageShader, A_POSITION);
687 GLint uvLocation = glGetAttribLocation(mImageShader, A_UV);
688 mImageTextureLocation = glGetUniformLocation(mImageShader, U_TEXTURE);
689 mImageFadeLocation = glGetUniformLocation(mImageShader, U_FADE);
690 glEnableVertexAttribArray(positionLocation);
691 glVertexAttribPointer(positionLocation, 2, GL_FLOAT, GL_FALSE, 0, quadPositions);
692 glVertexAttribPointer(uvLocation, 2, GL_FLOAT, GL_FALSE, 0, quadUVs);
693 glEnableVertexAttribArray(uvLocation);
694
695 // Initialize text shader.
696 mTextShader = linkShader(vertexShader, textFragmentShader);
697 positionLocation = glGetAttribLocation(mTextShader, A_POSITION);
698 uvLocation = glGetAttribLocation(mTextShader, A_UV);
699 mTextTextureLocation = glGetUniformLocation(mTextShader, U_TEXTURE);
700 mTextCropAreaLocation = glGetUniformLocation(mTextShader, U_CROP_AREA);
701 glEnableVertexAttribArray(positionLocation);
702 glVertexAttribPointer(positionLocation, 2, GL_FLOAT, GL_FALSE, 0, quadPositions);
703 glVertexAttribPointer(uvLocation, 2, GL_FLOAT, GL_FALSE, 0, quadUVs);
704 glEnableVertexAttribArray(uvLocation);
705}
706
Marin Shalamanov9070c052020-05-18 19:05:17 +0200707bool BootAnimation::threadLoop() {
708 bool result;
Lucas Dupin8eedc022021-07-29 21:49:07 +0000709 initShaders();
710
Narayan Kamathafd31e02013-12-03 13:16:03 +0000711 // We have no bootanimation file, so we use the stock android logo
712 // animation.
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700713 if (mZipFileName.isEmpty()) {
Marin Shalamanov9070c052020-05-18 19:05:17 +0200714 result = android();
Mathias Agopiana8826d62009-10-01 03:10:14 -0700715 } else {
Marin Shalamanov9070c052020-05-18 19:05:17 +0200716 result = movie();
Mathias Agopiana8826d62009-10-01 03:10:14 -0700717 }
718
Philip Junker60600562020-11-25 18:16:16 +0100719 mCallbacks->shutdown();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800720 eglMakeCurrent(mDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
721 eglDestroyContext(mDisplay, mContext);
722 eglDestroySurface(mDisplay, mSurface);
Mathias Agopian6cf0db22009-04-17 19:36:26 -0700723 mFlingerSurface.clear();
Mathias Agopian17f638b2009-04-16 20:04:08 -0700724 mFlingerSurfaceControl.clear();
Mathias Agopian627e7b52009-05-21 19:21:59 -0700725 eglTerminate(mDisplay);
Sai Kiran Korwar3eee9fb2015-06-16 17:13:35 +0530726 eglReleaseThread();
Mathias Agopian627e7b52009-05-21 19:21:59 -0700727 IPCThreadState::self()->stopProcess();
Marin Shalamanov9070c052020-05-18 19:05:17 +0200728 return result;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800729}
730
Marin Shalamanov9070c052020-05-18 19:05:17 +0200731bool BootAnimation::android() {
Lucas Dupin8eedc022021-07-29 21:49:07 +0000732 glActiveTexture(GL_TEXTURE0);
733
Wei Wang159a4102018-10-23 23:15:58 -0700734 SLOGD("%sAnimationShownTiming start time: %" PRId64 "ms", mShuttingDown ? "Shutdown" : "Boot",
Keun-young Parkd1794cd2017-04-04 12:21:19 -0700735 elapsedRealtime());
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700736 initTexture(&mAndroid[0], mAssets, "images/android-logo-mask.png");
737 initTexture(&mAndroid[1], mAssets, "images/android-logo-shine.png");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800738
Ed Coyne7464ac92017-06-08 12:26:48 -0700739 mCallbacks->init({});
740
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800741 // clear screen
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700742 glDisable(GL_DITHER);
743 glDisable(GL_SCISSOR_TEST);
Mathias Agopian59f19e42011-05-06 19:22:12 -0700744 glClearColor(0,0,0,1);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800745 glClear(GL_COLOR_BUFFER_BIT);
746 eglSwapBuffers(mDisplay, mSurface);
747
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700748 // Blend state
749 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700750
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800751 const nsecs_t startTime = systemTime();
752 do {
Marin Shalamanov9070c052020-05-18 19:05:17 +0200753 processDisplayEvents();
754 const GLint xc = (mWidth - mAndroid[0].w) / 2;
755 const GLint yc = (mHeight - mAndroid[0].h) / 2;
756 const Rect updateRect(xc, yc, xc + mAndroid[0].w, yc + mAndroid[0].h);
757 glScissor(updateRect.left, mHeight - updateRect.bottom, updateRect.width(),
758 updateRect.height());
759
Mathias Agopian13796652009-03-24 22:49:21 -0700760 nsecs_t now = systemTime();
761 double time = now - startTime;
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700762 float t = 4.0f * float(time / us2ns(16667)) / mAndroid[1].w;
763 GLint offset = (1 - (t - floorf(t))) * mAndroid[1].w;
764 GLint x = xc - offset;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800765
Mathias Agopian81668642009-07-28 11:41:30 -0700766 glDisable(GL_SCISSOR_TEST);
767 glClear(GL_COLOR_BUFFER_BIT);
768
769 glEnable(GL_SCISSOR_TEST);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800770 glDisable(GL_BLEND);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800771 glBindTexture(GL_TEXTURE_2D, mAndroid[1].name);
Lucas Dupin8eedc022021-07-29 21:49:07 +0000772 drawTexturedQuad(x, yc, mAndroid[1].w, mAndroid[1].h);
773 drawTexturedQuad(x + mAndroid[1].w, yc, mAndroid[1].w, mAndroid[1].h);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800774
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700775 glEnable(GL_BLEND);
776 glBindTexture(GL_TEXTURE_2D, mAndroid[0].name);
Lucas Dupin8eedc022021-07-29 21:49:07 +0000777 drawTexturedQuad(xc, yc, mAndroid[0].w, mAndroid[0].h);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800778
Mathias Agopian627e7b52009-05-21 19:21:59 -0700779 EGLBoolean res = eglSwapBuffers(mDisplay, mSurface);
780 if (res == EGL_FALSE)
781 break;
782
Mathias Agopian13796652009-03-24 22:49:21 -0700783 // 12fps: don't animate too fast to preserve CPU
784 const nsecs_t sleepTime = 83333 - ns2us(systemTime() - now);
785 if (sleepTime > 0)
Mathias Agopiana8826d62009-10-01 03:10:14 -0700786 usleep(sleepTime);
Kevin Hesterd3782b22012-04-26 10:38:55 -0700787
788 checkExit();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800789 } while (!exitPending());
790
791 glDeleteTextures(1, &mAndroid[0].name);
792 glDeleteTextures(1, &mAndroid[1].name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800793 return false;
794}
795
Kevin Hesterd3782b22012-04-26 10:38:55 -0700796void BootAnimation::checkExit() {
797 // Allow surface flinger to gracefully request shutdown
798 char value[PROPERTY_VALUE_MAX];
799 property_get(EXIT_PROP_NAME, value, "0");
800 int exitnow = atoi(value);
801 if (exitnow) {
802 requestExit();
803 }
804}
805
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700806bool BootAnimation::validClock(const Animation::Part& part) {
807 return part.clockPosX != TEXT_MISSING_VALUE && part.clockPosY != TEXT_MISSING_VALUE;
808}
809
810bool parseTextCoord(const char* str, int* dest) {
811 if (strcmp("c", str) == 0) {
812 *dest = TEXT_CENTER_VALUE;
813 return true;
814 }
815
816 char* end;
817 int val = (int) strtol(str, &end, 0);
818 if (end == str || *end != '\0' || val == INT_MAX || val == INT_MIN) {
819 return false;
820 }
821 *dest = val;
822 return true;
823}
824
825// Parse two position coordinates. If only string is non-empty, treat it as the y value.
826void parsePosition(const char* str1, const char* str2, int* x, int* y) {
827 bool success = false;
828 if (strlen(str1) == 0) { // No values were specified
829 // success = false
830 } else if (strlen(str2) == 0) { // we have only one value
831 if (parseTextCoord(str1, y)) {
832 *x = TEXT_CENTER_VALUE;
833 success = true;
834 }
835 } else {
836 if (parseTextCoord(str1, x) && parseTextCoord(str2, y)) {
837 success = true;
838 }
839 }
840
841 if (!success) {
842 *x = TEXT_MISSING_VALUE;
843 *y = TEXT_MISSING_VALUE;
844 }
845}
846
Jesse Hall083b84c2014-09-22 10:51:09 -0700847// Parse a color represented as an HTML-style 'RRGGBB' string: each pair of
848// characters in str is a hex number in [0, 255], which are converted to
849// floating point values in the range [0.0, 1.0] and placed in the
850// corresponding elements of color.
851//
852// If the input string isn't valid, parseColor returns false and color is
853// left unchanged.
854static bool parseColor(const char str[7], float color[3]) {
855 float tmpColor[3];
856 for (int i = 0; i < 3; i++) {
857 int val = 0;
858 for (int j = 0; j < 2; j++) {
859 val *= 16;
860 char c = str[2*i + j];
861 if (c >= '0' && c <= '9') val += c - '0';
862 else if (c >= 'A' && c <= 'F') val += (c - 'A') + 10;
863 else if (c >= 'a' && c <= 'f') val += (c - 'a') + 10;
864 else return false;
865 }
866 tmpColor[i] = static_cast<float>(val) / 255.0f;
867 }
868 memcpy(color, tmpColor, sizeof(tmpColor));
869 return true;
870}
871
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700872
Marin Shalamanov9070c052020-05-18 19:05:17 +0200873static bool readFile(ZipFileRO* zip, const char* name, String8& outString) {
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700874 ZipEntryRO entry = zip->findEntryByName(name);
Wei Wang159a4102018-10-23 23:15:58 -0700875 SLOGE_IF(!entry, "couldn't find %s", name);
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700876 if (!entry) {
877 return false;
878 }
879
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700880 FileMap* entryMap = zip->createEntryFileMap(entry);
881 zip->releaseEntry(entry);
Wei Wang159a4102018-10-23 23:15:58 -0700882 SLOGE_IF(!entryMap, "entryMap is null");
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700883 if (!entryMap) {
884 return false;
885 }
886
887 outString.setTo((char const*)entryMap->getDataPtr(), entryMap->getDataLength());
Narayan Kamath688ff4c2015-02-23 15:47:54 +0000888 delete entryMap;
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700889 return true;
890}
891
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700892// The font image should be a 96x2 array of character images. The
893// columns are the printable ASCII characters 0x20 - 0x7f. The
894// top row is regular text; the bottom row is bold.
895status_t BootAnimation::initFont(Font* font, const char* fallback) {
896 status_t status = NO_ERROR;
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800897
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700898 if (font->map != nullptr) {
899 glGenTextures(1, &font->texture.name);
900 glBindTexture(GL_TEXTURE_2D, font->texture.name);
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800901
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700902 status = initTexture(font->map, &font->texture.w, &font->texture.h);
903
Lucas Dupin8eedc022021-07-29 21:49:07 +0000904 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
905 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
906 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
907 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700908 } else if (fallback != nullptr) {
909 status = initTexture(&font->texture, mAssets, fallback);
910 } else {
911 return NO_INIT;
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800912 }
913
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700914 if (status == NO_ERROR) {
915 font->char_width = font->texture.w / FONT_NUM_COLS;
916 font->char_height = font->texture.h / FONT_NUM_ROWS / 2; // There are bold and regular rows
917 }
918
919 return status;
920}
921
922void BootAnimation::drawText(const char* str, const Font& font, bool bold, int* x, int* y) {
923 glEnable(GL_BLEND); // Allow us to draw on top of the animation
924 glBindTexture(GL_TEXTURE_2D, font.texture.name);
Lucas Dupin8eedc022021-07-29 21:49:07 +0000925 glUseProgram(mTextShader);
926 glUniform1i(mTextTextureLocation, 0);
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700927
928 const int len = strlen(str);
929 const int strWidth = font.char_width * len;
930
931 if (*x == TEXT_CENTER_VALUE) {
932 *x = (mWidth - strWidth) / 2;
933 } else if (*x < 0) {
934 *x = mWidth + *x - strWidth;
935 }
936 if (*y == TEXT_CENTER_VALUE) {
937 *y = (mHeight - font.char_height) / 2;
938 } else if (*y < 0) {
939 *y = mHeight + *y - font.char_height;
940 }
941
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700942 for (int i = 0; i < len; i++) {
943 char c = str[i];
944
945 if (c < FONT_BEGIN_CHAR || c > FONT_END_CHAR) {
946 c = '?';
947 }
948
949 // Crop the texture to only the pixels in the current glyph
950 const int charPos = (c - FONT_BEGIN_CHAR); // Position in the list of valid characters
951 const int row = charPos / FONT_NUM_COLS;
952 const int col = charPos % FONT_NUM_COLS;
Lucas Dupin8eedc022021-07-29 21:49:07 +0000953 // Bold fonts are expected in the second half of each row.
954 float v0 = (row + (bold ? 0.5f : 0.0f)) / FONT_NUM_ROWS;
955 float u0 = ((float)col) / FONT_NUM_COLS;
956 float v1 = v0 + 1.0f / FONT_NUM_ROWS / 2;
957 float u1 = u0 + 1.0f / FONT_NUM_COLS;
958 glUniform4f(mTextCropAreaLocation, u0, v0, u1, v1);
959 drawTexturedQuad(*x, *y, font.char_width, font.char_height);
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700960
961 *x += font.char_width;
962 }
963
964 glDisable(GL_BLEND); // Return to the animation's default behaviour
965 glBindTexture(GL_TEXTURE_2D, 0);
966}
967
Damien Bargiacchi9071db12016-10-28 17:38:22 -0700968// We render 12 or 24 hour time.
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700969void BootAnimation::drawClock(const Font& font, const int xPos, const int yPos) {
Damien Bargiacchi9071db12016-10-28 17:38:22 -0700970 static constexpr char TIME_FORMAT_12[] = "%l:%M";
971 static constexpr char TIME_FORMAT_24[] = "%H:%M";
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700972 static constexpr int TIME_LENGTH = 6;
973
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800974 time_t rawtime;
975 time(&rawtime);
976 struct tm* timeInfo = localtime(&rawtime);
977
978 char timeBuff[TIME_LENGTH];
Damien Bargiacchi9071db12016-10-28 17:38:22 -0700979 const char* timeFormat = mTimeFormat12Hour ? TIME_FORMAT_12 : TIME_FORMAT_24;
980 size_t length = strftime(timeBuff, TIME_LENGTH, timeFormat, timeInfo);
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800981
982 if (length != TIME_LENGTH - 1) {
Wei Wang159a4102018-10-23 23:15:58 -0700983 SLOGE("Couldn't format time; abandoning boot animation clock");
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800984 mClockEnabled = false;
985 return;
986 }
987
Damien Bargiacchi45a76442016-12-05 18:02:18 -0800988 char* out = timeBuff[0] == ' ' ? &timeBuff[1] : &timeBuff[0];
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700989 int x = xPos;
990 int y = yPos;
Damien Bargiacchi45a76442016-12-05 18:02:18 -0800991 drawText(out, font, false, &x, &y);
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800992}
993
Nicolas Geoffray41fffb42020-12-15 10:50:29 +0000994void BootAnimation::drawProgress(int percent, const Font& font, const int xPos, const int yPos) {
995 static constexpr int PERCENT_LENGTH = 5;
996
997 char percentBuff[PERCENT_LENGTH];
998 // ';' has the ascii code just after ':', and the font resource contains '%'
999 // for that ascii code.
1000 sprintf(percentBuff, "%d;", percent);
1001 int x = xPos;
1002 int y = yPos;
1003 drawText(percentBuff, font, false, &x, &y);
1004}
1005
Marin Shalamanov9070c052020-05-18 19:05:17 +02001006bool BootAnimation::parseAnimationDesc(Animation& animation) {
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -07001007 String8 desString;
1008
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001009 if (!readFile(animation.zip, "desc.txt", desString)) {
Narayan Kamathafd31e02013-12-03 13:16:03 +00001010 return false;
1011 }
Mathias Agopiana8826d62009-10-01 03:10:14 -07001012 char const* s = desString.string();
1013
Mathias Agopiana8826d62009-10-01 03:10:14 -07001014 // Parse the description file
1015 for (;;) {
1016 const char* endl = strstr(s, "\n");
Yi Kong911ac232019-03-24 01:49:02 -07001017 if (endl == nullptr) break;
Mathias Agopiana8826d62009-10-01 03:10:14 -07001018 String8 line(s, endl - s);
1019 const char* l = line.string();
Damien Bargiacchia704b7d2016-02-16 16:55:49 -08001020 int fps = 0;
1021 int width = 0;
1022 int height = 0;
1023 int count = 0;
1024 int pause = 0;
Nicolas Geoffray41fffb42020-12-15 10:50:29 +00001025 int progress = 0;
Yegor Malyshev6925e1b2020-09-10 16:42:20 +02001026 int framesToFadeCount = 0;
Narayan Kamathafd31e02013-12-03 13:16:03 +00001027 char path[ANIM_ENTRY_NAME_MAX];
Jesse Hall083b84c2014-09-22 10:51:09 -07001028 char color[7] = "000000"; // default to black if unspecified
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -07001029 char clockPos1[TEXT_POS_LEN_MAX + 1] = "";
1030 char clockPos2[TEXT_POS_LEN_MAX + 1] = "";
Kevin Hesterd3782b22012-04-26 10:38:55 -07001031 char pathType;
Yegor Malyshev6925e1b2020-09-10 16:42:20 +02001032
1033 int nextReadPos;
1034
Nicolas Geoffray41fffb42020-12-15 10:50:29 +00001035 int topLineNumbers = sscanf(l, "%d %d %d %d", &width, &height, &fps, &progress);
1036 if (topLineNumbers == 3 || topLineNumbers == 4) {
1037 // SLOGD("> w=%d, h=%d, fps=%d, progress=%d", width, height, fps, progress);
Mathias Agopiana8826d62009-10-01 03:10:14 -07001038 animation.width = width;
1039 animation.height = height;
1040 animation.fps = fps;
Nicolas Geoffray41fffb42020-12-15 10:50:29 +00001041 if (topLineNumbers == 4) {
1042 animation.progressEnabled = (progress != 0);
1043 } else {
1044 animation.progressEnabled = false;
1045 }
Yegor Malyshev6925e1b2020-09-10 16:42:20 +02001046 } else if (sscanf(l, "%c %d %d %" STRTO(ANIM_PATH_MAX) "s%n",
1047 &pathType, &count, &pause, path, &nextReadPos) >= 4) {
1048 if (pathType == 'f') {
1049 sscanf(l + nextReadPos, " %d #%6s %16s %16s", &framesToFadeCount, color, clockPos1,
1050 clockPos2);
1051 } else {
1052 sscanf(l + nextReadPos, " #%6s %16s %16s", color, clockPos1, clockPos2);
1053 }
1054 // SLOGD("> type=%c, count=%d, pause=%d, path=%s, framesToFadeCount=%d, color=%s, "
1055 // "clockPos1=%s, clockPos2=%s",
1056 // pathType, count, pause, path, framesToFadeCount, color, clockPos1, clockPos2);
Mathias Agopiana8826d62009-10-01 03:10:14 -07001057 Animation::Part part;
Kevin Hesterd3782b22012-04-26 10:38:55 -07001058 part.playUntilComplete = pathType == 'c';
Yegor Malyshev6925e1b2020-09-10 16:42:20 +02001059 part.framesToFadeCount = framesToFadeCount;
Mathias Agopiana8826d62009-10-01 03:10:14 -07001060 part.count = count;
1061 part.pause = pause;
1062 part.path = path;
Yi Kong911ac232019-03-24 01:49:02 -07001063 part.audioData = nullptr;
1064 part.animation = nullptr;
Jesse Hall083b84c2014-09-22 10:51:09 -07001065 if (!parseColor(color, part.backgroundColor)) {
Wei Wang159a4102018-10-23 23:15:58 -07001066 SLOGE("> invalid color '#%s'", color);
Jesse Hall083b84c2014-09-22 10:51:09 -07001067 part.backgroundColor[0] = 0.0f;
1068 part.backgroundColor[1] = 0.0f;
1069 part.backgroundColor[2] = 0.0f;
1070 }
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -07001071 parsePosition(clockPos1, clockPos2, &part.clockPosX, &part.clockPosY);
Mathias Agopiana8826d62009-10-01 03:10:14 -07001072 animation.parts.add(part);
1073 }
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001074 else if (strcmp(l, "$SYSTEM") == 0) {
Wei Wang159a4102018-10-23 23:15:58 -07001075 // SLOGD("> SYSTEM");
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001076 Animation::Part part;
1077 part.playUntilComplete = false;
Yegor Malyshev6925e1b2020-09-10 16:42:20 +02001078 part.framesToFadeCount = 0;
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001079 part.count = 1;
1080 part.pause = 0;
Yi Kong911ac232019-03-24 01:49:02 -07001081 part.audioData = nullptr;
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001082 part.animation = loadAnimation(String8(SYSTEM_BOOTANIMATION_FILE));
Yi Kong911ac232019-03-24 01:49:02 -07001083 if (part.animation != nullptr)
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001084 animation.parts.add(part);
1085 }
Mathias Agopiana8826d62009-10-01 03:10:14 -07001086 s = ++endl;
1087 }
1088
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001089 return true;
1090}
1091
Marin Shalamanov9070c052020-05-18 19:05:17 +02001092bool BootAnimation::preloadZip(Animation& animation) {
Mathias Agopiana8826d62009-10-01 03:10:14 -07001093 // read all the data structures
1094 const size_t pcount = animation.parts.size();
Yi Kong911ac232019-03-24 01:49:02 -07001095 void *cookie = nullptr;
Geoffrey Pitschdd214a72016-06-27 17:14:30 -04001096 ZipFileRO* zip = animation.zip;
1097 if (!zip->startIteration(&cookie)) {
Narayan Kamathafd31e02013-12-03 13:16:03 +00001098 return false;
1099 }
1100
1101 ZipEntryRO entry;
1102 char name[ANIM_ENTRY_NAME_MAX];
Yi Kong911ac232019-03-24 01:49:02 -07001103 while ((entry = zip->nextEntry(cookie)) != nullptr) {
Geoffrey Pitschdd214a72016-06-27 17:14:30 -04001104 const int foundEntryName = zip->getEntryFileName(entry, name, ANIM_ENTRY_NAME_MAX);
Narayan Kamathafd31e02013-12-03 13:16:03 +00001105 if (foundEntryName > ANIM_ENTRY_NAME_MAX || foundEntryName == -1) {
Wei Wang159a4102018-10-23 23:15:58 -07001106 SLOGE("Error fetching entry file name");
Narayan Kamathafd31e02013-12-03 13:16:03 +00001107 continue;
1108 }
1109
1110 const String8 entryName(name);
1111 const String8 path(entryName.getPathDir());
1112 const String8 leaf(entryName.getPathLeaf());
1113 if (leaf.size() > 0) {
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -07001114 if (entryName == CLOCK_FONT_ZIP_NAME) {
1115 FileMap* map = zip->createEntryFileMap(entry);
1116 if (map) {
1117 animation.clockFont.map = map;
1118 }
1119 continue;
1120 }
1121
Nicolas Geoffray41fffb42020-12-15 10:50:29 +00001122 if (entryName == PROGRESS_FONT_ZIP_NAME) {
1123 FileMap* map = zip->createEntryFileMap(entry);
1124 if (map) {
1125 animation.progressFont.map = map;
1126 }
1127 continue;
1128 }
1129
Geoffrey Pitschdd214a72016-06-27 17:14:30 -04001130 for (size_t j = 0; j < pcount; j++) {
Narayan Kamathafd31e02013-12-03 13:16:03 +00001131 if (path == animation.parts[j].path) {
Narayan Kamath4600dd02015-06-16 12:02:57 +01001132 uint16_t method;
Narayan Kamathafd31e02013-12-03 13:16:03 +00001133 // supports only stored png files
Yi Kong911ac232019-03-24 01:49:02 -07001134 if (zip->getEntryInfo(entry, &method, nullptr, nullptr, nullptr, nullptr, nullptr)) {
Narayan Kamathafd31e02013-12-03 13:16:03 +00001135 if (method == ZipFileRO::kCompressStored) {
Geoffrey Pitschdd214a72016-06-27 17:14:30 -04001136 FileMap* map = zip->createEntryFileMap(entry);
Narayan Kamathafd31e02013-12-03 13:16:03 +00001137 if (map) {
Narayan Kamathafd31e02013-12-03 13:16:03 +00001138 Animation::Part& part(animation.parts.editItemAt(j));
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -07001139 if (leaf == "audio.wav") {
1140 // a part may have at most one audio file
Geoffrey Pitschd6d9a1d2016-06-08 00:38:58 -07001141 part.audioData = (uint8_t *)map->getDataPtr();
1142 part.audioLength = map->getDataLength();
Geoffrey Pitschdd214a72016-06-27 17:14:30 -04001143 } else if (leaf == "trim.txt") {
1144 part.trimData.setTo((char const*)map->getDataPtr(),
1145 map->getDataLength());
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -07001146 } else {
1147 Animation::Frame frame;
1148 frame.name = leaf;
1149 frame.map = map;
Geoffrey Pitschdd214a72016-06-27 17:14:30 -04001150 frame.trimWidth = animation.width;
1151 frame.trimHeight = animation.height;
1152 frame.trimX = 0;
1153 frame.trimY = 0;
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -07001154 part.frames.add(frame);
1155 }
Mathias Agopiana8826d62009-10-01 03:10:14 -07001156 }
Geoffrey Pitschd6d9a1d2016-06-08 00:38:58 -07001157 } else {
Wei Wang159a4102018-10-23 23:15:58 -07001158 SLOGE("bootanimation.zip is compressed; must be only stored");
Mathias Agopiana8826d62009-10-01 03:10:14 -07001159 }
1160 }
1161 }
1162 }
1163 }
1164 }
1165
Geoffrey Pitschdd214a72016-06-27 17:14:30 -04001166 // If there is trimData present, override the positioning defaults.
1167 for (Animation::Part& part : animation.parts) {
1168 const char* trimDataStr = part.trimData.string();
1169 for (size_t frameIdx = 0; frameIdx < part.frames.size(); frameIdx++) {
1170 const char* endl = strstr(trimDataStr, "\n");
1171 // No more trimData for this part.
Yi Kong911ac232019-03-24 01:49:02 -07001172 if (endl == nullptr) {
Geoffrey Pitschdd214a72016-06-27 17:14:30 -04001173 break;
1174 }
1175 String8 line(trimDataStr, endl - trimDataStr);
1176 const char* lineStr = line.string();
1177 trimDataStr = ++endl;
1178 int width = 0, height = 0, x = 0, y = 0;
1179 if (sscanf(lineStr, "%dx%d+%d+%d", &width, &height, &x, &y) == 4) {
1180 Animation::Frame& frame(part.frames.editItemAt(frameIdx));
1181 frame.trimWidth = width;
1182 frame.trimHeight = height;
1183 frame.trimX = x;
1184 frame.trimY = y;
1185 } else {
Wei Wang159a4102018-10-23 23:15:58 -07001186 SLOGE("Error parsing trim.txt, line: %s", lineStr);
Geoffrey Pitschdd214a72016-06-27 17:14:30 -04001187 break;
1188 }
1189 }
1190 }
1191
1192 zip->endIteration(cookie);
Narayan Kamathafd31e02013-12-03 13:16:03 +00001193
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001194 return true;
1195}
1196
Marin Shalamanov9070c052020-05-18 19:05:17 +02001197bool BootAnimation::movie() {
Huihong Luo50a2f362018-08-11 09:27:57 -07001198 if (mAnimation == nullptr) {
1199 mAnimation = loadAnimation(mZipFileName);
1200 }
1201
1202 if (mAnimation == nullptr)
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001203 return false;
1204
Huihong Luo50a2f362018-08-11 09:27:57 -07001205 // mCallbacks->init() may get called recursively,
1206 // this loop is needed to get the same results
1207 for (const Animation::Part& part : mAnimation->parts) {
1208 if (part.animation != nullptr) {
1209 mCallbacks->init(part.animation->parts);
1210 }
1211 }
1212 mCallbacks->init(mAnimation->parts);
1213
Damien Bargiacchi97480862016-03-29 14:55:55 -07001214 bool anyPartHasClock = false;
Huihong Luo50a2f362018-08-11 09:27:57 -07001215 for (size_t i=0; i < mAnimation->parts.size(); i++) {
1216 if(validClock(mAnimation->parts[i])) {
Damien Bargiacchi97480862016-03-29 14:55:55 -07001217 anyPartHasClock = true;
1218 break;
1219 }
1220 }
1221 if (!anyPartHasClock) {
1222 mClockEnabled = false;
1223 }
1224
Sai Kiran Korwar27167492015-07-07 20:00:06 +05301225 // Check if npot textures are supported
1226 mUseNpotTextures = false;
1227 String8 gl_extensions;
1228 const char* exts = reinterpret_cast<const char*>(glGetString(GL_EXTENSIONS));
1229 if (!exts) {
1230 glGetError();
1231 } else {
1232 gl_extensions.setTo(exts);
1233 if ((gl_extensions.find("GL_ARB_texture_non_power_of_two") != -1) ||
1234 (gl_extensions.find("GL_OES_texture_npot") != -1)) {
1235 mUseNpotTextures = true;
1236 }
1237 }
1238
Damien Bargiacchia704b7d2016-02-16 16:55:49 -08001239 // Blend required to draw time on top of animation frames.
1240 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
Mathias Agopiana8826d62009-10-01 03:10:14 -07001241 glDisable(GL_DITHER);
1242 glDisable(GL_SCISSOR_TEST);
1243 glDisable(GL_BLEND);
Mathias Agopiana8826d62009-10-01 03:10:14 -07001244
Greg Kaiser2ded8002021-07-19 17:03:27 +00001245 glEnable(GL_TEXTURE_2D);
Lucas Dupin8eedc022021-07-29 21:49:07 +00001246 glBindTexture(GL_TEXTURE_2D, 0);
1247 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
1248 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
1249 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
1250 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -07001251 bool clockFontInitialized = false;
Damien Bargiacchia704b7d2016-02-16 16:55:49 -08001252 if (mClockEnabled) {
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -07001253 clockFontInitialized =
Huihong Luo50a2f362018-08-11 09:27:57 -07001254 (initFont(&mAnimation->clockFont, CLOCK_FONT_ASSET) == NO_ERROR);
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -07001255 mClockEnabled = clockFontInitialized;
Damien Bargiacchia704b7d2016-02-16 16:55:49 -08001256 }
1257
Nicolas Geoffray41fffb42020-12-15 10:50:29 +00001258 initFont(&mAnimation->progressFont, PROGRESS_FONT_ASSET);
1259
Damien Bargiacchi97480862016-03-29 14:55:55 -07001260 if (mClockEnabled && !updateIsTimeAccurate()) {
1261 mTimeCheckThread = new TimeCheckThread(this);
1262 mTimeCheckThread->run("BootAnimation::TimeCheckThread", PRIORITY_NORMAL);
1263 }
1264
Huihong Luo50a2f362018-08-11 09:27:57 -07001265 playAnimation(*mAnimation);
Damien Bargiacchi97480862016-03-29 14:55:55 -07001266
Geoffrey Pitscha9173532016-07-19 14:56:39 -04001267 if (mTimeCheckThread != nullptr) {
Damien Bargiacchi97480862016-03-29 14:55:55 -07001268 mTimeCheckThread->requestExit();
Geoffrey Pitscha9173532016-07-19 14:56:39 -04001269 mTimeCheckThread = nullptr;
1270 }
1271
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -07001272 if (clockFontInitialized) {
Huihong Luo50a2f362018-08-11 09:27:57 -07001273 glDeleteTextures(1, &mAnimation->clockFont.texture.name);
Andriy Naborskyy815e51d2016-03-24 16:43:34 -07001274 }
1275
Huihong Luo50a2f362018-08-11 09:27:57 -07001276 releaseAnimation(mAnimation);
1277 mAnimation = nullptr;
Damien Bargiacchi1a8a2132018-07-24 15:20:26 -07001278
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001279 return false;
1280}
1281
Nicolas Geoffraya1298752021-04-20 15:15:06 +01001282bool BootAnimation::shouldStopPlayingPart(const Animation::Part& part,
1283 const int fadedFramesCount,
1284 const int lastDisplayedProgress) {
Yegor Malyshev6925e1b2020-09-10 16:42:20 +02001285 // stop playing only if it is time to exit and it's a partial part which has been faded out
Nicolas Geoffraya1298752021-04-20 15:15:06 +01001286 return exitPending() && !part.playUntilComplete && fadedFramesCount >= part.framesToFadeCount &&
1287 (lastDisplayedProgress == 0 || lastDisplayedProgress == 100);
Yegor Malyshev6925e1b2020-09-10 16:42:20 +02001288}
1289
Lucas Dupin8eedc022021-07-29 21:49:07 +00001290// Linear mapping from range <a1, a2> to range <b1, b2>
1291float mapLinear(float x, float a1, float a2, float b1, float b2) {
1292 return b1 + ( x - a1 ) * ( b2 - b1 ) / ( a2 - a1 );
1293}
1294
1295void BootAnimation::drawTexturedQuad(float xStart, float yStart, float width, float height) {
1296 // Map coordinates from screen space to world space.
1297 float x0 = mapLinear(xStart, 0, mWidth, -1, 1);
1298 float y0 = mapLinear(yStart, 0, mHeight, -1, 1);
1299 float x1 = mapLinear(xStart + width, 0, mWidth, -1, 1);
1300 float y1 = mapLinear(yStart + height, 0, mHeight, -1, 1);
1301 // Update quad vertex positions.
1302 quadPositions[0] = x0;
1303 quadPositions[1] = y0;
1304 quadPositions[2] = x1;
1305 quadPositions[3] = y0;
1306 quadPositions[4] = x1;
1307 quadPositions[5] = y1;
1308 quadPositions[6] = x1;
1309 quadPositions[7] = y1;
1310 quadPositions[8] = x0;
1311 quadPositions[9] = y1;
1312 quadPositions[10] = x0;
1313 quadPositions[11] = y0;
1314 glDrawArrays(GL_TRIANGLES, 0,
1315 sizeof(quadPositions) / sizeof(quadPositions[0]) / 2);
1316}
1317
Marin Shalamanov9070c052020-05-18 19:05:17 +02001318bool BootAnimation::playAnimation(const Animation& animation) {
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001319 const size_t pcount = animation.parts.size();
Mathias Agopiana8826d62009-10-01 03:10:14 -07001320 nsecs_t frameDuration = s2ns(1) / animation.fps;
Mathias Agopian9f3020d2009-11-06 16:30:18 -08001321
Wei Wang159a4102018-10-23 23:15:58 -07001322 SLOGD("%sAnimationShownTiming start time: %" PRId64 "ms", mShuttingDown ? "Shutdown" : "Boot",
Keun-young Parkd1794cd2017-04-04 12:21:19 -07001323 elapsedRealtime());
Yegor Malyshev6925e1b2020-09-10 16:42:20 +02001324
1325 int fadedFramesCount = 0;
Nicolas Geoffray41fffb42020-12-15 10:50:29 +00001326 int lastDisplayedProgress = 0;
Narayan Kamathafd31e02013-12-03 13:16:03 +00001327 for (size_t i=0 ; i<pcount ; i++) {
Mathias Agopiana8826d62009-10-01 03:10:14 -07001328 const Animation::Part& part(animation.parts[i]);
1329 const size_t fcount = part.frames.size();
Mathias Agopiana8826d62009-10-01 03:10:14 -07001330
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001331 // Handle animation package
Yi Kong911ac232019-03-24 01:49:02 -07001332 if (part.animation != nullptr) {
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001333 playAnimation(*part.animation);
1334 if (exitPending())
1335 break;
1336 continue; //to next part
1337 }
1338
Yegor Malyshev6925e1b2020-09-10 16:42:20 +02001339 // process the part not only while the count allows but also if already fading
1340 for (int r=0 ; !part.count || r<part.count || fadedFramesCount > 0 ; r++) {
Nicolas Geoffraya1298752021-04-20 15:15:06 +01001341 if (shouldStopPlayingPart(part, fadedFramesCount, lastDisplayedProgress)) break;
Kevin Hesterd3782b22012-04-26 10:38:55 -07001342
Ed Coyne7464ac92017-06-08 12:26:48 -07001343 mCallbacks->playPart(i, part, r);
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -07001344
Jesse Hall083b84c2014-09-22 10:51:09 -07001345 glClearColor(
1346 part.backgroundColor[0],
1347 part.backgroundColor[1],
1348 part.backgroundColor[2],
1349 1.0f);
1350
Nicolas Geoffray41fffb42020-12-15 10:50:29 +00001351 // For the last animation, if we have progress indicator from
1352 // the system, display it.
1353 int currentProgress = android::base::GetIntProperty(PROGRESS_PROP_NAME, 0);
1354 bool displayProgress = animation.progressEnabled &&
1355 (i == (pcount -1)) && currentProgress != 0;
1356
Yegor Malyshev6925e1b2020-09-10 16:42:20 +02001357 for (size_t j=0 ; j<fcount ; j++) {
Nicolas Geoffraya1298752021-04-20 15:15:06 +01001358 if (shouldStopPlayingPart(part, fadedFramesCount, lastDisplayedProgress)) break;
Yegor Malyshev6925e1b2020-09-10 16:42:20 +02001359
Marin Shalamanov9070c052020-05-18 19:05:17 +02001360 processDisplayEvents();
1361
1362 const int animationX = (mWidth - animation.width) / 2;
1363 const int animationY = (mHeight - animation.height) / 2;
1364
Mathias Agopiana8826d62009-10-01 03:10:14 -07001365 const Animation::Frame& frame(part.frames[j]);
Mathias Agopiandb7dd2a2012-05-12 15:08:21 -07001366 nsecs_t lastFrame = systemTime();
Mathias Agopiana8826d62009-10-01 03:10:14 -07001367
1368 if (r > 0) {
1369 glBindTexture(GL_TEXTURE_2D, frame.tid);
1370 } else {
Lucas Dupin8eedc022021-07-29 21:49:07 +00001371 glGenTextures(1, &frame.tid);
1372 glBindTexture(GL_TEXTURE_2D, frame.tid);
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -07001373 int w, h;
1374 initTexture(frame.map, &w, &h);
Mathias Agopiana8826d62009-10-01 03:10:14 -07001375 }
1376
Geoffrey Pitschdd214a72016-06-27 17:14:30 -04001377 const int xc = animationX + frame.trimX;
1378 const int yc = animationY + frame.trimY;
1379 Region clearReg(Rect(mWidth, mHeight));
1380 clearReg.subtractSelf(Rect(xc, yc, xc+frame.trimWidth, yc+frame.trimHeight));
Mathias Agopian9f3020d2009-11-06 16:30:18 -08001381 if (!clearReg.isEmpty()) {
1382 Region::const_iterator head(clearReg.begin());
1383 Region::const_iterator tail(clearReg.end());
1384 glEnable(GL_SCISSOR_TEST);
1385 while (head != tail) {
Andreas Gampecfedceb2014-09-30 21:48:18 -07001386 const Rect& r2(*head++);
Geoffrey Pitschdd214a72016-06-27 17:14:30 -04001387 glScissor(r2.left, mHeight - r2.bottom, r2.width(), r2.height());
Mathias Agopian9f3020d2009-11-06 16:30:18 -08001388 glClear(GL_COLOR_BUFFER_BIT);
1389 }
1390 glDisable(GL_SCISSOR_TEST);
1391 }
Geoffrey Pitschdd214a72016-06-27 17:14:30 -04001392 // specify the y center as ceiling((mHeight - frame.trimHeight) / 2)
1393 // which is equivalent to mHeight - (yc + frame.trimHeight)
Yegor Malyshev6925e1b2020-09-10 16:42:20 +02001394 const int frameDrawY = mHeight - (yc + frame.trimHeight);
Yegor Malyshev6925e1b2020-09-10 16:42:20 +02001395
Lucas Dupin8eedc022021-07-29 21:49:07 +00001396 float fade = 0;
Yegor Malyshev6925e1b2020-09-10 16:42:20 +02001397 // if the part hasn't been stopped yet then continue fading if necessary
1398 if (exitPending() && part.hasFadingPhase()) {
Lucas Dupin8eedc022021-07-29 21:49:07 +00001399 fade = static_cast<float>(++fadedFramesCount) / part.framesToFadeCount;
Yegor Malyshev6925e1b2020-09-10 16:42:20 +02001400 if (fadedFramesCount >= part.framesToFadeCount) {
1401 fadedFramesCount = MAX_FADED_FRAMES_COUNT; // no more fading
1402 }
1403 }
Lucas Dupin8eedc022021-07-29 21:49:07 +00001404 glUseProgram(mImageShader);
1405 glUniform1i(mImageTextureLocation, 0);
1406 glUniform1f(mImageFadeLocation, fade);
1407 glEnable(GL_BLEND);
1408 drawTexturedQuad(xc, frameDrawY, frame.trimWidth, frame.trimHeight);
1409 glDisable(GL_BLEND);
Yegor Malyshev6925e1b2020-09-10 16:42:20 +02001410
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -07001411 if (mClockEnabled && mTimeIsAccurate && validClock(part)) {
1412 drawClock(animation.clockFont, part.clockPosX, part.clockPosY);
Damien Bargiacchia704b7d2016-02-16 16:55:49 -08001413 }
Yegor Malyshev6925e1b2020-09-10 16:42:20 +02001414
Nicolas Geoffray41fffb42020-12-15 10:50:29 +00001415 if (displayProgress) {
1416 int newProgress = android::base::GetIntProperty(PROGRESS_PROP_NAME, 0);
1417 // In case the new progress jumped suddenly, still show an
1418 // increment of 1.
1419 if (lastDisplayedProgress != 100) {
1420 // Artificially sleep 1/10th a second to slow down the animation.
1421 usleep(100000);
1422 if (lastDisplayedProgress < newProgress) {
1423 lastDisplayedProgress++;
1424 }
1425 }
1426 // Put the progress percentage right below the animation.
1427 int posY = animation.height / 3;
1428 int posX = TEXT_CENTER_VALUE;
1429 drawProgress(lastDisplayedProgress, animation.progressFont, posX, posY);
1430 }
1431
Adrian Roos9ee5dff2018-08-22 20:19:49 +02001432 handleViewport(frameDuration);
Damien Bargiacchia704b7d2016-02-16 16:55:49 -08001433
Mathias Agopiana8826d62009-10-01 03:10:14 -07001434 eglSwapBuffers(mDisplay, mSurface);
1435
1436 nsecs_t now = systemTime();
1437 nsecs_t delay = frameDuration - (now - lastFrame);
Wei Wang159a4102018-10-23 23:15:58 -07001438 //SLOGD("%lld, %lld", ns2ms(now - lastFrame), ns2ms(delay));
Mathias Agopiana8826d62009-10-01 03:10:14 -07001439 lastFrame = now;
Mathias Agopiandb7dd2a2012-05-12 15:08:21 -07001440
1441 if (delay > 0) {
1442 struct timespec spec;
1443 spec.tv_sec = (now + delay) / 1000000000;
1444 spec.tv_nsec = (now + delay) % 1000000000;
1445 int err;
1446 do {
Yi Kong911ac232019-03-24 01:49:02 -07001447 err = clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &spec, nullptr);
Mathias Agopiandb7dd2a2012-05-12 15:08:21 -07001448 } while (err<0 && errno == EINTR);
1449 }
Kevin Hesterd3782b22012-04-26 10:38:55 -07001450
1451 checkExit();
Mathias Agopiana8826d62009-10-01 03:10:14 -07001452 }
Kevin Hesterd3782b22012-04-26 10:38:55 -07001453
Mathias Agopiana8826d62009-10-01 03:10:14 -07001454 usleep(part.pause * ns2us(frameDuration));
Kevin Hesterd3782b22012-04-26 10:38:55 -07001455
Yegor Malyshev6925e1b2020-09-10 16:42:20 +02001456 if (exitPending() && !part.count && mCurrentInset >= mTargetInset &&
1457 !part.hasFadingPhase()) {
Nicolas Geoffraya1298752021-04-20 15:15:06 +01001458 if (lastDisplayedProgress != 0 && lastDisplayedProgress != 100) {
1459 android::base::SetProperty(PROGRESS_PROP_NAME, "100");
1460 continue;
1461 }
Yegor Malyshev6925e1b2020-09-10 16:42:20 +02001462 break; // exit the infinite non-fading part when it has been played at least once
1463 }
Mathias Agopiana8826d62009-10-01 03:10:14 -07001464 }
Geoffrey Pitsch2fb30fb2016-07-06 16:16:20 -04001465 }
1466
1467 // Free textures created for looping parts now that the animation is done.
1468 for (const Animation::Part& part : animation.parts) {
Mathias Agopiana8826d62009-10-01 03:10:14 -07001469 if (part.count != 1) {
Geoffrey Pitsch2fb30fb2016-07-06 16:16:20 -04001470 const size_t fcount = part.frames.size();
1471 for (size_t j = 0; j < fcount; j++) {
Mathias Agopiana8826d62009-10-01 03:10:14 -07001472 const Animation::Frame& frame(part.frames[j]);
1473 glDeleteTextures(1, &frame.tid);
1474 }
1475 }
1476 }
Geoffrey Pitschd6d9a1d2016-06-08 00:38:58 -07001477
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001478 return true;
Mathias Agopiana8826d62009-10-01 03:10:14 -07001479}
1480
Marin Shalamanov9070c052020-05-18 19:05:17 +02001481void BootAnimation::processDisplayEvents() {
1482 // This will poll mDisplayEventReceiver and if there are new events it'll call
1483 // displayEventCallback synchronously.
1484 mLooper->pollOnce(0);
1485}
1486
Adrian Roos9ee5dff2018-08-22 20:19:49 +02001487void BootAnimation::handleViewport(nsecs_t timestep) {
1488 if (mShuttingDown || !mFlingerSurfaceControl || mTargetInset == 0) {
1489 return;
1490 }
1491 if (mTargetInset < 0) {
1492 // Poll the amount for the top display inset. This will return -1 until persistent properties
1493 // have been loaded.
1494 mTargetInset = android::base::GetIntProperty("persist.sys.displayinset.top",
1495 -1 /* default */, -1 /* min */, mHeight / 2 /* max */);
1496 }
1497 if (mTargetInset <= 0) {
1498 return;
1499 }
1500
1501 if (mCurrentInset < mTargetInset) {
1502 // After the device boots, the inset will effectively be cropped away. We animate this here.
1503 float fraction = static_cast<float>(mCurrentInset) / mTargetInset;
1504 int interpolatedInset = (cosf((fraction + 1) * M_PI) / 2.0f + 0.5f) * mTargetInset;
1505
1506 SurfaceComposerClient::Transaction()
1507 .setCrop(mFlingerSurfaceControl, Rect(0, interpolatedInset, mWidth, mHeight))
1508 .apply();
1509 } else {
1510 // At the end of the animation, we switch to the viewport that DisplayManager will apply
1511 // later. This changes the coordinate system, and means we must move the surface up by
1512 // the inset amount.
Adrian Roos9ee5dff2018-08-22 20:19:49 +02001513 Rect layerStackRect(0, 0, mWidth, mHeight - mTargetInset);
1514 Rect displayRect(0, mTargetInset, mWidth, mHeight);
1515
1516 SurfaceComposerClient::Transaction t;
1517 t.setPosition(mFlingerSurfaceControl, 0, -mTargetInset)
1518 .setCrop(mFlingerSurfaceControl, Rect(0, mTargetInset, mWidth, mHeight));
Dominik Laskowski2a3d9aa2019-11-18 20:26:39 -08001519 t.setDisplayProjection(mDisplayToken, ui::ROTATION_0, layerStackRect, displayRect);
Adrian Roos9ee5dff2018-08-22 20:19:49 +02001520 t.apply();
1521
1522 mTargetInset = mCurrentInset = 0;
1523 }
1524
1525 int delta = timestep * mTargetInset / ms2ns(200);
1526 mCurrentInset += delta;
1527}
1528
Marin Shalamanov9070c052020-05-18 19:05:17 +02001529void BootAnimation::releaseAnimation(Animation* animation) const {
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001530 for (Vector<Animation::Part>::iterator it = animation->parts.begin(),
1531 e = animation->parts.end(); it != e; ++it) {
1532 if (it->animation)
1533 releaseAnimation(it->animation);
1534 }
1535 if (animation->zip)
1536 delete animation->zip;
1537 delete animation;
1538}
1539
Marin Shalamanov9070c052020-05-18 19:05:17 +02001540BootAnimation::Animation* BootAnimation::loadAnimation(const String8& fn) {
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001541 if (mLoadedFiles.indexOf(fn) >= 0) {
Wei Wang159a4102018-10-23 23:15:58 -07001542 SLOGE("File \"%s\" is already loaded. Cyclic ref is not allowed",
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001543 fn.string());
Yi Kong911ac232019-03-24 01:49:02 -07001544 return nullptr;
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001545 }
1546 ZipFileRO *zip = ZipFileRO::open(fn);
Yi Kong911ac232019-03-24 01:49:02 -07001547 if (zip == nullptr) {
Wei Wang159a4102018-10-23 23:15:58 -07001548 SLOGE("Failed to open animation zip \"%s\": %s",
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001549 fn.string(), strerror(errno));
Yi Kong911ac232019-03-24 01:49:02 -07001550 return nullptr;
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001551 }
1552
1553 Animation *animation = new Animation;
1554 animation->fileName = fn;
1555 animation->zip = zip;
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -07001556 animation->clockFont.map = nullptr;
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001557 mLoadedFiles.add(animation->fileName);
1558
1559 parseAnimationDesc(*animation);
Geoffrey Pitscha91a2d72016-07-12 14:46:19 -04001560 if (!preloadZip(*animation)) {
Greg Kaiser7426ec82019-09-11 14:34:27 -07001561 releaseAnimation(animation);
Yi Kong911ac232019-03-24 01:49:02 -07001562 return nullptr;
Geoffrey Pitscha91a2d72016-07-12 14:46:19 -04001563 }
1564
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001565 mLoadedFiles.remove(fn);
1566 return animation;
1567}
Damien Bargiacchi97480862016-03-29 14:55:55 -07001568
1569bool BootAnimation::updateIsTimeAccurate() {
1570 static constexpr long long MAX_TIME_IN_PAST = 60000LL * 60LL * 24LL * 30LL; // 30 days
1571 static constexpr long long MAX_TIME_IN_FUTURE = 60000LL * 90LL; // 90 minutes
1572
1573 if (mTimeIsAccurate) {
1574 return true;
1575 }
Keun-young Parkb5938422017-03-23 13:46:24 -07001576 if (mShuttingDown) return true;
Damien Bargiacchi97480862016-03-29 14:55:55 -07001577 struct stat statResult;
Damien Bargiacchi9071db12016-10-28 17:38:22 -07001578
1579 if(stat(TIME_FORMAT_12_HOUR_FLAG_FILE_PATH, &statResult) == 0) {
1580 mTimeFormat12Hour = true;
1581 }
1582
Damien Bargiacchi97480862016-03-29 14:55:55 -07001583 if(stat(ACCURATE_TIME_FLAG_FILE_PATH, &statResult) == 0) {
1584 mTimeIsAccurate = true;
1585 return true;
1586 }
1587
1588 FILE* file = fopen(LAST_TIME_CHANGED_FILE_PATH, "r");
Yi Kong911ac232019-03-24 01:49:02 -07001589 if (file != nullptr) {
Damien Bargiacchi97480862016-03-29 14:55:55 -07001590 long long lastChangedTime = 0;
1591 fscanf(file, "%lld", &lastChangedTime);
1592 fclose(file);
1593 if (lastChangedTime > 0) {
1594 struct timespec now;
1595 clock_gettime(CLOCK_REALTIME, &now);
1596 // Match the Java timestamp format
1597 long long rtcNow = (now.tv_sec * 1000LL) + (now.tv_nsec / 1000000LL);
Damien Bargiacchi96762812016-07-12 15:53:40 -07001598 if (ACCURATE_TIME_EPOCH < rtcNow
1599 && lastChangedTime > (rtcNow - MAX_TIME_IN_PAST)
1600 && lastChangedTime < (rtcNow + MAX_TIME_IN_FUTURE)) {
Damien Bargiacchi97480862016-03-29 14:55:55 -07001601 mTimeIsAccurate = true;
1602 }
1603 }
1604 }
1605
1606 return mTimeIsAccurate;
1607}
1608
1609BootAnimation::TimeCheckThread::TimeCheckThread(BootAnimation* bootAnimation) : Thread(false),
1610 mInotifyFd(-1), mSystemWd(-1), mTimeWd(-1), mBootAnimation(bootAnimation) {}
1611
1612BootAnimation::TimeCheckThread::~TimeCheckThread() {
1613 // mInotifyFd may be -1 but that's ok since we're not at risk of attempting to close a valid FD.
1614 close(mInotifyFd);
1615}
1616
1617bool BootAnimation::TimeCheckThread::threadLoop() {
1618 bool shouldLoop = doThreadLoop() && !mBootAnimation->mTimeIsAccurate
1619 && mBootAnimation->mClockEnabled;
1620 if (!shouldLoop) {
1621 close(mInotifyFd);
1622 mInotifyFd = -1;
1623 }
1624 return shouldLoop;
1625}
1626
1627bool BootAnimation::TimeCheckThread::doThreadLoop() {
1628 static constexpr int BUFF_LEN (10 * (sizeof(struct inotify_event) + NAME_MAX + 1));
1629
1630 // Poll instead of doing a blocking read so the Thread can exit if requested.
1631 struct pollfd pfd = { mInotifyFd, POLLIN, 0 };
1632 ssize_t pollResult = poll(&pfd, 1, 1000);
1633
1634 if (pollResult == 0) {
1635 return true;
1636 } else if (pollResult < 0) {
Wei Wang159a4102018-10-23 23:15:58 -07001637 SLOGE("Could not poll inotify events");
Damien Bargiacchi97480862016-03-29 14:55:55 -07001638 return false;
1639 }
1640
1641 char buff[BUFF_LEN] __attribute__ ((aligned(__alignof__(struct inotify_event))));;
1642 ssize_t length = read(mInotifyFd, buff, BUFF_LEN);
1643 if (length == 0) {
1644 return true;
1645 } else if (length < 0) {
Wei Wang159a4102018-10-23 23:15:58 -07001646 SLOGE("Could not read inotify events");
Damien Bargiacchi97480862016-03-29 14:55:55 -07001647 return false;
1648 }
1649
1650 const struct inotify_event *event;
1651 for (char* ptr = buff; ptr < buff + length; ptr += sizeof(struct inotify_event) + event->len) {
1652 event = (const struct inotify_event *) ptr;
1653 if (event->wd == mSystemWd && strcmp(SYSTEM_TIME_DIR_NAME, event->name) == 0) {
1654 addTimeDirWatch();
1655 } else if (event->wd == mTimeWd && (strcmp(LAST_TIME_CHANGED_FILE_NAME, event->name) == 0
1656 || strcmp(ACCURATE_TIME_FLAG_FILE_NAME, event->name) == 0)) {
1657 return !mBootAnimation->updateIsTimeAccurate();
1658 }
1659 }
1660
1661 return true;
1662}
1663
1664void BootAnimation::TimeCheckThread::addTimeDirWatch() {
1665 mTimeWd = inotify_add_watch(mInotifyFd, SYSTEM_TIME_DIR_PATH,
1666 IN_CLOSE_WRITE | IN_MOVED_TO | IN_ATTRIB);
1667 if (mTimeWd > 0) {
1668 // No need to watch for the time directory to be created if it already exists
1669 inotify_rm_watch(mInotifyFd, mSystemWd);
1670 mSystemWd = -1;
1671 }
1672}
1673
1674status_t BootAnimation::TimeCheckThread::readyToRun() {
1675 mInotifyFd = inotify_init();
1676 if (mInotifyFd < 0) {
Wei Wang159a4102018-10-23 23:15:58 -07001677 SLOGE("Could not initialize inotify fd");
Damien Bargiacchi97480862016-03-29 14:55:55 -07001678 return NO_INIT;
1679 }
1680
1681 mSystemWd = inotify_add_watch(mInotifyFd, SYSTEM_DATA_DIR_PATH, IN_CREATE | IN_ATTRIB);
1682 if (mSystemWd < 0) {
1683 close(mInotifyFd);
1684 mInotifyFd = -1;
Dan Sandler562d46d2020-05-11 17:09:22 -04001685 SLOGE("Could not add watch for %s: %s", SYSTEM_DATA_DIR_PATH, strerror(errno));
Damien Bargiacchi97480862016-03-29 14:55:55 -07001686 return NO_INIT;
1687 }
1688
1689 addTimeDirWatch();
1690
1691 if (mBootAnimation->updateIsTimeAccurate()) {
1692 close(mInotifyFd);
1693 mInotifyFd = -1;
1694 return ALREADY_EXISTS;
1695 }
1696
1697 return NO_ERROR;
1698}
1699
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001700// ---------------------------------------------------------------------------
1701
Marin Shalamanov9070c052020-05-18 19:05:17 +02001702} // namespace android