blob: b43905b1923923f010916c06e26f238a99bdcb3c [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
Rahul Banerjee0c27488a2024-07-03 18:20:18 -070018
Mathias Agopianbc726112009-09-23 15:44:05 -070019#define LOG_TAG "BootAnimation"
Parth Sane7f7b2212023-04-12 17:23:02 +000020#define ATRACE_TAG ATRACE_TAG_GRAPHICS
Mathias Agopianbc726112009-09-23 15:44:05 -070021
Tomasz Wasilczyk9dcea572023-08-22 20:39:20 +000022#include <filesystem>
Nikita Ioffe06c986e2020-01-27 17:16:03 +000023#include <vector>
24
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080025#include <stdint.h>
Wei Wanga90c54c2017-02-02 11:35:21 -080026#include <inttypes.h>
Damien Bargiacchi97480862016-03-29 14:55:55 -070027#include <sys/inotify.h>
28#include <sys/poll.h>
29#include <sys/stat.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080030#include <sys/types.h>
31#include <math.h>
32#include <fcntl.h>
33#include <utils/misc.h>
Parth Sane7f7b2212023-04-12 17:23:02 +000034#include <utils/Trace.h>
Mathias Agopianb4d5a722009-09-23 17:05:19 -070035#include <signal.h>
Elliott Hughesbb94f312014-10-21 10:41:33 -070036#include <time.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080037
Steven Morelandfb7952f2018-02-23 14:58:50 -080038#include <cutils/atomic.h>
Jason parksbd9a08d2011-01-31 15:04:34 -060039#include <cutils/properties.h>
40
Derek Sollenberger22d0d4c72020-04-08 10:53:46 -040041#include <android/imagedecoder.h>
Mathias Agopianb13b9bd2012-02-17 18:27:36 -080042#include <androidfw/AssetManager.h>
Mathias Agopianac31a3b2009-05-21 19:59:24 -070043#include <binder/IPCThreadState.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080044#include <utils/Errors.h>
45#include <utils/Log.h>
Wei Wanga90c54c2017-02-02 11:35:21 -080046#include <utils/SystemClock.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080047
Keun-young Parkb5938422017-03-23 13:46:24 -070048#include <android-base/properties.h>
49
Marin Shalamanovcb783ca2021-01-29 21:26:07 +010050#include <ui/DisplayMode.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080051#include <ui/PixelFormat.h>
52#include <ui/Rect.h>
53#include <ui/Region.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080054
Jeff Brown0b722fe2012-08-24 22:40:14 -070055#include <gui/ISurfaceComposer.h>
Marin Shalamanov9070c052020-05-18 19:05:17 +020056#include <gui/DisplayEventReceiver.h>
Mathias Agopian8335f1c2012-02-25 18:48:35 -080057#include <gui/Surface.h>
58#include <gui/SurfaceComposerClient.h>
Lucas Dupin8eedc022021-07-29 21:49:07 +000059#include <GLES2/gl2.h>
60#include <GLES2/gl2ext.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080061#include <EGL/eglext.h>
62
63#include "BootAnimation.h"
64
Rahul Banerjee0c27488a2024-07-03 18:20:18 -070065#include <com_android_graphics_bootanimation_flags.h>
66
Kyeongkab.Nam35a8a112019-07-17 16:41:48 +090067#define ANIM_PATH_MAX 255
68#define STR(x) #x
69#define STRTO(x) STR(x)
70
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080071namespace android {
72
Marin Shalamanovcb783ca2021-01-29 21:26:07 +010073using ui::DisplayMode;
74
Damien Bargiacchi97480862016-03-29 14:55:55 -070075static const char OEM_BOOTANIMATION_FILE[] = "/oem/media/bootanimation.zip";
jorgetmbd89e9b2024-09-09 19:42:26 +000076static const char PRODUCT_BOOTANIMATION_DIR[] = "/product/media/";
77static const char PRODUCT_BOOTANIMATION_DARK_FILE[] = "bootanimation-dark.zip";
78static const char PRODUCT_BOOTANIMATION_FILE[] = "bootanimation.zip";
Damien Bargiacchi97480862016-03-29 14:55:55 -070079static const char SYSTEM_BOOTANIMATION_FILE[] = "/system/media/bootanimation.zip";
Anders Fridlunde0b4d232018-11-06 14:23:25 +010080static const char APEX_BOOTANIMATION_FILE[] = "/apex/com.android.bootanimation/etc/bootanimation.zip";
Keun-young Parkb5938422017-03-23 13:46:24 -070081static const char OEM_SHUTDOWNANIMATION_FILE[] = "/oem/media/shutdownanimation.zip";
Jaekyun Seokc521bb32018-01-30 11:52:47 +090082static const char PRODUCT_SHUTDOWNANIMATION_FILE[] = "/product/media/shutdownanimation.zip";
Keun-young Parkb5938422017-03-23 13:46:24 -070083static const char SYSTEM_SHUTDOWNANIMATION_FILE[] = "/system/media/shutdownanimation.zip";
84
Nikita Ioffe06c986e2020-01-27 17:16:03 +000085static constexpr const char* PRODUCT_USERSPACE_REBOOT_ANIMATION_FILE = "/product/media/userspace-reboot.zip";
86static constexpr const char* OEM_USERSPACE_REBOOT_ANIMATION_FILE = "/oem/media/userspace-reboot.zip";
87static constexpr const char* SYSTEM_USERSPACE_REBOOT_ANIMATION_FILE = "/system/media/userspace-reboot.zip";
88
Lee George Thomas7a2548b2023-07-25 14:47:39 -070089static const char BOOTANIM_DATA_DIR_PATH[] = "/data/misc/bootanim";
Josh Yangf95b2002021-12-23 14:32:28 -080090static const char BOOTANIM_TIME_DIR_NAME[] = "time";
Lee George Thomas7a2548b2023-07-25 14:47:39 -070091static const char BOOTANIM_TIME_DIR_PATH[] = "/data/misc/bootanim/time";
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -070092static const char CLOCK_FONT_ASSET[] = "images/clock_font.png";
93static const char CLOCK_FONT_ZIP_NAME[] = "clock_font.png";
Nicolas Geoffray41fffb42020-12-15 10:50:29 +000094static const char PROGRESS_FONT_ASSET[] = "images/progress_font.png";
95static const char PROGRESS_FONT_ZIP_NAME[] = "progress_font.png";
Damien Bargiacchi97480862016-03-29 14:55:55 -070096static const char LAST_TIME_CHANGED_FILE_NAME[] = "last_time_change";
Lee George Thomas7a2548b2023-07-25 14:47:39 -070097static const char LAST_TIME_CHANGED_FILE_PATH[] = "/data/misc/bootanim/time/last_time_change";
Damien Bargiacchi97480862016-03-29 14:55:55 -070098static const char ACCURATE_TIME_FLAG_FILE_NAME[] = "time_is_accurate";
Lee George Thomas7a2548b2023-07-25 14:47:39 -070099static const char ACCURATE_TIME_FLAG_FILE_PATH[] = "/data/misc/bootanim/time/time_is_accurate";
100static const char TIME_FORMAT_12_HOUR_FLAG_FILE_PATH[] = "/data/misc/bootanim/time/time_format_12_hour";
Damien Bargiacchi96762812016-07-12 15:53:40 -0700101// Java timestamp format. Don't show the clock if the date is before 2000-01-01 00:00:00.
102static const long long ACCURATE_TIME_EPOCH = 946684800000;
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700103static constexpr char FONT_BEGIN_CHAR = ' ';
104static constexpr char FONT_END_CHAR = '~' + 1;
105static constexpr size_t FONT_NUM_CHARS = FONT_END_CHAR - FONT_BEGIN_CHAR + 1;
106static constexpr size_t FONT_NUM_COLS = 16;
107static constexpr size_t FONT_NUM_ROWS = FONT_NUM_CHARS / FONT_NUM_COLS;
108static const int TEXT_CENTER_VALUE = INT_MAX;
109static const int TEXT_MISSING_VALUE = INT_MIN;
Damien Bargiacchi97480862016-03-29 14:55:55 -0700110static const char EXIT_PROP_NAME[] = "service.bootanim.exit";
Nicolas Geoffray41fffb42020-12-15 10:50:29 +0000111static const char PROGRESS_PROP_NAME[] = "service.bootanim.progress";
Thiébaud Weksteen08de4432021-11-18 13:25:59 +1100112static const char CLOCK_ENABLED_PROP_NAME[] = "persist.sys.bootanim.clock.enabled";
Kyeongkab.Nam35a8a112019-07-17 16:41:48 +0900113static const int ANIM_ENTRY_NAME_MAX = ANIM_PATH_MAX + 1;
niuwenchao9cd6ac82022-12-23 17:34:22 +0800114static const int MAX_CHECK_EXIT_INTERVAL_US = 50000;
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700115static constexpr size_t TEXT_POS_LEN_MAX = 16;
Shan Huang2b31a362021-08-16 16:45:39 +0000116static const int DYNAMIC_COLOR_COUNT = 4;
Lucas Dupin8eedc022021-07-29 21:49:07 +0000117static const char U_TEXTURE[] = "uTexture";
118static const char U_FADE[] = "uFade";
119static const char U_CROP_AREA[] = "uCropArea";
Shan Huang2b31a362021-08-16 16:45:39 +0000120static const char U_START_COLOR_PREFIX[] = "uStartColor";
121static const char U_END_COLOR_PREFIX[] = "uEndColor";
122static const char U_COLOR_PROGRESS[] = "uColorProgress";
Lucas Dupin8eedc022021-07-29 21:49:07 +0000123static const char A_UV[] = "aUv";
124static const char A_POSITION[] = "aPosition";
125static const char VERTEX_SHADER_SOURCE[] = R"(
126 precision mediump float;
127 attribute vec4 aPosition;
128 attribute highp vec2 aUv;
129 varying highp vec2 vUv;
130 void main() {
131 gl_Position = aPosition;
132 vUv = aUv;
133 })";
Shan Huang2b31a362021-08-16 16:45:39 +0000134static const char IMAGE_FRAG_DYNAMIC_COLORING_SHADER_SOURCE[] = R"(
135 precision mediump float;
Shan Huang81846ba2021-09-07 19:07:47 +0000136 const float cWhiteMaskThreshold = 0.05;
Shan Huang2b31a362021-08-16 16:45:39 +0000137 uniform sampler2D uTexture;
138 uniform float uFade;
139 uniform float uColorProgress;
Andrew Zengf9f58222022-11-08 11:10:48 -0800140 uniform vec3 uStartColor0;
141 uniform vec3 uStartColor1;
142 uniform vec3 uStartColor2;
143 uniform vec3 uStartColor3;
144 uniform vec3 uEndColor0;
145 uniform vec3 uEndColor1;
146 uniform vec3 uEndColor2;
147 uniform vec3 uEndColor3;
Shan Huang2b31a362021-08-16 16:45:39 +0000148 varying highp vec2 vUv;
149 void main() {
150 vec4 mask = texture2D(uTexture, vUv);
Shan Huang488ff742021-08-25 20:10:46 +0000151 float r = mask.r;
152 float g = mask.g;
153 float b = mask.b;
154 float a = mask.a;
155 // If all channels have values, render pixel as a shade of white.
156 float useWhiteMask = step(cWhiteMaskThreshold, r)
157 * step(cWhiteMaskThreshold, g)
158 * step(cWhiteMaskThreshold, b)
159 * step(cWhiteMaskThreshold, a);
Andrew Zengf9f58222022-11-08 11:10:48 -0800160 vec3 color = r * mix(uStartColor0, uEndColor0, uColorProgress)
Shan Huang488ff742021-08-25 20:10:46 +0000161 + g * mix(uStartColor1, uEndColor1, uColorProgress)
162 + b * mix(uStartColor2, uEndColor2, uColorProgress)
163 + a * mix(uStartColor3, uEndColor3, uColorProgress);
Andrew Zengf9f58222022-11-08 11:10:48 -0800164 color = mix(color, vec3((r + g + b + a) * 0.25), useWhiteMask);
165 gl_FragColor = vec4(color.x, color.y, color.z, (1.0 - uFade));
Shan Huang2b31a362021-08-16 16:45:39 +0000166 })";
Lucas Dupin8eedc022021-07-29 21:49:07 +0000167static const char IMAGE_FRAG_SHADER_SOURCE[] = R"(
168 precision mediump float;
169 uniform sampler2D uTexture;
170 uniform float uFade;
171 varying highp vec2 vUv;
172 void main() {
173 vec4 color = texture2D(uTexture, vUv);
Shan Huang2b31a362021-08-16 16:45:39 +0000174 gl_FragColor = vec4(color.x, color.y, color.z, (1.0 - uFade)) * color.a;
Lucas Dupin8eedc022021-07-29 21:49:07 +0000175 })";
176static const char TEXT_FRAG_SHADER_SOURCE[] = R"(
177 precision mediump float;
178 uniform sampler2D uTexture;
179 uniform vec4 uCropArea;
180 varying highp vec2 vUv;
181 void main() {
182 vec2 uv = vec2(mix(uCropArea.x, uCropArea.z, vUv.x),
183 mix(uCropArea.y, uCropArea.w, vUv.y));
184 gl_FragColor = texture2D(uTexture, uv);
185 })";
186
187static GLfloat quadPositions[] = {
188 -0.5f, -0.5f,
189 +0.5f, -0.5f,
190 +0.5f, +0.5f,
191 +0.5f, +0.5f,
192 -0.5f, +0.5f,
193 -0.5f, -0.5f
194};
195static GLfloat quadUVs[] = {
196 0.0f, 1.0f,
197 1.0f, 1.0f,
198 1.0f, 0.0f,
199 1.0f, 0.0f,
200 0.0f, 0.0f,
201 0.0f, 1.0f
202};
Narayan Kamathafd31e02013-12-03 13:16:03 +0000203
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800204// ---------------------------------------------------------------------------
205
Ed Coyne7464ac92017-06-08 12:26:48 -0700206BootAnimation::BootAnimation(sp<Callbacks> callbacks)
Marin Shalamanov9070c052020-05-18 19:05:17 +0200207 : Thread(false), mLooper(new Looper(false)), mClockEnabled(true), mTimeIsAccurate(false),
Yi Kong911ac232019-03-24 01:49:02 -0700208 mTimeFormat12Hour(false), mTimeCheckThread(nullptr), mCallbacks(callbacks) {
Parth Sane7f7b2212023-04-12 17:23:02 +0000209 ATRACE_CALL();
Mathias Agopian627e7b52009-05-21 19:21:59 -0700210 mSession = new SurfaceComposerClient();
Geoffrey Pitsch290c4352016-08-09 14:35:10 -0400211
Keun-young Parkb5938422017-03-23 13:46:24 -0700212 std::string powerCtl = android::base::GetProperty("sys.powerctl", "");
213 if (powerCtl.empty()) {
214 mShuttingDown = false;
215 } else {
216 mShuttingDown = true;
217 }
Huihong Luo50a2f362018-08-11 09:27:57 -0700218 ALOGD("%sAnimationStartTiming start time: %" PRId64 "ms", mShuttingDown ? "Shutdown" : "Boot",
219 elapsedRealtime());
220}
221
222BootAnimation::~BootAnimation() {
Parth Sane7f7b2212023-04-12 17:23:02 +0000223 ATRACE_CALL();
Huihong Luo50a2f362018-08-11 09:27:57 -0700224 if (mAnimation != nullptr) {
225 releaseAnimation(mAnimation);
226 mAnimation = nullptr;
227 }
228 ALOGD("%sAnimationStopTiming start time: %" PRId64 "ms", mShuttingDown ? "Shutdown" : "Boot",
229 elapsedRealtime());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800230}
231
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800232void BootAnimation::onFirstRef() {
Parth Sane7f7b2212023-04-12 17:23:02 +0000233 ATRACE_CALL();
Mathias Agopianbc726112009-09-23 15:44:05 -0700234 status_t err = mSession->linkToComposerDeath(this);
Wei Wang159a4102018-10-23 23:15:58 -0700235 SLOGE_IF(err, "linkToComposerDeath failed (%s) ", strerror(-err));
Mathias Agopian8434c532009-09-23 18:52:49 -0700236 if (err == NO_ERROR) {
Huihong Luo50a2f362018-08-11 09:27:57 -0700237 // Load the animation content -- this can be slow (eg 200ms)
238 // called before waitForSurfaceFlinger() in main() to avoid wait
239 ALOGD("%sAnimationPreloadTiming start time: %" PRId64 "ms",
240 mShuttingDown ? "Shutdown" : "Boot", elapsedRealtime());
241 preloadAnimation();
jiayongqiang43342022024-07-13 19:46:08 +0800242 ALOGD("%sAnimationPreloadTiming stop time: %" PRId64 "ms",
Huihong Luo50a2f362018-08-11 09:27:57 -0700243 mShuttingDown ? "Shutdown" : "Boot", elapsedRealtime());
Mathias Agopianbc726112009-09-23 15:44:05 -0700244 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800245}
246
Mathias Agopianbc726112009-09-23 15:44:05 -0700247sp<SurfaceComposerClient> BootAnimation::session() const {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800248 return mSession;
249}
250
Marin Shalamanov9070c052020-05-18 19:05:17 +0200251void BootAnimation::binderDied(const wp<IBinder>&) {
Parth Sane7f7b2212023-04-12 17:23:02 +0000252 ATRACE_CALL();
Mathias Agopianbc726112009-09-23 15:44:05 -0700253 // woah, surfaceflinger died!
Wei Wang159a4102018-10-23 23:15:58 -0700254 SLOGD("SurfaceFlinger died, exiting...");
Mathias Agopianbc726112009-09-23 15:44:05 -0700255
256 // calling requestExit() is not enough here because the Surface code
257 // might be blocked on a condition variable that will never be updated.
258 kill( getpid(), SIGKILL );
259 requestExit();
260}
261
Shan Huang2b31a362021-08-16 16:45:39 +0000262static void* decodeImage(const void* encodedData, size_t dataLength, AndroidBitmapInfo* outInfo,
263 bool premultiplyAlpha) {
Parth Sane7f7b2212023-04-12 17:23:02 +0000264 ATRACE_CALL();
Derek Sollenberger22d0d4c72020-04-08 10:53:46 -0400265 AImageDecoder* decoder = nullptr;
266 AImageDecoder_createFromBuffer(encodedData, dataLength, &decoder);
267 if (!decoder) {
268 return nullptr;
269 }
270
271 const AImageDecoderHeaderInfo* info = AImageDecoder_getHeaderInfo(decoder);
272 outInfo->width = AImageDecoderHeaderInfo_getWidth(info);
273 outInfo->height = AImageDecoderHeaderInfo_getHeight(info);
274 outInfo->format = AImageDecoderHeaderInfo_getAndroidBitmapFormat(info);
275 outInfo->stride = AImageDecoder_getMinimumStride(decoder);
276 outInfo->flags = 0;
277
Shan Huang2b31a362021-08-16 16:45:39 +0000278 if (!premultiplyAlpha) {
279 AImageDecoder_setUnpremultipliedRequired(decoder, true);
280 }
281
Derek Sollenberger22d0d4c72020-04-08 10:53:46 -0400282 const size_t size = outInfo->stride * outInfo->height;
283 void* pixels = malloc(size);
284 int result = AImageDecoder_decodeImage(decoder, pixels, outInfo->stride, size);
285 AImageDecoder_delete(decoder);
286
287 if (result != ANDROID_IMAGE_DECODER_SUCCESS) {
288 free(pixels);
289 return nullptr;
290 }
291 return pixels;
292}
293
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800294status_t BootAnimation::initTexture(Texture* texture, AssetManager& assets,
Shan Huang2b31a362021-08-16 16:45:39 +0000295 const char* name, bool premultiplyAlpha) {
Parth Sane7f7b2212023-04-12 17:23:02 +0000296 ATRACE_CALL();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800297 Asset* asset = assets.open(name, Asset::ACCESS_BUFFER);
Yi Kong911ac232019-03-24 01:49:02 -0700298 if (asset == nullptr)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800299 return NO_INIT;
Derek Sollenberger22d0d4c72020-04-08 10:53:46 -0400300
301 AndroidBitmapInfo bitmapInfo;
Shan Huang2b31a362021-08-16 16:45:39 +0000302 void* pixels = decodeImage(asset->getBuffer(false), asset->getLength(), &bitmapInfo,
303 premultiplyAlpha);
Derek Sollenberger22d0d4c72020-04-08 10:53:46 -0400304 auto pixelDeleter = std::unique_ptr<void, decltype(free)*>{ pixels, free };
305
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800306 asset->close();
307 delete asset;
308
Derek Sollenberger22d0d4c72020-04-08 10:53:46 -0400309 if (!pixels) {
310 return NO_INIT;
311 }
312
313 const int w = bitmapInfo.width;
314 const int h = bitmapInfo.height;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800315
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800316 texture->w = w;
317 texture->h = h;
318
319 glGenTextures(1, &texture->name);
320 glBindTexture(GL_TEXTURE_2D, texture->name);
321
Derek Sollenberger22d0d4c72020-04-08 10:53:46 -0400322 switch (bitmapInfo.format) {
323 case ANDROID_BITMAP_FORMAT_A_8:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800324 glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, w, h, 0, GL_ALPHA,
Derek Sollenberger22d0d4c72020-04-08 10:53:46 -0400325 GL_UNSIGNED_BYTE, pixels);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800326 break;
Derek Sollenberger22d0d4c72020-04-08 10:53:46 -0400327 case ANDROID_BITMAP_FORMAT_RGBA_4444:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800328 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA,
Derek Sollenberger22d0d4c72020-04-08 10:53:46 -0400329 GL_UNSIGNED_SHORT_4_4_4_4, pixels);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800330 break;
Derek Sollenberger22d0d4c72020-04-08 10:53:46 -0400331 case ANDROID_BITMAP_FORMAT_RGBA_8888:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800332 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA,
Derek Sollenberger22d0d4c72020-04-08 10:53:46 -0400333 GL_UNSIGNED_BYTE, pixels);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800334 break;
Derek Sollenberger22d0d4c72020-04-08 10:53:46 -0400335 case ANDROID_BITMAP_FORMAT_RGB_565:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800336 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, w, h, 0, GL_RGB,
Derek Sollenberger22d0d4c72020-04-08 10:53:46 -0400337 GL_UNSIGNED_SHORT_5_6_5, pixels);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800338 break;
339 default:
340 break;
341 }
342
Lucas Dupin8eedc022021-07-29 21:49:07 +0000343 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
344 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
345 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
346 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700347
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800348 return NO_ERROR;
349}
350
Shan Huang2b31a362021-08-16 16:45:39 +0000351status_t BootAnimation::initTexture(FileMap* map, int* width, int* height,
352 bool premultiplyAlpha) {
Parth Sane7f7b2212023-04-12 17:23:02 +0000353 ATRACE_CALL();
Derek Sollenberger22d0d4c72020-04-08 10:53:46 -0400354 AndroidBitmapInfo bitmapInfo;
Shan Huang2b31a362021-08-16 16:45:39 +0000355 void* pixels = decodeImage(map->getDataPtr(), map->getDataLength(), &bitmapInfo,
356 premultiplyAlpha);
Derek Sollenberger22d0d4c72020-04-08 10:53:46 -0400357 auto pixelDeleter = std::unique_ptr<void, decltype(free)*>{ pixels, free };
Mathias Agopiana8826d62009-10-01 03:10:14 -0700358
Mykola Kondratenko0c1eeb32014-04-15 09:35:44 +0200359 // FileMap memory is never released until application exit.
360 // Release it now as the texture is already loaded and the memory used for
361 // the packed resource can be released.
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700362 delete map;
Mykola Kondratenko0c1eeb32014-04-15 09:35:44 +0200363
Derek Sollenberger22d0d4c72020-04-08 10:53:46 -0400364 if (!pixels) {
365 return NO_INIT;
366 }
367
368 const int w = bitmapInfo.width;
369 const int h = bitmapInfo.height;
Mathias Agopiana8826d62009-10-01 03:10:14 -0700370
Mathias Agopiana8826d62009-10-01 03:10:14 -0700371 int tw = 1 << (31 - __builtin_clz(w));
372 int th = 1 << (31 - __builtin_clz(h));
373 if (tw < w) tw <<= 1;
374 if (th < h) th <<= 1;
375
Derek Sollenberger22d0d4c72020-04-08 10:53:46 -0400376 switch (bitmapInfo.format) {
377 case ANDROID_BITMAP_FORMAT_RGBA_8888:
Sai Kiran Korwar27167492015-07-07 20:00:06 +0530378 if (!mUseNpotTextures && (tw != w || th != h)) {
Mathias Agopiana8826d62009-10-01 03:10:14 -0700379 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, tw, th, 0, GL_RGBA,
Yi Kong911ac232019-03-24 01:49:02 -0700380 GL_UNSIGNED_BYTE, nullptr);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700381 glTexSubImage2D(GL_TEXTURE_2D, 0,
Derek Sollenberger22d0d4c72020-04-08 10:53:46 -0400382 0, 0, w, h, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700383 } else {
Sai Kiran Korwar27167492015-07-07 20:00:06 +0530384 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA,
Derek Sollenberger22d0d4c72020-04-08 10:53:46 -0400385 GL_UNSIGNED_BYTE, pixels);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700386 }
387 break;
388
Derek Sollenberger22d0d4c72020-04-08 10:53:46 -0400389 case ANDROID_BITMAP_FORMAT_RGB_565:
Sai Kiran Korwar27167492015-07-07 20:00:06 +0530390 if (!mUseNpotTextures && (tw != w || th != h)) {
Mathias Agopiana8826d62009-10-01 03:10:14 -0700391 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, tw, th, 0, GL_RGB,
Yi Kong911ac232019-03-24 01:49:02 -0700392 GL_UNSIGNED_SHORT_5_6_5, nullptr);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700393 glTexSubImage2D(GL_TEXTURE_2D, 0,
Derek Sollenberger22d0d4c72020-04-08 10:53:46 -0400394 0, 0, w, h, GL_RGB, GL_UNSIGNED_SHORT_5_6_5, pixels);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700395 } else {
Sai Kiran Korwar27167492015-07-07 20:00:06 +0530396 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, w, h, 0, GL_RGB,
Derek Sollenberger22d0d4c72020-04-08 10:53:46 -0400397 GL_UNSIGNED_SHORT_5_6_5, pixels);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700398 }
399 break;
400 default:
401 break;
402 }
403
niuwenchaoa76df892023-09-11 17:19:04 +0800404 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
405 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
Lucas Dupin8eedc022021-07-29 21:49:07 +0000406 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
407 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700408
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700409 *width = w;
410 *height = h;
411
Mathias Agopiana8826d62009-10-01 03:10:14 -0700412 return NO_ERROR;
413}
414
Marin Shalamanov9070c052020-05-18 19:05:17 +0200415class BootAnimation::DisplayEventCallback : public LooperCallback {
416 BootAnimation* mBootAnimation;
417
418public:
419 DisplayEventCallback(BootAnimation* bootAnimation) {
Parth Sane7f7b2212023-04-12 17:23:02 +0000420 ATRACE_CALL();
Marin Shalamanov9070c052020-05-18 19:05:17 +0200421 mBootAnimation = bootAnimation;
422 }
423
424 int handleEvent(int /* fd */, int events, void* /* data */) {
Parth Sane7f7b2212023-04-12 17:23:02 +0000425 ATRACE_CALL();
Marin Shalamanov9070c052020-05-18 19:05:17 +0200426 if (events & (Looper::EVENT_ERROR | Looper::EVENT_HANGUP)) {
427 ALOGE("Display event receiver pipe was closed or an error occurred. events=0x%x",
428 events);
429 return 0; // remove the callback
430 }
431
432 if (!(events & Looper::EVENT_INPUT)) {
433 ALOGW("Received spurious callback for unhandled poll event. events=0x%x", events);
434 return 1; // keep the callback
435 }
436
437 constexpr int kBufferSize = 100;
438 DisplayEventReceiver::Event buffer[kBufferSize];
439 ssize_t numEvents;
440 do {
441 numEvents = mBootAnimation->mDisplayEventReceiver->getEvents(buffer, kBufferSize);
442 for (size_t i = 0; i < static_cast<size_t>(numEvents); i++) {
443 const auto& event = buffer[i];
444 if (event.header.type == DisplayEventReceiver::DISPLAY_EVENT_HOTPLUG) {
445 SLOGV("Hotplug received");
446
447 if (!event.hotplug.connected) {
448 // ignore hotplug disconnect
449 continue;
450 }
451 auto token = SurfaceComposerClient::getPhysicalDisplayToken(
452 event.header.displayId);
453
Rahul Banerjee8bc38e32024-11-11 16:08:52 -0800454 auto& firstDisplay = mBootAnimation->mDisplays.front();
Rahul Banerjee0c27488a2024-07-03 18:20:18 -0700455 if (token != firstDisplay.displayToken) {
Marin Shalamanov9070c052020-05-18 19:05:17 +0200456 // ignore hotplug of a secondary display
457 continue;
458 }
459
Marin Shalamanovcb783ca2021-01-29 21:26:07 +0100460 DisplayMode displayMode;
461 const status_t error = SurfaceComposerClient::getActiveDisplayMode(
Rahul Banerjee0c27488a2024-07-03 18:20:18 -0700462 firstDisplay.displayToken, &displayMode);
Marin Shalamanov9070c052020-05-18 19:05:17 +0200463 if (error != NO_ERROR) {
Marin Shalamanovcb783ca2021-01-29 21:26:07 +0100464 SLOGE("Can't get active display mode.");
Marin Shalamanov9070c052020-05-18 19:05:17 +0200465 }
Marin Shalamanovcb783ca2021-01-29 21:26:07 +0100466 mBootAnimation->resizeSurface(displayMode.resolution.getWidth(),
Rahul Banerjee0c27488a2024-07-03 18:20:18 -0700467 displayMode.resolution.getHeight(),
468 firstDisplay);
Marin Shalamanov9070c052020-05-18 19:05:17 +0200469 }
470 }
471 } while (numEvents > 0);
472
473 return 1; // keep the callback
474 }
475};
476
477EGLConfig BootAnimation::getEglConfig(const EGLDisplay& display) {
478 const EGLint attribs[] = {
Andrii Chepurnyid93f7b22023-03-15 11:22:07 +0200479 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
Marin Shalamanov9070c052020-05-18 19:05:17 +0200480 EGL_RED_SIZE, 8,
481 EGL_GREEN_SIZE, 8,
482 EGL_BLUE_SIZE, 8,
483 EGL_DEPTH_SIZE, 0,
484 EGL_NONE
485 };
486 EGLint numConfigs;
487 EGLConfig config;
488 eglChooseConfig(display, attribs, &config, 1, &numConfigs);
489 return config;
490}
491
Marin Shalamanov047802d2020-05-19 23:55:12 +0200492ui::Size BootAnimation::limitSurfaceSize(int width, int height) const {
493 ui::Size limited(width, height);
494 bool wasLimited = false;
495 const float aspectRatio = float(width) / float(height);
496 if (mMaxWidth != 0 && width > mMaxWidth) {
497 limited.height = mMaxWidth / aspectRatio;
498 limited.width = mMaxWidth;
499 wasLimited = true;
500 }
501 if (mMaxHeight != 0 && limited.height > mMaxHeight) {
502 limited.height = mMaxHeight;
503 limited.width = mMaxHeight * aspectRatio;
504 wasLimited = true;
505 }
506 SLOGV_IF(wasLimited, "Surface size has been limited to [%dx%d] from [%dx%d]",
507 limited.width, limited.height, width, height);
508 return limited;
509}
510
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800511status_t BootAnimation::readyToRun() {
Parth Sane7f7b2212023-04-12 17:23:02 +0000512 ATRACE_CALL();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800513 mAssets.addDefaultAssets();
Rahul Banerjee0c27488a2024-07-03 18:20:18 -0700514 return initDisplaysAndSurfaces();
515}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800516
Rahul Banerjee0c27488a2024-07-03 18:20:18 -0700517status_t BootAnimation::initDisplaysAndSurfaces() {
518 std::vector<PhysicalDisplayId> displayIds = SurfaceComposerClient::getPhysicalDisplayIds();
519 if (displayIds.empty()) {
520 SLOGE("Failed to get ID for any displays");
Dominik Laskowski69b281d2019-11-22 14:13:12 -0800521 return NAME_NOT_FOUND;
Huihong Luo44305102022-09-26 11:16:20 -0700522 }
Dominik Laskowski3316a0a2019-01-25 02:56:41 -0800523
Rahul Banerjee0c27488a2024-07-03 18:20:18 -0700524 // If Multi-Display isn't explicitly enabled, ignore all displays after the first one
525 if (!com::android::graphics::bootanimation::flags::multidisplay()) {
526 displayIds.erase(displayIds.begin() + 1, displayIds.end());
Huihong Luo44305102022-09-26 11:16:20 -0700527 }
528
Rahul Banerjee0c27488a2024-07-03 18:20:18 -0700529 for (const auto id : displayIds) {
530 if (const auto token = SurfaceComposerClient::getPhysicalDisplayToken(id)) {
531 mDisplays.push_back({.displayToken = token});
532 } else {
533 SLOGE("Failed to get display token for a display");
534 SLOGE("Failed to get display token for display %" PRIu64, id.value);
535 return NAME_NOT_FOUND;
536 }
Huihong Luo44305102022-09-26 11:16:20 -0700537 }
538
Rahul Banerjee0c27488a2024-07-03 18:20:18 -0700539 // Initialize EGL
540 mEgl = eglGetDisplay(EGL_DEFAULT_DISPLAY);
541 eglInitialize(mEgl, nullptr, nullptr);
542 EGLConfig config = getEglConfig(mEgl);
543 EGLint contextAttributes[] = {EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE};
544 mEglContext = eglCreateContext(mEgl, config, nullptr, contextAttributes);
545
Huihong Luo44305102022-09-26 11:16:20 -0700546 mMaxWidth = android::base::GetIntProperty("ro.surface_flinger.max_graphics_width", 0);
547 mMaxHeight = android::base::GetIntProperty("ro.surface_flinger.max_graphics_height", 0);
Huihong Luo44305102022-09-26 11:16:20 -0700548
Rahul Banerjee0c27488a2024-07-03 18:20:18 -0700549 for (size_t displayIdx = 0; displayIdx < mDisplays.size(); displayIdx++) {
550 auto& display = mDisplays[displayIdx];
551 DisplayMode displayMode;
552 const status_t error =
553 SurfaceComposerClient::getActiveDisplayMode(display.displayToken, &displayMode);
554 if (error != NO_ERROR) {
555 return error;
556 }
557 ui::Size resolution = displayMode.resolution;
558 // Clamp each surface to max size
559 resolution = limitSurfaceSize(resolution.width, resolution.height);
560 // Create the native surface
561 display.surfaceControl =
562 session()->createSurface(String8("BootAnimation"), resolution.width,
563 resolution.height, PIXEL_FORMAT_RGB_565,
564 ISurfaceComposerClient::eOpaque);
565 // Attach surface to layerstack, and associate layerstack with physical display
566 configureDisplayAndLayerStack(display, ui::LayerStack::fromValue(displayIdx));
567 display.surface = display.surfaceControl->getSurface();
568 display.eglSurface = eglCreateWindowSurface(mEgl, config, display.surface.get(), nullptr);
Huihong Luof97c9e22019-05-29 11:08:01 -0700569
Rahul Banerjee0c27488a2024-07-03 18:20:18 -0700570 EGLint w, h;
571 eglQuerySurface(mEgl, display.eglSurface, EGL_WIDTH, &w);
572 eglQuerySurface(mEgl, display.eglSurface, EGL_HEIGHT, &h);
573 if (eglMakeCurrent(mEgl, display.eglSurface, display.eglSurface,
574 mEglContext) == EGL_FALSE) {
575 return NO_INIT;
576 }
577 display.initWidth = display.width = w;
578 display.initHeight = display.height = h;
579 mTargetInset = -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800580
Rahul Banerjee0c27488a2024-07-03 18:20:18 -0700581 // Rotate the boot animation according to the value specified in the sysprop
582 // ro.bootanim.set_orientation_<display_id>. Four values are supported: ORIENTATION_0,
583 // ORIENTATION_90, ORIENTATION_180 and ORIENTATION_270.
584 // If the value isn't specified or is ORIENTATION_0, nothing will be changed.
585 // This is needed to support boot animation in orientations different from the natural
586 // device orientation. For example, on tablets that may want to keep natural orientation
587 // portrait for applications compatibility and to have the boot animation in landscape.
588 rotateAwayFromNaturalOrientationIfNeeded(display);
Mathias Agopian17f638b2009-04-16 20:04:08 -0700589
Rahul Banerjee0c27488a2024-07-03 18:20:18 -0700590 projectSceneToWindow(display);
591 } // end iteration over all display tokens
Yegor Malyshev6925e1b2020-09-10 16:42:20 +0200592
Marin Shalamanov9070c052020-05-18 19:05:17 +0200593 // Register a display event receiver
594 mDisplayEventReceiver = std::make_unique<DisplayEventReceiver>();
595 status_t status = mDisplayEventReceiver->initCheck();
596 SLOGE_IF(status != NO_ERROR, "Initialization of DisplayEventReceiver failed with status: %d",
Rahul Banerjee0c27488a2024-07-03 18:20:18 -0700597 status);
Marin Shalamanov9070c052020-05-18 19:05:17 +0200598 mLooper->addFd(mDisplayEventReceiver->getFd(), 0, Looper::EVENT_INPUT,
Rahul Banerjee0c27488a2024-07-03 18:20:18 -0700599 new DisplayEventCallback(this), nullptr);
Marin Shalamanov9070c052020-05-18 19:05:17 +0200600
Huihong Luo50a2f362018-08-11 09:27:57 -0700601 return NO_ERROR;
602}
603
Rahul Banerjee0c27488a2024-07-03 18:20:18 -0700604void BootAnimation::configureDisplayAndLayerStack(const Display& display,
605 ui::LayerStack layerStack) {
606 SurfaceComposerClient::Transaction t;
607 t.setDisplayLayerStack(display.displayToken, layerStack);
608 t.setLayerStack(display.surfaceControl, layerStack)
609 .setLayer(display.surfaceControl, 0x40000000)
610 .apply();
611}
612
613void BootAnimation::rotateAwayFromNaturalOrientationIfNeeded(Display& display) {
Parth Sane7f7b2212023-04-12 17:23:02 +0000614 ATRACE_CALL();
Mariia Sandrikovaa166f012022-12-10 02:32:14 +0000615 const auto orientation = parseOrientationProperty();
616
617 if (orientation == ui::ROTATION_0) {
618 // Do nothing if the sysprop isn't set or is set to ROTATION_0.
619 return;
620 }
621
622 if (orientation == ui::ROTATION_90 || orientation == ui::ROTATION_270) {
Rahul Banerjee0c27488a2024-07-03 18:20:18 -0700623 std::swap(display.width, display.height);
624 std::swap(display.initWidth, display.initHeight);
625 display.surfaceControl->updateDefaultBufferSize(display.width, display.height);
Mariia Sandrikovaa166f012022-12-10 02:32:14 +0000626 }
627
Rahul Banerjee0c27488a2024-07-03 18:20:18 -0700628 Rect displayRect(0, 0, display.width, display.height);
629 Rect layerStackRect(0, 0, display.width, display.height);
Mariia Sandrikovaa166f012022-12-10 02:32:14 +0000630
631 SurfaceComposerClient::Transaction t;
Rahul Banerjee0c27488a2024-07-03 18:20:18 -0700632 t.setDisplayProjection(display.displayToken, orientation, layerStackRect, displayRect);
Mariia Sandrikovaa166f012022-12-10 02:32:14 +0000633 t.apply();
634}
635
636ui::Rotation BootAnimation::parseOrientationProperty() {
Parth Sane7f7b2212023-04-12 17:23:02 +0000637 ATRACE_CALL();
Mariia Sandrikovaa166f012022-12-10 02:32:14 +0000638 const auto displayIds = SurfaceComposerClient::getPhysicalDisplayIds();
639 if (displayIds.size() == 0) {
640 return ui::ROTATION_0;
641 }
642 const auto displayId = displayIds[0];
643 const auto syspropName = [displayId] {
644 std::stringstream ss;
645 ss << "ro.bootanim.set_orientation_" << displayId.value;
646 return ss.str();
647 }();
YoungJoon Yang9cf990e2023-11-13 15:34:28 +0900648 auto syspropValue = android::base::GetProperty(syspropName, "");
649 if (syspropValue == "") {
650 syspropValue = android::base::GetProperty("ro.bootanim.set_orientation_logical_0", "");
651 }
652
Mariia Sandrikovaa166f012022-12-10 02:32:14 +0000653 if (syspropValue == "ORIENTATION_90") {
654 return ui::ROTATION_90;
655 } else if (syspropValue == "ORIENTATION_180") {
656 return ui::ROTATION_180;
657 } else if (syspropValue == "ORIENTATION_270") {
658 return ui::ROTATION_270;
659 }
660 return ui::ROTATION_0;
661}
662
Rahul Banerjee0c27488a2024-07-03 18:20:18 -0700663void BootAnimation::projectSceneToWindow(const Display& display) {
Parth Sane7f7b2212023-04-12 17:23:02 +0000664 ATRACE_CALL();
Rahul Banerjee0c27488a2024-07-03 18:20:18 -0700665 glViewport(0, 0, display.width, display.height);
666 glScissor(0, 0, display.width, display.height);
Yegor Malyshev6925e1b2020-09-10 16:42:20 +0200667}
668
Rahul Banerjee0c27488a2024-07-03 18:20:18 -0700669void BootAnimation::resizeSurface(int newWidth, int newHeight, Display& display) {
Parth Sane7f7b2212023-04-12 17:23:02 +0000670 ATRACE_CALL();
Marin Shalamanov9070c052020-05-18 19:05:17 +0200671 // We assume this function is called on the animation thread.
Rahul Banerjee0c27488a2024-07-03 18:20:18 -0700672 if (newWidth == display.width && newHeight == display.height) {
Marin Shalamanov9070c052020-05-18 19:05:17 +0200673 return;
674 }
Marin Shalamanov9070c052020-05-18 19:05:17 +0200675
Rahul Banerjee0c27488a2024-07-03 18:20:18 -0700676 eglMakeCurrent(mEgl, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
677 eglDestroySurface(mEgl, display.eglSurface);
Marin Shalamanov9070c052020-05-18 19:05:17 +0200678
Marin Shalamanov047802d2020-05-19 23:55:12 +0200679 const auto limitedSize = limitSurfaceSize(newWidth, newHeight);
Rahul Banerjee0c27488a2024-07-03 18:20:18 -0700680 display.width = limitedSize.width;
681 display.height = limitedSize.height;
Marin Shalamanov9070c052020-05-18 19:05:17 +0200682
Rahul Banerjee0c27488a2024-07-03 18:20:18 -0700683 display.surfaceControl->updateDefaultBufferSize(display.width, display.height);
684 EGLConfig config = getEglConfig(mEgl);
685 EGLSurface eglSurface = eglCreateWindowSurface(mEgl, config, display.surface.get(), nullptr);
686 if (eglMakeCurrent(mEgl, eglSurface, eglSurface, mEglContext) == EGL_FALSE) {
687 SLOGE("Can't make the new eglSurface current. Error %d", eglGetError());
Marin Shalamanov9070c052020-05-18 19:05:17 +0200688 return;
689 }
Yegor Malyshev6925e1b2020-09-10 16:42:20 +0200690
Rahul Banerjee0c27488a2024-07-03 18:20:18 -0700691 projectSceneToWindow(display);
Marin Shalamanov9070c052020-05-18 19:05:17 +0200692
Rahul Banerjee0c27488a2024-07-03 18:20:18 -0700693 display.eglSurface = eglSurface;
Marin Shalamanov9070c052020-05-18 19:05:17 +0200694}
695
Huihong Luo50a2f362018-08-11 09:27:57 -0700696bool BootAnimation::preloadAnimation() {
Parth Sane7f7b2212023-04-12 17:23:02 +0000697 ATRACE_CALL();
Huihong Luo50a2f362018-08-11 09:27:57 -0700698 findBootAnimationFile();
Tomasz Wasilczyke8fb3c72023-08-24 19:11:22 +0000699 if (!mZipFileName.empty()) {
Huihong Luo50a2f362018-08-11 09:27:57 -0700700 mAnimation = loadAnimation(mZipFileName);
701 return (mAnimation != nullptr);
702 }
703
704 return false;
705}
706
Nikita Ioffe06c986e2020-01-27 17:16:03 +0000707bool BootAnimation::findBootAnimationFileInternal(const std::vector<std::string> &files) {
Parth Sane7f7b2212023-04-12 17:23:02 +0000708 ATRACE_CALL();
Nikita Ioffe06c986e2020-01-27 17:16:03 +0000709 for (const std::string& f : files) {
710 if (access(f.c_str(), R_OK) == 0) {
711 mZipFileName = f.c_str();
712 return true;
713 }
714 }
715 return false;
716}
717
Huihong Luo50a2f362018-08-11 09:27:57 -0700718void BootAnimation::findBootAnimationFile() {
Parth Sane7f7b2212023-04-12 17:23:02 +0000719 ATRACE_CALL();
Beverly34ffe252019-05-17 11:03:54 -0400720 const bool playDarkAnim = android::base::GetIntProperty("ro.boot.theme", 0) == 1;
jorgetmbd89e9b2024-09-09 19:42:26 +0000721 const std::string productBootanimationFile = PRODUCT_BOOTANIMATION_DIR +
722 android::base::GetProperty("ro.product.bootanim.file", playDarkAnim ?
723 PRODUCT_BOOTANIMATION_DARK_FILE : PRODUCT_BOOTANIMATION_FILE);
Nikita Ioffe06c986e2020-01-27 17:16:03 +0000724 static const std::vector<std::string> bootFiles = {
jorgetmbd89e9b2024-09-09 19:42:26 +0000725 APEX_BOOTANIMATION_FILE, productBootanimationFile,
Nikita Ioffe06c986e2020-01-27 17:16:03 +0000726 OEM_BOOTANIMATION_FILE, SYSTEM_BOOTANIMATION_FILE
727 };
728 static const std::vector<std::string> shutdownFiles = {
729 PRODUCT_SHUTDOWNANIMATION_FILE, OEM_SHUTDOWNANIMATION_FILE, SYSTEM_SHUTDOWNANIMATION_FILE, ""
730 };
731 static const std::vector<std::string> userspaceRebootFiles = {
732 PRODUCT_USERSPACE_REBOOT_ANIMATION_FILE, OEM_USERSPACE_REBOOT_ANIMATION_FILE,
733 SYSTEM_USERSPACE_REBOOT_ANIMATION_FILE,
734 };
Keun-young Parkb5938422017-03-23 13:46:24 -0700735
Nikita Ioffe06c986e2020-01-27 17:16:03 +0000736 if (android::base::GetBoolProperty("sys.init.userspace_reboot.in_progress", false)) {
737 findBootAnimationFileInternal(userspaceRebootFiles);
738 } else if (mShuttingDown) {
739 findBootAnimationFileInternal(shutdownFiles);
740 } else {
741 findBootAnimationFileInternal(bootFiles);
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700742 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800743}
744
Lucas Dupin8eedc022021-07-29 21:49:07 +0000745GLuint compileShader(GLenum shaderType, const GLchar *source) {
Parth Sane7f7b2212023-04-12 17:23:02 +0000746 ATRACE_CALL();
Lucas Dupin8eedc022021-07-29 21:49:07 +0000747 GLuint shader = glCreateShader(shaderType);
748 glShaderSource(shader, 1, &source, 0);
749 glCompileShader(shader);
750 GLint isCompiled = 0;
751 glGetShaderiv(shader, GL_COMPILE_STATUS, &isCompiled);
752 if (isCompiled == GL_FALSE) {
753 SLOGE("Compile shader failed. Shader type: %d", shaderType);
Shan Huang81846ba2021-09-07 19:07:47 +0000754 GLint maxLength = 0;
755 glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &maxLength);
756 std::vector<GLchar> errorLog(maxLength);
757 glGetShaderInfoLog(shader, maxLength, &maxLength, &errorLog[0]);
758 SLOGE("Shader compilation error: %s", &errorLog[0]);
Lucas Dupin8eedc022021-07-29 21:49:07 +0000759 return 0;
760 }
761 return shader;
762}
763
764GLuint linkShader(GLuint vertexShader, GLuint fragmentShader) {
Parth Sane7f7b2212023-04-12 17:23:02 +0000765 ATRACE_CALL();
Lucas Dupin8eedc022021-07-29 21:49:07 +0000766 GLuint program = glCreateProgram();
767 glAttachShader(program, vertexShader);
768 glAttachShader(program, fragmentShader);
769 glLinkProgram(program);
770 GLint isLinked = 0;
771 glGetProgramiv(program, GL_LINK_STATUS, (int *)&isLinked);
772 if (isLinked == GL_FALSE) {
773 SLOGE("Linking shader failed. Shader handles: vert %d, frag %d",
774 vertexShader, fragmentShader);
775 return 0;
776 }
777 return program;
778}
779
780void BootAnimation::initShaders() {
Parth Sane7f7b2212023-04-12 17:23:02 +0000781 ATRACE_CALL();
Shan Huang2b31a362021-08-16 16:45:39 +0000782 bool dynamicColoringEnabled = mAnimation != nullptr && mAnimation->dynamicColoringEnabled;
Lucas Dupin8eedc022021-07-29 21:49:07 +0000783 GLuint vertexShader = compileShader(GL_VERTEX_SHADER, (const GLchar *)VERTEX_SHADER_SOURCE);
784 GLuint imageFragmentShader =
Shan Huang2b31a362021-08-16 16:45:39 +0000785 compileShader(GL_FRAGMENT_SHADER, dynamicColoringEnabled
786 ? (const GLchar *)IMAGE_FRAG_DYNAMIC_COLORING_SHADER_SOURCE
787 : (const GLchar *)IMAGE_FRAG_SHADER_SOURCE);
Lucas Dupin8eedc022021-07-29 21:49:07 +0000788 GLuint textFragmentShader =
789 compileShader(GL_FRAGMENT_SHADER, (const GLchar *)TEXT_FRAG_SHADER_SOURCE);
790
791 // Initialize image shader.
792 mImageShader = linkShader(vertexShader, imageFragmentShader);
793 GLint positionLocation = glGetAttribLocation(mImageShader, A_POSITION);
794 GLint uvLocation = glGetAttribLocation(mImageShader, A_UV);
795 mImageTextureLocation = glGetUniformLocation(mImageShader, U_TEXTURE);
796 mImageFadeLocation = glGetUniformLocation(mImageShader, U_FADE);
797 glEnableVertexAttribArray(positionLocation);
798 glVertexAttribPointer(positionLocation, 2, GL_FLOAT, GL_FALSE, 0, quadPositions);
799 glVertexAttribPointer(uvLocation, 2, GL_FLOAT, GL_FALSE, 0, quadUVs);
800 glEnableVertexAttribArray(uvLocation);
801
802 // Initialize text shader.
803 mTextShader = linkShader(vertexShader, textFragmentShader);
804 positionLocation = glGetAttribLocation(mTextShader, A_POSITION);
805 uvLocation = glGetAttribLocation(mTextShader, A_UV);
806 mTextTextureLocation = glGetUniformLocation(mTextShader, U_TEXTURE);
807 mTextCropAreaLocation = glGetUniformLocation(mTextShader, U_CROP_AREA);
808 glEnableVertexAttribArray(positionLocation);
809 glVertexAttribPointer(positionLocation, 2, GL_FLOAT, GL_FALSE, 0, quadPositions);
810 glVertexAttribPointer(uvLocation, 2, GL_FLOAT, GL_FALSE, 0, quadUVs);
811 glEnableVertexAttribArray(uvLocation);
812}
813
Marin Shalamanov9070c052020-05-18 19:05:17 +0200814bool BootAnimation::threadLoop() {
Parth Sane7f7b2212023-04-12 17:23:02 +0000815 ATRACE_CALL();
Marin Shalamanov9070c052020-05-18 19:05:17 +0200816 bool result;
Lucas Dupin8eedc022021-07-29 21:49:07 +0000817 initShaders();
818
Narayan Kamathafd31e02013-12-03 13:16:03 +0000819 // We have no bootanimation file, so we use the stock android logo
820 // animation.
Tomasz Wasilczyke8fb3c72023-08-24 19:11:22 +0000821 if (mZipFileName.empty()) {
Yoshiaki Maruoka9ef19b82017-10-25 13:55:49 +0900822 ALOGD("No animation file");
Rahul Banerjee0c27488a2024-07-03 18:20:18 -0700823 result = android(mDisplays.front());
Mathias Agopiana8826d62009-10-01 03:10:14 -0700824 } else {
Marin Shalamanov9070c052020-05-18 19:05:17 +0200825 result = movie();
Mathias Agopiana8826d62009-10-01 03:10:14 -0700826 }
827
Philip Junker60600562020-11-25 18:16:16 +0100828 mCallbacks->shutdown();
Rahul Banerjee0c27488a2024-07-03 18:20:18 -0700829 eglMakeCurrent(mEgl, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
830 eglDestroyContext(mEgl, mEglContext);
831 for (auto& display : mDisplays) {
832 eglDestroySurface(mEgl, display.eglSurface);
833 display.surface.clear();
834 display.surfaceControl.clear();
835 }
836 eglTerminate(mEgl);
Sai Kiran Korwar3eee9fb2015-06-16 17:13:35 +0530837 eglReleaseThread();
Mathias Agopian627e7b52009-05-21 19:21:59 -0700838 IPCThreadState::self()->stopProcess();
Marin Shalamanov9070c052020-05-18 19:05:17 +0200839 return result;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800840}
841
Rahul Banerjee0c27488a2024-07-03 18:20:18 -0700842bool BootAnimation::android(const Display& display) {
Parth Sane7f7b2212023-04-12 17:23:02 +0000843 ATRACE_CALL();
Lucas Dupin8eedc022021-07-29 21:49:07 +0000844 glActiveTexture(GL_TEXTURE0);
845
Wei Wang159a4102018-10-23 23:15:58 -0700846 SLOGD("%sAnimationShownTiming start time: %" PRId64 "ms", mShuttingDown ? "Shutdown" : "Boot",
Keun-young Parkd1794cd2017-04-04 12:21:19 -0700847 elapsedRealtime());
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700848 initTexture(&mAndroid[0], mAssets, "images/android-logo-mask.png");
849 initTexture(&mAndroid[1], mAssets, "images/android-logo-shine.png");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800850
Ed Coyne7464ac92017-06-08 12:26:48 -0700851 mCallbacks->init({});
852
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800853 // clear screen
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700854 glDisable(GL_DITHER);
855 glDisable(GL_SCISSOR_TEST);
Shan Huangbfd2d3c2021-09-09 23:19:04 +0000856 glUseProgram(mImageShader);
857
Mathias Agopian59f19e42011-05-06 19:22:12 -0700858 glClearColor(0,0,0,1);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800859 glClear(GL_COLOR_BUFFER_BIT);
Rahul Banerjee0c27488a2024-07-03 18:20:18 -0700860 eglSwapBuffers(mEgl, display.eglSurface);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800861
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700862 // Blend state
863 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700864
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800865 const nsecs_t startTime = systemTime();
866 do {
Marin Shalamanov9070c052020-05-18 19:05:17 +0200867 processDisplayEvents();
Rahul Banerjee0c27488a2024-07-03 18:20:18 -0700868 const GLint xc = (display.width - mAndroid[0].w) / 2;
869 const GLint yc = (display.height - mAndroid[0].h) / 2;
Marin Shalamanov9070c052020-05-18 19:05:17 +0200870 const Rect updateRect(xc, yc, xc + mAndroid[0].w, yc + mAndroid[0].h);
Rahul Banerjee0c27488a2024-07-03 18:20:18 -0700871 glScissor(updateRect.left, display.height - updateRect.bottom, updateRect.width(),
872 updateRect.height());
Marin Shalamanov9070c052020-05-18 19:05:17 +0200873
Mathias Agopian13796652009-03-24 22:49:21 -0700874 nsecs_t now = systemTime();
875 double time = now - startTime;
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700876 float t = 4.0f * float(time / us2ns(16667)) / mAndroid[1].w;
877 GLint offset = (1 - (t - floorf(t))) * mAndroid[1].w;
878 GLint x = xc - offset;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800879
Mathias Agopian81668642009-07-28 11:41:30 -0700880 glDisable(GL_SCISSOR_TEST);
881 glClear(GL_COLOR_BUFFER_BIT);
882
883 glEnable(GL_SCISSOR_TEST);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800884 glDisable(GL_BLEND);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800885 glBindTexture(GL_TEXTURE_2D, mAndroid[1].name);
Rahul Banerjee0c27488a2024-07-03 18:20:18 -0700886 drawTexturedQuad(x, yc, mAndroid[1].w, mAndroid[1].h, display);
887 drawTexturedQuad(x + mAndroid[1].w, yc, mAndroid[1].w, mAndroid[1].h, display);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800888
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700889 glEnable(GL_BLEND);
890 glBindTexture(GL_TEXTURE_2D, mAndroid[0].name);
Rahul Banerjee0c27488a2024-07-03 18:20:18 -0700891 drawTexturedQuad(xc, yc, mAndroid[0].w, mAndroid[0].h, display);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800892
Rahul Banerjee0c27488a2024-07-03 18:20:18 -0700893 EGLBoolean res = eglSwapBuffers(mEgl, display.eglSurface);
Mathias Agopian627e7b52009-05-21 19:21:59 -0700894 if (res == EGL_FALSE)
895 break;
896
Mathias Agopian13796652009-03-24 22:49:21 -0700897 // 12fps: don't animate too fast to preserve CPU
898 const nsecs_t sleepTime = 83333 - ns2us(systemTime() - now);
899 if (sleepTime > 0)
Mathias Agopiana8826d62009-10-01 03:10:14 -0700900 usleep(sleepTime);
Kevin Hesterd3782b22012-04-26 10:38:55 -0700901
902 checkExit();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800903 } while (!exitPending());
904
905 glDeleteTextures(1, &mAndroid[0].name);
906 glDeleteTextures(1, &mAndroid[1].name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800907 return false;
908}
909
Kevin Hesterd3782b22012-04-26 10:38:55 -0700910void BootAnimation::checkExit() {
Parth Sane7f7b2212023-04-12 17:23:02 +0000911 ATRACE_CALL();
Rahul Banerjee0c27488a2024-07-03 18:20:18 -0700912 // Allow SurfaceFlinger to gracefully request shutdown
Kevin Hesterd3782b22012-04-26 10:38:55 -0700913 char value[PROPERTY_VALUE_MAX];
914 property_get(EXIT_PROP_NAME, value, "0");
915 int exitnow = atoi(value);
916 if (exitnow) {
917 requestExit();
918 }
919}
920
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700921bool BootAnimation::validClock(const Animation::Part& part) {
Parth Sane7f7b2212023-04-12 17:23:02 +0000922 ATRACE_CALL();
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700923 return part.clockPosX != TEXT_MISSING_VALUE && part.clockPosY != TEXT_MISSING_VALUE;
924}
925
926bool parseTextCoord(const char* str, int* dest) {
Parth Sane7f7b2212023-04-12 17:23:02 +0000927 ATRACE_CALL();
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700928 if (strcmp("c", str) == 0) {
929 *dest = TEXT_CENTER_VALUE;
930 return true;
931 }
932
933 char* end;
934 int val = (int) strtol(str, &end, 0);
935 if (end == str || *end != '\0' || val == INT_MAX || val == INT_MIN) {
936 return false;
937 }
938 *dest = val;
939 return true;
940}
941
942// Parse two position coordinates. If only string is non-empty, treat it as the y value.
943void parsePosition(const char* str1, const char* str2, int* x, int* y) {
Parth Sane7f7b2212023-04-12 17:23:02 +0000944 ATRACE_CALL();
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700945 bool success = false;
946 if (strlen(str1) == 0) { // No values were specified
947 // success = false
948 } else if (strlen(str2) == 0) { // we have only one value
949 if (parseTextCoord(str1, y)) {
950 *x = TEXT_CENTER_VALUE;
951 success = true;
952 }
953 } else {
954 if (parseTextCoord(str1, x) && parseTextCoord(str2, y)) {
955 success = true;
956 }
957 }
958
959 if (!success) {
960 *x = TEXT_MISSING_VALUE;
961 *y = TEXT_MISSING_VALUE;
962 }
963}
964
Jesse Hall083b84c2014-09-22 10:51:09 -0700965// Parse a color represented as an HTML-style 'RRGGBB' string: each pair of
966// characters in str is a hex number in [0, 255], which are converted to
967// floating point values in the range [0.0, 1.0] and placed in the
968// corresponding elements of color.
969//
970// If the input string isn't valid, parseColor returns false and color is
971// left unchanged.
972static bool parseColor(const char str[7], float color[3]) {
Parth Sane7f7b2212023-04-12 17:23:02 +0000973 ATRACE_CALL();
Jesse Hall083b84c2014-09-22 10:51:09 -0700974 float tmpColor[3];
975 for (int i = 0; i < 3; i++) {
976 int val = 0;
977 for (int j = 0; j < 2; j++) {
978 val *= 16;
979 char c = str[2*i + j];
980 if (c >= '0' && c <= '9') val += c - '0';
981 else if (c >= 'A' && c <= 'F') val += (c - 'A') + 10;
982 else if (c >= 'a' && c <= 'f') val += (c - 'a') + 10;
983 else return false;
984 }
985 tmpColor[i] = static_cast<float>(val) / 255.0f;
986 }
987 memcpy(color, tmpColor, sizeof(tmpColor));
988 return true;
989}
990
Shan Huang2b31a362021-08-16 16:45:39 +0000991// Parse a color represented as a signed decimal int string.
992// E.g. "-2757722" (whose hex 2's complement is 0xFFD5EBA6).
993// If the input color string is empty, set color with values in defaultColor.
994static void parseColorDecimalString(const std::string& colorString,
995 float color[3], float defaultColor[3]) {
Parth Sane7f7b2212023-04-12 17:23:02 +0000996 ATRACE_CALL();
Shan Huang2b31a362021-08-16 16:45:39 +0000997 if (colorString == "") {
998 memcpy(color, defaultColor, sizeof(float) * 3);
999 return;
1000 }
1001 int colorInt = atoi(colorString.c_str());
1002 color[0] = ((float)((colorInt >> 16) & 0xFF)) / 0xFF; // r
1003 color[1] = ((float)((colorInt >> 8) & 0xFF)) / 0xFF; // g
1004 color[2] = ((float)(colorInt & 0xFF)) / 0xFF; // b
1005}
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001006
Marin Shalamanov9070c052020-05-18 19:05:17 +02001007static bool readFile(ZipFileRO* zip, const char* name, String8& outString) {
Parth Sane7f7b2212023-04-12 17:23:02 +00001008 ATRACE_CALL();
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001009 ZipEntryRO entry = zip->findEntryByName(name);
Wei Wang159a4102018-10-23 23:15:58 -07001010 SLOGE_IF(!entry, "couldn't find %s", name);
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -07001011 if (!entry) {
1012 return false;
1013 }
1014
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001015 FileMap* entryMap = zip->createEntryFileMap(entry);
1016 zip->releaseEntry(entry);
Wei Wang159a4102018-10-23 23:15:58 -07001017 SLOGE_IF(!entryMap, "entryMap is null");
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -07001018 if (!entryMap) {
1019 return false;
1020 }
1021
Tomasz Wasilczyk31eb3c892023-08-23 22:12:33 +00001022 outString = String8((char const*)entryMap->getDataPtr(), entryMap->getDataLength());
Narayan Kamath688ff4c2015-02-23 15:47:54 +00001023 delete entryMap;
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -07001024 return true;
1025}
1026
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -07001027// The font image should be a 96x2 array of character images. The
1028// columns are the printable ASCII characters 0x20 - 0x7f. The
1029// top row is regular text; the bottom row is bold.
1030status_t BootAnimation::initFont(Font* font, const char* fallback) {
Parth Sane7f7b2212023-04-12 17:23:02 +00001031 ATRACE_CALL();
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -07001032 status_t status = NO_ERROR;
Damien Bargiacchia704b7d2016-02-16 16:55:49 -08001033
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -07001034 if (font->map != nullptr) {
1035 glGenTextures(1, &font->texture.name);
1036 glBindTexture(GL_TEXTURE_2D, font->texture.name);
Damien Bargiacchia704b7d2016-02-16 16:55:49 -08001037
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -07001038 status = initTexture(font->map, &font->texture.w, &font->texture.h);
1039
Lucas Dupin8eedc022021-07-29 21:49:07 +00001040 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
1041 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
1042 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
1043 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -07001044 } else if (fallback != nullptr) {
1045 status = initTexture(&font->texture, mAssets, fallback);
1046 } else {
1047 return NO_INIT;
Damien Bargiacchia704b7d2016-02-16 16:55:49 -08001048 }
1049
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -07001050 if (status == NO_ERROR) {
1051 font->char_width = font->texture.w / FONT_NUM_COLS;
1052 font->char_height = font->texture.h / FONT_NUM_ROWS / 2; // There are bold and regular rows
1053 }
1054
1055 return status;
1056}
1057
Rahul Banerjee0c27488a2024-07-03 18:20:18 -07001058void BootAnimation::drawText(const char* str, const Font& font, bool bold,
1059 int* x, int* y, const Display& display) {
Parth Sane7f7b2212023-04-12 17:23:02 +00001060 ATRACE_CALL();
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -07001061 glEnable(GL_BLEND); // Allow us to draw on top of the animation
1062 glBindTexture(GL_TEXTURE_2D, font.texture.name);
Lucas Dupin8eedc022021-07-29 21:49:07 +00001063 glUseProgram(mTextShader);
1064 glUniform1i(mTextTextureLocation, 0);
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -07001065
1066 const int len = strlen(str);
1067 const int strWidth = font.char_width * len;
1068
1069 if (*x == TEXT_CENTER_VALUE) {
Rahul Banerjee0c27488a2024-07-03 18:20:18 -07001070 *x = (display.width - strWidth) / 2;
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -07001071 } else if (*x < 0) {
Rahul Banerjee0c27488a2024-07-03 18:20:18 -07001072 *x = display.width + *x - strWidth;
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -07001073 }
1074 if (*y == TEXT_CENTER_VALUE) {
Rahul Banerjee0c27488a2024-07-03 18:20:18 -07001075 *y = (display.height - font.char_height) / 2;
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -07001076 } else if (*y < 0) {
Rahul Banerjee0c27488a2024-07-03 18:20:18 -07001077 *y = display.height + *y - font.char_height;
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -07001078 }
1079
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -07001080 for (int i = 0; i < len; i++) {
1081 char c = str[i];
1082
1083 if (c < FONT_BEGIN_CHAR || c > FONT_END_CHAR) {
1084 c = '?';
1085 }
1086
1087 // Crop the texture to only the pixels in the current glyph
1088 const int charPos = (c - FONT_BEGIN_CHAR); // Position in the list of valid characters
1089 const int row = charPos / FONT_NUM_COLS;
1090 const int col = charPos % FONT_NUM_COLS;
Lucas Dupin8eedc022021-07-29 21:49:07 +00001091 // Bold fonts are expected in the second half of each row.
1092 float v0 = (row + (bold ? 0.5f : 0.0f)) / FONT_NUM_ROWS;
1093 float u0 = ((float)col) / FONT_NUM_COLS;
1094 float v1 = v0 + 1.0f / FONT_NUM_ROWS / 2;
1095 float u1 = u0 + 1.0f / FONT_NUM_COLS;
1096 glUniform4f(mTextCropAreaLocation, u0, v0, u1, v1);
Rahul Banerjee0c27488a2024-07-03 18:20:18 -07001097 drawTexturedQuad(*x, *y, font.char_width, font.char_height, display);
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -07001098
1099 *x += font.char_width;
1100 }
1101
1102 glDisable(GL_BLEND); // Return to the animation's default behaviour
1103 glBindTexture(GL_TEXTURE_2D, 0);
1104}
1105
Damien Bargiacchi9071db12016-10-28 17:38:22 -07001106// We render 12 or 24 hour time.
Rahul Banerjee0c27488a2024-07-03 18:20:18 -07001107void BootAnimation::drawClock(const Font& font, const int xPos, const int yPos,
1108 const Display& display) {
Parth Sane7f7b2212023-04-12 17:23:02 +00001109 ATRACE_CALL();
Damien Bargiacchi9071db12016-10-28 17:38:22 -07001110 static constexpr char TIME_FORMAT_12[] = "%l:%M";
1111 static constexpr char TIME_FORMAT_24[] = "%H:%M";
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -07001112 static constexpr int TIME_LENGTH = 6;
1113
Damien Bargiacchia704b7d2016-02-16 16:55:49 -08001114 time_t rawtime;
1115 time(&rawtime);
1116 struct tm* timeInfo = localtime(&rawtime);
1117
1118 char timeBuff[TIME_LENGTH];
Damien Bargiacchi9071db12016-10-28 17:38:22 -07001119 const char* timeFormat = mTimeFormat12Hour ? TIME_FORMAT_12 : TIME_FORMAT_24;
1120 size_t length = strftime(timeBuff, TIME_LENGTH, timeFormat, timeInfo);
Damien Bargiacchia704b7d2016-02-16 16:55:49 -08001121
1122 if (length != TIME_LENGTH - 1) {
Wei Wang159a4102018-10-23 23:15:58 -07001123 SLOGE("Couldn't format time; abandoning boot animation clock");
Damien Bargiacchia704b7d2016-02-16 16:55:49 -08001124 mClockEnabled = false;
1125 return;
1126 }
1127
Damien Bargiacchi45a76442016-12-05 18:02:18 -08001128 char* out = timeBuff[0] == ' ' ? &timeBuff[1] : &timeBuff[0];
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -07001129 int x = xPos;
1130 int y = yPos;
Rahul Banerjee0c27488a2024-07-03 18:20:18 -07001131 drawText(out, font, false, &x, &y, display);
Damien Bargiacchia704b7d2016-02-16 16:55:49 -08001132}
1133
Rahul Banerjee0c27488a2024-07-03 18:20:18 -07001134void BootAnimation::drawProgress(int percent, const Font& font, const int xPos, const int yPos,
1135 const Display& display) {
Parth Sane7f7b2212023-04-12 17:23:02 +00001136 ATRACE_CALL();
Nicolas Geoffray41fffb42020-12-15 10:50:29 +00001137 static constexpr int PERCENT_LENGTH = 5;
1138
1139 char percentBuff[PERCENT_LENGTH];
1140 // ';' has the ascii code just after ':', and the font resource contains '%'
1141 // for that ascii code.
1142 sprintf(percentBuff, "%d;", percent);
1143 int x = xPos;
1144 int y = yPos;
Rahul Banerjee0c27488a2024-07-03 18:20:18 -07001145 drawText(percentBuff, font, false, &x, &y, display);
Nicolas Geoffray41fffb42020-12-15 10:50:29 +00001146}
1147
Marin Shalamanov9070c052020-05-18 19:05:17 +02001148bool BootAnimation::parseAnimationDesc(Animation& animation) {
Parth Sane7f7b2212023-04-12 17:23:02 +00001149 ATRACE_CALL();
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -07001150 String8 desString;
1151
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001152 if (!readFile(animation.zip, "desc.txt", desString)) {
Narayan Kamathafd31e02013-12-03 13:16:03 +00001153 return false;
1154 }
Tomasz Wasilczyk3815d342023-08-10 23:54:44 +00001155 char const* s = desString.c_str();
Shan Huang2b31a362021-08-16 16:45:39 +00001156 std::string dynamicColoringPartName = "";
1157 bool postDynamicColoring = false;
Mathias Agopiana8826d62009-10-01 03:10:14 -07001158
Mathias Agopiana8826d62009-10-01 03:10:14 -07001159 // Parse the description file
1160 for (;;) {
1161 const char* endl = strstr(s, "\n");
Yi Kong911ac232019-03-24 01:49:02 -07001162 if (endl == nullptr) break;
Mathias Agopiana8826d62009-10-01 03:10:14 -07001163 String8 line(s, endl - s);
Tomasz Wasilczyk3815d342023-08-10 23:54:44 +00001164 const char* l = line.c_str();
Damien Bargiacchia704b7d2016-02-16 16:55:49 -08001165 int fps = 0;
1166 int width = 0;
1167 int height = 0;
1168 int count = 0;
1169 int pause = 0;
Nicolas Geoffray41fffb42020-12-15 10:50:29 +00001170 int progress = 0;
Yegor Malyshev6925e1b2020-09-10 16:42:20 +02001171 int framesToFadeCount = 0;
Shan Huang488ff742021-08-25 20:10:46 +00001172 int colorTransitionStart = 0;
1173 int colorTransitionEnd = 0;
Narayan Kamathafd31e02013-12-03 13:16:03 +00001174 char path[ANIM_ENTRY_NAME_MAX];
Jesse Hall083b84c2014-09-22 10:51:09 -07001175 char color[7] = "000000"; // default to black if unspecified
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -07001176 char clockPos1[TEXT_POS_LEN_MAX + 1] = "";
1177 char clockPos2[TEXT_POS_LEN_MAX + 1] = "";
Shan Huang2b31a362021-08-16 16:45:39 +00001178 char dynamicColoringPartNameBuffer[ANIM_ENTRY_NAME_MAX];
Kevin Hesterd3782b22012-04-26 10:38:55 -07001179 char pathType;
Shan Huang2b31a362021-08-16 16:45:39 +00001180 // start colors default to black if unspecified
1181 char start_color_0[7] = "000000";
1182 char start_color_1[7] = "000000";
1183 char start_color_2[7] = "000000";
1184 char start_color_3[7] = "000000";
Yegor Malyshev6925e1b2020-09-10 16:42:20 +02001185
1186 int nextReadPos;
1187
yidong zhang87bb3462022-12-07 15:55:31 +08001188 if (strlen(l) == 0) {
1189 s = ++endl;
1190 continue;
1191 }
1192
Nicolas Geoffray41fffb42020-12-15 10:50:29 +00001193 int topLineNumbers = sscanf(l, "%d %d %d %d", &width, &height, &fps, &progress);
1194 if (topLineNumbers == 3 || topLineNumbers == 4) {
1195 // SLOGD("> w=%d, h=%d, fps=%d, progress=%d", width, height, fps, progress);
Mathias Agopiana8826d62009-10-01 03:10:14 -07001196 animation.width = width;
1197 animation.height = height;
1198 animation.fps = fps;
Nicolas Geoffray41fffb42020-12-15 10:50:29 +00001199 if (topLineNumbers == 4) {
1200 animation.progressEnabled = (progress != 0);
1201 } else {
1202 animation.progressEnabled = false;
1203 }
Shan Huang488ff742021-08-25 20:10:46 +00001204 } else if (sscanf(l, "dynamic_colors %" STRTO(ANIM_PATH_MAX) "s #%6s #%6s #%6s #%6s %d %d",
Shan Huang2b31a362021-08-16 16:45:39 +00001205 dynamicColoringPartNameBuffer,
Shan Huang488ff742021-08-25 20:10:46 +00001206 start_color_0, start_color_1, start_color_2, start_color_3,
1207 &colorTransitionStart, &colorTransitionEnd)) {
Shan Huang2b31a362021-08-16 16:45:39 +00001208 animation.dynamicColoringEnabled = true;
1209 parseColor(start_color_0, animation.startColors[0]);
1210 parseColor(start_color_1, animation.startColors[1]);
1211 parseColor(start_color_2, animation.startColors[2]);
1212 parseColor(start_color_3, animation.startColors[3]);
Shan Huang488ff742021-08-25 20:10:46 +00001213 animation.colorTransitionStart = colorTransitionStart;
1214 animation.colorTransitionEnd = colorTransitionEnd;
Shan Huang2b31a362021-08-16 16:45:39 +00001215 dynamicColoringPartName = std::string(dynamicColoringPartNameBuffer);
Yegor Malyshev6925e1b2020-09-10 16:42:20 +02001216 } else if (sscanf(l, "%c %d %d %" STRTO(ANIM_PATH_MAX) "s%n",
1217 &pathType, &count, &pause, path, &nextReadPos) >= 4) {
1218 if (pathType == 'f') {
1219 sscanf(l + nextReadPos, " %d #%6s %16s %16s", &framesToFadeCount, color, clockPos1,
1220 clockPos2);
1221 } else {
1222 sscanf(l + nextReadPos, " #%6s %16s %16s", color, clockPos1, clockPos2);
1223 }
1224 // SLOGD("> type=%c, count=%d, pause=%d, path=%s, framesToFadeCount=%d, color=%s, "
1225 // "clockPos1=%s, clockPos2=%s",
1226 // pathType, count, pause, path, framesToFadeCount, color, clockPos1, clockPos2);
Mathias Agopiana8826d62009-10-01 03:10:14 -07001227 Animation::Part part;
Shan Huang2b31a362021-08-16 16:45:39 +00001228 if (path == dynamicColoringPartName) {
1229 // Part is specified to use dynamic coloring.
1230 part.useDynamicColoring = true;
1231 part.postDynamicColoring = false;
1232 postDynamicColoring = true;
1233 } else {
1234 // Part does not use dynamic coloring.
1235 part.useDynamicColoring = false;
1236 part.postDynamicColoring = postDynamicColoring;
1237 }
Kevin Hesterd3782b22012-04-26 10:38:55 -07001238 part.playUntilComplete = pathType == 'c';
Yegor Malyshev6925e1b2020-09-10 16:42:20 +02001239 part.framesToFadeCount = framesToFadeCount;
Mathias Agopiana8826d62009-10-01 03:10:14 -07001240 part.count = count;
1241 part.pause = pause;
1242 part.path = path;
Yi Kong911ac232019-03-24 01:49:02 -07001243 part.audioData = nullptr;
1244 part.animation = nullptr;
Jesse Hall083b84c2014-09-22 10:51:09 -07001245 if (!parseColor(color, part.backgroundColor)) {
Wei Wang159a4102018-10-23 23:15:58 -07001246 SLOGE("> invalid color '#%s'", color);
Jesse Hall083b84c2014-09-22 10:51:09 -07001247 part.backgroundColor[0] = 0.0f;
1248 part.backgroundColor[1] = 0.0f;
1249 part.backgroundColor[2] = 0.0f;
1250 }
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -07001251 parsePosition(clockPos1, clockPos2, &part.clockPosX, &part.clockPosY);
Mathias Agopiana8826d62009-10-01 03:10:14 -07001252 animation.parts.add(part);
1253 }
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001254 else if (strcmp(l, "$SYSTEM") == 0) {
Wei Wang159a4102018-10-23 23:15:58 -07001255 // SLOGD("> SYSTEM");
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001256 Animation::Part part;
1257 part.playUntilComplete = false;
Yegor Malyshev6925e1b2020-09-10 16:42:20 +02001258 part.framesToFadeCount = 0;
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001259 part.count = 1;
1260 part.pause = 0;
Yi Kong911ac232019-03-24 01:49:02 -07001261 part.audioData = nullptr;
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001262 part.animation = loadAnimation(String8(SYSTEM_BOOTANIMATION_FILE));
Yi Kong911ac232019-03-24 01:49:02 -07001263 if (part.animation != nullptr)
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001264 animation.parts.add(part);
1265 }
Mathias Agopiana8826d62009-10-01 03:10:14 -07001266 s = ++endl;
1267 }
1268
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001269 return true;
1270}
1271
Marin Shalamanov9070c052020-05-18 19:05:17 +02001272bool BootAnimation::preloadZip(Animation& animation) {
Parth Sane7f7b2212023-04-12 17:23:02 +00001273 ATRACE_CALL();
Rahul Banerjee0c27488a2024-07-03 18:20:18 -07001274 const size_t numParts = animation.parts.size();
Yi Kong911ac232019-03-24 01:49:02 -07001275 void *cookie = nullptr;
Geoffrey Pitschdd214a72016-06-27 17:14:30 -04001276 ZipFileRO* zip = animation.zip;
1277 if (!zip->startIteration(&cookie)) {
Narayan Kamathafd31e02013-12-03 13:16:03 +00001278 return false;
1279 }
1280
1281 ZipEntryRO entry;
1282 char name[ANIM_ENTRY_NAME_MAX];
Yi Kong911ac232019-03-24 01:49:02 -07001283 while ((entry = zip->nextEntry(cookie)) != nullptr) {
Geoffrey Pitschdd214a72016-06-27 17:14:30 -04001284 const int foundEntryName = zip->getEntryFileName(entry, name, ANIM_ENTRY_NAME_MAX);
Narayan Kamathafd31e02013-12-03 13:16:03 +00001285 if (foundEntryName > ANIM_ENTRY_NAME_MAX || foundEntryName == -1) {
Wei Wang159a4102018-10-23 23:15:58 -07001286 SLOGE("Error fetching entry file name");
Narayan Kamathafd31e02013-12-03 13:16:03 +00001287 continue;
1288 }
1289
Tomasz Wasilczyk9dcea572023-08-22 20:39:20 +00001290 const std::filesystem::path entryName(name);
1291 const std::filesystem::path path(entryName.parent_path());
1292 const std::filesystem::path leaf(entryName.filename());
1293 if (!leaf.empty()) {
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -07001294 if (entryName == CLOCK_FONT_ZIP_NAME) {
1295 FileMap* map = zip->createEntryFileMap(entry);
1296 if (map) {
1297 animation.clockFont.map = map;
1298 }
1299 continue;
1300 }
1301
Nicolas Geoffray41fffb42020-12-15 10:50:29 +00001302 if (entryName == PROGRESS_FONT_ZIP_NAME) {
1303 FileMap* map = zip->createEntryFileMap(entry);
1304 if (map) {
1305 animation.progressFont.map = map;
1306 }
1307 continue;
1308 }
1309
Rahul Banerjee0c27488a2024-07-03 18:20:18 -07001310 for (size_t partIdx = 0; partIdx < numParts; partIdx++) {
1311 if (path.string() == animation.parts[partIdx].path.c_str()) {
Narayan Kamath4600dd02015-06-16 12:02:57 +01001312 uint16_t method;
Narayan Kamathafd31e02013-12-03 13:16:03 +00001313 // supports only stored png files
Pawan Wagh8efc3c42024-06-03 21:43:13 +00001314 if (zip->getEntryInfo(entry, &method, nullptr, nullptr, nullptr, nullptr,
1315 nullptr, nullptr)) {
Narayan Kamathafd31e02013-12-03 13:16:03 +00001316 if (method == ZipFileRO::kCompressStored) {
Geoffrey Pitschdd214a72016-06-27 17:14:30 -04001317 FileMap* map = zip->createEntryFileMap(entry);
Narayan Kamathafd31e02013-12-03 13:16:03 +00001318 if (map) {
Rahul Banerjee0c27488a2024-07-03 18:20:18 -07001319 Animation::Part& part(animation.parts.editItemAt(partIdx));
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -07001320 if (leaf == "audio.wav") {
1321 // a part may have at most one audio file
Geoffrey Pitschd6d9a1d2016-06-08 00:38:58 -07001322 part.audioData = (uint8_t *)map->getDataPtr();
1323 part.audioLength = map->getDataLength();
Geoffrey Pitschdd214a72016-06-27 17:14:30 -04001324 } else if (leaf == "trim.txt") {
Tomasz Wasilczyk31eb3c892023-08-23 22:12:33 +00001325 part.trimData = String8((char const*)map->getDataPtr(),
Geoffrey Pitschdd214a72016-06-27 17:14:30 -04001326 map->getDataLength());
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -07001327 } else {
1328 Animation::Frame frame;
Tomasz Wasilczyk9dcea572023-08-22 20:39:20 +00001329 frame.name = leaf.c_str();
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -07001330 frame.map = map;
Geoffrey Pitschdd214a72016-06-27 17:14:30 -04001331 frame.trimWidth = animation.width;
1332 frame.trimHeight = animation.height;
1333 frame.trimX = 0;
1334 frame.trimY = 0;
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -07001335 part.frames.add(frame);
1336 }
Mathias Agopiana8826d62009-10-01 03:10:14 -07001337 }
Geoffrey Pitschd6d9a1d2016-06-08 00:38:58 -07001338 } else {
Wei Wang159a4102018-10-23 23:15:58 -07001339 SLOGE("bootanimation.zip is compressed; must be only stored");
Mathias Agopiana8826d62009-10-01 03:10:14 -07001340 }
1341 }
1342 }
1343 }
1344 }
1345 }
1346
Geoffrey Pitschdd214a72016-06-27 17:14:30 -04001347 // If there is trimData present, override the positioning defaults.
1348 for (Animation::Part& part : animation.parts) {
Tomasz Wasilczyk3815d342023-08-10 23:54:44 +00001349 const char* trimDataStr = part.trimData.c_str();
Rahul Banerjee0c27488a2024-07-03 18:20:18 -07001350 const size_t numFramesInPart = part.frames.size();
1351 for (size_t frameIdxInPart = 0; frameIdxInPart < numFramesInPart; frameIdxInPart++) {
Geoffrey Pitschdd214a72016-06-27 17:14:30 -04001352 const char* endl = strstr(trimDataStr, "\n");
1353 // No more trimData for this part.
Yi Kong911ac232019-03-24 01:49:02 -07001354 if (endl == nullptr) {
Geoffrey Pitschdd214a72016-06-27 17:14:30 -04001355 break;
1356 }
1357 String8 line(trimDataStr, endl - trimDataStr);
Tomasz Wasilczyk3815d342023-08-10 23:54:44 +00001358 const char* lineStr = line.c_str();
Geoffrey Pitschdd214a72016-06-27 17:14:30 -04001359 trimDataStr = ++endl;
1360 int width = 0, height = 0, x = 0, y = 0;
1361 if (sscanf(lineStr, "%dx%d+%d+%d", &width, &height, &x, &y) == 4) {
Rahul Banerjee0c27488a2024-07-03 18:20:18 -07001362 Animation::Frame& frame(part.frames.editItemAt(frameIdxInPart));
Geoffrey Pitschdd214a72016-06-27 17:14:30 -04001363 frame.trimWidth = width;
1364 frame.trimHeight = height;
1365 frame.trimX = x;
1366 frame.trimY = y;
1367 } else {
Wei Wang159a4102018-10-23 23:15:58 -07001368 SLOGE("Error parsing trim.txt, line: %s", lineStr);
Geoffrey Pitschdd214a72016-06-27 17:14:30 -04001369 break;
1370 }
1371 }
1372 }
1373
1374 zip->endIteration(cookie);
Narayan Kamathafd31e02013-12-03 13:16:03 +00001375
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001376 return true;
1377}
1378
Marin Shalamanov9070c052020-05-18 19:05:17 +02001379bool BootAnimation::movie() {
Parth Sane7f7b2212023-04-12 17:23:02 +00001380 ATRACE_CALL();
Huihong Luo50a2f362018-08-11 09:27:57 -07001381 if (mAnimation == nullptr) {
1382 mAnimation = loadAnimation(mZipFileName);
1383 }
1384
1385 if (mAnimation == nullptr)
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001386 return false;
1387
Huihong Luo50a2f362018-08-11 09:27:57 -07001388 // mCallbacks->init() may get called recursively,
1389 // this loop is needed to get the same results
1390 for (const Animation::Part& part : mAnimation->parts) {
1391 if (part.animation != nullptr) {
1392 mCallbacks->init(part.animation->parts);
1393 }
1394 }
1395 mCallbacks->init(mAnimation->parts);
1396
Damien Bargiacchi97480862016-03-29 14:55:55 -07001397 bool anyPartHasClock = false;
Huihong Luo50a2f362018-08-11 09:27:57 -07001398 for (size_t i=0; i < mAnimation->parts.size(); i++) {
1399 if(validClock(mAnimation->parts[i])) {
Damien Bargiacchi97480862016-03-29 14:55:55 -07001400 anyPartHasClock = true;
1401 break;
1402 }
1403 }
1404 if (!anyPartHasClock) {
1405 mClockEnabled = false;
Thiébaud Weksteen08de4432021-11-18 13:25:59 +11001406 } else if (!android::base::GetBoolProperty(CLOCK_ENABLED_PROP_NAME, false)) {
1407 mClockEnabled = false;
Damien Bargiacchi97480862016-03-29 14:55:55 -07001408 }
1409
Sai Kiran Korwar27167492015-07-07 20:00:06 +05301410 // Check if npot textures are supported
1411 mUseNpotTextures = false;
1412 String8 gl_extensions;
1413 const char* exts = reinterpret_cast<const char*>(glGetString(GL_EXTENSIONS));
1414 if (!exts) {
1415 glGetError();
1416 } else {
Tomasz Wasilczyk31eb3c892023-08-23 22:12:33 +00001417 gl_extensions = exts;
Sai Kiran Korwar27167492015-07-07 20:00:06 +05301418 if ((gl_extensions.find("GL_ARB_texture_non_power_of_two") != -1) ||
1419 (gl_extensions.find("GL_OES_texture_npot") != -1)) {
1420 mUseNpotTextures = true;
1421 }
1422 }
1423
Damien Bargiacchia704b7d2016-02-16 16:55:49 -08001424 // Blend required to draw time on top of animation frames.
1425 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
Mathias Agopiana8826d62009-10-01 03:10:14 -07001426 glDisable(GL_DITHER);
1427 glDisable(GL_SCISSOR_TEST);
1428 glDisable(GL_BLEND);
Mathias Agopiana8826d62009-10-01 03:10:14 -07001429
Greg Kaiser2ded8002021-07-19 17:03:27 +00001430 glEnable(GL_TEXTURE_2D);
Lucas Dupin8eedc022021-07-29 21:49:07 +00001431 glBindTexture(GL_TEXTURE_2D, 0);
1432 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
1433 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
1434 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
1435 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -07001436 bool clockFontInitialized = false;
Damien Bargiacchia704b7d2016-02-16 16:55:49 -08001437 if (mClockEnabled) {
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -07001438 clockFontInitialized =
Huihong Luo50a2f362018-08-11 09:27:57 -07001439 (initFont(&mAnimation->clockFont, CLOCK_FONT_ASSET) == NO_ERROR);
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -07001440 mClockEnabled = clockFontInitialized;
Damien Bargiacchia704b7d2016-02-16 16:55:49 -08001441 }
1442
Nicolas Geoffray41fffb42020-12-15 10:50:29 +00001443 initFont(&mAnimation->progressFont, PROGRESS_FONT_ASSET);
1444
Damien Bargiacchi97480862016-03-29 14:55:55 -07001445 if (mClockEnabled && !updateIsTimeAccurate()) {
1446 mTimeCheckThread = new TimeCheckThread(this);
1447 mTimeCheckThread->run("BootAnimation::TimeCheckThread", PRIORITY_NORMAL);
1448 }
1449
George Burgess IV0f8e8112021-08-23 17:32:23 -07001450 if (mAnimation->dynamicColoringEnabled) {
Shan Huang7fee3522021-08-18 00:56:08 +00001451 initDynamicColors();
1452 }
1453
Huihong Luo50a2f362018-08-11 09:27:57 -07001454 playAnimation(*mAnimation);
Damien Bargiacchi97480862016-03-29 14:55:55 -07001455
Geoffrey Pitscha9173532016-07-19 14:56:39 -04001456 if (mTimeCheckThread != nullptr) {
Damien Bargiacchi97480862016-03-29 14:55:55 -07001457 mTimeCheckThread->requestExit();
Geoffrey Pitscha9173532016-07-19 14:56:39 -04001458 mTimeCheckThread = nullptr;
1459 }
1460
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -07001461 if (clockFontInitialized) {
Huihong Luo50a2f362018-08-11 09:27:57 -07001462 glDeleteTextures(1, &mAnimation->clockFont.texture.name);
Andriy Naborskyy815e51d2016-03-24 16:43:34 -07001463 }
1464
Huihong Luo50a2f362018-08-11 09:27:57 -07001465 releaseAnimation(mAnimation);
1466 mAnimation = nullptr;
Damien Bargiacchi1a8a2132018-07-24 15:20:26 -07001467
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001468 return false;
1469}
1470
Nicolas Geoffraya1298752021-04-20 15:15:06 +01001471bool BootAnimation::shouldStopPlayingPart(const Animation::Part& part,
1472 const int fadedFramesCount,
1473 const int lastDisplayedProgress) {
Parth Sane7f7b2212023-04-12 17:23:02 +00001474 ATRACE_CALL();
Yegor Malyshev6925e1b2020-09-10 16:42:20 +02001475 // 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 +01001476 return exitPending() && !part.playUntilComplete && fadedFramesCount >= part.framesToFadeCount &&
1477 (lastDisplayedProgress == 0 || lastDisplayedProgress == 100);
Yegor Malyshev6925e1b2020-09-10 16:42:20 +02001478}
1479
Lucas Dupin8eedc022021-07-29 21:49:07 +00001480// Linear mapping from range <a1, a2> to range <b1, b2>
1481float mapLinear(float x, float a1, float a2, float b1, float b2) {
1482 return b1 + ( x - a1 ) * ( b2 - b1 ) / ( a2 - a1 );
1483}
1484
Rahul Banerjee0c27488a2024-07-03 18:20:18 -07001485void BootAnimation::drawTexturedQuad(float xStart, float yStart,
1486 float width, float height,
1487 const Display& display) {
Parth Sane7f7b2212023-04-12 17:23:02 +00001488 ATRACE_CALL();
Lucas Dupin8eedc022021-07-29 21:49:07 +00001489 // Map coordinates from screen space to world space.
Rahul Banerjee0c27488a2024-07-03 18:20:18 -07001490 float x0 = mapLinear(xStart, 0, display.width, -1, 1);
1491 float y0 = mapLinear(yStart, 0, display.height, -1, 1);
1492 float x1 = mapLinear(xStart + width, 0, display.width, -1, 1);
1493 float y1 = mapLinear(yStart + height, 0, display.height, -1, 1);
Lucas Dupin8eedc022021-07-29 21:49:07 +00001494 // Update quad vertex positions.
1495 quadPositions[0] = x0;
1496 quadPositions[1] = y0;
1497 quadPositions[2] = x1;
1498 quadPositions[3] = y0;
1499 quadPositions[4] = x1;
1500 quadPositions[5] = y1;
1501 quadPositions[6] = x1;
1502 quadPositions[7] = y1;
1503 quadPositions[8] = x0;
1504 quadPositions[9] = y1;
1505 quadPositions[10] = x0;
1506 quadPositions[11] = y0;
1507 glDrawArrays(GL_TRIANGLES, 0,
1508 sizeof(quadPositions) / sizeof(quadPositions[0]) / 2);
1509}
1510
Shan Huang7fee3522021-08-18 00:56:08 +00001511void BootAnimation::initDynamicColors() {
Parth Sane7f7b2212023-04-12 17:23:02 +00001512 ATRACE_CALL();
Shan Huang7fee3522021-08-18 00:56:08 +00001513 for (int i = 0; i < DYNAMIC_COLOR_COUNT; i++) {
Lucas Dupincd256842022-03-29 15:46:36 -07001514 const auto syspropName = "persist.bootanim.color" + std::to_string(i + 1);
1515 const auto syspropValue = android::base::GetProperty(syspropName, "");
1516 if (syspropValue != "") {
1517 SLOGI("Loaded dynamic color: %s -> %s", syspropName.c_str(), syspropValue.c_str());
1518 mDynamicColorsApplied = true;
1519 }
1520 parseColorDecimalString(syspropValue,
Shan Huang7fee3522021-08-18 00:56:08 +00001521 mAnimation->endColors[i], mAnimation->startColors[i]);
1522 }
1523 glUseProgram(mImageShader);
Lucas Dupincd256842022-03-29 15:46:36 -07001524 SLOGI("Dynamically coloring boot animation. Sysprops loaded? %i", mDynamicColorsApplied);
Shan Huang7fee3522021-08-18 00:56:08 +00001525 for (int i = 0; i < DYNAMIC_COLOR_COUNT; i++) {
1526 float *startColor = mAnimation->startColors[i];
1527 float *endColor = mAnimation->endColors[i];
Andrew Zengf9f58222022-11-08 11:10:48 -08001528 glUniform3f(glGetUniformLocation(mImageShader,
Shan Huang7fee3522021-08-18 00:56:08 +00001529 (U_START_COLOR_PREFIX + std::to_string(i)).c_str()),
Andrew Zengf9f58222022-11-08 11:10:48 -08001530 startColor[0], startColor[1], startColor[2]);
1531 glUniform3f(glGetUniformLocation(mImageShader,
Shan Huang7fee3522021-08-18 00:56:08 +00001532 (U_END_COLOR_PREFIX + std::to_string(i)).c_str()),
Andrew Zengf9f58222022-11-08 11:10:48 -08001533 endColor[0], endColor[1], endColor[2]);
Shan Huang7fee3522021-08-18 00:56:08 +00001534 }
1535 mImageColorProgressLocation = glGetUniformLocation(mImageShader, U_COLOR_PROGRESS);
1536}
1537
Marin Shalamanov9070c052020-05-18 19:05:17 +02001538bool BootAnimation::playAnimation(const Animation& animation) {
Parth Sane7f7b2212023-04-12 17:23:02 +00001539 ATRACE_CALL();
Rahul Banerjee0c27488a2024-07-03 18:20:18 -07001540 const size_t numParts = animation.parts.size();
Mathias Agopiana8826d62009-10-01 03:10:14 -07001541 nsecs_t frameDuration = s2ns(1) / animation.fps;
Mathias Agopian9f3020d2009-11-06 16:30:18 -08001542
Wei Wang159a4102018-10-23 23:15:58 -07001543 SLOGD("%sAnimationShownTiming start time: %" PRId64 "ms", mShuttingDown ? "Shutdown" : "Boot",
Keun-young Parkd1794cd2017-04-04 12:21:19 -07001544 elapsedRealtime());
Yegor Malyshev6925e1b2020-09-10 16:42:20 +02001545
1546 int fadedFramesCount = 0;
Nicolas Geoffray41fffb42020-12-15 10:50:29 +00001547 int lastDisplayedProgress = 0;
Lucas Dupinbfd18122022-04-25 11:26:08 -07001548 int colorTransitionStart = animation.colorTransitionStart;
1549 int colorTransitionEnd = animation.colorTransitionEnd;
Rahul Banerjee0c27488a2024-07-03 18:20:18 -07001550 for (size_t partIdx = 0; partIdx < numParts; partIdx++) {
1551 const Animation::Part& part(animation.parts[partIdx]);
1552 const size_t numFramesInPart = part.frames.size();
Oleg Lyovin6a4b84d2023-07-01 14:39:59 +03001553 glBindTexture(GL_TEXTURE_2D, 0);
Mathias Agopiana8826d62009-10-01 03:10:14 -07001554
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001555 // Handle animation package
Yi Kong911ac232019-03-24 01:49:02 -07001556 if (part.animation != nullptr) {
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001557 playAnimation(*part.animation);
1558 if (exitPending())
1559 break;
1560 continue; //to next part
1561 }
1562
Yegor Malyshev6925e1b2020-09-10 16:42:20 +02001563 // process the part not only while the count allows but also if already fading
Rahul Banerjee0c27488a2024-07-03 18:20:18 -07001564 for (int frameIdx = 0;
1565 !part.count || frameIdx < part.count || fadedFramesCount > 0;
1566 frameIdx++) {
Nicolas Geoffraya1298752021-04-20 15:15:06 +01001567 if (shouldStopPlayingPart(part, fadedFramesCount, lastDisplayedProgress)) break;
Kevin Hesterd3782b22012-04-26 10:38:55 -07001568
Lucas Dupinbfd18122022-04-25 11:26:08 -07001569 // It's possible that the sysprops were not loaded yet at this boot phase.
1570 // If that's the case, then we should keep trying until they are available.
1571 if (animation.dynamicColoringEnabled && !mDynamicColorsApplied
1572 && (part.useDynamicColoring || part.postDynamicColoring)) {
1573 SLOGD("Trying to load dynamic color sysprops.");
1574 initDynamicColors();
1575 if (mDynamicColorsApplied) {
1576 // Sysprops were loaded. Next step is to adjust the animation if we loaded
1577 // the colors after the animation should have started.
1578 const int transitionLength = colorTransitionEnd - colorTransitionStart;
1579 if (part.postDynamicColoring) {
1580 colorTransitionStart = 0;
Rahul Banerjee0c27488a2024-07-03 18:20:18 -07001581 colorTransitionEnd = fmin(transitionLength, numFramesInPart - 1);
Lucas Dupinbfd18122022-04-25 11:26:08 -07001582 }
1583 }
1584 }
1585
Rahul Banerjee0c27488a2024-07-03 18:20:18 -07001586 mCallbacks->playPart(partIdx, part, frameIdx);
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -07001587
Jesse Hall083b84c2014-09-22 10:51:09 -07001588 glClearColor(
1589 part.backgroundColor[0],
1590 part.backgroundColor[1],
1591 part.backgroundColor[2],
1592 1.0f);
1593
Yoshiaki Maruoka9ef19b82017-10-25 13:55:49 +09001594 ALOGD("Playing files = %s/%s, Requested repeat = %d, playUntilComplete = %s",
Tomasz Wasilczyk3815d342023-08-10 23:54:44 +00001595 animation.fileName.c_str(), part.path.c_str(), part.count,
Yoshiaki Maruoka9ef19b82017-10-25 13:55:49 +09001596 part.playUntilComplete ? "true" : "false");
1597
Nicolas Geoffray41fffb42020-12-15 10:50:29 +00001598 // For the last animation, if we have progress indicator from
1599 // the system, display it.
Rahul Banerjee0c27488a2024-07-03 18:20:18 -07001600 const bool displayProgress = animation.progressEnabled && (partIdx == (numParts - 1)) &&
1601 android::base::GetIntProperty(PROGRESS_PROP_NAME, 0) != 0;
Nicolas Geoffray41fffb42020-12-15 10:50:29 +00001602
Rahul Banerjee0c27488a2024-07-03 18:20:18 -07001603 for (size_t frameIdxInPart = 0; frameIdxInPart < numFramesInPart; frameIdxInPart++) {
Nicolas Geoffraya1298752021-04-20 15:15:06 +01001604 if (shouldStopPlayingPart(part, fadedFramesCount, lastDisplayedProgress)) break;
Yegor Malyshev6925e1b2020-09-10 16:42:20 +02001605
Shan Huang2b31a362021-08-16 16:45:39 +00001606 // Color progress is
Shan Huang488ff742021-08-25 20:10:46 +00001607 // - the animation progress, normalized from
1608 // [colorTransitionStart,colorTransitionEnd] to [0, 1] for the dynamic coloring
1609 // part.
Shan Huang2b31a362021-08-16 16:45:39 +00001610 // - 0 for parts that come before,
1611 // - 1 for parts that come after.
1612 float colorProgress = part.useDynamicColoring
Shan Huang488ff742021-08-25 20:10:46 +00001613 ? fmin(fmax(
Rahul Banerjee0c27488a2024-07-03 18:20:18 -07001614 (static_cast<float>(frameIdxInPart) - colorTransitionStart) /
Lucas Dupinbfd18122022-04-25 11:26:08 -07001615 fmax(colorTransitionEnd - colorTransitionStart, 1.0f), 0.0f), 1.0f)
Shan Huang2b31a362021-08-16 16:45:39 +00001616 : (part.postDynamicColoring ? 1 : 0);
Marin Shalamanov9070c052020-05-18 19:05:17 +02001617 processDisplayEvents();
1618
Rahul Banerjee0c27488a2024-07-03 18:20:18 -07001619 const Animation::Frame& frame(part.frames[frameIdxInPart]);
Mathias Agopiandb7dd2a2012-05-12 15:08:21 -07001620 nsecs_t lastFrame = systemTime();
Mathias Agopiana8826d62009-10-01 03:10:14 -07001621
Rahul Banerjee0c27488a2024-07-03 18:20:18 -07001622 if (frameIdx > 0) {
Mathias Agopiana8826d62009-10-01 03:10:14 -07001623 glBindTexture(GL_TEXTURE_2D, frame.tid);
1624 } else {
Oleg Lyovin6a4b84d2023-07-01 14:39:59 +03001625 if (part.count != 1) {
1626 glGenTextures(1, &frame.tid);
1627 glBindTexture(GL_TEXTURE_2D, frame.tid);
1628 }
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -07001629 int w, h;
Shan Huang2b31a362021-08-16 16:45:39 +00001630 // Set decoding option to alpha unpremultiplied so that the R, G, B channels
1631 // of transparent pixels are preserved.
1632 initTexture(frame.map, &w, &h, false /* don't premultiply alpha */);
Mathias Agopiana8826d62009-10-01 03:10:14 -07001633 }
1634
Lucas Dupin8eedc022021-07-29 21:49:07 +00001635 float fade = 0;
Yegor Malyshev6925e1b2020-09-10 16:42:20 +02001636 // if the part hasn't been stopped yet then continue fading if necessary
1637 if (exitPending() && part.hasFadingPhase()) {
Lucas Dupin8eedc022021-07-29 21:49:07 +00001638 fade = static_cast<float>(++fadedFramesCount) / part.framesToFadeCount;
Yegor Malyshev6925e1b2020-09-10 16:42:20 +02001639 if (fadedFramesCount >= part.framesToFadeCount) {
1640 fadedFramesCount = MAX_FADED_FRAMES_COUNT; // no more fading
1641 }
1642 }
1643
Rahul Banerjee0c27488a2024-07-03 18:20:18 -07001644 // Draw the current frame's texture on every physical display that is enabled.
1645 for (const auto& display : mDisplays) {
1646 eglMakeCurrent(mEgl, display.eglSurface, display.eglSurface, mEglContext);
Yegor Malyshev6925e1b2020-09-10 16:42:20 +02001647
Rahul Banerjee0c27488a2024-07-03 18:20:18 -07001648 const double ratioW =
1649 static_cast<double>(display.width) / display.initWidth;
1650 const double ratioH =
1651 static_cast<double>(display.height) / display.initHeight;
1652 const int animationX = (display.width - animation.width * ratioW) / 2;
1653 const int animationY = (display.height - animation.height * ratioH) / 2;
1654
1655 const int trimWidth = frame.trimWidth * ratioW;
1656 const int trimHeight = frame.trimHeight * ratioH;
1657 const int trimX = frame.trimX * ratioW;
1658 const int trimY = frame.trimY * ratioH;
1659 const int xc = animationX + trimX;
1660 const int yc = animationY + trimY;
1661 projectSceneToWindow(display);
1662 handleViewport(frameDuration, display);
1663 glClear(GL_COLOR_BUFFER_BIT);
1664 // specify the y center as ceiling((height - frame.trimHeight) / 2)
1665 // which is equivalent to height - (yc + frame.trimHeight)
1666 const int frameDrawY = display.height - (yc + trimHeight);
1667
1668 glUseProgram(mImageShader);
1669 glUniform1i(mImageTextureLocation, 0);
1670 glUniform1f(mImageFadeLocation, fade);
1671 if (animation.dynamicColoringEnabled) {
1672 glUniform1f(mImageColorProgressLocation, colorProgress);
Nicolas Geoffray41fffb42020-12-15 10:50:29 +00001673 }
Rahul Banerjee0c27488a2024-07-03 18:20:18 -07001674 glEnable(GL_BLEND);
1675 drawTexturedQuad(xc, frameDrawY, trimWidth, trimHeight, display);
1676 glDisable(GL_BLEND);
1677
1678 if (mClockEnabled && mTimeIsAccurate && validClock(part)) {
1679 drawClock(animation.clockFont, part.clockPosX, part.clockPosY, display);
1680 }
1681
1682 if (displayProgress) {
1683 int newProgress = android::base::GetIntProperty(PROGRESS_PROP_NAME, 0);
1684 // In case the new progress jumped suddenly, still show an
1685 // increment of 1.
1686 if (lastDisplayedProgress != 100) {
1687 // Artificially sleep 1/10th a second to slow down the animation.
1688 usleep(100000);
1689 if (lastDisplayedProgress < newProgress) {
1690 lastDisplayedProgress++;
1691 }
1692 }
1693 // Put the progress percentage right below the animation.
1694 int posY = animation.height / 3;
1695 int posX = TEXT_CENTER_VALUE;
1696 drawProgress(lastDisplayedProgress,
1697 animation.progressFont, posX, posY, display);
1698 }
1699
1700 eglSwapBuffers(mEgl, display.eglSurface);
Nicolas Geoffray41fffb42020-12-15 10:50:29 +00001701 }
1702
Mathias Agopiana8826d62009-10-01 03:10:14 -07001703
1704 nsecs_t now = systemTime();
1705 nsecs_t delay = frameDuration - (now - lastFrame);
Wei Wang159a4102018-10-23 23:15:58 -07001706 //SLOGD("%lld, %lld", ns2ms(now - lastFrame), ns2ms(delay));
Mathias Agopiana8826d62009-10-01 03:10:14 -07001707 lastFrame = now;
Mathias Agopiandb7dd2a2012-05-12 15:08:21 -07001708
1709 if (delay > 0) {
1710 struct timespec spec;
1711 spec.tv_sec = (now + delay) / 1000000000;
1712 spec.tv_nsec = (now + delay) % 1000000000;
1713 int err;
1714 do {
Yi Kong911ac232019-03-24 01:49:02 -07001715 err = clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &spec, nullptr);
Keith Mok0f2e6682021-11-15 05:50:32 +00001716 } while (err == EINTR);
Mathias Agopiandb7dd2a2012-05-12 15:08:21 -07001717 }
Kevin Hesterd3782b22012-04-26 10:38:55 -07001718
1719 checkExit();
Mathias Agopiana8826d62009-10-01 03:10:14 -07001720 }
Kevin Hesterd3782b22012-04-26 10:38:55 -07001721
niuwenchao9cd6ac82022-12-23 17:34:22 +08001722 int pauseDuration = part.pause * ns2us(frameDuration);
1723 while(pauseDuration > 0 && !exitPending()){
1724 if (pauseDuration > MAX_CHECK_EXIT_INTERVAL_US) {
1725 usleep(MAX_CHECK_EXIT_INTERVAL_US);
1726 pauseDuration -= MAX_CHECK_EXIT_INTERVAL_US;
1727 } else {
1728 usleep(pauseDuration);
1729 break;
1730 }
1731 checkExit();
1732 }
Kevin Hesterd3782b22012-04-26 10:38:55 -07001733
Yegor Malyshev6925e1b2020-09-10 16:42:20 +02001734 if (exitPending() && !part.count && mCurrentInset >= mTargetInset &&
1735 !part.hasFadingPhase()) {
Nicolas Geoffraya1298752021-04-20 15:15:06 +01001736 if (lastDisplayedProgress != 0 && lastDisplayedProgress != 100) {
1737 android::base::SetProperty(PROGRESS_PROP_NAME, "100");
1738 continue;
1739 }
Yegor Malyshev6925e1b2020-09-10 16:42:20 +02001740 break; // exit the infinite non-fading part when it has been played at least once
1741 }
Mathias Agopiana8826d62009-10-01 03:10:14 -07001742 }
Geoffrey Pitsch2fb30fb2016-07-06 16:16:20 -04001743 }
1744
1745 // Free textures created for looping parts now that the animation is done.
1746 for (const Animation::Part& part : animation.parts) {
Mathias Agopiana8826d62009-10-01 03:10:14 -07001747 if (part.count != 1) {
Rahul Banerjee0c27488a2024-07-03 18:20:18 -07001748 for (const auto& frame : part.frames) {
Mathias Agopiana8826d62009-10-01 03:10:14 -07001749 glDeleteTextures(1, &frame.tid);
1750 }
1751 }
1752 }
Geoffrey Pitschd6d9a1d2016-06-08 00:38:58 -07001753
Yoshiaki Maruoka9ef19b82017-10-25 13:55:49 +09001754 ALOGD("%sAnimationShownTiming End time: %" PRId64 "ms", mShuttingDown ? "Shutdown" : "Boot",
1755 elapsedRealtime());
1756
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001757 return true;
Mathias Agopiana8826d62009-10-01 03:10:14 -07001758}
1759
Marin Shalamanov9070c052020-05-18 19:05:17 +02001760void BootAnimation::processDisplayEvents() {
Parth Sane7f7b2212023-04-12 17:23:02 +00001761 ATRACE_CALL();
Marin Shalamanov9070c052020-05-18 19:05:17 +02001762 // This will poll mDisplayEventReceiver and if there are new events it'll call
1763 // displayEventCallback synchronously.
1764 mLooper->pollOnce(0);
1765}
1766
Rahul Banerjee0c27488a2024-07-03 18:20:18 -07001767void BootAnimation::handleViewport(nsecs_t timestep, const Display& display) {
Parth Sane7f7b2212023-04-12 17:23:02 +00001768 ATRACE_CALL();
Rahul Banerjee0c27488a2024-07-03 18:20:18 -07001769 if (mShuttingDown || !display.surfaceControl || mTargetInset == 0) {
Adrian Roos9ee5dff2018-08-22 20:19:49 +02001770 return;
1771 }
1772 if (mTargetInset < 0) {
1773 // Poll the amount for the top display inset. This will return -1 until persistent properties
1774 // have been loaded.
Rahul Banerjee0c27488a2024-07-03 18:20:18 -07001775 mTargetInset =
1776 android::base::GetIntProperty("persist.sys.displayinset.top", -1 /* default */,
1777 -1 /* min */, display.height / 2 /* max */);
Adrian Roos9ee5dff2018-08-22 20:19:49 +02001778 }
1779 if (mTargetInset <= 0) {
1780 return;
1781 }
1782
1783 if (mCurrentInset < mTargetInset) {
1784 // After the device boots, the inset will effectively be cropped away. We animate this here.
1785 float fraction = static_cast<float>(mCurrentInset) / mTargetInset;
1786 int interpolatedInset = (cosf((fraction + 1) * M_PI) / 2.0f + 0.5f) * mTargetInset;
1787
1788 SurfaceComposerClient::Transaction()
Rahul Banerjee0c27488a2024-07-03 18:20:18 -07001789 .setCrop(display.surfaceControl,
1790 Rect(0, interpolatedInset, display.width, display.height))
Adrian Roos9ee5dff2018-08-22 20:19:49 +02001791 .apply();
1792 } else {
1793 // At the end of the animation, we switch to the viewport that DisplayManager will apply
1794 // later. This changes the coordinate system, and means we must move the surface up by
1795 // the inset amount.
Rahul Banerjee0c27488a2024-07-03 18:20:18 -07001796 Rect layerStackRect(0, 0,
1797 display.width,
1798 display.height - mTargetInset);
1799 Rect displayRect(0, mTargetInset,
1800 display.width,
1801 display.height);
Adrian Roos9ee5dff2018-08-22 20:19:49 +02001802 SurfaceComposerClient::Transaction t;
Rahul Banerjee0c27488a2024-07-03 18:20:18 -07001803 t.setPosition(display.surfaceControl, 0, -mTargetInset)
1804 .setCrop(display.surfaceControl,
1805 Rect(0, mTargetInset,
1806 display.width,
1807 display.height));
1808 t.setDisplayProjection(display.displayToken,
1809 ui::ROTATION_0, layerStackRect, displayRect);
Adrian Roos9ee5dff2018-08-22 20:19:49 +02001810 t.apply();
1811
1812 mTargetInset = mCurrentInset = 0;
1813 }
1814
1815 int delta = timestep * mTargetInset / ms2ns(200);
1816 mCurrentInset += delta;
1817}
1818
Marin Shalamanov9070c052020-05-18 19:05:17 +02001819void BootAnimation::releaseAnimation(Animation* animation) const {
Parth Sane7f7b2212023-04-12 17:23:02 +00001820 ATRACE_CALL();
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001821 for (Vector<Animation::Part>::iterator it = animation->parts.begin(),
1822 e = animation->parts.end(); it != e; ++it) {
1823 if (it->animation)
1824 releaseAnimation(it->animation);
1825 }
1826 if (animation->zip)
1827 delete animation->zip;
1828 delete animation;
1829}
1830
Marin Shalamanov9070c052020-05-18 19:05:17 +02001831BootAnimation::Animation* BootAnimation::loadAnimation(const String8& fn) {
Parth Sane7f7b2212023-04-12 17:23:02 +00001832 ATRACE_CALL();
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001833 if (mLoadedFiles.indexOf(fn) >= 0) {
Wei Wang159a4102018-10-23 23:15:58 -07001834 SLOGE("File \"%s\" is already loaded. Cyclic ref is not allowed",
Tomasz Wasilczyk3815d342023-08-10 23:54:44 +00001835 fn.c_str());
Yi Kong911ac232019-03-24 01:49:02 -07001836 return nullptr;
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001837 }
Tomasz Wasilczyk835dfe52023-08-17 16:27:22 +00001838 ZipFileRO *zip = ZipFileRO::open(fn.c_str());
Yi Kong911ac232019-03-24 01:49:02 -07001839 if (zip == nullptr) {
Wei Wang159a4102018-10-23 23:15:58 -07001840 SLOGE("Failed to open animation zip \"%s\": %s",
Tomasz Wasilczyk3815d342023-08-10 23:54:44 +00001841 fn.c_str(), strerror(errno));
Yi Kong911ac232019-03-24 01:49:02 -07001842 return nullptr;
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001843 }
1844
Tomasz Wasilczyk3815d342023-08-10 23:54:44 +00001845 ALOGD("%s is loaded successfully", fn.c_str());
Yoshiaki Maruoka9ef19b82017-10-25 13:55:49 +09001846
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001847 Animation *animation = new Animation;
1848 animation->fileName = fn;
1849 animation->zip = zip;
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -07001850 animation->clockFont.map = nullptr;
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001851 mLoadedFiles.add(animation->fileName);
1852
1853 parseAnimationDesc(*animation);
Geoffrey Pitscha91a2d72016-07-12 14:46:19 -04001854 if (!preloadZip(*animation)) {
Greg Kaiser7426ec82019-09-11 14:34:27 -07001855 releaseAnimation(animation);
Yi Kong911ac232019-03-24 01:49:02 -07001856 return nullptr;
Geoffrey Pitscha91a2d72016-07-12 14:46:19 -04001857 }
1858
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001859 mLoadedFiles.remove(fn);
1860 return animation;
1861}
Damien Bargiacchi97480862016-03-29 14:55:55 -07001862
1863bool BootAnimation::updateIsTimeAccurate() {
Parth Sane7f7b2212023-04-12 17:23:02 +00001864 ATRACE_CALL();
Damien Bargiacchi97480862016-03-29 14:55:55 -07001865 static constexpr long long MAX_TIME_IN_PAST = 60000LL * 60LL * 24LL * 30LL; // 30 days
1866 static constexpr long long MAX_TIME_IN_FUTURE = 60000LL * 90LL; // 90 minutes
1867
1868 if (mTimeIsAccurate) {
1869 return true;
1870 }
Keun-young Parkb5938422017-03-23 13:46:24 -07001871 if (mShuttingDown) return true;
Damien Bargiacchi97480862016-03-29 14:55:55 -07001872 struct stat statResult;
Damien Bargiacchi9071db12016-10-28 17:38:22 -07001873
1874 if(stat(TIME_FORMAT_12_HOUR_FLAG_FILE_PATH, &statResult) == 0) {
1875 mTimeFormat12Hour = true;
1876 }
1877
Damien Bargiacchi97480862016-03-29 14:55:55 -07001878 if(stat(ACCURATE_TIME_FLAG_FILE_PATH, &statResult) == 0) {
1879 mTimeIsAccurate = true;
1880 return true;
1881 }
1882
1883 FILE* file = fopen(LAST_TIME_CHANGED_FILE_PATH, "r");
Yi Kong911ac232019-03-24 01:49:02 -07001884 if (file != nullptr) {
Damien Bargiacchi97480862016-03-29 14:55:55 -07001885 long long lastChangedTime = 0;
1886 fscanf(file, "%lld", &lastChangedTime);
1887 fclose(file);
1888 if (lastChangedTime > 0) {
1889 struct timespec now;
1890 clock_gettime(CLOCK_REALTIME, &now);
1891 // Match the Java timestamp format
1892 long long rtcNow = (now.tv_sec * 1000LL) + (now.tv_nsec / 1000000LL);
Damien Bargiacchi96762812016-07-12 15:53:40 -07001893 if (ACCURATE_TIME_EPOCH < rtcNow
1894 && lastChangedTime > (rtcNow - MAX_TIME_IN_PAST)
1895 && lastChangedTime < (rtcNow + MAX_TIME_IN_FUTURE)) {
Damien Bargiacchi97480862016-03-29 14:55:55 -07001896 mTimeIsAccurate = true;
1897 }
1898 }
1899 }
1900
1901 return mTimeIsAccurate;
1902}
1903
1904BootAnimation::TimeCheckThread::TimeCheckThread(BootAnimation* bootAnimation) : Thread(false),
Josh Yangf95b2002021-12-23 14:32:28 -08001905 mInotifyFd(-1), mBootAnimWd(-1), mTimeWd(-1), mBootAnimation(bootAnimation) {}
Damien Bargiacchi97480862016-03-29 14:55:55 -07001906
1907BootAnimation::TimeCheckThread::~TimeCheckThread() {
Parth Sane7f7b2212023-04-12 17:23:02 +00001908 ATRACE_CALL();
Damien Bargiacchi97480862016-03-29 14:55:55 -07001909 // mInotifyFd may be -1 but that's ok since we're not at risk of attempting to close a valid FD.
1910 close(mInotifyFd);
1911}
1912
1913bool BootAnimation::TimeCheckThread::threadLoop() {
Parth Sane7f7b2212023-04-12 17:23:02 +00001914 ATRACE_CALL();
Damien Bargiacchi97480862016-03-29 14:55:55 -07001915 bool shouldLoop = doThreadLoop() && !mBootAnimation->mTimeIsAccurate
1916 && mBootAnimation->mClockEnabled;
1917 if (!shouldLoop) {
1918 close(mInotifyFd);
1919 mInotifyFd = -1;
1920 }
1921 return shouldLoop;
1922}
1923
1924bool BootAnimation::TimeCheckThread::doThreadLoop() {
Parth Sane7f7b2212023-04-12 17:23:02 +00001925 ATRACE_CALL();
Damien Bargiacchi97480862016-03-29 14:55:55 -07001926 static constexpr int BUFF_LEN (10 * (sizeof(struct inotify_event) + NAME_MAX + 1));
1927
1928 // Poll instead of doing a blocking read so the Thread can exit if requested.
1929 struct pollfd pfd = { mInotifyFd, POLLIN, 0 };
1930 ssize_t pollResult = poll(&pfd, 1, 1000);
1931
1932 if (pollResult == 0) {
1933 return true;
1934 } else if (pollResult < 0) {
Wei Wang159a4102018-10-23 23:15:58 -07001935 SLOGE("Could not poll inotify events");
Damien Bargiacchi97480862016-03-29 14:55:55 -07001936 return false;
1937 }
1938
1939 char buff[BUFF_LEN] __attribute__ ((aligned(__alignof__(struct inotify_event))));;
1940 ssize_t length = read(mInotifyFd, buff, BUFF_LEN);
1941 if (length == 0) {
1942 return true;
1943 } else if (length < 0) {
Wei Wang159a4102018-10-23 23:15:58 -07001944 SLOGE("Could not read inotify events");
Damien Bargiacchi97480862016-03-29 14:55:55 -07001945 return false;
1946 }
1947
1948 const struct inotify_event *event;
1949 for (char* ptr = buff; ptr < buff + length; ptr += sizeof(struct inotify_event) + event->len) {
1950 event = (const struct inotify_event *) ptr;
Josh Yangf95b2002021-12-23 14:32:28 -08001951 if (event->wd == mBootAnimWd && strcmp(BOOTANIM_TIME_DIR_NAME, event->name) == 0) {
Damien Bargiacchi97480862016-03-29 14:55:55 -07001952 addTimeDirWatch();
1953 } else if (event->wd == mTimeWd && (strcmp(LAST_TIME_CHANGED_FILE_NAME, event->name) == 0
1954 || strcmp(ACCURATE_TIME_FLAG_FILE_NAME, event->name) == 0)) {
1955 return !mBootAnimation->updateIsTimeAccurate();
1956 }
1957 }
1958
1959 return true;
1960}
1961
1962void BootAnimation::TimeCheckThread::addTimeDirWatch() {
Parth Sane7f7b2212023-04-12 17:23:02 +00001963 ATRACE_CALL();
Josh Yangf95b2002021-12-23 14:32:28 -08001964 mTimeWd = inotify_add_watch(mInotifyFd, BOOTANIM_TIME_DIR_PATH,
Damien Bargiacchi97480862016-03-29 14:55:55 -07001965 IN_CLOSE_WRITE | IN_MOVED_TO | IN_ATTRIB);
1966 if (mTimeWd > 0) {
1967 // No need to watch for the time directory to be created if it already exists
Josh Yangf95b2002021-12-23 14:32:28 -08001968 inotify_rm_watch(mInotifyFd, mBootAnimWd);
1969 mBootAnimWd = -1;
Damien Bargiacchi97480862016-03-29 14:55:55 -07001970 }
1971}
1972
1973status_t BootAnimation::TimeCheckThread::readyToRun() {
Parth Sane7f7b2212023-04-12 17:23:02 +00001974 ATRACE_CALL();
Damien Bargiacchi97480862016-03-29 14:55:55 -07001975 mInotifyFd = inotify_init();
1976 if (mInotifyFd < 0) {
Wei Wang159a4102018-10-23 23:15:58 -07001977 SLOGE("Could not initialize inotify fd");
Damien Bargiacchi97480862016-03-29 14:55:55 -07001978 return NO_INIT;
1979 }
1980
Josh Yangf95b2002021-12-23 14:32:28 -08001981 mBootAnimWd = inotify_add_watch(mInotifyFd, BOOTANIM_DATA_DIR_PATH, IN_CREATE | IN_ATTRIB);
1982 if (mBootAnimWd < 0) {
Damien Bargiacchi97480862016-03-29 14:55:55 -07001983 close(mInotifyFd);
1984 mInotifyFd = -1;
Josh Yangf95b2002021-12-23 14:32:28 -08001985 SLOGE("Could not add watch for %s: %s", BOOTANIM_DATA_DIR_PATH, strerror(errno));
Damien Bargiacchi97480862016-03-29 14:55:55 -07001986 return NO_INIT;
1987 }
1988
1989 addTimeDirWatch();
1990
1991 if (mBootAnimation->updateIsTimeAccurate()) {
1992 close(mInotifyFd);
1993 mInotifyFd = -1;
1994 return ALREADY_EXISTS;
1995 }
1996
1997 return NO_ERROR;
1998}
1999
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002000// ---------------------------------------------------------------------------
2001
Marin Shalamanov9070c052020-05-18 19:05:17 +02002002} // namespace android