blob: c216d1675bb36ad5fa6d38b825fbcf6c00172aca [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
Nikita Ioffe06c986e2020-01-27 17:16:03 +000021#include <vector>
22
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080023#include <stdint.h>
Wei Wanga90c54c2017-02-02 11:35:21 -080024#include <inttypes.h>
Damien Bargiacchi97480862016-03-29 14:55:55 -070025#include <sys/inotify.h>
26#include <sys/poll.h>
27#include <sys/stat.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080028#include <sys/types.h>
29#include <math.h>
30#include <fcntl.h>
31#include <utils/misc.h>
Parth Sane7f7b2212023-04-12 17:23:02 +000032#include <utils/Trace.h>
Mathias Agopianb4d5a722009-09-23 17:05:19 -070033#include <signal.h>
Elliott Hughesbb94f312014-10-21 10:41:33 -070034#include <time.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080035
Steven Morelandfb7952f2018-02-23 14:58:50 -080036#include <cutils/atomic.h>
Jason parksbd9a08d2011-01-31 15:04:34 -060037#include <cutils/properties.h>
38
Derek Sollenberger22d0d4c72020-04-08 10:53:46 -040039#include <android/imagedecoder.h>
Mathias Agopianb13b9bd2012-02-17 18:27:36 -080040#include <androidfw/AssetManager.h>
Mathias Agopianac31a3b2009-05-21 19:59:24 -070041#include <binder/IPCThreadState.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080042#include <utils/Errors.h>
43#include <utils/Log.h>
Wei Wanga90c54c2017-02-02 11:35:21 -080044#include <utils/SystemClock.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080045
Keun-young Parkb5938422017-03-23 13:46:24 -070046#include <android-base/properties.h>
47
Marin Shalamanovcb783ca2021-01-29 21:26:07 +010048#include <ui/DisplayMode.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080049#include <ui/PixelFormat.h>
50#include <ui/Rect.h>
51#include <ui/Region.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080052
Jeff Brown0b722fe2012-08-24 22:40:14 -070053#include <gui/ISurfaceComposer.h>
Marin Shalamanov9070c052020-05-18 19:05:17 +020054#include <gui/DisplayEventReceiver.h>
Mathias Agopian8335f1c2012-02-25 18:48:35 -080055#include <gui/Surface.h>
56#include <gui/SurfaceComposerClient.h>
Lucas Dupin8eedc022021-07-29 21:49:07 +000057#include <GLES2/gl2.h>
58#include <GLES2/gl2ext.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080059#include <EGL/eglext.h>
60
61#include "BootAnimation.h"
62
Kyeongkab.Nam35a8a112019-07-17 16:41:48 +090063#define ANIM_PATH_MAX 255
64#define STR(x) #x
65#define STRTO(x) STR(x)
66
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080067namespace android {
68
Marin Shalamanovcb783ca2021-01-29 21:26:07 +010069using ui::DisplayMode;
70
Damien Bargiacchi97480862016-03-29 14:55:55 -070071static const char OEM_BOOTANIMATION_FILE[] = "/oem/media/bootanimation.zip";
Beverly34ffe252019-05-17 11:03:54 -040072static const char PRODUCT_BOOTANIMATION_DARK_FILE[] = "/product/media/bootanimation-dark.zip";
Jaekyun Seokc521bb32018-01-30 11:52:47 +090073static const char PRODUCT_BOOTANIMATION_FILE[] = "/product/media/bootanimation.zip";
Damien Bargiacchi97480862016-03-29 14:55:55 -070074static const char SYSTEM_BOOTANIMATION_FILE[] = "/system/media/bootanimation.zip";
Anders Fridlunde0b4d232018-11-06 14:23:25 +010075static const char APEX_BOOTANIMATION_FILE[] = "/apex/com.android.bootanimation/etc/bootanimation.zip";
Keun-young Parkb5938422017-03-23 13:46:24 -070076static const char OEM_SHUTDOWNANIMATION_FILE[] = "/oem/media/shutdownanimation.zip";
Jaekyun Seokc521bb32018-01-30 11:52:47 +090077static const char PRODUCT_SHUTDOWNANIMATION_FILE[] = "/product/media/shutdownanimation.zip";
Keun-young Parkb5938422017-03-23 13:46:24 -070078static const char SYSTEM_SHUTDOWNANIMATION_FILE[] = "/system/media/shutdownanimation.zip";
79
Nikita Ioffe06c986e2020-01-27 17:16:03 +000080static constexpr const char* PRODUCT_USERSPACE_REBOOT_ANIMATION_FILE = "/product/media/userspace-reboot.zip";
81static constexpr const char* OEM_USERSPACE_REBOOT_ANIMATION_FILE = "/oem/media/userspace-reboot.zip";
82static constexpr const char* SYSTEM_USERSPACE_REBOOT_ANIMATION_FILE = "/system/media/userspace-reboot.zip";
83
Josh Yangf95b2002021-12-23 14:32:28 -080084static const char BOOTANIM_DATA_DIR_PATH[] = "/data/bootanim";
85static const char BOOTANIM_TIME_DIR_NAME[] = "time";
86static const char BOOTANIM_TIME_DIR_PATH[] = "/data/bootanim/time";
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -070087static const char CLOCK_FONT_ASSET[] = "images/clock_font.png";
88static const char CLOCK_FONT_ZIP_NAME[] = "clock_font.png";
Nicolas Geoffray41fffb42020-12-15 10:50:29 +000089static const char PROGRESS_FONT_ASSET[] = "images/progress_font.png";
90static const char PROGRESS_FONT_ZIP_NAME[] = "progress_font.png";
Damien Bargiacchi97480862016-03-29 14:55:55 -070091static const char LAST_TIME_CHANGED_FILE_NAME[] = "last_time_change";
Josh Yangf95b2002021-12-23 14:32:28 -080092static const char LAST_TIME_CHANGED_FILE_PATH[] = "/data/bootanim/time/last_time_change";
Damien Bargiacchi97480862016-03-29 14:55:55 -070093static const char ACCURATE_TIME_FLAG_FILE_NAME[] = "time_is_accurate";
Josh Yangf95b2002021-12-23 14:32:28 -080094static const char ACCURATE_TIME_FLAG_FILE_PATH[] = "/data/bootanim/time/time_is_accurate";
95static const char TIME_FORMAT_12_HOUR_FLAG_FILE_PATH[] = "/data/bootanim/time/time_format_12_hour";
Damien Bargiacchi96762812016-07-12 15:53:40 -070096// Java timestamp format. Don't show the clock if the date is before 2000-01-01 00:00:00.
97static const long long ACCURATE_TIME_EPOCH = 946684800000;
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -070098static constexpr char FONT_BEGIN_CHAR = ' ';
99static constexpr char FONT_END_CHAR = '~' + 1;
100static constexpr size_t FONT_NUM_CHARS = FONT_END_CHAR - FONT_BEGIN_CHAR + 1;
101static constexpr size_t FONT_NUM_COLS = 16;
102static constexpr size_t FONT_NUM_ROWS = FONT_NUM_CHARS / FONT_NUM_COLS;
103static const int TEXT_CENTER_VALUE = INT_MAX;
104static const int TEXT_MISSING_VALUE = INT_MIN;
Damien Bargiacchi97480862016-03-29 14:55:55 -0700105static const char EXIT_PROP_NAME[] = "service.bootanim.exit";
Nicolas Geoffray41fffb42020-12-15 10:50:29 +0000106static const char PROGRESS_PROP_NAME[] = "service.bootanim.progress";
Huihong Luod4a217e2020-01-16 15:56:33 -0800107static const char DISPLAYS_PROP_NAME[] = "persist.service.bootanim.displays";
Thiébaud Weksteen08de4432021-11-18 13:25:59 +1100108static const char CLOCK_ENABLED_PROP_NAME[] = "persist.sys.bootanim.clock.enabled";
Kyeongkab.Nam35a8a112019-07-17 16:41:48 +0900109static const int ANIM_ENTRY_NAME_MAX = ANIM_PATH_MAX + 1;
niuwenchao9cd6ac82022-12-23 17:34:22 +0800110static const int MAX_CHECK_EXIT_INTERVAL_US = 50000;
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700111static constexpr size_t TEXT_POS_LEN_MAX = 16;
Shan Huang2b31a362021-08-16 16:45:39 +0000112static const int DYNAMIC_COLOR_COUNT = 4;
Lucas Dupin8eedc022021-07-29 21:49:07 +0000113static const char U_TEXTURE[] = "uTexture";
114static const char U_FADE[] = "uFade";
115static const char U_CROP_AREA[] = "uCropArea";
Shan Huang2b31a362021-08-16 16:45:39 +0000116static const char U_START_COLOR_PREFIX[] = "uStartColor";
117static const char U_END_COLOR_PREFIX[] = "uEndColor";
118static const char U_COLOR_PROGRESS[] = "uColorProgress";
Lucas Dupin8eedc022021-07-29 21:49:07 +0000119static const char A_UV[] = "aUv";
120static const char A_POSITION[] = "aPosition";
121static const char VERTEX_SHADER_SOURCE[] = R"(
122 precision mediump float;
123 attribute vec4 aPosition;
124 attribute highp vec2 aUv;
125 varying highp vec2 vUv;
126 void main() {
127 gl_Position = aPosition;
128 vUv = aUv;
129 })";
Shan Huang2b31a362021-08-16 16:45:39 +0000130static const char IMAGE_FRAG_DYNAMIC_COLORING_SHADER_SOURCE[] = R"(
131 precision mediump float;
Shan Huang81846ba2021-09-07 19:07:47 +0000132 const float cWhiteMaskThreshold = 0.05;
Shan Huang2b31a362021-08-16 16:45:39 +0000133 uniform sampler2D uTexture;
134 uniform float uFade;
135 uniform float uColorProgress;
Andrew Zengf9f58222022-11-08 11:10:48 -0800136 uniform vec3 uStartColor0;
137 uniform vec3 uStartColor1;
138 uniform vec3 uStartColor2;
139 uniform vec3 uStartColor3;
140 uniform vec3 uEndColor0;
141 uniform vec3 uEndColor1;
142 uniform vec3 uEndColor2;
143 uniform vec3 uEndColor3;
Shan Huang2b31a362021-08-16 16:45:39 +0000144 varying highp vec2 vUv;
145 void main() {
146 vec4 mask = texture2D(uTexture, vUv);
Shan Huang488ff742021-08-25 20:10:46 +0000147 float r = mask.r;
148 float g = mask.g;
149 float b = mask.b;
150 float a = mask.a;
151 // If all channels have values, render pixel as a shade of white.
152 float useWhiteMask = step(cWhiteMaskThreshold, r)
153 * step(cWhiteMaskThreshold, g)
154 * step(cWhiteMaskThreshold, b)
155 * step(cWhiteMaskThreshold, a);
Andrew Zengf9f58222022-11-08 11:10:48 -0800156 vec3 color = r * mix(uStartColor0, uEndColor0, uColorProgress)
Shan Huang488ff742021-08-25 20:10:46 +0000157 + g * mix(uStartColor1, uEndColor1, uColorProgress)
158 + b * mix(uStartColor2, uEndColor2, uColorProgress)
159 + a * mix(uStartColor3, uEndColor3, uColorProgress);
Andrew Zengf9f58222022-11-08 11:10:48 -0800160 color = mix(color, vec3((r + g + b + a) * 0.25), useWhiteMask);
161 gl_FragColor = vec4(color.x, color.y, color.z, (1.0 - uFade));
Shan Huang2b31a362021-08-16 16:45:39 +0000162 })";
Lucas Dupin8eedc022021-07-29 21:49:07 +0000163static const char IMAGE_FRAG_SHADER_SOURCE[] = R"(
164 precision mediump float;
165 uniform sampler2D uTexture;
166 uniform float uFade;
167 varying highp vec2 vUv;
168 void main() {
169 vec4 color = texture2D(uTexture, vUv);
Shan Huang2b31a362021-08-16 16:45:39 +0000170 gl_FragColor = vec4(color.x, color.y, color.z, (1.0 - uFade)) * color.a;
Lucas Dupin8eedc022021-07-29 21:49:07 +0000171 })";
172static const char TEXT_FRAG_SHADER_SOURCE[] = R"(
173 precision mediump float;
174 uniform sampler2D uTexture;
175 uniform vec4 uCropArea;
176 varying highp vec2 vUv;
177 void main() {
178 vec2 uv = vec2(mix(uCropArea.x, uCropArea.z, vUv.x),
179 mix(uCropArea.y, uCropArea.w, vUv.y));
180 gl_FragColor = texture2D(uTexture, uv);
181 })";
182
183static GLfloat quadPositions[] = {
184 -0.5f, -0.5f,
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};
191static GLfloat quadUVs[] = {
192 0.0f, 1.0f,
193 1.0f, 1.0f,
194 1.0f, 0.0f,
195 1.0f, 0.0f,
196 0.0f, 0.0f,
197 0.0f, 1.0f
198};
Narayan Kamathafd31e02013-12-03 13:16:03 +0000199
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800200// ---------------------------------------------------------------------------
201
Ed Coyne7464ac92017-06-08 12:26:48 -0700202BootAnimation::BootAnimation(sp<Callbacks> callbacks)
Marin Shalamanov9070c052020-05-18 19:05:17 +0200203 : Thread(false), mLooper(new Looper(false)), mClockEnabled(true), mTimeIsAccurate(false),
Yi Kong911ac232019-03-24 01:49:02 -0700204 mTimeFormat12Hour(false), mTimeCheckThread(nullptr), mCallbacks(callbacks) {
Parth Sane7f7b2212023-04-12 17:23:02 +0000205 ATRACE_CALL();
Mathias Agopian627e7b52009-05-21 19:21:59 -0700206 mSession = new SurfaceComposerClient();
Geoffrey Pitsch290c4352016-08-09 14:35:10 -0400207
Keun-young Parkb5938422017-03-23 13:46:24 -0700208 std::string powerCtl = android::base::GetProperty("sys.powerctl", "");
209 if (powerCtl.empty()) {
210 mShuttingDown = false;
211 } else {
212 mShuttingDown = true;
213 }
Huihong Luo50a2f362018-08-11 09:27:57 -0700214 ALOGD("%sAnimationStartTiming start time: %" PRId64 "ms", mShuttingDown ? "Shutdown" : "Boot",
215 elapsedRealtime());
216}
217
218BootAnimation::~BootAnimation() {
Parth Sane7f7b2212023-04-12 17:23:02 +0000219 ATRACE_CALL();
Huihong Luo50a2f362018-08-11 09:27:57 -0700220 if (mAnimation != nullptr) {
221 releaseAnimation(mAnimation);
222 mAnimation = nullptr;
223 }
224 ALOGD("%sAnimationStopTiming start time: %" PRId64 "ms", mShuttingDown ? "Shutdown" : "Boot",
225 elapsedRealtime());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800226}
227
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800228void BootAnimation::onFirstRef() {
Parth Sane7f7b2212023-04-12 17:23:02 +0000229 ATRACE_CALL();
Mathias Agopianbc726112009-09-23 15:44:05 -0700230 status_t err = mSession->linkToComposerDeath(this);
Wei Wang159a4102018-10-23 23:15:58 -0700231 SLOGE_IF(err, "linkToComposerDeath failed (%s) ", strerror(-err));
Mathias Agopian8434c532009-09-23 18:52:49 -0700232 if (err == NO_ERROR) {
Huihong Luo50a2f362018-08-11 09:27:57 -0700233 // Load the animation content -- this can be slow (eg 200ms)
234 // called before waitForSurfaceFlinger() in main() to avoid wait
235 ALOGD("%sAnimationPreloadTiming start time: %" PRId64 "ms",
236 mShuttingDown ? "Shutdown" : "Boot", elapsedRealtime());
237 preloadAnimation();
238 ALOGD("%sAnimationPreloadStopTiming start time: %" PRId64 "ms",
239 mShuttingDown ? "Shutdown" : "Boot", elapsedRealtime());
Mathias Agopianbc726112009-09-23 15:44:05 -0700240 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800241}
242
Mathias Agopianbc726112009-09-23 15:44:05 -0700243sp<SurfaceComposerClient> BootAnimation::session() const {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800244 return mSession;
245}
246
Marin Shalamanov9070c052020-05-18 19:05:17 +0200247void BootAnimation::binderDied(const wp<IBinder>&) {
Parth Sane7f7b2212023-04-12 17:23:02 +0000248 ATRACE_CALL();
Mathias Agopianbc726112009-09-23 15:44:05 -0700249 // woah, surfaceflinger died!
Wei Wang159a4102018-10-23 23:15:58 -0700250 SLOGD("SurfaceFlinger died, exiting...");
Mathias Agopianbc726112009-09-23 15:44:05 -0700251
252 // calling requestExit() is not enough here because the Surface code
253 // might be blocked on a condition variable that will never be updated.
254 kill( getpid(), SIGKILL );
255 requestExit();
256}
257
Shan Huang2b31a362021-08-16 16:45:39 +0000258static void* decodeImage(const void* encodedData, size_t dataLength, AndroidBitmapInfo* outInfo,
259 bool premultiplyAlpha) {
Parth Sane7f7b2212023-04-12 17:23:02 +0000260 ATRACE_CALL();
Derek Sollenberger22d0d4c72020-04-08 10:53:46 -0400261 AImageDecoder* decoder = nullptr;
262 AImageDecoder_createFromBuffer(encodedData, dataLength, &decoder);
263 if (!decoder) {
264 return nullptr;
265 }
266
267 const AImageDecoderHeaderInfo* info = AImageDecoder_getHeaderInfo(decoder);
268 outInfo->width = AImageDecoderHeaderInfo_getWidth(info);
269 outInfo->height = AImageDecoderHeaderInfo_getHeight(info);
270 outInfo->format = AImageDecoderHeaderInfo_getAndroidBitmapFormat(info);
271 outInfo->stride = AImageDecoder_getMinimumStride(decoder);
272 outInfo->flags = 0;
273
Shan Huang2b31a362021-08-16 16:45:39 +0000274 if (!premultiplyAlpha) {
275 AImageDecoder_setUnpremultipliedRequired(decoder, true);
276 }
277
Derek Sollenberger22d0d4c72020-04-08 10:53:46 -0400278 const size_t size = outInfo->stride * outInfo->height;
279 void* pixels = malloc(size);
280 int result = AImageDecoder_decodeImage(decoder, pixels, outInfo->stride, size);
281 AImageDecoder_delete(decoder);
282
283 if (result != ANDROID_IMAGE_DECODER_SUCCESS) {
284 free(pixels);
285 return nullptr;
286 }
287 return pixels;
288}
289
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800290status_t BootAnimation::initTexture(Texture* texture, AssetManager& assets,
Shan Huang2b31a362021-08-16 16:45:39 +0000291 const char* name, bool premultiplyAlpha) {
Parth Sane7f7b2212023-04-12 17:23:02 +0000292 ATRACE_CALL();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800293 Asset* asset = assets.open(name, Asset::ACCESS_BUFFER);
Yi Kong911ac232019-03-24 01:49:02 -0700294 if (asset == nullptr)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800295 return NO_INIT;
Derek Sollenberger22d0d4c72020-04-08 10:53:46 -0400296
297 AndroidBitmapInfo bitmapInfo;
Shan Huang2b31a362021-08-16 16:45:39 +0000298 void* pixels = decodeImage(asset->getBuffer(false), asset->getLength(), &bitmapInfo,
299 premultiplyAlpha);
Derek Sollenberger22d0d4c72020-04-08 10:53:46 -0400300 auto pixelDeleter = std::unique_ptr<void, decltype(free)*>{ pixels, free };
301
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800302 asset->close();
303 delete asset;
304
Derek Sollenberger22d0d4c72020-04-08 10:53:46 -0400305 if (!pixels) {
306 return NO_INIT;
307 }
308
309 const int w = bitmapInfo.width;
310 const int h = bitmapInfo.height;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800311
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800312 texture->w = w;
313 texture->h = h;
314
315 glGenTextures(1, &texture->name);
316 glBindTexture(GL_TEXTURE_2D, texture->name);
317
Derek Sollenberger22d0d4c72020-04-08 10:53:46 -0400318 switch (bitmapInfo.format) {
319 case ANDROID_BITMAP_FORMAT_A_8:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800320 glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, w, h, 0, GL_ALPHA,
Derek Sollenberger22d0d4c72020-04-08 10:53:46 -0400321 GL_UNSIGNED_BYTE, pixels);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800322 break;
Derek Sollenberger22d0d4c72020-04-08 10:53:46 -0400323 case ANDROID_BITMAP_FORMAT_RGBA_4444:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800324 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA,
Derek Sollenberger22d0d4c72020-04-08 10:53:46 -0400325 GL_UNSIGNED_SHORT_4_4_4_4, pixels);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800326 break;
Derek Sollenberger22d0d4c72020-04-08 10:53:46 -0400327 case ANDROID_BITMAP_FORMAT_RGBA_8888:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800328 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA,
Derek Sollenberger22d0d4c72020-04-08 10:53:46 -0400329 GL_UNSIGNED_BYTE, pixels);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800330 break;
Derek Sollenberger22d0d4c72020-04-08 10:53:46 -0400331 case ANDROID_BITMAP_FORMAT_RGB_565:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800332 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, w, h, 0, GL_RGB,
Derek Sollenberger22d0d4c72020-04-08 10:53:46 -0400333 GL_UNSIGNED_SHORT_5_6_5, pixels);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800334 break;
335 default:
336 break;
337 }
338
Lucas Dupin8eedc022021-07-29 21:49:07 +0000339 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
340 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
341 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
342 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700343
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800344 return NO_ERROR;
345}
346
Shan Huang2b31a362021-08-16 16:45:39 +0000347status_t BootAnimation::initTexture(FileMap* map, int* width, int* height,
348 bool premultiplyAlpha) {
Parth Sane7f7b2212023-04-12 17:23:02 +0000349 ATRACE_CALL();
Derek Sollenberger22d0d4c72020-04-08 10:53:46 -0400350 AndroidBitmapInfo bitmapInfo;
Shan Huang2b31a362021-08-16 16:45:39 +0000351 void* pixels = decodeImage(map->getDataPtr(), map->getDataLength(), &bitmapInfo,
352 premultiplyAlpha);
Derek Sollenberger22d0d4c72020-04-08 10:53:46 -0400353 auto pixelDeleter = std::unique_ptr<void, decltype(free)*>{ pixels, free };
Mathias Agopiana8826d62009-10-01 03:10:14 -0700354
Mykola Kondratenko0c1eeb32014-04-15 09:35:44 +0200355 // FileMap memory is never released until application exit.
356 // Release it now as the texture is already loaded and the memory used for
357 // the packed resource can be released.
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700358 delete map;
Mykola Kondratenko0c1eeb32014-04-15 09:35:44 +0200359
Derek Sollenberger22d0d4c72020-04-08 10:53:46 -0400360 if (!pixels) {
361 return NO_INIT;
362 }
363
364 const int w = bitmapInfo.width;
365 const int h = bitmapInfo.height;
Mathias Agopiana8826d62009-10-01 03:10:14 -0700366
Mathias Agopiana8826d62009-10-01 03:10:14 -0700367 int tw = 1 << (31 - __builtin_clz(w));
368 int th = 1 << (31 - __builtin_clz(h));
369 if (tw < w) tw <<= 1;
370 if (th < h) th <<= 1;
371
Derek Sollenberger22d0d4c72020-04-08 10:53:46 -0400372 switch (bitmapInfo.format) {
373 case ANDROID_BITMAP_FORMAT_RGBA_8888:
Sai Kiran Korwar27167492015-07-07 20:00:06 +0530374 if (!mUseNpotTextures && (tw != w || th != h)) {
Mathias Agopiana8826d62009-10-01 03:10:14 -0700375 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, tw, th, 0, GL_RGBA,
Yi Kong911ac232019-03-24 01:49:02 -0700376 GL_UNSIGNED_BYTE, nullptr);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700377 glTexSubImage2D(GL_TEXTURE_2D, 0,
Derek Sollenberger22d0d4c72020-04-08 10:53:46 -0400378 0, 0, w, h, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700379 } else {
Sai Kiran Korwar27167492015-07-07 20:00:06 +0530380 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA,
Derek Sollenberger22d0d4c72020-04-08 10:53:46 -0400381 GL_UNSIGNED_BYTE, pixels);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700382 }
383 break;
384
Derek Sollenberger22d0d4c72020-04-08 10:53:46 -0400385 case ANDROID_BITMAP_FORMAT_RGB_565:
Sai Kiran Korwar27167492015-07-07 20:00:06 +0530386 if (!mUseNpotTextures && (tw != w || th != h)) {
Mathias Agopiana8826d62009-10-01 03:10:14 -0700387 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, tw, th, 0, GL_RGB,
Yi Kong911ac232019-03-24 01:49:02 -0700388 GL_UNSIGNED_SHORT_5_6_5, nullptr);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700389 glTexSubImage2D(GL_TEXTURE_2D, 0,
Derek Sollenberger22d0d4c72020-04-08 10:53:46 -0400390 0, 0, w, h, GL_RGB, GL_UNSIGNED_SHORT_5_6_5, pixels);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700391 } else {
Sai Kiran Korwar27167492015-07-07 20:00:06 +0530392 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, w, h, 0, GL_RGB,
Derek Sollenberger22d0d4c72020-04-08 10:53:46 -0400393 GL_UNSIGNED_SHORT_5_6_5, pixels);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700394 }
395 break;
396 default:
397 break;
398 }
399
Lucas Dupin8eedc022021-07-29 21:49:07 +0000400 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
401 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
402 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
403 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700404
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700405 *width = w;
406 *height = h;
407
Mathias Agopiana8826d62009-10-01 03:10:14 -0700408 return NO_ERROR;
409}
410
Marin Shalamanov9070c052020-05-18 19:05:17 +0200411class BootAnimation::DisplayEventCallback : public LooperCallback {
412 BootAnimation* mBootAnimation;
413
414public:
415 DisplayEventCallback(BootAnimation* bootAnimation) {
Parth Sane7f7b2212023-04-12 17:23:02 +0000416 ATRACE_CALL();
Marin Shalamanov9070c052020-05-18 19:05:17 +0200417 mBootAnimation = bootAnimation;
418 }
419
420 int handleEvent(int /* fd */, int events, void* /* data */) {
Parth Sane7f7b2212023-04-12 17:23:02 +0000421 ATRACE_CALL();
Marin Shalamanov9070c052020-05-18 19:05:17 +0200422 if (events & (Looper::EVENT_ERROR | Looper::EVENT_HANGUP)) {
423 ALOGE("Display event receiver pipe was closed or an error occurred. events=0x%x",
424 events);
425 return 0; // remove the callback
426 }
427
428 if (!(events & Looper::EVENT_INPUT)) {
429 ALOGW("Received spurious callback for unhandled poll event. events=0x%x", events);
430 return 1; // keep the callback
431 }
432
433 constexpr int kBufferSize = 100;
434 DisplayEventReceiver::Event buffer[kBufferSize];
435 ssize_t numEvents;
436 do {
437 numEvents = mBootAnimation->mDisplayEventReceiver->getEvents(buffer, kBufferSize);
438 for (size_t i = 0; i < static_cast<size_t>(numEvents); i++) {
439 const auto& event = buffer[i];
440 if (event.header.type == DisplayEventReceiver::DISPLAY_EVENT_HOTPLUG) {
441 SLOGV("Hotplug received");
442
443 if (!event.hotplug.connected) {
444 // ignore hotplug disconnect
445 continue;
446 }
447 auto token = SurfaceComposerClient::getPhysicalDisplayToken(
448 event.header.displayId);
449
450 if (token != mBootAnimation->mDisplayToken) {
451 // ignore hotplug of a secondary display
452 continue;
453 }
454
Marin Shalamanovcb783ca2021-01-29 21:26:07 +0100455 DisplayMode displayMode;
456 const status_t error = SurfaceComposerClient::getActiveDisplayMode(
457 mBootAnimation->mDisplayToken, &displayMode);
Marin Shalamanov9070c052020-05-18 19:05:17 +0200458 if (error != NO_ERROR) {
Marin Shalamanovcb783ca2021-01-29 21:26:07 +0100459 SLOGE("Can't get active display mode.");
Marin Shalamanov9070c052020-05-18 19:05:17 +0200460 }
Marin Shalamanovcb783ca2021-01-29 21:26:07 +0100461 mBootAnimation->resizeSurface(displayMode.resolution.getWidth(),
462 displayMode.resolution.getHeight());
Marin Shalamanov9070c052020-05-18 19:05:17 +0200463 }
464 }
465 } while (numEvents > 0);
466
467 return 1; // keep the callback
468 }
469};
470
471EGLConfig BootAnimation::getEglConfig(const EGLDisplay& display) {
472 const EGLint attribs[] = {
Andrii Chepurnyid93f7b22023-03-15 11:22:07 +0200473 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
Marin Shalamanov9070c052020-05-18 19:05:17 +0200474 EGL_RED_SIZE, 8,
475 EGL_GREEN_SIZE, 8,
476 EGL_BLUE_SIZE, 8,
477 EGL_DEPTH_SIZE, 0,
478 EGL_NONE
479 };
480 EGLint numConfigs;
481 EGLConfig config;
482 eglChooseConfig(display, attribs, &config, 1, &numConfigs);
483 return config;
484}
485
Marin Shalamanov047802d2020-05-19 23:55:12 +0200486ui::Size BootAnimation::limitSurfaceSize(int width, int height) const {
487 ui::Size limited(width, height);
488 bool wasLimited = false;
489 const float aspectRatio = float(width) / float(height);
490 if (mMaxWidth != 0 && width > mMaxWidth) {
491 limited.height = mMaxWidth / aspectRatio;
492 limited.width = mMaxWidth;
493 wasLimited = true;
494 }
495 if (mMaxHeight != 0 && limited.height > mMaxHeight) {
496 limited.height = mMaxHeight;
497 limited.width = mMaxHeight * aspectRatio;
498 wasLimited = true;
499 }
500 SLOGV_IF(wasLimited, "Surface size has been limited to [%dx%d] from [%dx%d]",
501 limited.width, limited.height, width, height);
502 return limited;
503}
504
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800505status_t BootAnimation::readyToRun() {
Parth Sane7f7b2212023-04-12 17:23:02 +0000506 ATRACE_CALL();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800507 mAssets.addDefaultAssets();
508
Huihong Luo44305102022-09-26 11:16:20 -0700509 const std::vector<PhysicalDisplayId> ids = SurfaceComposerClient::getPhysicalDisplayIds();
510 if (ids.empty()) {
511 SLOGE("Failed to get ID for any displays\n");
Dominik Laskowski69b281d2019-11-22 14:13:12 -0800512 return NAME_NOT_FOUND;
Huihong Luo44305102022-09-26 11:16:20 -0700513 }
Dominik Laskowski3316a0a2019-01-25 02:56:41 -0800514
Huihong Luo44305102022-09-26 11:16:20 -0700515 // this system property specifies multi-display IDs to show the boot animation
Huihong Luof97c9e22019-05-29 11:08:01 -0700516 // multiple ids can be set with comma (,) as separator, for example:
Huihong Luod4a217e2020-01-16 15:56:33 -0800517 // setprop persist.boot.animation.displays 19260422155234049,19261083906282754
Marin Shalamanov11599ee2020-07-27 21:31:48 +0200518 Vector<PhysicalDisplayId> physicalDisplayIds;
Huihong Luof97c9e22019-05-29 11:08:01 -0700519 char displayValue[PROPERTY_VALUE_MAX] = "";
Huihong Luod4a217e2020-01-16 15:56:33 -0800520 property_get(DISPLAYS_PROP_NAME, displayValue, "");
Huihong Luof97c9e22019-05-29 11:08:01 -0700521 bool isValid = displayValue[0] != '\0';
522 if (isValid) {
523 char *p = displayValue;
524 while (*p) {
525 if (!isdigit(*p) && *p != ',') {
526 isValid = false;
527 break;
528 }
529 p ++;
530 }
531 if (!isValid)
Huihong Luod4a217e2020-01-16 15:56:33 -0800532 SLOGE("Invalid syntax for the value of system prop: %s", DISPLAYS_PROP_NAME);
Huihong Luof97c9e22019-05-29 11:08:01 -0700533 }
534 if (isValid) {
535 std::istringstream stream(displayValue);
Marin Shalamanov11599ee2020-07-27 21:31:48 +0200536 for (PhysicalDisplayId id; stream >> id.value; ) {
Huihong Luof97c9e22019-05-29 11:08:01 -0700537 physicalDisplayIds.add(id);
538 if (stream.peek() == ',')
539 stream.ignore();
540 }
541
Huihong Luo44305102022-09-26 11:16:20 -0700542 // the first specified display id is used to retrieve mDisplayToken
543 for (const auto id : physicalDisplayIds) {
544 if (std::find(ids.begin(), ids.end(), id) != ids.end()) {
545 if (const auto token = SurfaceComposerClient::getPhysicalDisplayToken(id)) {
546 mDisplayToken = token;
547 break;
548 }
549 }
550 }
551 }
552
553 // If the system property is not present or invalid, display 0 is used
554 if (mDisplayToken == nullptr) {
555 mDisplayToken = SurfaceComposerClient::getPhysicalDisplayToken(ids.front());
556 if (mDisplayToken == nullptr) {
557 return NAME_NOT_FOUND;
558 }
559 }
560
561 DisplayMode displayMode;
562 const status_t error =
563 SurfaceComposerClient::getActiveDisplayMode(mDisplayToken, &displayMode);
564 if (error != NO_ERROR) {
565 return error;
566 }
567
568 mMaxWidth = android::base::GetIntProperty("ro.surface_flinger.max_graphics_width", 0);
569 mMaxHeight = android::base::GetIntProperty("ro.surface_flinger.max_graphics_height", 0);
570 ui::Size resolution = displayMode.resolution;
571 resolution = limitSurfaceSize(resolution.width, resolution.height);
572 // create the native surface
573 sp<SurfaceControl> control = session()->createSurface(String8("BootAnimation"),
574 resolution.getWidth(), resolution.getHeight(), PIXEL_FORMAT_RGB_565,
575 ISurfaceComposerClient::eOpaque);
576
577 SurfaceComposerClient::Transaction t;
578 if (isValid) {
Huihong Luof97c9e22019-05-29 11:08:01 -0700579 // In the case of multi-display, boot animation shows on the specified displays
Dominik Laskowski989de3d2021-07-27 21:09:34 -0700580 for (const auto id : physicalDisplayIds) {
Huihong Luof97c9e22019-05-29 11:08:01 -0700581 if (std::find(ids.begin(), ids.end(), id) != ids.end()) {
Dominik Laskowski989de3d2021-07-27 21:09:34 -0700582 if (const auto token = SurfaceComposerClient::getPhysicalDisplayToken(id)) {
583 t.setDisplayLayerStack(token, ui::DEFAULT_LAYER_STACK);
584 }
Huihong Luof97c9e22019-05-29 11:08:01 -0700585 }
586 }
Dominik Laskowski989de3d2021-07-27 21:09:34 -0700587 t.setLayerStack(control, ui::DEFAULT_LAYER_STACK);
Huihong Luof97c9e22019-05-29 11:08:01 -0700588 }
589
Robert Carre13b58e2017-08-31 14:50:44 -0700590 t.setLayer(control, 0x40000000)
591 .apply();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800592
Mathias Agopian17f638b2009-04-16 20:04:08 -0700593 sp<Surface> s = control->getSurface();
594
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800595 // initialize opengl and egl
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800596 EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
Yi Kong911ac232019-03-24 01:49:02 -0700597 eglInitialize(display, nullptr, nullptr);
Marin Shalamanov9070c052020-05-18 19:05:17 +0200598 EGLConfig config = getEglConfig(display);
599 EGLSurface surface = eglCreateWindowSurface(display, config, s.get(), nullptr);
Lucas Dupin8eedc022021-07-29 21:49:07 +0000600 // Initialize egl context with client version number 2.0.
601 EGLint contextAttributes[] = {EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE};
602 EGLContext context = eglCreateContext(display, config, nullptr, contextAttributes);
Marin Shalamanov9070c052020-05-18 19:05:17 +0200603 EGLint w, h;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800604 eglQuerySurface(display, surface, EGL_WIDTH, &w);
605 eglQuerySurface(display, surface, EGL_HEIGHT, &h);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700606
Huihong Luo44305102022-09-26 11:16:20 -0700607 if (eglMakeCurrent(display, surface, surface, context) == EGL_FALSE) {
Mathias Agopianabac0102009-07-31 14:47:00 -0700608 return NO_INIT;
Huihong Luo44305102022-09-26 11:16:20 -0700609 }
Mathias Agopiana8826d62009-10-01 03:10:14 -0700610
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800611 mDisplay = display;
612 mContext = context;
613 mSurface = surface;
joenchen764ea042022-04-11 23:58:34 +0800614 mInitWidth = mWidth = w;
615 mInitHeight = mHeight = h;
Mathias Agopian17f638b2009-04-16 20:04:08 -0700616 mFlingerSurfaceControl = control;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800617 mFlingerSurface = s;
Adrian Roos9ee5dff2018-08-22 20:19:49 +0200618 mTargetInset = -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800619
Mariia Sandrikovaa166f012022-12-10 02:32:14 +0000620 // Rotate the boot animation according to the value specified in the sysprop
621 // ro.bootanim.set_orientation_<display_id>. Four values are supported: ORIENTATION_0,
622 // ORIENTATION_90, ORIENTATION_180 and ORIENTATION_270.
623 // If the value isn't specified or is ORIENTATION_0, nothing will be changed.
624 // This is needed to support having boot animation in orientations different from the natural
625 // device orientation. For example, on tablets that may want to keep natural orientation
626 // portrait for applications compatibility and to have the boot animation in landscape.
627 rotateAwayFromNaturalOrientationIfNeeded();
628
Yegor Malyshev6925e1b2020-09-10 16:42:20 +0200629 projectSceneToWindow();
630
Marin Shalamanov9070c052020-05-18 19:05:17 +0200631 // Register a display event receiver
632 mDisplayEventReceiver = std::make_unique<DisplayEventReceiver>();
633 status_t status = mDisplayEventReceiver->initCheck();
634 SLOGE_IF(status != NO_ERROR, "Initialization of DisplayEventReceiver failed with status: %d",
635 status);
636 mLooper->addFd(mDisplayEventReceiver->getFd(), 0, Looper::EVENT_INPUT,
637 new DisplayEventCallback(this), nullptr);
638
Huihong Luo50a2f362018-08-11 09:27:57 -0700639 return NO_ERROR;
640}
641
Mariia Sandrikovaa166f012022-12-10 02:32:14 +0000642void BootAnimation::rotateAwayFromNaturalOrientationIfNeeded() {
Parth Sane7f7b2212023-04-12 17:23:02 +0000643 ATRACE_CALL();
Mariia Sandrikovaa166f012022-12-10 02:32:14 +0000644 const auto orientation = parseOrientationProperty();
645
646 if (orientation == ui::ROTATION_0) {
647 // Do nothing if the sysprop isn't set or is set to ROTATION_0.
648 return;
649 }
650
651 if (orientation == ui::ROTATION_90 || orientation == ui::ROTATION_270) {
652 std::swap(mWidth, mHeight);
653 std::swap(mInitWidth, mInitHeight);
654 mFlingerSurfaceControl->updateDefaultBufferSize(mWidth, mHeight);
655 }
656
657 Rect displayRect(0, 0, mWidth, mHeight);
658 Rect layerStackRect(0, 0, mWidth, mHeight);
659
660 SurfaceComposerClient::Transaction t;
661 t.setDisplayProjection(mDisplayToken, orientation, layerStackRect, displayRect);
662 t.apply();
663}
664
665ui::Rotation BootAnimation::parseOrientationProperty() {
Parth Sane7f7b2212023-04-12 17:23:02 +0000666 ATRACE_CALL();
Mariia Sandrikovaa166f012022-12-10 02:32:14 +0000667 const auto displayIds = SurfaceComposerClient::getPhysicalDisplayIds();
668 if (displayIds.size() == 0) {
669 return ui::ROTATION_0;
670 }
671 const auto displayId = displayIds[0];
672 const auto syspropName = [displayId] {
673 std::stringstream ss;
674 ss << "ro.bootanim.set_orientation_" << displayId.value;
675 return ss.str();
676 }();
677 const auto syspropValue = android::base::GetProperty(syspropName, "ORIENTATION_0");
678 if (syspropValue == "ORIENTATION_90") {
679 return ui::ROTATION_90;
680 } else if (syspropValue == "ORIENTATION_180") {
681 return ui::ROTATION_180;
682 } else if (syspropValue == "ORIENTATION_270") {
683 return ui::ROTATION_270;
684 }
685 return ui::ROTATION_0;
686}
687
Yegor Malyshev6925e1b2020-09-10 16:42:20 +0200688void BootAnimation::projectSceneToWindow() {
Parth Sane7f7b2212023-04-12 17:23:02 +0000689 ATRACE_CALL();
Yegor Malyshev6925e1b2020-09-10 16:42:20 +0200690 glViewport(0, 0, mWidth, mHeight);
691 glScissor(0, 0, mWidth, mHeight);
Yegor Malyshev6925e1b2020-09-10 16:42:20 +0200692}
693
Marin Shalamanov9070c052020-05-18 19:05:17 +0200694void BootAnimation::resizeSurface(int newWidth, int newHeight) {
Parth Sane7f7b2212023-04-12 17:23:02 +0000695 ATRACE_CALL();
Marin Shalamanov9070c052020-05-18 19:05:17 +0200696 // We assume this function is called on the animation thread.
697 if (newWidth == mWidth && newHeight == mHeight) {
698 return;
699 }
700 SLOGV("Resizing the boot animation surface to %d %d", newWidth, newHeight);
701
702 eglMakeCurrent(mDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
703 eglDestroySurface(mDisplay, mSurface);
704
joenchen764ea042022-04-11 23:58:34 +0800705 mFlingerSurfaceControl->updateDefaultBufferSize(newWidth, newHeight);
Marin Shalamanov047802d2020-05-19 23:55:12 +0200706 const auto limitedSize = limitSurfaceSize(newWidth, newHeight);
707 mWidth = limitedSize.width;
708 mHeight = limitedSize.height;
Marin Shalamanov9070c052020-05-18 19:05:17 +0200709
Marin Shalamanov9070c052020-05-18 19:05:17 +0200710 EGLConfig config = getEglConfig(mDisplay);
711 EGLSurface surface = eglCreateWindowSurface(mDisplay, config, mFlingerSurface.get(), nullptr);
712 if (eglMakeCurrent(mDisplay, surface, surface, mContext) == EGL_FALSE) {
713 SLOGE("Can't make the new surface current. Error %d", eglGetError());
714 return;
715 }
Yegor Malyshev6925e1b2020-09-10 16:42:20 +0200716
717 projectSceneToWindow();
Marin Shalamanov9070c052020-05-18 19:05:17 +0200718
719 mSurface = surface;
720}
721
Huihong Luo50a2f362018-08-11 09:27:57 -0700722bool BootAnimation::preloadAnimation() {
Parth Sane7f7b2212023-04-12 17:23:02 +0000723 ATRACE_CALL();
Huihong Luo50a2f362018-08-11 09:27:57 -0700724 findBootAnimationFile();
725 if (!mZipFileName.isEmpty()) {
726 mAnimation = loadAnimation(mZipFileName);
727 return (mAnimation != nullptr);
728 }
729
730 return false;
731}
732
Nikita Ioffe06c986e2020-01-27 17:16:03 +0000733bool BootAnimation::findBootAnimationFileInternal(const std::vector<std::string> &files) {
Parth Sane7f7b2212023-04-12 17:23:02 +0000734 ATRACE_CALL();
Nikita Ioffe06c986e2020-01-27 17:16:03 +0000735 for (const std::string& f : files) {
736 if (access(f.c_str(), R_OK) == 0) {
737 mZipFileName = f.c_str();
738 return true;
739 }
740 }
741 return false;
742}
743
Huihong Luo50a2f362018-08-11 09:27:57 -0700744void BootAnimation::findBootAnimationFile() {
Parth Sane7f7b2212023-04-12 17:23:02 +0000745 ATRACE_CALL();
Beverly34ffe252019-05-17 11:03:54 -0400746 const bool playDarkAnim = android::base::GetIntProperty("ro.boot.theme", 0) == 1;
Nikita Ioffe06c986e2020-01-27 17:16:03 +0000747 static const std::vector<std::string> bootFiles = {
748 APEX_BOOTANIMATION_FILE, playDarkAnim ? PRODUCT_BOOTANIMATION_DARK_FILE : PRODUCT_BOOTANIMATION_FILE,
749 OEM_BOOTANIMATION_FILE, SYSTEM_BOOTANIMATION_FILE
750 };
751 static const std::vector<std::string> shutdownFiles = {
752 PRODUCT_SHUTDOWNANIMATION_FILE, OEM_SHUTDOWNANIMATION_FILE, SYSTEM_SHUTDOWNANIMATION_FILE, ""
753 };
754 static const std::vector<std::string> userspaceRebootFiles = {
755 PRODUCT_USERSPACE_REBOOT_ANIMATION_FILE, OEM_USERSPACE_REBOOT_ANIMATION_FILE,
756 SYSTEM_USERSPACE_REBOOT_ANIMATION_FILE,
757 };
Keun-young Parkb5938422017-03-23 13:46:24 -0700758
Nikita Ioffe06c986e2020-01-27 17:16:03 +0000759 if (android::base::GetBoolProperty("sys.init.userspace_reboot.in_progress", false)) {
760 findBootAnimationFileInternal(userspaceRebootFiles);
761 } else if (mShuttingDown) {
762 findBootAnimationFileInternal(shutdownFiles);
763 } else {
764 findBootAnimationFileInternal(bootFiles);
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700765 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800766}
767
Lucas Dupin8eedc022021-07-29 21:49:07 +0000768GLuint compileShader(GLenum shaderType, const GLchar *source) {
Parth Sane7f7b2212023-04-12 17:23:02 +0000769 ATRACE_CALL();
Lucas Dupin8eedc022021-07-29 21:49:07 +0000770 GLuint shader = glCreateShader(shaderType);
771 glShaderSource(shader, 1, &source, 0);
772 glCompileShader(shader);
773 GLint isCompiled = 0;
774 glGetShaderiv(shader, GL_COMPILE_STATUS, &isCompiled);
775 if (isCompiled == GL_FALSE) {
776 SLOGE("Compile shader failed. Shader type: %d", shaderType);
Shan Huang81846ba2021-09-07 19:07:47 +0000777 GLint maxLength = 0;
778 glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &maxLength);
779 std::vector<GLchar> errorLog(maxLength);
780 glGetShaderInfoLog(shader, maxLength, &maxLength, &errorLog[0]);
781 SLOGE("Shader compilation error: %s", &errorLog[0]);
Lucas Dupin8eedc022021-07-29 21:49:07 +0000782 return 0;
783 }
784 return shader;
785}
786
787GLuint linkShader(GLuint vertexShader, GLuint fragmentShader) {
Parth Sane7f7b2212023-04-12 17:23:02 +0000788 ATRACE_CALL();
Lucas Dupin8eedc022021-07-29 21:49:07 +0000789 GLuint program = glCreateProgram();
790 glAttachShader(program, vertexShader);
791 glAttachShader(program, fragmentShader);
792 glLinkProgram(program);
793 GLint isLinked = 0;
794 glGetProgramiv(program, GL_LINK_STATUS, (int *)&isLinked);
795 if (isLinked == GL_FALSE) {
796 SLOGE("Linking shader failed. Shader handles: vert %d, frag %d",
797 vertexShader, fragmentShader);
798 return 0;
799 }
800 return program;
801}
802
803void BootAnimation::initShaders() {
Parth Sane7f7b2212023-04-12 17:23:02 +0000804 ATRACE_CALL();
Shan Huang2b31a362021-08-16 16:45:39 +0000805 bool dynamicColoringEnabled = mAnimation != nullptr && mAnimation->dynamicColoringEnabled;
Lucas Dupin8eedc022021-07-29 21:49:07 +0000806 GLuint vertexShader = compileShader(GL_VERTEX_SHADER, (const GLchar *)VERTEX_SHADER_SOURCE);
807 GLuint imageFragmentShader =
Shan Huang2b31a362021-08-16 16:45:39 +0000808 compileShader(GL_FRAGMENT_SHADER, dynamicColoringEnabled
809 ? (const GLchar *)IMAGE_FRAG_DYNAMIC_COLORING_SHADER_SOURCE
810 : (const GLchar *)IMAGE_FRAG_SHADER_SOURCE);
Lucas Dupin8eedc022021-07-29 21:49:07 +0000811 GLuint textFragmentShader =
812 compileShader(GL_FRAGMENT_SHADER, (const GLchar *)TEXT_FRAG_SHADER_SOURCE);
813
814 // Initialize image shader.
815 mImageShader = linkShader(vertexShader, imageFragmentShader);
816 GLint positionLocation = glGetAttribLocation(mImageShader, A_POSITION);
817 GLint uvLocation = glGetAttribLocation(mImageShader, A_UV);
818 mImageTextureLocation = glGetUniformLocation(mImageShader, U_TEXTURE);
819 mImageFadeLocation = glGetUniformLocation(mImageShader, U_FADE);
820 glEnableVertexAttribArray(positionLocation);
821 glVertexAttribPointer(positionLocation, 2, GL_FLOAT, GL_FALSE, 0, quadPositions);
822 glVertexAttribPointer(uvLocation, 2, GL_FLOAT, GL_FALSE, 0, quadUVs);
823 glEnableVertexAttribArray(uvLocation);
824
825 // Initialize text shader.
826 mTextShader = linkShader(vertexShader, textFragmentShader);
827 positionLocation = glGetAttribLocation(mTextShader, A_POSITION);
828 uvLocation = glGetAttribLocation(mTextShader, A_UV);
829 mTextTextureLocation = glGetUniformLocation(mTextShader, U_TEXTURE);
830 mTextCropAreaLocation = glGetUniformLocation(mTextShader, U_CROP_AREA);
831 glEnableVertexAttribArray(positionLocation);
832 glVertexAttribPointer(positionLocation, 2, GL_FLOAT, GL_FALSE, 0, quadPositions);
833 glVertexAttribPointer(uvLocation, 2, GL_FLOAT, GL_FALSE, 0, quadUVs);
834 glEnableVertexAttribArray(uvLocation);
835}
836
Marin Shalamanov9070c052020-05-18 19:05:17 +0200837bool BootAnimation::threadLoop() {
Parth Sane7f7b2212023-04-12 17:23:02 +0000838 ATRACE_CALL();
Marin Shalamanov9070c052020-05-18 19:05:17 +0200839 bool result;
Lucas Dupin8eedc022021-07-29 21:49:07 +0000840 initShaders();
841
Narayan Kamathafd31e02013-12-03 13:16:03 +0000842 // We have no bootanimation file, so we use the stock android logo
843 // animation.
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700844 if (mZipFileName.isEmpty()) {
Yoshiaki Maruoka9ef19b82017-10-25 13:55:49 +0900845 ALOGD("No animation file");
Marin Shalamanov9070c052020-05-18 19:05:17 +0200846 result = android();
Mathias Agopiana8826d62009-10-01 03:10:14 -0700847 } else {
Marin Shalamanov9070c052020-05-18 19:05:17 +0200848 result = movie();
Mathias Agopiana8826d62009-10-01 03:10:14 -0700849 }
850
Philip Junker60600562020-11-25 18:16:16 +0100851 mCallbacks->shutdown();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800852 eglMakeCurrent(mDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
853 eglDestroyContext(mDisplay, mContext);
854 eglDestroySurface(mDisplay, mSurface);
Mathias Agopian6cf0db22009-04-17 19:36:26 -0700855 mFlingerSurface.clear();
Mathias Agopian17f638b2009-04-16 20:04:08 -0700856 mFlingerSurfaceControl.clear();
Mathias Agopian627e7b52009-05-21 19:21:59 -0700857 eglTerminate(mDisplay);
Sai Kiran Korwar3eee9fb2015-06-16 17:13:35 +0530858 eglReleaseThread();
Mathias Agopian627e7b52009-05-21 19:21:59 -0700859 IPCThreadState::self()->stopProcess();
Marin Shalamanov9070c052020-05-18 19:05:17 +0200860 return result;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800861}
862
Marin Shalamanov9070c052020-05-18 19:05:17 +0200863bool BootAnimation::android() {
Parth Sane7f7b2212023-04-12 17:23:02 +0000864 ATRACE_CALL();
Lucas Dupin8eedc022021-07-29 21:49:07 +0000865 glActiveTexture(GL_TEXTURE0);
866
Wei Wang159a4102018-10-23 23:15:58 -0700867 SLOGD("%sAnimationShownTiming start time: %" PRId64 "ms", mShuttingDown ? "Shutdown" : "Boot",
Keun-young Parkd1794cd2017-04-04 12:21:19 -0700868 elapsedRealtime());
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700869 initTexture(&mAndroid[0], mAssets, "images/android-logo-mask.png");
870 initTexture(&mAndroid[1], mAssets, "images/android-logo-shine.png");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800871
Ed Coyne7464ac92017-06-08 12:26:48 -0700872 mCallbacks->init({});
873
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800874 // clear screen
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700875 glDisable(GL_DITHER);
876 glDisable(GL_SCISSOR_TEST);
Shan Huangbfd2d3c2021-09-09 23:19:04 +0000877 glUseProgram(mImageShader);
878
Mathias Agopian59f19e42011-05-06 19:22:12 -0700879 glClearColor(0,0,0,1);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800880 glClear(GL_COLOR_BUFFER_BIT);
881 eglSwapBuffers(mDisplay, mSurface);
882
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700883 // Blend state
884 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700885
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800886 const nsecs_t startTime = systemTime();
887 do {
Marin Shalamanov9070c052020-05-18 19:05:17 +0200888 processDisplayEvents();
889 const GLint xc = (mWidth - mAndroid[0].w) / 2;
890 const GLint yc = (mHeight - mAndroid[0].h) / 2;
891 const Rect updateRect(xc, yc, xc + mAndroid[0].w, yc + mAndroid[0].h);
892 glScissor(updateRect.left, mHeight - updateRect.bottom, updateRect.width(),
893 updateRect.height());
894
Mathias Agopian13796652009-03-24 22:49:21 -0700895 nsecs_t now = systemTime();
896 double time = now - startTime;
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700897 float t = 4.0f * float(time / us2ns(16667)) / mAndroid[1].w;
898 GLint offset = (1 - (t - floorf(t))) * mAndroid[1].w;
899 GLint x = xc - offset;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800900
Mathias Agopian81668642009-07-28 11:41:30 -0700901 glDisable(GL_SCISSOR_TEST);
902 glClear(GL_COLOR_BUFFER_BIT);
903
904 glEnable(GL_SCISSOR_TEST);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800905 glDisable(GL_BLEND);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800906 glBindTexture(GL_TEXTURE_2D, mAndroid[1].name);
Lucas Dupin8eedc022021-07-29 21:49:07 +0000907 drawTexturedQuad(x, yc, mAndroid[1].w, mAndroid[1].h);
908 drawTexturedQuad(x + mAndroid[1].w, yc, mAndroid[1].w, mAndroid[1].h);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800909
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700910 glEnable(GL_BLEND);
911 glBindTexture(GL_TEXTURE_2D, mAndroid[0].name);
Lucas Dupin8eedc022021-07-29 21:49:07 +0000912 drawTexturedQuad(xc, yc, mAndroid[0].w, mAndroid[0].h);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800913
Mathias Agopian627e7b52009-05-21 19:21:59 -0700914 EGLBoolean res = eglSwapBuffers(mDisplay, mSurface);
915 if (res == EGL_FALSE)
916 break;
917
Mathias Agopian13796652009-03-24 22:49:21 -0700918 // 12fps: don't animate too fast to preserve CPU
919 const nsecs_t sleepTime = 83333 - ns2us(systemTime() - now);
920 if (sleepTime > 0)
Mathias Agopiana8826d62009-10-01 03:10:14 -0700921 usleep(sleepTime);
Kevin Hesterd3782b22012-04-26 10:38:55 -0700922
923 checkExit();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800924 } while (!exitPending());
925
926 glDeleteTextures(1, &mAndroid[0].name);
927 glDeleteTextures(1, &mAndroid[1].name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800928 return false;
929}
930
Kevin Hesterd3782b22012-04-26 10:38:55 -0700931void BootAnimation::checkExit() {
Parth Sane7f7b2212023-04-12 17:23:02 +0000932 ATRACE_CALL();
Kevin Hesterd3782b22012-04-26 10:38:55 -0700933 // Allow surface flinger to gracefully request shutdown
934 char value[PROPERTY_VALUE_MAX];
935 property_get(EXIT_PROP_NAME, value, "0");
936 int exitnow = atoi(value);
937 if (exitnow) {
938 requestExit();
939 }
940}
941
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700942bool BootAnimation::validClock(const Animation::Part& part) {
Parth Sane7f7b2212023-04-12 17:23:02 +0000943 ATRACE_CALL();
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700944 return part.clockPosX != TEXT_MISSING_VALUE && part.clockPosY != TEXT_MISSING_VALUE;
945}
946
947bool parseTextCoord(const char* str, int* dest) {
Parth Sane7f7b2212023-04-12 17:23:02 +0000948 ATRACE_CALL();
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700949 if (strcmp("c", str) == 0) {
950 *dest = TEXT_CENTER_VALUE;
951 return true;
952 }
953
954 char* end;
955 int val = (int) strtol(str, &end, 0);
956 if (end == str || *end != '\0' || val == INT_MAX || val == INT_MIN) {
957 return false;
958 }
959 *dest = val;
960 return true;
961}
962
963// Parse two position coordinates. If only string is non-empty, treat it as the y value.
964void parsePosition(const char* str1, const char* str2, int* x, int* y) {
Parth Sane7f7b2212023-04-12 17:23:02 +0000965 ATRACE_CALL();
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700966 bool success = false;
967 if (strlen(str1) == 0) { // No values were specified
968 // success = false
969 } else if (strlen(str2) == 0) { // we have only one value
970 if (parseTextCoord(str1, y)) {
971 *x = TEXT_CENTER_VALUE;
972 success = true;
973 }
974 } else {
975 if (parseTextCoord(str1, x) && parseTextCoord(str2, y)) {
976 success = true;
977 }
978 }
979
980 if (!success) {
981 *x = TEXT_MISSING_VALUE;
982 *y = TEXT_MISSING_VALUE;
983 }
984}
985
Jesse Hall083b84c2014-09-22 10:51:09 -0700986// Parse a color represented as an HTML-style 'RRGGBB' string: each pair of
987// characters in str is a hex number in [0, 255], which are converted to
988// floating point values in the range [0.0, 1.0] and placed in the
989// corresponding elements of color.
990//
991// If the input string isn't valid, parseColor returns false and color is
992// left unchanged.
993static bool parseColor(const char str[7], float color[3]) {
Parth Sane7f7b2212023-04-12 17:23:02 +0000994 ATRACE_CALL();
Jesse Hall083b84c2014-09-22 10:51:09 -0700995 float tmpColor[3];
996 for (int i = 0; i < 3; i++) {
997 int val = 0;
998 for (int j = 0; j < 2; j++) {
999 val *= 16;
1000 char c = str[2*i + j];
1001 if (c >= '0' && c <= '9') val += c - '0';
1002 else if (c >= 'A' && c <= 'F') val += (c - 'A') + 10;
1003 else if (c >= 'a' && c <= 'f') val += (c - 'a') + 10;
1004 else return false;
1005 }
1006 tmpColor[i] = static_cast<float>(val) / 255.0f;
1007 }
1008 memcpy(color, tmpColor, sizeof(tmpColor));
1009 return true;
1010}
1011
Shan Huang2b31a362021-08-16 16:45:39 +00001012// Parse a color represented as a signed decimal int string.
1013// E.g. "-2757722" (whose hex 2's complement is 0xFFD5EBA6).
1014// If the input color string is empty, set color with values in defaultColor.
1015static void parseColorDecimalString(const std::string& colorString,
1016 float color[3], float defaultColor[3]) {
Parth Sane7f7b2212023-04-12 17:23:02 +00001017 ATRACE_CALL();
Shan Huang2b31a362021-08-16 16:45:39 +00001018 if (colorString == "") {
1019 memcpy(color, defaultColor, sizeof(float) * 3);
1020 return;
1021 }
1022 int colorInt = atoi(colorString.c_str());
1023 color[0] = ((float)((colorInt >> 16) & 0xFF)) / 0xFF; // r
1024 color[1] = ((float)((colorInt >> 8) & 0xFF)) / 0xFF; // g
1025 color[2] = ((float)(colorInt & 0xFF)) / 0xFF; // b
1026}
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001027
Marin Shalamanov9070c052020-05-18 19:05:17 +02001028static bool readFile(ZipFileRO* zip, const char* name, String8& outString) {
Parth Sane7f7b2212023-04-12 17:23:02 +00001029 ATRACE_CALL();
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001030 ZipEntryRO entry = zip->findEntryByName(name);
Wei Wang159a4102018-10-23 23:15:58 -07001031 SLOGE_IF(!entry, "couldn't find %s", name);
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -07001032 if (!entry) {
1033 return false;
1034 }
1035
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001036 FileMap* entryMap = zip->createEntryFileMap(entry);
1037 zip->releaseEntry(entry);
Wei Wang159a4102018-10-23 23:15:58 -07001038 SLOGE_IF(!entryMap, "entryMap is null");
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -07001039 if (!entryMap) {
1040 return false;
1041 }
1042
1043 outString.setTo((char const*)entryMap->getDataPtr(), entryMap->getDataLength());
Narayan Kamath688ff4c2015-02-23 15:47:54 +00001044 delete entryMap;
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -07001045 return true;
1046}
1047
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -07001048// The font image should be a 96x2 array of character images. The
1049// columns are the printable ASCII characters 0x20 - 0x7f. The
1050// top row is regular text; the bottom row is bold.
1051status_t BootAnimation::initFont(Font* font, const char* fallback) {
Parth Sane7f7b2212023-04-12 17:23:02 +00001052 ATRACE_CALL();
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -07001053 status_t status = NO_ERROR;
Damien Bargiacchia704b7d2016-02-16 16:55:49 -08001054
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -07001055 if (font->map != nullptr) {
1056 glGenTextures(1, &font->texture.name);
1057 glBindTexture(GL_TEXTURE_2D, font->texture.name);
Damien Bargiacchia704b7d2016-02-16 16:55:49 -08001058
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -07001059 status = initTexture(font->map, &font->texture.w, &font->texture.h);
1060
Lucas Dupin8eedc022021-07-29 21:49:07 +00001061 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
1062 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
1063 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
1064 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -07001065 } else if (fallback != nullptr) {
1066 status = initTexture(&font->texture, mAssets, fallback);
1067 } else {
1068 return NO_INIT;
Damien Bargiacchia704b7d2016-02-16 16:55:49 -08001069 }
1070
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -07001071 if (status == NO_ERROR) {
1072 font->char_width = font->texture.w / FONT_NUM_COLS;
1073 font->char_height = font->texture.h / FONT_NUM_ROWS / 2; // There are bold and regular rows
1074 }
1075
1076 return status;
1077}
1078
1079void BootAnimation::drawText(const char* str, const Font& font, bool bold, int* x, int* y) {
Parth Sane7f7b2212023-04-12 17:23:02 +00001080 ATRACE_CALL();
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -07001081 glEnable(GL_BLEND); // Allow us to draw on top of the animation
1082 glBindTexture(GL_TEXTURE_2D, font.texture.name);
Lucas Dupin8eedc022021-07-29 21:49:07 +00001083 glUseProgram(mTextShader);
1084 glUniform1i(mTextTextureLocation, 0);
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -07001085
1086 const int len = strlen(str);
1087 const int strWidth = font.char_width * len;
1088
1089 if (*x == TEXT_CENTER_VALUE) {
1090 *x = (mWidth - strWidth) / 2;
1091 } else if (*x < 0) {
1092 *x = mWidth + *x - strWidth;
1093 }
1094 if (*y == TEXT_CENTER_VALUE) {
1095 *y = (mHeight - font.char_height) / 2;
1096 } else if (*y < 0) {
1097 *y = mHeight + *y - font.char_height;
1098 }
1099
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -07001100 for (int i = 0; i < len; i++) {
1101 char c = str[i];
1102
1103 if (c < FONT_BEGIN_CHAR || c > FONT_END_CHAR) {
1104 c = '?';
1105 }
1106
1107 // Crop the texture to only the pixels in the current glyph
1108 const int charPos = (c - FONT_BEGIN_CHAR); // Position in the list of valid characters
1109 const int row = charPos / FONT_NUM_COLS;
1110 const int col = charPos % FONT_NUM_COLS;
Lucas Dupin8eedc022021-07-29 21:49:07 +00001111 // Bold fonts are expected in the second half of each row.
1112 float v0 = (row + (bold ? 0.5f : 0.0f)) / FONT_NUM_ROWS;
1113 float u0 = ((float)col) / FONT_NUM_COLS;
1114 float v1 = v0 + 1.0f / FONT_NUM_ROWS / 2;
1115 float u1 = u0 + 1.0f / FONT_NUM_COLS;
1116 glUniform4f(mTextCropAreaLocation, u0, v0, u1, v1);
1117 drawTexturedQuad(*x, *y, font.char_width, font.char_height);
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -07001118
1119 *x += font.char_width;
1120 }
1121
1122 glDisable(GL_BLEND); // Return to the animation's default behaviour
1123 glBindTexture(GL_TEXTURE_2D, 0);
1124}
1125
Damien Bargiacchi9071db12016-10-28 17:38:22 -07001126// We render 12 or 24 hour time.
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -07001127void BootAnimation::drawClock(const Font& font, const int xPos, const int yPos) {
Parth Sane7f7b2212023-04-12 17:23:02 +00001128 ATRACE_CALL();
Damien Bargiacchi9071db12016-10-28 17:38:22 -07001129 static constexpr char TIME_FORMAT_12[] = "%l:%M";
1130 static constexpr char TIME_FORMAT_24[] = "%H:%M";
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -07001131 static constexpr int TIME_LENGTH = 6;
1132
Damien Bargiacchia704b7d2016-02-16 16:55:49 -08001133 time_t rawtime;
1134 time(&rawtime);
1135 struct tm* timeInfo = localtime(&rawtime);
1136
1137 char timeBuff[TIME_LENGTH];
Damien Bargiacchi9071db12016-10-28 17:38:22 -07001138 const char* timeFormat = mTimeFormat12Hour ? TIME_FORMAT_12 : TIME_FORMAT_24;
1139 size_t length = strftime(timeBuff, TIME_LENGTH, timeFormat, timeInfo);
Damien Bargiacchia704b7d2016-02-16 16:55:49 -08001140
1141 if (length != TIME_LENGTH - 1) {
Wei Wang159a4102018-10-23 23:15:58 -07001142 SLOGE("Couldn't format time; abandoning boot animation clock");
Damien Bargiacchia704b7d2016-02-16 16:55:49 -08001143 mClockEnabled = false;
1144 return;
1145 }
1146
Damien Bargiacchi45a76442016-12-05 18:02:18 -08001147 char* out = timeBuff[0] == ' ' ? &timeBuff[1] : &timeBuff[0];
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -07001148 int x = xPos;
1149 int y = yPos;
Damien Bargiacchi45a76442016-12-05 18:02:18 -08001150 drawText(out, font, false, &x, &y);
Damien Bargiacchia704b7d2016-02-16 16:55:49 -08001151}
1152
Nicolas Geoffray41fffb42020-12-15 10:50:29 +00001153void BootAnimation::drawProgress(int percent, const Font& font, const int xPos, const int yPos) {
Parth Sane7f7b2212023-04-12 17:23:02 +00001154 ATRACE_CALL();
Nicolas Geoffray41fffb42020-12-15 10:50:29 +00001155 static constexpr int PERCENT_LENGTH = 5;
1156
1157 char percentBuff[PERCENT_LENGTH];
1158 // ';' has the ascii code just after ':', and the font resource contains '%'
1159 // for that ascii code.
1160 sprintf(percentBuff, "%d;", percent);
1161 int x = xPos;
1162 int y = yPos;
1163 drawText(percentBuff, font, false, &x, &y);
1164}
1165
Marin Shalamanov9070c052020-05-18 19:05:17 +02001166bool BootAnimation::parseAnimationDesc(Animation& animation) {
Parth Sane7f7b2212023-04-12 17:23:02 +00001167 ATRACE_CALL();
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -07001168 String8 desString;
1169
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001170 if (!readFile(animation.zip, "desc.txt", desString)) {
Narayan Kamathafd31e02013-12-03 13:16:03 +00001171 return false;
1172 }
Mathias Agopiana8826d62009-10-01 03:10:14 -07001173 char const* s = desString.string();
Shan Huang2b31a362021-08-16 16:45:39 +00001174 std::string dynamicColoringPartName = "";
1175 bool postDynamicColoring = false;
Mathias Agopiana8826d62009-10-01 03:10:14 -07001176
Mathias Agopiana8826d62009-10-01 03:10:14 -07001177 // Parse the description file
1178 for (;;) {
1179 const char* endl = strstr(s, "\n");
Yi Kong911ac232019-03-24 01:49:02 -07001180 if (endl == nullptr) break;
Mathias Agopiana8826d62009-10-01 03:10:14 -07001181 String8 line(s, endl - s);
1182 const char* l = line.string();
Damien Bargiacchia704b7d2016-02-16 16:55:49 -08001183 int fps = 0;
1184 int width = 0;
1185 int height = 0;
1186 int count = 0;
1187 int pause = 0;
Nicolas Geoffray41fffb42020-12-15 10:50:29 +00001188 int progress = 0;
Yegor Malyshev6925e1b2020-09-10 16:42:20 +02001189 int framesToFadeCount = 0;
Shan Huang488ff742021-08-25 20:10:46 +00001190 int colorTransitionStart = 0;
1191 int colorTransitionEnd = 0;
Narayan Kamathafd31e02013-12-03 13:16:03 +00001192 char path[ANIM_ENTRY_NAME_MAX];
Jesse Hall083b84c2014-09-22 10:51:09 -07001193 char color[7] = "000000"; // default to black if unspecified
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -07001194 char clockPos1[TEXT_POS_LEN_MAX + 1] = "";
1195 char clockPos2[TEXT_POS_LEN_MAX + 1] = "";
Shan Huang2b31a362021-08-16 16:45:39 +00001196 char dynamicColoringPartNameBuffer[ANIM_ENTRY_NAME_MAX];
Kevin Hesterd3782b22012-04-26 10:38:55 -07001197 char pathType;
Shan Huang2b31a362021-08-16 16:45:39 +00001198 // start colors default to black if unspecified
1199 char start_color_0[7] = "000000";
1200 char start_color_1[7] = "000000";
1201 char start_color_2[7] = "000000";
1202 char start_color_3[7] = "000000";
Yegor Malyshev6925e1b2020-09-10 16:42:20 +02001203
1204 int nextReadPos;
1205
yidong zhang87bb3462022-12-07 15:55:31 +08001206 if (strlen(l) == 0) {
1207 s = ++endl;
1208 continue;
1209 }
1210
Nicolas Geoffray41fffb42020-12-15 10:50:29 +00001211 int topLineNumbers = sscanf(l, "%d %d %d %d", &width, &height, &fps, &progress);
1212 if (topLineNumbers == 3 || topLineNumbers == 4) {
1213 // SLOGD("> w=%d, h=%d, fps=%d, progress=%d", width, height, fps, progress);
Mathias Agopiana8826d62009-10-01 03:10:14 -07001214 animation.width = width;
1215 animation.height = height;
1216 animation.fps = fps;
Nicolas Geoffray41fffb42020-12-15 10:50:29 +00001217 if (topLineNumbers == 4) {
1218 animation.progressEnabled = (progress != 0);
1219 } else {
1220 animation.progressEnabled = false;
1221 }
Shan Huang488ff742021-08-25 20:10:46 +00001222 } else if (sscanf(l, "dynamic_colors %" STRTO(ANIM_PATH_MAX) "s #%6s #%6s #%6s #%6s %d %d",
Shan Huang2b31a362021-08-16 16:45:39 +00001223 dynamicColoringPartNameBuffer,
Shan Huang488ff742021-08-25 20:10:46 +00001224 start_color_0, start_color_1, start_color_2, start_color_3,
1225 &colorTransitionStart, &colorTransitionEnd)) {
Shan Huang2b31a362021-08-16 16:45:39 +00001226 animation.dynamicColoringEnabled = true;
1227 parseColor(start_color_0, animation.startColors[0]);
1228 parseColor(start_color_1, animation.startColors[1]);
1229 parseColor(start_color_2, animation.startColors[2]);
1230 parseColor(start_color_3, animation.startColors[3]);
Shan Huang488ff742021-08-25 20:10:46 +00001231 animation.colorTransitionStart = colorTransitionStart;
1232 animation.colorTransitionEnd = colorTransitionEnd;
Shan Huang2b31a362021-08-16 16:45:39 +00001233 dynamicColoringPartName = std::string(dynamicColoringPartNameBuffer);
Yegor Malyshev6925e1b2020-09-10 16:42:20 +02001234 } else if (sscanf(l, "%c %d %d %" STRTO(ANIM_PATH_MAX) "s%n",
1235 &pathType, &count, &pause, path, &nextReadPos) >= 4) {
1236 if (pathType == 'f') {
1237 sscanf(l + nextReadPos, " %d #%6s %16s %16s", &framesToFadeCount, color, clockPos1,
1238 clockPos2);
1239 } else {
1240 sscanf(l + nextReadPos, " #%6s %16s %16s", color, clockPos1, clockPos2);
1241 }
1242 // SLOGD("> type=%c, count=%d, pause=%d, path=%s, framesToFadeCount=%d, color=%s, "
1243 // "clockPos1=%s, clockPos2=%s",
1244 // pathType, count, pause, path, framesToFadeCount, color, clockPos1, clockPos2);
Mathias Agopiana8826d62009-10-01 03:10:14 -07001245 Animation::Part part;
Shan Huang2b31a362021-08-16 16:45:39 +00001246 if (path == dynamicColoringPartName) {
1247 // Part is specified to use dynamic coloring.
1248 part.useDynamicColoring = true;
1249 part.postDynamicColoring = false;
1250 postDynamicColoring = true;
1251 } else {
1252 // Part does not use dynamic coloring.
1253 part.useDynamicColoring = false;
1254 part.postDynamicColoring = postDynamicColoring;
1255 }
Kevin Hesterd3782b22012-04-26 10:38:55 -07001256 part.playUntilComplete = pathType == 'c';
Yegor Malyshev6925e1b2020-09-10 16:42:20 +02001257 part.framesToFadeCount = framesToFadeCount;
Mathias Agopiana8826d62009-10-01 03:10:14 -07001258 part.count = count;
1259 part.pause = pause;
1260 part.path = path;
Yi Kong911ac232019-03-24 01:49:02 -07001261 part.audioData = nullptr;
1262 part.animation = nullptr;
Jesse Hall083b84c2014-09-22 10:51:09 -07001263 if (!parseColor(color, part.backgroundColor)) {
Wei Wang159a4102018-10-23 23:15:58 -07001264 SLOGE("> invalid color '#%s'", color);
Jesse Hall083b84c2014-09-22 10:51:09 -07001265 part.backgroundColor[0] = 0.0f;
1266 part.backgroundColor[1] = 0.0f;
1267 part.backgroundColor[2] = 0.0f;
1268 }
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -07001269 parsePosition(clockPos1, clockPos2, &part.clockPosX, &part.clockPosY);
Mathias Agopiana8826d62009-10-01 03:10:14 -07001270 animation.parts.add(part);
1271 }
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001272 else if (strcmp(l, "$SYSTEM") == 0) {
Wei Wang159a4102018-10-23 23:15:58 -07001273 // SLOGD("> SYSTEM");
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001274 Animation::Part part;
1275 part.playUntilComplete = false;
Yegor Malyshev6925e1b2020-09-10 16:42:20 +02001276 part.framesToFadeCount = 0;
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001277 part.count = 1;
1278 part.pause = 0;
Yi Kong911ac232019-03-24 01:49:02 -07001279 part.audioData = nullptr;
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001280 part.animation = loadAnimation(String8(SYSTEM_BOOTANIMATION_FILE));
Yi Kong911ac232019-03-24 01:49:02 -07001281 if (part.animation != nullptr)
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001282 animation.parts.add(part);
1283 }
Mathias Agopiana8826d62009-10-01 03:10:14 -07001284 s = ++endl;
1285 }
1286
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001287 return true;
1288}
1289
Marin Shalamanov9070c052020-05-18 19:05:17 +02001290bool BootAnimation::preloadZip(Animation& animation) {
Parth Sane7f7b2212023-04-12 17:23:02 +00001291 ATRACE_CALL();
Mathias Agopiana8826d62009-10-01 03:10:14 -07001292 // read all the data structures
1293 const size_t pcount = animation.parts.size();
Yi Kong911ac232019-03-24 01:49:02 -07001294 void *cookie = nullptr;
Geoffrey Pitschdd214a72016-06-27 17:14:30 -04001295 ZipFileRO* zip = animation.zip;
1296 if (!zip->startIteration(&cookie)) {
Narayan Kamathafd31e02013-12-03 13:16:03 +00001297 return false;
1298 }
1299
1300 ZipEntryRO entry;
1301 char name[ANIM_ENTRY_NAME_MAX];
Yi Kong911ac232019-03-24 01:49:02 -07001302 while ((entry = zip->nextEntry(cookie)) != nullptr) {
Geoffrey Pitschdd214a72016-06-27 17:14:30 -04001303 const int foundEntryName = zip->getEntryFileName(entry, name, ANIM_ENTRY_NAME_MAX);
Narayan Kamathafd31e02013-12-03 13:16:03 +00001304 if (foundEntryName > ANIM_ENTRY_NAME_MAX || foundEntryName == -1) {
Wei Wang159a4102018-10-23 23:15:58 -07001305 SLOGE("Error fetching entry file name");
Narayan Kamathafd31e02013-12-03 13:16:03 +00001306 continue;
1307 }
1308
1309 const String8 entryName(name);
1310 const String8 path(entryName.getPathDir());
1311 const String8 leaf(entryName.getPathLeaf());
1312 if (leaf.size() > 0) {
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -07001313 if (entryName == CLOCK_FONT_ZIP_NAME) {
1314 FileMap* map = zip->createEntryFileMap(entry);
1315 if (map) {
1316 animation.clockFont.map = map;
1317 }
1318 continue;
1319 }
1320
Nicolas Geoffray41fffb42020-12-15 10:50:29 +00001321 if (entryName == PROGRESS_FONT_ZIP_NAME) {
1322 FileMap* map = zip->createEntryFileMap(entry);
1323 if (map) {
1324 animation.progressFont.map = map;
1325 }
1326 continue;
1327 }
1328
Geoffrey Pitschdd214a72016-06-27 17:14:30 -04001329 for (size_t j = 0; j < pcount; j++) {
Narayan Kamathafd31e02013-12-03 13:16:03 +00001330 if (path == animation.parts[j].path) {
Narayan Kamath4600dd02015-06-16 12:02:57 +01001331 uint16_t method;
Narayan Kamathafd31e02013-12-03 13:16:03 +00001332 // supports only stored png files
Yi Kong911ac232019-03-24 01:49:02 -07001333 if (zip->getEntryInfo(entry, &method, nullptr, nullptr, nullptr, nullptr, nullptr)) {
Narayan Kamathafd31e02013-12-03 13:16:03 +00001334 if (method == ZipFileRO::kCompressStored) {
Geoffrey Pitschdd214a72016-06-27 17:14:30 -04001335 FileMap* map = zip->createEntryFileMap(entry);
Narayan Kamathafd31e02013-12-03 13:16:03 +00001336 if (map) {
Narayan Kamathafd31e02013-12-03 13:16:03 +00001337 Animation::Part& part(animation.parts.editItemAt(j));
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -07001338 if (leaf == "audio.wav") {
1339 // a part may have at most one audio file
Geoffrey Pitschd6d9a1d2016-06-08 00:38:58 -07001340 part.audioData = (uint8_t *)map->getDataPtr();
1341 part.audioLength = map->getDataLength();
Geoffrey Pitschdd214a72016-06-27 17:14:30 -04001342 } else if (leaf == "trim.txt") {
1343 part.trimData.setTo((char const*)map->getDataPtr(),
1344 map->getDataLength());
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -07001345 } else {
1346 Animation::Frame frame;
1347 frame.name = leaf;
1348 frame.map = map;
Geoffrey Pitschdd214a72016-06-27 17:14:30 -04001349 frame.trimWidth = animation.width;
1350 frame.trimHeight = animation.height;
1351 frame.trimX = 0;
1352 frame.trimY = 0;
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -07001353 part.frames.add(frame);
1354 }
Mathias Agopiana8826d62009-10-01 03:10:14 -07001355 }
Geoffrey Pitschd6d9a1d2016-06-08 00:38:58 -07001356 } else {
Wei Wang159a4102018-10-23 23:15:58 -07001357 SLOGE("bootanimation.zip is compressed; must be only stored");
Mathias Agopiana8826d62009-10-01 03:10:14 -07001358 }
1359 }
1360 }
1361 }
1362 }
1363 }
1364
Geoffrey Pitschdd214a72016-06-27 17:14:30 -04001365 // If there is trimData present, override the positioning defaults.
1366 for (Animation::Part& part : animation.parts) {
1367 const char* trimDataStr = part.trimData.string();
1368 for (size_t frameIdx = 0; frameIdx < part.frames.size(); frameIdx++) {
1369 const char* endl = strstr(trimDataStr, "\n");
1370 // No more trimData for this part.
Yi Kong911ac232019-03-24 01:49:02 -07001371 if (endl == nullptr) {
Geoffrey Pitschdd214a72016-06-27 17:14:30 -04001372 break;
1373 }
1374 String8 line(trimDataStr, endl - trimDataStr);
1375 const char* lineStr = line.string();
1376 trimDataStr = ++endl;
1377 int width = 0, height = 0, x = 0, y = 0;
1378 if (sscanf(lineStr, "%dx%d+%d+%d", &width, &height, &x, &y) == 4) {
1379 Animation::Frame& frame(part.frames.editItemAt(frameIdx));
1380 frame.trimWidth = width;
1381 frame.trimHeight = height;
1382 frame.trimX = x;
1383 frame.trimY = y;
1384 } else {
Wei Wang159a4102018-10-23 23:15:58 -07001385 SLOGE("Error parsing trim.txt, line: %s", lineStr);
Geoffrey Pitschdd214a72016-06-27 17:14:30 -04001386 break;
1387 }
1388 }
1389 }
1390
1391 zip->endIteration(cookie);
Narayan Kamathafd31e02013-12-03 13:16:03 +00001392
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001393 return true;
1394}
1395
Marin Shalamanov9070c052020-05-18 19:05:17 +02001396bool BootAnimation::movie() {
Parth Sane7f7b2212023-04-12 17:23:02 +00001397 ATRACE_CALL();
Huihong Luo50a2f362018-08-11 09:27:57 -07001398 if (mAnimation == nullptr) {
1399 mAnimation = loadAnimation(mZipFileName);
1400 }
1401
1402 if (mAnimation == nullptr)
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001403 return false;
1404
Huihong Luo50a2f362018-08-11 09:27:57 -07001405 // mCallbacks->init() may get called recursively,
1406 // this loop is needed to get the same results
1407 for (const Animation::Part& part : mAnimation->parts) {
1408 if (part.animation != nullptr) {
1409 mCallbacks->init(part.animation->parts);
1410 }
1411 }
1412 mCallbacks->init(mAnimation->parts);
1413
Damien Bargiacchi97480862016-03-29 14:55:55 -07001414 bool anyPartHasClock = false;
Huihong Luo50a2f362018-08-11 09:27:57 -07001415 for (size_t i=0; i < mAnimation->parts.size(); i++) {
1416 if(validClock(mAnimation->parts[i])) {
Damien Bargiacchi97480862016-03-29 14:55:55 -07001417 anyPartHasClock = true;
1418 break;
1419 }
1420 }
1421 if (!anyPartHasClock) {
1422 mClockEnabled = false;
Thiébaud Weksteen08de4432021-11-18 13:25:59 +11001423 } else if (!android::base::GetBoolProperty(CLOCK_ENABLED_PROP_NAME, false)) {
1424 mClockEnabled = false;
Damien Bargiacchi97480862016-03-29 14:55:55 -07001425 }
1426
Sai Kiran Korwar27167492015-07-07 20:00:06 +05301427 // Check if npot textures are supported
1428 mUseNpotTextures = false;
1429 String8 gl_extensions;
1430 const char* exts = reinterpret_cast<const char*>(glGetString(GL_EXTENSIONS));
1431 if (!exts) {
1432 glGetError();
1433 } else {
1434 gl_extensions.setTo(exts);
1435 if ((gl_extensions.find("GL_ARB_texture_non_power_of_two") != -1) ||
1436 (gl_extensions.find("GL_OES_texture_npot") != -1)) {
1437 mUseNpotTextures = true;
1438 }
1439 }
1440
Damien Bargiacchia704b7d2016-02-16 16:55:49 -08001441 // Blend required to draw time on top of animation frames.
1442 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
Mathias Agopiana8826d62009-10-01 03:10:14 -07001443 glDisable(GL_DITHER);
1444 glDisable(GL_SCISSOR_TEST);
1445 glDisable(GL_BLEND);
Mathias Agopiana8826d62009-10-01 03:10:14 -07001446
Greg Kaiser2ded8002021-07-19 17:03:27 +00001447 glEnable(GL_TEXTURE_2D);
Lucas Dupin8eedc022021-07-29 21:49:07 +00001448 glBindTexture(GL_TEXTURE_2D, 0);
1449 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
1450 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
1451 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
1452 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -07001453 bool clockFontInitialized = false;
Damien Bargiacchia704b7d2016-02-16 16:55:49 -08001454 if (mClockEnabled) {
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -07001455 clockFontInitialized =
Huihong Luo50a2f362018-08-11 09:27:57 -07001456 (initFont(&mAnimation->clockFont, CLOCK_FONT_ASSET) == NO_ERROR);
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -07001457 mClockEnabled = clockFontInitialized;
Damien Bargiacchia704b7d2016-02-16 16:55:49 -08001458 }
1459
Nicolas Geoffray41fffb42020-12-15 10:50:29 +00001460 initFont(&mAnimation->progressFont, PROGRESS_FONT_ASSET);
1461
Damien Bargiacchi97480862016-03-29 14:55:55 -07001462 if (mClockEnabled && !updateIsTimeAccurate()) {
1463 mTimeCheckThread = new TimeCheckThread(this);
1464 mTimeCheckThread->run("BootAnimation::TimeCheckThread", PRIORITY_NORMAL);
1465 }
1466
George Burgess IV0f8e8112021-08-23 17:32:23 -07001467 if (mAnimation->dynamicColoringEnabled) {
Shan Huang7fee3522021-08-18 00:56:08 +00001468 initDynamicColors();
1469 }
1470
Huihong Luo50a2f362018-08-11 09:27:57 -07001471 playAnimation(*mAnimation);
Damien Bargiacchi97480862016-03-29 14:55:55 -07001472
Geoffrey Pitscha9173532016-07-19 14:56:39 -04001473 if (mTimeCheckThread != nullptr) {
Damien Bargiacchi97480862016-03-29 14:55:55 -07001474 mTimeCheckThread->requestExit();
Geoffrey Pitscha9173532016-07-19 14:56:39 -04001475 mTimeCheckThread = nullptr;
1476 }
1477
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -07001478 if (clockFontInitialized) {
Huihong Luo50a2f362018-08-11 09:27:57 -07001479 glDeleteTextures(1, &mAnimation->clockFont.texture.name);
Andriy Naborskyy815e51d2016-03-24 16:43:34 -07001480 }
1481
Huihong Luo50a2f362018-08-11 09:27:57 -07001482 releaseAnimation(mAnimation);
1483 mAnimation = nullptr;
Damien Bargiacchi1a8a2132018-07-24 15:20:26 -07001484
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001485 return false;
1486}
1487
Nicolas Geoffraya1298752021-04-20 15:15:06 +01001488bool BootAnimation::shouldStopPlayingPart(const Animation::Part& part,
1489 const int fadedFramesCount,
1490 const int lastDisplayedProgress) {
Parth Sane7f7b2212023-04-12 17:23:02 +00001491 ATRACE_CALL();
Yegor Malyshev6925e1b2020-09-10 16:42:20 +02001492 // 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 +01001493 return exitPending() && !part.playUntilComplete && fadedFramesCount >= part.framesToFadeCount &&
1494 (lastDisplayedProgress == 0 || lastDisplayedProgress == 100);
Yegor Malyshev6925e1b2020-09-10 16:42:20 +02001495}
1496
Lucas Dupin8eedc022021-07-29 21:49:07 +00001497// Linear mapping from range <a1, a2> to range <b1, b2>
1498float mapLinear(float x, float a1, float a2, float b1, float b2) {
1499 return b1 + ( x - a1 ) * ( b2 - b1 ) / ( a2 - a1 );
1500}
1501
1502void BootAnimation::drawTexturedQuad(float xStart, float yStart, float width, float height) {
Parth Sane7f7b2212023-04-12 17:23:02 +00001503 ATRACE_CALL();
Lucas Dupin8eedc022021-07-29 21:49:07 +00001504 // Map coordinates from screen space to world space.
1505 float x0 = mapLinear(xStart, 0, mWidth, -1, 1);
1506 float y0 = mapLinear(yStart, 0, mHeight, -1, 1);
1507 float x1 = mapLinear(xStart + width, 0, mWidth, -1, 1);
1508 float y1 = mapLinear(yStart + height, 0, mHeight, -1, 1);
1509 // Update quad vertex positions.
1510 quadPositions[0] = x0;
1511 quadPositions[1] = y0;
1512 quadPositions[2] = x1;
1513 quadPositions[3] = y0;
1514 quadPositions[4] = x1;
1515 quadPositions[5] = y1;
1516 quadPositions[6] = x1;
1517 quadPositions[7] = y1;
1518 quadPositions[8] = x0;
1519 quadPositions[9] = y1;
1520 quadPositions[10] = x0;
1521 quadPositions[11] = y0;
1522 glDrawArrays(GL_TRIANGLES, 0,
1523 sizeof(quadPositions) / sizeof(quadPositions[0]) / 2);
1524}
1525
Shan Huang7fee3522021-08-18 00:56:08 +00001526void BootAnimation::initDynamicColors() {
Parth Sane7f7b2212023-04-12 17:23:02 +00001527 ATRACE_CALL();
Shan Huang7fee3522021-08-18 00:56:08 +00001528 for (int i = 0; i < DYNAMIC_COLOR_COUNT; i++) {
Lucas Dupincd256842022-03-29 15:46:36 -07001529 const auto syspropName = "persist.bootanim.color" + std::to_string(i + 1);
1530 const auto syspropValue = android::base::GetProperty(syspropName, "");
1531 if (syspropValue != "") {
1532 SLOGI("Loaded dynamic color: %s -> %s", syspropName.c_str(), syspropValue.c_str());
1533 mDynamicColorsApplied = true;
1534 }
1535 parseColorDecimalString(syspropValue,
Shan Huang7fee3522021-08-18 00:56:08 +00001536 mAnimation->endColors[i], mAnimation->startColors[i]);
1537 }
1538 glUseProgram(mImageShader);
Lucas Dupincd256842022-03-29 15:46:36 -07001539 SLOGI("Dynamically coloring boot animation. Sysprops loaded? %i", mDynamicColorsApplied);
Shan Huang7fee3522021-08-18 00:56:08 +00001540 for (int i = 0; i < DYNAMIC_COLOR_COUNT; i++) {
1541 float *startColor = mAnimation->startColors[i];
1542 float *endColor = mAnimation->endColors[i];
Andrew Zengf9f58222022-11-08 11:10:48 -08001543 glUniform3f(glGetUniformLocation(mImageShader,
Shan Huang7fee3522021-08-18 00:56:08 +00001544 (U_START_COLOR_PREFIX + std::to_string(i)).c_str()),
Andrew Zengf9f58222022-11-08 11:10:48 -08001545 startColor[0], startColor[1], startColor[2]);
1546 glUniform3f(glGetUniformLocation(mImageShader,
Shan Huang7fee3522021-08-18 00:56:08 +00001547 (U_END_COLOR_PREFIX + std::to_string(i)).c_str()),
Andrew Zengf9f58222022-11-08 11:10:48 -08001548 endColor[0], endColor[1], endColor[2]);
Shan Huang7fee3522021-08-18 00:56:08 +00001549 }
1550 mImageColorProgressLocation = glGetUniformLocation(mImageShader, U_COLOR_PROGRESS);
1551}
1552
Marin Shalamanov9070c052020-05-18 19:05:17 +02001553bool BootAnimation::playAnimation(const Animation& animation) {
Parth Sane7f7b2212023-04-12 17:23:02 +00001554 ATRACE_CALL();
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001555 const size_t pcount = animation.parts.size();
Mathias Agopiana8826d62009-10-01 03:10:14 -07001556 nsecs_t frameDuration = s2ns(1) / animation.fps;
Mathias Agopian9f3020d2009-11-06 16:30:18 -08001557
Wei Wang159a4102018-10-23 23:15:58 -07001558 SLOGD("%sAnimationShownTiming start time: %" PRId64 "ms", mShuttingDown ? "Shutdown" : "Boot",
Keun-young Parkd1794cd2017-04-04 12:21:19 -07001559 elapsedRealtime());
Yegor Malyshev6925e1b2020-09-10 16:42:20 +02001560
1561 int fadedFramesCount = 0;
Nicolas Geoffray41fffb42020-12-15 10:50:29 +00001562 int lastDisplayedProgress = 0;
Lucas Dupinbfd18122022-04-25 11:26:08 -07001563 int colorTransitionStart = animation.colorTransitionStart;
1564 int colorTransitionEnd = animation.colorTransitionEnd;
Narayan Kamathafd31e02013-12-03 13:16:03 +00001565 for (size_t i=0 ; i<pcount ; i++) {
Mathias Agopiana8826d62009-10-01 03:10:14 -07001566 const Animation::Part& part(animation.parts[i]);
1567 const size_t fcount = part.frames.size();
Mathias Agopiana8826d62009-10-01 03:10:14 -07001568
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001569 // Handle animation package
Yi Kong911ac232019-03-24 01:49:02 -07001570 if (part.animation != nullptr) {
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001571 playAnimation(*part.animation);
1572 if (exitPending())
1573 break;
1574 continue; //to next part
1575 }
1576
Yegor Malyshev6925e1b2020-09-10 16:42:20 +02001577 // process the part not only while the count allows but also if already fading
1578 for (int r=0 ; !part.count || r<part.count || fadedFramesCount > 0 ; r++) {
Nicolas Geoffraya1298752021-04-20 15:15:06 +01001579 if (shouldStopPlayingPart(part, fadedFramesCount, lastDisplayedProgress)) break;
Kevin Hesterd3782b22012-04-26 10:38:55 -07001580
Lucas Dupinbfd18122022-04-25 11:26:08 -07001581 // It's possible that the sysprops were not loaded yet at this boot phase.
1582 // If that's the case, then we should keep trying until they are available.
1583 if (animation.dynamicColoringEnabled && !mDynamicColorsApplied
1584 && (part.useDynamicColoring || part.postDynamicColoring)) {
1585 SLOGD("Trying to load dynamic color sysprops.");
1586 initDynamicColors();
1587 if (mDynamicColorsApplied) {
1588 // Sysprops were loaded. Next step is to adjust the animation if we loaded
1589 // the colors after the animation should have started.
1590 const int transitionLength = colorTransitionEnd - colorTransitionStart;
1591 if (part.postDynamicColoring) {
1592 colorTransitionStart = 0;
1593 colorTransitionEnd = fmin(transitionLength, fcount - 1);
1594 }
1595 }
1596 }
1597
Ed Coyne7464ac92017-06-08 12:26:48 -07001598 mCallbacks->playPart(i, part, r);
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -07001599
Jesse Hall083b84c2014-09-22 10:51:09 -07001600 glClearColor(
1601 part.backgroundColor[0],
1602 part.backgroundColor[1],
1603 part.backgroundColor[2],
1604 1.0f);
1605
Yoshiaki Maruoka9ef19b82017-10-25 13:55:49 +09001606 ALOGD("Playing files = %s/%s, Requested repeat = %d, playUntilComplete = %s",
1607 animation.fileName.string(), part.path.string(), part.count,
1608 part.playUntilComplete ? "true" : "false");
1609
Nicolas Geoffray41fffb42020-12-15 10:50:29 +00001610 // For the last animation, if we have progress indicator from
1611 // the system, display it.
1612 int currentProgress = android::base::GetIntProperty(PROGRESS_PROP_NAME, 0);
1613 bool displayProgress = animation.progressEnabled &&
1614 (i == (pcount -1)) && currentProgress != 0;
1615
Yegor Malyshev6925e1b2020-09-10 16:42:20 +02001616 for (size_t j=0 ; j<fcount ; j++) {
Nicolas Geoffraya1298752021-04-20 15:15:06 +01001617 if (shouldStopPlayingPart(part, fadedFramesCount, lastDisplayedProgress)) break;
Yegor Malyshev6925e1b2020-09-10 16:42:20 +02001618
Shan Huang2b31a362021-08-16 16:45:39 +00001619 // Color progress is
Shan Huang488ff742021-08-25 20:10:46 +00001620 // - the animation progress, normalized from
1621 // [colorTransitionStart,colorTransitionEnd] to [0, 1] for the dynamic coloring
1622 // part.
Shan Huang2b31a362021-08-16 16:45:39 +00001623 // - 0 for parts that come before,
1624 // - 1 for parts that come after.
1625 float colorProgress = part.useDynamicColoring
Shan Huang488ff742021-08-25 20:10:46 +00001626 ? fmin(fmax(
Lucas Dupinbfd18122022-04-25 11:26:08 -07001627 ((float)j - colorTransitionStart) /
1628 fmax(colorTransitionEnd - colorTransitionStart, 1.0f), 0.0f), 1.0f)
Shan Huang2b31a362021-08-16 16:45:39 +00001629 : (part.postDynamicColoring ? 1 : 0);
1630
Marin Shalamanov9070c052020-05-18 19:05:17 +02001631 processDisplayEvents();
1632
joenchen764ea042022-04-11 23:58:34 +08001633 const double ratio_w = static_cast<double>(mWidth) / mInitWidth;
1634 const double ratio_h = static_cast<double>(mHeight) / mInitHeight;
1635 const int animationX = (mWidth - animation.width * ratio_w) / 2;
1636 const int animationY = (mHeight - animation.height * ratio_h) / 2;
Marin Shalamanov9070c052020-05-18 19:05:17 +02001637
Mathias Agopiana8826d62009-10-01 03:10:14 -07001638 const Animation::Frame& frame(part.frames[j]);
Mathias Agopiandb7dd2a2012-05-12 15:08:21 -07001639 nsecs_t lastFrame = systemTime();
Mathias Agopiana8826d62009-10-01 03:10:14 -07001640
1641 if (r > 0) {
1642 glBindTexture(GL_TEXTURE_2D, frame.tid);
1643 } else {
Lucas Dupin8eedc022021-07-29 21:49:07 +00001644 glGenTextures(1, &frame.tid);
1645 glBindTexture(GL_TEXTURE_2D, frame.tid);
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -07001646 int w, h;
Shan Huang2b31a362021-08-16 16:45:39 +00001647 // Set decoding option to alpha unpremultiplied so that the R, G, B channels
1648 // of transparent pixels are preserved.
1649 initTexture(frame.map, &w, &h, false /* don't premultiply alpha */);
Mathias Agopiana8826d62009-10-01 03:10:14 -07001650 }
1651
joenchen764ea042022-04-11 23:58:34 +08001652 const int trimWidth = frame.trimWidth * ratio_w;
1653 const int trimHeight = frame.trimHeight * ratio_h;
1654 const int trimX = frame.trimX * ratio_w;
1655 const int trimY = frame.trimY * ratio_h;
1656 const int xc = animationX + trimX;
1657 const int yc = animationY + trimY;
Shan Huang2b31a362021-08-16 16:45:39 +00001658 glClear(GL_COLOR_BUFFER_BIT);
Geoffrey Pitschdd214a72016-06-27 17:14:30 -04001659 // specify the y center as ceiling((mHeight - frame.trimHeight) / 2)
1660 // which is equivalent to mHeight - (yc + frame.trimHeight)
joenchen764ea042022-04-11 23:58:34 +08001661 const int frameDrawY = mHeight - (yc + trimHeight);
Yegor Malyshev6925e1b2020-09-10 16:42:20 +02001662
Lucas Dupin8eedc022021-07-29 21:49:07 +00001663 float fade = 0;
Yegor Malyshev6925e1b2020-09-10 16:42:20 +02001664 // if the part hasn't been stopped yet then continue fading if necessary
1665 if (exitPending() && part.hasFadingPhase()) {
Lucas Dupin8eedc022021-07-29 21:49:07 +00001666 fade = static_cast<float>(++fadedFramesCount) / part.framesToFadeCount;
Yegor Malyshev6925e1b2020-09-10 16:42:20 +02001667 if (fadedFramesCount >= part.framesToFadeCount) {
1668 fadedFramesCount = MAX_FADED_FRAMES_COUNT; // no more fading
1669 }
1670 }
Lucas Dupin8eedc022021-07-29 21:49:07 +00001671 glUseProgram(mImageShader);
1672 glUniform1i(mImageTextureLocation, 0);
1673 glUniform1f(mImageFadeLocation, fade);
Shan Huang2b31a362021-08-16 16:45:39 +00001674 if (animation.dynamicColoringEnabled) {
1675 glUniform1f(mImageColorProgressLocation, colorProgress);
1676 }
Lucas Dupin8eedc022021-07-29 21:49:07 +00001677 glEnable(GL_BLEND);
joenchen764ea042022-04-11 23:58:34 +08001678 drawTexturedQuad(xc, frameDrawY, trimWidth, trimHeight);
Lucas Dupin8eedc022021-07-29 21:49:07 +00001679 glDisable(GL_BLEND);
Yegor Malyshev6925e1b2020-09-10 16:42:20 +02001680
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -07001681 if (mClockEnabled && mTimeIsAccurate && validClock(part)) {
1682 drawClock(animation.clockFont, part.clockPosX, part.clockPosY);
Damien Bargiacchia704b7d2016-02-16 16:55:49 -08001683 }
Yegor Malyshev6925e1b2020-09-10 16:42:20 +02001684
Nicolas Geoffray41fffb42020-12-15 10:50:29 +00001685 if (displayProgress) {
1686 int newProgress = android::base::GetIntProperty(PROGRESS_PROP_NAME, 0);
1687 // In case the new progress jumped suddenly, still show an
1688 // increment of 1.
1689 if (lastDisplayedProgress != 100) {
1690 // Artificially sleep 1/10th a second to slow down the animation.
1691 usleep(100000);
1692 if (lastDisplayedProgress < newProgress) {
1693 lastDisplayedProgress++;
1694 }
1695 }
1696 // Put the progress percentage right below the animation.
1697 int posY = animation.height / 3;
1698 int posX = TEXT_CENTER_VALUE;
1699 drawProgress(lastDisplayedProgress, animation.progressFont, posX, posY);
1700 }
1701
Adrian Roos9ee5dff2018-08-22 20:19:49 +02001702 handleViewport(frameDuration);
Damien Bargiacchia704b7d2016-02-16 16:55:49 -08001703
Mathias Agopiana8826d62009-10-01 03:10:14 -07001704 eglSwapBuffers(mDisplay, mSurface);
1705
1706 nsecs_t now = systemTime();
1707 nsecs_t delay = frameDuration - (now - lastFrame);
Wei Wang159a4102018-10-23 23:15:58 -07001708 //SLOGD("%lld, %lld", ns2ms(now - lastFrame), ns2ms(delay));
Mathias Agopiana8826d62009-10-01 03:10:14 -07001709 lastFrame = now;
Mathias Agopiandb7dd2a2012-05-12 15:08:21 -07001710
1711 if (delay > 0) {
1712 struct timespec spec;
1713 spec.tv_sec = (now + delay) / 1000000000;
1714 spec.tv_nsec = (now + delay) % 1000000000;
1715 int err;
1716 do {
Yi Kong911ac232019-03-24 01:49:02 -07001717 err = clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &spec, nullptr);
Keith Mok0f2e6682021-11-15 05:50:32 +00001718 } while (err == EINTR);
Mathias Agopiandb7dd2a2012-05-12 15:08:21 -07001719 }
Kevin Hesterd3782b22012-04-26 10:38:55 -07001720
1721 checkExit();
Mathias Agopiana8826d62009-10-01 03:10:14 -07001722 }
Kevin Hesterd3782b22012-04-26 10:38:55 -07001723
niuwenchao9cd6ac82022-12-23 17:34:22 +08001724 int pauseDuration = part.pause * ns2us(frameDuration);
1725 while(pauseDuration > 0 && !exitPending()){
1726 if (pauseDuration > MAX_CHECK_EXIT_INTERVAL_US) {
1727 usleep(MAX_CHECK_EXIT_INTERVAL_US);
1728 pauseDuration -= MAX_CHECK_EXIT_INTERVAL_US;
1729 } else {
1730 usleep(pauseDuration);
1731 break;
1732 }
1733 checkExit();
1734 }
Kevin Hesterd3782b22012-04-26 10:38:55 -07001735
Yegor Malyshev6925e1b2020-09-10 16:42:20 +02001736 if (exitPending() && !part.count && mCurrentInset >= mTargetInset &&
1737 !part.hasFadingPhase()) {
Nicolas Geoffraya1298752021-04-20 15:15:06 +01001738 if (lastDisplayedProgress != 0 && lastDisplayedProgress != 100) {
1739 android::base::SetProperty(PROGRESS_PROP_NAME, "100");
1740 continue;
1741 }
Yegor Malyshev6925e1b2020-09-10 16:42:20 +02001742 break; // exit the infinite non-fading part when it has been played at least once
1743 }
Mathias Agopiana8826d62009-10-01 03:10:14 -07001744 }
Geoffrey Pitsch2fb30fb2016-07-06 16:16:20 -04001745 }
1746
1747 // Free textures created for looping parts now that the animation is done.
1748 for (const Animation::Part& part : animation.parts) {
Mathias Agopiana8826d62009-10-01 03:10:14 -07001749 if (part.count != 1) {
Geoffrey Pitsch2fb30fb2016-07-06 16:16:20 -04001750 const size_t fcount = part.frames.size();
1751 for (size_t j = 0; j < fcount; j++) {
Mathias Agopiana8826d62009-10-01 03:10:14 -07001752 const Animation::Frame& frame(part.frames[j]);
1753 glDeleteTextures(1, &frame.tid);
1754 }
1755 }
1756 }
Geoffrey Pitschd6d9a1d2016-06-08 00:38:58 -07001757
Yoshiaki Maruoka9ef19b82017-10-25 13:55:49 +09001758 ALOGD("%sAnimationShownTiming End time: %" PRId64 "ms", mShuttingDown ? "Shutdown" : "Boot",
1759 elapsedRealtime());
1760
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001761 return true;
Mathias Agopiana8826d62009-10-01 03:10:14 -07001762}
1763
Marin Shalamanov9070c052020-05-18 19:05:17 +02001764void BootAnimation::processDisplayEvents() {
Parth Sane7f7b2212023-04-12 17:23:02 +00001765 ATRACE_CALL();
Marin Shalamanov9070c052020-05-18 19:05:17 +02001766 // This will poll mDisplayEventReceiver and if there are new events it'll call
1767 // displayEventCallback synchronously.
1768 mLooper->pollOnce(0);
1769}
1770
Adrian Roos9ee5dff2018-08-22 20:19:49 +02001771void BootAnimation::handleViewport(nsecs_t timestep) {
Parth Sane7f7b2212023-04-12 17:23:02 +00001772 ATRACE_CALL();
Adrian Roos9ee5dff2018-08-22 20:19:49 +02001773 if (mShuttingDown || !mFlingerSurfaceControl || mTargetInset == 0) {
1774 return;
1775 }
1776 if (mTargetInset < 0) {
1777 // Poll the amount for the top display inset. This will return -1 until persistent properties
1778 // have been loaded.
1779 mTargetInset = android::base::GetIntProperty("persist.sys.displayinset.top",
1780 -1 /* default */, -1 /* min */, mHeight / 2 /* max */);
1781 }
1782 if (mTargetInset <= 0) {
1783 return;
1784 }
1785
1786 if (mCurrentInset < mTargetInset) {
1787 // After the device boots, the inset will effectively be cropped away. We animate this here.
1788 float fraction = static_cast<float>(mCurrentInset) / mTargetInset;
1789 int interpolatedInset = (cosf((fraction + 1) * M_PI) / 2.0f + 0.5f) * mTargetInset;
1790
1791 SurfaceComposerClient::Transaction()
1792 .setCrop(mFlingerSurfaceControl, Rect(0, interpolatedInset, mWidth, mHeight))
1793 .apply();
1794 } else {
1795 // At the end of the animation, we switch to the viewport that DisplayManager will apply
1796 // later. This changes the coordinate system, and means we must move the surface up by
1797 // the inset amount.
Adrian Roos9ee5dff2018-08-22 20:19:49 +02001798 Rect layerStackRect(0, 0, mWidth, mHeight - mTargetInset);
1799 Rect displayRect(0, mTargetInset, mWidth, mHeight);
1800
1801 SurfaceComposerClient::Transaction t;
1802 t.setPosition(mFlingerSurfaceControl, 0, -mTargetInset)
1803 .setCrop(mFlingerSurfaceControl, Rect(0, mTargetInset, mWidth, mHeight));
Dominik Laskowski2a3d9aa2019-11-18 20:26:39 -08001804 t.setDisplayProjection(mDisplayToken, ui::ROTATION_0, layerStackRect, displayRect);
Adrian Roos9ee5dff2018-08-22 20:19:49 +02001805 t.apply();
1806
1807 mTargetInset = mCurrentInset = 0;
1808 }
1809
1810 int delta = timestep * mTargetInset / ms2ns(200);
1811 mCurrentInset += delta;
1812}
1813
Marin Shalamanov9070c052020-05-18 19:05:17 +02001814void BootAnimation::releaseAnimation(Animation* animation) const {
Parth Sane7f7b2212023-04-12 17:23:02 +00001815 ATRACE_CALL();
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001816 for (Vector<Animation::Part>::iterator it = animation->parts.begin(),
1817 e = animation->parts.end(); it != e; ++it) {
1818 if (it->animation)
1819 releaseAnimation(it->animation);
1820 }
1821 if (animation->zip)
1822 delete animation->zip;
1823 delete animation;
1824}
1825
Marin Shalamanov9070c052020-05-18 19:05:17 +02001826BootAnimation::Animation* BootAnimation::loadAnimation(const String8& fn) {
Parth Sane7f7b2212023-04-12 17:23:02 +00001827 ATRACE_CALL();
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001828 if (mLoadedFiles.indexOf(fn) >= 0) {
Wei Wang159a4102018-10-23 23:15:58 -07001829 SLOGE("File \"%s\" is already loaded. Cyclic ref is not allowed",
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001830 fn.string());
Yi Kong911ac232019-03-24 01:49:02 -07001831 return nullptr;
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001832 }
1833 ZipFileRO *zip = ZipFileRO::open(fn);
Yi Kong911ac232019-03-24 01:49:02 -07001834 if (zip == nullptr) {
Wei Wang159a4102018-10-23 23:15:58 -07001835 SLOGE("Failed to open animation zip \"%s\": %s",
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001836 fn.string(), strerror(errno));
Yi Kong911ac232019-03-24 01:49:02 -07001837 return nullptr;
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001838 }
1839
Yoshiaki Maruoka9ef19b82017-10-25 13:55:49 +09001840 ALOGD("%s is loaded successfully", fn.string());
1841
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001842 Animation *animation = new Animation;
1843 animation->fileName = fn;
1844 animation->zip = zip;
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -07001845 animation->clockFont.map = nullptr;
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001846 mLoadedFiles.add(animation->fileName);
1847
1848 parseAnimationDesc(*animation);
Geoffrey Pitscha91a2d72016-07-12 14:46:19 -04001849 if (!preloadZip(*animation)) {
Greg Kaiser7426ec82019-09-11 14:34:27 -07001850 releaseAnimation(animation);
Yi Kong911ac232019-03-24 01:49:02 -07001851 return nullptr;
Geoffrey Pitscha91a2d72016-07-12 14:46:19 -04001852 }
1853
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001854 mLoadedFiles.remove(fn);
1855 return animation;
1856}
Damien Bargiacchi97480862016-03-29 14:55:55 -07001857
1858bool BootAnimation::updateIsTimeAccurate() {
Parth Sane7f7b2212023-04-12 17:23:02 +00001859 ATRACE_CALL();
Damien Bargiacchi97480862016-03-29 14:55:55 -07001860 static constexpr long long MAX_TIME_IN_PAST = 60000LL * 60LL * 24LL * 30LL; // 30 days
1861 static constexpr long long MAX_TIME_IN_FUTURE = 60000LL * 90LL; // 90 minutes
1862
1863 if (mTimeIsAccurate) {
1864 return true;
1865 }
Keun-young Parkb5938422017-03-23 13:46:24 -07001866 if (mShuttingDown) return true;
Damien Bargiacchi97480862016-03-29 14:55:55 -07001867 struct stat statResult;
Damien Bargiacchi9071db12016-10-28 17:38:22 -07001868
1869 if(stat(TIME_FORMAT_12_HOUR_FLAG_FILE_PATH, &statResult) == 0) {
1870 mTimeFormat12Hour = true;
1871 }
1872
Damien Bargiacchi97480862016-03-29 14:55:55 -07001873 if(stat(ACCURATE_TIME_FLAG_FILE_PATH, &statResult) == 0) {
1874 mTimeIsAccurate = true;
1875 return true;
1876 }
1877
1878 FILE* file = fopen(LAST_TIME_CHANGED_FILE_PATH, "r");
Yi Kong911ac232019-03-24 01:49:02 -07001879 if (file != nullptr) {
Damien Bargiacchi97480862016-03-29 14:55:55 -07001880 long long lastChangedTime = 0;
1881 fscanf(file, "%lld", &lastChangedTime);
1882 fclose(file);
1883 if (lastChangedTime > 0) {
1884 struct timespec now;
1885 clock_gettime(CLOCK_REALTIME, &now);
1886 // Match the Java timestamp format
1887 long long rtcNow = (now.tv_sec * 1000LL) + (now.tv_nsec / 1000000LL);
Damien Bargiacchi96762812016-07-12 15:53:40 -07001888 if (ACCURATE_TIME_EPOCH < rtcNow
1889 && lastChangedTime > (rtcNow - MAX_TIME_IN_PAST)
1890 && lastChangedTime < (rtcNow + MAX_TIME_IN_FUTURE)) {
Damien Bargiacchi97480862016-03-29 14:55:55 -07001891 mTimeIsAccurate = true;
1892 }
1893 }
1894 }
1895
1896 return mTimeIsAccurate;
1897}
1898
1899BootAnimation::TimeCheckThread::TimeCheckThread(BootAnimation* bootAnimation) : Thread(false),
Josh Yangf95b2002021-12-23 14:32:28 -08001900 mInotifyFd(-1), mBootAnimWd(-1), mTimeWd(-1), mBootAnimation(bootAnimation) {}
Damien Bargiacchi97480862016-03-29 14:55:55 -07001901
1902BootAnimation::TimeCheckThread::~TimeCheckThread() {
Parth Sane7f7b2212023-04-12 17:23:02 +00001903 ATRACE_CALL();
Damien Bargiacchi97480862016-03-29 14:55:55 -07001904 // mInotifyFd may be -1 but that's ok since we're not at risk of attempting to close a valid FD.
1905 close(mInotifyFd);
1906}
1907
1908bool BootAnimation::TimeCheckThread::threadLoop() {
Parth Sane7f7b2212023-04-12 17:23:02 +00001909 ATRACE_CALL();
Damien Bargiacchi97480862016-03-29 14:55:55 -07001910 bool shouldLoop = doThreadLoop() && !mBootAnimation->mTimeIsAccurate
1911 && mBootAnimation->mClockEnabled;
1912 if (!shouldLoop) {
1913 close(mInotifyFd);
1914 mInotifyFd = -1;
1915 }
1916 return shouldLoop;
1917}
1918
1919bool BootAnimation::TimeCheckThread::doThreadLoop() {
Parth Sane7f7b2212023-04-12 17:23:02 +00001920 ATRACE_CALL();
Damien Bargiacchi97480862016-03-29 14:55:55 -07001921 static constexpr int BUFF_LEN (10 * (sizeof(struct inotify_event) + NAME_MAX + 1));
1922
1923 // Poll instead of doing a blocking read so the Thread can exit if requested.
1924 struct pollfd pfd = { mInotifyFd, POLLIN, 0 };
1925 ssize_t pollResult = poll(&pfd, 1, 1000);
1926
1927 if (pollResult == 0) {
1928 return true;
1929 } else if (pollResult < 0) {
Wei Wang159a4102018-10-23 23:15:58 -07001930 SLOGE("Could not poll inotify events");
Damien Bargiacchi97480862016-03-29 14:55:55 -07001931 return false;
1932 }
1933
1934 char buff[BUFF_LEN] __attribute__ ((aligned(__alignof__(struct inotify_event))));;
1935 ssize_t length = read(mInotifyFd, buff, BUFF_LEN);
1936 if (length == 0) {
1937 return true;
1938 } else if (length < 0) {
Wei Wang159a4102018-10-23 23:15:58 -07001939 SLOGE("Could not read inotify events");
Damien Bargiacchi97480862016-03-29 14:55:55 -07001940 return false;
1941 }
1942
1943 const struct inotify_event *event;
1944 for (char* ptr = buff; ptr < buff + length; ptr += sizeof(struct inotify_event) + event->len) {
1945 event = (const struct inotify_event *) ptr;
Josh Yangf95b2002021-12-23 14:32:28 -08001946 if (event->wd == mBootAnimWd && strcmp(BOOTANIM_TIME_DIR_NAME, event->name) == 0) {
Damien Bargiacchi97480862016-03-29 14:55:55 -07001947 addTimeDirWatch();
1948 } else if (event->wd == mTimeWd && (strcmp(LAST_TIME_CHANGED_FILE_NAME, event->name) == 0
1949 || strcmp(ACCURATE_TIME_FLAG_FILE_NAME, event->name) == 0)) {
1950 return !mBootAnimation->updateIsTimeAccurate();
1951 }
1952 }
1953
1954 return true;
1955}
1956
1957void BootAnimation::TimeCheckThread::addTimeDirWatch() {
Parth Sane7f7b2212023-04-12 17:23:02 +00001958 ATRACE_CALL();
Josh Yangf95b2002021-12-23 14:32:28 -08001959 mTimeWd = inotify_add_watch(mInotifyFd, BOOTANIM_TIME_DIR_PATH,
Damien Bargiacchi97480862016-03-29 14:55:55 -07001960 IN_CLOSE_WRITE | IN_MOVED_TO | IN_ATTRIB);
1961 if (mTimeWd > 0) {
1962 // No need to watch for the time directory to be created if it already exists
Josh Yangf95b2002021-12-23 14:32:28 -08001963 inotify_rm_watch(mInotifyFd, mBootAnimWd);
1964 mBootAnimWd = -1;
Damien Bargiacchi97480862016-03-29 14:55:55 -07001965 }
1966}
1967
1968status_t BootAnimation::TimeCheckThread::readyToRun() {
Parth Sane7f7b2212023-04-12 17:23:02 +00001969 ATRACE_CALL();
Damien Bargiacchi97480862016-03-29 14:55:55 -07001970 mInotifyFd = inotify_init();
1971 if (mInotifyFd < 0) {
Wei Wang159a4102018-10-23 23:15:58 -07001972 SLOGE("Could not initialize inotify fd");
Damien Bargiacchi97480862016-03-29 14:55:55 -07001973 return NO_INIT;
1974 }
1975
Josh Yangf95b2002021-12-23 14:32:28 -08001976 mBootAnimWd = inotify_add_watch(mInotifyFd, BOOTANIM_DATA_DIR_PATH, IN_CREATE | IN_ATTRIB);
1977 if (mBootAnimWd < 0) {
Damien Bargiacchi97480862016-03-29 14:55:55 -07001978 close(mInotifyFd);
1979 mInotifyFd = -1;
Josh Yangf95b2002021-12-23 14:32:28 -08001980 SLOGE("Could not add watch for %s: %s", BOOTANIM_DATA_DIR_PATH, strerror(errno));
Damien Bargiacchi97480862016-03-29 14:55:55 -07001981 return NO_INIT;
1982 }
1983
1984 addTimeDirWatch();
1985
1986 if (mBootAnimation->updateIsTimeAccurate()) {
1987 close(mInotifyFd);
1988 mInotifyFd = -1;
1989 return ALREADY_EXISTS;
1990 }
1991
1992 return NO_ERROR;
1993}
1994
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001995// ---------------------------------------------------------------------------
1996
Marin Shalamanov9070c052020-05-18 19:05:17 +02001997} // namespace android