blob: f55c497f06f45a0f547c141eb30cf609bc3dc110 [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"
19
Nikita Ioffe06c986e2020-01-27 17:16:03 +000020#include <vector>
21
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080022#include <stdint.h>
Wei Wanga90c54c2017-02-02 11:35:21 -080023#include <inttypes.h>
Damien Bargiacchi97480862016-03-29 14:55:55 -070024#include <sys/inotify.h>
25#include <sys/poll.h>
26#include <sys/stat.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080027#include <sys/types.h>
28#include <math.h>
29#include <fcntl.h>
30#include <utils/misc.h>
Mathias Agopianb4d5a722009-09-23 17:05:19 -070031#include <signal.h>
Elliott Hughesbb94f312014-10-21 10:41:33 -070032#include <time.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080033
Steven Morelandfb7952f2018-02-23 14:58:50 -080034#include <cutils/atomic.h>
Jason parksbd9a08d2011-01-31 15:04:34 -060035#include <cutils/properties.h>
36
Derek Sollenberger22d0d4c72020-04-08 10:53:46 -040037#include <android/imagedecoder.h>
Mathias Agopianb13b9bd2012-02-17 18:27:36 -080038#include <androidfw/AssetManager.h>
Mathias Agopianac31a3b2009-05-21 19:59:24 -070039#include <binder/IPCThreadState.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080040#include <utils/Errors.h>
41#include <utils/Log.h>
Wei Wanga90c54c2017-02-02 11:35:21 -080042#include <utils/SystemClock.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080043
Keun-young Parkb5938422017-03-23 13:46:24 -070044#include <android-base/properties.h>
45
Marin Shalamanovcb783ca2021-01-29 21:26:07 +010046#include <ui/DisplayMode.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080047#include <ui/PixelFormat.h>
48#include <ui/Rect.h>
49#include <ui/Region.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080050
Jeff Brown0b722fe2012-08-24 22:40:14 -070051#include <gui/ISurfaceComposer.h>
Marin Shalamanov9070c052020-05-18 19:05:17 +020052#include <gui/DisplayEventReceiver.h>
Mathias Agopian8335f1c2012-02-25 18:48:35 -080053#include <gui/Surface.h>
54#include <gui/SurfaceComposerClient.h>
Lucas Dupin8eedc022021-07-29 21:49:07 +000055#include <GLES2/gl2.h>
56#include <GLES2/gl2ext.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080057#include <EGL/eglext.h>
58
59#include "BootAnimation.h"
60
Kyeongkab.Nam35a8a112019-07-17 16:41:48 +090061#define ANIM_PATH_MAX 255
62#define STR(x) #x
63#define STRTO(x) STR(x)
64
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080065namespace android {
66
Marin Shalamanovcb783ca2021-01-29 21:26:07 +010067using ui::DisplayMode;
68
Damien Bargiacchi97480862016-03-29 14:55:55 -070069static const char OEM_BOOTANIMATION_FILE[] = "/oem/media/bootanimation.zip";
Beverly34ffe252019-05-17 11:03:54 -040070static const char PRODUCT_BOOTANIMATION_DARK_FILE[] = "/product/media/bootanimation-dark.zip";
Jaekyun Seokc521bb32018-01-30 11:52:47 +090071static const char PRODUCT_BOOTANIMATION_FILE[] = "/product/media/bootanimation.zip";
Damien Bargiacchi97480862016-03-29 14:55:55 -070072static const char SYSTEM_BOOTANIMATION_FILE[] = "/system/media/bootanimation.zip";
Anders Fridlunde0b4d232018-11-06 14:23:25 +010073static const char APEX_BOOTANIMATION_FILE[] = "/apex/com.android.bootanimation/etc/bootanimation.zip";
Jaekyun Seokc521bb32018-01-30 11:52:47 +090074static const char PRODUCT_ENCRYPTED_BOOTANIMATION_FILE[] = "/product/media/bootanimation-encrypted.zip";
Damien Bargiacchi97480862016-03-29 14:55:55 -070075static const char SYSTEM_ENCRYPTED_BOOTANIMATION_FILE[] = "/system/media/bootanimation-encrypted.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
Damien Bargiacchi97480862016-03-29 14:55:55 -070084static const char SYSTEM_DATA_DIR_PATH[] = "/data/system";
85static const char SYSTEM_TIME_DIR_NAME[] = "time";
86static const char SYSTEM_TIME_DIR_PATH[] = "/data/system/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";
92static const char LAST_TIME_CHANGED_FILE_PATH[] = "/data/system/time/last_time_change";
93static const char ACCURATE_TIME_FLAG_FILE_NAME[] = "time_is_accurate";
94static const char ACCURATE_TIME_FLAG_FILE_PATH[] = "/data/system/time/time_is_accurate";
Damien Bargiacchi9071db12016-10-28 17:38:22 -070095static const char TIME_FORMAT_12_HOUR_FLAG_FILE_PATH[] = "/data/system/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";
Kyeongkab.Nam35a8a112019-07-17 16:41:48 +0900108static const int ANIM_ENTRY_NAME_MAX = ANIM_PATH_MAX + 1;
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700109static constexpr size_t TEXT_POS_LEN_MAX = 16;
Shan Huang79d079e2021-08-09 21:09:11 +0000110static const int DYNAMIC_COLOR_COUNT = 4;
Lucas Dupin8eedc022021-07-29 21:49:07 +0000111static const char U_TEXTURE[] = "uTexture";
112static const char U_FADE[] = "uFade";
113static const char U_CROP_AREA[] = "uCropArea";
Shan Huang79d079e2021-08-09 21:09:11 +0000114static const char U_START_COLOR_PREFIX[] = "uStartColor";
115static const char U_END_COLOR_PREFIX[] = "uEndColor";
116static const char U_COLOR_PROGRESS[] = "uColorProgress";
Lucas Dupin8eedc022021-07-29 21:49:07 +0000117static const char A_UV[] = "aUv";
118static const char A_POSITION[] = "aPosition";
119static const char VERTEX_SHADER_SOURCE[] = R"(
120 precision mediump float;
121 attribute vec4 aPosition;
122 attribute highp vec2 aUv;
123 varying highp vec2 vUv;
124 void main() {
125 gl_Position = aPosition;
126 vUv = aUv;
127 })";
Shan Huang79d079e2021-08-09 21:09:11 +0000128static const char IMAGE_FRAG_DYNAMIC_COLORING_SHADER_SOURCE[] = R"(
129 precision mediump float;
130 uniform sampler2D uTexture;
131 uniform float uFade;
132 uniform float uColorProgress;
133 uniform vec4 uStartColor0;
134 uniform vec4 uStartColor1;
135 uniform vec4 uStartColor2;
136 uniform vec4 uStartColor3;
137 uniform vec4 uEndColor0;
138 uniform vec4 uEndColor1;
139 uniform vec4 uEndColor2;
140 uniform vec4 uEndColor3;
141 varying highp vec2 vUv;
142 void main() {
143 vec4 mask = texture2D(uTexture, vUv);
144 vec4 color = mask.r * mix(uStartColor0, uEndColor0, uColorProgress)
145 + mask.g * mix(uStartColor1, uEndColor1, uColorProgress)
146 + mask.b * mix(uStartColor2, uEndColor2, uColorProgress)
147 + mask.a * mix(uStartColor3, uEndColor3, uColorProgress);
148 gl_FragColor = vec4(color.x, color.y, color.z, (1.0 - uFade)) * color.a;
149 })";
Lucas Dupin8eedc022021-07-29 21:49:07 +0000150static const char IMAGE_FRAG_SHADER_SOURCE[] = R"(
151 precision mediump float;
152 uniform sampler2D uTexture;
153 uniform float uFade;
154 varying highp vec2 vUv;
155 void main() {
156 vec4 color = texture2D(uTexture, vUv);
Shan Huang79d079e2021-08-09 21:09:11 +0000157 gl_FragColor = vec4(color.x, color.y, color.z, (1.0 - uFade)) * color.a;
Lucas Dupin8eedc022021-07-29 21:49:07 +0000158 })";
159static const char TEXT_FRAG_SHADER_SOURCE[] = R"(
160 precision mediump float;
161 uniform sampler2D uTexture;
162 uniform vec4 uCropArea;
163 varying highp vec2 vUv;
164 void main() {
165 vec2 uv = vec2(mix(uCropArea.x, uCropArea.z, vUv.x),
166 mix(uCropArea.y, uCropArea.w, vUv.y));
167 gl_FragColor = texture2D(uTexture, uv);
168 })";
169
170static GLfloat quadPositions[] = {
171 -0.5f, -0.5f,
172 +0.5f, -0.5f,
173 +0.5f, +0.5f,
174 +0.5f, +0.5f,
175 -0.5f, +0.5f,
176 -0.5f, -0.5f
177};
178static GLfloat quadUVs[] = {
179 0.0f, 1.0f,
180 1.0f, 1.0f,
181 1.0f, 0.0f,
182 1.0f, 0.0f,
183 0.0f, 0.0f,
184 0.0f, 1.0f
185};
Narayan Kamathafd31e02013-12-03 13:16:03 +0000186
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800187// ---------------------------------------------------------------------------
188
Ed Coyne7464ac92017-06-08 12:26:48 -0700189BootAnimation::BootAnimation(sp<Callbacks> callbacks)
Marin Shalamanov9070c052020-05-18 19:05:17 +0200190 : Thread(false), mLooper(new Looper(false)), mClockEnabled(true), mTimeIsAccurate(false),
Yi Kong911ac232019-03-24 01:49:02 -0700191 mTimeFormat12Hour(false), mTimeCheckThread(nullptr), mCallbacks(callbacks) {
Mathias Agopian627e7b52009-05-21 19:21:59 -0700192 mSession = new SurfaceComposerClient();
Geoffrey Pitsch290c4352016-08-09 14:35:10 -0400193
Keun-young Parkb5938422017-03-23 13:46:24 -0700194 std::string powerCtl = android::base::GetProperty("sys.powerctl", "");
195 if (powerCtl.empty()) {
196 mShuttingDown = false;
197 } else {
198 mShuttingDown = true;
199 }
Huihong Luo50a2f362018-08-11 09:27:57 -0700200 ALOGD("%sAnimationStartTiming start time: %" PRId64 "ms", mShuttingDown ? "Shutdown" : "Boot",
201 elapsedRealtime());
202}
203
204BootAnimation::~BootAnimation() {
205 if (mAnimation != nullptr) {
206 releaseAnimation(mAnimation);
207 mAnimation = nullptr;
208 }
209 ALOGD("%sAnimationStopTiming start time: %" PRId64 "ms", mShuttingDown ? "Shutdown" : "Boot",
210 elapsedRealtime());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800211}
212
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800213void BootAnimation::onFirstRef() {
Mathias Agopianbc726112009-09-23 15:44:05 -0700214 status_t err = mSession->linkToComposerDeath(this);
Wei Wang159a4102018-10-23 23:15:58 -0700215 SLOGE_IF(err, "linkToComposerDeath failed (%s) ", strerror(-err));
Mathias Agopian8434c532009-09-23 18:52:49 -0700216 if (err == NO_ERROR) {
Huihong Luo50a2f362018-08-11 09:27:57 -0700217 // Load the animation content -- this can be slow (eg 200ms)
218 // called before waitForSurfaceFlinger() in main() to avoid wait
219 ALOGD("%sAnimationPreloadTiming start time: %" PRId64 "ms",
220 mShuttingDown ? "Shutdown" : "Boot", elapsedRealtime());
221 preloadAnimation();
222 ALOGD("%sAnimationPreloadStopTiming start time: %" PRId64 "ms",
223 mShuttingDown ? "Shutdown" : "Boot", elapsedRealtime());
Mathias Agopianbc726112009-09-23 15:44:05 -0700224 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800225}
226
Mathias Agopianbc726112009-09-23 15:44:05 -0700227sp<SurfaceComposerClient> BootAnimation::session() const {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800228 return mSession;
229}
230
Marin Shalamanov9070c052020-05-18 19:05:17 +0200231void BootAnimation::binderDied(const wp<IBinder>&) {
Mathias Agopianbc726112009-09-23 15:44:05 -0700232 // woah, surfaceflinger died!
Wei Wang159a4102018-10-23 23:15:58 -0700233 SLOGD("SurfaceFlinger died, exiting...");
Mathias Agopianbc726112009-09-23 15:44:05 -0700234
235 // calling requestExit() is not enough here because the Surface code
236 // might be blocked on a condition variable that will never be updated.
237 kill( getpid(), SIGKILL );
238 requestExit();
239}
240
Derek Sollenberger22d0d4c72020-04-08 10:53:46 -0400241static void* decodeImage(const void* encodedData, size_t dataLength, AndroidBitmapInfo* outInfo) {
242 AImageDecoder* decoder = nullptr;
243 AImageDecoder_createFromBuffer(encodedData, dataLength, &decoder);
244 if (!decoder) {
245 return nullptr;
246 }
247
248 const AImageDecoderHeaderInfo* info = AImageDecoder_getHeaderInfo(decoder);
249 outInfo->width = AImageDecoderHeaderInfo_getWidth(info);
250 outInfo->height = AImageDecoderHeaderInfo_getHeight(info);
251 outInfo->format = AImageDecoderHeaderInfo_getAndroidBitmapFormat(info);
252 outInfo->stride = AImageDecoder_getMinimumStride(decoder);
253 outInfo->flags = 0;
254
Shan Huang79d079e2021-08-09 21:09:11 +0000255 // Set decoding option to alpha unpremultiplied so that the R, G, B channels
256 // of transparent pixels are preserved.
257 AImageDecoder_setUnpremultipliedRequired(decoder, true);
258
Derek Sollenberger22d0d4c72020-04-08 10:53:46 -0400259 const size_t size = outInfo->stride * outInfo->height;
260 void* pixels = malloc(size);
261 int result = AImageDecoder_decodeImage(decoder, pixels, outInfo->stride, size);
262 AImageDecoder_delete(decoder);
263
264 if (result != ANDROID_IMAGE_DECODER_SUCCESS) {
265 free(pixels);
266 return nullptr;
267 }
268 return pixels;
269}
270
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800271status_t BootAnimation::initTexture(Texture* texture, AssetManager& assets,
272 const char* name) {
273 Asset* asset = assets.open(name, Asset::ACCESS_BUFFER);
Yi Kong911ac232019-03-24 01:49:02 -0700274 if (asset == nullptr)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800275 return NO_INIT;
Derek Sollenberger22d0d4c72020-04-08 10:53:46 -0400276
277 AndroidBitmapInfo bitmapInfo;
278 void* pixels = decodeImage(asset->getBuffer(false), asset->getLength(), &bitmapInfo);
279 auto pixelDeleter = std::unique_ptr<void, decltype(free)*>{ pixels, free };
280
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800281 asset->close();
282 delete asset;
283
Derek Sollenberger22d0d4c72020-04-08 10:53:46 -0400284 if (!pixels) {
285 return NO_INIT;
286 }
287
288 const int w = bitmapInfo.width;
289 const int h = bitmapInfo.height;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800290
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800291 texture->w = w;
292 texture->h = h;
293
294 glGenTextures(1, &texture->name);
295 glBindTexture(GL_TEXTURE_2D, texture->name);
296
Derek Sollenberger22d0d4c72020-04-08 10:53:46 -0400297 switch (bitmapInfo.format) {
298 case ANDROID_BITMAP_FORMAT_A_8:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800299 glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, w, h, 0, GL_ALPHA,
Derek Sollenberger22d0d4c72020-04-08 10:53:46 -0400300 GL_UNSIGNED_BYTE, pixels);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800301 break;
Derek Sollenberger22d0d4c72020-04-08 10:53:46 -0400302 case ANDROID_BITMAP_FORMAT_RGBA_4444:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800303 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA,
Derek Sollenberger22d0d4c72020-04-08 10:53:46 -0400304 GL_UNSIGNED_SHORT_4_4_4_4, pixels);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800305 break;
Derek Sollenberger22d0d4c72020-04-08 10:53:46 -0400306 case ANDROID_BITMAP_FORMAT_RGBA_8888:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800307 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA,
Derek Sollenberger22d0d4c72020-04-08 10:53:46 -0400308 GL_UNSIGNED_BYTE, pixels);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800309 break;
Derek Sollenberger22d0d4c72020-04-08 10:53:46 -0400310 case ANDROID_BITMAP_FORMAT_RGB_565:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800311 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, w, h, 0, GL_RGB,
Derek Sollenberger22d0d4c72020-04-08 10:53:46 -0400312 GL_UNSIGNED_SHORT_5_6_5, pixels);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800313 break;
314 default:
315 break;
316 }
317
Lucas Dupin8eedc022021-07-29 21:49:07 +0000318 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
319 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
320 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
321 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700322
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800323 return NO_ERROR;
324}
325
Marin Shalamanov9070c052020-05-18 19:05:17 +0200326status_t BootAnimation::initTexture(FileMap* map, int* width, int* height) {
Derek Sollenberger22d0d4c72020-04-08 10:53:46 -0400327 AndroidBitmapInfo bitmapInfo;
328 void* pixels = decodeImage(map->getDataPtr(), map->getDataLength(), &bitmapInfo);
329 auto pixelDeleter = std::unique_ptr<void, decltype(free)*>{ pixels, free };
Mathias Agopiana8826d62009-10-01 03:10:14 -0700330
Mykola Kondratenko0c1eeb32014-04-15 09:35:44 +0200331 // FileMap memory is never released until application exit.
332 // Release it now as the texture is already loaded and the memory used for
333 // the packed resource can be released.
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700334 delete map;
Mykola Kondratenko0c1eeb32014-04-15 09:35:44 +0200335
Derek Sollenberger22d0d4c72020-04-08 10:53:46 -0400336 if (!pixels) {
337 return NO_INIT;
338 }
339
340 const int w = bitmapInfo.width;
341 const int h = bitmapInfo.height;
Mathias Agopiana8826d62009-10-01 03:10:14 -0700342
Mathias Agopiana8826d62009-10-01 03:10:14 -0700343 int tw = 1 << (31 - __builtin_clz(w));
344 int th = 1 << (31 - __builtin_clz(h));
345 if (tw < w) tw <<= 1;
346 if (th < h) th <<= 1;
347
Derek Sollenberger22d0d4c72020-04-08 10:53:46 -0400348 switch (bitmapInfo.format) {
349 case ANDROID_BITMAP_FORMAT_RGBA_8888:
Sai Kiran Korwar27167492015-07-07 20:00:06 +0530350 if (!mUseNpotTextures && (tw != w || th != h)) {
Mathias Agopiana8826d62009-10-01 03:10:14 -0700351 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, tw, th, 0, GL_RGBA,
Yi Kong911ac232019-03-24 01:49:02 -0700352 GL_UNSIGNED_BYTE, nullptr);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700353 glTexSubImage2D(GL_TEXTURE_2D, 0,
Derek Sollenberger22d0d4c72020-04-08 10:53:46 -0400354 0, 0, w, h, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700355 } else {
Sai Kiran Korwar27167492015-07-07 20:00:06 +0530356 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA,
Derek Sollenberger22d0d4c72020-04-08 10:53:46 -0400357 GL_UNSIGNED_BYTE, pixels);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700358 }
359 break;
360
Derek Sollenberger22d0d4c72020-04-08 10:53:46 -0400361 case ANDROID_BITMAP_FORMAT_RGB_565:
Sai Kiran Korwar27167492015-07-07 20:00:06 +0530362 if (!mUseNpotTextures && (tw != w || th != h)) {
Mathias Agopiana8826d62009-10-01 03:10:14 -0700363 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, tw, th, 0, GL_RGB,
Yi Kong911ac232019-03-24 01:49:02 -0700364 GL_UNSIGNED_SHORT_5_6_5, nullptr);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700365 glTexSubImage2D(GL_TEXTURE_2D, 0,
Derek Sollenberger22d0d4c72020-04-08 10:53:46 -0400366 0, 0, w, h, GL_RGB, GL_UNSIGNED_SHORT_5_6_5, pixels);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700367 } else {
Sai Kiran Korwar27167492015-07-07 20:00:06 +0530368 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, w, h, 0, GL_RGB,
Derek Sollenberger22d0d4c72020-04-08 10:53:46 -0400369 GL_UNSIGNED_SHORT_5_6_5, pixels);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700370 }
371 break;
372 default:
373 break;
374 }
375
Lucas Dupin8eedc022021-07-29 21:49:07 +0000376 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
377 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
378 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
379 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700380
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700381 *width = w;
382 *height = h;
383
Mathias Agopiana8826d62009-10-01 03:10:14 -0700384 return NO_ERROR;
385}
386
Marin Shalamanov9070c052020-05-18 19:05:17 +0200387class BootAnimation::DisplayEventCallback : public LooperCallback {
388 BootAnimation* mBootAnimation;
389
390public:
391 DisplayEventCallback(BootAnimation* bootAnimation) {
392 mBootAnimation = bootAnimation;
393 }
394
395 int handleEvent(int /* fd */, int events, void* /* data */) {
396 if (events & (Looper::EVENT_ERROR | Looper::EVENT_HANGUP)) {
397 ALOGE("Display event receiver pipe was closed or an error occurred. events=0x%x",
398 events);
399 return 0; // remove the callback
400 }
401
402 if (!(events & Looper::EVENT_INPUT)) {
403 ALOGW("Received spurious callback for unhandled poll event. events=0x%x", events);
404 return 1; // keep the callback
405 }
406
407 constexpr int kBufferSize = 100;
408 DisplayEventReceiver::Event buffer[kBufferSize];
409 ssize_t numEvents;
410 do {
411 numEvents = mBootAnimation->mDisplayEventReceiver->getEvents(buffer, kBufferSize);
412 for (size_t i = 0; i < static_cast<size_t>(numEvents); i++) {
413 const auto& event = buffer[i];
414 if (event.header.type == DisplayEventReceiver::DISPLAY_EVENT_HOTPLUG) {
415 SLOGV("Hotplug received");
416
417 if (!event.hotplug.connected) {
418 // ignore hotplug disconnect
419 continue;
420 }
421 auto token = SurfaceComposerClient::getPhysicalDisplayToken(
422 event.header.displayId);
423
424 if (token != mBootAnimation->mDisplayToken) {
425 // ignore hotplug of a secondary display
426 continue;
427 }
428
Marin Shalamanovcb783ca2021-01-29 21:26:07 +0100429 DisplayMode displayMode;
430 const status_t error = SurfaceComposerClient::getActiveDisplayMode(
431 mBootAnimation->mDisplayToken, &displayMode);
Marin Shalamanov9070c052020-05-18 19:05:17 +0200432 if (error != NO_ERROR) {
Marin Shalamanovcb783ca2021-01-29 21:26:07 +0100433 SLOGE("Can't get active display mode.");
Marin Shalamanov9070c052020-05-18 19:05:17 +0200434 }
Marin Shalamanovcb783ca2021-01-29 21:26:07 +0100435 mBootAnimation->resizeSurface(displayMode.resolution.getWidth(),
436 displayMode.resolution.getHeight());
Marin Shalamanov9070c052020-05-18 19:05:17 +0200437 }
438 }
439 } while (numEvents > 0);
440
441 return 1; // keep the callback
442 }
443};
444
445EGLConfig BootAnimation::getEglConfig(const EGLDisplay& display) {
446 const EGLint attribs[] = {
447 EGL_RED_SIZE, 8,
448 EGL_GREEN_SIZE, 8,
449 EGL_BLUE_SIZE, 8,
450 EGL_DEPTH_SIZE, 0,
451 EGL_NONE
452 };
453 EGLint numConfigs;
454 EGLConfig config;
455 eglChooseConfig(display, attribs, &config, 1, &numConfigs);
456 return config;
457}
458
Marin Shalamanov047802d2020-05-19 23:55:12 +0200459ui::Size BootAnimation::limitSurfaceSize(int width, int height) const {
460 ui::Size limited(width, height);
461 bool wasLimited = false;
462 const float aspectRatio = float(width) / float(height);
463 if (mMaxWidth != 0 && width > mMaxWidth) {
464 limited.height = mMaxWidth / aspectRatio;
465 limited.width = mMaxWidth;
466 wasLimited = true;
467 }
468 if (mMaxHeight != 0 && limited.height > mMaxHeight) {
469 limited.height = mMaxHeight;
470 limited.width = mMaxHeight * aspectRatio;
471 wasLimited = true;
472 }
473 SLOGV_IF(wasLimited, "Surface size has been limited to [%dx%d] from [%dx%d]",
474 limited.width, limited.height, width, height);
475 return limited;
476}
477
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800478status_t BootAnimation::readyToRun() {
479 mAssets.addDefaultAssets();
480
Dominik Laskowski3316a0a2019-01-25 02:56:41 -0800481 mDisplayToken = SurfaceComposerClient::getInternalDisplayToken();
482 if (mDisplayToken == nullptr)
Dominik Laskowski69b281d2019-11-22 14:13:12 -0800483 return NAME_NOT_FOUND;
Dominik Laskowski3316a0a2019-01-25 02:56:41 -0800484
Marin Shalamanovcb783ca2021-01-29 21:26:07 +0100485 DisplayMode displayMode;
Dominik Laskowski69b281d2019-11-22 14:13:12 -0800486 const status_t error =
Marin Shalamanovcb783ca2021-01-29 21:26:07 +0100487 SurfaceComposerClient::getActiveDisplayMode(mDisplayToken, &displayMode);
Dominik Laskowski69b281d2019-11-22 14:13:12 -0800488 if (error != NO_ERROR)
489 return error;
490
Marin Shalamanov047802d2020-05-19 23:55:12 +0200491 mMaxWidth = android::base::GetIntProperty("ro.surface_flinger.max_graphics_width", 0);
492 mMaxHeight = android::base::GetIntProperty("ro.surface_flinger.max_graphics_height", 0);
Marin Shalamanovcb783ca2021-01-29 21:26:07 +0100493 ui::Size resolution = displayMode.resolution;
Marin Shalamanov047802d2020-05-19 23:55:12 +0200494 resolution = limitSurfaceSize(resolution.width, resolution.height);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800495 // create the native surface
Jeff Brown0b722fe2012-08-24 22:40:14 -0700496 sp<SurfaceControl> control = session()->createSurface(String8("BootAnimation"),
Dominik Laskowski69b281d2019-11-22 14:13:12 -0800497 resolution.getWidth(), resolution.getHeight(), PIXEL_FORMAT_RGB_565);
Mathias Agopian439863f2011-06-28 19:09:31 -0700498
Robert Carre13b58e2017-08-31 14:50:44 -0700499 SurfaceComposerClient::Transaction t;
Huihong Luof97c9e22019-05-29 11:08:01 -0700500
501 // this guest property specifies multi-display IDs to show the boot animation
502 // multiple ids can be set with comma (,) as separator, for example:
Huihong Luod4a217e2020-01-16 15:56:33 -0800503 // setprop persist.boot.animation.displays 19260422155234049,19261083906282754
Marin Shalamanov11599ee2020-07-27 21:31:48 +0200504 Vector<PhysicalDisplayId> physicalDisplayIds;
Huihong Luof97c9e22019-05-29 11:08:01 -0700505 char displayValue[PROPERTY_VALUE_MAX] = "";
Huihong Luod4a217e2020-01-16 15:56:33 -0800506 property_get(DISPLAYS_PROP_NAME, displayValue, "");
Huihong Luof97c9e22019-05-29 11:08:01 -0700507 bool isValid = displayValue[0] != '\0';
508 if (isValid) {
509 char *p = displayValue;
510 while (*p) {
511 if (!isdigit(*p) && *p != ',') {
512 isValid = false;
513 break;
514 }
515 p ++;
516 }
517 if (!isValid)
Huihong Luod4a217e2020-01-16 15:56:33 -0800518 SLOGE("Invalid syntax for the value of system prop: %s", DISPLAYS_PROP_NAME);
Huihong Luof97c9e22019-05-29 11:08:01 -0700519 }
520 if (isValid) {
521 std::istringstream stream(displayValue);
Marin Shalamanov11599ee2020-07-27 21:31:48 +0200522 for (PhysicalDisplayId id; stream >> id.value; ) {
Huihong Luof97c9e22019-05-29 11:08:01 -0700523 physicalDisplayIds.add(id);
524 if (stream.peek() == ',')
525 stream.ignore();
526 }
527
528 // In the case of multi-display, boot animation shows on the specified displays
529 // in addition to the primary display
530 auto ids = SurfaceComposerClient::getPhysicalDisplayIds();
531 constexpr uint32_t LAYER_STACK = 0;
532 for (auto id : physicalDisplayIds) {
533 if (std::find(ids.begin(), ids.end(), id) != ids.end()) {
534 sp<IBinder> token = SurfaceComposerClient::getPhysicalDisplayToken(id);
535 if (token != nullptr)
536 t.setDisplayLayerStack(token, LAYER_STACK);
537 }
538 }
539 t.setLayerStack(control, LAYER_STACK);
540 }
541
Robert Carre13b58e2017-08-31 14:50:44 -0700542 t.setLayer(control, 0x40000000)
543 .apply();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800544
Mathias Agopian17f638b2009-04-16 20:04:08 -0700545 sp<Surface> s = control->getSurface();
546
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800547 // initialize opengl and egl
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800548 EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
Yi Kong911ac232019-03-24 01:49:02 -0700549 eglInitialize(display, nullptr, nullptr);
Marin Shalamanov9070c052020-05-18 19:05:17 +0200550 EGLConfig config = getEglConfig(display);
551 EGLSurface surface = eglCreateWindowSurface(display, config, s.get(), nullptr);
Lucas Dupin8eedc022021-07-29 21:49:07 +0000552 // Initialize egl context with client version number 2.0.
553 EGLint contextAttributes[] = {EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE};
554 EGLContext context = eglCreateContext(display, config, nullptr, contextAttributes);
Marin Shalamanov9070c052020-05-18 19:05:17 +0200555 EGLint w, h;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800556 eglQuerySurface(display, surface, EGL_WIDTH, &w);
557 eglQuerySurface(display, surface, EGL_HEIGHT, &h);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700558
Mathias Agopianabac0102009-07-31 14:47:00 -0700559 if (eglMakeCurrent(display, surface, surface, context) == EGL_FALSE)
560 return NO_INIT;
Mathias Agopiana8826d62009-10-01 03:10:14 -0700561
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800562 mDisplay = display;
563 mContext = context;
564 mSurface = surface;
565 mWidth = w;
566 mHeight = h;
Mathias Agopian17f638b2009-04-16 20:04:08 -0700567 mFlingerSurfaceControl = control;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800568 mFlingerSurface = s;
Adrian Roos9ee5dff2018-08-22 20:19:49 +0200569 mTargetInset = -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800570
Yegor Malyshev6925e1b2020-09-10 16:42:20 +0200571 projectSceneToWindow();
572
Marin Shalamanov9070c052020-05-18 19:05:17 +0200573 // Register a display event receiver
574 mDisplayEventReceiver = std::make_unique<DisplayEventReceiver>();
575 status_t status = mDisplayEventReceiver->initCheck();
576 SLOGE_IF(status != NO_ERROR, "Initialization of DisplayEventReceiver failed with status: %d",
577 status);
578 mLooper->addFd(mDisplayEventReceiver->getFd(), 0, Looper::EVENT_INPUT,
579 new DisplayEventCallback(this), nullptr);
580
Huihong Luo50a2f362018-08-11 09:27:57 -0700581 return NO_ERROR;
582}
583
Yegor Malyshev6925e1b2020-09-10 16:42:20 +0200584void BootAnimation::projectSceneToWindow() {
585 glViewport(0, 0, mWidth, mHeight);
586 glScissor(0, 0, mWidth, mHeight);
Yegor Malyshev6925e1b2020-09-10 16:42:20 +0200587}
588
Marin Shalamanov9070c052020-05-18 19:05:17 +0200589void BootAnimation::resizeSurface(int newWidth, int newHeight) {
590 // We assume this function is called on the animation thread.
591 if (newWidth == mWidth && newHeight == mHeight) {
592 return;
593 }
594 SLOGV("Resizing the boot animation surface to %d %d", newWidth, newHeight);
595
596 eglMakeCurrent(mDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
597 eglDestroySurface(mDisplay, mSurface);
598
Marin Shalamanov047802d2020-05-19 23:55:12 +0200599 const auto limitedSize = limitSurfaceSize(newWidth, newHeight);
600 mWidth = limitedSize.width;
601 mHeight = limitedSize.height;
Marin Shalamanov9070c052020-05-18 19:05:17 +0200602
603 SurfaceComposerClient::Transaction t;
604 t.setSize(mFlingerSurfaceControl, mWidth, mHeight);
605 t.apply();
606
607 EGLConfig config = getEglConfig(mDisplay);
608 EGLSurface surface = eglCreateWindowSurface(mDisplay, config, mFlingerSurface.get(), nullptr);
609 if (eglMakeCurrent(mDisplay, surface, surface, mContext) == EGL_FALSE) {
610 SLOGE("Can't make the new surface current. Error %d", eglGetError());
611 return;
612 }
Yegor Malyshev6925e1b2020-09-10 16:42:20 +0200613
614 projectSceneToWindow();
Marin Shalamanov9070c052020-05-18 19:05:17 +0200615
616 mSurface = surface;
617}
618
Huihong Luo50a2f362018-08-11 09:27:57 -0700619bool BootAnimation::preloadAnimation() {
620 findBootAnimationFile();
621 if (!mZipFileName.isEmpty()) {
622 mAnimation = loadAnimation(mZipFileName);
623 return (mAnimation != nullptr);
624 }
625
626 return false;
627}
628
Nikita Ioffe06c986e2020-01-27 17:16:03 +0000629bool BootAnimation::findBootAnimationFileInternal(const std::vector<std::string> &files) {
630 for (const std::string& f : files) {
631 if (access(f.c_str(), R_OK) == 0) {
632 mZipFileName = f.c_str();
633 return true;
634 }
635 }
636 return false;
637}
638
Huihong Luo50a2f362018-08-11 09:27:57 -0700639void BootAnimation::findBootAnimationFile() {
Elliott Hughesc367d482013-10-29 13:12:55 -0700640 // If the device has encryption turned on or is in process
Jason parksbd9a08d2011-01-31 15:04:34 -0600641 // of being encrypted we show the encrypted boot animation.
642 char decrypt[PROPERTY_VALUE_MAX];
643 property_get("vold.decrypt", decrypt, "");
644
Keun-young Parkb5938422017-03-23 13:46:24 -0700645 bool encryptedAnimation = atoi(decrypt) != 0 ||
646 !strcmp("trigger_restart_min_framework", decrypt);
Jason parksbd9a08d2011-01-31 15:04:34 -0600647
Jaekyun Seokc521bb32018-01-30 11:52:47 +0900648 if (!mShuttingDown && encryptedAnimation) {
Nikita Ioffe06c986e2020-01-27 17:16:03 +0000649 static const std::vector<std::string> encryptedBootFiles = {
650 PRODUCT_ENCRYPTED_BOOTANIMATION_FILE, SYSTEM_ENCRYPTED_BOOTANIMATION_FILE,
651 };
652 if (findBootAnimationFileInternal(encryptedBootFiles)) {
653 return;
Jaekyun Seokc521bb32018-01-30 11:52:47 +0900654 }
Jason parksbd9a08d2011-01-31 15:04:34 -0600655 }
Beverly34ffe252019-05-17 11:03:54 -0400656
657 const bool playDarkAnim = android::base::GetIntProperty("ro.boot.theme", 0) == 1;
Nikita Ioffe06c986e2020-01-27 17:16:03 +0000658 static const std::vector<std::string> bootFiles = {
659 APEX_BOOTANIMATION_FILE, playDarkAnim ? PRODUCT_BOOTANIMATION_DARK_FILE : PRODUCT_BOOTANIMATION_FILE,
660 OEM_BOOTANIMATION_FILE, SYSTEM_BOOTANIMATION_FILE
661 };
662 static const std::vector<std::string> shutdownFiles = {
663 PRODUCT_SHUTDOWNANIMATION_FILE, OEM_SHUTDOWNANIMATION_FILE, SYSTEM_SHUTDOWNANIMATION_FILE, ""
664 };
665 static const std::vector<std::string> userspaceRebootFiles = {
666 PRODUCT_USERSPACE_REBOOT_ANIMATION_FILE, OEM_USERSPACE_REBOOT_ANIMATION_FILE,
667 SYSTEM_USERSPACE_REBOOT_ANIMATION_FILE,
668 };
Keun-young Parkb5938422017-03-23 13:46:24 -0700669
Nikita Ioffe06c986e2020-01-27 17:16:03 +0000670 if (android::base::GetBoolProperty("sys.init.userspace_reboot.in_progress", false)) {
671 findBootAnimationFileInternal(userspaceRebootFiles);
672 } else if (mShuttingDown) {
673 findBootAnimationFileInternal(shutdownFiles);
674 } else {
675 findBootAnimationFileInternal(bootFiles);
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700676 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800677}
678
Lucas Dupin8eedc022021-07-29 21:49:07 +0000679GLuint compileShader(GLenum shaderType, const GLchar *source) {
680 GLuint shader = glCreateShader(shaderType);
681 glShaderSource(shader, 1, &source, 0);
682 glCompileShader(shader);
683 GLint isCompiled = 0;
684 glGetShaderiv(shader, GL_COMPILE_STATUS, &isCompiled);
685 if (isCompiled == GL_FALSE) {
686 SLOGE("Compile shader failed. Shader type: %d", shaderType);
687 return 0;
688 }
689 return shader;
690}
691
692GLuint linkShader(GLuint vertexShader, GLuint fragmentShader) {
693 GLuint program = glCreateProgram();
694 glAttachShader(program, vertexShader);
695 glAttachShader(program, fragmentShader);
696 glLinkProgram(program);
697 GLint isLinked = 0;
698 glGetProgramiv(program, GL_LINK_STATUS, (int *)&isLinked);
699 if (isLinked == GL_FALSE) {
700 SLOGE("Linking shader failed. Shader handles: vert %d, frag %d",
701 vertexShader, fragmentShader);
702 return 0;
703 }
704 return program;
705}
706
707void BootAnimation::initShaders() {
708 GLuint vertexShader = compileShader(GL_VERTEX_SHADER, (const GLchar *)VERTEX_SHADER_SOURCE);
709 GLuint imageFragmentShader =
Shan Huang79d079e2021-08-09 21:09:11 +0000710 compileShader(GL_FRAGMENT_SHADER, mAnimation->dynamicColoringEnabled
711 ? (const GLchar *)IMAGE_FRAG_DYNAMIC_COLORING_SHADER_SOURCE
712 : (const GLchar *)IMAGE_FRAG_SHADER_SOURCE);
Lucas Dupin8eedc022021-07-29 21:49:07 +0000713 GLuint textFragmentShader =
714 compileShader(GL_FRAGMENT_SHADER, (const GLchar *)TEXT_FRAG_SHADER_SOURCE);
715
716 // Initialize image shader.
717 mImageShader = linkShader(vertexShader, imageFragmentShader);
718 GLint positionLocation = glGetAttribLocation(mImageShader, A_POSITION);
719 GLint uvLocation = glGetAttribLocation(mImageShader, A_UV);
720 mImageTextureLocation = glGetUniformLocation(mImageShader, U_TEXTURE);
721 mImageFadeLocation = glGetUniformLocation(mImageShader, U_FADE);
Shan Huang79d079e2021-08-09 21:09:11 +0000722 mImageColorProgressLocation = glGetUniformLocation(mImageShader, U_COLOR_PROGRESS);
Lucas Dupin8eedc022021-07-29 21:49:07 +0000723 glEnableVertexAttribArray(positionLocation);
724 glVertexAttribPointer(positionLocation, 2, GL_FLOAT, GL_FALSE, 0, quadPositions);
725 glVertexAttribPointer(uvLocation, 2, GL_FLOAT, GL_FALSE, 0, quadUVs);
726 glEnableVertexAttribArray(uvLocation);
727
Shan Huang79d079e2021-08-09 21:09:11 +0000728 if (mAnimation->dynamicColoringEnabled) {
729 glUseProgram(mImageShader);
730 SLOGI("[BootAnimation] Dynamically coloring boot animation.");
731 for (int i = 0; i < DYNAMIC_COLOR_COUNT; i++) {
732 float *startColor = mAnimation->startColors[i];
733 float *endColor = mAnimation->endColors[i];
734 glUniform4f(glGetUniformLocation(mImageShader,
735 (U_START_COLOR_PREFIX + std::to_string(i)).c_str()),
736 startColor[0], startColor[1], startColor[2], 1 /* alpha */);
737 glUniform4f(glGetUniformLocation(mImageShader,
738 (U_END_COLOR_PREFIX + std::to_string(i)).c_str()),
739 endColor[0], endColor[1], endColor[2], 1 /* alpha */);
740 }
741 }
742
Lucas Dupin8eedc022021-07-29 21:49:07 +0000743 // Initialize text shader.
744 mTextShader = linkShader(vertexShader, textFragmentShader);
745 positionLocation = glGetAttribLocation(mTextShader, A_POSITION);
746 uvLocation = glGetAttribLocation(mTextShader, A_UV);
747 mTextTextureLocation = glGetUniformLocation(mTextShader, U_TEXTURE);
748 mTextCropAreaLocation = glGetUniformLocation(mTextShader, U_CROP_AREA);
749 glEnableVertexAttribArray(positionLocation);
750 glVertexAttribPointer(positionLocation, 2, GL_FLOAT, GL_FALSE, 0, quadPositions);
751 glVertexAttribPointer(uvLocation, 2, GL_FLOAT, GL_FALSE, 0, quadUVs);
752 glEnableVertexAttribArray(uvLocation);
753}
754
Marin Shalamanov9070c052020-05-18 19:05:17 +0200755bool BootAnimation::threadLoop() {
756 bool result;
Lucas Dupin8eedc022021-07-29 21:49:07 +0000757 initShaders();
758
Narayan Kamathafd31e02013-12-03 13:16:03 +0000759 // We have no bootanimation file, so we use the stock android logo
760 // animation.
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700761 if (mZipFileName.isEmpty()) {
Marin Shalamanov9070c052020-05-18 19:05:17 +0200762 result = android();
Mathias Agopiana8826d62009-10-01 03:10:14 -0700763 } else {
Marin Shalamanov9070c052020-05-18 19:05:17 +0200764 result = movie();
Mathias Agopiana8826d62009-10-01 03:10:14 -0700765 }
766
Philip Junker60600562020-11-25 18:16:16 +0100767 mCallbacks->shutdown();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800768 eglMakeCurrent(mDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
769 eglDestroyContext(mDisplay, mContext);
770 eglDestroySurface(mDisplay, mSurface);
Mathias Agopian6cf0db22009-04-17 19:36:26 -0700771 mFlingerSurface.clear();
Mathias Agopian17f638b2009-04-16 20:04:08 -0700772 mFlingerSurfaceControl.clear();
Mathias Agopian627e7b52009-05-21 19:21:59 -0700773 eglTerminate(mDisplay);
Sai Kiran Korwar3eee9fb2015-06-16 17:13:35 +0530774 eglReleaseThread();
Mathias Agopian627e7b52009-05-21 19:21:59 -0700775 IPCThreadState::self()->stopProcess();
Marin Shalamanov9070c052020-05-18 19:05:17 +0200776 return result;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800777}
778
Marin Shalamanov9070c052020-05-18 19:05:17 +0200779bool BootAnimation::android() {
Lucas Dupin8eedc022021-07-29 21:49:07 +0000780 glActiveTexture(GL_TEXTURE0);
781
Wei Wang159a4102018-10-23 23:15:58 -0700782 SLOGD("%sAnimationShownTiming start time: %" PRId64 "ms", mShuttingDown ? "Shutdown" : "Boot",
Keun-young Parkd1794cd2017-04-04 12:21:19 -0700783 elapsedRealtime());
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700784 initTexture(&mAndroid[0], mAssets, "images/android-logo-mask.png");
785 initTexture(&mAndroid[1], mAssets, "images/android-logo-shine.png");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800786
Ed Coyne7464ac92017-06-08 12:26:48 -0700787 mCallbacks->init({});
788
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800789 // clear screen
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700790 glDisable(GL_DITHER);
791 glDisable(GL_SCISSOR_TEST);
Mathias Agopian59f19e42011-05-06 19:22:12 -0700792 glClearColor(0,0,0,1);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800793 glClear(GL_COLOR_BUFFER_BIT);
794 eglSwapBuffers(mDisplay, mSurface);
795
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700796 // Blend state
797 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700798
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800799 const nsecs_t startTime = systemTime();
800 do {
Marin Shalamanov9070c052020-05-18 19:05:17 +0200801 processDisplayEvents();
802 const GLint xc = (mWidth - mAndroid[0].w) / 2;
803 const GLint yc = (mHeight - mAndroid[0].h) / 2;
804 const Rect updateRect(xc, yc, xc + mAndroid[0].w, yc + mAndroid[0].h);
805 glScissor(updateRect.left, mHeight - updateRect.bottom, updateRect.width(),
806 updateRect.height());
807
Mathias Agopian13796652009-03-24 22:49:21 -0700808 nsecs_t now = systemTime();
809 double time = now - startTime;
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700810 float t = 4.0f * float(time / us2ns(16667)) / mAndroid[1].w;
811 GLint offset = (1 - (t - floorf(t))) * mAndroid[1].w;
812 GLint x = xc - offset;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800813
Mathias Agopian81668642009-07-28 11:41:30 -0700814 glDisable(GL_SCISSOR_TEST);
815 glClear(GL_COLOR_BUFFER_BIT);
816
817 glEnable(GL_SCISSOR_TEST);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800818 glDisable(GL_BLEND);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800819 glBindTexture(GL_TEXTURE_2D, mAndroid[1].name);
Lucas Dupin8eedc022021-07-29 21:49:07 +0000820 drawTexturedQuad(x, yc, mAndroid[1].w, mAndroid[1].h);
821 drawTexturedQuad(x + mAndroid[1].w, yc, mAndroid[1].w, mAndroid[1].h);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800822
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700823 glEnable(GL_BLEND);
824 glBindTexture(GL_TEXTURE_2D, mAndroid[0].name);
Lucas Dupin8eedc022021-07-29 21:49:07 +0000825 drawTexturedQuad(xc, yc, mAndroid[0].w, mAndroid[0].h);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800826
Mathias Agopian627e7b52009-05-21 19:21:59 -0700827 EGLBoolean res = eglSwapBuffers(mDisplay, mSurface);
828 if (res == EGL_FALSE)
829 break;
830
Mathias Agopian13796652009-03-24 22:49:21 -0700831 // 12fps: don't animate too fast to preserve CPU
832 const nsecs_t sleepTime = 83333 - ns2us(systemTime() - now);
833 if (sleepTime > 0)
Mathias Agopiana8826d62009-10-01 03:10:14 -0700834 usleep(sleepTime);
Kevin Hesterd3782b22012-04-26 10:38:55 -0700835
836 checkExit();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800837 } while (!exitPending());
838
839 glDeleteTextures(1, &mAndroid[0].name);
840 glDeleteTextures(1, &mAndroid[1].name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800841 return false;
842}
843
Kevin Hesterd3782b22012-04-26 10:38:55 -0700844void BootAnimation::checkExit() {
845 // Allow surface flinger to gracefully request shutdown
846 char value[PROPERTY_VALUE_MAX];
847 property_get(EXIT_PROP_NAME, value, "0");
848 int exitnow = atoi(value);
849 if (exitnow) {
850 requestExit();
851 }
852}
853
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700854bool BootAnimation::validClock(const Animation::Part& part) {
855 return part.clockPosX != TEXT_MISSING_VALUE && part.clockPosY != TEXT_MISSING_VALUE;
856}
857
858bool parseTextCoord(const char* str, int* dest) {
859 if (strcmp("c", str) == 0) {
860 *dest = TEXT_CENTER_VALUE;
861 return true;
862 }
863
864 char* end;
865 int val = (int) strtol(str, &end, 0);
866 if (end == str || *end != '\0' || val == INT_MAX || val == INT_MIN) {
867 return false;
868 }
869 *dest = val;
870 return true;
871}
872
873// Parse two position coordinates. If only string is non-empty, treat it as the y value.
874void parsePosition(const char* str1, const char* str2, int* x, int* y) {
875 bool success = false;
876 if (strlen(str1) == 0) { // No values were specified
877 // success = false
878 } else if (strlen(str2) == 0) { // we have only one value
879 if (parseTextCoord(str1, y)) {
880 *x = TEXT_CENTER_VALUE;
881 success = true;
882 }
883 } else {
884 if (parseTextCoord(str1, x) && parseTextCoord(str2, y)) {
885 success = true;
886 }
887 }
888
889 if (!success) {
890 *x = TEXT_MISSING_VALUE;
891 *y = TEXT_MISSING_VALUE;
892 }
893}
894
Jesse Hall083b84c2014-09-22 10:51:09 -0700895// Parse a color represented as an HTML-style 'RRGGBB' string: each pair of
896// characters in str is a hex number in [0, 255], which are converted to
897// floating point values in the range [0.0, 1.0] and placed in the
898// corresponding elements of color.
899//
900// If the input string isn't valid, parseColor returns false and color is
901// left unchanged.
902static bool parseColor(const char str[7], float color[3]) {
903 float tmpColor[3];
904 for (int i = 0; i < 3; i++) {
905 int val = 0;
906 for (int j = 0; j < 2; j++) {
907 val *= 16;
908 char c = str[2*i + j];
909 if (c >= '0' && c <= '9') val += c - '0';
910 else if (c >= 'A' && c <= 'F') val += (c - 'A') + 10;
911 else if (c >= 'a' && c <= 'f') val += (c - 'a') + 10;
912 else return false;
913 }
914 tmpColor[i] = static_cast<float>(val) / 255.0f;
915 }
916 memcpy(color, tmpColor, sizeof(tmpColor));
917 return true;
918}
919
Shan Huang79d079e2021-08-09 21:09:11 +0000920// Parse a color represented as a signed decimal int string.
921// E.g. "-2757722" (whose hex 2's complement is 0xFFD5EBA6).
922// If the input color string is empty, set color with values in defaultColor.
923static void parseColorDecimalString(const std::string& colorString,
924 float color[3], float defaultColor[3]) {
925 if (colorString == "") {
926 memcpy(color, defaultColor, sizeof(float) * 3);
927 return;
928 }
929 int colorInt = atoi(colorString.c_str());
930 color[0] = ((float)((colorInt >> 16) & 0xFF)) / 0xFF; // r
931 color[1] = ((float)((colorInt >> 8) & 0xFF)) / 0xFF; // g
932 color[2] = ((float)(colorInt & 0xFF)) / 0xFF; // b
933}
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700934
Marin Shalamanov9070c052020-05-18 19:05:17 +0200935static bool readFile(ZipFileRO* zip, const char* name, String8& outString) {
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700936 ZipEntryRO entry = zip->findEntryByName(name);
Wei Wang159a4102018-10-23 23:15:58 -0700937 SLOGE_IF(!entry, "couldn't find %s", name);
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700938 if (!entry) {
939 return false;
940 }
941
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700942 FileMap* entryMap = zip->createEntryFileMap(entry);
943 zip->releaseEntry(entry);
Wei Wang159a4102018-10-23 23:15:58 -0700944 SLOGE_IF(!entryMap, "entryMap is null");
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700945 if (!entryMap) {
946 return false;
947 }
948
949 outString.setTo((char const*)entryMap->getDataPtr(), entryMap->getDataLength());
Narayan Kamath688ff4c2015-02-23 15:47:54 +0000950 delete entryMap;
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700951 return true;
952}
953
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700954// The font image should be a 96x2 array of character images. The
955// columns are the printable ASCII characters 0x20 - 0x7f. The
956// top row is regular text; the bottom row is bold.
957status_t BootAnimation::initFont(Font* font, const char* fallback) {
958 status_t status = NO_ERROR;
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800959
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700960 if (font->map != nullptr) {
961 glGenTextures(1, &font->texture.name);
962 glBindTexture(GL_TEXTURE_2D, font->texture.name);
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800963
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700964 status = initTexture(font->map, &font->texture.w, &font->texture.h);
965
Lucas Dupin8eedc022021-07-29 21:49:07 +0000966 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
967 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
968 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
969 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700970 } else if (fallback != nullptr) {
971 status = initTexture(&font->texture, mAssets, fallback);
972 } else {
973 return NO_INIT;
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800974 }
975
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700976 if (status == NO_ERROR) {
977 font->char_width = font->texture.w / FONT_NUM_COLS;
978 font->char_height = font->texture.h / FONT_NUM_ROWS / 2; // There are bold and regular rows
979 }
980
981 return status;
982}
983
984void BootAnimation::drawText(const char* str, const Font& font, bool bold, int* x, int* y) {
985 glEnable(GL_BLEND); // Allow us to draw on top of the animation
986 glBindTexture(GL_TEXTURE_2D, font.texture.name);
Lucas Dupin8eedc022021-07-29 21:49:07 +0000987 glUseProgram(mTextShader);
988 glUniform1i(mTextTextureLocation, 0);
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700989
990 const int len = strlen(str);
991 const int strWidth = font.char_width * len;
992
993 if (*x == TEXT_CENTER_VALUE) {
994 *x = (mWidth - strWidth) / 2;
995 } else if (*x < 0) {
996 *x = mWidth + *x - strWidth;
997 }
998 if (*y == TEXT_CENTER_VALUE) {
999 *y = (mHeight - font.char_height) / 2;
1000 } else if (*y < 0) {
1001 *y = mHeight + *y - font.char_height;
1002 }
1003
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -07001004 for (int i = 0; i < len; i++) {
1005 char c = str[i];
1006
1007 if (c < FONT_BEGIN_CHAR || c > FONT_END_CHAR) {
1008 c = '?';
1009 }
1010
1011 // Crop the texture to only the pixels in the current glyph
1012 const int charPos = (c - FONT_BEGIN_CHAR); // Position in the list of valid characters
1013 const int row = charPos / FONT_NUM_COLS;
1014 const int col = charPos % FONT_NUM_COLS;
Lucas Dupin8eedc022021-07-29 21:49:07 +00001015 // Bold fonts are expected in the second half of each row.
1016 float v0 = (row + (bold ? 0.5f : 0.0f)) / FONT_NUM_ROWS;
1017 float u0 = ((float)col) / FONT_NUM_COLS;
1018 float v1 = v0 + 1.0f / FONT_NUM_ROWS / 2;
1019 float u1 = u0 + 1.0f / FONT_NUM_COLS;
1020 glUniform4f(mTextCropAreaLocation, u0, v0, u1, v1);
1021 drawTexturedQuad(*x, *y, font.char_width, font.char_height);
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -07001022
1023 *x += font.char_width;
1024 }
1025
1026 glDisable(GL_BLEND); // Return to the animation's default behaviour
1027 glBindTexture(GL_TEXTURE_2D, 0);
1028}
1029
Damien Bargiacchi9071db12016-10-28 17:38:22 -07001030// We render 12 or 24 hour time.
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -07001031void BootAnimation::drawClock(const Font& font, const int xPos, const int yPos) {
Damien Bargiacchi9071db12016-10-28 17:38:22 -07001032 static constexpr char TIME_FORMAT_12[] = "%l:%M";
1033 static constexpr char TIME_FORMAT_24[] = "%H:%M";
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -07001034 static constexpr int TIME_LENGTH = 6;
1035
Damien Bargiacchia704b7d2016-02-16 16:55:49 -08001036 time_t rawtime;
1037 time(&rawtime);
1038 struct tm* timeInfo = localtime(&rawtime);
1039
1040 char timeBuff[TIME_LENGTH];
Damien Bargiacchi9071db12016-10-28 17:38:22 -07001041 const char* timeFormat = mTimeFormat12Hour ? TIME_FORMAT_12 : TIME_FORMAT_24;
1042 size_t length = strftime(timeBuff, TIME_LENGTH, timeFormat, timeInfo);
Damien Bargiacchia704b7d2016-02-16 16:55:49 -08001043
1044 if (length != TIME_LENGTH - 1) {
Wei Wang159a4102018-10-23 23:15:58 -07001045 SLOGE("Couldn't format time; abandoning boot animation clock");
Damien Bargiacchia704b7d2016-02-16 16:55:49 -08001046 mClockEnabled = false;
1047 return;
1048 }
1049
Damien Bargiacchi45a76442016-12-05 18:02:18 -08001050 char* out = timeBuff[0] == ' ' ? &timeBuff[1] : &timeBuff[0];
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -07001051 int x = xPos;
1052 int y = yPos;
Damien Bargiacchi45a76442016-12-05 18:02:18 -08001053 drawText(out, font, false, &x, &y);
Damien Bargiacchia704b7d2016-02-16 16:55:49 -08001054}
1055
Nicolas Geoffray41fffb42020-12-15 10:50:29 +00001056void BootAnimation::drawProgress(int percent, const Font& font, const int xPos, const int yPos) {
1057 static constexpr int PERCENT_LENGTH = 5;
1058
1059 char percentBuff[PERCENT_LENGTH];
1060 // ';' has the ascii code just after ':', and the font resource contains '%'
1061 // for that ascii code.
1062 sprintf(percentBuff, "%d;", percent);
1063 int x = xPos;
1064 int y = yPos;
1065 drawText(percentBuff, font, false, &x, &y);
1066}
1067
Marin Shalamanov9070c052020-05-18 19:05:17 +02001068bool BootAnimation::parseAnimationDesc(Animation& animation) {
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -07001069 String8 desString;
1070
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001071 if (!readFile(animation.zip, "desc.txt", desString)) {
Narayan Kamathafd31e02013-12-03 13:16:03 +00001072 return false;
1073 }
Mathias Agopiana8826d62009-10-01 03:10:14 -07001074 char const* s = desString.string();
Shan Huang79d079e2021-08-09 21:09:11 +00001075 std::string dynamicColoringPartName = "";
1076 bool postDynamicColoring = false;
Mathias Agopiana8826d62009-10-01 03:10:14 -07001077
Mathias Agopiana8826d62009-10-01 03:10:14 -07001078 // Parse the description file
1079 for (;;) {
1080 const char* endl = strstr(s, "\n");
Yi Kong911ac232019-03-24 01:49:02 -07001081 if (endl == nullptr) break;
Mathias Agopiana8826d62009-10-01 03:10:14 -07001082 String8 line(s, endl - s);
1083 const char* l = line.string();
Damien Bargiacchia704b7d2016-02-16 16:55:49 -08001084 int fps = 0;
1085 int width = 0;
1086 int height = 0;
1087 int count = 0;
1088 int pause = 0;
Nicolas Geoffray41fffb42020-12-15 10:50:29 +00001089 int progress = 0;
Yegor Malyshev6925e1b2020-09-10 16:42:20 +02001090 int framesToFadeCount = 0;
Narayan Kamathafd31e02013-12-03 13:16:03 +00001091 char path[ANIM_ENTRY_NAME_MAX];
Jesse Hall083b84c2014-09-22 10:51:09 -07001092 char color[7] = "000000"; // default to black if unspecified
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -07001093 char clockPos1[TEXT_POS_LEN_MAX + 1] = "";
1094 char clockPos2[TEXT_POS_LEN_MAX + 1] = "";
Shan Huang79d079e2021-08-09 21:09:11 +00001095 char dynamicColoringPartNameBuffer[ANIM_ENTRY_NAME_MAX];
Kevin Hesterd3782b22012-04-26 10:38:55 -07001096 char pathType;
Shan Huang79d079e2021-08-09 21:09:11 +00001097 // start colors default to black if unspecified
1098 char start_color_0[7] = "000000";
1099 char start_color_1[7] = "000000";
1100 char start_color_2[7] = "000000";
1101 char start_color_3[7] = "000000";
Yegor Malyshev6925e1b2020-09-10 16:42:20 +02001102
1103 int nextReadPos;
1104
Nicolas Geoffray41fffb42020-12-15 10:50:29 +00001105 int topLineNumbers = sscanf(l, "%d %d %d %d", &width, &height, &fps, &progress);
1106 if (topLineNumbers == 3 || topLineNumbers == 4) {
1107 // SLOGD("> w=%d, h=%d, fps=%d, progress=%d", width, height, fps, progress);
Mathias Agopiana8826d62009-10-01 03:10:14 -07001108 animation.width = width;
1109 animation.height = height;
1110 animation.fps = fps;
Nicolas Geoffray41fffb42020-12-15 10:50:29 +00001111 if (topLineNumbers == 4) {
1112 animation.progressEnabled = (progress != 0);
1113 } else {
1114 animation.progressEnabled = false;
1115 }
Shan Huang79d079e2021-08-09 21:09:11 +00001116 } else if (sscanf(l, "dynamic_colors %" STRTO(ANIM_PATH_MAX) "s #%6s #%6s #%6s #%6s",
1117 dynamicColoringPartNameBuffer,
1118 start_color_0, start_color_1, start_color_2, start_color_3)) {
1119 animation.dynamicColoringEnabled = true;
1120 parseColor(start_color_0, animation.startColors[0]);
1121 parseColor(start_color_1, animation.startColors[1]);
1122 parseColor(start_color_2, animation.startColors[2]);
1123 parseColor(start_color_3, animation.startColors[3]);
1124 dynamicColoringPartName = std::string(dynamicColoringPartNameBuffer);
Yegor Malyshev6925e1b2020-09-10 16:42:20 +02001125 } else if (sscanf(l, "%c %d %d %" STRTO(ANIM_PATH_MAX) "s%n",
1126 &pathType, &count, &pause, path, &nextReadPos) >= 4) {
1127 if (pathType == 'f') {
1128 sscanf(l + nextReadPos, " %d #%6s %16s %16s", &framesToFadeCount, color, clockPos1,
1129 clockPos2);
1130 } else {
1131 sscanf(l + nextReadPos, " #%6s %16s %16s", color, clockPos1, clockPos2);
1132 }
1133 // SLOGD("> type=%c, count=%d, pause=%d, path=%s, framesToFadeCount=%d, color=%s, "
1134 // "clockPos1=%s, clockPos2=%s",
1135 // pathType, count, pause, path, framesToFadeCount, color, clockPos1, clockPos2);
Mathias Agopiana8826d62009-10-01 03:10:14 -07001136 Animation::Part part;
Shan Huang79d079e2021-08-09 21:09:11 +00001137 if (path == dynamicColoringPartName) {
1138 // Part is specified to use dynamic coloring.
1139 part.useDynamicColoring = true;
1140 part.postDynamicColoring = false;
1141 postDynamicColoring = true;
1142 } else {
1143 // Part does not use dynamic coloring.
1144 part.useDynamicColoring = false;
1145 part.postDynamicColoring = postDynamicColoring;
1146 }
Kevin Hesterd3782b22012-04-26 10:38:55 -07001147 part.playUntilComplete = pathType == 'c';
Yegor Malyshev6925e1b2020-09-10 16:42:20 +02001148 part.framesToFadeCount = framesToFadeCount;
Mathias Agopiana8826d62009-10-01 03:10:14 -07001149 part.count = count;
1150 part.pause = pause;
1151 part.path = path;
Yi Kong911ac232019-03-24 01:49:02 -07001152 part.audioData = nullptr;
1153 part.animation = nullptr;
Jesse Hall083b84c2014-09-22 10:51:09 -07001154 if (!parseColor(color, part.backgroundColor)) {
Wei Wang159a4102018-10-23 23:15:58 -07001155 SLOGE("> invalid color '#%s'", color);
Jesse Hall083b84c2014-09-22 10:51:09 -07001156 part.backgroundColor[0] = 0.0f;
1157 part.backgroundColor[1] = 0.0f;
1158 part.backgroundColor[2] = 0.0f;
1159 }
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -07001160 parsePosition(clockPos1, clockPos2, &part.clockPosX, &part.clockPosY);
Mathias Agopiana8826d62009-10-01 03:10:14 -07001161 animation.parts.add(part);
1162 }
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001163 else if (strcmp(l, "$SYSTEM") == 0) {
Wei Wang159a4102018-10-23 23:15:58 -07001164 // SLOGD("> SYSTEM");
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001165 Animation::Part part;
1166 part.playUntilComplete = false;
Yegor Malyshev6925e1b2020-09-10 16:42:20 +02001167 part.framesToFadeCount = 0;
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001168 part.count = 1;
1169 part.pause = 0;
Yi Kong911ac232019-03-24 01:49:02 -07001170 part.audioData = nullptr;
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001171 part.animation = loadAnimation(String8(SYSTEM_BOOTANIMATION_FILE));
Yi Kong911ac232019-03-24 01:49:02 -07001172 if (part.animation != nullptr)
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001173 animation.parts.add(part);
1174 }
Mathias Agopiana8826d62009-10-01 03:10:14 -07001175 s = ++endl;
1176 }
1177
Shan Huang79d079e2021-08-09 21:09:11 +00001178 for (int i = 0; i < DYNAMIC_COLOR_COUNT; i++) {
1179 parseColorDecimalString(
1180 android::base::GetProperty("persist.bootanim.color" + std::to_string(i + 1), ""),
1181 animation.endColors[i], animation.startColors[i]);
1182 }
1183
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001184 return true;
1185}
1186
Marin Shalamanov9070c052020-05-18 19:05:17 +02001187bool BootAnimation::preloadZip(Animation& animation) {
Mathias Agopiana8826d62009-10-01 03:10:14 -07001188 // read all the data structures
1189 const size_t pcount = animation.parts.size();
Yi Kong911ac232019-03-24 01:49:02 -07001190 void *cookie = nullptr;
Geoffrey Pitschdd214a72016-06-27 17:14:30 -04001191 ZipFileRO* zip = animation.zip;
1192 if (!zip->startIteration(&cookie)) {
Narayan Kamathafd31e02013-12-03 13:16:03 +00001193 return false;
1194 }
1195
1196 ZipEntryRO entry;
1197 char name[ANIM_ENTRY_NAME_MAX];
Yi Kong911ac232019-03-24 01:49:02 -07001198 while ((entry = zip->nextEntry(cookie)) != nullptr) {
Geoffrey Pitschdd214a72016-06-27 17:14:30 -04001199 const int foundEntryName = zip->getEntryFileName(entry, name, ANIM_ENTRY_NAME_MAX);
Narayan Kamathafd31e02013-12-03 13:16:03 +00001200 if (foundEntryName > ANIM_ENTRY_NAME_MAX || foundEntryName == -1) {
Wei Wang159a4102018-10-23 23:15:58 -07001201 SLOGE("Error fetching entry file name");
Narayan Kamathafd31e02013-12-03 13:16:03 +00001202 continue;
1203 }
1204
1205 const String8 entryName(name);
1206 const String8 path(entryName.getPathDir());
1207 const String8 leaf(entryName.getPathLeaf());
1208 if (leaf.size() > 0) {
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -07001209 if (entryName == CLOCK_FONT_ZIP_NAME) {
1210 FileMap* map = zip->createEntryFileMap(entry);
1211 if (map) {
1212 animation.clockFont.map = map;
1213 }
1214 continue;
1215 }
1216
Nicolas Geoffray41fffb42020-12-15 10:50:29 +00001217 if (entryName == PROGRESS_FONT_ZIP_NAME) {
1218 FileMap* map = zip->createEntryFileMap(entry);
1219 if (map) {
1220 animation.progressFont.map = map;
1221 }
1222 continue;
1223 }
1224
Geoffrey Pitschdd214a72016-06-27 17:14:30 -04001225 for (size_t j = 0; j < pcount; j++) {
Narayan Kamathafd31e02013-12-03 13:16:03 +00001226 if (path == animation.parts[j].path) {
Narayan Kamath4600dd02015-06-16 12:02:57 +01001227 uint16_t method;
Narayan Kamathafd31e02013-12-03 13:16:03 +00001228 // supports only stored png files
Yi Kong911ac232019-03-24 01:49:02 -07001229 if (zip->getEntryInfo(entry, &method, nullptr, nullptr, nullptr, nullptr, nullptr)) {
Narayan Kamathafd31e02013-12-03 13:16:03 +00001230 if (method == ZipFileRO::kCompressStored) {
Geoffrey Pitschdd214a72016-06-27 17:14:30 -04001231 FileMap* map = zip->createEntryFileMap(entry);
Narayan Kamathafd31e02013-12-03 13:16:03 +00001232 if (map) {
Narayan Kamathafd31e02013-12-03 13:16:03 +00001233 Animation::Part& part(animation.parts.editItemAt(j));
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -07001234 if (leaf == "audio.wav") {
1235 // a part may have at most one audio file
Geoffrey Pitschd6d9a1d2016-06-08 00:38:58 -07001236 part.audioData = (uint8_t *)map->getDataPtr();
1237 part.audioLength = map->getDataLength();
Geoffrey Pitschdd214a72016-06-27 17:14:30 -04001238 } else if (leaf == "trim.txt") {
1239 part.trimData.setTo((char const*)map->getDataPtr(),
1240 map->getDataLength());
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -07001241 } else {
1242 Animation::Frame frame;
1243 frame.name = leaf;
1244 frame.map = map;
Geoffrey Pitschdd214a72016-06-27 17:14:30 -04001245 frame.trimWidth = animation.width;
1246 frame.trimHeight = animation.height;
1247 frame.trimX = 0;
1248 frame.trimY = 0;
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -07001249 part.frames.add(frame);
1250 }
Mathias Agopiana8826d62009-10-01 03:10:14 -07001251 }
Geoffrey Pitschd6d9a1d2016-06-08 00:38:58 -07001252 } else {
Wei Wang159a4102018-10-23 23:15:58 -07001253 SLOGE("bootanimation.zip is compressed; must be only stored");
Mathias Agopiana8826d62009-10-01 03:10:14 -07001254 }
1255 }
1256 }
1257 }
1258 }
1259 }
1260
Geoffrey Pitschdd214a72016-06-27 17:14:30 -04001261 // If there is trimData present, override the positioning defaults.
1262 for (Animation::Part& part : animation.parts) {
1263 const char* trimDataStr = part.trimData.string();
1264 for (size_t frameIdx = 0; frameIdx < part.frames.size(); frameIdx++) {
1265 const char* endl = strstr(trimDataStr, "\n");
1266 // No more trimData for this part.
Yi Kong911ac232019-03-24 01:49:02 -07001267 if (endl == nullptr) {
Geoffrey Pitschdd214a72016-06-27 17:14:30 -04001268 break;
1269 }
1270 String8 line(trimDataStr, endl - trimDataStr);
1271 const char* lineStr = line.string();
1272 trimDataStr = ++endl;
1273 int width = 0, height = 0, x = 0, y = 0;
1274 if (sscanf(lineStr, "%dx%d+%d+%d", &width, &height, &x, &y) == 4) {
1275 Animation::Frame& frame(part.frames.editItemAt(frameIdx));
1276 frame.trimWidth = width;
1277 frame.trimHeight = height;
1278 frame.trimX = x;
1279 frame.trimY = y;
1280 } else {
Wei Wang159a4102018-10-23 23:15:58 -07001281 SLOGE("Error parsing trim.txt, line: %s", lineStr);
Geoffrey Pitschdd214a72016-06-27 17:14:30 -04001282 break;
1283 }
1284 }
1285 }
1286
1287 zip->endIteration(cookie);
Narayan Kamathafd31e02013-12-03 13:16:03 +00001288
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001289 return true;
1290}
1291
Marin Shalamanov9070c052020-05-18 19:05:17 +02001292bool BootAnimation::movie() {
Huihong Luo50a2f362018-08-11 09:27:57 -07001293 if (mAnimation == nullptr) {
1294 mAnimation = loadAnimation(mZipFileName);
1295 }
1296
1297 if (mAnimation == nullptr)
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001298 return false;
1299
Huihong Luo50a2f362018-08-11 09:27:57 -07001300 // mCallbacks->init() may get called recursively,
1301 // this loop is needed to get the same results
1302 for (const Animation::Part& part : mAnimation->parts) {
1303 if (part.animation != nullptr) {
1304 mCallbacks->init(part.animation->parts);
1305 }
1306 }
1307 mCallbacks->init(mAnimation->parts);
1308
Damien Bargiacchi97480862016-03-29 14:55:55 -07001309 bool anyPartHasClock = false;
Huihong Luo50a2f362018-08-11 09:27:57 -07001310 for (size_t i=0; i < mAnimation->parts.size(); i++) {
1311 if(validClock(mAnimation->parts[i])) {
Damien Bargiacchi97480862016-03-29 14:55:55 -07001312 anyPartHasClock = true;
1313 break;
1314 }
1315 }
1316 if (!anyPartHasClock) {
1317 mClockEnabled = false;
1318 }
1319
Sai Kiran Korwar27167492015-07-07 20:00:06 +05301320 // Check if npot textures are supported
1321 mUseNpotTextures = false;
1322 String8 gl_extensions;
1323 const char* exts = reinterpret_cast<const char*>(glGetString(GL_EXTENSIONS));
1324 if (!exts) {
1325 glGetError();
1326 } else {
1327 gl_extensions.setTo(exts);
1328 if ((gl_extensions.find("GL_ARB_texture_non_power_of_two") != -1) ||
1329 (gl_extensions.find("GL_OES_texture_npot") != -1)) {
1330 mUseNpotTextures = true;
1331 }
1332 }
1333
Damien Bargiacchia704b7d2016-02-16 16:55:49 -08001334 // Blend required to draw time on top of animation frames.
1335 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
Mathias Agopiana8826d62009-10-01 03:10:14 -07001336 glDisable(GL_DITHER);
1337 glDisable(GL_SCISSOR_TEST);
1338 glDisable(GL_BLEND);
Mathias Agopiana8826d62009-10-01 03:10:14 -07001339
Greg Kaiser2ded8002021-07-19 17:03:27 +00001340 glEnable(GL_TEXTURE_2D);
Lucas Dupin8eedc022021-07-29 21:49:07 +00001341 glBindTexture(GL_TEXTURE_2D, 0);
1342 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
1343 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
1344 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
1345 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -07001346 bool clockFontInitialized = false;
Damien Bargiacchia704b7d2016-02-16 16:55:49 -08001347 if (mClockEnabled) {
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -07001348 clockFontInitialized =
Huihong Luo50a2f362018-08-11 09:27:57 -07001349 (initFont(&mAnimation->clockFont, CLOCK_FONT_ASSET) == NO_ERROR);
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -07001350 mClockEnabled = clockFontInitialized;
Damien Bargiacchia704b7d2016-02-16 16:55:49 -08001351 }
1352
Nicolas Geoffray41fffb42020-12-15 10:50:29 +00001353 initFont(&mAnimation->progressFont, PROGRESS_FONT_ASSET);
1354
Damien Bargiacchi97480862016-03-29 14:55:55 -07001355 if (mClockEnabled && !updateIsTimeAccurate()) {
1356 mTimeCheckThread = new TimeCheckThread(this);
1357 mTimeCheckThread->run("BootAnimation::TimeCheckThread", PRIORITY_NORMAL);
1358 }
1359
Huihong Luo50a2f362018-08-11 09:27:57 -07001360 playAnimation(*mAnimation);
Damien Bargiacchi97480862016-03-29 14:55:55 -07001361
Geoffrey Pitscha9173532016-07-19 14:56:39 -04001362 if (mTimeCheckThread != nullptr) {
Damien Bargiacchi97480862016-03-29 14:55:55 -07001363 mTimeCheckThread->requestExit();
Geoffrey Pitscha9173532016-07-19 14:56:39 -04001364 mTimeCheckThread = nullptr;
1365 }
1366
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -07001367 if (clockFontInitialized) {
Huihong Luo50a2f362018-08-11 09:27:57 -07001368 glDeleteTextures(1, &mAnimation->clockFont.texture.name);
Andriy Naborskyy815e51d2016-03-24 16:43:34 -07001369 }
1370
Huihong Luo50a2f362018-08-11 09:27:57 -07001371 releaseAnimation(mAnimation);
1372 mAnimation = nullptr;
Damien Bargiacchi1a8a2132018-07-24 15:20:26 -07001373
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001374 return false;
1375}
1376
Nicolas Geoffraya1298752021-04-20 15:15:06 +01001377bool BootAnimation::shouldStopPlayingPart(const Animation::Part& part,
1378 const int fadedFramesCount,
1379 const int lastDisplayedProgress) {
Yegor Malyshev6925e1b2020-09-10 16:42:20 +02001380 // 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 +01001381 return exitPending() && !part.playUntilComplete && fadedFramesCount >= part.framesToFadeCount &&
1382 (lastDisplayedProgress == 0 || lastDisplayedProgress == 100);
Yegor Malyshev6925e1b2020-09-10 16:42:20 +02001383}
1384
Lucas Dupin8eedc022021-07-29 21:49:07 +00001385// Linear mapping from range <a1, a2> to range <b1, b2>
1386float mapLinear(float x, float a1, float a2, float b1, float b2) {
1387 return b1 + ( x - a1 ) * ( b2 - b1 ) / ( a2 - a1 );
1388}
1389
1390void BootAnimation::drawTexturedQuad(float xStart, float yStart, float width, float height) {
1391 // Map coordinates from screen space to world space.
1392 float x0 = mapLinear(xStart, 0, mWidth, -1, 1);
1393 float y0 = mapLinear(yStart, 0, mHeight, -1, 1);
1394 float x1 = mapLinear(xStart + width, 0, mWidth, -1, 1);
1395 float y1 = mapLinear(yStart + height, 0, mHeight, -1, 1);
1396 // Update quad vertex positions.
1397 quadPositions[0] = x0;
1398 quadPositions[1] = y0;
1399 quadPositions[2] = x1;
1400 quadPositions[3] = y0;
1401 quadPositions[4] = x1;
1402 quadPositions[5] = y1;
1403 quadPositions[6] = x1;
1404 quadPositions[7] = y1;
1405 quadPositions[8] = x0;
1406 quadPositions[9] = y1;
1407 quadPositions[10] = x0;
1408 quadPositions[11] = y0;
1409 glDrawArrays(GL_TRIANGLES, 0,
1410 sizeof(quadPositions) / sizeof(quadPositions[0]) / 2);
1411}
1412
Marin Shalamanov9070c052020-05-18 19:05:17 +02001413bool BootAnimation::playAnimation(const Animation& animation) {
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001414 const size_t pcount = animation.parts.size();
Mathias Agopiana8826d62009-10-01 03:10:14 -07001415 nsecs_t frameDuration = s2ns(1) / animation.fps;
Mathias Agopian9f3020d2009-11-06 16:30:18 -08001416
Wei Wang159a4102018-10-23 23:15:58 -07001417 SLOGD("%sAnimationShownTiming start time: %" PRId64 "ms", mShuttingDown ? "Shutdown" : "Boot",
Keun-young Parkd1794cd2017-04-04 12:21:19 -07001418 elapsedRealtime());
Yegor Malyshev6925e1b2020-09-10 16:42:20 +02001419
1420 int fadedFramesCount = 0;
Nicolas Geoffray41fffb42020-12-15 10:50:29 +00001421 int lastDisplayedProgress = 0;
Narayan Kamathafd31e02013-12-03 13:16:03 +00001422 for (size_t i=0 ; i<pcount ; i++) {
Mathias Agopiana8826d62009-10-01 03:10:14 -07001423 const Animation::Part& part(animation.parts[i]);
1424 const size_t fcount = part.frames.size();
Mathias Agopiana8826d62009-10-01 03:10:14 -07001425
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001426 // Handle animation package
Yi Kong911ac232019-03-24 01:49:02 -07001427 if (part.animation != nullptr) {
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001428 playAnimation(*part.animation);
1429 if (exitPending())
1430 break;
1431 continue; //to next part
1432 }
1433
Yegor Malyshev6925e1b2020-09-10 16:42:20 +02001434 // process the part not only while the count allows but also if already fading
1435 for (int r=0 ; !part.count || r<part.count || fadedFramesCount > 0 ; r++) {
Nicolas Geoffraya1298752021-04-20 15:15:06 +01001436 if (shouldStopPlayingPart(part, fadedFramesCount, lastDisplayedProgress)) break;
Kevin Hesterd3782b22012-04-26 10:38:55 -07001437
Ed Coyne7464ac92017-06-08 12:26:48 -07001438 mCallbacks->playPart(i, part, r);
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -07001439
Jesse Hall083b84c2014-09-22 10:51:09 -07001440 glClearColor(
1441 part.backgroundColor[0],
1442 part.backgroundColor[1],
1443 part.backgroundColor[2],
1444 1.0f);
1445
Nicolas Geoffray41fffb42020-12-15 10:50:29 +00001446 // For the last animation, if we have progress indicator from
1447 // the system, display it.
1448 int currentProgress = android::base::GetIntProperty(PROGRESS_PROP_NAME, 0);
1449 bool displayProgress = animation.progressEnabled &&
1450 (i == (pcount -1)) && currentProgress != 0;
1451
Yegor Malyshev6925e1b2020-09-10 16:42:20 +02001452 for (size_t j=0 ; j<fcount ; j++) {
Nicolas Geoffraya1298752021-04-20 15:15:06 +01001453 if (shouldStopPlayingPart(part, fadedFramesCount, lastDisplayedProgress)) break;
Yegor Malyshev6925e1b2020-09-10 16:42:20 +02001454
Shan Huang79d079e2021-08-09 21:09:11 +00001455 // Color progress is
1456 // - the normalized animation progress between [0, 1] for the dynamic coloring part,
1457 // - 0 for parts that come before,
1458 // - 1 for parts that come after.
1459 float colorProgress = part.useDynamicColoring
1460 ? (float)j / fcount
1461 : (part.postDynamicColoring ? 1 : 0);
1462
Marin Shalamanov9070c052020-05-18 19:05:17 +02001463 processDisplayEvents();
1464
1465 const int animationX = (mWidth - animation.width) / 2;
1466 const int animationY = (mHeight - animation.height) / 2;
1467
Mathias Agopiana8826d62009-10-01 03:10:14 -07001468 const Animation::Frame& frame(part.frames[j]);
Mathias Agopiandb7dd2a2012-05-12 15:08:21 -07001469 nsecs_t lastFrame = systemTime();
Mathias Agopiana8826d62009-10-01 03:10:14 -07001470
1471 if (r > 0) {
1472 glBindTexture(GL_TEXTURE_2D, frame.tid);
1473 } else {
Lucas Dupin8eedc022021-07-29 21:49:07 +00001474 glGenTextures(1, &frame.tid);
1475 glBindTexture(GL_TEXTURE_2D, frame.tid);
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -07001476 int w, h;
1477 initTexture(frame.map, &w, &h);
Mathias Agopiana8826d62009-10-01 03:10:14 -07001478 }
1479
Geoffrey Pitschdd214a72016-06-27 17:14:30 -04001480 const int xc = animationX + frame.trimX;
1481 const int yc = animationY + frame.trimY;
Shan Huang79d079e2021-08-09 21:09:11 +00001482 glClear(GL_COLOR_BUFFER_BIT);
Geoffrey Pitschdd214a72016-06-27 17:14:30 -04001483 // specify the y center as ceiling((mHeight - frame.trimHeight) / 2)
1484 // which is equivalent to mHeight - (yc + frame.trimHeight)
Yegor Malyshev6925e1b2020-09-10 16:42:20 +02001485 const int frameDrawY = mHeight - (yc + frame.trimHeight);
Yegor Malyshev6925e1b2020-09-10 16:42:20 +02001486
Lucas Dupin8eedc022021-07-29 21:49:07 +00001487 float fade = 0;
Yegor Malyshev6925e1b2020-09-10 16:42:20 +02001488 // if the part hasn't been stopped yet then continue fading if necessary
1489 if (exitPending() && part.hasFadingPhase()) {
Lucas Dupin8eedc022021-07-29 21:49:07 +00001490 fade = static_cast<float>(++fadedFramesCount) / part.framesToFadeCount;
Yegor Malyshev6925e1b2020-09-10 16:42:20 +02001491 if (fadedFramesCount >= part.framesToFadeCount) {
1492 fadedFramesCount = MAX_FADED_FRAMES_COUNT; // no more fading
1493 }
1494 }
Lucas Dupin8eedc022021-07-29 21:49:07 +00001495 glUseProgram(mImageShader);
1496 glUniform1i(mImageTextureLocation, 0);
1497 glUniform1f(mImageFadeLocation, fade);
Shan Huang79d079e2021-08-09 21:09:11 +00001498 glUniform1f(mImageColorProgressLocation, colorProgress);
Lucas Dupin8eedc022021-07-29 21:49:07 +00001499 glEnable(GL_BLEND);
1500 drawTexturedQuad(xc, frameDrawY, frame.trimWidth, frame.trimHeight);
1501 glDisable(GL_BLEND);
Yegor Malyshev6925e1b2020-09-10 16:42:20 +02001502
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -07001503 if (mClockEnabled && mTimeIsAccurate && validClock(part)) {
1504 drawClock(animation.clockFont, part.clockPosX, part.clockPosY);
Damien Bargiacchia704b7d2016-02-16 16:55:49 -08001505 }
Yegor Malyshev6925e1b2020-09-10 16:42:20 +02001506
Nicolas Geoffray41fffb42020-12-15 10:50:29 +00001507 if (displayProgress) {
1508 int newProgress = android::base::GetIntProperty(PROGRESS_PROP_NAME, 0);
1509 // In case the new progress jumped suddenly, still show an
1510 // increment of 1.
1511 if (lastDisplayedProgress != 100) {
1512 // Artificially sleep 1/10th a second to slow down the animation.
1513 usleep(100000);
1514 if (lastDisplayedProgress < newProgress) {
1515 lastDisplayedProgress++;
1516 }
1517 }
1518 // Put the progress percentage right below the animation.
1519 int posY = animation.height / 3;
1520 int posX = TEXT_CENTER_VALUE;
1521 drawProgress(lastDisplayedProgress, animation.progressFont, posX, posY);
1522 }
1523
Adrian Roos9ee5dff2018-08-22 20:19:49 +02001524 handleViewport(frameDuration);
Damien Bargiacchia704b7d2016-02-16 16:55:49 -08001525
Mathias Agopiana8826d62009-10-01 03:10:14 -07001526 eglSwapBuffers(mDisplay, mSurface);
1527
1528 nsecs_t now = systemTime();
1529 nsecs_t delay = frameDuration - (now - lastFrame);
Wei Wang159a4102018-10-23 23:15:58 -07001530 //SLOGD("%lld, %lld", ns2ms(now - lastFrame), ns2ms(delay));
Mathias Agopiana8826d62009-10-01 03:10:14 -07001531 lastFrame = now;
Mathias Agopiandb7dd2a2012-05-12 15:08:21 -07001532
1533 if (delay > 0) {
1534 struct timespec spec;
1535 spec.tv_sec = (now + delay) / 1000000000;
1536 spec.tv_nsec = (now + delay) % 1000000000;
1537 int err;
1538 do {
Yi Kong911ac232019-03-24 01:49:02 -07001539 err = clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &spec, nullptr);
Mathias Agopiandb7dd2a2012-05-12 15:08:21 -07001540 } while (err<0 && errno == EINTR);
1541 }
Kevin Hesterd3782b22012-04-26 10:38:55 -07001542
1543 checkExit();
Mathias Agopiana8826d62009-10-01 03:10:14 -07001544 }
Kevin Hesterd3782b22012-04-26 10:38:55 -07001545
Mathias Agopiana8826d62009-10-01 03:10:14 -07001546 usleep(part.pause * ns2us(frameDuration));
Kevin Hesterd3782b22012-04-26 10:38:55 -07001547
Yegor Malyshev6925e1b2020-09-10 16:42:20 +02001548 if (exitPending() && !part.count && mCurrentInset >= mTargetInset &&
1549 !part.hasFadingPhase()) {
Nicolas Geoffraya1298752021-04-20 15:15:06 +01001550 if (lastDisplayedProgress != 0 && lastDisplayedProgress != 100) {
1551 android::base::SetProperty(PROGRESS_PROP_NAME, "100");
1552 continue;
1553 }
Yegor Malyshev6925e1b2020-09-10 16:42:20 +02001554 break; // exit the infinite non-fading part when it has been played at least once
1555 }
Mathias Agopiana8826d62009-10-01 03:10:14 -07001556 }
Geoffrey Pitsch2fb30fb2016-07-06 16:16:20 -04001557 }
1558
1559 // Free textures created for looping parts now that the animation is done.
1560 for (const Animation::Part& part : animation.parts) {
Mathias Agopiana8826d62009-10-01 03:10:14 -07001561 if (part.count != 1) {
Geoffrey Pitsch2fb30fb2016-07-06 16:16:20 -04001562 const size_t fcount = part.frames.size();
1563 for (size_t j = 0; j < fcount; j++) {
Mathias Agopiana8826d62009-10-01 03:10:14 -07001564 const Animation::Frame& frame(part.frames[j]);
1565 glDeleteTextures(1, &frame.tid);
1566 }
1567 }
1568 }
Geoffrey Pitschd6d9a1d2016-06-08 00:38:58 -07001569
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001570 return true;
Mathias Agopiana8826d62009-10-01 03:10:14 -07001571}
1572
Marin Shalamanov9070c052020-05-18 19:05:17 +02001573void BootAnimation::processDisplayEvents() {
1574 // This will poll mDisplayEventReceiver and if there are new events it'll call
1575 // displayEventCallback synchronously.
1576 mLooper->pollOnce(0);
1577}
1578
Adrian Roos9ee5dff2018-08-22 20:19:49 +02001579void BootAnimation::handleViewport(nsecs_t timestep) {
1580 if (mShuttingDown || !mFlingerSurfaceControl || mTargetInset == 0) {
1581 return;
1582 }
1583 if (mTargetInset < 0) {
1584 // Poll the amount for the top display inset. This will return -1 until persistent properties
1585 // have been loaded.
1586 mTargetInset = android::base::GetIntProperty("persist.sys.displayinset.top",
1587 -1 /* default */, -1 /* min */, mHeight / 2 /* max */);
1588 }
1589 if (mTargetInset <= 0) {
1590 return;
1591 }
1592
1593 if (mCurrentInset < mTargetInset) {
1594 // After the device boots, the inset will effectively be cropped away. We animate this here.
1595 float fraction = static_cast<float>(mCurrentInset) / mTargetInset;
1596 int interpolatedInset = (cosf((fraction + 1) * M_PI) / 2.0f + 0.5f) * mTargetInset;
1597
1598 SurfaceComposerClient::Transaction()
1599 .setCrop(mFlingerSurfaceControl, Rect(0, interpolatedInset, mWidth, mHeight))
1600 .apply();
1601 } else {
1602 // At the end of the animation, we switch to the viewport that DisplayManager will apply
1603 // later. This changes the coordinate system, and means we must move the surface up by
1604 // the inset amount.
Adrian Roos9ee5dff2018-08-22 20:19:49 +02001605 Rect layerStackRect(0, 0, mWidth, mHeight - mTargetInset);
1606 Rect displayRect(0, mTargetInset, mWidth, mHeight);
1607
1608 SurfaceComposerClient::Transaction t;
1609 t.setPosition(mFlingerSurfaceControl, 0, -mTargetInset)
1610 .setCrop(mFlingerSurfaceControl, Rect(0, mTargetInset, mWidth, mHeight));
Dominik Laskowski2a3d9aa2019-11-18 20:26:39 -08001611 t.setDisplayProjection(mDisplayToken, ui::ROTATION_0, layerStackRect, displayRect);
Adrian Roos9ee5dff2018-08-22 20:19:49 +02001612 t.apply();
1613
1614 mTargetInset = mCurrentInset = 0;
1615 }
1616
1617 int delta = timestep * mTargetInset / ms2ns(200);
1618 mCurrentInset += delta;
1619}
1620
Marin Shalamanov9070c052020-05-18 19:05:17 +02001621void BootAnimation::releaseAnimation(Animation* animation) const {
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001622 for (Vector<Animation::Part>::iterator it = animation->parts.begin(),
1623 e = animation->parts.end(); it != e; ++it) {
1624 if (it->animation)
1625 releaseAnimation(it->animation);
1626 }
1627 if (animation->zip)
1628 delete animation->zip;
1629 delete animation;
1630}
1631
Marin Shalamanov9070c052020-05-18 19:05:17 +02001632BootAnimation::Animation* BootAnimation::loadAnimation(const String8& fn) {
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001633 if (mLoadedFiles.indexOf(fn) >= 0) {
Wei Wang159a4102018-10-23 23:15:58 -07001634 SLOGE("File \"%s\" is already loaded. Cyclic ref is not allowed",
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001635 fn.string());
Yi Kong911ac232019-03-24 01:49:02 -07001636 return nullptr;
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001637 }
1638 ZipFileRO *zip = ZipFileRO::open(fn);
Yi Kong911ac232019-03-24 01:49:02 -07001639 if (zip == nullptr) {
Wei Wang159a4102018-10-23 23:15:58 -07001640 SLOGE("Failed to open animation zip \"%s\": %s",
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001641 fn.string(), strerror(errno));
Yi Kong911ac232019-03-24 01:49:02 -07001642 return nullptr;
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001643 }
1644
1645 Animation *animation = new Animation;
1646 animation->fileName = fn;
1647 animation->zip = zip;
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -07001648 animation->clockFont.map = nullptr;
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001649 mLoadedFiles.add(animation->fileName);
1650
1651 parseAnimationDesc(*animation);
Geoffrey Pitscha91a2d72016-07-12 14:46:19 -04001652 if (!preloadZip(*animation)) {
Greg Kaiser7426ec82019-09-11 14:34:27 -07001653 releaseAnimation(animation);
Yi Kong911ac232019-03-24 01:49:02 -07001654 return nullptr;
Geoffrey Pitscha91a2d72016-07-12 14:46:19 -04001655 }
1656
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001657 mLoadedFiles.remove(fn);
1658 return animation;
1659}
Damien Bargiacchi97480862016-03-29 14:55:55 -07001660
1661bool BootAnimation::updateIsTimeAccurate() {
1662 static constexpr long long MAX_TIME_IN_PAST = 60000LL * 60LL * 24LL * 30LL; // 30 days
1663 static constexpr long long MAX_TIME_IN_FUTURE = 60000LL * 90LL; // 90 minutes
1664
1665 if (mTimeIsAccurate) {
1666 return true;
1667 }
Keun-young Parkb5938422017-03-23 13:46:24 -07001668 if (mShuttingDown) return true;
Damien Bargiacchi97480862016-03-29 14:55:55 -07001669 struct stat statResult;
Damien Bargiacchi9071db12016-10-28 17:38:22 -07001670
1671 if(stat(TIME_FORMAT_12_HOUR_FLAG_FILE_PATH, &statResult) == 0) {
1672 mTimeFormat12Hour = true;
1673 }
1674
Damien Bargiacchi97480862016-03-29 14:55:55 -07001675 if(stat(ACCURATE_TIME_FLAG_FILE_PATH, &statResult) == 0) {
1676 mTimeIsAccurate = true;
1677 return true;
1678 }
1679
1680 FILE* file = fopen(LAST_TIME_CHANGED_FILE_PATH, "r");
Yi Kong911ac232019-03-24 01:49:02 -07001681 if (file != nullptr) {
Damien Bargiacchi97480862016-03-29 14:55:55 -07001682 long long lastChangedTime = 0;
1683 fscanf(file, "%lld", &lastChangedTime);
1684 fclose(file);
1685 if (lastChangedTime > 0) {
1686 struct timespec now;
1687 clock_gettime(CLOCK_REALTIME, &now);
1688 // Match the Java timestamp format
1689 long long rtcNow = (now.tv_sec * 1000LL) + (now.tv_nsec / 1000000LL);
Damien Bargiacchi96762812016-07-12 15:53:40 -07001690 if (ACCURATE_TIME_EPOCH < rtcNow
1691 && lastChangedTime > (rtcNow - MAX_TIME_IN_PAST)
1692 && lastChangedTime < (rtcNow + MAX_TIME_IN_FUTURE)) {
Damien Bargiacchi97480862016-03-29 14:55:55 -07001693 mTimeIsAccurate = true;
1694 }
1695 }
1696 }
1697
1698 return mTimeIsAccurate;
1699}
1700
1701BootAnimation::TimeCheckThread::TimeCheckThread(BootAnimation* bootAnimation) : Thread(false),
1702 mInotifyFd(-1), mSystemWd(-1), mTimeWd(-1), mBootAnimation(bootAnimation) {}
1703
1704BootAnimation::TimeCheckThread::~TimeCheckThread() {
1705 // mInotifyFd may be -1 but that's ok since we're not at risk of attempting to close a valid FD.
1706 close(mInotifyFd);
1707}
1708
1709bool BootAnimation::TimeCheckThread::threadLoop() {
1710 bool shouldLoop = doThreadLoop() && !mBootAnimation->mTimeIsAccurate
1711 && mBootAnimation->mClockEnabled;
1712 if (!shouldLoop) {
1713 close(mInotifyFd);
1714 mInotifyFd = -1;
1715 }
1716 return shouldLoop;
1717}
1718
1719bool BootAnimation::TimeCheckThread::doThreadLoop() {
1720 static constexpr int BUFF_LEN (10 * (sizeof(struct inotify_event) + NAME_MAX + 1));
1721
1722 // Poll instead of doing a blocking read so the Thread can exit if requested.
1723 struct pollfd pfd = { mInotifyFd, POLLIN, 0 };
1724 ssize_t pollResult = poll(&pfd, 1, 1000);
1725
1726 if (pollResult == 0) {
1727 return true;
1728 } else if (pollResult < 0) {
Wei Wang159a4102018-10-23 23:15:58 -07001729 SLOGE("Could not poll inotify events");
Damien Bargiacchi97480862016-03-29 14:55:55 -07001730 return false;
1731 }
1732
1733 char buff[BUFF_LEN] __attribute__ ((aligned(__alignof__(struct inotify_event))));;
1734 ssize_t length = read(mInotifyFd, buff, BUFF_LEN);
1735 if (length == 0) {
1736 return true;
1737 } else if (length < 0) {
Wei Wang159a4102018-10-23 23:15:58 -07001738 SLOGE("Could not read inotify events");
Damien Bargiacchi97480862016-03-29 14:55:55 -07001739 return false;
1740 }
1741
1742 const struct inotify_event *event;
1743 for (char* ptr = buff; ptr < buff + length; ptr += sizeof(struct inotify_event) + event->len) {
1744 event = (const struct inotify_event *) ptr;
1745 if (event->wd == mSystemWd && strcmp(SYSTEM_TIME_DIR_NAME, event->name) == 0) {
1746 addTimeDirWatch();
1747 } else if (event->wd == mTimeWd && (strcmp(LAST_TIME_CHANGED_FILE_NAME, event->name) == 0
1748 || strcmp(ACCURATE_TIME_FLAG_FILE_NAME, event->name) == 0)) {
1749 return !mBootAnimation->updateIsTimeAccurate();
1750 }
1751 }
1752
1753 return true;
1754}
1755
1756void BootAnimation::TimeCheckThread::addTimeDirWatch() {
1757 mTimeWd = inotify_add_watch(mInotifyFd, SYSTEM_TIME_DIR_PATH,
1758 IN_CLOSE_WRITE | IN_MOVED_TO | IN_ATTRIB);
1759 if (mTimeWd > 0) {
1760 // No need to watch for the time directory to be created if it already exists
1761 inotify_rm_watch(mInotifyFd, mSystemWd);
1762 mSystemWd = -1;
1763 }
1764}
1765
1766status_t BootAnimation::TimeCheckThread::readyToRun() {
1767 mInotifyFd = inotify_init();
1768 if (mInotifyFd < 0) {
Wei Wang159a4102018-10-23 23:15:58 -07001769 SLOGE("Could not initialize inotify fd");
Damien Bargiacchi97480862016-03-29 14:55:55 -07001770 return NO_INIT;
1771 }
1772
1773 mSystemWd = inotify_add_watch(mInotifyFd, SYSTEM_DATA_DIR_PATH, IN_CREATE | IN_ATTRIB);
1774 if (mSystemWd < 0) {
1775 close(mInotifyFd);
1776 mInotifyFd = -1;
Dan Sandler562d46d2020-05-11 17:09:22 -04001777 SLOGE("Could not add watch for %s: %s", SYSTEM_DATA_DIR_PATH, strerror(errno));
Damien Bargiacchi97480862016-03-29 14:55:55 -07001778 return NO_INIT;
1779 }
1780
1781 addTimeDirWatch();
1782
1783 if (mBootAnimation->updateIsTimeAccurate()) {
1784 close(mInotifyFd);
1785 mInotifyFd = -1;
1786 return ALREADY_EXISTS;
1787 }
1788
1789 return NO_ERROR;
1790}
1791
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001792// ---------------------------------------------------------------------------
1793
Marin Shalamanov9070c052020-05-18 19:05:17 +02001794} // namespace android