blob: 2c7ee212b7b5825e8af275df470346a7d93ddcd2 [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
Mathias Agopianb13b9bd2012-02-17 18:27:36 -080037#include <androidfw/AssetManager.h>
Mathias Agopianac31a3b2009-05-21 19:59:24 -070038#include <binder/IPCThreadState.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080039#include <utils/Errors.h>
40#include <utils/Log.h>
Wei Wanga90c54c2017-02-02 11:35:21 -080041#include <utils/SystemClock.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080042
Keun-young Parkb5938422017-03-23 13:46:24 -070043#include <android-base/properties.h>
44
Dominik Laskowski69b281d2019-11-22 14:13:12 -080045#include <ui/DisplayConfig.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080046#include <ui/PixelFormat.h>
47#include <ui/Rect.h>
48#include <ui/Region.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080049
Jeff Brown0b722fe2012-08-24 22:40:14 -070050#include <gui/ISurfaceComposer.h>
Marin Shalamanov1a6e36892020-05-18 19:05:17 +020051#include <gui/DisplayEventReceiver.h>
Mathias Agopian8335f1c2012-02-25 18:48:35 -080052#include <gui/Surface.h>
53#include <gui/SurfaceComposerClient.h>
Mathias Agopian000479f2010-02-09 17:46:37 -080054
Andreas Gampecfedceb2014-09-30 21:48:18 -070055// TODO: Fix Skia.
56#pragma GCC diagnostic push
57#pragma GCC diagnostic ignored "-Wunused-parameter"
Derek Sollenbergereece0dd2014-02-27 14:31:29 -050058#include <SkBitmap.h>
Matt Sarett8898c162016-03-24 16:11:01 -040059#include <SkImage.h>
Derek Sollenbergereece0dd2014-02-27 14:31:29 -050060#include <SkStream.h>
Andreas Gampecfedceb2014-09-30 21:48:18 -070061#pragma GCC diagnostic pop
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080062
63#include <GLES/gl.h>
64#include <GLES/glext.h>
65#include <EGL/eglext.h>
66
67#include "BootAnimation.h"
68
Kyeongkab.Nam35a8a112019-07-17 16:41:48 +090069#define ANIM_PATH_MAX 255
70#define STR(x) #x
71#define STRTO(x) STR(x)
72
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080073namespace android {
74
Damien Bargiacchi97480862016-03-29 14:55:55 -070075static const char OEM_BOOTANIMATION_FILE[] = "/oem/media/bootanimation.zip";
Beverly34ffe252019-05-17 11:03:54 -040076static const char PRODUCT_BOOTANIMATION_DARK_FILE[] = "/product/media/bootanimation-dark.zip";
Jaekyun Seokc521bb32018-01-30 11:52:47 +090077static const char PRODUCT_BOOTANIMATION_FILE[] = "/product/media/bootanimation.zip";
Damien Bargiacchi97480862016-03-29 14:55:55 -070078static const char SYSTEM_BOOTANIMATION_FILE[] = "/system/media/bootanimation.zip";
Anders Fridlunde0b4d232018-11-06 14:23:25 +010079static const char APEX_BOOTANIMATION_FILE[] = "/apex/com.android.bootanimation/etc/bootanimation.zip";
Jaekyun Seokc521bb32018-01-30 11:52:47 +090080static const char PRODUCT_ENCRYPTED_BOOTANIMATION_FILE[] = "/product/media/bootanimation-encrypted.zip";
Damien Bargiacchi97480862016-03-29 14:55:55 -070081static const char SYSTEM_ENCRYPTED_BOOTANIMATION_FILE[] = "/system/media/bootanimation-encrypted.zip";
Keun-young Parkb5938422017-03-23 13:46:24 -070082static const char OEM_SHUTDOWNANIMATION_FILE[] = "/oem/media/shutdownanimation.zip";
Jaekyun Seokc521bb32018-01-30 11:52:47 +090083static const char PRODUCT_SHUTDOWNANIMATION_FILE[] = "/product/media/shutdownanimation.zip";
Keun-young Parkb5938422017-03-23 13:46:24 -070084static const char SYSTEM_SHUTDOWNANIMATION_FILE[] = "/system/media/shutdownanimation.zip";
85
Nikita Ioffe06c986e2020-01-27 17:16:03 +000086static constexpr const char* PRODUCT_USERSPACE_REBOOT_ANIMATION_FILE = "/product/media/userspace-reboot.zip";
87static constexpr const char* OEM_USERSPACE_REBOOT_ANIMATION_FILE = "/oem/media/userspace-reboot.zip";
88static constexpr const char* SYSTEM_USERSPACE_REBOOT_ANIMATION_FILE = "/system/media/userspace-reboot.zip";
89
Damien Bargiacchi97480862016-03-29 14:55:55 -070090static const char SYSTEM_DATA_DIR_PATH[] = "/data/system";
91static const char SYSTEM_TIME_DIR_NAME[] = "time";
92static const char SYSTEM_TIME_DIR_PATH[] = "/data/system/time";
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -070093static const char CLOCK_FONT_ASSET[] = "images/clock_font.png";
94static const char CLOCK_FONT_ZIP_NAME[] = "clock_font.png";
Damien Bargiacchi97480862016-03-29 14:55:55 -070095static const char LAST_TIME_CHANGED_FILE_NAME[] = "last_time_change";
96static const char LAST_TIME_CHANGED_FILE_PATH[] = "/data/system/time/last_time_change";
97static const char ACCURATE_TIME_FLAG_FILE_NAME[] = "time_is_accurate";
98static const char ACCURATE_TIME_FLAG_FILE_PATH[] = "/data/system/time/time_is_accurate";
Damien Bargiacchi9071db12016-10-28 17:38:22 -070099static const char TIME_FORMAT_12_HOUR_FLAG_FILE_PATH[] = "/data/system/time/time_format_12_hour";
Damien Bargiacchi96762812016-07-12 15:53:40 -0700100// Java timestamp format. Don't show the clock if the date is before 2000-01-01 00:00:00.
101static const long long ACCURATE_TIME_EPOCH = 946684800000;
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700102static constexpr char FONT_BEGIN_CHAR = ' ';
103static constexpr char FONT_END_CHAR = '~' + 1;
104static constexpr size_t FONT_NUM_CHARS = FONT_END_CHAR - FONT_BEGIN_CHAR + 1;
105static constexpr size_t FONT_NUM_COLS = 16;
106static constexpr size_t FONT_NUM_ROWS = FONT_NUM_CHARS / FONT_NUM_COLS;
107static const int TEXT_CENTER_VALUE = INT_MAX;
108static const int TEXT_MISSING_VALUE = INT_MIN;
Damien Bargiacchi97480862016-03-29 14:55:55 -0700109static const char EXIT_PROP_NAME[] = "service.bootanim.exit";
Huihong Luod4a217e2020-01-16 15:56:33 -0800110static const char DISPLAYS_PROP_NAME[] = "persist.service.bootanim.displays";
Kyeongkab.Nam35a8a112019-07-17 16:41:48 +0900111static const int ANIM_ENTRY_NAME_MAX = ANIM_PATH_MAX + 1;
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700112static constexpr size_t TEXT_POS_LEN_MAX = 16;
Narayan Kamathafd31e02013-12-03 13:16:03 +0000113
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800114// ---------------------------------------------------------------------------
115
Ed Coyne7464ac92017-06-08 12:26:48 -0700116BootAnimation::BootAnimation(sp<Callbacks> callbacks)
Marin Shalamanov1a6e36892020-05-18 19:05:17 +0200117 : Thread(false), mClockEnabled(true), mTimeIsAccurate(false), mTimeFormat12Hour(false),
118 mTimeCheckThread(nullptr), mCallbacks(callbacks), mLooper(new Looper(false)) {
Mathias Agopian627e7b52009-05-21 19:21:59 -0700119 mSession = new SurfaceComposerClient();
Geoffrey Pitsch290c4352016-08-09 14:35:10 -0400120
Keun-young Parkb5938422017-03-23 13:46:24 -0700121 std::string powerCtl = android::base::GetProperty("sys.powerctl", "");
122 if (powerCtl.empty()) {
123 mShuttingDown = false;
124 } else {
125 mShuttingDown = true;
126 }
Huihong Luo50a2f362018-08-11 09:27:57 -0700127 ALOGD("%sAnimationStartTiming start time: %" PRId64 "ms", mShuttingDown ? "Shutdown" : "Boot",
128 elapsedRealtime());
129}
130
131BootAnimation::~BootAnimation() {
132 if (mAnimation != nullptr) {
133 releaseAnimation(mAnimation);
134 mAnimation = nullptr;
135 }
136 ALOGD("%sAnimationStopTiming start time: %" PRId64 "ms", mShuttingDown ? "Shutdown" : "Boot",
137 elapsedRealtime());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800138}
139
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800140void BootAnimation::onFirstRef() {
Mathias Agopianbc726112009-09-23 15:44:05 -0700141 status_t err = mSession->linkToComposerDeath(this);
Wei Wang159a4102018-10-23 23:15:58 -0700142 SLOGE_IF(err, "linkToComposerDeath failed (%s) ", strerror(-err));
Mathias Agopian8434c532009-09-23 18:52:49 -0700143 if (err == NO_ERROR) {
Huihong Luo50a2f362018-08-11 09:27:57 -0700144 // Load the animation content -- this can be slow (eg 200ms)
145 // called before waitForSurfaceFlinger() in main() to avoid wait
146 ALOGD("%sAnimationPreloadTiming start time: %" PRId64 "ms",
147 mShuttingDown ? "Shutdown" : "Boot", elapsedRealtime());
148 preloadAnimation();
149 ALOGD("%sAnimationPreloadStopTiming start time: %" PRId64 "ms",
150 mShuttingDown ? "Shutdown" : "Boot", elapsedRealtime());
Mathias Agopianbc726112009-09-23 15:44:05 -0700151 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800152}
153
Mathias Agopianbc726112009-09-23 15:44:05 -0700154sp<SurfaceComposerClient> BootAnimation::session() const {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800155 return mSession;
156}
157
Marin Shalamanov1a6e36892020-05-18 19:05:17 +0200158void BootAnimation::binderDied(const wp<IBinder>&) {
Mathias Agopianbc726112009-09-23 15:44:05 -0700159 // woah, surfaceflinger died!
Wei Wang159a4102018-10-23 23:15:58 -0700160 SLOGD("SurfaceFlinger died, exiting...");
Mathias Agopianbc726112009-09-23 15:44:05 -0700161
162 // calling requestExit() is not enough here because the Surface code
163 // might be blocked on a condition variable that will never be updated.
164 kill( getpid(), SIGKILL );
165 requestExit();
166}
167
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800168status_t BootAnimation::initTexture(Texture* texture, AssetManager& assets,
169 const char* name) {
170 Asset* asset = assets.open(name, Asset::ACCESS_BUFFER);
Yi Kong911ac232019-03-24 01:49:02 -0700171 if (asset == nullptr)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800172 return NO_INIT;
173 SkBitmap bitmap;
Matt Sarett8898c162016-03-24 16:11:01 -0400174 sk_sp<SkData> data = SkData::MakeWithoutCopy(asset->getBuffer(false),
175 asset->getLength());
176 sk_sp<SkImage> image = SkImage::MakeFromEncoded(data);
177 image->asLegacyBitmap(&bitmap, SkImage::kRO_LegacyBitmapMode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800178 asset->close();
179 delete asset;
180
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800181 const int w = bitmap.width();
182 const int h = bitmap.height();
183 const void* p = bitmap.getPixels();
184
185 GLint crop[4] = { 0, h, w, -h };
186 texture->w = w;
187 texture->h = h;
188
189 glGenTextures(1, &texture->name);
190 glBindTexture(GL_TEXTURE_2D, texture->name);
191
Mike Reed42a1d082014-07-07 18:06:18 -0400192 switch (bitmap.colorType()) {
193 case kAlpha_8_SkColorType:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800194 glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, w, h, 0, GL_ALPHA,
195 GL_UNSIGNED_BYTE, p);
196 break;
Mike Reed42a1d082014-07-07 18:06:18 -0400197 case kARGB_4444_SkColorType:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800198 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA,
199 GL_UNSIGNED_SHORT_4_4_4_4, p);
200 break;
Mike Reed42a1d082014-07-07 18:06:18 -0400201 case kN32_SkColorType:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800202 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA,
203 GL_UNSIGNED_BYTE, p);
204 break;
Mike Reed42a1d082014-07-07 18:06:18 -0400205 case kRGB_565_SkColorType:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800206 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, w, h, 0, GL_RGB,
207 GL_UNSIGNED_SHORT_5_6_5, p);
208 break;
209 default:
210 break;
211 }
212
213 glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_CROP_RECT_OES, crop);
214 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
215 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
216 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
217 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700218
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800219 return NO_ERROR;
220}
221
Marin Shalamanov1a6e36892020-05-18 19:05:17 +0200222status_t BootAnimation::initTexture(FileMap* map, int* width, int* height) {
Mathias Agopiana8826d62009-10-01 03:10:14 -0700223 SkBitmap bitmap;
Damien Bargiacchif67b3172016-09-07 20:17:14 -0700224 sk_sp<SkData> data = SkData::MakeWithoutCopy(map->getDataPtr(),
225 map->getDataLength());
Matt Sarett8898c162016-03-24 16:11:01 -0400226 sk_sp<SkImage> image = SkImage::MakeFromEncoded(data);
227 image->asLegacyBitmap(&bitmap, SkImage::kRO_LegacyBitmapMode);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700228
Mykola Kondratenko0c1eeb32014-04-15 09:35:44 +0200229 // FileMap memory is never released until application exit.
230 // Release it now as the texture is already loaded and the memory used for
231 // the packed resource can be released.
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700232 delete map;
Mykola Kondratenko0c1eeb32014-04-15 09:35:44 +0200233
Mathias Agopiana8826d62009-10-01 03:10:14 -0700234 const int w = bitmap.width();
235 const int h = bitmap.height();
236 const void* p = bitmap.getPixels();
237
238 GLint crop[4] = { 0, h, w, -h };
239 int tw = 1 << (31 - __builtin_clz(w));
240 int th = 1 << (31 - __builtin_clz(h));
241 if (tw < w) tw <<= 1;
242 if (th < h) th <<= 1;
243
Mike Reed42a1d082014-07-07 18:06:18 -0400244 switch (bitmap.colorType()) {
245 case kN32_SkColorType:
Sai Kiran Korwar27167492015-07-07 20:00:06 +0530246 if (!mUseNpotTextures && (tw != w || th != h)) {
Mathias Agopiana8826d62009-10-01 03:10:14 -0700247 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, tw, th, 0, GL_RGBA,
Yi Kong911ac232019-03-24 01:49:02 -0700248 GL_UNSIGNED_BYTE, nullptr);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700249 glTexSubImage2D(GL_TEXTURE_2D, 0,
250 0, 0, w, h, GL_RGBA, GL_UNSIGNED_BYTE, p);
251 } else {
Sai Kiran Korwar27167492015-07-07 20:00:06 +0530252 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA,
Mathias Agopiana8826d62009-10-01 03:10:14 -0700253 GL_UNSIGNED_BYTE, p);
254 }
255 break;
256
Mike Reed42a1d082014-07-07 18:06:18 -0400257 case kRGB_565_SkColorType:
Sai Kiran Korwar27167492015-07-07 20:00:06 +0530258 if (!mUseNpotTextures && (tw != w || th != h)) {
Mathias Agopiana8826d62009-10-01 03:10:14 -0700259 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, tw, th, 0, GL_RGB,
Yi Kong911ac232019-03-24 01:49:02 -0700260 GL_UNSIGNED_SHORT_5_6_5, nullptr);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700261 glTexSubImage2D(GL_TEXTURE_2D, 0,
262 0, 0, w, h, GL_RGB, GL_UNSIGNED_SHORT_5_6_5, p);
263 } else {
Sai Kiran Korwar27167492015-07-07 20:00:06 +0530264 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, w, h, 0, GL_RGB,
Mathias Agopiana8826d62009-10-01 03:10:14 -0700265 GL_UNSIGNED_SHORT_5_6_5, p);
266 }
267 break;
268 default:
269 break;
270 }
271
272 glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_CROP_RECT_OES, crop);
273
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700274 *width = w;
275 *height = h;
276
Mathias Agopiana8826d62009-10-01 03:10:14 -0700277 return NO_ERROR;
278}
279
Marin Shalamanov1a6e36892020-05-18 19:05:17 +0200280class BootAnimation::DisplayEventCallback : public LooperCallback {
281 BootAnimation* mBootAnimation;
282
283public:
284 DisplayEventCallback(BootAnimation* bootAnimation) {
285 mBootAnimation = bootAnimation;
286 }
287
288 int handleEvent(int /* fd */, int events, void* /* data */) {
289 if (events & (Looper::EVENT_ERROR | Looper::EVENT_HANGUP)) {
290 ALOGE("Display event receiver pipe was closed or an error occurred. events=0x%x",
291 events);
292 return 0; // remove the callback
293 }
294
295 if (!(events & Looper::EVENT_INPUT)) {
296 ALOGW("Received spurious callback for unhandled poll event. events=0x%x", events);
297 return 1; // keep the callback
298 }
299
300 constexpr int kBufferSize = 100;
301 DisplayEventReceiver::Event buffer[kBufferSize];
302 ssize_t numEvents;
303 do {
304 numEvents = mBootAnimation->mDisplayEventReceiver->getEvents(buffer, kBufferSize);
305 for (size_t i = 0; i < static_cast<size_t>(numEvents); i++) {
306 const auto& event = buffer[i];
307 if (event.header.type == DisplayEventReceiver::DISPLAY_EVENT_HOTPLUG) {
308 SLOGV("Hotplug received");
309
310 if (!event.hotplug.connected) {
311 // ignore hotplug disconnect
312 continue;
313 }
314 auto token = SurfaceComposerClient::getPhysicalDisplayToken(
315 event.header.displayId);
316
317 if (token != mBootAnimation->mDisplayToken) {
318 // ignore hotplug of a secondary display
319 continue;
320 }
321
322 DisplayConfig displayConfig;
323 const status_t error = SurfaceComposerClient::getActiveDisplayConfig(
324 mBootAnimation->mDisplayToken, &displayConfig);
325 if (error != NO_ERROR) {
326 SLOGE("Can't get active display configuration.");
327 }
328 mBootAnimation->resizeSurface(displayConfig.resolution.getWidth(),
329 displayConfig.resolution.getHeight());
330 }
331 }
332 } while (numEvents > 0);
333
334 return 1; // keep the callback
335 }
336};
337
338EGLConfig BootAnimation::getEglConfig(const EGLDisplay& display) {
339 const EGLint attribs[] = {
340 EGL_RED_SIZE, 8,
341 EGL_GREEN_SIZE, 8,
342 EGL_BLUE_SIZE, 8,
343 EGL_DEPTH_SIZE, 0,
344 EGL_NONE
345 };
346 EGLint numConfigs;
347 EGLConfig config;
348 eglChooseConfig(display, attribs, &config, 1, &numConfigs);
349 return config;
350}
351
Marin Shalamanov047802d2020-05-19 23:55:12 +0200352ui::Size BootAnimation::limitSurfaceSize(int width, int height) const {
353 ui::Size limited(width, height);
354 bool wasLimited = false;
355 const float aspectRatio = float(width) / float(height);
356 if (mMaxWidth != 0 && width > mMaxWidth) {
357 limited.height = mMaxWidth / aspectRatio;
358 limited.width = mMaxWidth;
359 wasLimited = true;
360 }
361 if (mMaxHeight != 0 && limited.height > mMaxHeight) {
362 limited.height = mMaxHeight;
363 limited.width = mMaxHeight * aspectRatio;
364 wasLimited = true;
365 }
366 SLOGV_IF(wasLimited, "Surface size has been limited to [%dx%d] from [%dx%d]",
367 limited.width, limited.height, width, height);
368 return limited;
369}
370
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800371status_t BootAnimation::readyToRun() {
372 mAssets.addDefaultAssets();
373
Dominik Laskowski3316a0a2019-01-25 02:56:41 -0800374 mDisplayToken = SurfaceComposerClient::getInternalDisplayToken();
375 if (mDisplayToken == nullptr)
Dominik Laskowski69b281d2019-11-22 14:13:12 -0800376 return NAME_NOT_FOUND;
Dominik Laskowski3316a0a2019-01-25 02:56:41 -0800377
Dominik Laskowski69b281d2019-11-22 14:13:12 -0800378 DisplayConfig displayConfig;
379 const status_t error =
380 SurfaceComposerClient::getActiveDisplayConfig(mDisplayToken, &displayConfig);
381 if (error != NO_ERROR)
382 return error;
383
Marin Shalamanov047802d2020-05-19 23:55:12 +0200384 mMaxWidth = android::base::GetIntProperty("ro.surface_flinger.max_graphics_width", 0);
385 mMaxHeight = android::base::GetIntProperty("ro.surface_flinger.max_graphics_height", 0);
386 ui::Size resolution = displayConfig.resolution;
387 resolution = limitSurfaceSize(resolution.width, resolution.height);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800388 // create the native surface
Jeff Brown0b722fe2012-08-24 22:40:14 -0700389 sp<SurfaceControl> control = session()->createSurface(String8("BootAnimation"),
Dominik Laskowski69b281d2019-11-22 14:13:12 -0800390 resolution.getWidth(), resolution.getHeight(), PIXEL_FORMAT_RGB_565);
Mathias Agopian439863f2011-06-28 19:09:31 -0700391
Robert Carre13b58e2017-08-31 14:50:44 -0700392 SurfaceComposerClient::Transaction t;
Huihong Luof97c9e22019-05-29 11:08:01 -0700393
394 // this guest property specifies multi-display IDs to show the boot animation
395 // multiple ids can be set with comma (,) as separator, for example:
Huihong Luod4a217e2020-01-16 15:56:33 -0800396 // setprop persist.boot.animation.displays 19260422155234049,19261083906282754
Huihong Luof97c9e22019-05-29 11:08:01 -0700397 Vector<uint64_t> physicalDisplayIds;
398 char displayValue[PROPERTY_VALUE_MAX] = "";
Huihong Luod4a217e2020-01-16 15:56:33 -0800399 property_get(DISPLAYS_PROP_NAME, displayValue, "");
Huihong Luof97c9e22019-05-29 11:08:01 -0700400 bool isValid = displayValue[0] != '\0';
401 if (isValid) {
402 char *p = displayValue;
403 while (*p) {
404 if (!isdigit(*p) && *p != ',') {
405 isValid = false;
406 break;
407 }
408 p ++;
409 }
410 if (!isValid)
Huihong Luod4a217e2020-01-16 15:56:33 -0800411 SLOGE("Invalid syntax for the value of system prop: %s", DISPLAYS_PROP_NAME);
Huihong Luof97c9e22019-05-29 11:08:01 -0700412 }
413 if (isValid) {
414 std::istringstream stream(displayValue);
415 for (PhysicalDisplayId id; stream >> id; ) {
416 physicalDisplayIds.add(id);
417 if (stream.peek() == ',')
418 stream.ignore();
419 }
420
421 // In the case of multi-display, boot animation shows on the specified displays
422 // in addition to the primary display
423 auto ids = SurfaceComposerClient::getPhysicalDisplayIds();
424 constexpr uint32_t LAYER_STACK = 0;
425 for (auto id : physicalDisplayIds) {
426 if (std::find(ids.begin(), ids.end(), id) != ids.end()) {
427 sp<IBinder> token = SurfaceComposerClient::getPhysicalDisplayToken(id);
428 if (token != nullptr)
429 t.setDisplayLayerStack(token, LAYER_STACK);
430 }
431 }
432 t.setLayerStack(control, LAYER_STACK);
433 }
434
Robert Carre13b58e2017-08-31 14:50:44 -0700435 t.setLayer(control, 0x40000000)
436 .apply();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800437
Mathias Agopian17f638b2009-04-16 20:04:08 -0700438 sp<Surface> s = control->getSurface();
439
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800440 // initialize opengl and egl
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800441 EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
Yi Kong911ac232019-03-24 01:49:02 -0700442 eglInitialize(display, nullptr, nullptr);
Marin Shalamanov1a6e36892020-05-18 19:05:17 +0200443 EGLConfig config = getEglConfig(display);
444 EGLSurface surface = eglCreateWindowSurface(display, config, s.get(), nullptr);
445 EGLContext context = eglCreateContext(display, config, nullptr, nullptr);
446 EGLint w, h;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800447 eglQuerySurface(display, surface, EGL_WIDTH, &w);
448 eglQuerySurface(display, surface, EGL_HEIGHT, &h);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700449
Mathias Agopianabac0102009-07-31 14:47:00 -0700450 if (eglMakeCurrent(display, surface, surface, context) == EGL_FALSE)
451 return NO_INIT;
Mathias Agopiana8826d62009-10-01 03:10:14 -0700452
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800453 mDisplay = display;
454 mContext = context;
455 mSurface = surface;
456 mWidth = w;
457 mHeight = h;
Mathias Agopian17f638b2009-04-16 20:04:08 -0700458 mFlingerSurfaceControl = control;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800459 mFlingerSurface = s;
Adrian Roos9ee5dff2018-08-22 20:19:49 +0200460 mTargetInset = -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800461
Yegor Malysheveafcb992020-09-10 16:42:20 +0200462 projectSceneToWindow();
463
Marin Shalamanov1a6e36892020-05-18 19:05:17 +0200464 // Register a display event receiver
465 mDisplayEventReceiver = std::make_unique<DisplayEventReceiver>();
466 status_t status = mDisplayEventReceiver->initCheck();
467 SLOGE_IF(status != NO_ERROR, "Initialization of DisplayEventReceiver failed with status: %d",
468 status);
469 mLooper->addFd(mDisplayEventReceiver->getFd(), 0, Looper::EVENT_INPUT,
470 new DisplayEventCallback(this), nullptr);
471
Huihong Luo50a2f362018-08-11 09:27:57 -0700472 return NO_ERROR;
473}
474
Yegor Malysheveafcb992020-09-10 16:42:20 +0200475void BootAnimation::projectSceneToWindow() {
476 glViewport(0, 0, mWidth, mHeight);
477 glScissor(0, 0, mWidth, mHeight);
478 glMatrixMode(GL_PROJECTION);
479 glLoadIdentity();
480 glOrthof(0, static_cast<float>(mWidth), 0, static_cast<float>(mHeight), -1, 1);
481 glMatrixMode(GL_MODELVIEW);
482 glLoadIdentity();
483}
484
Marin Shalamanov1a6e36892020-05-18 19:05:17 +0200485void BootAnimation::resizeSurface(int newWidth, int newHeight) {
486 // We assume this function is called on the animation thread.
487 if (newWidth == mWidth && newHeight == mHeight) {
488 return;
489 }
490 SLOGV("Resizing the boot animation surface to %d %d", newWidth, newHeight);
491
492 eglMakeCurrent(mDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
493 eglDestroySurface(mDisplay, mSurface);
494
Marin Shalamanov047802d2020-05-19 23:55:12 +0200495 const auto limitedSize = limitSurfaceSize(newWidth, newHeight);
496 mWidth = limitedSize.width;
497 mHeight = limitedSize.height;
Marin Shalamanov1a6e36892020-05-18 19:05:17 +0200498
499 SurfaceComposerClient::Transaction t;
500 t.setSize(mFlingerSurfaceControl, mWidth, mHeight);
501 t.apply();
502
503 EGLConfig config = getEglConfig(mDisplay);
504 EGLSurface surface = eglCreateWindowSurface(mDisplay, config, mFlingerSurface.get(), nullptr);
505 if (eglMakeCurrent(mDisplay, surface, surface, mContext) == EGL_FALSE) {
506 SLOGE("Can't make the new surface current. Error %d", eglGetError());
507 return;
508 }
Yegor Malysheveafcb992020-09-10 16:42:20 +0200509
510 projectSceneToWindow();
Marin Shalamanov1a6e36892020-05-18 19:05:17 +0200511
512 mSurface = surface;
513}
514
Huihong Luo50a2f362018-08-11 09:27:57 -0700515bool BootAnimation::preloadAnimation() {
516 findBootAnimationFile();
517 if (!mZipFileName.isEmpty()) {
518 mAnimation = loadAnimation(mZipFileName);
519 return (mAnimation != nullptr);
520 }
521
522 return false;
523}
524
Nikita Ioffe06c986e2020-01-27 17:16:03 +0000525bool BootAnimation::findBootAnimationFileInternal(const std::vector<std::string> &files) {
526 for (const std::string& f : files) {
527 if (access(f.c_str(), R_OK) == 0) {
528 mZipFileName = f.c_str();
529 return true;
530 }
531 }
532 return false;
533}
534
Huihong Luo50a2f362018-08-11 09:27:57 -0700535void BootAnimation::findBootAnimationFile() {
Elliott Hughesc367d482013-10-29 13:12:55 -0700536 // If the device has encryption turned on or is in process
Jason parksbd9a08d2011-01-31 15:04:34 -0600537 // of being encrypted we show the encrypted boot animation.
538 char decrypt[PROPERTY_VALUE_MAX];
539 property_get("vold.decrypt", decrypt, "");
540
Keun-young Parkb5938422017-03-23 13:46:24 -0700541 bool encryptedAnimation = atoi(decrypt) != 0 ||
542 !strcmp("trigger_restart_min_framework", decrypt);
Jason parksbd9a08d2011-01-31 15:04:34 -0600543
Jaekyun Seokc521bb32018-01-30 11:52:47 +0900544 if (!mShuttingDown && encryptedAnimation) {
Nikita Ioffe06c986e2020-01-27 17:16:03 +0000545 static const std::vector<std::string> encryptedBootFiles = {
546 PRODUCT_ENCRYPTED_BOOTANIMATION_FILE, SYSTEM_ENCRYPTED_BOOTANIMATION_FILE,
547 };
548 if (findBootAnimationFileInternal(encryptedBootFiles)) {
549 return;
Jaekyun Seokc521bb32018-01-30 11:52:47 +0900550 }
Jason parksbd9a08d2011-01-31 15:04:34 -0600551 }
Beverly34ffe252019-05-17 11:03:54 -0400552
553 const bool playDarkAnim = android::base::GetIntProperty("ro.boot.theme", 0) == 1;
Nikita Ioffe06c986e2020-01-27 17:16:03 +0000554 static const std::vector<std::string> bootFiles = {
555 APEX_BOOTANIMATION_FILE, playDarkAnim ? PRODUCT_BOOTANIMATION_DARK_FILE : PRODUCT_BOOTANIMATION_FILE,
556 OEM_BOOTANIMATION_FILE, SYSTEM_BOOTANIMATION_FILE
557 };
558 static const std::vector<std::string> shutdownFiles = {
559 PRODUCT_SHUTDOWNANIMATION_FILE, OEM_SHUTDOWNANIMATION_FILE, SYSTEM_SHUTDOWNANIMATION_FILE, ""
560 };
561 static const std::vector<std::string> userspaceRebootFiles = {
562 PRODUCT_USERSPACE_REBOOT_ANIMATION_FILE, OEM_USERSPACE_REBOOT_ANIMATION_FILE,
563 SYSTEM_USERSPACE_REBOOT_ANIMATION_FILE,
564 };
Keun-young Parkb5938422017-03-23 13:46:24 -0700565
Nikita Ioffe06c986e2020-01-27 17:16:03 +0000566 if (android::base::GetBoolProperty("sys.init.userspace_reboot.in_progress", false)) {
567 findBootAnimationFileInternal(userspaceRebootFiles);
568 } else if (mShuttingDown) {
569 findBootAnimationFileInternal(shutdownFiles);
570 } else {
571 findBootAnimationFileInternal(bootFiles);
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700572 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800573}
574
Marin Shalamanov1a6e36892020-05-18 19:05:17 +0200575bool BootAnimation::threadLoop() {
576 bool result;
Narayan Kamathafd31e02013-12-03 13:16:03 +0000577 // We have no bootanimation file, so we use the stock android logo
578 // animation.
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700579 if (mZipFileName.isEmpty()) {
Marin Shalamanov1a6e36892020-05-18 19:05:17 +0200580 result = android();
Mathias Agopiana8826d62009-10-01 03:10:14 -0700581 } else {
Marin Shalamanov1a6e36892020-05-18 19:05:17 +0200582 result = movie();
Mathias Agopiana8826d62009-10-01 03:10:14 -0700583 }
584
Philip Junkerf1291ba2020-11-25 18:16:16 +0100585 mCallbacks->shutdown();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800586 eglMakeCurrent(mDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
587 eglDestroyContext(mDisplay, mContext);
588 eglDestroySurface(mDisplay, mSurface);
Mathias Agopian6cf0db22009-04-17 19:36:26 -0700589 mFlingerSurface.clear();
Mathias Agopian17f638b2009-04-16 20:04:08 -0700590 mFlingerSurfaceControl.clear();
Mathias Agopian627e7b52009-05-21 19:21:59 -0700591 eglTerminate(mDisplay);
Sai Kiran Korwar3eee9fb2015-06-16 17:13:35 +0530592 eglReleaseThread();
Mathias Agopian627e7b52009-05-21 19:21:59 -0700593 IPCThreadState::self()->stopProcess();
Marin Shalamanov1a6e36892020-05-18 19:05:17 +0200594 return result;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800595}
596
Marin Shalamanov1a6e36892020-05-18 19:05:17 +0200597bool BootAnimation::android() {
Wei Wang159a4102018-10-23 23:15:58 -0700598 SLOGD("%sAnimationShownTiming start time: %" PRId64 "ms", mShuttingDown ? "Shutdown" : "Boot",
Keun-young Parkd1794cd2017-04-04 12:21:19 -0700599 elapsedRealtime());
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700600 initTexture(&mAndroid[0], mAssets, "images/android-logo-mask.png");
601 initTexture(&mAndroid[1], mAssets, "images/android-logo-shine.png");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800602
Ed Coyne7464ac92017-06-08 12:26:48 -0700603 mCallbacks->init({});
604
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800605 // clear screen
Mathias Agopiana8826d62009-10-01 03:10:14 -0700606 glShadeModel(GL_FLAT);
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700607 glDisable(GL_DITHER);
608 glDisable(GL_SCISSOR_TEST);
Mathias Agopian59f19e42011-05-06 19:22:12 -0700609 glClearColor(0,0,0,1);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800610 glClear(GL_COLOR_BUFFER_BIT);
611 eglSwapBuffers(mDisplay, mSurface);
612
Mathias Agopiana8826d62009-10-01 03:10:14 -0700613 glEnable(GL_TEXTURE_2D);
614 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
615
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700616 // Blend state
617 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
618 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
619
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800620 const nsecs_t startTime = systemTime();
621 do {
Marin Shalamanov1a6e36892020-05-18 19:05:17 +0200622 processDisplayEvents();
623 const GLint xc = (mWidth - mAndroid[0].w) / 2;
624 const GLint yc = (mHeight - mAndroid[0].h) / 2;
625 const Rect updateRect(xc, yc, xc + mAndroid[0].w, yc + mAndroid[0].h);
626 glScissor(updateRect.left, mHeight - updateRect.bottom, updateRect.width(),
627 updateRect.height());
628
Mathias Agopian13796652009-03-24 22:49:21 -0700629 nsecs_t now = systemTime();
630 double time = now - startTime;
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700631 float t = 4.0f * float(time / us2ns(16667)) / mAndroid[1].w;
632 GLint offset = (1 - (t - floorf(t))) * mAndroid[1].w;
633 GLint x = xc - offset;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800634
Mathias Agopian81668642009-07-28 11:41:30 -0700635 glDisable(GL_SCISSOR_TEST);
636 glClear(GL_COLOR_BUFFER_BIT);
637
638 glEnable(GL_SCISSOR_TEST);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800639 glDisable(GL_BLEND);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800640 glBindTexture(GL_TEXTURE_2D, mAndroid[1].name);
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700641 glDrawTexiOES(x, yc, 0, mAndroid[1].w, mAndroid[1].h);
642 glDrawTexiOES(x + mAndroid[1].w, yc, 0, mAndroid[1].w, mAndroid[1].h);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800643
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700644 glEnable(GL_BLEND);
645 glBindTexture(GL_TEXTURE_2D, mAndroid[0].name);
646 glDrawTexiOES(xc, yc, 0, mAndroid[0].w, mAndroid[0].h);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800647
Mathias Agopian627e7b52009-05-21 19:21:59 -0700648 EGLBoolean res = eglSwapBuffers(mDisplay, mSurface);
649 if (res == EGL_FALSE)
650 break;
651
Mathias Agopian13796652009-03-24 22:49:21 -0700652 // 12fps: don't animate too fast to preserve CPU
653 const nsecs_t sleepTime = 83333 - ns2us(systemTime() - now);
654 if (sleepTime > 0)
Mathias Agopiana8826d62009-10-01 03:10:14 -0700655 usleep(sleepTime);
Kevin Hesterd3782b22012-04-26 10:38:55 -0700656
657 checkExit();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800658 } while (!exitPending());
659
660 glDeleteTextures(1, &mAndroid[0].name);
661 glDeleteTextures(1, &mAndroid[1].name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800662 return false;
663}
664
Kevin Hesterd3782b22012-04-26 10:38:55 -0700665void BootAnimation::checkExit() {
666 // Allow surface flinger to gracefully request shutdown
667 char value[PROPERTY_VALUE_MAX];
668 property_get(EXIT_PROP_NAME, value, "0");
669 int exitnow = atoi(value);
670 if (exitnow) {
671 requestExit();
672 }
673}
674
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700675bool BootAnimation::validClock(const Animation::Part& part) {
676 return part.clockPosX != TEXT_MISSING_VALUE && part.clockPosY != TEXT_MISSING_VALUE;
677}
678
679bool parseTextCoord(const char* str, int* dest) {
680 if (strcmp("c", str) == 0) {
681 *dest = TEXT_CENTER_VALUE;
682 return true;
683 }
684
685 char* end;
686 int val = (int) strtol(str, &end, 0);
687 if (end == str || *end != '\0' || val == INT_MAX || val == INT_MIN) {
688 return false;
689 }
690 *dest = val;
691 return true;
692}
693
694// Parse two position coordinates. If only string is non-empty, treat it as the y value.
695void parsePosition(const char* str1, const char* str2, int* x, int* y) {
696 bool success = false;
697 if (strlen(str1) == 0) { // No values were specified
698 // success = false
699 } else if (strlen(str2) == 0) { // we have only one value
700 if (parseTextCoord(str1, y)) {
701 *x = TEXT_CENTER_VALUE;
702 success = true;
703 }
704 } else {
705 if (parseTextCoord(str1, x) && parseTextCoord(str2, y)) {
706 success = true;
707 }
708 }
709
710 if (!success) {
711 *x = TEXT_MISSING_VALUE;
712 *y = TEXT_MISSING_VALUE;
713 }
714}
715
Jesse Hall083b84c2014-09-22 10:51:09 -0700716// Parse a color represented as an HTML-style 'RRGGBB' string: each pair of
717// characters in str is a hex number in [0, 255], which are converted to
718// floating point values in the range [0.0, 1.0] and placed in the
719// corresponding elements of color.
720//
721// If the input string isn't valid, parseColor returns false and color is
722// left unchanged.
723static bool parseColor(const char str[7], float color[3]) {
724 float tmpColor[3];
725 for (int i = 0; i < 3; i++) {
726 int val = 0;
727 for (int j = 0; j < 2; j++) {
728 val *= 16;
729 char c = str[2*i + j];
730 if (c >= '0' && c <= '9') val += c - '0';
731 else if (c >= 'A' && c <= 'F') val += (c - 'A') + 10;
732 else if (c >= 'a' && c <= 'f') val += (c - 'a') + 10;
733 else return false;
734 }
735 tmpColor[i] = static_cast<float>(val) / 255.0f;
736 }
737 memcpy(color, tmpColor, sizeof(tmpColor));
738 return true;
739}
740
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700741
Marin Shalamanov1a6e36892020-05-18 19:05:17 +0200742static bool readFile(ZipFileRO* zip, const char* name, String8& outString) {
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700743 ZipEntryRO entry = zip->findEntryByName(name);
Wei Wang159a4102018-10-23 23:15:58 -0700744 SLOGE_IF(!entry, "couldn't find %s", name);
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700745 if (!entry) {
746 return false;
747 }
748
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700749 FileMap* entryMap = zip->createEntryFileMap(entry);
750 zip->releaseEntry(entry);
Wei Wang159a4102018-10-23 23:15:58 -0700751 SLOGE_IF(!entryMap, "entryMap is null");
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700752 if (!entryMap) {
753 return false;
754 }
755
756 outString.setTo((char const*)entryMap->getDataPtr(), entryMap->getDataLength());
Narayan Kamath688ff4c2015-02-23 15:47:54 +0000757 delete entryMap;
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700758 return true;
759}
760
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700761// The font image should be a 96x2 array of character images. The
762// columns are the printable ASCII characters 0x20 - 0x7f. The
763// top row is regular text; the bottom row is bold.
764status_t BootAnimation::initFont(Font* font, const char* fallback) {
765 status_t status = NO_ERROR;
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800766
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700767 if (font->map != nullptr) {
768 glGenTextures(1, &font->texture.name);
769 glBindTexture(GL_TEXTURE_2D, font->texture.name);
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800770
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700771 status = initTexture(font->map, &font->texture.w, &font->texture.h);
772
773 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
774 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
775 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
776 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
777 } else if (fallback != nullptr) {
778 status = initTexture(&font->texture, mAssets, fallback);
779 } else {
780 return NO_INIT;
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800781 }
782
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700783 if (status == NO_ERROR) {
784 font->char_width = font->texture.w / FONT_NUM_COLS;
785 font->char_height = font->texture.h / FONT_NUM_ROWS / 2; // There are bold and regular rows
786 }
787
788 return status;
789}
790
Yegor Malysheveafcb992020-09-10 16:42:20 +0200791void BootAnimation::fadeFrame(const int frameLeft, const int frameBottom, const int frameWidth,
792 const int frameHeight, const Animation::Part& part,
793 const int fadedFramesCount) {
794 glEnable(GL_BLEND);
795 glEnableClientState(GL_VERTEX_ARRAY);
796 glDisable(GL_TEXTURE_2D);
797 // avoid creating a hole due to mixing result alpha with GL_REPLACE texture
798 glBlendFuncSeparateOES(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ZERO, GL_ONE);
799
800 const float alpha = static_cast<float>(fadedFramesCount) / part.framesToFadeCount;
801 glColor4f(part.backgroundColor[0], part.backgroundColor[1], part.backgroundColor[2], alpha);
802
803 const float frameStartX = static_cast<float>(frameLeft);
804 const float frameStartY = static_cast<float>(frameBottom);
805 const float frameEndX = frameStartX + frameWidth;
806 const float frameEndY = frameStartY + frameHeight;
807 const GLfloat frameRect[] = {
808 frameStartX, frameStartY,
809 frameEndX, frameStartY,
810 frameEndX, frameEndY,
811 frameStartX, frameEndY
812 };
813 glVertexPointer(2, GL_FLOAT, 0, frameRect);
814 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
815
816 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
817 glEnable(GL_TEXTURE_2D);
818 glDisableClientState(GL_VERTEX_ARRAY);
819 glDisable(GL_BLEND);
820}
821
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700822void BootAnimation::drawText(const char* str, const Font& font, bool bold, int* x, int* y) {
823 glEnable(GL_BLEND); // Allow us to draw on top of the animation
824 glBindTexture(GL_TEXTURE_2D, font.texture.name);
825
826 const int len = strlen(str);
827 const int strWidth = font.char_width * len;
828
829 if (*x == TEXT_CENTER_VALUE) {
830 *x = (mWidth - strWidth) / 2;
831 } else if (*x < 0) {
832 *x = mWidth + *x - strWidth;
833 }
834 if (*y == TEXT_CENTER_VALUE) {
835 *y = (mHeight - font.char_height) / 2;
836 } else if (*y < 0) {
837 *y = mHeight + *y - font.char_height;
838 }
839
840 int cropRect[4] = { 0, 0, font.char_width, -font.char_height };
841
842 for (int i = 0; i < len; i++) {
843 char c = str[i];
844
845 if (c < FONT_BEGIN_CHAR || c > FONT_END_CHAR) {
846 c = '?';
847 }
848
849 // Crop the texture to only the pixels in the current glyph
850 const int charPos = (c - FONT_BEGIN_CHAR); // Position in the list of valid characters
851 const int row = charPos / FONT_NUM_COLS;
852 const int col = charPos % FONT_NUM_COLS;
853 cropRect[0] = col * font.char_width; // Left of column
854 cropRect[1] = row * font.char_height * 2; // Top of row
855 // Move down to bottom of regular (one char_heigh) or bold (two char_heigh) line
856 cropRect[1] += bold ? 2 * font.char_height : font.char_height;
857 glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_CROP_RECT_OES, cropRect);
858
859 glDrawTexiOES(*x, *y, 0, font.char_width, font.char_height);
860
861 *x += font.char_width;
862 }
863
864 glDisable(GL_BLEND); // Return to the animation's default behaviour
865 glBindTexture(GL_TEXTURE_2D, 0);
866}
867
Damien Bargiacchi9071db12016-10-28 17:38:22 -0700868// We render 12 or 24 hour time.
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700869void BootAnimation::drawClock(const Font& font, const int xPos, const int yPos) {
Damien Bargiacchi9071db12016-10-28 17:38:22 -0700870 static constexpr char TIME_FORMAT_12[] = "%l:%M";
871 static constexpr char TIME_FORMAT_24[] = "%H:%M";
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700872 static constexpr int TIME_LENGTH = 6;
873
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800874 time_t rawtime;
875 time(&rawtime);
876 struct tm* timeInfo = localtime(&rawtime);
877
878 char timeBuff[TIME_LENGTH];
Damien Bargiacchi9071db12016-10-28 17:38:22 -0700879 const char* timeFormat = mTimeFormat12Hour ? TIME_FORMAT_12 : TIME_FORMAT_24;
880 size_t length = strftime(timeBuff, TIME_LENGTH, timeFormat, timeInfo);
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800881
882 if (length != TIME_LENGTH - 1) {
Wei Wang159a4102018-10-23 23:15:58 -0700883 SLOGE("Couldn't format time; abandoning boot animation clock");
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800884 mClockEnabled = false;
885 return;
886 }
887
Damien Bargiacchi45a76442016-12-05 18:02:18 -0800888 char* out = timeBuff[0] == ' ' ? &timeBuff[1] : &timeBuff[0];
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700889 int x = xPos;
890 int y = yPos;
Damien Bargiacchi45a76442016-12-05 18:02:18 -0800891 drawText(out, font, false, &x, &y);
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800892}
893
Marin Shalamanov1a6e36892020-05-18 19:05:17 +0200894bool BootAnimation::parseAnimationDesc(Animation& animation) {
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700895 String8 desString;
896
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700897 if (!readFile(animation.zip, "desc.txt", desString)) {
Narayan Kamathafd31e02013-12-03 13:16:03 +0000898 return false;
899 }
Mathias Agopiana8826d62009-10-01 03:10:14 -0700900 char const* s = desString.string();
901
Mathias Agopiana8826d62009-10-01 03:10:14 -0700902 // Parse the description file
903 for (;;) {
904 const char* endl = strstr(s, "\n");
Yi Kong911ac232019-03-24 01:49:02 -0700905 if (endl == nullptr) break;
Mathias Agopiana8826d62009-10-01 03:10:14 -0700906 String8 line(s, endl - s);
907 const char* l = line.string();
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800908 int fps = 0;
909 int width = 0;
910 int height = 0;
911 int count = 0;
912 int pause = 0;
Yegor Malysheveafcb992020-09-10 16:42:20 +0200913 int framesToFadeCount = 0;
Narayan Kamathafd31e02013-12-03 13:16:03 +0000914 char path[ANIM_ENTRY_NAME_MAX];
Jesse Hall083b84c2014-09-22 10:51:09 -0700915 char color[7] = "000000"; // default to black if unspecified
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700916 char clockPos1[TEXT_POS_LEN_MAX + 1] = "";
917 char clockPos2[TEXT_POS_LEN_MAX + 1] = "";
Kevin Hesterd3782b22012-04-26 10:38:55 -0700918 char pathType;
Yegor Malysheveafcb992020-09-10 16:42:20 +0200919
920 int nextReadPos;
921
Mathias Agopiana8826d62009-10-01 03:10:14 -0700922 if (sscanf(l, "%d %d %d", &width, &height, &fps) == 3) {
Wei Wang159a4102018-10-23 23:15:58 -0700923 // SLOGD("> w=%d, h=%d, fps=%d", width, height, fps);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700924 animation.width = width;
925 animation.height = height;
926 animation.fps = fps;
Yegor Malysheveafcb992020-09-10 16:42:20 +0200927 } else if (sscanf(l, "%c %d %d %" STRTO(ANIM_PATH_MAX) "s%n",
928 &pathType, &count, &pause, path, &nextReadPos) >= 4) {
929 if (pathType == 'f') {
930 sscanf(l + nextReadPos, " %d #%6s %16s %16s", &framesToFadeCount, color, clockPos1,
931 clockPos2);
932 } else {
933 sscanf(l + nextReadPos, " #%6s %16s %16s", color, clockPos1, clockPos2);
934 }
935 // SLOGD("> type=%c, count=%d, pause=%d, path=%s, framesToFadeCount=%d, color=%s, "
936 // "clockPos1=%s, clockPos2=%s",
937 // pathType, count, pause, path, framesToFadeCount, color, clockPos1, clockPos2);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700938 Animation::Part part;
Kevin Hesterd3782b22012-04-26 10:38:55 -0700939 part.playUntilComplete = pathType == 'c';
Yegor Malysheveafcb992020-09-10 16:42:20 +0200940 part.framesToFadeCount = framesToFadeCount;
Mathias Agopiana8826d62009-10-01 03:10:14 -0700941 part.count = count;
942 part.pause = pause;
943 part.path = path;
Yi Kong911ac232019-03-24 01:49:02 -0700944 part.audioData = nullptr;
945 part.animation = nullptr;
Jesse Hall083b84c2014-09-22 10:51:09 -0700946 if (!parseColor(color, part.backgroundColor)) {
Wei Wang159a4102018-10-23 23:15:58 -0700947 SLOGE("> invalid color '#%s'", color);
Jesse Hall083b84c2014-09-22 10:51:09 -0700948 part.backgroundColor[0] = 0.0f;
949 part.backgroundColor[1] = 0.0f;
950 part.backgroundColor[2] = 0.0f;
951 }
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700952 parsePosition(clockPos1, clockPos2, &part.clockPosX, &part.clockPosY);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700953 animation.parts.add(part);
954 }
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700955 else if (strcmp(l, "$SYSTEM") == 0) {
Wei Wang159a4102018-10-23 23:15:58 -0700956 // SLOGD("> SYSTEM");
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700957 Animation::Part part;
958 part.playUntilComplete = false;
Yegor Malysheveafcb992020-09-10 16:42:20 +0200959 part.framesToFadeCount = 0;
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700960 part.count = 1;
961 part.pause = 0;
Yi Kong911ac232019-03-24 01:49:02 -0700962 part.audioData = nullptr;
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700963 part.animation = loadAnimation(String8(SYSTEM_BOOTANIMATION_FILE));
Yi Kong911ac232019-03-24 01:49:02 -0700964 if (part.animation != nullptr)
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700965 animation.parts.add(part);
966 }
Mathias Agopiana8826d62009-10-01 03:10:14 -0700967 s = ++endl;
968 }
969
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700970 return true;
971}
972
Marin Shalamanov1a6e36892020-05-18 19:05:17 +0200973bool BootAnimation::preloadZip(Animation& animation) {
Mathias Agopiana8826d62009-10-01 03:10:14 -0700974 // read all the data structures
975 const size_t pcount = animation.parts.size();
Yi Kong911ac232019-03-24 01:49:02 -0700976 void *cookie = nullptr;
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400977 ZipFileRO* zip = animation.zip;
978 if (!zip->startIteration(&cookie)) {
Narayan Kamathafd31e02013-12-03 13:16:03 +0000979 return false;
980 }
981
982 ZipEntryRO entry;
983 char name[ANIM_ENTRY_NAME_MAX];
Yi Kong911ac232019-03-24 01:49:02 -0700984 while ((entry = zip->nextEntry(cookie)) != nullptr) {
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400985 const int foundEntryName = zip->getEntryFileName(entry, name, ANIM_ENTRY_NAME_MAX);
Narayan Kamathafd31e02013-12-03 13:16:03 +0000986 if (foundEntryName > ANIM_ENTRY_NAME_MAX || foundEntryName == -1) {
Wei Wang159a4102018-10-23 23:15:58 -0700987 SLOGE("Error fetching entry file name");
Narayan Kamathafd31e02013-12-03 13:16:03 +0000988 continue;
989 }
990
991 const String8 entryName(name);
992 const String8 path(entryName.getPathDir());
993 const String8 leaf(entryName.getPathLeaf());
994 if (leaf.size() > 0) {
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700995 if (entryName == CLOCK_FONT_ZIP_NAME) {
996 FileMap* map = zip->createEntryFileMap(entry);
997 if (map) {
998 animation.clockFont.map = map;
999 }
1000 continue;
1001 }
1002
Geoffrey Pitschdd214a72016-06-27 17:14:30 -04001003 for (size_t j = 0; j < pcount; j++) {
Narayan Kamathafd31e02013-12-03 13:16:03 +00001004 if (path == animation.parts[j].path) {
Narayan Kamath4600dd02015-06-16 12:02:57 +01001005 uint16_t method;
Narayan Kamathafd31e02013-12-03 13:16:03 +00001006 // supports only stored png files
Yi Kong911ac232019-03-24 01:49:02 -07001007 if (zip->getEntryInfo(entry, &method, nullptr, nullptr, nullptr, nullptr, nullptr)) {
Narayan Kamathafd31e02013-12-03 13:16:03 +00001008 if (method == ZipFileRO::kCompressStored) {
Geoffrey Pitschdd214a72016-06-27 17:14:30 -04001009 FileMap* map = zip->createEntryFileMap(entry);
Narayan Kamathafd31e02013-12-03 13:16:03 +00001010 if (map) {
Narayan Kamathafd31e02013-12-03 13:16:03 +00001011 Animation::Part& part(animation.parts.editItemAt(j));
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -07001012 if (leaf == "audio.wav") {
1013 // a part may have at most one audio file
Geoffrey Pitschd6d9a1d2016-06-08 00:38:58 -07001014 part.audioData = (uint8_t *)map->getDataPtr();
1015 part.audioLength = map->getDataLength();
Geoffrey Pitschdd214a72016-06-27 17:14:30 -04001016 } else if (leaf == "trim.txt") {
1017 part.trimData.setTo((char const*)map->getDataPtr(),
1018 map->getDataLength());
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -07001019 } else {
1020 Animation::Frame frame;
1021 frame.name = leaf;
1022 frame.map = map;
Geoffrey Pitschdd214a72016-06-27 17:14:30 -04001023 frame.trimWidth = animation.width;
1024 frame.trimHeight = animation.height;
1025 frame.trimX = 0;
1026 frame.trimY = 0;
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -07001027 part.frames.add(frame);
1028 }
Mathias Agopiana8826d62009-10-01 03:10:14 -07001029 }
Geoffrey Pitschd6d9a1d2016-06-08 00:38:58 -07001030 } else {
Wei Wang159a4102018-10-23 23:15:58 -07001031 SLOGE("bootanimation.zip is compressed; must be only stored");
Mathias Agopiana8826d62009-10-01 03:10:14 -07001032 }
1033 }
1034 }
1035 }
1036 }
1037 }
1038
Geoffrey Pitschdd214a72016-06-27 17:14:30 -04001039 // If there is trimData present, override the positioning defaults.
1040 for (Animation::Part& part : animation.parts) {
1041 const char* trimDataStr = part.trimData.string();
1042 for (size_t frameIdx = 0; frameIdx < part.frames.size(); frameIdx++) {
1043 const char* endl = strstr(trimDataStr, "\n");
1044 // No more trimData for this part.
Yi Kong911ac232019-03-24 01:49:02 -07001045 if (endl == nullptr) {
Geoffrey Pitschdd214a72016-06-27 17:14:30 -04001046 break;
1047 }
1048 String8 line(trimDataStr, endl - trimDataStr);
1049 const char* lineStr = line.string();
1050 trimDataStr = ++endl;
1051 int width = 0, height = 0, x = 0, y = 0;
1052 if (sscanf(lineStr, "%dx%d+%d+%d", &width, &height, &x, &y) == 4) {
1053 Animation::Frame& frame(part.frames.editItemAt(frameIdx));
1054 frame.trimWidth = width;
1055 frame.trimHeight = height;
1056 frame.trimX = x;
1057 frame.trimY = y;
1058 } else {
Wei Wang159a4102018-10-23 23:15:58 -07001059 SLOGE("Error parsing trim.txt, line: %s", lineStr);
Geoffrey Pitschdd214a72016-06-27 17:14:30 -04001060 break;
1061 }
1062 }
1063 }
1064
1065 zip->endIteration(cookie);
Narayan Kamathafd31e02013-12-03 13:16:03 +00001066
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001067 return true;
1068}
1069
Marin Shalamanov1a6e36892020-05-18 19:05:17 +02001070bool BootAnimation::movie() {
Huihong Luo50a2f362018-08-11 09:27:57 -07001071 if (mAnimation == nullptr) {
1072 mAnimation = loadAnimation(mZipFileName);
1073 }
1074
1075 if (mAnimation == nullptr)
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001076 return false;
1077
Huihong Luo50a2f362018-08-11 09:27:57 -07001078 // mCallbacks->init() may get called recursively,
1079 // this loop is needed to get the same results
1080 for (const Animation::Part& part : mAnimation->parts) {
1081 if (part.animation != nullptr) {
1082 mCallbacks->init(part.animation->parts);
1083 }
1084 }
1085 mCallbacks->init(mAnimation->parts);
1086
Damien Bargiacchi97480862016-03-29 14:55:55 -07001087 bool anyPartHasClock = false;
Huihong Luo50a2f362018-08-11 09:27:57 -07001088 for (size_t i=0; i < mAnimation->parts.size(); i++) {
1089 if(validClock(mAnimation->parts[i])) {
Damien Bargiacchi97480862016-03-29 14:55:55 -07001090 anyPartHasClock = true;
1091 break;
1092 }
1093 }
1094 if (!anyPartHasClock) {
1095 mClockEnabled = false;
1096 }
1097
Sai Kiran Korwar27167492015-07-07 20:00:06 +05301098 // Check if npot textures are supported
1099 mUseNpotTextures = false;
1100 String8 gl_extensions;
1101 const char* exts = reinterpret_cast<const char*>(glGetString(GL_EXTENSIONS));
1102 if (!exts) {
1103 glGetError();
1104 } else {
1105 gl_extensions.setTo(exts);
1106 if ((gl_extensions.find("GL_ARB_texture_non_power_of_two") != -1) ||
1107 (gl_extensions.find("GL_OES_texture_npot") != -1)) {
1108 mUseNpotTextures = true;
1109 }
1110 }
1111
Damien Bargiacchia704b7d2016-02-16 16:55:49 -08001112 // Blend required to draw time on top of animation frames.
1113 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
Mathias Agopiana8826d62009-10-01 03:10:14 -07001114 glShadeModel(GL_FLAT);
1115 glDisable(GL_DITHER);
1116 glDisable(GL_SCISSOR_TEST);
1117 glDisable(GL_BLEND);
Mathias Agopiana8826d62009-10-01 03:10:14 -07001118
1119 glBindTexture(GL_TEXTURE_2D, 0);
1120 glEnable(GL_TEXTURE_2D);
1121 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
1122 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
1123 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
1124 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
1125 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
1126
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -07001127 bool clockFontInitialized = false;
Damien Bargiacchia704b7d2016-02-16 16:55:49 -08001128 if (mClockEnabled) {
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -07001129 clockFontInitialized =
Huihong Luo50a2f362018-08-11 09:27:57 -07001130 (initFont(&mAnimation->clockFont, CLOCK_FONT_ASSET) == NO_ERROR);
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -07001131 mClockEnabled = clockFontInitialized;
Damien Bargiacchia704b7d2016-02-16 16:55:49 -08001132 }
1133
Damien Bargiacchi97480862016-03-29 14:55:55 -07001134 if (mClockEnabled && !updateIsTimeAccurate()) {
1135 mTimeCheckThread = new TimeCheckThread(this);
1136 mTimeCheckThread->run("BootAnimation::TimeCheckThread", PRIORITY_NORMAL);
1137 }
1138
Huihong Luo50a2f362018-08-11 09:27:57 -07001139 playAnimation(*mAnimation);
Damien Bargiacchi97480862016-03-29 14:55:55 -07001140
Geoffrey Pitscha9173532016-07-19 14:56:39 -04001141 if (mTimeCheckThread != nullptr) {
Damien Bargiacchi97480862016-03-29 14:55:55 -07001142 mTimeCheckThread->requestExit();
Geoffrey Pitscha9173532016-07-19 14:56:39 -04001143 mTimeCheckThread = nullptr;
1144 }
1145
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -07001146 if (clockFontInitialized) {
Huihong Luo50a2f362018-08-11 09:27:57 -07001147 glDeleteTextures(1, &mAnimation->clockFont.texture.name);
Andriy Naborskyy815e51d2016-03-24 16:43:34 -07001148 }
1149
Huihong Luo50a2f362018-08-11 09:27:57 -07001150 releaseAnimation(mAnimation);
1151 mAnimation = nullptr;
Damien Bargiacchi1a8a2132018-07-24 15:20:26 -07001152
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001153 return false;
1154}
1155
Yegor Malysheveafcb992020-09-10 16:42:20 +02001156bool BootAnimation::shouldStopPlayingPart(const Animation::Part& part, const int fadedFramesCount) {
1157 // stop playing only if it is time to exit and it's a partial part which has been faded out
1158 return exitPending() && !part.playUntilComplete && fadedFramesCount >= part.framesToFadeCount;
1159}
1160
Marin Shalamanov1a6e36892020-05-18 19:05:17 +02001161bool BootAnimation::playAnimation(const Animation& animation) {
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001162 const size_t pcount = animation.parts.size();
Mathias Agopiana8826d62009-10-01 03:10:14 -07001163 nsecs_t frameDuration = s2ns(1) / animation.fps;
Mathias Agopian9f3020d2009-11-06 16:30:18 -08001164
Wei Wang159a4102018-10-23 23:15:58 -07001165 SLOGD("%sAnimationShownTiming start time: %" PRId64 "ms", mShuttingDown ? "Shutdown" : "Boot",
Keun-young Parkd1794cd2017-04-04 12:21:19 -07001166 elapsedRealtime());
Yegor Malysheveafcb992020-09-10 16:42:20 +02001167
1168 int fadedFramesCount = 0;
Narayan Kamathafd31e02013-12-03 13:16:03 +00001169 for (size_t i=0 ; i<pcount ; i++) {
Mathias Agopiana8826d62009-10-01 03:10:14 -07001170 const Animation::Part& part(animation.parts[i]);
1171 const size_t fcount = part.frames.size();
1172 glBindTexture(GL_TEXTURE_2D, 0);
1173
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001174 // Handle animation package
Yi Kong911ac232019-03-24 01:49:02 -07001175 if (part.animation != nullptr) {
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001176 playAnimation(*part.animation);
1177 if (exitPending())
1178 break;
1179 continue; //to next part
1180 }
1181
Yegor Malysheveafcb992020-09-10 16:42:20 +02001182 // process the part not only while the count allows but also if already fading
1183 for (int r=0 ; !part.count || r<part.count || fadedFramesCount > 0 ; r++) {
1184 if (shouldStopPlayingPart(part, fadedFramesCount)) break;
Kevin Hesterd3782b22012-04-26 10:38:55 -07001185
Ed Coyne7464ac92017-06-08 12:26:48 -07001186 mCallbacks->playPart(i, part, r);
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -07001187
Jesse Hall083b84c2014-09-22 10:51:09 -07001188 glClearColor(
1189 part.backgroundColor[0],
1190 part.backgroundColor[1],
1191 part.backgroundColor[2],
1192 1.0f);
1193
Yegor Malysheveafcb992020-09-10 16:42:20 +02001194 for (size_t j=0 ; j<fcount ; j++) {
1195 if (shouldStopPlayingPart(part, fadedFramesCount)) break;
1196
Marin Shalamanov1a6e36892020-05-18 19:05:17 +02001197 processDisplayEvents();
1198
1199 const int animationX = (mWidth - animation.width) / 2;
1200 const int animationY = (mHeight - animation.height) / 2;
1201
Mathias Agopiana8826d62009-10-01 03:10:14 -07001202 const Animation::Frame& frame(part.frames[j]);
Mathias Agopiandb7dd2a2012-05-12 15:08:21 -07001203 nsecs_t lastFrame = systemTime();
Mathias Agopiana8826d62009-10-01 03:10:14 -07001204
1205 if (r > 0) {
1206 glBindTexture(GL_TEXTURE_2D, frame.tid);
1207 } else {
1208 if (part.count != 1) {
1209 glGenTextures(1, &frame.tid);
1210 glBindTexture(GL_TEXTURE_2D, frame.tid);
1211 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
1212 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
1213 }
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -07001214 int w, h;
1215 initTexture(frame.map, &w, &h);
Mathias Agopiana8826d62009-10-01 03:10:14 -07001216 }
1217
Geoffrey Pitschdd214a72016-06-27 17:14:30 -04001218 const int xc = animationX + frame.trimX;
1219 const int yc = animationY + frame.trimY;
1220 Region clearReg(Rect(mWidth, mHeight));
1221 clearReg.subtractSelf(Rect(xc, yc, xc+frame.trimWidth, yc+frame.trimHeight));
Mathias Agopian9f3020d2009-11-06 16:30:18 -08001222 if (!clearReg.isEmpty()) {
1223 Region::const_iterator head(clearReg.begin());
1224 Region::const_iterator tail(clearReg.end());
1225 glEnable(GL_SCISSOR_TEST);
1226 while (head != tail) {
Andreas Gampecfedceb2014-09-30 21:48:18 -07001227 const Rect& r2(*head++);
Geoffrey Pitschdd214a72016-06-27 17:14:30 -04001228 glScissor(r2.left, mHeight - r2.bottom, r2.width(), r2.height());
Mathias Agopian9f3020d2009-11-06 16:30:18 -08001229 glClear(GL_COLOR_BUFFER_BIT);
1230 }
1231 glDisable(GL_SCISSOR_TEST);
1232 }
Geoffrey Pitschdd214a72016-06-27 17:14:30 -04001233 // specify the y center as ceiling((mHeight - frame.trimHeight) / 2)
1234 // which is equivalent to mHeight - (yc + frame.trimHeight)
Yegor Malysheveafcb992020-09-10 16:42:20 +02001235 const int frameDrawY = mHeight - (yc + frame.trimHeight);
1236 glDrawTexiOES(xc, frameDrawY, 0, frame.trimWidth, frame.trimHeight);
1237
1238 // if the part hasn't been stopped yet then continue fading if necessary
1239 if (exitPending() && part.hasFadingPhase()) {
1240 fadeFrame(xc, frameDrawY, frame.trimWidth, frame.trimHeight, part,
1241 ++fadedFramesCount);
1242 if (fadedFramesCount >= part.framesToFadeCount) {
1243 fadedFramesCount = MAX_FADED_FRAMES_COUNT; // no more fading
1244 }
1245 }
1246
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -07001247 if (mClockEnabled && mTimeIsAccurate && validClock(part)) {
1248 drawClock(animation.clockFont, part.clockPosX, part.clockPosY);
Damien Bargiacchia704b7d2016-02-16 16:55:49 -08001249 }
Yegor Malysheveafcb992020-09-10 16:42:20 +02001250
Adrian Roos9ee5dff2018-08-22 20:19:49 +02001251 handleViewport(frameDuration);
Damien Bargiacchia704b7d2016-02-16 16:55:49 -08001252
Mathias Agopiana8826d62009-10-01 03:10:14 -07001253 eglSwapBuffers(mDisplay, mSurface);
1254
1255 nsecs_t now = systemTime();
1256 nsecs_t delay = frameDuration - (now - lastFrame);
Wei Wang159a4102018-10-23 23:15:58 -07001257 //SLOGD("%lld, %lld", ns2ms(now - lastFrame), ns2ms(delay));
Mathias Agopiana8826d62009-10-01 03:10:14 -07001258 lastFrame = now;
Mathias Agopiandb7dd2a2012-05-12 15:08:21 -07001259
1260 if (delay > 0) {
1261 struct timespec spec;
1262 spec.tv_sec = (now + delay) / 1000000000;
1263 spec.tv_nsec = (now + delay) % 1000000000;
1264 int err;
1265 do {
Yi Kong911ac232019-03-24 01:49:02 -07001266 err = clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &spec, nullptr);
Mathias Agopiandb7dd2a2012-05-12 15:08:21 -07001267 } while (err<0 && errno == EINTR);
1268 }
Kevin Hesterd3782b22012-04-26 10:38:55 -07001269
1270 checkExit();
Mathias Agopiana8826d62009-10-01 03:10:14 -07001271 }
Kevin Hesterd3782b22012-04-26 10:38:55 -07001272
Mathias Agopiana8826d62009-10-01 03:10:14 -07001273 usleep(part.pause * ns2us(frameDuration));
Kevin Hesterd3782b22012-04-26 10:38:55 -07001274
Yegor Malysheveafcb992020-09-10 16:42:20 +02001275 if (exitPending() && !part.count && mCurrentInset >= mTargetInset &&
1276 !part.hasFadingPhase()) {
1277 break; // exit the infinite non-fading part when it has been played at least once
1278 }
Mathias Agopiana8826d62009-10-01 03:10:14 -07001279 }
Geoffrey Pitsch2fb30fb2016-07-06 16:16:20 -04001280 }
1281
1282 // Free textures created for looping parts now that the animation is done.
1283 for (const Animation::Part& part : animation.parts) {
Mathias Agopiana8826d62009-10-01 03:10:14 -07001284 if (part.count != 1) {
Geoffrey Pitsch2fb30fb2016-07-06 16:16:20 -04001285 const size_t fcount = part.frames.size();
1286 for (size_t j = 0; j < fcount; j++) {
Mathias Agopiana8826d62009-10-01 03:10:14 -07001287 const Animation::Frame& frame(part.frames[j]);
1288 glDeleteTextures(1, &frame.tid);
1289 }
1290 }
1291 }
Geoffrey Pitschd6d9a1d2016-06-08 00:38:58 -07001292
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001293 return true;
Mathias Agopiana8826d62009-10-01 03:10:14 -07001294}
1295
Marin Shalamanov1a6e36892020-05-18 19:05:17 +02001296void BootAnimation::processDisplayEvents() {
1297 // This will poll mDisplayEventReceiver and if there are new events it'll call
1298 // displayEventCallback synchronously.
1299 mLooper->pollOnce(0);
1300}
1301
Adrian Roos9ee5dff2018-08-22 20:19:49 +02001302void BootAnimation::handleViewport(nsecs_t timestep) {
1303 if (mShuttingDown || !mFlingerSurfaceControl || mTargetInset == 0) {
1304 return;
1305 }
1306 if (mTargetInset < 0) {
1307 // Poll the amount for the top display inset. This will return -1 until persistent properties
1308 // have been loaded.
1309 mTargetInset = android::base::GetIntProperty("persist.sys.displayinset.top",
1310 -1 /* default */, -1 /* min */, mHeight / 2 /* max */);
1311 }
1312 if (mTargetInset <= 0) {
1313 return;
1314 }
1315
1316 if (mCurrentInset < mTargetInset) {
1317 // After the device boots, the inset will effectively be cropped away. We animate this here.
1318 float fraction = static_cast<float>(mCurrentInset) / mTargetInset;
1319 int interpolatedInset = (cosf((fraction + 1) * M_PI) / 2.0f + 0.5f) * mTargetInset;
1320
1321 SurfaceComposerClient::Transaction()
1322 .setCrop(mFlingerSurfaceControl, Rect(0, interpolatedInset, mWidth, mHeight))
1323 .apply();
1324 } else {
1325 // At the end of the animation, we switch to the viewport that DisplayManager will apply
1326 // later. This changes the coordinate system, and means we must move the surface up by
1327 // the inset amount.
Adrian Roos9ee5dff2018-08-22 20:19:49 +02001328 Rect layerStackRect(0, 0, mWidth, mHeight - mTargetInset);
1329 Rect displayRect(0, mTargetInset, mWidth, mHeight);
1330
1331 SurfaceComposerClient::Transaction t;
1332 t.setPosition(mFlingerSurfaceControl, 0, -mTargetInset)
1333 .setCrop(mFlingerSurfaceControl, Rect(0, mTargetInset, mWidth, mHeight));
Dominik Laskowski2a3d9aa2019-11-18 20:26:39 -08001334 t.setDisplayProjection(mDisplayToken, ui::ROTATION_0, layerStackRect, displayRect);
Adrian Roos9ee5dff2018-08-22 20:19:49 +02001335 t.apply();
1336
1337 mTargetInset = mCurrentInset = 0;
1338 }
1339
1340 int delta = timestep * mTargetInset / ms2ns(200);
1341 mCurrentInset += delta;
1342}
1343
Marin Shalamanov1a6e36892020-05-18 19:05:17 +02001344void BootAnimation::releaseAnimation(Animation* animation) const {
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001345 for (Vector<Animation::Part>::iterator it = animation->parts.begin(),
1346 e = animation->parts.end(); it != e; ++it) {
1347 if (it->animation)
1348 releaseAnimation(it->animation);
1349 }
1350 if (animation->zip)
1351 delete animation->zip;
1352 delete animation;
1353}
1354
Marin Shalamanov1a6e36892020-05-18 19:05:17 +02001355BootAnimation::Animation* BootAnimation::loadAnimation(const String8& fn) {
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001356 if (mLoadedFiles.indexOf(fn) >= 0) {
Wei Wang159a4102018-10-23 23:15:58 -07001357 SLOGE("File \"%s\" is already loaded. Cyclic ref is not allowed",
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001358 fn.string());
Yi Kong911ac232019-03-24 01:49:02 -07001359 return nullptr;
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001360 }
1361 ZipFileRO *zip = ZipFileRO::open(fn);
Yi Kong911ac232019-03-24 01:49:02 -07001362 if (zip == nullptr) {
Wei Wang159a4102018-10-23 23:15:58 -07001363 SLOGE("Failed to open animation zip \"%s\": %s",
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001364 fn.string(), strerror(errno));
Yi Kong911ac232019-03-24 01:49:02 -07001365 return nullptr;
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001366 }
1367
1368 Animation *animation = new Animation;
1369 animation->fileName = fn;
1370 animation->zip = zip;
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -07001371 animation->clockFont.map = nullptr;
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001372 mLoadedFiles.add(animation->fileName);
1373
1374 parseAnimationDesc(*animation);
Geoffrey Pitscha91a2d72016-07-12 14:46:19 -04001375 if (!preloadZip(*animation)) {
Greg Kaiser7426ec82019-09-11 14:34:27 -07001376 releaseAnimation(animation);
Yi Kong911ac232019-03-24 01:49:02 -07001377 return nullptr;
Geoffrey Pitscha91a2d72016-07-12 14:46:19 -04001378 }
1379
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001380 mLoadedFiles.remove(fn);
1381 return animation;
1382}
Damien Bargiacchi97480862016-03-29 14:55:55 -07001383
1384bool BootAnimation::updateIsTimeAccurate() {
1385 static constexpr long long MAX_TIME_IN_PAST = 60000LL * 60LL * 24LL * 30LL; // 30 days
1386 static constexpr long long MAX_TIME_IN_FUTURE = 60000LL * 90LL; // 90 minutes
1387
1388 if (mTimeIsAccurate) {
1389 return true;
1390 }
Keun-young Parkb5938422017-03-23 13:46:24 -07001391 if (mShuttingDown) return true;
Damien Bargiacchi97480862016-03-29 14:55:55 -07001392 struct stat statResult;
Damien Bargiacchi9071db12016-10-28 17:38:22 -07001393
1394 if(stat(TIME_FORMAT_12_HOUR_FLAG_FILE_PATH, &statResult) == 0) {
1395 mTimeFormat12Hour = true;
1396 }
1397
Damien Bargiacchi97480862016-03-29 14:55:55 -07001398 if(stat(ACCURATE_TIME_FLAG_FILE_PATH, &statResult) == 0) {
1399 mTimeIsAccurate = true;
1400 return true;
1401 }
1402
1403 FILE* file = fopen(LAST_TIME_CHANGED_FILE_PATH, "r");
Yi Kong911ac232019-03-24 01:49:02 -07001404 if (file != nullptr) {
Damien Bargiacchi97480862016-03-29 14:55:55 -07001405 long long lastChangedTime = 0;
1406 fscanf(file, "%lld", &lastChangedTime);
1407 fclose(file);
1408 if (lastChangedTime > 0) {
1409 struct timespec now;
1410 clock_gettime(CLOCK_REALTIME, &now);
1411 // Match the Java timestamp format
1412 long long rtcNow = (now.tv_sec * 1000LL) + (now.tv_nsec / 1000000LL);
Damien Bargiacchi96762812016-07-12 15:53:40 -07001413 if (ACCURATE_TIME_EPOCH < rtcNow
1414 && lastChangedTime > (rtcNow - MAX_TIME_IN_PAST)
1415 && lastChangedTime < (rtcNow + MAX_TIME_IN_FUTURE)) {
Damien Bargiacchi97480862016-03-29 14:55:55 -07001416 mTimeIsAccurate = true;
1417 }
1418 }
1419 }
1420
1421 return mTimeIsAccurate;
1422}
1423
1424BootAnimation::TimeCheckThread::TimeCheckThread(BootAnimation* bootAnimation) : Thread(false),
1425 mInotifyFd(-1), mSystemWd(-1), mTimeWd(-1), mBootAnimation(bootAnimation) {}
1426
1427BootAnimation::TimeCheckThread::~TimeCheckThread() {
1428 // mInotifyFd may be -1 but that's ok since we're not at risk of attempting to close a valid FD.
1429 close(mInotifyFd);
1430}
1431
1432bool BootAnimation::TimeCheckThread::threadLoop() {
1433 bool shouldLoop = doThreadLoop() && !mBootAnimation->mTimeIsAccurate
1434 && mBootAnimation->mClockEnabled;
1435 if (!shouldLoop) {
1436 close(mInotifyFd);
1437 mInotifyFd = -1;
1438 }
1439 return shouldLoop;
1440}
1441
1442bool BootAnimation::TimeCheckThread::doThreadLoop() {
1443 static constexpr int BUFF_LEN (10 * (sizeof(struct inotify_event) + NAME_MAX + 1));
1444
1445 // Poll instead of doing a blocking read so the Thread can exit if requested.
1446 struct pollfd pfd = { mInotifyFd, POLLIN, 0 };
1447 ssize_t pollResult = poll(&pfd, 1, 1000);
1448
1449 if (pollResult == 0) {
1450 return true;
1451 } else if (pollResult < 0) {
Wei Wang159a4102018-10-23 23:15:58 -07001452 SLOGE("Could not poll inotify events");
Damien Bargiacchi97480862016-03-29 14:55:55 -07001453 return false;
1454 }
1455
1456 char buff[BUFF_LEN] __attribute__ ((aligned(__alignof__(struct inotify_event))));;
1457 ssize_t length = read(mInotifyFd, buff, BUFF_LEN);
1458 if (length == 0) {
1459 return true;
1460 } else if (length < 0) {
Wei Wang159a4102018-10-23 23:15:58 -07001461 SLOGE("Could not read inotify events");
Damien Bargiacchi97480862016-03-29 14:55:55 -07001462 return false;
1463 }
1464
1465 const struct inotify_event *event;
1466 for (char* ptr = buff; ptr < buff + length; ptr += sizeof(struct inotify_event) + event->len) {
1467 event = (const struct inotify_event *) ptr;
1468 if (event->wd == mSystemWd && strcmp(SYSTEM_TIME_DIR_NAME, event->name) == 0) {
1469 addTimeDirWatch();
1470 } else if (event->wd == mTimeWd && (strcmp(LAST_TIME_CHANGED_FILE_NAME, event->name) == 0
1471 || strcmp(ACCURATE_TIME_FLAG_FILE_NAME, event->name) == 0)) {
1472 return !mBootAnimation->updateIsTimeAccurate();
1473 }
1474 }
1475
1476 return true;
1477}
1478
1479void BootAnimation::TimeCheckThread::addTimeDirWatch() {
1480 mTimeWd = inotify_add_watch(mInotifyFd, SYSTEM_TIME_DIR_PATH,
1481 IN_CLOSE_WRITE | IN_MOVED_TO | IN_ATTRIB);
1482 if (mTimeWd > 0) {
1483 // No need to watch for the time directory to be created if it already exists
1484 inotify_rm_watch(mInotifyFd, mSystemWd);
1485 mSystemWd = -1;
1486 }
1487}
1488
1489status_t BootAnimation::TimeCheckThread::readyToRun() {
1490 mInotifyFd = inotify_init();
1491 if (mInotifyFd < 0) {
Wei Wang159a4102018-10-23 23:15:58 -07001492 SLOGE("Could not initialize inotify fd");
Damien Bargiacchi97480862016-03-29 14:55:55 -07001493 return NO_INIT;
1494 }
1495
1496 mSystemWd = inotify_add_watch(mInotifyFd, SYSTEM_DATA_DIR_PATH, IN_CREATE | IN_ATTRIB);
1497 if (mSystemWd < 0) {
1498 close(mInotifyFd);
1499 mInotifyFd = -1;
Dan Sandler562d46d2020-05-11 17:09:22 -04001500 SLOGE("Could not add watch for %s: %s", SYSTEM_DATA_DIR_PATH, strerror(errno));
Damien Bargiacchi97480862016-03-29 14:55:55 -07001501 return NO_INIT;
1502 }
1503
1504 addTimeDirWatch();
1505
1506 if (mBootAnimation->updateIsTimeAccurate()) {
1507 close(mInotifyFd);
1508 mInotifyFd = -1;
1509 return ALREADY_EXISTS;
1510 }
1511
1512 return NO_ERROR;
1513}
1514
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001515// ---------------------------------------------------------------------------
1516
Marin Shalamanov1a6e36892020-05-18 19:05:17 +02001517} // namespace android