blob: 87c9fa4dbe28cfc0fbfdd46483d2668d9617c767 [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"
Parth Sane7f7b2212023-04-12 17:23:02 +000019#define ATRACE_TAG ATRACE_TAG_GRAPHICS
Mathias Agopianbc726112009-09-23 15:44:05 -070020
Tomasz Wasilczyk9dcea572023-08-22 20:39:20 +000021#include <filesystem>
Nikita Ioffe06c986e2020-01-27 17:16:03 +000022#include <vector>
23
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080024#include <stdint.h>
Wei Wanga90c54c2017-02-02 11:35:21 -080025#include <inttypes.h>
Damien Bargiacchi97480862016-03-29 14:55:55 -070026#include <sys/inotify.h>
27#include <sys/poll.h>
28#include <sys/stat.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080029#include <sys/types.h>
30#include <math.h>
31#include <fcntl.h>
32#include <utils/misc.h>
Parth Sane7f7b2212023-04-12 17:23:02 +000033#include <utils/Trace.h>
Mathias Agopianb4d5a722009-09-23 17:05:19 -070034#include <signal.h>
Elliott Hughesbb94f312014-10-21 10:41:33 -070035#include <time.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080036
Steven Morelandfb7952f2018-02-23 14:58:50 -080037#include <cutils/atomic.h>
Jason parksbd9a08d2011-01-31 15:04:34 -060038#include <cutils/properties.h>
39
Derek Sollenberger22d0d4c72020-04-08 10:53:46 -040040#include <android/imagedecoder.h>
Mathias Agopianb13b9bd2012-02-17 18:27:36 -080041#include <androidfw/AssetManager.h>
Mathias Agopianac31a3b2009-05-21 19:59:24 -070042#include <binder/IPCThreadState.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080043#include <utils/Errors.h>
44#include <utils/Log.h>
Wei Wanga90c54c2017-02-02 11:35:21 -080045#include <utils/SystemClock.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080046
Keun-young Parkb5938422017-03-23 13:46:24 -070047#include <android-base/properties.h>
48
Marin Shalamanovcb783ca2021-01-29 21:26:07 +010049#include <ui/DisplayMode.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080050#include <ui/PixelFormat.h>
51#include <ui/Rect.h>
52#include <ui/Region.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080053
Jeff Brown0b722fe2012-08-24 22:40:14 -070054#include <gui/ISurfaceComposer.h>
Marin Shalamanov9070c052020-05-18 19:05:17 +020055#include <gui/DisplayEventReceiver.h>
Mathias Agopian8335f1c2012-02-25 18:48:35 -080056#include <gui/Surface.h>
57#include <gui/SurfaceComposerClient.h>
Lucas Dupin8eedc022021-07-29 21:49:07 +000058#include <GLES2/gl2.h>
59#include <GLES2/gl2ext.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080060#include <EGL/eglext.h>
61
62#include "BootAnimation.h"
63
Kyeongkab.Nam35a8a112019-07-17 16:41:48 +090064#define ANIM_PATH_MAX 255
65#define STR(x) #x
66#define STRTO(x) STR(x)
67
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080068namespace android {
69
Marin Shalamanovcb783ca2021-01-29 21:26:07 +010070using ui::DisplayMode;
71
Damien Bargiacchi97480862016-03-29 14:55:55 -070072static const char OEM_BOOTANIMATION_FILE[] = "/oem/media/bootanimation.zip";
jorgetmbd89e9b2024-09-09 19:42:26 +000073static const char PRODUCT_BOOTANIMATION_DIR[] = "/product/media/";
74static const char PRODUCT_BOOTANIMATION_DARK_FILE[] = "bootanimation-dark.zip";
75static const char PRODUCT_BOOTANIMATION_FILE[] = "bootanimation.zip";
Damien Bargiacchi97480862016-03-29 14:55:55 -070076static const char SYSTEM_BOOTANIMATION_FILE[] = "/system/media/bootanimation.zip";
Anders Fridlunde0b4d232018-11-06 14:23:25 +010077static const char APEX_BOOTANIMATION_FILE[] = "/apex/com.android.bootanimation/etc/bootanimation.zip";
Keun-young Parkb5938422017-03-23 13:46:24 -070078static const char OEM_SHUTDOWNANIMATION_FILE[] = "/oem/media/shutdownanimation.zip";
Jaekyun Seokc521bb32018-01-30 11:52:47 +090079static const char PRODUCT_SHUTDOWNANIMATION_FILE[] = "/product/media/shutdownanimation.zip";
Keun-young Parkb5938422017-03-23 13:46:24 -070080static const char SYSTEM_SHUTDOWNANIMATION_FILE[] = "/system/media/shutdownanimation.zip";
81
Nikita Ioffe06c986e2020-01-27 17:16:03 +000082static constexpr const char* PRODUCT_USERSPACE_REBOOT_ANIMATION_FILE = "/product/media/userspace-reboot.zip";
83static constexpr const char* OEM_USERSPACE_REBOOT_ANIMATION_FILE = "/oem/media/userspace-reboot.zip";
84static constexpr const char* SYSTEM_USERSPACE_REBOOT_ANIMATION_FILE = "/system/media/userspace-reboot.zip";
85
Lee George Thomas7a2548b2023-07-25 14:47:39 -070086static const char BOOTANIM_DATA_DIR_PATH[] = "/data/misc/bootanim";
Josh Yangf95b2002021-12-23 14:32:28 -080087static const char BOOTANIM_TIME_DIR_NAME[] = "time";
Lee George Thomas7a2548b2023-07-25 14:47:39 -070088static const char BOOTANIM_TIME_DIR_PATH[] = "/data/misc/bootanim/time";
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -070089static const char CLOCK_FONT_ASSET[] = "images/clock_font.png";
90static const char CLOCK_FONT_ZIP_NAME[] = "clock_font.png";
Nicolas Geoffray41fffb42020-12-15 10:50:29 +000091static const char PROGRESS_FONT_ASSET[] = "images/progress_font.png";
92static const char PROGRESS_FONT_ZIP_NAME[] = "progress_font.png";
Damien Bargiacchi97480862016-03-29 14:55:55 -070093static const char LAST_TIME_CHANGED_FILE_NAME[] = "last_time_change";
Lee George Thomas7a2548b2023-07-25 14:47:39 -070094static const char LAST_TIME_CHANGED_FILE_PATH[] = "/data/misc/bootanim/time/last_time_change";
Damien Bargiacchi97480862016-03-29 14:55:55 -070095static const char ACCURATE_TIME_FLAG_FILE_NAME[] = "time_is_accurate";
Lee George Thomas7a2548b2023-07-25 14:47:39 -070096static const char ACCURATE_TIME_FLAG_FILE_PATH[] = "/data/misc/bootanim/time/time_is_accurate";
97static const char TIME_FORMAT_12_HOUR_FLAG_FILE_PATH[] = "/data/misc/bootanim/time/time_format_12_hour";
Damien Bargiacchi96762812016-07-12 15:53:40 -070098// Java timestamp format. Don't show the clock if the date is before 2000-01-01 00:00:00.
99static const long long ACCURATE_TIME_EPOCH = 946684800000;
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700100static constexpr char FONT_BEGIN_CHAR = ' ';
101static constexpr char FONT_END_CHAR = '~' + 1;
102static constexpr size_t FONT_NUM_CHARS = FONT_END_CHAR - FONT_BEGIN_CHAR + 1;
103static constexpr size_t FONT_NUM_COLS = 16;
104static constexpr size_t FONT_NUM_ROWS = FONT_NUM_CHARS / FONT_NUM_COLS;
105static const int TEXT_CENTER_VALUE = INT_MAX;
106static const int TEXT_MISSING_VALUE = INT_MIN;
Damien Bargiacchi97480862016-03-29 14:55:55 -0700107static const char EXIT_PROP_NAME[] = "service.bootanim.exit";
Nicolas Geoffray41fffb42020-12-15 10:50:29 +0000108static const char PROGRESS_PROP_NAME[] = "service.bootanim.progress";
Thiébaud Weksteen08de4432021-11-18 13:25:59 +1100109static const char CLOCK_ENABLED_PROP_NAME[] = "persist.sys.bootanim.clock.enabled";
Kyeongkab.Nam35a8a112019-07-17 16:41:48 +0900110static const int ANIM_ENTRY_NAME_MAX = ANIM_PATH_MAX + 1;
niuwenchao9cd6ac82022-12-23 17:34:22 +0800111static const int MAX_CHECK_EXIT_INTERVAL_US = 50000;
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700112static constexpr size_t TEXT_POS_LEN_MAX = 16;
Shan Huang2b31a362021-08-16 16:45:39 +0000113static const int DYNAMIC_COLOR_COUNT = 4;
Lucas Dupin8eedc022021-07-29 21:49:07 +0000114static const char U_TEXTURE[] = "uTexture";
115static const char U_FADE[] = "uFade";
116static const char U_CROP_AREA[] = "uCropArea";
Shan Huang2b31a362021-08-16 16:45:39 +0000117static const char U_START_COLOR_PREFIX[] = "uStartColor";
118static const char U_END_COLOR_PREFIX[] = "uEndColor";
119static const char U_COLOR_PROGRESS[] = "uColorProgress";
Lucas Dupin8eedc022021-07-29 21:49:07 +0000120static const char A_UV[] = "aUv";
121static const char A_POSITION[] = "aPosition";
122static const char VERTEX_SHADER_SOURCE[] = R"(
123 precision mediump float;
124 attribute vec4 aPosition;
125 attribute highp vec2 aUv;
126 varying highp vec2 vUv;
127 void main() {
128 gl_Position = aPosition;
129 vUv = aUv;
130 })";
Shan Huang2b31a362021-08-16 16:45:39 +0000131static const char IMAGE_FRAG_DYNAMIC_COLORING_SHADER_SOURCE[] = R"(
132 precision mediump float;
Shan Huang81846ba2021-09-07 19:07:47 +0000133 const float cWhiteMaskThreshold = 0.05;
Shan Huang2b31a362021-08-16 16:45:39 +0000134 uniform sampler2D uTexture;
135 uniform float uFade;
136 uniform float uColorProgress;
Andrew Zengf9f58222022-11-08 11:10:48 -0800137 uniform vec3 uStartColor0;
138 uniform vec3 uStartColor1;
139 uniform vec3 uStartColor2;
140 uniform vec3 uStartColor3;
141 uniform vec3 uEndColor0;
142 uniform vec3 uEndColor1;
143 uniform vec3 uEndColor2;
144 uniform vec3 uEndColor3;
Shan Huang2b31a362021-08-16 16:45:39 +0000145 varying highp vec2 vUv;
146 void main() {
147 vec4 mask = texture2D(uTexture, vUv);
Shan Huang488ff742021-08-25 20:10:46 +0000148 float r = mask.r;
149 float g = mask.g;
150 float b = mask.b;
151 float a = mask.a;
152 // If all channels have values, render pixel as a shade of white.
153 float useWhiteMask = step(cWhiteMaskThreshold, r)
154 * step(cWhiteMaskThreshold, g)
155 * step(cWhiteMaskThreshold, b)
156 * step(cWhiteMaskThreshold, a);
Andrew Zengf9f58222022-11-08 11:10:48 -0800157 vec3 color = r * mix(uStartColor0, uEndColor0, uColorProgress)
Shan Huang488ff742021-08-25 20:10:46 +0000158 + g * mix(uStartColor1, uEndColor1, uColorProgress)
159 + b * mix(uStartColor2, uEndColor2, uColorProgress)
160 + a * mix(uStartColor3, uEndColor3, uColorProgress);
Andrew Zengf9f58222022-11-08 11:10:48 -0800161 color = mix(color, vec3((r + g + b + a) * 0.25), useWhiteMask);
162 gl_FragColor = vec4(color.x, color.y, color.z, (1.0 - uFade));
Shan Huang2b31a362021-08-16 16:45:39 +0000163 })";
Lucas Dupin8eedc022021-07-29 21:49:07 +0000164static const char IMAGE_FRAG_SHADER_SOURCE[] = R"(
165 precision mediump float;
166 uniform sampler2D uTexture;
167 uniform float uFade;
168 varying highp vec2 vUv;
169 void main() {
170 vec4 color = texture2D(uTexture, vUv);
Shan Huang2b31a362021-08-16 16:45:39 +0000171 gl_FragColor = vec4(color.x, color.y, color.z, (1.0 - uFade)) * color.a;
Lucas Dupin8eedc022021-07-29 21:49:07 +0000172 })";
173static const char TEXT_FRAG_SHADER_SOURCE[] = R"(
174 precision mediump float;
175 uniform sampler2D uTexture;
176 uniform vec4 uCropArea;
177 varying highp vec2 vUv;
178 void main() {
179 vec2 uv = vec2(mix(uCropArea.x, uCropArea.z, vUv.x),
180 mix(uCropArea.y, uCropArea.w, vUv.y));
181 gl_FragColor = texture2D(uTexture, uv);
182 })";
183
184static GLfloat quadPositions[] = {
185 -0.5f, -0.5f,
186 +0.5f, -0.5f,
187 +0.5f, +0.5f,
188 +0.5f, +0.5f,
189 -0.5f, +0.5f,
190 -0.5f, -0.5f
191};
192static GLfloat quadUVs[] = {
193 0.0f, 1.0f,
194 1.0f, 1.0f,
195 1.0f, 0.0f,
196 1.0f, 0.0f,
197 0.0f, 0.0f,
198 0.0f, 1.0f
199};
Narayan Kamathafd31e02013-12-03 13:16:03 +0000200
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800201// ---------------------------------------------------------------------------
202
Ed Coyne7464ac92017-06-08 12:26:48 -0700203BootAnimation::BootAnimation(sp<Callbacks> callbacks)
Marin Shalamanov9070c052020-05-18 19:05:17 +0200204 : Thread(false), mLooper(new Looper(false)), mClockEnabled(true), mTimeIsAccurate(false),
Yi Kong911ac232019-03-24 01:49:02 -0700205 mTimeFormat12Hour(false), mTimeCheckThread(nullptr), mCallbacks(callbacks) {
Parth Sane7f7b2212023-04-12 17:23:02 +0000206 ATRACE_CALL();
Mathias Agopian627e7b52009-05-21 19:21:59 -0700207 mSession = new SurfaceComposerClient();
Geoffrey Pitsch290c4352016-08-09 14:35:10 -0400208
Keun-young Parkb5938422017-03-23 13:46:24 -0700209 std::string powerCtl = android::base::GetProperty("sys.powerctl", "");
210 if (powerCtl.empty()) {
211 mShuttingDown = false;
212 } else {
213 mShuttingDown = true;
214 }
Huihong Luo50a2f362018-08-11 09:27:57 -0700215 ALOGD("%sAnimationStartTiming start time: %" PRId64 "ms", mShuttingDown ? "Shutdown" : "Boot",
216 elapsedRealtime());
217}
218
219BootAnimation::~BootAnimation() {
Parth Sane7f7b2212023-04-12 17:23:02 +0000220 ATRACE_CALL();
Huihong Luo50a2f362018-08-11 09:27:57 -0700221 if (mAnimation != nullptr) {
222 releaseAnimation(mAnimation);
223 mAnimation = nullptr;
224 }
225 ALOGD("%sAnimationStopTiming start time: %" PRId64 "ms", mShuttingDown ? "Shutdown" : "Boot",
226 elapsedRealtime());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800227}
228
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800229void BootAnimation::onFirstRef() {
Parth Sane7f7b2212023-04-12 17:23:02 +0000230 ATRACE_CALL();
Mathias Agopianbc726112009-09-23 15:44:05 -0700231 status_t err = mSession->linkToComposerDeath(this);
Wei Wang159a4102018-10-23 23:15:58 -0700232 SLOGE_IF(err, "linkToComposerDeath failed (%s) ", strerror(-err));
Mathias Agopian8434c532009-09-23 18:52:49 -0700233 if (err == NO_ERROR) {
Huihong Luo50a2f362018-08-11 09:27:57 -0700234 // Load the animation content -- this can be slow (eg 200ms)
235 // called before waitForSurfaceFlinger() in main() to avoid wait
236 ALOGD("%sAnimationPreloadTiming start time: %" PRId64 "ms",
237 mShuttingDown ? "Shutdown" : "Boot", elapsedRealtime());
238 preloadAnimation();
jiayongqiang43342022024-07-13 19:46:08 +0800239 ALOGD("%sAnimationPreloadTiming stop time: %" PRId64 "ms",
Huihong Luo50a2f362018-08-11 09:27:57 -0700240 mShuttingDown ? "Shutdown" : "Boot", elapsedRealtime());
Mathias Agopianbc726112009-09-23 15:44:05 -0700241 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800242}
243
Mathias Agopianbc726112009-09-23 15:44:05 -0700244sp<SurfaceComposerClient> BootAnimation::session() const {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800245 return mSession;
246}
247
Marin Shalamanov9070c052020-05-18 19:05:17 +0200248void BootAnimation::binderDied(const wp<IBinder>&) {
Parth Sane7f7b2212023-04-12 17:23:02 +0000249 ATRACE_CALL();
Mathias Agopianbc726112009-09-23 15:44:05 -0700250 // woah, surfaceflinger died!
Wei Wang159a4102018-10-23 23:15:58 -0700251 SLOGD("SurfaceFlinger died, exiting...");
Mathias Agopianbc726112009-09-23 15:44:05 -0700252
253 // calling requestExit() is not enough here because the Surface code
254 // might be blocked on a condition variable that will never be updated.
255 kill( getpid(), SIGKILL );
256 requestExit();
257}
258
Shan Huang2b31a362021-08-16 16:45:39 +0000259static void* decodeImage(const void* encodedData, size_t dataLength, AndroidBitmapInfo* outInfo,
260 bool premultiplyAlpha) {
Parth Sane7f7b2212023-04-12 17:23:02 +0000261 ATRACE_CALL();
Derek Sollenberger22d0d4c72020-04-08 10:53:46 -0400262 AImageDecoder* decoder = nullptr;
263 AImageDecoder_createFromBuffer(encodedData, dataLength, &decoder);
264 if (!decoder) {
265 return nullptr;
266 }
267
268 const AImageDecoderHeaderInfo* info = AImageDecoder_getHeaderInfo(decoder);
269 outInfo->width = AImageDecoderHeaderInfo_getWidth(info);
270 outInfo->height = AImageDecoderHeaderInfo_getHeight(info);
271 outInfo->format = AImageDecoderHeaderInfo_getAndroidBitmapFormat(info);
272 outInfo->stride = AImageDecoder_getMinimumStride(decoder);
273 outInfo->flags = 0;
274
Shan Huang2b31a362021-08-16 16:45:39 +0000275 if (!premultiplyAlpha) {
276 AImageDecoder_setUnpremultipliedRequired(decoder, true);
277 }
278
Derek Sollenberger22d0d4c72020-04-08 10:53:46 -0400279 const size_t size = outInfo->stride * outInfo->height;
280 void* pixels = malloc(size);
281 int result = AImageDecoder_decodeImage(decoder, pixels, outInfo->stride, size);
282 AImageDecoder_delete(decoder);
283
284 if (result != ANDROID_IMAGE_DECODER_SUCCESS) {
285 free(pixels);
286 return nullptr;
287 }
288 return pixels;
289}
290
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800291status_t BootAnimation::initTexture(Texture* texture, AssetManager& assets,
Shan Huang2b31a362021-08-16 16:45:39 +0000292 const char* name, bool premultiplyAlpha) {
Parth Sane7f7b2212023-04-12 17:23:02 +0000293 ATRACE_CALL();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800294 Asset* asset = assets.open(name, Asset::ACCESS_BUFFER);
Yi Kong911ac232019-03-24 01:49:02 -0700295 if (asset == nullptr)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800296 return NO_INIT;
Derek Sollenberger22d0d4c72020-04-08 10:53:46 -0400297
298 AndroidBitmapInfo bitmapInfo;
Shan Huang2b31a362021-08-16 16:45:39 +0000299 void* pixels = decodeImage(asset->getBuffer(false), asset->getLength(), &bitmapInfo,
300 premultiplyAlpha);
Derek Sollenberger22d0d4c72020-04-08 10:53:46 -0400301 auto pixelDeleter = std::unique_ptr<void, decltype(free)*>{ pixels, free };
302
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800303 asset->close();
304 delete asset;
305
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;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800312
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800313 texture->w = w;
314 texture->h = h;
315
316 glGenTextures(1, &texture->name);
317 glBindTexture(GL_TEXTURE_2D, texture->name);
318
Derek Sollenberger22d0d4c72020-04-08 10:53:46 -0400319 switch (bitmapInfo.format) {
320 case ANDROID_BITMAP_FORMAT_A_8:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800321 glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, w, h, 0, GL_ALPHA,
Derek Sollenberger22d0d4c72020-04-08 10:53:46 -0400322 GL_UNSIGNED_BYTE, pixels);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800323 break;
Derek Sollenberger22d0d4c72020-04-08 10:53:46 -0400324 case ANDROID_BITMAP_FORMAT_RGBA_4444:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800325 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA,
Derek Sollenberger22d0d4c72020-04-08 10:53:46 -0400326 GL_UNSIGNED_SHORT_4_4_4_4, pixels);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800327 break;
Derek Sollenberger22d0d4c72020-04-08 10:53:46 -0400328 case ANDROID_BITMAP_FORMAT_RGBA_8888:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800329 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA,
Derek Sollenberger22d0d4c72020-04-08 10:53:46 -0400330 GL_UNSIGNED_BYTE, pixels);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800331 break;
Derek Sollenberger22d0d4c72020-04-08 10:53:46 -0400332 case ANDROID_BITMAP_FORMAT_RGB_565:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800333 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, w, h, 0, GL_RGB,
Derek Sollenberger22d0d4c72020-04-08 10:53:46 -0400334 GL_UNSIGNED_SHORT_5_6_5, pixels);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800335 break;
336 default:
337 break;
338 }
339
Lucas Dupin8eedc022021-07-29 21:49:07 +0000340 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
341 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
342 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
343 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700344
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800345 return NO_ERROR;
346}
347
Shan Huang2b31a362021-08-16 16:45:39 +0000348status_t BootAnimation::initTexture(FileMap* map, int* width, int* height,
349 bool premultiplyAlpha) {
Parth Sane7f7b2212023-04-12 17:23:02 +0000350 ATRACE_CALL();
Derek Sollenberger22d0d4c72020-04-08 10:53:46 -0400351 AndroidBitmapInfo bitmapInfo;
Shan Huang2b31a362021-08-16 16:45:39 +0000352 void* pixels = decodeImage(map->getDataPtr(), map->getDataLength(), &bitmapInfo,
353 premultiplyAlpha);
Derek Sollenberger22d0d4c72020-04-08 10:53:46 -0400354 auto pixelDeleter = std::unique_ptr<void, decltype(free)*>{ pixels, free };
Mathias Agopiana8826d62009-10-01 03:10:14 -0700355
Mykola Kondratenko0c1eeb32014-04-15 09:35:44 +0200356 // FileMap memory is never released until application exit.
357 // Release it now as the texture is already loaded and the memory used for
358 // the packed resource can be released.
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700359 delete map;
Mykola Kondratenko0c1eeb32014-04-15 09:35:44 +0200360
Derek Sollenberger22d0d4c72020-04-08 10:53:46 -0400361 if (!pixels) {
362 return NO_INIT;
363 }
364
365 const int w = bitmapInfo.width;
366 const int h = bitmapInfo.height;
Mathias Agopiana8826d62009-10-01 03:10:14 -0700367
Mathias Agopiana8826d62009-10-01 03:10:14 -0700368 int tw = 1 << (31 - __builtin_clz(w));
369 int th = 1 << (31 - __builtin_clz(h));
370 if (tw < w) tw <<= 1;
371 if (th < h) th <<= 1;
372
Derek Sollenberger22d0d4c72020-04-08 10:53:46 -0400373 switch (bitmapInfo.format) {
374 case ANDROID_BITMAP_FORMAT_RGBA_8888:
Sai Kiran Korwar27167492015-07-07 20:00:06 +0530375 if (!mUseNpotTextures && (tw != w || th != h)) {
Mathias Agopiana8826d62009-10-01 03:10:14 -0700376 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, tw, th, 0, GL_RGBA,
Yi Kong911ac232019-03-24 01:49:02 -0700377 GL_UNSIGNED_BYTE, nullptr);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700378 glTexSubImage2D(GL_TEXTURE_2D, 0,
Derek Sollenberger22d0d4c72020-04-08 10:53:46 -0400379 0, 0, w, h, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700380 } else {
Sai Kiran Korwar27167492015-07-07 20:00:06 +0530381 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA,
Derek Sollenberger22d0d4c72020-04-08 10:53:46 -0400382 GL_UNSIGNED_BYTE, pixels);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700383 }
384 break;
385
Derek Sollenberger22d0d4c72020-04-08 10:53:46 -0400386 case ANDROID_BITMAP_FORMAT_RGB_565:
Sai Kiran Korwar27167492015-07-07 20:00:06 +0530387 if (!mUseNpotTextures && (tw != w || th != h)) {
Mathias Agopiana8826d62009-10-01 03:10:14 -0700388 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, tw, th, 0, GL_RGB,
Yi Kong911ac232019-03-24 01:49:02 -0700389 GL_UNSIGNED_SHORT_5_6_5, nullptr);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700390 glTexSubImage2D(GL_TEXTURE_2D, 0,
Derek Sollenberger22d0d4c72020-04-08 10:53:46 -0400391 0, 0, w, h, GL_RGB, GL_UNSIGNED_SHORT_5_6_5, pixels);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700392 } else {
Sai Kiran Korwar27167492015-07-07 20:00:06 +0530393 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, w, h, 0, GL_RGB,
Derek Sollenberger22d0d4c72020-04-08 10:53:46 -0400394 GL_UNSIGNED_SHORT_5_6_5, pixels);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700395 }
396 break;
397 default:
398 break;
399 }
400
niuwenchaoa76df892023-09-11 17:19:04 +0800401 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
402 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
Lucas Dupin8eedc022021-07-29 21:49:07 +0000403 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
404 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700405
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700406 *width = w;
407 *height = h;
408
Mathias Agopiana8826d62009-10-01 03:10:14 -0700409 return NO_ERROR;
410}
411
Marin Shalamanov9070c052020-05-18 19:05:17 +0200412class BootAnimation::DisplayEventCallback : public LooperCallback {
413 BootAnimation* mBootAnimation;
414
415public:
416 DisplayEventCallback(BootAnimation* bootAnimation) {
Parth Sane7f7b2212023-04-12 17:23:02 +0000417 ATRACE_CALL();
Marin Shalamanov9070c052020-05-18 19:05:17 +0200418 mBootAnimation = bootAnimation;
419 }
420
421 int handleEvent(int /* fd */, int events, void* /* data */) {
Parth Sane7f7b2212023-04-12 17:23:02 +0000422 ATRACE_CALL();
Marin Shalamanov9070c052020-05-18 19:05:17 +0200423 if (events & (Looper::EVENT_ERROR | Looper::EVENT_HANGUP)) {
424 ALOGE("Display event receiver pipe was closed or an error occurred. events=0x%x",
425 events);
426 return 0; // remove the callback
427 }
428
429 if (!(events & Looper::EVENT_INPUT)) {
430 ALOGW("Received spurious callback for unhandled poll event. events=0x%x", events);
431 return 1; // keep the callback
432 }
433
434 constexpr int kBufferSize = 100;
435 DisplayEventReceiver::Event buffer[kBufferSize];
436 ssize_t numEvents;
437 do {
438 numEvents = mBootAnimation->mDisplayEventReceiver->getEvents(buffer, kBufferSize);
439 for (size_t i = 0; i < static_cast<size_t>(numEvents); i++) {
440 const auto& event = buffer[i];
441 if (event.header.type == DisplayEventReceiver::DISPLAY_EVENT_HOTPLUG) {
442 SLOGV("Hotplug received");
443
444 if (!event.hotplug.connected) {
445 // ignore hotplug disconnect
446 continue;
447 }
448 auto token = SurfaceComposerClient::getPhysicalDisplayToken(
449 event.header.displayId);
450
451 if (token != mBootAnimation->mDisplayToken) {
452 // ignore hotplug of a secondary display
453 continue;
454 }
455
Marin Shalamanovcb783ca2021-01-29 21:26:07 +0100456 DisplayMode displayMode;
457 const status_t error = SurfaceComposerClient::getActiveDisplayMode(
458 mBootAnimation->mDisplayToken, &displayMode);
Marin Shalamanov9070c052020-05-18 19:05:17 +0200459 if (error != NO_ERROR) {
Marin Shalamanovcb783ca2021-01-29 21:26:07 +0100460 SLOGE("Can't get active display mode.");
Marin Shalamanov9070c052020-05-18 19:05:17 +0200461 }
Marin Shalamanovcb783ca2021-01-29 21:26:07 +0100462 mBootAnimation->resizeSurface(displayMode.resolution.getWidth(),
463 displayMode.resolution.getHeight());
Marin Shalamanov9070c052020-05-18 19:05:17 +0200464 }
465 }
466 } while (numEvents > 0);
467
468 return 1; // keep the callback
469 }
470};
471
472EGLConfig BootAnimation::getEglConfig(const EGLDisplay& display) {
473 const EGLint attribs[] = {
Andrii Chepurnyid93f7b22023-03-15 11:22:07 +0200474 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
Marin Shalamanov9070c052020-05-18 19:05:17 +0200475 EGL_RED_SIZE, 8,
476 EGL_GREEN_SIZE, 8,
477 EGL_BLUE_SIZE, 8,
478 EGL_DEPTH_SIZE, 0,
479 EGL_NONE
480 };
481 EGLint numConfigs;
482 EGLConfig config;
483 eglChooseConfig(display, attribs, &config, 1, &numConfigs);
484 return config;
485}
486
Marin Shalamanov047802d2020-05-19 23:55:12 +0200487ui::Size BootAnimation::limitSurfaceSize(int width, int height) const {
488 ui::Size limited(width, height);
489 bool wasLimited = false;
490 const float aspectRatio = float(width) / float(height);
491 if (mMaxWidth != 0 && width > mMaxWidth) {
492 limited.height = mMaxWidth / aspectRatio;
493 limited.width = mMaxWidth;
494 wasLimited = true;
495 }
496 if (mMaxHeight != 0 && limited.height > mMaxHeight) {
497 limited.height = mMaxHeight;
498 limited.width = mMaxHeight * aspectRatio;
499 wasLimited = true;
500 }
501 SLOGV_IF(wasLimited, "Surface size has been limited to [%dx%d] from [%dx%d]",
502 limited.width, limited.height, width, height);
503 return limited;
504}
505
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800506status_t BootAnimation::readyToRun() {
Parth Sane7f7b2212023-04-12 17:23:02 +0000507 ATRACE_CALL();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800508 mAssets.addDefaultAssets();
509
Huihong Luo44305102022-09-26 11:16:20 -0700510 const std::vector<PhysicalDisplayId> ids = SurfaceComposerClient::getPhysicalDisplayIds();
511 if (ids.empty()) {
512 SLOGE("Failed to get ID for any displays\n");
Dominik Laskowski69b281d2019-11-22 14:13:12 -0800513 return NAME_NOT_FOUND;
Huihong Luo44305102022-09-26 11:16:20 -0700514 }
Dominik Laskowski3316a0a2019-01-25 02:56:41 -0800515
Rahul Banerjeedcf939f2024-10-04 15:22:15 -0700516 mDisplayToken = SurfaceComposerClient::getPhysicalDisplayToken(ids.front());
Huihong Luo44305102022-09-26 11:16:20 -0700517 if (mDisplayToken == nullptr) {
Rahul Banerjeedcf939f2024-10-04 15:22:15 -0700518 return NAME_NOT_FOUND;
Huihong Luo44305102022-09-26 11:16:20 -0700519 }
520
521 DisplayMode displayMode;
522 const status_t error =
523 SurfaceComposerClient::getActiveDisplayMode(mDisplayToken, &displayMode);
524 if (error != NO_ERROR) {
525 return error;
526 }
527
528 mMaxWidth = android::base::GetIntProperty("ro.surface_flinger.max_graphics_width", 0);
529 mMaxHeight = android::base::GetIntProperty("ro.surface_flinger.max_graphics_height", 0);
530 ui::Size resolution = displayMode.resolution;
531 resolution = limitSurfaceSize(resolution.width, resolution.height);
532 // create the native surface
533 sp<SurfaceControl> control = session()->createSurface(String8("BootAnimation"),
534 resolution.getWidth(), resolution.getHeight(), PIXEL_FORMAT_RGB_565,
535 ISurfaceComposerClient::eOpaque);
536
537 SurfaceComposerClient::Transaction t;
Rahul Banerjeedcf939f2024-10-04 15:22:15 -0700538 t.setDisplayLayerStack(mDisplayToken, ui::DEFAULT_LAYER_STACK);
539 t.setLayerStack(control, ui::DEFAULT_LAYER_STACK);
Huihong Luof97c9e22019-05-29 11:08:01 -0700540
Robert Carre13b58e2017-08-31 14:50:44 -0700541 t.setLayer(control, 0x40000000)
542 .apply();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800543
Mathias Agopian17f638b2009-04-16 20:04:08 -0700544 sp<Surface> s = control->getSurface();
545
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800546 // initialize opengl and egl
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800547 EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
Yi Kong911ac232019-03-24 01:49:02 -0700548 eglInitialize(display, nullptr, nullptr);
Marin Shalamanov9070c052020-05-18 19:05:17 +0200549 EGLConfig config = getEglConfig(display);
550 EGLSurface surface = eglCreateWindowSurface(display, config, s.get(), nullptr);
Lucas Dupin8eedc022021-07-29 21:49:07 +0000551 // Initialize egl context with client version number 2.0.
552 EGLint contextAttributes[] = {EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE};
553 EGLContext context = eglCreateContext(display, config, nullptr, contextAttributes);
Marin Shalamanov9070c052020-05-18 19:05:17 +0200554 EGLint w, h;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800555 eglQuerySurface(display, surface, EGL_WIDTH, &w);
556 eglQuerySurface(display, surface, EGL_HEIGHT, &h);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700557
Huihong Luo44305102022-09-26 11:16:20 -0700558 if (eglMakeCurrent(display, surface, surface, context) == EGL_FALSE) {
Mathias Agopianabac0102009-07-31 14:47:00 -0700559 return NO_INIT;
Huihong Luo44305102022-09-26 11:16:20 -0700560 }
Mathias Agopiana8826d62009-10-01 03:10:14 -0700561
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800562 mDisplay = display;
563 mContext = context;
564 mSurface = surface;
joenchen85b339f2022-04-11 23:58:34 +0800565 mInitWidth = mWidth = w;
566 mInitHeight = mHeight = h;
Mathias Agopian17f638b2009-04-16 20:04:08 -0700567 mFlingerSurfaceControl = control;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800568 mFlingerSurface = s;
Adrian Roos9ee5dff2018-08-22 20:19:49 +0200569 mTargetInset = -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800570
Mariia Sandrikovaa166f012022-12-10 02:32:14 +0000571 // Rotate the boot animation according to the value specified in the sysprop
572 // ro.bootanim.set_orientation_<display_id>. Four values are supported: ORIENTATION_0,
573 // ORIENTATION_90, ORIENTATION_180 and ORIENTATION_270.
574 // If the value isn't specified or is ORIENTATION_0, nothing will be changed.
575 // This is needed to support having boot animation in orientations different from the natural
576 // device orientation. For example, on tablets that may want to keep natural orientation
577 // portrait for applications compatibility and to have the boot animation in landscape.
578 rotateAwayFromNaturalOrientationIfNeeded();
579
Yegor Malyshev6925e1b2020-09-10 16:42:20 +0200580 projectSceneToWindow();
581
Marin Shalamanov9070c052020-05-18 19:05:17 +0200582 // Register a display event receiver
583 mDisplayEventReceiver = std::make_unique<DisplayEventReceiver>();
584 status_t status = mDisplayEventReceiver->initCheck();
585 SLOGE_IF(status != NO_ERROR, "Initialization of DisplayEventReceiver failed with status: %d",
586 status);
587 mLooper->addFd(mDisplayEventReceiver->getFd(), 0, Looper::EVENT_INPUT,
588 new DisplayEventCallback(this), nullptr);
589
Huihong Luo50a2f362018-08-11 09:27:57 -0700590 return NO_ERROR;
591}
592
Mariia Sandrikovaa166f012022-12-10 02:32:14 +0000593void BootAnimation::rotateAwayFromNaturalOrientationIfNeeded() {
Parth Sane7f7b2212023-04-12 17:23:02 +0000594 ATRACE_CALL();
Mariia Sandrikovaa166f012022-12-10 02:32:14 +0000595 const auto orientation = parseOrientationProperty();
596
597 if (orientation == ui::ROTATION_0) {
598 // Do nothing if the sysprop isn't set or is set to ROTATION_0.
599 return;
600 }
601
602 if (orientation == ui::ROTATION_90 || orientation == ui::ROTATION_270) {
603 std::swap(mWidth, mHeight);
604 std::swap(mInitWidth, mInitHeight);
605 mFlingerSurfaceControl->updateDefaultBufferSize(mWidth, mHeight);
606 }
607
608 Rect displayRect(0, 0, mWidth, mHeight);
609 Rect layerStackRect(0, 0, mWidth, mHeight);
610
611 SurfaceComposerClient::Transaction t;
612 t.setDisplayProjection(mDisplayToken, orientation, layerStackRect, displayRect);
613 t.apply();
614}
615
616ui::Rotation BootAnimation::parseOrientationProperty() {
Parth Sane7f7b2212023-04-12 17:23:02 +0000617 ATRACE_CALL();
Mariia Sandrikovaa166f012022-12-10 02:32:14 +0000618 const auto displayIds = SurfaceComposerClient::getPhysicalDisplayIds();
619 if (displayIds.size() == 0) {
620 return ui::ROTATION_0;
621 }
622 const auto displayId = displayIds[0];
623 const auto syspropName = [displayId] {
624 std::stringstream ss;
625 ss << "ro.bootanim.set_orientation_" << displayId.value;
626 return ss.str();
627 }();
YoungJoon Yang9cf990e2023-11-13 15:34:28 +0900628 auto syspropValue = android::base::GetProperty(syspropName, "");
629 if (syspropValue == "") {
630 syspropValue = android::base::GetProperty("ro.bootanim.set_orientation_logical_0", "");
631 }
632
Mariia Sandrikovaa166f012022-12-10 02:32:14 +0000633 if (syspropValue == "ORIENTATION_90") {
634 return ui::ROTATION_90;
635 } else if (syspropValue == "ORIENTATION_180") {
636 return ui::ROTATION_180;
637 } else if (syspropValue == "ORIENTATION_270") {
638 return ui::ROTATION_270;
639 }
640 return ui::ROTATION_0;
641}
642
Yegor Malyshev6925e1b2020-09-10 16:42:20 +0200643void BootAnimation::projectSceneToWindow() {
Parth Sane7f7b2212023-04-12 17:23:02 +0000644 ATRACE_CALL();
Yegor Malyshev6925e1b2020-09-10 16:42:20 +0200645 glViewport(0, 0, mWidth, mHeight);
646 glScissor(0, 0, mWidth, mHeight);
Yegor Malyshev6925e1b2020-09-10 16:42:20 +0200647}
648
Marin Shalamanov9070c052020-05-18 19:05:17 +0200649void BootAnimation::resizeSurface(int newWidth, int newHeight) {
Parth Sane7f7b2212023-04-12 17:23:02 +0000650 ATRACE_CALL();
Marin Shalamanov9070c052020-05-18 19:05:17 +0200651 // We assume this function is called on the animation thread.
652 if (newWidth == mWidth && newHeight == mHeight) {
653 return;
654 }
655 SLOGV("Resizing the boot animation surface to %d %d", newWidth, newHeight);
656
657 eglMakeCurrent(mDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
658 eglDestroySurface(mDisplay, mSurface);
659
Marin Shalamanov047802d2020-05-19 23:55:12 +0200660 const auto limitedSize = limitSurfaceSize(newWidth, newHeight);
661 mWidth = limitedSize.width;
662 mHeight = limitedSize.height;
Marin Shalamanov9070c052020-05-18 19:05:17 +0200663
derek.li92310112024-05-10 17:59:51 +0800664 mFlingerSurfaceControl->updateDefaultBufferSize(mWidth, mHeight);
Marin Shalamanov9070c052020-05-18 19:05:17 +0200665 EGLConfig config = getEglConfig(mDisplay);
666 EGLSurface surface = eglCreateWindowSurface(mDisplay, config, mFlingerSurface.get(), nullptr);
667 if (eglMakeCurrent(mDisplay, surface, surface, mContext) == EGL_FALSE) {
668 SLOGE("Can't make the new surface current. Error %d", eglGetError());
669 return;
670 }
Yegor Malyshev6925e1b2020-09-10 16:42:20 +0200671
672 projectSceneToWindow();
Marin Shalamanov9070c052020-05-18 19:05:17 +0200673
674 mSurface = surface;
675}
676
Huihong Luo50a2f362018-08-11 09:27:57 -0700677bool BootAnimation::preloadAnimation() {
Parth Sane7f7b2212023-04-12 17:23:02 +0000678 ATRACE_CALL();
Huihong Luo50a2f362018-08-11 09:27:57 -0700679 findBootAnimationFile();
Tomasz Wasilczyke8fb3c72023-08-24 19:11:22 +0000680 if (!mZipFileName.empty()) {
Huihong Luo50a2f362018-08-11 09:27:57 -0700681 mAnimation = loadAnimation(mZipFileName);
682 return (mAnimation != nullptr);
683 }
684
685 return false;
686}
687
Nikita Ioffe06c986e2020-01-27 17:16:03 +0000688bool BootAnimation::findBootAnimationFileInternal(const std::vector<std::string> &files) {
Parth Sane7f7b2212023-04-12 17:23:02 +0000689 ATRACE_CALL();
Nikita Ioffe06c986e2020-01-27 17:16:03 +0000690 for (const std::string& f : files) {
691 if (access(f.c_str(), R_OK) == 0) {
692 mZipFileName = f.c_str();
693 return true;
694 }
695 }
696 return false;
697}
698
Huihong Luo50a2f362018-08-11 09:27:57 -0700699void BootAnimation::findBootAnimationFile() {
Parth Sane7f7b2212023-04-12 17:23:02 +0000700 ATRACE_CALL();
Beverly34ffe252019-05-17 11:03:54 -0400701 const bool playDarkAnim = android::base::GetIntProperty("ro.boot.theme", 0) == 1;
jorgetmbd89e9b2024-09-09 19:42:26 +0000702 const std::string productBootanimationFile = PRODUCT_BOOTANIMATION_DIR +
703 android::base::GetProperty("ro.product.bootanim.file", playDarkAnim ?
704 PRODUCT_BOOTANIMATION_DARK_FILE : PRODUCT_BOOTANIMATION_FILE);
Nikita Ioffe06c986e2020-01-27 17:16:03 +0000705 static const std::vector<std::string> bootFiles = {
jorgetmbd89e9b2024-09-09 19:42:26 +0000706 APEX_BOOTANIMATION_FILE, productBootanimationFile,
Nikita Ioffe06c986e2020-01-27 17:16:03 +0000707 OEM_BOOTANIMATION_FILE, SYSTEM_BOOTANIMATION_FILE
708 };
709 static const std::vector<std::string> shutdownFiles = {
710 PRODUCT_SHUTDOWNANIMATION_FILE, OEM_SHUTDOWNANIMATION_FILE, SYSTEM_SHUTDOWNANIMATION_FILE, ""
711 };
712 static const std::vector<std::string> userspaceRebootFiles = {
713 PRODUCT_USERSPACE_REBOOT_ANIMATION_FILE, OEM_USERSPACE_REBOOT_ANIMATION_FILE,
714 SYSTEM_USERSPACE_REBOOT_ANIMATION_FILE,
715 };
Keun-young Parkb5938422017-03-23 13:46:24 -0700716
Nikita Ioffe06c986e2020-01-27 17:16:03 +0000717 if (android::base::GetBoolProperty("sys.init.userspace_reboot.in_progress", false)) {
718 findBootAnimationFileInternal(userspaceRebootFiles);
719 } else if (mShuttingDown) {
720 findBootAnimationFileInternal(shutdownFiles);
721 } else {
722 findBootAnimationFileInternal(bootFiles);
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700723 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800724}
725
Lucas Dupin8eedc022021-07-29 21:49:07 +0000726GLuint compileShader(GLenum shaderType, const GLchar *source) {
Parth Sane7f7b2212023-04-12 17:23:02 +0000727 ATRACE_CALL();
Lucas Dupin8eedc022021-07-29 21:49:07 +0000728 GLuint shader = glCreateShader(shaderType);
729 glShaderSource(shader, 1, &source, 0);
730 glCompileShader(shader);
731 GLint isCompiled = 0;
732 glGetShaderiv(shader, GL_COMPILE_STATUS, &isCompiled);
733 if (isCompiled == GL_FALSE) {
734 SLOGE("Compile shader failed. Shader type: %d", shaderType);
Shan Huang81846ba2021-09-07 19:07:47 +0000735 GLint maxLength = 0;
736 glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &maxLength);
737 std::vector<GLchar> errorLog(maxLength);
738 glGetShaderInfoLog(shader, maxLength, &maxLength, &errorLog[0]);
739 SLOGE("Shader compilation error: %s", &errorLog[0]);
Lucas Dupin8eedc022021-07-29 21:49:07 +0000740 return 0;
741 }
742 return shader;
743}
744
745GLuint linkShader(GLuint vertexShader, GLuint fragmentShader) {
Parth Sane7f7b2212023-04-12 17:23:02 +0000746 ATRACE_CALL();
Lucas Dupin8eedc022021-07-29 21:49:07 +0000747 GLuint program = glCreateProgram();
748 glAttachShader(program, vertexShader);
749 glAttachShader(program, fragmentShader);
750 glLinkProgram(program);
751 GLint isLinked = 0;
752 glGetProgramiv(program, GL_LINK_STATUS, (int *)&isLinked);
753 if (isLinked == GL_FALSE) {
754 SLOGE("Linking shader failed. Shader handles: vert %d, frag %d",
755 vertexShader, fragmentShader);
756 return 0;
757 }
758 return program;
759}
760
761void BootAnimation::initShaders() {
Parth Sane7f7b2212023-04-12 17:23:02 +0000762 ATRACE_CALL();
Shan Huang2b31a362021-08-16 16:45:39 +0000763 bool dynamicColoringEnabled = mAnimation != nullptr && mAnimation->dynamicColoringEnabled;
Lucas Dupin8eedc022021-07-29 21:49:07 +0000764 GLuint vertexShader = compileShader(GL_VERTEX_SHADER, (const GLchar *)VERTEX_SHADER_SOURCE);
765 GLuint imageFragmentShader =
Shan Huang2b31a362021-08-16 16:45:39 +0000766 compileShader(GL_FRAGMENT_SHADER, dynamicColoringEnabled
767 ? (const GLchar *)IMAGE_FRAG_DYNAMIC_COLORING_SHADER_SOURCE
768 : (const GLchar *)IMAGE_FRAG_SHADER_SOURCE);
Lucas Dupin8eedc022021-07-29 21:49:07 +0000769 GLuint textFragmentShader =
770 compileShader(GL_FRAGMENT_SHADER, (const GLchar *)TEXT_FRAG_SHADER_SOURCE);
771
772 // Initialize image shader.
773 mImageShader = linkShader(vertexShader, imageFragmentShader);
774 GLint positionLocation = glGetAttribLocation(mImageShader, A_POSITION);
775 GLint uvLocation = glGetAttribLocation(mImageShader, A_UV);
776 mImageTextureLocation = glGetUniformLocation(mImageShader, U_TEXTURE);
777 mImageFadeLocation = glGetUniformLocation(mImageShader, U_FADE);
778 glEnableVertexAttribArray(positionLocation);
779 glVertexAttribPointer(positionLocation, 2, GL_FLOAT, GL_FALSE, 0, quadPositions);
780 glVertexAttribPointer(uvLocation, 2, GL_FLOAT, GL_FALSE, 0, quadUVs);
781 glEnableVertexAttribArray(uvLocation);
782
783 // Initialize text shader.
784 mTextShader = linkShader(vertexShader, textFragmentShader);
785 positionLocation = glGetAttribLocation(mTextShader, A_POSITION);
786 uvLocation = glGetAttribLocation(mTextShader, A_UV);
787 mTextTextureLocation = glGetUniformLocation(mTextShader, U_TEXTURE);
788 mTextCropAreaLocation = glGetUniformLocation(mTextShader, U_CROP_AREA);
789 glEnableVertexAttribArray(positionLocation);
790 glVertexAttribPointer(positionLocation, 2, GL_FLOAT, GL_FALSE, 0, quadPositions);
791 glVertexAttribPointer(uvLocation, 2, GL_FLOAT, GL_FALSE, 0, quadUVs);
792 glEnableVertexAttribArray(uvLocation);
793}
794
Marin Shalamanov9070c052020-05-18 19:05:17 +0200795bool BootAnimation::threadLoop() {
Parth Sane7f7b2212023-04-12 17:23:02 +0000796 ATRACE_CALL();
Marin Shalamanov9070c052020-05-18 19:05:17 +0200797 bool result;
Lucas Dupin8eedc022021-07-29 21:49:07 +0000798 initShaders();
799
Narayan Kamathafd31e02013-12-03 13:16:03 +0000800 // We have no bootanimation file, so we use the stock android logo
801 // animation.
Tomasz Wasilczyke8fb3c72023-08-24 19:11:22 +0000802 if (mZipFileName.empty()) {
Yoshiaki Maruoka9ef19b82017-10-25 13:55:49 +0900803 ALOGD("No animation file");
Marin Shalamanov9070c052020-05-18 19:05:17 +0200804 result = android();
Mathias Agopiana8826d62009-10-01 03:10:14 -0700805 } else {
Marin Shalamanov9070c052020-05-18 19:05:17 +0200806 result = movie();
Mathias Agopiana8826d62009-10-01 03:10:14 -0700807 }
808
Philip Junker60600562020-11-25 18:16:16 +0100809 mCallbacks->shutdown();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800810 eglMakeCurrent(mDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
811 eglDestroyContext(mDisplay, mContext);
812 eglDestroySurface(mDisplay, mSurface);
Mathias Agopian6cf0db22009-04-17 19:36:26 -0700813 mFlingerSurface.clear();
Mathias Agopian17f638b2009-04-16 20:04:08 -0700814 mFlingerSurfaceControl.clear();
Mathias Agopian627e7b52009-05-21 19:21:59 -0700815 eglTerminate(mDisplay);
Sai Kiran Korwar3eee9fb2015-06-16 17:13:35 +0530816 eglReleaseThread();
Mathias Agopian627e7b52009-05-21 19:21:59 -0700817 IPCThreadState::self()->stopProcess();
Marin Shalamanov9070c052020-05-18 19:05:17 +0200818 return result;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800819}
820
Marin Shalamanov9070c052020-05-18 19:05:17 +0200821bool BootAnimation::android() {
Parth Sane7f7b2212023-04-12 17:23:02 +0000822 ATRACE_CALL();
Lucas Dupin8eedc022021-07-29 21:49:07 +0000823 glActiveTexture(GL_TEXTURE0);
824
Wei Wang159a4102018-10-23 23:15:58 -0700825 SLOGD("%sAnimationShownTiming start time: %" PRId64 "ms", mShuttingDown ? "Shutdown" : "Boot",
Keun-young Parkd1794cd2017-04-04 12:21:19 -0700826 elapsedRealtime());
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700827 initTexture(&mAndroid[0], mAssets, "images/android-logo-mask.png");
828 initTexture(&mAndroid[1], mAssets, "images/android-logo-shine.png");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800829
Ed Coyne7464ac92017-06-08 12:26:48 -0700830 mCallbacks->init({});
831
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800832 // clear screen
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700833 glDisable(GL_DITHER);
834 glDisable(GL_SCISSOR_TEST);
Shan Huangbfd2d3c2021-09-09 23:19:04 +0000835 glUseProgram(mImageShader);
836
Mathias Agopian59f19e42011-05-06 19:22:12 -0700837 glClearColor(0,0,0,1);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800838 glClear(GL_COLOR_BUFFER_BIT);
839 eglSwapBuffers(mDisplay, mSurface);
840
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700841 // Blend state
842 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700843
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800844 const nsecs_t startTime = systemTime();
845 do {
Marin Shalamanov9070c052020-05-18 19:05:17 +0200846 processDisplayEvents();
847 const GLint xc = (mWidth - mAndroid[0].w) / 2;
848 const GLint yc = (mHeight - mAndroid[0].h) / 2;
849 const Rect updateRect(xc, yc, xc + mAndroid[0].w, yc + mAndroid[0].h);
850 glScissor(updateRect.left, mHeight - updateRect.bottom, updateRect.width(),
851 updateRect.height());
852
Mathias Agopian13796652009-03-24 22:49:21 -0700853 nsecs_t now = systemTime();
854 double time = now - startTime;
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700855 float t = 4.0f * float(time / us2ns(16667)) / mAndroid[1].w;
856 GLint offset = (1 - (t - floorf(t))) * mAndroid[1].w;
857 GLint x = xc - offset;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800858
Mathias Agopian81668642009-07-28 11:41:30 -0700859 glDisable(GL_SCISSOR_TEST);
860 glClear(GL_COLOR_BUFFER_BIT);
861
862 glEnable(GL_SCISSOR_TEST);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800863 glDisable(GL_BLEND);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800864 glBindTexture(GL_TEXTURE_2D, mAndroid[1].name);
Lucas Dupin8eedc022021-07-29 21:49:07 +0000865 drawTexturedQuad(x, yc, mAndroid[1].w, mAndroid[1].h);
866 drawTexturedQuad(x + mAndroid[1].w, yc, mAndroid[1].w, mAndroid[1].h);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800867
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700868 glEnable(GL_BLEND);
869 glBindTexture(GL_TEXTURE_2D, mAndroid[0].name);
Lucas Dupin8eedc022021-07-29 21:49:07 +0000870 drawTexturedQuad(xc, yc, mAndroid[0].w, mAndroid[0].h);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800871
Mathias Agopian627e7b52009-05-21 19:21:59 -0700872 EGLBoolean res = eglSwapBuffers(mDisplay, mSurface);
873 if (res == EGL_FALSE)
874 break;
875
Mathias Agopian13796652009-03-24 22:49:21 -0700876 // 12fps: don't animate too fast to preserve CPU
877 const nsecs_t sleepTime = 83333 - ns2us(systemTime() - now);
878 if (sleepTime > 0)
Mathias Agopiana8826d62009-10-01 03:10:14 -0700879 usleep(sleepTime);
Kevin Hesterd3782b22012-04-26 10:38:55 -0700880
881 checkExit();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800882 } while (!exitPending());
883
884 glDeleteTextures(1, &mAndroid[0].name);
885 glDeleteTextures(1, &mAndroid[1].name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800886 return false;
887}
888
Kevin Hesterd3782b22012-04-26 10:38:55 -0700889void BootAnimation::checkExit() {
Parth Sane7f7b2212023-04-12 17:23:02 +0000890 ATRACE_CALL();
Kevin Hesterd3782b22012-04-26 10:38:55 -0700891 // Allow surface flinger to gracefully request shutdown
892 char value[PROPERTY_VALUE_MAX];
893 property_get(EXIT_PROP_NAME, value, "0");
894 int exitnow = atoi(value);
895 if (exitnow) {
896 requestExit();
897 }
898}
899
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700900bool BootAnimation::validClock(const Animation::Part& part) {
Parth Sane7f7b2212023-04-12 17:23:02 +0000901 ATRACE_CALL();
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700902 return part.clockPosX != TEXT_MISSING_VALUE && part.clockPosY != TEXT_MISSING_VALUE;
903}
904
905bool parseTextCoord(const char* str, int* dest) {
Parth Sane7f7b2212023-04-12 17:23:02 +0000906 ATRACE_CALL();
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700907 if (strcmp("c", str) == 0) {
908 *dest = TEXT_CENTER_VALUE;
909 return true;
910 }
911
912 char* end;
913 int val = (int) strtol(str, &end, 0);
914 if (end == str || *end != '\0' || val == INT_MAX || val == INT_MIN) {
915 return false;
916 }
917 *dest = val;
918 return true;
919}
920
921// Parse two position coordinates. If only string is non-empty, treat it as the y value.
922void parsePosition(const char* str1, const char* str2, int* x, int* y) {
Parth Sane7f7b2212023-04-12 17:23:02 +0000923 ATRACE_CALL();
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700924 bool success = false;
925 if (strlen(str1) == 0) { // No values were specified
926 // success = false
927 } else if (strlen(str2) == 0) { // we have only one value
928 if (parseTextCoord(str1, y)) {
929 *x = TEXT_CENTER_VALUE;
930 success = true;
931 }
932 } else {
933 if (parseTextCoord(str1, x) && parseTextCoord(str2, y)) {
934 success = true;
935 }
936 }
937
938 if (!success) {
939 *x = TEXT_MISSING_VALUE;
940 *y = TEXT_MISSING_VALUE;
941 }
942}
943
Jesse Hall083b84c2014-09-22 10:51:09 -0700944// Parse a color represented as an HTML-style 'RRGGBB' string: each pair of
945// characters in str is a hex number in [0, 255], which are converted to
946// floating point values in the range [0.0, 1.0] and placed in the
947// corresponding elements of color.
948//
949// If the input string isn't valid, parseColor returns false and color is
950// left unchanged.
951static bool parseColor(const char str[7], float color[3]) {
Parth Sane7f7b2212023-04-12 17:23:02 +0000952 ATRACE_CALL();
Jesse Hall083b84c2014-09-22 10:51:09 -0700953 float tmpColor[3];
954 for (int i = 0; i < 3; i++) {
955 int val = 0;
956 for (int j = 0; j < 2; j++) {
957 val *= 16;
958 char c = str[2*i + j];
959 if (c >= '0' && c <= '9') val += c - '0';
960 else if (c >= 'A' && c <= 'F') val += (c - 'A') + 10;
961 else if (c >= 'a' && c <= 'f') val += (c - 'a') + 10;
962 else return false;
963 }
964 tmpColor[i] = static_cast<float>(val) / 255.0f;
965 }
966 memcpy(color, tmpColor, sizeof(tmpColor));
967 return true;
968}
969
Shan Huang2b31a362021-08-16 16:45:39 +0000970// Parse a color represented as a signed decimal int string.
971// E.g. "-2757722" (whose hex 2's complement is 0xFFD5EBA6).
972// If the input color string is empty, set color with values in defaultColor.
973static void parseColorDecimalString(const std::string& colorString,
974 float color[3], float defaultColor[3]) {
Parth Sane7f7b2212023-04-12 17:23:02 +0000975 ATRACE_CALL();
Shan Huang2b31a362021-08-16 16:45:39 +0000976 if (colorString == "") {
977 memcpy(color, defaultColor, sizeof(float) * 3);
978 return;
979 }
980 int colorInt = atoi(colorString.c_str());
981 color[0] = ((float)((colorInt >> 16) & 0xFF)) / 0xFF; // r
982 color[1] = ((float)((colorInt >> 8) & 0xFF)) / 0xFF; // g
983 color[2] = ((float)(colorInt & 0xFF)) / 0xFF; // b
984}
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700985
Marin Shalamanov9070c052020-05-18 19:05:17 +0200986static bool readFile(ZipFileRO* zip, const char* name, String8& outString) {
Parth Sane7f7b2212023-04-12 17:23:02 +0000987 ATRACE_CALL();
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700988 ZipEntryRO entry = zip->findEntryByName(name);
Wei Wang159a4102018-10-23 23:15:58 -0700989 SLOGE_IF(!entry, "couldn't find %s", name);
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700990 if (!entry) {
991 return false;
992 }
993
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700994 FileMap* entryMap = zip->createEntryFileMap(entry);
995 zip->releaseEntry(entry);
Wei Wang159a4102018-10-23 23:15:58 -0700996 SLOGE_IF(!entryMap, "entryMap is null");
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700997 if (!entryMap) {
998 return false;
999 }
1000
Tomasz Wasilczyk31eb3c892023-08-23 22:12:33 +00001001 outString = String8((char const*)entryMap->getDataPtr(), entryMap->getDataLength());
Narayan Kamath688ff4c2015-02-23 15:47:54 +00001002 delete entryMap;
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -07001003 return true;
1004}
1005
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -07001006// The font image should be a 96x2 array of character images. The
1007// columns are the printable ASCII characters 0x20 - 0x7f. The
1008// top row is regular text; the bottom row is bold.
1009status_t BootAnimation::initFont(Font* font, const char* fallback) {
Parth Sane7f7b2212023-04-12 17:23:02 +00001010 ATRACE_CALL();
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -07001011 status_t status = NO_ERROR;
Damien Bargiacchia704b7d2016-02-16 16:55:49 -08001012
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -07001013 if (font->map != nullptr) {
1014 glGenTextures(1, &font->texture.name);
1015 glBindTexture(GL_TEXTURE_2D, font->texture.name);
Damien Bargiacchia704b7d2016-02-16 16:55:49 -08001016
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -07001017 status = initTexture(font->map, &font->texture.w, &font->texture.h);
1018
Lucas Dupin8eedc022021-07-29 21:49:07 +00001019 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
1020 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
1021 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
1022 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -07001023 } else if (fallback != nullptr) {
1024 status = initTexture(&font->texture, mAssets, fallback);
1025 } else {
1026 return NO_INIT;
Damien Bargiacchia704b7d2016-02-16 16:55:49 -08001027 }
1028
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -07001029 if (status == NO_ERROR) {
1030 font->char_width = font->texture.w / FONT_NUM_COLS;
1031 font->char_height = font->texture.h / FONT_NUM_ROWS / 2; // There are bold and regular rows
1032 }
1033
1034 return status;
1035}
1036
1037void BootAnimation::drawText(const char* str, const Font& font, bool bold, int* x, int* y) {
Parth Sane7f7b2212023-04-12 17:23:02 +00001038 ATRACE_CALL();
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -07001039 glEnable(GL_BLEND); // Allow us to draw on top of the animation
1040 glBindTexture(GL_TEXTURE_2D, font.texture.name);
Lucas Dupin8eedc022021-07-29 21:49:07 +00001041 glUseProgram(mTextShader);
1042 glUniform1i(mTextTextureLocation, 0);
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -07001043
1044 const int len = strlen(str);
1045 const int strWidth = font.char_width * len;
1046
1047 if (*x == TEXT_CENTER_VALUE) {
1048 *x = (mWidth - strWidth) / 2;
1049 } else if (*x < 0) {
1050 *x = mWidth + *x - strWidth;
1051 }
1052 if (*y == TEXT_CENTER_VALUE) {
1053 *y = (mHeight - font.char_height) / 2;
1054 } else if (*y < 0) {
1055 *y = mHeight + *y - font.char_height;
1056 }
1057
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -07001058 for (int i = 0; i < len; i++) {
1059 char c = str[i];
1060
1061 if (c < FONT_BEGIN_CHAR || c > FONT_END_CHAR) {
1062 c = '?';
1063 }
1064
1065 // Crop the texture to only the pixels in the current glyph
1066 const int charPos = (c - FONT_BEGIN_CHAR); // Position in the list of valid characters
1067 const int row = charPos / FONT_NUM_COLS;
1068 const int col = charPos % FONT_NUM_COLS;
Lucas Dupin8eedc022021-07-29 21:49:07 +00001069 // Bold fonts are expected in the second half of each row.
1070 float v0 = (row + (bold ? 0.5f : 0.0f)) / FONT_NUM_ROWS;
1071 float u0 = ((float)col) / FONT_NUM_COLS;
1072 float v1 = v0 + 1.0f / FONT_NUM_ROWS / 2;
1073 float u1 = u0 + 1.0f / FONT_NUM_COLS;
1074 glUniform4f(mTextCropAreaLocation, u0, v0, u1, v1);
1075 drawTexturedQuad(*x, *y, font.char_width, font.char_height);
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -07001076
1077 *x += font.char_width;
1078 }
1079
1080 glDisable(GL_BLEND); // Return to the animation's default behaviour
1081 glBindTexture(GL_TEXTURE_2D, 0);
1082}
1083
Damien Bargiacchi9071db12016-10-28 17:38:22 -07001084// We render 12 or 24 hour time.
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -07001085void BootAnimation::drawClock(const Font& font, const int xPos, const int yPos) {
Parth Sane7f7b2212023-04-12 17:23:02 +00001086 ATRACE_CALL();
Damien Bargiacchi9071db12016-10-28 17:38:22 -07001087 static constexpr char TIME_FORMAT_12[] = "%l:%M";
1088 static constexpr char TIME_FORMAT_24[] = "%H:%M";
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -07001089 static constexpr int TIME_LENGTH = 6;
1090
Damien Bargiacchia704b7d2016-02-16 16:55:49 -08001091 time_t rawtime;
1092 time(&rawtime);
1093 struct tm* timeInfo = localtime(&rawtime);
1094
1095 char timeBuff[TIME_LENGTH];
Damien Bargiacchi9071db12016-10-28 17:38:22 -07001096 const char* timeFormat = mTimeFormat12Hour ? TIME_FORMAT_12 : TIME_FORMAT_24;
1097 size_t length = strftime(timeBuff, TIME_LENGTH, timeFormat, timeInfo);
Damien Bargiacchia704b7d2016-02-16 16:55:49 -08001098
1099 if (length != TIME_LENGTH - 1) {
Wei Wang159a4102018-10-23 23:15:58 -07001100 SLOGE("Couldn't format time; abandoning boot animation clock");
Damien Bargiacchia704b7d2016-02-16 16:55:49 -08001101 mClockEnabled = false;
1102 return;
1103 }
1104
Damien Bargiacchi45a76442016-12-05 18:02:18 -08001105 char* out = timeBuff[0] == ' ' ? &timeBuff[1] : &timeBuff[0];
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -07001106 int x = xPos;
1107 int y = yPos;
Damien Bargiacchi45a76442016-12-05 18:02:18 -08001108 drawText(out, font, false, &x, &y);
Damien Bargiacchia704b7d2016-02-16 16:55:49 -08001109}
1110
Nicolas Geoffray41fffb42020-12-15 10:50:29 +00001111void BootAnimation::drawProgress(int percent, const Font& font, const int xPos, const int yPos) {
Parth Sane7f7b2212023-04-12 17:23:02 +00001112 ATRACE_CALL();
Nicolas Geoffray41fffb42020-12-15 10:50:29 +00001113 static constexpr int PERCENT_LENGTH = 5;
1114
1115 char percentBuff[PERCENT_LENGTH];
1116 // ';' has the ascii code just after ':', and the font resource contains '%'
1117 // for that ascii code.
1118 sprintf(percentBuff, "%d;", percent);
1119 int x = xPos;
1120 int y = yPos;
1121 drawText(percentBuff, font, false, &x, &y);
1122}
1123
Marin Shalamanov9070c052020-05-18 19:05:17 +02001124bool BootAnimation::parseAnimationDesc(Animation& animation) {
Parth Sane7f7b2212023-04-12 17:23:02 +00001125 ATRACE_CALL();
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -07001126 String8 desString;
1127
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001128 if (!readFile(animation.zip, "desc.txt", desString)) {
Narayan Kamathafd31e02013-12-03 13:16:03 +00001129 return false;
1130 }
Tomasz Wasilczyk3815d342023-08-10 23:54:44 +00001131 char const* s = desString.c_str();
Shan Huang2b31a362021-08-16 16:45:39 +00001132 std::string dynamicColoringPartName = "";
1133 bool postDynamicColoring = false;
Mathias Agopiana8826d62009-10-01 03:10:14 -07001134
Mathias Agopiana8826d62009-10-01 03:10:14 -07001135 // Parse the description file
1136 for (;;) {
1137 const char* endl = strstr(s, "\n");
Yi Kong911ac232019-03-24 01:49:02 -07001138 if (endl == nullptr) break;
Mathias Agopiana8826d62009-10-01 03:10:14 -07001139 String8 line(s, endl - s);
Tomasz Wasilczyk3815d342023-08-10 23:54:44 +00001140 const char* l = line.c_str();
Damien Bargiacchia704b7d2016-02-16 16:55:49 -08001141 int fps = 0;
1142 int width = 0;
1143 int height = 0;
1144 int count = 0;
1145 int pause = 0;
Nicolas Geoffray41fffb42020-12-15 10:50:29 +00001146 int progress = 0;
Yegor Malyshev6925e1b2020-09-10 16:42:20 +02001147 int framesToFadeCount = 0;
Shan Huang488ff742021-08-25 20:10:46 +00001148 int colorTransitionStart = 0;
1149 int colorTransitionEnd = 0;
Narayan Kamathafd31e02013-12-03 13:16:03 +00001150 char path[ANIM_ENTRY_NAME_MAX];
Jesse Hall083b84c2014-09-22 10:51:09 -07001151 char color[7] = "000000"; // default to black if unspecified
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -07001152 char clockPos1[TEXT_POS_LEN_MAX + 1] = "";
1153 char clockPos2[TEXT_POS_LEN_MAX + 1] = "";
Shan Huang2b31a362021-08-16 16:45:39 +00001154 char dynamicColoringPartNameBuffer[ANIM_ENTRY_NAME_MAX];
Kevin Hesterd3782b22012-04-26 10:38:55 -07001155 char pathType;
Shan Huang2b31a362021-08-16 16:45:39 +00001156 // start colors default to black if unspecified
1157 char start_color_0[7] = "000000";
1158 char start_color_1[7] = "000000";
1159 char start_color_2[7] = "000000";
1160 char start_color_3[7] = "000000";
Yegor Malyshev6925e1b2020-09-10 16:42:20 +02001161
1162 int nextReadPos;
1163
yidong zhang87bb3462022-12-07 15:55:31 +08001164 if (strlen(l) == 0) {
1165 s = ++endl;
1166 continue;
1167 }
1168
Nicolas Geoffray41fffb42020-12-15 10:50:29 +00001169 int topLineNumbers = sscanf(l, "%d %d %d %d", &width, &height, &fps, &progress);
1170 if (topLineNumbers == 3 || topLineNumbers == 4) {
1171 // SLOGD("> w=%d, h=%d, fps=%d, progress=%d", width, height, fps, progress);
Mathias Agopiana8826d62009-10-01 03:10:14 -07001172 animation.width = width;
1173 animation.height = height;
1174 animation.fps = fps;
Nicolas Geoffray41fffb42020-12-15 10:50:29 +00001175 if (topLineNumbers == 4) {
1176 animation.progressEnabled = (progress != 0);
1177 } else {
1178 animation.progressEnabled = false;
1179 }
Shan Huang488ff742021-08-25 20:10:46 +00001180 } else if (sscanf(l, "dynamic_colors %" STRTO(ANIM_PATH_MAX) "s #%6s #%6s #%6s #%6s %d %d",
Shan Huang2b31a362021-08-16 16:45:39 +00001181 dynamicColoringPartNameBuffer,
Shan Huang488ff742021-08-25 20:10:46 +00001182 start_color_0, start_color_1, start_color_2, start_color_3,
1183 &colorTransitionStart, &colorTransitionEnd)) {
Shan Huang2b31a362021-08-16 16:45:39 +00001184 animation.dynamicColoringEnabled = true;
1185 parseColor(start_color_0, animation.startColors[0]);
1186 parseColor(start_color_1, animation.startColors[1]);
1187 parseColor(start_color_2, animation.startColors[2]);
1188 parseColor(start_color_3, animation.startColors[3]);
Shan Huang488ff742021-08-25 20:10:46 +00001189 animation.colorTransitionStart = colorTransitionStart;
1190 animation.colorTransitionEnd = colorTransitionEnd;
Shan Huang2b31a362021-08-16 16:45:39 +00001191 dynamicColoringPartName = std::string(dynamicColoringPartNameBuffer);
Yegor Malyshev6925e1b2020-09-10 16:42:20 +02001192 } else if (sscanf(l, "%c %d %d %" STRTO(ANIM_PATH_MAX) "s%n",
1193 &pathType, &count, &pause, path, &nextReadPos) >= 4) {
1194 if (pathType == 'f') {
1195 sscanf(l + nextReadPos, " %d #%6s %16s %16s", &framesToFadeCount, color, clockPos1,
1196 clockPos2);
1197 } else {
1198 sscanf(l + nextReadPos, " #%6s %16s %16s", color, clockPos1, clockPos2);
1199 }
1200 // SLOGD("> type=%c, count=%d, pause=%d, path=%s, framesToFadeCount=%d, color=%s, "
1201 // "clockPos1=%s, clockPos2=%s",
1202 // pathType, count, pause, path, framesToFadeCount, color, clockPos1, clockPos2);
Mathias Agopiana8826d62009-10-01 03:10:14 -07001203 Animation::Part part;
Shan Huang2b31a362021-08-16 16:45:39 +00001204 if (path == dynamicColoringPartName) {
1205 // Part is specified to use dynamic coloring.
1206 part.useDynamicColoring = true;
1207 part.postDynamicColoring = false;
1208 postDynamicColoring = true;
1209 } else {
1210 // Part does not use dynamic coloring.
1211 part.useDynamicColoring = false;
1212 part.postDynamicColoring = postDynamicColoring;
1213 }
Kevin Hesterd3782b22012-04-26 10:38:55 -07001214 part.playUntilComplete = pathType == 'c';
Yegor Malyshev6925e1b2020-09-10 16:42:20 +02001215 part.framesToFadeCount = framesToFadeCount;
Mathias Agopiana8826d62009-10-01 03:10:14 -07001216 part.count = count;
1217 part.pause = pause;
1218 part.path = path;
Yi Kong911ac232019-03-24 01:49:02 -07001219 part.audioData = nullptr;
1220 part.animation = nullptr;
Jesse Hall083b84c2014-09-22 10:51:09 -07001221 if (!parseColor(color, part.backgroundColor)) {
Wei Wang159a4102018-10-23 23:15:58 -07001222 SLOGE("> invalid color '#%s'", color);
Jesse Hall083b84c2014-09-22 10:51:09 -07001223 part.backgroundColor[0] = 0.0f;
1224 part.backgroundColor[1] = 0.0f;
1225 part.backgroundColor[2] = 0.0f;
1226 }
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -07001227 parsePosition(clockPos1, clockPos2, &part.clockPosX, &part.clockPosY);
Mathias Agopiana8826d62009-10-01 03:10:14 -07001228 animation.parts.add(part);
1229 }
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001230 else if (strcmp(l, "$SYSTEM") == 0) {
Wei Wang159a4102018-10-23 23:15:58 -07001231 // SLOGD("> SYSTEM");
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001232 Animation::Part part;
1233 part.playUntilComplete = false;
Yegor Malyshev6925e1b2020-09-10 16:42:20 +02001234 part.framesToFadeCount = 0;
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001235 part.count = 1;
1236 part.pause = 0;
Yi Kong911ac232019-03-24 01:49:02 -07001237 part.audioData = nullptr;
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001238 part.animation = loadAnimation(String8(SYSTEM_BOOTANIMATION_FILE));
Yi Kong911ac232019-03-24 01:49:02 -07001239 if (part.animation != nullptr)
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001240 animation.parts.add(part);
1241 }
Mathias Agopiana8826d62009-10-01 03:10:14 -07001242 s = ++endl;
1243 }
1244
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001245 return true;
1246}
1247
Marin Shalamanov9070c052020-05-18 19:05:17 +02001248bool BootAnimation::preloadZip(Animation& animation) {
Parth Sane7f7b2212023-04-12 17:23:02 +00001249 ATRACE_CALL();
Mathias Agopiana8826d62009-10-01 03:10:14 -07001250 // read all the data structures
1251 const size_t pcount = animation.parts.size();
Yi Kong911ac232019-03-24 01:49:02 -07001252 void *cookie = nullptr;
Geoffrey Pitschdd214a72016-06-27 17:14:30 -04001253 ZipFileRO* zip = animation.zip;
1254 if (!zip->startIteration(&cookie)) {
Narayan Kamathafd31e02013-12-03 13:16:03 +00001255 return false;
1256 }
1257
1258 ZipEntryRO entry;
1259 char name[ANIM_ENTRY_NAME_MAX];
Yi Kong911ac232019-03-24 01:49:02 -07001260 while ((entry = zip->nextEntry(cookie)) != nullptr) {
Geoffrey Pitschdd214a72016-06-27 17:14:30 -04001261 const int foundEntryName = zip->getEntryFileName(entry, name, ANIM_ENTRY_NAME_MAX);
Narayan Kamathafd31e02013-12-03 13:16:03 +00001262 if (foundEntryName > ANIM_ENTRY_NAME_MAX || foundEntryName == -1) {
Wei Wang159a4102018-10-23 23:15:58 -07001263 SLOGE("Error fetching entry file name");
Narayan Kamathafd31e02013-12-03 13:16:03 +00001264 continue;
1265 }
1266
Tomasz Wasilczyk9dcea572023-08-22 20:39:20 +00001267 const std::filesystem::path entryName(name);
1268 const std::filesystem::path path(entryName.parent_path());
1269 const std::filesystem::path leaf(entryName.filename());
1270 if (!leaf.empty()) {
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -07001271 if (entryName == CLOCK_FONT_ZIP_NAME) {
1272 FileMap* map = zip->createEntryFileMap(entry);
1273 if (map) {
1274 animation.clockFont.map = map;
1275 }
1276 continue;
1277 }
1278
Nicolas Geoffray41fffb42020-12-15 10:50:29 +00001279 if (entryName == PROGRESS_FONT_ZIP_NAME) {
1280 FileMap* map = zip->createEntryFileMap(entry);
1281 if (map) {
1282 animation.progressFont.map = map;
1283 }
1284 continue;
1285 }
1286
Geoffrey Pitschdd214a72016-06-27 17:14:30 -04001287 for (size_t j = 0; j < pcount; j++) {
Tomasz Wasilczyk9dcea572023-08-22 20:39:20 +00001288 if (path.string() == animation.parts[j].path.c_str()) {
Narayan Kamath4600dd02015-06-16 12:02:57 +01001289 uint16_t method;
Narayan Kamathafd31e02013-12-03 13:16:03 +00001290 // supports only stored png files
Pawan Wagh8efc3c42024-06-03 21:43:13 +00001291 if (zip->getEntryInfo(entry, &method, nullptr, nullptr, nullptr, nullptr,
1292 nullptr, nullptr)) {
Narayan Kamathafd31e02013-12-03 13:16:03 +00001293 if (method == ZipFileRO::kCompressStored) {
Geoffrey Pitschdd214a72016-06-27 17:14:30 -04001294 FileMap* map = zip->createEntryFileMap(entry);
Narayan Kamathafd31e02013-12-03 13:16:03 +00001295 if (map) {
Narayan Kamathafd31e02013-12-03 13:16:03 +00001296 Animation::Part& part(animation.parts.editItemAt(j));
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -07001297 if (leaf == "audio.wav") {
1298 // a part may have at most one audio file
Geoffrey Pitschd6d9a1d2016-06-08 00:38:58 -07001299 part.audioData = (uint8_t *)map->getDataPtr();
1300 part.audioLength = map->getDataLength();
Geoffrey Pitschdd214a72016-06-27 17:14:30 -04001301 } else if (leaf == "trim.txt") {
Tomasz Wasilczyk31eb3c892023-08-23 22:12:33 +00001302 part.trimData = String8((char const*)map->getDataPtr(),
Geoffrey Pitschdd214a72016-06-27 17:14:30 -04001303 map->getDataLength());
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -07001304 } else {
1305 Animation::Frame frame;
Tomasz Wasilczyk9dcea572023-08-22 20:39:20 +00001306 frame.name = leaf.c_str();
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -07001307 frame.map = map;
Geoffrey Pitschdd214a72016-06-27 17:14:30 -04001308 frame.trimWidth = animation.width;
1309 frame.trimHeight = animation.height;
1310 frame.trimX = 0;
1311 frame.trimY = 0;
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -07001312 part.frames.add(frame);
1313 }
Mathias Agopiana8826d62009-10-01 03:10:14 -07001314 }
Geoffrey Pitschd6d9a1d2016-06-08 00:38:58 -07001315 } else {
Wei Wang159a4102018-10-23 23:15:58 -07001316 SLOGE("bootanimation.zip is compressed; must be only stored");
Mathias Agopiana8826d62009-10-01 03:10:14 -07001317 }
1318 }
1319 }
1320 }
1321 }
1322 }
1323
Geoffrey Pitschdd214a72016-06-27 17:14:30 -04001324 // If there is trimData present, override the positioning defaults.
1325 for (Animation::Part& part : animation.parts) {
Tomasz Wasilczyk3815d342023-08-10 23:54:44 +00001326 const char* trimDataStr = part.trimData.c_str();
Geoffrey Pitschdd214a72016-06-27 17:14:30 -04001327 for (size_t frameIdx = 0; frameIdx < part.frames.size(); frameIdx++) {
1328 const char* endl = strstr(trimDataStr, "\n");
1329 // No more trimData for this part.
Yi Kong911ac232019-03-24 01:49:02 -07001330 if (endl == nullptr) {
Geoffrey Pitschdd214a72016-06-27 17:14:30 -04001331 break;
1332 }
1333 String8 line(trimDataStr, endl - trimDataStr);
Tomasz Wasilczyk3815d342023-08-10 23:54:44 +00001334 const char* lineStr = line.c_str();
Geoffrey Pitschdd214a72016-06-27 17:14:30 -04001335 trimDataStr = ++endl;
1336 int width = 0, height = 0, x = 0, y = 0;
1337 if (sscanf(lineStr, "%dx%d+%d+%d", &width, &height, &x, &y) == 4) {
1338 Animation::Frame& frame(part.frames.editItemAt(frameIdx));
1339 frame.trimWidth = width;
1340 frame.trimHeight = height;
1341 frame.trimX = x;
1342 frame.trimY = y;
1343 } else {
Wei Wang159a4102018-10-23 23:15:58 -07001344 SLOGE("Error parsing trim.txt, line: %s", lineStr);
Geoffrey Pitschdd214a72016-06-27 17:14:30 -04001345 break;
1346 }
1347 }
1348 }
1349
1350 zip->endIteration(cookie);
Narayan Kamathafd31e02013-12-03 13:16:03 +00001351
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001352 return true;
1353}
1354
Marin Shalamanov9070c052020-05-18 19:05:17 +02001355bool BootAnimation::movie() {
Parth Sane7f7b2212023-04-12 17:23:02 +00001356 ATRACE_CALL();
Huihong Luo50a2f362018-08-11 09:27:57 -07001357 if (mAnimation == nullptr) {
1358 mAnimation = loadAnimation(mZipFileName);
1359 }
1360
1361 if (mAnimation == nullptr)
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001362 return false;
1363
Huihong Luo50a2f362018-08-11 09:27:57 -07001364 // mCallbacks->init() may get called recursively,
1365 // this loop is needed to get the same results
1366 for (const Animation::Part& part : mAnimation->parts) {
1367 if (part.animation != nullptr) {
1368 mCallbacks->init(part.animation->parts);
1369 }
1370 }
1371 mCallbacks->init(mAnimation->parts);
1372
Damien Bargiacchi97480862016-03-29 14:55:55 -07001373 bool anyPartHasClock = false;
Huihong Luo50a2f362018-08-11 09:27:57 -07001374 for (size_t i=0; i < mAnimation->parts.size(); i++) {
1375 if(validClock(mAnimation->parts[i])) {
Damien Bargiacchi97480862016-03-29 14:55:55 -07001376 anyPartHasClock = true;
1377 break;
1378 }
1379 }
1380 if (!anyPartHasClock) {
1381 mClockEnabled = false;
Thiébaud Weksteen08de4432021-11-18 13:25:59 +11001382 } else if (!android::base::GetBoolProperty(CLOCK_ENABLED_PROP_NAME, false)) {
1383 mClockEnabled = false;
Damien Bargiacchi97480862016-03-29 14:55:55 -07001384 }
1385
Sai Kiran Korwar27167492015-07-07 20:00:06 +05301386 // Check if npot textures are supported
1387 mUseNpotTextures = false;
1388 String8 gl_extensions;
1389 const char* exts = reinterpret_cast<const char*>(glGetString(GL_EXTENSIONS));
1390 if (!exts) {
1391 glGetError();
1392 } else {
Tomasz Wasilczyk31eb3c892023-08-23 22:12:33 +00001393 gl_extensions = exts;
Sai Kiran Korwar27167492015-07-07 20:00:06 +05301394 if ((gl_extensions.find("GL_ARB_texture_non_power_of_two") != -1) ||
1395 (gl_extensions.find("GL_OES_texture_npot") != -1)) {
1396 mUseNpotTextures = true;
1397 }
1398 }
1399
Damien Bargiacchia704b7d2016-02-16 16:55:49 -08001400 // Blend required to draw time on top of animation frames.
1401 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
Mathias Agopiana8826d62009-10-01 03:10:14 -07001402 glDisable(GL_DITHER);
1403 glDisable(GL_SCISSOR_TEST);
1404 glDisable(GL_BLEND);
Mathias Agopiana8826d62009-10-01 03:10:14 -07001405
Greg Kaiser2ded8002021-07-19 17:03:27 +00001406 glEnable(GL_TEXTURE_2D);
Lucas Dupin8eedc022021-07-29 21:49:07 +00001407 glBindTexture(GL_TEXTURE_2D, 0);
1408 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
1409 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
1410 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
1411 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -07001412 bool clockFontInitialized = false;
Damien Bargiacchia704b7d2016-02-16 16:55:49 -08001413 if (mClockEnabled) {
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -07001414 clockFontInitialized =
Huihong Luo50a2f362018-08-11 09:27:57 -07001415 (initFont(&mAnimation->clockFont, CLOCK_FONT_ASSET) == NO_ERROR);
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -07001416 mClockEnabled = clockFontInitialized;
Damien Bargiacchia704b7d2016-02-16 16:55:49 -08001417 }
1418
Nicolas Geoffray41fffb42020-12-15 10:50:29 +00001419 initFont(&mAnimation->progressFont, PROGRESS_FONT_ASSET);
1420
Damien Bargiacchi97480862016-03-29 14:55:55 -07001421 if (mClockEnabled && !updateIsTimeAccurate()) {
1422 mTimeCheckThread = new TimeCheckThread(this);
1423 mTimeCheckThread->run("BootAnimation::TimeCheckThread", PRIORITY_NORMAL);
1424 }
1425
George Burgess IV0f8e8112021-08-23 17:32:23 -07001426 if (mAnimation->dynamicColoringEnabled) {
Shan Huang7fee3522021-08-18 00:56:08 +00001427 initDynamicColors();
1428 }
1429
Huihong Luo50a2f362018-08-11 09:27:57 -07001430 playAnimation(*mAnimation);
Damien Bargiacchi97480862016-03-29 14:55:55 -07001431
Geoffrey Pitscha9173532016-07-19 14:56:39 -04001432 if (mTimeCheckThread != nullptr) {
Damien Bargiacchi97480862016-03-29 14:55:55 -07001433 mTimeCheckThread->requestExit();
Geoffrey Pitscha9173532016-07-19 14:56:39 -04001434 mTimeCheckThread = nullptr;
1435 }
1436
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -07001437 if (clockFontInitialized) {
Huihong Luo50a2f362018-08-11 09:27:57 -07001438 glDeleteTextures(1, &mAnimation->clockFont.texture.name);
Andriy Naborskyy815e51d2016-03-24 16:43:34 -07001439 }
1440
Huihong Luo50a2f362018-08-11 09:27:57 -07001441 releaseAnimation(mAnimation);
1442 mAnimation = nullptr;
Damien Bargiacchi1a8a2132018-07-24 15:20:26 -07001443
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001444 return false;
1445}
1446
Nicolas Geoffraya1298752021-04-20 15:15:06 +01001447bool BootAnimation::shouldStopPlayingPart(const Animation::Part& part,
1448 const int fadedFramesCount,
1449 const int lastDisplayedProgress) {
Parth Sane7f7b2212023-04-12 17:23:02 +00001450 ATRACE_CALL();
Yegor Malyshev6925e1b2020-09-10 16:42:20 +02001451 // 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 +01001452 return exitPending() && !part.playUntilComplete && fadedFramesCount >= part.framesToFadeCount &&
1453 (lastDisplayedProgress == 0 || lastDisplayedProgress == 100);
Yegor Malyshev6925e1b2020-09-10 16:42:20 +02001454}
1455
Lucas Dupin8eedc022021-07-29 21:49:07 +00001456// Linear mapping from range <a1, a2> to range <b1, b2>
1457float mapLinear(float x, float a1, float a2, float b1, float b2) {
1458 return b1 + ( x - a1 ) * ( b2 - b1 ) / ( a2 - a1 );
1459}
1460
1461void BootAnimation::drawTexturedQuad(float xStart, float yStart, float width, float height) {
Parth Sane7f7b2212023-04-12 17:23:02 +00001462 ATRACE_CALL();
Lucas Dupin8eedc022021-07-29 21:49:07 +00001463 // Map coordinates from screen space to world space.
1464 float x0 = mapLinear(xStart, 0, mWidth, -1, 1);
1465 float y0 = mapLinear(yStart, 0, mHeight, -1, 1);
1466 float x1 = mapLinear(xStart + width, 0, mWidth, -1, 1);
1467 float y1 = mapLinear(yStart + height, 0, mHeight, -1, 1);
1468 // Update quad vertex positions.
1469 quadPositions[0] = x0;
1470 quadPositions[1] = y0;
1471 quadPositions[2] = x1;
1472 quadPositions[3] = y0;
1473 quadPositions[4] = x1;
1474 quadPositions[5] = y1;
1475 quadPositions[6] = x1;
1476 quadPositions[7] = y1;
1477 quadPositions[8] = x0;
1478 quadPositions[9] = y1;
1479 quadPositions[10] = x0;
1480 quadPositions[11] = y0;
1481 glDrawArrays(GL_TRIANGLES, 0,
1482 sizeof(quadPositions) / sizeof(quadPositions[0]) / 2);
1483}
1484
Shan Huang7fee3522021-08-18 00:56:08 +00001485void BootAnimation::initDynamicColors() {
Parth Sane7f7b2212023-04-12 17:23:02 +00001486 ATRACE_CALL();
Shan Huang7fee3522021-08-18 00:56:08 +00001487 for (int i = 0; i < DYNAMIC_COLOR_COUNT; i++) {
Lucas Dupincd256842022-03-29 15:46:36 -07001488 const auto syspropName = "persist.bootanim.color" + std::to_string(i + 1);
1489 const auto syspropValue = android::base::GetProperty(syspropName, "");
1490 if (syspropValue != "") {
1491 SLOGI("Loaded dynamic color: %s -> %s", syspropName.c_str(), syspropValue.c_str());
1492 mDynamicColorsApplied = true;
1493 }
1494 parseColorDecimalString(syspropValue,
Shan Huang7fee3522021-08-18 00:56:08 +00001495 mAnimation->endColors[i], mAnimation->startColors[i]);
1496 }
1497 glUseProgram(mImageShader);
Lucas Dupincd256842022-03-29 15:46:36 -07001498 SLOGI("Dynamically coloring boot animation. Sysprops loaded? %i", mDynamicColorsApplied);
Shan Huang7fee3522021-08-18 00:56:08 +00001499 for (int i = 0; i < DYNAMIC_COLOR_COUNT; i++) {
1500 float *startColor = mAnimation->startColors[i];
1501 float *endColor = mAnimation->endColors[i];
Andrew Zengf9f58222022-11-08 11:10:48 -08001502 glUniform3f(glGetUniformLocation(mImageShader,
Shan Huang7fee3522021-08-18 00:56:08 +00001503 (U_START_COLOR_PREFIX + std::to_string(i)).c_str()),
Andrew Zengf9f58222022-11-08 11:10:48 -08001504 startColor[0], startColor[1], startColor[2]);
1505 glUniform3f(glGetUniformLocation(mImageShader,
Shan Huang7fee3522021-08-18 00:56:08 +00001506 (U_END_COLOR_PREFIX + std::to_string(i)).c_str()),
Andrew Zengf9f58222022-11-08 11:10:48 -08001507 endColor[0], endColor[1], endColor[2]);
Shan Huang7fee3522021-08-18 00:56:08 +00001508 }
1509 mImageColorProgressLocation = glGetUniformLocation(mImageShader, U_COLOR_PROGRESS);
1510}
1511
Marin Shalamanov9070c052020-05-18 19:05:17 +02001512bool BootAnimation::playAnimation(const Animation& animation) {
Parth Sane7f7b2212023-04-12 17:23:02 +00001513 ATRACE_CALL();
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001514 const size_t pcount = animation.parts.size();
Mathias Agopiana8826d62009-10-01 03:10:14 -07001515 nsecs_t frameDuration = s2ns(1) / animation.fps;
Mathias Agopian9f3020d2009-11-06 16:30:18 -08001516
Wei Wang159a4102018-10-23 23:15:58 -07001517 SLOGD("%sAnimationShownTiming start time: %" PRId64 "ms", mShuttingDown ? "Shutdown" : "Boot",
Keun-young Parkd1794cd2017-04-04 12:21:19 -07001518 elapsedRealtime());
Yegor Malyshev6925e1b2020-09-10 16:42:20 +02001519
1520 int fadedFramesCount = 0;
Nicolas Geoffray41fffb42020-12-15 10:50:29 +00001521 int lastDisplayedProgress = 0;
Lucas Dupinbfd18122022-04-25 11:26:08 -07001522 int colorTransitionStart = animation.colorTransitionStart;
1523 int colorTransitionEnd = animation.colorTransitionEnd;
Narayan Kamathafd31e02013-12-03 13:16:03 +00001524 for (size_t i=0 ; i<pcount ; i++) {
Mathias Agopiana8826d62009-10-01 03:10:14 -07001525 const Animation::Part& part(animation.parts[i]);
1526 const size_t fcount = part.frames.size();
Oleg Lyovin6a4b84d2023-07-01 14:39:59 +03001527 glBindTexture(GL_TEXTURE_2D, 0);
Mathias Agopiana8826d62009-10-01 03:10:14 -07001528
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001529 // Handle animation package
Yi Kong911ac232019-03-24 01:49:02 -07001530 if (part.animation != nullptr) {
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001531 playAnimation(*part.animation);
1532 if (exitPending())
1533 break;
1534 continue; //to next part
1535 }
1536
Yegor Malyshev6925e1b2020-09-10 16:42:20 +02001537 // process the part not only while the count allows but also if already fading
1538 for (int r=0 ; !part.count || r<part.count || fadedFramesCount > 0 ; r++) {
Nicolas Geoffraya1298752021-04-20 15:15:06 +01001539 if (shouldStopPlayingPart(part, fadedFramesCount, lastDisplayedProgress)) break;
Kevin Hesterd3782b22012-04-26 10:38:55 -07001540
Lucas Dupinbfd18122022-04-25 11:26:08 -07001541 // It's possible that the sysprops were not loaded yet at this boot phase.
1542 // If that's the case, then we should keep trying until they are available.
1543 if (animation.dynamicColoringEnabled && !mDynamicColorsApplied
1544 && (part.useDynamicColoring || part.postDynamicColoring)) {
1545 SLOGD("Trying to load dynamic color sysprops.");
1546 initDynamicColors();
1547 if (mDynamicColorsApplied) {
1548 // Sysprops were loaded. Next step is to adjust the animation if we loaded
1549 // the colors after the animation should have started.
1550 const int transitionLength = colorTransitionEnd - colorTransitionStart;
1551 if (part.postDynamicColoring) {
1552 colorTransitionStart = 0;
1553 colorTransitionEnd = fmin(transitionLength, fcount - 1);
1554 }
1555 }
1556 }
1557
Ed Coyne7464ac92017-06-08 12:26:48 -07001558 mCallbacks->playPart(i, part, r);
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -07001559
Jesse Hall083b84c2014-09-22 10:51:09 -07001560 glClearColor(
1561 part.backgroundColor[0],
1562 part.backgroundColor[1],
1563 part.backgroundColor[2],
1564 1.0f);
1565
Yoshiaki Maruoka9ef19b82017-10-25 13:55:49 +09001566 ALOGD("Playing files = %s/%s, Requested repeat = %d, playUntilComplete = %s",
Tomasz Wasilczyk3815d342023-08-10 23:54:44 +00001567 animation.fileName.c_str(), part.path.c_str(), part.count,
Yoshiaki Maruoka9ef19b82017-10-25 13:55:49 +09001568 part.playUntilComplete ? "true" : "false");
1569
Nicolas Geoffray41fffb42020-12-15 10:50:29 +00001570 // For the last animation, if we have progress indicator from
1571 // the system, display it.
1572 int currentProgress = android::base::GetIntProperty(PROGRESS_PROP_NAME, 0);
1573 bool displayProgress = animation.progressEnabled &&
1574 (i == (pcount -1)) && currentProgress != 0;
1575
Yegor Malyshev6925e1b2020-09-10 16:42:20 +02001576 for (size_t j=0 ; j<fcount ; j++) {
Nicolas Geoffraya1298752021-04-20 15:15:06 +01001577 if (shouldStopPlayingPart(part, fadedFramesCount, lastDisplayedProgress)) break;
Yegor Malyshev6925e1b2020-09-10 16:42:20 +02001578
Shan Huang2b31a362021-08-16 16:45:39 +00001579 // Color progress is
Shan Huang488ff742021-08-25 20:10:46 +00001580 // - the animation progress, normalized from
1581 // [colorTransitionStart,colorTransitionEnd] to [0, 1] for the dynamic coloring
1582 // part.
Shan Huang2b31a362021-08-16 16:45:39 +00001583 // - 0 for parts that come before,
1584 // - 1 for parts that come after.
1585 float colorProgress = part.useDynamicColoring
Shan Huang488ff742021-08-25 20:10:46 +00001586 ? fmin(fmax(
Lucas Dupinbfd18122022-04-25 11:26:08 -07001587 ((float)j - colorTransitionStart) /
1588 fmax(colorTransitionEnd - colorTransitionStart, 1.0f), 0.0f), 1.0f)
Shan Huang2b31a362021-08-16 16:45:39 +00001589 : (part.postDynamicColoring ? 1 : 0);
1590
Marin Shalamanov9070c052020-05-18 19:05:17 +02001591 processDisplayEvents();
1592
joenchen85b339f2022-04-11 23:58:34 +08001593 const double ratio_w = static_cast<double>(mWidth) / mInitWidth;
1594 const double ratio_h = static_cast<double>(mHeight) / mInitHeight;
1595 const int animationX = (mWidth - animation.width * ratio_w) / 2;
1596 const int animationY = (mHeight - animation.height * ratio_h) / 2;
Marin Shalamanov9070c052020-05-18 19:05:17 +02001597
Mathias Agopiana8826d62009-10-01 03:10:14 -07001598 const Animation::Frame& frame(part.frames[j]);
Mathias Agopiandb7dd2a2012-05-12 15:08:21 -07001599 nsecs_t lastFrame = systemTime();
Mathias Agopiana8826d62009-10-01 03:10:14 -07001600
1601 if (r > 0) {
1602 glBindTexture(GL_TEXTURE_2D, frame.tid);
1603 } else {
Oleg Lyovin6a4b84d2023-07-01 14:39:59 +03001604 if (part.count != 1) {
1605 glGenTextures(1, &frame.tid);
1606 glBindTexture(GL_TEXTURE_2D, frame.tid);
1607 }
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -07001608 int w, h;
Shan Huang2b31a362021-08-16 16:45:39 +00001609 // Set decoding option to alpha unpremultiplied so that the R, G, B channels
1610 // of transparent pixels are preserved.
1611 initTexture(frame.map, &w, &h, false /* don't premultiply alpha */);
Mathias Agopiana8826d62009-10-01 03:10:14 -07001612 }
1613
joenchen85b339f2022-04-11 23:58:34 +08001614 const int trimWidth = frame.trimWidth * ratio_w;
1615 const int trimHeight = frame.trimHeight * ratio_h;
1616 const int trimX = frame.trimX * ratio_w;
1617 const int trimY = frame.trimY * ratio_h;
1618 const int xc = animationX + trimX;
1619 const int yc = animationY + trimY;
Shan Huang2b31a362021-08-16 16:45:39 +00001620 glClear(GL_COLOR_BUFFER_BIT);
Geoffrey Pitschdd214a72016-06-27 17:14:30 -04001621 // specify the y center as ceiling((mHeight - frame.trimHeight) / 2)
1622 // which is equivalent to mHeight - (yc + frame.trimHeight)
joenchen85b339f2022-04-11 23:58:34 +08001623 const int frameDrawY = mHeight - (yc + trimHeight);
Yegor Malyshev6925e1b2020-09-10 16:42:20 +02001624
Lucas Dupin8eedc022021-07-29 21:49:07 +00001625 float fade = 0;
Yegor Malyshev6925e1b2020-09-10 16:42:20 +02001626 // if the part hasn't been stopped yet then continue fading if necessary
1627 if (exitPending() && part.hasFadingPhase()) {
Lucas Dupin8eedc022021-07-29 21:49:07 +00001628 fade = static_cast<float>(++fadedFramesCount) / part.framesToFadeCount;
Yegor Malyshev6925e1b2020-09-10 16:42:20 +02001629 if (fadedFramesCount >= part.framesToFadeCount) {
1630 fadedFramesCount = MAX_FADED_FRAMES_COUNT; // no more fading
1631 }
1632 }
Lucas Dupin8eedc022021-07-29 21:49:07 +00001633 glUseProgram(mImageShader);
1634 glUniform1i(mImageTextureLocation, 0);
1635 glUniform1f(mImageFadeLocation, fade);
Shan Huang2b31a362021-08-16 16:45:39 +00001636 if (animation.dynamicColoringEnabled) {
1637 glUniform1f(mImageColorProgressLocation, colorProgress);
1638 }
Lucas Dupin8eedc022021-07-29 21:49:07 +00001639 glEnable(GL_BLEND);
joenchen85b339f2022-04-11 23:58:34 +08001640 drawTexturedQuad(xc, frameDrawY, trimWidth, trimHeight);
Lucas Dupin8eedc022021-07-29 21:49:07 +00001641 glDisable(GL_BLEND);
Yegor Malyshev6925e1b2020-09-10 16:42:20 +02001642
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -07001643 if (mClockEnabled && mTimeIsAccurate && validClock(part)) {
1644 drawClock(animation.clockFont, part.clockPosX, part.clockPosY);
Damien Bargiacchia704b7d2016-02-16 16:55:49 -08001645 }
Yegor Malyshev6925e1b2020-09-10 16:42:20 +02001646
Nicolas Geoffray41fffb42020-12-15 10:50:29 +00001647 if (displayProgress) {
1648 int newProgress = android::base::GetIntProperty(PROGRESS_PROP_NAME, 0);
1649 // In case the new progress jumped suddenly, still show an
1650 // increment of 1.
1651 if (lastDisplayedProgress != 100) {
1652 // Artificially sleep 1/10th a second to slow down the animation.
1653 usleep(100000);
1654 if (lastDisplayedProgress < newProgress) {
1655 lastDisplayedProgress++;
1656 }
1657 }
1658 // Put the progress percentage right below the animation.
1659 int posY = animation.height / 3;
1660 int posX = TEXT_CENTER_VALUE;
1661 drawProgress(lastDisplayedProgress, animation.progressFont, posX, posY);
1662 }
1663
Adrian Roos9ee5dff2018-08-22 20:19:49 +02001664 handleViewport(frameDuration);
Damien Bargiacchia704b7d2016-02-16 16:55:49 -08001665
Mathias Agopiana8826d62009-10-01 03:10:14 -07001666 eglSwapBuffers(mDisplay, mSurface);
1667
1668 nsecs_t now = systemTime();
1669 nsecs_t delay = frameDuration - (now - lastFrame);
Wei Wang159a4102018-10-23 23:15:58 -07001670 //SLOGD("%lld, %lld", ns2ms(now - lastFrame), ns2ms(delay));
Mathias Agopiana8826d62009-10-01 03:10:14 -07001671 lastFrame = now;
Mathias Agopiandb7dd2a2012-05-12 15:08:21 -07001672
1673 if (delay > 0) {
1674 struct timespec spec;
1675 spec.tv_sec = (now + delay) / 1000000000;
1676 spec.tv_nsec = (now + delay) % 1000000000;
1677 int err;
1678 do {
Yi Kong911ac232019-03-24 01:49:02 -07001679 err = clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &spec, nullptr);
Keith Mok0f2e6682021-11-15 05:50:32 +00001680 } while (err == EINTR);
Mathias Agopiandb7dd2a2012-05-12 15:08:21 -07001681 }
Kevin Hesterd3782b22012-04-26 10:38:55 -07001682
1683 checkExit();
Mathias Agopiana8826d62009-10-01 03:10:14 -07001684 }
Kevin Hesterd3782b22012-04-26 10:38:55 -07001685
niuwenchao9cd6ac82022-12-23 17:34:22 +08001686 int pauseDuration = part.pause * ns2us(frameDuration);
1687 while(pauseDuration > 0 && !exitPending()){
1688 if (pauseDuration > MAX_CHECK_EXIT_INTERVAL_US) {
1689 usleep(MAX_CHECK_EXIT_INTERVAL_US);
1690 pauseDuration -= MAX_CHECK_EXIT_INTERVAL_US;
1691 } else {
1692 usleep(pauseDuration);
1693 break;
1694 }
1695 checkExit();
1696 }
Kevin Hesterd3782b22012-04-26 10:38:55 -07001697
Yegor Malyshev6925e1b2020-09-10 16:42:20 +02001698 if (exitPending() && !part.count && mCurrentInset >= mTargetInset &&
1699 !part.hasFadingPhase()) {
Nicolas Geoffraya1298752021-04-20 15:15:06 +01001700 if (lastDisplayedProgress != 0 && lastDisplayedProgress != 100) {
1701 android::base::SetProperty(PROGRESS_PROP_NAME, "100");
1702 continue;
1703 }
Yegor Malyshev6925e1b2020-09-10 16:42:20 +02001704 break; // exit the infinite non-fading part when it has been played at least once
1705 }
Mathias Agopiana8826d62009-10-01 03:10:14 -07001706 }
Geoffrey Pitsch2fb30fb2016-07-06 16:16:20 -04001707 }
1708
1709 // Free textures created for looping parts now that the animation is done.
1710 for (const Animation::Part& part : animation.parts) {
Mathias Agopiana8826d62009-10-01 03:10:14 -07001711 if (part.count != 1) {
Geoffrey Pitsch2fb30fb2016-07-06 16:16:20 -04001712 const size_t fcount = part.frames.size();
1713 for (size_t j = 0; j < fcount; j++) {
Mathias Agopiana8826d62009-10-01 03:10:14 -07001714 const Animation::Frame& frame(part.frames[j]);
1715 glDeleteTextures(1, &frame.tid);
1716 }
1717 }
1718 }
Geoffrey Pitschd6d9a1d2016-06-08 00:38:58 -07001719
Yoshiaki Maruoka9ef19b82017-10-25 13:55:49 +09001720 ALOGD("%sAnimationShownTiming End time: %" PRId64 "ms", mShuttingDown ? "Shutdown" : "Boot",
1721 elapsedRealtime());
1722
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001723 return true;
Mathias Agopiana8826d62009-10-01 03:10:14 -07001724}
1725
Marin Shalamanov9070c052020-05-18 19:05:17 +02001726void BootAnimation::processDisplayEvents() {
Parth Sane7f7b2212023-04-12 17:23:02 +00001727 ATRACE_CALL();
Marin Shalamanov9070c052020-05-18 19:05:17 +02001728 // This will poll mDisplayEventReceiver and if there are new events it'll call
1729 // displayEventCallback synchronously.
1730 mLooper->pollOnce(0);
1731}
1732
Adrian Roos9ee5dff2018-08-22 20:19:49 +02001733void BootAnimation::handleViewport(nsecs_t timestep) {
Parth Sane7f7b2212023-04-12 17:23:02 +00001734 ATRACE_CALL();
Adrian Roos9ee5dff2018-08-22 20:19:49 +02001735 if (mShuttingDown || !mFlingerSurfaceControl || mTargetInset == 0) {
1736 return;
1737 }
1738 if (mTargetInset < 0) {
1739 // Poll the amount for the top display inset. This will return -1 until persistent properties
1740 // have been loaded.
1741 mTargetInset = android::base::GetIntProperty("persist.sys.displayinset.top",
1742 -1 /* default */, -1 /* min */, mHeight / 2 /* max */);
1743 }
1744 if (mTargetInset <= 0) {
1745 return;
1746 }
1747
1748 if (mCurrentInset < mTargetInset) {
1749 // After the device boots, the inset will effectively be cropped away. We animate this here.
1750 float fraction = static_cast<float>(mCurrentInset) / mTargetInset;
1751 int interpolatedInset = (cosf((fraction + 1) * M_PI) / 2.0f + 0.5f) * mTargetInset;
1752
1753 SurfaceComposerClient::Transaction()
1754 .setCrop(mFlingerSurfaceControl, Rect(0, interpolatedInset, mWidth, mHeight))
1755 .apply();
1756 } else {
1757 // At the end of the animation, we switch to the viewport that DisplayManager will apply
1758 // later. This changes the coordinate system, and means we must move the surface up by
1759 // the inset amount.
Adrian Roos9ee5dff2018-08-22 20:19:49 +02001760 Rect layerStackRect(0, 0, mWidth, mHeight - mTargetInset);
1761 Rect displayRect(0, mTargetInset, mWidth, mHeight);
1762
1763 SurfaceComposerClient::Transaction t;
1764 t.setPosition(mFlingerSurfaceControl, 0, -mTargetInset)
1765 .setCrop(mFlingerSurfaceControl, Rect(0, mTargetInset, mWidth, mHeight));
Dominik Laskowski2a3d9aa2019-11-18 20:26:39 -08001766 t.setDisplayProjection(mDisplayToken, ui::ROTATION_0, layerStackRect, displayRect);
Adrian Roos9ee5dff2018-08-22 20:19:49 +02001767 t.apply();
1768
1769 mTargetInset = mCurrentInset = 0;
1770 }
1771
1772 int delta = timestep * mTargetInset / ms2ns(200);
1773 mCurrentInset += delta;
1774}
1775
Marin Shalamanov9070c052020-05-18 19:05:17 +02001776void BootAnimation::releaseAnimation(Animation* animation) const {
Parth Sane7f7b2212023-04-12 17:23:02 +00001777 ATRACE_CALL();
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001778 for (Vector<Animation::Part>::iterator it = animation->parts.begin(),
1779 e = animation->parts.end(); it != e; ++it) {
1780 if (it->animation)
1781 releaseAnimation(it->animation);
1782 }
1783 if (animation->zip)
1784 delete animation->zip;
1785 delete animation;
1786}
1787
Marin Shalamanov9070c052020-05-18 19:05:17 +02001788BootAnimation::Animation* BootAnimation::loadAnimation(const String8& fn) {
Parth Sane7f7b2212023-04-12 17:23:02 +00001789 ATRACE_CALL();
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001790 if (mLoadedFiles.indexOf(fn) >= 0) {
Wei Wang159a4102018-10-23 23:15:58 -07001791 SLOGE("File \"%s\" is already loaded. Cyclic ref is not allowed",
Tomasz Wasilczyk3815d342023-08-10 23:54:44 +00001792 fn.c_str());
Yi Kong911ac232019-03-24 01:49:02 -07001793 return nullptr;
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001794 }
Tomasz Wasilczyk835dfe52023-08-17 16:27:22 +00001795 ZipFileRO *zip = ZipFileRO::open(fn.c_str());
Yi Kong911ac232019-03-24 01:49:02 -07001796 if (zip == nullptr) {
Wei Wang159a4102018-10-23 23:15:58 -07001797 SLOGE("Failed to open animation zip \"%s\": %s",
Tomasz Wasilczyk3815d342023-08-10 23:54:44 +00001798 fn.c_str(), strerror(errno));
Yi Kong911ac232019-03-24 01:49:02 -07001799 return nullptr;
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001800 }
1801
Tomasz Wasilczyk3815d342023-08-10 23:54:44 +00001802 ALOGD("%s is loaded successfully", fn.c_str());
Yoshiaki Maruoka9ef19b82017-10-25 13:55:49 +09001803
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001804 Animation *animation = new Animation;
1805 animation->fileName = fn;
1806 animation->zip = zip;
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -07001807 animation->clockFont.map = nullptr;
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001808 mLoadedFiles.add(animation->fileName);
1809
1810 parseAnimationDesc(*animation);
Geoffrey Pitscha91a2d72016-07-12 14:46:19 -04001811 if (!preloadZip(*animation)) {
Greg Kaiser7426ec82019-09-11 14:34:27 -07001812 releaseAnimation(animation);
Yi Kong911ac232019-03-24 01:49:02 -07001813 return nullptr;
Geoffrey Pitscha91a2d72016-07-12 14:46:19 -04001814 }
1815
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001816 mLoadedFiles.remove(fn);
1817 return animation;
1818}
Damien Bargiacchi97480862016-03-29 14:55:55 -07001819
1820bool BootAnimation::updateIsTimeAccurate() {
Parth Sane7f7b2212023-04-12 17:23:02 +00001821 ATRACE_CALL();
Damien Bargiacchi97480862016-03-29 14:55:55 -07001822 static constexpr long long MAX_TIME_IN_PAST = 60000LL * 60LL * 24LL * 30LL; // 30 days
1823 static constexpr long long MAX_TIME_IN_FUTURE = 60000LL * 90LL; // 90 minutes
1824
1825 if (mTimeIsAccurate) {
1826 return true;
1827 }
Keun-young Parkb5938422017-03-23 13:46:24 -07001828 if (mShuttingDown) return true;
Damien Bargiacchi97480862016-03-29 14:55:55 -07001829 struct stat statResult;
Damien Bargiacchi9071db12016-10-28 17:38:22 -07001830
1831 if(stat(TIME_FORMAT_12_HOUR_FLAG_FILE_PATH, &statResult) == 0) {
1832 mTimeFormat12Hour = true;
1833 }
1834
Damien Bargiacchi97480862016-03-29 14:55:55 -07001835 if(stat(ACCURATE_TIME_FLAG_FILE_PATH, &statResult) == 0) {
1836 mTimeIsAccurate = true;
1837 return true;
1838 }
1839
1840 FILE* file = fopen(LAST_TIME_CHANGED_FILE_PATH, "r");
Yi Kong911ac232019-03-24 01:49:02 -07001841 if (file != nullptr) {
Damien Bargiacchi97480862016-03-29 14:55:55 -07001842 long long lastChangedTime = 0;
1843 fscanf(file, "%lld", &lastChangedTime);
1844 fclose(file);
1845 if (lastChangedTime > 0) {
1846 struct timespec now;
1847 clock_gettime(CLOCK_REALTIME, &now);
1848 // Match the Java timestamp format
1849 long long rtcNow = (now.tv_sec * 1000LL) + (now.tv_nsec / 1000000LL);
Damien Bargiacchi96762812016-07-12 15:53:40 -07001850 if (ACCURATE_TIME_EPOCH < rtcNow
1851 && lastChangedTime > (rtcNow - MAX_TIME_IN_PAST)
1852 && lastChangedTime < (rtcNow + MAX_TIME_IN_FUTURE)) {
Damien Bargiacchi97480862016-03-29 14:55:55 -07001853 mTimeIsAccurate = true;
1854 }
1855 }
1856 }
1857
1858 return mTimeIsAccurate;
1859}
1860
1861BootAnimation::TimeCheckThread::TimeCheckThread(BootAnimation* bootAnimation) : Thread(false),
Josh Yangf95b2002021-12-23 14:32:28 -08001862 mInotifyFd(-1), mBootAnimWd(-1), mTimeWd(-1), mBootAnimation(bootAnimation) {}
Damien Bargiacchi97480862016-03-29 14:55:55 -07001863
1864BootAnimation::TimeCheckThread::~TimeCheckThread() {
Parth Sane7f7b2212023-04-12 17:23:02 +00001865 ATRACE_CALL();
Damien Bargiacchi97480862016-03-29 14:55:55 -07001866 // mInotifyFd may be -1 but that's ok since we're not at risk of attempting to close a valid FD.
1867 close(mInotifyFd);
1868}
1869
1870bool BootAnimation::TimeCheckThread::threadLoop() {
Parth Sane7f7b2212023-04-12 17:23:02 +00001871 ATRACE_CALL();
Damien Bargiacchi97480862016-03-29 14:55:55 -07001872 bool shouldLoop = doThreadLoop() && !mBootAnimation->mTimeIsAccurate
1873 && mBootAnimation->mClockEnabled;
1874 if (!shouldLoop) {
1875 close(mInotifyFd);
1876 mInotifyFd = -1;
1877 }
1878 return shouldLoop;
1879}
1880
1881bool BootAnimation::TimeCheckThread::doThreadLoop() {
Parth Sane7f7b2212023-04-12 17:23:02 +00001882 ATRACE_CALL();
Damien Bargiacchi97480862016-03-29 14:55:55 -07001883 static constexpr int BUFF_LEN (10 * (sizeof(struct inotify_event) + NAME_MAX + 1));
1884
1885 // Poll instead of doing a blocking read so the Thread can exit if requested.
1886 struct pollfd pfd = { mInotifyFd, POLLIN, 0 };
1887 ssize_t pollResult = poll(&pfd, 1, 1000);
1888
1889 if (pollResult == 0) {
1890 return true;
1891 } else if (pollResult < 0) {
Wei Wang159a4102018-10-23 23:15:58 -07001892 SLOGE("Could not poll inotify events");
Damien Bargiacchi97480862016-03-29 14:55:55 -07001893 return false;
1894 }
1895
1896 char buff[BUFF_LEN] __attribute__ ((aligned(__alignof__(struct inotify_event))));;
1897 ssize_t length = read(mInotifyFd, buff, BUFF_LEN);
1898 if (length == 0) {
1899 return true;
1900 } else if (length < 0) {
Wei Wang159a4102018-10-23 23:15:58 -07001901 SLOGE("Could not read inotify events");
Damien Bargiacchi97480862016-03-29 14:55:55 -07001902 return false;
1903 }
1904
1905 const struct inotify_event *event;
1906 for (char* ptr = buff; ptr < buff + length; ptr += sizeof(struct inotify_event) + event->len) {
1907 event = (const struct inotify_event *) ptr;
Josh Yangf95b2002021-12-23 14:32:28 -08001908 if (event->wd == mBootAnimWd && strcmp(BOOTANIM_TIME_DIR_NAME, event->name) == 0) {
Damien Bargiacchi97480862016-03-29 14:55:55 -07001909 addTimeDirWatch();
1910 } else if (event->wd == mTimeWd && (strcmp(LAST_TIME_CHANGED_FILE_NAME, event->name) == 0
1911 || strcmp(ACCURATE_TIME_FLAG_FILE_NAME, event->name) == 0)) {
1912 return !mBootAnimation->updateIsTimeAccurate();
1913 }
1914 }
1915
1916 return true;
1917}
1918
1919void BootAnimation::TimeCheckThread::addTimeDirWatch() {
Parth Sane7f7b2212023-04-12 17:23:02 +00001920 ATRACE_CALL();
Josh Yangf95b2002021-12-23 14:32:28 -08001921 mTimeWd = inotify_add_watch(mInotifyFd, BOOTANIM_TIME_DIR_PATH,
Damien Bargiacchi97480862016-03-29 14:55:55 -07001922 IN_CLOSE_WRITE | IN_MOVED_TO | IN_ATTRIB);
1923 if (mTimeWd > 0) {
1924 // No need to watch for the time directory to be created if it already exists
Josh Yangf95b2002021-12-23 14:32:28 -08001925 inotify_rm_watch(mInotifyFd, mBootAnimWd);
1926 mBootAnimWd = -1;
Damien Bargiacchi97480862016-03-29 14:55:55 -07001927 }
1928}
1929
1930status_t BootAnimation::TimeCheckThread::readyToRun() {
Parth Sane7f7b2212023-04-12 17:23:02 +00001931 ATRACE_CALL();
Damien Bargiacchi97480862016-03-29 14:55:55 -07001932 mInotifyFd = inotify_init();
1933 if (mInotifyFd < 0) {
Wei Wang159a4102018-10-23 23:15:58 -07001934 SLOGE("Could not initialize inotify fd");
Damien Bargiacchi97480862016-03-29 14:55:55 -07001935 return NO_INIT;
1936 }
1937
Josh Yangf95b2002021-12-23 14:32:28 -08001938 mBootAnimWd = inotify_add_watch(mInotifyFd, BOOTANIM_DATA_DIR_PATH, IN_CREATE | IN_ATTRIB);
1939 if (mBootAnimWd < 0) {
Damien Bargiacchi97480862016-03-29 14:55:55 -07001940 close(mInotifyFd);
1941 mInotifyFd = -1;
Josh Yangf95b2002021-12-23 14:32:28 -08001942 SLOGE("Could not add watch for %s: %s", BOOTANIM_DATA_DIR_PATH, strerror(errno));
Damien Bargiacchi97480862016-03-29 14:55:55 -07001943 return NO_INIT;
1944 }
1945
1946 addTimeDirWatch();
1947
1948 if (mBootAnimation->updateIsTimeAccurate()) {
1949 close(mInotifyFd);
1950 mInotifyFd = -1;
1951 return ALREADY_EXISTS;
1952 }
1953
1954 return NO_ERROR;
1955}
1956
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001957// ---------------------------------------------------------------------------
1958
Marin Shalamanov9070c052020-05-18 19:05:17 +02001959} // namespace android