blob: 25b7f249fc49dd8ec3b9b552bcac6ca0482e39f8 [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>
Shan Huangd711ac42021-07-13 18:36:57 +080055#include <GLES2/gl2.h>
56#include <GLES2/gl2ext.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080057#include <EGL/eglext.h>
58
59#include "BootAnimation.h"
60
Kyeongkab.Nam35a8a112019-07-17 16:41:48 +090061#define ANIM_PATH_MAX 255
62#define STR(x) #x
63#define STRTO(x) STR(x)
64
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080065namespace android {
66
Marin Shalamanovcb783ca2021-01-29 21:26:07 +010067using ui::DisplayMode;
68
Damien Bargiacchi97480862016-03-29 14:55:55 -070069static const char OEM_BOOTANIMATION_FILE[] = "/oem/media/bootanimation.zip";
Beverly34ffe252019-05-17 11:03:54 -040070static const char PRODUCT_BOOTANIMATION_DARK_FILE[] = "/product/media/bootanimation-dark.zip";
Jaekyun Seokc521bb32018-01-30 11:52:47 +090071static const char PRODUCT_BOOTANIMATION_FILE[] = "/product/media/bootanimation.zip";
Damien Bargiacchi97480862016-03-29 14:55:55 -070072static const char SYSTEM_BOOTANIMATION_FILE[] = "/system/media/bootanimation.zip";
Anders Fridlunde0b4d232018-11-06 14:23:25 +010073static const char APEX_BOOTANIMATION_FILE[] = "/apex/com.android.bootanimation/etc/bootanimation.zip";
Jaekyun Seokc521bb32018-01-30 11:52:47 +090074static const char PRODUCT_ENCRYPTED_BOOTANIMATION_FILE[] = "/product/media/bootanimation-encrypted.zip";
Damien Bargiacchi97480862016-03-29 14:55:55 -070075static const char SYSTEM_ENCRYPTED_BOOTANIMATION_FILE[] = "/system/media/bootanimation-encrypted.zip";
Keun-young Parkb5938422017-03-23 13:46:24 -070076static const char OEM_SHUTDOWNANIMATION_FILE[] = "/oem/media/shutdownanimation.zip";
Jaekyun Seokc521bb32018-01-30 11:52:47 +090077static const char PRODUCT_SHUTDOWNANIMATION_FILE[] = "/product/media/shutdownanimation.zip";
Keun-young Parkb5938422017-03-23 13:46:24 -070078static const char SYSTEM_SHUTDOWNANIMATION_FILE[] = "/system/media/shutdownanimation.zip";
79
Nikita Ioffe06c986e2020-01-27 17:16:03 +000080static constexpr const char* PRODUCT_USERSPACE_REBOOT_ANIMATION_FILE = "/product/media/userspace-reboot.zip";
81static constexpr const char* OEM_USERSPACE_REBOOT_ANIMATION_FILE = "/oem/media/userspace-reboot.zip";
82static constexpr const char* SYSTEM_USERSPACE_REBOOT_ANIMATION_FILE = "/system/media/userspace-reboot.zip";
83
Damien Bargiacchi97480862016-03-29 14:55:55 -070084static const char SYSTEM_DATA_DIR_PATH[] = "/data/system";
85static const char SYSTEM_TIME_DIR_NAME[] = "time";
86static const char SYSTEM_TIME_DIR_PATH[] = "/data/system/time";
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -070087static const char CLOCK_FONT_ASSET[] = "images/clock_font.png";
88static const char CLOCK_FONT_ZIP_NAME[] = "clock_font.png";
Nicolas Geoffray41fffb42020-12-15 10:50:29 +000089static const char PROGRESS_FONT_ASSET[] = "images/progress_font.png";
90static const char PROGRESS_FONT_ZIP_NAME[] = "progress_font.png";
Damien Bargiacchi97480862016-03-29 14:55:55 -070091static const char LAST_TIME_CHANGED_FILE_NAME[] = "last_time_change";
92static const char LAST_TIME_CHANGED_FILE_PATH[] = "/data/system/time/last_time_change";
93static const char ACCURATE_TIME_FLAG_FILE_NAME[] = "time_is_accurate";
94static const char ACCURATE_TIME_FLAG_FILE_PATH[] = "/data/system/time/time_is_accurate";
Damien Bargiacchi9071db12016-10-28 17:38:22 -070095static const char TIME_FORMAT_12_HOUR_FLAG_FILE_PATH[] = "/data/system/time/time_format_12_hour";
Damien Bargiacchi96762812016-07-12 15:53:40 -070096// Java timestamp format. Don't show the clock if the date is before 2000-01-01 00:00:00.
97static const long long ACCURATE_TIME_EPOCH = 946684800000;
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -070098static constexpr char FONT_BEGIN_CHAR = ' ';
99static constexpr char FONT_END_CHAR = '~' + 1;
100static constexpr size_t FONT_NUM_CHARS = FONT_END_CHAR - FONT_BEGIN_CHAR + 1;
101static constexpr size_t FONT_NUM_COLS = 16;
102static constexpr size_t FONT_NUM_ROWS = FONT_NUM_CHARS / FONT_NUM_COLS;
103static const int TEXT_CENTER_VALUE = INT_MAX;
104static const int TEXT_MISSING_VALUE = INT_MIN;
Damien Bargiacchi97480862016-03-29 14:55:55 -0700105static const char EXIT_PROP_NAME[] = "service.bootanim.exit";
Nicolas Geoffray41fffb42020-12-15 10:50:29 +0000106static const char PROGRESS_PROP_NAME[] = "service.bootanim.progress";
Huihong Luod4a217e2020-01-16 15:56:33 -0800107static const char DISPLAYS_PROP_NAME[] = "persist.service.bootanim.displays";
Kyeongkab.Nam35a8a112019-07-17 16:41:48 +0900108static const int ANIM_ENTRY_NAME_MAX = ANIM_PATH_MAX + 1;
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700109static constexpr size_t TEXT_POS_LEN_MAX = 16;
Shan Huangd711ac42021-07-13 18:36:57 +0800110static const char U_TEXTURE[] = "uTexture";
111static const char U_FADE[] = "uFade";
112static const char U_CROP_AREA[] = "uCropArea";
113static const char A_UV[] = "aUv";
114static const char A_POSITION[] = "aPosition";
115static const char VERTEX_SHADER_SOURCE[] = R"(
116 attribute vec4 aPosition;
117 attribute vec2 aUv;
118 varying vec2 vUv;
119 void main() {
120 gl_Position = aPosition;
121 vUv = aUv;
122 })";
123static const char IMAGE_FRAG_SHADER_SOURCE[] = R"(
124 uniform sampler2D uTexture;
125 uniform float uFade;
126 varying vec2 vUv;
127 void main() {
128 vec4 color = texture2D(uTexture, vUv);
129 gl_FragColor = vec4(color.x, color.y, color.z, 1.0 - uFade);
130 })";
131static const char TEXT_FRAG_SHADER_SOURCE[] = R"(
132 uniform sampler2D uTexture;
133 uniform vec4 uCropArea;
134 varying vec2 vUv;
135 void main() {
136 vec2 uv = vec2(mix(uCropArea.x, uCropArea.z, vUv.x),
137 mix(uCropArea.y, uCropArea.w, vUv.y));
138 gl_FragColor = texture2D(uTexture, uv);
139 })";
140
141static GLfloat quadPositions[] = {
142 -0.5f, -0.5f,
143 +0.5f, -0.5f,
144 +0.5f, +0.5f,
145 +0.5f, +0.5f,
146 -0.5f, +0.5f,
147 -0.5f, -0.5f
148};
149static GLfloat quadUVs[] = {
150 0.0f, 1.0f,
151 1.0f, 1.0f,
152 1.0f, 0.0f,
153 1.0f, 0.0f,
154 0.0f, 0.0f,
155 0.0f, 1.0f
156};
Narayan Kamathafd31e02013-12-03 13:16:03 +0000157
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800158// ---------------------------------------------------------------------------
159
Ed Coyne7464ac92017-06-08 12:26:48 -0700160BootAnimation::BootAnimation(sp<Callbacks> callbacks)
Marin Shalamanov9070c052020-05-18 19:05:17 +0200161 : Thread(false), mLooper(new Looper(false)), mClockEnabled(true), mTimeIsAccurate(false),
Yi Kong911ac232019-03-24 01:49:02 -0700162 mTimeFormat12Hour(false), mTimeCheckThread(nullptr), mCallbacks(callbacks) {
Mathias Agopian627e7b52009-05-21 19:21:59 -0700163 mSession = new SurfaceComposerClient();
Geoffrey Pitsch290c4352016-08-09 14:35:10 -0400164
Keun-young Parkb5938422017-03-23 13:46:24 -0700165 std::string powerCtl = android::base::GetProperty("sys.powerctl", "");
166 if (powerCtl.empty()) {
167 mShuttingDown = false;
168 } else {
169 mShuttingDown = true;
170 }
Huihong Luo50a2f362018-08-11 09:27:57 -0700171 ALOGD("%sAnimationStartTiming start time: %" PRId64 "ms", mShuttingDown ? "Shutdown" : "Boot",
172 elapsedRealtime());
173}
174
175BootAnimation::~BootAnimation() {
176 if (mAnimation != nullptr) {
177 releaseAnimation(mAnimation);
178 mAnimation = nullptr;
179 }
180 ALOGD("%sAnimationStopTiming start time: %" PRId64 "ms", mShuttingDown ? "Shutdown" : "Boot",
181 elapsedRealtime());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800182}
183
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800184void BootAnimation::onFirstRef() {
Mathias Agopianbc726112009-09-23 15:44:05 -0700185 status_t err = mSession->linkToComposerDeath(this);
Wei Wang159a4102018-10-23 23:15:58 -0700186 SLOGE_IF(err, "linkToComposerDeath failed (%s) ", strerror(-err));
Mathias Agopian8434c532009-09-23 18:52:49 -0700187 if (err == NO_ERROR) {
Huihong Luo50a2f362018-08-11 09:27:57 -0700188 // Load the animation content -- this can be slow (eg 200ms)
189 // called before waitForSurfaceFlinger() in main() to avoid wait
190 ALOGD("%sAnimationPreloadTiming start time: %" PRId64 "ms",
191 mShuttingDown ? "Shutdown" : "Boot", elapsedRealtime());
192 preloadAnimation();
193 ALOGD("%sAnimationPreloadStopTiming start time: %" PRId64 "ms",
194 mShuttingDown ? "Shutdown" : "Boot", elapsedRealtime());
Mathias Agopianbc726112009-09-23 15:44:05 -0700195 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800196}
197
Mathias Agopianbc726112009-09-23 15:44:05 -0700198sp<SurfaceComposerClient> BootAnimation::session() const {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800199 return mSession;
200}
201
Marin Shalamanov9070c052020-05-18 19:05:17 +0200202void BootAnimation::binderDied(const wp<IBinder>&) {
Mathias Agopianbc726112009-09-23 15:44:05 -0700203 // woah, surfaceflinger died!
Wei Wang159a4102018-10-23 23:15:58 -0700204 SLOGD("SurfaceFlinger died, exiting...");
Mathias Agopianbc726112009-09-23 15:44:05 -0700205
206 // calling requestExit() is not enough here because the Surface code
207 // might be blocked on a condition variable that will never be updated.
208 kill( getpid(), SIGKILL );
209 requestExit();
210}
211
Derek Sollenberger22d0d4c72020-04-08 10:53:46 -0400212static void* decodeImage(const void* encodedData, size_t dataLength, AndroidBitmapInfo* outInfo) {
213 AImageDecoder* decoder = nullptr;
214 AImageDecoder_createFromBuffer(encodedData, dataLength, &decoder);
215 if (!decoder) {
216 return nullptr;
217 }
218
219 const AImageDecoderHeaderInfo* info = AImageDecoder_getHeaderInfo(decoder);
220 outInfo->width = AImageDecoderHeaderInfo_getWidth(info);
221 outInfo->height = AImageDecoderHeaderInfo_getHeight(info);
222 outInfo->format = AImageDecoderHeaderInfo_getAndroidBitmapFormat(info);
223 outInfo->stride = AImageDecoder_getMinimumStride(decoder);
224 outInfo->flags = 0;
225
226 const size_t size = outInfo->stride * outInfo->height;
227 void* pixels = malloc(size);
228 int result = AImageDecoder_decodeImage(decoder, pixels, outInfo->stride, size);
229 AImageDecoder_delete(decoder);
230
231 if (result != ANDROID_IMAGE_DECODER_SUCCESS) {
232 free(pixels);
233 return nullptr;
234 }
235 return pixels;
236}
237
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800238status_t BootAnimation::initTexture(Texture* texture, AssetManager& assets,
239 const char* name) {
240 Asset* asset = assets.open(name, Asset::ACCESS_BUFFER);
Yi Kong911ac232019-03-24 01:49:02 -0700241 if (asset == nullptr)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800242 return NO_INIT;
Derek Sollenberger22d0d4c72020-04-08 10:53:46 -0400243
244 AndroidBitmapInfo bitmapInfo;
245 void* pixels = decodeImage(asset->getBuffer(false), asset->getLength(), &bitmapInfo);
246 auto pixelDeleter = std::unique_ptr<void, decltype(free)*>{ pixels, free };
247
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800248 asset->close();
249 delete asset;
250
Derek Sollenberger22d0d4c72020-04-08 10:53:46 -0400251 if (!pixels) {
252 return NO_INIT;
253 }
254
255 const int w = bitmapInfo.width;
256 const int h = bitmapInfo.height;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800257
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800258 texture->w = w;
259 texture->h = h;
260
261 glGenTextures(1, &texture->name);
262 glBindTexture(GL_TEXTURE_2D, texture->name);
263
Derek Sollenberger22d0d4c72020-04-08 10:53:46 -0400264 switch (bitmapInfo.format) {
265 case ANDROID_BITMAP_FORMAT_A_8:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800266 glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, w, h, 0, GL_ALPHA,
Derek Sollenberger22d0d4c72020-04-08 10:53:46 -0400267 GL_UNSIGNED_BYTE, pixels);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800268 break;
Derek Sollenberger22d0d4c72020-04-08 10:53:46 -0400269 case ANDROID_BITMAP_FORMAT_RGBA_4444:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800270 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA,
Derek Sollenberger22d0d4c72020-04-08 10:53:46 -0400271 GL_UNSIGNED_SHORT_4_4_4_4, pixels);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800272 break;
Derek Sollenberger22d0d4c72020-04-08 10:53:46 -0400273 case ANDROID_BITMAP_FORMAT_RGBA_8888:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800274 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA,
Derek Sollenberger22d0d4c72020-04-08 10:53:46 -0400275 GL_UNSIGNED_BYTE, pixels);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800276 break;
Derek Sollenberger22d0d4c72020-04-08 10:53:46 -0400277 case ANDROID_BITMAP_FORMAT_RGB_565:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800278 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, w, h, 0, GL_RGB,
Derek Sollenberger22d0d4c72020-04-08 10:53:46 -0400279 GL_UNSIGNED_SHORT_5_6_5, pixels);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800280 break;
281 default:
282 break;
283 }
284
Shan Huangd711ac42021-07-13 18:36:57 +0800285 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
286 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
287 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
288 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700289
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800290 return NO_ERROR;
291}
292
Marin Shalamanov9070c052020-05-18 19:05:17 +0200293status_t BootAnimation::initTexture(FileMap* map, int* width, int* height) {
Derek Sollenberger22d0d4c72020-04-08 10:53:46 -0400294 AndroidBitmapInfo bitmapInfo;
295 void* pixels = decodeImage(map->getDataPtr(), map->getDataLength(), &bitmapInfo);
296 auto pixelDeleter = std::unique_ptr<void, decltype(free)*>{ pixels, free };
Mathias Agopiana8826d62009-10-01 03:10:14 -0700297
Mykola Kondratenko0c1eeb32014-04-15 09:35:44 +0200298 // FileMap memory is never released until application exit.
299 // Release it now as the texture is already loaded and the memory used for
300 // the packed resource can be released.
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700301 delete map;
Mykola Kondratenko0c1eeb32014-04-15 09:35:44 +0200302
Derek Sollenberger22d0d4c72020-04-08 10:53:46 -0400303 if (!pixels) {
304 return NO_INIT;
305 }
306
307 const int w = bitmapInfo.width;
308 const int h = bitmapInfo.height;
Mathias Agopiana8826d62009-10-01 03:10:14 -0700309
Mathias Agopiana8826d62009-10-01 03:10:14 -0700310 int tw = 1 << (31 - __builtin_clz(w));
311 int th = 1 << (31 - __builtin_clz(h));
312 if (tw < w) tw <<= 1;
313 if (th < h) th <<= 1;
314
Derek Sollenberger22d0d4c72020-04-08 10:53:46 -0400315 switch (bitmapInfo.format) {
316 case ANDROID_BITMAP_FORMAT_RGBA_8888:
Sai Kiran Korwar27167492015-07-07 20:00:06 +0530317 if (!mUseNpotTextures && (tw != w || th != h)) {
Mathias Agopiana8826d62009-10-01 03:10:14 -0700318 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, tw, th, 0, GL_RGBA,
Yi Kong911ac232019-03-24 01:49:02 -0700319 GL_UNSIGNED_BYTE, nullptr);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700320 glTexSubImage2D(GL_TEXTURE_2D, 0,
Derek Sollenberger22d0d4c72020-04-08 10:53:46 -0400321 0, 0, w, h, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700322 } else {
Sai Kiran Korwar27167492015-07-07 20:00:06 +0530323 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA,
Derek Sollenberger22d0d4c72020-04-08 10:53:46 -0400324 GL_UNSIGNED_BYTE, pixels);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700325 }
326 break;
327
Derek Sollenberger22d0d4c72020-04-08 10:53:46 -0400328 case ANDROID_BITMAP_FORMAT_RGB_565:
Sai Kiran Korwar27167492015-07-07 20:00:06 +0530329 if (!mUseNpotTextures && (tw != w || th != h)) {
Mathias Agopiana8826d62009-10-01 03:10:14 -0700330 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, tw, th, 0, GL_RGB,
Yi Kong911ac232019-03-24 01:49:02 -0700331 GL_UNSIGNED_SHORT_5_6_5, nullptr);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700332 glTexSubImage2D(GL_TEXTURE_2D, 0,
Derek Sollenberger22d0d4c72020-04-08 10:53:46 -0400333 0, 0, w, h, GL_RGB, GL_UNSIGNED_SHORT_5_6_5, pixels);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700334 } else {
Sai Kiran Korwar27167492015-07-07 20:00:06 +0530335 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, w, h, 0, GL_RGB,
Derek Sollenberger22d0d4c72020-04-08 10:53:46 -0400336 GL_UNSIGNED_SHORT_5_6_5, pixels);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700337 }
338 break;
339 default:
340 break;
341 }
342
Shan Huangd711ac42021-07-13 18:36:57 +0800343 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
344 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
345 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
346 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700347
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700348 *width = w;
349 *height = h;
350
Mathias Agopiana8826d62009-10-01 03:10:14 -0700351 return NO_ERROR;
352}
353
Marin Shalamanov9070c052020-05-18 19:05:17 +0200354class BootAnimation::DisplayEventCallback : public LooperCallback {
355 BootAnimation* mBootAnimation;
356
357public:
358 DisplayEventCallback(BootAnimation* bootAnimation) {
359 mBootAnimation = bootAnimation;
360 }
361
362 int handleEvent(int /* fd */, int events, void* /* data */) {
363 if (events & (Looper::EVENT_ERROR | Looper::EVENT_HANGUP)) {
364 ALOGE("Display event receiver pipe was closed or an error occurred. events=0x%x",
365 events);
366 return 0; // remove the callback
367 }
368
369 if (!(events & Looper::EVENT_INPUT)) {
370 ALOGW("Received spurious callback for unhandled poll event. events=0x%x", events);
371 return 1; // keep the callback
372 }
373
374 constexpr int kBufferSize = 100;
375 DisplayEventReceiver::Event buffer[kBufferSize];
376 ssize_t numEvents;
377 do {
378 numEvents = mBootAnimation->mDisplayEventReceiver->getEvents(buffer, kBufferSize);
379 for (size_t i = 0; i < static_cast<size_t>(numEvents); i++) {
380 const auto& event = buffer[i];
381 if (event.header.type == DisplayEventReceiver::DISPLAY_EVENT_HOTPLUG) {
382 SLOGV("Hotplug received");
383
384 if (!event.hotplug.connected) {
385 // ignore hotplug disconnect
386 continue;
387 }
388 auto token = SurfaceComposerClient::getPhysicalDisplayToken(
389 event.header.displayId);
390
391 if (token != mBootAnimation->mDisplayToken) {
392 // ignore hotplug of a secondary display
393 continue;
394 }
395
Marin Shalamanovcb783ca2021-01-29 21:26:07 +0100396 DisplayMode displayMode;
397 const status_t error = SurfaceComposerClient::getActiveDisplayMode(
398 mBootAnimation->mDisplayToken, &displayMode);
Marin Shalamanov9070c052020-05-18 19:05:17 +0200399 if (error != NO_ERROR) {
Marin Shalamanovcb783ca2021-01-29 21:26:07 +0100400 SLOGE("Can't get active display mode.");
Marin Shalamanov9070c052020-05-18 19:05:17 +0200401 }
Marin Shalamanovcb783ca2021-01-29 21:26:07 +0100402 mBootAnimation->resizeSurface(displayMode.resolution.getWidth(),
403 displayMode.resolution.getHeight());
Marin Shalamanov9070c052020-05-18 19:05:17 +0200404 }
405 }
406 } while (numEvents > 0);
407
408 return 1; // keep the callback
409 }
410};
411
412EGLConfig BootAnimation::getEglConfig(const EGLDisplay& display) {
413 const EGLint attribs[] = {
414 EGL_RED_SIZE, 8,
415 EGL_GREEN_SIZE, 8,
416 EGL_BLUE_SIZE, 8,
417 EGL_DEPTH_SIZE, 0,
418 EGL_NONE
419 };
420 EGLint numConfigs;
421 EGLConfig config;
422 eglChooseConfig(display, attribs, &config, 1, &numConfigs);
423 return config;
424}
425
Marin Shalamanov047802d2020-05-19 23:55:12 +0200426ui::Size BootAnimation::limitSurfaceSize(int width, int height) const {
427 ui::Size limited(width, height);
428 bool wasLimited = false;
429 const float aspectRatio = float(width) / float(height);
430 if (mMaxWidth != 0 && width > mMaxWidth) {
431 limited.height = mMaxWidth / aspectRatio;
432 limited.width = mMaxWidth;
433 wasLimited = true;
434 }
435 if (mMaxHeight != 0 && limited.height > mMaxHeight) {
436 limited.height = mMaxHeight;
437 limited.width = mMaxHeight * aspectRatio;
438 wasLimited = true;
439 }
440 SLOGV_IF(wasLimited, "Surface size has been limited to [%dx%d] from [%dx%d]",
441 limited.width, limited.height, width, height);
442 return limited;
443}
444
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800445status_t BootAnimation::readyToRun() {
446 mAssets.addDefaultAssets();
447
Dominik Laskowski3316a0a2019-01-25 02:56:41 -0800448 mDisplayToken = SurfaceComposerClient::getInternalDisplayToken();
449 if (mDisplayToken == nullptr)
Dominik Laskowski69b281d2019-11-22 14:13:12 -0800450 return NAME_NOT_FOUND;
Dominik Laskowski3316a0a2019-01-25 02:56:41 -0800451
Marin Shalamanovcb783ca2021-01-29 21:26:07 +0100452 DisplayMode displayMode;
Dominik Laskowski69b281d2019-11-22 14:13:12 -0800453 const status_t error =
Marin Shalamanovcb783ca2021-01-29 21:26:07 +0100454 SurfaceComposerClient::getActiveDisplayMode(mDisplayToken, &displayMode);
Dominik Laskowski69b281d2019-11-22 14:13:12 -0800455 if (error != NO_ERROR)
456 return error;
457
Marin Shalamanov047802d2020-05-19 23:55:12 +0200458 mMaxWidth = android::base::GetIntProperty("ro.surface_flinger.max_graphics_width", 0);
459 mMaxHeight = android::base::GetIntProperty("ro.surface_flinger.max_graphics_height", 0);
Marin Shalamanovcb783ca2021-01-29 21:26:07 +0100460 ui::Size resolution = displayMode.resolution;
Marin Shalamanov047802d2020-05-19 23:55:12 +0200461 resolution = limitSurfaceSize(resolution.width, resolution.height);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800462 // create the native surface
Jeff Brown0b722fe2012-08-24 22:40:14 -0700463 sp<SurfaceControl> control = session()->createSurface(String8("BootAnimation"),
Dominik Laskowski69b281d2019-11-22 14:13:12 -0800464 resolution.getWidth(), resolution.getHeight(), PIXEL_FORMAT_RGB_565);
Mathias Agopian439863f2011-06-28 19:09:31 -0700465
Robert Carre13b58e2017-08-31 14:50:44 -0700466 SurfaceComposerClient::Transaction t;
Huihong Luof97c9e22019-05-29 11:08:01 -0700467
468 // this guest property specifies multi-display IDs to show the boot animation
469 // multiple ids can be set with comma (,) as separator, for example:
Huihong Luod4a217e2020-01-16 15:56:33 -0800470 // setprop persist.boot.animation.displays 19260422155234049,19261083906282754
Marin Shalamanov11599ee2020-07-27 21:31:48 +0200471 Vector<PhysicalDisplayId> physicalDisplayIds;
Huihong Luof97c9e22019-05-29 11:08:01 -0700472 char displayValue[PROPERTY_VALUE_MAX] = "";
Huihong Luod4a217e2020-01-16 15:56:33 -0800473 property_get(DISPLAYS_PROP_NAME, displayValue, "");
Huihong Luof97c9e22019-05-29 11:08:01 -0700474 bool isValid = displayValue[0] != '\0';
475 if (isValid) {
476 char *p = displayValue;
477 while (*p) {
478 if (!isdigit(*p) && *p != ',') {
479 isValid = false;
480 break;
481 }
482 p ++;
483 }
484 if (!isValid)
Huihong Luod4a217e2020-01-16 15:56:33 -0800485 SLOGE("Invalid syntax for the value of system prop: %s", DISPLAYS_PROP_NAME);
Huihong Luof97c9e22019-05-29 11:08:01 -0700486 }
487 if (isValid) {
488 std::istringstream stream(displayValue);
Marin Shalamanov11599ee2020-07-27 21:31:48 +0200489 for (PhysicalDisplayId id; stream >> id.value; ) {
Huihong Luof97c9e22019-05-29 11:08:01 -0700490 physicalDisplayIds.add(id);
491 if (stream.peek() == ',')
492 stream.ignore();
493 }
494
495 // In the case of multi-display, boot animation shows on the specified displays
496 // in addition to the primary display
497 auto ids = SurfaceComposerClient::getPhysicalDisplayIds();
498 constexpr uint32_t LAYER_STACK = 0;
499 for (auto id : physicalDisplayIds) {
500 if (std::find(ids.begin(), ids.end(), id) != ids.end()) {
501 sp<IBinder> token = SurfaceComposerClient::getPhysicalDisplayToken(id);
502 if (token != nullptr)
503 t.setDisplayLayerStack(token, LAYER_STACK);
504 }
505 }
506 t.setLayerStack(control, LAYER_STACK);
507 }
508
Robert Carre13b58e2017-08-31 14:50:44 -0700509 t.setLayer(control, 0x40000000)
510 .apply();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800511
Mathias Agopian17f638b2009-04-16 20:04:08 -0700512 sp<Surface> s = control->getSurface();
513
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800514 // initialize opengl and egl
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800515 EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
Yi Kong911ac232019-03-24 01:49:02 -0700516 eglInitialize(display, nullptr, nullptr);
Marin Shalamanov9070c052020-05-18 19:05:17 +0200517 EGLConfig config = getEglConfig(display);
518 EGLSurface surface = eglCreateWindowSurface(display, config, s.get(), nullptr);
Shan Huangd711ac42021-07-13 18:36:57 +0800519 // Initialize egl context with client version number 2.0.
520 EGLint contextAttributes[] = {EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE};
521 EGLContext context = eglCreateContext(display, config, nullptr, contextAttributes);
Marin Shalamanov9070c052020-05-18 19:05:17 +0200522 EGLint w, h;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800523 eglQuerySurface(display, surface, EGL_WIDTH, &w);
524 eglQuerySurface(display, surface, EGL_HEIGHT, &h);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700525
Mathias Agopianabac0102009-07-31 14:47:00 -0700526 if (eglMakeCurrent(display, surface, surface, context) == EGL_FALSE)
527 return NO_INIT;
Mathias Agopiana8826d62009-10-01 03:10:14 -0700528
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800529 mDisplay = display;
530 mContext = context;
531 mSurface = surface;
532 mWidth = w;
533 mHeight = h;
Mathias Agopian17f638b2009-04-16 20:04:08 -0700534 mFlingerSurfaceControl = control;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800535 mFlingerSurface = s;
Adrian Roos9ee5dff2018-08-22 20:19:49 +0200536 mTargetInset = -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800537
Yegor Malyshev6925e1b2020-09-10 16:42:20 +0200538 projectSceneToWindow();
539
Marin Shalamanov9070c052020-05-18 19:05:17 +0200540 // Register a display event receiver
541 mDisplayEventReceiver = std::make_unique<DisplayEventReceiver>();
542 status_t status = mDisplayEventReceiver->initCheck();
543 SLOGE_IF(status != NO_ERROR, "Initialization of DisplayEventReceiver failed with status: %d",
544 status);
545 mLooper->addFd(mDisplayEventReceiver->getFd(), 0, Looper::EVENT_INPUT,
546 new DisplayEventCallback(this), nullptr);
547
Huihong Luo50a2f362018-08-11 09:27:57 -0700548 return NO_ERROR;
549}
550
Yegor Malyshev6925e1b2020-09-10 16:42:20 +0200551void BootAnimation::projectSceneToWindow() {
552 glViewport(0, 0, mWidth, mHeight);
553 glScissor(0, 0, mWidth, mHeight);
Yegor Malyshev6925e1b2020-09-10 16:42:20 +0200554}
555
Marin Shalamanov9070c052020-05-18 19:05:17 +0200556void BootAnimation::resizeSurface(int newWidth, int newHeight) {
557 // We assume this function is called on the animation thread.
558 if (newWidth == mWidth && newHeight == mHeight) {
559 return;
560 }
561 SLOGV("Resizing the boot animation surface to %d %d", newWidth, newHeight);
562
563 eglMakeCurrent(mDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
564 eglDestroySurface(mDisplay, mSurface);
565
Marin Shalamanov047802d2020-05-19 23:55:12 +0200566 const auto limitedSize = limitSurfaceSize(newWidth, newHeight);
567 mWidth = limitedSize.width;
568 mHeight = limitedSize.height;
Marin Shalamanov9070c052020-05-18 19:05:17 +0200569
570 SurfaceComposerClient::Transaction t;
571 t.setSize(mFlingerSurfaceControl, mWidth, mHeight);
572 t.apply();
573
574 EGLConfig config = getEglConfig(mDisplay);
575 EGLSurface surface = eglCreateWindowSurface(mDisplay, config, mFlingerSurface.get(), nullptr);
576 if (eglMakeCurrent(mDisplay, surface, surface, mContext) == EGL_FALSE) {
577 SLOGE("Can't make the new surface current. Error %d", eglGetError());
578 return;
579 }
Yegor Malyshev6925e1b2020-09-10 16:42:20 +0200580
581 projectSceneToWindow();
Marin Shalamanov9070c052020-05-18 19:05:17 +0200582
583 mSurface = surface;
584}
585
Huihong Luo50a2f362018-08-11 09:27:57 -0700586bool BootAnimation::preloadAnimation() {
587 findBootAnimationFile();
588 if (!mZipFileName.isEmpty()) {
589 mAnimation = loadAnimation(mZipFileName);
590 return (mAnimation != nullptr);
591 }
592
593 return false;
594}
595
Nikita Ioffe06c986e2020-01-27 17:16:03 +0000596bool BootAnimation::findBootAnimationFileInternal(const std::vector<std::string> &files) {
597 for (const std::string& f : files) {
598 if (access(f.c_str(), R_OK) == 0) {
599 mZipFileName = f.c_str();
600 return true;
601 }
602 }
603 return false;
604}
605
Huihong Luo50a2f362018-08-11 09:27:57 -0700606void BootAnimation::findBootAnimationFile() {
Elliott Hughesc367d482013-10-29 13:12:55 -0700607 // If the device has encryption turned on or is in process
Jason parksbd9a08d2011-01-31 15:04:34 -0600608 // of being encrypted we show the encrypted boot animation.
609 char decrypt[PROPERTY_VALUE_MAX];
610 property_get("vold.decrypt", decrypt, "");
611
Keun-young Parkb5938422017-03-23 13:46:24 -0700612 bool encryptedAnimation = atoi(decrypt) != 0 ||
613 !strcmp("trigger_restart_min_framework", decrypt);
Jason parksbd9a08d2011-01-31 15:04:34 -0600614
Jaekyun Seokc521bb32018-01-30 11:52:47 +0900615 if (!mShuttingDown && encryptedAnimation) {
Nikita Ioffe06c986e2020-01-27 17:16:03 +0000616 static const std::vector<std::string> encryptedBootFiles = {
617 PRODUCT_ENCRYPTED_BOOTANIMATION_FILE, SYSTEM_ENCRYPTED_BOOTANIMATION_FILE,
618 };
619 if (findBootAnimationFileInternal(encryptedBootFiles)) {
620 return;
Jaekyun Seokc521bb32018-01-30 11:52:47 +0900621 }
Jason parksbd9a08d2011-01-31 15:04:34 -0600622 }
Beverly34ffe252019-05-17 11:03:54 -0400623
624 const bool playDarkAnim = android::base::GetIntProperty("ro.boot.theme", 0) == 1;
Nikita Ioffe06c986e2020-01-27 17:16:03 +0000625 static const std::vector<std::string> bootFiles = {
626 APEX_BOOTANIMATION_FILE, playDarkAnim ? PRODUCT_BOOTANIMATION_DARK_FILE : PRODUCT_BOOTANIMATION_FILE,
627 OEM_BOOTANIMATION_FILE, SYSTEM_BOOTANIMATION_FILE
628 };
629 static const std::vector<std::string> shutdownFiles = {
630 PRODUCT_SHUTDOWNANIMATION_FILE, OEM_SHUTDOWNANIMATION_FILE, SYSTEM_SHUTDOWNANIMATION_FILE, ""
631 };
632 static const std::vector<std::string> userspaceRebootFiles = {
633 PRODUCT_USERSPACE_REBOOT_ANIMATION_FILE, OEM_USERSPACE_REBOOT_ANIMATION_FILE,
634 SYSTEM_USERSPACE_REBOOT_ANIMATION_FILE,
635 };
Keun-young Parkb5938422017-03-23 13:46:24 -0700636
Nikita Ioffe06c986e2020-01-27 17:16:03 +0000637 if (android::base::GetBoolProperty("sys.init.userspace_reboot.in_progress", false)) {
638 findBootAnimationFileInternal(userspaceRebootFiles);
639 } else if (mShuttingDown) {
640 findBootAnimationFileInternal(shutdownFiles);
641 } else {
642 findBootAnimationFileInternal(bootFiles);
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700643 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800644}
645
Shan Huangd711ac42021-07-13 18:36:57 +0800646GLuint compileShader(GLenum shaderType, const GLchar *source) {
647 GLuint shader = glCreateShader(shaderType);
648 glShaderSource(shader, 1, &source, 0);
649 glCompileShader(shader);
650 GLint isCompiled = 0;
651 glGetShaderiv(shader, GL_COMPILE_STATUS, &isCompiled);
652 if (isCompiled == GL_FALSE) {
653 SLOGE("Compile shader failed. Shader type: %d", shaderType);
654 return 0;
655 }
656 return shader;
657}
658
659GLuint linkShader(GLuint vertexShader, GLuint fragmentShader) {
660 GLuint program = glCreateProgram();
661 glAttachShader(program, vertexShader);
662 glAttachShader(program, fragmentShader);
663 glLinkProgram(program);
664 GLint isLinked = 0;
665 glGetProgramiv(program, GL_LINK_STATUS, (int *)&isLinked);
666 if (isLinked == GL_FALSE) {
667 SLOGE("Linking shader failed. Shader handles: vert %d, frag %d",
668 vertexShader, fragmentShader);
669 return 0;
670 }
671 return program;
672}
673
674void BootAnimation::initShaders() {
675 GLuint vertexShader = compileShader(GL_VERTEX_SHADER, (const GLchar *)VERTEX_SHADER_SOURCE);
676 GLuint imageFragmentShader =
677 compileShader(GL_FRAGMENT_SHADER, (const GLchar *)IMAGE_FRAG_SHADER_SOURCE);
678 GLuint textFragmentShader =
679 compileShader(GL_FRAGMENT_SHADER, (const GLchar *)TEXT_FRAG_SHADER_SOURCE);
680
681 // Initialize image shader.
682 mImageShader = linkShader(vertexShader, imageFragmentShader);
683 GLint positionLocation = glGetAttribLocation(mImageShader, A_POSITION);
684 GLint uvLocation = glGetAttribLocation(mImageShader, A_UV);
685 mImageTextureLocation = glGetUniformLocation(mImageShader, U_TEXTURE);
686 mImageFadeLocation = glGetUniformLocation(mImageShader, U_FADE);
687 glEnableVertexAttribArray(positionLocation);
688 glVertexAttribPointer(positionLocation, 2, GL_FLOAT, GL_FALSE, 0, quadPositions);
689 glVertexAttribPointer(uvLocation, 2, GL_FLOAT, GL_FALSE, 0, quadUVs);
690 glEnableVertexAttribArray(uvLocation);
691
692 // Initialize text shader.
693 mTextShader = linkShader(vertexShader, textFragmentShader);
694 positionLocation = glGetAttribLocation(mTextShader, A_POSITION);
695 uvLocation = glGetAttribLocation(mTextShader, A_UV);
696 mTextTextureLocation = glGetUniformLocation(mTextShader, U_TEXTURE);
697 mTextCropAreaLocation = glGetUniformLocation(mTextShader, U_CROP_AREA);
698 glEnableVertexAttribArray(positionLocation);
699 glVertexAttribPointer(positionLocation, 2, GL_FLOAT, GL_FALSE, 0, quadPositions);
700 glVertexAttribPointer(uvLocation, 2, GL_FLOAT, GL_FALSE, 0, quadUVs);
701 glEnableVertexAttribArray(uvLocation);
702}
703
Marin Shalamanov9070c052020-05-18 19:05:17 +0200704bool BootAnimation::threadLoop() {
705 bool result;
Shan Huangd711ac42021-07-13 18:36:57 +0800706 initShaders();
707
Narayan Kamathafd31e02013-12-03 13:16:03 +0000708 // We have no bootanimation file, so we use the stock android logo
709 // animation.
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700710 if (mZipFileName.isEmpty()) {
Marin Shalamanov9070c052020-05-18 19:05:17 +0200711 result = android();
Mathias Agopiana8826d62009-10-01 03:10:14 -0700712 } else {
Marin Shalamanov9070c052020-05-18 19:05:17 +0200713 result = movie();
Mathias Agopiana8826d62009-10-01 03:10:14 -0700714 }
715
Philip Junker60600562020-11-25 18:16:16 +0100716 mCallbacks->shutdown();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800717 eglMakeCurrent(mDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
718 eglDestroyContext(mDisplay, mContext);
719 eglDestroySurface(mDisplay, mSurface);
Mathias Agopian6cf0db22009-04-17 19:36:26 -0700720 mFlingerSurface.clear();
Mathias Agopian17f638b2009-04-16 20:04:08 -0700721 mFlingerSurfaceControl.clear();
Mathias Agopian627e7b52009-05-21 19:21:59 -0700722 eglTerminate(mDisplay);
Sai Kiran Korwar3eee9fb2015-06-16 17:13:35 +0530723 eglReleaseThread();
Mathias Agopian627e7b52009-05-21 19:21:59 -0700724 IPCThreadState::self()->stopProcess();
Marin Shalamanov9070c052020-05-18 19:05:17 +0200725 return result;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800726}
727
Marin Shalamanov9070c052020-05-18 19:05:17 +0200728bool BootAnimation::android() {
Shan Huangd711ac42021-07-13 18:36:57 +0800729 glActiveTexture(GL_TEXTURE0);
730
Wei Wang159a4102018-10-23 23:15:58 -0700731 SLOGD("%sAnimationShownTiming start time: %" PRId64 "ms", mShuttingDown ? "Shutdown" : "Boot",
Keun-young Parkd1794cd2017-04-04 12:21:19 -0700732 elapsedRealtime());
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700733 initTexture(&mAndroid[0], mAssets, "images/android-logo-mask.png");
734 initTexture(&mAndroid[1], mAssets, "images/android-logo-shine.png");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800735
Ed Coyne7464ac92017-06-08 12:26:48 -0700736 mCallbacks->init({});
737
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800738 // clear screen
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700739 glDisable(GL_DITHER);
740 glDisable(GL_SCISSOR_TEST);
Mathias Agopian59f19e42011-05-06 19:22:12 -0700741 glClearColor(0,0,0,1);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800742 glClear(GL_COLOR_BUFFER_BIT);
743 eglSwapBuffers(mDisplay, mSurface);
744
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700745 // Blend state
746 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700747
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800748 const nsecs_t startTime = systemTime();
749 do {
Marin Shalamanov9070c052020-05-18 19:05:17 +0200750 processDisplayEvents();
751 const GLint xc = (mWidth - mAndroid[0].w) / 2;
752 const GLint yc = (mHeight - mAndroid[0].h) / 2;
753 const Rect updateRect(xc, yc, xc + mAndroid[0].w, yc + mAndroid[0].h);
754 glScissor(updateRect.left, mHeight - updateRect.bottom, updateRect.width(),
755 updateRect.height());
756
Mathias Agopian13796652009-03-24 22:49:21 -0700757 nsecs_t now = systemTime();
758 double time = now - startTime;
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700759 float t = 4.0f * float(time / us2ns(16667)) / mAndroid[1].w;
760 GLint offset = (1 - (t - floorf(t))) * mAndroid[1].w;
761 GLint x = xc - offset;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800762
Mathias Agopian81668642009-07-28 11:41:30 -0700763 glDisable(GL_SCISSOR_TEST);
764 glClear(GL_COLOR_BUFFER_BIT);
765
766 glEnable(GL_SCISSOR_TEST);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800767 glDisable(GL_BLEND);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800768 glBindTexture(GL_TEXTURE_2D, mAndroid[1].name);
Shan Huangd711ac42021-07-13 18:36:57 +0800769 drawTexturedQuad(x, yc, mAndroid[1].w, mAndroid[1].h);
770 drawTexturedQuad(x + mAndroid[1].w, yc, mAndroid[1].w, mAndroid[1].h);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800771
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700772 glEnable(GL_BLEND);
773 glBindTexture(GL_TEXTURE_2D, mAndroid[0].name);
Shan Huangd711ac42021-07-13 18:36:57 +0800774 drawTexturedQuad(xc, yc, mAndroid[0].w, mAndroid[0].h);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800775
Mathias Agopian627e7b52009-05-21 19:21:59 -0700776 EGLBoolean res = eglSwapBuffers(mDisplay, mSurface);
777 if (res == EGL_FALSE)
778 break;
779
Mathias Agopian13796652009-03-24 22:49:21 -0700780 // 12fps: don't animate too fast to preserve CPU
781 const nsecs_t sleepTime = 83333 - ns2us(systemTime() - now);
782 if (sleepTime > 0)
Mathias Agopiana8826d62009-10-01 03:10:14 -0700783 usleep(sleepTime);
Kevin Hesterd3782b22012-04-26 10:38:55 -0700784
785 checkExit();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800786 } while (!exitPending());
787
788 glDeleteTextures(1, &mAndroid[0].name);
789 glDeleteTextures(1, &mAndroid[1].name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800790 return false;
791}
792
Kevin Hesterd3782b22012-04-26 10:38:55 -0700793void BootAnimation::checkExit() {
794 // Allow surface flinger to gracefully request shutdown
795 char value[PROPERTY_VALUE_MAX];
796 property_get(EXIT_PROP_NAME, value, "0");
797 int exitnow = atoi(value);
798 if (exitnow) {
799 requestExit();
800 }
801}
802
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700803bool BootAnimation::validClock(const Animation::Part& part) {
804 return part.clockPosX != TEXT_MISSING_VALUE && part.clockPosY != TEXT_MISSING_VALUE;
805}
806
807bool parseTextCoord(const char* str, int* dest) {
808 if (strcmp("c", str) == 0) {
809 *dest = TEXT_CENTER_VALUE;
810 return true;
811 }
812
813 char* end;
814 int val = (int) strtol(str, &end, 0);
815 if (end == str || *end != '\0' || val == INT_MAX || val == INT_MIN) {
816 return false;
817 }
818 *dest = val;
819 return true;
820}
821
822// Parse two position coordinates. If only string is non-empty, treat it as the y value.
823void parsePosition(const char* str1, const char* str2, int* x, int* y) {
824 bool success = false;
825 if (strlen(str1) == 0) { // No values were specified
826 // success = false
827 } else if (strlen(str2) == 0) { // we have only one value
828 if (parseTextCoord(str1, y)) {
829 *x = TEXT_CENTER_VALUE;
830 success = true;
831 }
832 } else {
833 if (parseTextCoord(str1, x) && parseTextCoord(str2, y)) {
834 success = true;
835 }
836 }
837
838 if (!success) {
839 *x = TEXT_MISSING_VALUE;
840 *y = TEXT_MISSING_VALUE;
841 }
842}
843
Jesse Hall083b84c2014-09-22 10:51:09 -0700844// Parse a color represented as an HTML-style 'RRGGBB' string: each pair of
845// characters in str is a hex number in [0, 255], which are converted to
846// floating point values in the range [0.0, 1.0] and placed in the
847// corresponding elements of color.
848//
849// If the input string isn't valid, parseColor returns false and color is
850// left unchanged.
851static bool parseColor(const char str[7], float color[3]) {
852 float tmpColor[3];
853 for (int i = 0; i < 3; i++) {
854 int val = 0;
855 for (int j = 0; j < 2; j++) {
856 val *= 16;
857 char c = str[2*i + j];
858 if (c >= '0' && c <= '9') val += c - '0';
859 else if (c >= 'A' && c <= 'F') val += (c - 'A') + 10;
860 else if (c >= 'a' && c <= 'f') val += (c - 'a') + 10;
861 else return false;
862 }
863 tmpColor[i] = static_cast<float>(val) / 255.0f;
864 }
865 memcpy(color, tmpColor, sizeof(tmpColor));
866 return true;
867}
868
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700869
Marin Shalamanov9070c052020-05-18 19:05:17 +0200870static bool readFile(ZipFileRO* zip, const char* name, String8& outString) {
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700871 ZipEntryRO entry = zip->findEntryByName(name);
Wei Wang159a4102018-10-23 23:15:58 -0700872 SLOGE_IF(!entry, "couldn't find %s", name);
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700873 if (!entry) {
874 return false;
875 }
876
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700877 FileMap* entryMap = zip->createEntryFileMap(entry);
878 zip->releaseEntry(entry);
Wei Wang159a4102018-10-23 23:15:58 -0700879 SLOGE_IF(!entryMap, "entryMap is null");
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700880 if (!entryMap) {
881 return false;
882 }
883
884 outString.setTo((char const*)entryMap->getDataPtr(), entryMap->getDataLength());
Narayan Kamath688ff4c2015-02-23 15:47:54 +0000885 delete entryMap;
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700886 return true;
887}
888
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700889// The font image should be a 96x2 array of character images. The
890// columns are the printable ASCII characters 0x20 - 0x7f. The
891// top row is regular text; the bottom row is bold.
892status_t BootAnimation::initFont(Font* font, const char* fallback) {
893 status_t status = NO_ERROR;
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800894
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700895 if (font->map != nullptr) {
896 glGenTextures(1, &font->texture.name);
897 glBindTexture(GL_TEXTURE_2D, font->texture.name);
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800898
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700899 status = initTexture(font->map, &font->texture.w, &font->texture.h);
900
Shan Huangd711ac42021-07-13 18:36:57 +0800901 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
902 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
903 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
904 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700905 } else if (fallback != nullptr) {
906 status = initTexture(&font->texture, mAssets, fallback);
907 } else {
908 return NO_INIT;
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800909 }
910
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700911 if (status == NO_ERROR) {
912 font->char_width = font->texture.w / FONT_NUM_COLS;
913 font->char_height = font->texture.h / FONT_NUM_ROWS / 2; // There are bold and regular rows
914 }
915
916 return status;
917}
918
919void BootAnimation::drawText(const char* str, const Font& font, bool bold, int* x, int* y) {
920 glEnable(GL_BLEND); // Allow us to draw on top of the animation
921 glBindTexture(GL_TEXTURE_2D, font.texture.name);
Shan Huangd711ac42021-07-13 18:36:57 +0800922 glUseProgram(mTextShader);
923 glUniform1i(mTextTextureLocation, 0);
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700924
925 const int len = strlen(str);
926 const int strWidth = font.char_width * len;
927
928 if (*x == TEXT_CENTER_VALUE) {
929 *x = (mWidth - strWidth) / 2;
930 } else if (*x < 0) {
931 *x = mWidth + *x - strWidth;
932 }
933 if (*y == TEXT_CENTER_VALUE) {
934 *y = (mHeight - font.char_height) / 2;
935 } else if (*y < 0) {
936 *y = mHeight + *y - font.char_height;
937 }
938
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700939 for (int i = 0; i < len; i++) {
940 char c = str[i];
941
942 if (c < FONT_BEGIN_CHAR || c > FONT_END_CHAR) {
943 c = '?';
944 }
945
946 // Crop the texture to only the pixels in the current glyph
947 const int charPos = (c - FONT_BEGIN_CHAR); // Position in the list of valid characters
948 const int row = charPos / FONT_NUM_COLS;
949 const int col = charPos % FONT_NUM_COLS;
Shan Huangd711ac42021-07-13 18:36:57 +0800950 // Bold fonts are expected in the second half of each row.
951 float v0 = (row + (bold ? 0.5f : 0.0f)) / FONT_NUM_ROWS;
952 float u0 = ((float)col) / FONT_NUM_COLS;
953 float v1 = v0 + 1.0f / FONT_NUM_ROWS / 2;
954 float u1 = u0 + 1.0f / FONT_NUM_COLS;
955 glUniform4f(mTextCropAreaLocation, u0, v0, u1, v1);
956 drawTexturedQuad(*x, *y, font.char_width, font.char_height);
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700957
958 *x += font.char_width;
959 }
960
961 glDisable(GL_BLEND); // Return to the animation's default behaviour
962 glBindTexture(GL_TEXTURE_2D, 0);
963}
964
Damien Bargiacchi9071db12016-10-28 17:38:22 -0700965// We render 12 or 24 hour time.
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700966void BootAnimation::drawClock(const Font& font, const int xPos, const int yPos) {
Damien Bargiacchi9071db12016-10-28 17:38:22 -0700967 static constexpr char TIME_FORMAT_12[] = "%l:%M";
968 static constexpr char TIME_FORMAT_24[] = "%H:%M";
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700969 static constexpr int TIME_LENGTH = 6;
970
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800971 time_t rawtime;
972 time(&rawtime);
973 struct tm* timeInfo = localtime(&rawtime);
974
975 char timeBuff[TIME_LENGTH];
Damien Bargiacchi9071db12016-10-28 17:38:22 -0700976 const char* timeFormat = mTimeFormat12Hour ? TIME_FORMAT_12 : TIME_FORMAT_24;
977 size_t length = strftime(timeBuff, TIME_LENGTH, timeFormat, timeInfo);
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800978
979 if (length != TIME_LENGTH - 1) {
Wei Wang159a4102018-10-23 23:15:58 -0700980 SLOGE("Couldn't format time; abandoning boot animation clock");
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800981 mClockEnabled = false;
982 return;
983 }
984
Damien Bargiacchi45a76442016-12-05 18:02:18 -0800985 char* out = timeBuff[0] == ' ' ? &timeBuff[1] : &timeBuff[0];
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700986 int x = xPos;
987 int y = yPos;
Damien Bargiacchi45a76442016-12-05 18:02:18 -0800988 drawText(out, font, false, &x, &y);
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800989}
990
Nicolas Geoffray41fffb42020-12-15 10:50:29 +0000991void BootAnimation::drawProgress(int percent, const Font& font, const int xPos, const int yPos) {
992 static constexpr int PERCENT_LENGTH = 5;
993
994 char percentBuff[PERCENT_LENGTH];
995 // ';' has the ascii code just after ':', and the font resource contains '%'
996 // for that ascii code.
997 sprintf(percentBuff, "%d;", percent);
998 int x = xPos;
999 int y = yPos;
1000 drawText(percentBuff, font, false, &x, &y);
1001}
1002
Marin Shalamanov9070c052020-05-18 19:05:17 +02001003bool BootAnimation::parseAnimationDesc(Animation& animation) {
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -07001004 String8 desString;
1005
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001006 if (!readFile(animation.zip, "desc.txt", desString)) {
Narayan Kamathafd31e02013-12-03 13:16:03 +00001007 return false;
1008 }
Mathias Agopiana8826d62009-10-01 03:10:14 -07001009 char const* s = desString.string();
1010
Mathias Agopiana8826d62009-10-01 03:10:14 -07001011 // Parse the description file
1012 for (;;) {
1013 const char* endl = strstr(s, "\n");
Yi Kong911ac232019-03-24 01:49:02 -07001014 if (endl == nullptr) break;
Mathias Agopiana8826d62009-10-01 03:10:14 -07001015 String8 line(s, endl - s);
1016 const char* l = line.string();
Damien Bargiacchia704b7d2016-02-16 16:55:49 -08001017 int fps = 0;
1018 int width = 0;
1019 int height = 0;
1020 int count = 0;
1021 int pause = 0;
Nicolas Geoffray41fffb42020-12-15 10:50:29 +00001022 int progress = 0;
Yegor Malyshev6925e1b2020-09-10 16:42:20 +02001023 int framesToFadeCount = 0;
Narayan Kamathafd31e02013-12-03 13:16:03 +00001024 char path[ANIM_ENTRY_NAME_MAX];
Jesse Hall083b84c2014-09-22 10:51:09 -07001025 char color[7] = "000000"; // default to black if unspecified
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -07001026 char clockPos1[TEXT_POS_LEN_MAX + 1] = "";
1027 char clockPos2[TEXT_POS_LEN_MAX + 1] = "";
Kevin Hesterd3782b22012-04-26 10:38:55 -07001028 char pathType;
Yegor Malyshev6925e1b2020-09-10 16:42:20 +02001029
1030 int nextReadPos;
1031
Nicolas Geoffray41fffb42020-12-15 10:50:29 +00001032 int topLineNumbers = sscanf(l, "%d %d %d %d", &width, &height, &fps, &progress);
1033 if (topLineNumbers == 3 || topLineNumbers == 4) {
1034 // SLOGD("> w=%d, h=%d, fps=%d, progress=%d", width, height, fps, progress);
Mathias Agopiana8826d62009-10-01 03:10:14 -07001035 animation.width = width;
1036 animation.height = height;
1037 animation.fps = fps;
Nicolas Geoffray41fffb42020-12-15 10:50:29 +00001038 if (topLineNumbers == 4) {
1039 animation.progressEnabled = (progress != 0);
1040 } else {
1041 animation.progressEnabled = false;
1042 }
Yegor Malyshev6925e1b2020-09-10 16:42:20 +02001043 } else if (sscanf(l, "%c %d %d %" STRTO(ANIM_PATH_MAX) "s%n",
1044 &pathType, &count, &pause, path, &nextReadPos) >= 4) {
1045 if (pathType == 'f') {
1046 sscanf(l + nextReadPos, " %d #%6s %16s %16s", &framesToFadeCount, color, clockPos1,
1047 clockPos2);
1048 } else {
1049 sscanf(l + nextReadPos, " #%6s %16s %16s", color, clockPos1, clockPos2);
1050 }
1051 // SLOGD("> type=%c, count=%d, pause=%d, path=%s, framesToFadeCount=%d, color=%s, "
1052 // "clockPos1=%s, clockPos2=%s",
1053 // pathType, count, pause, path, framesToFadeCount, color, clockPos1, clockPos2);
Mathias Agopiana8826d62009-10-01 03:10:14 -07001054 Animation::Part part;
Kevin Hesterd3782b22012-04-26 10:38:55 -07001055 part.playUntilComplete = pathType == 'c';
Yegor Malyshev6925e1b2020-09-10 16:42:20 +02001056 part.framesToFadeCount = framesToFadeCount;
Mathias Agopiana8826d62009-10-01 03:10:14 -07001057 part.count = count;
1058 part.pause = pause;
1059 part.path = path;
Yi Kong911ac232019-03-24 01:49:02 -07001060 part.audioData = nullptr;
1061 part.animation = nullptr;
Jesse Hall083b84c2014-09-22 10:51:09 -07001062 if (!parseColor(color, part.backgroundColor)) {
Wei Wang159a4102018-10-23 23:15:58 -07001063 SLOGE("> invalid color '#%s'", color);
Jesse Hall083b84c2014-09-22 10:51:09 -07001064 part.backgroundColor[0] = 0.0f;
1065 part.backgroundColor[1] = 0.0f;
1066 part.backgroundColor[2] = 0.0f;
1067 }
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -07001068 parsePosition(clockPos1, clockPos2, &part.clockPosX, &part.clockPosY);
Mathias Agopiana8826d62009-10-01 03:10:14 -07001069 animation.parts.add(part);
1070 }
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001071 else if (strcmp(l, "$SYSTEM") == 0) {
Wei Wang159a4102018-10-23 23:15:58 -07001072 // SLOGD("> SYSTEM");
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001073 Animation::Part part;
1074 part.playUntilComplete = false;
Yegor Malyshev6925e1b2020-09-10 16:42:20 +02001075 part.framesToFadeCount = 0;
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001076 part.count = 1;
1077 part.pause = 0;
Yi Kong911ac232019-03-24 01:49:02 -07001078 part.audioData = nullptr;
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001079 part.animation = loadAnimation(String8(SYSTEM_BOOTANIMATION_FILE));
Yi Kong911ac232019-03-24 01:49:02 -07001080 if (part.animation != nullptr)
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001081 animation.parts.add(part);
1082 }
Mathias Agopiana8826d62009-10-01 03:10:14 -07001083 s = ++endl;
1084 }
1085
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001086 return true;
1087}
1088
Marin Shalamanov9070c052020-05-18 19:05:17 +02001089bool BootAnimation::preloadZip(Animation& animation) {
Mathias Agopiana8826d62009-10-01 03:10:14 -07001090 // read all the data structures
1091 const size_t pcount = animation.parts.size();
Yi Kong911ac232019-03-24 01:49:02 -07001092 void *cookie = nullptr;
Geoffrey Pitschdd214a72016-06-27 17:14:30 -04001093 ZipFileRO* zip = animation.zip;
1094 if (!zip->startIteration(&cookie)) {
Narayan Kamathafd31e02013-12-03 13:16:03 +00001095 return false;
1096 }
1097
1098 ZipEntryRO entry;
1099 char name[ANIM_ENTRY_NAME_MAX];
Yi Kong911ac232019-03-24 01:49:02 -07001100 while ((entry = zip->nextEntry(cookie)) != nullptr) {
Geoffrey Pitschdd214a72016-06-27 17:14:30 -04001101 const int foundEntryName = zip->getEntryFileName(entry, name, ANIM_ENTRY_NAME_MAX);
Narayan Kamathafd31e02013-12-03 13:16:03 +00001102 if (foundEntryName > ANIM_ENTRY_NAME_MAX || foundEntryName == -1) {
Wei Wang159a4102018-10-23 23:15:58 -07001103 SLOGE("Error fetching entry file name");
Narayan Kamathafd31e02013-12-03 13:16:03 +00001104 continue;
1105 }
1106
1107 const String8 entryName(name);
1108 const String8 path(entryName.getPathDir());
1109 const String8 leaf(entryName.getPathLeaf());
1110 if (leaf.size() > 0) {
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -07001111 if (entryName == CLOCK_FONT_ZIP_NAME) {
1112 FileMap* map = zip->createEntryFileMap(entry);
1113 if (map) {
1114 animation.clockFont.map = map;
1115 }
1116 continue;
1117 }
1118
Nicolas Geoffray41fffb42020-12-15 10:50:29 +00001119 if (entryName == PROGRESS_FONT_ZIP_NAME) {
1120 FileMap* map = zip->createEntryFileMap(entry);
1121 if (map) {
1122 animation.progressFont.map = map;
1123 }
1124 continue;
1125 }
1126
Geoffrey Pitschdd214a72016-06-27 17:14:30 -04001127 for (size_t j = 0; j < pcount; j++) {
Narayan Kamathafd31e02013-12-03 13:16:03 +00001128 if (path == animation.parts[j].path) {
Narayan Kamath4600dd02015-06-16 12:02:57 +01001129 uint16_t method;
Narayan Kamathafd31e02013-12-03 13:16:03 +00001130 // supports only stored png files
Yi Kong911ac232019-03-24 01:49:02 -07001131 if (zip->getEntryInfo(entry, &method, nullptr, nullptr, nullptr, nullptr, nullptr)) {
Narayan Kamathafd31e02013-12-03 13:16:03 +00001132 if (method == ZipFileRO::kCompressStored) {
Geoffrey Pitschdd214a72016-06-27 17:14:30 -04001133 FileMap* map = zip->createEntryFileMap(entry);
Narayan Kamathafd31e02013-12-03 13:16:03 +00001134 if (map) {
Narayan Kamathafd31e02013-12-03 13:16:03 +00001135 Animation::Part& part(animation.parts.editItemAt(j));
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -07001136 if (leaf == "audio.wav") {
1137 // a part may have at most one audio file
Geoffrey Pitschd6d9a1d2016-06-08 00:38:58 -07001138 part.audioData = (uint8_t *)map->getDataPtr();
1139 part.audioLength = map->getDataLength();
Geoffrey Pitschdd214a72016-06-27 17:14:30 -04001140 } else if (leaf == "trim.txt") {
1141 part.trimData.setTo((char const*)map->getDataPtr(),
1142 map->getDataLength());
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -07001143 } else {
1144 Animation::Frame frame;
1145 frame.name = leaf;
1146 frame.map = map;
Geoffrey Pitschdd214a72016-06-27 17:14:30 -04001147 frame.trimWidth = animation.width;
1148 frame.trimHeight = animation.height;
1149 frame.trimX = 0;
1150 frame.trimY = 0;
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -07001151 part.frames.add(frame);
1152 }
Mathias Agopiana8826d62009-10-01 03:10:14 -07001153 }
Geoffrey Pitschd6d9a1d2016-06-08 00:38:58 -07001154 } else {
Wei Wang159a4102018-10-23 23:15:58 -07001155 SLOGE("bootanimation.zip is compressed; must be only stored");
Mathias Agopiana8826d62009-10-01 03:10:14 -07001156 }
1157 }
1158 }
1159 }
1160 }
1161 }
1162
Geoffrey Pitschdd214a72016-06-27 17:14:30 -04001163 // If there is trimData present, override the positioning defaults.
1164 for (Animation::Part& part : animation.parts) {
1165 const char* trimDataStr = part.trimData.string();
1166 for (size_t frameIdx = 0; frameIdx < part.frames.size(); frameIdx++) {
1167 const char* endl = strstr(trimDataStr, "\n");
1168 // No more trimData for this part.
Yi Kong911ac232019-03-24 01:49:02 -07001169 if (endl == nullptr) {
Geoffrey Pitschdd214a72016-06-27 17:14:30 -04001170 break;
1171 }
1172 String8 line(trimDataStr, endl - trimDataStr);
1173 const char* lineStr = line.string();
1174 trimDataStr = ++endl;
1175 int width = 0, height = 0, x = 0, y = 0;
1176 if (sscanf(lineStr, "%dx%d+%d+%d", &width, &height, &x, &y) == 4) {
1177 Animation::Frame& frame(part.frames.editItemAt(frameIdx));
1178 frame.trimWidth = width;
1179 frame.trimHeight = height;
1180 frame.trimX = x;
1181 frame.trimY = y;
1182 } else {
Wei Wang159a4102018-10-23 23:15:58 -07001183 SLOGE("Error parsing trim.txt, line: %s", lineStr);
Geoffrey Pitschdd214a72016-06-27 17:14:30 -04001184 break;
1185 }
1186 }
1187 }
1188
1189 zip->endIteration(cookie);
Narayan Kamathafd31e02013-12-03 13:16:03 +00001190
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001191 return true;
1192}
1193
Marin Shalamanov9070c052020-05-18 19:05:17 +02001194bool BootAnimation::movie() {
Huihong Luo50a2f362018-08-11 09:27:57 -07001195 if (mAnimation == nullptr) {
1196 mAnimation = loadAnimation(mZipFileName);
1197 }
1198
1199 if (mAnimation == nullptr)
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001200 return false;
1201
Huihong Luo50a2f362018-08-11 09:27:57 -07001202 // mCallbacks->init() may get called recursively,
1203 // this loop is needed to get the same results
1204 for (const Animation::Part& part : mAnimation->parts) {
1205 if (part.animation != nullptr) {
1206 mCallbacks->init(part.animation->parts);
1207 }
1208 }
1209 mCallbacks->init(mAnimation->parts);
1210
Damien Bargiacchi97480862016-03-29 14:55:55 -07001211 bool anyPartHasClock = false;
Huihong Luo50a2f362018-08-11 09:27:57 -07001212 for (size_t i=0; i < mAnimation->parts.size(); i++) {
1213 if(validClock(mAnimation->parts[i])) {
Damien Bargiacchi97480862016-03-29 14:55:55 -07001214 anyPartHasClock = true;
1215 break;
1216 }
1217 }
1218 if (!anyPartHasClock) {
1219 mClockEnabled = false;
1220 }
1221
Sai Kiran Korwar27167492015-07-07 20:00:06 +05301222 // Check if npot textures are supported
1223 mUseNpotTextures = false;
1224 String8 gl_extensions;
1225 const char* exts = reinterpret_cast<const char*>(glGetString(GL_EXTENSIONS));
1226 if (!exts) {
1227 glGetError();
1228 } else {
1229 gl_extensions.setTo(exts);
1230 if ((gl_extensions.find("GL_ARB_texture_non_power_of_two") != -1) ||
1231 (gl_extensions.find("GL_OES_texture_npot") != -1)) {
1232 mUseNpotTextures = true;
1233 }
1234 }
1235
Damien Bargiacchia704b7d2016-02-16 16:55:49 -08001236 // Blend required to draw time on top of animation frames.
1237 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
Mathias Agopiana8826d62009-10-01 03:10:14 -07001238 glDisable(GL_DITHER);
1239 glDisable(GL_SCISSOR_TEST);
1240 glDisable(GL_BLEND);
Mathias Agopiana8826d62009-10-01 03:10:14 -07001241
Mathias Agopiana8826d62009-10-01 03:10:14 -07001242 glEnable(GL_TEXTURE_2D);
Shan Huangd711ac42021-07-13 18:36:57 +08001243 glBindTexture(GL_TEXTURE_2D, 0);
1244 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
1245 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
1246 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
1247 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -07001248 bool clockFontInitialized = false;
Damien Bargiacchia704b7d2016-02-16 16:55:49 -08001249 if (mClockEnabled) {
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -07001250 clockFontInitialized =
Huihong Luo50a2f362018-08-11 09:27:57 -07001251 (initFont(&mAnimation->clockFont, CLOCK_FONT_ASSET) == NO_ERROR);
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -07001252 mClockEnabled = clockFontInitialized;
Damien Bargiacchia704b7d2016-02-16 16:55:49 -08001253 }
1254
Nicolas Geoffray41fffb42020-12-15 10:50:29 +00001255 initFont(&mAnimation->progressFont, PROGRESS_FONT_ASSET);
1256
Damien Bargiacchi97480862016-03-29 14:55:55 -07001257 if (mClockEnabled && !updateIsTimeAccurate()) {
1258 mTimeCheckThread = new TimeCheckThread(this);
1259 mTimeCheckThread->run("BootAnimation::TimeCheckThread", PRIORITY_NORMAL);
1260 }
1261
Huihong Luo50a2f362018-08-11 09:27:57 -07001262 playAnimation(*mAnimation);
Damien Bargiacchi97480862016-03-29 14:55:55 -07001263
Geoffrey Pitscha9173532016-07-19 14:56:39 -04001264 if (mTimeCheckThread != nullptr) {
Damien Bargiacchi97480862016-03-29 14:55:55 -07001265 mTimeCheckThread->requestExit();
Geoffrey Pitscha9173532016-07-19 14:56:39 -04001266 mTimeCheckThread = nullptr;
1267 }
1268
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -07001269 if (clockFontInitialized) {
Huihong Luo50a2f362018-08-11 09:27:57 -07001270 glDeleteTextures(1, &mAnimation->clockFont.texture.name);
Andriy Naborskyy815e51d2016-03-24 16:43:34 -07001271 }
1272
Huihong Luo50a2f362018-08-11 09:27:57 -07001273 releaseAnimation(mAnimation);
1274 mAnimation = nullptr;
Damien Bargiacchi1a8a2132018-07-24 15:20:26 -07001275
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001276 return false;
1277}
1278
Nicolas Geoffraya1298752021-04-20 15:15:06 +01001279bool BootAnimation::shouldStopPlayingPart(const Animation::Part& part,
1280 const int fadedFramesCount,
1281 const int lastDisplayedProgress) {
Yegor Malyshev6925e1b2020-09-10 16:42:20 +02001282 // 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 +01001283 return exitPending() && !part.playUntilComplete && fadedFramesCount >= part.framesToFadeCount &&
1284 (lastDisplayedProgress == 0 || lastDisplayedProgress == 100);
Yegor Malyshev6925e1b2020-09-10 16:42:20 +02001285}
1286
Shan Huangd711ac42021-07-13 18:36:57 +08001287// Linear mapping from range <a1, a2> to range <b1, b2>
1288float mapLinear(float x, float a1, float a2, float b1, float b2) {
1289 return b1 + ( x - a1 ) * ( b2 - b1 ) / ( a2 - a1 );
1290}
1291
1292void BootAnimation::drawTexturedQuad(float xStart, float yStart, float width, float height) {
1293 // Map coordinates from screen space to world space.
1294 float x0 = mapLinear(xStart, 0, mWidth, -1, 1);
1295 float y0 = mapLinear(yStart, 0, mHeight, -1, 1);
1296 float x1 = mapLinear(xStart + width, 0, mWidth, -1, 1);
1297 float y1 = mapLinear(yStart + height, 0, mHeight, -1, 1);
1298 // Update quad vertex positions.
1299 quadPositions[0] = x0;
1300 quadPositions[1] = y0;
1301 quadPositions[2] = x1;
1302 quadPositions[3] = y0;
1303 quadPositions[4] = x1;
1304 quadPositions[5] = y1;
1305 quadPositions[6] = x1;
1306 quadPositions[7] = y1;
1307 quadPositions[8] = x0;
1308 quadPositions[9] = y1;
1309 quadPositions[10] = x0;
1310 quadPositions[11] = y0;
1311 glDrawArrays(GL_TRIANGLES, 0,
1312 sizeof(quadPositions) / sizeof(quadPositions[0]) / 2);
1313}
1314
Marin Shalamanov9070c052020-05-18 19:05:17 +02001315bool BootAnimation::playAnimation(const Animation& animation) {
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001316 const size_t pcount = animation.parts.size();
Mathias Agopiana8826d62009-10-01 03:10:14 -07001317 nsecs_t frameDuration = s2ns(1) / animation.fps;
Mathias Agopian9f3020d2009-11-06 16:30:18 -08001318
Wei Wang159a4102018-10-23 23:15:58 -07001319 SLOGD("%sAnimationShownTiming start time: %" PRId64 "ms", mShuttingDown ? "Shutdown" : "Boot",
Keun-young Parkd1794cd2017-04-04 12:21:19 -07001320 elapsedRealtime());
Yegor Malyshev6925e1b2020-09-10 16:42:20 +02001321
1322 int fadedFramesCount = 0;
Nicolas Geoffray41fffb42020-12-15 10:50:29 +00001323 int lastDisplayedProgress = 0;
Narayan Kamathafd31e02013-12-03 13:16:03 +00001324 for (size_t i=0 ; i<pcount ; i++) {
Mathias Agopiana8826d62009-10-01 03:10:14 -07001325 const Animation::Part& part(animation.parts[i]);
1326 const size_t fcount = part.frames.size();
Mathias Agopiana8826d62009-10-01 03:10:14 -07001327
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001328 // Handle animation package
Yi Kong911ac232019-03-24 01:49:02 -07001329 if (part.animation != nullptr) {
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001330 playAnimation(*part.animation);
1331 if (exitPending())
1332 break;
1333 continue; //to next part
1334 }
1335
Yegor Malyshev6925e1b2020-09-10 16:42:20 +02001336 // process the part not only while the count allows but also if already fading
1337 for (int r=0 ; !part.count || r<part.count || fadedFramesCount > 0 ; r++) {
Nicolas Geoffraya1298752021-04-20 15:15:06 +01001338 if (shouldStopPlayingPart(part, fadedFramesCount, lastDisplayedProgress)) break;
Kevin Hesterd3782b22012-04-26 10:38:55 -07001339
Ed Coyne7464ac92017-06-08 12:26:48 -07001340 mCallbacks->playPart(i, part, r);
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -07001341
Jesse Hall083b84c2014-09-22 10:51:09 -07001342 glClearColor(
1343 part.backgroundColor[0],
1344 part.backgroundColor[1],
1345 part.backgroundColor[2],
1346 1.0f);
1347
Nicolas Geoffray41fffb42020-12-15 10:50:29 +00001348 // For the last animation, if we have progress indicator from
1349 // the system, display it.
1350 int currentProgress = android::base::GetIntProperty(PROGRESS_PROP_NAME, 0);
1351 bool displayProgress = animation.progressEnabled &&
1352 (i == (pcount -1)) && currentProgress != 0;
1353
Yegor Malyshev6925e1b2020-09-10 16:42:20 +02001354 for (size_t j=0 ; j<fcount ; j++) {
Nicolas Geoffraya1298752021-04-20 15:15:06 +01001355 if (shouldStopPlayingPart(part, fadedFramesCount, lastDisplayedProgress)) break;
Yegor Malyshev6925e1b2020-09-10 16:42:20 +02001356
Marin Shalamanov9070c052020-05-18 19:05:17 +02001357 processDisplayEvents();
1358
1359 const int animationX = (mWidth - animation.width) / 2;
1360 const int animationY = (mHeight - animation.height) / 2;
1361
Mathias Agopiana8826d62009-10-01 03:10:14 -07001362 const Animation::Frame& frame(part.frames[j]);
Mathias Agopiandb7dd2a2012-05-12 15:08:21 -07001363 nsecs_t lastFrame = systemTime();
Mathias Agopiana8826d62009-10-01 03:10:14 -07001364
1365 if (r > 0) {
1366 glBindTexture(GL_TEXTURE_2D, frame.tid);
1367 } else {
Shan Huangd711ac42021-07-13 18:36:57 +08001368 glGenTextures(1, &frame.tid);
1369 glBindTexture(GL_TEXTURE_2D, frame.tid);
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -07001370 int w, h;
1371 initTexture(frame.map, &w, &h);
Mathias Agopiana8826d62009-10-01 03:10:14 -07001372 }
1373
Geoffrey Pitschdd214a72016-06-27 17:14:30 -04001374 const int xc = animationX + frame.trimX;
1375 const int yc = animationY + frame.trimY;
1376 Region clearReg(Rect(mWidth, mHeight));
1377 clearReg.subtractSelf(Rect(xc, yc, xc+frame.trimWidth, yc+frame.trimHeight));
Mathias Agopian9f3020d2009-11-06 16:30:18 -08001378 if (!clearReg.isEmpty()) {
1379 Region::const_iterator head(clearReg.begin());
1380 Region::const_iterator tail(clearReg.end());
1381 glEnable(GL_SCISSOR_TEST);
1382 while (head != tail) {
Andreas Gampecfedceb2014-09-30 21:48:18 -07001383 const Rect& r2(*head++);
Geoffrey Pitschdd214a72016-06-27 17:14:30 -04001384 glScissor(r2.left, mHeight - r2.bottom, r2.width(), r2.height());
Mathias Agopian9f3020d2009-11-06 16:30:18 -08001385 glClear(GL_COLOR_BUFFER_BIT);
1386 }
1387 glDisable(GL_SCISSOR_TEST);
1388 }
Geoffrey Pitschdd214a72016-06-27 17:14:30 -04001389 // specify the y center as ceiling((mHeight - frame.trimHeight) / 2)
1390 // which is equivalent to mHeight - (yc + frame.trimHeight)
Yegor Malyshev6925e1b2020-09-10 16:42:20 +02001391 const int frameDrawY = mHeight - (yc + frame.trimHeight);
Yegor Malyshev6925e1b2020-09-10 16:42:20 +02001392
Shan Huangd711ac42021-07-13 18:36:57 +08001393 float fade = 0;
Yegor Malyshev6925e1b2020-09-10 16:42:20 +02001394 // if the part hasn't been stopped yet then continue fading if necessary
1395 if (exitPending() && part.hasFadingPhase()) {
Shan Huangd711ac42021-07-13 18:36:57 +08001396 fade = static_cast<float>(++fadedFramesCount) / part.framesToFadeCount;
Yegor Malyshev6925e1b2020-09-10 16:42:20 +02001397 if (fadedFramesCount >= part.framesToFadeCount) {
1398 fadedFramesCount = MAX_FADED_FRAMES_COUNT; // no more fading
1399 }
1400 }
Shan Huangd711ac42021-07-13 18:36:57 +08001401 glUseProgram(mImageShader);
1402 glUniform1i(mImageTextureLocation, 0);
1403 glUniform1f(mImageFadeLocation, fade);
1404 glEnable(GL_BLEND);
1405 drawTexturedQuad(xc, frameDrawY, frame.trimWidth, frame.trimHeight);
1406 glDisable(GL_BLEND);
Yegor Malyshev6925e1b2020-09-10 16:42:20 +02001407
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -07001408 if (mClockEnabled && mTimeIsAccurate && validClock(part)) {
1409 drawClock(animation.clockFont, part.clockPosX, part.clockPosY);
Damien Bargiacchia704b7d2016-02-16 16:55:49 -08001410 }
Yegor Malyshev6925e1b2020-09-10 16:42:20 +02001411
Nicolas Geoffray41fffb42020-12-15 10:50:29 +00001412 if (displayProgress) {
1413 int newProgress = android::base::GetIntProperty(PROGRESS_PROP_NAME, 0);
1414 // In case the new progress jumped suddenly, still show an
1415 // increment of 1.
1416 if (lastDisplayedProgress != 100) {
1417 // Artificially sleep 1/10th a second to slow down the animation.
1418 usleep(100000);
1419 if (lastDisplayedProgress < newProgress) {
1420 lastDisplayedProgress++;
1421 }
1422 }
1423 // Put the progress percentage right below the animation.
1424 int posY = animation.height / 3;
1425 int posX = TEXT_CENTER_VALUE;
1426 drawProgress(lastDisplayedProgress, animation.progressFont, posX, posY);
1427 }
1428
Adrian Roos9ee5dff2018-08-22 20:19:49 +02001429 handleViewport(frameDuration);
Damien Bargiacchia704b7d2016-02-16 16:55:49 -08001430
Mathias Agopiana8826d62009-10-01 03:10:14 -07001431 eglSwapBuffers(mDisplay, mSurface);
1432
1433 nsecs_t now = systemTime();
1434 nsecs_t delay = frameDuration - (now - lastFrame);
Wei Wang159a4102018-10-23 23:15:58 -07001435 //SLOGD("%lld, %lld", ns2ms(now - lastFrame), ns2ms(delay));
Mathias Agopiana8826d62009-10-01 03:10:14 -07001436 lastFrame = now;
Mathias Agopiandb7dd2a2012-05-12 15:08:21 -07001437
1438 if (delay > 0) {
1439 struct timespec spec;
1440 spec.tv_sec = (now + delay) / 1000000000;
1441 spec.tv_nsec = (now + delay) % 1000000000;
1442 int err;
1443 do {
Yi Kong911ac232019-03-24 01:49:02 -07001444 err = clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &spec, nullptr);
Mathias Agopiandb7dd2a2012-05-12 15:08:21 -07001445 } while (err<0 && errno == EINTR);
1446 }
Kevin Hesterd3782b22012-04-26 10:38:55 -07001447
1448 checkExit();
Mathias Agopiana8826d62009-10-01 03:10:14 -07001449 }
Kevin Hesterd3782b22012-04-26 10:38:55 -07001450
Mathias Agopiana8826d62009-10-01 03:10:14 -07001451 usleep(part.pause * ns2us(frameDuration));
Kevin Hesterd3782b22012-04-26 10:38:55 -07001452
Yegor Malyshev6925e1b2020-09-10 16:42:20 +02001453 if (exitPending() && !part.count && mCurrentInset >= mTargetInset &&
1454 !part.hasFadingPhase()) {
Nicolas Geoffraya1298752021-04-20 15:15:06 +01001455 if (lastDisplayedProgress != 0 && lastDisplayedProgress != 100) {
1456 android::base::SetProperty(PROGRESS_PROP_NAME, "100");
1457 continue;
1458 }
Yegor Malyshev6925e1b2020-09-10 16:42:20 +02001459 break; // exit the infinite non-fading part when it has been played at least once
1460 }
Mathias Agopiana8826d62009-10-01 03:10:14 -07001461 }
Geoffrey Pitsch2fb30fb2016-07-06 16:16:20 -04001462 }
1463
1464 // Free textures created for looping parts now that the animation is done.
1465 for (const Animation::Part& part : animation.parts) {
Mathias Agopiana8826d62009-10-01 03:10:14 -07001466 if (part.count != 1) {
Geoffrey Pitsch2fb30fb2016-07-06 16:16:20 -04001467 const size_t fcount = part.frames.size();
1468 for (size_t j = 0; j < fcount; j++) {
Mathias Agopiana8826d62009-10-01 03:10:14 -07001469 const Animation::Frame& frame(part.frames[j]);
1470 glDeleteTextures(1, &frame.tid);
1471 }
1472 }
1473 }
Geoffrey Pitschd6d9a1d2016-06-08 00:38:58 -07001474
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001475 return true;
Mathias Agopiana8826d62009-10-01 03:10:14 -07001476}
1477
Marin Shalamanov9070c052020-05-18 19:05:17 +02001478void BootAnimation::processDisplayEvents() {
1479 // This will poll mDisplayEventReceiver and if there are new events it'll call
1480 // displayEventCallback synchronously.
1481 mLooper->pollOnce(0);
1482}
1483
Adrian Roos9ee5dff2018-08-22 20:19:49 +02001484void BootAnimation::handleViewport(nsecs_t timestep) {
1485 if (mShuttingDown || !mFlingerSurfaceControl || mTargetInset == 0) {
1486 return;
1487 }
1488 if (mTargetInset < 0) {
1489 // Poll the amount for the top display inset. This will return -1 until persistent properties
1490 // have been loaded.
1491 mTargetInset = android::base::GetIntProperty("persist.sys.displayinset.top",
1492 -1 /* default */, -1 /* min */, mHeight / 2 /* max */);
1493 }
1494 if (mTargetInset <= 0) {
1495 return;
1496 }
1497
1498 if (mCurrentInset < mTargetInset) {
1499 // After the device boots, the inset will effectively be cropped away. We animate this here.
1500 float fraction = static_cast<float>(mCurrentInset) / mTargetInset;
1501 int interpolatedInset = (cosf((fraction + 1) * M_PI) / 2.0f + 0.5f) * mTargetInset;
1502
1503 SurfaceComposerClient::Transaction()
1504 .setCrop(mFlingerSurfaceControl, Rect(0, interpolatedInset, mWidth, mHeight))
1505 .apply();
1506 } else {
1507 // At the end of the animation, we switch to the viewport that DisplayManager will apply
1508 // later. This changes the coordinate system, and means we must move the surface up by
1509 // the inset amount.
Adrian Roos9ee5dff2018-08-22 20:19:49 +02001510 Rect layerStackRect(0, 0, mWidth, mHeight - mTargetInset);
1511 Rect displayRect(0, mTargetInset, mWidth, mHeight);
1512
1513 SurfaceComposerClient::Transaction t;
1514 t.setPosition(mFlingerSurfaceControl, 0, -mTargetInset)
1515 .setCrop(mFlingerSurfaceControl, Rect(0, mTargetInset, mWidth, mHeight));
Dominik Laskowski2a3d9aa2019-11-18 20:26:39 -08001516 t.setDisplayProjection(mDisplayToken, ui::ROTATION_0, layerStackRect, displayRect);
Adrian Roos9ee5dff2018-08-22 20:19:49 +02001517 t.apply();
1518
1519 mTargetInset = mCurrentInset = 0;
1520 }
1521
1522 int delta = timestep * mTargetInset / ms2ns(200);
1523 mCurrentInset += delta;
1524}
1525
Marin Shalamanov9070c052020-05-18 19:05:17 +02001526void BootAnimation::releaseAnimation(Animation* animation) const {
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001527 for (Vector<Animation::Part>::iterator it = animation->parts.begin(),
1528 e = animation->parts.end(); it != e; ++it) {
1529 if (it->animation)
1530 releaseAnimation(it->animation);
1531 }
1532 if (animation->zip)
1533 delete animation->zip;
1534 delete animation;
1535}
1536
Marin Shalamanov9070c052020-05-18 19:05:17 +02001537BootAnimation::Animation* BootAnimation::loadAnimation(const String8& fn) {
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001538 if (mLoadedFiles.indexOf(fn) >= 0) {
Wei Wang159a4102018-10-23 23:15:58 -07001539 SLOGE("File \"%s\" is already loaded. Cyclic ref is not allowed",
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001540 fn.string());
Yi Kong911ac232019-03-24 01:49:02 -07001541 return nullptr;
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001542 }
1543 ZipFileRO *zip = ZipFileRO::open(fn);
Yi Kong911ac232019-03-24 01:49:02 -07001544 if (zip == nullptr) {
Wei Wang159a4102018-10-23 23:15:58 -07001545 SLOGE("Failed to open animation zip \"%s\": %s",
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001546 fn.string(), strerror(errno));
Yi Kong911ac232019-03-24 01:49:02 -07001547 return nullptr;
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001548 }
1549
1550 Animation *animation = new Animation;
1551 animation->fileName = fn;
1552 animation->zip = zip;
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -07001553 animation->clockFont.map = nullptr;
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001554 mLoadedFiles.add(animation->fileName);
1555
1556 parseAnimationDesc(*animation);
Geoffrey Pitscha91a2d72016-07-12 14:46:19 -04001557 if (!preloadZip(*animation)) {
Greg Kaiser7426ec82019-09-11 14:34:27 -07001558 releaseAnimation(animation);
Yi Kong911ac232019-03-24 01:49:02 -07001559 return nullptr;
Geoffrey Pitscha91a2d72016-07-12 14:46:19 -04001560 }
1561
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001562 mLoadedFiles.remove(fn);
1563 return animation;
1564}
Damien Bargiacchi97480862016-03-29 14:55:55 -07001565
1566bool BootAnimation::updateIsTimeAccurate() {
1567 static constexpr long long MAX_TIME_IN_PAST = 60000LL * 60LL * 24LL * 30LL; // 30 days
1568 static constexpr long long MAX_TIME_IN_FUTURE = 60000LL * 90LL; // 90 minutes
1569
1570 if (mTimeIsAccurate) {
1571 return true;
1572 }
Keun-young Parkb5938422017-03-23 13:46:24 -07001573 if (mShuttingDown) return true;
Damien Bargiacchi97480862016-03-29 14:55:55 -07001574 struct stat statResult;
Damien Bargiacchi9071db12016-10-28 17:38:22 -07001575
1576 if(stat(TIME_FORMAT_12_HOUR_FLAG_FILE_PATH, &statResult) == 0) {
1577 mTimeFormat12Hour = true;
1578 }
1579
Damien Bargiacchi97480862016-03-29 14:55:55 -07001580 if(stat(ACCURATE_TIME_FLAG_FILE_PATH, &statResult) == 0) {
1581 mTimeIsAccurate = true;
1582 return true;
1583 }
1584
1585 FILE* file = fopen(LAST_TIME_CHANGED_FILE_PATH, "r");
Yi Kong911ac232019-03-24 01:49:02 -07001586 if (file != nullptr) {
Damien Bargiacchi97480862016-03-29 14:55:55 -07001587 long long lastChangedTime = 0;
1588 fscanf(file, "%lld", &lastChangedTime);
1589 fclose(file);
1590 if (lastChangedTime > 0) {
1591 struct timespec now;
1592 clock_gettime(CLOCK_REALTIME, &now);
1593 // Match the Java timestamp format
1594 long long rtcNow = (now.tv_sec * 1000LL) + (now.tv_nsec / 1000000LL);
Damien Bargiacchi96762812016-07-12 15:53:40 -07001595 if (ACCURATE_TIME_EPOCH < rtcNow
1596 && lastChangedTime > (rtcNow - MAX_TIME_IN_PAST)
1597 && lastChangedTime < (rtcNow + MAX_TIME_IN_FUTURE)) {
Damien Bargiacchi97480862016-03-29 14:55:55 -07001598 mTimeIsAccurate = true;
1599 }
1600 }
1601 }
1602
1603 return mTimeIsAccurate;
1604}
1605
1606BootAnimation::TimeCheckThread::TimeCheckThread(BootAnimation* bootAnimation) : Thread(false),
1607 mInotifyFd(-1), mSystemWd(-1), mTimeWd(-1), mBootAnimation(bootAnimation) {}
1608
1609BootAnimation::TimeCheckThread::~TimeCheckThread() {
1610 // mInotifyFd may be -1 but that's ok since we're not at risk of attempting to close a valid FD.
1611 close(mInotifyFd);
1612}
1613
1614bool BootAnimation::TimeCheckThread::threadLoop() {
1615 bool shouldLoop = doThreadLoop() && !mBootAnimation->mTimeIsAccurate
1616 && mBootAnimation->mClockEnabled;
1617 if (!shouldLoop) {
1618 close(mInotifyFd);
1619 mInotifyFd = -1;
1620 }
1621 return shouldLoop;
1622}
1623
1624bool BootAnimation::TimeCheckThread::doThreadLoop() {
1625 static constexpr int BUFF_LEN (10 * (sizeof(struct inotify_event) + NAME_MAX + 1));
1626
1627 // Poll instead of doing a blocking read so the Thread can exit if requested.
1628 struct pollfd pfd = { mInotifyFd, POLLIN, 0 };
1629 ssize_t pollResult = poll(&pfd, 1, 1000);
1630
1631 if (pollResult == 0) {
1632 return true;
1633 } else if (pollResult < 0) {
Wei Wang159a4102018-10-23 23:15:58 -07001634 SLOGE("Could not poll inotify events");
Damien Bargiacchi97480862016-03-29 14:55:55 -07001635 return false;
1636 }
1637
1638 char buff[BUFF_LEN] __attribute__ ((aligned(__alignof__(struct inotify_event))));;
1639 ssize_t length = read(mInotifyFd, buff, BUFF_LEN);
1640 if (length == 0) {
1641 return true;
1642 } else if (length < 0) {
Wei Wang159a4102018-10-23 23:15:58 -07001643 SLOGE("Could not read inotify events");
Damien Bargiacchi97480862016-03-29 14:55:55 -07001644 return false;
1645 }
1646
1647 const struct inotify_event *event;
1648 for (char* ptr = buff; ptr < buff + length; ptr += sizeof(struct inotify_event) + event->len) {
1649 event = (const struct inotify_event *) ptr;
1650 if (event->wd == mSystemWd && strcmp(SYSTEM_TIME_DIR_NAME, event->name) == 0) {
1651 addTimeDirWatch();
1652 } else if (event->wd == mTimeWd && (strcmp(LAST_TIME_CHANGED_FILE_NAME, event->name) == 0
1653 || strcmp(ACCURATE_TIME_FLAG_FILE_NAME, event->name) == 0)) {
1654 return !mBootAnimation->updateIsTimeAccurate();
1655 }
1656 }
1657
1658 return true;
1659}
1660
1661void BootAnimation::TimeCheckThread::addTimeDirWatch() {
1662 mTimeWd = inotify_add_watch(mInotifyFd, SYSTEM_TIME_DIR_PATH,
1663 IN_CLOSE_WRITE | IN_MOVED_TO | IN_ATTRIB);
1664 if (mTimeWd > 0) {
1665 // No need to watch for the time directory to be created if it already exists
1666 inotify_rm_watch(mInotifyFd, mSystemWd);
1667 mSystemWd = -1;
1668 }
1669}
1670
1671status_t BootAnimation::TimeCheckThread::readyToRun() {
1672 mInotifyFd = inotify_init();
1673 if (mInotifyFd < 0) {
Wei Wang159a4102018-10-23 23:15:58 -07001674 SLOGE("Could not initialize inotify fd");
Damien Bargiacchi97480862016-03-29 14:55:55 -07001675 return NO_INIT;
1676 }
1677
1678 mSystemWd = inotify_add_watch(mInotifyFd, SYSTEM_DATA_DIR_PATH, IN_CREATE | IN_ATTRIB);
1679 if (mSystemWd < 0) {
1680 close(mInotifyFd);
1681 mInotifyFd = -1;
Dan Sandler562d46d2020-05-11 17:09:22 -04001682 SLOGE("Could not add watch for %s: %s", SYSTEM_DATA_DIR_PATH, strerror(errno));
Damien Bargiacchi97480862016-03-29 14:55:55 -07001683 return NO_INIT;
1684 }
1685
1686 addTimeDirWatch();
1687
1688 if (mBootAnimation->updateIsTimeAccurate()) {
1689 close(mInotifyFd);
1690 mInotifyFd = -1;
1691 return ALREADY_EXISTS;
1692 }
1693
1694 return NO_ERROR;
1695}
1696
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001697// ---------------------------------------------------------------------------
1698
Marin Shalamanov9070c052020-05-18 19:05:17 +02001699} // namespace android