blob: 87f7224702e156e47fa3fe71bfe23530e838ad74 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -070017#define LOG_NDEBUG 0
Mathias Agopianbc726112009-09-23 15:44:05 -070018#define LOG_TAG "BootAnimation"
19
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080020#include <stdint.h>
Wei Wanga90c54c2017-02-02 11:35:21 -080021#include <inttypes.h>
Damien Bargiacchi97480862016-03-29 14:55:55 -070022#include <sys/inotify.h>
23#include <sys/poll.h>
24#include <sys/stat.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080025#include <sys/types.h>
26#include <math.h>
27#include <fcntl.h>
28#include <utils/misc.h>
Mathias Agopianb4d5a722009-09-23 17:05:19 -070029#include <signal.h>
Elliott Hughesbb94f312014-10-21 10:41:33 -070030#include <time.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080031
Steven Morelandfb7952f2018-02-23 14:58:50 -080032#include <cutils/atomic.h>
Jason parksbd9a08d2011-01-31 15:04:34 -060033#include <cutils/properties.h>
34
Mathias Agopianb13b9bd2012-02-17 18:27:36 -080035#include <androidfw/AssetManager.h>
Mathias Agopianac31a3b2009-05-21 19:59:24 -070036#include <binder/IPCThreadState.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080037#include <utils/Errors.h>
38#include <utils/Log.h>
Wei Wanga90c54c2017-02-02 11:35:21 -080039#include <utils/SystemClock.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080040
Keun-young Parkb5938422017-03-23 13:46:24 -070041#include <android-base/properties.h>
42
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080043#include <ui/PixelFormat.h>
44#include <ui/Rect.h>
45#include <ui/Region.h>
46#include <ui/DisplayInfo.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080047
Jeff Brown0b722fe2012-08-24 22:40:14 -070048#include <gui/ISurfaceComposer.h>
Mathias Agopian8335f1c2012-02-25 18:48:35 -080049#include <gui/Surface.h>
50#include <gui/SurfaceComposerClient.h>
Mathias Agopian000479f2010-02-09 17:46:37 -080051
Andreas Gampecfedceb2014-09-30 21:48:18 -070052// TODO: Fix Skia.
53#pragma GCC diagnostic push
54#pragma GCC diagnostic ignored "-Wunused-parameter"
Derek Sollenbergereece0dd2014-02-27 14:31:29 -050055#include <SkBitmap.h>
Matt Sarett8898c162016-03-24 16:11:01 -040056#include <SkImage.h>
Derek Sollenbergereece0dd2014-02-27 14:31:29 -050057#include <SkStream.h>
Andreas Gampecfedceb2014-09-30 21:48:18 -070058#pragma GCC diagnostic pop
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080059
60#include <GLES/gl.h>
61#include <GLES/glext.h>
62#include <EGL/eglext.h>
63
64#include "BootAnimation.h"
65
66namespace android {
67
Damien Bargiacchi97480862016-03-29 14:55:55 -070068static const char OEM_BOOTANIMATION_FILE[] = "/oem/media/bootanimation.zip";
Beverly34ffe252019-05-17 11:03:54 -040069static const char PRODUCT_BOOTANIMATION_DARK_FILE[] = "/product/media/bootanimation-dark.zip";
Jaekyun Seokc521bb32018-01-30 11:52:47 +090070static const char PRODUCT_BOOTANIMATION_FILE[] = "/product/media/bootanimation.zip";
Damien Bargiacchi97480862016-03-29 14:55:55 -070071static const char SYSTEM_BOOTANIMATION_FILE[] = "/system/media/bootanimation.zip";
Anders Fridlunde0b4d232018-11-06 14:23:25 +010072static const char APEX_BOOTANIMATION_FILE[] = "/apex/com.android.bootanimation/etc/bootanimation.zip";
Jaekyun Seokc521bb32018-01-30 11:52:47 +090073static const char PRODUCT_ENCRYPTED_BOOTANIMATION_FILE[] = "/product/media/bootanimation-encrypted.zip";
Damien Bargiacchi97480862016-03-29 14:55:55 -070074static const char SYSTEM_ENCRYPTED_BOOTANIMATION_FILE[] = "/system/media/bootanimation-encrypted.zip";
Keun-young Parkb5938422017-03-23 13:46:24 -070075static const char OEM_SHUTDOWNANIMATION_FILE[] = "/oem/media/shutdownanimation.zip";
Jaekyun Seokc521bb32018-01-30 11:52:47 +090076static const char PRODUCT_SHUTDOWNANIMATION_FILE[] = "/product/media/shutdownanimation.zip";
Keun-young Parkb5938422017-03-23 13:46:24 -070077static const char SYSTEM_SHUTDOWNANIMATION_FILE[] = "/system/media/shutdownanimation.zip";
78
Damien Bargiacchi97480862016-03-29 14:55:55 -070079static const char SYSTEM_DATA_DIR_PATH[] = "/data/system";
80static const char SYSTEM_TIME_DIR_NAME[] = "time";
81static const char SYSTEM_TIME_DIR_PATH[] = "/data/system/time";
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -070082static const char CLOCK_FONT_ASSET[] = "images/clock_font.png";
83static const char CLOCK_FONT_ZIP_NAME[] = "clock_font.png";
Damien Bargiacchi97480862016-03-29 14:55:55 -070084static const char LAST_TIME_CHANGED_FILE_NAME[] = "last_time_change";
85static const char LAST_TIME_CHANGED_FILE_PATH[] = "/data/system/time/last_time_change";
86static const char ACCURATE_TIME_FLAG_FILE_NAME[] = "time_is_accurate";
87static const char ACCURATE_TIME_FLAG_FILE_PATH[] = "/data/system/time/time_is_accurate";
Damien Bargiacchi9071db12016-10-28 17:38:22 -070088static const char TIME_FORMAT_12_HOUR_FLAG_FILE_PATH[] = "/data/system/time/time_format_12_hour";
Damien Bargiacchi96762812016-07-12 15:53:40 -070089// Java timestamp format. Don't show the clock if the date is before 2000-01-01 00:00:00.
90static const long long ACCURATE_TIME_EPOCH = 946684800000;
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -070091static constexpr char FONT_BEGIN_CHAR = ' ';
92static constexpr char FONT_END_CHAR = '~' + 1;
93static constexpr size_t FONT_NUM_CHARS = FONT_END_CHAR - FONT_BEGIN_CHAR + 1;
94static constexpr size_t FONT_NUM_COLS = 16;
95static constexpr size_t FONT_NUM_ROWS = FONT_NUM_CHARS / FONT_NUM_COLS;
96static const int TEXT_CENTER_VALUE = INT_MAX;
97static const int TEXT_MISSING_VALUE = INT_MIN;
Damien Bargiacchi97480862016-03-29 14:55:55 -070098static const char EXIT_PROP_NAME[] = "service.bootanim.exit";
Narayan Kamathafd31e02013-12-03 13:16:03 +000099static const int ANIM_ENTRY_NAME_MAX = 256;
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700100static constexpr size_t TEXT_POS_LEN_MAX = 16;
Narayan Kamathafd31e02013-12-03 13:16:03 +0000101
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800102// ---------------------------------------------------------------------------
103
Ed Coyne7464ac92017-06-08 12:26:48 -0700104BootAnimation::BootAnimation(sp<Callbacks> callbacks)
Ed Coyne2c9e94a2017-05-31 10:08:28 -0700105 : Thread(false), mClockEnabled(true), mTimeIsAccurate(false),
Yi Kong911ac232019-03-24 01:49:02 -0700106 mTimeFormat12Hour(false), mTimeCheckThread(nullptr), mCallbacks(callbacks) {
Mathias Agopian627e7b52009-05-21 19:21:59 -0700107 mSession = new SurfaceComposerClient();
Geoffrey Pitsch290c4352016-08-09 14:35:10 -0400108
Keun-young Parkb5938422017-03-23 13:46:24 -0700109 std::string powerCtl = android::base::GetProperty("sys.powerctl", "");
110 if (powerCtl.empty()) {
111 mShuttingDown = false;
112 } else {
113 mShuttingDown = true;
114 }
Huihong Luo50a2f362018-08-11 09:27:57 -0700115 ALOGD("%sAnimationStartTiming start time: %" PRId64 "ms", mShuttingDown ? "Shutdown" : "Boot",
116 elapsedRealtime());
117}
118
119BootAnimation::~BootAnimation() {
120 if (mAnimation != nullptr) {
121 releaseAnimation(mAnimation);
122 mAnimation = nullptr;
123 }
124 ALOGD("%sAnimationStopTiming start time: %" PRId64 "ms", mShuttingDown ? "Shutdown" : "Boot",
125 elapsedRealtime());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800126}
127
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800128void BootAnimation::onFirstRef() {
Mathias Agopianbc726112009-09-23 15:44:05 -0700129 status_t err = mSession->linkToComposerDeath(this);
Wei Wang159a4102018-10-23 23:15:58 -0700130 SLOGE_IF(err, "linkToComposerDeath failed (%s) ", strerror(-err));
Mathias Agopian8434c532009-09-23 18:52:49 -0700131 if (err == NO_ERROR) {
Huihong Luo50a2f362018-08-11 09:27:57 -0700132 // Load the animation content -- this can be slow (eg 200ms)
133 // called before waitForSurfaceFlinger() in main() to avoid wait
134 ALOGD("%sAnimationPreloadTiming start time: %" PRId64 "ms",
135 mShuttingDown ? "Shutdown" : "Boot", elapsedRealtime());
136 preloadAnimation();
137 ALOGD("%sAnimationPreloadStopTiming start time: %" PRId64 "ms",
138 mShuttingDown ? "Shutdown" : "Boot", elapsedRealtime());
Mathias Agopianbc726112009-09-23 15:44:05 -0700139 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800140}
141
Mathias Agopianbc726112009-09-23 15:44:05 -0700142sp<SurfaceComposerClient> BootAnimation::session() const {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800143 return mSession;
144}
145
Narayan Kamathafd31e02013-12-03 13:16:03 +0000146void BootAnimation::binderDied(const wp<IBinder>&)
Mathias Agopianbc726112009-09-23 15:44:05 -0700147{
148 // woah, surfaceflinger died!
Wei Wang159a4102018-10-23 23:15:58 -0700149 SLOGD("SurfaceFlinger died, exiting...");
Mathias Agopianbc726112009-09-23 15:44:05 -0700150
151 // calling requestExit() is not enough here because the Surface code
152 // might be blocked on a condition variable that will never be updated.
153 kill( getpid(), SIGKILL );
154 requestExit();
155}
156
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800157status_t BootAnimation::initTexture(Texture* texture, AssetManager& assets,
158 const char* name) {
159 Asset* asset = assets.open(name, Asset::ACCESS_BUFFER);
Yi Kong911ac232019-03-24 01:49:02 -0700160 if (asset == nullptr)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800161 return NO_INIT;
162 SkBitmap bitmap;
Matt Sarett8898c162016-03-24 16:11:01 -0400163 sk_sp<SkData> data = SkData::MakeWithoutCopy(asset->getBuffer(false),
164 asset->getLength());
165 sk_sp<SkImage> image = SkImage::MakeFromEncoded(data);
166 image->asLegacyBitmap(&bitmap, SkImage::kRO_LegacyBitmapMode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800167 asset->close();
168 delete asset;
169
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800170 const int w = bitmap.width();
171 const int h = bitmap.height();
172 const void* p = bitmap.getPixels();
173
174 GLint crop[4] = { 0, h, w, -h };
175 texture->w = w;
176 texture->h = h;
177
178 glGenTextures(1, &texture->name);
179 glBindTexture(GL_TEXTURE_2D, texture->name);
180
Mike Reed42a1d082014-07-07 18:06:18 -0400181 switch (bitmap.colorType()) {
182 case kAlpha_8_SkColorType:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800183 glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, w, h, 0, GL_ALPHA,
184 GL_UNSIGNED_BYTE, p);
185 break;
Mike Reed42a1d082014-07-07 18:06:18 -0400186 case kARGB_4444_SkColorType:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800187 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA,
188 GL_UNSIGNED_SHORT_4_4_4_4, p);
189 break;
Mike Reed42a1d082014-07-07 18:06:18 -0400190 case kN32_SkColorType:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800191 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA,
192 GL_UNSIGNED_BYTE, p);
193 break;
Mike Reed42a1d082014-07-07 18:06:18 -0400194 case kRGB_565_SkColorType:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800195 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, w, h, 0, GL_RGB,
196 GL_UNSIGNED_SHORT_5_6_5, p);
197 break;
198 default:
199 break;
200 }
201
202 glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_CROP_RECT_OES, crop);
203 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
204 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
205 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
206 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700207
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800208 return NO_ERROR;
209}
210
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700211status_t BootAnimation::initTexture(FileMap* map, int* width, int* height)
Mathias Agopiana8826d62009-10-01 03:10:14 -0700212{
Mathias Agopiana8826d62009-10-01 03:10:14 -0700213 SkBitmap bitmap;
Damien Bargiacchif67b3172016-09-07 20:17:14 -0700214 sk_sp<SkData> data = SkData::MakeWithoutCopy(map->getDataPtr(),
215 map->getDataLength());
Matt Sarett8898c162016-03-24 16:11:01 -0400216 sk_sp<SkImage> image = SkImage::MakeFromEncoded(data);
217 image->asLegacyBitmap(&bitmap, SkImage::kRO_LegacyBitmapMode);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700218
Mykola Kondratenko0c1eeb32014-04-15 09:35:44 +0200219 // FileMap memory is never released until application exit.
220 // Release it now as the texture is already loaded and the memory used for
221 // the packed resource can be released.
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700222 delete map;
Mykola Kondratenko0c1eeb32014-04-15 09:35:44 +0200223
Mathias Agopiana8826d62009-10-01 03:10:14 -0700224 const int w = bitmap.width();
225 const int h = bitmap.height();
226 const void* p = bitmap.getPixels();
227
228 GLint crop[4] = { 0, h, w, -h };
229 int tw = 1 << (31 - __builtin_clz(w));
230 int th = 1 << (31 - __builtin_clz(h));
231 if (tw < w) tw <<= 1;
232 if (th < h) th <<= 1;
233
Mike Reed42a1d082014-07-07 18:06:18 -0400234 switch (bitmap.colorType()) {
235 case kN32_SkColorType:
Sai Kiran Korwar27167492015-07-07 20:00:06 +0530236 if (!mUseNpotTextures && (tw != w || th != h)) {
Mathias Agopiana8826d62009-10-01 03:10:14 -0700237 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, tw, th, 0, GL_RGBA,
Yi Kong911ac232019-03-24 01:49:02 -0700238 GL_UNSIGNED_BYTE, nullptr);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700239 glTexSubImage2D(GL_TEXTURE_2D, 0,
240 0, 0, w, h, GL_RGBA, GL_UNSIGNED_BYTE, p);
241 } else {
Sai Kiran Korwar27167492015-07-07 20:00:06 +0530242 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA,
Mathias Agopiana8826d62009-10-01 03:10:14 -0700243 GL_UNSIGNED_BYTE, p);
244 }
245 break;
246
Mike Reed42a1d082014-07-07 18:06:18 -0400247 case kRGB_565_SkColorType:
Sai Kiran Korwar27167492015-07-07 20:00:06 +0530248 if (!mUseNpotTextures && (tw != w || th != h)) {
Mathias Agopiana8826d62009-10-01 03:10:14 -0700249 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, tw, th, 0, GL_RGB,
Yi Kong911ac232019-03-24 01:49:02 -0700250 GL_UNSIGNED_SHORT_5_6_5, nullptr);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700251 glTexSubImage2D(GL_TEXTURE_2D, 0,
252 0, 0, w, h, GL_RGB, GL_UNSIGNED_SHORT_5_6_5, p);
253 } else {
Sai Kiran Korwar27167492015-07-07 20:00:06 +0530254 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, w, h, 0, GL_RGB,
Mathias Agopiana8826d62009-10-01 03:10:14 -0700255 GL_UNSIGNED_SHORT_5_6_5, p);
256 }
257 break;
258 default:
259 break;
260 }
261
262 glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_CROP_RECT_OES, crop);
263
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700264 *width = w;
265 *height = h;
266
Mathias Agopiana8826d62009-10-01 03:10:14 -0700267 return NO_ERROR;
268}
269
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800270status_t BootAnimation::readyToRun() {
271 mAssets.addDefaultAssets();
272
Dominik Laskowski3316a0a2019-01-25 02:56:41 -0800273 mDisplayToken = SurfaceComposerClient::getInternalDisplayToken();
274 if (mDisplayToken == nullptr)
275 return -1;
276
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800277 DisplayInfo dinfo;
Dominik Laskowski3316a0a2019-01-25 02:56:41 -0800278 status_t status = SurfaceComposerClient::getDisplayInfo(mDisplayToken, &dinfo);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800279 if (status)
280 return -1;
281
282 // create the native surface
Jeff Brown0b722fe2012-08-24 22:40:14 -0700283 sp<SurfaceControl> control = session()->createSurface(String8("BootAnimation"),
284 dinfo.w, dinfo.h, PIXEL_FORMAT_RGB_565);
Mathias Agopian439863f2011-06-28 19:09:31 -0700285
Robert Carre13b58e2017-08-31 14:50:44 -0700286 SurfaceComposerClient::Transaction t;
287 t.setLayer(control, 0x40000000)
288 .apply();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800289
Mathias Agopian17f638b2009-04-16 20:04:08 -0700290 sp<Surface> s = control->getSurface();
291
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800292 // initialize opengl and egl
Mathias Agopian738b9a42009-08-06 16:41:02 -0700293 const EGLint attribs[] = {
Mathias Agopian1b253b72011-08-15 15:20:22 -0700294 EGL_RED_SIZE, 8,
295 EGL_GREEN_SIZE, 8,
296 EGL_BLUE_SIZE, 8,
Mathias Agopiana8826d62009-10-01 03:10:14 -0700297 EGL_DEPTH_SIZE, 0,
298 EGL_NONE
Mathias Agopian738b9a42009-08-06 16:41:02 -0700299 };
Andreas Gampecfedceb2014-09-30 21:48:18 -0700300 EGLint w, h;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800301 EGLint numConfigs;
302 EGLConfig config;
303 EGLSurface surface;
304 EGLContext context;
Mathias Agopian627e7b52009-05-21 19:21:59 -0700305
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800306 EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
Mathias Agopian627e7b52009-05-21 19:21:59 -0700307
Yi Kong911ac232019-03-24 01:49:02 -0700308 eglInitialize(display, nullptr, nullptr);
Mathias Agopian1b253b72011-08-15 15:20:22 -0700309 eglChooseConfig(display, attribs, &config, 1, &numConfigs);
Yi Kong911ac232019-03-24 01:49:02 -0700310 surface = eglCreateWindowSurface(display, config, s.get(), nullptr);
311 context = eglCreateContext(display, config, nullptr, nullptr);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800312 eglQuerySurface(display, surface, EGL_WIDTH, &w);
313 eglQuerySurface(display, surface, EGL_HEIGHT, &h);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700314
Mathias Agopianabac0102009-07-31 14:47:00 -0700315 if (eglMakeCurrent(display, surface, surface, context) == EGL_FALSE)
316 return NO_INIT;
Mathias Agopiana8826d62009-10-01 03:10:14 -0700317
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800318 mDisplay = display;
319 mContext = context;
320 mSurface = surface;
321 mWidth = w;
322 mHeight = h;
Mathias Agopian17f638b2009-04-16 20:04:08 -0700323 mFlingerSurfaceControl = control;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800324 mFlingerSurface = s;
Adrian Roos9ee5dff2018-08-22 20:19:49 +0200325 mTargetInset = -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800326
Huihong Luo50a2f362018-08-11 09:27:57 -0700327 return NO_ERROR;
328}
329
330bool BootAnimation::preloadAnimation() {
331 findBootAnimationFile();
332 if (!mZipFileName.isEmpty()) {
333 mAnimation = loadAnimation(mZipFileName);
334 return (mAnimation != nullptr);
335 }
336
337 return false;
338}
339
340void BootAnimation::findBootAnimationFile() {
Elliott Hughesc367d482013-10-29 13:12:55 -0700341 // If the device has encryption turned on or is in process
Jason parksbd9a08d2011-01-31 15:04:34 -0600342 // of being encrypted we show the encrypted boot animation.
343 char decrypt[PROPERTY_VALUE_MAX];
344 property_get("vold.decrypt", decrypt, "");
345
Keun-young Parkb5938422017-03-23 13:46:24 -0700346 bool encryptedAnimation = atoi(decrypt) != 0 ||
347 !strcmp("trigger_restart_min_framework", decrypt);
Jason parksbd9a08d2011-01-31 15:04:34 -0600348
Jaekyun Seokc521bb32018-01-30 11:52:47 +0900349 if (!mShuttingDown && encryptedAnimation) {
350 static const char* encryptedBootFiles[] =
351 {PRODUCT_ENCRYPTED_BOOTANIMATION_FILE, SYSTEM_ENCRYPTED_BOOTANIMATION_FILE};
352 for (const char* f : encryptedBootFiles) {
353 if (access(f, R_OK) == 0) {
354 mZipFileName = f;
Huihong Luo50a2f362018-08-11 09:27:57 -0700355 return;
Jaekyun Seokc521bb32018-01-30 11:52:47 +0900356 }
357 }
Jason parksbd9a08d2011-01-31 15:04:34 -0600358 }
Beverly34ffe252019-05-17 11:03:54 -0400359
Weijie Wangf0ee1b92020-02-03 17:02:23 +0800360 std::string custAnimProp = !mShuttingDown ?
361 android::base::GetProperty("persist.sys.customanim.boot", ""):
362 android::base::GetProperty("persist.sys.customanim.shutdown", "");
363 const char *custAnim = custAnimProp.c_str();
364 ALOGD("Animation customzation path: %s", custAnim);
365 if (access(custAnim, R_OK) == 0) {
366 mZipFileName = custAnim;
367 ALOGD("%sAnimation customzation path: %s", mShuttingDown ? "Shutdown" : "Boot", mZipFileName.c_str());
368 return;
369 }
370
Beverly34ffe252019-05-17 11:03:54 -0400371 const bool playDarkAnim = android::base::GetIntProperty("ro.boot.theme", 0) == 1;
Jaekyun Seokc521bb32018-01-30 11:52:47 +0900372 static const char* bootFiles[] =
Anders Fridlunde0b4d232018-11-06 14:23:25 +0100373 {APEX_BOOTANIMATION_FILE, playDarkAnim ? PRODUCT_BOOTANIMATION_DARK_FILE : PRODUCT_BOOTANIMATION_FILE,
Beverly34ffe252019-05-17 11:03:54 -0400374 OEM_BOOTANIMATION_FILE, SYSTEM_BOOTANIMATION_FILE};
Keun-young Parkb5938422017-03-23 13:46:24 -0700375 static const char* shutdownFiles[] =
Anders Fridlunde0b4d232018-11-06 14:23:25 +0100376 {PRODUCT_SHUTDOWNANIMATION_FILE, OEM_SHUTDOWNANIMATION_FILE, SYSTEM_SHUTDOWNANIMATION_FILE, ""};
Keun-young Parkb5938422017-03-23 13:46:24 -0700377
378 for (const char* f : (!mShuttingDown ? bootFiles : shutdownFiles)) {
379 if (access(f, R_OK) == 0) {
380 mZipFileName = f;
Huihong Luo50a2f362018-08-11 09:27:57 -0700381 return;
Keun-young Parkb5938422017-03-23 13:46:24 -0700382 }
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700383 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800384}
385
Mathias Agopiana8826d62009-10-01 03:10:14 -0700386bool BootAnimation::threadLoop()
387{
388 bool r;
Narayan Kamathafd31e02013-12-03 13:16:03 +0000389 // We have no bootanimation file, so we use the stock android logo
390 // animation.
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700391 if (mZipFileName.isEmpty()) {
Mathias Agopiana8826d62009-10-01 03:10:14 -0700392 r = android();
393 } else {
394 r = movie();
395 }
396
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800397 eglMakeCurrent(mDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
398 eglDestroyContext(mDisplay, mContext);
399 eglDestroySurface(mDisplay, mSurface);
Mathias Agopian6cf0db22009-04-17 19:36:26 -0700400 mFlingerSurface.clear();
Mathias Agopian17f638b2009-04-16 20:04:08 -0700401 mFlingerSurfaceControl.clear();
Mathias Agopian627e7b52009-05-21 19:21:59 -0700402 eglTerminate(mDisplay);
Sai Kiran Korwar3eee9fb2015-06-16 17:13:35 +0530403 eglReleaseThread();
Mathias Agopian627e7b52009-05-21 19:21:59 -0700404 IPCThreadState::self()->stopProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800405 return r;
406}
407
Mathias Agopiana8826d62009-10-01 03:10:14 -0700408bool BootAnimation::android()
409{
Wei Wang159a4102018-10-23 23:15:58 -0700410 SLOGD("%sAnimationShownTiming start time: %" PRId64 "ms", mShuttingDown ? "Shutdown" : "Boot",
Keun-young Parkd1794cd2017-04-04 12:21:19 -0700411 elapsedRealtime());
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700412 initTexture(&mAndroid[0], mAssets, "images/android-logo-mask.png");
413 initTexture(&mAndroid[1], mAssets, "images/android-logo-shine.png");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800414
Ed Coyne7464ac92017-06-08 12:26:48 -0700415 mCallbacks->init({});
416
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800417 // clear screen
Mathias Agopiana8826d62009-10-01 03:10:14 -0700418 glShadeModel(GL_FLAT);
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700419 glDisable(GL_DITHER);
420 glDisable(GL_SCISSOR_TEST);
Mathias Agopian59f19e42011-05-06 19:22:12 -0700421 glClearColor(0,0,0,1);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800422 glClear(GL_COLOR_BUFFER_BIT);
423 eglSwapBuffers(mDisplay, mSurface);
424
Mathias Agopiana8826d62009-10-01 03:10:14 -0700425 glEnable(GL_TEXTURE_2D);
426 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
427
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700428 const GLint xc = (mWidth - mAndroid[0].w) / 2;
429 const GLint yc = (mHeight - mAndroid[0].h) / 2;
430 const Rect updateRect(xc, yc, xc + mAndroid[0].w, yc + mAndroid[0].h);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800431
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800432 glScissor(updateRect.left, mHeight - updateRect.bottom, updateRect.width(),
433 updateRect.height());
434
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700435 // Blend state
436 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
437 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
438
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800439 const nsecs_t startTime = systemTime();
440 do {
Mathias Agopian13796652009-03-24 22:49:21 -0700441 nsecs_t now = systemTime();
442 double time = now - startTime;
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700443 float t = 4.0f * float(time / us2ns(16667)) / mAndroid[1].w;
444 GLint offset = (1 - (t - floorf(t))) * mAndroid[1].w;
445 GLint x = xc - offset;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800446
Mathias Agopian81668642009-07-28 11:41:30 -0700447 glDisable(GL_SCISSOR_TEST);
448 glClear(GL_COLOR_BUFFER_BIT);
449
450 glEnable(GL_SCISSOR_TEST);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800451 glDisable(GL_BLEND);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800452 glBindTexture(GL_TEXTURE_2D, mAndroid[1].name);
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700453 glDrawTexiOES(x, yc, 0, mAndroid[1].w, mAndroid[1].h);
454 glDrawTexiOES(x + mAndroid[1].w, yc, 0, mAndroid[1].w, mAndroid[1].h);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800455
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700456 glEnable(GL_BLEND);
457 glBindTexture(GL_TEXTURE_2D, mAndroid[0].name);
458 glDrawTexiOES(xc, yc, 0, mAndroid[0].w, mAndroid[0].h);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800459
Mathias Agopian627e7b52009-05-21 19:21:59 -0700460 EGLBoolean res = eglSwapBuffers(mDisplay, mSurface);
461 if (res == EGL_FALSE)
462 break;
463
Mathias Agopian13796652009-03-24 22:49:21 -0700464 // 12fps: don't animate too fast to preserve CPU
465 const nsecs_t sleepTime = 83333 - ns2us(systemTime() - now);
466 if (sleepTime > 0)
Mathias Agopiana8826d62009-10-01 03:10:14 -0700467 usleep(sleepTime);
Kevin Hesterd3782b22012-04-26 10:38:55 -0700468
469 checkExit();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800470 } while (!exitPending());
471
472 glDeleteTextures(1, &mAndroid[0].name);
473 glDeleteTextures(1, &mAndroid[1].name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800474 return false;
475}
476
Kevin Hesterd3782b22012-04-26 10:38:55 -0700477void BootAnimation::checkExit() {
478 // Allow surface flinger to gracefully request shutdown
479 char value[PROPERTY_VALUE_MAX];
480 property_get(EXIT_PROP_NAME, value, "0");
481 int exitnow = atoi(value);
482 if (exitnow) {
483 requestExit();
Ed Coyne7464ac92017-06-08 12:26:48 -0700484 mCallbacks->shutdown();
Kevin Hesterd3782b22012-04-26 10:38:55 -0700485 }
486}
487
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700488bool BootAnimation::validClock(const Animation::Part& part) {
489 return part.clockPosX != TEXT_MISSING_VALUE && part.clockPosY != TEXT_MISSING_VALUE;
490}
491
492bool parseTextCoord(const char* str, int* dest) {
493 if (strcmp("c", str) == 0) {
494 *dest = TEXT_CENTER_VALUE;
495 return true;
496 }
497
498 char* end;
499 int val = (int) strtol(str, &end, 0);
500 if (end == str || *end != '\0' || val == INT_MAX || val == INT_MIN) {
501 return false;
502 }
503 *dest = val;
504 return true;
505}
506
507// Parse two position coordinates. If only string is non-empty, treat it as the y value.
508void parsePosition(const char* str1, const char* str2, int* x, int* y) {
509 bool success = false;
510 if (strlen(str1) == 0) { // No values were specified
511 // success = false
512 } else if (strlen(str2) == 0) { // we have only one value
513 if (parseTextCoord(str1, y)) {
514 *x = TEXT_CENTER_VALUE;
515 success = true;
516 }
517 } else {
518 if (parseTextCoord(str1, x) && parseTextCoord(str2, y)) {
519 success = true;
520 }
521 }
522
523 if (!success) {
524 *x = TEXT_MISSING_VALUE;
525 *y = TEXT_MISSING_VALUE;
526 }
527}
528
Jesse Hall083b84c2014-09-22 10:51:09 -0700529// Parse a color represented as an HTML-style 'RRGGBB' string: each pair of
530// characters in str is a hex number in [0, 255], which are converted to
531// floating point values in the range [0.0, 1.0] and placed in the
532// corresponding elements of color.
533//
534// If the input string isn't valid, parseColor returns false and color is
535// left unchanged.
536static bool parseColor(const char str[7], float color[3]) {
537 float tmpColor[3];
538 for (int i = 0; i < 3; i++) {
539 int val = 0;
540 for (int j = 0; j < 2; j++) {
541 val *= 16;
542 char c = str[2*i + j];
543 if (c >= '0' && c <= '9') val += c - '0';
544 else if (c >= 'A' && c <= 'F') val += (c - 'A') + 10;
545 else if (c >= 'a' && c <= 'f') val += (c - 'a') + 10;
546 else return false;
547 }
548 tmpColor[i] = static_cast<float>(val) / 255.0f;
549 }
550 memcpy(color, tmpColor, sizeof(tmpColor));
551 return true;
552}
553
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700554
555static bool readFile(ZipFileRO* zip, const char* name, String8& outString)
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700556{
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700557 ZipEntryRO entry = zip->findEntryByName(name);
Wei Wang159a4102018-10-23 23:15:58 -0700558 SLOGE_IF(!entry, "couldn't find %s", name);
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700559 if (!entry) {
560 return false;
561 }
562
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700563 FileMap* entryMap = zip->createEntryFileMap(entry);
564 zip->releaseEntry(entry);
Wei Wang159a4102018-10-23 23:15:58 -0700565 SLOGE_IF(!entryMap, "entryMap is null");
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700566 if (!entryMap) {
567 return false;
568 }
569
570 outString.setTo((char const*)entryMap->getDataPtr(), entryMap->getDataLength());
Narayan Kamath688ff4c2015-02-23 15:47:54 +0000571 delete entryMap;
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700572 return true;
573}
574
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700575// The font image should be a 96x2 array of character images. The
576// columns are the printable ASCII characters 0x20 - 0x7f. The
577// top row is regular text; the bottom row is bold.
578status_t BootAnimation::initFont(Font* font, const char* fallback) {
579 status_t status = NO_ERROR;
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800580
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700581 if (font->map != nullptr) {
582 glGenTextures(1, &font->texture.name);
583 glBindTexture(GL_TEXTURE_2D, font->texture.name);
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800584
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700585 status = initTexture(font->map, &font->texture.w, &font->texture.h);
586
587 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
588 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
589 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
590 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
591 } else if (fallback != nullptr) {
592 status = initTexture(&font->texture, mAssets, fallback);
593 } else {
594 return NO_INIT;
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800595 }
596
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700597 if (status == NO_ERROR) {
598 font->char_width = font->texture.w / FONT_NUM_COLS;
599 font->char_height = font->texture.h / FONT_NUM_ROWS / 2; // There are bold and regular rows
600 }
601
602 return status;
603}
604
605void BootAnimation::drawText(const char* str, const Font& font, bool bold, int* x, int* y) {
606 glEnable(GL_BLEND); // Allow us to draw on top of the animation
607 glBindTexture(GL_TEXTURE_2D, font.texture.name);
608
609 const int len = strlen(str);
610 const int strWidth = font.char_width * len;
611
612 if (*x == TEXT_CENTER_VALUE) {
613 *x = (mWidth - strWidth) / 2;
614 } else if (*x < 0) {
615 *x = mWidth + *x - strWidth;
616 }
617 if (*y == TEXT_CENTER_VALUE) {
618 *y = (mHeight - font.char_height) / 2;
619 } else if (*y < 0) {
620 *y = mHeight + *y - font.char_height;
621 }
622
623 int cropRect[4] = { 0, 0, font.char_width, -font.char_height };
624
625 for (int i = 0; i < len; i++) {
626 char c = str[i];
627
628 if (c < FONT_BEGIN_CHAR || c > FONT_END_CHAR) {
629 c = '?';
630 }
631
632 // Crop the texture to only the pixels in the current glyph
633 const int charPos = (c - FONT_BEGIN_CHAR); // Position in the list of valid characters
634 const int row = charPos / FONT_NUM_COLS;
635 const int col = charPos % FONT_NUM_COLS;
636 cropRect[0] = col * font.char_width; // Left of column
637 cropRect[1] = row * font.char_height * 2; // Top of row
638 // Move down to bottom of regular (one char_heigh) or bold (two char_heigh) line
639 cropRect[1] += bold ? 2 * font.char_height : font.char_height;
640 glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_CROP_RECT_OES, cropRect);
641
642 glDrawTexiOES(*x, *y, 0, font.char_width, font.char_height);
643
644 *x += font.char_width;
645 }
646
647 glDisable(GL_BLEND); // Return to the animation's default behaviour
648 glBindTexture(GL_TEXTURE_2D, 0);
649}
650
Damien Bargiacchi9071db12016-10-28 17:38:22 -0700651// We render 12 or 24 hour time.
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700652void BootAnimation::drawClock(const Font& font, const int xPos, const int yPos) {
Damien Bargiacchi9071db12016-10-28 17:38:22 -0700653 static constexpr char TIME_FORMAT_12[] = "%l:%M";
654 static constexpr char TIME_FORMAT_24[] = "%H:%M";
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700655 static constexpr int TIME_LENGTH = 6;
656
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800657 time_t rawtime;
658 time(&rawtime);
659 struct tm* timeInfo = localtime(&rawtime);
660
661 char timeBuff[TIME_LENGTH];
Damien Bargiacchi9071db12016-10-28 17:38:22 -0700662 const char* timeFormat = mTimeFormat12Hour ? TIME_FORMAT_12 : TIME_FORMAT_24;
663 size_t length = strftime(timeBuff, TIME_LENGTH, timeFormat, timeInfo);
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800664
665 if (length != TIME_LENGTH - 1) {
Wei Wang159a4102018-10-23 23:15:58 -0700666 SLOGE("Couldn't format time; abandoning boot animation clock");
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800667 mClockEnabled = false;
668 return;
669 }
670
Damien Bargiacchi45a76442016-12-05 18:02:18 -0800671 char* out = timeBuff[0] == ' ' ? &timeBuff[1] : &timeBuff[0];
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700672 int x = xPos;
673 int y = yPos;
Damien Bargiacchi45a76442016-12-05 18:02:18 -0800674 drawText(out, font, false, &x, &y);
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800675}
676
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700677bool BootAnimation::parseAnimationDesc(Animation& animation)
Mathias Agopiana8826d62009-10-01 03:10:14 -0700678{
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700679 String8 desString;
680
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700681 if (!readFile(animation.zip, "desc.txt", desString)) {
Narayan Kamathafd31e02013-12-03 13:16:03 +0000682 return false;
683 }
Mathias Agopiana8826d62009-10-01 03:10:14 -0700684 char const* s = desString.string();
685
Mathias Agopiana8826d62009-10-01 03:10:14 -0700686 // Parse the description file
687 for (;;) {
688 const char* endl = strstr(s, "\n");
Yi Kong911ac232019-03-24 01:49:02 -0700689 if (endl == nullptr) break;
Mathias Agopiana8826d62009-10-01 03:10:14 -0700690 String8 line(s, endl - s);
691 const char* l = line.string();
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800692 int fps = 0;
693 int width = 0;
694 int height = 0;
695 int count = 0;
696 int pause = 0;
Narayan Kamathafd31e02013-12-03 13:16:03 +0000697 char path[ANIM_ENTRY_NAME_MAX];
Jesse Hall083b84c2014-09-22 10:51:09 -0700698 char color[7] = "000000"; // default to black if unspecified
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700699 char clockPos1[TEXT_POS_LEN_MAX + 1] = "";
700 char clockPos2[TEXT_POS_LEN_MAX + 1] = "";
Jesse Hall083b84c2014-09-22 10:51:09 -0700701
Kevin Hesterd3782b22012-04-26 10:38:55 -0700702 char pathType;
Mathias Agopiana8826d62009-10-01 03:10:14 -0700703 if (sscanf(l, "%d %d %d", &width, &height, &fps) == 3) {
Wei Wang159a4102018-10-23 23:15:58 -0700704 // SLOGD("> w=%d, h=%d, fps=%d", width, height, fps);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700705 animation.width = width;
706 animation.height = height;
707 animation.fps = fps;
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700708 } else if (sscanf(l, " %c %d %d %s #%6s %16s %16s",
709 &pathType, &count, &pause, path, color, clockPos1, clockPos2) >= 4) {
Wei Wang159a4102018-10-23 23:15:58 -0700710 //SLOGD("> type=%c, count=%d, pause=%d, path=%s, color=%s, clockPos1=%s, clockPos2=%s",
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700711 // pathType, count, pause, path, color, clockPos1, clockPos2);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700712 Animation::Part part;
Kevin Hesterd3782b22012-04-26 10:38:55 -0700713 part.playUntilComplete = pathType == 'c';
Mathias Agopiana8826d62009-10-01 03:10:14 -0700714 part.count = count;
715 part.pause = pause;
716 part.path = path;
Yi Kong911ac232019-03-24 01:49:02 -0700717 part.audioData = nullptr;
718 part.animation = nullptr;
Jesse Hall083b84c2014-09-22 10:51:09 -0700719 if (!parseColor(color, part.backgroundColor)) {
Wei Wang159a4102018-10-23 23:15:58 -0700720 SLOGE("> invalid color '#%s'", color);
Jesse Hall083b84c2014-09-22 10:51:09 -0700721 part.backgroundColor[0] = 0.0f;
722 part.backgroundColor[1] = 0.0f;
723 part.backgroundColor[2] = 0.0f;
724 }
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700725 parsePosition(clockPos1, clockPos2, &part.clockPosX, &part.clockPosY);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700726 animation.parts.add(part);
727 }
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700728 else if (strcmp(l, "$SYSTEM") == 0) {
Wei Wang159a4102018-10-23 23:15:58 -0700729 // SLOGD("> SYSTEM");
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700730 Animation::Part part;
731 part.playUntilComplete = false;
732 part.count = 1;
733 part.pause = 0;
Yi Kong911ac232019-03-24 01:49:02 -0700734 part.audioData = nullptr;
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700735 part.animation = loadAnimation(String8(SYSTEM_BOOTANIMATION_FILE));
Yi Kong911ac232019-03-24 01:49:02 -0700736 if (part.animation != nullptr)
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700737 animation.parts.add(part);
738 }
Mathias Agopiana8826d62009-10-01 03:10:14 -0700739 s = ++endl;
740 }
741
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700742 return true;
743}
744
745bool BootAnimation::preloadZip(Animation& animation)
746{
Mathias Agopiana8826d62009-10-01 03:10:14 -0700747 // read all the data structures
748 const size_t pcount = animation.parts.size();
Yi Kong911ac232019-03-24 01:49:02 -0700749 void *cookie = nullptr;
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400750 ZipFileRO* zip = animation.zip;
751 if (!zip->startIteration(&cookie)) {
Narayan Kamathafd31e02013-12-03 13:16:03 +0000752 return false;
753 }
754
755 ZipEntryRO entry;
756 char name[ANIM_ENTRY_NAME_MAX];
Yi Kong911ac232019-03-24 01:49:02 -0700757 while ((entry = zip->nextEntry(cookie)) != nullptr) {
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400758 const int foundEntryName = zip->getEntryFileName(entry, name, ANIM_ENTRY_NAME_MAX);
Narayan Kamathafd31e02013-12-03 13:16:03 +0000759 if (foundEntryName > ANIM_ENTRY_NAME_MAX || foundEntryName == -1) {
Wei Wang159a4102018-10-23 23:15:58 -0700760 SLOGE("Error fetching entry file name");
Narayan Kamathafd31e02013-12-03 13:16:03 +0000761 continue;
762 }
763
764 const String8 entryName(name);
765 const String8 path(entryName.getPathDir());
766 const String8 leaf(entryName.getPathLeaf());
767 if (leaf.size() > 0) {
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700768 if (entryName == CLOCK_FONT_ZIP_NAME) {
769 FileMap* map = zip->createEntryFileMap(entry);
770 if (map) {
771 animation.clockFont.map = map;
772 }
773 continue;
774 }
775
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400776 for (size_t j = 0; j < pcount; j++) {
Narayan Kamathafd31e02013-12-03 13:16:03 +0000777 if (path == animation.parts[j].path) {
Narayan Kamath4600dd02015-06-16 12:02:57 +0100778 uint16_t method;
Narayan Kamathafd31e02013-12-03 13:16:03 +0000779 // supports only stored png files
Yi Kong911ac232019-03-24 01:49:02 -0700780 if (zip->getEntryInfo(entry, &method, nullptr, nullptr, nullptr, nullptr, nullptr)) {
Narayan Kamathafd31e02013-12-03 13:16:03 +0000781 if (method == ZipFileRO::kCompressStored) {
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400782 FileMap* map = zip->createEntryFileMap(entry);
Narayan Kamathafd31e02013-12-03 13:16:03 +0000783 if (map) {
Narayan Kamathafd31e02013-12-03 13:16:03 +0000784 Animation::Part& part(animation.parts.editItemAt(j));
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700785 if (leaf == "audio.wav") {
786 // a part may have at most one audio file
Geoffrey Pitschd6d9a1d2016-06-08 00:38:58 -0700787 part.audioData = (uint8_t *)map->getDataPtr();
788 part.audioLength = map->getDataLength();
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400789 } else if (leaf == "trim.txt") {
790 part.trimData.setTo((char const*)map->getDataPtr(),
791 map->getDataLength());
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700792 } else {
793 Animation::Frame frame;
794 frame.name = leaf;
795 frame.map = map;
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400796 frame.trimWidth = animation.width;
797 frame.trimHeight = animation.height;
798 frame.trimX = 0;
799 frame.trimY = 0;
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700800 part.frames.add(frame);
801 }
Mathias Agopiana8826d62009-10-01 03:10:14 -0700802 }
Geoffrey Pitschd6d9a1d2016-06-08 00:38:58 -0700803 } else {
Wei Wang159a4102018-10-23 23:15:58 -0700804 SLOGE("bootanimation.zip is compressed; must be only stored");
Mathias Agopiana8826d62009-10-01 03:10:14 -0700805 }
806 }
807 }
808 }
809 }
810 }
811
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400812 // If there is trimData present, override the positioning defaults.
813 for (Animation::Part& part : animation.parts) {
814 const char* trimDataStr = part.trimData.string();
815 for (size_t frameIdx = 0; frameIdx < part.frames.size(); frameIdx++) {
816 const char* endl = strstr(trimDataStr, "\n");
817 // No more trimData for this part.
Yi Kong911ac232019-03-24 01:49:02 -0700818 if (endl == nullptr) {
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400819 break;
820 }
821 String8 line(trimDataStr, endl - trimDataStr);
822 const char* lineStr = line.string();
823 trimDataStr = ++endl;
824 int width = 0, height = 0, x = 0, y = 0;
825 if (sscanf(lineStr, "%dx%d+%d+%d", &width, &height, &x, &y) == 4) {
826 Animation::Frame& frame(part.frames.editItemAt(frameIdx));
827 frame.trimWidth = width;
828 frame.trimHeight = height;
829 frame.trimX = x;
830 frame.trimY = y;
831 } else {
Wei Wang159a4102018-10-23 23:15:58 -0700832 SLOGE("Error parsing trim.txt, line: %s", lineStr);
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400833 break;
834 }
835 }
836 }
837
838 zip->endIteration(cookie);
Narayan Kamathafd31e02013-12-03 13:16:03 +0000839
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700840 return true;
841}
842
843bool BootAnimation::movie()
844{
Huihong Luo50a2f362018-08-11 09:27:57 -0700845 if (mAnimation == nullptr) {
846 mAnimation = loadAnimation(mZipFileName);
847 }
848
849 if (mAnimation == nullptr)
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700850 return false;
851
Huihong Luo50a2f362018-08-11 09:27:57 -0700852 // mCallbacks->init() may get called recursively,
853 // this loop is needed to get the same results
854 for (const Animation::Part& part : mAnimation->parts) {
855 if (part.animation != nullptr) {
856 mCallbacks->init(part.animation->parts);
857 }
858 }
859 mCallbacks->init(mAnimation->parts);
860
Damien Bargiacchi97480862016-03-29 14:55:55 -0700861 bool anyPartHasClock = false;
Huihong Luo50a2f362018-08-11 09:27:57 -0700862 for (size_t i=0; i < mAnimation->parts.size(); i++) {
863 if(validClock(mAnimation->parts[i])) {
Damien Bargiacchi97480862016-03-29 14:55:55 -0700864 anyPartHasClock = true;
865 break;
866 }
867 }
868 if (!anyPartHasClock) {
869 mClockEnabled = false;
870 }
871
Sai Kiran Korwar27167492015-07-07 20:00:06 +0530872 // Check if npot textures are supported
873 mUseNpotTextures = false;
874 String8 gl_extensions;
875 const char* exts = reinterpret_cast<const char*>(glGetString(GL_EXTENSIONS));
876 if (!exts) {
877 glGetError();
878 } else {
879 gl_extensions.setTo(exts);
880 if ((gl_extensions.find("GL_ARB_texture_non_power_of_two") != -1) ||
881 (gl_extensions.find("GL_OES_texture_npot") != -1)) {
882 mUseNpotTextures = true;
883 }
884 }
885
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800886 // Blend required to draw time on top of animation frames.
887 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700888 glShadeModel(GL_FLAT);
889 glDisable(GL_DITHER);
890 glDisable(GL_SCISSOR_TEST);
891 glDisable(GL_BLEND);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700892
893 glBindTexture(GL_TEXTURE_2D, 0);
894 glEnable(GL_TEXTURE_2D);
895 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
896 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
897 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
898 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
899 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
900
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700901 bool clockFontInitialized = false;
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800902 if (mClockEnabled) {
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700903 clockFontInitialized =
Huihong Luo50a2f362018-08-11 09:27:57 -0700904 (initFont(&mAnimation->clockFont, CLOCK_FONT_ASSET) == NO_ERROR);
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700905 mClockEnabled = clockFontInitialized;
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800906 }
907
Damien Bargiacchi97480862016-03-29 14:55:55 -0700908 if (mClockEnabled && !updateIsTimeAccurate()) {
909 mTimeCheckThread = new TimeCheckThread(this);
910 mTimeCheckThread->run("BootAnimation::TimeCheckThread", PRIORITY_NORMAL);
911 }
912
Huihong Luo50a2f362018-08-11 09:27:57 -0700913 playAnimation(*mAnimation);
Damien Bargiacchi97480862016-03-29 14:55:55 -0700914
Geoffrey Pitscha9173532016-07-19 14:56:39 -0400915 if (mTimeCheckThread != nullptr) {
Damien Bargiacchi97480862016-03-29 14:55:55 -0700916 mTimeCheckThread->requestExit();
Geoffrey Pitscha9173532016-07-19 14:56:39 -0400917 mTimeCheckThread = nullptr;
918 }
919
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700920 if (clockFontInitialized) {
Huihong Luo50a2f362018-08-11 09:27:57 -0700921 glDeleteTextures(1, &mAnimation->clockFont.texture.name);
Andriy Naborskyy815e51d2016-03-24 16:43:34 -0700922 }
923
Huihong Luo50a2f362018-08-11 09:27:57 -0700924 releaseAnimation(mAnimation);
925 mAnimation = nullptr;
Damien Bargiacchi1a8a2132018-07-24 15:20:26 -0700926
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700927 return false;
928}
929
930bool BootAnimation::playAnimation(const Animation& animation)
931{
932 const size_t pcount = animation.parts.size();
Mathias Agopiana8826d62009-10-01 03:10:14 -0700933 nsecs_t frameDuration = s2ns(1) / animation.fps;
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400934 const int animationX = (mWidth - animation.width) / 2;
935 const int animationY = (mHeight - animation.height) / 2;
Mathias Agopian9f3020d2009-11-06 16:30:18 -0800936
Wei Wang159a4102018-10-23 23:15:58 -0700937 SLOGD("%sAnimationShownTiming start time: %" PRId64 "ms", mShuttingDown ? "Shutdown" : "Boot",
Keun-young Parkd1794cd2017-04-04 12:21:19 -0700938 elapsedRealtime());
Narayan Kamathafd31e02013-12-03 13:16:03 +0000939 for (size_t i=0 ; i<pcount ; i++) {
Mathias Agopiana8826d62009-10-01 03:10:14 -0700940 const Animation::Part& part(animation.parts[i]);
941 const size_t fcount = part.frames.size();
942 glBindTexture(GL_TEXTURE_2D, 0);
943
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700944 // Handle animation package
Yi Kong911ac232019-03-24 01:49:02 -0700945 if (part.animation != nullptr) {
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700946 playAnimation(*part.animation);
947 if (exitPending())
948 break;
949 continue; //to next part
950 }
951
Mathias Agopiana8826d62009-10-01 03:10:14 -0700952 for (int r=0 ; !part.count || r<part.count ; r++) {
Kevin Hesterd3782b22012-04-26 10:38:55 -0700953 // Exit any non playuntil complete parts immediately
954 if(exitPending() && !part.playUntilComplete)
955 break;
956
Ed Coyne7464ac92017-06-08 12:26:48 -0700957 mCallbacks->playPart(i, part, r);
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700958
Jesse Hall083b84c2014-09-22 10:51:09 -0700959 glClearColor(
960 part.backgroundColor[0],
961 part.backgroundColor[1],
962 part.backgroundColor[2],
963 1.0f);
964
Narayan Kamathafd31e02013-12-03 13:16:03 +0000965 for (size_t j=0 ; j<fcount && (!exitPending() || part.playUntilComplete) ; j++) {
Mathias Agopiana8826d62009-10-01 03:10:14 -0700966 const Animation::Frame& frame(part.frames[j]);
Mathias Agopiandb7dd2a2012-05-12 15:08:21 -0700967 nsecs_t lastFrame = systemTime();
Mathias Agopiana8826d62009-10-01 03:10:14 -0700968
969 if (r > 0) {
970 glBindTexture(GL_TEXTURE_2D, frame.tid);
971 } else {
972 if (part.count != 1) {
973 glGenTextures(1, &frame.tid);
974 glBindTexture(GL_TEXTURE_2D, frame.tid);
975 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
976 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
977 }
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700978 int w, h;
979 initTexture(frame.map, &w, &h);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700980 }
981
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400982 const int xc = animationX + frame.trimX;
983 const int yc = animationY + frame.trimY;
984 Region clearReg(Rect(mWidth, mHeight));
985 clearReg.subtractSelf(Rect(xc, yc, xc+frame.trimWidth, yc+frame.trimHeight));
Mathias Agopian9f3020d2009-11-06 16:30:18 -0800986 if (!clearReg.isEmpty()) {
987 Region::const_iterator head(clearReg.begin());
988 Region::const_iterator tail(clearReg.end());
989 glEnable(GL_SCISSOR_TEST);
990 while (head != tail) {
Andreas Gampecfedceb2014-09-30 21:48:18 -0700991 const Rect& r2(*head++);
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400992 glScissor(r2.left, mHeight - r2.bottom, r2.width(), r2.height());
Mathias Agopian9f3020d2009-11-06 16:30:18 -0800993 glClear(GL_COLOR_BUFFER_BIT);
994 }
995 glDisable(GL_SCISSOR_TEST);
996 }
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400997 // specify the y center as ceiling((mHeight - frame.trimHeight) / 2)
998 // which is equivalent to mHeight - (yc + frame.trimHeight)
999 glDrawTexiOES(xc, mHeight - (yc + frame.trimHeight),
1000 0, frame.trimWidth, frame.trimHeight);
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -07001001 if (mClockEnabled && mTimeIsAccurate && validClock(part)) {
1002 drawClock(animation.clockFont, part.clockPosX, part.clockPosY);
Damien Bargiacchia704b7d2016-02-16 16:55:49 -08001003 }
Adrian Roos9ee5dff2018-08-22 20:19:49 +02001004 handleViewport(frameDuration);
Damien Bargiacchia704b7d2016-02-16 16:55:49 -08001005
Mathias Agopiana8826d62009-10-01 03:10:14 -07001006 eglSwapBuffers(mDisplay, mSurface);
1007
1008 nsecs_t now = systemTime();
1009 nsecs_t delay = frameDuration - (now - lastFrame);
Wei Wang159a4102018-10-23 23:15:58 -07001010 //SLOGD("%lld, %lld", ns2ms(now - lastFrame), ns2ms(delay));
Mathias Agopiana8826d62009-10-01 03:10:14 -07001011 lastFrame = now;
Mathias Agopiandb7dd2a2012-05-12 15:08:21 -07001012
1013 if (delay > 0) {
1014 struct timespec spec;
1015 spec.tv_sec = (now + delay) / 1000000000;
1016 spec.tv_nsec = (now + delay) % 1000000000;
1017 int err;
1018 do {
Yi Kong911ac232019-03-24 01:49:02 -07001019 err = clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &spec, nullptr);
Mathias Agopiandb7dd2a2012-05-12 15:08:21 -07001020 } while (err<0 && errno == EINTR);
1021 }
Kevin Hesterd3782b22012-04-26 10:38:55 -07001022
1023 checkExit();
Mathias Agopiana8826d62009-10-01 03:10:14 -07001024 }
Kevin Hesterd3782b22012-04-26 10:38:55 -07001025
Mathias Agopiana8826d62009-10-01 03:10:14 -07001026 usleep(part.pause * ns2us(frameDuration));
Kevin Hesterd3782b22012-04-26 10:38:55 -07001027
1028 // For infinite parts, we've now played them at least once, so perhaps exit
Adrian Roos9ee5dff2018-08-22 20:19:49 +02001029 if(exitPending() && !part.count && mCurrentInset >= mTargetInset)
Kevin Hesterd3782b22012-04-26 10:38:55 -07001030 break;
Mathias Agopiana8826d62009-10-01 03:10:14 -07001031 }
1032
Geoffrey Pitsch2fb30fb2016-07-06 16:16:20 -04001033 }
1034
1035 // Free textures created for looping parts now that the animation is done.
1036 for (const Animation::Part& part : animation.parts) {
Mathias Agopiana8826d62009-10-01 03:10:14 -07001037 if (part.count != 1) {
Geoffrey Pitsch2fb30fb2016-07-06 16:16:20 -04001038 const size_t fcount = part.frames.size();
1039 for (size_t j = 0; j < fcount; j++) {
Mathias Agopiana8826d62009-10-01 03:10:14 -07001040 const Animation::Frame& frame(part.frames[j]);
1041 glDeleteTextures(1, &frame.tid);
1042 }
1043 }
1044 }
Geoffrey Pitschd6d9a1d2016-06-08 00:38:58 -07001045
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001046 return true;
Mathias Agopiana8826d62009-10-01 03:10:14 -07001047}
1048
Adrian Roos9ee5dff2018-08-22 20:19:49 +02001049void BootAnimation::handleViewport(nsecs_t timestep) {
1050 if (mShuttingDown || !mFlingerSurfaceControl || mTargetInset == 0) {
1051 return;
1052 }
1053 if (mTargetInset < 0) {
1054 // Poll the amount for the top display inset. This will return -1 until persistent properties
1055 // have been loaded.
1056 mTargetInset = android::base::GetIntProperty("persist.sys.displayinset.top",
1057 -1 /* default */, -1 /* min */, mHeight / 2 /* max */);
1058 }
1059 if (mTargetInset <= 0) {
1060 return;
1061 }
1062
1063 if (mCurrentInset < mTargetInset) {
1064 // After the device boots, the inset will effectively be cropped away. We animate this here.
1065 float fraction = static_cast<float>(mCurrentInset) / mTargetInset;
1066 int interpolatedInset = (cosf((fraction + 1) * M_PI) / 2.0f + 0.5f) * mTargetInset;
1067
1068 SurfaceComposerClient::Transaction()
1069 .setCrop(mFlingerSurfaceControl, Rect(0, interpolatedInset, mWidth, mHeight))
1070 .apply();
1071 } else {
1072 // At the end of the animation, we switch to the viewport that DisplayManager will apply
1073 // later. This changes the coordinate system, and means we must move the surface up by
1074 // the inset amount.
Adrian Roos9ee5dff2018-08-22 20:19:49 +02001075 Rect layerStackRect(0, 0, mWidth, mHeight - mTargetInset);
1076 Rect displayRect(0, mTargetInset, mWidth, mHeight);
1077
1078 SurfaceComposerClient::Transaction t;
1079 t.setPosition(mFlingerSurfaceControl, 0, -mTargetInset)
1080 .setCrop(mFlingerSurfaceControl, Rect(0, mTargetInset, mWidth, mHeight));
Dominik Laskowski3316a0a2019-01-25 02:56:41 -08001081 t.setDisplayProjection(mDisplayToken, 0 /* orientation */, layerStackRect, displayRect);
Adrian Roos9ee5dff2018-08-22 20:19:49 +02001082 t.apply();
1083
1084 mTargetInset = mCurrentInset = 0;
1085 }
1086
1087 int delta = timestep * mTargetInset / ms2ns(200);
1088 mCurrentInset += delta;
1089}
1090
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001091void BootAnimation::releaseAnimation(Animation* animation) const
1092{
1093 for (Vector<Animation::Part>::iterator it = animation->parts.begin(),
1094 e = animation->parts.end(); it != e; ++it) {
1095 if (it->animation)
1096 releaseAnimation(it->animation);
1097 }
1098 if (animation->zip)
1099 delete animation->zip;
1100 delete animation;
1101}
1102
1103BootAnimation::Animation* BootAnimation::loadAnimation(const String8& fn)
1104{
1105 if (mLoadedFiles.indexOf(fn) >= 0) {
Wei Wang159a4102018-10-23 23:15:58 -07001106 SLOGE("File \"%s\" is already loaded. Cyclic ref is not allowed",
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001107 fn.string());
Yi Kong911ac232019-03-24 01:49:02 -07001108 return nullptr;
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001109 }
1110 ZipFileRO *zip = ZipFileRO::open(fn);
Yi Kong911ac232019-03-24 01:49:02 -07001111 if (zip == nullptr) {
Wei Wang159a4102018-10-23 23:15:58 -07001112 SLOGE("Failed to open animation zip \"%s\": %s",
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001113 fn.string(), strerror(errno));
Yi Kong911ac232019-03-24 01:49:02 -07001114 return nullptr;
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001115 }
1116
1117 Animation *animation = new Animation;
1118 animation->fileName = fn;
1119 animation->zip = zip;
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -07001120 animation->clockFont.map = nullptr;
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001121 mLoadedFiles.add(animation->fileName);
1122
1123 parseAnimationDesc(*animation);
Geoffrey Pitscha91a2d72016-07-12 14:46:19 -04001124 if (!preloadZip(*animation)) {
Yi Kong911ac232019-03-24 01:49:02 -07001125 return nullptr;
Geoffrey Pitscha91a2d72016-07-12 14:46:19 -04001126 }
1127
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001128
1129 mLoadedFiles.remove(fn);
1130 return animation;
1131}
Damien Bargiacchi97480862016-03-29 14:55:55 -07001132
1133bool BootAnimation::updateIsTimeAccurate() {
1134 static constexpr long long MAX_TIME_IN_PAST = 60000LL * 60LL * 24LL * 30LL; // 30 days
1135 static constexpr long long MAX_TIME_IN_FUTURE = 60000LL * 90LL; // 90 minutes
1136
1137 if (mTimeIsAccurate) {
1138 return true;
1139 }
Keun-young Parkb5938422017-03-23 13:46:24 -07001140 if (mShuttingDown) return true;
Damien Bargiacchi97480862016-03-29 14:55:55 -07001141 struct stat statResult;
Damien Bargiacchi9071db12016-10-28 17:38:22 -07001142
1143 if(stat(TIME_FORMAT_12_HOUR_FLAG_FILE_PATH, &statResult) == 0) {
1144 mTimeFormat12Hour = true;
1145 }
1146
Damien Bargiacchi97480862016-03-29 14:55:55 -07001147 if(stat(ACCURATE_TIME_FLAG_FILE_PATH, &statResult) == 0) {
1148 mTimeIsAccurate = true;
1149 return true;
1150 }
1151
1152 FILE* file = fopen(LAST_TIME_CHANGED_FILE_PATH, "r");
Yi Kong911ac232019-03-24 01:49:02 -07001153 if (file != nullptr) {
Damien Bargiacchi97480862016-03-29 14:55:55 -07001154 long long lastChangedTime = 0;
1155 fscanf(file, "%lld", &lastChangedTime);
1156 fclose(file);
1157 if (lastChangedTime > 0) {
1158 struct timespec now;
1159 clock_gettime(CLOCK_REALTIME, &now);
1160 // Match the Java timestamp format
1161 long long rtcNow = (now.tv_sec * 1000LL) + (now.tv_nsec / 1000000LL);
Damien Bargiacchi96762812016-07-12 15:53:40 -07001162 if (ACCURATE_TIME_EPOCH < rtcNow
1163 && lastChangedTime > (rtcNow - MAX_TIME_IN_PAST)
1164 && lastChangedTime < (rtcNow + MAX_TIME_IN_FUTURE)) {
Damien Bargiacchi97480862016-03-29 14:55:55 -07001165 mTimeIsAccurate = true;
1166 }
1167 }
1168 }
1169
1170 return mTimeIsAccurate;
1171}
1172
1173BootAnimation::TimeCheckThread::TimeCheckThread(BootAnimation* bootAnimation) : Thread(false),
1174 mInotifyFd(-1), mSystemWd(-1), mTimeWd(-1), mBootAnimation(bootAnimation) {}
1175
1176BootAnimation::TimeCheckThread::~TimeCheckThread() {
1177 // mInotifyFd may be -1 but that's ok since we're not at risk of attempting to close a valid FD.
1178 close(mInotifyFd);
1179}
1180
1181bool BootAnimation::TimeCheckThread::threadLoop() {
1182 bool shouldLoop = doThreadLoop() && !mBootAnimation->mTimeIsAccurate
1183 && mBootAnimation->mClockEnabled;
1184 if (!shouldLoop) {
1185 close(mInotifyFd);
1186 mInotifyFd = -1;
1187 }
1188 return shouldLoop;
1189}
1190
1191bool BootAnimation::TimeCheckThread::doThreadLoop() {
1192 static constexpr int BUFF_LEN (10 * (sizeof(struct inotify_event) + NAME_MAX + 1));
1193
1194 // Poll instead of doing a blocking read so the Thread can exit if requested.
1195 struct pollfd pfd = { mInotifyFd, POLLIN, 0 };
1196 ssize_t pollResult = poll(&pfd, 1, 1000);
1197
1198 if (pollResult == 0) {
1199 return true;
1200 } else if (pollResult < 0) {
Wei Wang159a4102018-10-23 23:15:58 -07001201 SLOGE("Could not poll inotify events");
Damien Bargiacchi97480862016-03-29 14:55:55 -07001202 return false;
1203 }
1204
1205 char buff[BUFF_LEN] __attribute__ ((aligned(__alignof__(struct inotify_event))));;
1206 ssize_t length = read(mInotifyFd, buff, BUFF_LEN);
1207 if (length == 0) {
1208 return true;
1209 } else if (length < 0) {
Wei Wang159a4102018-10-23 23:15:58 -07001210 SLOGE("Could not read inotify events");
Damien Bargiacchi97480862016-03-29 14:55:55 -07001211 return false;
1212 }
1213
1214 const struct inotify_event *event;
1215 for (char* ptr = buff; ptr < buff + length; ptr += sizeof(struct inotify_event) + event->len) {
1216 event = (const struct inotify_event *) ptr;
1217 if (event->wd == mSystemWd && strcmp(SYSTEM_TIME_DIR_NAME, event->name) == 0) {
1218 addTimeDirWatch();
1219 } else if (event->wd == mTimeWd && (strcmp(LAST_TIME_CHANGED_FILE_NAME, event->name) == 0
1220 || strcmp(ACCURATE_TIME_FLAG_FILE_NAME, event->name) == 0)) {
1221 return !mBootAnimation->updateIsTimeAccurate();
1222 }
1223 }
1224
1225 return true;
1226}
1227
1228void BootAnimation::TimeCheckThread::addTimeDirWatch() {
1229 mTimeWd = inotify_add_watch(mInotifyFd, SYSTEM_TIME_DIR_PATH,
1230 IN_CLOSE_WRITE | IN_MOVED_TO | IN_ATTRIB);
1231 if (mTimeWd > 0) {
1232 // No need to watch for the time directory to be created if it already exists
1233 inotify_rm_watch(mInotifyFd, mSystemWd);
1234 mSystemWd = -1;
1235 }
1236}
1237
1238status_t BootAnimation::TimeCheckThread::readyToRun() {
1239 mInotifyFd = inotify_init();
1240 if (mInotifyFd < 0) {
Wei Wang159a4102018-10-23 23:15:58 -07001241 SLOGE("Could not initialize inotify fd");
Damien Bargiacchi97480862016-03-29 14:55:55 -07001242 return NO_INIT;
1243 }
1244
1245 mSystemWd = inotify_add_watch(mInotifyFd, SYSTEM_DATA_DIR_PATH, IN_CREATE | IN_ATTRIB);
1246 if (mSystemWd < 0) {
1247 close(mInotifyFd);
1248 mInotifyFd = -1;
Wei Wang159a4102018-10-23 23:15:58 -07001249 SLOGE("Could not add watch for %s", SYSTEM_DATA_DIR_PATH);
Damien Bargiacchi97480862016-03-29 14:55:55 -07001250 return NO_INIT;
1251 }
1252
1253 addTimeDirWatch();
1254
1255 if (mBootAnimation->updateIsTimeAccurate()) {
1256 close(mInotifyFd);
1257 mInotifyFd = -1;
1258 return ALREADY_EXISTS;
1259 }
1260
1261 return NO_ERROR;
1262}
1263
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001264// ---------------------------------------------------------------------------
1265
1266}
1267; // namespace android