blob: 16af0719d9dd52a7177c7850c0e60fcb6acf625b [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";
Jaekyun Seokc521bb32018-01-30 11:52:47 +090072static const char PRODUCT_ENCRYPTED_BOOTANIMATION_FILE[] = "/product/media/bootanimation-encrypted.zip";
Damien Bargiacchi97480862016-03-29 14:55:55 -070073static const char SYSTEM_ENCRYPTED_BOOTANIMATION_FILE[] = "/system/media/bootanimation-encrypted.zip";
Keun-young Parkb5938422017-03-23 13:46:24 -070074static const char OEM_SHUTDOWNANIMATION_FILE[] = "/oem/media/shutdownanimation.zip";
Jaekyun Seokc521bb32018-01-30 11:52:47 +090075static const char PRODUCT_SHUTDOWNANIMATION_FILE[] = "/product/media/shutdownanimation.zip";
Keun-young Parkb5938422017-03-23 13:46:24 -070076static const char SYSTEM_SHUTDOWNANIMATION_FILE[] = "/system/media/shutdownanimation.zip";
77
Damien Bargiacchi97480862016-03-29 14:55:55 -070078static const char SYSTEM_DATA_DIR_PATH[] = "/data/system";
79static const char SYSTEM_TIME_DIR_NAME[] = "time";
80static const char SYSTEM_TIME_DIR_PATH[] = "/data/system/time";
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -070081static const char CLOCK_FONT_ASSET[] = "images/clock_font.png";
82static const char CLOCK_FONT_ZIP_NAME[] = "clock_font.png";
Damien Bargiacchi97480862016-03-29 14:55:55 -070083static const char LAST_TIME_CHANGED_FILE_NAME[] = "last_time_change";
84static const char LAST_TIME_CHANGED_FILE_PATH[] = "/data/system/time/last_time_change";
85static const char ACCURATE_TIME_FLAG_FILE_NAME[] = "time_is_accurate";
86static const char ACCURATE_TIME_FLAG_FILE_PATH[] = "/data/system/time/time_is_accurate";
Damien Bargiacchi9071db12016-10-28 17:38:22 -070087static const char TIME_FORMAT_12_HOUR_FLAG_FILE_PATH[] = "/data/system/time/time_format_12_hour";
Damien Bargiacchi96762812016-07-12 15:53:40 -070088// Java timestamp format. Don't show the clock if the date is before 2000-01-01 00:00:00.
89static const long long ACCURATE_TIME_EPOCH = 946684800000;
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -070090static constexpr char FONT_BEGIN_CHAR = ' ';
91static constexpr char FONT_END_CHAR = '~' + 1;
92static constexpr size_t FONT_NUM_CHARS = FONT_END_CHAR - FONT_BEGIN_CHAR + 1;
93static constexpr size_t FONT_NUM_COLS = 16;
94static constexpr size_t FONT_NUM_ROWS = FONT_NUM_CHARS / FONT_NUM_COLS;
95static const int TEXT_CENTER_VALUE = INT_MAX;
96static const int TEXT_MISSING_VALUE = INT_MIN;
Damien Bargiacchi97480862016-03-29 14:55:55 -070097static const char EXIT_PROP_NAME[] = "service.bootanim.exit";
Narayan Kamathafd31e02013-12-03 13:16:03 +000098static const int ANIM_ENTRY_NAME_MAX = 256;
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -070099static constexpr size_t TEXT_POS_LEN_MAX = 16;
Narayan Kamathafd31e02013-12-03 13:16:03 +0000100
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800101// ---------------------------------------------------------------------------
102
Ed Coyne7464ac92017-06-08 12:26:48 -0700103BootAnimation::BootAnimation(sp<Callbacks> callbacks)
Ed Coyne2c9e94a2017-05-31 10:08:28 -0700104 : Thread(false), mClockEnabled(true), mTimeIsAccurate(false),
Yi Kong911ac232019-03-24 01:49:02 -0700105 mTimeFormat12Hour(false), mTimeCheckThread(nullptr), mCallbacks(callbacks) {
Mathias Agopian627e7b52009-05-21 19:21:59 -0700106 mSession = new SurfaceComposerClient();
Geoffrey Pitsch290c4352016-08-09 14:35:10 -0400107
Keun-young Parkb5938422017-03-23 13:46:24 -0700108 std::string powerCtl = android::base::GetProperty("sys.powerctl", "");
109 if (powerCtl.empty()) {
110 mShuttingDown = false;
111 } else {
112 mShuttingDown = true;
113 }
Huihong Luo50a2f362018-08-11 09:27:57 -0700114 ALOGD("%sAnimationStartTiming start time: %" PRId64 "ms", mShuttingDown ? "Shutdown" : "Boot",
115 elapsedRealtime());
116}
117
118BootAnimation::~BootAnimation() {
119 if (mAnimation != nullptr) {
120 releaseAnimation(mAnimation);
121 mAnimation = nullptr;
122 }
123 ALOGD("%sAnimationStopTiming start time: %" PRId64 "ms", mShuttingDown ? "Shutdown" : "Boot",
124 elapsedRealtime());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800125}
126
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800127void BootAnimation::onFirstRef() {
Mathias Agopianbc726112009-09-23 15:44:05 -0700128 status_t err = mSession->linkToComposerDeath(this);
Wei Wang159a4102018-10-23 23:15:58 -0700129 SLOGE_IF(err, "linkToComposerDeath failed (%s) ", strerror(-err));
Mathias Agopian8434c532009-09-23 18:52:49 -0700130 if (err == NO_ERROR) {
Huihong Luo50a2f362018-08-11 09:27:57 -0700131 // Load the animation content -- this can be slow (eg 200ms)
132 // called before waitForSurfaceFlinger() in main() to avoid wait
133 ALOGD("%sAnimationPreloadTiming start time: %" PRId64 "ms",
134 mShuttingDown ? "Shutdown" : "Boot", elapsedRealtime());
135 preloadAnimation();
136 ALOGD("%sAnimationPreloadStopTiming start time: %" PRId64 "ms",
137 mShuttingDown ? "Shutdown" : "Boot", elapsedRealtime());
Mathias Agopianbc726112009-09-23 15:44:05 -0700138 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800139}
140
Mathias Agopianbc726112009-09-23 15:44:05 -0700141sp<SurfaceComposerClient> BootAnimation::session() const {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800142 return mSession;
143}
144
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 }
Beverly34ffe252019-05-17 11:03:54 -0400358
359 const bool playDarkAnim = android::base::GetIntProperty("ro.boot.theme", 0) == 1;
Jaekyun Seokc521bb32018-01-30 11:52:47 +0900360 static const char* bootFiles[] =
Beverly34ffe252019-05-17 11:03:54 -0400361 {playDarkAnim ? PRODUCT_BOOTANIMATION_DARK_FILE : PRODUCT_BOOTANIMATION_FILE,
362 OEM_BOOTANIMATION_FILE, SYSTEM_BOOTANIMATION_FILE};
Keun-young Parkb5938422017-03-23 13:46:24 -0700363 static const char* shutdownFiles[] =
Jaekyun Seokc521bb32018-01-30 11:52:47 +0900364 {PRODUCT_SHUTDOWNANIMATION_FILE, OEM_SHUTDOWNANIMATION_FILE, SYSTEM_SHUTDOWNANIMATION_FILE};
Keun-young Parkb5938422017-03-23 13:46:24 -0700365
366 for (const char* f : (!mShuttingDown ? bootFiles : shutdownFiles)) {
367 if (access(f, R_OK) == 0) {
368 mZipFileName = f;
Huihong Luo50a2f362018-08-11 09:27:57 -0700369 return;
Keun-young Parkb5938422017-03-23 13:46:24 -0700370 }
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700371 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800372}
373
Mathias Agopiana8826d62009-10-01 03:10:14 -0700374bool BootAnimation::threadLoop()
375{
376 bool r;
Narayan Kamathafd31e02013-12-03 13:16:03 +0000377 // We have no bootanimation file, so we use the stock android logo
378 // animation.
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700379 if (mZipFileName.isEmpty()) {
Mathias Agopiana8826d62009-10-01 03:10:14 -0700380 r = android();
381 } else {
382 r = movie();
383 }
384
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800385 eglMakeCurrent(mDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
386 eglDestroyContext(mDisplay, mContext);
387 eglDestroySurface(mDisplay, mSurface);
Mathias Agopian6cf0db22009-04-17 19:36:26 -0700388 mFlingerSurface.clear();
Mathias Agopian17f638b2009-04-16 20:04:08 -0700389 mFlingerSurfaceControl.clear();
Mathias Agopian627e7b52009-05-21 19:21:59 -0700390 eglTerminate(mDisplay);
Sai Kiran Korwar3eee9fb2015-06-16 17:13:35 +0530391 eglReleaseThread();
Mathias Agopian627e7b52009-05-21 19:21:59 -0700392 IPCThreadState::self()->stopProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800393 return r;
394}
395
Mathias Agopiana8826d62009-10-01 03:10:14 -0700396bool BootAnimation::android()
397{
Wei Wang159a4102018-10-23 23:15:58 -0700398 SLOGD("%sAnimationShownTiming start time: %" PRId64 "ms", mShuttingDown ? "Shutdown" : "Boot",
Keun-young Parkd1794cd2017-04-04 12:21:19 -0700399 elapsedRealtime());
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700400 initTexture(&mAndroid[0], mAssets, "images/android-logo-mask.png");
401 initTexture(&mAndroid[1], mAssets, "images/android-logo-shine.png");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800402
Ed Coyne7464ac92017-06-08 12:26:48 -0700403 mCallbacks->init({});
404
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800405 // clear screen
Mathias Agopiana8826d62009-10-01 03:10:14 -0700406 glShadeModel(GL_FLAT);
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700407 glDisable(GL_DITHER);
408 glDisable(GL_SCISSOR_TEST);
Mathias Agopian59f19e42011-05-06 19:22:12 -0700409 glClearColor(0,0,0,1);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800410 glClear(GL_COLOR_BUFFER_BIT);
411 eglSwapBuffers(mDisplay, mSurface);
412
Mathias Agopiana8826d62009-10-01 03:10:14 -0700413 glEnable(GL_TEXTURE_2D);
414 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
415
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700416 const GLint xc = (mWidth - mAndroid[0].w) / 2;
417 const GLint yc = (mHeight - mAndroid[0].h) / 2;
418 const Rect updateRect(xc, yc, xc + mAndroid[0].w, yc + mAndroid[0].h);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800419
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800420 glScissor(updateRect.left, mHeight - updateRect.bottom, updateRect.width(),
421 updateRect.height());
422
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700423 // Blend state
424 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
425 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
426
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800427 const nsecs_t startTime = systemTime();
428 do {
Mathias Agopian13796652009-03-24 22:49:21 -0700429 nsecs_t now = systemTime();
430 double time = now - startTime;
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700431 float t = 4.0f * float(time / us2ns(16667)) / mAndroid[1].w;
432 GLint offset = (1 - (t - floorf(t))) * mAndroid[1].w;
433 GLint x = xc - offset;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800434
Mathias Agopian81668642009-07-28 11:41:30 -0700435 glDisable(GL_SCISSOR_TEST);
436 glClear(GL_COLOR_BUFFER_BIT);
437
438 glEnable(GL_SCISSOR_TEST);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800439 glDisable(GL_BLEND);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800440 glBindTexture(GL_TEXTURE_2D, mAndroid[1].name);
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700441 glDrawTexiOES(x, yc, 0, mAndroid[1].w, mAndroid[1].h);
442 glDrawTexiOES(x + mAndroid[1].w, yc, 0, mAndroid[1].w, mAndroid[1].h);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800443
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700444 glEnable(GL_BLEND);
445 glBindTexture(GL_TEXTURE_2D, mAndroid[0].name);
446 glDrawTexiOES(xc, yc, 0, mAndroid[0].w, mAndroid[0].h);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800447
Mathias Agopian627e7b52009-05-21 19:21:59 -0700448 EGLBoolean res = eglSwapBuffers(mDisplay, mSurface);
449 if (res == EGL_FALSE)
450 break;
451
Mathias Agopian13796652009-03-24 22:49:21 -0700452 // 12fps: don't animate too fast to preserve CPU
453 const nsecs_t sleepTime = 83333 - ns2us(systemTime() - now);
454 if (sleepTime > 0)
Mathias Agopiana8826d62009-10-01 03:10:14 -0700455 usleep(sleepTime);
Kevin Hesterd3782b22012-04-26 10:38:55 -0700456
457 checkExit();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800458 } while (!exitPending());
459
460 glDeleteTextures(1, &mAndroid[0].name);
461 glDeleteTextures(1, &mAndroid[1].name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800462 return false;
463}
464
Kevin Hesterd3782b22012-04-26 10:38:55 -0700465void BootAnimation::checkExit() {
466 // Allow surface flinger to gracefully request shutdown
467 char value[PROPERTY_VALUE_MAX];
468 property_get(EXIT_PROP_NAME, value, "0");
469 int exitnow = atoi(value);
470 if (exitnow) {
471 requestExit();
Ed Coyne7464ac92017-06-08 12:26:48 -0700472 mCallbacks->shutdown();
Kevin Hesterd3782b22012-04-26 10:38:55 -0700473 }
474}
475
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700476bool BootAnimation::validClock(const Animation::Part& part) {
477 return part.clockPosX != TEXT_MISSING_VALUE && part.clockPosY != TEXT_MISSING_VALUE;
478}
479
480bool parseTextCoord(const char* str, int* dest) {
481 if (strcmp("c", str) == 0) {
482 *dest = TEXT_CENTER_VALUE;
483 return true;
484 }
485
486 char* end;
487 int val = (int) strtol(str, &end, 0);
488 if (end == str || *end != '\0' || val == INT_MAX || val == INT_MIN) {
489 return false;
490 }
491 *dest = val;
492 return true;
493}
494
495// Parse two position coordinates. If only string is non-empty, treat it as the y value.
496void parsePosition(const char* str1, const char* str2, int* x, int* y) {
497 bool success = false;
498 if (strlen(str1) == 0) { // No values were specified
499 // success = false
500 } else if (strlen(str2) == 0) { // we have only one value
501 if (parseTextCoord(str1, y)) {
502 *x = TEXT_CENTER_VALUE;
503 success = true;
504 }
505 } else {
506 if (parseTextCoord(str1, x) && parseTextCoord(str2, y)) {
507 success = true;
508 }
509 }
510
511 if (!success) {
512 *x = TEXT_MISSING_VALUE;
513 *y = TEXT_MISSING_VALUE;
514 }
515}
516
Jesse Hall083b84c2014-09-22 10:51:09 -0700517// Parse a color represented as an HTML-style 'RRGGBB' string: each pair of
518// characters in str is a hex number in [0, 255], which are converted to
519// floating point values in the range [0.0, 1.0] and placed in the
520// corresponding elements of color.
521//
522// If the input string isn't valid, parseColor returns false and color is
523// left unchanged.
524static bool parseColor(const char str[7], float color[3]) {
525 float tmpColor[3];
526 for (int i = 0; i < 3; i++) {
527 int val = 0;
528 for (int j = 0; j < 2; j++) {
529 val *= 16;
530 char c = str[2*i + j];
531 if (c >= '0' && c <= '9') val += c - '0';
532 else if (c >= 'A' && c <= 'F') val += (c - 'A') + 10;
533 else if (c >= 'a' && c <= 'f') val += (c - 'a') + 10;
534 else return false;
535 }
536 tmpColor[i] = static_cast<float>(val) / 255.0f;
537 }
538 memcpy(color, tmpColor, sizeof(tmpColor));
539 return true;
540}
541
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700542
543static bool readFile(ZipFileRO* zip, const char* name, String8& outString)
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700544{
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700545 ZipEntryRO entry = zip->findEntryByName(name);
Wei Wang159a4102018-10-23 23:15:58 -0700546 SLOGE_IF(!entry, "couldn't find %s", name);
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700547 if (!entry) {
548 return false;
549 }
550
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700551 FileMap* entryMap = zip->createEntryFileMap(entry);
552 zip->releaseEntry(entry);
Wei Wang159a4102018-10-23 23:15:58 -0700553 SLOGE_IF(!entryMap, "entryMap is null");
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700554 if (!entryMap) {
555 return false;
556 }
557
558 outString.setTo((char const*)entryMap->getDataPtr(), entryMap->getDataLength());
Narayan Kamath688ff4c2015-02-23 15:47:54 +0000559 delete entryMap;
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700560 return true;
561}
562
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700563// The font image should be a 96x2 array of character images. The
564// columns are the printable ASCII characters 0x20 - 0x7f. The
565// top row is regular text; the bottom row is bold.
566status_t BootAnimation::initFont(Font* font, const char* fallback) {
567 status_t status = NO_ERROR;
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800568
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700569 if (font->map != nullptr) {
570 glGenTextures(1, &font->texture.name);
571 glBindTexture(GL_TEXTURE_2D, font->texture.name);
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800572
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700573 status = initTexture(font->map, &font->texture.w, &font->texture.h);
574
575 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
576 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
577 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
578 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
579 } else if (fallback != nullptr) {
580 status = initTexture(&font->texture, mAssets, fallback);
581 } else {
582 return NO_INIT;
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800583 }
584
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700585 if (status == NO_ERROR) {
586 font->char_width = font->texture.w / FONT_NUM_COLS;
587 font->char_height = font->texture.h / FONT_NUM_ROWS / 2; // There are bold and regular rows
588 }
589
590 return status;
591}
592
593void BootAnimation::drawText(const char* str, const Font& font, bool bold, int* x, int* y) {
594 glEnable(GL_BLEND); // Allow us to draw on top of the animation
595 glBindTexture(GL_TEXTURE_2D, font.texture.name);
596
597 const int len = strlen(str);
598 const int strWidth = font.char_width * len;
599
600 if (*x == TEXT_CENTER_VALUE) {
601 *x = (mWidth - strWidth) / 2;
602 } else if (*x < 0) {
603 *x = mWidth + *x - strWidth;
604 }
605 if (*y == TEXT_CENTER_VALUE) {
606 *y = (mHeight - font.char_height) / 2;
607 } else if (*y < 0) {
608 *y = mHeight + *y - font.char_height;
609 }
610
611 int cropRect[4] = { 0, 0, font.char_width, -font.char_height };
612
613 for (int i = 0; i < len; i++) {
614 char c = str[i];
615
616 if (c < FONT_BEGIN_CHAR || c > FONT_END_CHAR) {
617 c = '?';
618 }
619
620 // Crop the texture to only the pixels in the current glyph
621 const int charPos = (c - FONT_BEGIN_CHAR); // Position in the list of valid characters
622 const int row = charPos / FONT_NUM_COLS;
623 const int col = charPos % FONT_NUM_COLS;
624 cropRect[0] = col * font.char_width; // Left of column
625 cropRect[1] = row * font.char_height * 2; // Top of row
626 // Move down to bottom of regular (one char_heigh) or bold (two char_heigh) line
627 cropRect[1] += bold ? 2 * font.char_height : font.char_height;
628 glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_CROP_RECT_OES, cropRect);
629
630 glDrawTexiOES(*x, *y, 0, font.char_width, font.char_height);
631
632 *x += font.char_width;
633 }
634
635 glDisable(GL_BLEND); // Return to the animation's default behaviour
636 glBindTexture(GL_TEXTURE_2D, 0);
637}
638
Damien Bargiacchi9071db12016-10-28 17:38:22 -0700639// We render 12 or 24 hour time.
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700640void BootAnimation::drawClock(const Font& font, const int xPos, const int yPos) {
Damien Bargiacchi9071db12016-10-28 17:38:22 -0700641 static constexpr char TIME_FORMAT_12[] = "%l:%M";
642 static constexpr char TIME_FORMAT_24[] = "%H:%M";
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700643 static constexpr int TIME_LENGTH = 6;
644
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800645 time_t rawtime;
646 time(&rawtime);
647 struct tm* timeInfo = localtime(&rawtime);
648
649 char timeBuff[TIME_LENGTH];
Damien Bargiacchi9071db12016-10-28 17:38:22 -0700650 const char* timeFormat = mTimeFormat12Hour ? TIME_FORMAT_12 : TIME_FORMAT_24;
651 size_t length = strftime(timeBuff, TIME_LENGTH, timeFormat, timeInfo);
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800652
653 if (length != TIME_LENGTH - 1) {
Wei Wang159a4102018-10-23 23:15:58 -0700654 SLOGE("Couldn't format time; abandoning boot animation clock");
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800655 mClockEnabled = false;
656 return;
657 }
658
Damien Bargiacchi45a76442016-12-05 18:02:18 -0800659 char* out = timeBuff[0] == ' ' ? &timeBuff[1] : &timeBuff[0];
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700660 int x = xPos;
661 int y = yPos;
Damien Bargiacchi45a76442016-12-05 18:02:18 -0800662 drawText(out, font, false, &x, &y);
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800663}
664
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700665bool BootAnimation::parseAnimationDesc(Animation& animation)
Mathias Agopiana8826d62009-10-01 03:10:14 -0700666{
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700667 String8 desString;
668
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700669 if (!readFile(animation.zip, "desc.txt", desString)) {
Narayan Kamathafd31e02013-12-03 13:16:03 +0000670 return false;
671 }
Mathias Agopiana8826d62009-10-01 03:10:14 -0700672 char const* s = desString.string();
673
Mathias Agopiana8826d62009-10-01 03:10:14 -0700674 // Parse the description file
675 for (;;) {
676 const char* endl = strstr(s, "\n");
Yi Kong911ac232019-03-24 01:49:02 -0700677 if (endl == nullptr) break;
Mathias Agopiana8826d62009-10-01 03:10:14 -0700678 String8 line(s, endl - s);
679 const char* l = line.string();
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800680 int fps = 0;
681 int width = 0;
682 int height = 0;
683 int count = 0;
684 int pause = 0;
Narayan Kamathafd31e02013-12-03 13:16:03 +0000685 char path[ANIM_ENTRY_NAME_MAX];
Jesse Hall083b84c2014-09-22 10:51:09 -0700686 char color[7] = "000000"; // default to black if unspecified
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700687 char clockPos1[TEXT_POS_LEN_MAX + 1] = "";
688 char clockPos2[TEXT_POS_LEN_MAX + 1] = "";
Jesse Hall083b84c2014-09-22 10:51:09 -0700689
Kevin Hesterd3782b22012-04-26 10:38:55 -0700690 char pathType;
Mathias Agopiana8826d62009-10-01 03:10:14 -0700691 if (sscanf(l, "%d %d %d", &width, &height, &fps) == 3) {
Wei Wang159a4102018-10-23 23:15:58 -0700692 // SLOGD("> w=%d, h=%d, fps=%d", width, height, fps);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700693 animation.width = width;
694 animation.height = height;
695 animation.fps = fps;
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700696 } else if (sscanf(l, " %c %d %d %s #%6s %16s %16s",
697 &pathType, &count, &pause, path, color, clockPos1, clockPos2) >= 4) {
Wei Wang159a4102018-10-23 23:15:58 -0700698 //SLOGD("> type=%c, count=%d, pause=%d, path=%s, color=%s, clockPos1=%s, clockPos2=%s",
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700699 // pathType, count, pause, path, color, clockPos1, clockPos2);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700700 Animation::Part part;
Kevin Hesterd3782b22012-04-26 10:38:55 -0700701 part.playUntilComplete = pathType == 'c';
Mathias Agopiana8826d62009-10-01 03:10:14 -0700702 part.count = count;
703 part.pause = pause;
704 part.path = path;
Yi Kong911ac232019-03-24 01:49:02 -0700705 part.audioData = nullptr;
706 part.animation = nullptr;
Jesse Hall083b84c2014-09-22 10:51:09 -0700707 if (!parseColor(color, part.backgroundColor)) {
Wei Wang159a4102018-10-23 23:15:58 -0700708 SLOGE("> invalid color '#%s'", color);
Jesse Hall083b84c2014-09-22 10:51:09 -0700709 part.backgroundColor[0] = 0.0f;
710 part.backgroundColor[1] = 0.0f;
711 part.backgroundColor[2] = 0.0f;
712 }
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700713 parsePosition(clockPos1, clockPos2, &part.clockPosX, &part.clockPosY);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700714 animation.parts.add(part);
715 }
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700716 else if (strcmp(l, "$SYSTEM") == 0) {
Wei Wang159a4102018-10-23 23:15:58 -0700717 // SLOGD("> SYSTEM");
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700718 Animation::Part part;
719 part.playUntilComplete = false;
720 part.count = 1;
721 part.pause = 0;
Yi Kong911ac232019-03-24 01:49:02 -0700722 part.audioData = nullptr;
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700723 part.animation = loadAnimation(String8(SYSTEM_BOOTANIMATION_FILE));
Yi Kong911ac232019-03-24 01:49:02 -0700724 if (part.animation != nullptr)
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700725 animation.parts.add(part);
726 }
Mathias Agopiana8826d62009-10-01 03:10:14 -0700727 s = ++endl;
728 }
729
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700730 return true;
731}
732
733bool BootAnimation::preloadZip(Animation& animation)
734{
Mathias Agopiana8826d62009-10-01 03:10:14 -0700735 // read all the data structures
736 const size_t pcount = animation.parts.size();
Yi Kong911ac232019-03-24 01:49:02 -0700737 void *cookie = nullptr;
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400738 ZipFileRO* zip = animation.zip;
739 if (!zip->startIteration(&cookie)) {
Narayan Kamathafd31e02013-12-03 13:16:03 +0000740 return false;
741 }
742
743 ZipEntryRO entry;
744 char name[ANIM_ENTRY_NAME_MAX];
Yi Kong911ac232019-03-24 01:49:02 -0700745 while ((entry = zip->nextEntry(cookie)) != nullptr) {
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400746 const int foundEntryName = zip->getEntryFileName(entry, name, ANIM_ENTRY_NAME_MAX);
Narayan Kamathafd31e02013-12-03 13:16:03 +0000747 if (foundEntryName > ANIM_ENTRY_NAME_MAX || foundEntryName == -1) {
Wei Wang159a4102018-10-23 23:15:58 -0700748 SLOGE("Error fetching entry file name");
Narayan Kamathafd31e02013-12-03 13:16:03 +0000749 continue;
750 }
751
752 const String8 entryName(name);
753 const String8 path(entryName.getPathDir());
754 const String8 leaf(entryName.getPathLeaf());
755 if (leaf.size() > 0) {
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700756 if (entryName == CLOCK_FONT_ZIP_NAME) {
757 FileMap* map = zip->createEntryFileMap(entry);
758 if (map) {
759 animation.clockFont.map = map;
760 }
761 continue;
762 }
763
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400764 for (size_t j = 0; j < pcount; j++) {
Narayan Kamathafd31e02013-12-03 13:16:03 +0000765 if (path == animation.parts[j].path) {
Narayan Kamath4600dd02015-06-16 12:02:57 +0100766 uint16_t method;
Narayan Kamathafd31e02013-12-03 13:16:03 +0000767 // supports only stored png files
Yi Kong911ac232019-03-24 01:49:02 -0700768 if (zip->getEntryInfo(entry, &method, nullptr, nullptr, nullptr, nullptr, nullptr)) {
Narayan Kamathafd31e02013-12-03 13:16:03 +0000769 if (method == ZipFileRO::kCompressStored) {
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400770 FileMap* map = zip->createEntryFileMap(entry);
Narayan Kamathafd31e02013-12-03 13:16:03 +0000771 if (map) {
Narayan Kamathafd31e02013-12-03 13:16:03 +0000772 Animation::Part& part(animation.parts.editItemAt(j));
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700773 if (leaf == "audio.wav") {
774 // a part may have at most one audio file
Geoffrey Pitschd6d9a1d2016-06-08 00:38:58 -0700775 part.audioData = (uint8_t *)map->getDataPtr();
776 part.audioLength = map->getDataLength();
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400777 } else if (leaf == "trim.txt") {
778 part.trimData.setTo((char const*)map->getDataPtr(),
779 map->getDataLength());
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700780 } else {
781 Animation::Frame frame;
782 frame.name = leaf;
783 frame.map = map;
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400784 frame.trimWidth = animation.width;
785 frame.trimHeight = animation.height;
786 frame.trimX = 0;
787 frame.trimY = 0;
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700788 part.frames.add(frame);
789 }
Mathias Agopiana8826d62009-10-01 03:10:14 -0700790 }
Geoffrey Pitschd6d9a1d2016-06-08 00:38:58 -0700791 } else {
Wei Wang159a4102018-10-23 23:15:58 -0700792 SLOGE("bootanimation.zip is compressed; must be only stored");
Mathias Agopiana8826d62009-10-01 03:10:14 -0700793 }
794 }
795 }
796 }
797 }
798 }
799
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400800 // If there is trimData present, override the positioning defaults.
801 for (Animation::Part& part : animation.parts) {
802 const char* trimDataStr = part.trimData.string();
803 for (size_t frameIdx = 0; frameIdx < part.frames.size(); frameIdx++) {
804 const char* endl = strstr(trimDataStr, "\n");
805 // No more trimData for this part.
Yi Kong911ac232019-03-24 01:49:02 -0700806 if (endl == nullptr) {
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400807 break;
808 }
809 String8 line(trimDataStr, endl - trimDataStr);
810 const char* lineStr = line.string();
811 trimDataStr = ++endl;
812 int width = 0, height = 0, x = 0, y = 0;
813 if (sscanf(lineStr, "%dx%d+%d+%d", &width, &height, &x, &y) == 4) {
814 Animation::Frame& frame(part.frames.editItemAt(frameIdx));
815 frame.trimWidth = width;
816 frame.trimHeight = height;
817 frame.trimX = x;
818 frame.trimY = y;
819 } else {
Wei Wang159a4102018-10-23 23:15:58 -0700820 SLOGE("Error parsing trim.txt, line: %s", lineStr);
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400821 break;
822 }
823 }
824 }
825
826 zip->endIteration(cookie);
Narayan Kamathafd31e02013-12-03 13:16:03 +0000827
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700828 return true;
829}
830
831bool BootAnimation::movie()
832{
Huihong Luo50a2f362018-08-11 09:27:57 -0700833 if (mAnimation == nullptr) {
834 mAnimation = loadAnimation(mZipFileName);
835 }
836
837 if (mAnimation == nullptr)
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700838 return false;
839
Huihong Luo50a2f362018-08-11 09:27:57 -0700840 // mCallbacks->init() may get called recursively,
841 // this loop is needed to get the same results
842 for (const Animation::Part& part : mAnimation->parts) {
843 if (part.animation != nullptr) {
844 mCallbacks->init(part.animation->parts);
845 }
846 }
847 mCallbacks->init(mAnimation->parts);
848
Damien Bargiacchi97480862016-03-29 14:55:55 -0700849 bool anyPartHasClock = false;
Huihong Luo50a2f362018-08-11 09:27:57 -0700850 for (size_t i=0; i < mAnimation->parts.size(); i++) {
851 if(validClock(mAnimation->parts[i])) {
Damien Bargiacchi97480862016-03-29 14:55:55 -0700852 anyPartHasClock = true;
853 break;
854 }
855 }
856 if (!anyPartHasClock) {
857 mClockEnabled = false;
858 }
859
Sai Kiran Korwar27167492015-07-07 20:00:06 +0530860 // Check if npot textures are supported
861 mUseNpotTextures = false;
862 String8 gl_extensions;
863 const char* exts = reinterpret_cast<const char*>(glGetString(GL_EXTENSIONS));
864 if (!exts) {
865 glGetError();
866 } else {
867 gl_extensions.setTo(exts);
868 if ((gl_extensions.find("GL_ARB_texture_non_power_of_two") != -1) ||
869 (gl_extensions.find("GL_OES_texture_npot") != -1)) {
870 mUseNpotTextures = true;
871 }
872 }
873
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800874 // Blend required to draw time on top of animation frames.
875 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700876 glShadeModel(GL_FLAT);
877 glDisable(GL_DITHER);
878 glDisable(GL_SCISSOR_TEST);
879 glDisable(GL_BLEND);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700880
881 glBindTexture(GL_TEXTURE_2D, 0);
882 glEnable(GL_TEXTURE_2D);
883 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
884 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
885 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
886 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
887 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
888
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700889 bool clockFontInitialized = false;
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800890 if (mClockEnabled) {
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700891 clockFontInitialized =
Huihong Luo50a2f362018-08-11 09:27:57 -0700892 (initFont(&mAnimation->clockFont, CLOCK_FONT_ASSET) == NO_ERROR);
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700893 mClockEnabled = clockFontInitialized;
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800894 }
895
Damien Bargiacchi97480862016-03-29 14:55:55 -0700896 if (mClockEnabled && !updateIsTimeAccurate()) {
897 mTimeCheckThread = new TimeCheckThread(this);
898 mTimeCheckThread->run("BootAnimation::TimeCheckThread", PRIORITY_NORMAL);
899 }
900
Huihong Luo50a2f362018-08-11 09:27:57 -0700901 playAnimation(*mAnimation);
Damien Bargiacchi97480862016-03-29 14:55:55 -0700902
Geoffrey Pitscha9173532016-07-19 14:56:39 -0400903 if (mTimeCheckThread != nullptr) {
Damien Bargiacchi97480862016-03-29 14:55:55 -0700904 mTimeCheckThread->requestExit();
Geoffrey Pitscha9173532016-07-19 14:56:39 -0400905 mTimeCheckThread = nullptr;
906 }
907
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700908 if (clockFontInitialized) {
Huihong Luo50a2f362018-08-11 09:27:57 -0700909 glDeleteTextures(1, &mAnimation->clockFont.texture.name);
Andriy Naborskyy815e51d2016-03-24 16:43:34 -0700910 }
911
Huihong Luo50a2f362018-08-11 09:27:57 -0700912 releaseAnimation(mAnimation);
913 mAnimation = nullptr;
Damien Bargiacchi1a8a2132018-07-24 15:20:26 -0700914
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700915 return false;
916}
917
918bool BootAnimation::playAnimation(const Animation& animation)
919{
920 const size_t pcount = animation.parts.size();
Mathias Agopiana8826d62009-10-01 03:10:14 -0700921 nsecs_t frameDuration = s2ns(1) / animation.fps;
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400922 const int animationX = (mWidth - animation.width) / 2;
923 const int animationY = (mHeight - animation.height) / 2;
Mathias Agopian9f3020d2009-11-06 16:30:18 -0800924
Wei Wang159a4102018-10-23 23:15:58 -0700925 SLOGD("%sAnimationShownTiming start time: %" PRId64 "ms", mShuttingDown ? "Shutdown" : "Boot",
Keun-young Parkd1794cd2017-04-04 12:21:19 -0700926 elapsedRealtime());
Narayan Kamathafd31e02013-12-03 13:16:03 +0000927 for (size_t i=0 ; i<pcount ; i++) {
Mathias Agopiana8826d62009-10-01 03:10:14 -0700928 const Animation::Part& part(animation.parts[i]);
929 const size_t fcount = part.frames.size();
930 glBindTexture(GL_TEXTURE_2D, 0);
931
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700932 // Handle animation package
Yi Kong911ac232019-03-24 01:49:02 -0700933 if (part.animation != nullptr) {
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700934 playAnimation(*part.animation);
935 if (exitPending())
936 break;
937 continue; //to next part
938 }
939
Mathias Agopiana8826d62009-10-01 03:10:14 -0700940 for (int r=0 ; !part.count || r<part.count ; r++) {
Kevin Hesterd3782b22012-04-26 10:38:55 -0700941 // Exit any non playuntil complete parts immediately
942 if(exitPending() && !part.playUntilComplete)
943 break;
944
Ed Coyne7464ac92017-06-08 12:26:48 -0700945 mCallbacks->playPart(i, part, r);
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700946
Jesse Hall083b84c2014-09-22 10:51:09 -0700947 glClearColor(
948 part.backgroundColor[0],
949 part.backgroundColor[1],
950 part.backgroundColor[2],
951 1.0f);
952
Narayan Kamathafd31e02013-12-03 13:16:03 +0000953 for (size_t j=0 ; j<fcount && (!exitPending() || part.playUntilComplete) ; j++) {
Mathias Agopiana8826d62009-10-01 03:10:14 -0700954 const Animation::Frame& frame(part.frames[j]);
Mathias Agopiandb7dd2a2012-05-12 15:08:21 -0700955 nsecs_t lastFrame = systemTime();
Mathias Agopiana8826d62009-10-01 03:10:14 -0700956
957 if (r > 0) {
958 glBindTexture(GL_TEXTURE_2D, frame.tid);
959 } else {
960 if (part.count != 1) {
961 glGenTextures(1, &frame.tid);
962 glBindTexture(GL_TEXTURE_2D, frame.tid);
963 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
964 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
965 }
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700966 int w, h;
967 initTexture(frame.map, &w, &h);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700968 }
969
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400970 const int xc = animationX + frame.trimX;
971 const int yc = animationY + frame.trimY;
972 Region clearReg(Rect(mWidth, mHeight));
973 clearReg.subtractSelf(Rect(xc, yc, xc+frame.trimWidth, yc+frame.trimHeight));
Mathias Agopian9f3020d2009-11-06 16:30:18 -0800974 if (!clearReg.isEmpty()) {
975 Region::const_iterator head(clearReg.begin());
976 Region::const_iterator tail(clearReg.end());
977 glEnable(GL_SCISSOR_TEST);
978 while (head != tail) {
Andreas Gampecfedceb2014-09-30 21:48:18 -0700979 const Rect& r2(*head++);
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400980 glScissor(r2.left, mHeight - r2.bottom, r2.width(), r2.height());
Mathias Agopian9f3020d2009-11-06 16:30:18 -0800981 glClear(GL_COLOR_BUFFER_BIT);
982 }
983 glDisable(GL_SCISSOR_TEST);
984 }
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400985 // specify the y center as ceiling((mHeight - frame.trimHeight) / 2)
986 // which is equivalent to mHeight - (yc + frame.trimHeight)
987 glDrawTexiOES(xc, mHeight - (yc + frame.trimHeight),
988 0, frame.trimWidth, frame.trimHeight);
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700989 if (mClockEnabled && mTimeIsAccurate && validClock(part)) {
990 drawClock(animation.clockFont, part.clockPosX, part.clockPosY);
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800991 }
Adrian Roos9ee5dff2018-08-22 20:19:49 +0200992 handleViewport(frameDuration);
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800993
Mathias Agopiana8826d62009-10-01 03:10:14 -0700994 eglSwapBuffers(mDisplay, mSurface);
995
996 nsecs_t now = systemTime();
997 nsecs_t delay = frameDuration - (now - lastFrame);
Wei Wang159a4102018-10-23 23:15:58 -0700998 //SLOGD("%lld, %lld", ns2ms(now - lastFrame), ns2ms(delay));
Mathias Agopiana8826d62009-10-01 03:10:14 -0700999 lastFrame = now;
Mathias Agopiandb7dd2a2012-05-12 15:08:21 -07001000
1001 if (delay > 0) {
1002 struct timespec spec;
1003 spec.tv_sec = (now + delay) / 1000000000;
1004 spec.tv_nsec = (now + delay) % 1000000000;
1005 int err;
1006 do {
Yi Kong911ac232019-03-24 01:49:02 -07001007 err = clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &spec, nullptr);
Mathias Agopiandb7dd2a2012-05-12 15:08:21 -07001008 } while (err<0 && errno == EINTR);
1009 }
Kevin Hesterd3782b22012-04-26 10:38:55 -07001010
1011 checkExit();
Mathias Agopiana8826d62009-10-01 03:10:14 -07001012 }
Kevin Hesterd3782b22012-04-26 10:38:55 -07001013
Mathias Agopiana8826d62009-10-01 03:10:14 -07001014 usleep(part.pause * ns2us(frameDuration));
Kevin Hesterd3782b22012-04-26 10:38:55 -07001015
1016 // For infinite parts, we've now played them at least once, so perhaps exit
Adrian Roos9ee5dff2018-08-22 20:19:49 +02001017 if(exitPending() && !part.count && mCurrentInset >= mTargetInset)
Kevin Hesterd3782b22012-04-26 10:38:55 -07001018 break;
Mathias Agopiana8826d62009-10-01 03:10:14 -07001019 }
1020
Geoffrey Pitsch2fb30fb2016-07-06 16:16:20 -04001021 }
1022
1023 // Free textures created for looping parts now that the animation is done.
1024 for (const Animation::Part& part : animation.parts) {
Mathias Agopiana8826d62009-10-01 03:10:14 -07001025 if (part.count != 1) {
Geoffrey Pitsch2fb30fb2016-07-06 16:16:20 -04001026 const size_t fcount = part.frames.size();
1027 for (size_t j = 0; j < fcount; j++) {
Mathias Agopiana8826d62009-10-01 03:10:14 -07001028 const Animation::Frame& frame(part.frames[j]);
1029 glDeleteTextures(1, &frame.tid);
1030 }
1031 }
1032 }
Geoffrey Pitschd6d9a1d2016-06-08 00:38:58 -07001033
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001034 return true;
Mathias Agopiana8826d62009-10-01 03:10:14 -07001035}
1036
Adrian Roos9ee5dff2018-08-22 20:19:49 +02001037void BootAnimation::handleViewport(nsecs_t timestep) {
1038 if (mShuttingDown || !mFlingerSurfaceControl || mTargetInset == 0) {
1039 return;
1040 }
1041 if (mTargetInset < 0) {
1042 // Poll the amount for the top display inset. This will return -1 until persistent properties
1043 // have been loaded.
1044 mTargetInset = android::base::GetIntProperty("persist.sys.displayinset.top",
1045 -1 /* default */, -1 /* min */, mHeight / 2 /* max */);
1046 }
1047 if (mTargetInset <= 0) {
1048 return;
1049 }
1050
1051 if (mCurrentInset < mTargetInset) {
1052 // After the device boots, the inset will effectively be cropped away. We animate this here.
1053 float fraction = static_cast<float>(mCurrentInset) / mTargetInset;
1054 int interpolatedInset = (cosf((fraction + 1) * M_PI) / 2.0f + 0.5f) * mTargetInset;
1055
1056 SurfaceComposerClient::Transaction()
1057 .setCrop(mFlingerSurfaceControl, Rect(0, interpolatedInset, mWidth, mHeight))
1058 .apply();
1059 } else {
1060 // At the end of the animation, we switch to the viewport that DisplayManager will apply
1061 // later. This changes the coordinate system, and means we must move the surface up by
1062 // the inset amount.
Adrian Roos9ee5dff2018-08-22 20:19:49 +02001063 Rect layerStackRect(0, 0, mWidth, mHeight - mTargetInset);
1064 Rect displayRect(0, mTargetInset, mWidth, mHeight);
1065
1066 SurfaceComposerClient::Transaction t;
1067 t.setPosition(mFlingerSurfaceControl, 0, -mTargetInset)
1068 .setCrop(mFlingerSurfaceControl, Rect(0, mTargetInset, mWidth, mHeight));
Dominik Laskowski3316a0a2019-01-25 02:56:41 -08001069 t.setDisplayProjection(mDisplayToken, 0 /* orientation */, layerStackRect, displayRect);
Adrian Roos9ee5dff2018-08-22 20:19:49 +02001070 t.apply();
1071
1072 mTargetInset = mCurrentInset = 0;
1073 }
1074
1075 int delta = timestep * mTargetInset / ms2ns(200);
1076 mCurrentInset += delta;
1077}
1078
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001079void BootAnimation::releaseAnimation(Animation* animation) const
1080{
1081 for (Vector<Animation::Part>::iterator it = animation->parts.begin(),
1082 e = animation->parts.end(); it != e; ++it) {
1083 if (it->animation)
1084 releaseAnimation(it->animation);
1085 }
1086 if (animation->zip)
1087 delete animation->zip;
1088 delete animation;
1089}
1090
1091BootAnimation::Animation* BootAnimation::loadAnimation(const String8& fn)
1092{
1093 if (mLoadedFiles.indexOf(fn) >= 0) {
Wei Wang159a4102018-10-23 23:15:58 -07001094 SLOGE("File \"%s\" is already loaded. Cyclic ref is not allowed",
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001095 fn.string());
Yi Kong911ac232019-03-24 01:49:02 -07001096 return nullptr;
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001097 }
1098 ZipFileRO *zip = ZipFileRO::open(fn);
Yi Kong911ac232019-03-24 01:49:02 -07001099 if (zip == nullptr) {
Wei Wang159a4102018-10-23 23:15:58 -07001100 SLOGE("Failed to open animation zip \"%s\": %s",
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001101 fn.string(), strerror(errno));
Yi Kong911ac232019-03-24 01:49:02 -07001102 return nullptr;
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001103 }
1104
1105 Animation *animation = new Animation;
1106 animation->fileName = fn;
1107 animation->zip = zip;
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -07001108 animation->clockFont.map = nullptr;
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001109 mLoadedFiles.add(animation->fileName);
1110
1111 parseAnimationDesc(*animation);
Geoffrey Pitscha91a2d72016-07-12 14:46:19 -04001112 if (!preloadZip(*animation)) {
Yi Kong911ac232019-03-24 01:49:02 -07001113 return nullptr;
Geoffrey Pitscha91a2d72016-07-12 14:46:19 -04001114 }
1115
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001116
1117 mLoadedFiles.remove(fn);
1118 return animation;
1119}
Damien Bargiacchi97480862016-03-29 14:55:55 -07001120
1121bool BootAnimation::updateIsTimeAccurate() {
1122 static constexpr long long MAX_TIME_IN_PAST = 60000LL * 60LL * 24LL * 30LL; // 30 days
1123 static constexpr long long MAX_TIME_IN_FUTURE = 60000LL * 90LL; // 90 minutes
1124
1125 if (mTimeIsAccurate) {
1126 return true;
1127 }
Keun-young Parkb5938422017-03-23 13:46:24 -07001128 if (mShuttingDown) return true;
Damien Bargiacchi97480862016-03-29 14:55:55 -07001129 struct stat statResult;
Damien Bargiacchi9071db12016-10-28 17:38:22 -07001130
1131 if(stat(TIME_FORMAT_12_HOUR_FLAG_FILE_PATH, &statResult) == 0) {
1132 mTimeFormat12Hour = true;
1133 }
1134
Damien Bargiacchi97480862016-03-29 14:55:55 -07001135 if(stat(ACCURATE_TIME_FLAG_FILE_PATH, &statResult) == 0) {
1136 mTimeIsAccurate = true;
1137 return true;
1138 }
1139
1140 FILE* file = fopen(LAST_TIME_CHANGED_FILE_PATH, "r");
Yi Kong911ac232019-03-24 01:49:02 -07001141 if (file != nullptr) {
Damien Bargiacchi97480862016-03-29 14:55:55 -07001142 long long lastChangedTime = 0;
1143 fscanf(file, "%lld", &lastChangedTime);
1144 fclose(file);
1145 if (lastChangedTime > 0) {
1146 struct timespec now;
1147 clock_gettime(CLOCK_REALTIME, &now);
1148 // Match the Java timestamp format
1149 long long rtcNow = (now.tv_sec * 1000LL) + (now.tv_nsec / 1000000LL);
Damien Bargiacchi96762812016-07-12 15:53:40 -07001150 if (ACCURATE_TIME_EPOCH < rtcNow
1151 && lastChangedTime > (rtcNow - MAX_TIME_IN_PAST)
1152 && lastChangedTime < (rtcNow + MAX_TIME_IN_FUTURE)) {
Damien Bargiacchi97480862016-03-29 14:55:55 -07001153 mTimeIsAccurate = true;
1154 }
1155 }
1156 }
1157
1158 return mTimeIsAccurate;
1159}
1160
1161BootAnimation::TimeCheckThread::TimeCheckThread(BootAnimation* bootAnimation) : Thread(false),
1162 mInotifyFd(-1), mSystemWd(-1), mTimeWd(-1), mBootAnimation(bootAnimation) {}
1163
1164BootAnimation::TimeCheckThread::~TimeCheckThread() {
1165 // mInotifyFd may be -1 but that's ok since we're not at risk of attempting to close a valid FD.
1166 close(mInotifyFd);
1167}
1168
1169bool BootAnimation::TimeCheckThread::threadLoop() {
1170 bool shouldLoop = doThreadLoop() && !mBootAnimation->mTimeIsAccurate
1171 && mBootAnimation->mClockEnabled;
1172 if (!shouldLoop) {
1173 close(mInotifyFd);
1174 mInotifyFd = -1;
1175 }
1176 return shouldLoop;
1177}
1178
1179bool BootAnimation::TimeCheckThread::doThreadLoop() {
1180 static constexpr int BUFF_LEN (10 * (sizeof(struct inotify_event) + NAME_MAX + 1));
1181
1182 // Poll instead of doing a blocking read so the Thread can exit if requested.
1183 struct pollfd pfd = { mInotifyFd, POLLIN, 0 };
1184 ssize_t pollResult = poll(&pfd, 1, 1000);
1185
1186 if (pollResult == 0) {
1187 return true;
1188 } else if (pollResult < 0) {
Wei Wang159a4102018-10-23 23:15:58 -07001189 SLOGE("Could not poll inotify events");
Damien Bargiacchi97480862016-03-29 14:55:55 -07001190 return false;
1191 }
1192
1193 char buff[BUFF_LEN] __attribute__ ((aligned(__alignof__(struct inotify_event))));;
1194 ssize_t length = read(mInotifyFd, buff, BUFF_LEN);
1195 if (length == 0) {
1196 return true;
1197 } else if (length < 0) {
Wei Wang159a4102018-10-23 23:15:58 -07001198 SLOGE("Could not read inotify events");
Damien Bargiacchi97480862016-03-29 14:55:55 -07001199 return false;
1200 }
1201
1202 const struct inotify_event *event;
1203 for (char* ptr = buff; ptr < buff + length; ptr += sizeof(struct inotify_event) + event->len) {
1204 event = (const struct inotify_event *) ptr;
1205 if (event->wd == mSystemWd && strcmp(SYSTEM_TIME_DIR_NAME, event->name) == 0) {
1206 addTimeDirWatch();
1207 } else if (event->wd == mTimeWd && (strcmp(LAST_TIME_CHANGED_FILE_NAME, event->name) == 0
1208 || strcmp(ACCURATE_TIME_FLAG_FILE_NAME, event->name) == 0)) {
1209 return !mBootAnimation->updateIsTimeAccurate();
1210 }
1211 }
1212
1213 return true;
1214}
1215
1216void BootAnimation::TimeCheckThread::addTimeDirWatch() {
1217 mTimeWd = inotify_add_watch(mInotifyFd, SYSTEM_TIME_DIR_PATH,
1218 IN_CLOSE_WRITE | IN_MOVED_TO | IN_ATTRIB);
1219 if (mTimeWd > 0) {
1220 // No need to watch for the time directory to be created if it already exists
1221 inotify_rm_watch(mInotifyFd, mSystemWd);
1222 mSystemWd = -1;
1223 }
1224}
1225
1226status_t BootAnimation::TimeCheckThread::readyToRun() {
1227 mInotifyFd = inotify_init();
1228 if (mInotifyFd < 0) {
Wei Wang159a4102018-10-23 23:15:58 -07001229 SLOGE("Could not initialize inotify fd");
Damien Bargiacchi97480862016-03-29 14:55:55 -07001230 return NO_INIT;
1231 }
1232
1233 mSystemWd = inotify_add_watch(mInotifyFd, SYSTEM_DATA_DIR_PATH, IN_CREATE | IN_ATTRIB);
1234 if (mSystemWd < 0) {
1235 close(mInotifyFd);
1236 mInotifyFd = -1;
Wei Wang159a4102018-10-23 23:15:58 -07001237 SLOGE("Could not add watch for %s", SYSTEM_DATA_DIR_PATH);
Damien Bargiacchi97480862016-03-29 14:55:55 -07001238 return NO_INIT;
1239 }
1240
1241 addTimeDirWatch();
1242
1243 if (mBootAnimation->updateIsTimeAccurate()) {
1244 close(mInotifyFd);
1245 mInotifyFd = -1;
1246 return ALREADY_EXISTS;
1247 }
1248
1249 return NO_ERROR;
1250}
1251
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001252// ---------------------------------------------------------------------------
1253
1254}
1255; // namespace android