blob: a25823b07b22a83056afcbfe4adccf266e0639a5 [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
Josh Yangf95b2002021-12-23 14:32:28 -080084static const char BOOTANIM_DATA_DIR_PATH[] = "/data/bootanim";
85static const char BOOTANIM_TIME_DIR_NAME[] = "time";
86static const char BOOTANIM_TIME_DIR_PATH[] = "/data/bootanim/time";
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -070087static const char CLOCK_FONT_ASSET[] = "images/clock_font.png";
88static const char CLOCK_FONT_ZIP_NAME[] = "clock_font.png";
Nicolas Geoffray41fffb42020-12-15 10:50:29 +000089static const char PROGRESS_FONT_ASSET[] = "images/progress_font.png";
90static const char PROGRESS_FONT_ZIP_NAME[] = "progress_font.png";
Damien Bargiacchi97480862016-03-29 14:55:55 -070091static const char LAST_TIME_CHANGED_FILE_NAME[] = "last_time_change";
Josh Yangf95b2002021-12-23 14:32:28 -080092static const char LAST_TIME_CHANGED_FILE_PATH[] = "/data/bootanim/time/last_time_change";
Damien Bargiacchi97480862016-03-29 14:55:55 -070093static const char ACCURATE_TIME_FLAG_FILE_NAME[] = "time_is_accurate";
Josh Yangf95b2002021-12-23 14:32:28 -080094static const char ACCURATE_TIME_FLAG_FILE_PATH[] = "/data/bootanim/time/time_is_accurate";
95static const char TIME_FORMAT_12_HOUR_FLAG_FILE_PATH[] = "/data/bootanim/time/time_format_12_hour";
Damien Bargiacchi96762812016-07-12 15:53:40 -070096// Java timestamp format. Don't show the clock if the date is before 2000-01-01 00:00:00.
97static const long long ACCURATE_TIME_EPOCH = 946684800000;
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -070098static constexpr char FONT_BEGIN_CHAR = ' ';
99static constexpr char FONT_END_CHAR = '~' + 1;
100static constexpr size_t FONT_NUM_CHARS = FONT_END_CHAR - FONT_BEGIN_CHAR + 1;
101static constexpr size_t FONT_NUM_COLS = 16;
102static constexpr size_t FONT_NUM_ROWS = FONT_NUM_CHARS / FONT_NUM_COLS;
103static const int TEXT_CENTER_VALUE = INT_MAX;
104static const int TEXT_MISSING_VALUE = INT_MIN;
Damien Bargiacchi97480862016-03-29 14:55:55 -0700105static const char EXIT_PROP_NAME[] = "service.bootanim.exit";
Nicolas Geoffray41fffb42020-12-15 10:50:29 +0000106static const char PROGRESS_PROP_NAME[] = "service.bootanim.progress";
Huihong Luod4a217e2020-01-16 15:56:33 -0800107static const char DISPLAYS_PROP_NAME[] = "persist.service.bootanim.displays";
Thiébaud Weksteen08de4432021-11-18 13:25:59 +1100108static const char CLOCK_ENABLED_PROP_NAME[] = "persist.sys.bootanim.clock.enabled";
Kyeongkab.Nam35a8a112019-07-17 16:41:48 +0900109static const int ANIM_ENTRY_NAME_MAX = ANIM_PATH_MAX + 1;
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700110static constexpr size_t TEXT_POS_LEN_MAX = 16;
Shan Huang2b31a362021-08-16 16:45:39 +0000111static const int DYNAMIC_COLOR_COUNT = 4;
Lucas Dupin8eedc022021-07-29 21:49:07 +0000112static const char U_TEXTURE[] = "uTexture";
113static const char U_FADE[] = "uFade";
114static const char U_CROP_AREA[] = "uCropArea";
Shan Huang2b31a362021-08-16 16:45:39 +0000115static const char U_START_COLOR_PREFIX[] = "uStartColor";
116static const char U_END_COLOR_PREFIX[] = "uEndColor";
117static const char U_COLOR_PROGRESS[] = "uColorProgress";
Lucas Dupin8eedc022021-07-29 21:49:07 +0000118static const char A_UV[] = "aUv";
119static const char A_POSITION[] = "aPosition";
120static const char VERTEX_SHADER_SOURCE[] = R"(
121 precision mediump float;
122 attribute vec4 aPosition;
123 attribute highp vec2 aUv;
124 varying highp vec2 vUv;
125 void main() {
126 gl_Position = aPosition;
127 vUv = aUv;
128 })";
Shan Huang2b31a362021-08-16 16:45:39 +0000129static const char IMAGE_FRAG_DYNAMIC_COLORING_SHADER_SOURCE[] = R"(
130 precision mediump float;
Shan Huang81846ba2021-09-07 19:07:47 +0000131 const float cWhiteMaskThreshold = 0.05;
Shan Huang2b31a362021-08-16 16:45:39 +0000132 uniform sampler2D uTexture;
133 uniform float uFade;
134 uniform float uColorProgress;
Andrew Zengf9f58222022-11-08 11:10:48 -0800135 uniform vec3 uStartColor0;
136 uniform vec3 uStartColor1;
137 uniform vec3 uStartColor2;
138 uniform vec3 uStartColor3;
139 uniform vec3 uEndColor0;
140 uniform vec3 uEndColor1;
141 uniform vec3 uEndColor2;
142 uniform vec3 uEndColor3;
Shan Huang2b31a362021-08-16 16:45:39 +0000143 varying highp vec2 vUv;
144 void main() {
145 vec4 mask = texture2D(uTexture, vUv);
Shan Huang488ff742021-08-25 20:10:46 +0000146 float r = mask.r;
147 float g = mask.g;
148 float b = mask.b;
149 float a = mask.a;
150 // If all channels have values, render pixel as a shade of white.
151 float useWhiteMask = step(cWhiteMaskThreshold, r)
152 * step(cWhiteMaskThreshold, g)
153 * step(cWhiteMaskThreshold, b)
154 * step(cWhiteMaskThreshold, a);
Andrew Zengf9f58222022-11-08 11:10:48 -0800155 vec3 color = r * mix(uStartColor0, uEndColor0, uColorProgress)
Shan Huang488ff742021-08-25 20:10:46 +0000156 + g * mix(uStartColor1, uEndColor1, uColorProgress)
157 + b * mix(uStartColor2, uEndColor2, uColorProgress)
158 + a * mix(uStartColor3, uEndColor3, uColorProgress);
Andrew Zengf9f58222022-11-08 11:10:48 -0800159 color = mix(color, vec3((r + g + b + a) * 0.25), useWhiteMask);
160 gl_FragColor = vec4(color.x, color.y, color.z, (1.0 - uFade));
Shan Huang2b31a362021-08-16 16:45:39 +0000161 })";
Lucas Dupin8eedc022021-07-29 21:49:07 +0000162static const char IMAGE_FRAG_SHADER_SOURCE[] = R"(
163 precision mediump float;
164 uniform sampler2D uTexture;
165 uniform float uFade;
166 varying highp vec2 vUv;
167 void main() {
168 vec4 color = texture2D(uTexture, vUv);
Shan Huang2b31a362021-08-16 16:45:39 +0000169 gl_FragColor = vec4(color.x, color.y, color.z, (1.0 - uFade)) * color.a;
Lucas Dupin8eedc022021-07-29 21:49:07 +0000170 })";
171static const char TEXT_FRAG_SHADER_SOURCE[] = R"(
172 precision mediump float;
173 uniform sampler2D uTexture;
174 uniform vec4 uCropArea;
175 varying highp vec2 vUv;
176 void main() {
177 vec2 uv = vec2(mix(uCropArea.x, uCropArea.z, vUv.x),
178 mix(uCropArea.y, uCropArea.w, vUv.y));
179 gl_FragColor = texture2D(uTexture, uv);
180 })";
181
182static GLfloat quadPositions[] = {
183 -0.5f, -0.5f,
184 +0.5f, -0.5f,
185 +0.5f, +0.5f,
186 +0.5f, +0.5f,
187 -0.5f, +0.5f,
188 -0.5f, -0.5f
189};
190static GLfloat quadUVs[] = {
191 0.0f, 1.0f,
192 1.0f, 1.0f,
193 1.0f, 0.0f,
194 1.0f, 0.0f,
195 0.0f, 0.0f,
196 0.0f, 1.0f
197};
Narayan Kamathafd31e02013-12-03 13:16:03 +0000198
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800199// ---------------------------------------------------------------------------
200
Ed Coyne7464ac92017-06-08 12:26:48 -0700201BootAnimation::BootAnimation(sp<Callbacks> callbacks)
Marin Shalamanov9070c052020-05-18 19:05:17 +0200202 : Thread(false), mLooper(new Looper(false)), mClockEnabled(true), mTimeIsAccurate(false),
Yi Kong911ac232019-03-24 01:49:02 -0700203 mTimeFormat12Hour(false), mTimeCheckThread(nullptr), mCallbacks(callbacks) {
Mathias Agopian627e7b52009-05-21 19:21:59 -0700204 mSession = new SurfaceComposerClient();
Geoffrey Pitsch290c4352016-08-09 14:35:10 -0400205
Keun-young Parkb5938422017-03-23 13:46:24 -0700206 std::string powerCtl = android::base::GetProperty("sys.powerctl", "");
207 if (powerCtl.empty()) {
208 mShuttingDown = false;
209 } else {
210 mShuttingDown = true;
211 }
Huihong Luo50a2f362018-08-11 09:27:57 -0700212 ALOGD("%sAnimationStartTiming start time: %" PRId64 "ms", mShuttingDown ? "Shutdown" : "Boot",
213 elapsedRealtime());
214}
215
216BootAnimation::~BootAnimation() {
217 if (mAnimation != nullptr) {
218 releaseAnimation(mAnimation);
219 mAnimation = nullptr;
220 }
221 ALOGD("%sAnimationStopTiming start time: %" PRId64 "ms", mShuttingDown ? "Shutdown" : "Boot",
222 elapsedRealtime());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800223}
224
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800225void BootAnimation::onFirstRef() {
Mathias Agopianbc726112009-09-23 15:44:05 -0700226 status_t err = mSession->linkToComposerDeath(this);
Wei Wang159a4102018-10-23 23:15:58 -0700227 SLOGE_IF(err, "linkToComposerDeath failed (%s) ", strerror(-err));
Mathias Agopian8434c532009-09-23 18:52:49 -0700228 if (err == NO_ERROR) {
Huihong Luo50a2f362018-08-11 09:27:57 -0700229 // Load the animation content -- this can be slow (eg 200ms)
230 // called before waitForSurfaceFlinger() in main() to avoid wait
231 ALOGD("%sAnimationPreloadTiming start time: %" PRId64 "ms",
232 mShuttingDown ? "Shutdown" : "Boot", elapsedRealtime());
233 preloadAnimation();
234 ALOGD("%sAnimationPreloadStopTiming start time: %" PRId64 "ms",
235 mShuttingDown ? "Shutdown" : "Boot", elapsedRealtime());
Mathias Agopianbc726112009-09-23 15:44:05 -0700236 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800237}
238
Mathias Agopianbc726112009-09-23 15:44:05 -0700239sp<SurfaceComposerClient> BootAnimation::session() const {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800240 return mSession;
241}
242
Marin Shalamanov9070c052020-05-18 19:05:17 +0200243void BootAnimation::binderDied(const wp<IBinder>&) {
Mathias Agopianbc726112009-09-23 15:44:05 -0700244 // woah, surfaceflinger died!
Wei Wang159a4102018-10-23 23:15:58 -0700245 SLOGD("SurfaceFlinger died, exiting...");
Mathias Agopianbc726112009-09-23 15:44:05 -0700246
247 // calling requestExit() is not enough here because the Surface code
248 // might be blocked on a condition variable that will never be updated.
249 kill( getpid(), SIGKILL );
250 requestExit();
251}
252
Shan Huang2b31a362021-08-16 16:45:39 +0000253static void* decodeImage(const void* encodedData, size_t dataLength, AndroidBitmapInfo* outInfo,
254 bool premultiplyAlpha) {
Derek Sollenberger22d0d4c72020-04-08 10:53:46 -0400255 AImageDecoder* decoder = nullptr;
256 AImageDecoder_createFromBuffer(encodedData, dataLength, &decoder);
257 if (!decoder) {
258 return nullptr;
259 }
260
261 const AImageDecoderHeaderInfo* info = AImageDecoder_getHeaderInfo(decoder);
262 outInfo->width = AImageDecoderHeaderInfo_getWidth(info);
263 outInfo->height = AImageDecoderHeaderInfo_getHeight(info);
264 outInfo->format = AImageDecoderHeaderInfo_getAndroidBitmapFormat(info);
265 outInfo->stride = AImageDecoder_getMinimumStride(decoder);
266 outInfo->flags = 0;
267
Shan Huang2b31a362021-08-16 16:45:39 +0000268 if (!premultiplyAlpha) {
269 AImageDecoder_setUnpremultipliedRequired(decoder, true);
270 }
271
Derek Sollenberger22d0d4c72020-04-08 10:53:46 -0400272 const size_t size = outInfo->stride * outInfo->height;
273 void* pixels = malloc(size);
274 int result = AImageDecoder_decodeImage(decoder, pixels, outInfo->stride, size);
275 AImageDecoder_delete(decoder);
276
277 if (result != ANDROID_IMAGE_DECODER_SUCCESS) {
278 free(pixels);
279 return nullptr;
280 }
281 return pixels;
282}
283
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800284status_t BootAnimation::initTexture(Texture* texture, AssetManager& assets,
Shan Huang2b31a362021-08-16 16:45:39 +0000285 const char* name, bool premultiplyAlpha) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800286 Asset* asset = assets.open(name, Asset::ACCESS_BUFFER);
Yi Kong911ac232019-03-24 01:49:02 -0700287 if (asset == nullptr)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800288 return NO_INIT;
Derek Sollenberger22d0d4c72020-04-08 10:53:46 -0400289
290 AndroidBitmapInfo bitmapInfo;
Shan Huang2b31a362021-08-16 16:45:39 +0000291 void* pixels = decodeImage(asset->getBuffer(false), asset->getLength(), &bitmapInfo,
292 premultiplyAlpha);
Derek Sollenberger22d0d4c72020-04-08 10:53:46 -0400293 auto pixelDeleter = std::unique_ptr<void, decltype(free)*>{ pixels, free };
294
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800295 asset->close();
296 delete asset;
297
Derek Sollenberger22d0d4c72020-04-08 10:53:46 -0400298 if (!pixels) {
299 return NO_INIT;
300 }
301
302 const int w = bitmapInfo.width;
303 const int h = bitmapInfo.height;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800304
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800305 texture->w = w;
306 texture->h = h;
307
308 glGenTextures(1, &texture->name);
309 glBindTexture(GL_TEXTURE_2D, texture->name);
310
Derek Sollenberger22d0d4c72020-04-08 10:53:46 -0400311 switch (bitmapInfo.format) {
312 case ANDROID_BITMAP_FORMAT_A_8:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800313 glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, w, h, 0, GL_ALPHA,
Derek Sollenberger22d0d4c72020-04-08 10:53:46 -0400314 GL_UNSIGNED_BYTE, pixels);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800315 break;
Derek Sollenberger22d0d4c72020-04-08 10:53:46 -0400316 case ANDROID_BITMAP_FORMAT_RGBA_4444:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800317 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA,
Derek Sollenberger22d0d4c72020-04-08 10:53:46 -0400318 GL_UNSIGNED_SHORT_4_4_4_4, pixels);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800319 break;
Derek Sollenberger22d0d4c72020-04-08 10:53:46 -0400320 case ANDROID_BITMAP_FORMAT_RGBA_8888:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800321 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA,
Derek Sollenberger22d0d4c72020-04-08 10:53:46 -0400322 GL_UNSIGNED_BYTE, pixels);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800323 break;
Derek Sollenberger22d0d4c72020-04-08 10:53:46 -0400324 case ANDROID_BITMAP_FORMAT_RGB_565:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800325 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, w, h, 0, GL_RGB,
Derek Sollenberger22d0d4c72020-04-08 10:53:46 -0400326 GL_UNSIGNED_SHORT_5_6_5, pixels);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800327 break;
328 default:
329 break;
330 }
331
Lucas Dupin8eedc022021-07-29 21:49:07 +0000332 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
333 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
334 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
335 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700336
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800337 return NO_ERROR;
338}
339
Shan Huang2b31a362021-08-16 16:45:39 +0000340status_t BootAnimation::initTexture(FileMap* map, int* width, int* height,
341 bool premultiplyAlpha) {
Derek Sollenberger22d0d4c72020-04-08 10:53:46 -0400342 AndroidBitmapInfo bitmapInfo;
Shan Huang2b31a362021-08-16 16:45:39 +0000343 void* pixels = decodeImage(map->getDataPtr(), map->getDataLength(), &bitmapInfo,
344 premultiplyAlpha);
Derek Sollenberger22d0d4c72020-04-08 10:53:46 -0400345 auto pixelDeleter = std::unique_ptr<void, decltype(free)*>{ pixels, free };
Mathias Agopiana8826d62009-10-01 03:10:14 -0700346
Mykola Kondratenko0c1eeb32014-04-15 09:35:44 +0200347 // FileMap memory is never released until application exit.
348 // Release it now as the texture is already loaded and the memory used for
349 // the packed resource can be released.
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700350 delete map;
Mykola Kondratenko0c1eeb32014-04-15 09:35:44 +0200351
Derek Sollenberger22d0d4c72020-04-08 10:53:46 -0400352 if (!pixels) {
353 return NO_INIT;
354 }
355
356 const int w = bitmapInfo.width;
357 const int h = bitmapInfo.height;
Mathias Agopiana8826d62009-10-01 03:10:14 -0700358
Mathias Agopiana8826d62009-10-01 03:10:14 -0700359 int tw = 1 << (31 - __builtin_clz(w));
360 int th = 1 << (31 - __builtin_clz(h));
361 if (tw < w) tw <<= 1;
362 if (th < h) th <<= 1;
363
Derek Sollenberger22d0d4c72020-04-08 10:53:46 -0400364 switch (bitmapInfo.format) {
365 case ANDROID_BITMAP_FORMAT_RGBA_8888:
Sai Kiran Korwar27167492015-07-07 20:00:06 +0530366 if (!mUseNpotTextures && (tw != w || th != h)) {
Mathias Agopiana8826d62009-10-01 03:10:14 -0700367 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, tw, th, 0, GL_RGBA,
Yi Kong911ac232019-03-24 01:49:02 -0700368 GL_UNSIGNED_BYTE, nullptr);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700369 glTexSubImage2D(GL_TEXTURE_2D, 0,
Derek Sollenberger22d0d4c72020-04-08 10:53:46 -0400370 0, 0, w, h, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700371 } else {
Sai Kiran Korwar27167492015-07-07 20:00:06 +0530372 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA,
Derek Sollenberger22d0d4c72020-04-08 10:53:46 -0400373 GL_UNSIGNED_BYTE, pixels);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700374 }
375 break;
376
Derek Sollenberger22d0d4c72020-04-08 10:53:46 -0400377 case ANDROID_BITMAP_FORMAT_RGB_565:
Sai Kiran Korwar27167492015-07-07 20:00:06 +0530378 if (!mUseNpotTextures && (tw != w || th != h)) {
Mathias Agopiana8826d62009-10-01 03:10:14 -0700379 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, tw, th, 0, GL_RGB,
Yi Kong911ac232019-03-24 01:49:02 -0700380 GL_UNSIGNED_SHORT_5_6_5, nullptr);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700381 glTexSubImage2D(GL_TEXTURE_2D, 0,
Derek Sollenberger22d0d4c72020-04-08 10:53:46 -0400382 0, 0, w, h, GL_RGB, GL_UNSIGNED_SHORT_5_6_5, pixels);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700383 } else {
Sai Kiran Korwar27167492015-07-07 20:00:06 +0530384 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, w, h, 0, GL_RGB,
Derek Sollenberger22d0d4c72020-04-08 10:53:46 -0400385 GL_UNSIGNED_SHORT_5_6_5, pixels);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700386 }
387 break;
388 default:
389 break;
390 }
391
Lucas Dupin8eedc022021-07-29 21:49:07 +0000392 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
393 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
394 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
395 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700396
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700397 *width = w;
398 *height = h;
399
Mathias Agopiana8826d62009-10-01 03:10:14 -0700400 return NO_ERROR;
401}
402
Marin Shalamanov9070c052020-05-18 19:05:17 +0200403class BootAnimation::DisplayEventCallback : public LooperCallback {
404 BootAnimation* mBootAnimation;
405
406public:
407 DisplayEventCallback(BootAnimation* bootAnimation) {
408 mBootAnimation = bootAnimation;
409 }
410
411 int handleEvent(int /* fd */, int events, void* /* data */) {
412 if (events & (Looper::EVENT_ERROR | Looper::EVENT_HANGUP)) {
413 ALOGE("Display event receiver pipe was closed or an error occurred. events=0x%x",
414 events);
415 return 0; // remove the callback
416 }
417
418 if (!(events & Looper::EVENT_INPUT)) {
419 ALOGW("Received spurious callback for unhandled poll event. events=0x%x", events);
420 return 1; // keep the callback
421 }
422
423 constexpr int kBufferSize = 100;
424 DisplayEventReceiver::Event buffer[kBufferSize];
425 ssize_t numEvents;
426 do {
427 numEvents = mBootAnimation->mDisplayEventReceiver->getEvents(buffer, kBufferSize);
428 for (size_t i = 0; i < static_cast<size_t>(numEvents); i++) {
429 const auto& event = buffer[i];
430 if (event.header.type == DisplayEventReceiver::DISPLAY_EVENT_HOTPLUG) {
431 SLOGV("Hotplug received");
432
433 if (!event.hotplug.connected) {
434 // ignore hotplug disconnect
435 continue;
436 }
437 auto token = SurfaceComposerClient::getPhysicalDisplayToken(
438 event.header.displayId);
439
440 if (token != mBootAnimation->mDisplayToken) {
441 // ignore hotplug of a secondary display
442 continue;
443 }
444
Marin Shalamanovcb783ca2021-01-29 21:26:07 +0100445 DisplayMode displayMode;
446 const status_t error = SurfaceComposerClient::getActiveDisplayMode(
447 mBootAnimation->mDisplayToken, &displayMode);
Marin Shalamanov9070c052020-05-18 19:05:17 +0200448 if (error != NO_ERROR) {
Marin Shalamanovcb783ca2021-01-29 21:26:07 +0100449 SLOGE("Can't get active display mode.");
Marin Shalamanov9070c052020-05-18 19:05:17 +0200450 }
Marin Shalamanovcb783ca2021-01-29 21:26:07 +0100451 mBootAnimation->resizeSurface(displayMode.resolution.getWidth(),
452 displayMode.resolution.getHeight());
Marin Shalamanov9070c052020-05-18 19:05:17 +0200453 }
454 }
455 } while (numEvents > 0);
456
457 return 1; // keep the callback
458 }
459};
460
461EGLConfig BootAnimation::getEglConfig(const EGLDisplay& display) {
462 const EGLint attribs[] = {
463 EGL_RED_SIZE, 8,
464 EGL_GREEN_SIZE, 8,
465 EGL_BLUE_SIZE, 8,
466 EGL_DEPTH_SIZE, 0,
467 EGL_NONE
468 };
469 EGLint numConfigs;
470 EGLConfig config;
471 eglChooseConfig(display, attribs, &config, 1, &numConfigs);
472 return config;
473}
474
Marin Shalamanov047802d2020-05-19 23:55:12 +0200475ui::Size BootAnimation::limitSurfaceSize(int width, int height) const {
476 ui::Size limited(width, height);
477 bool wasLimited = false;
478 const float aspectRatio = float(width) / float(height);
479 if (mMaxWidth != 0 && width > mMaxWidth) {
480 limited.height = mMaxWidth / aspectRatio;
481 limited.width = mMaxWidth;
482 wasLimited = true;
483 }
484 if (mMaxHeight != 0 && limited.height > mMaxHeight) {
485 limited.height = mMaxHeight;
486 limited.width = mMaxHeight * aspectRatio;
487 wasLimited = true;
488 }
489 SLOGV_IF(wasLimited, "Surface size has been limited to [%dx%d] from [%dx%d]",
490 limited.width, limited.height, width, height);
491 return limited;
492}
493
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800494status_t BootAnimation::readyToRun() {
495 mAssets.addDefaultAssets();
496
Dominik Laskowski3316a0a2019-01-25 02:56:41 -0800497 mDisplayToken = SurfaceComposerClient::getInternalDisplayToken();
498 if (mDisplayToken == nullptr)
Dominik Laskowski69b281d2019-11-22 14:13:12 -0800499 return NAME_NOT_FOUND;
Dominik Laskowski3316a0a2019-01-25 02:56:41 -0800500
Marin Shalamanovcb783ca2021-01-29 21:26:07 +0100501 DisplayMode displayMode;
Dominik Laskowski69b281d2019-11-22 14:13:12 -0800502 const status_t error =
Marin Shalamanovcb783ca2021-01-29 21:26:07 +0100503 SurfaceComposerClient::getActiveDisplayMode(mDisplayToken, &displayMode);
Dominik Laskowski69b281d2019-11-22 14:13:12 -0800504 if (error != NO_ERROR)
505 return error;
506
Marin Shalamanov047802d2020-05-19 23:55:12 +0200507 mMaxWidth = android::base::GetIntProperty("ro.surface_flinger.max_graphics_width", 0);
508 mMaxHeight = android::base::GetIntProperty("ro.surface_flinger.max_graphics_height", 0);
Marin Shalamanovcb783ca2021-01-29 21:26:07 +0100509 ui::Size resolution = displayMode.resolution;
Marin Shalamanov047802d2020-05-19 23:55:12 +0200510 resolution = limitSurfaceSize(resolution.width, resolution.height);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800511 // create the native surface
Jeff Brown0b722fe2012-08-24 22:40:14 -0700512 sp<SurfaceControl> control = session()->createSurface(String8("BootAnimation"),
xi yuc3415442022-05-09 08:58:02 +0000513 resolution.getWidth(), resolution.getHeight(), PIXEL_FORMAT_RGB_565,
514 ISurfaceComposerClient::eOpaque);
Mathias Agopian439863f2011-06-28 19:09:31 -0700515
Robert Carre13b58e2017-08-31 14:50:44 -0700516 SurfaceComposerClient::Transaction t;
Huihong Luof97c9e22019-05-29 11:08:01 -0700517
518 // this guest property specifies multi-display IDs to show the boot animation
519 // multiple ids can be set with comma (,) as separator, for example:
Huihong Luod4a217e2020-01-16 15:56:33 -0800520 // setprop persist.boot.animation.displays 19260422155234049,19261083906282754
Marin Shalamanov11599ee2020-07-27 21:31:48 +0200521 Vector<PhysicalDisplayId> physicalDisplayIds;
Huihong Luof97c9e22019-05-29 11:08:01 -0700522 char displayValue[PROPERTY_VALUE_MAX] = "";
Huihong Luod4a217e2020-01-16 15:56:33 -0800523 property_get(DISPLAYS_PROP_NAME, displayValue, "");
Huihong Luof97c9e22019-05-29 11:08:01 -0700524 bool isValid = displayValue[0] != '\0';
525 if (isValid) {
526 char *p = displayValue;
527 while (*p) {
528 if (!isdigit(*p) && *p != ',') {
529 isValid = false;
530 break;
531 }
532 p ++;
533 }
534 if (!isValid)
Huihong Luod4a217e2020-01-16 15:56:33 -0800535 SLOGE("Invalid syntax for the value of system prop: %s", DISPLAYS_PROP_NAME);
Huihong Luof97c9e22019-05-29 11:08:01 -0700536 }
537 if (isValid) {
538 std::istringstream stream(displayValue);
Marin Shalamanov11599ee2020-07-27 21:31:48 +0200539 for (PhysicalDisplayId id; stream >> id.value; ) {
Huihong Luof97c9e22019-05-29 11:08:01 -0700540 physicalDisplayIds.add(id);
541 if (stream.peek() == ',')
542 stream.ignore();
543 }
544
545 // In the case of multi-display, boot animation shows on the specified displays
546 // in addition to the primary display
Dominik Laskowski989de3d2021-07-27 21:09:34 -0700547 const auto ids = SurfaceComposerClient::getPhysicalDisplayIds();
548 for (const auto id : physicalDisplayIds) {
Huihong Luof97c9e22019-05-29 11:08:01 -0700549 if (std::find(ids.begin(), ids.end(), id) != ids.end()) {
Dominik Laskowski989de3d2021-07-27 21:09:34 -0700550 if (const auto token = SurfaceComposerClient::getPhysicalDisplayToken(id)) {
551 t.setDisplayLayerStack(token, ui::DEFAULT_LAYER_STACK);
552 }
Huihong Luof97c9e22019-05-29 11:08:01 -0700553 }
554 }
Dominik Laskowski989de3d2021-07-27 21:09:34 -0700555 t.setLayerStack(control, ui::DEFAULT_LAYER_STACK);
Huihong Luof97c9e22019-05-29 11:08:01 -0700556 }
557
Robert Carre13b58e2017-08-31 14:50:44 -0700558 t.setLayer(control, 0x40000000)
559 .apply();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800560
Mathias Agopian17f638b2009-04-16 20:04:08 -0700561 sp<Surface> s = control->getSurface();
562
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800563 // initialize opengl and egl
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800564 EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
Yi Kong911ac232019-03-24 01:49:02 -0700565 eglInitialize(display, nullptr, nullptr);
Marin Shalamanov9070c052020-05-18 19:05:17 +0200566 EGLConfig config = getEglConfig(display);
567 EGLSurface surface = eglCreateWindowSurface(display, config, s.get(), nullptr);
Lucas Dupin8eedc022021-07-29 21:49:07 +0000568 // Initialize egl context with client version number 2.0.
569 EGLint contextAttributes[] = {EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE};
570 EGLContext context = eglCreateContext(display, config, nullptr, contextAttributes);
Marin Shalamanov9070c052020-05-18 19:05:17 +0200571 EGLint w, h;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800572 eglQuerySurface(display, surface, EGL_WIDTH, &w);
573 eglQuerySurface(display, surface, EGL_HEIGHT, &h);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700574
Mathias Agopianabac0102009-07-31 14:47:00 -0700575 if (eglMakeCurrent(display, surface, surface, context) == EGL_FALSE)
576 return NO_INIT;
Mathias Agopiana8826d62009-10-01 03:10:14 -0700577
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800578 mDisplay = display;
579 mContext = context;
580 mSurface = surface;
joenchen85b339f2022-04-11 23:58:34 +0800581 mInitWidth = mWidth = w;
582 mInitHeight = mHeight = h;
Mathias Agopian17f638b2009-04-16 20:04:08 -0700583 mFlingerSurfaceControl = control;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800584 mFlingerSurface = s;
Adrian Roos9ee5dff2018-08-22 20:19:49 +0200585 mTargetInset = -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800586
Yegor Malyshev6925e1b2020-09-10 16:42:20 +0200587 projectSceneToWindow();
588
Marin Shalamanov9070c052020-05-18 19:05:17 +0200589 // Register a display event receiver
590 mDisplayEventReceiver = std::make_unique<DisplayEventReceiver>();
591 status_t status = mDisplayEventReceiver->initCheck();
592 SLOGE_IF(status != NO_ERROR, "Initialization of DisplayEventReceiver failed with status: %d",
593 status);
594 mLooper->addFd(mDisplayEventReceiver->getFd(), 0, Looper::EVENT_INPUT,
595 new DisplayEventCallback(this), nullptr);
596
Huihong Luo50a2f362018-08-11 09:27:57 -0700597 return NO_ERROR;
598}
599
Yegor Malyshev6925e1b2020-09-10 16:42:20 +0200600void BootAnimation::projectSceneToWindow() {
601 glViewport(0, 0, mWidth, mHeight);
602 glScissor(0, 0, mWidth, mHeight);
Yegor Malyshev6925e1b2020-09-10 16:42:20 +0200603}
604
Marin Shalamanov9070c052020-05-18 19:05:17 +0200605void BootAnimation::resizeSurface(int newWidth, int newHeight) {
606 // We assume this function is called on the animation thread.
607 if (newWidth == mWidth && newHeight == mHeight) {
608 return;
609 }
610 SLOGV("Resizing the boot animation surface to %d %d", newWidth, newHeight);
611
612 eglMakeCurrent(mDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
613 eglDestroySurface(mDisplay, mSurface);
614
joenchen85b339f2022-04-11 23:58:34 +0800615 mFlingerSurfaceControl->updateDefaultBufferSize(newWidth, newHeight);
Marin Shalamanov047802d2020-05-19 23:55:12 +0200616 const auto limitedSize = limitSurfaceSize(newWidth, newHeight);
617 mWidth = limitedSize.width;
618 mHeight = limitedSize.height;
Marin Shalamanov9070c052020-05-18 19:05:17 +0200619
620 SurfaceComposerClient::Transaction t;
621 t.setSize(mFlingerSurfaceControl, mWidth, mHeight);
622 t.apply();
623
624 EGLConfig config = getEglConfig(mDisplay);
625 EGLSurface surface = eglCreateWindowSurface(mDisplay, config, mFlingerSurface.get(), nullptr);
626 if (eglMakeCurrent(mDisplay, surface, surface, mContext) == EGL_FALSE) {
627 SLOGE("Can't make the new surface current. Error %d", eglGetError());
628 return;
629 }
Yegor Malyshev6925e1b2020-09-10 16:42:20 +0200630
631 projectSceneToWindow();
Marin Shalamanov9070c052020-05-18 19:05:17 +0200632
633 mSurface = surface;
634}
635
Huihong Luo50a2f362018-08-11 09:27:57 -0700636bool BootAnimation::preloadAnimation() {
637 findBootAnimationFile();
638 if (!mZipFileName.isEmpty()) {
639 mAnimation = loadAnimation(mZipFileName);
640 return (mAnimation != nullptr);
641 }
642
643 return false;
644}
645
Nikita Ioffe06c986e2020-01-27 17:16:03 +0000646bool BootAnimation::findBootAnimationFileInternal(const std::vector<std::string> &files) {
647 for (const std::string& f : files) {
648 if (access(f.c_str(), R_OK) == 0) {
649 mZipFileName = f.c_str();
650 return true;
651 }
652 }
653 return false;
654}
655
Huihong Luo50a2f362018-08-11 09:27:57 -0700656void BootAnimation::findBootAnimationFile() {
Elliott Hughesc367d482013-10-29 13:12:55 -0700657 // If the device has encryption turned on or is in process
Jason parksbd9a08d2011-01-31 15:04:34 -0600658 // of being encrypted we show the encrypted boot animation.
659 char decrypt[PROPERTY_VALUE_MAX];
660 property_get("vold.decrypt", decrypt, "");
661
Keun-young Parkb5938422017-03-23 13:46:24 -0700662 bool encryptedAnimation = atoi(decrypt) != 0 ||
663 !strcmp("trigger_restart_min_framework", decrypt);
Jason parksbd9a08d2011-01-31 15:04:34 -0600664
Jaekyun Seokc521bb32018-01-30 11:52:47 +0900665 if (!mShuttingDown && encryptedAnimation) {
Nikita Ioffe06c986e2020-01-27 17:16:03 +0000666 static const std::vector<std::string> encryptedBootFiles = {
667 PRODUCT_ENCRYPTED_BOOTANIMATION_FILE, SYSTEM_ENCRYPTED_BOOTANIMATION_FILE,
668 };
669 if (findBootAnimationFileInternal(encryptedBootFiles)) {
670 return;
Jaekyun Seokc521bb32018-01-30 11:52:47 +0900671 }
Jason parksbd9a08d2011-01-31 15:04:34 -0600672 }
Beverly34ffe252019-05-17 11:03:54 -0400673
674 const bool playDarkAnim = android::base::GetIntProperty("ro.boot.theme", 0) == 1;
Nikita Ioffe06c986e2020-01-27 17:16:03 +0000675 static const std::vector<std::string> bootFiles = {
676 APEX_BOOTANIMATION_FILE, playDarkAnim ? PRODUCT_BOOTANIMATION_DARK_FILE : PRODUCT_BOOTANIMATION_FILE,
677 OEM_BOOTANIMATION_FILE, SYSTEM_BOOTANIMATION_FILE
678 };
679 static const std::vector<std::string> shutdownFiles = {
680 PRODUCT_SHUTDOWNANIMATION_FILE, OEM_SHUTDOWNANIMATION_FILE, SYSTEM_SHUTDOWNANIMATION_FILE, ""
681 };
682 static const std::vector<std::string> userspaceRebootFiles = {
683 PRODUCT_USERSPACE_REBOOT_ANIMATION_FILE, OEM_USERSPACE_REBOOT_ANIMATION_FILE,
684 SYSTEM_USERSPACE_REBOOT_ANIMATION_FILE,
685 };
Keun-young Parkb5938422017-03-23 13:46:24 -0700686
Nikita Ioffe06c986e2020-01-27 17:16:03 +0000687 if (android::base::GetBoolProperty("sys.init.userspace_reboot.in_progress", false)) {
688 findBootAnimationFileInternal(userspaceRebootFiles);
689 } else if (mShuttingDown) {
690 findBootAnimationFileInternal(shutdownFiles);
691 } else {
692 findBootAnimationFileInternal(bootFiles);
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700693 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800694}
695
Lucas Dupin8eedc022021-07-29 21:49:07 +0000696GLuint compileShader(GLenum shaderType, const GLchar *source) {
697 GLuint shader = glCreateShader(shaderType);
698 glShaderSource(shader, 1, &source, 0);
699 glCompileShader(shader);
700 GLint isCompiled = 0;
701 glGetShaderiv(shader, GL_COMPILE_STATUS, &isCompiled);
702 if (isCompiled == GL_FALSE) {
703 SLOGE("Compile shader failed. Shader type: %d", shaderType);
Shan Huang81846ba2021-09-07 19:07:47 +0000704 GLint maxLength = 0;
705 glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &maxLength);
706 std::vector<GLchar> errorLog(maxLength);
707 glGetShaderInfoLog(shader, maxLength, &maxLength, &errorLog[0]);
708 SLOGE("Shader compilation error: %s", &errorLog[0]);
Lucas Dupin8eedc022021-07-29 21:49:07 +0000709 return 0;
710 }
711 return shader;
712}
713
714GLuint linkShader(GLuint vertexShader, GLuint fragmentShader) {
715 GLuint program = glCreateProgram();
716 glAttachShader(program, vertexShader);
717 glAttachShader(program, fragmentShader);
718 glLinkProgram(program);
719 GLint isLinked = 0;
720 glGetProgramiv(program, GL_LINK_STATUS, (int *)&isLinked);
721 if (isLinked == GL_FALSE) {
722 SLOGE("Linking shader failed. Shader handles: vert %d, frag %d",
723 vertexShader, fragmentShader);
724 return 0;
725 }
726 return program;
727}
728
729void BootAnimation::initShaders() {
Shan Huang2b31a362021-08-16 16:45:39 +0000730 bool dynamicColoringEnabled = mAnimation != nullptr && mAnimation->dynamicColoringEnabled;
Lucas Dupin8eedc022021-07-29 21:49:07 +0000731 GLuint vertexShader = compileShader(GL_VERTEX_SHADER, (const GLchar *)VERTEX_SHADER_SOURCE);
732 GLuint imageFragmentShader =
Shan Huang2b31a362021-08-16 16:45:39 +0000733 compileShader(GL_FRAGMENT_SHADER, dynamicColoringEnabled
734 ? (const GLchar *)IMAGE_FRAG_DYNAMIC_COLORING_SHADER_SOURCE
735 : (const GLchar *)IMAGE_FRAG_SHADER_SOURCE);
Lucas Dupin8eedc022021-07-29 21:49:07 +0000736 GLuint textFragmentShader =
737 compileShader(GL_FRAGMENT_SHADER, (const GLchar *)TEXT_FRAG_SHADER_SOURCE);
738
739 // Initialize image shader.
740 mImageShader = linkShader(vertexShader, imageFragmentShader);
741 GLint positionLocation = glGetAttribLocation(mImageShader, A_POSITION);
742 GLint uvLocation = glGetAttribLocation(mImageShader, A_UV);
743 mImageTextureLocation = glGetUniformLocation(mImageShader, U_TEXTURE);
744 mImageFadeLocation = glGetUniformLocation(mImageShader, U_FADE);
745 glEnableVertexAttribArray(positionLocation);
746 glVertexAttribPointer(positionLocation, 2, GL_FLOAT, GL_FALSE, 0, quadPositions);
747 glVertexAttribPointer(uvLocation, 2, GL_FLOAT, GL_FALSE, 0, quadUVs);
748 glEnableVertexAttribArray(uvLocation);
749
750 // Initialize text shader.
751 mTextShader = linkShader(vertexShader, textFragmentShader);
752 positionLocation = glGetAttribLocation(mTextShader, A_POSITION);
753 uvLocation = glGetAttribLocation(mTextShader, A_UV);
754 mTextTextureLocation = glGetUniformLocation(mTextShader, U_TEXTURE);
755 mTextCropAreaLocation = glGetUniformLocation(mTextShader, U_CROP_AREA);
756 glEnableVertexAttribArray(positionLocation);
757 glVertexAttribPointer(positionLocation, 2, GL_FLOAT, GL_FALSE, 0, quadPositions);
758 glVertexAttribPointer(uvLocation, 2, GL_FLOAT, GL_FALSE, 0, quadUVs);
759 glEnableVertexAttribArray(uvLocation);
760}
761
Marin Shalamanov9070c052020-05-18 19:05:17 +0200762bool BootAnimation::threadLoop() {
763 bool result;
Lucas Dupin8eedc022021-07-29 21:49:07 +0000764 initShaders();
765
Narayan Kamathafd31e02013-12-03 13:16:03 +0000766 // We have no bootanimation file, so we use the stock android logo
767 // animation.
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700768 if (mZipFileName.isEmpty()) {
Yoshiaki Maruoka9ef19b82017-10-25 13:55:49 +0900769 ALOGD("No animation file");
Marin Shalamanov9070c052020-05-18 19:05:17 +0200770 result = android();
Mathias Agopiana8826d62009-10-01 03:10:14 -0700771 } else {
Marin Shalamanov9070c052020-05-18 19:05:17 +0200772 result = movie();
Mathias Agopiana8826d62009-10-01 03:10:14 -0700773 }
774
Philip Junker60600562020-11-25 18:16:16 +0100775 mCallbacks->shutdown();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800776 eglMakeCurrent(mDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
777 eglDestroyContext(mDisplay, mContext);
778 eglDestroySurface(mDisplay, mSurface);
Mathias Agopian6cf0db22009-04-17 19:36:26 -0700779 mFlingerSurface.clear();
Mathias Agopian17f638b2009-04-16 20:04:08 -0700780 mFlingerSurfaceControl.clear();
Mathias Agopian627e7b52009-05-21 19:21:59 -0700781 eglTerminate(mDisplay);
Sai Kiran Korwar3eee9fb2015-06-16 17:13:35 +0530782 eglReleaseThread();
Mathias Agopian627e7b52009-05-21 19:21:59 -0700783 IPCThreadState::self()->stopProcess();
Marin Shalamanov9070c052020-05-18 19:05:17 +0200784 return result;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800785}
786
Marin Shalamanov9070c052020-05-18 19:05:17 +0200787bool BootAnimation::android() {
Lucas Dupin8eedc022021-07-29 21:49:07 +0000788 glActiveTexture(GL_TEXTURE0);
789
Wei Wang159a4102018-10-23 23:15:58 -0700790 SLOGD("%sAnimationShownTiming start time: %" PRId64 "ms", mShuttingDown ? "Shutdown" : "Boot",
Keun-young Parkd1794cd2017-04-04 12:21:19 -0700791 elapsedRealtime());
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700792 initTexture(&mAndroid[0], mAssets, "images/android-logo-mask.png");
793 initTexture(&mAndroid[1], mAssets, "images/android-logo-shine.png");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800794
Ed Coyne7464ac92017-06-08 12:26:48 -0700795 mCallbacks->init({});
796
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800797 // clear screen
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700798 glDisable(GL_DITHER);
799 glDisable(GL_SCISSOR_TEST);
Shan Huangbfd2d3c2021-09-09 23:19:04 +0000800 glUseProgram(mImageShader);
801
Mathias Agopian59f19e42011-05-06 19:22:12 -0700802 glClearColor(0,0,0,1);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800803 glClear(GL_COLOR_BUFFER_BIT);
804 eglSwapBuffers(mDisplay, mSurface);
805
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700806 // Blend state
807 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700808
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800809 const nsecs_t startTime = systemTime();
810 do {
Marin Shalamanov9070c052020-05-18 19:05:17 +0200811 processDisplayEvents();
812 const GLint xc = (mWidth - mAndroid[0].w) / 2;
813 const GLint yc = (mHeight - mAndroid[0].h) / 2;
814 const Rect updateRect(xc, yc, xc + mAndroid[0].w, yc + mAndroid[0].h);
815 glScissor(updateRect.left, mHeight - updateRect.bottom, updateRect.width(),
816 updateRect.height());
817
Mathias Agopian13796652009-03-24 22:49:21 -0700818 nsecs_t now = systemTime();
819 double time = now - startTime;
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700820 float t = 4.0f * float(time / us2ns(16667)) / mAndroid[1].w;
821 GLint offset = (1 - (t - floorf(t))) * mAndroid[1].w;
822 GLint x = xc - offset;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800823
Mathias Agopian81668642009-07-28 11:41:30 -0700824 glDisable(GL_SCISSOR_TEST);
825 glClear(GL_COLOR_BUFFER_BIT);
826
827 glEnable(GL_SCISSOR_TEST);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800828 glDisable(GL_BLEND);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800829 glBindTexture(GL_TEXTURE_2D, mAndroid[1].name);
Lucas Dupin8eedc022021-07-29 21:49:07 +0000830 drawTexturedQuad(x, yc, mAndroid[1].w, mAndroid[1].h);
831 drawTexturedQuad(x + mAndroid[1].w, yc, mAndroid[1].w, mAndroid[1].h);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800832
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700833 glEnable(GL_BLEND);
834 glBindTexture(GL_TEXTURE_2D, mAndroid[0].name);
Lucas Dupin8eedc022021-07-29 21:49:07 +0000835 drawTexturedQuad(xc, yc, mAndroid[0].w, mAndroid[0].h);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800836
Mathias Agopian627e7b52009-05-21 19:21:59 -0700837 EGLBoolean res = eglSwapBuffers(mDisplay, mSurface);
838 if (res == EGL_FALSE)
839 break;
840
Mathias Agopian13796652009-03-24 22:49:21 -0700841 // 12fps: don't animate too fast to preserve CPU
842 const nsecs_t sleepTime = 83333 - ns2us(systemTime() - now);
843 if (sleepTime > 0)
Mathias Agopiana8826d62009-10-01 03:10:14 -0700844 usleep(sleepTime);
Kevin Hesterd3782b22012-04-26 10:38:55 -0700845
846 checkExit();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800847 } while (!exitPending());
848
849 glDeleteTextures(1, &mAndroid[0].name);
850 glDeleteTextures(1, &mAndroid[1].name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800851 return false;
852}
853
Kevin Hesterd3782b22012-04-26 10:38:55 -0700854void BootAnimation::checkExit() {
855 // Allow surface flinger to gracefully request shutdown
856 char value[PROPERTY_VALUE_MAX];
857 property_get(EXIT_PROP_NAME, value, "0");
858 int exitnow = atoi(value);
859 if (exitnow) {
860 requestExit();
861 }
862}
863
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700864bool BootAnimation::validClock(const Animation::Part& part) {
865 return part.clockPosX != TEXT_MISSING_VALUE && part.clockPosY != TEXT_MISSING_VALUE;
866}
867
868bool parseTextCoord(const char* str, int* dest) {
869 if (strcmp("c", str) == 0) {
870 *dest = TEXT_CENTER_VALUE;
871 return true;
872 }
873
874 char* end;
875 int val = (int) strtol(str, &end, 0);
876 if (end == str || *end != '\0' || val == INT_MAX || val == INT_MIN) {
877 return false;
878 }
879 *dest = val;
880 return true;
881}
882
883// Parse two position coordinates. If only string is non-empty, treat it as the y value.
884void parsePosition(const char* str1, const char* str2, int* x, int* y) {
885 bool success = false;
886 if (strlen(str1) == 0) { // No values were specified
887 // success = false
888 } else if (strlen(str2) == 0) { // we have only one value
889 if (parseTextCoord(str1, y)) {
890 *x = TEXT_CENTER_VALUE;
891 success = true;
892 }
893 } else {
894 if (parseTextCoord(str1, x) && parseTextCoord(str2, y)) {
895 success = true;
896 }
897 }
898
899 if (!success) {
900 *x = TEXT_MISSING_VALUE;
901 *y = TEXT_MISSING_VALUE;
902 }
903}
904
Jesse Hall083b84c2014-09-22 10:51:09 -0700905// Parse a color represented as an HTML-style 'RRGGBB' string: each pair of
906// characters in str is a hex number in [0, 255], which are converted to
907// floating point values in the range [0.0, 1.0] and placed in the
908// corresponding elements of color.
909//
910// If the input string isn't valid, parseColor returns false and color is
911// left unchanged.
912static bool parseColor(const char str[7], float color[3]) {
913 float tmpColor[3];
914 for (int i = 0; i < 3; i++) {
915 int val = 0;
916 for (int j = 0; j < 2; j++) {
917 val *= 16;
918 char c = str[2*i + j];
919 if (c >= '0' && c <= '9') val += c - '0';
920 else if (c >= 'A' && c <= 'F') val += (c - 'A') + 10;
921 else if (c >= 'a' && c <= 'f') val += (c - 'a') + 10;
922 else return false;
923 }
924 tmpColor[i] = static_cast<float>(val) / 255.0f;
925 }
926 memcpy(color, tmpColor, sizeof(tmpColor));
927 return true;
928}
929
Shan Huang2b31a362021-08-16 16:45:39 +0000930// Parse a color represented as a signed decimal int string.
931// E.g. "-2757722" (whose hex 2's complement is 0xFFD5EBA6).
932// If the input color string is empty, set color with values in defaultColor.
933static void parseColorDecimalString(const std::string& colorString,
934 float color[3], float defaultColor[3]) {
935 if (colorString == "") {
936 memcpy(color, defaultColor, sizeof(float) * 3);
937 return;
938 }
939 int colorInt = atoi(colorString.c_str());
940 color[0] = ((float)((colorInt >> 16) & 0xFF)) / 0xFF; // r
941 color[1] = ((float)((colorInt >> 8) & 0xFF)) / 0xFF; // g
942 color[2] = ((float)(colorInt & 0xFF)) / 0xFF; // b
943}
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700944
Marin Shalamanov9070c052020-05-18 19:05:17 +0200945static bool readFile(ZipFileRO* zip, const char* name, String8& outString) {
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700946 ZipEntryRO entry = zip->findEntryByName(name);
Wei Wang159a4102018-10-23 23:15:58 -0700947 SLOGE_IF(!entry, "couldn't find %s", name);
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700948 if (!entry) {
949 return false;
950 }
951
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700952 FileMap* entryMap = zip->createEntryFileMap(entry);
953 zip->releaseEntry(entry);
Wei Wang159a4102018-10-23 23:15:58 -0700954 SLOGE_IF(!entryMap, "entryMap is null");
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700955 if (!entryMap) {
956 return false;
957 }
958
959 outString.setTo((char const*)entryMap->getDataPtr(), entryMap->getDataLength());
Narayan Kamath688ff4c2015-02-23 15:47:54 +0000960 delete entryMap;
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700961 return true;
962}
963
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700964// The font image should be a 96x2 array of character images. The
965// columns are the printable ASCII characters 0x20 - 0x7f. The
966// top row is regular text; the bottom row is bold.
967status_t BootAnimation::initFont(Font* font, const char* fallback) {
968 status_t status = NO_ERROR;
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800969
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700970 if (font->map != nullptr) {
971 glGenTextures(1, &font->texture.name);
972 glBindTexture(GL_TEXTURE_2D, font->texture.name);
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800973
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700974 status = initTexture(font->map, &font->texture.w, &font->texture.h);
975
Lucas Dupin8eedc022021-07-29 21:49:07 +0000976 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
977 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
978 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
979 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700980 } else if (fallback != nullptr) {
981 status = initTexture(&font->texture, mAssets, fallback);
982 } else {
983 return NO_INIT;
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800984 }
985
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700986 if (status == NO_ERROR) {
987 font->char_width = font->texture.w / FONT_NUM_COLS;
988 font->char_height = font->texture.h / FONT_NUM_ROWS / 2; // There are bold and regular rows
989 }
990
991 return status;
992}
993
994void BootAnimation::drawText(const char* str, const Font& font, bool bold, int* x, int* y) {
995 glEnable(GL_BLEND); // Allow us to draw on top of the animation
996 glBindTexture(GL_TEXTURE_2D, font.texture.name);
Lucas Dupin8eedc022021-07-29 21:49:07 +0000997 glUseProgram(mTextShader);
998 glUniform1i(mTextTextureLocation, 0);
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700999
1000 const int len = strlen(str);
1001 const int strWidth = font.char_width * len;
1002
1003 if (*x == TEXT_CENTER_VALUE) {
1004 *x = (mWidth - strWidth) / 2;
1005 } else if (*x < 0) {
1006 *x = mWidth + *x - strWidth;
1007 }
1008 if (*y == TEXT_CENTER_VALUE) {
1009 *y = (mHeight - font.char_height) / 2;
1010 } else if (*y < 0) {
1011 *y = mHeight + *y - font.char_height;
1012 }
1013
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -07001014 for (int i = 0; i < len; i++) {
1015 char c = str[i];
1016
1017 if (c < FONT_BEGIN_CHAR || c > FONT_END_CHAR) {
1018 c = '?';
1019 }
1020
1021 // Crop the texture to only the pixels in the current glyph
1022 const int charPos = (c - FONT_BEGIN_CHAR); // Position in the list of valid characters
1023 const int row = charPos / FONT_NUM_COLS;
1024 const int col = charPos % FONT_NUM_COLS;
Lucas Dupin8eedc022021-07-29 21:49:07 +00001025 // Bold fonts are expected in the second half of each row.
1026 float v0 = (row + (bold ? 0.5f : 0.0f)) / FONT_NUM_ROWS;
1027 float u0 = ((float)col) / FONT_NUM_COLS;
1028 float v1 = v0 + 1.0f / FONT_NUM_ROWS / 2;
1029 float u1 = u0 + 1.0f / FONT_NUM_COLS;
1030 glUniform4f(mTextCropAreaLocation, u0, v0, u1, v1);
1031 drawTexturedQuad(*x, *y, font.char_width, font.char_height);
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -07001032
1033 *x += font.char_width;
1034 }
1035
1036 glDisable(GL_BLEND); // Return to the animation's default behaviour
1037 glBindTexture(GL_TEXTURE_2D, 0);
1038}
1039
Damien Bargiacchi9071db12016-10-28 17:38:22 -07001040// We render 12 or 24 hour time.
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -07001041void BootAnimation::drawClock(const Font& font, const int xPos, const int yPos) {
Damien Bargiacchi9071db12016-10-28 17:38:22 -07001042 static constexpr char TIME_FORMAT_12[] = "%l:%M";
1043 static constexpr char TIME_FORMAT_24[] = "%H:%M";
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -07001044 static constexpr int TIME_LENGTH = 6;
1045
Damien Bargiacchia704b7d2016-02-16 16:55:49 -08001046 time_t rawtime;
1047 time(&rawtime);
1048 struct tm* timeInfo = localtime(&rawtime);
1049
1050 char timeBuff[TIME_LENGTH];
Damien Bargiacchi9071db12016-10-28 17:38:22 -07001051 const char* timeFormat = mTimeFormat12Hour ? TIME_FORMAT_12 : TIME_FORMAT_24;
1052 size_t length = strftime(timeBuff, TIME_LENGTH, timeFormat, timeInfo);
Damien Bargiacchia704b7d2016-02-16 16:55:49 -08001053
1054 if (length != TIME_LENGTH - 1) {
Wei Wang159a4102018-10-23 23:15:58 -07001055 SLOGE("Couldn't format time; abandoning boot animation clock");
Damien Bargiacchia704b7d2016-02-16 16:55:49 -08001056 mClockEnabled = false;
1057 return;
1058 }
1059
Damien Bargiacchi45a76442016-12-05 18:02:18 -08001060 char* out = timeBuff[0] == ' ' ? &timeBuff[1] : &timeBuff[0];
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -07001061 int x = xPos;
1062 int y = yPos;
Damien Bargiacchi45a76442016-12-05 18:02:18 -08001063 drawText(out, font, false, &x, &y);
Damien Bargiacchia704b7d2016-02-16 16:55:49 -08001064}
1065
Nicolas Geoffray41fffb42020-12-15 10:50:29 +00001066void BootAnimation::drawProgress(int percent, const Font& font, const int xPos, const int yPos) {
1067 static constexpr int PERCENT_LENGTH = 5;
1068
1069 char percentBuff[PERCENT_LENGTH];
1070 // ';' has the ascii code just after ':', and the font resource contains '%'
1071 // for that ascii code.
1072 sprintf(percentBuff, "%d;", percent);
1073 int x = xPos;
1074 int y = yPos;
1075 drawText(percentBuff, font, false, &x, &y);
1076}
1077
Marin Shalamanov9070c052020-05-18 19:05:17 +02001078bool BootAnimation::parseAnimationDesc(Animation& animation) {
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -07001079 String8 desString;
1080
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001081 if (!readFile(animation.zip, "desc.txt", desString)) {
Narayan Kamathafd31e02013-12-03 13:16:03 +00001082 return false;
1083 }
Mathias Agopiana8826d62009-10-01 03:10:14 -07001084 char const* s = desString.string();
Shan Huang2b31a362021-08-16 16:45:39 +00001085 std::string dynamicColoringPartName = "";
1086 bool postDynamicColoring = false;
Mathias Agopiana8826d62009-10-01 03:10:14 -07001087
Mathias Agopiana8826d62009-10-01 03:10:14 -07001088 // Parse the description file
1089 for (;;) {
1090 const char* endl = strstr(s, "\n");
Yi Kong911ac232019-03-24 01:49:02 -07001091 if (endl == nullptr) break;
Mathias Agopiana8826d62009-10-01 03:10:14 -07001092 String8 line(s, endl - s);
1093 const char* l = line.string();
Damien Bargiacchia704b7d2016-02-16 16:55:49 -08001094 int fps = 0;
1095 int width = 0;
1096 int height = 0;
1097 int count = 0;
1098 int pause = 0;
Nicolas Geoffray41fffb42020-12-15 10:50:29 +00001099 int progress = 0;
Yegor Malyshev6925e1b2020-09-10 16:42:20 +02001100 int framesToFadeCount = 0;
Shan Huang488ff742021-08-25 20:10:46 +00001101 int colorTransitionStart = 0;
1102 int colorTransitionEnd = 0;
Narayan Kamathafd31e02013-12-03 13:16:03 +00001103 char path[ANIM_ENTRY_NAME_MAX];
Jesse Hall083b84c2014-09-22 10:51:09 -07001104 char color[7] = "000000"; // default to black if unspecified
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -07001105 char clockPos1[TEXT_POS_LEN_MAX + 1] = "";
1106 char clockPos2[TEXT_POS_LEN_MAX + 1] = "";
Shan Huang2b31a362021-08-16 16:45:39 +00001107 char dynamicColoringPartNameBuffer[ANIM_ENTRY_NAME_MAX];
Kevin Hesterd3782b22012-04-26 10:38:55 -07001108 char pathType;
Shan Huang2b31a362021-08-16 16:45:39 +00001109 // start colors default to black if unspecified
1110 char start_color_0[7] = "000000";
1111 char start_color_1[7] = "000000";
1112 char start_color_2[7] = "000000";
1113 char start_color_3[7] = "000000";
Yegor Malyshev6925e1b2020-09-10 16:42:20 +02001114
1115 int nextReadPos;
1116
yidong zhang87bb3462022-12-07 15:55:31 +08001117 if (strlen(l) == 0) {
1118 s = ++endl;
1119 continue;
1120 }
1121
Nicolas Geoffray41fffb42020-12-15 10:50:29 +00001122 int topLineNumbers = sscanf(l, "%d %d %d %d", &width, &height, &fps, &progress);
1123 if (topLineNumbers == 3 || topLineNumbers == 4) {
1124 // SLOGD("> w=%d, h=%d, fps=%d, progress=%d", width, height, fps, progress);
Mathias Agopiana8826d62009-10-01 03:10:14 -07001125 animation.width = width;
1126 animation.height = height;
1127 animation.fps = fps;
Nicolas Geoffray41fffb42020-12-15 10:50:29 +00001128 if (topLineNumbers == 4) {
1129 animation.progressEnabled = (progress != 0);
1130 } else {
1131 animation.progressEnabled = false;
1132 }
Shan Huang488ff742021-08-25 20:10:46 +00001133 } else if (sscanf(l, "dynamic_colors %" STRTO(ANIM_PATH_MAX) "s #%6s #%6s #%6s #%6s %d %d",
Shan Huang2b31a362021-08-16 16:45:39 +00001134 dynamicColoringPartNameBuffer,
Shan Huang488ff742021-08-25 20:10:46 +00001135 start_color_0, start_color_1, start_color_2, start_color_3,
1136 &colorTransitionStart, &colorTransitionEnd)) {
Shan Huang2b31a362021-08-16 16:45:39 +00001137 animation.dynamicColoringEnabled = true;
1138 parseColor(start_color_0, animation.startColors[0]);
1139 parseColor(start_color_1, animation.startColors[1]);
1140 parseColor(start_color_2, animation.startColors[2]);
1141 parseColor(start_color_3, animation.startColors[3]);
Shan Huang488ff742021-08-25 20:10:46 +00001142 animation.colorTransitionStart = colorTransitionStart;
1143 animation.colorTransitionEnd = colorTransitionEnd;
Shan Huang2b31a362021-08-16 16:45:39 +00001144 dynamicColoringPartName = std::string(dynamicColoringPartNameBuffer);
Yegor Malyshev6925e1b2020-09-10 16:42:20 +02001145 } else if (sscanf(l, "%c %d %d %" STRTO(ANIM_PATH_MAX) "s%n",
1146 &pathType, &count, &pause, path, &nextReadPos) >= 4) {
1147 if (pathType == 'f') {
1148 sscanf(l + nextReadPos, " %d #%6s %16s %16s", &framesToFadeCount, color, clockPos1,
1149 clockPos2);
1150 } else {
1151 sscanf(l + nextReadPos, " #%6s %16s %16s", color, clockPos1, clockPos2);
1152 }
1153 // SLOGD("> type=%c, count=%d, pause=%d, path=%s, framesToFadeCount=%d, color=%s, "
1154 // "clockPos1=%s, clockPos2=%s",
1155 // pathType, count, pause, path, framesToFadeCount, color, clockPos1, clockPos2);
Mathias Agopiana8826d62009-10-01 03:10:14 -07001156 Animation::Part part;
Shan Huang2b31a362021-08-16 16:45:39 +00001157 if (path == dynamicColoringPartName) {
1158 // Part is specified to use dynamic coloring.
1159 part.useDynamicColoring = true;
1160 part.postDynamicColoring = false;
1161 postDynamicColoring = true;
1162 } else {
1163 // Part does not use dynamic coloring.
1164 part.useDynamicColoring = false;
1165 part.postDynamicColoring = postDynamicColoring;
1166 }
Kevin Hesterd3782b22012-04-26 10:38:55 -07001167 part.playUntilComplete = pathType == 'c';
Yegor Malyshev6925e1b2020-09-10 16:42:20 +02001168 part.framesToFadeCount = framesToFadeCount;
Mathias Agopiana8826d62009-10-01 03:10:14 -07001169 part.count = count;
1170 part.pause = pause;
1171 part.path = path;
Yi Kong911ac232019-03-24 01:49:02 -07001172 part.audioData = nullptr;
1173 part.animation = nullptr;
Jesse Hall083b84c2014-09-22 10:51:09 -07001174 if (!parseColor(color, part.backgroundColor)) {
Wei Wang159a4102018-10-23 23:15:58 -07001175 SLOGE("> invalid color '#%s'", color);
Jesse Hall083b84c2014-09-22 10:51:09 -07001176 part.backgroundColor[0] = 0.0f;
1177 part.backgroundColor[1] = 0.0f;
1178 part.backgroundColor[2] = 0.0f;
1179 }
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -07001180 parsePosition(clockPos1, clockPos2, &part.clockPosX, &part.clockPosY);
Mathias Agopiana8826d62009-10-01 03:10:14 -07001181 animation.parts.add(part);
1182 }
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001183 else if (strcmp(l, "$SYSTEM") == 0) {
Wei Wang159a4102018-10-23 23:15:58 -07001184 // SLOGD("> SYSTEM");
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001185 Animation::Part part;
1186 part.playUntilComplete = false;
Yegor Malyshev6925e1b2020-09-10 16:42:20 +02001187 part.framesToFadeCount = 0;
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001188 part.count = 1;
1189 part.pause = 0;
Yi Kong911ac232019-03-24 01:49:02 -07001190 part.audioData = nullptr;
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001191 part.animation = loadAnimation(String8(SYSTEM_BOOTANIMATION_FILE));
Yi Kong911ac232019-03-24 01:49:02 -07001192 if (part.animation != nullptr)
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001193 animation.parts.add(part);
1194 }
Mathias Agopiana8826d62009-10-01 03:10:14 -07001195 s = ++endl;
1196 }
1197
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001198 return true;
1199}
1200
Marin Shalamanov9070c052020-05-18 19:05:17 +02001201bool BootAnimation::preloadZip(Animation& animation) {
Mathias Agopiana8826d62009-10-01 03:10:14 -07001202 // read all the data structures
1203 const size_t pcount = animation.parts.size();
Yi Kong911ac232019-03-24 01:49:02 -07001204 void *cookie = nullptr;
Geoffrey Pitschdd214a72016-06-27 17:14:30 -04001205 ZipFileRO* zip = animation.zip;
1206 if (!zip->startIteration(&cookie)) {
Narayan Kamathafd31e02013-12-03 13:16:03 +00001207 return false;
1208 }
1209
1210 ZipEntryRO entry;
1211 char name[ANIM_ENTRY_NAME_MAX];
Yi Kong911ac232019-03-24 01:49:02 -07001212 while ((entry = zip->nextEntry(cookie)) != nullptr) {
Geoffrey Pitschdd214a72016-06-27 17:14:30 -04001213 const int foundEntryName = zip->getEntryFileName(entry, name, ANIM_ENTRY_NAME_MAX);
Narayan Kamathafd31e02013-12-03 13:16:03 +00001214 if (foundEntryName > ANIM_ENTRY_NAME_MAX || foundEntryName == -1) {
Wei Wang159a4102018-10-23 23:15:58 -07001215 SLOGE("Error fetching entry file name");
Narayan Kamathafd31e02013-12-03 13:16:03 +00001216 continue;
1217 }
1218
1219 const String8 entryName(name);
1220 const String8 path(entryName.getPathDir());
1221 const String8 leaf(entryName.getPathLeaf());
1222 if (leaf.size() > 0) {
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -07001223 if (entryName == CLOCK_FONT_ZIP_NAME) {
1224 FileMap* map = zip->createEntryFileMap(entry);
1225 if (map) {
1226 animation.clockFont.map = map;
1227 }
1228 continue;
1229 }
1230
Nicolas Geoffray41fffb42020-12-15 10:50:29 +00001231 if (entryName == PROGRESS_FONT_ZIP_NAME) {
1232 FileMap* map = zip->createEntryFileMap(entry);
1233 if (map) {
1234 animation.progressFont.map = map;
1235 }
1236 continue;
1237 }
1238
Geoffrey Pitschdd214a72016-06-27 17:14:30 -04001239 for (size_t j = 0; j < pcount; j++) {
Narayan Kamathafd31e02013-12-03 13:16:03 +00001240 if (path == animation.parts[j].path) {
Narayan Kamath4600dd02015-06-16 12:02:57 +01001241 uint16_t method;
Narayan Kamathafd31e02013-12-03 13:16:03 +00001242 // supports only stored png files
Yi Kong911ac232019-03-24 01:49:02 -07001243 if (zip->getEntryInfo(entry, &method, nullptr, nullptr, nullptr, nullptr, nullptr)) {
Narayan Kamathafd31e02013-12-03 13:16:03 +00001244 if (method == ZipFileRO::kCompressStored) {
Geoffrey Pitschdd214a72016-06-27 17:14:30 -04001245 FileMap* map = zip->createEntryFileMap(entry);
Narayan Kamathafd31e02013-12-03 13:16:03 +00001246 if (map) {
Narayan Kamathafd31e02013-12-03 13:16:03 +00001247 Animation::Part& part(animation.parts.editItemAt(j));
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -07001248 if (leaf == "audio.wav") {
1249 // a part may have at most one audio file
Geoffrey Pitschd6d9a1d2016-06-08 00:38:58 -07001250 part.audioData = (uint8_t *)map->getDataPtr();
1251 part.audioLength = map->getDataLength();
Geoffrey Pitschdd214a72016-06-27 17:14:30 -04001252 } else if (leaf == "trim.txt") {
1253 part.trimData.setTo((char const*)map->getDataPtr(),
1254 map->getDataLength());
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -07001255 } else {
1256 Animation::Frame frame;
1257 frame.name = leaf;
1258 frame.map = map;
Geoffrey Pitschdd214a72016-06-27 17:14:30 -04001259 frame.trimWidth = animation.width;
1260 frame.trimHeight = animation.height;
1261 frame.trimX = 0;
1262 frame.trimY = 0;
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -07001263 part.frames.add(frame);
1264 }
Mathias Agopiana8826d62009-10-01 03:10:14 -07001265 }
Geoffrey Pitschd6d9a1d2016-06-08 00:38:58 -07001266 } else {
Wei Wang159a4102018-10-23 23:15:58 -07001267 SLOGE("bootanimation.zip is compressed; must be only stored");
Mathias Agopiana8826d62009-10-01 03:10:14 -07001268 }
1269 }
1270 }
1271 }
1272 }
1273 }
1274
Geoffrey Pitschdd214a72016-06-27 17:14:30 -04001275 // If there is trimData present, override the positioning defaults.
1276 for (Animation::Part& part : animation.parts) {
1277 const char* trimDataStr = part.trimData.string();
1278 for (size_t frameIdx = 0; frameIdx < part.frames.size(); frameIdx++) {
1279 const char* endl = strstr(trimDataStr, "\n");
1280 // No more trimData for this part.
Yi Kong911ac232019-03-24 01:49:02 -07001281 if (endl == nullptr) {
Geoffrey Pitschdd214a72016-06-27 17:14:30 -04001282 break;
1283 }
1284 String8 line(trimDataStr, endl - trimDataStr);
1285 const char* lineStr = line.string();
1286 trimDataStr = ++endl;
1287 int width = 0, height = 0, x = 0, y = 0;
1288 if (sscanf(lineStr, "%dx%d+%d+%d", &width, &height, &x, &y) == 4) {
1289 Animation::Frame& frame(part.frames.editItemAt(frameIdx));
1290 frame.trimWidth = width;
1291 frame.trimHeight = height;
1292 frame.trimX = x;
1293 frame.trimY = y;
1294 } else {
Wei Wang159a4102018-10-23 23:15:58 -07001295 SLOGE("Error parsing trim.txt, line: %s", lineStr);
Geoffrey Pitschdd214a72016-06-27 17:14:30 -04001296 break;
1297 }
1298 }
1299 }
1300
1301 zip->endIteration(cookie);
Narayan Kamathafd31e02013-12-03 13:16:03 +00001302
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001303 return true;
1304}
1305
Marin Shalamanov9070c052020-05-18 19:05:17 +02001306bool BootAnimation::movie() {
Huihong Luo50a2f362018-08-11 09:27:57 -07001307 if (mAnimation == nullptr) {
1308 mAnimation = loadAnimation(mZipFileName);
1309 }
1310
1311 if (mAnimation == nullptr)
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001312 return false;
1313
Huihong Luo50a2f362018-08-11 09:27:57 -07001314 // mCallbacks->init() may get called recursively,
1315 // this loop is needed to get the same results
1316 for (const Animation::Part& part : mAnimation->parts) {
1317 if (part.animation != nullptr) {
1318 mCallbacks->init(part.animation->parts);
1319 }
1320 }
1321 mCallbacks->init(mAnimation->parts);
1322
Damien Bargiacchi97480862016-03-29 14:55:55 -07001323 bool anyPartHasClock = false;
Huihong Luo50a2f362018-08-11 09:27:57 -07001324 for (size_t i=0; i < mAnimation->parts.size(); i++) {
1325 if(validClock(mAnimation->parts[i])) {
Damien Bargiacchi97480862016-03-29 14:55:55 -07001326 anyPartHasClock = true;
1327 break;
1328 }
1329 }
1330 if (!anyPartHasClock) {
1331 mClockEnabled = false;
Thiébaud Weksteen08de4432021-11-18 13:25:59 +11001332 } else if (!android::base::GetBoolProperty(CLOCK_ENABLED_PROP_NAME, false)) {
1333 mClockEnabled = false;
Damien Bargiacchi97480862016-03-29 14:55:55 -07001334 }
1335
Sai Kiran Korwar27167492015-07-07 20:00:06 +05301336 // Check if npot textures are supported
1337 mUseNpotTextures = false;
1338 String8 gl_extensions;
1339 const char* exts = reinterpret_cast<const char*>(glGetString(GL_EXTENSIONS));
1340 if (!exts) {
1341 glGetError();
1342 } else {
1343 gl_extensions.setTo(exts);
1344 if ((gl_extensions.find("GL_ARB_texture_non_power_of_two") != -1) ||
1345 (gl_extensions.find("GL_OES_texture_npot") != -1)) {
1346 mUseNpotTextures = true;
1347 }
1348 }
1349
Damien Bargiacchia704b7d2016-02-16 16:55:49 -08001350 // Blend required to draw time on top of animation frames.
1351 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
Mathias Agopiana8826d62009-10-01 03:10:14 -07001352 glDisable(GL_DITHER);
1353 glDisable(GL_SCISSOR_TEST);
1354 glDisable(GL_BLEND);
Mathias Agopiana8826d62009-10-01 03:10:14 -07001355
Greg Kaiser2ded8002021-07-19 17:03:27 +00001356 glEnable(GL_TEXTURE_2D);
Lucas Dupin8eedc022021-07-29 21:49:07 +00001357 glBindTexture(GL_TEXTURE_2D, 0);
1358 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
1359 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
1360 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
1361 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -07001362 bool clockFontInitialized = false;
Damien Bargiacchia704b7d2016-02-16 16:55:49 -08001363 if (mClockEnabled) {
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -07001364 clockFontInitialized =
Huihong Luo50a2f362018-08-11 09:27:57 -07001365 (initFont(&mAnimation->clockFont, CLOCK_FONT_ASSET) == NO_ERROR);
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -07001366 mClockEnabled = clockFontInitialized;
Damien Bargiacchia704b7d2016-02-16 16:55:49 -08001367 }
1368
Nicolas Geoffray41fffb42020-12-15 10:50:29 +00001369 initFont(&mAnimation->progressFont, PROGRESS_FONT_ASSET);
1370
Damien Bargiacchi97480862016-03-29 14:55:55 -07001371 if (mClockEnabled && !updateIsTimeAccurate()) {
1372 mTimeCheckThread = new TimeCheckThread(this);
1373 mTimeCheckThread->run("BootAnimation::TimeCheckThread", PRIORITY_NORMAL);
1374 }
1375
George Burgess IV0f8e8112021-08-23 17:32:23 -07001376 if (mAnimation->dynamicColoringEnabled) {
Shan Huang7fee3522021-08-18 00:56:08 +00001377 initDynamicColors();
1378 }
1379
Huihong Luo50a2f362018-08-11 09:27:57 -07001380 playAnimation(*mAnimation);
Damien Bargiacchi97480862016-03-29 14:55:55 -07001381
Geoffrey Pitscha9173532016-07-19 14:56:39 -04001382 if (mTimeCheckThread != nullptr) {
Damien Bargiacchi97480862016-03-29 14:55:55 -07001383 mTimeCheckThread->requestExit();
Geoffrey Pitscha9173532016-07-19 14:56:39 -04001384 mTimeCheckThread = nullptr;
1385 }
1386
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -07001387 if (clockFontInitialized) {
Huihong Luo50a2f362018-08-11 09:27:57 -07001388 glDeleteTextures(1, &mAnimation->clockFont.texture.name);
Andriy Naborskyy815e51d2016-03-24 16:43:34 -07001389 }
1390
Huihong Luo50a2f362018-08-11 09:27:57 -07001391 releaseAnimation(mAnimation);
1392 mAnimation = nullptr;
Damien Bargiacchi1a8a2132018-07-24 15:20:26 -07001393
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001394 return false;
1395}
1396
Nicolas Geoffraya1298752021-04-20 15:15:06 +01001397bool BootAnimation::shouldStopPlayingPart(const Animation::Part& part,
1398 const int fadedFramesCount,
1399 const int lastDisplayedProgress) {
Yegor Malyshev6925e1b2020-09-10 16:42:20 +02001400 // 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 +01001401 return exitPending() && !part.playUntilComplete && fadedFramesCount >= part.framesToFadeCount &&
1402 (lastDisplayedProgress == 0 || lastDisplayedProgress == 100);
Yegor Malyshev6925e1b2020-09-10 16:42:20 +02001403}
1404
Lucas Dupin8eedc022021-07-29 21:49:07 +00001405// Linear mapping from range <a1, a2> to range <b1, b2>
1406float mapLinear(float x, float a1, float a2, float b1, float b2) {
1407 return b1 + ( x - a1 ) * ( b2 - b1 ) / ( a2 - a1 );
1408}
1409
1410void BootAnimation::drawTexturedQuad(float xStart, float yStart, float width, float height) {
1411 // Map coordinates from screen space to world space.
1412 float x0 = mapLinear(xStart, 0, mWidth, -1, 1);
1413 float y0 = mapLinear(yStart, 0, mHeight, -1, 1);
1414 float x1 = mapLinear(xStart + width, 0, mWidth, -1, 1);
1415 float y1 = mapLinear(yStart + height, 0, mHeight, -1, 1);
1416 // Update quad vertex positions.
1417 quadPositions[0] = x0;
1418 quadPositions[1] = y0;
1419 quadPositions[2] = x1;
1420 quadPositions[3] = y0;
1421 quadPositions[4] = x1;
1422 quadPositions[5] = y1;
1423 quadPositions[6] = x1;
1424 quadPositions[7] = y1;
1425 quadPositions[8] = x0;
1426 quadPositions[9] = y1;
1427 quadPositions[10] = x0;
1428 quadPositions[11] = y0;
1429 glDrawArrays(GL_TRIANGLES, 0,
1430 sizeof(quadPositions) / sizeof(quadPositions[0]) / 2);
1431}
1432
Shan Huang7fee3522021-08-18 00:56:08 +00001433void BootAnimation::initDynamicColors() {
1434 for (int i = 0; i < DYNAMIC_COLOR_COUNT; i++) {
Lucas Dupincd256842022-03-29 15:46:36 -07001435 const auto syspropName = "persist.bootanim.color" + std::to_string(i + 1);
1436 const auto syspropValue = android::base::GetProperty(syspropName, "");
1437 if (syspropValue != "") {
1438 SLOGI("Loaded dynamic color: %s -> %s", syspropName.c_str(), syspropValue.c_str());
1439 mDynamicColorsApplied = true;
1440 }
1441 parseColorDecimalString(syspropValue,
Shan Huang7fee3522021-08-18 00:56:08 +00001442 mAnimation->endColors[i], mAnimation->startColors[i]);
1443 }
1444 glUseProgram(mImageShader);
Lucas Dupincd256842022-03-29 15:46:36 -07001445 SLOGI("Dynamically coloring boot animation. Sysprops loaded? %i", mDynamicColorsApplied);
Shan Huang7fee3522021-08-18 00:56:08 +00001446 for (int i = 0; i < DYNAMIC_COLOR_COUNT; i++) {
1447 float *startColor = mAnimation->startColors[i];
1448 float *endColor = mAnimation->endColors[i];
Andrew Zengf9f58222022-11-08 11:10:48 -08001449 glUniform3f(glGetUniformLocation(mImageShader,
Shan Huang7fee3522021-08-18 00:56:08 +00001450 (U_START_COLOR_PREFIX + std::to_string(i)).c_str()),
Andrew Zengf9f58222022-11-08 11:10:48 -08001451 startColor[0], startColor[1], startColor[2]);
1452 glUniform3f(glGetUniformLocation(mImageShader,
Shan Huang7fee3522021-08-18 00:56:08 +00001453 (U_END_COLOR_PREFIX + std::to_string(i)).c_str()),
Andrew Zengf9f58222022-11-08 11:10:48 -08001454 endColor[0], endColor[1], endColor[2]);
Shan Huang7fee3522021-08-18 00:56:08 +00001455 }
1456 mImageColorProgressLocation = glGetUniformLocation(mImageShader, U_COLOR_PROGRESS);
1457}
1458
Marin Shalamanov9070c052020-05-18 19:05:17 +02001459bool BootAnimation::playAnimation(const Animation& animation) {
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001460 const size_t pcount = animation.parts.size();
Mathias Agopiana8826d62009-10-01 03:10:14 -07001461 nsecs_t frameDuration = s2ns(1) / animation.fps;
Mathias Agopian9f3020d2009-11-06 16:30:18 -08001462
Wei Wang159a4102018-10-23 23:15:58 -07001463 SLOGD("%sAnimationShownTiming start time: %" PRId64 "ms", mShuttingDown ? "Shutdown" : "Boot",
Keun-young Parkd1794cd2017-04-04 12:21:19 -07001464 elapsedRealtime());
Yegor Malyshev6925e1b2020-09-10 16:42:20 +02001465
1466 int fadedFramesCount = 0;
Nicolas Geoffray41fffb42020-12-15 10:50:29 +00001467 int lastDisplayedProgress = 0;
Lucas Dupinbfd18122022-04-25 11:26:08 -07001468 int colorTransitionStart = animation.colorTransitionStart;
1469 int colorTransitionEnd = animation.colorTransitionEnd;
Narayan Kamathafd31e02013-12-03 13:16:03 +00001470 for (size_t i=0 ; i<pcount ; i++) {
Mathias Agopiana8826d62009-10-01 03:10:14 -07001471 const Animation::Part& part(animation.parts[i]);
1472 const size_t fcount = part.frames.size();
Mathias Agopiana8826d62009-10-01 03:10:14 -07001473
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001474 // Handle animation package
Yi Kong911ac232019-03-24 01:49:02 -07001475 if (part.animation != nullptr) {
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001476 playAnimation(*part.animation);
1477 if (exitPending())
1478 break;
1479 continue; //to next part
1480 }
1481
Yegor Malyshev6925e1b2020-09-10 16:42:20 +02001482 // process the part not only while the count allows but also if already fading
1483 for (int r=0 ; !part.count || r<part.count || fadedFramesCount > 0 ; r++) {
Nicolas Geoffraya1298752021-04-20 15:15:06 +01001484 if (shouldStopPlayingPart(part, fadedFramesCount, lastDisplayedProgress)) break;
Kevin Hesterd3782b22012-04-26 10:38:55 -07001485
Lucas Dupinbfd18122022-04-25 11:26:08 -07001486 // It's possible that the sysprops were not loaded yet at this boot phase.
1487 // If that's the case, then we should keep trying until they are available.
1488 if (animation.dynamicColoringEnabled && !mDynamicColorsApplied
1489 && (part.useDynamicColoring || part.postDynamicColoring)) {
1490 SLOGD("Trying to load dynamic color sysprops.");
1491 initDynamicColors();
1492 if (mDynamicColorsApplied) {
1493 // Sysprops were loaded. Next step is to adjust the animation if we loaded
1494 // the colors after the animation should have started.
1495 const int transitionLength = colorTransitionEnd - colorTransitionStart;
1496 if (part.postDynamicColoring) {
1497 colorTransitionStart = 0;
1498 colorTransitionEnd = fmin(transitionLength, fcount - 1);
1499 }
1500 }
1501 }
1502
Ed Coyne7464ac92017-06-08 12:26:48 -07001503 mCallbacks->playPart(i, part, r);
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -07001504
Jesse Hall083b84c2014-09-22 10:51:09 -07001505 glClearColor(
1506 part.backgroundColor[0],
1507 part.backgroundColor[1],
1508 part.backgroundColor[2],
1509 1.0f);
1510
Yoshiaki Maruoka9ef19b82017-10-25 13:55:49 +09001511 ALOGD("Playing files = %s/%s, Requested repeat = %d, playUntilComplete = %s",
1512 animation.fileName.string(), part.path.string(), part.count,
1513 part.playUntilComplete ? "true" : "false");
1514
Nicolas Geoffray41fffb42020-12-15 10:50:29 +00001515 // For the last animation, if we have progress indicator from
1516 // the system, display it.
1517 int currentProgress = android::base::GetIntProperty(PROGRESS_PROP_NAME, 0);
1518 bool displayProgress = animation.progressEnabled &&
1519 (i == (pcount -1)) && currentProgress != 0;
1520
Yegor Malyshev6925e1b2020-09-10 16:42:20 +02001521 for (size_t j=0 ; j<fcount ; j++) {
Nicolas Geoffraya1298752021-04-20 15:15:06 +01001522 if (shouldStopPlayingPart(part, fadedFramesCount, lastDisplayedProgress)) break;
Yegor Malyshev6925e1b2020-09-10 16:42:20 +02001523
Shan Huang2b31a362021-08-16 16:45:39 +00001524 // Color progress is
Shan Huang488ff742021-08-25 20:10:46 +00001525 // - the animation progress, normalized from
1526 // [colorTransitionStart,colorTransitionEnd] to [0, 1] for the dynamic coloring
1527 // part.
Shan Huang2b31a362021-08-16 16:45:39 +00001528 // - 0 for parts that come before,
1529 // - 1 for parts that come after.
1530 float colorProgress = part.useDynamicColoring
Shan Huang488ff742021-08-25 20:10:46 +00001531 ? fmin(fmax(
Lucas Dupinbfd18122022-04-25 11:26:08 -07001532 ((float)j - colorTransitionStart) /
1533 fmax(colorTransitionEnd - colorTransitionStart, 1.0f), 0.0f), 1.0f)
Shan Huang2b31a362021-08-16 16:45:39 +00001534 : (part.postDynamicColoring ? 1 : 0);
1535
Marin Shalamanov9070c052020-05-18 19:05:17 +02001536 processDisplayEvents();
1537
joenchen85b339f2022-04-11 23:58:34 +08001538 const double ratio_w = static_cast<double>(mWidth) / mInitWidth;
1539 const double ratio_h = static_cast<double>(mHeight) / mInitHeight;
1540 const int animationX = (mWidth - animation.width * ratio_w) / 2;
1541 const int animationY = (mHeight - animation.height * ratio_h) / 2;
Marin Shalamanov9070c052020-05-18 19:05:17 +02001542
Mathias Agopiana8826d62009-10-01 03:10:14 -07001543 const Animation::Frame& frame(part.frames[j]);
Mathias Agopiandb7dd2a2012-05-12 15:08:21 -07001544 nsecs_t lastFrame = systemTime();
Mathias Agopiana8826d62009-10-01 03:10:14 -07001545
1546 if (r > 0) {
1547 glBindTexture(GL_TEXTURE_2D, frame.tid);
1548 } else {
Lucas Dupin8eedc022021-07-29 21:49:07 +00001549 glGenTextures(1, &frame.tid);
1550 glBindTexture(GL_TEXTURE_2D, frame.tid);
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -07001551 int w, h;
Shan Huang2b31a362021-08-16 16:45:39 +00001552 // Set decoding option to alpha unpremultiplied so that the R, G, B channels
1553 // of transparent pixels are preserved.
1554 initTexture(frame.map, &w, &h, false /* don't premultiply alpha */);
Mathias Agopiana8826d62009-10-01 03:10:14 -07001555 }
1556
joenchen85b339f2022-04-11 23:58:34 +08001557 const int trimWidth = frame.trimWidth * ratio_w;
1558 const int trimHeight = frame.trimHeight * ratio_h;
1559 const int trimX = frame.trimX * ratio_w;
1560 const int trimY = frame.trimY * ratio_h;
1561 const int xc = animationX + trimX;
1562 const int yc = animationY + trimY;
Shan Huang2b31a362021-08-16 16:45:39 +00001563 glClear(GL_COLOR_BUFFER_BIT);
Geoffrey Pitschdd214a72016-06-27 17:14:30 -04001564 // specify the y center as ceiling((mHeight - frame.trimHeight) / 2)
1565 // which is equivalent to mHeight - (yc + frame.trimHeight)
joenchen85b339f2022-04-11 23:58:34 +08001566 const int frameDrawY = mHeight - (yc + trimHeight);
Yegor Malyshev6925e1b2020-09-10 16:42:20 +02001567
Lucas Dupin8eedc022021-07-29 21:49:07 +00001568 float fade = 0;
Yegor Malyshev6925e1b2020-09-10 16:42:20 +02001569 // if the part hasn't been stopped yet then continue fading if necessary
1570 if (exitPending() && part.hasFadingPhase()) {
Lucas Dupin8eedc022021-07-29 21:49:07 +00001571 fade = static_cast<float>(++fadedFramesCount) / part.framesToFadeCount;
Yegor Malyshev6925e1b2020-09-10 16:42:20 +02001572 if (fadedFramesCount >= part.framesToFadeCount) {
1573 fadedFramesCount = MAX_FADED_FRAMES_COUNT; // no more fading
1574 }
1575 }
Lucas Dupin8eedc022021-07-29 21:49:07 +00001576 glUseProgram(mImageShader);
1577 glUniform1i(mImageTextureLocation, 0);
1578 glUniform1f(mImageFadeLocation, fade);
Shan Huang2b31a362021-08-16 16:45:39 +00001579 if (animation.dynamicColoringEnabled) {
1580 glUniform1f(mImageColorProgressLocation, colorProgress);
1581 }
Lucas Dupin8eedc022021-07-29 21:49:07 +00001582 glEnable(GL_BLEND);
joenchen85b339f2022-04-11 23:58:34 +08001583 drawTexturedQuad(xc, frameDrawY, trimWidth, trimHeight);
Lucas Dupin8eedc022021-07-29 21:49:07 +00001584 glDisable(GL_BLEND);
Yegor Malyshev6925e1b2020-09-10 16:42:20 +02001585
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -07001586 if (mClockEnabled && mTimeIsAccurate && validClock(part)) {
1587 drawClock(animation.clockFont, part.clockPosX, part.clockPosY);
Damien Bargiacchia704b7d2016-02-16 16:55:49 -08001588 }
Yegor Malyshev6925e1b2020-09-10 16:42:20 +02001589
Nicolas Geoffray41fffb42020-12-15 10:50:29 +00001590 if (displayProgress) {
1591 int newProgress = android::base::GetIntProperty(PROGRESS_PROP_NAME, 0);
1592 // In case the new progress jumped suddenly, still show an
1593 // increment of 1.
1594 if (lastDisplayedProgress != 100) {
1595 // Artificially sleep 1/10th a second to slow down the animation.
1596 usleep(100000);
1597 if (lastDisplayedProgress < newProgress) {
1598 lastDisplayedProgress++;
1599 }
1600 }
1601 // Put the progress percentage right below the animation.
1602 int posY = animation.height / 3;
1603 int posX = TEXT_CENTER_VALUE;
1604 drawProgress(lastDisplayedProgress, animation.progressFont, posX, posY);
1605 }
1606
Adrian Roos9ee5dff2018-08-22 20:19:49 +02001607 handleViewport(frameDuration);
Damien Bargiacchia704b7d2016-02-16 16:55:49 -08001608
Mathias Agopiana8826d62009-10-01 03:10:14 -07001609 eglSwapBuffers(mDisplay, mSurface);
1610
1611 nsecs_t now = systemTime();
1612 nsecs_t delay = frameDuration - (now - lastFrame);
Wei Wang159a4102018-10-23 23:15:58 -07001613 //SLOGD("%lld, %lld", ns2ms(now - lastFrame), ns2ms(delay));
Mathias Agopiana8826d62009-10-01 03:10:14 -07001614 lastFrame = now;
Mathias Agopiandb7dd2a2012-05-12 15:08:21 -07001615
1616 if (delay > 0) {
1617 struct timespec spec;
1618 spec.tv_sec = (now + delay) / 1000000000;
1619 spec.tv_nsec = (now + delay) % 1000000000;
1620 int err;
1621 do {
Yi Kong911ac232019-03-24 01:49:02 -07001622 err = clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &spec, nullptr);
Keith Mok0f2e6682021-11-15 05:50:32 +00001623 } while (err == EINTR);
Mathias Agopiandb7dd2a2012-05-12 15:08:21 -07001624 }
Kevin Hesterd3782b22012-04-26 10:38:55 -07001625
1626 checkExit();
Mathias Agopiana8826d62009-10-01 03:10:14 -07001627 }
Kevin Hesterd3782b22012-04-26 10:38:55 -07001628
Mathias Agopiana8826d62009-10-01 03:10:14 -07001629 usleep(part.pause * ns2us(frameDuration));
Kevin Hesterd3782b22012-04-26 10:38:55 -07001630
Yegor Malyshev6925e1b2020-09-10 16:42:20 +02001631 if (exitPending() && !part.count && mCurrentInset >= mTargetInset &&
1632 !part.hasFadingPhase()) {
Nicolas Geoffraya1298752021-04-20 15:15:06 +01001633 if (lastDisplayedProgress != 0 && lastDisplayedProgress != 100) {
1634 android::base::SetProperty(PROGRESS_PROP_NAME, "100");
1635 continue;
1636 }
Yegor Malyshev6925e1b2020-09-10 16:42:20 +02001637 break; // exit the infinite non-fading part when it has been played at least once
1638 }
Mathias Agopiana8826d62009-10-01 03:10:14 -07001639 }
Geoffrey Pitsch2fb30fb2016-07-06 16:16:20 -04001640 }
1641
1642 // Free textures created for looping parts now that the animation is done.
1643 for (const Animation::Part& part : animation.parts) {
Mathias Agopiana8826d62009-10-01 03:10:14 -07001644 if (part.count != 1) {
Geoffrey Pitsch2fb30fb2016-07-06 16:16:20 -04001645 const size_t fcount = part.frames.size();
1646 for (size_t j = 0; j < fcount; j++) {
Mathias Agopiana8826d62009-10-01 03:10:14 -07001647 const Animation::Frame& frame(part.frames[j]);
1648 glDeleteTextures(1, &frame.tid);
1649 }
1650 }
1651 }
Geoffrey Pitschd6d9a1d2016-06-08 00:38:58 -07001652
Yoshiaki Maruoka9ef19b82017-10-25 13:55:49 +09001653 ALOGD("%sAnimationShownTiming End time: %" PRId64 "ms", mShuttingDown ? "Shutdown" : "Boot",
1654 elapsedRealtime());
1655
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001656 return true;
Mathias Agopiana8826d62009-10-01 03:10:14 -07001657}
1658
Marin Shalamanov9070c052020-05-18 19:05:17 +02001659void BootAnimation::processDisplayEvents() {
1660 // This will poll mDisplayEventReceiver and if there are new events it'll call
1661 // displayEventCallback synchronously.
1662 mLooper->pollOnce(0);
1663}
1664
Adrian Roos9ee5dff2018-08-22 20:19:49 +02001665void BootAnimation::handleViewport(nsecs_t timestep) {
1666 if (mShuttingDown || !mFlingerSurfaceControl || mTargetInset == 0) {
1667 return;
1668 }
1669 if (mTargetInset < 0) {
1670 // Poll the amount for the top display inset. This will return -1 until persistent properties
1671 // have been loaded.
1672 mTargetInset = android::base::GetIntProperty("persist.sys.displayinset.top",
1673 -1 /* default */, -1 /* min */, mHeight / 2 /* max */);
1674 }
1675 if (mTargetInset <= 0) {
1676 return;
1677 }
1678
1679 if (mCurrentInset < mTargetInset) {
1680 // After the device boots, the inset will effectively be cropped away. We animate this here.
1681 float fraction = static_cast<float>(mCurrentInset) / mTargetInset;
1682 int interpolatedInset = (cosf((fraction + 1) * M_PI) / 2.0f + 0.5f) * mTargetInset;
1683
1684 SurfaceComposerClient::Transaction()
1685 .setCrop(mFlingerSurfaceControl, Rect(0, interpolatedInset, mWidth, mHeight))
1686 .apply();
1687 } else {
1688 // At the end of the animation, we switch to the viewport that DisplayManager will apply
1689 // later. This changes the coordinate system, and means we must move the surface up by
1690 // the inset amount.
Adrian Roos9ee5dff2018-08-22 20:19:49 +02001691 Rect layerStackRect(0, 0, mWidth, mHeight - mTargetInset);
1692 Rect displayRect(0, mTargetInset, mWidth, mHeight);
1693
1694 SurfaceComposerClient::Transaction t;
1695 t.setPosition(mFlingerSurfaceControl, 0, -mTargetInset)
1696 .setCrop(mFlingerSurfaceControl, Rect(0, mTargetInset, mWidth, mHeight));
Dominik Laskowski2a3d9aa2019-11-18 20:26:39 -08001697 t.setDisplayProjection(mDisplayToken, ui::ROTATION_0, layerStackRect, displayRect);
Adrian Roos9ee5dff2018-08-22 20:19:49 +02001698 t.apply();
1699
1700 mTargetInset = mCurrentInset = 0;
1701 }
1702
1703 int delta = timestep * mTargetInset / ms2ns(200);
1704 mCurrentInset += delta;
1705}
1706
Marin Shalamanov9070c052020-05-18 19:05:17 +02001707void BootAnimation::releaseAnimation(Animation* animation) const {
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001708 for (Vector<Animation::Part>::iterator it = animation->parts.begin(),
1709 e = animation->parts.end(); it != e; ++it) {
1710 if (it->animation)
1711 releaseAnimation(it->animation);
1712 }
1713 if (animation->zip)
1714 delete animation->zip;
1715 delete animation;
1716}
1717
Marin Shalamanov9070c052020-05-18 19:05:17 +02001718BootAnimation::Animation* BootAnimation::loadAnimation(const String8& fn) {
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001719 if (mLoadedFiles.indexOf(fn) >= 0) {
Wei Wang159a4102018-10-23 23:15:58 -07001720 SLOGE("File \"%s\" is already loaded. Cyclic ref is not allowed",
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001721 fn.string());
Yi Kong911ac232019-03-24 01:49:02 -07001722 return nullptr;
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001723 }
1724 ZipFileRO *zip = ZipFileRO::open(fn);
Yi Kong911ac232019-03-24 01:49:02 -07001725 if (zip == nullptr) {
Wei Wang159a4102018-10-23 23:15:58 -07001726 SLOGE("Failed to open animation zip \"%s\": %s",
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001727 fn.string(), strerror(errno));
Yi Kong911ac232019-03-24 01:49:02 -07001728 return nullptr;
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001729 }
1730
Yoshiaki Maruoka9ef19b82017-10-25 13:55:49 +09001731 ALOGD("%s is loaded successfully", fn.string());
1732
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001733 Animation *animation = new Animation;
1734 animation->fileName = fn;
1735 animation->zip = zip;
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -07001736 animation->clockFont.map = nullptr;
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001737 mLoadedFiles.add(animation->fileName);
1738
1739 parseAnimationDesc(*animation);
Geoffrey Pitscha91a2d72016-07-12 14:46:19 -04001740 if (!preloadZip(*animation)) {
Greg Kaiser7426ec82019-09-11 14:34:27 -07001741 releaseAnimation(animation);
Yi Kong911ac232019-03-24 01:49:02 -07001742 return nullptr;
Geoffrey Pitscha91a2d72016-07-12 14:46:19 -04001743 }
1744
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001745 mLoadedFiles.remove(fn);
1746 return animation;
1747}
Damien Bargiacchi97480862016-03-29 14:55:55 -07001748
1749bool BootAnimation::updateIsTimeAccurate() {
1750 static constexpr long long MAX_TIME_IN_PAST = 60000LL * 60LL * 24LL * 30LL; // 30 days
1751 static constexpr long long MAX_TIME_IN_FUTURE = 60000LL * 90LL; // 90 minutes
1752
1753 if (mTimeIsAccurate) {
1754 return true;
1755 }
Keun-young Parkb5938422017-03-23 13:46:24 -07001756 if (mShuttingDown) return true;
Damien Bargiacchi97480862016-03-29 14:55:55 -07001757 struct stat statResult;
Damien Bargiacchi9071db12016-10-28 17:38:22 -07001758
1759 if(stat(TIME_FORMAT_12_HOUR_FLAG_FILE_PATH, &statResult) == 0) {
1760 mTimeFormat12Hour = true;
1761 }
1762
Damien Bargiacchi97480862016-03-29 14:55:55 -07001763 if(stat(ACCURATE_TIME_FLAG_FILE_PATH, &statResult) == 0) {
1764 mTimeIsAccurate = true;
1765 return true;
1766 }
1767
1768 FILE* file = fopen(LAST_TIME_CHANGED_FILE_PATH, "r");
Yi Kong911ac232019-03-24 01:49:02 -07001769 if (file != nullptr) {
Damien Bargiacchi97480862016-03-29 14:55:55 -07001770 long long lastChangedTime = 0;
1771 fscanf(file, "%lld", &lastChangedTime);
1772 fclose(file);
1773 if (lastChangedTime > 0) {
1774 struct timespec now;
1775 clock_gettime(CLOCK_REALTIME, &now);
1776 // Match the Java timestamp format
1777 long long rtcNow = (now.tv_sec * 1000LL) + (now.tv_nsec / 1000000LL);
Damien Bargiacchi96762812016-07-12 15:53:40 -07001778 if (ACCURATE_TIME_EPOCH < rtcNow
1779 && lastChangedTime > (rtcNow - MAX_TIME_IN_PAST)
1780 && lastChangedTime < (rtcNow + MAX_TIME_IN_FUTURE)) {
Damien Bargiacchi97480862016-03-29 14:55:55 -07001781 mTimeIsAccurate = true;
1782 }
1783 }
1784 }
1785
1786 return mTimeIsAccurate;
1787}
1788
1789BootAnimation::TimeCheckThread::TimeCheckThread(BootAnimation* bootAnimation) : Thread(false),
Josh Yangf95b2002021-12-23 14:32:28 -08001790 mInotifyFd(-1), mBootAnimWd(-1), mTimeWd(-1), mBootAnimation(bootAnimation) {}
Damien Bargiacchi97480862016-03-29 14:55:55 -07001791
1792BootAnimation::TimeCheckThread::~TimeCheckThread() {
1793 // mInotifyFd may be -1 but that's ok since we're not at risk of attempting to close a valid FD.
1794 close(mInotifyFd);
1795}
1796
1797bool BootAnimation::TimeCheckThread::threadLoop() {
1798 bool shouldLoop = doThreadLoop() && !mBootAnimation->mTimeIsAccurate
1799 && mBootAnimation->mClockEnabled;
1800 if (!shouldLoop) {
1801 close(mInotifyFd);
1802 mInotifyFd = -1;
1803 }
1804 return shouldLoop;
1805}
1806
1807bool BootAnimation::TimeCheckThread::doThreadLoop() {
1808 static constexpr int BUFF_LEN (10 * (sizeof(struct inotify_event) + NAME_MAX + 1));
1809
1810 // Poll instead of doing a blocking read so the Thread can exit if requested.
1811 struct pollfd pfd = { mInotifyFd, POLLIN, 0 };
1812 ssize_t pollResult = poll(&pfd, 1, 1000);
1813
1814 if (pollResult == 0) {
1815 return true;
1816 } else if (pollResult < 0) {
Wei Wang159a4102018-10-23 23:15:58 -07001817 SLOGE("Could not poll inotify events");
Damien Bargiacchi97480862016-03-29 14:55:55 -07001818 return false;
1819 }
1820
1821 char buff[BUFF_LEN] __attribute__ ((aligned(__alignof__(struct inotify_event))));;
1822 ssize_t length = read(mInotifyFd, buff, BUFF_LEN);
1823 if (length == 0) {
1824 return true;
1825 } else if (length < 0) {
Wei Wang159a4102018-10-23 23:15:58 -07001826 SLOGE("Could not read inotify events");
Damien Bargiacchi97480862016-03-29 14:55:55 -07001827 return false;
1828 }
1829
1830 const struct inotify_event *event;
1831 for (char* ptr = buff; ptr < buff + length; ptr += sizeof(struct inotify_event) + event->len) {
1832 event = (const struct inotify_event *) ptr;
Josh Yangf95b2002021-12-23 14:32:28 -08001833 if (event->wd == mBootAnimWd && strcmp(BOOTANIM_TIME_DIR_NAME, event->name) == 0) {
Damien Bargiacchi97480862016-03-29 14:55:55 -07001834 addTimeDirWatch();
1835 } else if (event->wd == mTimeWd && (strcmp(LAST_TIME_CHANGED_FILE_NAME, event->name) == 0
1836 || strcmp(ACCURATE_TIME_FLAG_FILE_NAME, event->name) == 0)) {
1837 return !mBootAnimation->updateIsTimeAccurate();
1838 }
1839 }
1840
1841 return true;
1842}
1843
1844void BootAnimation::TimeCheckThread::addTimeDirWatch() {
Josh Yangf95b2002021-12-23 14:32:28 -08001845 mTimeWd = inotify_add_watch(mInotifyFd, BOOTANIM_TIME_DIR_PATH,
Damien Bargiacchi97480862016-03-29 14:55:55 -07001846 IN_CLOSE_WRITE | IN_MOVED_TO | IN_ATTRIB);
1847 if (mTimeWd > 0) {
1848 // No need to watch for the time directory to be created if it already exists
Josh Yangf95b2002021-12-23 14:32:28 -08001849 inotify_rm_watch(mInotifyFd, mBootAnimWd);
1850 mBootAnimWd = -1;
Damien Bargiacchi97480862016-03-29 14:55:55 -07001851 }
1852}
1853
1854status_t BootAnimation::TimeCheckThread::readyToRun() {
1855 mInotifyFd = inotify_init();
1856 if (mInotifyFd < 0) {
Wei Wang159a4102018-10-23 23:15:58 -07001857 SLOGE("Could not initialize inotify fd");
Damien Bargiacchi97480862016-03-29 14:55:55 -07001858 return NO_INIT;
1859 }
1860
Josh Yangf95b2002021-12-23 14:32:28 -08001861 mBootAnimWd = inotify_add_watch(mInotifyFd, BOOTANIM_DATA_DIR_PATH, IN_CREATE | IN_ATTRIB);
1862 if (mBootAnimWd < 0) {
Damien Bargiacchi97480862016-03-29 14:55:55 -07001863 close(mInotifyFd);
1864 mInotifyFd = -1;
Josh Yangf95b2002021-12-23 14:32:28 -08001865 SLOGE("Could not add watch for %s: %s", BOOTANIM_DATA_DIR_PATH, strerror(errno));
Damien Bargiacchi97480862016-03-29 14:55:55 -07001866 return NO_INIT;
1867 }
1868
1869 addTimeDirWatch();
1870
1871 if (mBootAnimation->updateIsTimeAccurate()) {
1872 close(mInotifyFd);
1873 mInotifyFd = -1;
1874 return ALREADY_EXISTS;
1875 }
1876
1877 return NO_ERROR;
1878}
1879
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001880// ---------------------------------------------------------------------------
1881
Marin Shalamanov9070c052020-05-18 19:05:17 +02001882} // namespace android