blob: 30f4ad4c5c6908ae1b28272fb23b04cdd0c10618 [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
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080020#include <stdint.h>
Wei Wanga90c54c2017-02-02 11:35:21 -080021#include <inttypes.h>
Damien Bargiacchi97480862016-03-29 14:55:55 -070022#include <sys/inotify.h>
23#include <sys/poll.h>
24#include <sys/stat.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080025#include <sys/types.h>
26#include <math.h>
27#include <fcntl.h>
28#include <utils/misc.h>
Mathias Agopianb4d5a722009-09-23 17:05:19 -070029#include <signal.h>
Elliott Hughesbb94f312014-10-21 10:41:33 -070030#include <time.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080031
Steven Morelandfb7952f2018-02-23 14:58:50 -080032#include <cutils/atomic.h>
Jason parksbd9a08d2011-01-31 15:04:34 -060033#include <cutils/properties.h>
34
Mathias Agopianb13b9bd2012-02-17 18:27:36 -080035#include <androidfw/AssetManager.h>
Mathias Agopianac31a3b2009-05-21 19:59:24 -070036#include <binder/IPCThreadState.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080037#include <utils/Errors.h>
38#include <utils/Log.h>
Wei Wanga90c54c2017-02-02 11:35:21 -080039#include <utils/SystemClock.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080040
Keun-young Parkb5938422017-03-23 13:46:24 -070041#include <android-base/properties.h>
42
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080043#include <ui/PixelFormat.h>
44#include <ui/Rect.h>
45#include <ui/Region.h>
46#include <ui/DisplayInfo.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080047
Jeff Brown0b722fe2012-08-24 22:40:14 -070048#include <gui/ISurfaceComposer.h>
Mathias Agopian8335f1c2012-02-25 18:48:35 -080049#include <gui/Surface.h>
50#include <gui/SurfaceComposerClient.h>
Mathias Agopian000479f2010-02-09 17:46:37 -080051
Andreas Gampecfedceb2014-09-30 21:48:18 -070052// TODO: Fix Skia.
53#pragma GCC diagnostic push
54#pragma GCC diagnostic ignored "-Wunused-parameter"
Derek Sollenbergereece0dd2014-02-27 14:31:29 -050055#include <SkBitmap.h>
Matt Sarett8898c162016-03-24 16:11:01 -040056#include <SkImage.h>
Derek Sollenbergereece0dd2014-02-27 14:31:29 -050057#include <SkStream.h>
Andreas Gampecfedceb2014-09-30 21:48:18 -070058#pragma GCC diagnostic pop
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080059
60#include <GLES/gl.h>
61#include <GLES/glext.h>
62#include <EGL/eglext.h>
63
64#include "BootAnimation.h"
65
66namespace android {
67
Damien Bargiacchi97480862016-03-29 14:55:55 -070068static const char OEM_BOOTANIMATION_FILE[] = "/oem/media/bootanimation.zip";
Beverly34ffe252019-05-17 11:03:54 -040069static const char PRODUCT_BOOTANIMATION_DARK_FILE[] = "/product/media/bootanimation-dark.zip";
Jaekyun Seokc521bb32018-01-30 11:52:47 +090070static const char PRODUCT_BOOTANIMATION_FILE[] = "/product/media/bootanimation.zip";
Damien Bargiacchi97480862016-03-29 14:55:55 -070071static const char SYSTEM_BOOTANIMATION_FILE[] = "/system/media/bootanimation.zip";
Anders Fridlunde0b4d232018-11-06 14:23:25 +010072static const char APEX_BOOTANIMATION_FILE[] = "/apex/com.android.bootanimation/etc/bootanimation.zip";
Jaekyun Seokc521bb32018-01-30 11:52:47 +090073static const char PRODUCT_ENCRYPTED_BOOTANIMATION_FILE[] = "/product/media/bootanimation-encrypted.zip";
Damien Bargiacchi97480862016-03-29 14:55:55 -070074static const char SYSTEM_ENCRYPTED_BOOTANIMATION_FILE[] = "/system/media/bootanimation-encrypted.zip";
Keun-young Parkb5938422017-03-23 13:46:24 -070075static const char OEM_SHUTDOWNANIMATION_FILE[] = "/oem/media/shutdownanimation.zip";
Jaekyun Seokc521bb32018-01-30 11:52:47 +090076static const char PRODUCT_SHUTDOWNANIMATION_FILE[] = "/product/media/shutdownanimation.zip";
Keun-young Parkb5938422017-03-23 13:46:24 -070077static const char SYSTEM_SHUTDOWNANIMATION_FILE[] = "/system/media/shutdownanimation.zip";
78
Damien Bargiacchi97480862016-03-29 14:55:55 -070079static const char SYSTEM_DATA_DIR_PATH[] = "/data/system";
80static const char SYSTEM_TIME_DIR_NAME[] = "time";
81static const char SYSTEM_TIME_DIR_PATH[] = "/data/system/time";
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -070082static const char CLOCK_FONT_ASSET[] = "images/clock_font.png";
83static const char CLOCK_FONT_ZIP_NAME[] = "clock_font.png";
Damien Bargiacchi97480862016-03-29 14:55:55 -070084static const char LAST_TIME_CHANGED_FILE_NAME[] = "last_time_change";
85static const char LAST_TIME_CHANGED_FILE_PATH[] = "/data/system/time/last_time_change";
86static const char ACCURATE_TIME_FLAG_FILE_NAME[] = "time_is_accurate";
87static const char ACCURATE_TIME_FLAG_FILE_PATH[] = "/data/system/time/time_is_accurate";
Damien Bargiacchi9071db12016-10-28 17:38:22 -070088static const char TIME_FORMAT_12_HOUR_FLAG_FILE_PATH[] = "/data/system/time/time_format_12_hour";
Damien Bargiacchi96762812016-07-12 15:53:40 -070089// Java timestamp format. Don't show the clock if the date is before 2000-01-01 00:00:00.
90static const long long ACCURATE_TIME_EPOCH = 946684800000;
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -070091static constexpr char FONT_BEGIN_CHAR = ' ';
92static constexpr char FONT_END_CHAR = '~' + 1;
93static constexpr size_t FONT_NUM_CHARS = FONT_END_CHAR - FONT_BEGIN_CHAR + 1;
94static constexpr size_t FONT_NUM_COLS = 16;
95static constexpr size_t FONT_NUM_ROWS = FONT_NUM_CHARS / FONT_NUM_COLS;
96static const int TEXT_CENTER_VALUE = INT_MAX;
97static const int TEXT_MISSING_VALUE = INT_MIN;
Damien Bargiacchi97480862016-03-29 14:55:55 -070098static const char EXIT_PROP_NAME[] = "service.bootanim.exit";
Narayan Kamathafd31e02013-12-03 13:16:03 +000099static const int ANIM_ENTRY_NAME_MAX = 256;
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700100static constexpr size_t TEXT_POS_LEN_MAX = 16;
Narayan Kamathafd31e02013-12-03 13:16:03 +0000101
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800102// ---------------------------------------------------------------------------
103
Ed Coyne7464ac92017-06-08 12:26:48 -0700104BootAnimation::BootAnimation(sp<Callbacks> callbacks)
Ed Coyne2c9e94a2017-05-31 10:08:28 -0700105 : Thread(false), mClockEnabled(true), mTimeIsAccurate(false),
Yi Kong911ac232019-03-24 01:49:02 -0700106 mTimeFormat12Hour(false), mTimeCheckThread(nullptr), mCallbacks(callbacks) {
Mathias Agopian627e7b52009-05-21 19:21:59 -0700107 mSession = new SurfaceComposerClient();
Geoffrey Pitsch290c4352016-08-09 14:35:10 -0400108
Keun-young Parkb5938422017-03-23 13:46:24 -0700109 std::string powerCtl = android::base::GetProperty("sys.powerctl", "");
110 if (powerCtl.empty()) {
111 mShuttingDown = false;
112 } else {
113 mShuttingDown = true;
114 }
Huihong Luo50a2f362018-08-11 09:27:57 -0700115 ALOGD("%sAnimationStartTiming start time: %" PRId64 "ms", mShuttingDown ? "Shutdown" : "Boot",
116 elapsedRealtime());
117}
118
119BootAnimation::~BootAnimation() {
120 if (mAnimation != nullptr) {
121 releaseAnimation(mAnimation);
122 mAnimation = nullptr;
123 }
124 ALOGD("%sAnimationStopTiming start time: %" PRId64 "ms", mShuttingDown ? "Shutdown" : "Boot",
125 elapsedRealtime());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800126}
127
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800128void BootAnimation::onFirstRef() {
Mathias Agopianbc726112009-09-23 15:44:05 -0700129 status_t err = mSession->linkToComposerDeath(this);
Wei Wang159a4102018-10-23 23:15:58 -0700130 SLOGE_IF(err, "linkToComposerDeath failed (%s) ", strerror(-err));
Mathias Agopian8434c532009-09-23 18:52:49 -0700131 if (err == NO_ERROR) {
Huihong Luo50a2f362018-08-11 09:27:57 -0700132 // Load the animation content -- this can be slow (eg 200ms)
133 // called before waitForSurfaceFlinger() in main() to avoid wait
134 ALOGD("%sAnimationPreloadTiming start time: %" PRId64 "ms",
135 mShuttingDown ? "Shutdown" : "Boot", elapsedRealtime());
136 preloadAnimation();
137 ALOGD("%sAnimationPreloadStopTiming start time: %" PRId64 "ms",
138 mShuttingDown ? "Shutdown" : "Boot", elapsedRealtime());
Mathias Agopianbc726112009-09-23 15:44:05 -0700139 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800140}
141
Mathias Agopianbc726112009-09-23 15:44:05 -0700142sp<SurfaceComposerClient> BootAnimation::session() const {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800143 return mSession;
144}
145
Narayan Kamathafd31e02013-12-03 13:16:03 +0000146void BootAnimation::binderDied(const wp<IBinder>&)
Mathias Agopianbc726112009-09-23 15:44:05 -0700147{
148 // woah, surfaceflinger died!
Wei Wang159a4102018-10-23 23:15:58 -0700149 SLOGD("SurfaceFlinger died, exiting...");
Mathias Agopianbc726112009-09-23 15:44:05 -0700150
151 // calling requestExit() is not enough here because the Surface code
152 // might be blocked on a condition variable that will never be updated.
153 kill( getpid(), SIGKILL );
154 requestExit();
155}
156
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800157status_t BootAnimation::initTexture(Texture* texture, AssetManager& assets,
158 const char* name) {
159 Asset* asset = assets.open(name, Asset::ACCESS_BUFFER);
Yi Kong911ac232019-03-24 01:49:02 -0700160 if (asset == nullptr)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800161 return NO_INIT;
162 SkBitmap bitmap;
Matt Sarett8898c162016-03-24 16:11:01 -0400163 sk_sp<SkData> data = SkData::MakeWithoutCopy(asset->getBuffer(false),
164 asset->getLength());
165 sk_sp<SkImage> image = SkImage::MakeFromEncoded(data);
166 image->asLegacyBitmap(&bitmap, SkImage::kRO_LegacyBitmapMode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800167 asset->close();
168 delete asset;
169
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800170 const int w = bitmap.width();
171 const int h = bitmap.height();
172 const void* p = bitmap.getPixels();
173
174 GLint crop[4] = { 0, h, w, -h };
175 texture->w = w;
176 texture->h = h;
177
178 glGenTextures(1, &texture->name);
179 glBindTexture(GL_TEXTURE_2D, texture->name);
180
Mike Reed42a1d082014-07-07 18:06:18 -0400181 switch (bitmap.colorType()) {
182 case kAlpha_8_SkColorType:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800183 glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, w, h, 0, GL_ALPHA,
184 GL_UNSIGNED_BYTE, p);
185 break;
Mike Reed42a1d082014-07-07 18:06:18 -0400186 case kARGB_4444_SkColorType:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800187 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA,
188 GL_UNSIGNED_SHORT_4_4_4_4, p);
189 break;
Mike Reed42a1d082014-07-07 18:06:18 -0400190 case kN32_SkColorType:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800191 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA,
192 GL_UNSIGNED_BYTE, p);
193 break;
Mike Reed42a1d082014-07-07 18:06:18 -0400194 case kRGB_565_SkColorType:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800195 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, w, h, 0, GL_RGB,
196 GL_UNSIGNED_SHORT_5_6_5, p);
197 break;
198 default:
199 break;
200 }
201
202 glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_CROP_RECT_OES, crop);
203 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
204 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
205 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
206 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700207
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800208 return NO_ERROR;
209}
210
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700211status_t BootAnimation::initTexture(FileMap* map, int* width, int* height)
Mathias Agopiana8826d62009-10-01 03:10:14 -0700212{
Mathias Agopiana8826d62009-10-01 03:10:14 -0700213 SkBitmap bitmap;
Damien Bargiacchif67b3172016-09-07 20:17:14 -0700214 sk_sp<SkData> data = SkData::MakeWithoutCopy(map->getDataPtr(),
215 map->getDataLength());
Matt Sarett8898c162016-03-24 16:11:01 -0400216 sk_sp<SkImage> image = SkImage::MakeFromEncoded(data);
217 image->asLegacyBitmap(&bitmap, SkImage::kRO_LegacyBitmapMode);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700218
Mykola Kondratenko0c1eeb32014-04-15 09:35:44 +0200219 // FileMap memory is never released until application exit.
220 // Release it now as the texture is already loaded and the memory used for
221 // the packed resource can be released.
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700222 delete map;
Mykola Kondratenko0c1eeb32014-04-15 09:35:44 +0200223
Mathias Agopiana8826d62009-10-01 03:10:14 -0700224 const int w = bitmap.width();
225 const int h = bitmap.height();
226 const void* p = bitmap.getPixels();
227
228 GLint crop[4] = { 0, h, w, -h };
229 int tw = 1 << (31 - __builtin_clz(w));
230 int th = 1 << (31 - __builtin_clz(h));
231 if (tw < w) tw <<= 1;
232 if (th < h) th <<= 1;
233
Mike Reed42a1d082014-07-07 18:06:18 -0400234 switch (bitmap.colorType()) {
235 case kN32_SkColorType:
Sai Kiran Korwar27167492015-07-07 20:00:06 +0530236 if (!mUseNpotTextures && (tw != w || th != h)) {
Mathias Agopiana8826d62009-10-01 03:10:14 -0700237 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, tw, th, 0, GL_RGBA,
Yi Kong911ac232019-03-24 01:49:02 -0700238 GL_UNSIGNED_BYTE, nullptr);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700239 glTexSubImage2D(GL_TEXTURE_2D, 0,
240 0, 0, w, h, GL_RGBA, GL_UNSIGNED_BYTE, p);
241 } else {
Sai Kiran Korwar27167492015-07-07 20:00:06 +0530242 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA,
Mathias Agopiana8826d62009-10-01 03:10:14 -0700243 GL_UNSIGNED_BYTE, p);
244 }
245 break;
246
Mike Reed42a1d082014-07-07 18:06:18 -0400247 case kRGB_565_SkColorType:
Sai Kiran Korwar27167492015-07-07 20:00:06 +0530248 if (!mUseNpotTextures && (tw != w || th != h)) {
Mathias Agopiana8826d62009-10-01 03:10:14 -0700249 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, tw, th, 0, GL_RGB,
Yi Kong911ac232019-03-24 01:49:02 -0700250 GL_UNSIGNED_SHORT_5_6_5, nullptr);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700251 glTexSubImage2D(GL_TEXTURE_2D, 0,
252 0, 0, w, h, GL_RGB, GL_UNSIGNED_SHORT_5_6_5, p);
253 } else {
Sai Kiran Korwar27167492015-07-07 20:00:06 +0530254 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, w, h, 0, GL_RGB,
Mathias Agopiana8826d62009-10-01 03:10:14 -0700255 GL_UNSIGNED_SHORT_5_6_5, p);
256 }
257 break;
258 default:
259 break;
260 }
261
262 glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_CROP_RECT_OES, crop);
263
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700264 *width = w;
265 *height = h;
266
Mathias Agopiana8826d62009-10-01 03:10:14 -0700267 return NO_ERROR;
268}
269
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800270status_t BootAnimation::readyToRun() {
271 mAssets.addDefaultAssets();
272
Dominik Laskowski3316a0a2019-01-25 02:56:41 -0800273 mDisplayToken = SurfaceComposerClient::getInternalDisplayToken();
274 if (mDisplayToken == nullptr)
275 return -1;
276
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800277 DisplayInfo dinfo;
Dominik Laskowski3316a0a2019-01-25 02:56:41 -0800278 status_t status = SurfaceComposerClient::getDisplayInfo(mDisplayToken, &dinfo);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800279 if (status)
280 return -1;
281
282 // create the native surface
Jeff Brown0b722fe2012-08-24 22:40:14 -0700283 sp<SurfaceControl> control = session()->createSurface(String8("BootAnimation"),
284 dinfo.w, dinfo.h, PIXEL_FORMAT_RGB_565);
Mathias Agopian439863f2011-06-28 19:09:31 -0700285
Robert Carre13b58e2017-08-31 14:50:44 -0700286 SurfaceComposerClient::Transaction t;
287 t.setLayer(control, 0x40000000)
288 .apply();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800289
Mathias Agopian17f638b2009-04-16 20:04:08 -0700290 sp<Surface> s = control->getSurface();
291
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800292 // initialize opengl and egl
Mathias Agopian738b9a42009-08-06 16:41:02 -0700293 const EGLint attribs[] = {
Mathias Agopian1b253b72011-08-15 15:20:22 -0700294 EGL_RED_SIZE, 8,
295 EGL_GREEN_SIZE, 8,
296 EGL_BLUE_SIZE, 8,
Mathias Agopiana8826d62009-10-01 03:10:14 -0700297 EGL_DEPTH_SIZE, 0,
298 EGL_NONE
Mathias Agopian738b9a42009-08-06 16:41:02 -0700299 };
Andreas Gampecfedceb2014-09-30 21:48:18 -0700300 EGLint w, h;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800301 EGLint numConfigs;
302 EGLConfig config;
303 EGLSurface surface;
304 EGLContext context;
Mathias Agopian627e7b52009-05-21 19:21:59 -0700305
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800306 EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
Mathias Agopian627e7b52009-05-21 19:21:59 -0700307
Yi Kong911ac232019-03-24 01:49:02 -0700308 eglInitialize(display, nullptr, nullptr);
Mathias Agopian1b253b72011-08-15 15:20:22 -0700309 eglChooseConfig(display, attribs, &config, 1, &numConfigs);
Yi Kong911ac232019-03-24 01:49:02 -0700310 surface = eglCreateWindowSurface(display, config, s.get(), nullptr);
311 context = eglCreateContext(display, config, nullptr, nullptr);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800312 eglQuerySurface(display, surface, EGL_WIDTH, &w);
313 eglQuerySurface(display, surface, EGL_HEIGHT, &h);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700314
Mathias Agopianabac0102009-07-31 14:47:00 -0700315 if (eglMakeCurrent(display, surface, surface, context) == EGL_FALSE)
316 return NO_INIT;
Mathias Agopiana8826d62009-10-01 03:10:14 -0700317
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800318 mDisplay = display;
319 mContext = context;
320 mSurface = surface;
321 mWidth = w;
322 mHeight = h;
Mathias Agopian17f638b2009-04-16 20:04:08 -0700323 mFlingerSurfaceControl = control;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800324 mFlingerSurface = s;
Adrian Roos9ee5dff2018-08-22 20:19:49 +0200325 mTargetInset = -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800326
Huihong Luo50a2f362018-08-11 09:27:57 -0700327 return NO_ERROR;
328}
329
330bool BootAnimation::preloadAnimation() {
331 findBootAnimationFile();
332 if (!mZipFileName.isEmpty()) {
333 mAnimation = loadAnimation(mZipFileName);
334 return (mAnimation != nullptr);
335 }
336
337 return false;
338}
339
340void BootAnimation::findBootAnimationFile() {
Elliott Hughesc367d482013-10-29 13:12:55 -0700341 // If the device has encryption turned on or is in process
Jason parksbd9a08d2011-01-31 15:04:34 -0600342 // of being encrypted we show the encrypted boot animation.
343 char decrypt[PROPERTY_VALUE_MAX];
344 property_get("vold.decrypt", decrypt, "");
345
Keun-young Parkb5938422017-03-23 13:46:24 -0700346 bool encryptedAnimation = atoi(decrypt) != 0 ||
347 !strcmp("trigger_restart_min_framework", decrypt);
Jason parksbd9a08d2011-01-31 15:04:34 -0600348
Jaekyun Seokc521bb32018-01-30 11:52:47 +0900349 if (!mShuttingDown && encryptedAnimation) {
350 static const char* encryptedBootFiles[] =
351 {PRODUCT_ENCRYPTED_BOOTANIMATION_FILE, SYSTEM_ENCRYPTED_BOOTANIMATION_FILE};
352 for (const char* f : encryptedBootFiles) {
353 if (access(f, R_OK) == 0) {
354 mZipFileName = f;
Huihong Luo50a2f362018-08-11 09:27:57 -0700355 return;
Jaekyun Seokc521bb32018-01-30 11:52:47 +0900356 }
357 }
Jason parksbd9a08d2011-01-31 15:04:34 -0600358 }
Beverly34ffe252019-05-17 11:03:54 -0400359
360 const bool playDarkAnim = android::base::GetIntProperty("ro.boot.theme", 0) == 1;
Jaekyun Seokc521bb32018-01-30 11:52:47 +0900361 static const char* bootFiles[] =
Anders Fridlunde0b4d232018-11-06 14:23:25 +0100362 {APEX_BOOTANIMATION_FILE, playDarkAnim ? PRODUCT_BOOTANIMATION_DARK_FILE : PRODUCT_BOOTANIMATION_FILE,
Beverly34ffe252019-05-17 11:03:54 -0400363 OEM_BOOTANIMATION_FILE, SYSTEM_BOOTANIMATION_FILE};
Keun-young Parkb5938422017-03-23 13:46:24 -0700364 static const char* shutdownFiles[] =
Anders Fridlunde0b4d232018-11-06 14:23:25 +0100365 {PRODUCT_SHUTDOWNANIMATION_FILE, OEM_SHUTDOWNANIMATION_FILE, SYSTEM_SHUTDOWNANIMATION_FILE, ""};
Keun-young Parkb5938422017-03-23 13:46:24 -0700366
367 for (const char* f : (!mShuttingDown ? bootFiles : shutdownFiles)) {
368 if (access(f, R_OK) == 0) {
369 mZipFileName = f;
Huihong Luo50a2f362018-08-11 09:27:57 -0700370 return;
Keun-young Parkb5938422017-03-23 13:46:24 -0700371 }
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700372 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800373}
374
Mathias Agopiana8826d62009-10-01 03:10:14 -0700375bool BootAnimation::threadLoop()
376{
377 bool r;
Narayan Kamathafd31e02013-12-03 13:16:03 +0000378 // We have no bootanimation file, so we use the stock android logo
379 // animation.
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700380 if (mZipFileName.isEmpty()) {
Mathias Agopiana8826d62009-10-01 03:10:14 -0700381 r = android();
382 } else {
383 r = movie();
384 }
385
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800386 eglMakeCurrent(mDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
387 eglDestroyContext(mDisplay, mContext);
388 eglDestroySurface(mDisplay, mSurface);
Mathias Agopian6cf0db22009-04-17 19:36:26 -0700389 mFlingerSurface.clear();
Mathias Agopian17f638b2009-04-16 20:04:08 -0700390 mFlingerSurfaceControl.clear();
Mathias Agopian627e7b52009-05-21 19:21:59 -0700391 eglTerminate(mDisplay);
Sai Kiran Korwar3eee9fb2015-06-16 17:13:35 +0530392 eglReleaseThread();
Mathias Agopian627e7b52009-05-21 19:21:59 -0700393 IPCThreadState::self()->stopProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800394 return r;
395}
396
Mathias Agopiana8826d62009-10-01 03:10:14 -0700397bool BootAnimation::android()
398{
Wei Wang159a4102018-10-23 23:15:58 -0700399 SLOGD("%sAnimationShownTiming start time: %" PRId64 "ms", mShuttingDown ? "Shutdown" : "Boot",
Keun-young Parkd1794cd2017-04-04 12:21:19 -0700400 elapsedRealtime());
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700401 initTexture(&mAndroid[0], mAssets, "images/android-logo-mask.png");
402 initTexture(&mAndroid[1], mAssets, "images/android-logo-shine.png");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800403
Ed Coyne7464ac92017-06-08 12:26:48 -0700404 mCallbacks->init({});
405
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800406 // clear screen
Mathias Agopiana8826d62009-10-01 03:10:14 -0700407 glShadeModel(GL_FLAT);
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700408 glDisable(GL_DITHER);
409 glDisable(GL_SCISSOR_TEST);
Mathias Agopian59f19e42011-05-06 19:22:12 -0700410 glClearColor(0,0,0,1);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800411 glClear(GL_COLOR_BUFFER_BIT);
412 eglSwapBuffers(mDisplay, mSurface);
413
Mathias Agopiana8826d62009-10-01 03:10:14 -0700414 glEnable(GL_TEXTURE_2D);
415 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
416
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700417 const GLint xc = (mWidth - mAndroid[0].w) / 2;
418 const GLint yc = (mHeight - mAndroid[0].h) / 2;
419 const Rect updateRect(xc, yc, xc + mAndroid[0].w, yc + mAndroid[0].h);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800420
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800421 glScissor(updateRect.left, mHeight - updateRect.bottom, updateRect.width(),
422 updateRect.height());
423
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700424 // Blend state
425 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
426 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
427
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800428 const nsecs_t startTime = systemTime();
429 do {
Mathias Agopian13796652009-03-24 22:49:21 -0700430 nsecs_t now = systemTime();
431 double time = now - startTime;
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700432 float t = 4.0f * float(time / us2ns(16667)) / mAndroid[1].w;
433 GLint offset = (1 - (t - floorf(t))) * mAndroid[1].w;
434 GLint x = xc - offset;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800435
Mathias Agopian81668642009-07-28 11:41:30 -0700436 glDisable(GL_SCISSOR_TEST);
437 glClear(GL_COLOR_BUFFER_BIT);
438
439 glEnable(GL_SCISSOR_TEST);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800440 glDisable(GL_BLEND);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800441 glBindTexture(GL_TEXTURE_2D, mAndroid[1].name);
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700442 glDrawTexiOES(x, yc, 0, mAndroid[1].w, mAndroid[1].h);
443 glDrawTexiOES(x + mAndroid[1].w, yc, 0, mAndroid[1].w, mAndroid[1].h);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800444
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700445 glEnable(GL_BLEND);
446 glBindTexture(GL_TEXTURE_2D, mAndroid[0].name);
447 glDrawTexiOES(xc, yc, 0, mAndroid[0].w, mAndroid[0].h);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800448
Mathias Agopian627e7b52009-05-21 19:21:59 -0700449 EGLBoolean res = eglSwapBuffers(mDisplay, mSurface);
450 if (res == EGL_FALSE)
451 break;
452
Mathias Agopian13796652009-03-24 22:49:21 -0700453 // 12fps: don't animate too fast to preserve CPU
454 const nsecs_t sleepTime = 83333 - ns2us(systemTime() - now);
455 if (sleepTime > 0)
Mathias Agopiana8826d62009-10-01 03:10:14 -0700456 usleep(sleepTime);
Kevin Hesterd3782b22012-04-26 10:38:55 -0700457
458 checkExit();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800459 } while (!exitPending());
460
461 glDeleteTextures(1, &mAndroid[0].name);
462 glDeleteTextures(1, &mAndroid[1].name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800463 return false;
464}
465
Kevin Hesterd3782b22012-04-26 10:38:55 -0700466void BootAnimation::checkExit() {
467 // Allow surface flinger to gracefully request shutdown
468 char value[PROPERTY_VALUE_MAX];
469 property_get(EXIT_PROP_NAME, value, "0");
470 int exitnow = atoi(value);
471 if (exitnow) {
472 requestExit();
Ed Coyne7464ac92017-06-08 12:26:48 -0700473 mCallbacks->shutdown();
Kevin Hesterd3782b22012-04-26 10:38:55 -0700474 }
475}
476
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700477bool BootAnimation::validClock(const Animation::Part& part) {
478 return part.clockPosX != TEXT_MISSING_VALUE && part.clockPosY != TEXT_MISSING_VALUE;
479}
480
481bool parseTextCoord(const char* str, int* dest) {
482 if (strcmp("c", str) == 0) {
483 *dest = TEXT_CENTER_VALUE;
484 return true;
485 }
486
487 char* end;
488 int val = (int) strtol(str, &end, 0);
489 if (end == str || *end != '\0' || val == INT_MAX || val == INT_MIN) {
490 return false;
491 }
492 *dest = val;
493 return true;
494}
495
496// Parse two position coordinates. If only string is non-empty, treat it as the y value.
497void parsePosition(const char* str1, const char* str2, int* x, int* y) {
498 bool success = false;
499 if (strlen(str1) == 0) { // No values were specified
500 // success = false
501 } else if (strlen(str2) == 0) { // we have only one value
502 if (parseTextCoord(str1, y)) {
503 *x = TEXT_CENTER_VALUE;
504 success = true;
505 }
506 } else {
507 if (parseTextCoord(str1, x) && parseTextCoord(str2, y)) {
508 success = true;
509 }
510 }
511
512 if (!success) {
513 *x = TEXT_MISSING_VALUE;
514 *y = TEXT_MISSING_VALUE;
515 }
516}
517
Jesse Hall083b84c2014-09-22 10:51:09 -0700518// Parse a color represented as an HTML-style 'RRGGBB' string: each pair of
519// characters in str is a hex number in [0, 255], which are converted to
520// floating point values in the range [0.0, 1.0] and placed in the
521// corresponding elements of color.
522//
523// If the input string isn't valid, parseColor returns false and color is
524// left unchanged.
525static bool parseColor(const char str[7], float color[3]) {
526 float tmpColor[3];
527 for (int i = 0; i < 3; i++) {
528 int val = 0;
529 for (int j = 0; j < 2; j++) {
530 val *= 16;
531 char c = str[2*i + j];
532 if (c >= '0' && c <= '9') val += c - '0';
533 else if (c >= 'A' && c <= 'F') val += (c - 'A') + 10;
534 else if (c >= 'a' && c <= 'f') val += (c - 'a') + 10;
535 else return false;
536 }
537 tmpColor[i] = static_cast<float>(val) / 255.0f;
538 }
539 memcpy(color, tmpColor, sizeof(tmpColor));
540 return true;
541}
542
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700543
544static bool readFile(ZipFileRO* zip, const char* name, String8& outString)
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700545{
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700546 ZipEntryRO entry = zip->findEntryByName(name);
Wei Wang159a4102018-10-23 23:15:58 -0700547 SLOGE_IF(!entry, "couldn't find %s", name);
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700548 if (!entry) {
549 return false;
550 }
551
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700552 FileMap* entryMap = zip->createEntryFileMap(entry);
553 zip->releaseEntry(entry);
Wei Wang159a4102018-10-23 23:15:58 -0700554 SLOGE_IF(!entryMap, "entryMap is null");
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700555 if (!entryMap) {
556 return false;
557 }
558
559 outString.setTo((char const*)entryMap->getDataPtr(), entryMap->getDataLength());
Narayan Kamath688ff4c2015-02-23 15:47:54 +0000560 delete entryMap;
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700561 return true;
562}
563
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700564// The font image should be a 96x2 array of character images. The
565// columns are the printable ASCII characters 0x20 - 0x7f. The
566// top row is regular text; the bottom row is bold.
567status_t BootAnimation::initFont(Font* font, const char* fallback) {
568 status_t status = NO_ERROR;
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800569
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700570 if (font->map != nullptr) {
571 glGenTextures(1, &font->texture.name);
572 glBindTexture(GL_TEXTURE_2D, font->texture.name);
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800573
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700574 status = initTexture(font->map, &font->texture.w, &font->texture.h);
575
576 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
577 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
578 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
579 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
580 } else if (fallback != nullptr) {
581 status = initTexture(&font->texture, mAssets, fallback);
582 } else {
583 return NO_INIT;
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800584 }
585
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700586 if (status == NO_ERROR) {
587 font->char_width = font->texture.w / FONT_NUM_COLS;
588 font->char_height = font->texture.h / FONT_NUM_ROWS / 2; // There are bold and regular rows
589 }
590
591 return status;
592}
593
594void BootAnimation::drawText(const char* str, const Font& font, bool bold, int* x, int* y) {
595 glEnable(GL_BLEND); // Allow us to draw on top of the animation
596 glBindTexture(GL_TEXTURE_2D, font.texture.name);
597
598 const int len = strlen(str);
599 const int strWidth = font.char_width * len;
600
601 if (*x == TEXT_CENTER_VALUE) {
602 *x = (mWidth - strWidth) / 2;
603 } else if (*x < 0) {
604 *x = mWidth + *x - strWidth;
605 }
606 if (*y == TEXT_CENTER_VALUE) {
607 *y = (mHeight - font.char_height) / 2;
608 } else if (*y < 0) {
609 *y = mHeight + *y - font.char_height;
610 }
611
612 int cropRect[4] = { 0, 0, font.char_width, -font.char_height };
613
614 for (int i = 0; i < len; i++) {
615 char c = str[i];
616
617 if (c < FONT_BEGIN_CHAR || c > FONT_END_CHAR) {
618 c = '?';
619 }
620
621 // Crop the texture to only the pixels in the current glyph
622 const int charPos = (c - FONT_BEGIN_CHAR); // Position in the list of valid characters
623 const int row = charPos / FONT_NUM_COLS;
624 const int col = charPos % FONT_NUM_COLS;
625 cropRect[0] = col * font.char_width; // Left of column
626 cropRect[1] = row * font.char_height * 2; // Top of row
627 // Move down to bottom of regular (one char_heigh) or bold (two char_heigh) line
628 cropRect[1] += bold ? 2 * font.char_height : font.char_height;
629 glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_CROP_RECT_OES, cropRect);
630
631 glDrawTexiOES(*x, *y, 0, font.char_width, font.char_height);
632
633 *x += font.char_width;
634 }
635
636 glDisable(GL_BLEND); // Return to the animation's default behaviour
637 glBindTexture(GL_TEXTURE_2D, 0);
638}
639
Damien Bargiacchi9071db12016-10-28 17:38:22 -0700640// We render 12 or 24 hour time.
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700641void BootAnimation::drawClock(const Font& font, const int xPos, const int yPos) {
Damien Bargiacchi9071db12016-10-28 17:38:22 -0700642 static constexpr char TIME_FORMAT_12[] = "%l:%M";
643 static constexpr char TIME_FORMAT_24[] = "%H:%M";
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700644 static constexpr int TIME_LENGTH = 6;
645
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800646 time_t rawtime;
647 time(&rawtime);
648 struct tm* timeInfo = localtime(&rawtime);
649
650 char timeBuff[TIME_LENGTH];
Damien Bargiacchi9071db12016-10-28 17:38:22 -0700651 const char* timeFormat = mTimeFormat12Hour ? TIME_FORMAT_12 : TIME_FORMAT_24;
652 size_t length = strftime(timeBuff, TIME_LENGTH, timeFormat, timeInfo);
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800653
654 if (length != TIME_LENGTH - 1) {
Wei Wang159a4102018-10-23 23:15:58 -0700655 SLOGE("Couldn't format time; abandoning boot animation clock");
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800656 mClockEnabled = false;
657 return;
658 }
659
Damien Bargiacchi45a76442016-12-05 18:02:18 -0800660 char* out = timeBuff[0] == ' ' ? &timeBuff[1] : &timeBuff[0];
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700661 int x = xPos;
662 int y = yPos;
Damien Bargiacchi45a76442016-12-05 18:02:18 -0800663 drawText(out, font, false, &x, &y);
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800664}
665
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700666bool BootAnimation::parseAnimationDesc(Animation& animation)
Mathias Agopiana8826d62009-10-01 03:10:14 -0700667{
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700668 String8 desString;
669
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700670 if (!readFile(animation.zip, "desc.txt", desString)) {
Narayan Kamathafd31e02013-12-03 13:16:03 +0000671 return false;
672 }
Mathias Agopiana8826d62009-10-01 03:10:14 -0700673 char const* s = desString.string();
674
Mathias Agopiana8826d62009-10-01 03:10:14 -0700675 // Parse the description file
676 for (;;) {
677 const char* endl = strstr(s, "\n");
Yi Kong911ac232019-03-24 01:49:02 -0700678 if (endl == nullptr) break;
Mathias Agopiana8826d62009-10-01 03:10:14 -0700679 String8 line(s, endl - s);
680 const char* l = line.string();
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800681 int fps = 0;
682 int width = 0;
683 int height = 0;
684 int count = 0;
685 int pause = 0;
Narayan Kamathafd31e02013-12-03 13:16:03 +0000686 char path[ANIM_ENTRY_NAME_MAX];
Jesse Hall083b84c2014-09-22 10:51:09 -0700687 char color[7] = "000000"; // default to black if unspecified
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700688 char clockPos1[TEXT_POS_LEN_MAX + 1] = "";
689 char clockPos2[TEXT_POS_LEN_MAX + 1] = "";
Jesse Hall083b84c2014-09-22 10:51:09 -0700690
Kevin Hesterd3782b22012-04-26 10:38:55 -0700691 char pathType;
Mathias Agopiana8826d62009-10-01 03:10:14 -0700692 if (sscanf(l, "%d %d %d", &width, &height, &fps) == 3) {
Wei Wang159a4102018-10-23 23:15:58 -0700693 // SLOGD("> w=%d, h=%d, fps=%d", width, height, fps);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700694 animation.width = width;
695 animation.height = height;
696 animation.fps = fps;
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700697 } else if (sscanf(l, " %c %d %d %s #%6s %16s %16s",
698 &pathType, &count, &pause, path, color, clockPos1, clockPos2) >= 4) {
Wei Wang159a4102018-10-23 23:15:58 -0700699 //SLOGD("> type=%c, count=%d, pause=%d, path=%s, color=%s, clockPos1=%s, clockPos2=%s",
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700700 // pathType, count, pause, path, color, clockPos1, clockPos2);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700701 Animation::Part part;
Kevin Hesterd3782b22012-04-26 10:38:55 -0700702 part.playUntilComplete = pathType == 'c';
Mathias Agopiana8826d62009-10-01 03:10:14 -0700703 part.count = count;
704 part.pause = pause;
705 part.path = path;
Yi Kong911ac232019-03-24 01:49:02 -0700706 part.audioData = nullptr;
707 part.animation = nullptr;
Jesse Hall083b84c2014-09-22 10:51:09 -0700708 if (!parseColor(color, part.backgroundColor)) {
Wei Wang159a4102018-10-23 23:15:58 -0700709 SLOGE("> invalid color '#%s'", color);
Jesse Hall083b84c2014-09-22 10:51:09 -0700710 part.backgroundColor[0] = 0.0f;
711 part.backgroundColor[1] = 0.0f;
712 part.backgroundColor[2] = 0.0f;
713 }
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700714 parsePosition(clockPos1, clockPos2, &part.clockPosX, &part.clockPosY);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700715 animation.parts.add(part);
716 }
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700717 else if (strcmp(l, "$SYSTEM") == 0) {
Wei Wang159a4102018-10-23 23:15:58 -0700718 // SLOGD("> SYSTEM");
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700719 Animation::Part part;
720 part.playUntilComplete = false;
721 part.count = 1;
722 part.pause = 0;
Yi Kong911ac232019-03-24 01:49:02 -0700723 part.audioData = nullptr;
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700724 part.animation = loadAnimation(String8(SYSTEM_BOOTANIMATION_FILE));
Yi Kong911ac232019-03-24 01:49:02 -0700725 if (part.animation != nullptr)
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700726 animation.parts.add(part);
727 }
Mathias Agopiana8826d62009-10-01 03:10:14 -0700728 s = ++endl;
729 }
730
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700731 return true;
732}
733
734bool BootAnimation::preloadZip(Animation& animation)
735{
Mathias Agopiana8826d62009-10-01 03:10:14 -0700736 // read all the data structures
737 const size_t pcount = animation.parts.size();
Yi Kong911ac232019-03-24 01:49:02 -0700738 void *cookie = nullptr;
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400739 ZipFileRO* zip = animation.zip;
740 if (!zip->startIteration(&cookie)) {
Narayan Kamathafd31e02013-12-03 13:16:03 +0000741 return false;
742 }
743
744 ZipEntryRO entry;
745 char name[ANIM_ENTRY_NAME_MAX];
Yi Kong911ac232019-03-24 01:49:02 -0700746 while ((entry = zip->nextEntry(cookie)) != nullptr) {
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400747 const int foundEntryName = zip->getEntryFileName(entry, name, ANIM_ENTRY_NAME_MAX);
Narayan Kamathafd31e02013-12-03 13:16:03 +0000748 if (foundEntryName > ANIM_ENTRY_NAME_MAX || foundEntryName == -1) {
Wei Wang159a4102018-10-23 23:15:58 -0700749 SLOGE("Error fetching entry file name");
Narayan Kamathafd31e02013-12-03 13:16:03 +0000750 continue;
751 }
752
753 const String8 entryName(name);
754 const String8 path(entryName.getPathDir());
755 const String8 leaf(entryName.getPathLeaf());
756 if (leaf.size() > 0) {
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700757 if (entryName == CLOCK_FONT_ZIP_NAME) {
758 FileMap* map = zip->createEntryFileMap(entry);
759 if (map) {
760 animation.clockFont.map = map;
761 }
762 continue;
763 }
764
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400765 for (size_t j = 0; j < pcount; j++) {
Narayan Kamathafd31e02013-12-03 13:16:03 +0000766 if (path == animation.parts[j].path) {
Narayan Kamath4600dd02015-06-16 12:02:57 +0100767 uint16_t method;
Narayan Kamathafd31e02013-12-03 13:16:03 +0000768 // supports only stored png files
Yi Kong911ac232019-03-24 01:49:02 -0700769 if (zip->getEntryInfo(entry, &method, nullptr, nullptr, nullptr, nullptr, nullptr)) {
Narayan Kamathafd31e02013-12-03 13:16:03 +0000770 if (method == ZipFileRO::kCompressStored) {
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400771 FileMap* map = zip->createEntryFileMap(entry);
Narayan Kamathafd31e02013-12-03 13:16:03 +0000772 if (map) {
Narayan Kamathafd31e02013-12-03 13:16:03 +0000773 Animation::Part& part(animation.parts.editItemAt(j));
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700774 if (leaf == "audio.wav") {
775 // a part may have at most one audio file
Geoffrey Pitschd6d9a1d2016-06-08 00:38:58 -0700776 part.audioData = (uint8_t *)map->getDataPtr();
777 part.audioLength = map->getDataLength();
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400778 } else if (leaf == "trim.txt") {
779 part.trimData.setTo((char const*)map->getDataPtr(),
780 map->getDataLength());
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700781 } else {
782 Animation::Frame frame;
783 frame.name = leaf;
784 frame.map = map;
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400785 frame.trimWidth = animation.width;
786 frame.trimHeight = animation.height;
787 frame.trimX = 0;
788 frame.trimY = 0;
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700789 part.frames.add(frame);
790 }
Mathias Agopiana8826d62009-10-01 03:10:14 -0700791 }
Geoffrey Pitschd6d9a1d2016-06-08 00:38:58 -0700792 } else {
Wei Wang159a4102018-10-23 23:15:58 -0700793 SLOGE("bootanimation.zip is compressed; must be only stored");
Mathias Agopiana8826d62009-10-01 03:10:14 -0700794 }
795 }
796 }
797 }
798 }
799 }
800
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400801 // If there is trimData present, override the positioning defaults.
802 for (Animation::Part& part : animation.parts) {
803 const char* trimDataStr = part.trimData.string();
804 for (size_t frameIdx = 0; frameIdx < part.frames.size(); frameIdx++) {
805 const char* endl = strstr(trimDataStr, "\n");
806 // No more trimData for this part.
Yi Kong911ac232019-03-24 01:49:02 -0700807 if (endl == nullptr) {
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400808 break;
809 }
810 String8 line(trimDataStr, endl - trimDataStr);
811 const char* lineStr = line.string();
812 trimDataStr = ++endl;
813 int width = 0, height = 0, x = 0, y = 0;
814 if (sscanf(lineStr, "%dx%d+%d+%d", &width, &height, &x, &y) == 4) {
815 Animation::Frame& frame(part.frames.editItemAt(frameIdx));
816 frame.trimWidth = width;
817 frame.trimHeight = height;
818 frame.trimX = x;
819 frame.trimY = y;
820 } else {
Wei Wang159a4102018-10-23 23:15:58 -0700821 SLOGE("Error parsing trim.txt, line: %s", lineStr);
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400822 break;
823 }
824 }
825 }
826
827 zip->endIteration(cookie);
Narayan Kamathafd31e02013-12-03 13:16:03 +0000828
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700829 return true;
830}
831
832bool BootAnimation::movie()
833{
Huihong Luo50a2f362018-08-11 09:27:57 -0700834 if (mAnimation == nullptr) {
835 mAnimation = loadAnimation(mZipFileName);
836 }
837
838 if (mAnimation == nullptr)
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700839 return false;
840
Huihong Luo50a2f362018-08-11 09:27:57 -0700841 // mCallbacks->init() may get called recursively,
842 // this loop is needed to get the same results
843 for (const Animation::Part& part : mAnimation->parts) {
844 if (part.animation != nullptr) {
845 mCallbacks->init(part.animation->parts);
846 }
847 }
848 mCallbacks->init(mAnimation->parts);
849
Damien Bargiacchi97480862016-03-29 14:55:55 -0700850 bool anyPartHasClock = false;
Huihong Luo50a2f362018-08-11 09:27:57 -0700851 for (size_t i=0; i < mAnimation->parts.size(); i++) {
852 if(validClock(mAnimation->parts[i])) {
Damien Bargiacchi97480862016-03-29 14:55:55 -0700853 anyPartHasClock = true;
854 break;
855 }
856 }
857 if (!anyPartHasClock) {
858 mClockEnabled = false;
859 }
860
Sai Kiran Korwar27167492015-07-07 20:00:06 +0530861 // Check if npot textures are supported
862 mUseNpotTextures = false;
863 String8 gl_extensions;
864 const char* exts = reinterpret_cast<const char*>(glGetString(GL_EXTENSIONS));
865 if (!exts) {
866 glGetError();
867 } else {
868 gl_extensions.setTo(exts);
869 if ((gl_extensions.find("GL_ARB_texture_non_power_of_two") != -1) ||
870 (gl_extensions.find("GL_OES_texture_npot") != -1)) {
871 mUseNpotTextures = true;
872 }
873 }
874
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800875 // Blend required to draw time on top of animation frames.
876 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700877 glShadeModel(GL_FLAT);
878 glDisable(GL_DITHER);
879 glDisable(GL_SCISSOR_TEST);
880 glDisable(GL_BLEND);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700881
882 glBindTexture(GL_TEXTURE_2D, 0);
883 glEnable(GL_TEXTURE_2D);
884 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
885 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
886 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
887 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
888 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
889
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700890 bool clockFontInitialized = false;
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800891 if (mClockEnabled) {
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700892 clockFontInitialized =
Huihong Luo50a2f362018-08-11 09:27:57 -0700893 (initFont(&mAnimation->clockFont, CLOCK_FONT_ASSET) == NO_ERROR);
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700894 mClockEnabled = clockFontInitialized;
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800895 }
896
Damien Bargiacchi97480862016-03-29 14:55:55 -0700897 if (mClockEnabled && !updateIsTimeAccurate()) {
898 mTimeCheckThread = new TimeCheckThread(this);
899 mTimeCheckThread->run("BootAnimation::TimeCheckThread", PRIORITY_NORMAL);
900 }
901
Huihong Luo50a2f362018-08-11 09:27:57 -0700902 playAnimation(*mAnimation);
Damien Bargiacchi97480862016-03-29 14:55:55 -0700903
Geoffrey Pitscha9173532016-07-19 14:56:39 -0400904 if (mTimeCheckThread != nullptr) {
Damien Bargiacchi97480862016-03-29 14:55:55 -0700905 mTimeCheckThread->requestExit();
Geoffrey Pitscha9173532016-07-19 14:56:39 -0400906 mTimeCheckThread = nullptr;
907 }
908
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700909 if (clockFontInitialized) {
Huihong Luo50a2f362018-08-11 09:27:57 -0700910 glDeleteTextures(1, &mAnimation->clockFont.texture.name);
Andriy Naborskyy815e51d2016-03-24 16:43:34 -0700911 }
912
Huihong Luo50a2f362018-08-11 09:27:57 -0700913 releaseAnimation(mAnimation);
914 mAnimation = nullptr;
Damien Bargiacchi1a8a2132018-07-24 15:20:26 -0700915
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700916 return false;
917}
918
919bool BootAnimation::playAnimation(const Animation& animation)
920{
921 const size_t pcount = animation.parts.size();
Mathias Agopiana8826d62009-10-01 03:10:14 -0700922 nsecs_t frameDuration = s2ns(1) / animation.fps;
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400923 const int animationX = (mWidth - animation.width) / 2;
924 const int animationY = (mHeight - animation.height) / 2;
Mathias Agopian9f3020d2009-11-06 16:30:18 -0800925
Wei Wang159a4102018-10-23 23:15:58 -0700926 SLOGD("%sAnimationShownTiming start time: %" PRId64 "ms", mShuttingDown ? "Shutdown" : "Boot",
Keun-young Parkd1794cd2017-04-04 12:21:19 -0700927 elapsedRealtime());
Narayan Kamathafd31e02013-12-03 13:16:03 +0000928 for (size_t i=0 ; i<pcount ; i++) {
Mathias Agopiana8826d62009-10-01 03:10:14 -0700929 const Animation::Part& part(animation.parts[i]);
930 const size_t fcount = part.frames.size();
931 glBindTexture(GL_TEXTURE_2D, 0);
932
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700933 // Handle animation package
Yi Kong911ac232019-03-24 01:49:02 -0700934 if (part.animation != nullptr) {
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700935 playAnimation(*part.animation);
936 if (exitPending())
937 break;
938 continue; //to next part
939 }
940
Mathias Agopiana8826d62009-10-01 03:10:14 -0700941 for (int r=0 ; !part.count || r<part.count ; r++) {
Kevin Hesterd3782b22012-04-26 10:38:55 -0700942 // Exit any non playuntil complete parts immediately
943 if(exitPending() && !part.playUntilComplete)
944 break;
945
Ed Coyne7464ac92017-06-08 12:26:48 -0700946 mCallbacks->playPart(i, part, r);
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700947
Jesse Hall083b84c2014-09-22 10:51:09 -0700948 glClearColor(
949 part.backgroundColor[0],
950 part.backgroundColor[1],
951 part.backgroundColor[2],
952 1.0f);
953
Narayan Kamathafd31e02013-12-03 13:16:03 +0000954 for (size_t j=0 ; j<fcount && (!exitPending() || part.playUntilComplete) ; j++) {
Mathias Agopiana8826d62009-10-01 03:10:14 -0700955 const Animation::Frame& frame(part.frames[j]);
Mathias Agopiandb7dd2a2012-05-12 15:08:21 -0700956 nsecs_t lastFrame = systemTime();
Mathias Agopiana8826d62009-10-01 03:10:14 -0700957
958 if (r > 0) {
959 glBindTexture(GL_TEXTURE_2D, frame.tid);
960 } else {
961 if (part.count != 1) {
962 glGenTextures(1, &frame.tid);
963 glBindTexture(GL_TEXTURE_2D, frame.tid);
964 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
965 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
966 }
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700967 int w, h;
968 initTexture(frame.map, &w, &h);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700969 }
970
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400971 const int xc = animationX + frame.trimX;
972 const int yc = animationY + frame.trimY;
973 Region clearReg(Rect(mWidth, mHeight));
974 clearReg.subtractSelf(Rect(xc, yc, xc+frame.trimWidth, yc+frame.trimHeight));
Mathias Agopian9f3020d2009-11-06 16:30:18 -0800975 if (!clearReg.isEmpty()) {
976 Region::const_iterator head(clearReg.begin());
977 Region::const_iterator tail(clearReg.end());
978 glEnable(GL_SCISSOR_TEST);
979 while (head != tail) {
Andreas Gampecfedceb2014-09-30 21:48:18 -0700980 const Rect& r2(*head++);
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400981 glScissor(r2.left, mHeight - r2.bottom, r2.width(), r2.height());
Mathias Agopian9f3020d2009-11-06 16:30:18 -0800982 glClear(GL_COLOR_BUFFER_BIT);
983 }
984 glDisable(GL_SCISSOR_TEST);
985 }
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400986 // specify the y center as ceiling((mHeight - frame.trimHeight) / 2)
987 // which is equivalent to mHeight - (yc + frame.trimHeight)
988 glDrawTexiOES(xc, mHeight - (yc + frame.trimHeight),
989 0, frame.trimWidth, frame.trimHeight);
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700990 if (mClockEnabled && mTimeIsAccurate && validClock(part)) {
991 drawClock(animation.clockFont, part.clockPosX, part.clockPosY);
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800992 }
Adrian Roos9ee5dff2018-08-22 20:19:49 +0200993 handleViewport(frameDuration);
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800994
Mathias Agopiana8826d62009-10-01 03:10:14 -0700995 eglSwapBuffers(mDisplay, mSurface);
996
997 nsecs_t now = systemTime();
998 nsecs_t delay = frameDuration - (now - lastFrame);
Wei Wang159a4102018-10-23 23:15:58 -0700999 //SLOGD("%lld, %lld", ns2ms(now - lastFrame), ns2ms(delay));
Mathias Agopiana8826d62009-10-01 03:10:14 -07001000 lastFrame = now;
Mathias Agopiandb7dd2a2012-05-12 15:08:21 -07001001
1002 if (delay > 0) {
1003 struct timespec spec;
1004 spec.tv_sec = (now + delay) / 1000000000;
1005 spec.tv_nsec = (now + delay) % 1000000000;
1006 int err;
1007 do {
Yi Kong911ac232019-03-24 01:49:02 -07001008 err = clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &spec, nullptr);
Mathias Agopiandb7dd2a2012-05-12 15:08:21 -07001009 } while (err<0 && errno == EINTR);
1010 }
Kevin Hesterd3782b22012-04-26 10:38:55 -07001011
1012 checkExit();
Mathias Agopiana8826d62009-10-01 03:10:14 -07001013 }
Kevin Hesterd3782b22012-04-26 10:38:55 -07001014
Mathias Agopiana8826d62009-10-01 03:10:14 -07001015 usleep(part.pause * ns2us(frameDuration));
Kevin Hesterd3782b22012-04-26 10:38:55 -07001016
1017 // For infinite parts, we've now played them at least once, so perhaps exit
Adrian Roos9ee5dff2018-08-22 20:19:49 +02001018 if(exitPending() && !part.count && mCurrentInset >= mTargetInset)
Kevin Hesterd3782b22012-04-26 10:38:55 -07001019 break;
Mathias Agopiana8826d62009-10-01 03:10:14 -07001020 }
1021
Geoffrey Pitsch2fb30fb2016-07-06 16:16:20 -04001022 }
1023
1024 // Free textures created for looping parts now that the animation is done.
1025 for (const Animation::Part& part : animation.parts) {
Mathias Agopiana8826d62009-10-01 03:10:14 -07001026 if (part.count != 1) {
Geoffrey Pitsch2fb30fb2016-07-06 16:16:20 -04001027 const size_t fcount = part.frames.size();
1028 for (size_t j = 0; j < fcount; j++) {
Mathias Agopiana8826d62009-10-01 03:10:14 -07001029 const Animation::Frame& frame(part.frames[j]);
1030 glDeleteTextures(1, &frame.tid);
1031 }
1032 }
1033 }
Geoffrey Pitschd6d9a1d2016-06-08 00:38:58 -07001034
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001035 return true;
Mathias Agopiana8826d62009-10-01 03:10:14 -07001036}
1037
Adrian Roos9ee5dff2018-08-22 20:19:49 +02001038void BootAnimation::handleViewport(nsecs_t timestep) {
1039 if (mShuttingDown || !mFlingerSurfaceControl || mTargetInset == 0) {
1040 return;
1041 }
1042 if (mTargetInset < 0) {
1043 // Poll the amount for the top display inset. This will return -1 until persistent properties
1044 // have been loaded.
1045 mTargetInset = android::base::GetIntProperty("persist.sys.displayinset.top",
1046 -1 /* default */, -1 /* min */, mHeight / 2 /* max */);
1047 }
1048 if (mTargetInset <= 0) {
1049 return;
1050 }
1051
1052 if (mCurrentInset < mTargetInset) {
1053 // After the device boots, the inset will effectively be cropped away. We animate this here.
1054 float fraction = static_cast<float>(mCurrentInset) / mTargetInset;
1055 int interpolatedInset = (cosf((fraction + 1) * M_PI) / 2.0f + 0.5f) * mTargetInset;
1056
1057 SurfaceComposerClient::Transaction()
1058 .setCrop(mFlingerSurfaceControl, Rect(0, interpolatedInset, mWidth, mHeight))
1059 .apply();
1060 } else {
1061 // At the end of the animation, we switch to the viewport that DisplayManager will apply
1062 // later. This changes the coordinate system, and means we must move the surface up by
1063 // the inset amount.
Adrian Roos9ee5dff2018-08-22 20:19:49 +02001064 Rect layerStackRect(0, 0, mWidth, mHeight - mTargetInset);
1065 Rect displayRect(0, mTargetInset, mWidth, mHeight);
1066
1067 SurfaceComposerClient::Transaction t;
1068 t.setPosition(mFlingerSurfaceControl, 0, -mTargetInset)
1069 .setCrop(mFlingerSurfaceControl, Rect(0, mTargetInset, mWidth, mHeight));
Dominik Laskowski3316a0a2019-01-25 02:56:41 -08001070 t.setDisplayProjection(mDisplayToken, 0 /* orientation */, layerStackRect, displayRect);
Adrian Roos9ee5dff2018-08-22 20:19:49 +02001071 t.apply();
1072
1073 mTargetInset = mCurrentInset = 0;
1074 }
1075
1076 int delta = timestep * mTargetInset / ms2ns(200);
1077 mCurrentInset += delta;
1078}
1079
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001080void BootAnimation::releaseAnimation(Animation* animation) const
1081{
1082 for (Vector<Animation::Part>::iterator it = animation->parts.begin(),
1083 e = animation->parts.end(); it != e; ++it) {
1084 if (it->animation)
1085 releaseAnimation(it->animation);
1086 }
1087 if (animation->zip)
1088 delete animation->zip;
1089 delete animation;
1090}
1091
1092BootAnimation::Animation* BootAnimation::loadAnimation(const String8& fn)
1093{
1094 if (mLoadedFiles.indexOf(fn) >= 0) {
Wei Wang159a4102018-10-23 23:15:58 -07001095 SLOGE("File \"%s\" is already loaded. Cyclic ref is not allowed",
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001096 fn.string());
Yi Kong911ac232019-03-24 01:49:02 -07001097 return nullptr;
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001098 }
1099 ZipFileRO *zip = ZipFileRO::open(fn);
Yi Kong911ac232019-03-24 01:49:02 -07001100 if (zip == nullptr) {
Wei Wang159a4102018-10-23 23:15:58 -07001101 SLOGE("Failed to open animation zip \"%s\": %s",
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001102 fn.string(), strerror(errno));
Yi Kong911ac232019-03-24 01:49:02 -07001103 return nullptr;
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001104 }
1105
1106 Animation *animation = new Animation;
1107 animation->fileName = fn;
1108 animation->zip = zip;
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -07001109 animation->clockFont.map = nullptr;
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001110 mLoadedFiles.add(animation->fileName);
1111
1112 parseAnimationDesc(*animation);
Geoffrey Pitscha91a2d72016-07-12 14:46:19 -04001113 if (!preloadZip(*animation)) {
Yi Kong911ac232019-03-24 01:49:02 -07001114 return nullptr;
Geoffrey Pitscha91a2d72016-07-12 14:46:19 -04001115 }
1116
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001117
1118 mLoadedFiles.remove(fn);
1119 return animation;
1120}
Damien Bargiacchi97480862016-03-29 14:55:55 -07001121
1122bool BootAnimation::updateIsTimeAccurate() {
1123 static constexpr long long MAX_TIME_IN_PAST = 60000LL * 60LL * 24LL * 30LL; // 30 days
1124 static constexpr long long MAX_TIME_IN_FUTURE = 60000LL * 90LL; // 90 minutes
1125
1126 if (mTimeIsAccurate) {
1127 return true;
1128 }
Keun-young Parkb5938422017-03-23 13:46:24 -07001129 if (mShuttingDown) return true;
Damien Bargiacchi97480862016-03-29 14:55:55 -07001130 struct stat statResult;
Damien Bargiacchi9071db12016-10-28 17:38:22 -07001131
1132 if(stat(TIME_FORMAT_12_HOUR_FLAG_FILE_PATH, &statResult) == 0) {
1133 mTimeFormat12Hour = true;
1134 }
1135
Damien Bargiacchi97480862016-03-29 14:55:55 -07001136 if(stat(ACCURATE_TIME_FLAG_FILE_PATH, &statResult) == 0) {
1137 mTimeIsAccurate = true;
1138 return true;
1139 }
1140
1141 FILE* file = fopen(LAST_TIME_CHANGED_FILE_PATH, "r");
Yi Kong911ac232019-03-24 01:49:02 -07001142 if (file != nullptr) {
Damien Bargiacchi97480862016-03-29 14:55:55 -07001143 long long lastChangedTime = 0;
1144 fscanf(file, "%lld", &lastChangedTime);
1145 fclose(file);
1146 if (lastChangedTime > 0) {
1147 struct timespec now;
1148 clock_gettime(CLOCK_REALTIME, &now);
1149 // Match the Java timestamp format
1150 long long rtcNow = (now.tv_sec * 1000LL) + (now.tv_nsec / 1000000LL);
Damien Bargiacchi96762812016-07-12 15:53:40 -07001151 if (ACCURATE_TIME_EPOCH < rtcNow
1152 && lastChangedTime > (rtcNow - MAX_TIME_IN_PAST)
1153 && lastChangedTime < (rtcNow + MAX_TIME_IN_FUTURE)) {
Damien Bargiacchi97480862016-03-29 14:55:55 -07001154 mTimeIsAccurate = true;
1155 }
1156 }
1157 }
1158
1159 return mTimeIsAccurate;
1160}
1161
1162BootAnimation::TimeCheckThread::TimeCheckThread(BootAnimation* bootAnimation) : Thread(false),
1163 mInotifyFd(-1), mSystemWd(-1), mTimeWd(-1), mBootAnimation(bootAnimation) {}
1164
1165BootAnimation::TimeCheckThread::~TimeCheckThread() {
1166 // mInotifyFd may be -1 but that's ok since we're not at risk of attempting to close a valid FD.
1167 close(mInotifyFd);
1168}
1169
1170bool BootAnimation::TimeCheckThread::threadLoop() {
1171 bool shouldLoop = doThreadLoop() && !mBootAnimation->mTimeIsAccurate
1172 && mBootAnimation->mClockEnabled;
1173 if (!shouldLoop) {
1174 close(mInotifyFd);
1175 mInotifyFd = -1;
1176 }
1177 return shouldLoop;
1178}
1179
1180bool BootAnimation::TimeCheckThread::doThreadLoop() {
1181 static constexpr int BUFF_LEN (10 * (sizeof(struct inotify_event) + NAME_MAX + 1));
1182
1183 // Poll instead of doing a blocking read so the Thread can exit if requested.
1184 struct pollfd pfd = { mInotifyFd, POLLIN, 0 };
1185 ssize_t pollResult = poll(&pfd, 1, 1000);
1186
1187 if (pollResult == 0) {
1188 return true;
1189 } else if (pollResult < 0) {
Wei Wang159a4102018-10-23 23:15:58 -07001190 SLOGE("Could not poll inotify events");
Damien Bargiacchi97480862016-03-29 14:55:55 -07001191 return false;
1192 }
1193
1194 char buff[BUFF_LEN] __attribute__ ((aligned(__alignof__(struct inotify_event))));;
1195 ssize_t length = read(mInotifyFd, buff, BUFF_LEN);
1196 if (length == 0) {
1197 return true;
1198 } else if (length < 0) {
Wei Wang159a4102018-10-23 23:15:58 -07001199 SLOGE("Could not read inotify events");
Damien Bargiacchi97480862016-03-29 14:55:55 -07001200 return false;
1201 }
1202
1203 const struct inotify_event *event;
1204 for (char* ptr = buff; ptr < buff + length; ptr += sizeof(struct inotify_event) + event->len) {
1205 event = (const struct inotify_event *) ptr;
1206 if (event->wd == mSystemWd && strcmp(SYSTEM_TIME_DIR_NAME, event->name) == 0) {
1207 addTimeDirWatch();
1208 } else if (event->wd == mTimeWd && (strcmp(LAST_TIME_CHANGED_FILE_NAME, event->name) == 0
1209 || strcmp(ACCURATE_TIME_FLAG_FILE_NAME, event->name) == 0)) {
1210 return !mBootAnimation->updateIsTimeAccurate();
1211 }
1212 }
1213
1214 return true;
1215}
1216
1217void BootAnimation::TimeCheckThread::addTimeDirWatch() {
1218 mTimeWd = inotify_add_watch(mInotifyFd, SYSTEM_TIME_DIR_PATH,
1219 IN_CLOSE_WRITE | IN_MOVED_TO | IN_ATTRIB);
1220 if (mTimeWd > 0) {
1221 // No need to watch for the time directory to be created if it already exists
1222 inotify_rm_watch(mInotifyFd, mSystemWd);
1223 mSystemWd = -1;
1224 }
1225}
1226
1227status_t BootAnimation::TimeCheckThread::readyToRun() {
1228 mInotifyFd = inotify_init();
1229 if (mInotifyFd < 0) {
Wei Wang159a4102018-10-23 23:15:58 -07001230 SLOGE("Could not initialize inotify fd");
Damien Bargiacchi97480862016-03-29 14:55:55 -07001231 return NO_INIT;
1232 }
1233
1234 mSystemWd = inotify_add_watch(mInotifyFd, SYSTEM_DATA_DIR_PATH, IN_CREATE | IN_ATTRIB);
1235 if (mSystemWd < 0) {
1236 close(mInotifyFd);
1237 mInotifyFd = -1;
Wei Wang159a4102018-10-23 23:15:58 -07001238 SLOGE("Could not add watch for %s", SYSTEM_DATA_DIR_PATH);
Damien Bargiacchi97480862016-03-29 14:55:55 -07001239 return NO_INIT;
1240 }
1241
1242 addTimeDirWatch();
1243
1244 if (mBootAnimation->updateIsTimeAccurate()) {
1245 close(mInotifyFd);
1246 mInotifyFd = -1;
1247 return ALREADY_EXISTS;
1248 }
1249
1250 return NO_ERROR;
1251}
1252
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001253// ---------------------------------------------------------------------------
1254
1255}
1256; // namespace android