blob: 929e63e35dcc87e235eb31b951a36fab0c542ad9 [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 Huang9f1123c2021-08-13 00:36:12 +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 Huang9f1123c2021-08-13 00:36:12 +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 Huang9f1123c2021-08-13 00:36:12 +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 Huang9f1123c2021-08-13 00:36:12 +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 Huang9f1123c2021-08-13 00:36:12 +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() {
Shan Huang9f1123c2021-08-13 00:36:12 +0000708 bool dynamicColoringEnabled = mAnimation != nullptr && mAnimation->dynamicColoringEnabled;
Lucas Dupin8eedc022021-07-29 21:49:07 +0000709 GLuint vertexShader = compileShader(GL_VERTEX_SHADER, (const GLchar *)VERTEX_SHADER_SOURCE);
710 GLuint imageFragmentShader =
Shan Huang9f1123c2021-08-13 00:36:12 +0000711 compileShader(GL_FRAGMENT_SHADER, dynamicColoringEnabled
712 ? (const GLchar *)IMAGE_FRAG_DYNAMIC_COLORING_SHADER_SOURCE
713 : (const GLchar *)IMAGE_FRAG_SHADER_SOURCE);
Lucas Dupin8eedc022021-07-29 21:49:07 +0000714 GLuint textFragmentShader =
715 compileShader(GL_FRAGMENT_SHADER, (const GLchar *)TEXT_FRAG_SHADER_SOURCE);
716
717 // Initialize image shader.
718 mImageShader = linkShader(vertexShader, imageFragmentShader);
719 GLint positionLocation = glGetAttribLocation(mImageShader, A_POSITION);
720 GLint uvLocation = glGetAttribLocation(mImageShader, A_UV);
721 mImageTextureLocation = glGetUniformLocation(mImageShader, U_TEXTURE);
722 mImageFadeLocation = glGetUniformLocation(mImageShader, U_FADE);
723 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 Huang9f1123c2021-08-13 00:36:12 +0000728 if (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 mImageColorProgressLocation = glGetUniformLocation(mImageShader, U_COLOR_PROGRESS);
742 }
743
Lucas Dupin8eedc022021-07-29 21:49:07 +0000744 // Initialize text shader.
745 mTextShader = linkShader(vertexShader, textFragmentShader);
746 positionLocation = glGetAttribLocation(mTextShader, A_POSITION);
747 uvLocation = glGetAttribLocation(mTextShader, A_UV);
748 mTextTextureLocation = glGetUniformLocation(mTextShader, U_TEXTURE);
749 mTextCropAreaLocation = glGetUniformLocation(mTextShader, U_CROP_AREA);
750 glEnableVertexAttribArray(positionLocation);
751 glVertexAttribPointer(positionLocation, 2, GL_FLOAT, GL_FALSE, 0, quadPositions);
752 glVertexAttribPointer(uvLocation, 2, GL_FLOAT, GL_FALSE, 0, quadUVs);
753 glEnableVertexAttribArray(uvLocation);
754}
755
Marin Shalamanov9070c052020-05-18 19:05:17 +0200756bool BootAnimation::threadLoop() {
757 bool result;
Lucas Dupin8eedc022021-07-29 21:49:07 +0000758 initShaders();
759
Narayan Kamathafd31e02013-12-03 13:16:03 +0000760 // We have no bootanimation file, so we use the stock android logo
761 // animation.
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700762 if (mZipFileName.isEmpty()) {
Marin Shalamanov9070c052020-05-18 19:05:17 +0200763 result = android();
Mathias Agopiana8826d62009-10-01 03:10:14 -0700764 } else {
Marin Shalamanov9070c052020-05-18 19:05:17 +0200765 result = movie();
Mathias Agopiana8826d62009-10-01 03:10:14 -0700766 }
767
Philip Junker60600562020-11-25 18:16:16 +0100768 mCallbacks->shutdown();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800769 eglMakeCurrent(mDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
770 eglDestroyContext(mDisplay, mContext);
771 eglDestroySurface(mDisplay, mSurface);
Mathias Agopian6cf0db22009-04-17 19:36:26 -0700772 mFlingerSurface.clear();
Mathias Agopian17f638b2009-04-16 20:04:08 -0700773 mFlingerSurfaceControl.clear();
Mathias Agopian627e7b52009-05-21 19:21:59 -0700774 eglTerminate(mDisplay);
Sai Kiran Korwar3eee9fb2015-06-16 17:13:35 +0530775 eglReleaseThread();
Mathias Agopian627e7b52009-05-21 19:21:59 -0700776 IPCThreadState::self()->stopProcess();
Marin Shalamanov9070c052020-05-18 19:05:17 +0200777 return result;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800778}
779
Marin Shalamanov9070c052020-05-18 19:05:17 +0200780bool BootAnimation::android() {
Lucas Dupin8eedc022021-07-29 21:49:07 +0000781 glActiveTexture(GL_TEXTURE0);
782
Wei Wang159a4102018-10-23 23:15:58 -0700783 SLOGD("%sAnimationShownTiming start time: %" PRId64 "ms", mShuttingDown ? "Shutdown" : "Boot",
Keun-young Parkd1794cd2017-04-04 12:21:19 -0700784 elapsedRealtime());
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700785 initTexture(&mAndroid[0], mAssets, "images/android-logo-mask.png");
786 initTexture(&mAndroid[1], mAssets, "images/android-logo-shine.png");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800787
Ed Coyne7464ac92017-06-08 12:26:48 -0700788 mCallbacks->init({});
789
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800790 // clear screen
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700791 glDisable(GL_DITHER);
792 glDisable(GL_SCISSOR_TEST);
Mathias Agopian59f19e42011-05-06 19:22:12 -0700793 glClearColor(0,0,0,1);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800794 glClear(GL_COLOR_BUFFER_BIT);
795 eglSwapBuffers(mDisplay, mSurface);
796
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700797 // Blend state
798 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700799
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800800 const nsecs_t startTime = systemTime();
801 do {
Marin Shalamanov9070c052020-05-18 19:05:17 +0200802 processDisplayEvents();
803 const GLint xc = (mWidth - mAndroid[0].w) / 2;
804 const GLint yc = (mHeight - mAndroid[0].h) / 2;
805 const Rect updateRect(xc, yc, xc + mAndroid[0].w, yc + mAndroid[0].h);
806 glScissor(updateRect.left, mHeight - updateRect.bottom, updateRect.width(),
807 updateRect.height());
808
Mathias Agopian13796652009-03-24 22:49:21 -0700809 nsecs_t now = systemTime();
810 double time = now - startTime;
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700811 float t = 4.0f * float(time / us2ns(16667)) / mAndroid[1].w;
812 GLint offset = (1 - (t - floorf(t))) * mAndroid[1].w;
813 GLint x = xc - offset;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800814
Mathias Agopian81668642009-07-28 11:41:30 -0700815 glDisable(GL_SCISSOR_TEST);
816 glClear(GL_COLOR_BUFFER_BIT);
817
818 glEnable(GL_SCISSOR_TEST);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800819 glDisable(GL_BLEND);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800820 glBindTexture(GL_TEXTURE_2D, mAndroid[1].name);
Lucas Dupin8eedc022021-07-29 21:49:07 +0000821 drawTexturedQuad(x, yc, mAndroid[1].w, mAndroid[1].h);
822 drawTexturedQuad(x + mAndroid[1].w, yc, mAndroid[1].w, mAndroid[1].h);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800823
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700824 glEnable(GL_BLEND);
825 glBindTexture(GL_TEXTURE_2D, mAndroid[0].name);
Lucas Dupin8eedc022021-07-29 21:49:07 +0000826 drawTexturedQuad(xc, yc, mAndroid[0].w, mAndroid[0].h);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800827
Mathias Agopian627e7b52009-05-21 19:21:59 -0700828 EGLBoolean res = eglSwapBuffers(mDisplay, mSurface);
829 if (res == EGL_FALSE)
830 break;
831
Mathias Agopian13796652009-03-24 22:49:21 -0700832 // 12fps: don't animate too fast to preserve CPU
833 const nsecs_t sleepTime = 83333 - ns2us(systemTime() - now);
834 if (sleepTime > 0)
Mathias Agopiana8826d62009-10-01 03:10:14 -0700835 usleep(sleepTime);
Kevin Hesterd3782b22012-04-26 10:38:55 -0700836
837 checkExit();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800838 } while (!exitPending());
839
840 glDeleteTextures(1, &mAndroid[0].name);
841 glDeleteTextures(1, &mAndroid[1].name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800842 return false;
843}
844
Kevin Hesterd3782b22012-04-26 10:38:55 -0700845void BootAnimation::checkExit() {
846 // Allow surface flinger to gracefully request shutdown
847 char value[PROPERTY_VALUE_MAX];
848 property_get(EXIT_PROP_NAME, value, "0");
849 int exitnow = atoi(value);
850 if (exitnow) {
851 requestExit();
852 }
853}
854
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700855bool BootAnimation::validClock(const Animation::Part& part) {
856 return part.clockPosX != TEXT_MISSING_VALUE && part.clockPosY != TEXT_MISSING_VALUE;
857}
858
859bool parseTextCoord(const char* str, int* dest) {
860 if (strcmp("c", str) == 0) {
861 *dest = TEXT_CENTER_VALUE;
862 return true;
863 }
864
865 char* end;
866 int val = (int) strtol(str, &end, 0);
867 if (end == str || *end != '\0' || val == INT_MAX || val == INT_MIN) {
868 return false;
869 }
870 *dest = val;
871 return true;
872}
873
874// Parse two position coordinates. If only string is non-empty, treat it as the y value.
875void parsePosition(const char* str1, const char* str2, int* x, int* y) {
876 bool success = false;
877 if (strlen(str1) == 0) { // No values were specified
878 // success = false
879 } else if (strlen(str2) == 0) { // we have only one value
880 if (parseTextCoord(str1, y)) {
881 *x = TEXT_CENTER_VALUE;
882 success = true;
883 }
884 } else {
885 if (parseTextCoord(str1, x) && parseTextCoord(str2, y)) {
886 success = true;
887 }
888 }
889
890 if (!success) {
891 *x = TEXT_MISSING_VALUE;
892 *y = TEXT_MISSING_VALUE;
893 }
894}
895
Jesse Hall083b84c2014-09-22 10:51:09 -0700896// Parse a color represented as an HTML-style 'RRGGBB' string: each pair of
897// characters in str is a hex number in [0, 255], which are converted to
898// floating point values in the range [0.0, 1.0] and placed in the
899// corresponding elements of color.
900//
901// If the input string isn't valid, parseColor returns false and color is
902// left unchanged.
903static bool parseColor(const char str[7], float color[3]) {
904 float tmpColor[3];
905 for (int i = 0; i < 3; i++) {
906 int val = 0;
907 for (int j = 0; j < 2; j++) {
908 val *= 16;
909 char c = str[2*i + j];
910 if (c >= '0' && c <= '9') val += c - '0';
911 else if (c >= 'A' && c <= 'F') val += (c - 'A') + 10;
912 else if (c >= 'a' && c <= 'f') val += (c - 'a') + 10;
913 else return false;
914 }
915 tmpColor[i] = static_cast<float>(val) / 255.0f;
916 }
917 memcpy(color, tmpColor, sizeof(tmpColor));
918 return true;
919}
920
Shan Huang9f1123c2021-08-13 00:36:12 +0000921// Parse a color represented as a signed decimal int string.
922// E.g. "-2757722" (whose hex 2's complement is 0xFFD5EBA6).
923// If the input color string is empty, set color with values in defaultColor.
924static void parseColorDecimalString(const std::string& colorString,
925 float color[3], float defaultColor[3]) {
926 if (colorString == "") {
927 memcpy(color, defaultColor, sizeof(float) * 3);
928 return;
929 }
930 int colorInt = atoi(colorString.c_str());
931 color[0] = ((float)((colorInt >> 16) & 0xFF)) / 0xFF; // r
932 color[1] = ((float)((colorInt >> 8) & 0xFF)) / 0xFF; // g
933 color[2] = ((float)(colorInt & 0xFF)) / 0xFF; // b
934}
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700935
Marin Shalamanov9070c052020-05-18 19:05:17 +0200936static bool readFile(ZipFileRO* zip, const char* name, String8& outString) {
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700937 ZipEntryRO entry = zip->findEntryByName(name);
Wei Wang159a4102018-10-23 23:15:58 -0700938 SLOGE_IF(!entry, "couldn't find %s", name);
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700939 if (!entry) {
940 return false;
941 }
942
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700943 FileMap* entryMap = zip->createEntryFileMap(entry);
944 zip->releaseEntry(entry);
Wei Wang159a4102018-10-23 23:15:58 -0700945 SLOGE_IF(!entryMap, "entryMap is null");
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700946 if (!entryMap) {
947 return false;
948 }
949
950 outString.setTo((char const*)entryMap->getDataPtr(), entryMap->getDataLength());
Narayan Kamath688ff4c2015-02-23 15:47:54 +0000951 delete entryMap;
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700952 return true;
953}
954
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700955// The font image should be a 96x2 array of character images. The
956// columns are the printable ASCII characters 0x20 - 0x7f. The
957// top row is regular text; the bottom row is bold.
958status_t BootAnimation::initFont(Font* font, const char* fallback) {
959 status_t status = NO_ERROR;
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800960
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700961 if (font->map != nullptr) {
962 glGenTextures(1, &font->texture.name);
963 glBindTexture(GL_TEXTURE_2D, font->texture.name);
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800964
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700965 status = initTexture(font->map, &font->texture.w, &font->texture.h);
966
Lucas Dupin8eedc022021-07-29 21:49:07 +0000967 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
968 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
969 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
970 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700971 } else if (fallback != nullptr) {
972 status = initTexture(&font->texture, mAssets, fallback);
973 } else {
974 return NO_INIT;
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800975 }
976
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700977 if (status == NO_ERROR) {
978 font->char_width = font->texture.w / FONT_NUM_COLS;
979 font->char_height = font->texture.h / FONT_NUM_ROWS / 2; // There are bold and regular rows
980 }
981
982 return status;
983}
984
985void BootAnimation::drawText(const char* str, const Font& font, bool bold, int* x, int* y) {
986 glEnable(GL_BLEND); // Allow us to draw on top of the animation
987 glBindTexture(GL_TEXTURE_2D, font.texture.name);
Lucas Dupin8eedc022021-07-29 21:49:07 +0000988 glUseProgram(mTextShader);
989 glUniform1i(mTextTextureLocation, 0);
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700990
991 const int len = strlen(str);
992 const int strWidth = font.char_width * len;
993
994 if (*x == TEXT_CENTER_VALUE) {
995 *x = (mWidth - strWidth) / 2;
996 } else if (*x < 0) {
997 *x = mWidth + *x - strWidth;
998 }
999 if (*y == TEXT_CENTER_VALUE) {
1000 *y = (mHeight - font.char_height) / 2;
1001 } else if (*y < 0) {
1002 *y = mHeight + *y - font.char_height;
1003 }
1004
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -07001005 for (int i = 0; i < len; i++) {
1006 char c = str[i];
1007
1008 if (c < FONT_BEGIN_CHAR || c > FONT_END_CHAR) {
1009 c = '?';
1010 }
1011
1012 // Crop the texture to only the pixels in the current glyph
1013 const int charPos = (c - FONT_BEGIN_CHAR); // Position in the list of valid characters
1014 const int row = charPos / FONT_NUM_COLS;
1015 const int col = charPos % FONT_NUM_COLS;
Lucas Dupin8eedc022021-07-29 21:49:07 +00001016 // Bold fonts are expected in the second half of each row.
1017 float v0 = (row + (bold ? 0.5f : 0.0f)) / FONT_NUM_ROWS;
1018 float u0 = ((float)col) / FONT_NUM_COLS;
1019 float v1 = v0 + 1.0f / FONT_NUM_ROWS / 2;
1020 float u1 = u0 + 1.0f / FONT_NUM_COLS;
1021 glUniform4f(mTextCropAreaLocation, u0, v0, u1, v1);
1022 drawTexturedQuad(*x, *y, font.char_width, font.char_height);
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -07001023
1024 *x += font.char_width;
1025 }
1026
1027 glDisable(GL_BLEND); // Return to the animation's default behaviour
1028 glBindTexture(GL_TEXTURE_2D, 0);
1029}
1030
Damien Bargiacchi9071db12016-10-28 17:38:22 -07001031// We render 12 or 24 hour time.
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -07001032void BootAnimation::drawClock(const Font& font, const int xPos, const int yPos) {
Damien Bargiacchi9071db12016-10-28 17:38:22 -07001033 static constexpr char TIME_FORMAT_12[] = "%l:%M";
1034 static constexpr char TIME_FORMAT_24[] = "%H:%M";
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -07001035 static constexpr int TIME_LENGTH = 6;
1036
Damien Bargiacchia704b7d2016-02-16 16:55:49 -08001037 time_t rawtime;
1038 time(&rawtime);
1039 struct tm* timeInfo = localtime(&rawtime);
1040
1041 char timeBuff[TIME_LENGTH];
Damien Bargiacchi9071db12016-10-28 17:38:22 -07001042 const char* timeFormat = mTimeFormat12Hour ? TIME_FORMAT_12 : TIME_FORMAT_24;
1043 size_t length = strftime(timeBuff, TIME_LENGTH, timeFormat, timeInfo);
Damien Bargiacchia704b7d2016-02-16 16:55:49 -08001044
1045 if (length != TIME_LENGTH - 1) {
Wei Wang159a4102018-10-23 23:15:58 -07001046 SLOGE("Couldn't format time; abandoning boot animation clock");
Damien Bargiacchia704b7d2016-02-16 16:55:49 -08001047 mClockEnabled = false;
1048 return;
1049 }
1050
Damien Bargiacchi45a76442016-12-05 18:02:18 -08001051 char* out = timeBuff[0] == ' ' ? &timeBuff[1] : &timeBuff[0];
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -07001052 int x = xPos;
1053 int y = yPos;
Damien Bargiacchi45a76442016-12-05 18:02:18 -08001054 drawText(out, font, false, &x, &y);
Damien Bargiacchia704b7d2016-02-16 16:55:49 -08001055}
1056
Nicolas Geoffray41fffb42020-12-15 10:50:29 +00001057void BootAnimation::drawProgress(int percent, const Font& font, const int xPos, const int yPos) {
1058 static constexpr int PERCENT_LENGTH = 5;
1059
1060 char percentBuff[PERCENT_LENGTH];
1061 // ';' has the ascii code just after ':', and the font resource contains '%'
1062 // for that ascii code.
1063 sprintf(percentBuff, "%d;", percent);
1064 int x = xPos;
1065 int y = yPos;
1066 drawText(percentBuff, font, false, &x, &y);
1067}
1068
Marin Shalamanov9070c052020-05-18 19:05:17 +02001069bool BootAnimation::parseAnimationDesc(Animation& animation) {
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -07001070 String8 desString;
1071
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001072 if (!readFile(animation.zip, "desc.txt", desString)) {
Narayan Kamathafd31e02013-12-03 13:16:03 +00001073 return false;
1074 }
Mathias Agopiana8826d62009-10-01 03:10:14 -07001075 char const* s = desString.string();
Shan Huang9f1123c2021-08-13 00:36:12 +00001076 std::string dynamicColoringPartName = "";
1077 bool postDynamicColoring = false;
Mathias Agopiana8826d62009-10-01 03:10:14 -07001078
Mathias Agopiana8826d62009-10-01 03:10:14 -07001079 // Parse the description file
1080 for (;;) {
1081 const char* endl = strstr(s, "\n");
Yi Kong911ac232019-03-24 01:49:02 -07001082 if (endl == nullptr) break;
Mathias Agopiana8826d62009-10-01 03:10:14 -07001083 String8 line(s, endl - s);
1084 const char* l = line.string();
Damien Bargiacchia704b7d2016-02-16 16:55:49 -08001085 int fps = 0;
1086 int width = 0;
1087 int height = 0;
1088 int count = 0;
1089 int pause = 0;
Nicolas Geoffray41fffb42020-12-15 10:50:29 +00001090 int progress = 0;
Yegor Malyshev6925e1b2020-09-10 16:42:20 +02001091 int framesToFadeCount = 0;
Narayan Kamathafd31e02013-12-03 13:16:03 +00001092 char path[ANIM_ENTRY_NAME_MAX];
Jesse Hall083b84c2014-09-22 10:51:09 -07001093 char color[7] = "000000"; // default to black if unspecified
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -07001094 char clockPos1[TEXT_POS_LEN_MAX + 1] = "";
1095 char clockPos2[TEXT_POS_LEN_MAX + 1] = "";
Shan Huang9f1123c2021-08-13 00:36:12 +00001096 char dynamicColoringPartNameBuffer[ANIM_ENTRY_NAME_MAX];
Kevin Hesterd3782b22012-04-26 10:38:55 -07001097 char pathType;
Shan Huang9f1123c2021-08-13 00:36:12 +00001098 // start colors default to black if unspecified
1099 char start_color_0[7] = "000000";
1100 char start_color_1[7] = "000000";
1101 char start_color_2[7] = "000000";
1102 char start_color_3[7] = "000000";
Yegor Malyshev6925e1b2020-09-10 16:42:20 +02001103
1104 int nextReadPos;
1105
Nicolas Geoffray41fffb42020-12-15 10:50:29 +00001106 int topLineNumbers = sscanf(l, "%d %d %d %d", &width, &height, &fps, &progress);
1107 if (topLineNumbers == 3 || topLineNumbers == 4) {
1108 // SLOGD("> w=%d, h=%d, fps=%d, progress=%d", width, height, fps, progress);
Mathias Agopiana8826d62009-10-01 03:10:14 -07001109 animation.width = width;
1110 animation.height = height;
1111 animation.fps = fps;
Nicolas Geoffray41fffb42020-12-15 10:50:29 +00001112 if (topLineNumbers == 4) {
1113 animation.progressEnabled = (progress != 0);
1114 } else {
1115 animation.progressEnabled = false;
1116 }
Shan Huang9f1123c2021-08-13 00:36:12 +00001117 } else if (sscanf(l, "dynamic_colors %" STRTO(ANIM_PATH_MAX) "s #%6s #%6s #%6s #%6s",
1118 dynamicColoringPartNameBuffer,
1119 start_color_0, start_color_1, start_color_2, start_color_3)) {
1120 animation.dynamicColoringEnabled = true;
1121 parseColor(start_color_0, animation.startColors[0]);
1122 parseColor(start_color_1, animation.startColors[1]);
1123 parseColor(start_color_2, animation.startColors[2]);
1124 parseColor(start_color_3, animation.startColors[3]);
1125 dynamicColoringPartName = std::string(dynamicColoringPartNameBuffer);
Yegor Malyshev6925e1b2020-09-10 16:42:20 +02001126 } else if (sscanf(l, "%c %d %d %" STRTO(ANIM_PATH_MAX) "s%n",
1127 &pathType, &count, &pause, path, &nextReadPos) >= 4) {
1128 if (pathType == 'f') {
1129 sscanf(l + nextReadPos, " %d #%6s %16s %16s", &framesToFadeCount, color, clockPos1,
1130 clockPos2);
1131 } else {
1132 sscanf(l + nextReadPos, " #%6s %16s %16s", color, clockPos1, clockPos2);
1133 }
1134 // SLOGD("> type=%c, count=%d, pause=%d, path=%s, framesToFadeCount=%d, color=%s, "
1135 // "clockPos1=%s, clockPos2=%s",
1136 // pathType, count, pause, path, framesToFadeCount, color, clockPos1, clockPos2);
Mathias Agopiana8826d62009-10-01 03:10:14 -07001137 Animation::Part part;
Shan Huang9f1123c2021-08-13 00:36:12 +00001138 if (path == dynamicColoringPartName) {
1139 // Part is specified to use dynamic coloring.
1140 part.useDynamicColoring = true;
1141 part.postDynamicColoring = false;
1142 postDynamicColoring = true;
1143 } else {
1144 // Part does not use dynamic coloring.
1145 part.useDynamicColoring = false;
1146 part.postDynamicColoring = postDynamicColoring;
1147 }
Kevin Hesterd3782b22012-04-26 10:38:55 -07001148 part.playUntilComplete = pathType == 'c';
Yegor Malyshev6925e1b2020-09-10 16:42:20 +02001149 part.framesToFadeCount = framesToFadeCount;
Mathias Agopiana8826d62009-10-01 03:10:14 -07001150 part.count = count;
1151 part.pause = pause;
1152 part.path = path;
Yi Kong911ac232019-03-24 01:49:02 -07001153 part.audioData = nullptr;
1154 part.animation = nullptr;
Jesse Hall083b84c2014-09-22 10:51:09 -07001155 if (!parseColor(color, part.backgroundColor)) {
Wei Wang159a4102018-10-23 23:15:58 -07001156 SLOGE("> invalid color '#%s'", color);
Jesse Hall083b84c2014-09-22 10:51:09 -07001157 part.backgroundColor[0] = 0.0f;
1158 part.backgroundColor[1] = 0.0f;
1159 part.backgroundColor[2] = 0.0f;
1160 }
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -07001161 parsePosition(clockPos1, clockPos2, &part.clockPosX, &part.clockPosY);
Mathias Agopiana8826d62009-10-01 03:10:14 -07001162 animation.parts.add(part);
1163 }
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001164 else if (strcmp(l, "$SYSTEM") == 0) {
Wei Wang159a4102018-10-23 23:15:58 -07001165 // SLOGD("> SYSTEM");
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001166 Animation::Part part;
1167 part.playUntilComplete = false;
Yegor Malyshev6925e1b2020-09-10 16:42:20 +02001168 part.framesToFadeCount = 0;
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001169 part.count = 1;
1170 part.pause = 0;
Yi Kong911ac232019-03-24 01:49:02 -07001171 part.audioData = nullptr;
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001172 part.animation = loadAnimation(String8(SYSTEM_BOOTANIMATION_FILE));
Yi Kong911ac232019-03-24 01:49:02 -07001173 if (part.animation != nullptr)
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001174 animation.parts.add(part);
1175 }
Mathias Agopiana8826d62009-10-01 03:10:14 -07001176 s = ++endl;
1177 }
1178
Shan Huang9f1123c2021-08-13 00:36:12 +00001179 for (int i = 0; i < DYNAMIC_COLOR_COUNT; i++) {
1180 parseColorDecimalString(
1181 android::base::GetProperty("persist.bootanim.color" + std::to_string(i + 1), ""),
1182 animation.endColors[i], animation.startColors[i]);
1183 }
1184
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001185 return true;
1186}
1187
Marin Shalamanov9070c052020-05-18 19:05:17 +02001188bool BootAnimation::preloadZip(Animation& animation) {
Mathias Agopiana8826d62009-10-01 03:10:14 -07001189 // read all the data structures
1190 const size_t pcount = animation.parts.size();
Yi Kong911ac232019-03-24 01:49:02 -07001191 void *cookie = nullptr;
Geoffrey Pitschdd214a72016-06-27 17:14:30 -04001192 ZipFileRO* zip = animation.zip;
1193 if (!zip->startIteration(&cookie)) {
Narayan Kamathafd31e02013-12-03 13:16:03 +00001194 return false;
1195 }
1196
1197 ZipEntryRO entry;
1198 char name[ANIM_ENTRY_NAME_MAX];
Yi Kong911ac232019-03-24 01:49:02 -07001199 while ((entry = zip->nextEntry(cookie)) != nullptr) {
Geoffrey Pitschdd214a72016-06-27 17:14:30 -04001200 const int foundEntryName = zip->getEntryFileName(entry, name, ANIM_ENTRY_NAME_MAX);
Narayan Kamathafd31e02013-12-03 13:16:03 +00001201 if (foundEntryName > ANIM_ENTRY_NAME_MAX || foundEntryName == -1) {
Wei Wang159a4102018-10-23 23:15:58 -07001202 SLOGE("Error fetching entry file name");
Narayan Kamathafd31e02013-12-03 13:16:03 +00001203 continue;
1204 }
1205
1206 const String8 entryName(name);
1207 const String8 path(entryName.getPathDir());
1208 const String8 leaf(entryName.getPathLeaf());
1209 if (leaf.size() > 0) {
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -07001210 if (entryName == CLOCK_FONT_ZIP_NAME) {
1211 FileMap* map = zip->createEntryFileMap(entry);
1212 if (map) {
1213 animation.clockFont.map = map;
1214 }
1215 continue;
1216 }
1217
Nicolas Geoffray41fffb42020-12-15 10:50:29 +00001218 if (entryName == PROGRESS_FONT_ZIP_NAME) {
1219 FileMap* map = zip->createEntryFileMap(entry);
1220 if (map) {
1221 animation.progressFont.map = map;
1222 }
1223 continue;
1224 }
1225
Geoffrey Pitschdd214a72016-06-27 17:14:30 -04001226 for (size_t j = 0; j < pcount; j++) {
Narayan Kamathafd31e02013-12-03 13:16:03 +00001227 if (path == animation.parts[j].path) {
Narayan Kamath4600dd02015-06-16 12:02:57 +01001228 uint16_t method;
Narayan Kamathafd31e02013-12-03 13:16:03 +00001229 // supports only stored png files
Yi Kong911ac232019-03-24 01:49:02 -07001230 if (zip->getEntryInfo(entry, &method, nullptr, nullptr, nullptr, nullptr, nullptr)) {
Narayan Kamathafd31e02013-12-03 13:16:03 +00001231 if (method == ZipFileRO::kCompressStored) {
Geoffrey Pitschdd214a72016-06-27 17:14:30 -04001232 FileMap* map = zip->createEntryFileMap(entry);
Narayan Kamathafd31e02013-12-03 13:16:03 +00001233 if (map) {
Narayan Kamathafd31e02013-12-03 13:16:03 +00001234 Animation::Part& part(animation.parts.editItemAt(j));
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -07001235 if (leaf == "audio.wav") {
1236 // a part may have at most one audio file
Geoffrey Pitschd6d9a1d2016-06-08 00:38:58 -07001237 part.audioData = (uint8_t *)map->getDataPtr();
1238 part.audioLength = map->getDataLength();
Geoffrey Pitschdd214a72016-06-27 17:14:30 -04001239 } else if (leaf == "trim.txt") {
1240 part.trimData.setTo((char const*)map->getDataPtr(),
1241 map->getDataLength());
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -07001242 } else {
1243 Animation::Frame frame;
1244 frame.name = leaf;
1245 frame.map = map;
Geoffrey Pitschdd214a72016-06-27 17:14:30 -04001246 frame.trimWidth = animation.width;
1247 frame.trimHeight = animation.height;
1248 frame.trimX = 0;
1249 frame.trimY = 0;
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -07001250 part.frames.add(frame);
1251 }
Mathias Agopiana8826d62009-10-01 03:10:14 -07001252 }
Geoffrey Pitschd6d9a1d2016-06-08 00:38:58 -07001253 } else {
Wei Wang159a4102018-10-23 23:15:58 -07001254 SLOGE("bootanimation.zip is compressed; must be only stored");
Mathias Agopiana8826d62009-10-01 03:10:14 -07001255 }
1256 }
1257 }
1258 }
1259 }
1260 }
1261
Geoffrey Pitschdd214a72016-06-27 17:14:30 -04001262 // If there is trimData present, override the positioning defaults.
1263 for (Animation::Part& part : animation.parts) {
1264 const char* trimDataStr = part.trimData.string();
1265 for (size_t frameIdx = 0; frameIdx < part.frames.size(); frameIdx++) {
1266 const char* endl = strstr(trimDataStr, "\n");
1267 // No more trimData for this part.
Yi Kong911ac232019-03-24 01:49:02 -07001268 if (endl == nullptr) {
Geoffrey Pitschdd214a72016-06-27 17:14:30 -04001269 break;
1270 }
1271 String8 line(trimDataStr, endl - trimDataStr);
1272 const char* lineStr = line.string();
1273 trimDataStr = ++endl;
1274 int width = 0, height = 0, x = 0, y = 0;
1275 if (sscanf(lineStr, "%dx%d+%d+%d", &width, &height, &x, &y) == 4) {
1276 Animation::Frame& frame(part.frames.editItemAt(frameIdx));
1277 frame.trimWidth = width;
1278 frame.trimHeight = height;
1279 frame.trimX = x;
1280 frame.trimY = y;
1281 } else {
Wei Wang159a4102018-10-23 23:15:58 -07001282 SLOGE("Error parsing trim.txt, line: %s", lineStr);
Geoffrey Pitschdd214a72016-06-27 17:14:30 -04001283 break;
1284 }
1285 }
1286 }
1287
1288 zip->endIteration(cookie);
Narayan Kamathafd31e02013-12-03 13:16:03 +00001289
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001290 return true;
1291}
1292
Marin Shalamanov9070c052020-05-18 19:05:17 +02001293bool BootAnimation::movie() {
Huihong Luo50a2f362018-08-11 09:27:57 -07001294 if (mAnimation == nullptr) {
1295 mAnimation = loadAnimation(mZipFileName);
1296 }
1297
1298 if (mAnimation == nullptr)
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001299 return false;
1300
Huihong Luo50a2f362018-08-11 09:27:57 -07001301 // mCallbacks->init() may get called recursively,
1302 // this loop is needed to get the same results
1303 for (const Animation::Part& part : mAnimation->parts) {
1304 if (part.animation != nullptr) {
1305 mCallbacks->init(part.animation->parts);
1306 }
1307 }
1308 mCallbacks->init(mAnimation->parts);
1309
Damien Bargiacchi97480862016-03-29 14:55:55 -07001310 bool anyPartHasClock = false;
Huihong Luo50a2f362018-08-11 09:27:57 -07001311 for (size_t i=0; i < mAnimation->parts.size(); i++) {
1312 if(validClock(mAnimation->parts[i])) {
Damien Bargiacchi97480862016-03-29 14:55:55 -07001313 anyPartHasClock = true;
1314 break;
1315 }
1316 }
1317 if (!anyPartHasClock) {
1318 mClockEnabled = false;
1319 }
1320
Sai Kiran Korwar27167492015-07-07 20:00:06 +05301321 // Check if npot textures are supported
1322 mUseNpotTextures = false;
1323 String8 gl_extensions;
1324 const char* exts = reinterpret_cast<const char*>(glGetString(GL_EXTENSIONS));
1325 if (!exts) {
1326 glGetError();
1327 } else {
1328 gl_extensions.setTo(exts);
1329 if ((gl_extensions.find("GL_ARB_texture_non_power_of_two") != -1) ||
1330 (gl_extensions.find("GL_OES_texture_npot") != -1)) {
1331 mUseNpotTextures = true;
1332 }
1333 }
1334
Damien Bargiacchia704b7d2016-02-16 16:55:49 -08001335 // Blend required to draw time on top of animation frames.
1336 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
Mathias Agopiana8826d62009-10-01 03:10:14 -07001337 glDisable(GL_DITHER);
1338 glDisable(GL_SCISSOR_TEST);
1339 glDisable(GL_BLEND);
Mathias Agopiana8826d62009-10-01 03:10:14 -07001340
Greg Kaiser2ded8002021-07-19 17:03:27 +00001341 glEnable(GL_TEXTURE_2D);
Lucas Dupin8eedc022021-07-29 21:49:07 +00001342 glBindTexture(GL_TEXTURE_2D, 0);
1343 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
1344 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
1345 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
1346 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -07001347 bool clockFontInitialized = false;
Damien Bargiacchia704b7d2016-02-16 16:55:49 -08001348 if (mClockEnabled) {
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -07001349 clockFontInitialized =
Huihong Luo50a2f362018-08-11 09:27:57 -07001350 (initFont(&mAnimation->clockFont, CLOCK_FONT_ASSET) == NO_ERROR);
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -07001351 mClockEnabled = clockFontInitialized;
Damien Bargiacchia704b7d2016-02-16 16:55:49 -08001352 }
1353
Nicolas Geoffray41fffb42020-12-15 10:50:29 +00001354 initFont(&mAnimation->progressFont, PROGRESS_FONT_ASSET);
1355
Damien Bargiacchi97480862016-03-29 14:55:55 -07001356 if (mClockEnabled && !updateIsTimeAccurate()) {
1357 mTimeCheckThread = new TimeCheckThread(this);
1358 mTimeCheckThread->run("BootAnimation::TimeCheckThread", PRIORITY_NORMAL);
1359 }
1360
Huihong Luo50a2f362018-08-11 09:27:57 -07001361 playAnimation(*mAnimation);
Damien Bargiacchi97480862016-03-29 14:55:55 -07001362
Geoffrey Pitscha9173532016-07-19 14:56:39 -04001363 if (mTimeCheckThread != nullptr) {
Damien Bargiacchi97480862016-03-29 14:55:55 -07001364 mTimeCheckThread->requestExit();
Geoffrey Pitscha9173532016-07-19 14:56:39 -04001365 mTimeCheckThread = nullptr;
1366 }
1367
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -07001368 if (clockFontInitialized) {
Huihong Luo50a2f362018-08-11 09:27:57 -07001369 glDeleteTextures(1, &mAnimation->clockFont.texture.name);
Andriy Naborskyy815e51d2016-03-24 16:43:34 -07001370 }
1371
Huihong Luo50a2f362018-08-11 09:27:57 -07001372 releaseAnimation(mAnimation);
1373 mAnimation = nullptr;
Damien Bargiacchi1a8a2132018-07-24 15:20:26 -07001374
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001375 return false;
1376}
1377
Nicolas Geoffraya1298752021-04-20 15:15:06 +01001378bool BootAnimation::shouldStopPlayingPart(const Animation::Part& part,
1379 const int fadedFramesCount,
1380 const int lastDisplayedProgress) {
Yegor Malyshev6925e1b2020-09-10 16:42:20 +02001381 // 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 +01001382 return exitPending() && !part.playUntilComplete && fadedFramesCount >= part.framesToFadeCount &&
1383 (lastDisplayedProgress == 0 || lastDisplayedProgress == 100);
Yegor Malyshev6925e1b2020-09-10 16:42:20 +02001384}
1385
Lucas Dupin8eedc022021-07-29 21:49:07 +00001386// Linear mapping from range <a1, a2> to range <b1, b2>
1387float mapLinear(float x, float a1, float a2, float b1, float b2) {
1388 return b1 + ( x - a1 ) * ( b2 - b1 ) / ( a2 - a1 );
1389}
1390
1391void BootAnimation::drawTexturedQuad(float xStart, float yStart, float width, float height) {
1392 // Map coordinates from screen space to world space.
1393 float x0 = mapLinear(xStart, 0, mWidth, -1, 1);
1394 float y0 = mapLinear(yStart, 0, mHeight, -1, 1);
1395 float x1 = mapLinear(xStart + width, 0, mWidth, -1, 1);
1396 float y1 = mapLinear(yStart + height, 0, mHeight, -1, 1);
1397 // Update quad vertex positions.
1398 quadPositions[0] = x0;
1399 quadPositions[1] = y0;
1400 quadPositions[2] = x1;
1401 quadPositions[3] = y0;
1402 quadPositions[4] = x1;
1403 quadPositions[5] = y1;
1404 quadPositions[6] = x1;
1405 quadPositions[7] = y1;
1406 quadPositions[8] = x0;
1407 quadPositions[9] = y1;
1408 quadPositions[10] = x0;
1409 quadPositions[11] = y0;
1410 glDrawArrays(GL_TRIANGLES, 0,
1411 sizeof(quadPositions) / sizeof(quadPositions[0]) / 2);
1412}
1413
Marin Shalamanov9070c052020-05-18 19:05:17 +02001414bool BootAnimation::playAnimation(const Animation& animation) {
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001415 const size_t pcount = animation.parts.size();
Mathias Agopiana8826d62009-10-01 03:10:14 -07001416 nsecs_t frameDuration = s2ns(1) / animation.fps;
Mathias Agopian9f3020d2009-11-06 16:30:18 -08001417
Wei Wang159a4102018-10-23 23:15:58 -07001418 SLOGD("%sAnimationShownTiming start time: %" PRId64 "ms", mShuttingDown ? "Shutdown" : "Boot",
Keun-young Parkd1794cd2017-04-04 12:21:19 -07001419 elapsedRealtime());
Yegor Malyshev6925e1b2020-09-10 16:42:20 +02001420
1421 int fadedFramesCount = 0;
Nicolas Geoffray41fffb42020-12-15 10:50:29 +00001422 int lastDisplayedProgress = 0;
Narayan Kamathafd31e02013-12-03 13:16:03 +00001423 for (size_t i=0 ; i<pcount ; i++) {
Mathias Agopiana8826d62009-10-01 03:10:14 -07001424 const Animation::Part& part(animation.parts[i]);
1425 const size_t fcount = part.frames.size();
Mathias Agopiana8826d62009-10-01 03:10:14 -07001426
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001427 // Handle animation package
Yi Kong911ac232019-03-24 01:49:02 -07001428 if (part.animation != nullptr) {
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001429 playAnimation(*part.animation);
1430 if (exitPending())
1431 break;
1432 continue; //to next part
1433 }
1434
Yegor Malyshev6925e1b2020-09-10 16:42:20 +02001435 // process the part not only while the count allows but also if already fading
1436 for (int r=0 ; !part.count || r<part.count || fadedFramesCount > 0 ; r++) {
Nicolas Geoffraya1298752021-04-20 15:15:06 +01001437 if (shouldStopPlayingPart(part, fadedFramesCount, lastDisplayedProgress)) break;
Kevin Hesterd3782b22012-04-26 10:38:55 -07001438
Ed Coyne7464ac92017-06-08 12:26:48 -07001439 mCallbacks->playPart(i, part, r);
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -07001440
Jesse Hall083b84c2014-09-22 10:51:09 -07001441 glClearColor(
1442 part.backgroundColor[0],
1443 part.backgroundColor[1],
1444 part.backgroundColor[2],
1445 1.0f);
1446
Nicolas Geoffray41fffb42020-12-15 10:50:29 +00001447 // For the last animation, if we have progress indicator from
1448 // the system, display it.
1449 int currentProgress = android::base::GetIntProperty(PROGRESS_PROP_NAME, 0);
1450 bool displayProgress = animation.progressEnabled &&
1451 (i == (pcount -1)) && currentProgress != 0;
1452
Yegor Malyshev6925e1b2020-09-10 16:42:20 +02001453 for (size_t j=0 ; j<fcount ; j++) {
Nicolas Geoffraya1298752021-04-20 15:15:06 +01001454 if (shouldStopPlayingPart(part, fadedFramesCount, lastDisplayedProgress)) break;
Yegor Malyshev6925e1b2020-09-10 16:42:20 +02001455
Shan Huang9f1123c2021-08-13 00:36:12 +00001456 // Color progress is
1457 // - the normalized animation progress between [0, 1] for the dynamic coloring part,
1458 // - 0 for parts that come before,
1459 // - 1 for parts that come after.
1460 float colorProgress = part.useDynamicColoring
1461 ? (float)j / fcount
1462 : (part.postDynamicColoring ? 1 : 0);
1463
Marin Shalamanov9070c052020-05-18 19:05:17 +02001464 processDisplayEvents();
1465
1466 const int animationX = (mWidth - animation.width) / 2;
1467 const int animationY = (mHeight - animation.height) / 2;
1468
Mathias Agopiana8826d62009-10-01 03:10:14 -07001469 const Animation::Frame& frame(part.frames[j]);
Mathias Agopiandb7dd2a2012-05-12 15:08:21 -07001470 nsecs_t lastFrame = systemTime();
Mathias Agopiana8826d62009-10-01 03:10:14 -07001471
1472 if (r > 0) {
1473 glBindTexture(GL_TEXTURE_2D, frame.tid);
1474 } else {
Lucas Dupin8eedc022021-07-29 21:49:07 +00001475 glGenTextures(1, &frame.tid);
1476 glBindTexture(GL_TEXTURE_2D, frame.tid);
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -07001477 int w, h;
1478 initTexture(frame.map, &w, &h);
Mathias Agopiana8826d62009-10-01 03:10:14 -07001479 }
1480
Geoffrey Pitschdd214a72016-06-27 17:14:30 -04001481 const int xc = animationX + frame.trimX;
1482 const int yc = animationY + frame.trimY;
Shan Huang9f1123c2021-08-13 00:36:12 +00001483 glClear(GL_COLOR_BUFFER_BIT);
Geoffrey Pitschdd214a72016-06-27 17:14:30 -04001484 // specify the y center as ceiling((mHeight - frame.trimHeight) / 2)
1485 // which is equivalent to mHeight - (yc + frame.trimHeight)
Yegor Malyshev6925e1b2020-09-10 16:42:20 +02001486 const int frameDrawY = mHeight - (yc + frame.trimHeight);
Yegor Malyshev6925e1b2020-09-10 16:42:20 +02001487
Lucas Dupin8eedc022021-07-29 21:49:07 +00001488 float fade = 0;
Yegor Malyshev6925e1b2020-09-10 16:42:20 +02001489 // if the part hasn't been stopped yet then continue fading if necessary
1490 if (exitPending() && part.hasFadingPhase()) {
Lucas Dupin8eedc022021-07-29 21:49:07 +00001491 fade = static_cast<float>(++fadedFramesCount) / part.framesToFadeCount;
Yegor Malyshev6925e1b2020-09-10 16:42:20 +02001492 if (fadedFramesCount >= part.framesToFadeCount) {
1493 fadedFramesCount = MAX_FADED_FRAMES_COUNT; // no more fading
1494 }
1495 }
Lucas Dupin8eedc022021-07-29 21:49:07 +00001496 glUseProgram(mImageShader);
1497 glUniform1i(mImageTextureLocation, 0);
1498 glUniform1f(mImageFadeLocation, fade);
Shan Huang9f1123c2021-08-13 00:36:12 +00001499 if (animation.dynamicColoringEnabled) {
1500 glUniform1f(mImageColorProgressLocation, colorProgress);
1501 }
Lucas Dupin8eedc022021-07-29 21:49:07 +00001502 glEnable(GL_BLEND);
1503 drawTexturedQuad(xc, frameDrawY, frame.trimWidth, frame.trimHeight);
1504 glDisable(GL_BLEND);
Yegor Malyshev6925e1b2020-09-10 16:42:20 +02001505
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -07001506 if (mClockEnabled && mTimeIsAccurate && validClock(part)) {
1507 drawClock(animation.clockFont, part.clockPosX, part.clockPosY);
Damien Bargiacchia704b7d2016-02-16 16:55:49 -08001508 }
Yegor Malyshev6925e1b2020-09-10 16:42:20 +02001509
Nicolas Geoffray41fffb42020-12-15 10:50:29 +00001510 if (displayProgress) {
1511 int newProgress = android::base::GetIntProperty(PROGRESS_PROP_NAME, 0);
1512 // In case the new progress jumped suddenly, still show an
1513 // increment of 1.
1514 if (lastDisplayedProgress != 100) {
1515 // Artificially sleep 1/10th a second to slow down the animation.
1516 usleep(100000);
1517 if (lastDisplayedProgress < newProgress) {
1518 lastDisplayedProgress++;
1519 }
1520 }
1521 // Put the progress percentage right below the animation.
1522 int posY = animation.height / 3;
1523 int posX = TEXT_CENTER_VALUE;
1524 drawProgress(lastDisplayedProgress, animation.progressFont, posX, posY);
1525 }
1526
Adrian Roos9ee5dff2018-08-22 20:19:49 +02001527 handleViewport(frameDuration);
Damien Bargiacchia704b7d2016-02-16 16:55:49 -08001528
Mathias Agopiana8826d62009-10-01 03:10:14 -07001529 eglSwapBuffers(mDisplay, mSurface);
1530
1531 nsecs_t now = systemTime();
1532 nsecs_t delay = frameDuration - (now - lastFrame);
Wei Wang159a4102018-10-23 23:15:58 -07001533 //SLOGD("%lld, %lld", ns2ms(now - lastFrame), ns2ms(delay));
Mathias Agopiana8826d62009-10-01 03:10:14 -07001534 lastFrame = now;
Mathias Agopiandb7dd2a2012-05-12 15:08:21 -07001535
1536 if (delay > 0) {
1537 struct timespec spec;
1538 spec.tv_sec = (now + delay) / 1000000000;
1539 spec.tv_nsec = (now + delay) % 1000000000;
1540 int err;
1541 do {
Yi Kong911ac232019-03-24 01:49:02 -07001542 err = clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &spec, nullptr);
Mathias Agopiandb7dd2a2012-05-12 15:08:21 -07001543 } while (err<0 && errno == EINTR);
1544 }
Kevin Hesterd3782b22012-04-26 10:38:55 -07001545
1546 checkExit();
Mathias Agopiana8826d62009-10-01 03:10:14 -07001547 }
Kevin Hesterd3782b22012-04-26 10:38:55 -07001548
Mathias Agopiana8826d62009-10-01 03:10:14 -07001549 usleep(part.pause * ns2us(frameDuration));
Kevin Hesterd3782b22012-04-26 10:38:55 -07001550
Yegor Malyshev6925e1b2020-09-10 16:42:20 +02001551 if (exitPending() && !part.count && mCurrentInset >= mTargetInset &&
1552 !part.hasFadingPhase()) {
Nicolas Geoffraya1298752021-04-20 15:15:06 +01001553 if (lastDisplayedProgress != 0 && lastDisplayedProgress != 100) {
1554 android::base::SetProperty(PROGRESS_PROP_NAME, "100");
1555 continue;
1556 }
Yegor Malyshev6925e1b2020-09-10 16:42:20 +02001557 break; // exit the infinite non-fading part when it has been played at least once
1558 }
Mathias Agopiana8826d62009-10-01 03:10:14 -07001559 }
Geoffrey Pitsch2fb30fb2016-07-06 16:16:20 -04001560 }
1561
1562 // Free textures created for looping parts now that the animation is done.
1563 for (const Animation::Part& part : animation.parts) {
Mathias Agopiana8826d62009-10-01 03:10:14 -07001564 if (part.count != 1) {
Geoffrey Pitsch2fb30fb2016-07-06 16:16:20 -04001565 const size_t fcount = part.frames.size();
1566 for (size_t j = 0; j < fcount; j++) {
Mathias Agopiana8826d62009-10-01 03:10:14 -07001567 const Animation::Frame& frame(part.frames[j]);
1568 glDeleteTextures(1, &frame.tid);
1569 }
1570 }
1571 }
Geoffrey Pitschd6d9a1d2016-06-08 00:38:58 -07001572
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001573 return true;
Mathias Agopiana8826d62009-10-01 03:10:14 -07001574}
1575
Marin Shalamanov9070c052020-05-18 19:05:17 +02001576void BootAnimation::processDisplayEvents() {
1577 // This will poll mDisplayEventReceiver and if there are new events it'll call
1578 // displayEventCallback synchronously.
1579 mLooper->pollOnce(0);
1580}
1581
Adrian Roos9ee5dff2018-08-22 20:19:49 +02001582void BootAnimation::handleViewport(nsecs_t timestep) {
1583 if (mShuttingDown || !mFlingerSurfaceControl || mTargetInset == 0) {
1584 return;
1585 }
1586 if (mTargetInset < 0) {
1587 // Poll the amount for the top display inset. This will return -1 until persistent properties
1588 // have been loaded.
1589 mTargetInset = android::base::GetIntProperty("persist.sys.displayinset.top",
1590 -1 /* default */, -1 /* min */, mHeight / 2 /* max */);
1591 }
1592 if (mTargetInset <= 0) {
1593 return;
1594 }
1595
1596 if (mCurrentInset < mTargetInset) {
1597 // After the device boots, the inset will effectively be cropped away. We animate this here.
1598 float fraction = static_cast<float>(mCurrentInset) / mTargetInset;
1599 int interpolatedInset = (cosf((fraction + 1) * M_PI) / 2.0f + 0.5f) * mTargetInset;
1600
1601 SurfaceComposerClient::Transaction()
1602 .setCrop(mFlingerSurfaceControl, Rect(0, interpolatedInset, mWidth, mHeight))
1603 .apply();
1604 } else {
1605 // At the end of the animation, we switch to the viewport that DisplayManager will apply
1606 // later. This changes the coordinate system, and means we must move the surface up by
1607 // the inset amount.
Adrian Roos9ee5dff2018-08-22 20:19:49 +02001608 Rect layerStackRect(0, 0, mWidth, mHeight - mTargetInset);
1609 Rect displayRect(0, mTargetInset, mWidth, mHeight);
1610
1611 SurfaceComposerClient::Transaction t;
1612 t.setPosition(mFlingerSurfaceControl, 0, -mTargetInset)
1613 .setCrop(mFlingerSurfaceControl, Rect(0, mTargetInset, mWidth, mHeight));
Dominik Laskowski2a3d9aa2019-11-18 20:26:39 -08001614 t.setDisplayProjection(mDisplayToken, ui::ROTATION_0, layerStackRect, displayRect);
Adrian Roos9ee5dff2018-08-22 20:19:49 +02001615 t.apply();
1616
1617 mTargetInset = mCurrentInset = 0;
1618 }
1619
1620 int delta = timestep * mTargetInset / ms2ns(200);
1621 mCurrentInset += delta;
1622}
1623
Marin Shalamanov9070c052020-05-18 19:05:17 +02001624void BootAnimation::releaseAnimation(Animation* animation) const {
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001625 for (Vector<Animation::Part>::iterator it = animation->parts.begin(),
1626 e = animation->parts.end(); it != e; ++it) {
1627 if (it->animation)
1628 releaseAnimation(it->animation);
1629 }
1630 if (animation->zip)
1631 delete animation->zip;
1632 delete animation;
1633}
1634
Marin Shalamanov9070c052020-05-18 19:05:17 +02001635BootAnimation::Animation* BootAnimation::loadAnimation(const String8& fn) {
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001636 if (mLoadedFiles.indexOf(fn) >= 0) {
Wei Wang159a4102018-10-23 23:15:58 -07001637 SLOGE("File \"%s\" is already loaded. Cyclic ref is not allowed",
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001638 fn.string());
Yi Kong911ac232019-03-24 01:49:02 -07001639 return nullptr;
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001640 }
1641 ZipFileRO *zip = ZipFileRO::open(fn);
Yi Kong911ac232019-03-24 01:49:02 -07001642 if (zip == nullptr) {
Wei Wang159a4102018-10-23 23:15:58 -07001643 SLOGE("Failed to open animation zip \"%s\": %s",
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001644 fn.string(), strerror(errno));
Yi Kong911ac232019-03-24 01:49:02 -07001645 return nullptr;
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001646 }
1647
1648 Animation *animation = new Animation;
1649 animation->fileName = fn;
1650 animation->zip = zip;
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -07001651 animation->clockFont.map = nullptr;
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001652 mLoadedFiles.add(animation->fileName);
1653
1654 parseAnimationDesc(*animation);
Geoffrey Pitscha91a2d72016-07-12 14:46:19 -04001655 if (!preloadZip(*animation)) {
Greg Kaiser7426ec82019-09-11 14:34:27 -07001656 releaseAnimation(animation);
Yi Kong911ac232019-03-24 01:49:02 -07001657 return nullptr;
Geoffrey Pitscha91a2d72016-07-12 14:46:19 -04001658 }
1659
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001660 mLoadedFiles.remove(fn);
1661 return animation;
1662}
Damien Bargiacchi97480862016-03-29 14:55:55 -07001663
1664bool BootAnimation::updateIsTimeAccurate() {
1665 static constexpr long long MAX_TIME_IN_PAST = 60000LL * 60LL * 24LL * 30LL; // 30 days
1666 static constexpr long long MAX_TIME_IN_FUTURE = 60000LL * 90LL; // 90 minutes
1667
1668 if (mTimeIsAccurate) {
1669 return true;
1670 }
Keun-young Parkb5938422017-03-23 13:46:24 -07001671 if (mShuttingDown) return true;
Damien Bargiacchi97480862016-03-29 14:55:55 -07001672 struct stat statResult;
Damien Bargiacchi9071db12016-10-28 17:38:22 -07001673
1674 if(stat(TIME_FORMAT_12_HOUR_FLAG_FILE_PATH, &statResult) == 0) {
1675 mTimeFormat12Hour = true;
1676 }
1677
Damien Bargiacchi97480862016-03-29 14:55:55 -07001678 if(stat(ACCURATE_TIME_FLAG_FILE_PATH, &statResult) == 0) {
1679 mTimeIsAccurate = true;
1680 return true;
1681 }
1682
1683 FILE* file = fopen(LAST_TIME_CHANGED_FILE_PATH, "r");
Yi Kong911ac232019-03-24 01:49:02 -07001684 if (file != nullptr) {
Damien Bargiacchi97480862016-03-29 14:55:55 -07001685 long long lastChangedTime = 0;
1686 fscanf(file, "%lld", &lastChangedTime);
1687 fclose(file);
1688 if (lastChangedTime > 0) {
1689 struct timespec now;
1690 clock_gettime(CLOCK_REALTIME, &now);
1691 // Match the Java timestamp format
1692 long long rtcNow = (now.tv_sec * 1000LL) + (now.tv_nsec / 1000000LL);
Damien Bargiacchi96762812016-07-12 15:53:40 -07001693 if (ACCURATE_TIME_EPOCH < rtcNow
1694 && lastChangedTime > (rtcNow - MAX_TIME_IN_PAST)
1695 && lastChangedTime < (rtcNow + MAX_TIME_IN_FUTURE)) {
Damien Bargiacchi97480862016-03-29 14:55:55 -07001696 mTimeIsAccurate = true;
1697 }
1698 }
1699 }
1700
1701 return mTimeIsAccurate;
1702}
1703
1704BootAnimation::TimeCheckThread::TimeCheckThread(BootAnimation* bootAnimation) : Thread(false),
1705 mInotifyFd(-1), mSystemWd(-1), mTimeWd(-1), mBootAnimation(bootAnimation) {}
1706
1707BootAnimation::TimeCheckThread::~TimeCheckThread() {
1708 // mInotifyFd may be -1 but that's ok since we're not at risk of attempting to close a valid FD.
1709 close(mInotifyFd);
1710}
1711
1712bool BootAnimation::TimeCheckThread::threadLoop() {
1713 bool shouldLoop = doThreadLoop() && !mBootAnimation->mTimeIsAccurate
1714 && mBootAnimation->mClockEnabled;
1715 if (!shouldLoop) {
1716 close(mInotifyFd);
1717 mInotifyFd = -1;
1718 }
1719 return shouldLoop;
1720}
1721
1722bool BootAnimation::TimeCheckThread::doThreadLoop() {
1723 static constexpr int BUFF_LEN (10 * (sizeof(struct inotify_event) + NAME_MAX + 1));
1724
1725 // Poll instead of doing a blocking read so the Thread can exit if requested.
1726 struct pollfd pfd = { mInotifyFd, POLLIN, 0 };
1727 ssize_t pollResult = poll(&pfd, 1, 1000);
1728
1729 if (pollResult == 0) {
1730 return true;
1731 } else if (pollResult < 0) {
Wei Wang159a4102018-10-23 23:15:58 -07001732 SLOGE("Could not poll inotify events");
Damien Bargiacchi97480862016-03-29 14:55:55 -07001733 return false;
1734 }
1735
1736 char buff[BUFF_LEN] __attribute__ ((aligned(__alignof__(struct inotify_event))));;
1737 ssize_t length = read(mInotifyFd, buff, BUFF_LEN);
1738 if (length == 0) {
1739 return true;
1740 } else if (length < 0) {
Wei Wang159a4102018-10-23 23:15:58 -07001741 SLOGE("Could not read inotify events");
Damien Bargiacchi97480862016-03-29 14:55:55 -07001742 return false;
1743 }
1744
1745 const struct inotify_event *event;
1746 for (char* ptr = buff; ptr < buff + length; ptr += sizeof(struct inotify_event) + event->len) {
1747 event = (const struct inotify_event *) ptr;
1748 if (event->wd == mSystemWd && strcmp(SYSTEM_TIME_DIR_NAME, event->name) == 0) {
1749 addTimeDirWatch();
1750 } else if (event->wd == mTimeWd && (strcmp(LAST_TIME_CHANGED_FILE_NAME, event->name) == 0
1751 || strcmp(ACCURATE_TIME_FLAG_FILE_NAME, event->name) == 0)) {
1752 return !mBootAnimation->updateIsTimeAccurate();
1753 }
1754 }
1755
1756 return true;
1757}
1758
1759void BootAnimation::TimeCheckThread::addTimeDirWatch() {
1760 mTimeWd = inotify_add_watch(mInotifyFd, SYSTEM_TIME_DIR_PATH,
1761 IN_CLOSE_WRITE | IN_MOVED_TO | IN_ATTRIB);
1762 if (mTimeWd > 0) {
1763 // No need to watch for the time directory to be created if it already exists
1764 inotify_rm_watch(mInotifyFd, mSystemWd);
1765 mSystemWd = -1;
1766 }
1767}
1768
1769status_t BootAnimation::TimeCheckThread::readyToRun() {
1770 mInotifyFd = inotify_init();
1771 if (mInotifyFd < 0) {
Wei Wang159a4102018-10-23 23:15:58 -07001772 SLOGE("Could not initialize inotify fd");
Damien Bargiacchi97480862016-03-29 14:55:55 -07001773 return NO_INIT;
1774 }
1775
1776 mSystemWd = inotify_add_watch(mInotifyFd, SYSTEM_DATA_DIR_PATH, IN_CREATE | IN_ATTRIB);
1777 if (mSystemWd < 0) {
1778 close(mInotifyFd);
1779 mInotifyFd = -1;
Dan Sandler562d46d2020-05-11 17:09:22 -04001780 SLOGE("Could not add watch for %s: %s", SYSTEM_DATA_DIR_PATH, strerror(errno));
Damien Bargiacchi97480862016-03-29 14:55:55 -07001781 return NO_INIT;
1782 }
1783
1784 addTimeDirWatch();
1785
1786 if (mBootAnimation->updateIsTimeAccurate()) {
1787 close(mInotifyFd);
1788 mInotifyFd = -1;
1789 return ALREADY_EXISTS;
1790 }
1791
1792 return NO_ERROR;
1793}
1794
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001795// ---------------------------------------------------------------------------
1796
Marin Shalamanov9070c052020-05-18 19:05:17 +02001797} // namespace android