blob: 5dcb392b002dd2c4b3c81bfedefa485d6010ee25 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -070017#define LOG_NDEBUG 0
Mathias Agopianbc726112009-09-23 15:44:05 -070018#define LOG_TAG "BootAnimation"
19
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080020#include <stdint.h>
Wei Wanga90c54c2017-02-02 11:35:21 -080021#include <inttypes.h>
Damien Bargiacchi97480862016-03-29 14:55:55 -070022#include <sys/inotify.h>
23#include <sys/poll.h>
24#include <sys/stat.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080025#include <sys/types.h>
26#include <math.h>
27#include <fcntl.h>
28#include <utils/misc.h>
Mathias Agopianb4d5a722009-09-23 17:05:19 -070029#include <signal.h>
Elliott Hughesbb94f312014-10-21 10:41:33 -070030#include <time.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080031
Steven Morelandfb7952f2018-02-23 14:58:50 -080032#include <cutils/atomic.h>
Jason parksbd9a08d2011-01-31 15:04:34 -060033#include <cutils/properties.h>
34
Mathias Agopianb13b9bd2012-02-17 18:27:36 -080035#include <androidfw/AssetManager.h>
Mathias Agopianac31a3b2009-05-21 19:59:24 -070036#include <binder/IPCThreadState.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080037#include <utils/Errors.h>
38#include <utils/Log.h>
Wei Wanga90c54c2017-02-02 11:35:21 -080039#include <utils/SystemClock.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080040
Keun-young Parkb5938422017-03-23 13:46:24 -070041#include <android-base/properties.h>
42
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080043#include <ui/PixelFormat.h>
44#include <ui/Rect.h>
45#include <ui/Region.h>
46#include <ui/DisplayInfo.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080047
Jeff Brown0b722fe2012-08-24 22:40:14 -070048#include <gui/ISurfaceComposer.h>
Mathias Agopian8335f1c2012-02-25 18:48:35 -080049#include <gui/Surface.h>
50#include <gui/SurfaceComposerClient.h>
Mathias Agopian000479f2010-02-09 17:46:37 -080051
Andreas Gampecfedceb2014-09-30 21:48:18 -070052// TODO: Fix Skia.
53#pragma GCC diagnostic push
54#pragma GCC diagnostic ignored "-Wunused-parameter"
Derek Sollenbergereece0dd2014-02-27 14:31:29 -050055#include <SkBitmap.h>
Matt Sarett8898c162016-03-24 16:11:01 -040056#include <SkImage.h>
Derek Sollenbergereece0dd2014-02-27 14:31:29 -050057#include <SkStream.h>
Andreas Gampecfedceb2014-09-30 21:48:18 -070058#pragma GCC diagnostic pop
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080059
60#include <GLES/gl.h>
61#include <GLES/glext.h>
62#include <EGL/eglext.h>
63
64#include "BootAnimation.h"
65
66namespace android {
67
Damien Bargiacchi97480862016-03-29 14:55:55 -070068static const char OEM_BOOTANIMATION_FILE[] = "/oem/media/bootanimation.zip";
Jaekyun Seokc521bb32018-01-30 11:52:47 +090069static const char PRODUCT_BOOTANIMATION_FILE[] = "/product/media/bootanimation.zip";
Damien Bargiacchi97480862016-03-29 14:55:55 -070070static const char SYSTEM_BOOTANIMATION_FILE[] = "/system/media/bootanimation.zip";
Jaekyun Seokc521bb32018-01-30 11:52:47 +090071static const char PRODUCT_ENCRYPTED_BOOTANIMATION_FILE[] = "/product/media/bootanimation-encrypted.zip";
Damien Bargiacchi97480862016-03-29 14:55:55 -070072static const char SYSTEM_ENCRYPTED_BOOTANIMATION_FILE[] = "/system/media/bootanimation-encrypted.zip";
Keun-young Parkb5938422017-03-23 13:46:24 -070073static const char OEM_SHUTDOWNANIMATION_FILE[] = "/oem/media/shutdownanimation.zip";
Jaekyun Seokc521bb32018-01-30 11:52:47 +090074static const char PRODUCT_SHUTDOWNANIMATION_FILE[] = "/product/media/shutdownanimation.zip";
Keun-young Parkb5938422017-03-23 13:46:24 -070075static const char SYSTEM_SHUTDOWNANIMATION_FILE[] = "/system/media/shutdownanimation.zip";
76
Damien Bargiacchi97480862016-03-29 14:55:55 -070077static const char SYSTEM_DATA_DIR_PATH[] = "/data/system";
78static const char SYSTEM_TIME_DIR_NAME[] = "time";
79static const char SYSTEM_TIME_DIR_PATH[] = "/data/system/time";
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -070080static const char CLOCK_FONT_ASSET[] = "images/clock_font.png";
81static const char CLOCK_FONT_ZIP_NAME[] = "clock_font.png";
Damien Bargiacchi97480862016-03-29 14:55:55 -070082static const char LAST_TIME_CHANGED_FILE_NAME[] = "last_time_change";
83static const char LAST_TIME_CHANGED_FILE_PATH[] = "/data/system/time/last_time_change";
84static const char ACCURATE_TIME_FLAG_FILE_NAME[] = "time_is_accurate";
85static const char ACCURATE_TIME_FLAG_FILE_PATH[] = "/data/system/time/time_is_accurate";
Damien Bargiacchi9071db12016-10-28 17:38:22 -070086static const char TIME_FORMAT_12_HOUR_FLAG_FILE_PATH[] = "/data/system/time/time_format_12_hour";
Damien Bargiacchi96762812016-07-12 15:53:40 -070087// Java timestamp format. Don't show the clock if the date is before 2000-01-01 00:00:00.
88static const long long ACCURATE_TIME_EPOCH = 946684800000;
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -070089static constexpr char FONT_BEGIN_CHAR = ' ';
90static constexpr char FONT_END_CHAR = '~' + 1;
91static constexpr size_t FONT_NUM_CHARS = FONT_END_CHAR - FONT_BEGIN_CHAR + 1;
92static constexpr size_t FONT_NUM_COLS = 16;
93static constexpr size_t FONT_NUM_ROWS = FONT_NUM_CHARS / FONT_NUM_COLS;
94static const int TEXT_CENTER_VALUE = INT_MAX;
95static const int TEXT_MISSING_VALUE = INT_MIN;
Damien Bargiacchi97480862016-03-29 14:55:55 -070096static const char EXIT_PROP_NAME[] = "service.bootanim.exit";
Narayan Kamathafd31e02013-12-03 13:16:03 +000097static const int ANIM_ENTRY_NAME_MAX = 256;
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -070098static constexpr size_t TEXT_POS_LEN_MAX = 16;
Narayan Kamathafd31e02013-12-03 13:16:03 +000099
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800100// ---------------------------------------------------------------------------
101
Ed Coyne7464ac92017-06-08 12:26:48 -0700102BootAnimation::BootAnimation(sp<Callbacks> callbacks)
Ed Coyne2c9e94a2017-05-31 10:08:28 -0700103 : Thread(false), mClockEnabled(true), mTimeIsAccurate(false),
Ed Coyne7464ac92017-06-08 12:26:48 -0700104 mTimeFormat12Hour(false), mTimeCheckThread(NULL), mCallbacks(callbacks) {
Mathias Agopian627e7b52009-05-21 19:21:59 -0700105 mSession = new SurfaceComposerClient();
Geoffrey Pitsch290c4352016-08-09 14:35:10 -0400106
Keun-young Parkb5938422017-03-23 13:46:24 -0700107 std::string powerCtl = android::base::GetProperty("sys.powerctl", "");
108 if (powerCtl.empty()) {
109 mShuttingDown = false;
110 } else {
111 mShuttingDown = true;
112 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800113}
114
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800115void BootAnimation::onFirstRef() {
Mathias Agopianbc726112009-09-23 15:44:05 -0700116 status_t err = mSession->linkToComposerDeath(this);
Wei Wang159a4102018-10-23 23:15:58 -0700117 SLOGE_IF(err, "linkToComposerDeath failed (%s) ", strerror(-err));
Mathias Agopian8434c532009-09-23 18:52:49 -0700118 if (err == NO_ERROR) {
Mathias Agopianbc726112009-09-23 15:44:05 -0700119 run("BootAnimation", PRIORITY_DISPLAY);
120 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800121}
122
Mathias Agopianbc726112009-09-23 15:44:05 -0700123sp<SurfaceComposerClient> BootAnimation::session() const {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800124 return mSession;
125}
126
Mathias Agopianbc726112009-09-23 15:44:05 -0700127
Narayan Kamathafd31e02013-12-03 13:16:03 +0000128void BootAnimation::binderDied(const wp<IBinder>&)
Mathias Agopianbc726112009-09-23 15:44:05 -0700129{
130 // woah, surfaceflinger died!
Wei Wang159a4102018-10-23 23:15:58 -0700131 SLOGD("SurfaceFlinger died, exiting...");
Mathias Agopianbc726112009-09-23 15:44:05 -0700132
133 // calling requestExit() is not enough here because the Surface code
134 // might be blocked on a condition variable that will never be updated.
135 kill( getpid(), SIGKILL );
136 requestExit();
137}
138
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800139status_t BootAnimation::initTexture(Texture* texture, AssetManager& assets,
140 const char* name) {
141 Asset* asset = assets.open(name, Asset::ACCESS_BUFFER);
Andreas Gampecfedceb2014-09-30 21:48:18 -0700142 if (asset == NULL)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800143 return NO_INIT;
144 SkBitmap bitmap;
Matt Sarett8898c162016-03-24 16:11:01 -0400145 sk_sp<SkData> data = SkData::MakeWithoutCopy(asset->getBuffer(false),
146 asset->getLength());
147 sk_sp<SkImage> image = SkImage::MakeFromEncoded(data);
148 image->asLegacyBitmap(&bitmap, SkImage::kRO_LegacyBitmapMode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800149 asset->close();
150 delete asset;
151
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800152 const int w = bitmap.width();
153 const int h = bitmap.height();
154 const void* p = bitmap.getPixels();
155
156 GLint crop[4] = { 0, h, w, -h };
157 texture->w = w;
158 texture->h = h;
159
160 glGenTextures(1, &texture->name);
161 glBindTexture(GL_TEXTURE_2D, texture->name);
162
Mike Reed42a1d082014-07-07 18:06:18 -0400163 switch (bitmap.colorType()) {
164 case kAlpha_8_SkColorType:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800165 glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, w, h, 0, GL_ALPHA,
166 GL_UNSIGNED_BYTE, p);
167 break;
Mike Reed42a1d082014-07-07 18:06:18 -0400168 case kARGB_4444_SkColorType:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800169 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA,
170 GL_UNSIGNED_SHORT_4_4_4_4, p);
171 break;
Mike Reed42a1d082014-07-07 18:06:18 -0400172 case kN32_SkColorType:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800173 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA,
174 GL_UNSIGNED_BYTE, p);
175 break;
Mike Reed42a1d082014-07-07 18:06:18 -0400176 case kRGB_565_SkColorType:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800177 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, w, h, 0, GL_RGB,
178 GL_UNSIGNED_SHORT_5_6_5, p);
179 break;
180 default:
181 break;
182 }
183
184 glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_CROP_RECT_OES, crop);
185 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
186 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
187 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
188 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700189
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800190 return NO_ERROR;
191}
192
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700193status_t BootAnimation::initTexture(FileMap* map, int* width, int* height)
Mathias Agopiana8826d62009-10-01 03:10:14 -0700194{
Mathias Agopiana8826d62009-10-01 03:10:14 -0700195 SkBitmap bitmap;
Damien Bargiacchif67b3172016-09-07 20:17:14 -0700196 sk_sp<SkData> data = SkData::MakeWithoutCopy(map->getDataPtr(),
197 map->getDataLength());
Matt Sarett8898c162016-03-24 16:11:01 -0400198 sk_sp<SkImage> image = SkImage::MakeFromEncoded(data);
199 image->asLegacyBitmap(&bitmap, SkImage::kRO_LegacyBitmapMode);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700200
Mykola Kondratenko0c1eeb32014-04-15 09:35:44 +0200201 // FileMap memory is never released until application exit.
202 // Release it now as the texture is already loaded and the memory used for
203 // the packed resource can be released.
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700204 delete map;
Mykola Kondratenko0c1eeb32014-04-15 09:35:44 +0200205
Mathias Agopiana8826d62009-10-01 03:10:14 -0700206 const int w = bitmap.width();
207 const int h = bitmap.height();
208 const void* p = bitmap.getPixels();
209
210 GLint crop[4] = { 0, h, w, -h };
211 int tw = 1 << (31 - __builtin_clz(w));
212 int th = 1 << (31 - __builtin_clz(h));
213 if (tw < w) tw <<= 1;
214 if (th < h) th <<= 1;
215
Mike Reed42a1d082014-07-07 18:06:18 -0400216 switch (bitmap.colorType()) {
217 case kN32_SkColorType:
Sai Kiran Korwar27167492015-07-07 20:00:06 +0530218 if (!mUseNpotTextures && (tw != w || th != h)) {
Mathias Agopiana8826d62009-10-01 03:10:14 -0700219 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, tw, th, 0, GL_RGBA,
220 GL_UNSIGNED_BYTE, 0);
221 glTexSubImage2D(GL_TEXTURE_2D, 0,
222 0, 0, w, h, GL_RGBA, GL_UNSIGNED_BYTE, p);
223 } else {
Sai Kiran Korwar27167492015-07-07 20:00:06 +0530224 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA,
Mathias Agopiana8826d62009-10-01 03:10:14 -0700225 GL_UNSIGNED_BYTE, p);
226 }
227 break;
228
Mike Reed42a1d082014-07-07 18:06:18 -0400229 case kRGB_565_SkColorType:
Sai Kiran Korwar27167492015-07-07 20:00:06 +0530230 if (!mUseNpotTextures && (tw != w || th != h)) {
Mathias Agopiana8826d62009-10-01 03:10:14 -0700231 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, tw, th, 0, GL_RGB,
232 GL_UNSIGNED_SHORT_5_6_5, 0);
233 glTexSubImage2D(GL_TEXTURE_2D, 0,
234 0, 0, w, h, GL_RGB, GL_UNSIGNED_SHORT_5_6_5, p);
235 } else {
Sai Kiran Korwar27167492015-07-07 20:00:06 +0530236 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, w, h, 0, GL_RGB,
Mathias Agopiana8826d62009-10-01 03:10:14 -0700237 GL_UNSIGNED_SHORT_5_6_5, p);
238 }
239 break;
240 default:
241 break;
242 }
243
244 glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_CROP_RECT_OES, crop);
245
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700246 *width = w;
247 *height = h;
248
Mathias Agopiana8826d62009-10-01 03:10:14 -0700249 return NO_ERROR;
250}
251
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800252status_t BootAnimation::readyToRun() {
253 mAssets.addDefaultAssets();
254
Jeff Brown0b722fe2012-08-24 22:40:14 -0700255 sp<IBinder> dtoken(SurfaceComposerClient::getBuiltInDisplay(
256 ISurfaceComposer::eDisplayIdMain));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800257 DisplayInfo dinfo;
Jeff Brown0b722fe2012-08-24 22:40:14 -0700258 status_t status = SurfaceComposerClient::getDisplayInfo(dtoken, &dinfo);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800259 if (status)
260 return -1;
261
262 // create the native surface
Jeff Brown0b722fe2012-08-24 22:40:14 -0700263 sp<SurfaceControl> control = session()->createSurface(String8("BootAnimation"),
264 dinfo.w, dinfo.h, PIXEL_FORMAT_RGB_565);
Mathias Agopian439863f2011-06-28 19:09:31 -0700265
Robert Carre13b58e2017-08-31 14:50:44 -0700266 SurfaceComposerClient::Transaction t;
267 t.setLayer(control, 0x40000000)
268 .apply();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800269
Mathias Agopian17f638b2009-04-16 20:04:08 -0700270 sp<Surface> s = control->getSurface();
271
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800272 // initialize opengl and egl
Mathias Agopian738b9a42009-08-06 16:41:02 -0700273 const EGLint attribs[] = {
Mathias Agopian1b253b72011-08-15 15:20:22 -0700274 EGL_RED_SIZE, 8,
275 EGL_GREEN_SIZE, 8,
276 EGL_BLUE_SIZE, 8,
Mathias Agopiana8826d62009-10-01 03:10:14 -0700277 EGL_DEPTH_SIZE, 0,
278 EGL_NONE
Mathias Agopian738b9a42009-08-06 16:41:02 -0700279 };
Andreas Gampecfedceb2014-09-30 21:48:18 -0700280 EGLint w, h;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800281 EGLint numConfigs;
282 EGLConfig config;
283 EGLSurface surface;
284 EGLContext context;
Mathias Agopian627e7b52009-05-21 19:21:59 -0700285
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800286 EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
Mathias Agopian627e7b52009-05-21 19:21:59 -0700287
288 eglInitialize(display, 0, 0);
Mathias Agopian1b253b72011-08-15 15:20:22 -0700289 eglChooseConfig(display, attribs, &config, 1, &numConfigs);
Mathias Agopian1473f462009-04-10 14:24:30 -0700290 surface = eglCreateWindowSurface(display, config, s.get(), NULL);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800291 context = eglCreateContext(display, config, NULL, NULL);
292 eglQuerySurface(display, surface, EGL_WIDTH, &w);
293 eglQuerySurface(display, surface, EGL_HEIGHT, &h);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700294
Mathias Agopianabac0102009-07-31 14:47:00 -0700295 if (eglMakeCurrent(display, surface, surface, context) == EGL_FALSE)
296 return NO_INIT;
Mathias Agopiana8826d62009-10-01 03:10:14 -0700297
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800298 mDisplay = display;
299 mContext = context;
300 mSurface = surface;
301 mWidth = w;
302 mHeight = h;
Mathias Agopian17f638b2009-04-16 20:04:08 -0700303 mFlingerSurfaceControl = control;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800304 mFlingerSurface = s;
Adrian Roos9ee5dff2018-08-22 20:19:49 +0200305 mTargetInset = -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800306
Elliott Hughesc367d482013-10-29 13:12:55 -0700307 // If the device has encryption turned on or is in process
Jason parksbd9a08d2011-01-31 15:04:34 -0600308 // of being encrypted we show the encrypted boot animation.
309 char decrypt[PROPERTY_VALUE_MAX];
310 property_get("vold.decrypt", decrypt, "");
311
Keun-young Parkb5938422017-03-23 13:46:24 -0700312 bool encryptedAnimation = atoi(decrypt) != 0 ||
313 !strcmp("trigger_restart_min_framework", decrypt);
Jason parksbd9a08d2011-01-31 15:04:34 -0600314
Jaekyun Seokc521bb32018-01-30 11:52:47 +0900315 if (!mShuttingDown && encryptedAnimation) {
316 static const char* encryptedBootFiles[] =
317 {PRODUCT_ENCRYPTED_BOOTANIMATION_FILE, SYSTEM_ENCRYPTED_BOOTANIMATION_FILE};
318 for (const char* f : encryptedBootFiles) {
319 if (access(f, R_OK) == 0) {
320 mZipFileName = f;
321 return NO_ERROR;
322 }
323 }
Jason parksbd9a08d2011-01-31 15:04:34 -0600324 }
Jaekyun Seokc521bb32018-01-30 11:52:47 +0900325 static const char* bootFiles[] =
326 {PRODUCT_BOOTANIMATION_FILE, OEM_BOOTANIMATION_FILE, SYSTEM_BOOTANIMATION_FILE};
Keun-young Parkb5938422017-03-23 13:46:24 -0700327 static const char* shutdownFiles[] =
Jaekyun Seokc521bb32018-01-30 11:52:47 +0900328 {PRODUCT_SHUTDOWNANIMATION_FILE, OEM_SHUTDOWNANIMATION_FILE, SYSTEM_SHUTDOWNANIMATION_FILE};
Keun-young Parkb5938422017-03-23 13:46:24 -0700329
330 for (const char* f : (!mShuttingDown ? bootFiles : shutdownFiles)) {
331 if (access(f, R_OK) == 0) {
332 mZipFileName = f;
333 return NO_ERROR;
334 }
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700335 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800336 return NO_ERROR;
337}
338
Mathias Agopiana8826d62009-10-01 03:10:14 -0700339bool BootAnimation::threadLoop()
340{
341 bool r;
Narayan Kamathafd31e02013-12-03 13:16:03 +0000342 // We have no bootanimation file, so we use the stock android logo
343 // animation.
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700344 if (mZipFileName.isEmpty()) {
Mathias Agopiana8826d62009-10-01 03:10:14 -0700345 r = android();
346 } else {
347 r = movie();
348 }
349
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800350 eglMakeCurrent(mDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
351 eglDestroyContext(mDisplay, mContext);
352 eglDestroySurface(mDisplay, mSurface);
Mathias Agopian6cf0db22009-04-17 19:36:26 -0700353 mFlingerSurface.clear();
Mathias Agopian17f638b2009-04-16 20:04:08 -0700354 mFlingerSurfaceControl.clear();
Mathias Agopian627e7b52009-05-21 19:21:59 -0700355 eglTerminate(mDisplay);
Sai Kiran Korwar3eee9fb2015-06-16 17:13:35 +0530356 eglReleaseThread();
Mathias Agopian627e7b52009-05-21 19:21:59 -0700357 IPCThreadState::self()->stopProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800358 return r;
359}
360
Mathias Agopiana8826d62009-10-01 03:10:14 -0700361bool BootAnimation::android()
362{
Wei Wang159a4102018-10-23 23:15:58 -0700363 SLOGD("%sAnimationShownTiming start time: %" PRId64 "ms", mShuttingDown ? "Shutdown" : "Boot",
Keun-young Parkd1794cd2017-04-04 12:21:19 -0700364 elapsedRealtime());
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700365 initTexture(&mAndroid[0], mAssets, "images/android-logo-mask.png");
366 initTexture(&mAndroid[1], mAssets, "images/android-logo-shine.png");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800367
Ed Coyne7464ac92017-06-08 12:26:48 -0700368 mCallbacks->init({});
369
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800370 // clear screen
Mathias Agopiana8826d62009-10-01 03:10:14 -0700371 glShadeModel(GL_FLAT);
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700372 glDisable(GL_DITHER);
373 glDisable(GL_SCISSOR_TEST);
Mathias Agopian59f19e42011-05-06 19:22:12 -0700374 glClearColor(0,0,0,1);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800375 glClear(GL_COLOR_BUFFER_BIT);
376 eglSwapBuffers(mDisplay, mSurface);
377
Mathias Agopiana8826d62009-10-01 03:10:14 -0700378 glEnable(GL_TEXTURE_2D);
379 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
380
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700381 const GLint xc = (mWidth - mAndroid[0].w) / 2;
382 const GLint yc = (mHeight - mAndroid[0].h) / 2;
383 const Rect updateRect(xc, yc, xc + mAndroid[0].w, yc + mAndroid[0].h);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800384
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800385 glScissor(updateRect.left, mHeight - updateRect.bottom, updateRect.width(),
386 updateRect.height());
387
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700388 // Blend state
389 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
390 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
391
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800392 const nsecs_t startTime = systemTime();
393 do {
Mathias Agopian13796652009-03-24 22:49:21 -0700394 nsecs_t now = systemTime();
395 double time = now - startTime;
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700396 float t = 4.0f * float(time / us2ns(16667)) / mAndroid[1].w;
397 GLint offset = (1 - (t - floorf(t))) * mAndroid[1].w;
398 GLint x = xc - offset;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800399
Mathias Agopian81668642009-07-28 11:41:30 -0700400 glDisable(GL_SCISSOR_TEST);
401 glClear(GL_COLOR_BUFFER_BIT);
402
403 glEnable(GL_SCISSOR_TEST);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800404 glDisable(GL_BLEND);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800405 glBindTexture(GL_TEXTURE_2D, mAndroid[1].name);
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700406 glDrawTexiOES(x, yc, 0, mAndroid[1].w, mAndroid[1].h);
407 glDrawTexiOES(x + mAndroid[1].w, yc, 0, mAndroid[1].w, mAndroid[1].h);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800408
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700409 glEnable(GL_BLEND);
410 glBindTexture(GL_TEXTURE_2D, mAndroid[0].name);
411 glDrawTexiOES(xc, yc, 0, mAndroid[0].w, mAndroid[0].h);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800412
Mathias Agopian627e7b52009-05-21 19:21:59 -0700413 EGLBoolean res = eglSwapBuffers(mDisplay, mSurface);
414 if (res == EGL_FALSE)
415 break;
416
Mathias Agopian13796652009-03-24 22:49:21 -0700417 // 12fps: don't animate too fast to preserve CPU
418 const nsecs_t sleepTime = 83333 - ns2us(systemTime() - now);
419 if (sleepTime > 0)
Mathias Agopiana8826d62009-10-01 03:10:14 -0700420 usleep(sleepTime);
Kevin Hesterd3782b22012-04-26 10:38:55 -0700421
422 checkExit();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800423 } while (!exitPending());
424
425 glDeleteTextures(1, &mAndroid[0].name);
426 glDeleteTextures(1, &mAndroid[1].name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800427 return false;
428}
429
Kevin Hesterd3782b22012-04-26 10:38:55 -0700430void BootAnimation::checkExit() {
431 // Allow surface flinger to gracefully request shutdown
432 char value[PROPERTY_VALUE_MAX];
433 property_get(EXIT_PROP_NAME, value, "0");
434 int exitnow = atoi(value);
435 if (exitnow) {
436 requestExit();
Ed Coyne7464ac92017-06-08 12:26:48 -0700437 mCallbacks->shutdown();
Kevin Hesterd3782b22012-04-26 10:38:55 -0700438 }
439}
440
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700441bool BootAnimation::validClock(const Animation::Part& part) {
442 return part.clockPosX != TEXT_MISSING_VALUE && part.clockPosY != TEXT_MISSING_VALUE;
443}
444
445bool parseTextCoord(const char* str, int* dest) {
446 if (strcmp("c", str) == 0) {
447 *dest = TEXT_CENTER_VALUE;
448 return true;
449 }
450
451 char* end;
452 int val = (int) strtol(str, &end, 0);
453 if (end == str || *end != '\0' || val == INT_MAX || val == INT_MIN) {
454 return false;
455 }
456 *dest = val;
457 return true;
458}
459
460// Parse two position coordinates. If only string is non-empty, treat it as the y value.
461void parsePosition(const char* str1, const char* str2, int* x, int* y) {
462 bool success = false;
463 if (strlen(str1) == 0) { // No values were specified
464 // success = false
465 } else if (strlen(str2) == 0) { // we have only one value
466 if (parseTextCoord(str1, y)) {
467 *x = TEXT_CENTER_VALUE;
468 success = true;
469 }
470 } else {
471 if (parseTextCoord(str1, x) && parseTextCoord(str2, y)) {
472 success = true;
473 }
474 }
475
476 if (!success) {
477 *x = TEXT_MISSING_VALUE;
478 *y = TEXT_MISSING_VALUE;
479 }
480}
481
Jesse Hall083b84c2014-09-22 10:51:09 -0700482// Parse a color represented as an HTML-style 'RRGGBB' string: each pair of
483// characters in str is a hex number in [0, 255], which are converted to
484// floating point values in the range [0.0, 1.0] and placed in the
485// corresponding elements of color.
486//
487// If the input string isn't valid, parseColor returns false and color is
488// left unchanged.
489static bool parseColor(const char str[7], float color[3]) {
490 float tmpColor[3];
491 for (int i = 0; i < 3; i++) {
492 int val = 0;
493 for (int j = 0; j < 2; j++) {
494 val *= 16;
495 char c = str[2*i + j];
496 if (c >= '0' && c <= '9') val += c - '0';
497 else if (c >= 'A' && c <= 'F') val += (c - 'A') + 10;
498 else if (c >= 'a' && c <= 'f') val += (c - 'a') + 10;
499 else return false;
500 }
501 tmpColor[i] = static_cast<float>(val) / 255.0f;
502 }
503 memcpy(color, tmpColor, sizeof(tmpColor));
504 return true;
505}
506
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700507
508static bool readFile(ZipFileRO* zip, const char* name, String8& outString)
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700509{
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700510 ZipEntryRO entry = zip->findEntryByName(name);
Wei Wang159a4102018-10-23 23:15:58 -0700511 SLOGE_IF(!entry, "couldn't find %s", name);
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700512 if (!entry) {
513 return false;
514 }
515
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700516 FileMap* entryMap = zip->createEntryFileMap(entry);
517 zip->releaseEntry(entry);
Wei Wang159a4102018-10-23 23:15:58 -0700518 SLOGE_IF(!entryMap, "entryMap is null");
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700519 if (!entryMap) {
520 return false;
521 }
522
523 outString.setTo((char const*)entryMap->getDataPtr(), entryMap->getDataLength());
Narayan Kamath688ff4c2015-02-23 15:47:54 +0000524 delete entryMap;
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700525 return true;
526}
527
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700528// The font image should be a 96x2 array of character images. The
529// columns are the printable ASCII characters 0x20 - 0x7f. The
530// top row is regular text; the bottom row is bold.
531status_t BootAnimation::initFont(Font* font, const char* fallback) {
532 status_t status = NO_ERROR;
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800533
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700534 if (font->map != nullptr) {
535 glGenTextures(1, &font->texture.name);
536 glBindTexture(GL_TEXTURE_2D, font->texture.name);
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800537
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700538 status = initTexture(font->map, &font->texture.w, &font->texture.h);
539
540 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
541 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
542 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
543 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
544 } else if (fallback != nullptr) {
545 status = initTexture(&font->texture, mAssets, fallback);
546 } else {
547 return NO_INIT;
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800548 }
549
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700550 if (status == NO_ERROR) {
551 font->char_width = font->texture.w / FONT_NUM_COLS;
552 font->char_height = font->texture.h / FONT_NUM_ROWS / 2; // There are bold and regular rows
553 }
554
555 return status;
556}
557
558void BootAnimation::drawText(const char* str, const Font& font, bool bold, int* x, int* y) {
559 glEnable(GL_BLEND); // Allow us to draw on top of the animation
560 glBindTexture(GL_TEXTURE_2D, font.texture.name);
561
562 const int len = strlen(str);
563 const int strWidth = font.char_width * len;
564
565 if (*x == TEXT_CENTER_VALUE) {
566 *x = (mWidth - strWidth) / 2;
567 } else if (*x < 0) {
568 *x = mWidth + *x - strWidth;
569 }
570 if (*y == TEXT_CENTER_VALUE) {
571 *y = (mHeight - font.char_height) / 2;
572 } else if (*y < 0) {
573 *y = mHeight + *y - font.char_height;
574 }
575
576 int cropRect[4] = { 0, 0, font.char_width, -font.char_height };
577
578 for (int i = 0; i < len; i++) {
579 char c = str[i];
580
581 if (c < FONT_BEGIN_CHAR || c > FONT_END_CHAR) {
582 c = '?';
583 }
584
585 // Crop the texture to only the pixels in the current glyph
586 const int charPos = (c - FONT_BEGIN_CHAR); // Position in the list of valid characters
587 const int row = charPos / FONT_NUM_COLS;
588 const int col = charPos % FONT_NUM_COLS;
589 cropRect[0] = col * font.char_width; // Left of column
590 cropRect[1] = row * font.char_height * 2; // Top of row
591 // Move down to bottom of regular (one char_heigh) or bold (two char_heigh) line
592 cropRect[1] += bold ? 2 * font.char_height : font.char_height;
593 glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_CROP_RECT_OES, cropRect);
594
595 glDrawTexiOES(*x, *y, 0, font.char_width, font.char_height);
596
597 *x += font.char_width;
598 }
599
600 glDisable(GL_BLEND); // Return to the animation's default behaviour
601 glBindTexture(GL_TEXTURE_2D, 0);
602}
603
Damien Bargiacchi9071db12016-10-28 17:38:22 -0700604// We render 12 or 24 hour time.
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700605void BootAnimation::drawClock(const Font& font, const int xPos, const int yPos) {
Damien Bargiacchi9071db12016-10-28 17:38:22 -0700606 static constexpr char TIME_FORMAT_12[] = "%l:%M";
607 static constexpr char TIME_FORMAT_24[] = "%H:%M";
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700608 static constexpr int TIME_LENGTH = 6;
609
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800610 time_t rawtime;
611 time(&rawtime);
612 struct tm* timeInfo = localtime(&rawtime);
613
614 char timeBuff[TIME_LENGTH];
Damien Bargiacchi9071db12016-10-28 17:38:22 -0700615 const char* timeFormat = mTimeFormat12Hour ? TIME_FORMAT_12 : TIME_FORMAT_24;
616 size_t length = strftime(timeBuff, TIME_LENGTH, timeFormat, timeInfo);
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800617
618 if (length != TIME_LENGTH - 1) {
Wei Wang159a4102018-10-23 23:15:58 -0700619 SLOGE("Couldn't format time; abandoning boot animation clock");
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800620 mClockEnabled = false;
621 return;
622 }
623
Damien Bargiacchi45a76442016-12-05 18:02:18 -0800624 char* out = timeBuff[0] == ' ' ? &timeBuff[1] : &timeBuff[0];
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700625 int x = xPos;
626 int y = yPos;
Damien Bargiacchi45a76442016-12-05 18:02:18 -0800627 drawText(out, font, false, &x, &y);
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800628}
629
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700630bool BootAnimation::parseAnimationDesc(Animation& animation)
Mathias Agopiana8826d62009-10-01 03:10:14 -0700631{
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700632 String8 desString;
633
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700634 if (!readFile(animation.zip, "desc.txt", desString)) {
Narayan Kamathafd31e02013-12-03 13:16:03 +0000635 return false;
636 }
Mathias Agopiana8826d62009-10-01 03:10:14 -0700637 char const* s = desString.string();
638
Mathias Agopiana8826d62009-10-01 03:10:14 -0700639 // Parse the description file
640 for (;;) {
641 const char* endl = strstr(s, "\n");
Andreas Gampecfedceb2014-09-30 21:48:18 -0700642 if (endl == NULL) break;
Mathias Agopiana8826d62009-10-01 03:10:14 -0700643 String8 line(s, endl - s);
644 const char* l = line.string();
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800645 int fps = 0;
646 int width = 0;
647 int height = 0;
648 int count = 0;
649 int pause = 0;
Narayan Kamathafd31e02013-12-03 13:16:03 +0000650 char path[ANIM_ENTRY_NAME_MAX];
Jesse Hall083b84c2014-09-22 10:51:09 -0700651 char color[7] = "000000"; // default to black if unspecified
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700652 char clockPos1[TEXT_POS_LEN_MAX + 1] = "";
653 char clockPos2[TEXT_POS_LEN_MAX + 1] = "";
Jesse Hall083b84c2014-09-22 10:51:09 -0700654
Kevin Hesterd3782b22012-04-26 10:38:55 -0700655 char pathType;
Mathias Agopiana8826d62009-10-01 03:10:14 -0700656 if (sscanf(l, "%d %d %d", &width, &height, &fps) == 3) {
Wei Wang159a4102018-10-23 23:15:58 -0700657 // SLOGD("> w=%d, h=%d, fps=%d", width, height, fps);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700658 animation.width = width;
659 animation.height = height;
660 animation.fps = fps;
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700661 } else if (sscanf(l, " %c %d %d %s #%6s %16s %16s",
662 &pathType, &count, &pause, path, color, clockPos1, clockPos2) >= 4) {
Wei Wang159a4102018-10-23 23:15:58 -0700663 //SLOGD("> type=%c, count=%d, pause=%d, path=%s, color=%s, clockPos1=%s, clockPos2=%s",
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700664 // pathType, count, pause, path, color, clockPos1, clockPos2);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700665 Animation::Part part;
Kevin Hesterd3782b22012-04-26 10:38:55 -0700666 part.playUntilComplete = pathType == 'c';
Mathias Agopiana8826d62009-10-01 03:10:14 -0700667 part.count = count;
668 part.pause = pause;
669 part.path = path;
Geoffrey Pitschd6d9a1d2016-06-08 00:38:58 -0700670 part.audioData = NULL;
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700671 part.animation = NULL;
Jesse Hall083b84c2014-09-22 10:51:09 -0700672 if (!parseColor(color, part.backgroundColor)) {
Wei Wang159a4102018-10-23 23:15:58 -0700673 SLOGE("> invalid color '#%s'", color);
Jesse Hall083b84c2014-09-22 10:51:09 -0700674 part.backgroundColor[0] = 0.0f;
675 part.backgroundColor[1] = 0.0f;
676 part.backgroundColor[2] = 0.0f;
677 }
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700678 parsePosition(clockPos1, clockPos2, &part.clockPosX, &part.clockPosY);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700679 animation.parts.add(part);
680 }
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700681 else if (strcmp(l, "$SYSTEM") == 0) {
Wei Wang159a4102018-10-23 23:15:58 -0700682 // SLOGD("> SYSTEM");
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700683 Animation::Part part;
684 part.playUntilComplete = false;
685 part.count = 1;
686 part.pause = 0;
Geoffrey Pitschd6d9a1d2016-06-08 00:38:58 -0700687 part.audioData = NULL;
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700688 part.animation = loadAnimation(String8(SYSTEM_BOOTANIMATION_FILE));
689 if (part.animation != NULL)
690 animation.parts.add(part);
691 }
Mathias Agopiana8826d62009-10-01 03:10:14 -0700692 s = ++endl;
693 }
694
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700695 return true;
696}
697
698bool BootAnimation::preloadZip(Animation& animation)
699{
Mathias Agopiana8826d62009-10-01 03:10:14 -0700700 // read all the data structures
701 const size_t pcount = animation.parts.size();
Narayan Kamathafd31e02013-12-03 13:16:03 +0000702 void *cookie = NULL;
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400703 ZipFileRO* zip = animation.zip;
704 if (!zip->startIteration(&cookie)) {
Narayan Kamathafd31e02013-12-03 13:16:03 +0000705 return false;
706 }
707
708 ZipEntryRO entry;
709 char name[ANIM_ENTRY_NAME_MAX];
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400710 while ((entry = zip->nextEntry(cookie)) != NULL) {
711 const int foundEntryName = zip->getEntryFileName(entry, name, ANIM_ENTRY_NAME_MAX);
Narayan Kamathafd31e02013-12-03 13:16:03 +0000712 if (foundEntryName > ANIM_ENTRY_NAME_MAX || foundEntryName == -1) {
Wei Wang159a4102018-10-23 23:15:58 -0700713 SLOGE("Error fetching entry file name");
Narayan Kamathafd31e02013-12-03 13:16:03 +0000714 continue;
715 }
716
717 const String8 entryName(name);
718 const String8 path(entryName.getPathDir());
719 const String8 leaf(entryName.getPathLeaf());
720 if (leaf.size() > 0) {
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700721 if (entryName == CLOCK_FONT_ZIP_NAME) {
722 FileMap* map = zip->createEntryFileMap(entry);
723 if (map) {
724 animation.clockFont.map = map;
725 }
726 continue;
727 }
728
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400729 for (size_t j = 0; j < pcount; j++) {
Narayan Kamathafd31e02013-12-03 13:16:03 +0000730 if (path == animation.parts[j].path) {
Narayan Kamath4600dd02015-06-16 12:02:57 +0100731 uint16_t method;
Narayan Kamathafd31e02013-12-03 13:16:03 +0000732 // supports only stored png files
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400733 if (zip->getEntryInfo(entry, &method, NULL, NULL, NULL, NULL, NULL)) {
Narayan Kamathafd31e02013-12-03 13:16:03 +0000734 if (method == ZipFileRO::kCompressStored) {
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400735 FileMap* map = zip->createEntryFileMap(entry);
Narayan Kamathafd31e02013-12-03 13:16:03 +0000736 if (map) {
Narayan Kamathafd31e02013-12-03 13:16:03 +0000737 Animation::Part& part(animation.parts.editItemAt(j));
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700738 if (leaf == "audio.wav") {
739 // a part may have at most one audio file
Geoffrey Pitschd6d9a1d2016-06-08 00:38:58 -0700740 part.audioData = (uint8_t *)map->getDataPtr();
741 part.audioLength = map->getDataLength();
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400742 } else if (leaf == "trim.txt") {
743 part.trimData.setTo((char const*)map->getDataPtr(),
744 map->getDataLength());
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700745 } else {
746 Animation::Frame frame;
747 frame.name = leaf;
748 frame.map = map;
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400749 frame.trimWidth = animation.width;
750 frame.trimHeight = animation.height;
751 frame.trimX = 0;
752 frame.trimY = 0;
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700753 part.frames.add(frame);
754 }
Mathias Agopiana8826d62009-10-01 03:10:14 -0700755 }
Geoffrey Pitschd6d9a1d2016-06-08 00:38:58 -0700756 } else {
Wei Wang159a4102018-10-23 23:15:58 -0700757 SLOGE("bootanimation.zip is compressed; must be only stored");
Mathias Agopiana8826d62009-10-01 03:10:14 -0700758 }
759 }
760 }
761 }
762 }
763 }
764
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400765 // If there is trimData present, override the positioning defaults.
766 for (Animation::Part& part : animation.parts) {
767 const char* trimDataStr = part.trimData.string();
768 for (size_t frameIdx = 0; frameIdx < part.frames.size(); frameIdx++) {
769 const char* endl = strstr(trimDataStr, "\n");
770 // No more trimData for this part.
771 if (endl == NULL) {
772 break;
773 }
774 String8 line(trimDataStr, endl - trimDataStr);
775 const char* lineStr = line.string();
776 trimDataStr = ++endl;
777 int width = 0, height = 0, x = 0, y = 0;
778 if (sscanf(lineStr, "%dx%d+%d+%d", &width, &height, &x, &y) == 4) {
779 Animation::Frame& frame(part.frames.editItemAt(frameIdx));
780 frame.trimWidth = width;
781 frame.trimHeight = height;
782 frame.trimX = x;
783 frame.trimY = y;
784 } else {
Wei Wang159a4102018-10-23 23:15:58 -0700785 SLOGE("Error parsing trim.txt, line: %s", lineStr);
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400786 break;
787 }
788 }
789 }
790
Ed Coyne7464ac92017-06-08 12:26:48 -0700791 mCallbacks->init(animation.parts);
Geoffrey Pitschd6d9a1d2016-06-08 00:38:58 -0700792
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400793 zip->endIteration(cookie);
Narayan Kamathafd31e02013-12-03 13:16:03 +0000794
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700795 return true;
796}
797
798bool BootAnimation::movie()
799{
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700800 Animation* animation = loadAnimation(mZipFileName);
801 if (animation == NULL)
802 return false;
803
Damien Bargiacchi97480862016-03-29 14:55:55 -0700804 bool anyPartHasClock = false;
805 for (size_t i=0; i < animation->parts.size(); i++) {
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700806 if(validClock(animation->parts[i])) {
Damien Bargiacchi97480862016-03-29 14:55:55 -0700807 anyPartHasClock = true;
808 break;
809 }
810 }
811 if (!anyPartHasClock) {
812 mClockEnabled = false;
813 }
814
Sai Kiran Korwar27167492015-07-07 20:00:06 +0530815 // Check if npot textures are supported
816 mUseNpotTextures = false;
817 String8 gl_extensions;
818 const char* exts = reinterpret_cast<const char*>(glGetString(GL_EXTENSIONS));
819 if (!exts) {
820 glGetError();
821 } else {
822 gl_extensions.setTo(exts);
823 if ((gl_extensions.find("GL_ARB_texture_non_power_of_two") != -1) ||
824 (gl_extensions.find("GL_OES_texture_npot") != -1)) {
825 mUseNpotTextures = true;
826 }
827 }
828
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800829 // Blend required to draw time on top of animation frames.
830 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700831 glShadeModel(GL_FLAT);
832 glDisable(GL_DITHER);
833 glDisable(GL_SCISSOR_TEST);
834 glDisable(GL_BLEND);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700835
836 glBindTexture(GL_TEXTURE_2D, 0);
837 glEnable(GL_TEXTURE_2D);
838 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
839 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
840 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
841 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
842 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
843
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700844 bool clockFontInitialized = false;
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800845 if (mClockEnabled) {
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700846 clockFontInitialized =
847 (initFont(&animation->clockFont, CLOCK_FONT_ASSET) == NO_ERROR);
848 mClockEnabled = clockFontInitialized;
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800849 }
850
Damien Bargiacchi97480862016-03-29 14:55:55 -0700851 if (mClockEnabled && !updateIsTimeAccurate()) {
852 mTimeCheckThread = new TimeCheckThread(this);
853 mTimeCheckThread->run("BootAnimation::TimeCheckThread", PRIORITY_NORMAL);
854 }
855
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700856 playAnimation(*animation);
Damien Bargiacchi97480862016-03-29 14:55:55 -0700857
Geoffrey Pitscha9173532016-07-19 14:56:39 -0400858 if (mTimeCheckThread != nullptr) {
Damien Bargiacchi97480862016-03-29 14:55:55 -0700859 mTimeCheckThread->requestExit();
Geoffrey Pitscha9173532016-07-19 14:56:39 -0400860 mTimeCheckThread = nullptr;
861 }
862
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700863 if (clockFontInitialized) {
864 glDeleteTextures(1, &animation->clockFont.texture.name);
Andriy Naborskyy815e51d2016-03-24 16:43:34 -0700865 }
866
Damien Bargiacchi1a8a2132018-07-24 15:20:26 -0700867 releaseAnimation(animation);
868
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700869 return false;
870}
871
872bool BootAnimation::playAnimation(const Animation& animation)
873{
874 const size_t pcount = animation.parts.size();
Mathias Agopiana8826d62009-10-01 03:10:14 -0700875 nsecs_t frameDuration = s2ns(1) / animation.fps;
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400876 const int animationX = (mWidth - animation.width) / 2;
877 const int animationY = (mHeight - animation.height) / 2;
Mathias Agopian9f3020d2009-11-06 16:30:18 -0800878
Wei Wang159a4102018-10-23 23:15:58 -0700879 SLOGD("%sAnimationShownTiming start time: %" PRId64 "ms", mShuttingDown ? "Shutdown" : "Boot",
Keun-young Parkd1794cd2017-04-04 12:21:19 -0700880 elapsedRealtime());
Narayan Kamathafd31e02013-12-03 13:16:03 +0000881 for (size_t i=0 ; i<pcount ; i++) {
Mathias Agopiana8826d62009-10-01 03:10:14 -0700882 const Animation::Part& part(animation.parts[i]);
883 const size_t fcount = part.frames.size();
884 glBindTexture(GL_TEXTURE_2D, 0);
885
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700886 // Handle animation package
887 if (part.animation != NULL) {
888 playAnimation(*part.animation);
889 if (exitPending())
890 break;
891 continue; //to next part
892 }
893
Mathias Agopiana8826d62009-10-01 03:10:14 -0700894 for (int r=0 ; !part.count || r<part.count ; r++) {
Kevin Hesterd3782b22012-04-26 10:38:55 -0700895 // Exit any non playuntil complete parts immediately
896 if(exitPending() && !part.playUntilComplete)
897 break;
898
Ed Coyne7464ac92017-06-08 12:26:48 -0700899 mCallbacks->playPart(i, part, r);
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700900
Jesse Hall083b84c2014-09-22 10:51:09 -0700901 glClearColor(
902 part.backgroundColor[0],
903 part.backgroundColor[1],
904 part.backgroundColor[2],
905 1.0f);
906
Narayan Kamathafd31e02013-12-03 13:16:03 +0000907 for (size_t j=0 ; j<fcount && (!exitPending() || part.playUntilComplete) ; j++) {
Mathias Agopiana8826d62009-10-01 03:10:14 -0700908 const Animation::Frame& frame(part.frames[j]);
Mathias Agopiandb7dd2a2012-05-12 15:08:21 -0700909 nsecs_t lastFrame = systemTime();
Mathias Agopiana8826d62009-10-01 03:10:14 -0700910
911 if (r > 0) {
912 glBindTexture(GL_TEXTURE_2D, frame.tid);
913 } else {
914 if (part.count != 1) {
915 glGenTextures(1, &frame.tid);
916 glBindTexture(GL_TEXTURE_2D, frame.tid);
917 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
918 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
919 }
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700920 int w, h;
921 initTexture(frame.map, &w, &h);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700922 }
923
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400924 const int xc = animationX + frame.trimX;
925 const int yc = animationY + frame.trimY;
926 Region clearReg(Rect(mWidth, mHeight));
927 clearReg.subtractSelf(Rect(xc, yc, xc+frame.trimWidth, yc+frame.trimHeight));
Mathias Agopian9f3020d2009-11-06 16:30:18 -0800928 if (!clearReg.isEmpty()) {
929 Region::const_iterator head(clearReg.begin());
930 Region::const_iterator tail(clearReg.end());
931 glEnable(GL_SCISSOR_TEST);
932 while (head != tail) {
Andreas Gampecfedceb2014-09-30 21:48:18 -0700933 const Rect& r2(*head++);
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400934 glScissor(r2.left, mHeight - r2.bottom, r2.width(), r2.height());
Mathias Agopian9f3020d2009-11-06 16:30:18 -0800935 glClear(GL_COLOR_BUFFER_BIT);
936 }
937 glDisable(GL_SCISSOR_TEST);
938 }
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400939 // specify the y center as ceiling((mHeight - frame.trimHeight) / 2)
940 // which is equivalent to mHeight - (yc + frame.trimHeight)
941 glDrawTexiOES(xc, mHeight - (yc + frame.trimHeight),
942 0, frame.trimWidth, frame.trimHeight);
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700943 if (mClockEnabled && mTimeIsAccurate && validClock(part)) {
944 drawClock(animation.clockFont, part.clockPosX, part.clockPosY);
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800945 }
Adrian Roos9ee5dff2018-08-22 20:19:49 +0200946 handleViewport(frameDuration);
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800947
Mathias Agopiana8826d62009-10-01 03:10:14 -0700948 eglSwapBuffers(mDisplay, mSurface);
949
950 nsecs_t now = systemTime();
951 nsecs_t delay = frameDuration - (now - lastFrame);
Wei Wang159a4102018-10-23 23:15:58 -0700952 //SLOGD("%lld, %lld", ns2ms(now - lastFrame), ns2ms(delay));
Mathias Agopiana8826d62009-10-01 03:10:14 -0700953 lastFrame = now;
Mathias Agopiandb7dd2a2012-05-12 15:08:21 -0700954
955 if (delay > 0) {
956 struct timespec spec;
957 spec.tv_sec = (now + delay) / 1000000000;
958 spec.tv_nsec = (now + delay) % 1000000000;
959 int err;
960 do {
961 err = clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &spec, NULL);
962 } while (err<0 && errno == EINTR);
963 }
Kevin Hesterd3782b22012-04-26 10:38:55 -0700964
965 checkExit();
Mathias Agopiana8826d62009-10-01 03:10:14 -0700966 }
Kevin Hesterd3782b22012-04-26 10:38:55 -0700967
Mathias Agopiana8826d62009-10-01 03:10:14 -0700968 usleep(part.pause * ns2us(frameDuration));
Kevin Hesterd3782b22012-04-26 10:38:55 -0700969
970 // For infinite parts, we've now played them at least once, so perhaps exit
Adrian Roos9ee5dff2018-08-22 20:19:49 +0200971 if(exitPending() && !part.count && mCurrentInset >= mTargetInset)
Kevin Hesterd3782b22012-04-26 10:38:55 -0700972 break;
Mathias Agopiana8826d62009-10-01 03:10:14 -0700973 }
974
Geoffrey Pitsch2fb30fb2016-07-06 16:16:20 -0400975 }
976
977 // Free textures created for looping parts now that the animation is done.
978 for (const Animation::Part& part : animation.parts) {
Mathias Agopiana8826d62009-10-01 03:10:14 -0700979 if (part.count != 1) {
Geoffrey Pitsch2fb30fb2016-07-06 16:16:20 -0400980 const size_t fcount = part.frames.size();
981 for (size_t j = 0; j < fcount; j++) {
Mathias Agopiana8826d62009-10-01 03:10:14 -0700982 const Animation::Frame& frame(part.frames[j]);
983 glDeleteTextures(1, &frame.tid);
984 }
985 }
986 }
Geoffrey Pitschd6d9a1d2016-06-08 00:38:58 -0700987
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700988 return true;
Mathias Agopiana8826d62009-10-01 03:10:14 -0700989}
990
Adrian Roos9ee5dff2018-08-22 20:19:49 +0200991void BootAnimation::handleViewport(nsecs_t timestep) {
992 if (mShuttingDown || !mFlingerSurfaceControl || mTargetInset == 0) {
993 return;
994 }
995 if (mTargetInset < 0) {
996 // Poll the amount for the top display inset. This will return -1 until persistent properties
997 // have been loaded.
998 mTargetInset = android::base::GetIntProperty("persist.sys.displayinset.top",
999 -1 /* default */, -1 /* min */, mHeight / 2 /* max */);
1000 }
1001 if (mTargetInset <= 0) {
1002 return;
1003 }
1004
1005 if (mCurrentInset < mTargetInset) {
1006 // After the device boots, the inset will effectively be cropped away. We animate this here.
1007 float fraction = static_cast<float>(mCurrentInset) / mTargetInset;
1008 int interpolatedInset = (cosf((fraction + 1) * M_PI) / 2.0f + 0.5f) * mTargetInset;
1009
1010 SurfaceComposerClient::Transaction()
1011 .setCrop(mFlingerSurfaceControl, Rect(0, interpolatedInset, mWidth, mHeight))
1012 .apply();
1013 } else {
1014 // At the end of the animation, we switch to the viewport that DisplayManager will apply
1015 // later. This changes the coordinate system, and means we must move the surface up by
1016 // the inset amount.
1017 sp<IBinder> dtoken(SurfaceComposerClient::getBuiltInDisplay(
1018 ISurfaceComposer::eDisplayIdMain));
1019
1020 Rect layerStackRect(0, 0, mWidth, mHeight - mTargetInset);
1021 Rect displayRect(0, mTargetInset, mWidth, mHeight);
1022
1023 SurfaceComposerClient::Transaction t;
1024 t.setPosition(mFlingerSurfaceControl, 0, -mTargetInset)
1025 .setCrop(mFlingerSurfaceControl, Rect(0, mTargetInset, mWidth, mHeight));
1026 t.setDisplayProjection(dtoken, 0 /* orientation */, layerStackRect, displayRect);
1027 t.apply();
1028
1029 mTargetInset = mCurrentInset = 0;
1030 }
1031
1032 int delta = timestep * mTargetInset / ms2ns(200);
1033 mCurrentInset += delta;
1034}
1035
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001036void BootAnimation::releaseAnimation(Animation* animation) const
1037{
1038 for (Vector<Animation::Part>::iterator it = animation->parts.begin(),
1039 e = animation->parts.end(); it != e; ++it) {
1040 if (it->animation)
1041 releaseAnimation(it->animation);
1042 }
1043 if (animation->zip)
1044 delete animation->zip;
1045 delete animation;
1046}
1047
1048BootAnimation::Animation* BootAnimation::loadAnimation(const String8& fn)
1049{
1050 if (mLoadedFiles.indexOf(fn) >= 0) {
Wei Wang159a4102018-10-23 23:15:58 -07001051 SLOGE("File \"%s\" is already loaded. Cyclic ref is not allowed",
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001052 fn.string());
1053 return NULL;
1054 }
1055 ZipFileRO *zip = ZipFileRO::open(fn);
1056 if (zip == NULL) {
Wei Wang159a4102018-10-23 23:15:58 -07001057 SLOGE("Failed to open animation zip \"%s\": %s",
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001058 fn.string(), strerror(errno));
1059 return NULL;
1060 }
1061
1062 Animation *animation = new Animation;
1063 animation->fileName = fn;
1064 animation->zip = zip;
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -07001065 animation->clockFont.map = nullptr;
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001066 mLoadedFiles.add(animation->fileName);
1067
1068 parseAnimationDesc(*animation);
Geoffrey Pitscha91a2d72016-07-12 14:46:19 -04001069 if (!preloadZip(*animation)) {
1070 return NULL;
1071 }
1072
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001073
1074 mLoadedFiles.remove(fn);
1075 return animation;
1076}
Damien Bargiacchi97480862016-03-29 14:55:55 -07001077
1078bool BootAnimation::updateIsTimeAccurate() {
1079 static constexpr long long MAX_TIME_IN_PAST = 60000LL * 60LL * 24LL * 30LL; // 30 days
1080 static constexpr long long MAX_TIME_IN_FUTURE = 60000LL * 90LL; // 90 minutes
1081
1082 if (mTimeIsAccurate) {
1083 return true;
1084 }
Keun-young Parkb5938422017-03-23 13:46:24 -07001085 if (mShuttingDown) return true;
Damien Bargiacchi97480862016-03-29 14:55:55 -07001086 struct stat statResult;
Damien Bargiacchi9071db12016-10-28 17:38:22 -07001087
1088 if(stat(TIME_FORMAT_12_HOUR_FLAG_FILE_PATH, &statResult) == 0) {
1089 mTimeFormat12Hour = true;
1090 }
1091
Damien Bargiacchi97480862016-03-29 14:55:55 -07001092 if(stat(ACCURATE_TIME_FLAG_FILE_PATH, &statResult) == 0) {
1093 mTimeIsAccurate = true;
1094 return true;
1095 }
1096
1097 FILE* file = fopen(LAST_TIME_CHANGED_FILE_PATH, "r");
1098 if (file != NULL) {
1099 long long lastChangedTime = 0;
1100 fscanf(file, "%lld", &lastChangedTime);
1101 fclose(file);
1102 if (lastChangedTime > 0) {
1103 struct timespec now;
1104 clock_gettime(CLOCK_REALTIME, &now);
1105 // Match the Java timestamp format
1106 long long rtcNow = (now.tv_sec * 1000LL) + (now.tv_nsec / 1000000LL);
Damien Bargiacchi96762812016-07-12 15:53:40 -07001107 if (ACCURATE_TIME_EPOCH < rtcNow
1108 && lastChangedTime > (rtcNow - MAX_TIME_IN_PAST)
1109 && lastChangedTime < (rtcNow + MAX_TIME_IN_FUTURE)) {
Damien Bargiacchi97480862016-03-29 14:55:55 -07001110 mTimeIsAccurate = true;
1111 }
1112 }
1113 }
1114
1115 return mTimeIsAccurate;
1116}
1117
1118BootAnimation::TimeCheckThread::TimeCheckThread(BootAnimation* bootAnimation) : Thread(false),
1119 mInotifyFd(-1), mSystemWd(-1), mTimeWd(-1), mBootAnimation(bootAnimation) {}
1120
1121BootAnimation::TimeCheckThread::~TimeCheckThread() {
1122 // mInotifyFd may be -1 but that's ok since we're not at risk of attempting to close a valid FD.
1123 close(mInotifyFd);
1124}
1125
1126bool BootAnimation::TimeCheckThread::threadLoop() {
1127 bool shouldLoop = doThreadLoop() && !mBootAnimation->mTimeIsAccurate
1128 && mBootAnimation->mClockEnabled;
1129 if (!shouldLoop) {
1130 close(mInotifyFd);
1131 mInotifyFd = -1;
1132 }
1133 return shouldLoop;
1134}
1135
1136bool BootAnimation::TimeCheckThread::doThreadLoop() {
1137 static constexpr int BUFF_LEN (10 * (sizeof(struct inotify_event) + NAME_MAX + 1));
1138
1139 // Poll instead of doing a blocking read so the Thread can exit if requested.
1140 struct pollfd pfd = { mInotifyFd, POLLIN, 0 };
1141 ssize_t pollResult = poll(&pfd, 1, 1000);
1142
1143 if (pollResult == 0) {
1144 return true;
1145 } else if (pollResult < 0) {
Wei Wang159a4102018-10-23 23:15:58 -07001146 SLOGE("Could not poll inotify events");
Damien Bargiacchi97480862016-03-29 14:55:55 -07001147 return false;
1148 }
1149
1150 char buff[BUFF_LEN] __attribute__ ((aligned(__alignof__(struct inotify_event))));;
1151 ssize_t length = read(mInotifyFd, buff, BUFF_LEN);
1152 if (length == 0) {
1153 return true;
1154 } else if (length < 0) {
Wei Wang159a4102018-10-23 23:15:58 -07001155 SLOGE("Could not read inotify events");
Damien Bargiacchi97480862016-03-29 14:55:55 -07001156 return false;
1157 }
1158
1159 const struct inotify_event *event;
1160 for (char* ptr = buff; ptr < buff + length; ptr += sizeof(struct inotify_event) + event->len) {
1161 event = (const struct inotify_event *) ptr;
1162 if (event->wd == mSystemWd && strcmp(SYSTEM_TIME_DIR_NAME, event->name) == 0) {
1163 addTimeDirWatch();
1164 } else if (event->wd == mTimeWd && (strcmp(LAST_TIME_CHANGED_FILE_NAME, event->name) == 0
1165 || strcmp(ACCURATE_TIME_FLAG_FILE_NAME, event->name) == 0)) {
1166 return !mBootAnimation->updateIsTimeAccurate();
1167 }
1168 }
1169
1170 return true;
1171}
1172
1173void BootAnimation::TimeCheckThread::addTimeDirWatch() {
1174 mTimeWd = inotify_add_watch(mInotifyFd, SYSTEM_TIME_DIR_PATH,
1175 IN_CLOSE_WRITE | IN_MOVED_TO | IN_ATTRIB);
1176 if (mTimeWd > 0) {
1177 // No need to watch for the time directory to be created if it already exists
1178 inotify_rm_watch(mInotifyFd, mSystemWd);
1179 mSystemWd = -1;
1180 }
1181}
1182
1183status_t BootAnimation::TimeCheckThread::readyToRun() {
1184 mInotifyFd = inotify_init();
1185 if (mInotifyFd < 0) {
Wei Wang159a4102018-10-23 23:15:58 -07001186 SLOGE("Could not initialize inotify fd");
Damien Bargiacchi97480862016-03-29 14:55:55 -07001187 return NO_INIT;
1188 }
1189
1190 mSystemWd = inotify_add_watch(mInotifyFd, SYSTEM_DATA_DIR_PATH, IN_CREATE | IN_ATTRIB);
1191 if (mSystemWd < 0) {
1192 close(mInotifyFd);
1193 mInotifyFd = -1;
Wei Wang159a4102018-10-23 23:15:58 -07001194 SLOGE("Could not add watch for %s", SYSTEM_DATA_DIR_PATH);
Damien Bargiacchi97480862016-03-29 14:55:55 -07001195 return NO_INIT;
1196 }
1197
1198 addTimeDirWatch();
1199
1200 if (mBootAnimation->updateIsTimeAccurate()) {
1201 close(mInotifyFd);
1202 mInotifyFd = -1;
1203 return ALREADY_EXISTS;
1204 }
1205
1206 return NO_ERROR;
1207}
1208
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001209// ---------------------------------------------------------------------------
1210
1211}
1212; // namespace android