blob: 89776dba787857bf067e0b3aca3699a9bdca08ff [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
Lucas Dupin8eedc022021-07-29 21:49:07 +0000401 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
402 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
403 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;
joenchen764ea042022-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 }();
678 const auto syspropValue = android::base::GetProperty(syspropName, "ORIENTATION_0");
679 if (syspropValue == "ORIENTATION_90") {
680 return ui::ROTATION_90;
681 } else if (syspropValue == "ORIENTATION_180") {
682 return ui::ROTATION_180;
683 } else if (syspropValue == "ORIENTATION_270") {
684 return ui::ROTATION_270;
685 }
686 return ui::ROTATION_0;
687}
688
Yegor Malyshev6925e1b2020-09-10 16:42:20 +0200689void BootAnimation::projectSceneToWindow() {
Parth Sane7f7b2212023-04-12 17:23:02 +0000690 ATRACE_CALL();
Yegor Malyshev6925e1b2020-09-10 16:42:20 +0200691 glViewport(0, 0, mWidth, mHeight);
692 glScissor(0, 0, mWidth, mHeight);
Yegor Malyshev6925e1b2020-09-10 16:42:20 +0200693}
694
Marin Shalamanov9070c052020-05-18 19:05:17 +0200695void BootAnimation::resizeSurface(int newWidth, int newHeight) {
Parth Sane7f7b2212023-04-12 17:23:02 +0000696 ATRACE_CALL();
Marin Shalamanov9070c052020-05-18 19:05:17 +0200697 // We assume this function is called on the animation thread.
698 if (newWidth == mWidth && newHeight == mHeight) {
699 return;
700 }
701 SLOGV("Resizing the boot animation surface to %d %d", newWidth, newHeight);
702
703 eglMakeCurrent(mDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
704 eglDestroySurface(mDisplay, mSurface);
705
joenchen764ea042022-04-11 23:58:34 +0800706 mFlingerSurfaceControl->updateDefaultBufferSize(newWidth, newHeight);
Marin Shalamanov047802d2020-05-19 23:55:12 +0200707 const auto limitedSize = limitSurfaceSize(newWidth, newHeight);
708 mWidth = limitedSize.width;
709 mHeight = limitedSize.height;
Marin Shalamanov9070c052020-05-18 19:05:17 +0200710
Marin Shalamanov9070c052020-05-18 19:05:17 +0200711 EGLConfig config = getEglConfig(mDisplay);
712 EGLSurface surface = eglCreateWindowSurface(mDisplay, config, mFlingerSurface.get(), nullptr);
713 if (eglMakeCurrent(mDisplay, surface, surface, mContext) == EGL_FALSE) {
714 SLOGE("Can't make the new surface current. Error %d", eglGetError());
715 return;
716 }
Yegor Malyshev6925e1b2020-09-10 16:42:20 +0200717
718 projectSceneToWindow();
Marin Shalamanov9070c052020-05-18 19:05:17 +0200719
720 mSurface = surface;
721}
722
Huihong Luo50a2f362018-08-11 09:27:57 -0700723bool BootAnimation::preloadAnimation() {
Parth Sane7f7b2212023-04-12 17:23:02 +0000724 ATRACE_CALL();
Huihong Luo50a2f362018-08-11 09:27:57 -0700725 findBootAnimationFile();
Tomasz Wasilczyke8fb3c72023-08-24 19:11:22 +0000726 if (!mZipFileName.empty()) {
Huihong Luo50a2f362018-08-11 09:27:57 -0700727 mAnimation = loadAnimation(mZipFileName);
728 return (mAnimation != nullptr);
729 }
730
731 return false;
732}
733
Nikita Ioffe06c986e2020-01-27 17:16:03 +0000734bool BootAnimation::findBootAnimationFileInternal(const std::vector<std::string> &files) {
Parth Sane7f7b2212023-04-12 17:23:02 +0000735 ATRACE_CALL();
Nikita Ioffe06c986e2020-01-27 17:16:03 +0000736 for (const std::string& f : files) {
737 if (access(f.c_str(), R_OK) == 0) {
738 mZipFileName = f.c_str();
739 return true;
740 }
741 }
742 return false;
743}
744
Huihong Luo50a2f362018-08-11 09:27:57 -0700745void BootAnimation::findBootAnimationFile() {
Parth Sane7f7b2212023-04-12 17:23:02 +0000746 ATRACE_CALL();
Beverly34ffe252019-05-17 11:03:54 -0400747 const bool playDarkAnim = android::base::GetIntProperty("ro.boot.theme", 0) == 1;
Nikita Ioffe06c986e2020-01-27 17:16:03 +0000748 static const std::vector<std::string> bootFiles = {
749 APEX_BOOTANIMATION_FILE, playDarkAnim ? PRODUCT_BOOTANIMATION_DARK_FILE : PRODUCT_BOOTANIMATION_FILE,
750 OEM_BOOTANIMATION_FILE, SYSTEM_BOOTANIMATION_FILE
751 };
752 static const std::vector<std::string> shutdownFiles = {
753 PRODUCT_SHUTDOWNANIMATION_FILE, OEM_SHUTDOWNANIMATION_FILE, SYSTEM_SHUTDOWNANIMATION_FILE, ""
754 };
755 static const std::vector<std::string> userspaceRebootFiles = {
756 PRODUCT_USERSPACE_REBOOT_ANIMATION_FILE, OEM_USERSPACE_REBOOT_ANIMATION_FILE,
757 SYSTEM_USERSPACE_REBOOT_ANIMATION_FILE,
758 };
Keun-young Parkb5938422017-03-23 13:46:24 -0700759
Nikita Ioffe06c986e2020-01-27 17:16:03 +0000760 if (android::base::GetBoolProperty("sys.init.userspace_reboot.in_progress", false)) {
761 findBootAnimationFileInternal(userspaceRebootFiles);
762 } else if (mShuttingDown) {
763 findBootAnimationFileInternal(shutdownFiles);
764 } else {
765 findBootAnimationFileInternal(bootFiles);
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700766 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800767}
768
Lucas Dupin8eedc022021-07-29 21:49:07 +0000769GLuint compileShader(GLenum shaderType, const GLchar *source) {
Parth Sane7f7b2212023-04-12 17:23:02 +0000770 ATRACE_CALL();
Lucas Dupin8eedc022021-07-29 21:49:07 +0000771 GLuint shader = glCreateShader(shaderType);
772 glShaderSource(shader, 1, &source, 0);
773 glCompileShader(shader);
774 GLint isCompiled = 0;
775 glGetShaderiv(shader, GL_COMPILE_STATUS, &isCompiled);
776 if (isCompiled == GL_FALSE) {
777 SLOGE("Compile shader failed. Shader type: %d", shaderType);
Shan Huang81846ba2021-09-07 19:07:47 +0000778 GLint maxLength = 0;
779 glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &maxLength);
780 std::vector<GLchar> errorLog(maxLength);
781 glGetShaderInfoLog(shader, maxLength, &maxLength, &errorLog[0]);
782 SLOGE("Shader compilation error: %s", &errorLog[0]);
Lucas Dupin8eedc022021-07-29 21:49:07 +0000783 return 0;
784 }
785 return shader;
786}
787
788GLuint linkShader(GLuint vertexShader, GLuint fragmentShader) {
Parth Sane7f7b2212023-04-12 17:23:02 +0000789 ATRACE_CALL();
Lucas Dupin8eedc022021-07-29 21:49:07 +0000790 GLuint program = glCreateProgram();
791 glAttachShader(program, vertexShader);
792 glAttachShader(program, fragmentShader);
793 glLinkProgram(program);
794 GLint isLinked = 0;
795 glGetProgramiv(program, GL_LINK_STATUS, (int *)&isLinked);
796 if (isLinked == GL_FALSE) {
797 SLOGE("Linking shader failed. Shader handles: vert %d, frag %d",
798 vertexShader, fragmentShader);
799 return 0;
800 }
801 return program;
802}
803
804void BootAnimation::initShaders() {
Parth Sane7f7b2212023-04-12 17:23:02 +0000805 ATRACE_CALL();
Shan Huang2b31a362021-08-16 16:45:39 +0000806 bool dynamicColoringEnabled = mAnimation != nullptr && mAnimation->dynamicColoringEnabled;
Lucas Dupin8eedc022021-07-29 21:49:07 +0000807 GLuint vertexShader = compileShader(GL_VERTEX_SHADER, (const GLchar *)VERTEX_SHADER_SOURCE);
808 GLuint imageFragmentShader =
Shan Huang2b31a362021-08-16 16:45:39 +0000809 compileShader(GL_FRAGMENT_SHADER, dynamicColoringEnabled
810 ? (const GLchar *)IMAGE_FRAG_DYNAMIC_COLORING_SHADER_SOURCE
811 : (const GLchar *)IMAGE_FRAG_SHADER_SOURCE);
Lucas Dupin8eedc022021-07-29 21:49:07 +0000812 GLuint textFragmentShader =
813 compileShader(GL_FRAGMENT_SHADER, (const GLchar *)TEXT_FRAG_SHADER_SOURCE);
814
815 // Initialize image shader.
816 mImageShader = linkShader(vertexShader, imageFragmentShader);
817 GLint positionLocation = glGetAttribLocation(mImageShader, A_POSITION);
818 GLint uvLocation = glGetAttribLocation(mImageShader, A_UV);
819 mImageTextureLocation = glGetUniformLocation(mImageShader, U_TEXTURE);
820 mImageFadeLocation = glGetUniformLocation(mImageShader, U_FADE);
821 glEnableVertexAttribArray(positionLocation);
822 glVertexAttribPointer(positionLocation, 2, GL_FLOAT, GL_FALSE, 0, quadPositions);
823 glVertexAttribPointer(uvLocation, 2, GL_FLOAT, GL_FALSE, 0, quadUVs);
824 glEnableVertexAttribArray(uvLocation);
825
826 // Initialize text shader.
827 mTextShader = linkShader(vertexShader, textFragmentShader);
828 positionLocation = glGetAttribLocation(mTextShader, A_POSITION);
829 uvLocation = glGetAttribLocation(mTextShader, A_UV);
830 mTextTextureLocation = glGetUniformLocation(mTextShader, U_TEXTURE);
831 mTextCropAreaLocation = glGetUniformLocation(mTextShader, U_CROP_AREA);
832 glEnableVertexAttribArray(positionLocation);
833 glVertexAttribPointer(positionLocation, 2, GL_FLOAT, GL_FALSE, 0, quadPositions);
834 glVertexAttribPointer(uvLocation, 2, GL_FLOAT, GL_FALSE, 0, quadUVs);
835 glEnableVertexAttribArray(uvLocation);
836}
837
Marin Shalamanov9070c052020-05-18 19:05:17 +0200838bool BootAnimation::threadLoop() {
Parth Sane7f7b2212023-04-12 17:23:02 +0000839 ATRACE_CALL();
Marin Shalamanov9070c052020-05-18 19:05:17 +0200840 bool result;
Lucas Dupin8eedc022021-07-29 21:49:07 +0000841 initShaders();
842
Narayan Kamathafd31e02013-12-03 13:16:03 +0000843 // We have no bootanimation file, so we use the stock android logo
844 // animation.
Tomasz Wasilczyke8fb3c72023-08-24 19:11:22 +0000845 if (mZipFileName.empty()) {
Yoshiaki Maruoka9ef19b82017-10-25 13:55:49 +0900846 ALOGD("No animation file");
Marin Shalamanov9070c052020-05-18 19:05:17 +0200847 result = android();
Mathias Agopiana8826d62009-10-01 03:10:14 -0700848 } else {
Marin Shalamanov9070c052020-05-18 19:05:17 +0200849 result = movie();
Mathias Agopiana8826d62009-10-01 03:10:14 -0700850 }
851
Philip Junker60600562020-11-25 18:16:16 +0100852 mCallbacks->shutdown();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800853 eglMakeCurrent(mDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
854 eglDestroyContext(mDisplay, mContext);
855 eglDestroySurface(mDisplay, mSurface);
Mathias Agopian6cf0db22009-04-17 19:36:26 -0700856 mFlingerSurface.clear();
Mathias Agopian17f638b2009-04-16 20:04:08 -0700857 mFlingerSurfaceControl.clear();
Mathias Agopian627e7b52009-05-21 19:21:59 -0700858 eglTerminate(mDisplay);
Sai Kiran Korwar3eee9fb2015-06-16 17:13:35 +0530859 eglReleaseThread();
Mathias Agopian627e7b52009-05-21 19:21:59 -0700860 IPCThreadState::self()->stopProcess();
Marin Shalamanov9070c052020-05-18 19:05:17 +0200861 return result;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800862}
863
Marin Shalamanov9070c052020-05-18 19:05:17 +0200864bool BootAnimation::android() {
Parth Sane7f7b2212023-04-12 17:23:02 +0000865 ATRACE_CALL();
Lucas Dupin8eedc022021-07-29 21:49:07 +0000866 glActiveTexture(GL_TEXTURE0);
867
Wei Wang159a4102018-10-23 23:15:58 -0700868 SLOGD("%sAnimationShownTiming start time: %" PRId64 "ms", mShuttingDown ? "Shutdown" : "Boot",
Keun-young Parkd1794cd2017-04-04 12:21:19 -0700869 elapsedRealtime());
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700870 initTexture(&mAndroid[0], mAssets, "images/android-logo-mask.png");
871 initTexture(&mAndroid[1], mAssets, "images/android-logo-shine.png");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800872
Ed Coyne7464ac92017-06-08 12:26:48 -0700873 mCallbacks->init({});
874
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800875 // clear screen
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700876 glDisable(GL_DITHER);
877 glDisable(GL_SCISSOR_TEST);
Shan Huangbfd2d3c2021-09-09 23:19:04 +0000878 glUseProgram(mImageShader);
879
Mathias Agopian59f19e42011-05-06 19:22:12 -0700880 glClearColor(0,0,0,1);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800881 glClear(GL_COLOR_BUFFER_BIT);
882 eglSwapBuffers(mDisplay, mSurface);
883
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700884 // Blend state
885 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700886
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800887 const nsecs_t startTime = systemTime();
888 do {
Marin Shalamanov9070c052020-05-18 19:05:17 +0200889 processDisplayEvents();
890 const GLint xc = (mWidth - mAndroid[0].w) / 2;
891 const GLint yc = (mHeight - mAndroid[0].h) / 2;
892 const Rect updateRect(xc, yc, xc + mAndroid[0].w, yc + mAndroid[0].h);
893 glScissor(updateRect.left, mHeight - updateRect.bottom, updateRect.width(),
894 updateRect.height());
895
Mathias Agopian13796652009-03-24 22:49:21 -0700896 nsecs_t now = systemTime();
897 double time = now - startTime;
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700898 float t = 4.0f * float(time / us2ns(16667)) / mAndroid[1].w;
899 GLint offset = (1 - (t - floorf(t))) * mAndroid[1].w;
900 GLint x = xc - offset;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800901
Mathias Agopian81668642009-07-28 11:41:30 -0700902 glDisable(GL_SCISSOR_TEST);
903 glClear(GL_COLOR_BUFFER_BIT);
904
905 glEnable(GL_SCISSOR_TEST);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800906 glDisable(GL_BLEND);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800907 glBindTexture(GL_TEXTURE_2D, mAndroid[1].name);
Lucas Dupin8eedc022021-07-29 21:49:07 +0000908 drawTexturedQuad(x, yc, mAndroid[1].w, mAndroid[1].h);
909 drawTexturedQuad(x + mAndroid[1].w, yc, mAndroid[1].w, mAndroid[1].h);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800910
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700911 glEnable(GL_BLEND);
912 glBindTexture(GL_TEXTURE_2D, mAndroid[0].name);
Lucas Dupin8eedc022021-07-29 21:49:07 +0000913 drawTexturedQuad(xc, yc, mAndroid[0].w, mAndroid[0].h);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800914
Mathias Agopian627e7b52009-05-21 19:21:59 -0700915 EGLBoolean res = eglSwapBuffers(mDisplay, mSurface);
916 if (res == EGL_FALSE)
917 break;
918
Mathias Agopian13796652009-03-24 22:49:21 -0700919 // 12fps: don't animate too fast to preserve CPU
920 const nsecs_t sleepTime = 83333 - ns2us(systemTime() - now);
921 if (sleepTime > 0)
Mathias Agopiana8826d62009-10-01 03:10:14 -0700922 usleep(sleepTime);
Kevin Hesterd3782b22012-04-26 10:38:55 -0700923
924 checkExit();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800925 } while (!exitPending());
926
927 glDeleteTextures(1, &mAndroid[0].name);
928 glDeleteTextures(1, &mAndroid[1].name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800929 return false;
930}
931
Kevin Hesterd3782b22012-04-26 10:38:55 -0700932void BootAnimation::checkExit() {
Parth Sane7f7b2212023-04-12 17:23:02 +0000933 ATRACE_CALL();
Kevin Hesterd3782b22012-04-26 10:38:55 -0700934 // Allow surface flinger to gracefully request shutdown
935 char value[PROPERTY_VALUE_MAX];
936 property_get(EXIT_PROP_NAME, value, "0");
937 int exitnow = atoi(value);
938 if (exitnow) {
939 requestExit();
940 }
941}
942
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700943bool BootAnimation::validClock(const Animation::Part& part) {
Parth Sane7f7b2212023-04-12 17:23:02 +0000944 ATRACE_CALL();
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700945 return part.clockPosX != TEXT_MISSING_VALUE && part.clockPosY != TEXT_MISSING_VALUE;
946}
947
948bool parseTextCoord(const char* str, int* dest) {
Parth Sane7f7b2212023-04-12 17:23:02 +0000949 ATRACE_CALL();
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700950 if (strcmp("c", str) == 0) {
951 *dest = TEXT_CENTER_VALUE;
952 return true;
953 }
954
955 char* end;
956 int val = (int) strtol(str, &end, 0);
957 if (end == str || *end != '\0' || val == INT_MAX || val == INT_MIN) {
958 return false;
959 }
960 *dest = val;
961 return true;
962}
963
964// Parse two position coordinates. If only string is non-empty, treat it as the y value.
965void parsePosition(const char* str1, const char* str2, int* x, int* y) {
Parth Sane7f7b2212023-04-12 17:23:02 +0000966 ATRACE_CALL();
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700967 bool success = false;
968 if (strlen(str1) == 0) { // No values were specified
969 // success = false
970 } else if (strlen(str2) == 0) { // we have only one value
971 if (parseTextCoord(str1, y)) {
972 *x = TEXT_CENTER_VALUE;
973 success = true;
974 }
975 } else {
976 if (parseTextCoord(str1, x) && parseTextCoord(str2, y)) {
977 success = true;
978 }
979 }
980
981 if (!success) {
982 *x = TEXT_MISSING_VALUE;
983 *y = TEXT_MISSING_VALUE;
984 }
985}
986
Jesse Hall083b84c2014-09-22 10:51:09 -0700987// Parse a color represented as an HTML-style 'RRGGBB' string: each pair of
988// characters in str is a hex number in [0, 255], which are converted to
989// floating point values in the range [0.0, 1.0] and placed in the
990// corresponding elements of color.
991//
992// If the input string isn't valid, parseColor returns false and color is
993// left unchanged.
994static bool parseColor(const char str[7], float color[3]) {
Parth Sane7f7b2212023-04-12 17:23:02 +0000995 ATRACE_CALL();
Jesse Hall083b84c2014-09-22 10:51:09 -0700996 float tmpColor[3];
997 for (int i = 0; i < 3; i++) {
998 int val = 0;
999 for (int j = 0; j < 2; j++) {
1000 val *= 16;
1001 char c = str[2*i + j];
1002 if (c >= '0' && c <= '9') val += c - '0';
1003 else if (c >= 'A' && c <= 'F') val += (c - 'A') + 10;
1004 else if (c >= 'a' && c <= 'f') val += (c - 'a') + 10;
1005 else return false;
1006 }
1007 tmpColor[i] = static_cast<float>(val) / 255.0f;
1008 }
1009 memcpy(color, tmpColor, sizeof(tmpColor));
1010 return true;
1011}
1012
Shan Huang2b31a362021-08-16 16:45:39 +00001013// Parse a color represented as a signed decimal int string.
1014// E.g. "-2757722" (whose hex 2's complement is 0xFFD5EBA6).
1015// If the input color string is empty, set color with values in defaultColor.
1016static void parseColorDecimalString(const std::string& colorString,
1017 float color[3], float defaultColor[3]) {
Parth Sane7f7b2212023-04-12 17:23:02 +00001018 ATRACE_CALL();
Shan Huang2b31a362021-08-16 16:45:39 +00001019 if (colorString == "") {
1020 memcpy(color, defaultColor, sizeof(float) * 3);
1021 return;
1022 }
1023 int colorInt = atoi(colorString.c_str());
1024 color[0] = ((float)((colorInt >> 16) & 0xFF)) / 0xFF; // r
1025 color[1] = ((float)((colorInt >> 8) & 0xFF)) / 0xFF; // g
1026 color[2] = ((float)(colorInt & 0xFF)) / 0xFF; // b
1027}
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001028
Marin Shalamanov9070c052020-05-18 19:05:17 +02001029static bool readFile(ZipFileRO* zip, const char* name, String8& outString) {
Parth Sane7f7b2212023-04-12 17:23:02 +00001030 ATRACE_CALL();
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001031 ZipEntryRO entry = zip->findEntryByName(name);
Wei Wang159a4102018-10-23 23:15:58 -07001032 SLOGE_IF(!entry, "couldn't find %s", name);
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -07001033 if (!entry) {
1034 return false;
1035 }
1036
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001037 FileMap* entryMap = zip->createEntryFileMap(entry);
1038 zip->releaseEntry(entry);
Wei Wang159a4102018-10-23 23:15:58 -07001039 SLOGE_IF(!entryMap, "entryMap is null");
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -07001040 if (!entryMap) {
1041 return false;
1042 }
1043
Tomasz Wasilczyk31eb3c892023-08-23 22:12:33 +00001044 outString = String8((char const*)entryMap->getDataPtr(), entryMap->getDataLength());
Narayan Kamath688ff4c2015-02-23 15:47:54 +00001045 delete entryMap;
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -07001046 return true;
1047}
1048
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -07001049// The font image should be a 96x2 array of character images. The
1050// columns are the printable ASCII characters 0x20 - 0x7f. The
1051// top row is regular text; the bottom row is bold.
1052status_t BootAnimation::initFont(Font* font, const char* fallback) {
Parth Sane7f7b2212023-04-12 17:23:02 +00001053 ATRACE_CALL();
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -07001054 status_t status = NO_ERROR;
Damien Bargiacchia704b7d2016-02-16 16:55:49 -08001055
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -07001056 if (font->map != nullptr) {
1057 glGenTextures(1, &font->texture.name);
1058 glBindTexture(GL_TEXTURE_2D, font->texture.name);
Damien Bargiacchia704b7d2016-02-16 16:55:49 -08001059
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -07001060 status = initTexture(font->map, &font->texture.w, &font->texture.h);
1061
Lucas Dupin8eedc022021-07-29 21:49:07 +00001062 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
1063 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
1064 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
1065 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -07001066 } else if (fallback != nullptr) {
1067 status = initTexture(&font->texture, mAssets, fallback);
1068 } else {
1069 return NO_INIT;
Damien Bargiacchia704b7d2016-02-16 16:55:49 -08001070 }
1071
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -07001072 if (status == NO_ERROR) {
1073 font->char_width = font->texture.w / FONT_NUM_COLS;
1074 font->char_height = font->texture.h / FONT_NUM_ROWS / 2; // There are bold and regular rows
1075 }
1076
1077 return status;
1078}
1079
1080void BootAnimation::drawText(const char* str, const Font& font, bool bold, int* x, int* y) {
Parth Sane7f7b2212023-04-12 17:23:02 +00001081 ATRACE_CALL();
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -07001082 glEnable(GL_BLEND); // Allow us to draw on top of the animation
1083 glBindTexture(GL_TEXTURE_2D, font.texture.name);
Lucas Dupin8eedc022021-07-29 21:49:07 +00001084 glUseProgram(mTextShader);
1085 glUniform1i(mTextTextureLocation, 0);
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -07001086
1087 const int len = strlen(str);
1088 const int strWidth = font.char_width * len;
1089
1090 if (*x == TEXT_CENTER_VALUE) {
1091 *x = (mWidth - strWidth) / 2;
1092 } else if (*x < 0) {
1093 *x = mWidth + *x - strWidth;
1094 }
1095 if (*y == TEXT_CENTER_VALUE) {
1096 *y = (mHeight - font.char_height) / 2;
1097 } else if (*y < 0) {
1098 *y = mHeight + *y - font.char_height;
1099 }
1100
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -07001101 for (int i = 0; i < len; i++) {
1102 char c = str[i];
1103
1104 if (c < FONT_BEGIN_CHAR || c > FONT_END_CHAR) {
1105 c = '?';
1106 }
1107
1108 // Crop the texture to only the pixels in the current glyph
1109 const int charPos = (c - FONT_BEGIN_CHAR); // Position in the list of valid characters
1110 const int row = charPos / FONT_NUM_COLS;
1111 const int col = charPos % FONT_NUM_COLS;
Lucas Dupin8eedc022021-07-29 21:49:07 +00001112 // Bold fonts are expected in the second half of each row.
1113 float v0 = (row + (bold ? 0.5f : 0.0f)) / FONT_NUM_ROWS;
1114 float u0 = ((float)col) / FONT_NUM_COLS;
1115 float v1 = v0 + 1.0f / FONT_NUM_ROWS / 2;
1116 float u1 = u0 + 1.0f / FONT_NUM_COLS;
1117 glUniform4f(mTextCropAreaLocation, u0, v0, u1, v1);
1118 drawTexturedQuad(*x, *y, font.char_width, font.char_height);
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -07001119
1120 *x += font.char_width;
1121 }
1122
1123 glDisable(GL_BLEND); // Return to the animation's default behaviour
1124 glBindTexture(GL_TEXTURE_2D, 0);
1125}
1126
Damien Bargiacchi9071db12016-10-28 17:38:22 -07001127// We render 12 or 24 hour time.
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -07001128void BootAnimation::drawClock(const Font& font, const int xPos, const int yPos) {
Parth Sane7f7b2212023-04-12 17:23:02 +00001129 ATRACE_CALL();
Damien Bargiacchi9071db12016-10-28 17:38:22 -07001130 static constexpr char TIME_FORMAT_12[] = "%l:%M";
1131 static constexpr char TIME_FORMAT_24[] = "%H:%M";
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -07001132 static constexpr int TIME_LENGTH = 6;
1133
Damien Bargiacchia704b7d2016-02-16 16:55:49 -08001134 time_t rawtime;
1135 time(&rawtime);
1136 struct tm* timeInfo = localtime(&rawtime);
1137
1138 char timeBuff[TIME_LENGTH];
Damien Bargiacchi9071db12016-10-28 17:38:22 -07001139 const char* timeFormat = mTimeFormat12Hour ? TIME_FORMAT_12 : TIME_FORMAT_24;
1140 size_t length = strftime(timeBuff, TIME_LENGTH, timeFormat, timeInfo);
Damien Bargiacchia704b7d2016-02-16 16:55:49 -08001141
1142 if (length != TIME_LENGTH - 1) {
Wei Wang159a4102018-10-23 23:15:58 -07001143 SLOGE("Couldn't format time; abandoning boot animation clock");
Damien Bargiacchia704b7d2016-02-16 16:55:49 -08001144 mClockEnabled = false;
1145 return;
1146 }
1147
Damien Bargiacchi45a76442016-12-05 18:02:18 -08001148 char* out = timeBuff[0] == ' ' ? &timeBuff[1] : &timeBuff[0];
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -07001149 int x = xPos;
1150 int y = yPos;
Damien Bargiacchi45a76442016-12-05 18:02:18 -08001151 drawText(out, font, false, &x, &y);
Damien Bargiacchia704b7d2016-02-16 16:55:49 -08001152}
1153
Nicolas Geoffray41fffb42020-12-15 10:50:29 +00001154void BootAnimation::drawProgress(int percent, const Font& font, const int xPos, const int yPos) {
Parth Sane7f7b2212023-04-12 17:23:02 +00001155 ATRACE_CALL();
Nicolas Geoffray41fffb42020-12-15 10:50:29 +00001156 static constexpr int PERCENT_LENGTH = 5;
1157
1158 char percentBuff[PERCENT_LENGTH];
1159 // ';' has the ascii code just after ':', and the font resource contains '%'
1160 // for that ascii code.
1161 sprintf(percentBuff, "%d;", percent);
1162 int x = xPos;
1163 int y = yPos;
1164 drawText(percentBuff, font, false, &x, &y);
1165}
1166
Marin Shalamanov9070c052020-05-18 19:05:17 +02001167bool BootAnimation::parseAnimationDesc(Animation& animation) {
Parth Sane7f7b2212023-04-12 17:23:02 +00001168 ATRACE_CALL();
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -07001169 String8 desString;
1170
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001171 if (!readFile(animation.zip, "desc.txt", desString)) {
Narayan Kamathafd31e02013-12-03 13:16:03 +00001172 return false;
1173 }
Tomasz Wasilczyk3815d342023-08-10 23:54:44 +00001174 char const* s = desString.c_str();
Shan Huang2b31a362021-08-16 16:45:39 +00001175 std::string dynamicColoringPartName = "";
1176 bool postDynamicColoring = false;
Mathias Agopiana8826d62009-10-01 03:10:14 -07001177
Mathias Agopiana8826d62009-10-01 03:10:14 -07001178 // Parse the description file
1179 for (;;) {
1180 const char* endl = strstr(s, "\n");
Yi Kong911ac232019-03-24 01:49:02 -07001181 if (endl == nullptr) break;
Mathias Agopiana8826d62009-10-01 03:10:14 -07001182 String8 line(s, endl - s);
Tomasz Wasilczyk3815d342023-08-10 23:54:44 +00001183 const char* l = line.c_str();
Damien Bargiacchia704b7d2016-02-16 16:55:49 -08001184 int fps = 0;
1185 int width = 0;
1186 int height = 0;
1187 int count = 0;
1188 int pause = 0;
Nicolas Geoffray41fffb42020-12-15 10:50:29 +00001189 int progress = 0;
Yegor Malyshev6925e1b2020-09-10 16:42:20 +02001190 int framesToFadeCount = 0;
Shan Huang488ff742021-08-25 20:10:46 +00001191 int colorTransitionStart = 0;
1192 int colorTransitionEnd = 0;
Narayan Kamathafd31e02013-12-03 13:16:03 +00001193 char path[ANIM_ENTRY_NAME_MAX];
Jesse Hall083b84c2014-09-22 10:51:09 -07001194 char color[7] = "000000"; // default to black if unspecified
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -07001195 char clockPos1[TEXT_POS_LEN_MAX + 1] = "";
1196 char clockPos2[TEXT_POS_LEN_MAX + 1] = "";
Shan Huang2b31a362021-08-16 16:45:39 +00001197 char dynamicColoringPartNameBuffer[ANIM_ENTRY_NAME_MAX];
Kevin Hesterd3782b22012-04-26 10:38:55 -07001198 char pathType;
Shan Huang2b31a362021-08-16 16:45:39 +00001199 // start colors default to black if unspecified
1200 char start_color_0[7] = "000000";
1201 char start_color_1[7] = "000000";
1202 char start_color_2[7] = "000000";
1203 char start_color_3[7] = "000000";
Yegor Malyshev6925e1b2020-09-10 16:42:20 +02001204
1205 int nextReadPos;
1206
yidong zhang87bb3462022-12-07 15:55:31 +08001207 if (strlen(l) == 0) {
1208 s = ++endl;
1209 continue;
1210 }
1211
Nicolas Geoffray41fffb42020-12-15 10:50:29 +00001212 int topLineNumbers = sscanf(l, "%d %d %d %d", &width, &height, &fps, &progress);
1213 if (topLineNumbers == 3 || topLineNumbers == 4) {
1214 // SLOGD("> w=%d, h=%d, fps=%d, progress=%d", width, height, fps, progress);
Mathias Agopiana8826d62009-10-01 03:10:14 -07001215 animation.width = width;
1216 animation.height = height;
1217 animation.fps = fps;
Nicolas Geoffray41fffb42020-12-15 10:50:29 +00001218 if (topLineNumbers == 4) {
1219 animation.progressEnabled = (progress != 0);
1220 } else {
1221 animation.progressEnabled = false;
1222 }
Shan Huang488ff742021-08-25 20:10:46 +00001223 } else if (sscanf(l, "dynamic_colors %" STRTO(ANIM_PATH_MAX) "s #%6s #%6s #%6s #%6s %d %d",
Shan Huang2b31a362021-08-16 16:45:39 +00001224 dynamicColoringPartNameBuffer,
Shan Huang488ff742021-08-25 20:10:46 +00001225 start_color_0, start_color_1, start_color_2, start_color_3,
1226 &colorTransitionStart, &colorTransitionEnd)) {
Shan Huang2b31a362021-08-16 16:45:39 +00001227 animation.dynamicColoringEnabled = true;
1228 parseColor(start_color_0, animation.startColors[0]);
1229 parseColor(start_color_1, animation.startColors[1]);
1230 parseColor(start_color_2, animation.startColors[2]);
1231 parseColor(start_color_3, animation.startColors[3]);
Shan Huang488ff742021-08-25 20:10:46 +00001232 animation.colorTransitionStart = colorTransitionStart;
1233 animation.colorTransitionEnd = colorTransitionEnd;
Shan Huang2b31a362021-08-16 16:45:39 +00001234 dynamicColoringPartName = std::string(dynamicColoringPartNameBuffer);
Yegor Malyshev6925e1b2020-09-10 16:42:20 +02001235 } else if (sscanf(l, "%c %d %d %" STRTO(ANIM_PATH_MAX) "s%n",
1236 &pathType, &count, &pause, path, &nextReadPos) >= 4) {
1237 if (pathType == 'f') {
1238 sscanf(l + nextReadPos, " %d #%6s %16s %16s", &framesToFadeCount, color, clockPos1,
1239 clockPos2);
1240 } else {
1241 sscanf(l + nextReadPos, " #%6s %16s %16s", color, clockPos1, clockPos2);
1242 }
1243 // SLOGD("> type=%c, count=%d, pause=%d, path=%s, framesToFadeCount=%d, color=%s, "
1244 // "clockPos1=%s, clockPos2=%s",
1245 // pathType, count, pause, path, framesToFadeCount, color, clockPos1, clockPos2);
Mathias Agopiana8826d62009-10-01 03:10:14 -07001246 Animation::Part part;
Shan Huang2b31a362021-08-16 16:45:39 +00001247 if (path == dynamicColoringPartName) {
1248 // Part is specified to use dynamic coloring.
1249 part.useDynamicColoring = true;
1250 part.postDynamicColoring = false;
1251 postDynamicColoring = true;
1252 } else {
1253 // Part does not use dynamic coloring.
1254 part.useDynamicColoring = false;
1255 part.postDynamicColoring = postDynamicColoring;
1256 }
Kevin Hesterd3782b22012-04-26 10:38:55 -07001257 part.playUntilComplete = pathType == 'c';
Yegor Malyshev6925e1b2020-09-10 16:42:20 +02001258 part.framesToFadeCount = framesToFadeCount;
Mathias Agopiana8826d62009-10-01 03:10:14 -07001259 part.count = count;
1260 part.pause = pause;
1261 part.path = path;
Yi Kong911ac232019-03-24 01:49:02 -07001262 part.audioData = nullptr;
1263 part.animation = nullptr;
Jesse Hall083b84c2014-09-22 10:51:09 -07001264 if (!parseColor(color, part.backgroundColor)) {
Wei Wang159a4102018-10-23 23:15:58 -07001265 SLOGE("> invalid color '#%s'", color);
Jesse Hall083b84c2014-09-22 10:51:09 -07001266 part.backgroundColor[0] = 0.0f;
1267 part.backgroundColor[1] = 0.0f;
1268 part.backgroundColor[2] = 0.0f;
1269 }
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -07001270 parsePosition(clockPos1, clockPos2, &part.clockPosX, &part.clockPosY);
Mathias Agopiana8826d62009-10-01 03:10:14 -07001271 animation.parts.add(part);
1272 }
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001273 else if (strcmp(l, "$SYSTEM") == 0) {
Wei Wang159a4102018-10-23 23:15:58 -07001274 // SLOGD("> SYSTEM");
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001275 Animation::Part part;
1276 part.playUntilComplete = false;
Yegor Malyshev6925e1b2020-09-10 16:42:20 +02001277 part.framesToFadeCount = 0;
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001278 part.count = 1;
1279 part.pause = 0;
Yi Kong911ac232019-03-24 01:49:02 -07001280 part.audioData = nullptr;
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001281 part.animation = loadAnimation(String8(SYSTEM_BOOTANIMATION_FILE));
Yi Kong911ac232019-03-24 01:49:02 -07001282 if (part.animation != nullptr)
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001283 animation.parts.add(part);
1284 }
Mathias Agopiana8826d62009-10-01 03:10:14 -07001285 s = ++endl;
1286 }
1287
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001288 return true;
1289}
1290
Marin Shalamanov9070c052020-05-18 19:05:17 +02001291bool BootAnimation::preloadZip(Animation& animation) {
Parth Sane7f7b2212023-04-12 17:23:02 +00001292 ATRACE_CALL();
Mathias Agopiana8826d62009-10-01 03:10:14 -07001293 // read all the data structures
1294 const size_t pcount = animation.parts.size();
Yi Kong911ac232019-03-24 01:49:02 -07001295 void *cookie = nullptr;
Geoffrey Pitschdd214a72016-06-27 17:14:30 -04001296 ZipFileRO* zip = animation.zip;
1297 if (!zip->startIteration(&cookie)) {
Narayan Kamathafd31e02013-12-03 13:16:03 +00001298 return false;
1299 }
1300
1301 ZipEntryRO entry;
1302 char name[ANIM_ENTRY_NAME_MAX];
Yi Kong911ac232019-03-24 01:49:02 -07001303 while ((entry = zip->nextEntry(cookie)) != nullptr) {
Geoffrey Pitschdd214a72016-06-27 17:14:30 -04001304 const int foundEntryName = zip->getEntryFileName(entry, name, ANIM_ENTRY_NAME_MAX);
Narayan Kamathafd31e02013-12-03 13:16:03 +00001305 if (foundEntryName > ANIM_ENTRY_NAME_MAX || foundEntryName == -1) {
Wei Wang159a4102018-10-23 23:15:58 -07001306 SLOGE("Error fetching entry file name");
Narayan Kamathafd31e02013-12-03 13:16:03 +00001307 continue;
1308 }
1309
Tomasz Wasilczyk9dcea572023-08-22 20:39:20 +00001310 const std::filesystem::path entryName(name);
1311 const std::filesystem::path path(entryName.parent_path());
1312 const std::filesystem::path leaf(entryName.filename());
1313 if (!leaf.empty()) {
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -07001314 if (entryName == CLOCK_FONT_ZIP_NAME) {
1315 FileMap* map = zip->createEntryFileMap(entry);
1316 if (map) {
1317 animation.clockFont.map = map;
1318 }
1319 continue;
1320 }
1321
Nicolas Geoffray41fffb42020-12-15 10:50:29 +00001322 if (entryName == PROGRESS_FONT_ZIP_NAME) {
1323 FileMap* map = zip->createEntryFileMap(entry);
1324 if (map) {
1325 animation.progressFont.map = map;
1326 }
1327 continue;
1328 }
1329
Geoffrey Pitschdd214a72016-06-27 17:14:30 -04001330 for (size_t j = 0; j < pcount; j++) {
Tomasz Wasilczyk9dcea572023-08-22 20:39:20 +00001331 if (path.string() == animation.parts[j].path.c_str()) {
Narayan Kamath4600dd02015-06-16 12:02:57 +01001332 uint16_t method;
Narayan Kamathafd31e02013-12-03 13:16:03 +00001333 // supports only stored png files
Yi Kong911ac232019-03-24 01:49:02 -07001334 if (zip->getEntryInfo(entry, &method, nullptr, nullptr, nullptr, nullptr, nullptr)) {
Narayan Kamathafd31e02013-12-03 13:16:03 +00001335 if (method == ZipFileRO::kCompressStored) {
Geoffrey Pitschdd214a72016-06-27 17:14:30 -04001336 FileMap* map = zip->createEntryFileMap(entry);
Narayan Kamathafd31e02013-12-03 13:16:03 +00001337 if (map) {
Narayan Kamathafd31e02013-12-03 13:16:03 +00001338 Animation::Part& part(animation.parts.editItemAt(j));
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -07001339 if (leaf == "audio.wav") {
1340 // a part may have at most one audio file
Geoffrey Pitschd6d9a1d2016-06-08 00:38:58 -07001341 part.audioData = (uint8_t *)map->getDataPtr();
1342 part.audioLength = map->getDataLength();
Geoffrey Pitschdd214a72016-06-27 17:14:30 -04001343 } else if (leaf == "trim.txt") {
Tomasz Wasilczyk31eb3c892023-08-23 22:12:33 +00001344 part.trimData = String8((char const*)map->getDataPtr(),
Geoffrey Pitschdd214a72016-06-27 17:14:30 -04001345 map->getDataLength());
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -07001346 } else {
1347 Animation::Frame frame;
Tomasz Wasilczyk9dcea572023-08-22 20:39:20 +00001348 frame.name = leaf.c_str();
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -07001349 frame.map = map;
Geoffrey Pitschdd214a72016-06-27 17:14:30 -04001350 frame.trimWidth = animation.width;
1351 frame.trimHeight = animation.height;
1352 frame.trimX = 0;
1353 frame.trimY = 0;
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -07001354 part.frames.add(frame);
1355 }
Mathias Agopiana8826d62009-10-01 03:10:14 -07001356 }
Geoffrey Pitschd6d9a1d2016-06-08 00:38:58 -07001357 } else {
Wei Wang159a4102018-10-23 23:15:58 -07001358 SLOGE("bootanimation.zip is compressed; must be only stored");
Mathias Agopiana8826d62009-10-01 03:10:14 -07001359 }
1360 }
1361 }
1362 }
1363 }
1364 }
1365
Geoffrey Pitschdd214a72016-06-27 17:14:30 -04001366 // If there is trimData present, override the positioning defaults.
1367 for (Animation::Part& part : animation.parts) {
Tomasz Wasilczyk3815d342023-08-10 23:54:44 +00001368 const char* trimDataStr = part.trimData.c_str();
Geoffrey Pitschdd214a72016-06-27 17:14:30 -04001369 for (size_t frameIdx = 0; frameIdx < part.frames.size(); frameIdx++) {
1370 const char* endl = strstr(trimDataStr, "\n");
1371 // No more trimData for this part.
Yi Kong911ac232019-03-24 01:49:02 -07001372 if (endl == nullptr) {
Geoffrey Pitschdd214a72016-06-27 17:14:30 -04001373 break;
1374 }
1375 String8 line(trimDataStr, endl - trimDataStr);
Tomasz Wasilczyk3815d342023-08-10 23:54:44 +00001376 const char* lineStr = line.c_str();
Geoffrey Pitschdd214a72016-06-27 17:14:30 -04001377 trimDataStr = ++endl;
1378 int width = 0, height = 0, x = 0, y = 0;
1379 if (sscanf(lineStr, "%dx%d+%d+%d", &width, &height, &x, &y) == 4) {
1380 Animation::Frame& frame(part.frames.editItemAt(frameIdx));
1381 frame.trimWidth = width;
1382 frame.trimHeight = height;
1383 frame.trimX = x;
1384 frame.trimY = y;
1385 } else {
Wei Wang159a4102018-10-23 23:15:58 -07001386 SLOGE("Error parsing trim.txt, line: %s", lineStr);
Geoffrey Pitschdd214a72016-06-27 17:14:30 -04001387 break;
1388 }
1389 }
1390 }
1391
1392 zip->endIteration(cookie);
Narayan Kamathafd31e02013-12-03 13:16:03 +00001393
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001394 return true;
1395}
1396
Marin Shalamanov9070c052020-05-18 19:05:17 +02001397bool BootAnimation::movie() {
Parth Sane7f7b2212023-04-12 17:23:02 +00001398 ATRACE_CALL();
Huihong Luo50a2f362018-08-11 09:27:57 -07001399 if (mAnimation == nullptr) {
1400 mAnimation = loadAnimation(mZipFileName);
1401 }
1402
1403 if (mAnimation == nullptr)
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001404 return false;
1405
Huihong Luo50a2f362018-08-11 09:27:57 -07001406 // mCallbacks->init() may get called recursively,
1407 // this loop is needed to get the same results
1408 for (const Animation::Part& part : mAnimation->parts) {
1409 if (part.animation != nullptr) {
1410 mCallbacks->init(part.animation->parts);
1411 }
1412 }
1413 mCallbacks->init(mAnimation->parts);
1414
Damien Bargiacchi97480862016-03-29 14:55:55 -07001415 bool anyPartHasClock = false;
Huihong Luo50a2f362018-08-11 09:27:57 -07001416 for (size_t i=0; i < mAnimation->parts.size(); i++) {
1417 if(validClock(mAnimation->parts[i])) {
Damien Bargiacchi97480862016-03-29 14:55:55 -07001418 anyPartHasClock = true;
1419 break;
1420 }
1421 }
1422 if (!anyPartHasClock) {
1423 mClockEnabled = false;
Thiébaud Weksteen08de4432021-11-18 13:25:59 +11001424 } else if (!android::base::GetBoolProperty(CLOCK_ENABLED_PROP_NAME, false)) {
1425 mClockEnabled = false;
Damien Bargiacchi97480862016-03-29 14:55:55 -07001426 }
1427
Sai Kiran Korwar27167492015-07-07 20:00:06 +05301428 // Check if npot textures are supported
1429 mUseNpotTextures = false;
1430 String8 gl_extensions;
1431 const char* exts = reinterpret_cast<const char*>(glGetString(GL_EXTENSIONS));
1432 if (!exts) {
1433 glGetError();
1434 } else {
Tomasz Wasilczyk31eb3c892023-08-23 22:12:33 +00001435 gl_extensions = exts;
Sai Kiran Korwar27167492015-07-07 20:00:06 +05301436 if ((gl_extensions.find("GL_ARB_texture_non_power_of_two") != -1) ||
1437 (gl_extensions.find("GL_OES_texture_npot") != -1)) {
1438 mUseNpotTextures = true;
1439 }
1440 }
1441
Damien Bargiacchia704b7d2016-02-16 16:55:49 -08001442 // Blend required to draw time on top of animation frames.
1443 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
Mathias Agopiana8826d62009-10-01 03:10:14 -07001444 glDisable(GL_DITHER);
1445 glDisable(GL_SCISSOR_TEST);
1446 glDisable(GL_BLEND);
Mathias Agopiana8826d62009-10-01 03:10:14 -07001447
Greg Kaiser2ded8002021-07-19 17:03:27 +00001448 glEnable(GL_TEXTURE_2D);
Lucas Dupin8eedc022021-07-29 21:49:07 +00001449 glBindTexture(GL_TEXTURE_2D, 0);
1450 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
1451 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
1452 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
1453 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -07001454 bool clockFontInitialized = false;
Damien Bargiacchia704b7d2016-02-16 16:55:49 -08001455 if (mClockEnabled) {
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -07001456 clockFontInitialized =
Huihong Luo50a2f362018-08-11 09:27:57 -07001457 (initFont(&mAnimation->clockFont, CLOCK_FONT_ASSET) == NO_ERROR);
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -07001458 mClockEnabled = clockFontInitialized;
Damien Bargiacchia704b7d2016-02-16 16:55:49 -08001459 }
1460
Nicolas Geoffray41fffb42020-12-15 10:50:29 +00001461 initFont(&mAnimation->progressFont, PROGRESS_FONT_ASSET);
1462
Damien Bargiacchi97480862016-03-29 14:55:55 -07001463 if (mClockEnabled && !updateIsTimeAccurate()) {
1464 mTimeCheckThread = new TimeCheckThread(this);
1465 mTimeCheckThread->run("BootAnimation::TimeCheckThread", PRIORITY_NORMAL);
1466 }
1467
George Burgess IV0f8e8112021-08-23 17:32:23 -07001468 if (mAnimation->dynamicColoringEnabled) {
Shan Huang7fee3522021-08-18 00:56:08 +00001469 initDynamicColors();
1470 }
1471
Huihong Luo50a2f362018-08-11 09:27:57 -07001472 playAnimation(*mAnimation);
Damien Bargiacchi97480862016-03-29 14:55:55 -07001473
Geoffrey Pitscha9173532016-07-19 14:56:39 -04001474 if (mTimeCheckThread != nullptr) {
Damien Bargiacchi97480862016-03-29 14:55:55 -07001475 mTimeCheckThread->requestExit();
Geoffrey Pitscha9173532016-07-19 14:56:39 -04001476 mTimeCheckThread = nullptr;
1477 }
1478
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -07001479 if (clockFontInitialized) {
Huihong Luo50a2f362018-08-11 09:27:57 -07001480 glDeleteTextures(1, &mAnimation->clockFont.texture.name);
Andriy Naborskyy815e51d2016-03-24 16:43:34 -07001481 }
1482
Huihong Luo50a2f362018-08-11 09:27:57 -07001483 releaseAnimation(mAnimation);
1484 mAnimation = nullptr;
Damien Bargiacchi1a8a2132018-07-24 15:20:26 -07001485
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001486 return false;
1487}
1488
Nicolas Geoffraya1298752021-04-20 15:15:06 +01001489bool BootAnimation::shouldStopPlayingPart(const Animation::Part& part,
1490 const int fadedFramesCount,
1491 const int lastDisplayedProgress) {
Parth Sane7f7b2212023-04-12 17:23:02 +00001492 ATRACE_CALL();
Yegor Malyshev6925e1b2020-09-10 16:42:20 +02001493 // 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 +01001494 return exitPending() && !part.playUntilComplete && fadedFramesCount >= part.framesToFadeCount &&
1495 (lastDisplayedProgress == 0 || lastDisplayedProgress == 100);
Yegor Malyshev6925e1b2020-09-10 16:42:20 +02001496}
1497
Lucas Dupin8eedc022021-07-29 21:49:07 +00001498// Linear mapping from range <a1, a2> to range <b1, b2>
1499float mapLinear(float x, float a1, float a2, float b1, float b2) {
1500 return b1 + ( x - a1 ) * ( b2 - b1 ) / ( a2 - a1 );
1501}
1502
1503void BootAnimation::drawTexturedQuad(float xStart, float yStart, float width, float height) {
Parth Sane7f7b2212023-04-12 17:23:02 +00001504 ATRACE_CALL();
Lucas Dupin8eedc022021-07-29 21:49:07 +00001505 // Map coordinates from screen space to world space.
1506 float x0 = mapLinear(xStart, 0, mWidth, -1, 1);
1507 float y0 = mapLinear(yStart, 0, mHeight, -1, 1);
1508 float x1 = mapLinear(xStart + width, 0, mWidth, -1, 1);
1509 float y1 = mapLinear(yStart + height, 0, mHeight, -1, 1);
1510 // Update quad vertex positions.
1511 quadPositions[0] = x0;
1512 quadPositions[1] = y0;
1513 quadPositions[2] = x1;
1514 quadPositions[3] = y0;
1515 quadPositions[4] = x1;
1516 quadPositions[5] = y1;
1517 quadPositions[6] = x1;
1518 quadPositions[7] = y1;
1519 quadPositions[8] = x0;
1520 quadPositions[9] = y1;
1521 quadPositions[10] = x0;
1522 quadPositions[11] = y0;
1523 glDrawArrays(GL_TRIANGLES, 0,
1524 sizeof(quadPositions) / sizeof(quadPositions[0]) / 2);
1525}
1526
Shan Huang7fee3522021-08-18 00:56:08 +00001527void BootAnimation::initDynamicColors() {
Parth Sane7f7b2212023-04-12 17:23:02 +00001528 ATRACE_CALL();
Shan Huang7fee3522021-08-18 00:56:08 +00001529 for (int i = 0; i < DYNAMIC_COLOR_COUNT; i++) {
Lucas Dupincd256842022-03-29 15:46:36 -07001530 const auto syspropName = "persist.bootanim.color" + std::to_string(i + 1);
1531 const auto syspropValue = android::base::GetProperty(syspropName, "");
1532 if (syspropValue != "") {
1533 SLOGI("Loaded dynamic color: %s -> %s", syspropName.c_str(), syspropValue.c_str());
1534 mDynamicColorsApplied = true;
1535 }
1536 parseColorDecimalString(syspropValue,
Shan Huang7fee3522021-08-18 00:56:08 +00001537 mAnimation->endColors[i], mAnimation->startColors[i]);
1538 }
1539 glUseProgram(mImageShader);
Lucas Dupincd256842022-03-29 15:46:36 -07001540 SLOGI("Dynamically coloring boot animation. Sysprops loaded? %i", mDynamicColorsApplied);
Shan Huang7fee3522021-08-18 00:56:08 +00001541 for (int i = 0; i < DYNAMIC_COLOR_COUNT; i++) {
1542 float *startColor = mAnimation->startColors[i];
1543 float *endColor = mAnimation->endColors[i];
Andrew Zengf9f58222022-11-08 11:10:48 -08001544 glUniform3f(glGetUniformLocation(mImageShader,
Shan Huang7fee3522021-08-18 00:56:08 +00001545 (U_START_COLOR_PREFIX + std::to_string(i)).c_str()),
Andrew Zengf9f58222022-11-08 11:10:48 -08001546 startColor[0], startColor[1], startColor[2]);
1547 glUniform3f(glGetUniformLocation(mImageShader,
Shan Huang7fee3522021-08-18 00:56:08 +00001548 (U_END_COLOR_PREFIX + std::to_string(i)).c_str()),
Andrew Zengf9f58222022-11-08 11:10:48 -08001549 endColor[0], endColor[1], endColor[2]);
Shan Huang7fee3522021-08-18 00:56:08 +00001550 }
1551 mImageColorProgressLocation = glGetUniformLocation(mImageShader, U_COLOR_PROGRESS);
1552}
1553
Marin Shalamanov9070c052020-05-18 19:05:17 +02001554bool BootAnimation::playAnimation(const Animation& animation) {
Parth Sane7f7b2212023-04-12 17:23:02 +00001555 ATRACE_CALL();
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001556 const size_t pcount = animation.parts.size();
Mathias Agopiana8826d62009-10-01 03:10:14 -07001557 nsecs_t frameDuration = s2ns(1) / animation.fps;
Mathias Agopian9f3020d2009-11-06 16:30:18 -08001558
Wei Wang159a4102018-10-23 23:15:58 -07001559 SLOGD("%sAnimationShownTiming start time: %" PRId64 "ms", mShuttingDown ? "Shutdown" : "Boot",
Keun-young Parkd1794cd2017-04-04 12:21:19 -07001560 elapsedRealtime());
Yegor Malyshev6925e1b2020-09-10 16:42:20 +02001561
1562 int fadedFramesCount = 0;
Nicolas Geoffray41fffb42020-12-15 10:50:29 +00001563 int lastDisplayedProgress = 0;
Lucas Dupinbfd18122022-04-25 11:26:08 -07001564 int colorTransitionStart = animation.colorTransitionStart;
1565 int colorTransitionEnd = animation.colorTransitionEnd;
Narayan Kamathafd31e02013-12-03 13:16:03 +00001566 for (size_t i=0 ; i<pcount ; i++) {
Mathias Agopiana8826d62009-10-01 03:10:14 -07001567 const Animation::Part& part(animation.parts[i]);
1568 const size_t fcount = part.frames.size();
Oleg Lyovin6a4b84d2023-07-01 14:39:59 +03001569 glBindTexture(GL_TEXTURE_2D, 0);
Mathias Agopiana8826d62009-10-01 03:10:14 -07001570
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001571 // Handle animation package
Yi Kong911ac232019-03-24 01:49:02 -07001572 if (part.animation != nullptr) {
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001573 playAnimation(*part.animation);
1574 if (exitPending())
1575 break;
1576 continue; //to next part
1577 }
1578
Yegor Malyshev6925e1b2020-09-10 16:42:20 +02001579 // process the part not only while the count allows but also if already fading
1580 for (int r=0 ; !part.count || r<part.count || fadedFramesCount > 0 ; r++) {
Nicolas Geoffraya1298752021-04-20 15:15:06 +01001581 if (shouldStopPlayingPart(part, fadedFramesCount, lastDisplayedProgress)) break;
Kevin Hesterd3782b22012-04-26 10:38:55 -07001582
Lucas Dupinbfd18122022-04-25 11:26:08 -07001583 // It's possible that the sysprops were not loaded yet at this boot phase.
1584 // If that's the case, then we should keep trying until they are available.
1585 if (animation.dynamicColoringEnabled && !mDynamicColorsApplied
1586 && (part.useDynamicColoring || part.postDynamicColoring)) {
1587 SLOGD("Trying to load dynamic color sysprops.");
1588 initDynamicColors();
1589 if (mDynamicColorsApplied) {
1590 // Sysprops were loaded. Next step is to adjust the animation if we loaded
1591 // the colors after the animation should have started.
1592 const int transitionLength = colorTransitionEnd - colorTransitionStart;
1593 if (part.postDynamicColoring) {
1594 colorTransitionStart = 0;
1595 colorTransitionEnd = fmin(transitionLength, fcount - 1);
1596 }
1597 }
1598 }
1599
Ed Coyne7464ac92017-06-08 12:26:48 -07001600 mCallbacks->playPart(i, part, r);
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -07001601
Jesse Hall083b84c2014-09-22 10:51:09 -07001602 glClearColor(
1603 part.backgroundColor[0],
1604 part.backgroundColor[1],
1605 part.backgroundColor[2],
1606 1.0f);
1607
Yoshiaki Maruoka9ef19b82017-10-25 13:55:49 +09001608 ALOGD("Playing files = %s/%s, Requested repeat = %d, playUntilComplete = %s",
Tomasz Wasilczyk3815d342023-08-10 23:54:44 +00001609 animation.fileName.c_str(), part.path.c_str(), part.count,
Yoshiaki Maruoka9ef19b82017-10-25 13:55:49 +09001610 part.playUntilComplete ? "true" : "false");
1611
Nicolas Geoffray41fffb42020-12-15 10:50:29 +00001612 // For the last animation, if we have progress indicator from
1613 // the system, display it.
1614 int currentProgress = android::base::GetIntProperty(PROGRESS_PROP_NAME, 0);
1615 bool displayProgress = animation.progressEnabled &&
1616 (i == (pcount -1)) && currentProgress != 0;
1617
Yegor Malyshev6925e1b2020-09-10 16:42:20 +02001618 for (size_t j=0 ; j<fcount ; j++) {
Nicolas Geoffraya1298752021-04-20 15:15:06 +01001619 if (shouldStopPlayingPart(part, fadedFramesCount, lastDisplayedProgress)) break;
Yegor Malyshev6925e1b2020-09-10 16:42:20 +02001620
Shan Huang2b31a362021-08-16 16:45:39 +00001621 // Color progress is
Shan Huang488ff742021-08-25 20:10:46 +00001622 // - the animation progress, normalized from
1623 // [colorTransitionStart,colorTransitionEnd] to [0, 1] for the dynamic coloring
1624 // part.
Shan Huang2b31a362021-08-16 16:45:39 +00001625 // - 0 for parts that come before,
1626 // - 1 for parts that come after.
1627 float colorProgress = part.useDynamicColoring
Shan Huang488ff742021-08-25 20:10:46 +00001628 ? fmin(fmax(
Lucas Dupinbfd18122022-04-25 11:26:08 -07001629 ((float)j - colorTransitionStart) /
1630 fmax(colorTransitionEnd - colorTransitionStart, 1.0f), 0.0f), 1.0f)
Shan Huang2b31a362021-08-16 16:45:39 +00001631 : (part.postDynamicColoring ? 1 : 0);
1632
Marin Shalamanov9070c052020-05-18 19:05:17 +02001633 processDisplayEvents();
1634
joenchen764ea042022-04-11 23:58:34 +08001635 const double ratio_w = static_cast<double>(mWidth) / mInitWidth;
1636 const double ratio_h = static_cast<double>(mHeight) / mInitHeight;
1637 const int animationX = (mWidth - animation.width * ratio_w) / 2;
1638 const int animationY = (mHeight - animation.height * ratio_h) / 2;
Marin Shalamanov9070c052020-05-18 19:05:17 +02001639
Mathias Agopiana8826d62009-10-01 03:10:14 -07001640 const Animation::Frame& frame(part.frames[j]);
Mathias Agopiandb7dd2a2012-05-12 15:08:21 -07001641 nsecs_t lastFrame = systemTime();
Mathias Agopiana8826d62009-10-01 03:10:14 -07001642
1643 if (r > 0) {
1644 glBindTexture(GL_TEXTURE_2D, frame.tid);
1645 } else {
Oleg Lyovin6a4b84d2023-07-01 14:39:59 +03001646 if (part.count != 1) {
1647 glGenTextures(1, &frame.tid);
1648 glBindTexture(GL_TEXTURE_2D, frame.tid);
1649 }
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -07001650 int w, h;
Shan Huang2b31a362021-08-16 16:45:39 +00001651 // Set decoding option to alpha unpremultiplied so that the R, G, B channels
1652 // of transparent pixels are preserved.
1653 initTexture(frame.map, &w, &h, false /* don't premultiply alpha */);
Mathias Agopiana8826d62009-10-01 03:10:14 -07001654 }
1655
joenchen764ea042022-04-11 23:58:34 +08001656 const int trimWidth = frame.trimWidth * ratio_w;
1657 const int trimHeight = frame.trimHeight * ratio_h;
1658 const int trimX = frame.trimX * ratio_w;
1659 const int trimY = frame.trimY * ratio_h;
1660 const int xc = animationX + trimX;
1661 const int yc = animationY + trimY;
Shan Huang2b31a362021-08-16 16:45:39 +00001662 glClear(GL_COLOR_BUFFER_BIT);
Geoffrey Pitschdd214a72016-06-27 17:14:30 -04001663 // specify the y center as ceiling((mHeight - frame.trimHeight) / 2)
1664 // which is equivalent to mHeight - (yc + frame.trimHeight)
joenchen764ea042022-04-11 23:58:34 +08001665 const int frameDrawY = mHeight - (yc + trimHeight);
Yegor Malyshev6925e1b2020-09-10 16:42:20 +02001666
Lucas Dupin8eedc022021-07-29 21:49:07 +00001667 float fade = 0;
Yegor Malyshev6925e1b2020-09-10 16:42:20 +02001668 // if the part hasn't been stopped yet then continue fading if necessary
1669 if (exitPending() && part.hasFadingPhase()) {
Lucas Dupin8eedc022021-07-29 21:49:07 +00001670 fade = static_cast<float>(++fadedFramesCount) / part.framesToFadeCount;
Yegor Malyshev6925e1b2020-09-10 16:42:20 +02001671 if (fadedFramesCount >= part.framesToFadeCount) {
1672 fadedFramesCount = MAX_FADED_FRAMES_COUNT; // no more fading
1673 }
1674 }
Lucas Dupin8eedc022021-07-29 21:49:07 +00001675 glUseProgram(mImageShader);
1676 glUniform1i(mImageTextureLocation, 0);
1677 glUniform1f(mImageFadeLocation, fade);
Shan Huang2b31a362021-08-16 16:45:39 +00001678 if (animation.dynamicColoringEnabled) {
1679 glUniform1f(mImageColorProgressLocation, colorProgress);
1680 }
Lucas Dupin8eedc022021-07-29 21:49:07 +00001681 glEnable(GL_BLEND);
joenchen764ea042022-04-11 23:58:34 +08001682 drawTexturedQuad(xc, frameDrawY, trimWidth, trimHeight);
Lucas Dupin8eedc022021-07-29 21:49:07 +00001683 glDisable(GL_BLEND);
Yegor Malyshev6925e1b2020-09-10 16:42:20 +02001684
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -07001685 if (mClockEnabled && mTimeIsAccurate && validClock(part)) {
1686 drawClock(animation.clockFont, part.clockPosX, part.clockPosY);
Damien Bargiacchia704b7d2016-02-16 16:55:49 -08001687 }
Yegor Malyshev6925e1b2020-09-10 16:42:20 +02001688
Nicolas Geoffray41fffb42020-12-15 10:50:29 +00001689 if (displayProgress) {
1690 int newProgress = android::base::GetIntProperty(PROGRESS_PROP_NAME, 0);
1691 // In case the new progress jumped suddenly, still show an
1692 // increment of 1.
1693 if (lastDisplayedProgress != 100) {
1694 // Artificially sleep 1/10th a second to slow down the animation.
1695 usleep(100000);
1696 if (lastDisplayedProgress < newProgress) {
1697 lastDisplayedProgress++;
1698 }
1699 }
1700 // Put the progress percentage right below the animation.
1701 int posY = animation.height / 3;
1702 int posX = TEXT_CENTER_VALUE;
1703 drawProgress(lastDisplayedProgress, animation.progressFont, posX, posY);
1704 }
1705
Adrian Roos9ee5dff2018-08-22 20:19:49 +02001706 handleViewport(frameDuration);
Damien Bargiacchia704b7d2016-02-16 16:55:49 -08001707
Mathias Agopiana8826d62009-10-01 03:10:14 -07001708 eglSwapBuffers(mDisplay, mSurface);
1709
1710 nsecs_t now = systemTime();
1711 nsecs_t delay = frameDuration - (now - lastFrame);
Wei Wang159a4102018-10-23 23:15:58 -07001712 //SLOGD("%lld, %lld", ns2ms(now - lastFrame), ns2ms(delay));
Mathias Agopiana8826d62009-10-01 03:10:14 -07001713 lastFrame = now;
Mathias Agopiandb7dd2a2012-05-12 15:08:21 -07001714
1715 if (delay > 0) {
1716 struct timespec spec;
1717 spec.tv_sec = (now + delay) / 1000000000;
1718 spec.tv_nsec = (now + delay) % 1000000000;
1719 int err;
1720 do {
Yi Kong911ac232019-03-24 01:49:02 -07001721 err = clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &spec, nullptr);
Keith Mok0f2e6682021-11-15 05:50:32 +00001722 } while (err == EINTR);
Mathias Agopiandb7dd2a2012-05-12 15:08:21 -07001723 }
Kevin Hesterd3782b22012-04-26 10:38:55 -07001724
1725 checkExit();
Mathias Agopiana8826d62009-10-01 03:10:14 -07001726 }
Kevin Hesterd3782b22012-04-26 10:38:55 -07001727
niuwenchao9cd6ac82022-12-23 17:34:22 +08001728 int pauseDuration = part.pause * ns2us(frameDuration);
1729 while(pauseDuration > 0 && !exitPending()){
1730 if (pauseDuration > MAX_CHECK_EXIT_INTERVAL_US) {
1731 usleep(MAX_CHECK_EXIT_INTERVAL_US);
1732 pauseDuration -= MAX_CHECK_EXIT_INTERVAL_US;
1733 } else {
1734 usleep(pauseDuration);
1735 break;
1736 }
1737 checkExit();
1738 }
Kevin Hesterd3782b22012-04-26 10:38:55 -07001739
Yegor Malyshev6925e1b2020-09-10 16:42:20 +02001740 if (exitPending() && !part.count && mCurrentInset >= mTargetInset &&
1741 !part.hasFadingPhase()) {
Nicolas Geoffraya1298752021-04-20 15:15:06 +01001742 if (lastDisplayedProgress != 0 && lastDisplayedProgress != 100) {
1743 android::base::SetProperty(PROGRESS_PROP_NAME, "100");
1744 continue;
1745 }
Yegor Malyshev6925e1b2020-09-10 16:42:20 +02001746 break; // exit the infinite non-fading part when it has been played at least once
1747 }
Mathias Agopiana8826d62009-10-01 03:10:14 -07001748 }
Geoffrey Pitsch2fb30fb2016-07-06 16:16:20 -04001749 }
1750
1751 // Free textures created for looping parts now that the animation is done.
1752 for (const Animation::Part& part : animation.parts) {
Mathias Agopiana8826d62009-10-01 03:10:14 -07001753 if (part.count != 1) {
Geoffrey Pitsch2fb30fb2016-07-06 16:16:20 -04001754 const size_t fcount = part.frames.size();
1755 for (size_t j = 0; j < fcount; j++) {
Mathias Agopiana8826d62009-10-01 03:10:14 -07001756 const Animation::Frame& frame(part.frames[j]);
1757 glDeleteTextures(1, &frame.tid);
1758 }
1759 }
1760 }
Geoffrey Pitschd6d9a1d2016-06-08 00:38:58 -07001761
Yoshiaki Maruoka9ef19b82017-10-25 13:55:49 +09001762 ALOGD("%sAnimationShownTiming End time: %" PRId64 "ms", mShuttingDown ? "Shutdown" : "Boot",
1763 elapsedRealtime());
1764
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001765 return true;
Mathias Agopiana8826d62009-10-01 03:10:14 -07001766}
1767
Marin Shalamanov9070c052020-05-18 19:05:17 +02001768void BootAnimation::processDisplayEvents() {
Parth Sane7f7b2212023-04-12 17:23:02 +00001769 ATRACE_CALL();
Marin Shalamanov9070c052020-05-18 19:05:17 +02001770 // This will poll mDisplayEventReceiver and if there are new events it'll call
1771 // displayEventCallback synchronously.
1772 mLooper->pollOnce(0);
1773}
1774
Adrian Roos9ee5dff2018-08-22 20:19:49 +02001775void BootAnimation::handleViewport(nsecs_t timestep) {
Parth Sane7f7b2212023-04-12 17:23:02 +00001776 ATRACE_CALL();
Adrian Roos9ee5dff2018-08-22 20:19:49 +02001777 if (mShuttingDown || !mFlingerSurfaceControl || mTargetInset == 0) {
1778 return;
1779 }
1780 if (mTargetInset < 0) {
1781 // Poll the amount for the top display inset. This will return -1 until persistent properties
1782 // have been loaded.
1783 mTargetInset = android::base::GetIntProperty("persist.sys.displayinset.top",
1784 -1 /* default */, -1 /* min */, mHeight / 2 /* max */);
1785 }
1786 if (mTargetInset <= 0) {
1787 return;
1788 }
1789
1790 if (mCurrentInset < mTargetInset) {
1791 // After the device boots, the inset will effectively be cropped away. We animate this here.
1792 float fraction = static_cast<float>(mCurrentInset) / mTargetInset;
1793 int interpolatedInset = (cosf((fraction + 1) * M_PI) / 2.0f + 0.5f) * mTargetInset;
1794
1795 SurfaceComposerClient::Transaction()
1796 .setCrop(mFlingerSurfaceControl, Rect(0, interpolatedInset, mWidth, mHeight))
1797 .apply();
1798 } else {
1799 // At the end of the animation, we switch to the viewport that DisplayManager will apply
1800 // later. This changes the coordinate system, and means we must move the surface up by
1801 // the inset amount.
Adrian Roos9ee5dff2018-08-22 20:19:49 +02001802 Rect layerStackRect(0, 0, mWidth, mHeight - mTargetInset);
1803 Rect displayRect(0, mTargetInset, mWidth, mHeight);
1804
1805 SurfaceComposerClient::Transaction t;
1806 t.setPosition(mFlingerSurfaceControl, 0, -mTargetInset)
1807 .setCrop(mFlingerSurfaceControl, Rect(0, mTargetInset, mWidth, mHeight));
Dominik Laskowski2a3d9aa2019-11-18 20:26:39 -08001808 t.setDisplayProjection(mDisplayToken, ui::ROTATION_0, layerStackRect, displayRect);
Adrian Roos9ee5dff2018-08-22 20:19:49 +02001809 t.apply();
1810
1811 mTargetInset = mCurrentInset = 0;
1812 }
1813
1814 int delta = timestep * mTargetInset / ms2ns(200);
1815 mCurrentInset += delta;
1816}
1817
Marin Shalamanov9070c052020-05-18 19:05:17 +02001818void BootAnimation::releaseAnimation(Animation* animation) const {
Parth Sane7f7b2212023-04-12 17:23:02 +00001819 ATRACE_CALL();
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001820 for (Vector<Animation::Part>::iterator it = animation->parts.begin(),
1821 e = animation->parts.end(); it != e; ++it) {
1822 if (it->animation)
1823 releaseAnimation(it->animation);
1824 }
1825 if (animation->zip)
1826 delete animation->zip;
1827 delete animation;
1828}
1829
Marin Shalamanov9070c052020-05-18 19:05:17 +02001830BootAnimation::Animation* BootAnimation::loadAnimation(const String8& fn) {
Parth Sane7f7b2212023-04-12 17:23:02 +00001831 ATRACE_CALL();
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001832 if (mLoadedFiles.indexOf(fn) >= 0) {
Wei Wang159a4102018-10-23 23:15:58 -07001833 SLOGE("File \"%s\" is already loaded. Cyclic ref is not allowed",
Tomasz Wasilczyk3815d342023-08-10 23:54:44 +00001834 fn.c_str());
Yi Kong911ac232019-03-24 01:49:02 -07001835 return nullptr;
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001836 }
Tomasz Wasilczyk835dfe52023-08-17 16:27:22 +00001837 ZipFileRO *zip = ZipFileRO::open(fn.c_str());
Yi Kong911ac232019-03-24 01:49:02 -07001838 if (zip == nullptr) {
Wei Wang159a4102018-10-23 23:15:58 -07001839 SLOGE("Failed to open animation zip \"%s\": %s",
Tomasz Wasilczyk3815d342023-08-10 23:54:44 +00001840 fn.c_str(), strerror(errno));
Yi Kong911ac232019-03-24 01:49:02 -07001841 return nullptr;
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001842 }
1843
Tomasz Wasilczyk3815d342023-08-10 23:54:44 +00001844 ALOGD("%s is loaded successfully", fn.c_str());
Yoshiaki Maruoka9ef19b82017-10-25 13:55:49 +09001845
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001846 Animation *animation = new Animation;
1847 animation->fileName = fn;
1848 animation->zip = zip;
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -07001849 animation->clockFont.map = nullptr;
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001850 mLoadedFiles.add(animation->fileName);
1851
1852 parseAnimationDesc(*animation);
Geoffrey Pitscha91a2d72016-07-12 14:46:19 -04001853 if (!preloadZip(*animation)) {
Greg Kaiser7426ec82019-09-11 14:34:27 -07001854 releaseAnimation(animation);
Yi Kong911ac232019-03-24 01:49:02 -07001855 return nullptr;
Geoffrey Pitscha91a2d72016-07-12 14:46:19 -04001856 }
1857
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001858 mLoadedFiles.remove(fn);
1859 return animation;
1860}
Damien Bargiacchi97480862016-03-29 14:55:55 -07001861
1862bool BootAnimation::updateIsTimeAccurate() {
Parth Sane7f7b2212023-04-12 17:23:02 +00001863 ATRACE_CALL();
Damien Bargiacchi97480862016-03-29 14:55:55 -07001864 static constexpr long long MAX_TIME_IN_PAST = 60000LL * 60LL * 24LL * 30LL; // 30 days
1865 static constexpr long long MAX_TIME_IN_FUTURE = 60000LL * 90LL; // 90 minutes
1866
1867 if (mTimeIsAccurate) {
1868 return true;
1869 }
Keun-young Parkb5938422017-03-23 13:46:24 -07001870 if (mShuttingDown) return true;
Damien Bargiacchi97480862016-03-29 14:55:55 -07001871 struct stat statResult;
Damien Bargiacchi9071db12016-10-28 17:38:22 -07001872
1873 if(stat(TIME_FORMAT_12_HOUR_FLAG_FILE_PATH, &statResult) == 0) {
1874 mTimeFormat12Hour = true;
1875 }
1876
Damien Bargiacchi97480862016-03-29 14:55:55 -07001877 if(stat(ACCURATE_TIME_FLAG_FILE_PATH, &statResult) == 0) {
1878 mTimeIsAccurate = true;
1879 return true;
1880 }
1881
1882 FILE* file = fopen(LAST_TIME_CHANGED_FILE_PATH, "r");
Yi Kong911ac232019-03-24 01:49:02 -07001883 if (file != nullptr) {
Damien Bargiacchi97480862016-03-29 14:55:55 -07001884 long long lastChangedTime = 0;
1885 fscanf(file, "%lld", &lastChangedTime);
1886 fclose(file);
1887 if (lastChangedTime > 0) {
1888 struct timespec now;
1889 clock_gettime(CLOCK_REALTIME, &now);
1890 // Match the Java timestamp format
1891 long long rtcNow = (now.tv_sec * 1000LL) + (now.tv_nsec / 1000000LL);
Damien Bargiacchi96762812016-07-12 15:53:40 -07001892 if (ACCURATE_TIME_EPOCH < rtcNow
1893 && lastChangedTime > (rtcNow - MAX_TIME_IN_PAST)
1894 && lastChangedTime < (rtcNow + MAX_TIME_IN_FUTURE)) {
Damien Bargiacchi97480862016-03-29 14:55:55 -07001895 mTimeIsAccurate = true;
1896 }
1897 }
1898 }
1899
1900 return mTimeIsAccurate;
1901}
1902
1903BootAnimation::TimeCheckThread::TimeCheckThread(BootAnimation* bootAnimation) : Thread(false),
Josh Yangf95b2002021-12-23 14:32:28 -08001904 mInotifyFd(-1), mBootAnimWd(-1), mTimeWd(-1), mBootAnimation(bootAnimation) {}
Damien Bargiacchi97480862016-03-29 14:55:55 -07001905
1906BootAnimation::TimeCheckThread::~TimeCheckThread() {
Parth Sane7f7b2212023-04-12 17:23:02 +00001907 ATRACE_CALL();
Damien Bargiacchi97480862016-03-29 14:55:55 -07001908 // mInotifyFd may be -1 but that's ok since we're not at risk of attempting to close a valid FD.
1909 close(mInotifyFd);
1910}
1911
1912bool BootAnimation::TimeCheckThread::threadLoop() {
Parth Sane7f7b2212023-04-12 17:23:02 +00001913 ATRACE_CALL();
Damien Bargiacchi97480862016-03-29 14:55:55 -07001914 bool shouldLoop = doThreadLoop() && !mBootAnimation->mTimeIsAccurate
1915 && mBootAnimation->mClockEnabled;
1916 if (!shouldLoop) {
1917 close(mInotifyFd);
1918 mInotifyFd = -1;
1919 }
1920 return shouldLoop;
1921}
1922
1923bool BootAnimation::TimeCheckThread::doThreadLoop() {
Parth Sane7f7b2212023-04-12 17:23:02 +00001924 ATRACE_CALL();
Damien Bargiacchi97480862016-03-29 14:55:55 -07001925 static constexpr int BUFF_LEN (10 * (sizeof(struct inotify_event) + NAME_MAX + 1));
1926
1927 // Poll instead of doing a blocking read so the Thread can exit if requested.
1928 struct pollfd pfd = { mInotifyFd, POLLIN, 0 };
1929 ssize_t pollResult = poll(&pfd, 1, 1000);
1930
1931 if (pollResult == 0) {
1932 return true;
1933 } else if (pollResult < 0) {
Wei Wang159a4102018-10-23 23:15:58 -07001934 SLOGE("Could not poll inotify events");
Damien Bargiacchi97480862016-03-29 14:55:55 -07001935 return false;
1936 }
1937
1938 char buff[BUFF_LEN] __attribute__ ((aligned(__alignof__(struct inotify_event))));;
1939 ssize_t length = read(mInotifyFd, buff, BUFF_LEN);
1940 if (length == 0) {
1941 return true;
1942 } else if (length < 0) {
Wei Wang159a4102018-10-23 23:15:58 -07001943 SLOGE("Could not read inotify events");
Damien Bargiacchi97480862016-03-29 14:55:55 -07001944 return false;
1945 }
1946
1947 const struct inotify_event *event;
1948 for (char* ptr = buff; ptr < buff + length; ptr += sizeof(struct inotify_event) + event->len) {
1949 event = (const struct inotify_event *) ptr;
Josh Yangf95b2002021-12-23 14:32:28 -08001950 if (event->wd == mBootAnimWd && strcmp(BOOTANIM_TIME_DIR_NAME, event->name) == 0) {
Damien Bargiacchi97480862016-03-29 14:55:55 -07001951 addTimeDirWatch();
1952 } else if (event->wd == mTimeWd && (strcmp(LAST_TIME_CHANGED_FILE_NAME, event->name) == 0
1953 || strcmp(ACCURATE_TIME_FLAG_FILE_NAME, event->name) == 0)) {
1954 return !mBootAnimation->updateIsTimeAccurate();
1955 }
1956 }
1957
1958 return true;
1959}
1960
1961void BootAnimation::TimeCheckThread::addTimeDirWatch() {
Parth Sane7f7b2212023-04-12 17:23:02 +00001962 ATRACE_CALL();
Josh Yangf95b2002021-12-23 14:32:28 -08001963 mTimeWd = inotify_add_watch(mInotifyFd, BOOTANIM_TIME_DIR_PATH,
Damien Bargiacchi97480862016-03-29 14:55:55 -07001964 IN_CLOSE_WRITE | IN_MOVED_TO | IN_ATTRIB);
1965 if (mTimeWd > 0) {
1966 // No need to watch for the time directory to be created if it already exists
Josh Yangf95b2002021-12-23 14:32:28 -08001967 inotify_rm_watch(mInotifyFd, mBootAnimWd);
1968 mBootAnimWd = -1;
Damien Bargiacchi97480862016-03-29 14:55:55 -07001969 }
1970}
1971
1972status_t BootAnimation::TimeCheckThread::readyToRun() {
Parth Sane7f7b2212023-04-12 17:23:02 +00001973 ATRACE_CALL();
Damien Bargiacchi97480862016-03-29 14:55:55 -07001974 mInotifyFd = inotify_init();
1975 if (mInotifyFd < 0) {
Wei Wang159a4102018-10-23 23:15:58 -07001976 SLOGE("Could not initialize inotify fd");
Damien Bargiacchi97480862016-03-29 14:55:55 -07001977 return NO_INIT;
1978 }
1979
Josh Yangf95b2002021-12-23 14:32:28 -08001980 mBootAnimWd = inotify_add_watch(mInotifyFd, BOOTANIM_DATA_DIR_PATH, IN_CREATE | IN_ATTRIB);
1981 if (mBootAnimWd < 0) {
Damien Bargiacchi97480862016-03-29 14:55:55 -07001982 close(mInotifyFd);
1983 mInotifyFd = -1;
Josh Yangf95b2002021-12-23 14:32:28 -08001984 SLOGE("Could not add watch for %s: %s", BOOTANIM_DATA_DIR_PATH, strerror(errno));
Damien Bargiacchi97480862016-03-29 14:55:55 -07001985 return NO_INIT;
1986 }
1987
1988 addTimeDirWatch();
1989
1990 if (mBootAnimation->updateIsTimeAccurate()) {
1991 close(mInotifyFd);
1992 mInotifyFd = -1;
1993 return ALREADY_EXISTS;
1994 }
1995
1996 return NO_ERROR;
1997}
1998
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001999// ---------------------------------------------------------------------------
2000
Marin Shalamanov9070c052020-05-18 19:05:17 +02002001} // namespace android