blob: 7eb9d0f3ea91dc4ac5614feab5876ae48b1ea4f1 [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";
Beverly34ffe252019-05-17 11:03:54 -040073static const char PRODUCT_BOOTANIMATION_DARK_FILE[] = "/product/media/bootanimation-dark.zip";
Jaekyun Seokc521bb32018-01-30 11:52:47 +090074static const char PRODUCT_BOOTANIMATION_FILE[] = "/product/media/bootanimation.zip";
Damien Bargiacchi97480862016-03-29 14:55:55 -070075static const char SYSTEM_BOOTANIMATION_FILE[] = "/system/media/bootanimation.zip";
Anders Fridlunde0b4d232018-11-06 14:23:25 +010076static const char APEX_BOOTANIMATION_FILE[] = "/apex/com.android.bootanimation/etc/bootanimation.zip";
Keun-young Parkb5938422017-03-23 13:46:24 -070077static const char OEM_SHUTDOWNANIMATION_FILE[] = "/oem/media/shutdownanimation.zip";
Jaekyun Seokc521bb32018-01-30 11:52:47 +090078static const char PRODUCT_SHUTDOWNANIMATION_FILE[] = "/product/media/shutdownanimation.zip";
Keun-young Parkb5938422017-03-23 13:46:24 -070079static const char SYSTEM_SHUTDOWNANIMATION_FILE[] = "/system/media/shutdownanimation.zip";
80
Nikita Ioffe06c986e2020-01-27 17:16:03 +000081static constexpr const char* PRODUCT_USERSPACE_REBOOT_ANIMATION_FILE = "/product/media/userspace-reboot.zip";
82static constexpr const char* OEM_USERSPACE_REBOOT_ANIMATION_FILE = "/oem/media/userspace-reboot.zip";
83static constexpr const char* SYSTEM_USERSPACE_REBOOT_ANIMATION_FILE = "/system/media/userspace-reboot.zip";
84
Lee George Thomas7a2548b2023-07-25 14:47:39 -070085static const char BOOTANIM_DATA_DIR_PATH[] = "/data/misc/bootanim";
Josh Yangf95b2002021-12-23 14:32:28 -080086static const char BOOTANIM_TIME_DIR_NAME[] = "time";
Lee George Thomas7a2548b2023-07-25 14:47:39 -070087static const char BOOTANIM_TIME_DIR_PATH[] = "/data/misc/bootanim/time";
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -070088static const char CLOCK_FONT_ASSET[] = "images/clock_font.png";
89static const char CLOCK_FONT_ZIP_NAME[] = "clock_font.png";
Nicolas Geoffray41fffb42020-12-15 10:50:29 +000090static const char PROGRESS_FONT_ASSET[] = "images/progress_font.png";
91static const char PROGRESS_FONT_ZIP_NAME[] = "progress_font.png";
Damien Bargiacchi97480862016-03-29 14:55:55 -070092static const char LAST_TIME_CHANGED_FILE_NAME[] = "last_time_change";
Lee George Thomas7a2548b2023-07-25 14:47:39 -070093static const char LAST_TIME_CHANGED_FILE_PATH[] = "/data/misc/bootanim/time/last_time_change";
Damien Bargiacchi97480862016-03-29 14:55:55 -070094static const char ACCURATE_TIME_FLAG_FILE_NAME[] = "time_is_accurate";
Lee George Thomas7a2548b2023-07-25 14:47:39 -070095static const char ACCURATE_TIME_FLAG_FILE_PATH[] = "/data/misc/bootanim/time/time_is_accurate";
96static const char TIME_FORMAT_12_HOUR_FLAG_FILE_PATH[] = "/data/misc/bootanim/time/time_format_12_hour";
Damien Bargiacchi96762812016-07-12 15:53:40 -070097// Java timestamp format. Don't show the clock if the date is before 2000-01-01 00:00:00.
98static const long long ACCURATE_TIME_EPOCH = 946684800000;
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -070099static constexpr char FONT_BEGIN_CHAR = ' ';
100static constexpr char FONT_END_CHAR = '~' + 1;
101static constexpr size_t FONT_NUM_CHARS = FONT_END_CHAR - FONT_BEGIN_CHAR + 1;
102static constexpr size_t FONT_NUM_COLS = 16;
103static constexpr size_t FONT_NUM_ROWS = FONT_NUM_CHARS / FONT_NUM_COLS;
104static const int TEXT_CENTER_VALUE = INT_MAX;
105static const int TEXT_MISSING_VALUE = INT_MIN;
Damien Bargiacchi97480862016-03-29 14:55:55 -0700106static const char EXIT_PROP_NAME[] = "service.bootanim.exit";
Nicolas Geoffray41fffb42020-12-15 10:50:29 +0000107static const char PROGRESS_PROP_NAME[] = "service.bootanim.progress";
Huihong Luod4a217e2020-01-16 15:56:33 -0800108static const char DISPLAYS_PROP_NAME[] = "persist.service.bootanim.displays";
Thiébaud Weksteen08de4432021-11-18 13:25:59 +1100109static const char CLOCK_ENABLED_PROP_NAME[] = "persist.sys.bootanim.clock.enabled";
Kyeongkab.Nam35a8a112019-07-17 16:41:48 +0900110static const int ANIM_ENTRY_NAME_MAX = ANIM_PATH_MAX + 1;
niuwenchao9cd6ac82022-12-23 17:34:22 +0800111static const int MAX_CHECK_EXIT_INTERVAL_US = 50000;
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700112static constexpr size_t TEXT_POS_LEN_MAX = 16;
Shan Huang2b31a362021-08-16 16:45:39 +0000113static const int DYNAMIC_COLOR_COUNT = 4;
Lucas Dupin8eedc022021-07-29 21:49:07 +0000114static const char U_TEXTURE[] = "uTexture";
115static const char U_FADE[] = "uFade";
116static const char U_CROP_AREA[] = "uCropArea";
Shan Huang2b31a362021-08-16 16:45:39 +0000117static const char U_START_COLOR_PREFIX[] = "uStartColor";
118static const char U_END_COLOR_PREFIX[] = "uEndColor";
119static const char U_COLOR_PROGRESS[] = "uColorProgress";
Lucas Dupin8eedc022021-07-29 21:49:07 +0000120static const char A_UV[] = "aUv";
121static const char A_POSITION[] = "aPosition";
122static const char VERTEX_SHADER_SOURCE[] = R"(
123 precision mediump float;
124 attribute vec4 aPosition;
125 attribute highp vec2 aUv;
126 varying highp vec2 vUv;
127 void main() {
128 gl_Position = aPosition;
129 vUv = aUv;
130 })";
Shan Huang2b31a362021-08-16 16:45:39 +0000131static const char IMAGE_FRAG_DYNAMIC_COLORING_SHADER_SOURCE[] = R"(
132 precision mediump float;
Shan Huang81846ba2021-09-07 19:07:47 +0000133 const float cWhiteMaskThreshold = 0.05;
Shan Huang2b31a362021-08-16 16:45:39 +0000134 uniform sampler2D uTexture;
135 uniform float uFade;
136 uniform float uColorProgress;
Andrew Zengf9f58222022-11-08 11:10:48 -0800137 uniform vec3 uStartColor0;
138 uniform vec3 uStartColor1;
139 uniform vec3 uStartColor2;
140 uniform vec3 uStartColor3;
141 uniform vec3 uEndColor0;
142 uniform vec3 uEndColor1;
143 uniform vec3 uEndColor2;
144 uniform vec3 uEndColor3;
Shan Huang2b31a362021-08-16 16:45:39 +0000145 varying highp vec2 vUv;
146 void main() {
147 vec4 mask = texture2D(uTexture, vUv);
Shan Huang488ff742021-08-25 20:10:46 +0000148 float r = mask.r;
149 float g = mask.g;
150 float b = mask.b;
151 float a = mask.a;
152 // If all channels have values, render pixel as a shade of white.
153 float useWhiteMask = step(cWhiteMaskThreshold, r)
154 * step(cWhiteMaskThreshold, g)
155 * step(cWhiteMaskThreshold, b)
156 * step(cWhiteMaskThreshold, a);
Andrew Zengf9f58222022-11-08 11:10:48 -0800157 vec3 color = r * mix(uStartColor0, uEndColor0, uColorProgress)
Shan Huang488ff742021-08-25 20:10:46 +0000158 + g * mix(uStartColor1, uEndColor1, uColorProgress)
159 + b * mix(uStartColor2, uEndColor2, uColorProgress)
160 + a * mix(uStartColor3, uEndColor3, uColorProgress);
Andrew Zengf9f58222022-11-08 11:10:48 -0800161 color = mix(color, vec3((r + g + b + a) * 0.25), useWhiteMask);
162 gl_FragColor = vec4(color.x, color.y, color.z, (1.0 - uFade));
Shan Huang2b31a362021-08-16 16:45:39 +0000163 })";
Lucas Dupin8eedc022021-07-29 21:49:07 +0000164static const char IMAGE_FRAG_SHADER_SOURCE[] = R"(
165 precision mediump float;
166 uniform sampler2D uTexture;
167 uniform float uFade;
168 varying highp vec2 vUv;
169 void main() {
170 vec4 color = texture2D(uTexture, vUv);
Shan Huang2b31a362021-08-16 16:45:39 +0000171 gl_FragColor = vec4(color.x, color.y, color.z, (1.0 - uFade)) * color.a;
Lucas Dupin8eedc022021-07-29 21:49:07 +0000172 })";
173static const char TEXT_FRAG_SHADER_SOURCE[] = R"(
174 precision mediump float;
175 uniform sampler2D uTexture;
176 uniform vec4 uCropArea;
177 varying highp vec2 vUv;
178 void main() {
179 vec2 uv = vec2(mix(uCropArea.x, uCropArea.z, vUv.x),
180 mix(uCropArea.y, uCropArea.w, vUv.y));
181 gl_FragColor = texture2D(uTexture, uv);
182 })";
183
184static GLfloat quadPositions[] = {
185 -0.5f, -0.5f,
186 +0.5f, -0.5f,
187 +0.5f, +0.5f,
188 +0.5f, +0.5f,
189 -0.5f, +0.5f,
190 -0.5f, -0.5f
191};
192static GLfloat quadUVs[] = {
193 0.0f, 1.0f,
194 1.0f, 1.0f,
195 1.0f, 0.0f,
196 1.0f, 0.0f,
197 0.0f, 0.0f,
198 0.0f, 1.0f
199};
Narayan Kamathafd31e02013-12-03 13:16:03 +0000200
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800201// ---------------------------------------------------------------------------
202
Ed Coyne7464ac92017-06-08 12:26:48 -0700203BootAnimation::BootAnimation(sp<Callbacks> callbacks)
Marin Shalamanov9070c052020-05-18 19:05:17 +0200204 : Thread(false), mLooper(new Looper(false)), mClockEnabled(true), mTimeIsAccurate(false),
Yi Kong911ac232019-03-24 01:49:02 -0700205 mTimeFormat12Hour(false), mTimeCheckThread(nullptr), mCallbacks(callbacks) {
Parth Sane7f7b2212023-04-12 17:23:02 +0000206 ATRACE_CALL();
Mathias Agopian627e7b52009-05-21 19:21:59 -0700207 mSession = new SurfaceComposerClient();
Geoffrey Pitsch290c4352016-08-09 14:35:10 -0400208
Keun-young Parkb5938422017-03-23 13:46:24 -0700209 std::string powerCtl = android::base::GetProperty("sys.powerctl", "");
210 if (powerCtl.empty()) {
211 mShuttingDown = false;
212 } else {
213 mShuttingDown = true;
214 }
Huihong Luo50a2f362018-08-11 09:27:57 -0700215 ALOGD("%sAnimationStartTiming start time: %" PRId64 "ms", mShuttingDown ? "Shutdown" : "Boot",
216 elapsedRealtime());
217}
218
219BootAnimation::~BootAnimation() {
Parth Sane7f7b2212023-04-12 17:23:02 +0000220 ATRACE_CALL();
Huihong Luo50a2f362018-08-11 09:27:57 -0700221 if (mAnimation != nullptr) {
222 releaseAnimation(mAnimation);
223 mAnimation = nullptr;
224 }
225 ALOGD("%sAnimationStopTiming start time: %" PRId64 "ms", mShuttingDown ? "Shutdown" : "Boot",
226 elapsedRealtime());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800227}
228
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800229void BootAnimation::onFirstRef() {
Parth Sane7f7b2212023-04-12 17:23:02 +0000230 ATRACE_CALL();
Mathias Agopianbc726112009-09-23 15:44:05 -0700231 status_t err = mSession->linkToComposerDeath(this);
Wei Wang159a4102018-10-23 23:15:58 -0700232 SLOGE_IF(err, "linkToComposerDeath failed (%s) ", strerror(-err));
Mathias Agopian8434c532009-09-23 18:52:49 -0700233 if (err == NO_ERROR) {
Huihong Luo50a2f362018-08-11 09:27:57 -0700234 // Load the animation content -- this can be slow (eg 200ms)
235 // called before waitForSurfaceFlinger() in main() to avoid wait
236 ALOGD("%sAnimationPreloadTiming start time: %" PRId64 "ms",
237 mShuttingDown ? "Shutdown" : "Boot", elapsedRealtime());
238 preloadAnimation();
239 ALOGD("%sAnimationPreloadStopTiming start time: %" PRId64 "ms",
240 mShuttingDown ? "Shutdown" : "Boot", elapsedRealtime());
Mathias Agopianbc726112009-09-23 15:44:05 -0700241 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800242}
243
Mathias Agopianbc726112009-09-23 15:44:05 -0700244sp<SurfaceComposerClient> BootAnimation::session() const {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800245 return mSession;
246}
247
Marin Shalamanov9070c052020-05-18 19:05:17 +0200248void BootAnimation::binderDied(const wp<IBinder>&) {
Parth Sane7f7b2212023-04-12 17:23:02 +0000249 ATRACE_CALL();
Mathias Agopianbc726112009-09-23 15:44:05 -0700250 // woah, surfaceflinger died!
Wei Wang159a4102018-10-23 23:15:58 -0700251 SLOGD("SurfaceFlinger died, exiting...");
Mathias Agopianbc726112009-09-23 15:44:05 -0700252
253 // calling requestExit() is not enough here because the Surface code
254 // might be blocked on a condition variable that will never be updated.
255 kill( getpid(), SIGKILL );
256 requestExit();
257}
258
Shan Huang2b31a362021-08-16 16:45:39 +0000259static void* decodeImage(const void* encodedData, size_t dataLength, AndroidBitmapInfo* outInfo,
260 bool premultiplyAlpha) {
Parth Sane7f7b2212023-04-12 17:23:02 +0000261 ATRACE_CALL();
Derek Sollenberger22d0d4c72020-04-08 10:53:46 -0400262 AImageDecoder* decoder = nullptr;
263 AImageDecoder_createFromBuffer(encodedData, dataLength, &decoder);
264 if (!decoder) {
265 return nullptr;
266 }
267
268 const AImageDecoderHeaderInfo* info = AImageDecoder_getHeaderInfo(decoder);
269 outInfo->width = AImageDecoderHeaderInfo_getWidth(info);
270 outInfo->height = AImageDecoderHeaderInfo_getHeight(info);
271 outInfo->format = AImageDecoderHeaderInfo_getAndroidBitmapFormat(info);
272 outInfo->stride = AImageDecoder_getMinimumStride(decoder);
273 outInfo->flags = 0;
274
Shan Huang2b31a362021-08-16 16:45:39 +0000275 if (!premultiplyAlpha) {
276 AImageDecoder_setUnpremultipliedRequired(decoder, true);
277 }
278
Derek Sollenberger22d0d4c72020-04-08 10:53:46 -0400279 const size_t size = outInfo->stride * outInfo->height;
280 void* pixels = malloc(size);
281 int result = AImageDecoder_decodeImage(decoder, pixels, outInfo->stride, size);
282 AImageDecoder_delete(decoder);
283
284 if (result != ANDROID_IMAGE_DECODER_SUCCESS) {
285 free(pixels);
286 return nullptr;
287 }
288 return pixels;
289}
290
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800291status_t BootAnimation::initTexture(Texture* texture, AssetManager& assets,
Shan Huang2b31a362021-08-16 16:45:39 +0000292 const char* name, bool premultiplyAlpha) {
Parth Sane7f7b2212023-04-12 17:23:02 +0000293 ATRACE_CALL();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800294 Asset* asset = assets.open(name, Asset::ACCESS_BUFFER);
Yi Kong911ac232019-03-24 01:49:02 -0700295 if (asset == nullptr)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800296 return NO_INIT;
Derek Sollenberger22d0d4c72020-04-08 10:53:46 -0400297
298 AndroidBitmapInfo bitmapInfo;
Shan Huang2b31a362021-08-16 16:45:39 +0000299 void* pixels = decodeImage(asset->getBuffer(false), asset->getLength(), &bitmapInfo,
300 premultiplyAlpha);
Derek Sollenberger22d0d4c72020-04-08 10:53:46 -0400301 auto pixelDeleter = std::unique_ptr<void, decltype(free)*>{ pixels, free };
302
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800303 asset->close();
304 delete asset;
305
Derek Sollenberger22d0d4c72020-04-08 10:53:46 -0400306 if (!pixels) {
307 return NO_INIT;
308 }
309
310 const int w = bitmapInfo.width;
311 const int h = bitmapInfo.height;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800312
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800313 texture->w = w;
314 texture->h = h;
315
316 glGenTextures(1, &texture->name);
317 glBindTexture(GL_TEXTURE_2D, texture->name);
318
Derek Sollenberger22d0d4c72020-04-08 10:53:46 -0400319 switch (bitmapInfo.format) {
320 case ANDROID_BITMAP_FORMAT_A_8:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800321 glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, w, h, 0, GL_ALPHA,
Derek Sollenberger22d0d4c72020-04-08 10:53:46 -0400322 GL_UNSIGNED_BYTE, pixels);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800323 break;
Derek Sollenberger22d0d4c72020-04-08 10:53:46 -0400324 case ANDROID_BITMAP_FORMAT_RGBA_4444:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800325 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA,
Derek Sollenberger22d0d4c72020-04-08 10:53:46 -0400326 GL_UNSIGNED_SHORT_4_4_4_4, pixels);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800327 break;
Derek Sollenberger22d0d4c72020-04-08 10:53:46 -0400328 case ANDROID_BITMAP_FORMAT_RGBA_8888:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800329 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA,
Derek Sollenberger22d0d4c72020-04-08 10:53:46 -0400330 GL_UNSIGNED_BYTE, pixels);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800331 break;
Derek Sollenberger22d0d4c72020-04-08 10:53:46 -0400332 case ANDROID_BITMAP_FORMAT_RGB_565:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800333 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, w, h, 0, GL_RGB,
Derek Sollenberger22d0d4c72020-04-08 10:53:46 -0400334 GL_UNSIGNED_SHORT_5_6_5, pixels);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800335 break;
336 default:
337 break;
338 }
339
Lucas Dupin8eedc022021-07-29 21:49:07 +0000340 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
341 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
342 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
343 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700344
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800345 return NO_ERROR;
346}
347
Shan Huang2b31a362021-08-16 16:45:39 +0000348status_t BootAnimation::initTexture(FileMap* map, int* width, int* height,
349 bool premultiplyAlpha) {
Parth Sane7f7b2212023-04-12 17:23:02 +0000350 ATRACE_CALL();
Derek Sollenberger22d0d4c72020-04-08 10:53:46 -0400351 AndroidBitmapInfo bitmapInfo;
Shan Huang2b31a362021-08-16 16:45:39 +0000352 void* pixels = decodeImage(map->getDataPtr(), map->getDataLength(), &bitmapInfo,
353 premultiplyAlpha);
Derek Sollenberger22d0d4c72020-04-08 10:53:46 -0400354 auto pixelDeleter = std::unique_ptr<void, decltype(free)*>{ pixels, free };
Mathias Agopiana8826d62009-10-01 03:10:14 -0700355
Mykola Kondratenko0c1eeb32014-04-15 09:35:44 +0200356 // FileMap memory is never released until application exit.
357 // Release it now as the texture is already loaded and the memory used for
358 // the packed resource can be released.
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700359 delete map;
Mykola Kondratenko0c1eeb32014-04-15 09:35:44 +0200360
Derek Sollenberger22d0d4c72020-04-08 10:53:46 -0400361 if (!pixels) {
362 return NO_INIT;
363 }
364
365 const int w = bitmapInfo.width;
366 const int h = bitmapInfo.height;
Mathias Agopiana8826d62009-10-01 03:10:14 -0700367
Mathias Agopiana8826d62009-10-01 03:10:14 -0700368 int tw = 1 << (31 - __builtin_clz(w));
369 int th = 1 << (31 - __builtin_clz(h));
370 if (tw < w) tw <<= 1;
371 if (th < h) th <<= 1;
372
Derek Sollenberger22d0d4c72020-04-08 10:53:46 -0400373 switch (bitmapInfo.format) {
374 case ANDROID_BITMAP_FORMAT_RGBA_8888:
Sai Kiran Korwar27167492015-07-07 20:00:06 +0530375 if (!mUseNpotTextures && (tw != w || th != h)) {
Mathias Agopiana8826d62009-10-01 03:10:14 -0700376 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, tw, th, 0, GL_RGBA,
Yi Kong911ac232019-03-24 01:49:02 -0700377 GL_UNSIGNED_BYTE, nullptr);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700378 glTexSubImage2D(GL_TEXTURE_2D, 0,
Derek Sollenberger22d0d4c72020-04-08 10:53:46 -0400379 0, 0, w, h, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700380 } else {
Sai Kiran Korwar27167492015-07-07 20:00:06 +0530381 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA,
Derek Sollenberger22d0d4c72020-04-08 10:53:46 -0400382 GL_UNSIGNED_BYTE, pixels);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700383 }
384 break;
385
Derek Sollenberger22d0d4c72020-04-08 10:53:46 -0400386 case ANDROID_BITMAP_FORMAT_RGB_565:
Sai Kiran Korwar27167492015-07-07 20:00:06 +0530387 if (!mUseNpotTextures && (tw != w || th != h)) {
Mathias Agopiana8826d62009-10-01 03:10:14 -0700388 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, tw, th, 0, GL_RGB,
Yi Kong911ac232019-03-24 01:49:02 -0700389 GL_UNSIGNED_SHORT_5_6_5, nullptr);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700390 glTexSubImage2D(GL_TEXTURE_2D, 0,
Derek Sollenberger22d0d4c72020-04-08 10:53:46 -0400391 0, 0, w, h, GL_RGB, GL_UNSIGNED_SHORT_5_6_5, pixels);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700392 } else {
Sai Kiran Korwar27167492015-07-07 20:00:06 +0530393 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, w, h, 0, GL_RGB,
Derek Sollenberger22d0d4c72020-04-08 10:53:46 -0400394 GL_UNSIGNED_SHORT_5_6_5, pixels);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700395 }
396 break;
397 default:
398 break;
399 }
400
niuwenchaoa76df892023-09-11 17:19:04 +0800401 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
402 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
Lucas Dupin8eedc022021-07-29 21:49:07 +0000403 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
404 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700405
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700406 *width = w;
407 *height = h;
408
Mathias Agopiana8826d62009-10-01 03:10:14 -0700409 return NO_ERROR;
410}
411
Marin Shalamanov9070c052020-05-18 19:05:17 +0200412class BootAnimation::DisplayEventCallback : public LooperCallback {
413 BootAnimation* mBootAnimation;
414
415public:
416 DisplayEventCallback(BootAnimation* bootAnimation) {
Parth Sane7f7b2212023-04-12 17:23:02 +0000417 ATRACE_CALL();
Marin Shalamanov9070c052020-05-18 19:05:17 +0200418 mBootAnimation = bootAnimation;
419 }
420
421 int handleEvent(int /* fd */, int events, void* /* data */) {
Parth Sane7f7b2212023-04-12 17:23:02 +0000422 ATRACE_CALL();
Marin Shalamanov9070c052020-05-18 19:05:17 +0200423 if (events & (Looper::EVENT_ERROR | Looper::EVENT_HANGUP)) {
424 ALOGE("Display event receiver pipe was closed or an error occurred. events=0x%x",
425 events);
426 return 0; // remove the callback
427 }
428
429 if (!(events & Looper::EVENT_INPUT)) {
430 ALOGW("Received spurious callback for unhandled poll event. events=0x%x", events);
431 return 1; // keep the callback
432 }
433
434 constexpr int kBufferSize = 100;
435 DisplayEventReceiver::Event buffer[kBufferSize];
436 ssize_t numEvents;
437 do {
438 numEvents = mBootAnimation->mDisplayEventReceiver->getEvents(buffer, kBufferSize);
439 for (size_t i = 0; i < static_cast<size_t>(numEvents); i++) {
440 const auto& event = buffer[i];
441 if (event.header.type == DisplayEventReceiver::DISPLAY_EVENT_HOTPLUG) {
442 SLOGV("Hotplug received");
443
444 if (!event.hotplug.connected) {
445 // ignore hotplug disconnect
446 continue;
447 }
448 auto token = SurfaceComposerClient::getPhysicalDisplayToken(
449 event.header.displayId);
450
451 if (token != mBootAnimation->mDisplayToken) {
452 // ignore hotplug of a secondary display
453 continue;
454 }
455
Marin Shalamanovcb783ca2021-01-29 21:26:07 +0100456 DisplayMode displayMode;
457 const status_t error = SurfaceComposerClient::getActiveDisplayMode(
458 mBootAnimation->mDisplayToken, &displayMode);
Marin Shalamanov9070c052020-05-18 19:05:17 +0200459 if (error != NO_ERROR) {
Marin Shalamanovcb783ca2021-01-29 21:26:07 +0100460 SLOGE("Can't get active display mode.");
Marin Shalamanov9070c052020-05-18 19:05:17 +0200461 }
Marin Shalamanovcb783ca2021-01-29 21:26:07 +0100462 mBootAnimation->resizeSurface(displayMode.resolution.getWidth(),
463 displayMode.resolution.getHeight());
Marin Shalamanov9070c052020-05-18 19:05:17 +0200464 }
465 }
466 } while (numEvents > 0);
467
468 return 1; // keep the callback
469 }
470};
471
472EGLConfig BootAnimation::getEglConfig(const EGLDisplay& display) {
473 const EGLint attribs[] = {
Andrii Chepurnyid93f7b22023-03-15 11:22:07 +0200474 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
Marin Shalamanov9070c052020-05-18 19:05:17 +0200475 EGL_RED_SIZE, 8,
476 EGL_GREEN_SIZE, 8,
477 EGL_BLUE_SIZE, 8,
478 EGL_DEPTH_SIZE, 0,
479 EGL_NONE
480 };
481 EGLint numConfigs;
482 EGLConfig config;
483 eglChooseConfig(display, attribs, &config, 1, &numConfigs);
484 return config;
485}
486
Marin Shalamanov047802d2020-05-19 23:55:12 +0200487ui::Size BootAnimation::limitSurfaceSize(int width, int height) const {
488 ui::Size limited(width, height);
489 bool wasLimited = false;
490 const float aspectRatio = float(width) / float(height);
491 if (mMaxWidth != 0 && width > mMaxWidth) {
492 limited.height = mMaxWidth / aspectRatio;
493 limited.width = mMaxWidth;
494 wasLimited = true;
495 }
496 if (mMaxHeight != 0 && limited.height > mMaxHeight) {
497 limited.height = mMaxHeight;
498 limited.width = mMaxHeight * aspectRatio;
499 wasLimited = true;
500 }
501 SLOGV_IF(wasLimited, "Surface size has been limited to [%dx%d] from [%dx%d]",
502 limited.width, limited.height, width, height);
503 return limited;
504}
505
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800506status_t BootAnimation::readyToRun() {
Parth Sane7f7b2212023-04-12 17:23:02 +0000507 ATRACE_CALL();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800508 mAssets.addDefaultAssets();
509
Huihong Luo44305102022-09-26 11:16:20 -0700510 const std::vector<PhysicalDisplayId> ids = SurfaceComposerClient::getPhysicalDisplayIds();
511 if (ids.empty()) {
512 SLOGE("Failed to get ID for any displays\n");
Dominik Laskowski69b281d2019-11-22 14:13:12 -0800513 return NAME_NOT_FOUND;
Huihong Luo44305102022-09-26 11:16:20 -0700514 }
Dominik Laskowski3316a0a2019-01-25 02:56:41 -0800515
Huihong Luo44305102022-09-26 11:16:20 -0700516 // this system property specifies multi-display IDs to show the boot animation
Huihong Luof97c9e22019-05-29 11:08:01 -0700517 // multiple ids can be set with comma (,) as separator, for example:
Huihong Luod4a217e2020-01-16 15:56:33 -0800518 // setprop persist.boot.animation.displays 19260422155234049,19261083906282754
Marin Shalamanov11599ee2020-07-27 21:31:48 +0200519 Vector<PhysicalDisplayId> physicalDisplayIds;
Huihong Luof97c9e22019-05-29 11:08:01 -0700520 char displayValue[PROPERTY_VALUE_MAX] = "";
Huihong Luod4a217e2020-01-16 15:56:33 -0800521 property_get(DISPLAYS_PROP_NAME, displayValue, "");
Huihong Luof97c9e22019-05-29 11:08:01 -0700522 bool isValid = displayValue[0] != '\0';
523 if (isValid) {
524 char *p = displayValue;
525 while (*p) {
526 if (!isdigit(*p) && *p != ',') {
527 isValid = false;
528 break;
529 }
530 p ++;
531 }
532 if (!isValid)
Huihong Luod4a217e2020-01-16 15:56:33 -0800533 SLOGE("Invalid syntax for the value of system prop: %s", DISPLAYS_PROP_NAME);
Huihong Luof97c9e22019-05-29 11:08:01 -0700534 }
535 if (isValid) {
536 std::istringstream stream(displayValue);
Marin Shalamanov11599ee2020-07-27 21:31:48 +0200537 for (PhysicalDisplayId id; stream >> id.value; ) {
Huihong Luof97c9e22019-05-29 11:08:01 -0700538 physicalDisplayIds.add(id);
539 if (stream.peek() == ',')
540 stream.ignore();
541 }
542
Huihong Luo44305102022-09-26 11:16:20 -0700543 // the first specified display id is used to retrieve mDisplayToken
544 for (const auto id : physicalDisplayIds) {
545 if (std::find(ids.begin(), ids.end(), id) != ids.end()) {
546 if (const auto token = SurfaceComposerClient::getPhysicalDisplayToken(id)) {
547 mDisplayToken = token;
548 break;
549 }
550 }
551 }
552 }
553
554 // If the system property is not present or invalid, display 0 is used
555 if (mDisplayToken == nullptr) {
556 mDisplayToken = SurfaceComposerClient::getPhysicalDisplayToken(ids.front());
557 if (mDisplayToken == nullptr) {
558 return NAME_NOT_FOUND;
559 }
560 }
561
562 DisplayMode displayMode;
563 const status_t error =
564 SurfaceComposerClient::getActiveDisplayMode(mDisplayToken, &displayMode);
565 if (error != NO_ERROR) {
566 return error;
567 }
568
569 mMaxWidth = android::base::GetIntProperty("ro.surface_flinger.max_graphics_width", 0);
570 mMaxHeight = android::base::GetIntProperty("ro.surface_flinger.max_graphics_height", 0);
571 ui::Size resolution = displayMode.resolution;
572 resolution = limitSurfaceSize(resolution.width, resolution.height);
573 // create the native surface
574 sp<SurfaceControl> control = session()->createSurface(String8("BootAnimation"),
575 resolution.getWidth(), resolution.getHeight(), PIXEL_FORMAT_RGB_565,
576 ISurfaceComposerClient::eOpaque);
577
578 SurfaceComposerClient::Transaction t;
579 if (isValid) {
Huihong Luof97c9e22019-05-29 11:08:01 -0700580 // In the case of multi-display, boot animation shows on the specified displays
Dominik Laskowski989de3d2021-07-27 21:09:34 -0700581 for (const auto id : physicalDisplayIds) {
Huihong Luof97c9e22019-05-29 11:08:01 -0700582 if (std::find(ids.begin(), ids.end(), id) != ids.end()) {
Dominik Laskowski989de3d2021-07-27 21:09:34 -0700583 if (const auto token = SurfaceComposerClient::getPhysicalDisplayToken(id)) {
584 t.setDisplayLayerStack(token, ui::DEFAULT_LAYER_STACK);
585 }
Huihong Luof97c9e22019-05-29 11:08:01 -0700586 }
587 }
Dominik Laskowski989de3d2021-07-27 21:09:34 -0700588 t.setLayerStack(control, ui::DEFAULT_LAYER_STACK);
Huihong Luof97c9e22019-05-29 11:08:01 -0700589 }
590
Robert Carre13b58e2017-08-31 14:50:44 -0700591 t.setLayer(control, 0x40000000)
592 .apply();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800593
Mathias Agopian17f638b2009-04-16 20:04:08 -0700594 sp<Surface> s = control->getSurface();
595
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800596 // initialize opengl and egl
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800597 EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
Yi Kong911ac232019-03-24 01:49:02 -0700598 eglInitialize(display, nullptr, nullptr);
Marin Shalamanov9070c052020-05-18 19:05:17 +0200599 EGLConfig config = getEglConfig(display);
600 EGLSurface surface = eglCreateWindowSurface(display, config, s.get(), nullptr);
Lucas Dupin8eedc022021-07-29 21:49:07 +0000601 // Initialize egl context with client version number 2.0.
602 EGLint contextAttributes[] = {EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE};
603 EGLContext context = eglCreateContext(display, config, nullptr, contextAttributes);
Marin Shalamanov9070c052020-05-18 19:05:17 +0200604 EGLint w, h;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800605 eglQuerySurface(display, surface, EGL_WIDTH, &w);
606 eglQuerySurface(display, surface, EGL_HEIGHT, &h);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700607
Huihong Luo44305102022-09-26 11:16:20 -0700608 if (eglMakeCurrent(display, surface, surface, context) == EGL_FALSE) {
Mathias Agopianabac0102009-07-31 14:47:00 -0700609 return NO_INIT;
Huihong Luo44305102022-09-26 11:16:20 -0700610 }
Mathias Agopiana8826d62009-10-01 03:10:14 -0700611
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800612 mDisplay = display;
613 mContext = context;
614 mSurface = surface;
joenchen85b339f2022-04-11 23:58:34 +0800615 mInitWidth = mWidth = w;
616 mInitHeight = mHeight = h;
Mathias Agopian17f638b2009-04-16 20:04:08 -0700617 mFlingerSurfaceControl = control;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800618 mFlingerSurface = s;
Adrian Roos9ee5dff2018-08-22 20:19:49 +0200619 mTargetInset = -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800620
Mariia Sandrikovaa166f012022-12-10 02:32:14 +0000621 // Rotate the boot animation according to the value specified in the sysprop
622 // ro.bootanim.set_orientation_<display_id>. Four values are supported: ORIENTATION_0,
623 // ORIENTATION_90, ORIENTATION_180 and ORIENTATION_270.
624 // If the value isn't specified or is ORIENTATION_0, nothing will be changed.
625 // This is needed to support having boot animation in orientations different from the natural
626 // device orientation. For example, on tablets that may want to keep natural orientation
627 // portrait for applications compatibility and to have the boot animation in landscape.
628 rotateAwayFromNaturalOrientationIfNeeded();
629
Yegor Malyshev6925e1b2020-09-10 16:42:20 +0200630 projectSceneToWindow();
631
Marin Shalamanov9070c052020-05-18 19:05:17 +0200632 // Register a display event receiver
633 mDisplayEventReceiver = std::make_unique<DisplayEventReceiver>();
634 status_t status = mDisplayEventReceiver->initCheck();
635 SLOGE_IF(status != NO_ERROR, "Initialization of DisplayEventReceiver failed with status: %d",
636 status);
637 mLooper->addFd(mDisplayEventReceiver->getFd(), 0, Looper::EVENT_INPUT,
638 new DisplayEventCallback(this), nullptr);
639
Huihong Luo50a2f362018-08-11 09:27:57 -0700640 return NO_ERROR;
641}
642
Mariia Sandrikovaa166f012022-12-10 02:32:14 +0000643void BootAnimation::rotateAwayFromNaturalOrientationIfNeeded() {
Parth Sane7f7b2212023-04-12 17:23:02 +0000644 ATRACE_CALL();
Mariia Sandrikovaa166f012022-12-10 02:32:14 +0000645 const auto orientation = parseOrientationProperty();
646
647 if (orientation == ui::ROTATION_0) {
648 // Do nothing if the sysprop isn't set or is set to ROTATION_0.
649 return;
650 }
651
652 if (orientation == ui::ROTATION_90 || orientation == ui::ROTATION_270) {
653 std::swap(mWidth, mHeight);
654 std::swap(mInitWidth, mInitHeight);
655 mFlingerSurfaceControl->updateDefaultBufferSize(mWidth, mHeight);
656 }
657
658 Rect displayRect(0, 0, mWidth, mHeight);
659 Rect layerStackRect(0, 0, mWidth, mHeight);
660
661 SurfaceComposerClient::Transaction t;
662 t.setDisplayProjection(mDisplayToken, orientation, layerStackRect, displayRect);
663 t.apply();
664}
665
666ui::Rotation BootAnimation::parseOrientationProperty() {
Parth Sane7f7b2212023-04-12 17:23:02 +0000667 ATRACE_CALL();
Mariia Sandrikovaa166f012022-12-10 02:32:14 +0000668 const auto displayIds = SurfaceComposerClient::getPhysicalDisplayIds();
669 if (displayIds.size() == 0) {
670 return ui::ROTATION_0;
671 }
672 const auto displayId = displayIds[0];
673 const auto syspropName = [displayId] {
674 std::stringstream ss;
675 ss << "ro.bootanim.set_orientation_" << displayId.value;
676 return ss.str();
677 }();
YoungJoon Yang9cf990e2023-11-13 15:34:28 +0900678 auto syspropValue = android::base::GetProperty(syspropName, "");
679 if (syspropValue == "") {
680 syspropValue = android::base::GetProperty("ro.bootanim.set_orientation_logical_0", "");
681 }
682
Mariia Sandrikovaa166f012022-12-10 02:32:14 +0000683 if (syspropValue == "ORIENTATION_90") {
684 return ui::ROTATION_90;
685 } else if (syspropValue == "ORIENTATION_180") {
686 return ui::ROTATION_180;
687 } else if (syspropValue == "ORIENTATION_270") {
688 return ui::ROTATION_270;
689 }
690 return ui::ROTATION_0;
691}
692
Yegor Malyshev6925e1b2020-09-10 16:42:20 +0200693void BootAnimation::projectSceneToWindow() {
Parth Sane7f7b2212023-04-12 17:23:02 +0000694 ATRACE_CALL();
Yegor Malyshev6925e1b2020-09-10 16:42:20 +0200695 glViewport(0, 0, mWidth, mHeight);
696 glScissor(0, 0, mWidth, mHeight);
Yegor Malyshev6925e1b2020-09-10 16:42:20 +0200697}
698
Marin Shalamanov9070c052020-05-18 19:05:17 +0200699void BootAnimation::resizeSurface(int newWidth, int newHeight) {
Parth Sane7f7b2212023-04-12 17:23:02 +0000700 ATRACE_CALL();
Marin Shalamanov9070c052020-05-18 19:05:17 +0200701 // We assume this function is called on the animation thread.
702 if (newWidth == mWidth && newHeight == mHeight) {
703 return;
704 }
705 SLOGV("Resizing the boot animation surface to %d %d", newWidth, newHeight);
706
707 eglMakeCurrent(mDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
708 eglDestroySurface(mDisplay, mSurface);
709
Marin Shalamanov047802d2020-05-19 23:55:12 +0200710 const auto limitedSize = limitSurfaceSize(newWidth, newHeight);
711 mWidth = limitedSize.width;
712 mHeight = limitedSize.height;
Marin Shalamanov9070c052020-05-18 19:05:17 +0200713
derek.li92310112024-05-10 17:59:51 +0800714 mFlingerSurfaceControl->updateDefaultBufferSize(mWidth, mHeight);
Marin Shalamanov9070c052020-05-18 19:05:17 +0200715 EGLConfig config = getEglConfig(mDisplay);
716 EGLSurface surface = eglCreateWindowSurface(mDisplay, config, mFlingerSurface.get(), nullptr);
717 if (eglMakeCurrent(mDisplay, surface, surface, mContext) == EGL_FALSE) {
718 SLOGE("Can't make the new surface current. Error %d", eglGetError());
719 return;
720 }
Yegor Malyshev6925e1b2020-09-10 16:42:20 +0200721
722 projectSceneToWindow();
Marin Shalamanov9070c052020-05-18 19:05:17 +0200723
724 mSurface = surface;
725}
726
Huihong Luo50a2f362018-08-11 09:27:57 -0700727bool BootAnimation::preloadAnimation() {
Parth Sane7f7b2212023-04-12 17:23:02 +0000728 ATRACE_CALL();
Huihong Luo50a2f362018-08-11 09:27:57 -0700729 findBootAnimationFile();
Tomasz Wasilczyke8fb3c72023-08-24 19:11:22 +0000730 if (!mZipFileName.empty()) {
Huihong Luo50a2f362018-08-11 09:27:57 -0700731 mAnimation = loadAnimation(mZipFileName);
732 return (mAnimation != nullptr);
733 }
734
735 return false;
736}
737
Nikita Ioffe06c986e2020-01-27 17:16:03 +0000738bool BootAnimation::findBootAnimationFileInternal(const std::vector<std::string> &files) {
Parth Sane7f7b2212023-04-12 17:23:02 +0000739 ATRACE_CALL();
Nikita Ioffe06c986e2020-01-27 17:16:03 +0000740 for (const std::string& f : files) {
741 if (access(f.c_str(), R_OK) == 0) {
742 mZipFileName = f.c_str();
743 return true;
744 }
745 }
746 return false;
747}
748
Huihong Luo50a2f362018-08-11 09:27:57 -0700749void BootAnimation::findBootAnimationFile() {
Parth Sane7f7b2212023-04-12 17:23:02 +0000750 ATRACE_CALL();
Beverly34ffe252019-05-17 11:03:54 -0400751 const bool playDarkAnim = android::base::GetIntProperty("ro.boot.theme", 0) == 1;
Nikita Ioffe06c986e2020-01-27 17:16:03 +0000752 static const std::vector<std::string> bootFiles = {
753 APEX_BOOTANIMATION_FILE, playDarkAnim ? PRODUCT_BOOTANIMATION_DARK_FILE : PRODUCT_BOOTANIMATION_FILE,
754 OEM_BOOTANIMATION_FILE, SYSTEM_BOOTANIMATION_FILE
755 };
756 static const std::vector<std::string> shutdownFiles = {
757 PRODUCT_SHUTDOWNANIMATION_FILE, OEM_SHUTDOWNANIMATION_FILE, SYSTEM_SHUTDOWNANIMATION_FILE, ""
758 };
759 static const std::vector<std::string> userspaceRebootFiles = {
760 PRODUCT_USERSPACE_REBOOT_ANIMATION_FILE, OEM_USERSPACE_REBOOT_ANIMATION_FILE,
761 SYSTEM_USERSPACE_REBOOT_ANIMATION_FILE,
762 };
Keun-young Parkb5938422017-03-23 13:46:24 -0700763
Nikita Ioffe06c986e2020-01-27 17:16:03 +0000764 if (android::base::GetBoolProperty("sys.init.userspace_reboot.in_progress", false)) {
765 findBootAnimationFileInternal(userspaceRebootFiles);
766 } else if (mShuttingDown) {
767 findBootAnimationFileInternal(shutdownFiles);
768 } else {
769 findBootAnimationFileInternal(bootFiles);
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700770 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800771}
772
Lucas Dupin8eedc022021-07-29 21:49:07 +0000773GLuint compileShader(GLenum shaderType, const GLchar *source) {
Parth Sane7f7b2212023-04-12 17:23:02 +0000774 ATRACE_CALL();
Lucas Dupin8eedc022021-07-29 21:49:07 +0000775 GLuint shader = glCreateShader(shaderType);
776 glShaderSource(shader, 1, &source, 0);
777 glCompileShader(shader);
778 GLint isCompiled = 0;
779 glGetShaderiv(shader, GL_COMPILE_STATUS, &isCompiled);
780 if (isCompiled == GL_FALSE) {
781 SLOGE("Compile shader failed. Shader type: %d", shaderType);
Shan Huang81846ba2021-09-07 19:07:47 +0000782 GLint maxLength = 0;
783 glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &maxLength);
784 std::vector<GLchar> errorLog(maxLength);
785 glGetShaderInfoLog(shader, maxLength, &maxLength, &errorLog[0]);
786 SLOGE("Shader compilation error: %s", &errorLog[0]);
Lucas Dupin8eedc022021-07-29 21:49:07 +0000787 return 0;
788 }
789 return shader;
790}
791
792GLuint linkShader(GLuint vertexShader, GLuint fragmentShader) {
Parth Sane7f7b2212023-04-12 17:23:02 +0000793 ATRACE_CALL();
Lucas Dupin8eedc022021-07-29 21:49:07 +0000794 GLuint program = glCreateProgram();
795 glAttachShader(program, vertexShader);
796 glAttachShader(program, fragmentShader);
797 glLinkProgram(program);
798 GLint isLinked = 0;
799 glGetProgramiv(program, GL_LINK_STATUS, (int *)&isLinked);
800 if (isLinked == GL_FALSE) {
801 SLOGE("Linking shader failed. Shader handles: vert %d, frag %d",
802 vertexShader, fragmentShader);
803 return 0;
804 }
805 return program;
806}
807
808void BootAnimation::initShaders() {
Parth Sane7f7b2212023-04-12 17:23:02 +0000809 ATRACE_CALL();
Shan Huang2b31a362021-08-16 16:45:39 +0000810 bool dynamicColoringEnabled = mAnimation != nullptr && mAnimation->dynamicColoringEnabled;
Lucas Dupin8eedc022021-07-29 21:49:07 +0000811 GLuint vertexShader = compileShader(GL_VERTEX_SHADER, (const GLchar *)VERTEX_SHADER_SOURCE);
812 GLuint imageFragmentShader =
Shan Huang2b31a362021-08-16 16:45:39 +0000813 compileShader(GL_FRAGMENT_SHADER, dynamicColoringEnabled
814 ? (const GLchar *)IMAGE_FRAG_DYNAMIC_COLORING_SHADER_SOURCE
815 : (const GLchar *)IMAGE_FRAG_SHADER_SOURCE);
Lucas Dupin8eedc022021-07-29 21:49:07 +0000816 GLuint textFragmentShader =
817 compileShader(GL_FRAGMENT_SHADER, (const GLchar *)TEXT_FRAG_SHADER_SOURCE);
818
819 // Initialize image shader.
820 mImageShader = linkShader(vertexShader, imageFragmentShader);
821 GLint positionLocation = glGetAttribLocation(mImageShader, A_POSITION);
822 GLint uvLocation = glGetAttribLocation(mImageShader, A_UV);
823 mImageTextureLocation = glGetUniformLocation(mImageShader, U_TEXTURE);
824 mImageFadeLocation = glGetUniformLocation(mImageShader, U_FADE);
825 glEnableVertexAttribArray(positionLocation);
826 glVertexAttribPointer(positionLocation, 2, GL_FLOAT, GL_FALSE, 0, quadPositions);
827 glVertexAttribPointer(uvLocation, 2, GL_FLOAT, GL_FALSE, 0, quadUVs);
828 glEnableVertexAttribArray(uvLocation);
829
830 // Initialize text shader.
831 mTextShader = linkShader(vertexShader, textFragmentShader);
832 positionLocation = glGetAttribLocation(mTextShader, A_POSITION);
833 uvLocation = glGetAttribLocation(mTextShader, A_UV);
834 mTextTextureLocation = glGetUniformLocation(mTextShader, U_TEXTURE);
835 mTextCropAreaLocation = glGetUniformLocation(mTextShader, U_CROP_AREA);
836 glEnableVertexAttribArray(positionLocation);
837 glVertexAttribPointer(positionLocation, 2, GL_FLOAT, GL_FALSE, 0, quadPositions);
838 glVertexAttribPointer(uvLocation, 2, GL_FLOAT, GL_FALSE, 0, quadUVs);
839 glEnableVertexAttribArray(uvLocation);
840}
841
Marin Shalamanov9070c052020-05-18 19:05:17 +0200842bool BootAnimation::threadLoop() {
Parth Sane7f7b2212023-04-12 17:23:02 +0000843 ATRACE_CALL();
Marin Shalamanov9070c052020-05-18 19:05:17 +0200844 bool result;
Lucas Dupin8eedc022021-07-29 21:49:07 +0000845 initShaders();
846
Narayan Kamathafd31e02013-12-03 13:16:03 +0000847 // We have no bootanimation file, so we use the stock android logo
848 // animation.
Tomasz Wasilczyke8fb3c72023-08-24 19:11:22 +0000849 if (mZipFileName.empty()) {
Yoshiaki Maruoka9ef19b82017-10-25 13:55:49 +0900850 ALOGD("No animation file");
Marin Shalamanov9070c052020-05-18 19:05:17 +0200851 result = android();
Mathias Agopiana8826d62009-10-01 03:10:14 -0700852 } else {
Marin Shalamanov9070c052020-05-18 19:05:17 +0200853 result = movie();
Mathias Agopiana8826d62009-10-01 03:10:14 -0700854 }
855
Philip Junker60600562020-11-25 18:16:16 +0100856 mCallbacks->shutdown();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800857 eglMakeCurrent(mDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
858 eglDestroyContext(mDisplay, mContext);
859 eglDestroySurface(mDisplay, mSurface);
Mathias Agopian6cf0db22009-04-17 19:36:26 -0700860 mFlingerSurface.clear();
Mathias Agopian17f638b2009-04-16 20:04:08 -0700861 mFlingerSurfaceControl.clear();
Mathias Agopian627e7b52009-05-21 19:21:59 -0700862 eglTerminate(mDisplay);
Sai Kiran Korwar3eee9fb2015-06-16 17:13:35 +0530863 eglReleaseThread();
Mathias Agopian627e7b52009-05-21 19:21:59 -0700864 IPCThreadState::self()->stopProcess();
Marin Shalamanov9070c052020-05-18 19:05:17 +0200865 return result;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800866}
867
Marin Shalamanov9070c052020-05-18 19:05:17 +0200868bool BootAnimation::android() {
Parth Sane7f7b2212023-04-12 17:23:02 +0000869 ATRACE_CALL();
Lucas Dupin8eedc022021-07-29 21:49:07 +0000870 glActiveTexture(GL_TEXTURE0);
871
Wei Wang159a4102018-10-23 23:15:58 -0700872 SLOGD("%sAnimationShownTiming start time: %" PRId64 "ms", mShuttingDown ? "Shutdown" : "Boot",
Keun-young Parkd1794cd2017-04-04 12:21:19 -0700873 elapsedRealtime());
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700874 initTexture(&mAndroid[0], mAssets, "images/android-logo-mask.png");
875 initTexture(&mAndroid[1], mAssets, "images/android-logo-shine.png");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800876
Ed Coyne7464ac92017-06-08 12:26:48 -0700877 mCallbacks->init({});
878
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800879 // clear screen
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700880 glDisable(GL_DITHER);
881 glDisable(GL_SCISSOR_TEST);
Shan Huangbfd2d3c2021-09-09 23:19:04 +0000882 glUseProgram(mImageShader);
883
Mathias Agopian59f19e42011-05-06 19:22:12 -0700884 glClearColor(0,0,0,1);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800885 glClear(GL_COLOR_BUFFER_BIT);
886 eglSwapBuffers(mDisplay, mSurface);
887
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700888 // Blend state
889 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700890
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800891 const nsecs_t startTime = systemTime();
892 do {
Marin Shalamanov9070c052020-05-18 19:05:17 +0200893 processDisplayEvents();
894 const GLint xc = (mWidth - mAndroid[0].w) / 2;
895 const GLint yc = (mHeight - mAndroid[0].h) / 2;
896 const Rect updateRect(xc, yc, xc + mAndroid[0].w, yc + mAndroid[0].h);
897 glScissor(updateRect.left, mHeight - updateRect.bottom, updateRect.width(),
898 updateRect.height());
899
Mathias Agopian13796652009-03-24 22:49:21 -0700900 nsecs_t now = systemTime();
901 double time = now - startTime;
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700902 float t = 4.0f * float(time / us2ns(16667)) / mAndroid[1].w;
903 GLint offset = (1 - (t - floorf(t))) * mAndroid[1].w;
904 GLint x = xc - offset;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800905
Mathias Agopian81668642009-07-28 11:41:30 -0700906 glDisable(GL_SCISSOR_TEST);
907 glClear(GL_COLOR_BUFFER_BIT);
908
909 glEnable(GL_SCISSOR_TEST);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800910 glDisable(GL_BLEND);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800911 glBindTexture(GL_TEXTURE_2D, mAndroid[1].name);
Lucas Dupin8eedc022021-07-29 21:49:07 +0000912 drawTexturedQuad(x, yc, mAndroid[1].w, mAndroid[1].h);
913 drawTexturedQuad(x + mAndroid[1].w, yc, mAndroid[1].w, mAndroid[1].h);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800914
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700915 glEnable(GL_BLEND);
916 glBindTexture(GL_TEXTURE_2D, mAndroid[0].name);
Lucas Dupin8eedc022021-07-29 21:49:07 +0000917 drawTexturedQuad(xc, yc, mAndroid[0].w, mAndroid[0].h);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800918
Mathias Agopian627e7b52009-05-21 19:21:59 -0700919 EGLBoolean res = eglSwapBuffers(mDisplay, mSurface);
920 if (res == EGL_FALSE)
921 break;
922
Mathias Agopian13796652009-03-24 22:49:21 -0700923 // 12fps: don't animate too fast to preserve CPU
924 const nsecs_t sleepTime = 83333 - ns2us(systemTime() - now);
925 if (sleepTime > 0)
Mathias Agopiana8826d62009-10-01 03:10:14 -0700926 usleep(sleepTime);
Kevin Hesterd3782b22012-04-26 10:38:55 -0700927
928 checkExit();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800929 } while (!exitPending());
930
931 glDeleteTextures(1, &mAndroid[0].name);
932 glDeleteTextures(1, &mAndroid[1].name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800933 return false;
934}
935
Kevin Hesterd3782b22012-04-26 10:38:55 -0700936void BootAnimation::checkExit() {
Parth Sane7f7b2212023-04-12 17:23:02 +0000937 ATRACE_CALL();
Kevin Hesterd3782b22012-04-26 10:38:55 -0700938 // Allow surface flinger to gracefully request shutdown
939 char value[PROPERTY_VALUE_MAX];
940 property_get(EXIT_PROP_NAME, value, "0");
941 int exitnow = atoi(value);
942 if (exitnow) {
943 requestExit();
944 }
945}
946
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700947bool BootAnimation::validClock(const Animation::Part& part) {
Parth Sane7f7b2212023-04-12 17:23:02 +0000948 ATRACE_CALL();
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700949 return part.clockPosX != TEXT_MISSING_VALUE && part.clockPosY != TEXT_MISSING_VALUE;
950}
951
952bool parseTextCoord(const char* str, int* dest) {
Parth Sane7f7b2212023-04-12 17:23:02 +0000953 ATRACE_CALL();
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700954 if (strcmp("c", str) == 0) {
955 *dest = TEXT_CENTER_VALUE;
956 return true;
957 }
958
959 char* end;
960 int val = (int) strtol(str, &end, 0);
961 if (end == str || *end != '\0' || val == INT_MAX || val == INT_MIN) {
962 return false;
963 }
964 *dest = val;
965 return true;
966}
967
968// Parse two position coordinates. If only string is non-empty, treat it as the y value.
969void parsePosition(const char* str1, const char* str2, int* x, int* y) {
Parth Sane7f7b2212023-04-12 17:23:02 +0000970 ATRACE_CALL();
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700971 bool success = false;
972 if (strlen(str1) == 0) { // No values were specified
973 // success = false
974 } else if (strlen(str2) == 0) { // we have only one value
975 if (parseTextCoord(str1, y)) {
976 *x = TEXT_CENTER_VALUE;
977 success = true;
978 }
979 } else {
980 if (parseTextCoord(str1, x) && parseTextCoord(str2, y)) {
981 success = true;
982 }
983 }
984
985 if (!success) {
986 *x = TEXT_MISSING_VALUE;
987 *y = TEXT_MISSING_VALUE;
988 }
989}
990
Jesse Hall083b84c2014-09-22 10:51:09 -0700991// Parse a color represented as an HTML-style 'RRGGBB' string: each pair of
992// characters in str is a hex number in [0, 255], which are converted to
993// floating point values in the range [0.0, 1.0] and placed in the
994// corresponding elements of color.
995//
996// If the input string isn't valid, parseColor returns false and color is
997// left unchanged.
998static bool parseColor(const char str[7], float color[3]) {
Parth Sane7f7b2212023-04-12 17:23:02 +0000999 ATRACE_CALL();
Jesse Hall083b84c2014-09-22 10:51:09 -07001000 float tmpColor[3];
1001 for (int i = 0; i < 3; i++) {
1002 int val = 0;
1003 for (int j = 0; j < 2; j++) {
1004 val *= 16;
1005 char c = str[2*i + j];
1006 if (c >= '0' && c <= '9') val += c - '0';
1007 else if (c >= 'A' && c <= 'F') val += (c - 'A') + 10;
1008 else if (c >= 'a' && c <= 'f') val += (c - 'a') + 10;
1009 else return false;
1010 }
1011 tmpColor[i] = static_cast<float>(val) / 255.0f;
1012 }
1013 memcpy(color, tmpColor, sizeof(tmpColor));
1014 return true;
1015}
1016
Shan Huang2b31a362021-08-16 16:45:39 +00001017// Parse a color represented as a signed decimal int string.
1018// E.g. "-2757722" (whose hex 2's complement is 0xFFD5EBA6).
1019// If the input color string is empty, set color with values in defaultColor.
1020static void parseColorDecimalString(const std::string& colorString,
1021 float color[3], float defaultColor[3]) {
Parth Sane7f7b2212023-04-12 17:23:02 +00001022 ATRACE_CALL();
Shan Huang2b31a362021-08-16 16:45:39 +00001023 if (colorString == "") {
1024 memcpy(color, defaultColor, sizeof(float) * 3);
1025 return;
1026 }
1027 int colorInt = atoi(colorString.c_str());
1028 color[0] = ((float)((colorInt >> 16) & 0xFF)) / 0xFF; // r
1029 color[1] = ((float)((colorInt >> 8) & 0xFF)) / 0xFF; // g
1030 color[2] = ((float)(colorInt & 0xFF)) / 0xFF; // b
1031}
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001032
Marin Shalamanov9070c052020-05-18 19:05:17 +02001033static bool readFile(ZipFileRO* zip, const char* name, String8& outString) {
Parth Sane7f7b2212023-04-12 17:23:02 +00001034 ATRACE_CALL();
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001035 ZipEntryRO entry = zip->findEntryByName(name);
Wei Wang159a4102018-10-23 23:15:58 -07001036 SLOGE_IF(!entry, "couldn't find %s", name);
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -07001037 if (!entry) {
1038 return false;
1039 }
1040
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001041 FileMap* entryMap = zip->createEntryFileMap(entry);
1042 zip->releaseEntry(entry);
Wei Wang159a4102018-10-23 23:15:58 -07001043 SLOGE_IF(!entryMap, "entryMap is null");
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -07001044 if (!entryMap) {
1045 return false;
1046 }
1047
Tomasz Wasilczyk31eb3c892023-08-23 22:12:33 +00001048 outString = String8((char const*)entryMap->getDataPtr(), entryMap->getDataLength());
Narayan Kamath688ff4c2015-02-23 15:47:54 +00001049 delete entryMap;
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -07001050 return true;
1051}
1052
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -07001053// The font image should be a 96x2 array of character images. The
1054// columns are the printable ASCII characters 0x20 - 0x7f. The
1055// top row is regular text; the bottom row is bold.
1056status_t BootAnimation::initFont(Font* font, const char* fallback) {
Parth Sane7f7b2212023-04-12 17:23:02 +00001057 ATRACE_CALL();
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -07001058 status_t status = NO_ERROR;
Damien Bargiacchia704b7d2016-02-16 16:55:49 -08001059
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -07001060 if (font->map != nullptr) {
1061 glGenTextures(1, &font->texture.name);
1062 glBindTexture(GL_TEXTURE_2D, font->texture.name);
Damien Bargiacchia704b7d2016-02-16 16:55:49 -08001063
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -07001064 status = initTexture(font->map, &font->texture.w, &font->texture.h);
1065
Lucas Dupin8eedc022021-07-29 21:49:07 +00001066 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
1067 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
1068 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
1069 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -07001070 } else if (fallback != nullptr) {
1071 status = initTexture(&font->texture, mAssets, fallback);
1072 } else {
1073 return NO_INIT;
Damien Bargiacchia704b7d2016-02-16 16:55:49 -08001074 }
1075
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -07001076 if (status == NO_ERROR) {
1077 font->char_width = font->texture.w / FONT_NUM_COLS;
1078 font->char_height = font->texture.h / FONT_NUM_ROWS / 2; // There are bold and regular rows
1079 }
1080
1081 return status;
1082}
1083
1084void BootAnimation::drawText(const char* str, const Font& font, bool bold, int* x, int* y) {
Parth Sane7f7b2212023-04-12 17:23:02 +00001085 ATRACE_CALL();
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -07001086 glEnable(GL_BLEND); // Allow us to draw on top of the animation
1087 glBindTexture(GL_TEXTURE_2D, font.texture.name);
Lucas Dupin8eedc022021-07-29 21:49:07 +00001088 glUseProgram(mTextShader);
1089 glUniform1i(mTextTextureLocation, 0);
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -07001090
1091 const int len = strlen(str);
1092 const int strWidth = font.char_width * len;
1093
1094 if (*x == TEXT_CENTER_VALUE) {
1095 *x = (mWidth - strWidth) / 2;
1096 } else if (*x < 0) {
1097 *x = mWidth + *x - strWidth;
1098 }
1099 if (*y == TEXT_CENTER_VALUE) {
1100 *y = (mHeight - font.char_height) / 2;
1101 } else if (*y < 0) {
1102 *y = mHeight + *y - font.char_height;
1103 }
1104
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -07001105 for (int i = 0; i < len; i++) {
1106 char c = str[i];
1107
1108 if (c < FONT_BEGIN_CHAR || c > FONT_END_CHAR) {
1109 c = '?';
1110 }
1111
1112 // Crop the texture to only the pixels in the current glyph
1113 const int charPos = (c - FONT_BEGIN_CHAR); // Position in the list of valid characters
1114 const int row = charPos / FONT_NUM_COLS;
1115 const int col = charPos % FONT_NUM_COLS;
Lucas Dupin8eedc022021-07-29 21:49:07 +00001116 // Bold fonts are expected in the second half of each row.
1117 float v0 = (row + (bold ? 0.5f : 0.0f)) / FONT_NUM_ROWS;
1118 float u0 = ((float)col) / FONT_NUM_COLS;
1119 float v1 = v0 + 1.0f / FONT_NUM_ROWS / 2;
1120 float u1 = u0 + 1.0f / FONT_NUM_COLS;
1121 glUniform4f(mTextCropAreaLocation, u0, v0, u1, v1);
1122 drawTexturedQuad(*x, *y, font.char_width, font.char_height);
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -07001123
1124 *x += font.char_width;
1125 }
1126
1127 glDisable(GL_BLEND); // Return to the animation's default behaviour
1128 glBindTexture(GL_TEXTURE_2D, 0);
1129}
1130
Damien Bargiacchi9071db12016-10-28 17:38:22 -07001131// We render 12 or 24 hour time.
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -07001132void BootAnimation::drawClock(const Font& font, const int xPos, const int yPos) {
Parth Sane7f7b2212023-04-12 17:23:02 +00001133 ATRACE_CALL();
Damien Bargiacchi9071db12016-10-28 17:38:22 -07001134 static constexpr char TIME_FORMAT_12[] = "%l:%M";
1135 static constexpr char TIME_FORMAT_24[] = "%H:%M";
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -07001136 static constexpr int TIME_LENGTH = 6;
1137
Damien Bargiacchia704b7d2016-02-16 16:55:49 -08001138 time_t rawtime;
1139 time(&rawtime);
1140 struct tm* timeInfo = localtime(&rawtime);
1141
1142 char timeBuff[TIME_LENGTH];
Damien Bargiacchi9071db12016-10-28 17:38:22 -07001143 const char* timeFormat = mTimeFormat12Hour ? TIME_FORMAT_12 : TIME_FORMAT_24;
1144 size_t length = strftime(timeBuff, TIME_LENGTH, timeFormat, timeInfo);
Damien Bargiacchia704b7d2016-02-16 16:55:49 -08001145
1146 if (length != TIME_LENGTH - 1) {
Wei Wang159a4102018-10-23 23:15:58 -07001147 SLOGE("Couldn't format time; abandoning boot animation clock");
Damien Bargiacchia704b7d2016-02-16 16:55:49 -08001148 mClockEnabled = false;
1149 return;
1150 }
1151
Damien Bargiacchi45a76442016-12-05 18:02:18 -08001152 char* out = timeBuff[0] == ' ' ? &timeBuff[1] : &timeBuff[0];
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -07001153 int x = xPos;
1154 int y = yPos;
Damien Bargiacchi45a76442016-12-05 18:02:18 -08001155 drawText(out, font, false, &x, &y);
Damien Bargiacchia704b7d2016-02-16 16:55:49 -08001156}
1157
Nicolas Geoffray41fffb42020-12-15 10:50:29 +00001158void BootAnimation::drawProgress(int percent, const Font& font, const int xPos, const int yPos) {
Parth Sane7f7b2212023-04-12 17:23:02 +00001159 ATRACE_CALL();
Nicolas Geoffray41fffb42020-12-15 10:50:29 +00001160 static constexpr int PERCENT_LENGTH = 5;
1161
1162 char percentBuff[PERCENT_LENGTH];
1163 // ';' has the ascii code just after ':', and the font resource contains '%'
1164 // for that ascii code.
1165 sprintf(percentBuff, "%d;", percent);
1166 int x = xPos;
1167 int y = yPos;
1168 drawText(percentBuff, font, false, &x, &y);
1169}
1170
Marin Shalamanov9070c052020-05-18 19:05:17 +02001171bool BootAnimation::parseAnimationDesc(Animation& animation) {
Parth Sane7f7b2212023-04-12 17:23:02 +00001172 ATRACE_CALL();
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -07001173 String8 desString;
1174
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001175 if (!readFile(animation.zip, "desc.txt", desString)) {
Narayan Kamathafd31e02013-12-03 13:16:03 +00001176 return false;
1177 }
Tomasz Wasilczyk3815d342023-08-10 23:54:44 +00001178 char const* s = desString.c_str();
Shan Huang2b31a362021-08-16 16:45:39 +00001179 std::string dynamicColoringPartName = "";
1180 bool postDynamicColoring = false;
Mathias Agopiana8826d62009-10-01 03:10:14 -07001181
Mathias Agopiana8826d62009-10-01 03:10:14 -07001182 // Parse the description file
1183 for (;;) {
1184 const char* endl = strstr(s, "\n");
Yi Kong911ac232019-03-24 01:49:02 -07001185 if (endl == nullptr) break;
Mathias Agopiana8826d62009-10-01 03:10:14 -07001186 String8 line(s, endl - s);
Tomasz Wasilczyk3815d342023-08-10 23:54:44 +00001187 const char* l = line.c_str();
Damien Bargiacchia704b7d2016-02-16 16:55:49 -08001188 int fps = 0;
1189 int width = 0;
1190 int height = 0;
1191 int count = 0;
1192 int pause = 0;
Nicolas Geoffray41fffb42020-12-15 10:50:29 +00001193 int progress = 0;
Yegor Malyshev6925e1b2020-09-10 16:42:20 +02001194 int framesToFadeCount = 0;
Shan Huang488ff742021-08-25 20:10:46 +00001195 int colorTransitionStart = 0;
1196 int colorTransitionEnd = 0;
Narayan Kamathafd31e02013-12-03 13:16:03 +00001197 char path[ANIM_ENTRY_NAME_MAX];
Jesse Hall083b84c2014-09-22 10:51:09 -07001198 char color[7] = "000000"; // default to black if unspecified
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -07001199 char clockPos1[TEXT_POS_LEN_MAX + 1] = "";
1200 char clockPos2[TEXT_POS_LEN_MAX + 1] = "";
Shan Huang2b31a362021-08-16 16:45:39 +00001201 char dynamicColoringPartNameBuffer[ANIM_ENTRY_NAME_MAX];
Kevin Hesterd3782b22012-04-26 10:38:55 -07001202 char pathType;
Shan Huang2b31a362021-08-16 16:45:39 +00001203 // start colors default to black if unspecified
1204 char start_color_0[7] = "000000";
1205 char start_color_1[7] = "000000";
1206 char start_color_2[7] = "000000";
1207 char start_color_3[7] = "000000";
Yegor Malyshev6925e1b2020-09-10 16:42:20 +02001208
1209 int nextReadPos;
1210
yidong zhang87bb3462022-12-07 15:55:31 +08001211 if (strlen(l) == 0) {
1212 s = ++endl;
1213 continue;
1214 }
1215
Nicolas Geoffray41fffb42020-12-15 10:50:29 +00001216 int topLineNumbers = sscanf(l, "%d %d %d %d", &width, &height, &fps, &progress);
1217 if (topLineNumbers == 3 || topLineNumbers == 4) {
1218 // SLOGD("> w=%d, h=%d, fps=%d, progress=%d", width, height, fps, progress);
Mathias Agopiana8826d62009-10-01 03:10:14 -07001219 animation.width = width;
1220 animation.height = height;
1221 animation.fps = fps;
Nicolas Geoffray41fffb42020-12-15 10:50:29 +00001222 if (topLineNumbers == 4) {
1223 animation.progressEnabled = (progress != 0);
1224 } else {
1225 animation.progressEnabled = false;
1226 }
Shan Huang488ff742021-08-25 20:10:46 +00001227 } else if (sscanf(l, "dynamic_colors %" STRTO(ANIM_PATH_MAX) "s #%6s #%6s #%6s #%6s %d %d",
Shan Huang2b31a362021-08-16 16:45:39 +00001228 dynamicColoringPartNameBuffer,
Shan Huang488ff742021-08-25 20:10:46 +00001229 start_color_0, start_color_1, start_color_2, start_color_3,
1230 &colorTransitionStart, &colorTransitionEnd)) {
Shan Huang2b31a362021-08-16 16:45:39 +00001231 animation.dynamicColoringEnabled = true;
1232 parseColor(start_color_0, animation.startColors[0]);
1233 parseColor(start_color_1, animation.startColors[1]);
1234 parseColor(start_color_2, animation.startColors[2]);
1235 parseColor(start_color_3, animation.startColors[3]);
Shan Huang488ff742021-08-25 20:10:46 +00001236 animation.colorTransitionStart = colorTransitionStart;
1237 animation.colorTransitionEnd = colorTransitionEnd;
Shan Huang2b31a362021-08-16 16:45:39 +00001238 dynamicColoringPartName = std::string(dynamicColoringPartNameBuffer);
Yegor Malyshev6925e1b2020-09-10 16:42:20 +02001239 } else if (sscanf(l, "%c %d %d %" STRTO(ANIM_PATH_MAX) "s%n",
1240 &pathType, &count, &pause, path, &nextReadPos) >= 4) {
1241 if (pathType == 'f') {
1242 sscanf(l + nextReadPos, " %d #%6s %16s %16s", &framesToFadeCount, color, clockPos1,
1243 clockPos2);
1244 } else {
1245 sscanf(l + nextReadPos, " #%6s %16s %16s", color, clockPos1, clockPos2);
1246 }
1247 // SLOGD("> type=%c, count=%d, pause=%d, path=%s, framesToFadeCount=%d, color=%s, "
1248 // "clockPos1=%s, clockPos2=%s",
1249 // pathType, count, pause, path, framesToFadeCount, color, clockPos1, clockPos2);
Mathias Agopiana8826d62009-10-01 03:10:14 -07001250 Animation::Part part;
Shan Huang2b31a362021-08-16 16:45:39 +00001251 if (path == dynamicColoringPartName) {
1252 // Part is specified to use dynamic coloring.
1253 part.useDynamicColoring = true;
1254 part.postDynamicColoring = false;
1255 postDynamicColoring = true;
1256 } else {
1257 // Part does not use dynamic coloring.
1258 part.useDynamicColoring = false;
1259 part.postDynamicColoring = postDynamicColoring;
1260 }
Kevin Hesterd3782b22012-04-26 10:38:55 -07001261 part.playUntilComplete = pathType == 'c';
Yegor Malyshev6925e1b2020-09-10 16:42:20 +02001262 part.framesToFadeCount = framesToFadeCount;
Mathias Agopiana8826d62009-10-01 03:10:14 -07001263 part.count = count;
1264 part.pause = pause;
1265 part.path = path;
Yi Kong911ac232019-03-24 01:49:02 -07001266 part.audioData = nullptr;
1267 part.animation = nullptr;
Jesse Hall083b84c2014-09-22 10:51:09 -07001268 if (!parseColor(color, part.backgroundColor)) {
Wei Wang159a4102018-10-23 23:15:58 -07001269 SLOGE("> invalid color '#%s'", color);
Jesse Hall083b84c2014-09-22 10:51:09 -07001270 part.backgroundColor[0] = 0.0f;
1271 part.backgroundColor[1] = 0.0f;
1272 part.backgroundColor[2] = 0.0f;
1273 }
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -07001274 parsePosition(clockPos1, clockPos2, &part.clockPosX, &part.clockPosY);
Mathias Agopiana8826d62009-10-01 03:10:14 -07001275 animation.parts.add(part);
1276 }
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001277 else if (strcmp(l, "$SYSTEM") == 0) {
Wei Wang159a4102018-10-23 23:15:58 -07001278 // SLOGD("> SYSTEM");
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001279 Animation::Part part;
1280 part.playUntilComplete = false;
Yegor Malyshev6925e1b2020-09-10 16:42:20 +02001281 part.framesToFadeCount = 0;
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001282 part.count = 1;
1283 part.pause = 0;
Yi Kong911ac232019-03-24 01:49:02 -07001284 part.audioData = nullptr;
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001285 part.animation = loadAnimation(String8(SYSTEM_BOOTANIMATION_FILE));
Yi Kong911ac232019-03-24 01:49:02 -07001286 if (part.animation != nullptr)
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001287 animation.parts.add(part);
1288 }
Mathias Agopiana8826d62009-10-01 03:10:14 -07001289 s = ++endl;
1290 }
1291
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001292 return true;
1293}
1294
Marin Shalamanov9070c052020-05-18 19:05:17 +02001295bool BootAnimation::preloadZip(Animation& animation) {
Parth Sane7f7b2212023-04-12 17:23:02 +00001296 ATRACE_CALL();
Mathias Agopiana8826d62009-10-01 03:10:14 -07001297 // read all the data structures
1298 const size_t pcount = animation.parts.size();
Yi Kong911ac232019-03-24 01:49:02 -07001299 void *cookie = nullptr;
Geoffrey Pitschdd214a72016-06-27 17:14:30 -04001300 ZipFileRO* zip = animation.zip;
1301 if (!zip->startIteration(&cookie)) {
Narayan Kamathafd31e02013-12-03 13:16:03 +00001302 return false;
1303 }
1304
1305 ZipEntryRO entry;
1306 char name[ANIM_ENTRY_NAME_MAX];
Yi Kong911ac232019-03-24 01:49:02 -07001307 while ((entry = zip->nextEntry(cookie)) != nullptr) {
Geoffrey Pitschdd214a72016-06-27 17:14:30 -04001308 const int foundEntryName = zip->getEntryFileName(entry, name, ANIM_ENTRY_NAME_MAX);
Narayan Kamathafd31e02013-12-03 13:16:03 +00001309 if (foundEntryName > ANIM_ENTRY_NAME_MAX || foundEntryName == -1) {
Wei Wang159a4102018-10-23 23:15:58 -07001310 SLOGE("Error fetching entry file name");
Narayan Kamathafd31e02013-12-03 13:16:03 +00001311 continue;
1312 }
1313
Tomasz Wasilczyk9dcea572023-08-22 20:39:20 +00001314 const std::filesystem::path entryName(name);
1315 const std::filesystem::path path(entryName.parent_path());
1316 const std::filesystem::path leaf(entryName.filename());
1317 if (!leaf.empty()) {
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -07001318 if (entryName == CLOCK_FONT_ZIP_NAME) {
1319 FileMap* map = zip->createEntryFileMap(entry);
1320 if (map) {
1321 animation.clockFont.map = map;
1322 }
1323 continue;
1324 }
1325
Nicolas Geoffray41fffb42020-12-15 10:50:29 +00001326 if (entryName == PROGRESS_FONT_ZIP_NAME) {
1327 FileMap* map = zip->createEntryFileMap(entry);
1328 if (map) {
1329 animation.progressFont.map = map;
1330 }
1331 continue;
1332 }
1333
Geoffrey Pitschdd214a72016-06-27 17:14:30 -04001334 for (size_t j = 0; j < pcount; j++) {
Tomasz Wasilczyk9dcea572023-08-22 20:39:20 +00001335 if (path.string() == animation.parts[j].path.c_str()) {
Narayan Kamath4600dd02015-06-16 12:02:57 +01001336 uint16_t method;
Narayan Kamathafd31e02013-12-03 13:16:03 +00001337 // supports only stored png files
Pawan Wagh8efc3c42024-06-03 21:43:13 +00001338 if (zip->getEntryInfo(entry, &method, nullptr, nullptr, nullptr, nullptr,
1339 nullptr, nullptr)) {
Narayan Kamathafd31e02013-12-03 13:16:03 +00001340 if (method == ZipFileRO::kCompressStored) {
Geoffrey Pitschdd214a72016-06-27 17:14:30 -04001341 FileMap* map = zip->createEntryFileMap(entry);
Narayan Kamathafd31e02013-12-03 13:16:03 +00001342 if (map) {
Narayan Kamathafd31e02013-12-03 13:16:03 +00001343 Animation::Part& part(animation.parts.editItemAt(j));
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -07001344 if (leaf == "audio.wav") {
1345 // a part may have at most one audio file
Geoffrey Pitschd6d9a1d2016-06-08 00:38:58 -07001346 part.audioData = (uint8_t *)map->getDataPtr();
1347 part.audioLength = map->getDataLength();
Geoffrey Pitschdd214a72016-06-27 17:14:30 -04001348 } else if (leaf == "trim.txt") {
Tomasz Wasilczyk31eb3c892023-08-23 22:12:33 +00001349 part.trimData = String8((char const*)map->getDataPtr(),
Geoffrey Pitschdd214a72016-06-27 17:14:30 -04001350 map->getDataLength());
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -07001351 } else {
1352 Animation::Frame frame;
Tomasz Wasilczyk9dcea572023-08-22 20:39:20 +00001353 frame.name = leaf.c_str();
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -07001354 frame.map = map;
Geoffrey Pitschdd214a72016-06-27 17:14:30 -04001355 frame.trimWidth = animation.width;
1356 frame.trimHeight = animation.height;
1357 frame.trimX = 0;
1358 frame.trimY = 0;
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -07001359 part.frames.add(frame);
1360 }
Mathias Agopiana8826d62009-10-01 03:10:14 -07001361 }
Geoffrey Pitschd6d9a1d2016-06-08 00:38:58 -07001362 } else {
Wei Wang159a4102018-10-23 23:15:58 -07001363 SLOGE("bootanimation.zip is compressed; must be only stored");
Mathias Agopiana8826d62009-10-01 03:10:14 -07001364 }
1365 }
1366 }
1367 }
1368 }
1369 }
1370
Geoffrey Pitschdd214a72016-06-27 17:14:30 -04001371 // If there is trimData present, override the positioning defaults.
1372 for (Animation::Part& part : animation.parts) {
Tomasz Wasilczyk3815d342023-08-10 23:54:44 +00001373 const char* trimDataStr = part.trimData.c_str();
Geoffrey Pitschdd214a72016-06-27 17:14:30 -04001374 for (size_t frameIdx = 0; frameIdx < part.frames.size(); frameIdx++) {
1375 const char* endl = strstr(trimDataStr, "\n");
1376 // No more trimData for this part.
Yi Kong911ac232019-03-24 01:49:02 -07001377 if (endl == nullptr) {
Geoffrey Pitschdd214a72016-06-27 17:14:30 -04001378 break;
1379 }
1380 String8 line(trimDataStr, endl - trimDataStr);
Tomasz Wasilczyk3815d342023-08-10 23:54:44 +00001381 const char* lineStr = line.c_str();
Geoffrey Pitschdd214a72016-06-27 17:14:30 -04001382 trimDataStr = ++endl;
1383 int width = 0, height = 0, x = 0, y = 0;
1384 if (sscanf(lineStr, "%dx%d+%d+%d", &width, &height, &x, &y) == 4) {
1385 Animation::Frame& frame(part.frames.editItemAt(frameIdx));
1386 frame.trimWidth = width;
1387 frame.trimHeight = height;
1388 frame.trimX = x;
1389 frame.trimY = y;
1390 } else {
Wei Wang159a4102018-10-23 23:15:58 -07001391 SLOGE("Error parsing trim.txt, line: %s", lineStr);
Geoffrey Pitschdd214a72016-06-27 17:14:30 -04001392 break;
1393 }
1394 }
1395 }
1396
1397 zip->endIteration(cookie);
Narayan Kamathafd31e02013-12-03 13:16:03 +00001398
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001399 return true;
1400}
1401
Marin Shalamanov9070c052020-05-18 19:05:17 +02001402bool BootAnimation::movie() {
Parth Sane7f7b2212023-04-12 17:23:02 +00001403 ATRACE_CALL();
Huihong Luo50a2f362018-08-11 09:27:57 -07001404 if (mAnimation == nullptr) {
1405 mAnimation = loadAnimation(mZipFileName);
1406 }
1407
1408 if (mAnimation == nullptr)
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001409 return false;
1410
Huihong Luo50a2f362018-08-11 09:27:57 -07001411 // mCallbacks->init() may get called recursively,
1412 // this loop is needed to get the same results
1413 for (const Animation::Part& part : mAnimation->parts) {
1414 if (part.animation != nullptr) {
1415 mCallbacks->init(part.animation->parts);
1416 }
1417 }
1418 mCallbacks->init(mAnimation->parts);
1419
Damien Bargiacchi97480862016-03-29 14:55:55 -07001420 bool anyPartHasClock = false;
Huihong Luo50a2f362018-08-11 09:27:57 -07001421 for (size_t i=0; i < mAnimation->parts.size(); i++) {
1422 if(validClock(mAnimation->parts[i])) {
Damien Bargiacchi97480862016-03-29 14:55:55 -07001423 anyPartHasClock = true;
1424 break;
1425 }
1426 }
1427 if (!anyPartHasClock) {
1428 mClockEnabled = false;
Thiébaud Weksteen08de4432021-11-18 13:25:59 +11001429 } else if (!android::base::GetBoolProperty(CLOCK_ENABLED_PROP_NAME, false)) {
1430 mClockEnabled = false;
Damien Bargiacchi97480862016-03-29 14:55:55 -07001431 }
1432
Sai Kiran Korwar27167492015-07-07 20:00:06 +05301433 // Check if npot textures are supported
1434 mUseNpotTextures = false;
1435 String8 gl_extensions;
1436 const char* exts = reinterpret_cast<const char*>(glGetString(GL_EXTENSIONS));
1437 if (!exts) {
1438 glGetError();
1439 } else {
Tomasz Wasilczyk31eb3c892023-08-23 22:12:33 +00001440 gl_extensions = exts;
Sai Kiran Korwar27167492015-07-07 20:00:06 +05301441 if ((gl_extensions.find("GL_ARB_texture_non_power_of_two") != -1) ||
1442 (gl_extensions.find("GL_OES_texture_npot") != -1)) {
1443 mUseNpotTextures = true;
1444 }
1445 }
1446
Damien Bargiacchia704b7d2016-02-16 16:55:49 -08001447 // Blend required to draw time on top of animation frames.
1448 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
Mathias Agopiana8826d62009-10-01 03:10:14 -07001449 glDisable(GL_DITHER);
1450 glDisable(GL_SCISSOR_TEST);
1451 glDisable(GL_BLEND);
Mathias Agopiana8826d62009-10-01 03:10:14 -07001452
Greg Kaiser2ded8002021-07-19 17:03:27 +00001453 glEnable(GL_TEXTURE_2D);
Lucas Dupin8eedc022021-07-29 21:49:07 +00001454 glBindTexture(GL_TEXTURE_2D, 0);
1455 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
1456 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
1457 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
1458 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -07001459 bool clockFontInitialized = false;
Damien Bargiacchia704b7d2016-02-16 16:55:49 -08001460 if (mClockEnabled) {
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -07001461 clockFontInitialized =
Huihong Luo50a2f362018-08-11 09:27:57 -07001462 (initFont(&mAnimation->clockFont, CLOCK_FONT_ASSET) == NO_ERROR);
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -07001463 mClockEnabled = clockFontInitialized;
Damien Bargiacchia704b7d2016-02-16 16:55:49 -08001464 }
1465
Nicolas Geoffray41fffb42020-12-15 10:50:29 +00001466 initFont(&mAnimation->progressFont, PROGRESS_FONT_ASSET);
1467
Damien Bargiacchi97480862016-03-29 14:55:55 -07001468 if (mClockEnabled && !updateIsTimeAccurate()) {
1469 mTimeCheckThread = new TimeCheckThread(this);
1470 mTimeCheckThread->run("BootAnimation::TimeCheckThread", PRIORITY_NORMAL);
1471 }
1472
George Burgess IV0f8e8112021-08-23 17:32:23 -07001473 if (mAnimation->dynamicColoringEnabled) {
Shan Huang7fee3522021-08-18 00:56:08 +00001474 initDynamicColors();
1475 }
1476
Huihong Luo50a2f362018-08-11 09:27:57 -07001477 playAnimation(*mAnimation);
Damien Bargiacchi97480862016-03-29 14:55:55 -07001478
Geoffrey Pitscha9173532016-07-19 14:56:39 -04001479 if (mTimeCheckThread != nullptr) {
Damien Bargiacchi97480862016-03-29 14:55:55 -07001480 mTimeCheckThread->requestExit();
Geoffrey Pitscha9173532016-07-19 14:56:39 -04001481 mTimeCheckThread = nullptr;
1482 }
1483
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -07001484 if (clockFontInitialized) {
Huihong Luo50a2f362018-08-11 09:27:57 -07001485 glDeleteTextures(1, &mAnimation->clockFont.texture.name);
Andriy Naborskyy815e51d2016-03-24 16:43:34 -07001486 }
1487
Huihong Luo50a2f362018-08-11 09:27:57 -07001488 releaseAnimation(mAnimation);
1489 mAnimation = nullptr;
Damien Bargiacchi1a8a2132018-07-24 15:20:26 -07001490
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001491 return false;
1492}
1493
Nicolas Geoffraya1298752021-04-20 15:15:06 +01001494bool BootAnimation::shouldStopPlayingPart(const Animation::Part& part,
1495 const int fadedFramesCount,
1496 const int lastDisplayedProgress) {
Parth Sane7f7b2212023-04-12 17:23:02 +00001497 ATRACE_CALL();
Yegor Malyshev6925e1b2020-09-10 16:42:20 +02001498 // 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 +01001499 return exitPending() && !part.playUntilComplete && fadedFramesCount >= part.framesToFadeCount &&
1500 (lastDisplayedProgress == 0 || lastDisplayedProgress == 100);
Yegor Malyshev6925e1b2020-09-10 16:42:20 +02001501}
1502
Lucas Dupin8eedc022021-07-29 21:49:07 +00001503// Linear mapping from range <a1, a2> to range <b1, b2>
1504float mapLinear(float x, float a1, float a2, float b1, float b2) {
1505 return b1 + ( x - a1 ) * ( b2 - b1 ) / ( a2 - a1 );
1506}
1507
1508void BootAnimation::drawTexturedQuad(float xStart, float yStart, float width, float height) {
Parth Sane7f7b2212023-04-12 17:23:02 +00001509 ATRACE_CALL();
Lucas Dupin8eedc022021-07-29 21:49:07 +00001510 // Map coordinates from screen space to world space.
1511 float x0 = mapLinear(xStart, 0, mWidth, -1, 1);
1512 float y0 = mapLinear(yStart, 0, mHeight, -1, 1);
1513 float x1 = mapLinear(xStart + width, 0, mWidth, -1, 1);
1514 float y1 = mapLinear(yStart + height, 0, mHeight, -1, 1);
1515 // Update quad vertex positions.
1516 quadPositions[0] = x0;
1517 quadPositions[1] = y0;
1518 quadPositions[2] = x1;
1519 quadPositions[3] = y0;
1520 quadPositions[4] = x1;
1521 quadPositions[5] = y1;
1522 quadPositions[6] = x1;
1523 quadPositions[7] = y1;
1524 quadPositions[8] = x0;
1525 quadPositions[9] = y1;
1526 quadPositions[10] = x0;
1527 quadPositions[11] = y0;
1528 glDrawArrays(GL_TRIANGLES, 0,
1529 sizeof(quadPositions) / sizeof(quadPositions[0]) / 2);
1530}
1531
Shan Huang7fee3522021-08-18 00:56:08 +00001532void BootAnimation::initDynamicColors() {
Parth Sane7f7b2212023-04-12 17:23:02 +00001533 ATRACE_CALL();
Shan Huang7fee3522021-08-18 00:56:08 +00001534 for (int i = 0; i < DYNAMIC_COLOR_COUNT; i++) {
Lucas Dupincd256842022-03-29 15:46:36 -07001535 const auto syspropName = "persist.bootanim.color" + std::to_string(i + 1);
1536 const auto syspropValue = android::base::GetProperty(syspropName, "");
1537 if (syspropValue != "") {
1538 SLOGI("Loaded dynamic color: %s -> %s", syspropName.c_str(), syspropValue.c_str());
1539 mDynamicColorsApplied = true;
1540 }
1541 parseColorDecimalString(syspropValue,
Shan Huang7fee3522021-08-18 00:56:08 +00001542 mAnimation->endColors[i], mAnimation->startColors[i]);
1543 }
1544 glUseProgram(mImageShader);
Lucas Dupincd256842022-03-29 15:46:36 -07001545 SLOGI("Dynamically coloring boot animation. Sysprops loaded? %i", mDynamicColorsApplied);
Shan Huang7fee3522021-08-18 00:56:08 +00001546 for (int i = 0; i < DYNAMIC_COLOR_COUNT; i++) {
1547 float *startColor = mAnimation->startColors[i];
1548 float *endColor = mAnimation->endColors[i];
Andrew Zengf9f58222022-11-08 11:10:48 -08001549 glUniform3f(glGetUniformLocation(mImageShader,
Shan Huang7fee3522021-08-18 00:56:08 +00001550 (U_START_COLOR_PREFIX + std::to_string(i)).c_str()),
Andrew Zengf9f58222022-11-08 11:10:48 -08001551 startColor[0], startColor[1], startColor[2]);
1552 glUniform3f(glGetUniformLocation(mImageShader,
Shan Huang7fee3522021-08-18 00:56:08 +00001553 (U_END_COLOR_PREFIX + std::to_string(i)).c_str()),
Andrew Zengf9f58222022-11-08 11:10:48 -08001554 endColor[0], endColor[1], endColor[2]);
Shan Huang7fee3522021-08-18 00:56:08 +00001555 }
1556 mImageColorProgressLocation = glGetUniformLocation(mImageShader, U_COLOR_PROGRESS);
1557}
1558
Marin Shalamanov9070c052020-05-18 19:05:17 +02001559bool BootAnimation::playAnimation(const Animation& animation) {
Parth Sane7f7b2212023-04-12 17:23:02 +00001560 ATRACE_CALL();
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001561 const size_t pcount = animation.parts.size();
Mathias Agopiana8826d62009-10-01 03:10:14 -07001562 nsecs_t frameDuration = s2ns(1) / animation.fps;
Mathias Agopian9f3020d2009-11-06 16:30:18 -08001563
Wei Wang159a4102018-10-23 23:15:58 -07001564 SLOGD("%sAnimationShownTiming start time: %" PRId64 "ms", mShuttingDown ? "Shutdown" : "Boot",
Keun-young Parkd1794cd2017-04-04 12:21:19 -07001565 elapsedRealtime());
Yegor Malyshev6925e1b2020-09-10 16:42:20 +02001566
1567 int fadedFramesCount = 0;
Nicolas Geoffray41fffb42020-12-15 10:50:29 +00001568 int lastDisplayedProgress = 0;
Lucas Dupinbfd18122022-04-25 11:26:08 -07001569 int colorTransitionStart = animation.colorTransitionStart;
1570 int colorTransitionEnd = animation.colorTransitionEnd;
Narayan Kamathafd31e02013-12-03 13:16:03 +00001571 for (size_t i=0 ; i<pcount ; i++) {
Mathias Agopiana8826d62009-10-01 03:10:14 -07001572 const Animation::Part& part(animation.parts[i]);
1573 const size_t fcount = part.frames.size();
Oleg Lyovin6a4b84d2023-07-01 14:39:59 +03001574 glBindTexture(GL_TEXTURE_2D, 0);
Mathias Agopiana8826d62009-10-01 03:10:14 -07001575
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001576 // Handle animation package
Yi Kong911ac232019-03-24 01:49:02 -07001577 if (part.animation != nullptr) {
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001578 playAnimation(*part.animation);
1579 if (exitPending())
1580 break;
1581 continue; //to next part
1582 }
1583
Yegor Malyshev6925e1b2020-09-10 16:42:20 +02001584 // process the part not only while the count allows but also if already fading
1585 for (int r=0 ; !part.count || r<part.count || fadedFramesCount > 0 ; r++) {
Nicolas Geoffraya1298752021-04-20 15:15:06 +01001586 if (shouldStopPlayingPart(part, fadedFramesCount, lastDisplayedProgress)) break;
Kevin Hesterd3782b22012-04-26 10:38:55 -07001587
Lucas Dupinbfd18122022-04-25 11:26:08 -07001588 // It's possible that the sysprops were not loaded yet at this boot phase.
1589 // If that's the case, then we should keep trying until they are available.
1590 if (animation.dynamicColoringEnabled && !mDynamicColorsApplied
1591 && (part.useDynamicColoring || part.postDynamicColoring)) {
1592 SLOGD("Trying to load dynamic color sysprops.");
1593 initDynamicColors();
1594 if (mDynamicColorsApplied) {
1595 // Sysprops were loaded. Next step is to adjust the animation if we loaded
1596 // the colors after the animation should have started.
1597 const int transitionLength = colorTransitionEnd - colorTransitionStart;
1598 if (part.postDynamicColoring) {
1599 colorTransitionStart = 0;
1600 colorTransitionEnd = fmin(transitionLength, fcount - 1);
1601 }
1602 }
1603 }
1604
Ed Coyne7464ac92017-06-08 12:26:48 -07001605 mCallbacks->playPart(i, part, r);
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -07001606
Jesse Hall083b84c2014-09-22 10:51:09 -07001607 glClearColor(
1608 part.backgroundColor[0],
1609 part.backgroundColor[1],
1610 part.backgroundColor[2],
1611 1.0f);
1612
Yoshiaki Maruoka9ef19b82017-10-25 13:55:49 +09001613 ALOGD("Playing files = %s/%s, Requested repeat = %d, playUntilComplete = %s",
Tomasz Wasilczyk3815d342023-08-10 23:54:44 +00001614 animation.fileName.c_str(), part.path.c_str(), part.count,
Yoshiaki Maruoka9ef19b82017-10-25 13:55:49 +09001615 part.playUntilComplete ? "true" : "false");
1616
Nicolas Geoffray41fffb42020-12-15 10:50:29 +00001617 // For the last animation, if we have progress indicator from
1618 // the system, display it.
1619 int currentProgress = android::base::GetIntProperty(PROGRESS_PROP_NAME, 0);
1620 bool displayProgress = animation.progressEnabled &&
1621 (i == (pcount -1)) && currentProgress != 0;
1622
Yegor Malyshev6925e1b2020-09-10 16:42:20 +02001623 for (size_t j=0 ; j<fcount ; j++) {
Nicolas Geoffraya1298752021-04-20 15:15:06 +01001624 if (shouldStopPlayingPart(part, fadedFramesCount, lastDisplayedProgress)) break;
Yegor Malyshev6925e1b2020-09-10 16:42:20 +02001625
Shan Huang2b31a362021-08-16 16:45:39 +00001626 // Color progress is
Shan Huang488ff742021-08-25 20:10:46 +00001627 // - the animation progress, normalized from
1628 // [colorTransitionStart,colorTransitionEnd] to [0, 1] for the dynamic coloring
1629 // part.
Shan Huang2b31a362021-08-16 16:45:39 +00001630 // - 0 for parts that come before,
1631 // - 1 for parts that come after.
1632 float colorProgress = part.useDynamicColoring
Shan Huang488ff742021-08-25 20:10:46 +00001633 ? fmin(fmax(
Lucas Dupinbfd18122022-04-25 11:26:08 -07001634 ((float)j - colorTransitionStart) /
1635 fmax(colorTransitionEnd - colorTransitionStart, 1.0f), 0.0f), 1.0f)
Shan Huang2b31a362021-08-16 16:45:39 +00001636 : (part.postDynamicColoring ? 1 : 0);
1637
Marin Shalamanov9070c052020-05-18 19:05:17 +02001638 processDisplayEvents();
1639
joenchen85b339f2022-04-11 23:58:34 +08001640 const double ratio_w = static_cast<double>(mWidth) / mInitWidth;
1641 const double ratio_h = static_cast<double>(mHeight) / mInitHeight;
1642 const int animationX = (mWidth - animation.width * ratio_w) / 2;
1643 const int animationY = (mHeight - animation.height * ratio_h) / 2;
Marin Shalamanov9070c052020-05-18 19:05:17 +02001644
Mathias Agopiana8826d62009-10-01 03:10:14 -07001645 const Animation::Frame& frame(part.frames[j]);
Mathias Agopiandb7dd2a2012-05-12 15:08:21 -07001646 nsecs_t lastFrame = systemTime();
Mathias Agopiana8826d62009-10-01 03:10:14 -07001647
1648 if (r > 0) {
1649 glBindTexture(GL_TEXTURE_2D, frame.tid);
1650 } else {
Oleg Lyovin6a4b84d2023-07-01 14:39:59 +03001651 if (part.count != 1) {
1652 glGenTextures(1, &frame.tid);
1653 glBindTexture(GL_TEXTURE_2D, frame.tid);
1654 }
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -07001655 int w, h;
Shan Huang2b31a362021-08-16 16:45:39 +00001656 // Set decoding option to alpha unpremultiplied so that the R, G, B channels
1657 // of transparent pixels are preserved.
1658 initTexture(frame.map, &w, &h, false /* don't premultiply alpha */);
Mathias Agopiana8826d62009-10-01 03:10:14 -07001659 }
1660
joenchen85b339f2022-04-11 23:58:34 +08001661 const int trimWidth = frame.trimWidth * ratio_w;
1662 const int trimHeight = frame.trimHeight * ratio_h;
1663 const int trimX = frame.trimX * ratio_w;
1664 const int trimY = frame.trimY * ratio_h;
1665 const int xc = animationX + trimX;
1666 const int yc = animationY + trimY;
Shan Huang2b31a362021-08-16 16:45:39 +00001667 glClear(GL_COLOR_BUFFER_BIT);
Geoffrey Pitschdd214a72016-06-27 17:14:30 -04001668 // specify the y center as ceiling((mHeight - frame.trimHeight) / 2)
1669 // which is equivalent to mHeight - (yc + frame.trimHeight)
joenchen85b339f2022-04-11 23:58:34 +08001670 const int frameDrawY = mHeight - (yc + trimHeight);
Yegor Malyshev6925e1b2020-09-10 16:42:20 +02001671
Lucas Dupin8eedc022021-07-29 21:49:07 +00001672 float fade = 0;
Yegor Malyshev6925e1b2020-09-10 16:42:20 +02001673 // if the part hasn't been stopped yet then continue fading if necessary
1674 if (exitPending() && part.hasFadingPhase()) {
Lucas Dupin8eedc022021-07-29 21:49:07 +00001675 fade = static_cast<float>(++fadedFramesCount) / part.framesToFadeCount;
Yegor Malyshev6925e1b2020-09-10 16:42:20 +02001676 if (fadedFramesCount >= part.framesToFadeCount) {
1677 fadedFramesCount = MAX_FADED_FRAMES_COUNT; // no more fading
1678 }
1679 }
Lucas Dupin8eedc022021-07-29 21:49:07 +00001680 glUseProgram(mImageShader);
1681 glUniform1i(mImageTextureLocation, 0);
1682 glUniform1f(mImageFadeLocation, fade);
Shan Huang2b31a362021-08-16 16:45:39 +00001683 if (animation.dynamicColoringEnabled) {
1684 glUniform1f(mImageColorProgressLocation, colorProgress);
1685 }
Lucas Dupin8eedc022021-07-29 21:49:07 +00001686 glEnable(GL_BLEND);
joenchen85b339f2022-04-11 23:58:34 +08001687 drawTexturedQuad(xc, frameDrawY, trimWidth, trimHeight);
Lucas Dupin8eedc022021-07-29 21:49:07 +00001688 glDisable(GL_BLEND);
Yegor Malyshev6925e1b2020-09-10 16:42:20 +02001689
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -07001690 if (mClockEnabled && mTimeIsAccurate && validClock(part)) {
1691 drawClock(animation.clockFont, part.clockPosX, part.clockPosY);
Damien Bargiacchia704b7d2016-02-16 16:55:49 -08001692 }
Yegor Malyshev6925e1b2020-09-10 16:42:20 +02001693
Nicolas Geoffray41fffb42020-12-15 10:50:29 +00001694 if (displayProgress) {
1695 int newProgress = android::base::GetIntProperty(PROGRESS_PROP_NAME, 0);
1696 // In case the new progress jumped suddenly, still show an
1697 // increment of 1.
1698 if (lastDisplayedProgress != 100) {
1699 // Artificially sleep 1/10th a second to slow down the animation.
1700 usleep(100000);
1701 if (lastDisplayedProgress < newProgress) {
1702 lastDisplayedProgress++;
1703 }
1704 }
1705 // Put the progress percentage right below the animation.
1706 int posY = animation.height / 3;
1707 int posX = TEXT_CENTER_VALUE;
1708 drawProgress(lastDisplayedProgress, animation.progressFont, posX, posY);
1709 }
1710
Adrian Roos9ee5dff2018-08-22 20:19:49 +02001711 handleViewport(frameDuration);
Damien Bargiacchia704b7d2016-02-16 16:55:49 -08001712
Mathias Agopiana8826d62009-10-01 03:10:14 -07001713 eglSwapBuffers(mDisplay, mSurface);
1714
1715 nsecs_t now = systemTime();
1716 nsecs_t delay = frameDuration - (now - lastFrame);
Wei Wang159a4102018-10-23 23:15:58 -07001717 //SLOGD("%lld, %lld", ns2ms(now - lastFrame), ns2ms(delay));
Mathias Agopiana8826d62009-10-01 03:10:14 -07001718 lastFrame = now;
Mathias Agopiandb7dd2a2012-05-12 15:08:21 -07001719
1720 if (delay > 0) {
1721 struct timespec spec;
1722 spec.tv_sec = (now + delay) / 1000000000;
1723 spec.tv_nsec = (now + delay) % 1000000000;
1724 int err;
1725 do {
Yi Kong911ac232019-03-24 01:49:02 -07001726 err = clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &spec, nullptr);
Keith Mok0f2e6682021-11-15 05:50:32 +00001727 } while (err == EINTR);
Mathias Agopiandb7dd2a2012-05-12 15:08:21 -07001728 }
Kevin Hesterd3782b22012-04-26 10:38:55 -07001729
1730 checkExit();
Mathias Agopiana8826d62009-10-01 03:10:14 -07001731 }
Kevin Hesterd3782b22012-04-26 10:38:55 -07001732
niuwenchao9cd6ac82022-12-23 17:34:22 +08001733 int pauseDuration = part.pause * ns2us(frameDuration);
1734 while(pauseDuration > 0 && !exitPending()){
1735 if (pauseDuration > MAX_CHECK_EXIT_INTERVAL_US) {
1736 usleep(MAX_CHECK_EXIT_INTERVAL_US);
1737 pauseDuration -= MAX_CHECK_EXIT_INTERVAL_US;
1738 } else {
1739 usleep(pauseDuration);
1740 break;
1741 }
1742 checkExit();
1743 }
Kevin Hesterd3782b22012-04-26 10:38:55 -07001744
Yegor Malyshev6925e1b2020-09-10 16:42:20 +02001745 if (exitPending() && !part.count && mCurrentInset >= mTargetInset &&
1746 !part.hasFadingPhase()) {
Nicolas Geoffraya1298752021-04-20 15:15:06 +01001747 if (lastDisplayedProgress != 0 && lastDisplayedProgress != 100) {
1748 android::base::SetProperty(PROGRESS_PROP_NAME, "100");
1749 continue;
1750 }
Yegor Malyshev6925e1b2020-09-10 16:42:20 +02001751 break; // exit the infinite non-fading part when it has been played at least once
1752 }
Mathias Agopiana8826d62009-10-01 03:10:14 -07001753 }
Geoffrey Pitsch2fb30fb2016-07-06 16:16:20 -04001754 }
1755
1756 // Free textures created for looping parts now that the animation is done.
1757 for (const Animation::Part& part : animation.parts) {
Mathias Agopiana8826d62009-10-01 03:10:14 -07001758 if (part.count != 1) {
Geoffrey Pitsch2fb30fb2016-07-06 16:16:20 -04001759 const size_t fcount = part.frames.size();
1760 for (size_t j = 0; j < fcount; j++) {
Mathias Agopiana8826d62009-10-01 03:10:14 -07001761 const Animation::Frame& frame(part.frames[j]);
1762 glDeleteTextures(1, &frame.tid);
1763 }
1764 }
1765 }
Geoffrey Pitschd6d9a1d2016-06-08 00:38:58 -07001766
Yoshiaki Maruoka9ef19b82017-10-25 13:55:49 +09001767 ALOGD("%sAnimationShownTiming End time: %" PRId64 "ms", mShuttingDown ? "Shutdown" : "Boot",
1768 elapsedRealtime());
1769
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001770 return true;
Mathias Agopiana8826d62009-10-01 03:10:14 -07001771}
1772
Marin Shalamanov9070c052020-05-18 19:05:17 +02001773void BootAnimation::processDisplayEvents() {
Parth Sane7f7b2212023-04-12 17:23:02 +00001774 ATRACE_CALL();
Marin Shalamanov9070c052020-05-18 19:05:17 +02001775 // This will poll mDisplayEventReceiver and if there are new events it'll call
1776 // displayEventCallback synchronously.
1777 mLooper->pollOnce(0);
1778}
1779
Adrian Roos9ee5dff2018-08-22 20:19:49 +02001780void BootAnimation::handleViewport(nsecs_t timestep) {
Parth Sane7f7b2212023-04-12 17:23:02 +00001781 ATRACE_CALL();
Adrian Roos9ee5dff2018-08-22 20:19:49 +02001782 if (mShuttingDown || !mFlingerSurfaceControl || mTargetInset == 0) {
1783 return;
1784 }
1785 if (mTargetInset < 0) {
1786 // Poll the amount for the top display inset. This will return -1 until persistent properties
1787 // have been loaded.
1788 mTargetInset = android::base::GetIntProperty("persist.sys.displayinset.top",
1789 -1 /* default */, -1 /* min */, mHeight / 2 /* max */);
1790 }
1791 if (mTargetInset <= 0) {
1792 return;
1793 }
1794
1795 if (mCurrentInset < mTargetInset) {
1796 // After the device boots, the inset will effectively be cropped away. We animate this here.
1797 float fraction = static_cast<float>(mCurrentInset) / mTargetInset;
1798 int interpolatedInset = (cosf((fraction + 1) * M_PI) / 2.0f + 0.5f) * mTargetInset;
1799
1800 SurfaceComposerClient::Transaction()
1801 .setCrop(mFlingerSurfaceControl, Rect(0, interpolatedInset, mWidth, mHeight))
1802 .apply();
1803 } else {
1804 // At the end of the animation, we switch to the viewport that DisplayManager will apply
1805 // later. This changes the coordinate system, and means we must move the surface up by
1806 // the inset amount.
Adrian Roos9ee5dff2018-08-22 20:19:49 +02001807 Rect layerStackRect(0, 0, mWidth, mHeight - mTargetInset);
1808 Rect displayRect(0, mTargetInset, mWidth, mHeight);
1809
1810 SurfaceComposerClient::Transaction t;
1811 t.setPosition(mFlingerSurfaceControl, 0, -mTargetInset)
1812 .setCrop(mFlingerSurfaceControl, Rect(0, mTargetInset, mWidth, mHeight));
Dominik Laskowski2a3d9aa2019-11-18 20:26:39 -08001813 t.setDisplayProjection(mDisplayToken, ui::ROTATION_0, layerStackRect, displayRect);
Adrian Roos9ee5dff2018-08-22 20:19:49 +02001814 t.apply();
1815
1816 mTargetInset = mCurrentInset = 0;
1817 }
1818
1819 int delta = timestep * mTargetInset / ms2ns(200);
1820 mCurrentInset += delta;
1821}
1822
Marin Shalamanov9070c052020-05-18 19:05:17 +02001823void BootAnimation::releaseAnimation(Animation* animation) const {
Parth Sane7f7b2212023-04-12 17:23:02 +00001824 ATRACE_CALL();
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001825 for (Vector<Animation::Part>::iterator it = animation->parts.begin(),
1826 e = animation->parts.end(); it != e; ++it) {
1827 if (it->animation)
1828 releaseAnimation(it->animation);
1829 }
1830 if (animation->zip)
1831 delete animation->zip;
1832 delete animation;
1833}
1834
Marin Shalamanov9070c052020-05-18 19:05:17 +02001835BootAnimation::Animation* BootAnimation::loadAnimation(const String8& fn) {
Parth Sane7f7b2212023-04-12 17:23:02 +00001836 ATRACE_CALL();
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001837 if (mLoadedFiles.indexOf(fn) >= 0) {
Wei Wang159a4102018-10-23 23:15:58 -07001838 SLOGE("File \"%s\" is already loaded. Cyclic ref is not allowed",
Tomasz Wasilczyk3815d342023-08-10 23:54:44 +00001839 fn.c_str());
Yi Kong911ac232019-03-24 01:49:02 -07001840 return nullptr;
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001841 }
Tomasz Wasilczyk835dfe52023-08-17 16:27:22 +00001842 ZipFileRO *zip = ZipFileRO::open(fn.c_str());
Yi Kong911ac232019-03-24 01:49:02 -07001843 if (zip == nullptr) {
Wei Wang159a4102018-10-23 23:15:58 -07001844 SLOGE("Failed to open animation zip \"%s\": %s",
Tomasz Wasilczyk3815d342023-08-10 23:54:44 +00001845 fn.c_str(), strerror(errno));
Yi Kong911ac232019-03-24 01:49:02 -07001846 return nullptr;
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001847 }
1848
Tomasz Wasilczyk3815d342023-08-10 23:54:44 +00001849 ALOGD("%s is loaded successfully", fn.c_str());
Yoshiaki Maruoka9ef19b82017-10-25 13:55:49 +09001850
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001851 Animation *animation = new Animation;
1852 animation->fileName = fn;
1853 animation->zip = zip;
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -07001854 animation->clockFont.map = nullptr;
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001855 mLoadedFiles.add(animation->fileName);
1856
1857 parseAnimationDesc(*animation);
Geoffrey Pitscha91a2d72016-07-12 14:46:19 -04001858 if (!preloadZip(*animation)) {
Greg Kaiser7426ec82019-09-11 14:34:27 -07001859 releaseAnimation(animation);
Yi Kong911ac232019-03-24 01:49:02 -07001860 return nullptr;
Geoffrey Pitscha91a2d72016-07-12 14:46:19 -04001861 }
1862
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001863 mLoadedFiles.remove(fn);
1864 return animation;
1865}
Damien Bargiacchi97480862016-03-29 14:55:55 -07001866
1867bool BootAnimation::updateIsTimeAccurate() {
Parth Sane7f7b2212023-04-12 17:23:02 +00001868 ATRACE_CALL();
Damien Bargiacchi97480862016-03-29 14:55:55 -07001869 static constexpr long long MAX_TIME_IN_PAST = 60000LL * 60LL * 24LL * 30LL; // 30 days
1870 static constexpr long long MAX_TIME_IN_FUTURE = 60000LL * 90LL; // 90 minutes
1871
1872 if (mTimeIsAccurate) {
1873 return true;
1874 }
Keun-young Parkb5938422017-03-23 13:46:24 -07001875 if (mShuttingDown) return true;
Damien Bargiacchi97480862016-03-29 14:55:55 -07001876 struct stat statResult;
Damien Bargiacchi9071db12016-10-28 17:38:22 -07001877
1878 if(stat(TIME_FORMAT_12_HOUR_FLAG_FILE_PATH, &statResult) == 0) {
1879 mTimeFormat12Hour = true;
1880 }
1881
Damien Bargiacchi97480862016-03-29 14:55:55 -07001882 if(stat(ACCURATE_TIME_FLAG_FILE_PATH, &statResult) == 0) {
1883 mTimeIsAccurate = true;
1884 return true;
1885 }
1886
1887 FILE* file = fopen(LAST_TIME_CHANGED_FILE_PATH, "r");
Yi Kong911ac232019-03-24 01:49:02 -07001888 if (file != nullptr) {
Damien Bargiacchi97480862016-03-29 14:55:55 -07001889 long long lastChangedTime = 0;
1890 fscanf(file, "%lld", &lastChangedTime);
1891 fclose(file);
1892 if (lastChangedTime > 0) {
1893 struct timespec now;
1894 clock_gettime(CLOCK_REALTIME, &now);
1895 // Match the Java timestamp format
1896 long long rtcNow = (now.tv_sec * 1000LL) + (now.tv_nsec / 1000000LL);
Damien Bargiacchi96762812016-07-12 15:53:40 -07001897 if (ACCURATE_TIME_EPOCH < rtcNow
1898 && lastChangedTime > (rtcNow - MAX_TIME_IN_PAST)
1899 && lastChangedTime < (rtcNow + MAX_TIME_IN_FUTURE)) {
Damien Bargiacchi97480862016-03-29 14:55:55 -07001900 mTimeIsAccurate = true;
1901 }
1902 }
1903 }
1904
1905 return mTimeIsAccurate;
1906}
1907
1908BootAnimation::TimeCheckThread::TimeCheckThread(BootAnimation* bootAnimation) : Thread(false),
Josh Yangf95b2002021-12-23 14:32:28 -08001909 mInotifyFd(-1), mBootAnimWd(-1), mTimeWd(-1), mBootAnimation(bootAnimation) {}
Damien Bargiacchi97480862016-03-29 14:55:55 -07001910
1911BootAnimation::TimeCheckThread::~TimeCheckThread() {
Parth Sane7f7b2212023-04-12 17:23:02 +00001912 ATRACE_CALL();
Damien Bargiacchi97480862016-03-29 14:55:55 -07001913 // mInotifyFd may be -1 but that's ok since we're not at risk of attempting to close a valid FD.
1914 close(mInotifyFd);
1915}
1916
1917bool BootAnimation::TimeCheckThread::threadLoop() {
Parth Sane7f7b2212023-04-12 17:23:02 +00001918 ATRACE_CALL();
Damien Bargiacchi97480862016-03-29 14:55:55 -07001919 bool shouldLoop = doThreadLoop() && !mBootAnimation->mTimeIsAccurate
1920 && mBootAnimation->mClockEnabled;
1921 if (!shouldLoop) {
1922 close(mInotifyFd);
1923 mInotifyFd = -1;
1924 }
1925 return shouldLoop;
1926}
1927
1928bool BootAnimation::TimeCheckThread::doThreadLoop() {
Parth Sane7f7b2212023-04-12 17:23:02 +00001929 ATRACE_CALL();
Damien Bargiacchi97480862016-03-29 14:55:55 -07001930 static constexpr int BUFF_LEN (10 * (sizeof(struct inotify_event) + NAME_MAX + 1));
1931
1932 // Poll instead of doing a blocking read so the Thread can exit if requested.
1933 struct pollfd pfd = { mInotifyFd, POLLIN, 0 };
1934 ssize_t pollResult = poll(&pfd, 1, 1000);
1935
1936 if (pollResult == 0) {
1937 return true;
1938 } else if (pollResult < 0) {
Wei Wang159a4102018-10-23 23:15:58 -07001939 SLOGE("Could not poll inotify events");
Damien Bargiacchi97480862016-03-29 14:55:55 -07001940 return false;
1941 }
1942
1943 char buff[BUFF_LEN] __attribute__ ((aligned(__alignof__(struct inotify_event))));;
1944 ssize_t length = read(mInotifyFd, buff, BUFF_LEN);
1945 if (length == 0) {
1946 return true;
1947 } else if (length < 0) {
Wei Wang159a4102018-10-23 23:15:58 -07001948 SLOGE("Could not read inotify events");
Damien Bargiacchi97480862016-03-29 14:55:55 -07001949 return false;
1950 }
1951
1952 const struct inotify_event *event;
1953 for (char* ptr = buff; ptr < buff + length; ptr += sizeof(struct inotify_event) + event->len) {
1954 event = (const struct inotify_event *) ptr;
Josh Yangf95b2002021-12-23 14:32:28 -08001955 if (event->wd == mBootAnimWd && strcmp(BOOTANIM_TIME_DIR_NAME, event->name) == 0) {
Damien Bargiacchi97480862016-03-29 14:55:55 -07001956 addTimeDirWatch();
1957 } else if (event->wd == mTimeWd && (strcmp(LAST_TIME_CHANGED_FILE_NAME, event->name) == 0
1958 || strcmp(ACCURATE_TIME_FLAG_FILE_NAME, event->name) == 0)) {
1959 return !mBootAnimation->updateIsTimeAccurate();
1960 }
1961 }
1962
1963 return true;
1964}
1965
1966void BootAnimation::TimeCheckThread::addTimeDirWatch() {
Parth Sane7f7b2212023-04-12 17:23:02 +00001967 ATRACE_CALL();
Josh Yangf95b2002021-12-23 14:32:28 -08001968 mTimeWd = inotify_add_watch(mInotifyFd, BOOTANIM_TIME_DIR_PATH,
Damien Bargiacchi97480862016-03-29 14:55:55 -07001969 IN_CLOSE_WRITE | IN_MOVED_TO | IN_ATTRIB);
1970 if (mTimeWd > 0) {
1971 // No need to watch for the time directory to be created if it already exists
Josh Yangf95b2002021-12-23 14:32:28 -08001972 inotify_rm_watch(mInotifyFd, mBootAnimWd);
1973 mBootAnimWd = -1;
Damien Bargiacchi97480862016-03-29 14:55:55 -07001974 }
1975}
1976
1977status_t BootAnimation::TimeCheckThread::readyToRun() {
Parth Sane7f7b2212023-04-12 17:23:02 +00001978 ATRACE_CALL();
Damien Bargiacchi97480862016-03-29 14:55:55 -07001979 mInotifyFd = inotify_init();
1980 if (mInotifyFd < 0) {
Wei Wang159a4102018-10-23 23:15:58 -07001981 SLOGE("Could not initialize inotify fd");
Damien Bargiacchi97480862016-03-29 14:55:55 -07001982 return NO_INIT;
1983 }
1984
Josh Yangf95b2002021-12-23 14:32:28 -08001985 mBootAnimWd = inotify_add_watch(mInotifyFd, BOOTANIM_DATA_DIR_PATH, IN_CREATE | IN_ATTRIB);
1986 if (mBootAnimWd < 0) {
Damien Bargiacchi97480862016-03-29 14:55:55 -07001987 close(mInotifyFd);
1988 mInotifyFd = -1;
Josh Yangf95b2002021-12-23 14:32:28 -08001989 SLOGE("Could not add watch for %s: %s", BOOTANIM_DATA_DIR_PATH, strerror(errno));
Damien Bargiacchi97480862016-03-29 14:55:55 -07001990 return NO_INIT;
1991 }
1992
1993 addTimeDirWatch();
1994
1995 if (mBootAnimation->updateIsTimeAccurate()) {
1996 close(mInotifyFd);
1997 mInotifyFd = -1;
1998 return ALREADY_EXISTS;
1999 }
2000
2001 return NO_ERROR;
2002}
2003
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002004// ---------------------------------------------------------------------------
2005
Marin Shalamanov9070c052020-05-18 19:05:17 +02002006} // namespace android