blob: 8e7277c55ed8beb20d07a8b466fd31747dd06a23 [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";
Jaekyun Seokc521bb32018-01-30 11:52:47 +090069static const char PRODUCT_BOOTANIMATION_FILE[] = "/product/media/bootanimation.zip";
Damien Bargiacchi97480862016-03-29 14:55:55 -070070static const char SYSTEM_BOOTANIMATION_FILE[] = "/system/media/bootanimation.zip";
Jaekyun Seokc521bb32018-01-30 11:52:47 +090071static const char PRODUCT_ENCRYPTED_BOOTANIMATION_FILE[] = "/product/media/bootanimation-encrypted.zip";
Damien Bargiacchi97480862016-03-29 14:55:55 -070072static const char SYSTEM_ENCRYPTED_BOOTANIMATION_FILE[] = "/system/media/bootanimation-encrypted.zip";
Keun-young Parkb5938422017-03-23 13:46:24 -070073static const char OEM_SHUTDOWNANIMATION_FILE[] = "/oem/media/shutdownanimation.zip";
Jaekyun Seokc521bb32018-01-30 11:52:47 +090074static const char PRODUCT_SHUTDOWNANIMATION_FILE[] = "/product/media/shutdownanimation.zip";
Keun-young Parkb5938422017-03-23 13:46:24 -070075static const char SYSTEM_SHUTDOWNANIMATION_FILE[] = "/system/media/shutdownanimation.zip";
76
Damien Bargiacchi97480862016-03-29 14:55:55 -070077static const char SYSTEM_DATA_DIR_PATH[] = "/data/system";
78static const char SYSTEM_TIME_DIR_NAME[] = "time";
79static const char SYSTEM_TIME_DIR_PATH[] = "/data/system/time";
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -070080static const char CLOCK_FONT_ASSET[] = "images/clock_font.png";
81static const char CLOCK_FONT_ZIP_NAME[] = "clock_font.png";
Damien Bargiacchi97480862016-03-29 14:55:55 -070082static const char LAST_TIME_CHANGED_FILE_NAME[] = "last_time_change";
83static const char LAST_TIME_CHANGED_FILE_PATH[] = "/data/system/time/last_time_change";
84static const char ACCURATE_TIME_FLAG_FILE_NAME[] = "time_is_accurate";
85static const char ACCURATE_TIME_FLAG_FILE_PATH[] = "/data/system/time/time_is_accurate";
Damien Bargiacchi9071db12016-10-28 17:38:22 -070086static const char TIME_FORMAT_12_HOUR_FLAG_FILE_PATH[] = "/data/system/time/time_format_12_hour";
Damien Bargiacchi96762812016-07-12 15:53:40 -070087// Java timestamp format. Don't show the clock if the date is before 2000-01-01 00:00:00.
88static const long long ACCURATE_TIME_EPOCH = 946684800000;
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -070089static constexpr char FONT_BEGIN_CHAR = ' ';
90static constexpr char FONT_END_CHAR = '~' + 1;
91static constexpr size_t FONT_NUM_CHARS = FONT_END_CHAR - FONT_BEGIN_CHAR + 1;
92static constexpr size_t FONT_NUM_COLS = 16;
93static constexpr size_t FONT_NUM_ROWS = FONT_NUM_CHARS / FONT_NUM_COLS;
94static const int TEXT_CENTER_VALUE = INT_MAX;
95static const int TEXT_MISSING_VALUE = INT_MIN;
Damien Bargiacchi97480862016-03-29 14:55:55 -070096static const char EXIT_PROP_NAME[] = "service.bootanim.exit";
Narayan Kamathafd31e02013-12-03 13:16:03 +000097static const int ANIM_ENTRY_NAME_MAX = 256;
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -070098static constexpr size_t TEXT_POS_LEN_MAX = 16;
Narayan Kamathafd31e02013-12-03 13:16:03 +000099
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800100// ---------------------------------------------------------------------------
101
Ed Coyne7464ac92017-06-08 12:26:48 -0700102BootAnimation::BootAnimation(sp<Callbacks> callbacks)
Ed Coyne2c9e94a2017-05-31 10:08:28 -0700103 : Thread(false), mClockEnabled(true), mTimeIsAccurate(false),
Yi Kong911ac232019-03-24 01:49:02 -0700104 mTimeFormat12Hour(false), mTimeCheckThread(nullptr), mCallbacks(callbacks) {
Mathias Agopian627e7b52009-05-21 19:21:59 -0700105 mSession = new SurfaceComposerClient();
Geoffrey Pitsch290c4352016-08-09 14:35:10 -0400106
Keun-young Parkb5938422017-03-23 13:46:24 -0700107 std::string powerCtl = android::base::GetProperty("sys.powerctl", "");
108 if (powerCtl.empty()) {
109 mShuttingDown = false;
110 } else {
111 mShuttingDown = true;
112 }
Huihong Luo50a2f362018-08-11 09:27:57 -0700113 ALOGD("%sAnimationStartTiming start time: %" PRId64 "ms", mShuttingDown ? "Shutdown" : "Boot",
114 elapsedRealtime());
115}
116
117BootAnimation::~BootAnimation() {
118 if (mAnimation != nullptr) {
119 releaseAnimation(mAnimation);
120 mAnimation = nullptr;
121 }
122 ALOGD("%sAnimationStopTiming start time: %" PRId64 "ms", mShuttingDown ? "Shutdown" : "Boot",
123 elapsedRealtime());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800124}
125
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800126void BootAnimation::onFirstRef() {
Mathias Agopianbc726112009-09-23 15:44:05 -0700127 status_t err = mSession->linkToComposerDeath(this);
Wei Wang159a4102018-10-23 23:15:58 -0700128 SLOGE_IF(err, "linkToComposerDeath failed (%s) ", strerror(-err));
Mathias Agopian8434c532009-09-23 18:52:49 -0700129 if (err == NO_ERROR) {
Huihong Luo50a2f362018-08-11 09:27:57 -0700130 // Load the animation content -- this can be slow (eg 200ms)
131 // called before waitForSurfaceFlinger() in main() to avoid wait
132 ALOGD("%sAnimationPreloadTiming start time: %" PRId64 "ms",
133 mShuttingDown ? "Shutdown" : "Boot", elapsedRealtime());
134 preloadAnimation();
135 ALOGD("%sAnimationPreloadStopTiming start time: %" PRId64 "ms",
136 mShuttingDown ? "Shutdown" : "Boot", elapsedRealtime());
Mathias Agopianbc726112009-09-23 15:44:05 -0700137 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800138}
139
Mathias Agopianbc726112009-09-23 15:44:05 -0700140sp<SurfaceComposerClient> BootAnimation::session() const {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800141 return mSession;
142}
143
Mathias Agopianbc726112009-09-23 15:44:05 -0700144
Narayan Kamathafd31e02013-12-03 13:16:03 +0000145void BootAnimation::binderDied(const wp<IBinder>&)
Mathias Agopianbc726112009-09-23 15:44:05 -0700146{
147 // woah, surfaceflinger died!
Wei Wang159a4102018-10-23 23:15:58 -0700148 SLOGD("SurfaceFlinger died, exiting...");
Mathias Agopianbc726112009-09-23 15:44:05 -0700149
150 // calling requestExit() is not enough here because the Surface code
151 // might be blocked on a condition variable that will never be updated.
152 kill( getpid(), SIGKILL );
153 requestExit();
154}
155
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800156status_t BootAnimation::initTexture(Texture* texture, AssetManager& assets,
157 const char* name) {
158 Asset* asset = assets.open(name, Asset::ACCESS_BUFFER);
Yi Kong911ac232019-03-24 01:49:02 -0700159 if (asset == nullptr)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800160 return NO_INIT;
161 SkBitmap bitmap;
Matt Sarett8898c162016-03-24 16:11:01 -0400162 sk_sp<SkData> data = SkData::MakeWithoutCopy(asset->getBuffer(false),
163 asset->getLength());
164 sk_sp<SkImage> image = SkImage::MakeFromEncoded(data);
165 image->asLegacyBitmap(&bitmap, SkImage::kRO_LegacyBitmapMode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800166 asset->close();
167 delete asset;
168
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800169 const int w = bitmap.width();
170 const int h = bitmap.height();
171 const void* p = bitmap.getPixels();
172
173 GLint crop[4] = { 0, h, w, -h };
174 texture->w = w;
175 texture->h = h;
176
177 glGenTextures(1, &texture->name);
178 glBindTexture(GL_TEXTURE_2D, texture->name);
179
Mike Reed42a1d082014-07-07 18:06:18 -0400180 switch (bitmap.colorType()) {
181 case kAlpha_8_SkColorType:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800182 glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, w, h, 0, GL_ALPHA,
183 GL_UNSIGNED_BYTE, p);
184 break;
Mike Reed42a1d082014-07-07 18:06:18 -0400185 case kARGB_4444_SkColorType:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800186 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA,
187 GL_UNSIGNED_SHORT_4_4_4_4, p);
188 break;
Mike Reed42a1d082014-07-07 18:06:18 -0400189 case kN32_SkColorType:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800190 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA,
191 GL_UNSIGNED_BYTE, p);
192 break;
Mike Reed42a1d082014-07-07 18:06:18 -0400193 case kRGB_565_SkColorType:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800194 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, w, h, 0, GL_RGB,
195 GL_UNSIGNED_SHORT_5_6_5, p);
196 break;
197 default:
198 break;
199 }
200
201 glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_CROP_RECT_OES, crop);
202 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
203 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
204 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
205 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700206
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800207 return NO_ERROR;
208}
209
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700210status_t BootAnimation::initTexture(FileMap* map, int* width, int* height)
Mathias Agopiana8826d62009-10-01 03:10:14 -0700211{
Mathias Agopiana8826d62009-10-01 03:10:14 -0700212 SkBitmap bitmap;
Damien Bargiacchif67b3172016-09-07 20:17:14 -0700213 sk_sp<SkData> data = SkData::MakeWithoutCopy(map->getDataPtr(),
214 map->getDataLength());
Matt Sarett8898c162016-03-24 16:11:01 -0400215 sk_sp<SkImage> image = SkImage::MakeFromEncoded(data);
216 image->asLegacyBitmap(&bitmap, SkImage::kRO_LegacyBitmapMode);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700217
Mykola Kondratenko0c1eeb32014-04-15 09:35:44 +0200218 // FileMap memory is never released until application exit.
219 // Release it now as the texture is already loaded and the memory used for
220 // the packed resource can be released.
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700221 delete map;
Mykola Kondratenko0c1eeb32014-04-15 09:35:44 +0200222
Mathias Agopiana8826d62009-10-01 03:10:14 -0700223 const int w = bitmap.width();
224 const int h = bitmap.height();
225 const void* p = bitmap.getPixels();
226
227 GLint crop[4] = { 0, h, w, -h };
228 int tw = 1 << (31 - __builtin_clz(w));
229 int th = 1 << (31 - __builtin_clz(h));
230 if (tw < w) tw <<= 1;
231 if (th < h) th <<= 1;
232
Mike Reed42a1d082014-07-07 18:06:18 -0400233 switch (bitmap.colorType()) {
234 case kN32_SkColorType:
Sai Kiran Korwar27167492015-07-07 20:00:06 +0530235 if (!mUseNpotTextures && (tw != w || th != h)) {
Mathias Agopiana8826d62009-10-01 03:10:14 -0700236 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, tw, th, 0, GL_RGBA,
Yi Kong911ac232019-03-24 01:49:02 -0700237 GL_UNSIGNED_BYTE, nullptr);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700238 glTexSubImage2D(GL_TEXTURE_2D, 0,
239 0, 0, w, h, GL_RGBA, GL_UNSIGNED_BYTE, p);
240 } else {
Sai Kiran Korwar27167492015-07-07 20:00:06 +0530241 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA,
Mathias Agopiana8826d62009-10-01 03:10:14 -0700242 GL_UNSIGNED_BYTE, p);
243 }
244 break;
245
Mike Reed42a1d082014-07-07 18:06:18 -0400246 case kRGB_565_SkColorType:
Sai Kiran Korwar27167492015-07-07 20:00:06 +0530247 if (!mUseNpotTextures && (tw != w || th != h)) {
Mathias Agopiana8826d62009-10-01 03:10:14 -0700248 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, tw, th, 0, GL_RGB,
Yi Kong911ac232019-03-24 01:49:02 -0700249 GL_UNSIGNED_SHORT_5_6_5, nullptr);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700250 glTexSubImage2D(GL_TEXTURE_2D, 0,
251 0, 0, w, h, GL_RGB, GL_UNSIGNED_SHORT_5_6_5, p);
252 } else {
Sai Kiran Korwar27167492015-07-07 20:00:06 +0530253 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, w, h, 0, GL_RGB,
Mathias Agopiana8826d62009-10-01 03:10:14 -0700254 GL_UNSIGNED_SHORT_5_6_5, p);
255 }
256 break;
257 default:
258 break;
259 }
260
261 glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_CROP_RECT_OES, crop);
262
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700263 *width = w;
264 *height = h;
265
Mathias Agopiana8826d62009-10-01 03:10:14 -0700266 return NO_ERROR;
267}
268
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800269status_t BootAnimation::readyToRun() {
270 mAssets.addDefaultAssets();
271
Dominik Laskowski3316a0a2019-01-25 02:56:41 -0800272 mDisplayToken = SurfaceComposerClient::getInternalDisplayToken();
273 if (mDisplayToken == nullptr)
274 return -1;
275
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800276 DisplayInfo dinfo;
Dominik Laskowski3316a0a2019-01-25 02:56:41 -0800277 status_t status = SurfaceComposerClient::getDisplayInfo(mDisplayToken, &dinfo);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800278 if (status)
279 return -1;
280
281 // create the native surface
Jeff Brown0b722fe2012-08-24 22:40:14 -0700282 sp<SurfaceControl> control = session()->createSurface(String8("BootAnimation"),
283 dinfo.w, dinfo.h, PIXEL_FORMAT_RGB_565);
Mathias Agopian439863f2011-06-28 19:09:31 -0700284
Robert Carre13b58e2017-08-31 14:50:44 -0700285 SurfaceComposerClient::Transaction t;
286 t.setLayer(control, 0x40000000)
287 .apply();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800288
Mathias Agopian17f638b2009-04-16 20:04:08 -0700289 sp<Surface> s = control->getSurface();
290
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800291 // initialize opengl and egl
Mathias Agopian738b9a42009-08-06 16:41:02 -0700292 const EGLint attribs[] = {
Mathias Agopian1b253b72011-08-15 15:20:22 -0700293 EGL_RED_SIZE, 8,
294 EGL_GREEN_SIZE, 8,
295 EGL_BLUE_SIZE, 8,
Mathias Agopiana8826d62009-10-01 03:10:14 -0700296 EGL_DEPTH_SIZE, 0,
297 EGL_NONE
Mathias Agopian738b9a42009-08-06 16:41:02 -0700298 };
Andreas Gampecfedceb2014-09-30 21:48:18 -0700299 EGLint w, h;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800300 EGLint numConfigs;
301 EGLConfig config;
302 EGLSurface surface;
303 EGLContext context;
Mathias Agopian627e7b52009-05-21 19:21:59 -0700304
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800305 EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
Mathias Agopian627e7b52009-05-21 19:21:59 -0700306
Yi Kong911ac232019-03-24 01:49:02 -0700307 eglInitialize(display, nullptr, nullptr);
Mathias Agopian1b253b72011-08-15 15:20:22 -0700308 eglChooseConfig(display, attribs, &config, 1, &numConfigs);
Yi Kong911ac232019-03-24 01:49:02 -0700309 surface = eglCreateWindowSurface(display, config, s.get(), nullptr);
310 context = eglCreateContext(display, config, nullptr, nullptr);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800311 eglQuerySurface(display, surface, EGL_WIDTH, &w);
312 eglQuerySurface(display, surface, EGL_HEIGHT, &h);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700313
Mathias Agopianabac0102009-07-31 14:47:00 -0700314 if (eglMakeCurrent(display, surface, surface, context) == EGL_FALSE)
315 return NO_INIT;
Mathias Agopiana8826d62009-10-01 03:10:14 -0700316
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800317 mDisplay = display;
318 mContext = context;
319 mSurface = surface;
320 mWidth = w;
321 mHeight = h;
Mathias Agopian17f638b2009-04-16 20:04:08 -0700322 mFlingerSurfaceControl = control;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800323 mFlingerSurface = s;
Adrian Roos9ee5dff2018-08-22 20:19:49 +0200324 mTargetInset = -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800325
Huihong Luo50a2f362018-08-11 09:27:57 -0700326 return NO_ERROR;
327}
328
329bool BootAnimation::preloadAnimation() {
330 findBootAnimationFile();
331 if (!mZipFileName.isEmpty()) {
332 mAnimation = loadAnimation(mZipFileName);
333 return (mAnimation != nullptr);
334 }
335
336 return false;
337}
338
339void BootAnimation::findBootAnimationFile() {
Elliott Hughesc367d482013-10-29 13:12:55 -0700340 // If the device has encryption turned on or is in process
Jason parksbd9a08d2011-01-31 15:04:34 -0600341 // of being encrypted we show the encrypted boot animation.
342 char decrypt[PROPERTY_VALUE_MAX];
343 property_get("vold.decrypt", decrypt, "");
344
Keun-young Parkb5938422017-03-23 13:46:24 -0700345 bool encryptedAnimation = atoi(decrypt) != 0 ||
346 !strcmp("trigger_restart_min_framework", decrypt);
Jason parksbd9a08d2011-01-31 15:04:34 -0600347
Jaekyun Seokc521bb32018-01-30 11:52:47 +0900348 if (!mShuttingDown && encryptedAnimation) {
349 static const char* encryptedBootFiles[] =
350 {PRODUCT_ENCRYPTED_BOOTANIMATION_FILE, SYSTEM_ENCRYPTED_BOOTANIMATION_FILE};
351 for (const char* f : encryptedBootFiles) {
352 if (access(f, R_OK) == 0) {
353 mZipFileName = f;
Huihong Luo50a2f362018-08-11 09:27:57 -0700354 return;
Jaekyun Seokc521bb32018-01-30 11:52:47 +0900355 }
356 }
Jason parksbd9a08d2011-01-31 15:04:34 -0600357 }
Jaekyun Seokc521bb32018-01-30 11:52:47 +0900358 static const char* bootFiles[] =
359 {PRODUCT_BOOTANIMATION_FILE, OEM_BOOTANIMATION_FILE, SYSTEM_BOOTANIMATION_FILE};
Keun-young Parkb5938422017-03-23 13:46:24 -0700360 static const char* shutdownFiles[] =
Jaekyun Seokc521bb32018-01-30 11:52:47 +0900361 {PRODUCT_SHUTDOWNANIMATION_FILE, OEM_SHUTDOWNANIMATION_FILE, SYSTEM_SHUTDOWNANIMATION_FILE};
Keun-young Parkb5938422017-03-23 13:46:24 -0700362
363 for (const char* f : (!mShuttingDown ? bootFiles : shutdownFiles)) {
364 if (access(f, R_OK) == 0) {
365 mZipFileName = f;
Huihong Luo50a2f362018-08-11 09:27:57 -0700366 return;
Keun-young Parkb5938422017-03-23 13:46:24 -0700367 }
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700368 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800369}
370
Mathias Agopiana8826d62009-10-01 03:10:14 -0700371bool BootAnimation::threadLoop()
372{
373 bool r;
Narayan Kamathafd31e02013-12-03 13:16:03 +0000374 // We have no bootanimation file, so we use the stock android logo
375 // animation.
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700376 if (mZipFileName.isEmpty()) {
Mathias Agopiana8826d62009-10-01 03:10:14 -0700377 r = android();
378 } else {
379 r = movie();
380 }
381
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800382 eglMakeCurrent(mDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
383 eglDestroyContext(mDisplay, mContext);
384 eglDestroySurface(mDisplay, mSurface);
Mathias Agopian6cf0db22009-04-17 19:36:26 -0700385 mFlingerSurface.clear();
Mathias Agopian17f638b2009-04-16 20:04:08 -0700386 mFlingerSurfaceControl.clear();
Mathias Agopian627e7b52009-05-21 19:21:59 -0700387 eglTerminate(mDisplay);
Sai Kiran Korwar3eee9fb2015-06-16 17:13:35 +0530388 eglReleaseThread();
Mathias Agopian627e7b52009-05-21 19:21:59 -0700389 IPCThreadState::self()->stopProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800390 return r;
391}
392
Mathias Agopiana8826d62009-10-01 03:10:14 -0700393bool BootAnimation::android()
394{
Wei Wang159a4102018-10-23 23:15:58 -0700395 SLOGD("%sAnimationShownTiming start time: %" PRId64 "ms", mShuttingDown ? "Shutdown" : "Boot",
Keun-young Parkd1794cd2017-04-04 12:21:19 -0700396 elapsedRealtime());
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700397 initTexture(&mAndroid[0], mAssets, "images/android-logo-mask.png");
398 initTexture(&mAndroid[1], mAssets, "images/android-logo-shine.png");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800399
Ed Coyne7464ac92017-06-08 12:26:48 -0700400 mCallbacks->init({});
401
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800402 // clear screen
Mathias Agopiana8826d62009-10-01 03:10:14 -0700403 glShadeModel(GL_FLAT);
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700404 glDisable(GL_DITHER);
405 glDisable(GL_SCISSOR_TEST);
Mathias Agopian59f19e42011-05-06 19:22:12 -0700406 glClearColor(0,0,0,1);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800407 glClear(GL_COLOR_BUFFER_BIT);
408 eglSwapBuffers(mDisplay, mSurface);
409
Mathias Agopiana8826d62009-10-01 03:10:14 -0700410 glEnable(GL_TEXTURE_2D);
411 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
412
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700413 const GLint xc = (mWidth - mAndroid[0].w) / 2;
414 const GLint yc = (mHeight - mAndroid[0].h) / 2;
415 const Rect updateRect(xc, yc, xc + mAndroid[0].w, yc + mAndroid[0].h);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800416
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800417 glScissor(updateRect.left, mHeight - updateRect.bottom, updateRect.width(),
418 updateRect.height());
419
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700420 // Blend state
421 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
422 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
423
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800424 const nsecs_t startTime = systemTime();
425 do {
Mathias Agopian13796652009-03-24 22:49:21 -0700426 nsecs_t now = systemTime();
427 double time = now - startTime;
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700428 float t = 4.0f * float(time / us2ns(16667)) / mAndroid[1].w;
429 GLint offset = (1 - (t - floorf(t))) * mAndroid[1].w;
430 GLint x = xc - offset;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800431
Mathias Agopian81668642009-07-28 11:41:30 -0700432 glDisable(GL_SCISSOR_TEST);
433 glClear(GL_COLOR_BUFFER_BIT);
434
435 glEnable(GL_SCISSOR_TEST);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800436 glDisable(GL_BLEND);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800437 glBindTexture(GL_TEXTURE_2D, mAndroid[1].name);
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700438 glDrawTexiOES(x, yc, 0, mAndroid[1].w, mAndroid[1].h);
439 glDrawTexiOES(x + mAndroid[1].w, yc, 0, mAndroid[1].w, mAndroid[1].h);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800440
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700441 glEnable(GL_BLEND);
442 glBindTexture(GL_TEXTURE_2D, mAndroid[0].name);
443 glDrawTexiOES(xc, yc, 0, mAndroid[0].w, mAndroid[0].h);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800444
Mathias Agopian627e7b52009-05-21 19:21:59 -0700445 EGLBoolean res = eglSwapBuffers(mDisplay, mSurface);
446 if (res == EGL_FALSE)
447 break;
448
Mathias Agopian13796652009-03-24 22:49:21 -0700449 // 12fps: don't animate too fast to preserve CPU
450 const nsecs_t sleepTime = 83333 - ns2us(systemTime() - now);
451 if (sleepTime > 0)
Mathias Agopiana8826d62009-10-01 03:10:14 -0700452 usleep(sleepTime);
Kevin Hesterd3782b22012-04-26 10:38:55 -0700453
454 checkExit();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800455 } while (!exitPending());
456
457 glDeleteTextures(1, &mAndroid[0].name);
458 glDeleteTextures(1, &mAndroid[1].name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800459 return false;
460}
461
Kevin Hesterd3782b22012-04-26 10:38:55 -0700462void BootAnimation::checkExit() {
463 // Allow surface flinger to gracefully request shutdown
464 char value[PROPERTY_VALUE_MAX];
465 property_get(EXIT_PROP_NAME, value, "0");
466 int exitnow = atoi(value);
467 if (exitnow) {
468 requestExit();
Ed Coyne7464ac92017-06-08 12:26:48 -0700469 mCallbacks->shutdown();
Kevin Hesterd3782b22012-04-26 10:38:55 -0700470 }
471}
472
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700473bool BootAnimation::validClock(const Animation::Part& part) {
474 return part.clockPosX != TEXT_MISSING_VALUE && part.clockPosY != TEXT_MISSING_VALUE;
475}
476
477bool parseTextCoord(const char* str, int* dest) {
478 if (strcmp("c", str) == 0) {
479 *dest = TEXT_CENTER_VALUE;
480 return true;
481 }
482
483 char* end;
484 int val = (int) strtol(str, &end, 0);
485 if (end == str || *end != '\0' || val == INT_MAX || val == INT_MIN) {
486 return false;
487 }
488 *dest = val;
489 return true;
490}
491
492// Parse two position coordinates. If only string is non-empty, treat it as the y value.
493void parsePosition(const char* str1, const char* str2, int* x, int* y) {
494 bool success = false;
495 if (strlen(str1) == 0) { // No values were specified
496 // success = false
497 } else if (strlen(str2) == 0) { // we have only one value
498 if (parseTextCoord(str1, y)) {
499 *x = TEXT_CENTER_VALUE;
500 success = true;
501 }
502 } else {
503 if (parseTextCoord(str1, x) && parseTextCoord(str2, y)) {
504 success = true;
505 }
506 }
507
508 if (!success) {
509 *x = TEXT_MISSING_VALUE;
510 *y = TEXT_MISSING_VALUE;
511 }
512}
513
Jesse Hall083b84c2014-09-22 10:51:09 -0700514// Parse a color represented as an HTML-style 'RRGGBB' string: each pair of
515// characters in str is a hex number in [0, 255], which are converted to
516// floating point values in the range [0.0, 1.0] and placed in the
517// corresponding elements of color.
518//
519// If the input string isn't valid, parseColor returns false and color is
520// left unchanged.
521static bool parseColor(const char str[7], float color[3]) {
522 float tmpColor[3];
523 for (int i = 0; i < 3; i++) {
524 int val = 0;
525 for (int j = 0; j < 2; j++) {
526 val *= 16;
527 char c = str[2*i + j];
528 if (c >= '0' && c <= '9') val += c - '0';
529 else if (c >= 'A' && c <= 'F') val += (c - 'A') + 10;
530 else if (c >= 'a' && c <= 'f') val += (c - 'a') + 10;
531 else return false;
532 }
533 tmpColor[i] = static_cast<float>(val) / 255.0f;
534 }
535 memcpy(color, tmpColor, sizeof(tmpColor));
536 return true;
537}
538
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700539
540static bool readFile(ZipFileRO* zip, const char* name, String8& outString)
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700541{
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700542 ZipEntryRO entry = zip->findEntryByName(name);
Wei Wang159a4102018-10-23 23:15:58 -0700543 SLOGE_IF(!entry, "couldn't find %s", name);
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700544 if (!entry) {
545 return false;
546 }
547
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700548 FileMap* entryMap = zip->createEntryFileMap(entry);
549 zip->releaseEntry(entry);
Wei Wang159a4102018-10-23 23:15:58 -0700550 SLOGE_IF(!entryMap, "entryMap is null");
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700551 if (!entryMap) {
552 return false;
553 }
554
555 outString.setTo((char const*)entryMap->getDataPtr(), entryMap->getDataLength());
Narayan Kamath688ff4c2015-02-23 15:47:54 +0000556 delete entryMap;
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700557 return true;
558}
559
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700560// The font image should be a 96x2 array of character images. The
561// columns are the printable ASCII characters 0x20 - 0x7f. The
562// top row is regular text; the bottom row is bold.
563status_t BootAnimation::initFont(Font* font, const char* fallback) {
564 status_t status = NO_ERROR;
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800565
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700566 if (font->map != nullptr) {
567 glGenTextures(1, &font->texture.name);
568 glBindTexture(GL_TEXTURE_2D, font->texture.name);
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800569
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700570 status = initTexture(font->map, &font->texture.w, &font->texture.h);
571
572 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
573 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
574 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
575 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
576 } else if (fallback != nullptr) {
577 status = initTexture(&font->texture, mAssets, fallback);
578 } else {
579 return NO_INIT;
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800580 }
581
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700582 if (status == NO_ERROR) {
583 font->char_width = font->texture.w / FONT_NUM_COLS;
584 font->char_height = font->texture.h / FONT_NUM_ROWS / 2; // There are bold and regular rows
585 }
586
587 return status;
588}
589
590void BootAnimation::drawText(const char* str, const Font& font, bool bold, int* x, int* y) {
591 glEnable(GL_BLEND); // Allow us to draw on top of the animation
592 glBindTexture(GL_TEXTURE_2D, font.texture.name);
593
594 const int len = strlen(str);
595 const int strWidth = font.char_width * len;
596
597 if (*x == TEXT_CENTER_VALUE) {
598 *x = (mWidth - strWidth) / 2;
599 } else if (*x < 0) {
600 *x = mWidth + *x - strWidth;
601 }
602 if (*y == TEXT_CENTER_VALUE) {
603 *y = (mHeight - font.char_height) / 2;
604 } else if (*y < 0) {
605 *y = mHeight + *y - font.char_height;
606 }
607
608 int cropRect[4] = { 0, 0, font.char_width, -font.char_height };
609
610 for (int i = 0; i < len; i++) {
611 char c = str[i];
612
613 if (c < FONT_BEGIN_CHAR || c > FONT_END_CHAR) {
614 c = '?';
615 }
616
617 // Crop the texture to only the pixels in the current glyph
618 const int charPos = (c - FONT_BEGIN_CHAR); // Position in the list of valid characters
619 const int row = charPos / FONT_NUM_COLS;
620 const int col = charPos % FONT_NUM_COLS;
621 cropRect[0] = col * font.char_width; // Left of column
622 cropRect[1] = row * font.char_height * 2; // Top of row
623 // Move down to bottom of regular (one char_heigh) or bold (two char_heigh) line
624 cropRect[1] += bold ? 2 * font.char_height : font.char_height;
625 glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_CROP_RECT_OES, cropRect);
626
627 glDrawTexiOES(*x, *y, 0, font.char_width, font.char_height);
628
629 *x += font.char_width;
630 }
631
632 glDisable(GL_BLEND); // Return to the animation's default behaviour
633 glBindTexture(GL_TEXTURE_2D, 0);
634}
635
Damien Bargiacchi9071db12016-10-28 17:38:22 -0700636// We render 12 or 24 hour time.
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700637void BootAnimation::drawClock(const Font& font, const int xPos, const int yPos) {
Damien Bargiacchi9071db12016-10-28 17:38:22 -0700638 static constexpr char TIME_FORMAT_12[] = "%l:%M";
639 static constexpr char TIME_FORMAT_24[] = "%H:%M";
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700640 static constexpr int TIME_LENGTH = 6;
641
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800642 time_t rawtime;
643 time(&rawtime);
644 struct tm* timeInfo = localtime(&rawtime);
645
646 char timeBuff[TIME_LENGTH];
Damien Bargiacchi9071db12016-10-28 17:38:22 -0700647 const char* timeFormat = mTimeFormat12Hour ? TIME_FORMAT_12 : TIME_FORMAT_24;
648 size_t length = strftime(timeBuff, TIME_LENGTH, timeFormat, timeInfo);
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800649
650 if (length != TIME_LENGTH - 1) {
Wei Wang159a4102018-10-23 23:15:58 -0700651 SLOGE("Couldn't format time; abandoning boot animation clock");
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800652 mClockEnabled = false;
653 return;
654 }
655
Damien Bargiacchi45a76442016-12-05 18:02:18 -0800656 char* out = timeBuff[0] == ' ' ? &timeBuff[1] : &timeBuff[0];
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700657 int x = xPos;
658 int y = yPos;
Damien Bargiacchi45a76442016-12-05 18:02:18 -0800659 drawText(out, font, false, &x, &y);
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800660}
661
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700662bool BootAnimation::parseAnimationDesc(Animation& animation)
Mathias Agopiana8826d62009-10-01 03:10:14 -0700663{
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700664 String8 desString;
665
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700666 if (!readFile(animation.zip, "desc.txt", desString)) {
Narayan Kamathafd31e02013-12-03 13:16:03 +0000667 return false;
668 }
Mathias Agopiana8826d62009-10-01 03:10:14 -0700669 char const* s = desString.string();
670
Mathias Agopiana8826d62009-10-01 03:10:14 -0700671 // Parse the description file
672 for (;;) {
673 const char* endl = strstr(s, "\n");
Yi Kong911ac232019-03-24 01:49:02 -0700674 if (endl == nullptr) break;
Mathias Agopiana8826d62009-10-01 03:10:14 -0700675 String8 line(s, endl - s);
676 const char* l = line.string();
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800677 int fps = 0;
678 int width = 0;
679 int height = 0;
680 int count = 0;
681 int pause = 0;
Narayan Kamathafd31e02013-12-03 13:16:03 +0000682 char path[ANIM_ENTRY_NAME_MAX];
Jesse Hall083b84c2014-09-22 10:51:09 -0700683 char color[7] = "000000"; // default to black if unspecified
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700684 char clockPos1[TEXT_POS_LEN_MAX + 1] = "";
685 char clockPos2[TEXT_POS_LEN_MAX + 1] = "";
Jesse Hall083b84c2014-09-22 10:51:09 -0700686
Kevin Hesterd3782b22012-04-26 10:38:55 -0700687 char pathType;
Mathias Agopiana8826d62009-10-01 03:10:14 -0700688 if (sscanf(l, "%d %d %d", &width, &height, &fps) == 3) {
Wei Wang159a4102018-10-23 23:15:58 -0700689 // SLOGD("> w=%d, h=%d, fps=%d", width, height, fps);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700690 animation.width = width;
691 animation.height = height;
692 animation.fps = fps;
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700693 } else if (sscanf(l, " %c %d %d %s #%6s %16s %16s",
694 &pathType, &count, &pause, path, color, clockPos1, clockPos2) >= 4) {
Wei Wang159a4102018-10-23 23:15:58 -0700695 //SLOGD("> type=%c, count=%d, pause=%d, path=%s, color=%s, clockPos1=%s, clockPos2=%s",
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700696 // pathType, count, pause, path, color, clockPos1, clockPos2);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700697 Animation::Part part;
Kevin Hesterd3782b22012-04-26 10:38:55 -0700698 part.playUntilComplete = pathType == 'c';
Mathias Agopiana8826d62009-10-01 03:10:14 -0700699 part.count = count;
700 part.pause = pause;
701 part.path = path;
Yi Kong911ac232019-03-24 01:49:02 -0700702 part.audioData = nullptr;
703 part.animation = nullptr;
Jesse Hall083b84c2014-09-22 10:51:09 -0700704 if (!parseColor(color, part.backgroundColor)) {
Wei Wang159a4102018-10-23 23:15:58 -0700705 SLOGE("> invalid color '#%s'", color);
Jesse Hall083b84c2014-09-22 10:51:09 -0700706 part.backgroundColor[0] = 0.0f;
707 part.backgroundColor[1] = 0.0f;
708 part.backgroundColor[2] = 0.0f;
709 }
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700710 parsePosition(clockPos1, clockPos2, &part.clockPosX, &part.clockPosY);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700711 animation.parts.add(part);
712 }
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700713 else if (strcmp(l, "$SYSTEM") == 0) {
Wei Wang159a4102018-10-23 23:15:58 -0700714 // SLOGD("> SYSTEM");
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700715 Animation::Part part;
716 part.playUntilComplete = false;
717 part.count = 1;
718 part.pause = 0;
Yi Kong911ac232019-03-24 01:49:02 -0700719 part.audioData = nullptr;
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700720 part.animation = loadAnimation(String8(SYSTEM_BOOTANIMATION_FILE));
Yi Kong911ac232019-03-24 01:49:02 -0700721 if (part.animation != nullptr)
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700722 animation.parts.add(part);
723 }
Mathias Agopiana8826d62009-10-01 03:10:14 -0700724 s = ++endl;
725 }
726
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700727 return true;
728}
729
730bool BootAnimation::preloadZip(Animation& animation)
731{
Mathias Agopiana8826d62009-10-01 03:10:14 -0700732 // read all the data structures
733 const size_t pcount = animation.parts.size();
Yi Kong911ac232019-03-24 01:49:02 -0700734 void *cookie = nullptr;
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400735 ZipFileRO* zip = animation.zip;
736 if (!zip->startIteration(&cookie)) {
Narayan Kamathafd31e02013-12-03 13:16:03 +0000737 return false;
738 }
739
740 ZipEntryRO entry;
741 char name[ANIM_ENTRY_NAME_MAX];
Yi Kong911ac232019-03-24 01:49:02 -0700742 while ((entry = zip->nextEntry(cookie)) != nullptr) {
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400743 const int foundEntryName = zip->getEntryFileName(entry, name, ANIM_ENTRY_NAME_MAX);
Narayan Kamathafd31e02013-12-03 13:16:03 +0000744 if (foundEntryName > ANIM_ENTRY_NAME_MAX || foundEntryName == -1) {
Wei Wang159a4102018-10-23 23:15:58 -0700745 SLOGE("Error fetching entry file name");
Narayan Kamathafd31e02013-12-03 13:16:03 +0000746 continue;
747 }
748
749 const String8 entryName(name);
750 const String8 path(entryName.getPathDir());
751 const String8 leaf(entryName.getPathLeaf());
752 if (leaf.size() > 0) {
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700753 if (entryName == CLOCK_FONT_ZIP_NAME) {
754 FileMap* map = zip->createEntryFileMap(entry);
755 if (map) {
756 animation.clockFont.map = map;
757 }
758 continue;
759 }
760
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400761 for (size_t j = 0; j < pcount; j++) {
Narayan Kamathafd31e02013-12-03 13:16:03 +0000762 if (path == animation.parts[j].path) {
Narayan Kamath4600dd02015-06-16 12:02:57 +0100763 uint16_t method;
Narayan Kamathafd31e02013-12-03 13:16:03 +0000764 // supports only stored png files
Yi Kong911ac232019-03-24 01:49:02 -0700765 if (zip->getEntryInfo(entry, &method, nullptr, nullptr, nullptr, nullptr, nullptr)) {
Narayan Kamathafd31e02013-12-03 13:16:03 +0000766 if (method == ZipFileRO::kCompressStored) {
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400767 FileMap* map = zip->createEntryFileMap(entry);
Narayan Kamathafd31e02013-12-03 13:16:03 +0000768 if (map) {
Narayan Kamathafd31e02013-12-03 13:16:03 +0000769 Animation::Part& part(animation.parts.editItemAt(j));
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700770 if (leaf == "audio.wav") {
771 // a part may have at most one audio file
Geoffrey Pitschd6d9a1d2016-06-08 00:38:58 -0700772 part.audioData = (uint8_t *)map->getDataPtr();
773 part.audioLength = map->getDataLength();
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400774 } else if (leaf == "trim.txt") {
775 part.trimData.setTo((char const*)map->getDataPtr(),
776 map->getDataLength());
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700777 } else {
778 Animation::Frame frame;
779 frame.name = leaf;
780 frame.map = map;
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400781 frame.trimWidth = animation.width;
782 frame.trimHeight = animation.height;
783 frame.trimX = 0;
784 frame.trimY = 0;
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700785 part.frames.add(frame);
786 }
Mathias Agopiana8826d62009-10-01 03:10:14 -0700787 }
Geoffrey Pitschd6d9a1d2016-06-08 00:38:58 -0700788 } else {
Wei Wang159a4102018-10-23 23:15:58 -0700789 SLOGE("bootanimation.zip is compressed; must be only stored");
Mathias Agopiana8826d62009-10-01 03:10:14 -0700790 }
791 }
792 }
793 }
794 }
795 }
796
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400797 // If there is trimData present, override the positioning defaults.
798 for (Animation::Part& part : animation.parts) {
799 const char* trimDataStr = part.trimData.string();
800 for (size_t frameIdx = 0; frameIdx < part.frames.size(); frameIdx++) {
801 const char* endl = strstr(trimDataStr, "\n");
802 // No more trimData for this part.
Yi Kong911ac232019-03-24 01:49:02 -0700803 if (endl == nullptr) {
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400804 break;
805 }
806 String8 line(trimDataStr, endl - trimDataStr);
807 const char* lineStr = line.string();
808 trimDataStr = ++endl;
809 int width = 0, height = 0, x = 0, y = 0;
810 if (sscanf(lineStr, "%dx%d+%d+%d", &width, &height, &x, &y) == 4) {
811 Animation::Frame& frame(part.frames.editItemAt(frameIdx));
812 frame.trimWidth = width;
813 frame.trimHeight = height;
814 frame.trimX = x;
815 frame.trimY = y;
816 } else {
Wei Wang159a4102018-10-23 23:15:58 -0700817 SLOGE("Error parsing trim.txt, line: %s", lineStr);
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400818 break;
819 }
820 }
821 }
822
823 zip->endIteration(cookie);
Narayan Kamathafd31e02013-12-03 13:16:03 +0000824
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700825 return true;
826}
827
828bool BootAnimation::movie()
829{
Huihong Luo50a2f362018-08-11 09:27:57 -0700830 if (mAnimation == nullptr) {
831 mAnimation = loadAnimation(mZipFileName);
832 }
833
834 if (mAnimation == nullptr)
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700835 return false;
836
Huihong Luo50a2f362018-08-11 09:27:57 -0700837 // mCallbacks->init() may get called recursively,
838 // this loop is needed to get the same results
839 for (const Animation::Part& part : mAnimation->parts) {
840 if (part.animation != nullptr) {
841 mCallbacks->init(part.animation->parts);
842 }
843 }
844 mCallbacks->init(mAnimation->parts);
845
Damien Bargiacchi97480862016-03-29 14:55:55 -0700846 bool anyPartHasClock = false;
Huihong Luo50a2f362018-08-11 09:27:57 -0700847 for (size_t i=0; i < mAnimation->parts.size(); i++) {
848 if(validClock(mAnimation->parts[i])) {
Damien Bargiacchi97480862016-03-29 14:55:55 -0700849 anyPartHasClock = true;
850 break;
851 }
852 }
853 if (!anyPartHasClock) {
854 mClockEnabled = false;
855 }
856
Sai Kiran Korwar27167492015-07-07 20:00:06 +0530857 // Check if npot textures are supported
858 mUseNpotTextures = false;
859 String8 gl_extensions;
860 const char* exts = reinterpret_cast<const char*>(glGetString(GL_EXTENSIONS));
861 if (!exts) {
862 glGetError();
863 } else {
864 gl_extensions.setTo(exts);
865 if ((gl_extensions.find("GL_ARB_texture_non_power_of_two") != -1) ||
866 (gl_extensions.find("GL_OES_texture_npot") != -1)) {
867 mUseNpotTextures = true;
868 }
869 }
870
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800871 // Blend required to draw time on top of animation frames.
872 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700873 glShadeModel(GL_FLAT);
874 glDisable(GL_DITHER);
875 glDisable(GL_SCISSOR_TEST);
876 glDisable(GL_BLEND);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700877
878 glBindTexture(GL_TEXTURE_2D, 0);
879 glEnable(GL_TEXTURE_2D);
880 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
881 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
882 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
883 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
884 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
885
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700886 bool clockFontInitialized = false;
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800887 if (mClockEnabled) {
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700888 clockFontInitialized =
Huihong Luo50a2f362018-08-11 09:27:57 -0700889 (initFont(&mAnimation->clockFont, CLOCK_FONT_ASSET) == NO_ERROR);
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700890 mClockEnabled = clockFontInitialized;
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800891 }
892
Damien Bargiacchi97480862016-03-29 14:55:55 -0700893 if (mClockEnabled && !updateIsTimeAccurate()) {
894 mTimeCheckThread = new TimeCheckThread(this);
895 mTimeCheckThread->run("BootAnimation::TimeCheckThread", PRIORITY_NORMAL);
896 }
897
Huihong Luo50a2f362018-08-11 09:27:57 -0700898 playAnimation(*mAnimation);
Damien Bargiacchi97480862016-03-29 14:55:55 -0700899
Geoffrey Pitscha9173532016-07-19 14:56:39 -0400900 if (mTimeCheckThread != nullptr) {
Damien Bargiacchi97480862016-03-29 14:55:55 -0700901 mTimeCheckThread->requestExit();
Geoffrey Pitscha9173532016-07-19 14:56:39 -0400902 mTimeCheckThread = nullptr;
903 }
904
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700905 if (clockFontInitialized) {
Huihong Luo50a2f362018-08-11 09:27:57 -0700906 glDeleteTextures(1, &mAnimation->clockFont.texture.name);
Andriy Naborskyy815e51d2016-03-24 16:43:34 -0700907 }
908
Huihong Luo50a2f362018-08-11 09:27:57 -0700909 releaseAnimation(mAnimation);
910 mAnimation = nullptr;
Damien Bargiacchi1a8a2132018-07-24 15:20:26 -0700911
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700912 return false;
913}
914
915bool BootAnimation::playAnimation(const Animation& animation)
916{
917 const size_t pcount = animation.parts.size();
Mathias Agopiana8826d62009-10-01 03:10:14 -0700918 nsecs_t frameDuration = s2ns(1) / animation.fps;
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400919 const int animationX = (mWidth - animation.width) / 2;
920 const int animationY = (mHeight - animation.height) / 2;
Mathias Agopian9f3020d2009-11-06 16:30:18 -0800921
Wei Wang159a4102018-10-23 23:15:58 -0700922 SLOGD("%sAnimationShownTiming start time: %" PRId64 "ms", mShuttingDown ? "Shutdown" : "Boot",
Keun-young Parkd1794cd2017-04-04 12:21:19 -0700923 elapsedRealtime());
Narayan Kamathafd31e02013-12-03 13:16:03 +0000924 for (size_t i=0 ; i<pcount ; i++) {
Mathias Agopiana8826d62009-10-01 03:10:14 -0700925 const Animation::Part& part(animation.parts[i]);
926 const size_t fcount = part.frames.size();
927 glBindTexture(GL_TEXTURE_2D, 0);
928
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700929 // Handle animation package
Yi Kong911ac232019-03-24 01:49:02 -0700930 if (part.animation != nullptr) {
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700931 playAnimation(*part.animation);
932 if (exitPending())
933 break;
934 continue; //to next part
935 }
936
Mathias Agopiana8826d62009-10-01 03:10:14 -0700937 for (int r=0 ; !part.count || r<part.count ; r++) {
Kevin Hesterd3782b22012-04-26 10:38:55 -0700938 // Exit any non playuntil complete parts immediately
939 if(exitPending() && !part.playUntilComplete)
940 break;
941
Ed Coyne7464ac92017-06-08 12:26:48 -0700942 mCallbacks->playPart(i, part, r);
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700943
Jesse Hall083b84c2014-09-22 10:51:09 -0700944 glClearColor(
945 part.backgroundColor[0],
946 part.backgroundColor[1],
947 part.backgroundColor[2],
948 1.0f);
949
Narayan Kamathafd31e02013-12-03 13:16:03 +0000950 for (size_t j=0 ; j<fcount && (!exitPending() || part.playUntilComplete) ; j++) {
Mathias Agopiana8826d62009-10-01 03:10:14 -0700951 const Animation::Frame& frame(part.frames[j]);
Mathias Agopiandb7dd2a2012-05-12 15:08:21 -0700952 nsecs_t lastFrame = systemTime();
Mathias Agopiana8826d62009-10-01 03:10:14 -0700953
954 if (r > 0) {
955 glBindTexture(GL_TEXTURE_2D, frame.tid);
956 } else {
957 if (part.count != 1) {
958 glGenTextures(1, &frame.tid);
959 glBindTexture(GL_TEXTURE_2D, frame.tid);
960 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
961 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
962 }
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700963 int w, h;
964 initTexture(frame.map, &w, &h);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700965 }
966
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400967 const int xc = animationX + frame.trimX;
968 const int yc = animationY + frame.trimY;
969 Region clearReg(Rect(mWidth, mHeight));
970 clearReg.subtractSelf(Rect(xc, yc, xc+frame.trimWidth, yc+frame.trimHeight));
Mathias Agopian9f3020d2009-11-06 16:30:18 -0800971 if (!clearReg.isEmpty()) {
972 Region::const_iterator head(clearReg.begin());
973 Region::const_iterator tail(clearReg.end());
974 glEnable(GL_SCISSOR_TEST);
975 while (head != tail) {
Andreas Gampecfedceb2014-09-30 21:48:18 -0700976 const Rect& r2(*head++);
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400977 glScissor(r2.left, mHeight - r2.bottom, r2.width(), r2.height());
Mathias Agopian9f3020d2009-11-06 16:30:18 -0800978 glClear(GL_COLOR_BUFFER_BIT);
979 }
980 glDisable(GL_SCISSOR_TEST);
981 }
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400982 // specify the y center as ceiling((mHeight - frame.trimHeight) / 2)
983 // which is equivalent to mHeight - (yc + frame.trimHeight)
984 glDrawTexiOES(xc, mHeight - (yc + frame.trimHeight),
985 0, frame.trimWidth, frame.trimHeight);
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700986 if (mClockEnabled && mTimeIsAccurate && validClock(part)) {
987 drawClock(animation.clockFont, part.clockPosX, part.clockPosY);
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800988 }
Adrian Roos9ee5dff2018-08-22 20:19:49 +0200989 handleViewport(frameDuration);
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800990
Mathias Agopiana8826d62009-10-01 03:10:14 -0700991 eglSwapBuffers(mDisplay, mSurface);
992
993 nsecs_t now = systemTime();
994 nsecs_t delay = frameDuration - (now - lastFrame);
Wei Wang159a4102018-10-23 23:15:58 -0700995 //SLOGD("%lld, %lld", ns2ms(now - lastFrame), ns2ms(delay));
Mathias Agopiana8826d62009-10-01 03:10:14 -0700996 lastFrame = now;
Mathias Agopiandb7dd2a2012-05-12 15:08:21 -0700997
998 if (delay > 0) {
999 struct timespec spec;
1000 spec.tv_sec = (now + delay) / 1000000000;
1001 spec.tv_nsec = (now + delay) % 1000000000;
1002 int err;
1003 do {
Yi Kong911ac232019-03-24 01:49:02 -07001004 err = clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &spec, nullptr);
Mathias Agopiandb7dd2a2012-05-12 15:08:21 -07001005 } while (err<0 && errno == EINTR);
1006 }
Kevin Hesterd3782b22012-04-26 10:38:55 -07001007
1008 checkExit();
Mathias Agopiana8826d62009-10-01 03:10:14 -07001009 }
Kevin Hesterd3782b22012-04-26 10:38:55 -07001010
Mathias Agopiana8826d62009-10-01 03:10:14 -07001011 usleep(part.pause * ns2us(frameDuration));
Kevin Hesterd3782b22012-04-26 10:38:55 -07001012
1013 // For infinite parts, we've now played them at least once, so perhaps exit
Adrian Roos9ee5dff2018-08-22 20:19:49 +02001014 if(exitPending() && !part.count && mCurrentInset >= mTargetInset)
Kevin Hesterd3782b22012-04-26 10:38:55 -07001015 break;
Mathias Agopiana8826d62009-10-01 03:10:14 -07001016 }
1017
Geoffrey Pitsch2fb30fb2016-07-06 16:16:20 -04001018 }
1019
1020 // Free textures created for looping parts now that the animation is done.
1021 for (const Animation::Part& part : animation.parts) {
Mathias Agopiana8826d62009-10-01 03:10:14 -07001022 if (part.count != 1) {
Geoffrey Pitsch2fb30fb2016-07-06 16:16:20 -04001023 const size_t fcount = part.frames.size();
1024 for (size_t j = 0; j < fcount; j++) {
Mathias Agopiana8826d62009-10-01 03:10:14 -07001025 const Animation::Frame& frame(part.frames[j]);
1026 glDeleteTextures(1, &frame.tid);
1027 }
1028 }
1029 }
Geoffrey Pitschd6d9a1d2016-06-08 00:38:58 -07001030
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001031 return true;
Mathias Agopiana8826d62009-10-01 03:10:14 -07001032}
1033
Adrian Roos9ee5dff2018-08-22 20:19:49 +02001034void BootAnimation::handleViewport(nsecs_t timestep) {
1035 if (mShuttingDown || !mFlingerSurfaceControl || mTargetInset == 0) {
1036 return;
1037 }
1038 if (mTargetInset < 0) {
1039 // Poll the amount for the top display inset. This will return -1 until persistent properties
1040 // have been loaded.
1041 mTargetInset = android::base::GetIntProperty("persist.sys.displayinset.top",
1042 -1 /* default */, -1 /* min */, mHeight / 2 /* max */);
1043 }
1044 if (mTargetInset <= 0) {
1045 return;
1046 }
1047
1048 if (mCurrentInset < mTargetInset) {
1049 // After the device boots, the inset will effectively be cropped away. We animate this here.
1050 float fraction = static_cast<float>(mCurrentInset) / mTargetInset;
1051 int interpolatedInset = (cosf((fraction + 1) * M_PI) / 2.0f + 0.5f) * mTargetInset;
1052
1053 SurfaceComposerClient::Transaction()
1054 .setCrop(mFlingerSurfaceControl, Rect(0, interpolatedInset, mWidth, mHeight))
1055 .apply();
1056 } else {
1057 // At the end of the animation, we switch to the viewport that DisplayManager will apply
1058 // later. This changes the coordinate system, and means we must move the surface up by
1059 // the inset amount.
Adrian Roos9ee5dff2018-08-22 20:19:49 +02001060 Rect layerStackRect(0, 0, mWidth, mHeight - mTargetInset);
1061 Rect displayRect(0, mTargetInset, mWidth, mHeight);
1062
1063 SurfaceComposerClient::Transaction t;
1064 t.setPosition(mFlingerSurfaceControl, 0, -mTargetInset)
1065 .setCrop(mFlingerSurfaceControl, Rect(0, mTargetInset, mWidth, mHeight));
Dominik Laskowski3316a0a2019-01-25 02:56:41 -08001066 t.setDisplayProjection(mDisplayToken, 0 /* orientation */, layerStackRect, displayRect);
Adrian Roos9ee5dff2018-08-22 20:19:49 +02001067 t.apply();
1068
1069 mTargetInset = mCurrentInset = 0;
1070 }
1071
1072 int delta = timestep * mTargetInset / ms2ns(200);
1073 mCurrentInset += delta;
1074}
1075
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001076void BootAnimation::releaseAnimation(Animation* animation) const
1077{
1078 for (Vector<Animation::Part>::iterator it = animation->parts.begin(),
1079 e = animation->parts.end(); it != e; ++it) {
1080 if (it->animation)
1081 releaseAnimation(it->animation);
1082 }
1083 if (animation->zip)
1084 delete animation->zip;
1085 delete animation;
1086}
1087
1088BootAnimation::Animation* BootAnimation::loadAnimation(const String8& fn)
1089{
1090 if (mLoadedFiles.indexOf(fn) >= 0) {
Wei Wang159a4102018-10-23 23:15:58 -07001091 SLOGE("File \"%s\" is already loaded. Cyclic ref is not allowed",
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001092 fn.string());
Yi Kong911ac232019-03-24 01:49:02 -07001093 return nullptr;
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001094 }
1095 ZipFileRO *zip = ZipFileRO::open(fn);
Yi Kong911ac232019-03-24 01:49:02 -07001096 if (zip == nullptr) {
Wei Wang159a4102018-10-23 23:15:58 -07001097 SLOGE("Failed to open animation zip \"%s\": %s",
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001098 fn.string(), strerror(errno));
Yi Kong911ac232019-03-24 01:49:02 -07001099 return nullptr;
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001100 }
1101
1102 Animation *animation = new Animation;
1103 animation->fileName = fn;
1104 animation->zip = zip;
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -07001105 animation->clockFont.map = nullptr;
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001106 mLoadedFiles.add(animation->fileName);
1107
1108 parseAnimationDesc(*animation);
Geoffrey Pitscha91a2d72016-07-12 14:46:19 -04001109 if (!preloadZip(*animation)) {
Yi Kong911ac232019-03-24 01:49:02 -07001110 return nullptr;
Geoffrey Pitscha91a2d72016-07-12 14:46:19 -04001111 }
1112
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001113
1114 mLoadedFiles.remove(fn);
1115 return animation;
1116}
Damien Bargiacchi97480862016-03-29 14:55:55 -07001117
1118bool BootAnimation::updateIsTimeAccurate() {
1119 static constexpr long long MAX_TIME_IN_PAST = 60000LL * 60LL * 24LL * 30LL; // 30 days
1120 static constexpr long long MAX_TIME_IN_FUTURE = 60000LL * 90LL; // 90 minutes
1121
1122 if (mTimeIsAccurate) {
1123 return true;
1124 }
Keun-young Parkb5938422017-03-23 13:46:24 -07001125 if (mShuttingDown) return true;
Damien Bargiacchi97480862016-03-29 14:55:55 -07001126 struct stat statResult;
Damien Bargiacchi9071db12016-10-28 17:38:22 -07001127
1128 if(stat(TIME_FORMAT_12_HOUR_FLAG_FILE_PATH, &statResult) == 0) {
1129 mTimeFormat12Hour = true;
1130 }
1131
Damien Bargiacchi97480862016-03-29 14:55:55 -07001132 if(stat(ACCURATE_TIME_FLAG_FILE_PATH, &statResult) == 0) {
1133 mTimeIsAccurate = true;
1134 return true;
1135 }
1136
1137 FILE* file = fopen(LAST_TIME_CHANGED_FILE_PATH, "r");
Yi Kong911ac232019-03-24 01:49:02 -07001138 if (file != nullptr) {
Damien Bargiacchi97480862016-03-29 14:55:55 -07001139 long long lastChangedTime = 0;
1140 fscanf(file, "%lld", &lastChangedTime);
1141 fclose(file);
1142 if (lastChangedTime > 0) {
1143 struct timespec now;
1144 clock_gettime(CLOCK_REALTIME, &now);
1145 // Match the Java timestamp format
1146 long long rtcNow = (now.tv_sec * 1000LL) + (now.tv_nsec / 1000000LL);
Damien Bargiacchi96762812016-07-12 15:53:40 -07001147 if (ACCURATE_TIME_EPOCH < rtcNow
1148 && lastChangedTime > (rtcNow - MAX_TIME_IN_PAST)
1149 && lastChangedTime < (rtcNow + MAX_TIME_IN_FUTURE)) {
Damien Bargiacchi97480862016-03-29 14:55:55 -07001150 mTimeIsAccurate = true;
1151 }
1152 }
1153 }
1154
1155 return mTimeIsAccurate;
1156}
1157
1158BootAnimation::TimeCheckThread::TimeCheckThread(BootAnimation* bootAnimation) : Thread(false),
1159 mInotifyFd(-1), mSystemWd(-1), mTimeWd(-1), mBootAnimation(bootAnimation) {}
1160
1161BootAnimation::TimeCheckThread::~TimeCheckThread() {
1162 // mInotifyFd may be -1 but that's ok since we're not at risk of attempting to close a valid FD.
1163 close(mInotifyFd);
1164}
1165
1166bool BootAnimation::TimeCheckThread::threadLoop() {
1167 bool shouldLoop = doThreadLoop() && !mBootAnimation->mTimeIsAccurate
1168 && mBootAnimation->mClockEnabled;
1169 if (!shouldLoop) {
1170 close(mInotifyFd);
1171 mInotifyFd = -1;
1172 }
1173 return shouldLoop;
1174}
1175
1176bool BootAnimation::TimeCheckThread::doThreadLoop() {
1177 static constexpr int BUFF_LEN (10 * (sizeof(struct inotify_event) + NAME_MAX + 1));
1178
1179 // Poll instead of doing a blocking read so the Thread can exit if requested.
1180 struct pollfd pfd = { mInotifyFd, POLLIN, 0 };
1181 ssize_t pollResult = poll(&pfd, 1, 1000);
1182
1183 if (pollResult == 0) {
1184 return true;
1185 } else if (pollResult < 0) {
Wei Wang159a4102018-10-23 23:15:58 -07001186 SLOGE("Could not poll inotify events");
Damien Bargiacchi97480862016-03-29 14:55:55 -07001187 return false;
1188 }
1189
1190 char buff[BUFF_LEN] __attribute__ ((aligned(__alignof__(struct inotify_event))));;
1191 ssize_t length = read(mInotifyFd, buff, BUFF_LEN);
1192 if (length == 0) {
1193 return true;
1194 } else if (length < 0) {
Wei Wang159a4102018-10-23 23:15:58 -07001195 SLOGE("Could not read inotify events");
Damien Bargiacchi97480862016-03-29 14:55:55 -07001196 return false;
1197 }
1198
1199 const struct inotify_event *event;
1200 for (char* ptr = buff; ptr < buff + length; ptr += sizeof(struct inotify_event) + event->len) {
1201 event = (const struct inotify_event *) ptr;
1202 if (event->wd == mSystemWd && strcmp(SYSTEM_TIME_DIR_NAME, event->name) == 0) {
1203 addTimeDirWatch();
1204 } else if (event->wd == mTimeWd && (strcmp(LAST_TIME_CHANGED_FILE_NAME, event->name) == 0
1205 || strcmp(ACCURATE_TIME_FLAG_FILE_NAME, event->name) == 0)) {
1206 return !mBootAnimation->updateIsTimeAccurate();
1207 }
1208 }
1209
1210 return true;
1211}
1212
1213void BootAnimation::TimeCheckThread::addTimeDirWatch() {
1214 mTimeWd = inotify_add_watch(mInotifyFd, SYSTEM_TIME_DIR_PATH,
1215 IN_CLOSE_WRITE | IN_MOVED_TO | IN_ATTRIB);
1216 if (mTimeWd > 0) {
1217 // No need to watch for the time directory to be created if it already exists
1218 inotify_rm_watch(mInotifyFd, mSystemWd);
1219 mSystemWd = -1;
1220 }
1221}
1222
1223status_t BootAnimation::TimeCheckThread::readyToRun() {
1224 mInotifyFd = inotify_init();
1225 if (mInotifyFd < 0) {
Wei Wang159a4102018-10-23 23:15:58 -07001226 SLOGE("Could not initialize inotify fd");
Damien Bargiacchi97480862016-03-29 14:55:55 -07001227 return NO_INIT;
1228 }
1229
1230 mSystemWd = inotify_add_watch(mInotifyFd, SYSTEM_DATA_DIR_PATH, IN_CREATE | IN_ATTRIB);
1231 if (mSystemWd < 0) {
1232 close(mInotifyFd);
1233 mInotifyFd = -1;
Wei Wang159a4102018-10-23 23:15:58 -07001234 SLOGE("Could not add watch for %s", SYSTEM_DATA_DIR_PATH);
Damien Bargiacchi97480862016-03-29 14:55:55 -07001235 return NO_INIT;
1236 }
1237
1238 addTimeDirWatch();
1239
1240 if (mBootAnimation->updateIsTimeAccurate()) {
1241 close(mInotifyFd);
1242 mInotifyFd = -1;
1243 return ALREADY_EXISTS;
1244 }
1245
1246 return NO_ERROR;
1247}
1248
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001249// ---------------------------------------------------------------------------
1250
1251}
1252; // namespace android