blob: c2f6e3072507fcdddc950bed3302b88e2657923e [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -070017#define LOG_NDEBUG 0
Mathias Agopianbc726112009-09-23 15:44:05 -070018#define LOG_TAG "BootAnimation"
Parth Sane7f7b2212023-04-12 17:23:02 +000019#define ATRACE_TAG ATRACE_TAG_GRAPHICS
Mathias Agopianbc726112009-09-23 15:44:05 -070020
Tomasz Wasilczyk9dcea572023-08-22 20:39:20 +000021#include <filesystem>
Nikita Ioffe06c986e2020-01-27 17:16:03 +000022#include <vector>
23
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080024#include <stdint.h>
Wei Wanga90c54c2017-02-02 11:35:21 -080025#include <inttypes.h>
Damien Bargiacchi97480862016-03-29 14:55:55 -070026#include <sys/inotify.h>
27#include <sys/poll.h>
28#include <sys/stat.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080029#include <sys/types.h>
30#include <math.h>
31#include <fcntl.h>
32#include <utils/misc.h>
Parth Sane7f7b2212023-04-12 17:23:02 +000033#include <utils/Trace.h>
Mathias Agopianb4d5a722009-09-23 17:05:19 -070034#include <signal.h>
Elliott Hughesbb94f312014-10-21 10:41:33 -070035#include <time.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080036
Steven Morelandfb7952f2018-02-23 14:58:50 -080037#include <cutils/atomic.h>
Jason parksbd9a08d2011-01-31 15:04:34 -060038#include <cutils/properties.h>
39
Derek Sollenberger22d0d4c72020-04-08 10:53:46 -040040#include <android/imagedecoder.h>
Mathias Agopianb13b9bd2012-02-17 18:27:36 -080041#include <androidfw/AssetManager.h>
Mathias Agopianac31a3b2009-05-21 19:59:24 -070042#include <binder/IPCThreadState.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080043#include <utils/Errors.h>
44#include <utils/Log.h>
Wei Wanga90c54c2017-02-02 11:35:21 -080045#include <utils/SystemClock.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080046
Keun-young Parkb5938422017-03-23 13:46:24 -070047#include <android-base/properties.h>
48
Marin Shalamanovcb783ca2021-01-29 21:26:07 +010049#include <ui/DisplayMode.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080050#include <ui/PixelFormat.h>
51#include <ui/Rect.h>
52#include <ui/Region.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080053
Jeff Brown0b722fe2012-08-24 22:40:14 -070054#include <gui/ISurfaceComposer.h>
Marin Shalamanov9070c052020-05-18 19:05:17 +020055#include <gui/DisplayEventReceiver.h>
Mathias Agopian8335f1c2012-02-25 18:48:35 -080056#include <gui/Surface.h>
57#include <gui/SurfaceComposerClient.h>
Lucas Dupin8eedc022021-07-29 21:49:07 +000058#include <GLES2/gl2.h>
59#include <GLES2/gl2ext.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080060#include <EGL/eglext.h>
61
62#include "BootAnimation.h"
63
Kyeongkab.Nam35a8a112019-07-17 16:41:48 +090064#define ANIM_PATH_MAX 255
65#define STR(x) #x
66#define STRTO(x) STR(x)
67
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080068namespace android {
69
Marin Shalamanovcb783ca2021-01-29 21:26:07 +010070using ui::DisplayMode;
71
Damien Bargiacchi97480862016-03-29 14:55:55 -070072static const char OEM_BOOTANIMATION_FILE[] = "/oem/media/bootanimation.zip";
jorgetmbd89e9b2024-09-09 19:42:26 +000073static const char PRODUCT_BOOTANIMATION_DIR[] = "/product/media/";
74static const char PRODUCT_BOOTANIMATION_DARK_FILE[] = "bootanimation-dark.zip";
75static const char PRODUCT_BOOTANIMATION_FILE[] = "bootanimation.zip";
Damien Bargiacchi97480862016-03-29 14:55:55 -070076static const char SYSTEM_BOOTANIMATION_FILE[] = "/system/media/bootanimation.zip";
Anders Fridlunde0b4d232018-11-06 14:23:25 +010077static const char APEX_BOOTANIMATION_FILE[] = "/apex/com.android.bootanimation/etc/bootanimation.zip";
Keun-young Parkb5938422017-03-23 13:46:24 -070078static const char OEM_SHUTDOWNANIMATION_FILE[] = "/oem/media/shutdownanimation.zip";
Jaekyun Seokc521bb32018-01-30 11:52:47 +090079static const char PRODUCT_SHUTDOWNANIMATION_FILE[] = "/product/media/shutdownanimation.zip";
Keun-young Parkb5938422017-03-23 13:46:24 -070080static const char SYSTEM_SHUTDOWNANIMATION_FILE[] = "/system/media/shutdownanimation.zip";
81
Nikita Ioffe06c986e2020-01-27 17:16:03 +000082static constexpr const char* PRODUCT_USERSPACE_REBOOT_ANIMATION_FILE = "/product/media/userspace-reboot.zip";
83static constexpr const char* OEM_USERSPACE_REBOOT_ANIMATION_FILE = "/oem/media/userspace-reboot.zip";
84static constexpr const char* SYSTEM_USERSPACE_REBOOT_ANIMATION_FILE = "/system/media/userspace-reboot.zip";
85
Lee George Thomas7a2548b2023-07-25 14:47:39 -070086static const char BOOTANIM_DATA_DIR_PATH[] = "/data/misc/bootanim";
Josh Yangf95b2002021-12-23 14:32:28 -080087static const char BOOTANIM_TIME_DIR_NAME[] = "time";
Lee George Thomas7a2548b2023-07-25 14:47:39 -070088static const char BOOTANIM_TIME_DIR_PATH[] = "/data/misc/bootanim/time";
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -070089static const char CLOCK_FONT_ASSET[] = "images/clock_font.png";
90static const char CLOCK_FONT_ZIP_NAME[] = "clock_font.png";
Nicolas Geoffray41fffb42020-12-15 10:50:29 +000091static const char PROGRESS_FONT_ASSET[] = "images/progress_font.png";
92static const char PROGRESS_FONT_ZIP_NAME[] = "progress_font.png";
Damien Bargiacchi97480862016-03-29 14:55:55 -070093static const char LAST_TIME_CHANGED_FILE_NAME[] = "last_time_change";
Lee George Thomas7a2548b2023-07-25 14:47:39 -070094static const char LAST_TIME_CHANGED_FILE_PATH[] = "/data/misc/bootanim/time/last_time_change";
Damien Bargiacchi97480862016-03-29 14:55:55 -070095static const char ACCURATE_TIME_FLAG_FILE_NAME[] = "time_is_accurate";
Lee George Thomas7a2548b2023-07-25 14:47:39 -070096static const char ACCURATE_TIME_FLAG_FILE_PATH[] = "/data/misc/bootanim/time/time_is_accurate";
97static const char TIME_FORMAT_12_HOUR_FLAG_FILE_PATH[] = "/data/misc/bootanim/time/time_format_12_hour";
Damien Bargiacchi96762812016-07-12 15:53:40 -070098// Java timestamp format. Don't show the clock if the date is before 2000-01-01 00:00:00.
99static const long long ACCURATE_TIME_EPOCH = 946684800000;
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700100static constexpr char FONT_BEGIN_CHAR = ' ';
101static constexpr char FONT_END_CHAR = '~' + 1;
102static constexpr size_t FONT_NUM_CHARS = FONT_END_CHAR - FONT_BEGIN_CHAR + 1;
103static constexpr size_t FONT_NUM_COLS = 16;
104static constexpr size_t FONT_NUM_ROWS = FONT_NUM_CHARS / FONT_NUM_COLS;
105static const int TEXT_CENTER_VALUE = INT_MAX;
106static const int TEXT_MISSING_VALUE = INT_MIN;
Damien Bargiacchi97480862016-03-29 14:55:55 -0700107static const char EXIT_PROP_NAME[] = "service.bootanim.exit";
Nicolas Geoffray41fffb42020-12-15 10:50:29 +0000108static const char PROGRESS_PROP_NAME[] = "service.bootanim.progress";
Huihong Luod4a217e2020-01-16 15:56:33 -0800109static const char DISPLAYS_PROP_NAME[] = "persist.service.bootanim.displays";
Thiébaud Weksteen08de4432021-11-18 13:25:59 +1100110static const char CLOCK_ENABLED_PROP_NAME[] = "persist.sys.bootanim.clock.enabled";
Kyeongkab.Nam35a8a112019-07-17 16:41:48 +0900111static const int ANIM_ENTRY_NAME_MAX = ANIM_PATH_MAX + 1;
niuwenchao9cd6ac82022-12-23 17:34:22 +0800112static const int MAX_CHECK_EXIT_INTERVAL_US = 50000;
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700113static constexpr size_t TEXT_POS_LEN_MAX = 16;
Shan Huang2b31a362021-08-16 16:45:39 +0000114static const int DYNAMIC_COLOR_COUNT = 4;
Lucas Dupin8eedc022021-07-29 21:49:07 +0000115static const char U_TEXTURE[] = "uTexture";
116static const char U_FADE[] = "uFade";
117static const char U_CROP_AREA[] = "uCropArea";
Shan Huang2b31a362021-08-16 16:45:39 +0000118static const char U_START_COLOR_PREFIX[] = "uStartColor";
119static const char U_END_COLOR_PREFIX[] = "uEndColor";
120static const char U_COLOR_PROGRESS[] = "uColorProgress";
Lucas Dupin8eedc022021-07-29 21:49:07 +0000121static const char A_UV[] = "aUv";
122static const char A_POSITION[] = "aPosition";
123static const char VERTEX_SHADER_SOURCE[] = R"(
124 precision mediump float;
125 attribute vec4 aPosition;
126 attribute highp vec2 aUv;
127 varying highp vec2 vUv;
128 void main() {
129 gl_Position = aPosition;
130 vUv = aUv;
131 })";
Shan Huang2b31a362021-08-16 16:45:39 +0000132static const char IMAGE_FRAG_DYNAMIC_COLORING_SHADER_SOURCE[] = R"(
133 precision mediump float;
Shan Huang81846ba2021-09-07 19:07:47 +0000134 const float cWhiteMaskThreshold = 0.05;
Shan Huang2b31a362021-08-16 16:45:39 +0000135 uniform sampler2D uTexture;
136 uniform float uFade;
137 uniform float uColorProgress;
Andrew Zengf9f58222022-11-08 11:10:48 -0800138 uniform vec3 uStartColor0;
139 uniform vec3 uStartColor1;
140 uniform vec3 uStartColor2;
141 uniform vec3 uStartColor3;
142 uniform vec3 uEndColor0;
143 uniform vec3 uEndColor1;
144 uniform vec3 uEndColor2;
145 uniform vec3 uEndColor3;
Shan Huang2b31a362021-08-16 16:45:39 +0000146 varying highp vec2 vUv;
147 void main() {
148 vec4 mask = texture2D(uTexture, vUv);
Shan Huang488ff742021-08-25 20:10:46 +0000149 float r = mask.r;
150 float g = mask.g;
151 float b = mask.b;
152 float a = mask.a;
153 // If all channels have values, render pixel as a shade of white.
154 float useWhiteMask = step(cWhiteMaskThreshold, r)
155 * step(cWhiteMaskThreshold, g)
156 * step(cWhiteMaskThreshold, b)
157 * step(cWhiteMaskThreshold, a);
Andrew Zengf9f58222022-11-08 11:10:48 -0800158 vec3 color = r * mix(uStartColor0, uEndColor0, uColorProgress)
Shan Huang488ff742021-08-25 20:10:46 +0000159 + g * mix(uStartColor1, uEndColor1, uColorProgress)
160 + b * mix(uStartColor2, uEndColor2, uColorProgress)
161 + a * mix(uStartColor3, uEndColor3, uColorProgress);
Andrew Zengf9f58222022-11-08 11:10:48 -0800162 color = mix(color, vec3((r + g + b + a) * 0.25), useWhiteMask);
163 gl_FragColor = vec4(color.x, color.y, color.z, (1.0 - uFade));
Shan Huang2b31a362021-08-16 16:45:39 +0000164 })";
Lucas Dupin8eedc022021-07-29 21:49:07 +0000165static const char IMAGE_FRAG_SHADER_SOURCE[] = R"(
166 precision mediump float;
167 uniform sampler2D uTexture;
168 uniform float uFade;
169 varying highp vec2 vUv;
170 void main() {
171 vec4 color = texture2D(uTexture, vUv);
Shan Huang2b31a362021-08-16 16:45:39 +0000172 gl_FragColor = vec4(color.x, color.y, color.z, (1.0 - uFade)) * color.a;
Lucas Dupin8eedc022021-07-29 21:49:07 +0000173 })";
174static const char TEXT_FRAG_SHADER_SOURCE[] = R"(
175 precision mediump float;
176 uniform sampler2D uTexture;
177 uniform vec4 uCropArea;
178 varying highp vec2 vUv;
179 void main() {
180 vec2 uv = vec2(mix(uCropArea.x, uCropArea.z, vUv.x),
181 mix(uCropArea.y, uCropArea.w, vUv.y));
182 gl_FragColor = texture2D(uTexture, uv);
183 })";
184
185static GLfloat quadPositions[] = {
186 -0.5f, -0.5f,
187 +0.5f, -0.5f,
188 +0.5f, +0.5f,
189 +0.5f, +0.5f,
190 -0.5f, +0.5f,
191 -0.5f, -0.5f
192};
193static GLfloat quadUVs[] = {
194 0.0f, 1.0f,
195 1.0f, 1.0f,
196 1.0f, 0.0f,
197 1.0f, 0.0f,
198 0.0f, 0.0f,
199 0.0f, 1.0f
200};
Narayan Kamathafd31e02013-12-03 13:16:03 +0000201
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800202// ---------------------------------------------------------------------------
203
Ed Coyne7464ac92017-06-08 12:26:48 -0700204BootAnimation::BootAnimation(sp<Callbacks> callbacks)
Marin Shalamanov9070c052020-05-18 19:05:17 +0200205 : Thread(false), mLooper(new Looper(false)), mClockEnabled(true), mTimeIsAccurate(false),
Yi Kong911ac232019-03-24 01:49:02 -0700206 mTimeFormat12Hour(false), mTimeCheckThread(nullptr), mCallbacks(callbacks) {
Parth Sane7f7b2212023-04-12 17:23:02 +0000207 ATRACE_CALL();
Mathias Agopian627e7b52009-05-21 19:21:59 -0700208 mSession = new SurfaceComposerClient();
Geoffrey Pitsch290c4352016-08-09 14:35:10 -0400209
Keun-young Parkb5938422017-03-23 13:46:24 -0700210 std::string powerCtl = android::base::GetProperty("sys.powerctl", "");
211 if (powerCtl.empty()) {
212 mShuttingDown = false;
213 } else {
214 mShuttingDown = true;
215 }
Huihong Luo50a2f362018-08-11 09:27:57 -0700216 ALOGD("%sAnimationStartTiming start time: %" PRId64 "ms", mShuttingDown ? "Shutdown" : "Boot",
217 elapsedRealtime());
218}
219
220BootAnimation::~BootAnimation() {
Parth Sane7f7b2212023-04-12 17:23:02 +0000221 ATRACE_CALL();
Huihong Luo50a2f362018-08-11 09:27:57 -0700222 if (mAnimation != nullptr) {
223 releaseAnimation(mAnimation);
224 mAnimation = nullptr;
225 }
226 ALOGD("%sAnimationStopTiming start time: %" PRId64 "ms", mShuttingDown ? "Shutdown" : "Boot",
227 elapsedRealtime());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800228}
229
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800230void BootAnimation::onFirstRef() {
Parth Sane7f7b2212023-04-12 17:23:02 +0000231 ATRACE_CALL();
Mathias Agopianbc726112009-09-23 15:44:05 -0700232 status_t err = mSession->linkToComposerDeath(this);
Wei Wang159a4102018-10-23 23:15:58 -0700233 SLOGE_IF(err, "linkToComposerDeath failed (%s) ", strerror(-err));
Mathias Agopian8434c532009-09-23 18:52:49 -0700234 if (err == NO_ERROR) {
Huihong Luo50a2f362018-08-11 09:27:57 -0700235 // Load the animation content -- this can be slow (eg 200ms)
236 // called before waitForSurfaceFlinger() in main() to avoid wait
237 ALOGD("%sAnimationPreloadTiming start time: %" PRId64 "ms",
238 mShuttingDown ? "Shutdown" : "Boot", elapsedRealtime());
239 preloadAnimation();
jiayongqiang43342022024-07-13 19:46:08 +0800240 ALOGD("%sAnimationPreloadTiming stop time: %" PRId64 "ms",
Huihong Luo50a2f362018-08-11 09:27:57 -0700241 mShuttingDown ? "Shutdown" : "Boot", elapsedRealtime());
Mathias Agopianbc726112009-09-23 15:44:05 -0700242 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800243}
244
Mathias Agopianbc726112009-09-23 15:44:05 -0700245sp<SurfaceComposerClient> BootAnimation::session() const {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800246 return mSession;
247}
248
Marin Shalamanov9070c052020-05-18 19:05:17 +0200249void BootAnimation::binderDied(const wp<IBinder>&) {
Parth Sane7f7b2212023-04-12 17:23:02 +0000250 ATRACE_CALL();
Mathias Agopianbc726112009-09-23 15:44:05 -0700251 // woah, surfaceflinger died!
Wei Wang159a4102018-10-23 23:15:58 -0700252 SLOGD("SurfaceFlinger died, exiting...");
Mathias Agopianbc726112009-09-23 15:44:05 -0700253
254 // calling requestExit() is not enough here because the Surface code
255 // might be blocked on a condition variable that will never be updated.
256 kill( getpid(), SIGKILL );
257 requestExit();
258}
259
Shan Huang2b31a362021-08-16 16:45:39 +0000260static void* decodeImage(const void* encodedData, size_t dataLength, AndroidBitmapInfo* outInfo,
261 bool premultiplyAlpha) {
Parth Sane7f7b2212023-04-12 17:23:02 +0000262 ATRACE_CALL();
Derek Sollenberger22d0d4c72020-04-08 10:53:46 -0400263 AImageDecoder* decoder = nullptr;
264 AImageDecoder_createFromBuffer(encodedData, dataLength, &decoder);
265 if (!decoder) {
266 return nullptr;
267 }
268
269 const AImageDecoderHeaderInfo* info = AImageDecoder_getHeaderInfo(decoder);
270 outInfo->width = AImageDecoderHeaderInfo_getWidth(info);
271 outInfo->height = AImageDecoderHeaderInfo_getHeight(info);
272 outInfo->format = AImageDecoderHeaderInfo_getAndroidBitmapFormat(info);
273 outInfo->stride = AImageDecoder_getMinimumStride(decoder);
274 outInfo->flags = 0;
275
Shan Huang2b31a362021-08-16 16:45:39 +0000276 if (!premultiplyAlpha) {
277 AImageDecoder_setUnpremultipliedRequired(decoder, true);
278 }
279
Derek Sollenberger22d0d4c72020-04-08 10:53:46 -0400280 const size_t size = outInfo->stride * outInfo->height;
281 void* pixels = malloc(size);
282 int result = AImageDecoder_decodeImage(decoder, pixels, outInfo->stride, size);
283 AImageDecoder_delete(decoder);
284
285 if (result != ANDROID_IMAGE_DECODER_SUCCESS) {
286 free(pixels);
287 return nullptr;
288 }
289 return pixels;
290}
291
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800292status_t BootAnimation::initTexture(Texture* texture, AssetManager& assets,
Shan Huang2b31a362021-08-16 16:45:39 +0000293 const char* name, bool premultiplyAlpha) {
Parth Sane7f7b2212023-04-12 17:23:02 +0000294 ATRACE_CALL();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800295 Asset* asset = assets.open(name, Asset::ACCESS_BUFFER);
Yi Kong911ac232019-03-24 01:49:02 -0700296 if (asset == nullptr)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800297 return NO_INIT;
Derek Sollenberger22d0d4c72020-04-08 10:53:46 -0400298
299 AndroidBitmapInfo bitmapInfo;
Shan Huang2b31a362021-08-16 16:45:39 +0000300 void* pixels = decodeImage(asset->getBuffer(false), asset->getLength(), &bitmapInfo,
301 premultiplyAlpha);
Derek Sollenberger22d0d4c72020-04-08 10:53:46 -0400302 auto pixelDeleter = std::unique_ptr<void, decltype(free)*>{ pixels, free };
303
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800304 asset->close();
305 delete asset;
306
Derek Sollenberger22d0d4c72020-04-08 10:53:46 -0400307 if (!pixels) {
308 return NO_INIT;
309 }
310
311 const int w = bitmapInfo.width;
312 const int h = bitmapInfo.height;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800313
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800314 texture->w = w;
315 texture->h = h;
316
317 glGenTextures(1, &texture->name);
318 glBindTexture(GL_TEXTURE_2D, texture->name);
319
Derek Sollenberger22d0d4c72020-04-08 10:53:46 -0400320 switch (bitmapInfo.format) {
321 case ANDROID_BITMAP_FORMAT_A_8:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800322 glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, w, h, 0, GL_ALPHA,
Derek Sollenberger22d0d4c72020-04-08 10:53:46 -0400323 GL_UNSIGNED_BYTE, pixels);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800324 break;
Derek Sollenberger22d0d4c72020-04-08 10:53:46 -0400325 case ANDROID_BITMAP_FORMAT_RGBA_4444:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800326 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA,
Derek Sollenberger22d0d4c72020-04-08 10:53:46 -0400327 GL_UNSIGNED_SHORT_4_4_4_4, pixels);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800328 break;
Derek Sollenberger22d0d4c72020-04-08 10:53:46 -0400329 case ANDROID_BITMAP_FORMAT_RGBA_8888:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800330 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA,
Derek Sollenberger22d0d4c72020-04-08 10:53:46 -0400331 GL_UNSIGNED_BYTE, pixels);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800332 break;
Derek Sollenberger22d0d4c72020-04-08 10:53:46 -0400333 case ANDROID_BITMAP_FORMAT_RGB_565:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800334 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, w, h, 0, GL_RGB,
Derek Sollenberger22d0d4c72020-04-08 10:53:46 -0400335 GL_UNSIGNED_SHORT_5_6_5, pixels);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800336 break;
337 default:
338 break;
339 }
340
Lucas Dupin8eedc022021-07-29 21:49:07 +0000341 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
342 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
343 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
344 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700345
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800346 return NO_ERROR;
347}
348
Shan Huang2b31a362021-08-16 16:45:39 +0000349status_t BootAnimation::initTexture(FileMap* map, int* width, int* height,
350 bool premultiplyAlpha) {
Parth Sane7f7b2212023-04-12 17:23:02 +0000351 ATRACE_CALL();
Derek Sollenberger22d0d4c72020-04-08 10:53:46 -0400352 AndroidBitmapInfo bitmapInfo;
Shan Huang2b31a362021-08-16 16:45:39 +0000353 void* pixels = decodeImage(map->getDataPtr(), map->getDataLength(), &bitmapInfo,
354 premultiplyAlpha);
Derek Sollenberger22d0d4c72020-04-08 10:53:46 -0400355 auto pixelDeleter = std::unique_ptr<void, decltype(free)*>{ pixels, free };
Mathias Agopiana8826d62009-10-01 03:10:14 -0700356
Mykola Kondratenko0c1eeb32014-04-15 09:35:44 +0200357 // FileMap memory is never released until application exit.
358 // Release it now as the texture is already loaded and the memory used for
359 // the packed resource can be released.
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700360 delete map;
Mykola Kondratenko0c1eeb32014-04-15 09:35:44 +0200361
Derek Sollenberger22d0d4c72020-04-08 10:53:46 -0400362 if (!pixels) {
363 return NO_INIT;
364 }
365
366 const int w = bitmapInfo.width;
367 const int h = bitmapInfo.height;
Mathias Agopiana8826d62009-10-01 03:10:14 -0700368
Mathias Agopiana8826d62009-10-01 03:10:14 -0700369 int tw = 1 << (31 - __builtin_clz(w));
370 int th = 1 << (31 - __builtin_clz(h));
371 if (tw < w) tw <<= 1;
372 if (th < h) th <<= 1;
373
Derek Sollenberger22d0d4c72020-04-08 10:53:46 -0400374 switch (bitmapInfo.format) {
375 case ANDROID_BITMAP_FORMAT_RGBA_8888:
Sai Kiran Korwar27167492015-07-07 20:00:06 +0530376 if (!mUseNpotTextures && (tw != w || th != h)) {
Mathias Agopiana8826d62009-10-01 03:10:14 -0700377 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, tw, th, 0, GL_RGBA,
Yi Kong911ac232019-03-24 01:49:02 -0700378 GL_UNSIGNED_BYTE, nullptr);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700379 glTexSubImage2D(GL_TEXTURE_2D, 0,
Derek Sollenberger22d0d4c72020-04-08 10:53:46 -0400380 0, 0, w, h, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700381 } else {
Sai Kiran Korwar27167492015-07-07 20:00:06 +0530382 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA,
Derek Sollenberger22d0d4c72020-04-08 10:53:46 -0400383 GL_UNSIGNED_BYTE, pixels);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700384 }
385 break;
386
Derek Sollenberger22d0d4c72020-04-08 10:53:46 -0400387 case ANDROID_BITMAP_FORMAT_RGB_565:
Sai Kiran Korwar27167492015-07-07 20:00:06 +0530388 if (!mUseNpotTextures && (tw != w || th != h)) {
Mathias Agopiana8826d62009-10-01 03:10:14 -0700389 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, tw, th, 0, GL_RGB,
Yi Kong911ac232019-03-24 01:49:02 -0700390 GL_UNSIGNED_SHORT_5_6_5, nullptr);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700391 glTexSubImage2D(GL_TEXTURE_2D, 0,
Derek Sollenberger22d0d4c72020-04-08 10:53:46 -0400392 0, 0, w, h, GL_RGB, GL_UNSIGNED_SHORT_5_6_5, pixels);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700393 } else {
Sai Kiran Korwar27167492015-07-07 20:00:06 +0530394 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, w, h, 0, GL_RGB,
Derek Sollenberger22d0d4c72020-04-08 10:53:46 -0400395 GL_UNSIGNED_SHORT_5_6_5, pixels);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700396 }
397 break;
398 default:
399 break;
400 }
401
niuwenchaoa76df892023-09-11 17:19:04 +0800402 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
403 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
Lucas Dupin8eedc022021-07-29 21:49:07 +0000404 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
405 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700406
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700407 *width = w;
408 *height = h;
409
Mathias Agopiana8826d62009-10-01 03:10:14 -0700410 return NO_ERROR;
411}
412
Marin Shalamanov9070c052020-05-18 19:05:17 +0200413class BootAnimation::DisplayEventCallback : public LooperCallback {
414 BootAnimation* mBootAnimation;
415
416public:
417 DisplayEventCallback(BootAnimation* bootAnimation) {
Parth Sane7f7b2212023-04-12 17:23:02 +0000418 ATRACE_CALL();
Marin Shalamanov9070c052020-05-18 19:05:17 +0200419 mBootAnimation = bootAnimation;
420 }
421
422 int handleEvent(int /* fd */, int events, void* /* data */) {
Parth Sane7f7b2212023-04-12 17:23:02 +0000423 ATRACE_CALL();
Marin Shalamanov9070c052020-05-18 19:05:17 +0200424 if (events & (Looper::EVENT_ERROR | Looper::EVENT_HANGUP)) {
425 ALOGE("Display event receiver pipe was closed or an error occurred. events=0x%x",
426 events);
427 return 0; // remove the callback
428 }
429
430 if (!(events & Looper::EVENT_INPUT)) {
431 ALOGW("Received spurious callback for unhandled poll event. events=0x%x", events);
432 return 1; // keep the callback
433 }
434
435 constexpr int kBufferSize = 100;
436 DisplayEventReceiver::Event buffer[kBufferSize];
437 ssize_t numEvents;
438 do {
439 numEvents = mBootAnimation->mDisplayEventReceiver->getEvents(buffer, kBufferSize);
440 for (size_t i = 0; i < static_cast<size_t>(numEvents); i++) {
441 const auto& event = buffer[i];
442 if (event.header.type == DisplayEventReceiver::DISPLAY_EVENT_HOTPLUG) {
443 SLOGV("Hotplug received");
444
445 if (!event.hotplug.connected) {
446 // ignore hotplug disconnect
447 continue;
448 }
449 auto token = SurfaceComposerClient::getPhysicalDisplayToken(
450 event.header.displayId);
451
452 if (token != mBootAnimation->mDisplayToken) {
453 // ignore hotplug of a secondary display
454 continue;
455 }
456
Marin Shalamanovcb783ca2021-01-29 21:26:07 +0100457 DisplayMode displayMode;
458 const status_t error = SurfaceComposerClient::getActiveDisplayMode(
459 mBootAnimation->mDisplayToken, &displayMode);
Marin Shalamanov9070c052020-05-18 19:05:17 +0200460 if (error != NO_ERROR) {
Marin Shalamanovcb783ca2021-01-29 21:26:07 +0100461 SLOGE("Can't get active display mode.");
Marin Shalamanov9070c052020-05-18 19:05:17 +0200462 }
Marin Shalamanovcb783ca2021-01-29 21:26:07 +0100463 mBootAnimation->resizeSurface(displayMode.resolution.getWidth(),
464 displayMode.resolution.getHeight());
Marin Shalamanov9070c052020-05-18 19:05:17 +0200465 }
466 }
467 } while (numEvents > 0);
468
469 return 1; // keep the callback
470 }
471};
472
473EGLConfig BootAnimation::getEglConfig(const EGLDisplay& display) {
474 const EGLint attribs[] = {
Andrii Chepurnyid93f7b22023-03-15 11:22:07 +0200475 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
Marin Shalamanov9070c052020-05-18 19:05:17 +0200476 EGL_RED_SIZE, 8,
477 EGL_GREEN_SIZE, 8,
478 EGL_BLUE_SIZE, 8,
479 EGL_DEPTH_SIZE, 0,
480 EGL_NONE
481 };
482 EGLint numConfigs;
483 EGLConfig config;
484 eglChooseConfig(display, attribs, &config, 1, &numConfigs);
485 return config;
486}
487
Marin Shalamanov047802d2020-05-19 23:55:12 +0200488ui::Size BootAnimation::limitSurfaceSize(int width, int height) const {
489 ui::Size limited(width, height);
490 bool wasLimited = false;
491 const float aspectRatio = float(width) / float(height);
492 if (mMaxWidth != 0 && width > mMaxWidth) {
493 limited.height = mMaxWidth / aspectRatio;
494 limited.width = mMaxWidth;
495 wasLimited = true;
496 }
497 if (mMaxHeight != 0 && limited.height > mMaxHeight) {
498 limited.height = mMaxHeight;
499 limited.width = mMaxHeight * aspectRatio;
500 wasLimited = true;
501 }
502 SLOGV_IF(wasLimited, "Surface size has been limited to [%dx%d] from [%dx%d]",
503 limited.width, limited.height, width, height);
504 return limited;
505}
506
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800507status_t BootAnimation::readyToRun() {
Parth Sane7f7b2212023-04-12 17:23:02 +0000508 ATRACE_CALL();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800509 mAssets.addDefaultAssets();
510
Huihong Luo44305102022-09-26 11:16:20 -0700511 const std::vector<PhysicalDisplayId> ids = SurfaceComposerClient::getPhysicalDisplayIds();
512 if (ids.empty()) {
513 SLOGE("Failed to get ID for any displays\n");
Dominik Laskowski69b281d2019-11-22 14:13:12 -0800514 return NAME_NOT_FOUND;
Huihong Luo44305102022-09-26 11:16:20 -0700515 }
Dominik Laskowski3316a0a2019-01-25 02:56:41 -0800516
Huihong Luo44305102022-09-26 11:16:20 -0700517 // this system property specifies multi-display IDs to show the boot animation
Huihong Luof97c9e22019-05-29 11:08:01 -0700518 // multiple ids can be set with comma (,) as separator, for example:
Huihong Luod4a217e2020-01-16 15:56:33 -0800519 // setprop persist.boot.animation.displays 19260422155234049,19261083906282754
Marin Shalamanov11599ee2020-07-27 21:31:48 +0200520 Vector<PhysicalDisplayId> physicalDisplayIds;
Huihong Luof97c9e22019-05-29 11:08:01 -0700521 char displayValue[PROPERTY_VALUE_MAX] = "";
Huihong Luod4a217e2020-01-16 15:56:33 -0800522 property_get(DISPLAYS_PROP_NAME, displayValue, "");
Huihong Luof97c9e22019-05-29 11:08:01 -0700523 bool isValid = displayValue[0] != '\0';
524 if (isValid) {
525 char *p = displayValue;
526 while (*p) {
527 if (!isdigit(*p) && *p != ',') {
528 isValid = false;
529 break;
530 }
531 p ++;
532 }
533 if (!isValid)
Huihong Luod4a217e2020-01-16 15:56:33 -0800534 SLOGE("Invalid syntax for the value of system prop: %s", DISPLAYS_PROP_NAME);
Huihong Luof97c9e22019-05-29 11:08:01 -0700535 }
536 if (isValid) {
537 std::istringstream stream(displayValue);
Marin Shalamanov11599ee2020-07-27 21:31:48 +0200538 for (PhysicalDisplayId id; stream >> id.value; ) {
Huihong Luof97c9e22019-05-29 11:08:01 -0700539 physicalDisplayIds.add(id);
540 if (stream.peek() == ',')
541 stream.ignore();
542 }
543
Huihong Luo44305102022-09-26 11:16:20 -0700544 // the first specified display id is used to retrieve mDisplayToken
545 for (const auto id : physicalDisplayIds) {
546 if (std::find(ids.begin(), ids.end(), id) != ids.end()) {
547 if (const auto token = SurfaceComposerClient::getPhysicalDisplayToken(id)) {
548 mDisplayToken = token;
549 break;
550 }
551 }
552 }
553 }
554
555 // If the system property is not present or invalid, display 0 is used
556 if (mDisplayToken == nullptr) {
557 mDisplayToken = SurfaceComposerClient::getPhysicalDisplayToken(ids.front());
558 if (mDisplayToken == nullptr) {
559 return NAME_NOT_FOUND;
560 }
561 }
562
563 DisplayMode displayMode;
564 const status_t error =
565 SurfaceComposerClient::getActiveDisplayMode(mDisplayToken, &displayMode);
566 if (error != NO_ERROR) {
567 return error;
568 }
569
570 mMaxWidth = android::base::GetIntProperty("ro.surface_flinger.max_graphics_width", 0);
571 mMaxHeight = android::base::GetIntProperty("ro.surface_flinger.max_graphics_height", 0);
572 ui::Size resolution = displayMode.resolution;
573 resolution = limitSurfaceSize(resolution.width, resolution.height);
574 // create the native surface
575 sp<SurfaceControl> control = session()->createSurface(String8("BootAnimation"),
576 resolution.getWidth(), resolution.getHeight(), PIXEL_FORMAT_RGB_565,
577 ISurfaceComposerClient::eOpaque);
578
579 SurfaceComposerClient::Transaction t;
580 if (isValid) {
Huihong Luof97c9e22019-05-29 11:08:01 -0700581 // In the case of multi-display, boot animation shows on the specified displays
Dominik Laskowski989de3d2021-07-27 21:09:34 -0700582 for (const auto id : physicalDisplayIds) {
Huihong Luof97c9e22019-05-29 11:08:01 -0700583 if (std::find(ids.begin(), ids.end(), id) != ids.end()) {
Dominik Laskowski989de3d2021-07-27 21:09:34 -0700584 if (const auto token = SurfaceComposerClient::getPhysicalDisplayToken(id)) {
585 t.setDisplayLayerStack(token, ui::DEFAULT_LAYER_STACK);
586 }
Huihong Luof97c9e22019-05-29 11:08:01 -0700587 }
588 }
Dominik Laskowski989de3d2021-07-27 21:09:34 -0700589 t.setLayerStack(control, ui::DEFAULT_LAYER_STACK);
Huihong Luof97c9e22019-05-29 11:08:01 -0700590 }
591
Robert Carre13b58e2017-08-31 14:50:44 -0700592 t.setLayer(control, 0x40000000)
593 .apply();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800594
Mathias Agopian17f638b2009-04-16 20:04:08 -0700595 sp<Surface> s = control->getSurface();
596
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800597 // initialize opengl and egl
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800598 EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
Yi Kong911ac232019-03-24 01:49:02 -0700599 eglInitialize(display, nullptr, nullptr);
Marin Shalamanov9070c052020-05-18 19:05:17 +0200600 EGLConfig config = getEglConfig(display);
601 EGLSurface surface = eglCreateWindowSurface(display, config, s.get(), nullptr);
Lucas Dupin8eedc022021-07-29 21:49:07 +0000602 // Initialize egl context with client version number 2.0.
603 EGLint contextAttributes[] = {EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE};
604 EGLContext context = eglCreateContext(display, config, nullptr, contextAttributes);
Marin Shalamanov9070c052020-05-18 19:05:17 +0200605 EGLint w, h;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800606 eglQuerySurface(display, surface, EGL_WIDTH, &w);
607 eglQuerySurface(display, surface, EGL_HEIGHT, &h);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700608
Huihong Luo44305102022-09-26 11:16:20 -0700609 if (eglMakeCurrent(display, surface, surface, context) == EGL_FALSE) {
Mathias Agopianabac0102009-07-31 14:47:00 -0700610 return NO_INIT;
Huihong Luo44305102022-09-26 11:16:20 -0700611 }
Mathias Agopiana8826d62009-10-01 03:10:14 -0700612
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800613 mDisplay = display;
614 mContext = context;
615 mSurface = surface;
joenchen85b339f2022-04-11 23:58:34 +0800616 mInitWidth = mWidth = w;
617 mInitHeight = mHeight = h;
Mathias Agopian17f638b2009-04-16 20:04:08 -0700618 mFlingerSurfaceControl = control;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800619 mFlingerSurface = s;
Adrian Roos9ee5dff2018-08-22 20:19:49 +0200620 mTargetInset = -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800621
Mariia Sandrikovaa166f012022-12-10 02:32:14 +0000622 // Rotate the boot animation according to the value specified in the sysprop
623 // ro.bootanim.set_orientation_<display_id>. Four values are supported: ORIENTATION_0,
624 // ORIENTATION_90, ORIENTATION_180 and ORIENTATION_270.
625 // If the value isn't specified or is ORIENTATION_0, nothing will be changed.
626 // This is needed to support having boot animation in orientations different from the natural
627 // device orientation. For example, on tablets that may want to keep natural orientation
628 // portrait for applications compatibility and to have the boot animation in landscape.
629 rotateAwayFromNaturalOrientationIfNeeded();
630
Yegor Malyshev6925e1b2020-09-10 16:42:20 +0200631 projectSceneToWindow();
632
Marin Shalamanov9070c052020-05-18 19:05:17 +0200633 // Register a display event receiver
634 mDisplayEventReceiver = std::make_unique<DisplayEventReceiver>();
635 status_t status = mDisplayEventReceiver->initCheck();
636 SLOGE_IF(status != NO_ERROR, "Initialization of DisplayEventReceiver failed with status: %d",
637 status);
638 mLooper->addFd(mDisplayEventReceiver->getFd(), 0, Looper::EVENT_INPUT,
639 new DisplayEventCallback(this), nullptr);
640
Huihong Luo50a2f362018-08-11 09:27:57 -0700641 return NO_ERROR;
642}
643
Mariia Sandrikovaa166f012022-12-10 02:32:14 +0000644void BootAnimation::rotateAwayFromNaturalOrientationIfNeeded() {
Parth Sane7f7b2212023-04-12 17:23:02 +0000645 ATRACE_CALL();
Mariia Sandrikovaa166f012022-12-10 02:32:14 +0000646 const auto orientation = parseOrientationProperty();
647
648 if (orientation == ui::ROTATION_0) {
649 // Do nothing if the sysprop isn't set or is set to ROTATION_0.
650 return;
651 }
652
653 if (orientation == ui::ROTATION_90 || orientation == ui::ROTATION_270) {
654 std::swap(mWidth, mHeight);
655 std::swap(mInitWidth, mInitHeight);
656 mFlingerSurfaceControl->updateDefaultBufferSize(mWidth, mHeight);
657 }
658
659 Rect displayRect(0, 0, mWidth, mHeight);
660 Rect layerStackRect(0, 0, mWidth, mHeight);
661
662 SurfaceComposerClient::Transaction t;
663 t.setDisplayProjection(mDisplayToken, orientation, layerStackRect, displayRect);
664 t.apply();
665}
666
667ui::Rotation BootAnimation::parseOrientationProperty() {
Parth Sane7f7b2212023-04-12 17:23:02 +0000668 ATRACE_CALL();
Mariia Sandrikovaa166f012022-12-10 02:32:14 +0000669 const auto displayIds = SurfaceComposerClient::getPhysicalDisplayIds();
670 if (displayIds.size() == 0) {
671 return ui::ROTATION_0;
672 }
673 const auto displayId = displayIds[0];
674 const auto syspropName = [displayId] {
675 std::stringstream ss;
676 ss << "ro.bootanim.set_orientation_" << displayId.value;
677 return ss.str();
678 }();
YoungJoon Yang9cf990e2023-11-13 15:34:28 +0900679 auto syspropValue = android::base::GetProperty(syspropName, "");
680 if (syspropValue == "") {
681 syspropValue = android::base::GetProperty("ro.bootanim.set_orientation_logical_0", "");
682 }
683
Mariia Sandrikovaa166f012022-12-10 02:32:14 +0000684 if (syspropValue == "ORIENTATION_90") {
685 return ui::ROTATION_90;
686 } else if (syspropValue == "ORIENTATION_180") {
687 return ui::ROTATION_180;
688 } else if (syspropValue == "ORIENTATION_270") {
689 return ui::ROTATION_270;
690 }
691 return ui::ROTATION_0;
692}
693
Yegor Malyshev6925e1b2020-09-10 16:42:20 +0200694void BootAnimation::projectSceneToWindow() {
Parth Sane7f7b2212023-04-12 17:23:02 +0000695 ATRACE_CALL();
Yegor Malyshev6925e1b2020-09-10 16:42:20 +0200696 glViewport(0, 0, mWidth, mHeight);
697 glScissor(0, 0, mWidth, mHeight);
Yegor Malyshev6925e1b2020-09-10 16:42:20 +0200698}
699
Marin Shalamanov9070c052020-05-18 19:05:17 +0200700void BootAnimation::resizeSurface(int newWidth, int newHeight) {
Parth Sane7f7b2212023-04-12 17:23:02 +0000701 ATRACE_CALL();
Marin Shalamanov9070c052020-05-18 19:05:17 +0200702 // We assume this function is called on the animation thread.
703 if (newWidth == mWidth && newHeight == mHeight) {
704 return;
705 }
706 SLOGV("Resizing the boot animation surface to %d %d", newWidth, newHeight);
707
708 eglMakeCurrent(mDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
709 eglDestroySurface(mDisplay, mSurface);
710
Marin Shalamanov047802d2020-05-19 23:55:12 +0200711 const auto limitedSize = limitSurfaceSize(newWidth, newHeight);
712 mWidth = limitedSize.width;
713 mHeight = limitedSize.height;
Marin Shalamanov9070c052020-05-18 19:05:17 +0200714
derek.li92310112024-05-10 17:59:51 +0800715 mFlingerSurfaceControl->updateDefaultBufferSize(mWidth, mHeight);
Marin Shalamanov9070c052020-05-18 19:05:17 +0200716 EGLConfig config = getEglConfig(mDisplay);
717 EGLSurface surface = eglCreateWindowSurface(mDisplay, config, mFlingerSurface.get(), nullptr);
718 if (eglMakeCurrent(mDisplay, surface, surface, mContext) == EGL_FALSE) {
719 SLOGE("Can't make the new surface current. Error %d", eglGetError());
720 return;
721 }
Yegor Malyshev6925e1b2020-09-10 16:42:20 +0200722
723 projectSceneToWindow();
Marin Shalamanov9070c052020-05-18 19:05:17 +0200724
725 mSurface = surface;
726}
727
Huihong Luo50a2f362018-08-11 09:27:57 -0700728bool BootAnimation::preloadAnimation() {
Parth Sane7f7b2212023-04-12 17:23:02 +0000729 ATRACE_CALL();
Huihong Luo50a2f362018-08-11 09:27:57 -0700730 findBootAnimationFile();
Tomasz Wasilczyke8fb3c72023-08-24 19:11:22 +0000731 if (!mZipFileName.empty()) {
Huihong Luo50a2f362018-08-11 09:27:57 -0700732 mAnimation = loadAnimation(mZipFileName);
733 return (mAnimation != nullptr);
734 }
735
736 return false;
737}
738
Nikita Ioffe06c986e2020-01-27 17:16:03 +0000739bool BootAnimation::findBootAnimationFileInternal(const std::vector<std::string> &files) {
Parth Sane7f7b2212023-04-12 17:23:02 +0000740 ATRACE_CALL();
Nikita Ioffe06c986e2020-01-27 17:16:03 +0000741 for (const std::string& f : files) {
742 if (access(f.c_str(), R_OK) == 0) {
743 mZipFileName = f.c_str();
744 return true;
745 }
746 }
747 return false;
748}
749
Huihong Luo50a2f362018-08-11 09:27:57 -0700750void BootAnimation::findBootAnimationFile() {
Parth Sane7f7b2212023-04-12 17:23:02 +0000751 ATRACE_CALL();
Beverly34ffe252019-05-17 11:03:54 -0400752 const bool playDarkAnim = android::base::GetIntProperty("ro.boot.theme", 0) == 1;
jorgetmbd89e9b2024-09-09 19:42:26 +0000753 const std::string productBootanimationFile = PRODUCT_BOOTANIMATION_DIR +
754 android::base::GetProperty("ro.product.bootanim.file", playDarkAnim ?
755 PRODUCT_BOOTANIMATION_DARK_FILE : PRODUCT_BOOTANIMATION_FILE);
Nikita Ioffe06c986e2020-01-27 17:16:03 +0000756 static const std::vector<std::string> bootFiles = {
jorgetmbd89e9b2024-09-09 19:42:26 +0000757 APEX_BOOTANIMATION_FILE, productBootanimationFile,
Nikita Ioffe06c986e2020-01-27 17:16:03 +0000758 OEM_BOOTANIMATION_FILE, SYSTEM_BOOTANIMATION_FILE
759 };
760 static const std::vector<std::string> shutdownFiles = {
761 PRODUCT_SHUTDOWNANIMATION_FILE, OEM_SHUTDOWNANIMATION_FILE, SYSTEM_SHUTDOWNANIMATION_FILE, ""
762 };
763 static const std::vector<std::string> userspaceRebootFiles = {
764 PRODUCT_USERSPACE_REBOOT_ANIMATION_FILE, OEM_USERSPACE_REBOOT_ANIMATION_FILE,
765 SYSTEM_USERSPACE_REBOOT_ANIMATION_FILE,
766 };
Keun-young Parkb5938422017-03-23 13:46:24 -0700767
Nikita Ioffe06c986e2020-01-27 17:16:03 +0000768 if (android::base::GetBoolProperty("sys.init.userspace_reboot.in_progress", false)) {
769 findBootAnimationFileInternal(userspaceRebootFiles);
770 } else if (mShuttingDown) {
771 findBootAnimationFileInternal(shutdownFiles);
772 } else {
773 findBootAnimationFileInternal(bootFiles);
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700774 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800775}
776
Lucas Dupin8eedc022021-07-29 21:49:07 +0000777GLuint compileShader(GLenum shaderType, const GLchar *source) {
Parth Sane7f7b2212023-04-12 17:23:02 +0000778 ATRACE_CALL();
Lucas Dupin8eedc022021-07-29 21:49:07 +0000779 GLuint shader = glCreateShader(shaderType);
780 glShaderSource(shader, 1, &source, 0);
781 glCompileShader(shader);
782 GLint isCompiled = 0;
783 glGetShaderiv(shader, GL_COMPILE_STATUS, &isCompiled);
784 if (isCompiled == GL_FALSE) {
785 SLOGE("Compile shader failed. Shader type: %d", shaderType);
Shan Huang81846ba2021-09-07 19:07:47 +0000786 GLint maxLength = 0;
787 glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &maxLength);
788 std::vector<GLchar> errorLog(maxLength);
789 glGetShaderInfoLog(shader, maxLength, &maxLength, &errorLog[0]);
790 SLOGE("Shader compilation error: %s", &errorLog[0]);
Lucas Dupin8eedc022021-07-29 21:49:07 +0000791 return 0;
792 }
793 return shader;
794}
795
796GLuint linkShader(GLuint vertexShader, GLuint fragmentShader) {
Parth Sane7f7b2212023-04-12 17:23:02 +0000797 ATRACE_CALL();
Lucas Dupin8eedc022021-07-29 21:49:07 +0000798 GLuint program = glCreateProgram();
799 glAttachShader(program, vertexShader);
800 glAttachShader(program, fragmentShader);
801 glLinkProgram(program);
802 GLint isLinked = 0;
803 glGetProgramiv(program, GL_LINK_STATUS, (int *)&isLinked);
804 if (isLinked == GL_FALSE) {
805 SLOGE("Linking shader failed. Shader handles: vert %d, frag %d",
806 vertexShader, fragmentShader);
807 return 0;
808 }
809 return program;
810}
811
812void BootAnimation::initShaders() {
Parth Sane7f7b2212023-04-12 17:23:02 +0000813 ATRACE_CALL();
Shan Huang2b31a362021-08-16 16:45:39 +0000814 bool dynamicColoringEnabled = mAnimation != nullptr && mAnimation->dynamicColoringEnabled;
Lucas Dupin8eedc022021-07-29 21:49:07 +0000815 GLuint vertexShader = compileShader(GL_VERTEX_SHADER, (const GLchar *)VERTEX_SHADER_SOURCE);
816 GLuint imageFragmentShader =
Shan Huang2b31a362021-08-16 16:45:39 +0000817 compileShader(GL_FRAGMENT_SHADER, dynamicColoringEnabled
818 ? (const GLchar *)IMAGE_FRAG_DYNAMIC_COLORING_SHADER_SOURCE
819 : (const GLchar *)IMAGE_FRAG_SHADER_SOURCE);
Lucas Dupin8eedc022021-07-29 21:49:07 +0000820 GLuint textFragmentShader =
821 compileShader(GL_FRAGMENT_SHADER, (const GLchar *)TEXT_FRAG_SHADER_SOURCE);
822
823 // Initialize image shader.
824 mImageShader = linkShader(vertexShader, imageFragmentShader);
825 GLint positionLocation = glGetAttribLocation(mImageShader, A_POSITION);
826 GLint uvLocation = glGetAttribLocation(mImageShader, A_UV);
827 mImageTextureLocation = glGetUniformLocation(mImageShader, U_TEXTURE);
828 mImageFadeLocation = glGetUniformLocation(mImageShader, U_FADE);
829 glEnableVertexAttribArray(positionLocation);
830 glVertexAttribPointer(positionLocation, 2, GL_FLOAT, GL_FALSE, 0, quadPositions);
831 glVertexAttribPointer(uvLocation, 2, GL_FLOAT, GL_FALSE, 0, quadUVs);
832 glEnableVertexAttribArray(uvLocation);
833
834 // Initialize text shader.
835 mTextShader = linkShader(vertexShader, textFragmentShader);
836 positionLocation = glGetAttribLocation(mTextShader, A_POSITION);
837 uvLocation = glGetAttribLocation(mTextShader, A_UV);
838 mTextTextureLocation = glGetUniformLocation(mTextShader, U_TEXTURE);
839 mTextCropAreaLocation = glGetUniformLocation(mTextShader, U_CROP_AREA);
840 glEnableVertexAttribArray(positionLocation);
841 glVertexAttribPointer(positionLocation, 2, GL_FLOAT, GL_FALSE, 0, quadPositions);
842 glVertexAttribPointer(uvLocation, 2, GL_FLOAT, GL_FALSE, 0, quadUVs);
843 glEnableVertexAttribArray(uvLocation);
844}
845
Marin Shalamanov9070c052020-05-18 19:05:17 +0200846bool BootAnimation::threadLoop() {
Parth Sane7f7b2212023-04-12 17:23:02 +0000847 ATRACE_CALL();
Marin Shalamanov9070c052020-05-18 19:05:17 +0200848 bool result;
Lucas Dupin8eedc022021-07-29 21:49:07 +0000849 initShaders();
850
Narayan Kamathafd31e02013-12-03 13:16:03 +0000851 // We have no bootanimation file, so we use the stock android logo
852 // animation.
Tomasz Wasilczyke8fb3c72023-08-24 19:11:22 +0000853 if (mZipFileName.empty()) {
Yoshiaki Maruoka9ef19b82017-10-25 13:55:49 +0900854 ALOGD("No animation file");
Marin Shalamanov9070c052020-05-18 19:05:17 +0200855 result = android();
Mathias Agopiana8826d62009-10-01 03:10:14 -0700856 } else {
Marin Shalamanov9070c052020-05-18 19:05:17 +0200857 result = movie();
Mathias Agopiana8826d62009-10-01 03:10:14 -0700858 }
859
Philip Junker60600562020-11-25 18:16:16 +0100860 mCallbacks->shutdown();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800861 eglMakeCurrent(mDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
862 eglDestroyContext(mDisplay, mContext);
863 eglDestroySurface(mDisplay, mSurface);
Mathias Agopian6cf0db22009-04-17 19:36:26 -0700864 mFlingerSurface.clear();
Mathias Agopian17f638b2009-04-16 20:04:08 -0700865 mFlingerSurfaceControl.clear();
Mathias Agopian627e7b52009-05-21 19:21:59 -0700866 eglTerminate(mDisplay);
Sai Kiran Korwar3eee9fb2015-06-16 17:13:35 +0530867 eglReleaseThread();
Mathias Agopian627e7b52009-05-21 19:21:59 -0700868 IPCThreadState::self()->stopProcess();
Marin Shalamanov9070c052020-05-18 19:05:17 +0200869 return result;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800870}
871
Marin Shalamanov9070c052020-05-18 19:05:17 +0200872bool BootAnimation::android() {
Parth Sane7f7b2212023-04-12 17:23:02 +0000873 ATRACE_CALL();
Lucas Dupin8eedc022021-07-29 21:49:07 +0000874 glActiveTexture(GL_TEXTURE0);
875
Wei Wang159a4102018-10-23 23:15:58 -0700876 SLOGD("%sAnimationShownTiming start time: %" PRId64 "ms", mShuttingDown ? "Shutdown" : "Boot",
Keun-young Parkd1794cd2017-04-04 12:21:19 -0700877 elapsedRealtime());
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700878 initTexture(&mAndroid[0], mAssets, "images/android-logo-mask.png");
879 initTexture(&mAndroid[1], mAssets, "images/android-logo-shine.png");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800880
Ed Coyne7464ac92017-06-08 12:26:48 -0700881 mCallbacks->init({});
882
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800883 // clear screen
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700884 glDisable(GL_DITHER);
885 glDisable(GL_SCISSOR_TEST);
Shan Huangbfd2d3c2021-09-09 23:19:04 +0000886 glUseProgram(mImageShader);
887
Mathias Agopian59f19e42011-05-06 19:22:12 -0700888 glClearColor(0,0,0,1);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800889 glClear(GL_COLOR_BUFFER_BIT);
890 eglSwapBuffers(mDisplay, mSurface);
891
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700892 // Blend state
893 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700894
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800895 const nsecs_t startTime = systemTime();
896 do {
Marin Shalamanov9070c052020-05-18 19:05:17 +0200897 processDisplayEvents();
898 const GLint xc = (mWidth - mAndroid[0].w) / 2;
899 const GLint yc = (mHeight - mAndroid[0].h) / 2;
900 const Rect updateRect(xc, yc, xc + mAndroid[0].w, yc + mAndroid[0].h);
901 glScissor(updateRect.left, mHeight - updateRect.bottom, updateRect.width(),
902 updateRect.height());
903
Mathias Agopian13796652009-03-24 22:49:21 -0700904 nsecs_t now = systemTime();
905 double time = now - startTime;
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700906 float t = 4.0f * float(time / us2ns(16667)) / mAndroid[1].w;
907 GLint offset = (1 - (t - floorf(t))) * mAndroid[1].w;
908 GLint x = xc - offset;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800909
Mathias Agopian81668642009-07-28 11:41:30 -0700910 glDisable(GL_SCISSOR_TEST);
911 glClear(GL_COLOR_BUFFER_BIT);
912
913 glEnable(GL_SCISSOR_TEST);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800914 glDisable(GL_BLEND);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800915 glBindTexture(GL_TEXTURE_2D, mAndroid[1].name);
Lucas Dupin8eedc022021-07-29 21:49:07 +0000916 drawTexturedQuad(x, yc, mAndroid[1].w, mAndroid[1].h);
917 drawTexturedQuad(x + mAndroid[1].w, yc, mAndroid[1].w, mAndroid[1].h);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800918
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700919 glEnable(GL_BLEND);
920 glBindTexture(GL_TEXTURE_2D, mAndroid[0].name);
Lucas Dupin8eedc022021-07-29 21:49:07 +0000921 drawTexturedQuad(xc, yc, mAndroid[0].w, mAndroid[0].h);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800922
Mathias Agopian627e7b52009-05-21 19:21:59 -0700923 EGLBoolean res = eglSwapBuffers(mDisplay, mSurface);
924 if (res == EGL_FALSE)
925 break;
926
Mathias Agopian13796652009-03-24 22:49:21 -0700927 // 12fps: don't animate too fast to preserve CPU
928 const nsecs_t sleepTime = 83333 - ns2us(systemTime() - now);
929 if (sleepTime > 0)
Mathias Agopiana8826d62009-10-01 03:10:14 -0700930 usleep(sleepTime);
Kevin Hesterd3782b22012-04-26 10:38:55 -0700931
932 checkExit();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800933 } while (!exitPending());
934
935 glDeleteTextures(1, &mAndroid[0].name);
936 glDeleteTextures(1, &mAndroid[1].name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800937 return false;
938}
939
Kevin Hesterd3782b22012-04-26 10:38:55 -0700940void BootAnimation::checkExit() {
Parth Sane7f7b2212023-04-12 17:23:02 +0000941 ATRACE_CALL();
Kevin Hesterd3782b22012-04-26 10:38:55 -0700942 // Allow surface flinger to gracefully request shutdown
943 char value[PROPERTY_VALUE_MAX];
944 property_get(EXIT_PROP_NAME, value, "0");
945 int exitnow = atoi(value);
946 if (exitnow) {
947 requestExit();
948 }
949}
950
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700951bool BootAnimation::validClock(const Animation::Part& part) {
Parth Sane7f7b2212023-04-12 17:23:02 +0000952 ATRACE_CALL();
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700953 return part.clockPosX != TEXT_MISSING_VALUE && part.clockPosY != TEXT_MISSING_VALUE;
954}
955
956bool parseTextCoord(const char* str, int* dest) {
Parth Sane7f7b2212023-04-12 17:23:02 +0000957 ATRACE_CALL();
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700958 if (strcmp("c", str) == 0) {
959 *dest = TEXT_CENTER_VALUE;
960 return true;
961 }
962
963 char* end;
964 int val = (int) strtol(str, &end, 0);
965 if (end == str || *end != '\0' || val == INT_MAX || val == INT_MIN) {
966 return false;
967 }
968 *dest = val;
969 return true;
970}
971
972// Parse two position coordinates. If only string is non-empty, treat it as the y value.
973void parsePosition(const char* str1, const char* str2, int* x, int* y) {
Parth Sane7f7b2212023-04-12 17:23:02 +0000974 ATRACE_CALL();
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700975 bool success = false;
976 if (strlen(str1) == 0) { // No values were specified
977 // success = false
978 } else if (strlen(str2) == 0) { // we have only one value
979 if (parseTextCoord(str1, y)) {
980 *x = TEXT_CENTER_VALUE;
981 success = true;
982 }
983 } else {
984 if (parseTextCoord(str1, x) && parseTextCoord(str2, y)) {
985 success = true;
986 }
987 }
988
989 if (!success) {
990 *x = TEXT_MISSING_VALUE;
991 *y = TEXT_MISSING_VALUE;
992 }
993}
994
Jesse Hall083b84c2014-09-22 10:51:09 -0700995// Parse a color represented as an HTML-style 'RRGGBB' string: each pair of
996// characters in str is a hex number in [0, 255], which are converted to
997// floating point values in the range [0.0, 1.0] and placed in the
998// corresponding elements of color.
999//
1000// If the input string isn't valid, parseColor returns false and color is
1001// left unchanged.
1002static bool parseColor(const char str[7], float color[3]) {
Parth Sane7f7b2212023-04-12 17:23:02 +00001003 ATRACE_CALL();
Jesse Hall083b84c2014-09-22 10:51:09 -07001004 float tmpColor[3];
1005 for (int i = 0; i < 3; i++) {
1006 int val = 0;
1007 for (int j = 0; j < 2; j++) {
1008 val *= 16;
1009 char c = str[2*i + j];
1010 if (c >= '0' && c <= '9') val += c - '0';
1011 else if (c >= 'A' && c <= 'F') val += (c - 'A') + 10;
1012 else if (c >= 'a' && c <= 'f') val += (c - 'a') + 10;
1013 else return false;
1014 }
1015 tmpColor[i] = static_cast<float>(val) / 255.0f;
1016 }
1017 memcpy(color, tmpColor, sizeof(tmpColor));
1018 return true;
1019}
1020
Shan Huang2b31a362021-08-16 16:45:39 +00001021// Parse a color represented as a signed decimal int string.
1022// E.g. "-2757722" (whose hex 2's complement is 0xFFD5EBA6).
1023// If the input color string is empty, set color with values in defaultColor.
1024static void parseColorDecimalString(const std::string& colorString,
1025 float color[3], float defaultColor[3]) {
Parth Sane7f7b2212023-04-12 17:23:02 +00001026 ATRACE_CALL();
Shan Huang2b31a362021-08-16 16:45:39 +00001027 if (colorString == "") {
1028 memcpy(color, defaultColor, sizeof(float) * 3);
1029 return;
1030 }
1031 int colorInt = atoi(colorString.c_str());
1032 color[0] = ((float)((colorInt >> 16) & 0xFF)) / 0xFF; // r
1033 color[1] = ((float)((colorInt >> 8) & 0xFF)) / 0xFF; // g
1034 color[2] = ((float)(colorInt & 0xFF)) / 0xFF; // b
1035}
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001036
Marin Shalamanov9070c052020-05-18 19:05:17 +02001037static bool readFile(ZipFileRO* zip, const char* name, String8& outString) {
Parth Sane7f7b2212023-04-12 17:23:02 +00001038 ATRACE_CALL();
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001039 ZipEntryRO entry = zip->findEntryByName(name);
Wei Wang159a4102018-10-23 23:15:58 -07001040 SLOGE_IF(!entry, "couldn't find %s", name);
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -07001041 if (!entry) {
1042 return false;
1043 }
1044
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001045 FileMap* entryMap = zip->createEntryFileMap(entry);
1046 zip->releaseEntry(entry);
Wei Wang159a4102018-10-23 23:15:58 -07001047 SLOGE_IF(!entryMap, "entryMap is null");
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -07001048 if (!entryMap) {
1049 return false;
1050 }
1051
Tomasz Wasilczyk31eb3c892023-08-23 22:12:33 +00001052 outString = String8((char const*)entryMap->getDataPtr(), entryMap->getDataLength());
Narayan Kamath688ff4c2015-02-23 15:47:54 +00001053 delete entryMap;
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -07001054 return true;
1055}
1056
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -07001057// The font image should be a 96x2 array of character images. The
1058// columns are the printable ASCII characters 0x20 - 0x7f. The
1059// top row is regular text; the bottom row is bold.
1060status_t BootAnimation::initFont(Font* font, const char* fallback) {
Parth Sane7f7b2212023-04-12 17:23:02 +00001061 ATRACE_CALL();
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -07001062 status_t status = NO_ERROR;
Damien Bargiacchia704b7d2016-02-16 16:55:49 -08001063
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -07001064 if (font->map != nullptr) {
1065 glGenTextures(1, &font->texture.name);
1066 glBindTexture(GL_TEXTURE_2D, font->texture.name);
Damien Bargiacchia704b7d2016-02-16 16:55:49 -08001067
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -07001068 status = initTexture(font->map, &font->texture.w, &font->texture.h);
1069
Lucas Dupin8eedc022021-07-29 21:49:07 +00001070 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
1071 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
1072 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
1073 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -07001074 } else if (fallback != nullptr) {
1075 status = initTexture(&font->texture, mAssets, fallback);
1076 } else {
1077 return NO_INIT;
Damien Bargiacchia704b7d2016-02-16 16:55:49 -08001078 }
1079
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -07001080 if (status == NO_ERROR) {
1081 font->char_width = font->texture.w / FONT_NUM_COLS;
1082 font->char_height = font->texture.h / FONT_NUM_ROWS / 2; // There are bold and regular rows
1083 }
1084
1085 return status;
1086}
1087
1088void BootAnimation::drawText(const char* str, const Font& font, bool bold, int* x, int* y) {
Parth Sane7f7b2212023-04-12 17:23:02 +00001089 ATRACE_CALL();
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -07001090 glEnable(GL_BLEND); // Allow us to draw on top of the animation
1091 glBindTexture(GL_TEXTURE_2D, font.texture.name);
Lucas Dupin8eedc022021-07-29 21:49:07 +00001092 glUseProgram(mTextShader);
1093 glUniform1i(mTextTextureLocation, 0);
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -07001094
1095 const int len = strlen(str);
1096 const int strWidth = font.char_width * len;
1097
1098 if (*x == TEXT_CENTER_VALUE) {
1099 *x = (mWidth - strWidth) / 2;
1100 } else if (*x < 0) {
1101 *x = mWidth + *x - strWidth;
1102 }
1103 if (*y == TEXT_CENTER_VALUE) {
1104 *y = (mHeight - font.char_height) / 2;
1105 } else if (*y < 0) {
1106 *y = mHeight + *y - font.char_height;
1107 }
1108
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -07001109 for (int i = 0; i < len; i++) {
1110 char c = str[i];
1111
1112 if (c < FONT_BEGIN_CHAR || c > FONT_END_CHAR) {
1113 c = '?';
1114 }
1115
1116 // Crop the texture to only the pixels in the current glyph
1117 const int charPos = (c - FONT_BEGIN_CHAR); // Position in the list of valid characters
1118 const int row = charPos / FONT_NUM_COLS;
1119 const int col = charPos % FONT_NUM_COLS;
Lucas Dupin8eedc022021-07-29 21:49:07 +00001120 // Bold fonts are expected in the second half of each row.
1121 float v0 = (row + (bold ? 0.5f : 0.0f)) / FONT_NUM_ROWS;
1122 float u0 = ((float)col) / FONT_NUM_COLS;
1123 float v1 = v0 + 1.0f / FONT_NUM_ROWS / 2;
1124 float u1 = u0 + 1.0f / FONT_NUM_COLS;
1125 glUniform4f(mTextCropAreaLocation, u0, v0, u1, v1);
1126 drawTexturedQuad(*x, *y, font.char_width, font.char_height);
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -07001127
1128 *x += font.char_width;
1129 }
1130
1131 glDisable(GL_BLEND); // Return to the animation's default behaviour
1132 glBindTexture(GL_TEXTURE_2D, 0);
1133}
1134
Damien Bargiacchi9071db12016-10-28 17:38:22 -07001135// We render 12 or 24 hour time.
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -07001136void BootAnimation::drawClock(const Font& font, const int xPos, const int yPos) {
Parth Sane7f7b2212023-04-12 17:23:02 +00001137 ATRACE_CALL();
Damien Bargiacchi9071db12016-10-28 17:38:22 -07001138 static constexpr char TIME_FORMAT_12[] = "%l:%M";
1139 static constexpr char TIME_FORMAT_24[] = "%H:%M";
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -07001140 static constexpr int TIME_LENGTH = 6;
1141
Damien Bargiacchia704b7d2016-02-16 16:55:49 -08001142 time_t rawtime;
1143 time(&rawtime);
1144 struct tm* timeInfo = localtime(&rawtime);
1145
1146 char timeBuff[TIME_LENGTH];
Damien Bargiacchi9071db12016-10-28 17:38:22 -07001147 const char* timeFormat = mTimeFormat12Hour ? TIME_FORMAT_12 : TIME_FORMAT_24;
1148 size_t length = strftime(timeBuff, TIME_LENGTH, timeFormat, timeInfo);
Damien Bargiacchia704b7d2016-02-16 16:55:49 -08001149
1150 if (length != TIME_LENGTH - 1) {
Wei Wang159a4102018-10-23 23:15:58 -07001151 SLOGE("Couldn't format time; abandoning boot animation clock");
Damien Bargiacchia704b7d2016-02-16 16:55:49 -08001152 mClockEnabled = false;
1153 return;
1154 }
1155
Damien Bargiacchi45a76442016-12-05 18:02:18 -08001156 char* out = timeBuff[0] == ' ' ? &timeBuff[1] : &timeBuff[0];
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -07001157 int x = xPos;
1158 int y = yPos;
Damien Bargiacchi45a76442016-12-05 18:02:18 -08001159 drawText(out, font, false, &x, &y);
Damien Bargiacchia704b7d2016-02-16 16:55:49 -08001160}
1161
Nicolas Geoffray41fffb42020-12-15 10:50:29 +00001162void BootAnimation::drawProgress(int percent, const Font& font, const int xPos, const int yPos) {
Parth Sane7f7b2212023-04-12 17:23:02 +00001163 ATRACE_CALL();
Nicolas Geoffray41fffb42020-12-15 10:50:29 +00001164 static constexpr int PERCENT_LENGTH = 5;
1165
1166 char percentBuff[PERCENT_LENGTH];
1167 // ';' has the ascii code just after ':', and the font resource contains '%'
1168 // for that ascii code.
1169 sprintf(percentBuff, "%d;", percent);
1170 int x = xPos;
1171 int y = yPos;
1172 drawText(percentBuff, font, false, &x, &y);
1173}
1174
Marin Shalamanov9070c052020-05-18 19:05:17 +02001175bool BootAnimation::parseAnimationDesc(Animation& animation) {
Parth Sane7f7b2212023-04-12 17:23:02 +00001176 ATRACE_CALL();
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -07001177 String8 desString;
1178
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001179 if (!readFile(animation.zip, "desc.txt", desString)) {
Narayan Kamathafd31e02013-12-03 13:16:03 +00001180 return false;
1181 }
Tomasz Wasilczyk3815d342023-08-10 23:54:44 +00001182 char const* s = desString.c_str();
Shan Huang2b31a362021-08-16 16:45:39 +00001183 std::string dynamicColoringPartName = "";
1184 bool postDynamicColoring = false;
Mathias Agopiana8826d62009-10-01 03:10:14 -07001185
Mathias Agopiana8826d62009-10-01 03:10:14 -07001186 // Parse the description file
1187 for (;;) {
1188 const char* endl = strstr(s, "\n");
Yi Kong911ac232019-03-24 01:49:02 -07001189 if (endl == nullptr) break;
Mathias Agopiana8826d62009-10-01 03:10:14 -07001190 String8 line(s, endl - s);
Tomasz Wasilczyk3815d342023-08-10 23:54:44 +00001191 const char* l = line.c_str();
Damien Bargiacchia704b7d2016-02-16 16:55:49 -08001192 int fps = 0;
1193 int width = 0;
1194 int height = 0;
1195 int count = 0;
1196 int pause = 0;
Nicolas Geoffray41fffb42020-12-15 10:50:29 +00001197 int progress = 0;
Yegor Malyshev6925e1b2020-09-10 16:42:20 +02001198 int framesToFadeCount = 0;
Shan Huang488ff742021-08-25 20:10:46 +00001199 int colorTransitionStart = 0;
1200 int colorTransitionEnd = 0;
Narayan Kamathafd31e02013-12-03 13:16:03 +00001201 char path[ANIM_ENTRY_NAME_MAX];
Jesse Hall083b84c2014-09-22 10:51:09 -07001202 char color[7] = "000000"; // default to black if unspecified
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -07001203 char clockPos1[TEXT_POS_LEN_MAX + 1] = "";
1204 char clockPos2[TEXT_POS_LEN_MAX + 1] = "";
Shan Huang2b31a362021-08-16 16:45:39 +00001205 char dynamicColoringPartNameBuffer[ANIM_ENTRY_NAME_MAX];
Kevin Hesterd3782b22012-04-26 10:38:55 -07001206 char pathType;
Shan Huang2b31a362021-08-16 16:45:39 +00001207 // start colors default to black if unspecified
1208 char start_color_0[7] = "000000";
1209 char start_color_1[7] = "000000";
1210 char start_color_2[7] = "000000";
1211 char start_color_3[7] = "000000";
Yegor Malyshev6925e1b2020-09-10 16:42:20 +02001212
1213 int nextReadPos;
1214
yidong zhang87bb3462022-12-07 15:55:31 +08001215 if (strlen(l) == 0) {
1216 s = ++endl;
1217 continue;
1218 }
1219
Nicolas Geoffray41fffb42020-12-15 10:50:29 +00001220 int topLineNumbers = sscanf(l, "%d %d %d %d", &width, &height, &fps, &progress);
1221 if (topLineNumbers == 3 || topLineNumbers == 4) {
1222 // SLOGD("> w=%d, h=%d, fps=%d, progress=%d", width, height, fps, progress);
Mathias Agopiana8826d62009-10-01 03:10:14 -07001223 animation.width = width;
1224 animation.height = height;
1225 animation.fps = fps;
Nicolas Geoffray41fffb42020-12-15 10:50:29 +00001226 if (topLineNumbers == 4) {
1227 animation.progressEnabled = (progress != 0);
1228 } else {
1229 animation.progressEnabled = false;
1230 }
Shan Huang488ff742021-08-25 20:10:46 +00001231 } else if (sscanf(l, "dynamic_colors %" STRTO(ANIM_PATH_MAX) "s #%6s #%6s #%6s #%6s %d %d",
Shan Huang2b31a362021-08-16 16:45:39 +00001232 dynamicColoringPartNameBuffer,
Shan Huang488ff742021-08-25 20:10:46 +00001233 start_color_0, start_color_1, start_color_2, start_color_3,
1234 &colorTransitionStart, &colorTransitionEnd)) {
Shan Huang2b31a362021-08-16 16:45:39 +00001235 animation.dynamicColoringEnabled = true;
1236 parseColor(start_color_0, animation.startColors[0]);
1237 parseColor(start_color_1, animation.startColors[1]);
1238 parseColor(start_color_2, animation.startColors[2]);
1239 parseColor(start_color_3, animation.startColors[3]);
Shan Huang488ff742021-08-25 20:10:46 +00001240 animation.colorTransitionStart = colorTransitionStart;
1241 animation.colorTransitionEnd = colorTransitionEnd;
Shan Huang2b31a362021-08-16 16:45:39 +00001242 dynamicColoringPartName = std::string(dynamicColoringPartNameBuffer);
Yegor Malyshev6925e1b2020-09-10 16:42:20 +02001243 } else if (sscanf(l, "%c %d %d %" STRTO(ANIM_PATH_MAX) "s%n",
1244 &pathType, &count, &pause, path, &nextReadPos) >= 4) {
1245 if (pathType == 'f') {
1246 sscanf(l + nextReadPos, " %d #%6s %16s %16s", &framesToFadeCount, color, clockPos1,
1247 clockPos2);
1248 } else {
1249 sscanf(l + nextReadPos, " #%6s %16s %16s", color, clockPos1, clockPos2);
1250 }
1251 // SLOGD("> type=%c, count=%d, pause=%d, path=%s, framesToFadeCount=%d, color=%s, "
1252 // "clockPos1=%s, clockPos2=%s",
1253 // pathType, count, pause, path, framesToFadeCount, color, clockPos1, clockPos2);
Mathias Agopiana8826d62009-10-01 03:10:14 -07001254 Animation::Part part;
Shan Huang2b31a362021-08-16 16:45:39 +00001255 if (path == dynamicColoringPartName) {
1256 // Part is specified to use dynamic coloring.
1257 part.useDynamicColoring = true;
1258 part.postDynamicColoring = false;
1259 postDynamicColoring = true;
1260 } else {
1261 // Part does not use dynamic coloring.
1262 part.useDynamicColoring = false;
1263 part.postDynamicColoring = postDynamicColoring;
1264 }
Kevin Hesterd3782b22012-04-26 10:38:55 -07001265 part.playUntilComplete = pathType == 'c';
Yegor Malyshev6925e1b2020-09-10 16:42:20 +02001266 part.framesToFadeCount = framesToFadeCount;
Mathias Agopiana8826d62009-10-01 03:10:14 -07001267 part.count = count;
1268 part.pause = pause;
1269 part.path = path;
Yi Kong911ac232019-03-24 01:49:02 -07001270 part.audioData = nullptr;
1271 part.animation = nullptr;
Jesse Hall083b84c2014-09-22 10:51:09 -07001272 if (!parseColor(color, part.backgroundColor)) {
Wei Wang159a4102018-10-23 23:15:58 -07001273 SLOGE("> invalid color '#%s'", color);
Jesse Hall083b84c2014-09-22 10:51:09 -07001274 part.backgroundColor[0] = 0.0f;
1275 part.backgroundColor[1] = 0.0f;
1276 part.backgroundColor[2] = 0.0f;
1277 }
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -07001278 parsePosition(clockPos1, clockPos2, &part.clockPosX, &part.clockPosY);
Mathias Agopiana8826d62009-10-01 03:10:14 -07001279 animation.parts.add(part);
1280 }
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001281 else if (strcmp(l, "$SYSTEM") == 0) {
Wei Wang159a4102018-10-23 23:15:58 -07001282 // SLOGD("> SYSTEM");
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001283 Animation::Part part;
1284 part.playUntilComplete = false;
Yegor Malyshev6925e1b2020-09-10 16:42:20 +02001285 part.framesToFadeCount = 0;
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001286 part.count = 1;
1287 part.pause = 0;
Yi Kong911ac232019-03-24 01:49:02 -07001288 part.audioData = nullptr;
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001289 part.animation = loadAnimation(String8(SYSTEM_BOOTANIMATION_FILE));
Yi Kong911ac232019-03-24 01:49:02 -07001290 if (part.animation != nullptr)
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001291 animation.parts.add(part);
1292 }
Mathias Agopiana8826d62009-10-01 03:10:14 -07001293 s = ++endl;
1294 }
1295
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001296 return true;
1297}
1298
Marin Shalamanov9070c052020-05-18 19:05:17 +02001299bool BootAnimation::preloadZip(Animation& animation) {
Parth Sane7f7b2212023-04-12 17:23:02 +00001300 ATRACE_CALL();
Mathias Agopiana8826d62009-10-01 03:10:14 -07001301 // read all the data structures
1302 const size_t pcount = animation.parts.size();
Yi Kong911ac232019-03-24 01:49:02 -07001303 void *cookie = nullptr;
Geoffrey Pitschdd214a72016-06-27 17:14:30 -04001304 ZipFileRO* zip = animation.zip;
1305 if (!zip->startIteration(&cookie)) {
Narayan Kamathafd31e02013-12-03 13:16:03 +00001306 return false;
1307 }
1308
1309 ZipEntryRO entry;
1310 char name[ANIM_ENTRY_NAME_MAX];
Yi Kong911ac232019-03-24 01:49:02 -07001311 while ((entry = zip->nextEntry(cookie)) != nullptr) {
Geoffrey Pitschdd214a72016-06-27 17:14:30 -04001312 const int foundEntryName = zip->getEntryFileName(entry, name, ANIM_ENTRY_NAME_MAX);
Narayan Kamathafd31e02013-12-03 13:16:03 +00001313 if (foundEntryName > ANIM_ENTRY_NAME_MAX || foundEntryName == -1) {
Wei Wang159a4102018-10-23 23:15:58 -07001314 SLOGE("Error fetching entry file name");
Narayan Kamathafd31e02013-12-03 13:16:03 +00001315 continue;
1316 }
1317
Tomasz Wasilczyk9dcea572023-08-22 20:39:20 +00001318 const std::filesystem::path entryName(name);
1319 const std::filesystem::path path(entryName.parent_path());
1320 const std::filesystem::path leaf(entryName.filename());
1321 if (!leaf.empty()) {
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -07001322 if (entryName == CLOCK_FONT_ZIP_NAME) {
1323 FileMap* map = zip->createEntryFileMap(entry);
1324 if (map) {
1325 animation.clockFont.map = map;
1326 }
1327 continue;
1328 }
1329
Nicolas Geoffray41fffb42020-12-15 10:50:29 +00001330 if (entryName == PROGRESS_FONT_ZIP_NAME) {
1331 FileMap* map = zip->createEntryFileMap(entry);
1332 if (map) {
1333 animation.progressFont.map = map;
1334 }
1335 continue;
1336 }
1337
Geoffrey Pitschdd214a72016-06-27 17:14:30 -04001338 for (size_t j = 0; j < pcount; j++) {
Tomasz Wasilczyk9dcea572023-08-22 20:39:20 +00001339 if (path.string() == animation.parts[j].path.c_str()) {
Narayan Kamath4600dd02015-06-16 12:02:57 +01001340 uint16_t method;
Narayan Kamathafd31e02013-12-03 13:16:03 +00001341 // supports only stored png files
Pawan Wagh8efc3c42024-06-03 21:43:13 +00001342 if (zip->getEntryInfo(entry, &method, nullptr, nullptr, nullptr, nullptr,
1343 nullptr, nullptr)) {
Narayan Kamathafd31e02013-12-03 13:16:03 +00001344 if (method == ZipFileRO::kCompressStored) {
Geoffrey Pitschdd214a72016-06-27 17:14:30 -04001345 FileMap* map = zip->createEntryFileMap(entry);
Narayan Kamathafd31e02013-12-03 13:16:03 +00001346 if (map) {
Narayan Kamathafd31e02013-12-03 13:16:03 +00001347 Animation::Part& part(animation.parts.editItemAt(j));
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -07001348 if (leaf == "audio.wav") {
1349 // a part may have at most one audio file
Geoffrey Pitschd6d9a1d2016-06-08 00:38:58 -07001350 part.audioData = (uint8_t *)map->getDataPtr();
1351 part.audioLength = map->getDataLength();
Geoffrey Pitschdd214a72016-06-27 17:14:30 -04001352 } else if (leaf == "trim.txt") {
Tomasz Wasilczyk31eb3c892023-08-23 22:12:33 +00001353 part.trimData = String8((char const*)map->getDataPtr(),
Geoffrey Pitschdd214a72016-06-27 17:14:30 -04001354 map->getDataLength());
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -07001355 } else {
1356 Animation::Frame frame;
Tomasz Wasilczyk9dcea572023-08-22 20:39:20 +00001357 frame.name = leaf.c_str();
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -07001358 frame.map = map;
Geoffrey Pitschdd214a72016-06-27 17:14:30 -04001359 frame.trimWidth = animation.width;
1360 frame.trimHeight = animation.height;
1361 frame.trimX = 0;
1362 frame.trimY = 0;
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -07001363 part.frames.add(frame);
1364 }
Mathias Agopiana8826d62009-10-01 03:10:14 -07001365 }
Geoffrey Pitschd6d9a1d2016-06-08 00:38:58 -07001366 } else {
Wei Wang159a4102018-10-23 23:15:58 -07001367 SLOGE("bootanimation.zip is compressed; must be only stored");
Mathias Agopiana8826d62009-10-01 03:10:14 -07001368 }
1369 }
1370 }
1371 }
1372 }
1373 }
1374
Geoffrey Pitschdd214a72016-06-27 17:14:30 -04001375 // If there is trimData present, override the positioning defaults.
1376 for (Animation::Part& part : animation.parts) {
Tomasz Wasilczyk3815d342023-08-10 23:54:44 +00001377 const char* trimDataStr = part.trimData.c_str();
Geoffrey Pitschdd214a72016-06-27 17:14:30 -04001378 for (size_t frameIdx = 0; frameIdx < part.frames.size(); frameIdx++) {
1379 const char* endl = strstr(trimDataStr, "\n");
1380 // No more trimData for this part.
Yi Kong911ac232019-03-24 01:49:02 -07001381 if (endl == nullptr) {
Geoffrey Pitschdd214a72016-06-27 17:14:30 -04001382 break;
1383 }
1384 String8 line(trimDataStr, endl - trimDataStr);
Tomasz Wasilczyk3815d342023-08-10 23:54:44 +00001385 const char* lineStr = line.c_str();
Geoffrey Pitschdd214a72016-06-27 17:14:30 -04001386 trimDataStr = ++endl;
1387 int width = 0, height = 0, x = 0, y = 0;
1388 if (sscanf(lineStr, "%dx%d+%d+%d", &width, &height, &x, &y) == 4) {
1389 Animation::Frame& frame(part.frames.editItemAt(frameIdx));
1390 frame.trimWidth = width;
1391 frame.trimHeight = height;
1392 frame.trimX = x;
1393 frame.trimY = y;
1394 } else {
Wei Wang159a4102018-10-23 23:15:58 -07001395 SLOGE("Error parsing trim.txt, line: %s", lineStr);
Geoffrey Pitschdd214a72016-06-27 17:14:30 -04001396 break;
1397 }
1398 }
1399 }
1400
1401 zip->endIteration(cookie);
Narayan Kamathafd31e02013-12-03 13:16:03 +00001402
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001403 return true;
1404}
1405
Marin Shalamanov9070c052020-05-18 19:05:17 +02001406bool BootAnimation::movie() {
Parth Sane7f7b2212023-04-12 17:23:02 +00001407 ATRACE_CALL();
Huihong Luo50a2f362018-08-11 09:27:57 -07001408 if (mAnimation == nullptr) {
1409 mAnimation = loadAnimation(mZipFileName);
1410 }
1411
1412 if (mAnimation == nullptr)
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001413 return false;
1414
Huihong Luo50a2f362018-08-11 09:27:57 -07001415 // mCallbacks->init() may get called recursively,
1416 // this loop is needed to get the same results
1417 for (const Animation::Part& part : mAnimation->parts) {
1418 if (part.animation != nullptr) {
1419 mCallbacks->init(part.animation->parts);
1420 }
1421 }
1422 mCallbacks->init(mAnimation->parts);
1423
Damien Bargiacchi97480862016-03-29 14:55:55 -07001424 bool anyPartHasClock = false;
Huihong Luo50a2f362018-08-11 09:27:57 -07001425 for (size_t i=0; i < mAnimation->parts.size(); i++) {
1426 if(validClock(mAnimation->parts[i])) {
Damien Bargiacchi97480862016-03-29 14:55:55 -07001427 anyPartHasClock = true;
1428 break;
1429 }
1430 }
1431 if (!anyPartHasClock) {
1432 mClockEnabled = false;
Thiébaud Weksteen08de4432021-11-18 13:25:59 +11001433 } else if (!android::base::GetBoolProperty(CLOCK_ENABLED_PROP_NAME, false)) {
1434 mClockEnabled = false;
Damien Bargiacchi97480862016-03-29 14:55:55 -07001435 }
1436
Sai Kiran Korwar27167492015-07-07 20:00:06 +05301437 // Check if npot textures are supported
1438 mUseNpotTextures = false;
1439 String8 gl_extensions;
1440 const char* exts = reinterpret_cast<const char*>(glGetString(GL_EXTENSIONS));
1441 if (!exts) {
1442 glGetError();
1443 } else {
Tomasz Wasilczyk31eb3c892023-08-23 22:12:33 +00001444 gl_extensions = exts;
Sai Kiran Korwar27167492015-07-07 20:00:06 +05301445 if ((gl_extensions.find("GL_ARB_texture_non_power_of_two") != -1) ||
1446 (gl_extensions.find("GL_OES_texture_npot") != -1)) {
1447 mUseNpotTextures = true;
1448 }
1449 }
1450
Damien Bargiacchia704b7d2016-02-16 16:55:49 -08001451 // Blend required to draw time on top of animation frames.
1452 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
Mathias Agopiana8826d62009-10-01 03:10:14 -07001453 glDisable(GL_DITHER);
1454 glDisable(GL_SCISSOR_TEST);
1455 glDisable(GL_BLEND);
Mathias Agopiana8826d62009-10-01 03:10:14 -07001456
Greg Kaiser2ded8002021-07-19 17:03:27 +00001457 glEnable(GL_TEXTURE_2D);
Lucas Dupin8eedc022021-07-29 21:49:07 +00001458 glBindTexture(GL_TEXTURE_2D, 0);
1459 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
1460 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
1461 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
1462 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -07001463 bool clockFontInitialized = false;
Damien Bargiacchia704b7d2016-02-16 16:55:49 -08001464 if (mClockEnabled) {
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -07001465 clockFontInitialized =
Huihong Luo50a2f362018-08-11 09:27:57 -07001466 (initFont(&mAnimation->clockFont, CLOCK_FONT_ASSET) == NO_ERROR);
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -07001467 mClockEnabled = clockFontInitialized;
Damien Bargiacchia704b7d2016-02-16 16:55:49 -08001468 }
1469
Nicolas Geoffray41fffb42020-12-15 10:50:29 +00001470 initFont(&mAnimation->progressFont, PROGRESS_FONT_ASSET);
1471
Damien Bargiacchi97480862016-03-29 14:55:55 -07001472 if (mClockEnabled && !updateIsTimeAccurate()) {
1473 mTimeCheckThread = new TimeCheckThread(this);
1474 mTimeCheckThread->run("BootAnimation::TimeCheckThread", PRIORITY_NORMAL);
1475 }
1476
George Burgess IV0f8e8112021-08-23 17:32:23 -07001477 if (mAnimation->dynamicColoringEnabled) {
Shan Huang7fee3522021-08-18 00:56:08 +00001478 initDynamicColors();
1479 }
1480
Huihong Luo50a2f362018-08-11 09:27:57 -07001481 playAnimation(*mAnimation);
Damien Bargiacchi97480862016-03-29 14:55:55 -07001482
Geoffrey Pitscha9173532016-07-19 14:56:39 -04001483 if (mTimeCheckThread != nullptr) {
Damien Bargiacchi97480862016-03-29 14:55:55 -07001484 mTimeCheckThread->requestExit();
Geoffrey Pitscha9173532016-07-19 14:56:39 -04001485 mTimeCheckThread = nullptr;
1486 }
1487
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -07001488 if (clockFontInitialized) {
Huihong Luo50a2f362018-08-11 09:27:57 -07001489 glDeleteTextures(1, &mAnimation->clockFont.texture.name);
Andriy Naborskyy815e51d2016-03-24 16:43:34 -07001490 }
1491
Huihong Luo50a2f362018-08-11 09:27:57 -07001492 releaseAnimation(mAnimation);
1493 mAnimation = nullptr;
Damien Bargiacchi1a8a2132018-07-24 15:20:26 -07001494
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001495 return false;
1496}
1497
Nicolas Geoffraya1298752021-04-20 15:15:06 +01001498bool BootAnimation::shouldStopPlayingPart(const Animation::Part& part,
1499 const int fadedFramesCount,
1500 const int lastDisplayedProgress) {
Parth Sane7f7b2212023-04-12 17:23:02 +00001501 ATRACE_CALL();
Yegor Malyshev6925e1b2020-09-10 16:42:20 +02001502 // 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 +01001503 return exitPending() && !part.playUntilComplete && fadedFramesCount >= part.framesToFadeCount &&
1504 (lastDisplayedProgress == 0 || lastDisplayedProgress == 100);
Yegor Malyshev6925e1b2020-09-10 16:42:20 +02001505}
1506
Lucas Dupin8eedc022021-07-29 21:49:07 +00001507// Linear mapping from range <a1, a2> to range <b1, b2>
1508float mapLinear(float x, float a1, float a2, float b1, float b2) {
1509 return b1 + ( x - a1 ) * ( b2 - b1 ) / ( a2 - a1 );
1510}
1511
1512void BootAnimation::drawTexturedQuad(float xStart, float yStart, float width, float height) {
Parth Sane7f7b2212023-04-12 17:23:02 +00001513 ATRACE_CALL();
Lucas Dupin8eedc022021-07-29 21:49:07 +00001514 // Map coordinates from screen space to world space.
1515 float x0 = mapLinear(xStart, 0, mWidth, -1, 1);
1516 float y0 = mapLinear(yStart, 0, mHeight, -1, 1);
1517 float x1 = mapLinear(xStart + width, 0, mWidth, -1, 1);
1518 float y1 = mapLinear(yStart + height, 0, mHeight, -1, 1);
1519 // Update quad vertex positions.
1520 quadPositions[0] = x0;
1521 quadPositions[1] = y0;
1522 quadPositions[2] = x1;
1523 quadPositions[3] = y0;
1524 quadPositions[4] = x1;
1525 quadPositions[5] = y1;
1526 quadPositions[6] = x1;
1527 quadPositions[7] = y1;
1528 quadPositions[8] = x0;
1529 quadPositions[9] = y1;
1530 quadPositions[10] = x0;
1531 quadPositions[11] = y0;
1532 glDrawArrays(GL_TRIANGLES, 0,
1533 sizeof(quadPositions) / sizeof(quadPositions[0]) / 2);
1534}
1535
Shan Huang7fee3522021-08-18 00:56:08 +00001536void BootAnimation::initDynamicColors() {
Parth Sane7f7b2212023-04-12 17:23:02 +00001537 ATRACE_CALL();
Shan Huang7fee3522021-08-18 00:56:08 +00001538 for (int i = 0; i < DYNAMIC_COLOR_COUNT; i++) {
Lucas Dupincd256842022-03-29 15:46:36 -07001539 const auto syspropName = "persist.bootanim.color" + std::to_string(i + 1);
1540 const auto syspropValue = android::base::GetProperty(syspropName, "");
1541 if (syspropValue != "") {
1542 SLOGI("Loaded dynamic color: %s -> %s", syspropName.c_str(), syspropValue.c_str());
1543 mDynamicColorsApplied = true;
1544 }
1545 parseColorDecimalString(syspropValue,
Shan Huang7fee3522021-08-18 00:56:08 +00001546 mAnimation->endColors[i], mAnimation->startColors[i]);
1547 }
1548 glUseProgram(mImageShader);
Lucas Dupincd256842022-03-29 15:46:36 -07001549 SLOGI("Dynamically coloring boot animation. Sysprops loaded? %i", mDynamicColorsApplied);
Shan Huang7fee3522021-08-18 00:56:08 +00001550 for (int i = 0; i < DYNAMIC_COLOR_COUNT; i++) {
1551 float *startColor = mAnimation->startColors[i];
1552 float *endColor = mAnimation->endColors[i];
Andrew Zengf9f58222022-11-08 11:10:48 -08001553 glUniform3f(glGetUniformLocation(mImageShader,
Shan Huang7fee3522021-08-18 00:56:08 +00001554 (U_START_COLOR_PREFIX + std::to_string(i)).c_str()),
Andrew Zengf9f58222022-11-08 11:10:48 -08001555 startColor[0], startColor[1], startColor[2]);
1556 glUniform3f(glGetUniformLocation(mImageShader,
Shan Huang7fee3522021-08-18 00:56:08 +00001557 (U_END_COLOR_PREFIX + std::to_string(i)).c_str()),
Andrew Zengf9f58222022-11-08 11:10:48 -08001558 endColor[0], endColor[1], endColor[2]);
Shan Huang7fee3522021-08-18 00:56:08 +00001559 }
1560 mImageColorProgressLocation = glGetUniformLocation(mImageShader, U_COLOR_PROGRESS);
1561}
1562
Marin Shalamanov9070c052020-05-18 19:05:17 +02001563bool BootAnimation::playAnimation(const Animation& animation) {
Parth Sane7f7b2212023-04-12 17:23:02 +00001564 ATRACE_CALL();
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001565 const size_t pcount = animation.parts.size();
Mathias Agopiana8826d62009-10-01 03:10:14 -07001566 nsecs_t frameDuration = s2ns(1) / animation.fps;
Mathias Agopian9f3020d2009-11-06 16:30:18 -08001567
Wei Wang159a4102018-10-23 23:15:58 -07001568 SLOGD("%sAnimationShownTiming start time: %" PRId64 "ms", mShuttingDown ? "Shutdown" : "Boot",
Keun-young Parkd1794cd2017-04-04 12:21:19 -07001569 elapsedRealtime());
Yegor Malyshev6925e1b2020-09-10 16:42:20 +02001570
1571 int fadedFramesCount = 0;
Nicolas Geoffray41fffb42020-12-15 10:50:29 +00001572 int lastDisplayedProgress = 0;
Lucas Dupinbfd18122022-04-25 11:26:08 -07001573 int colorTransitionStart = animation.colorTransitionStart;
1574 int colorTransitionEnd = animation.colorTransitionEnd;
Narayan Kamathafd31e02013-12-03 13:16:03 +00001575 for (size_t i=0 ; i<pcount ; i++) {
Mathias Agopiana8826d62009-10-01 03:10:14 -07001576 const Animation::Part& part(animation.parts[i]);
1577 const size_t fcount = part.frames.size();
Oleg Lyovin6a4b84d2023-07-01 14:39:59 +03001578 glBindTexture(GL_TEXTURE_2D, 0);
Mathias Agopiana8826d62009-10-01 03:10:14 -07001579
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001580 // Handle animation package
Yi Kong911ac232019-03-24 01:49:02 -07001581 if (part.animation != nullptr) {
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001582 playAnimation(*part.animation);
1583 if (exitPending())
1584 break;
1585 continue; //to next part
1586 }
1587
Yegor Malyshev6925e1b2020-09-10 16:42:20 +02001588 // process the part not only while the count allows but also if already fading
1589 for (int r=0 ; !part.count || r<part.count || fadedFramesCount > 0 ; r++) {
Nicolas Geoffraya1298752021-04-20 15:15:06 +01001590 if (shouldStopPlayingPart(part, fadedFramesCount, lastDisplayedProgress)) break;
Kevin Hesterd3782b22012-04-26 10:38:55 -07001591
Lucas Dupinbfd18122022-04-25 11:26:08 -07001592 // It's possible that the sysprops were not loaded yet at this boot phase.
1593 // If that's the case, then we should keep trying until they are available.
1594 if (animation.dynamicColoringEnabled && !mDynamicColorsApplied
1595 && (part.useDynamicColoring || part.postDynamicColoring)) {
1596 SLOGD("Trying to load dynamic color sysprops.");
1597 initDynamicColors();
1598 if (mDynamicColorsApplied) {
1599 // Sysprops were loaded. Next step is to adjust the animation if we loaded
1600 // the colors after the animation should have started.
1601 const int transitionLength = colorTransitionEnd - colorTransitionStart;
1602 if (part.postDynamicColoring) {
1603 colorTransitionStart = 0;
1604 colorTransitionEnd = fmin(transitionLength, fcount - 1);
1605 }
1606 }
1607 }
1608
Ed Coyne7464ac92017-06-08 12:26:48 -07001609 mCallbacks->playPart(i, part, r);
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -07001610
Jesse Hall083b84c2014-09-22 10:51:09 -07001611 glClearColor(
1612 part.backgroundColor[0],
1613 part.backgroundColor[1],
1614 part.backgroundColor[2],
1615 1.0f);
1616
Yoshiaki Maruoka9ef19b82017-10-25 13:55:49 +09001617 ALOGD("Playing files = %s/%s, Requested repeat = %d, playUntilComplete = %s",
Tomasz Wasilczyk3815d342023-08-10 23:54:44 +00001618 animation.fileName.c_str(), part.path.c_str(), part.count,
Yoshiaki Maruoka9ef19b82017-10-25 13:55:49 +09001619 part.playUntilComplete ? "true" : "false");
1620
Nicolas Geoffray41fffb42020-12-15 10:50:29 +00001621 // For the last animation, if we have progress indicator from
1622 // the system, display it.
1623 int currentProgress = android::base::GetIntProperty(PROGRESS_PROP_NAME, 0);
1624 bool displayProgress = animation.progressEnabled &&
1625 (i == (pcount -1)) && currentProgress != 0;
1626
Yegor Malyshev6925e1b2020-09-10 16:42:20 +02001627 for (size_t j=0 ; j<fcount ; j++) {
Nicolas Geoffraya1298752021-04-20 15:15:06 +01001628 if (shouldStopPlayingPart(part, fadedFramesCount, lastDisplayedProgress)) break;
Yegor Malyshev6925e1b2020-09-10 16:42:20 +02001629
Shan Huang2b31a362021-08-16 16:45:39 +00001630 // Color progress is
Shan Huang488ff742021-08-25 20:10:46 +00001631 // - the animation progress, normalized from
1632 // [colorTransitionStart,colorTransitionEnd] to [0, 1] for the dynamic coloring
1633 // part.
Shan Huang2b31a362021-08-16 16:45:39 +00001634 // - 0 for parts that come before,
1635 // - 1 for parts that come after.
1636 float colorProgress = part.useDynamicColoring
Shan Huang488ff742021-08-25 20:10:46 +00001637 ? fmin(fmax(
Lucas Dupinbfd18122022-04-25 11:26:08 -07001638 ((float)j - colorTransitionStart) /
1639 fmax(colorTransitionEnd - colorTransitionStart, 1.0f), 0.0f), 1.0f)
Shan Huang2b31a362021-08-16 16:45:39 +00001640 : (part.postDynamicColoring ? 1 : 0);
1641
Marin Shalamanov9070c052020-05-18 19:05:17 +02001642 processDisplayEvents();
1643
joenchen85b339f2022-04-11 23:58:34 +08001644 const double ratio_w = static_cast<double>(mWidth) / mInitWidth;
1645 const double ratio_h = static_cast<double>(mHeight) / mInitHeight;
1646 const int animationX = (mWidth - animation.width * ratio_w) / 2;
1647 const int animationY = (mHeight - animation.height * ratio_h) / 2;
Marin Shalamanov9070c052020-05-18 19:05:17 +02001648
Mathias Agopiana8826d62009-10-01 03:10:14 -07001649 const Animation::Frame& frame(part.frames[j]);
Mathias Agopiandb7dd2a2012-05-12 15:08:21 -07001650 nsecs_t lastFrame = systemTime();
Mathias Agopiana8826d62009-10-01 03:10:14 -07001651
1652 if (r > 0) {
1653 glBindTexture(GL_TEXTURE_2D, frame.tid);
1654 } else {
Oleg Lyovin6a4b84d2023-07-01 14:39:59 +03001655 if (part.count != 1) {
1656 glGenTextures(1, &frame.tid);
1657 glBindTexture(GL_TEXTURE_2D, frame.tid);
1658 }
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -07001659 int w, h;
Shan Huang2b31a362021-08-16 16:45:39 +00001660 // Set decoding option to alpha unpremultiplied so that the R, G, B channels
1661 // of transparent pixels are preserved.
1662 initTexture(frame.map, &w, &h, false /* don't premultiply alpha */);
Mathias Agopiana8826d62009-10-01 03:10:14 -07001663 }
1664
joenchen85b339f2022-04-11 23:58:34 +08001665 const int trimWidth = frame.trimWidth * ratio_w;
1666 const int trimHeight = frame.trimHeight * ratio_h;
1667 const int trimX = frame.trimX * ratio_w;
1668 const int trimY = frame.trimY * ratio_h;
1669 const int xc = animationX + trimX;
1670 const int yc = animationY + trimY;
Shan Huang2b31a362021-08-16 16:45:39 +00001671 glClear(GL_COLOR_BUFFER_BIT);
Geoffrey Pitschdd214a72016-06-27 17:14:30 -04001672 // specify the y center as ceiling((mHeight - frame.trimHeight) / 2)
1673 // which is equivalent to mHeight - (yc + frame.trimHeight)
joenchen85b339f2022-04-11 23:58:34 +08001674 const int frameDrawY = mHeight - (yc + trimHeight);
Yegor Malyshev6925e1b2020-09-10 16:42:20 +02001675
Lucas Dupin8eedc022021-07-29 21:49:07 +00001676 float fade = 0;
Yegor Malyshev6925e1b2020-09-10 16:42:20 +02001677 // if the part hasn't been stopped yet then continue fading if necessary
1678 if (exitPending() && part.hasFadingPhase()) {
Lucas Dupin8eedc022021-07-29 21:49:07 +00001679 fade = static_cast<float>(++fadedFramesCount) / part.framesToFadeCount;
Yegor Malyshev6925e1b2020-09-10 16:42:20 +02001680 if (fadedFramesCount >= part.framesToFadeCount) {
1681 fadedFramesCount = MAX_FADED_FRAMES_COUNT; // no more fading
1682 }
1683 }
Lucas Dupin8eedc022021-07-29 21:49:07 +00001684 glUseProgram(mImageShader);
1685 glUniform1i(mImageTextureLocation, 0);
1686 glUniform1f(mImageFadeLocation, fade);
Shan Huang2b31a362021-08-16 16:45:39 +00001687 if (animation.dynamicColoringEnabled) {
1688 glUniform1f(mImageColorProgressLocation, colorProgress);
1689 }
Lucas Dupin8eedc022021-07-29 21:49:07 +00001690 glEnable(GL_BLEND);
joenchen85b339f2022-04-11 23:58:34 +08001691 drawTexturedQuad(xc, frameDrawY, trimWidth, trimHeight);
Lucas Dupin8eedc022021-07-29 21:49:07 +00001692 glDisable(GL_BLEND);
Yegor Malyshev6925e1b2020-09-10 16:42:20 +02001693
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -07001694 if (mClockEnabled && mTimeIsAccurate && validClock(part)) {
1695 drawClock(animation.clockFont, part.clockPosX, part.clockPosY);
Damien Bargiacchia704b7d2016-02-16 16:55:49 -08001696 }
Yegor Malyshev6925e1b2020-09-10 16:42:20 +02001697
Nicolas Geoffray41fffb42020-12-15 10:50:29 +00001698 if (displayProgress) {
1699 int newProgress = android::base::GetIntProperty(PROGRESS_PROP_NAME, 0);
1700 // In case the new progress jumped suddenly, still show an
1701 // increment of 1.
1702 if (lastDisplayedProgress != 100) {
1703 // Artificially sleep 1/10th a second to slow down the animation.
1704 usleep(100000);
1705 if (lastDisplayedProgress < newProgress) {
1706 lastDisplayedProgress++;
1707 }
1708 }
1709 // Put the progress percentage right below the animation.
1710 int posY = animation.height / 3;
1711 int posX = TEXT_CENTER_VALUE;
1712 drawProgress(lastDisplayedProgress, animation.progressFont, posX, posY);
1713 }
1714
Adrian Roos9ee5dff2018-08-22 20:19:49 +02001715 handleViewport(frameDuration);
Damien Bargiacchia704b7d2016-02-16 16:55:49 -08001716
Mathias Agopiana8826d62009-10-01 03:10:14 -07001717 eglSwapBuffers(mDisplay, mSurface);
1718
1719 nsecs_t now = systemTime();
1720 nsecs_t delay = frameDuration - (now - lastFrame);
Wei Wang159a4102018-10-23 23:15:58 -07001721 //SLOGD("%lld, %lld", ns2ms(now - lastFrame), ns2ms(delay));
Mathias Agopiana8826d62009-10-01 03:10:14 -07001722 lastFrame = now;
Mathias Agopiandb7dd2a2012-05-12 15:08:21 -07001723
1724 if (delay > 0) {
1725 struct timespec spec;
1726 spec.tv_sec = (now + delay) / 1000000000;
1727 spec.tv_nsec = (now + delay) % 1000000000;
1728 int err;
1729 do {
Yi Kong911ac232019-03-24 01:49:02 -07001730 err = clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &spec, nullptr);
Keith Mok0f2e6682021-11-15 05:50:32 +00001731 } while (err == EINTR);
Mathias Agopiandb7dd2a2012-05-12 15:08:21 -07001732 }
Kevin Hesterd3782b22012-04-26 10:38:55 -07001733
1734 checkExit();
Mathias Agopiana8826d62009-10-01 03:10:14 -07001735 }
Kevin Hesterd3782b22012-04-26 10:38:55 -07001736
niuwenchao9cd6ac82022-12-23 17:34:22 +08001737 int pauseDuration = part.pause * ns2us(frameDuration);
1738 while(pauseDuration > 0 && !exitPending()){
1739 if (pauseDuration > MAX_CHECK_EXIT_INTERVAL_US) {
1740 usleep(MAX_CHECK_EXIT_INTERVAL_US);
1741 pauseDuration -= MAX_CHECK_EXIT_INTERVAL_US;
1742 } else {
1743 usleep(pauseDuration);
1744 break;
1745 }
1746 checkExit();
1747 }
Kevin Hesterd3782b22012-04-26 10:38:55 -07001748
Yegor Malyshev6925e1b2020-09-10 16:42:20 +02001749 if (exitPending() && !part.count && mCurrentInset >= mTargetInset &&
1750 !part.hasFadingPhase()) {
Nicolas Geoffraya1298752021-04-20 15:15:06 +01001751 if (lastDisplayedProgress != 0 && lastDisplayedProgress != 100) {
1752 android::base::SetProperty(PROGRESS_PROP_NAME, "100");
1753 continue;
1754 }
Yegor Malyshev6925e1b2020-09-10 16:42:20 +02001755 break; // exit the infinite non-fading part when it has been played at least once
1756 }
Mathias Agopiana8826d62009-10-01 03:10:14 -07001757 }
Geoffrey Pitsch2fb30fb2016-07-06 16:16:20 -04001758 }
1759
1760 // Free textures created for looping parts now that the animation is done.
1761 for (const Animation::Part& part : animation.parts) {
Mathias Agopiana8826d62009-10-01 03:10:14 -07001762 if (part.count != 1) {
Geoffrey Pitsch2fb30fb2016-07-06 16:16:20 -04001763 const size_t fcount = part.frames.size();
1764 for (size_t j = 0; j < fcount; j++) {
Mathias Agopiana8826d62009-10-01 03:10:14 -07001765 const Animation::Frame& frame(part.frames[j]);
1766 glDeleteTextures(1, &frame.tid);
1767 }
1768 }
1769 }
Geoffrey Pitschd6d9a1d2016-06-08 00:38:58 -07001770
Yoshiaki Maruoka9ef19b82017-10-25 13:55:49 +09001771 ALOGD("%sAnimationShownTiming End time: %" PRId64 "ms", mShuttingDown ? "Shutdown" : "Boot",
1772 elapsedRealtime());
1773
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001774 return true;
Mathias Agopiana8826d62009-10-01 03:10:14 -07001775}
1776
Marin Shalamanov9070c052020-05-18 19:05:17 +02001777void BootAnimation::processDisplayEvents() {
Parth Sane7f7b2212023-04-12 17:23:02 +00001778 ATRACE_CALL();
Marin Shalamanov9070c052020-05-18 19:05:17 +02001779 // This will poll mDisplayEventReceiver and if there are new events it'll call
1780 // displayEventCallback synchronously.
1781 mLooper->pollOnce(0);
1782}
1783
Adrian Roos9ee5dff2018-08-22 20:19:49 +02001784void BootAnimation::handleViewport(nsecs_t timestep) {
Parth Sane7f7b2212023-04-12 17:23:02 +00001785 ATRACE_CALL();
Adrian Roos9ee5dff2018-08-22 20:19:49 +02001786 if (mShuttingDown || !mFlingerSurfaceControl || mTargetInset == 0) {
1787 return;
1788 }
1789 if (mTargetInset < 0) {
1790 // Poll the amount for the top display inset. This will return -1 until persistent properties
1791 // have been loaded.
1792 mTargetInset = android::base::GetIntProperty("persist.sys.displayinset.top",
1793 -1 /* default */, -1 /* min */, mHeight / 2 /* max */);
1794 }
1795 if (mTargetInset <= 0) {
1796 return;
1797 }
1798
1799 if (mCurrentInset < mTargetInset) {
1800 // After the device boots, the inset will effectively be cropped away. We animate this here.
1801 float fraction = static_cast<float>(mCurrentInset) / mTargetInset;
1802 int interpolatedInset = (cosf((fraction + 1) * M_PI) / 2.0f + 0.5f) * mTargetInset;
1803
1804 SurfaceComposerClient::Transaction()
1805 .setCrop(mFlingerSurfaceControl, Rect(0, interpolatedInset, mWidth, mHeight))
1806 .apply();
1807 } else {
1808 // At the end of the animation, we switch to the viewport that DisplayManager will apply
1809 // later. This changes the coordinate system, and means we must move the surface up by
1810 // the inset amount.
Adrian Roos9ee5dff2018-08-22 20:19:49 +02001811 Rect layerStackRect(0, 0, mWidth, mHeight - mTargetInset);
1812 Rect displayRect(0, mTargetInset, mWidth, mHeight);
1813
1814 SurfaceComposerClient::Transaction t;
1815 t.setPosition(mFlingerSurfaceControl, 0, -mTargetInset)
1816 .setCrop(mFlingerSurfaceControl, Rect(0, mTargetInset, mWidth, mHeight));
Dominik Laskowski2a3d9aa2019-11-18 20:26:39 -08001817 t.setDisplayProjection(mDisplayToken, ui::ROTATION_0, layerStackRect, displayRect);
Adrian Roos9ee5dff2018-08-22 20:19:49 +02001818 t.apply();
1819
1820 mTargetInset = mCurrentInset = 0;
1821 }
1822
1823 int delta = timestep * mTargetInset / ms2ns(200);
1824 mCurrentInset += delta;
1825}
1826
Marin Shalamanov9070c052020-05-18 19:05:17 +02001827void BootAnimation::releaseAnimation(Animation* animation) const {
Parth Sane7f7b2212023-04-12 17:23:02 +00001828 ATRACE_CALL();
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001829 for (Vector<Animation::Part>::iterator it = animation->parts.begin(),
1830 e = animation->parts.end(); it != e; ++it) {
1831 if (it->animation)
1832 releaseAnimation(it->animation);
1833 }
1834 if (animation->zip)
1835 delete animation->zip;
1836 delete animation;
1837}
1838
Marin Shalamanov9070c052020-05-18 19:05:17 +02001839BootAnimation::Animation* BootAnimation::loadAnimation(const String8& fn) {
Parth Sane7f7b2212023-04-12 17:23:02 +00001840 ATRACE_CALL();
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001841 if (mLoadedFiles.indexOf(fn) >= 0) {
Wei Wang159a4102018-10-23 23:15:58 -07001842 SLOGE("File \"%s\" is already loaded. Cyclic ref is not allowed",
Tomasz Wasilczyk3815d342023-08-10 23:54:44 +00001843 fn.c_str());
Yi Kong911ac232019-03-24 01:49:02 -07001844 return nullptr;
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001845 }
Tomasz Wasilczyk835dfe52023-08-17 16:27:22 +00001846 ZipFileRO *zip = ZipFileRO::open(fn.c_str());
Yi Kong911ac232019-03-24 01:49:02 -07001847 if (zip == nullptr) {
Wei Wang159a4102018-10-23 23:15:58 -07001848 SLOGE("Failed to open animation zip \"%s\": %s",
Tomasz Wasilczyk3815d342023-08-10 23:54:44 +00001849 fn.c_str(), strerror(errno));
Yi Kong911ac232019-03-24 01:49:02 -07001850 return nullptr;
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001851 }
1852
Tomasz Wasilczyk3815d342023-08-10 23:54:44 +00001853 ALOGD("%s is loaded successfully", fn.c_str());
Yoshiaki Maruoka9ef19b82017-10-25 13:55:49 +09001854
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001855 Animation *animation = new Animation;
1856 animation->fileName = fn;
1857 animation->zip = zip;
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -07001858 animation->clockFont.map = nullptr;
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001859 mLoadedFiles.add(animation->fileName);
1860
1861 parseAnimationDesc(*animation);
Geoffrey Pitscha91a2d72016-07-12 14:46:19 -04001862 if (!preloadZip(*animation)) {
Greg Kaiser7426ec82019-09-11 14:34:27 -07001863 releaseAnimation(animation);
Yi Kong911ac232019-03-24 01:49:02 -07001864 return nullptr;
Geoffrey Pitscha91a2d72016-07-12 14:46:19 -04001865 }
1866
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001867 mLoadedFiles.remove(fn);
1868 return animation;
1869}
Damien Bargiacchi97480862016-03-29 14:55:55 -07001870
1871bool BootAnimation::updateIsTimeAccurate() {
Parth Sane7f7b2212023-04-12 17:23:02 +00001872 ATRACE_CALL();
Damien Bargiacchi97480862016-03-29 14:55:55 -07001873 static constexpr long long MAX_TIME_IN_PAST = 60000LL * 60LL * 24LL * 30LL; // 30 days
1874 static constexpr long long MAX_TIME_IN_FUTURE = 60000LL * 90LL; // 90 minutes
1875
1876 if (mTimeIsAccurate) {
1877 return true;
1878 }
Keun-young Parkb5938422017-03-23 13:46:24 -07001879 if (mShuttingDown) return true;
Damien Bargiacchi97480862016-03-29 14:55:55 -07001880 struct stat statResult;
Damien Bargiacchi9071db12016-10-28 17:38:22 -07001881
1882 if(stat(TIME_FORMAT_12_HOUR_FLAG_FILE_PATH, &statResult) == 0) {
1883 mTimeFormat12Hour = true;
1884 }
1885
Damien Bargiacchi97480862016-03-29 14:55:55 -07001886 if(stat(ACCURATE_TIME_FLAG_FILE_PATH, &statResult) == 0) {
1887 mTimeIsAccurate = true;
1888 return true;
1889 }
1890
1891 FILE* file = fopen(LAST_TIME_CHANGED_FILE_PATH, "r");
Yi Kong911ac232019-03-24 01:49:02 -07001892 if (file != nullptr) {
Damien Bargiacchi97480862016-03-29 14:55:55 -07001893 long long lastChangedTime = 0;
1894 fscanf(file, "%lld", &lastChangedTime);
1895 fclose(file);
1896 if (lastChangedTime > 0) {
1897 struct timespec now;
1898 clock_gettime(CLOCK_REALTIME, &now);
1899 // Match the Java timestamp format
1900 long long rtcNow = (now.tv_sec * 1000LL) + (now.tv_nsec / 1000000LL);
Damien Bargiacchi96762812016-07-12 15:53:40 -07001901 if (ACCURATE_TIME_EPOCH < rtcNow
1902 && lastChangedTime > (rtcNow - MAX_TIME_IN_PAST)
1903 && lastChangedTime < (rtcNow + MAX_TIME_IN_FUTURE)) {
Damien Bargiacchi97480862016-03-29 14:55:55 -07001904 mTimeIsAccurate = true;
1905 }
1906 }
1907 }
1908
1909 return mTimeIsAccurate;
1910}
1911
1912BootAnimation::TimeCheckThread::TimeCheckThread(BootAnimation* bootAnimation) : Thread(false),
Josh Yangf95b2002021-12-23 14:32:28 -08001913 mInotifyFd(-1), mBootAnimWd(-1), mTimeWd(-1), mBootAnimation(bootAnimation) {}
Damien Bargiacchi97480862016-03-29 14:55:55 -07001914
1915BootAnimation::TimeCheckThread::~TimeCheckThread() {
Parth Sane7f7b2212023-04-12 17:23:02 +00001916 ATRACE_CALL();
Damien Bargiacchi97480862016-03-29 14:55:55 -07001917 // mInotifyFd may be -1 but that's ok since we're not at risk of attempting to close a valid FD.
1918 close(mInotifyFd);
1919}
1920
1921bool BootAnimation::TimeCheckThread::threadLoop() {
Parth Sane7f7b2212023-04-12 17:23:02 +00001922 ATRACE_CALL();
Damien Bargiacchi97480862016-03-29 14:55:55 -07001923 bool shouldLoop = doThreadLoop() && !mBootAnimation->mTimeIsAccurate
1924 && mBootAnimation->mClockEnabled;
1925 if (!shouldLoop) {
1926 close(mInotifyFd);
1927 mInotifyFd = -1;
1928 }
1929 return shouldLoop;
1930}
1931
1932bool BootAnimation::TimeCheckThread::doThreadLoop() {
Parth Sane7f7b2212023-04-12 17:23:02 +00001933 ATRACE_CALL();
Damien Bargiacchi97480862016-03-29 14:55:55 -07001934 static constexpr int BUFF_LEN (10 * (sizeof(struct inotify_event) + NAME_MAX + 1));
1935
1936 // Poll instead of doing a blocking read so the Thread can exit if requested.
1937 struct pollfd pfd = { mInotifyFd, POLLIN, 0 };
1938 ssize_t pollResult = poll(&pfd, 1, 1000);
1939
1940 if (pollResult == 0) {
1941 return true;
1942 } else if (pollResult < 0) {
Wei Wang159a4102018-10-23 23:15:58 -07001943 SLOGE("Could not poll inotify events");
Damien Bargiacchi97480862016-03-29 14:55:55 -07001944 return false;
1945 }
1946
1947 char buff[BUFF_LEN] __attribute__ ((aligned(__alignof__(struct inotify_event))));;
1948 ssize_t length = read(mInotifyFd, buff, BUFF_LEN);
1949 if (length == 0) {
1950 return true;
1951 } else if (length < 0) {
Wei Wang159a4102018-10-23 23:15:58 -07001952 SLOGE("Could not read inotify events");
Damien Bargiacchi97480862016-03-29 14:55:55 -07001953 return false;
1954 }
1955
1956 const struct inotify_event *event;
1957 for (char* ptr = buff; ptr < buff + length; ptr += sizeof(struct inotify_event) + event->len) {
1958 event = (const struct inotify_event *) ptr;
Josh Yangf95b2002021-12-23 14:32:28 -08001959 if (event->wd == mBootAnimWd && strcmp(BOOTANIM_TIME_DIR_NAME, event->name) == 0) {
Damien Bargiacchi97480862016-03-29 14:55:55 -07001960 addTimeDirWatch();
1961 } else if (event->wd == mTimeWd && (strcmp(LAST_TIME_CHANGED_FILE_NAME, event->name) == 0
1962 || strcmp(ACCURATE_TIME_FLAG_FILE_NAME, event->name) == 0)) {
1963 return !mBootAnimation->updateIsTimeAccurate();
1964 }
1965 }
1966
1967 return true;
1968}
1969
1970void BootAnimation::TimeCheckThread::addTimeDirWatch() {
Parth Sane7f7b2212023-04-12 17:23:02 +00001971 ATRACE_CALL();
Josh Yangf95b2002021-12-23 14:32:28 -08001972 mTimeWd = inotify_add_watch(mInotifyFd, BOOTANIM_TIME_DIR_PATH,
Damien Bargiacchi97480862016-03-29 14:55:55 -07001973 IN_CLOSE_WRITE | IN_MOVED_TO | IN_ATTRIB);
1974 if (mTimeWd > 0) {
1975 // No need to watch for the time directory to be created if it already exists
Josh Yangf95b2002021-12-23 14:32:28 -08001976 inotify_rm_watch(mInotifyFd, mBootAnimWd);
1977 mBootAnimWd = -1;
Damien Bargiacchi97480862016-03-29 14:55:55 -07001978 }
1979}
1980
1981status_t BootAnimation::TimeCheckThread::readyToRun() {
Parth Sane7f7b2212023-04-12 17:23:02 +00001982 ATRACE_CALL();
Damien Bargiacchi97480862016-03-29 14:55:55 -07001983 mInotifyFd = inotify_init();
1984 if (mInotifyFd < 0) {
Wei Wang159a4102018-10-23 23:15:58 -07001985 SLOGE("Could not initialize inotify fd");
Damien Bargiacchi97480862016-03-29 14:55:55 -07001986 return NO_INIT;
1987 }
1988
Josh Yangf95b2002021-12-23 14:32:28 -08001989 mBootAnimWd = inotify_add_watch(mInotifyFd, BOOTANIM_DATA_DIR_PATH, IN_CREATE | IN_ATTRIB);
1990 if (mBootAnimWd < 0) {
Damien Bargiacchi97480862016-03-29 14:55:55 -07001991 close(mInotifyFd);
1992 mInotifyFd = -1;
Josh Yangf95b2002021-12-23 14:32:28 -08001993 SLOGE("Could not add watch for %s: %s", BOOTANIM_DATA_DIR_PATH, strerror(errno));
Damien Bargiacchi97480862016-03-29 14:55:55 -07001994 return NO_INIT;
1995 }
1996
1997 addTimeDirWatch();
1998
1999 if (mBootAnimation->updateIsTimeAccurate()) {
2000 close(mInotifyFd);
2001 mInotifyFd = -1;
2002 return ALREADY_EXISTS;
2003 }
2004
2005 return NO_ERROR;
2006}
2007
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002008// ---------------------------------------------------------------------------
2009
Marin Shalamanov9070c052020-05-18 19:05:17 +02002010} // namespace android