blob: 46917e4f6062a4b3deda049ee5e0ebf41edea928 [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
Dominik Laskowski3316a0a2019-01-25 02:56:41 -0800255 mDisplayToken = SurfaceComposerClient::getInternalDisplayToken();
256 if (mDisplayToken == nullptr)
257 return -1;
258
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800259 DisplayInfo dinfo;
Dominik Laskowski3316a0a2019-01-25 02:56:41 -0800260 status_t status = SurfaceComposerClient::getDisplayInfo(mDisplayToken, &dinfo);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800261 if (status)
262 return -1;
263
264 // create the native surface
Jeff Brown0b722fe2012-08-24 22:40:14 -0700265 sp<SurfaceControl> control = session()->createSurface(String8("BootAnimation"),
266 dinfo.w, dinfo.h, PIXEL_FORMAT_RGB_565);
Mathias Agopian439863f2011-06-28 19:09:31 -0700267
Robert Carre13b58e2017-08-31 14:50:44 -0700268 SurfaceComposerClient::Transaction t;
269 t.setLayer(control, 0x40000000)
270 .apply();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800271
Mathias Agopian17f638b2009-04-16 20:04:08 -0700272 sp<Surface> s = control->getSurface();
273
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800274 // initialize opengl and egl
Mathias Agopian738b9a42009-08-06 16:41:02 -0700275 const EGLint attribs[] = {
Mathias Agopian1b253b72011-08-15 15:20:22 -0700276 EGL_RED_SIZE, 8,
277 EGL_GREEN_SIZE, 8,
278 EGL_BLUE_SIZE, 8,
Mathias Agopiana8826d62009-10-01 03:10:14 -0700279 EGL_DEPTH_SIZE, 0,
280 EGL_NONE
Mathias Agopian738b9a42009-08-06 16:41:02 -0700281 };
Andreas Gampecfedceb2014-09-30 21:48:18 -0700282 EGLint w, h;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800283 EGLint numConfigs;
284 EGLConfig config;
285 EGLSurface surface;
286 EGLContext context;
Mathias Agopian627e7b52009-05-21 19:21:59 -0700287
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800288 EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
Mathias Agopian627e7b52009-05-21 19:21:59 -0700289
290 eglInitialize(display, 0, 0);
Mathias Agopian1b253b72011-08-15 15:20:22 -0700291 eglChooseConfig(display, attribs, &config, 1, &numConfigs);
Mathias Agopian1473f462009-04-10 14:24:30 -0700292 surface = eglCreateWindowSurface(display, config, s.get(), NULL);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800293 context = eglCreateContext(display, config, NULL, NULL);
294 eglQuerySurface(display, surface, EGL_WIDTH, &w);
295 eglQuerySurface(display, surface, EGL_HEIGHT, &h);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700296
Mathias Agopianabac0102009-07-31 14:47:00 -0700297 if (eglMakeCurrent(display, surface, surface, context) == EGL_FALSE)
298 return NO_INIT;
Mathias Agopiana8826d62009-10-01 03:10:14 -0700299
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800300 mDisplay = display;
301 mContext = context;
302 mSurface = surface;
303 mWidth = w;
304 mHeight = h;
Mathias Agopian17f638b2009-04-16 20:04:08 -0700305 mFlingerSurfaceControl = control;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800306 mFlingerSurface = s;
Adrian Roos9ee5dff2018-08-22 20:19:49 +0200307 mTargetInset = -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800308
Elliott Hughesc367d482013-10-29 13:12:55 -0700309 // If the device has encryption turned on or is in process
Jason parksbd9a08d2011-01-31 15:04:34 -0600310 // of being encrypted we show the encrypted boot animation.
311 char decrypt[PROPERTY_VALUE_MAX];
312 property_get("vold.decrypt", decrypt, "");
313
Keun-young Parkb5938422017-03-23 13:46:24 -0700314 bool encryptedAnimation = atoi(decrypt) != 0 ||
315 !strcmp("trigger_restart_min_framework", decrypt);
Jason parksbd9a08d2011-01-31 15:04:34 -0600316
Jaekyun Seokc521bb32018-01-30 11:52:47 +0900317 if (!mShuttingDown && encryptedAnimation) {
318 static const char* encryptedBootFiles[] =
319 {PRODUCT_ENCRYPTED_BOOTANIMATION_FILE, SYSTEM_ENCRYPTED_BOOTANIMATION_FILE};
320 for (const char* f : encryptedBootFiles) {
321 if (access(f, R_OK) == 0) {
322 mZipFileName = f;
323 return NO_ERROR;
324 }
325 }
Jason parksbd9a08d2011-01-31 15:04:34 -0600326 }
Jaekyun Seokc521bb32018-01-30 11:52:47 +0900327 static const char* bootFiles[] =
328 {PRODUCT_BOOTANIMATION_FILE, OEM_BOOTANIMATION_FILE, SYSTEM_BOOTANIMATION_FILE};
Keun-young Parkb5938422017-03-23 13:46:24 -0700329 static const char* shutdownFiles[] =
Jaekyun Seokc521bb32018-01-30 11:52:47 +0900330 {PRODUCT_SHUTDOWNANIMATION_FILE, OEM_SHUTDOWNANIMATION_FILE, SYSTEM_SHUTDOWNANIMATION_FILE};
Keun-young Parkb5938422017-03-23 13:46:24 -0700331
332 for (const char* f : (!mShuttingDown ? bootFiles : shutdownFiles)) {
333 if (access(f, R_OK) == 0) {
334 mZipFileName = f;
335 return NO_ERROR;
336 }
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700337 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800338 return NO_ERROR;
339}
340
Mathias Agopiana8826d62009-10-01 03:10:14 -0700341bool BootAnimation::threadLoop()
342{
343 bool r;
Narayan Kamathafd31e02013-12-03 13:16:03 +0000344 // We have no bootanimation file, so we use the stock android logo
345 // animation.
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700346 if (mZipFileName.isEmpty()) {
Mathias Agopiana8826d62009-10-01 03:10:14 -0700347 r = android();
348 } else {
349 r = movie();
350 }
351
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800352 eglMakeCurrent(mDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
353 eglDestroyContext(mDisplay, mContext);
354 eglDestroySurface(mDisplay, mSurface);
Mathias Agopian6cf0db22009-04-17 19:36:26 -0700355 mFlingerSurface.clear();
Mathias Agopian17f638b2009-04-16 20:04:08 -0700356 mFlingerSurfaceControl.clear();
Mathias Agopian627e7b52009-05-21 19:21:59 -0700357 eglTerminate(mDisplay);
Sai Kiran Korwar3eee9fb2015-06-16 17:13:35 +0530358 eglReleaseThread();
Mathias Agopian627e7b52009-05-21 19:21:59 -0700359 IPCThreadState::self()->stopProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800360 return r;
361}
362
Mathias Agopiana8826d62009-10-01 03:10:14 -0700363bool BootAnimation::android()
364{
Wei Wang159a4102018-10-23 23:15:58 -0700365 SLOGD("%sAnimationShownTiming start time: %" PRId64 "ms", mShuttingDown ? "Shutdown" : "Boot",
Keun-young Parkd1794cd2017-04-04 12:21:19 -0700366 elapsedRealtime());
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700367 initTexture(&mAndroid[0], mAssets, "images/android-logo-mask.png");
368 initTexture(&mAndroid[1], mAssets, "images/android-logo-shine.png");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800369
Ed Coyne7464ac92017-06-08 12:26:48 -0700370 mCallbacks->init({});
371
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800372 // clear screen
Mathias Agopiana8826d62009-10-01 03:10:14 -0700373 glShadeModel(GL_FLAT);
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700374 glDisable(GL_DITHER);
375 glDisable(GL_SCISSOR_TEST);
Mathias Agopian59f19e42011-05-06 19:22:12 -0700376 glClearColor(0,0,0,1);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800377 glClear(GL_COLOR_BUFFER_BIT);
378 eglSwapBuffers(mDisplay, mSurface);
379
Mathias Agopiana8826d62009-10-01 03:10:14 -0700380 glEnable(GL_TEXTURE_2D);
381 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
382
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700383 const GLint xc = (mWidth - mAndroid[0].w) / 2;
384 const GLint yc = (mHeight - mAndroid[0].h) / 2;
385 const Rect updateRect(xc, yc, xc + mAndroid[0].w, yc + mAndroid[0].h);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800386
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800387 glScissor(updateRect.left, mHeight - updateRect.bottom, updateRect.width(),
388 updateRect.height());
389
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700390 // Blend state
391 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
392 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
393
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800394 const nsecs_t startTime = systemTime();
395 do {
Mathias Agopian13796652009-03-24 22:49:21 -0700396 nsecs_t now = systemTime();
397 double time = now - startTime;
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700398 float t = 4.0f * float(time / us2ns(16667)) / mAndroid[1].w;
399 GLint offset = (1 - (t - floorf(t))) * mAndroid[1].w;
400 GLint x = xc - offset;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800401
Mathias Agopian81668642009-07-28 11:41:30 -0700402 glDisable(GL_SCISSOR_TEST);
403 glClear(GL_COLOR_BUFFER_BIT);
404
405 glEnable(GL_SCISSOR_TEST);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800406 glDisable(GL_BLEND);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800407 glBindTexture(GL_TEXTURE_2D, mAndroid[1].name);
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700408 glDrawTexiOES(x, yc, 0, mAndroid[1].w, mAndroid[1].h);
409 glDrawTexiOES(x + mAndroid[1].w, yc, 0, mAndroid[1].w, mAndroid[1].h);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800410
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700411 glEnable(GL_BLEND);
412 glBindTexture(GL_TEXTURE_2D, mAndroid[0].name);
413 glDrawTexiOES(xc, yc, 0, mAndroid[0].w, mAndroid[0].h);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800414
Mathias Agopian627e7b52009-05-21 19:21:59 -0700415 EGLBoolean res = eglSwapBuffers(mDisplay, mSurface);
416 if (res == EGL_FALSE)
417 break;
418
Mathias Agopian13796652009-03-24 22:49:21 -0700419 // 12fps: don't animate too fast to preserve CPU
420 const nsecs_t sleepTime = 83333 - ns2us(systemTime() - now);
421 if (sleepTime > 0)
Mathias Agopiana8826d62009-10-01 03:10:14 -0700422 usleep(sleepTime);
Kevin Hesterd3782b22012-04-26 10:38:55 -0700423
424 checkExit();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800425 } while (!exitPending());
426
427 glDeleteTextures(1, &mAndroid[0].name);
428 glDeleteTextures(1, &mAndroid[1].name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800429 return false;
430}
431
Kevin Hesterd3782b22012-04-26 10:38:55 -0700432void BootAnimation::checkExit() {
433 // Allow surface flinger to gracefully request shutdown
434 char value[PROPERTY_VALUE_MAX];
435 property_get(EXIT_PROP_NAME, value, "0");
436 int exitnow = atoi(value);
437 if (exitnow) {
438 requestExit();
Ed Coyne7464ac92017-06-08 12:26:48 -0700439 mCallbacks->shutdown();
Kevin Hesterd3782b22012-04-26 10:38:55 -0700440 }
441}
442
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700443bool BootAnimation::validClock(const Animation::Part& part) {
444 return part.clockPosX != TEXT_MISSING_VALUE && part.clockPosY != TEXT_MISSING_VALUE;
445}
446
447bool parseTextCoord(const char* str, int* dest) {
448 if (strcmp("c", str) == 0) {
449 *dest = TEXT_CENTER_VALUE;
450 return true;
451 }
452
453 char* end;
454 int val = (int) strtol(str, &end, 0);
455 if (end == str || *end != '\0' || val == INT_MAX || val == INT_MIN) {
456 return false;
457 }
458 *dest = val;
459 return true;
460}
461
462// Parse two position coordinates. If only string is non-empty, treat it as the y value.
463void parsePosition(const char* str1, const char* str2, int* x, int* y) {
464 bool success = false;
465 if (strlen(str1) == 0) { // No values were specified
466 // success = false
467 } else if (strlen(str2) == 0) { // we have only one value
468 if (parseTextCoord(str1, y)) {
469 *x = TEXT_CENTER_VALUE;
470 success = true;
471 }
472 } else {
473 if (parseTextCoord(str1, x) && parseTextCoord(str2, y)) {
474 success = true;
475 }
476 }
477
478 if (!success) {
479 *x = TEXT_MISSING_VALUE;
480 *y = TEXT_MISSING_VALUE;
481 }
482}
483
Jesse Hall083b84c2014-09-22 10:51:09 -0700484// Parse a color represented as an HTML-style 'RRGGBB' string: each pair of
485// characters in str is a hex number in [0, 255], which are converted to
486// floating point values in the range [0.0, 1.0] and placed in the
487// corresponding elements of color.
488//
489// If the input string isn't valid, parseColor returns false and color is
490// left unchanged.
491static bool parseColor(const char str[7], float color[3]) {
492 float tmpColor[3];
493 for (int i = 0; i < 3; i++) {
494 int val = 0;
495 for (int j = 0; j < 2; j++) {
496 val *= 16;
497 char c = str[2*i + j];
498 if (c >= '0' && c <= '9') val += c - '0';
499 else if (c >= 'A' && c <= 'F') val += (c - 'A') + 10;
500 else if (c >= 'a' && c <= 'f') val += (c - 'a') + 10;
501 else return false;
502 }
503 tmpColor[i] = static_cast<float>(val) / 255.0f;
504 }
505 memcpy(color, tmpColor, sizeof(tmpColor));
506 return true;
507}
508
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700509
510static bool readFile(ZipFileRO* zip, const char* name, String8& outString)
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700511{
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700512 ZipEntryRO entry = zip->findEntryByName(name);
Wei Wang159a4102018-10-23 23:15:58 -0700513 SLOGE_IF(!entry, "couldn't find %s", name);
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700514 if (!entry) {
515 return false;
516 }
517
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700518 FileMap* entryMap = zip->createEntryFileMap(entry);
519 zip->releaseEntry(entry);
Wei Wang159a4102018-10-23 23:15:58 -0700520 SLOGE_IF(!entryMap, "entryMap is null");
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700521 if (!entryMap) {
522 return false;
523 }
524
525 outString.setTo((char const*)entryMap->getDataPtr(), entryMap->getDataLength());
Narayan Kamath688ff4c2015-02-23 15:47:54 +0000526 delete entryMap;
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700527 return true;
528}
529
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700530// The font image should be a 96x2 array of character images. The
531// columns are the printable ASCII characters 0x20 - 0x7f. The
532// top row is regular text; the bottom row is bold.
533status_t BootAnimation::initFont(Font* font, const char* fallback) {
534 status_t status = NO_ERROR;
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800535
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700536 if (font->map != nullptr) {
537 glGenTextures(1, &font->texture.name);
538 glBindTexture(GL_TEXTURE_2D, font->texture.name);
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800539
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700540 status = initTexture(font->map, &font->texture.w, &font->texture.h);
541
542 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
543 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
544 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
545 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
546 } else if (fallback != nullptr) {
547 status = initTexture(&font->texture, mAssets, fallback);
548 } else {
549 return NO_INIT;
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800550 }
551
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700552 if (status == NO_ERROR) {
553 font->char_width = font->texture.w / FONT_NUM_COLS;
554 font->char_height = font->texture.h / FONT_NUM_ROWS / 2; // There are bold and regular rows
555 }
556
557 return status;
558}
559
560void BootAnimation::drawText(const char* str, const Font& font, bool bold, int* x, int* y) {
561 glEnable(GL_BLEND); // Allow us to draw on top of the animation
562 glBindTexture(GL_TEXTURE_2D, font.texture.name);
563
564 const int len = strlen(str);
565 const int strWidth = font.char_width * len;
566
567 if (*x == TEXT_CENTER_VALUE) {
568 *x = (mWidth - strWidth) / 2;
569 } else if (*x < 0) {
570 *x = mWidth + *x - strWidth;
571 }
572 if (*y == TEXT_CENTER_VALUE) {
573 *y = (mHeight - font.char_height) / 2;
574 } else if (*y < 0) {
575 *y = mHeight + *y - font.char_height;
576 }
577
578 int cropRect[4] = { 0, 0, font.char_width, -font.char_height };
579
580 for (int i = 0; i < len; i++) {
581 char c = str[i];
582
583 if (c < FONT_BEGIN_CHAR || c > FONT_END_CHAR) {
584 c = '?';
585 }
586
587 // Crop the texture to only the pixels in the current glyph
588 const int charPos = (c - FONT_BEGIN_CHAR); // Position in the list of valid characters
589 const int row = charPos / FONT_NUM_COLS;
590 const int col = charPos % FONT_NUM_COLS;
591 cropRect[0] = col * font.char_width; // Left of column
592 cropRect[1] = row * font.char_height * 2; // Top of row
593 // Move down to bottom of regular (one char_heigh) or bold (two char_heigh) line
594 cropRect[1] += bold ? 2 * font.char_height : font.char_height;
595 glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_CROP_RECT_OES, cropRect);
596
597 glDrawTexiOES(*x, *y, 0, font.char_width, font.char_height);
598
599 *x += font.char_width;
600 }
601
602 glDisable(GL_BLEND); // Return to the animation's default behaviour
603 glBindTexture(GL_TEXTURE_2D, 0);
604}
605
Damien Bargiacchi9071db12016-10-28 17:38:22 -0700606// We render 12 or 24 hour time.
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700607void BootAnimation::drawClock(const Font& font, const int xPos, const int yPos) {
Damien Bargiacchi9071db12016-10-28 17:38:22 -0700608 static constexpr char TIME_FORMAT_12[] = "%l:%M";
609 static constexpr char TIME_FORMAT_24[] = "%H:%M";
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700610 static constexpr int TIME_LENGTH = 6;
611
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800612 time_t rawtime;
613 time(&rawtime);
614 struct tm* timeInfo = localtime(&rawtime);
615
616 char timeBuff[TIME_LENGTH];
Damien Bargiacchi9071db12016-10-28 17:38:22 -0700617 const char* timeFormat = mTimeFormat12Hour ? TIME_FORMAT_12 : TIME_FORMAT_24;
618 size_t length = strftime(timeBuff, TIME_LENGTH, timeFormat, timeInfo);
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800619
620 if (length != TIME_LENGTH - 1) {
Wei Wang159a4102018-10-23 23:15:58 -0700621 SLOGE("Couldn't format time; abandoning boot animation clock");
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800622 mClockEnabled = false;
623 return;
624 }
625
Damien Bargiacchi45a76442016-12-05 18:02:18 -0800626 char* out = timeBuff[0] == ' ' ? &timeBuff[1] : &timeBuff[0];
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700627 int x = xPos;
628 int y = yPos;
Damien Bargiacchi45a76442016-12-05 18:02:18 -0800629 drawText(out, font, false, &x, &y);
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800630}
631
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700632bool BootAnimation::parseAnimationDesc(Animation& animation)
Mathias Agopiana8826d62009-10-01 03:10:14 -0700633{
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700634 String8 desString;
635
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700636 if (!readFile(animation.zip, "desc.txt", desString)) {
Narayan Kamathafd31e02013-12-03 13:16:03 +0000637 return false;
638 }
Mathias Agopiana8826d62009-10-01 03:10:14 -0700639 char const* s = desString.string();
640
Mathias Agopiana8826d62009-10-01 03:10:14 -0700641 // Parse the description file
642 for (;;) {
643 const char* endl = strstr(s, "\n");
Andreas Gampecfedceb2014-09-30 21:48:18 -0700644 if (endl == NULL) break;
Mathias Agopiana8826d62009-10-01 03:10:14 -0700645 String8 line(s, endl - s);
646 const char* l = line.string();
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800647 int fps = 0;
648 int width = 0;
649 int height = 0;
650 int count = 0;
651 int pause = 0;
Narayan Kamathafd31e02013-12-03 13:16:03 +0000652 char path[ANIM_ENTRY_NAME_MAX];
Jesse Hall083b84c2014-09-22 10:51:09 -0700653 char color[7] = "000000"; // default to black if unspecified
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700654 char clockPos1[TEXT_POS_LEN_MAX + 1] = "";
655 char clockPos2[TEXT_POS_LEN_MAX + 1] = "";
Jesse Hall083b84c2014-09-22 10:51:09 -0700656
Kevin Hesterd3782b22012-04-26 10:38:55 -0700657 char pathType;
Mathias Agopiana8826d62009-10-01 03:10:14 -0700658 if (sscanf(l, "%d %d %d", &width, &height, &fps) == 3) {
Wei Wang159a4102018-10-23 23:15:58 -0700659 // SLOGD("> w=%d, h=%d, fps=%d", width, height, fps);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700660 animation.width = width;
661 animation.height = height;
662 animation.fps = fps;
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700663 } else if (sscanf(l, " %c %d %d %s #%6s %16s %16s",
664 &pathType, &count, &pause, path, color, clockPos1, clockPos2) >= 4) {
Wei Wang159a4102018-10-23 23:15:58 -0700665 //SLOGD("> type=%c, count=%d, pause=%d, path=%s, color=%s, clockPos1=%s, clockPos2=%s",
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700666 // pathType, count, pause, path, color, clockPos1, clockPos2);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700667 Animation::Part part;
Kevin Hesterd3782b22012-04-26 10:38:55 -0700668 part.playUntilComplete = pathType == 'c';
Mathias Agopiana8826d62009-10-01 03:10:14 -0700669 part.count = count;
670 part.pause = pause;
671 part.path = path;
Geoffrey Pitschd6d9a1d2016-06-08 00:38:58 -0700672 part.audioData = NULL;
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700673 part.animation = NULL;
Jesse Hall083b84c2014-09-22 10:51:09 -0700674 if (!parseColor(color, part.backgroundColor)) {
Wei Wang159a4102018-10-23 23:15:58 -0700675 SLOGE("> invalid color '#%s'", color);
Jesse Hall083b84c2014-09-22 10:51:09 -0700676 part.backgroundColor[0] = 0.0f;
677 part.backgroundColor[1] = 0.0f;
678 part.backgroundColor[2] = 0.0f;
679 }
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700680 parsePosition(clockPos1, clockPos2, &part.clockPosX, &part.clockPosY);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700681 animation.parts.add(part);
682 }
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700683 else if (strcmp(l, "$SYSTEM") == 0) {
Wei Wang159a4102018-10-23 23:15:58 -0700684 // SLOGD("> SYSTEM");
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700685 Animation::Part part;
686 part.playUntilComplete = false;
687 part.count = 1;
688 part.pause = 0;
Geoffrey Pitschd6d9a1d2016-06-08 00:38:58 -0700689 part.audioData = NULL;
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700690 part.animation = loadAnimation(String8(SYSTEM_BOOTANIMATION_FILE));
691 if (part.animation != NULL)
692 animation.parts.add(part);
693 }
Mathias Agopiana8826d62009-10-01 03:10:14 -0700694 s = ++endl;
695 }
696
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700697 return true;
698}
699
700bool BootAnimation::preloadZip(Animation& animation)
701{
Mathias Agopiana8826d62009-10-01 03:10:14 -0700702 // read all the data structures
703 const size_t pcount = animation.parts.size();
Narayan Kamathafd31e02013-12-03 13:16:03 +0000704 void *cookie = NULL;
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400705 ZipFileRO* zip = animation.zip;
706 if (!zip->startIteration(&cookie)) {
Narayan Kamathafd31e02013-12-03 13:16:03 +0000707 return false;
708 }
709
710 ZipEntryRO entry;
711 char name[ANIM_ENTRY_NAME_MAX];
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400712 while ((entry = zip->nextEntry(cookie)) != NULL) {
713 const int foundEntryName = zip->getEntryFileName(entry, name, ANIM_ENTRY_NAME_MAX);
Narayan Kamathafd31e02013-12-03 13:16:03 +0000714 if (foundEntryName > ANIM_ENTRY_NAME_MAX || foundEntryName == -1) {
Wei Wang159a4102018-10-23 23:15:58 -0700715 SLOGE("Error fetching entry file name");
Narayan Kamathafd31e02013-12-03 13:16:03 +0000716 continue;
717 }
718
719 const String8 entryName(name);
720 const String8 path(entryName.getPathDir());
721 const String8 leaf(entryName.getPathLeaf());
722 if (leaf.size() > 0) {
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700723 if (entryName == CLOCK_FONT_ZIP_NAME) {
724 FileMap* map = zip->createEntryFileMap(entry);
725 if (map) {
726 animation.clockFont.map = map;
727 }
728 continue;
729 }
730
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400731 for (size_t j = 0; j < pcount; j++) {
Narayan Kamathafd31e02013-12-03 13:16:03 +0000732 if (path == animation.parts[j].path) {
Narayan Kamath4600dd02015-06-16 12:02:57 +0100733 uint16_t method;
Narayan Kamathafd31e02013-12-03 13:16:03 +0000734 // supports only stored png files
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400735 if (zip->getEntryInfo(entry, &method, NULL, NULL, NULL, NULL, NULL)) {
Narayan Kamathafd31e02013-12-03 13:16:03 +0000736 if (method == ZipFileRO::kCompressStored) {
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400737 FileMap* map = zip->createEntryFileMap(entry);
Narayan Kamathafd31e02013-12-03 13:16:03 +0000738 if (map) {
Narayan Kamathafd31e02013-12-03 13:16:03 +0000739 Animation::Part& part(animation.parts.editItemAt(j));
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700740 if (leaf == "audio.wav") {
741 // a part may have at most one audio file
Geoffrey Pitschd6d9a1d2016-06-08 00:38:58 -0700742 part.audioData = (uint8_t *)map->getDataPtr();
743 part.audioLength = map->getDataLength();
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400744 } else if (leaf == "trim.txt") {
745 part.trimData.setTo((char const*)map->getDataPtr(),
746 map->getDataLength());
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700747 } else {
748 Animation::Frame frame;
749 frame.name = leaf;
750 frame.map = map;
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400751 frame.trimWidth = animation.width;
752 frame.trimHeight = animation.height;
753 frame.trimX = 0;
754 frame.trimY = 0;
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700755 part.frames.add(frame);
756 }
Mathias Agopiana8826d62009-10-01 03:10:14 -0700757 }
Geoffrey Pitschd6d9a1d2016-06-08 00:38:58 -0700758 } else {
Wei Wang159a4102018-10-23 23:15:58 -0700759 SLOGE("bootanimation.zip is compressed; must be only stored");
Mathias Agopiana8826d62009-10-01 03:10:14 -0700760 }
761 }
762 }
763 }
764 }
765 }
766
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400767 // If there is trimData present, override the positioning defaults.
768 for (Animation::Part& part : animation.parts) {
769 const char* trimDataStr = part.trimData.string();
770 for (size_t frameIdx = 0; frameIdx < part.frames.size(); frameIdx++) {
771 const char* endl = strstr(trimDataStr, "\n");
772 // No more trimData for this part.
773 if (endl == NULL) {
774 break;
775 }
776 String8 line(trimDataStr, endl - trimDataStr);
777 const char* lineStr = line.string();
778 trimDataStr = ++endl;
779 int width = 0, height = 0, x = 0, y = 0;
780 if (sscanf(lineStr, "%dx%d+%d+%d", &width, &height, &x, &y) == 4) {
781 Animation::Frame& frame(part.frames.editItemAt(frameIdx));
782 frame.trimWidth = width;
783 frame.trimHeight = height;
784 frame.trimX = x;
785 frame.trimY = y;
786 } else {
Wei Wang159a4102018-10-23 23:15:58 -0700787 SLOGE("Error parsing trim.txt, line: %s", lineStr);
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400788 break;
789 }
790 }
791 }
792
Ed Coyne7464ac92017-06-08 12:26:48 -0700793 mCallbacks->init(animation.parts);
Geoffrey Pitschd6d9a1d2016-06-08 00:38:58 -0700794
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400795 zip->endIteration(cookie);
Narayan Kamathafd31e02013-12-03 13:16:03 +0000796
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700797 return true;
798}
799
800bool BootAnimation::movie()
801{
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700802 Animation* animation = loadAnimation(mZipFileName);
803 if (animation == NULL)
804 return false;
805
Damien Bargiacchi97480862016-03-29 14:55:55 -0700806 bool anyPartHasClock = false;
807 for (size_t i=0; i < animation->parts.size(); i++) {
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700808 if(validClock(animation->parts[i])) {
Damien Bargiacchi97480862016-03-29 14:55:55 -0700809 anyPartHasClock = true;
810 break;
811 }
812 }
813 if (!anyPartHasClock) {
814 mClockEnabled = false;
815 }
816
Sai Kiran Korwar27167492015-07-07 20:00:06 +0530817 // Check if npot textures are supported
818 mUseNpotTextures = false;
819 String8 gl_extensions;
820 const char* exts = reinterpret_cast<const char*>(glGetString(GL_EXTENSIONS));
821 if (!exts) {
822 glGetError();
823 } else {
824 gl_extensions.setTo(exts);
825 if ((gl_extensions.find("GL_ARB_texture_non_power_of_two") != -1) ||
826 (gl_extensions.find("GL_OES_texture_npot") != -1)) {
827 mUseNpotTextures = true;
828 }
829 }
830
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800831 // Blend required to draw time on top of animation frames.
832 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700833 glShadeModel(GL_FLAT);
834 glDisable(GL_DITHER);
835 glDisable(GL_SCISSOR_TEST);
836 glDisable(GL_BLEND);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700837
838 glBindTexture(GL_TEXTURE_2D, 0);
839 glEnable(GL_TEXTURE_2D);
840 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
841 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
842 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
843 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
844 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
845
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700846 bool clockFontInitialized = false;
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800847 if (mClockEnabled) {
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700848 clockFontInitialized =
849 (initFont(&animation->clockFont, CLOCK_FONT_ASSET) == NO_ERROR);
850 mClockEnabled = clockFontInitialized;
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800851 }
852
Damien Bargiacchi97480862016-03-29 14:55:55 -0700853 if (mClockEnabled && !updateIsTimeAccurate()) {
854 mTimeCheckThread = new TimeCheckThread(this);
855 mTimeCheckThread->run("BootAnimation::TimeCheckThread", PRIORITY_NORMAL);
856 }
857
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700858 playAnimation(*animation);
Damien Bargiacchi97480862016-03-29 14:55:55 -0700859
Geoffrey Pitscha9173532016-07-19 14:56:39 -0400860 if (mTimeCheckThread != nullptr) {
Damien Bargiacchi97480862016-03-29 14:55:55 -0700861 mTimeCheckThread->requestExit();
Geoffrey Pitscha9173532016-07-19 14:56:39 -0400862 mTimeCheckThread = nullptr;
863 }
864
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700865 if (clockFontInitialized) {
866 glDeleteTextures(1, &animation->clockFont.texture.name);
Andriy Naborskyy815e51d2016-03-24 16:43:34 -0700867 }
868
Damien Bargiacchi1a8a2132018-07-24 15:20:26 -0700869 releaseAnimation(animation);
870
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700871 return false;
872}
873
874bool BootAnimation::playAnimation(const Animation& animation)
875{
876 const size_t pcount = animation.parts.size();
Mathias Agopiana8826d62009-10-01 03:10:14 -0700877 nsecs_t frameDuration = s2ns(1) / animation.fps;
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400878 const int animationX = (mWidth - animation.width) / 2;
879 const int animationY = (mHeight - animation.height) / 2;
Mathias Agopian9f3020d2009-11-06 16:30:18 -0800880
Wei Wang159a4102018-10-23 23:15:58 -0700881 SLOGD("%sAnimationShownTiming start time: %" PRId64 "ms", mShuttingDown ? "Shutdown" : "Boot",
Keun-young Parkd1794cd2017-04-04 12:21:19 -0700882 elapsedRealtime());
Narayan Kamathafd31e02013-12-03 13:16:03 +0000883 for (size_t i=0 ; i<pcount ; i++) {
Mathias Agopiana8826d62009-10-01 03:10:14 -0700884 const Animation::Part& part(animation.parts[i]);
885 const size_t fcount = part.frames.size();
886 glBindTexture(GL_TEXTURE_2D, 0);
887
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700888 // Handle animation package
889 if (part.animation != NULL) {
890 playAnimation(*part.animation);
891 if (exitPending())
892 break;
893 continue; //to next part
894 }
895
Mathias Agopiana8826d62009-10-01 03:10:14 -0700896 for (int r=0 ; !part.count || r<part.count ; r++) {
Kevin Hesterd3782b22012-04-26 10:38:55 -0700897 // Exit any non playuntil complete parts immediately
898 if(exitPending() && !part.playUntilComplete)
899 break;
900
Ed Coyne7464ac92017-06-08 12:26:48 -0700901 mCallbacks->playPart(i, part, r);
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700902
Jesse Hall083b84c2014-09-22 10:51:09 -0700903 glClearColor(
904 part.backgroundColor[0],
905 part.backgroundColor[1],
906 part.backgroundColor[2],
907 1.0f);
908
Narayan Kamathafd31e02013-12-03 13:16:03 +0000909 for (size_t j=0 ; j<fcount && (!exitPending() || part.playUntilComplete) ; j++) {
Mathias Agopiana8826d62009-10-01 03:10:14 -0700910 const Animation::Frame& frame(part.frames[j]);
Mathias Agopiandb7dd2a2012-05-12 15:08:21 -0700911 nsecs_t lastFrame = systemTime();
Mathias Agopiana8826d62009-10-01 03:10:14 -0700912
913 if (r > 0) {
914 glBindTexture(GL_TEXTURE_2D, frame.tid);
915 } else {
916 if (part.count != 1) {
917 glGenTextures(1, &frame.tid);
918 glBindTexture(GL_TEXTURE_2D, frame.tid);
919 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
920 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
921 }
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700922 int w, h;
923 initTexture(frame.map, &w, &h);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700924 }
925
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400926 const int xc = animationX + frame.trimX;
927 const int yc = animationY + frame.trimY;
928 Region clearReg(Rect(mWidth, mHeight));
929 clearReg.subtractSelf(Rect(xc, yc, xc+frame.trimWidth, yc+frame.trimHeight));
Mathias Agopian9f3020d2009-11-06 16:30:18 -0800930 if (!clearReg.isEmpty()) {
931 Region::const_iterator head(clearReg.begin());
932 Region::const_iterator tail(clearReg.end());
933 glEnable(GL_SCISSOR_TEST);
934 while (head != tail) {
Andreas Gampecfedceb2014-09-30 21:48:18 -0700935 const Rect& r2(*head++);
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400936 glScissor(r2.left, mHeight - r2.bottom, r2.width(), r2.height());
Mathias Agopian9f3020d2009-11-06 16:30:18 -0800937 glClear(GL_COLOR_BUFFER_BIT);
938 }
939 glDisable(GL_SCISSOR_TEST);
940 }
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400941 // specify the y center as ceiling((mHeight - frame.trimHeight) / 2)
942 // which is equivalent to mHeight - (yc + frame.trimHeight)
943 glDrawTexiOES(xc, mHeight - (yc + frame.trimHeight),
944 0, frame.trimWidth, frame.trimHeight);
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700945 if (mClockEnabled && mTimeIsAccurate && validClock(part)) {
946 drawClock(animation.clockFont, part.clockPosX, part.clockPosY);
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800947 }
Adrian Roos9ee5dff2018-08-22 20:19:49 +0200948 handleViewport(frameDuration);
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800949
Mathias Agopiana8826d62009-10-01 03:10:14 -0700950 eglSwapBuffers(mDisplay, mSurface);
951
952 nsecs_t now = systemTime();
953 nsecs_t delay = frameDuration - (now - lastFrame);
Wei Wang159a4102018-10-23 23:15:58 -0700954 //SLOGD("%lld, %lld", ns2ms(now - lastFrame), ns2ms(delay));
Mathias Agopiana8826d62009-10-01 03:10:14 -0700955 lastFrame = now;
Mathias Agopiandb7dd2a2012-05-12 15:08:21 -0700956
957 if (delay > 0) {
958 struct timespec spec;
959 spec.tv_sec = (now + delay) / 1000000000;
960 spec.tv_nsec = (now + delay) % 1000000000;
961 int err;
962 do {
963 err = clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &spec, NULL);
964 } while (err<0 && errno == EINTR);
965 }
Kevin Hesterd3782b22012-04-26 10:38:55 -0700966
967 checkExit();
Mathias Agopiana8826d62009-10-01 03:10:14 -0700968 }
Kevin Hesterd3782b22012-04-26 10:38:55 -0700969
Mathias Agopiana8826d62009-10-01 03:10:14 -0700970 usleep(part.pause * ns2us(frameDuration));
Kevin Hesterd3782b22012-04-26 10:38:55 -0700971
972 // For infinite parts, we've now played them at least once, so perhaps exit
Adrian Roos9ee5dff2018-08-22 20:19:49 +0200973 if(exitPending() && !part.count && mCurrentInset >= mTargetInset)
Kevin Hesterd3782b22012-04-26 10:38:55 -0700974 break;
Mathias Agopiana8826d62009-10-01 03:10:14 -0700975 }
976
Geoffrey Pitsch2fb30fb2016-07-06 16:16:20 -0400977 }
978
979 // Free textures created for looping parts now that the animation is done.
980 for (const Animation::Part& part : animation.parts) {
Mathias Agopiana8826d62009-10-01 03:10:14 -0700981 if (part.count != 1) {
Geoffrey Pitsch2fb30fb2016-07-06 16:16:20 -0400982 const size_t fcount = part.frames.size();
983 for (size_t j = 0; j < fcount; j++) {
Mathias Agopiana8826d62009-10-01 03:10:14 -0700984 const Animation::Frame& frame(part.frames[j]);
985 glDeleteTextures(1, &frame.tid);
986 }
987 }
988 }
Geoffrey Pitschd6d9a1d2016-06-08 00:38:58 -0700989
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700990 return true;
Mathias Agopiana8826d62009-10-01 03:10:14 -0700991}
992
Adrian Roos9ee5dff2018-08-22 20:19:49 +0200993void BootAnimation::handleViewport(nsecs_t timestep) {
994 if (mShuttingDown || !mFlingerSurfaceControl || mTargetInset == 0) {
995 return;
996 }
997 if (mTargetInset < 0) {
998 // Poll the amount for the top display inset. This will return -1 until persistent properties
999 // have been loaded.
1000 mTargetInset = android::base::GetIntProperty("persist.sys.displayinset.top",
1001 -1 /* default */, -1 /* min */, mHeight / 2 /* max */);
1002 }
1003 if (mTargetInset <= 0) {
1004 return;
1005 }
1006
1007 if (mCurrentInset < mTargetInset) {
1008 // After the device boots, the inset will effectively be cropped away. We animate this here.
1009 float fraction = static_cast<float>(mCurrentInset) / mTargetInset;
1010 int interpolatedInset = (cosf((fraction + 1) * M_PI) / 2.0f + 0.5f) * mTargetInset;
1011
1012 SurfaceComposerClient::Transaction()
1013 .setCrop(mFlingerSurfaceControl, Rect(0, interpolatedInset, mWidth, mHeight))
1014 .apply();
1015 } else {
1016 // At the end of the animation, we switch to the viewport that DisplayManager will apply
1017 // later. This changes the coordinate system, and means we must move the surface up by
1018 // the inset amount.
Adrian Roos9ee5dff2018-08-22 20:19:49 +02001019 Rect layerStackRect(0, 0, mWidth, mHeight - mTargetInset);
1020 Rect displayRect(0, mTargetInset, mWidth, mHeight);
1021
1022 SurfaceComposerClient::Transaction t;
1023 t.setPosition(mFlingerSurfaceControl, 0, -mTargetInset)
1024 .setCrop(mFlingerSurfaceControl, Rect(0, mTargetInset, mWidth, mHeight));
Dominik Laskowski3316a0a2019-01-25 02:56:41 -08001025 t.setDisplayProjection(mDisplayToken, 0 /* orientation */, layerStackRect, displayRect);
Adrian Roos9ee5dff2018-08-22 20:19:49 +02001026 t.apply();
1027
1028 mTargetInset = mCurrentInset = 0;
1029 }
1030
1031 int delta = timestep * mTargetInset / ms2ns(200);
1032 mCurrentInset += delta;
1033}
1034
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001035void BootAnimation::releaseAnimation(Animation* animation) const
1036{
1037 for (Vector<Animation::Part>::iterator it = animation->parts.begin(),
1038 e = animation->parts.end(); it != e; ++it) {
1039 if (it->animation)
1040 releaseAnimation(it->animation);
1041 }
1042 if (animation->zip)
1043 delete animation->zip;
1044 delete animation;
1045}
1046
1047BootAnimation::Animation* BootAnimation::loadAnimation(const String8& fn)
1048{
1049 if (mLoadedFiles.indexOf(fn) >= 0) {
Wei Wang159a4102018-10-23 23:15:58 -07001050 SLOGE("File \"%s\" is already loaded. Cyclic ref is not allowed",
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001051 fn.string());
1052 return NULL;
1053 }
1054 ZipFileRO *zip = ZipFileRO::open(fn);
1055 if (zip == NULL) {
Wei Wang159a4102018-10-23 23:15:58 -07001056 SLOGE("Failed to open animation zip \"%s\": %s",
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001057 fn.string(), strerror(errno));
1058 return NULL;
1059 }
1060
1061 Animation *animation = new Animation;
1062 animation->fileName = fn;
1063 animation->zip = zip;
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -07001064 animation->clockFont.map = nullptr;
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001065 mLoadedFiles.add(animation->fileName);
1066
1067 parseAnimationDesc(*animation);
Geoffrey Pitscha91a2d72016-07-12 14:46:19 -04001068 if (!preloadZip(*animation)) {
1069 return NULL;
1070 }
1071
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001072
1073 mLoadedFiles.remove(fn);
1074 return animation;
1075}
Damien Bargiacchi97480862016-03-29 14:55:55 -07001076
1077bool BootAnimation::updateIsTimeAccurate() {
1078 static constexpr long long MAX_TIME_IN_PAST = 60000LL * 60LL * 24LL * 30LL; // 30 days
1079 static constexpr long long MAX_TIME_IN_FUTURE = 60000LL * 90LL; // 90 minutes
1080
1081 if (mTimeIsAccurate) {
1082 return true;
1083 }
Keun-young Parkb5938422017-03-23 13:46:24 -07001084 if (mShuttingDown) return true;
Damien Bargiacchi97480862016-03-29 14:55:55 -07001085 struct stat statResult;
Damien Bargiacchi9071db12016-10-28 17:38:22 -07001086
1087 if(stat(TIME_FORMAT_12_HOUR_FLAG_FILE_PATH, &statResult) == 0) {
1088 mTimeFormat12Hour = true;
1089 }
1090
Damien Bargiacchi97480862016-03-29 14:55:55 -07001091 if(stat(ACCURATE_TIME_FLAG_FILE_PATH, &statResult) == 0) {
1092 mTimeIsAccurate = true;
1093 return true;
1094 }
1095
1096 FILE* file = fopen(LAST_TIME_CHANGED_FILE_PATH, "r");
1097 if (file != NULL) {
1098 long long lastChangedTime = 0;
1099 fscanf(file, "%lld", &lastChangedTime);
1100 fclose(file);
1101 if (lastChangedTime > 0) {
1102 struct timespec now;
1103 clock_gettime(CLOCK_REALTIME, &now);
1104 // Match the Java timestamp format
1105 long long rtcNow = (now.tv_sec * 1000LL) + (now.tv_nsec / 1000000LL);
Damien Bargiacchi96762812016-07-12 15:53:40 -07001106 if (ACCURATE_TIME_EPOCH < rtcNow
1107 && lastChangedTime > (rtcNow - MAX_TIME_IN_PAST)
1108 && lastChangedTime < (rtcNow + MAX_TIME_IN_FUTURE)) {
Damien Bargiacchi97480862016-03-29 14:55:55 -07001109 mTimeIsAccurate = true;
1110 }
1111 }
1112 }
1113
1114 return mTimeIsAccurate;
1115}
1116
1117BootAnimation::TimeCheckThread::TimeCheckThread(BootAnimation* bootAnimation) : Thread(false),
1118 mInotifyFd(-1), mSystemWd(-1), mTimeWd(-1), mBootAnimation(bootAnimation) {}
1119
1120BootAnimation::TimeCheckThread::~TimeCheckThread() {
1121 // mInotifyFd may be -1 but that's ok since we're not at risk of attempting to close a valid FD.
1122 close(mInotifyFd);
1123}
1124
1125bool BootAnimation::TimeCheckThread::threadLoop() {
1126 bool shouldLoop = doThreadLoop() && !mBootAnimation->mTimeIsAccurate
1127 && mBootAnimation->mClockEnabled;
1128 if (!shouldLoop) {
1129 close(mInotifyFd);
1130 mInotifyFd = -1;
1131 }
1132 return shouldLoop;
1133}
1134
1135bool BootAnimation::TimeCheckThread::doThreadLoop() {
1136 static constexpr int BUFF_LEN (10 * (sizeof(struct inotify_event) + NAME_MAX + 1));
1137
1138 // Poll instead of doing a blocking read so the Thread can exit if requested.
1139 struct pollfd pfd = { mInotifyFd, POLLIN, 0 };
1140 ssize_t pollResult = poll(&pfd, 1, 1000);
1141
1142 if (pollResult == 0) {
1143 return true;
1144 } else if (pollResult < 0) {
Wei Wang159a4102018-10-23 23:15:58 -07001145 SLOGE("Could not poll inotify events");
Damien Bargiacchi97480862016-03-29 14:55:55 -07001146 return false;
1147 }
1148
1149 char buff[BUFF_LEN] __attribute__ ((aligned(__alignof__(struct inotify_event))));;
1150 ssize_t length = read(mInotifyFd, buff, BUFF_LEN);
1151 if (length == 0) {
1152 return true;
1153 } else if (length < 0) {
Wei Wang159a4102018-10-23 23:15:58 -07001154 SLOGE("Could not read inotify events");
Damien Bargiacchi97480862016-03-29 14:55:55 -07001155 return false;
1156 }
1157
1158 const struct inotify_event *event;
1159 for (char* ptr = buff; ptr < buff + length; ptr += sizeof(struct inotify_event) + event->len) {
1160 event = (const struct inotify_event *) ptr;
1161 if (event->wd == mSystemWd && strcmp(SYSTEM_TIME_DIR_NAME, event->name) == 0) {
1162 addTimeDirWatch();
1163 } else if (event->wd == mTimeWd && (strcmp(LAST_TIME_CHANGED_FILE_NAME, event->name) == 0
1164 || strcmp(ACCURATE_TIME_FLAG_FILE_NAME, event->name) == 0)) {
1165 return !mBootAnimation->updateIsTimeAccurate();
1166 }
1167 }
1168
1169 return true;
1170}
1171
1172void BootAnimation::TimeCheckThread::addTimeDirWatch() {
1173 mTimeWd = inotify_add_watch(mInotifyFd, SYSTEM_TIME_DIR_PATH,
1174 IN_CLOSE_WRITE | IN_MOVED_TO | IN_ATTRIB);
1175 if (mTimeWd > 0) {
1176 // No need to watch for the time directory to be created if it already exists
1177 inotify_rm_watch(mInotifyFd, mSystemWd);
1178 mSystemWd = -1;
1179 }
1180}
1181
1182status_t BootAnimation::TimeCheckThread::readyToRun() {
1183 mInotifyFd = inotify_init();
1184 if (mInotifyFd < 0) {
Wei Wang159a4102018-10-23 23:15:58 -07001185 SLOGE("Could not initialize inotify fd");
Damien Bargiacchi97480862016-03-29 14:55:55 -07001186 return NO_INIT;
1187 }
1188
1189 mSystemWd = inotify_add_watch(mInotifyFd, SYSTEM_DATA_DIR_PATH, IN_CREATE | IN_ATTRIB);
1190 if (mSystemWd < 0) {
1191 close(mInotifyFd);
1192 mInotifyFd = -1;
Wei Wang159a4102018-10-23 23:15:58 -07001193 SLOGE("Could not add watch for %s", SYSTEM_DATA_DIR_PATH);
Damien Bargiacchi97480862016-03-29 14:55:55 -07001194 return NO_INIT;
1195 }
1196
1197 addTimeDirWatch();
1198
1199 if (mBootAnimation->updateIsTimeAccurate()) {
1200 close(mInotifyFd);
1201 mInotifyFd = -1;
1202 return ALREADY_EXISTS;
1203 }
1204
1205 return NO_ERROR;
1206}
1207
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001208// ---------------------------------------------------------------------------
1209
1210}
1211; // namespace android