blob: 54785ca131859d1e3c4e26221e0af502a601554f [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
Jason parksbd9a08d2011-01-31 15:04:34 -060032#include <cutils/properties.h>
33
Mathias Agopianb13b9bd2012-02-17 18:27:36 -080034#include <androidfw/AssetManager.h>
Mathias Agopianac31a3b2009-05-21 19:59:24 -070035#include <binder/IPCThreadState.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080036#include <utils/Atomic.h>
37#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);
Steve Block3762c312012-01-06 19:20:56 +0000117 ALOGE_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!
Steve Block5baa3a62011-12-20 16:23:08 +0000131 ALOGD("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;
305
Elliott Hughesc367d482013-10-29 13:12:55 -0700306 // If the device has encryption turned on or is in process
Jason parksbd9a08d2011-01-31 15:04:34 -0600307 // of being encrypted we show the encrypted boot animation.
308 char decrypt[PROPERTY_VALUE_MAX];
309 property_get("vold.decrypt", decrypt, "");
310
Keun-young Parkb5938422017-03-23 13:46:24 -0700311 bool encryptedAnimation = atoi(decrypt) != 0 ||
312 !strcmp("trigger_restart_min_framework", decrypt);
Jason parksbd9a08d2011-01-31 15:04:34 -0600313
Jaekyun Seokc521bb32018-01-30 11:52:47 +0900314 if (!mShuttingDown && encryptedAnimation) {
315 static const char* encryptedBootFiles[] =
316 {PRODUCT_ENCRYPTED_BOOTANIMATION_FILE, SYSTEM_ENCRYPTED_BOOTANIMATION_FILE};
317 for (const char* f : encryptedBootFiles) {
318 if (access(f, R_OK) == 0) {
319 mZipFileName = f;
320 return NO_ERROR;
321 }
322 }
Jason parksbd9a08d2011-01-31 15:04:34 -0600323 }
Jaekyun Seokc521bb32018-01-30 11:52:47 +0900324 static const char* bootFiles[] =
325 {PRODUCT_BOOTANIMATION_FILE, OEM_BOOTANIMATION_FILE, SYSTEM_BOOTANIMATION_FILE};
Keun-young Parkb5938422017-03-23 13:46:24 -0700326 static const char* shutdownFiles[] =
Jaekyun Seokc521bb32018-01-30 11:52:47 +0900327 {PRODUCT_SHUTDOWNANIMATION_FILE, OEM_SHUTDOWNANIMATION_FILE, SYSTEM_SHUTDOWNANIMATION_FILE};
Keun-young Parkb5938422017-03-23 13:46:24 -0700328
329 for (const char* f : (!mShuttingDown ? bootFiles : shutdownFiles)) {
330 if (access(f, R_OK) == 0) {
331 mZipFileName = f;
332 return NO_ERROR;
333 }
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700334 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800335 return NO_ERROR;
336}
337
Mathias Agopiana8826d62009-10-01 03:10:14 -0700338bool BootAnimation::threadLoop()
339{
340 bool r;
Narayan Kamathafd31e02013-12-03 13:16:03 +0000341 // We have no bootanimation file, so we use the stock android logo
342 // animation.
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700343 if (mZipFileName.isEmpty()) {
Mathias Agopiana8826d62009-10-01 03:10:14 -0700344 r = android();
345 } else {
346 r = movie();
347 }
348
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800349 eglMakeCurrent(mDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
350 eglDestroyContext(mDisplay, mContext);
351 eglDestroySurface(mDisplay, mSurface);
Mathias Agopian6cf0db22009-04-17 19:36:26 -0700352 mFlingerSurface.clear();
Mathias Agopian17f638b2009-04-16 20:04:08 -0700353 mFlingerSurfaceControl.clear();
Mathias Agopian627e7b52009-05-21 19:21:59 -0700354 eglTerminate(mDisplay);
Sai Kiran Korwar3eee9fb2015-06-16 17:13:35 +0530355 eglReleaseThread();
Mathias Agopian627e7b52009-05-21 19:21:59 -0700356 IPCThreadState::self()->stopProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800357 return r;
358}
359
Mathias Agopiana8826d62009-10-01 03:10:14 -0700360bool BootAnimation::android()
361{
Keun-young Parkd1794cd2017-04-04 12:21:19 -0700362 ALOGD("%sAnimationShownTiming start time: %" PRId64 "ms", mShuttingDown ? "Shutdown" : "Boot",
363 elapsedRealtime());
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700364 initTexture(&mAndroid[0], mAssets, "images/android-logo-mask.png");
365 initTexture(&mAndroid[1], mAssets, "images/android-logo-shine.png");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800366
Ed Coyne7464ac92017-06-08 12:26:48 -0700367 mCallbacks->init({});
368
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800369 // clear screen
Mathias Agopiana8826d62009-10-01 03:10:14 -0700370 glShadeModel(GL_FLAT);
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700371 glDisable(GL_DITHER);
372 glDisable(GL_SCISSOR_TEST);
Mathias Agopian59f19e42011-05-06 19:22:12 -0700373 glClearColor(0,0,0,1);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800374 glClear(GL_COLOR_BUFFER_BIT);
375 eglSwapBuffers(mDisplay, mSurface);
376
Mathias Agopiana8826d62009-10-01 03:10:14 -0700377 glEnable(GL_TEXTURE_2D);
378 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
379
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700380 const GLint xc = (mWidth - mAndroid[0].w) / 2;
381 const GLint yc = (mHeight - mAndroid[0].h) / 2;
382 const Rect updateRect(xc, yc, xc + mAndroid[0].w, yc + mAndroid[0].h);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800383
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800384 glScissor(updateRect.left, mHeight - updateRect.bottom, updateRect.width(),
385 updateRect.height());
386
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700387 // Blend state
388 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
389 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
390
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800391 const nsecs_t startTime = systemTime();
392 do {
Mathias Agopian13796652009-03-24 22:49:21 -0700393 nsecs_t now = systemTime();
394 double time = now - startTime;
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700395 float t = 4.0f * float(time / us2ns(16667)) / mAndroid[1].w;
396 GLint offset = (1 - (t - floorf(t))) * mAndroid[1].w;
397 GLint x = xc - offset;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800398
Mathias Agopian81668642009-07-28 11:41:30 -0700399 glDisable(GL_SCISSOR_TEST);
400 glClear(GL_COLOR_BUFFER_BIT);
401
402 glEnable(GL_SCISSOR_TEST);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800403 glDisable(GL_BLEND);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800404 glBindTexture(GL_TEXTURE_2D, mAndroid[1].name);
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700405 glDrawTexiOES(x, yc, 0, mAndroid[1].w, mAndroid[1].h);
406 glDrawTexiOES(x + mAndroid[1].w, yc, 0, mAndroid[1].w, mAndroid[1].h);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800407
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700408 glEnable(GL_BLEND);
409 glBindTexture(GL_TEXTURE_2D, mAndroid[0].name);
410 glDrawTexiOES(xc, yc, 0, mAndroid[0].w, mAndroid[0].h);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800411
Mathias Agopian627e7b52009-05-21 19:21:59 -0700412 EGLBoolean res = eglSwapBuffers(mDisplay, mSurface);
413 if (res == EGL_FALSE)
414 break;
415
Mathias Agopian13796652009-03-24 22:49:21 -0700416 // 12fps: don't animate too fast to preserve CPU
417 const nsecs_t sleepTime = 83333 - ns2us(systemTime() - now);
418 if (sleepTime > 0)
Mathias Agopiana8826d62009-10-01 03:10:14 -0700419 usleep(sleepTime);
Kevin Hesterd3782b22012-04-26 10:38:55 -0700420
421 checkExit();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800422 } while (!exitPending());
423
424 glDeleteTextures(1, &mAndroid[0].name);
425 glDeleteTextures(1, &mAndroid[1].name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800426 return false;
427}
428
Kevin Hesterd3782b22012-04-26 10:38:55 -0700429void BootAnimation::checkExit() {
430 // Allow surface flinger to gracefully request shutdown
431 char value[PROPERTY_VALUE_MAX];
432 property_get(EXIT_PROP_NAME, value, "0");
433 int exitnow = atoi(value);
434 if (exitnow) {
435 requestExit();
Ed Coyne7464ac92017-06-08 12:26:48 -0700436 mCallbacks->shutdown();
Kevin Hesterd3782b22012-04-26 10:38:55 -0700437 }
438}
439
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700440bool BootAnimation::validClock(const Animation::Part& part) {
441 return part.clockPosX != TEXT_MISSING_VALUE && part.clockPosY != TEXT_MISSING_VALUE;
442}
443
444bool parseTextCoord(const char* str, int* dest) {
445 if (strcmp("c", str) == 0) {
446 *dest = TEXT_CENTER_VALUE;
447 return true;
448 }
449
450 char* end;
451 int val = (int) strtol(str, &end, 0);
452 if (end == str || *end != '\0' || val == INT_MAX || val == INT_MIN) {
453 return false;
454 }
455 *dest = val;
456 return true;
457}
458
459// Parse two position coordinates. If only string is non-empty, treat it as the y value.
460void parsePosition(const char* str1, const char* str2, int* x, int* y) {
461 bool success = false;
462 if (strlen(str1) == 0) { // No values were specified
463 // success = false
464 } else if (strlen(str2) == 0) { // we have only one value
465 if (parseTextCoord(str1, y)) {
466 *x = TEXT_CENTER_VALUE;
467 success = true;
468 }
469 } else {
470 if (parseTextCoord(str1, x) && parseTextCoord(str2, y)) {
471 success = true;
472 }
473 }
474
475 if (!success) {
476 *x = TEXT_MISSING_VALUE;
477 *y = TEXT_MISSING_VALUE;
478 }
479}
480
Jesse Hall083b84c2014-09-22 10:51:09 -0700481// Parse a color represented as an HTML-style 'RRGGBB' string: each pair of
482// characters in str is a hex number in [0, 255], which are converted to
483// floating point values in the range [0.0, 1.0] and placed in the
484// corresponding elements of color.
485//
486// If the input string isn't valid, parseColor returns false and color is
487// left unchanged.
488static bool parseColor(const char str[7], float color[3]) {
489 float tmpColor[3];
490 for (int i = 0; i < 3; i++) {
491 int val = 0;
492 for (int j = 0; j < 2; j++) {
493 val *= 16;
494 char c = str[2*i + j];
495 if (c >= '0' && c <= '9') val += c - '0';
496 else if (c >= 'A' && c <= 'F') val += (c - 'A') + 10;
497 else if (c >= 'a' && c <= 'f') val += (c - 'a') + 10;
498 else return false;
499 }
500 tmpColor[i] = static_cast<float>(val) / 255.0f;
501 }
502 memcpy(color, tmpColor, sizeof(tmpColor));
503 return true;
504}
505
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700506
507static bool readFile(ZipFileRO* zip, const char* name, String8& outString)
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700508{
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700509 ZipEntryRO entry = zip->findEntryByName(name);
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700510 ALOGE_IF(!entry, "couldn't find %s", name);
511 if (!entry) {
512 return false;
513 }
514
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700515 FileMap* entryMap = zip->createEntryFileMap(entry);
516 zip->releaseEntry(entry);
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700517 ALOGE_IF(!entryMap, "entryMap is null");
518 if (!entryMap) {
519 return false;
520 }
521
522 outString.setTo((char const*)entryMap->getDataPtr(), entryMap->getDataLength());
Narayan Kamath688ff4c2015-02-23 15:47:54 +0000523 delete entryMap;
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700524 return true;
525}
526
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700527// The font image should be a 96x2 array of character images. The
528// columns are the printable ASCII characters 0x20 - 0x7f. The
529// top row is regular text; the bottom row is bold.
530status_t BootAnimation::initFont(Font* font, const char* fallback) {
531 status_t status = NO_ERROR;
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800532
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700533 if (font->map != nullptr) {
534 glGenTextures(1, &font->texture.name);
535 glBindTexture(GL_TEXTURE_2D, font->texture.name);
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800536
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700537 status = initTexture(font->map, &font->texture.w, &font->texture.h);
538
539 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
540 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
541 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
542 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
543 } else if (fallback != nullptr) {
544 status = initTexture(&font->texture, mAssets, fallback);
545 } else {
546 return NO_INIT;
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800547 }
548
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700549 if (status == NO_ERROR) {
550 font->char_width = font->texture.w / FONT_NUM_COLS;
551 font->char_height = font->texture.h / FONT_NUM_ROWS / 2; // There are bold and regular rows
552 }
553
554 return status;
555}
556
557void BootAnimation::drawText(const char* str, const Font& font, bool bold, int* x, int* y) {
558 glEnable(GL_BLEND); // Allow us to draw on top of the animation
559 glBindTexture(GL_TEXTURE_2D, font.texture.name);
560
561 const int len = strlen(str);
562 const int strWidth = font.char_width * len;
563
564 if (*x == TEXT_CENTER_VALUE) {
565 *x = (mWidth - strWidth) / 2;
566 } else if (*x < 0) {
567 *x = mWidth + *x - strWidth;
568 }
569 if (*y == TEXT_CENTER_VALUE) {
570 *y = (mHeight - font.char_height) / 2;
571 } else if (*y < 0) {
572 *y = mHeight + *y - font.char_height;
573 }
574
575 int cropRect[4] = { 0, 0, font.char_width, -font.char_height };
576
577 for (int i = 0; i < len; i++) {
578 char c = str[i];
579
580 if (c < FONT_BEGIN_CHAR || c > FONT_END_CHAR) {
581 c = '?';
582 }
583
584 // Crop the texture to only the pixels in the current glyph
585 const int charPos = (c - FONT_BEGIN_CHAR); // Position in the list of valid characters
586 const int row = charPos / FONT_NUM_COLS;
587 const int col = charPos % FONT_NUM_COLS;
588 cropRect[0] = col * font.char_width; // Left of column
589 cropRect[1] = row * font.char_height * 2; // Top of row
590 // Move down to bottom of regular (one char_heigh) or bold (two char_heigh) line
591 cropRect[1] += bold ? 2 * font.char_height : font.char_height;
592 glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_CROP_RECT_OES, cropRect);
593
594 glDrawTexiOES(*x, *y, 0, font.char_width, font.char_height);
595
596 *x += font.char_width;
597 }
598
599 glDisable(GL_BLEND); // Return to the animation's default behaviour
600 glBindTexture(GL_TEXTURE_2D, 0);
601}
602
Damien Bargiacchi9071db12016-10-28 17:38:22 -0700603// We render 12 or 24 hour time.
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700604void BootAnimation::drawClock(const Font& font, const int xPos, const int yPos) {
Damien Bargiacchi9071db12016-10-28 17:38:22 -0700605 static constexpr char TIME_FORMAT_12[] = "%l:%M";
606 static constexpr char TIME_FORMAT_24[] = "%H:%M";
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700607 static constexpr int TIME_LENGTH = 6;
608
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800609 time_t rawtime;
610 time(&rawtime);
611 struct tm* timeInfo = localtime(&rawtime);
612
613 char timeBuff[TIME_LENGTH];
Damien Bargiacchi9071db12016-10-28 17:38:22 -0700614 const char* timeFormat = mTimeFormat12Hour ? TIME_FORMAT_12 : TIME_FORMAT_24;
615 size_t length = strftime(timeBuff, TIME_LENGTH, timeFormat, timeInfo);
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800616
617 if (length != TIME_LENGTH - 1) {
618 ALOGE("Couldn't format time; abandoning boot animation clock");
619 mClockEnabled = false;
620 return;
621 }
622
Damien Bargiacchi45a76442016-12-05 18:02:18 -0800623 char* out = timeBuff[0] == ' ' ? &timeBuff[1] : &timeBuff[0];
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700624 int x = xPos;
625 int y = yPos;
Damien Bargiacchi45a76442016-12-05 18:02:18 -0800626 drawText(out, font, false, &x, &y);
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800627}
628
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700629bool BootAnimation::parseAnimationDesc(Animation& animation)
Mathias Agopiana8826d62009-10-01 03:10:14 -0700630{
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700631 String8 desString;
632
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700633 if (!readFile(animation.zip, "desc.txt", desString)) {
Narayan Kamathafd31e02013-12-03 13:16:03 +0000634 return false;
635 }
Mathias Agopiana8826d62009-10-01 03:10:14 -0700636 char const* s = desString.string();
637
Mathias Agopiana8826d62009-10-01 03:10:14 -0700638 // Parse the description file
639 for (;;) {
640 const char* endl = strstr(s, "\n");
Andreas Gampecfedceb2014-09-30 21:48:18 -0700641 if (endl == NULL) break;
Mathias Agopiana8826d62009-10-01 03:10:14 -0700642 String8 line(s, endl - s);
643 const char* l = line.string();
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800644 int fps = 0;
645 int width = 0;
646 int height = 0;
647 int count = 0;
648 int pause = 0;
Narayan Kamathafd31e02013-12-03 13:16:03 +0000649 char path[ANIM_ENTRY_NAME_MAX];
Jesse Hall083b84c2014-09-22 10:51:09 -0700650 char color[7] = "000000"; // default to black if unspecified
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700651 char clockPos1[TEXT_POS_LEN_MAX + 1] = "";
652 char clockPos2[TEXT_POS_LEN_MAX + 1] = "";
Jesse Hall083b84c2014-09-22 10:51:09 -0700653
Kevin Hesterd3782b22012-04-26 10:38:55 -0700654 char pathType;
Mathias Agopiana8826d62009-10-01 03:10:14 -0700655 if (sscanf(l, "%d %d %d", &width, &height, &fps) == 3) {
Jesse Hall083b84c2014-09-22 10:51:09 -0700656 // ALOGD("> w=%d, h=%d, fps=%d", width, height, fps);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700657 animation.width = width;
658 animation.height = height;
659 animation.fps = fps;
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700660 } else if (sscanf(l, " %c %d %d %s #%6s %16s %16s",
661 &pathType, &count, &pause, path, color, clockPos1, clockPos2) >= 4) {
662 //ALOGD("> type=%c, count=%d, pause=%d, path=%s, color=%s, clockPos1=%s, clockPos2=%s",
663 // pathType, count, pause, path, color, clockPos1, clockPos2);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700664 Animation::Part part;
Kevin Hesterd3782b22012-04-26 10:38:55 -0700665 part.playUntilComplete = pathType == 'c';
Mathias Agopiana8826d62009-10-01 03:10:14 -0700666 part.count = count;
667 part.pause = pause;
668 part.path = path;
Geoffrey Pitschd6d9a1d2016-06-08 00:38:58 -0700669 part.audioData = NULL;
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700670 part.animation = NULL;
Jesse Hall083b84c2014-09-22 10:51:09 -0700671 if (!parseColor(color, part.backgroundColor)) {
672 ALOGE("> invalid color '#%s'", color);
673 part.backgroundColor[0] = 0.0f;
674 part.backgroundColor[1] = 0.0f;
675 part.backgroundColor[2] = 0.0f;
676 }
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700677 parsePosition(clockPos1, clockPos2, &part.clockPosX, &part.clockPosY);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700678 animation.parts.add(part);
679 }
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700680 else if (strcmp(l, "$SYSTEM") == 0) {
681 // ALOGD("> SYSTEM");
682 Animation::Part part;
683 part.playUntilComplete = false;
684 part.count = 1;
685 part.pause = 0;
Geoffrey Pitschd6d9a1d2016-06-08 00:38:58 -0700686 part.audioData = NULL;
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700687 part.animation = loadAnimation(String8(SYSTEM_BOOTANIMATION_FILE));
688 if (part.animation != NULL)
689 animation.parts.add(part);
690 }
Mathias Agopiana8826d62009-10-01 03:10:14 -0700691 s = ++endl;
692 }
693
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700694 return true;
695}
696
697bool BootAnimation::preloadZip(Animation& animation)
698{
Mathias Agopiana8826d62009-10-01 03:10:14 -0700699 // read all the data structures
700 const size_t pcount = animation.parts.size();
Narayan Kamathafd31e02013-12-03 13:16:03 +0000701 void *cookie = NULL;
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400702 ZipFileRO* zip = animation.zip;
703 if (!zip->startIteration(&cookie)) {
Narayan Kamathafd31e02013-12-03 13:16:03 +0000704 return false;
705 }
706
707 ZipEntryRO entry;
708 char name[ANIM_ENTRY_NAME_MAX];
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400709 while ((entry = zip->nextEntry(cookie)) != NULL) {
710 const int foundEntryName = zip->getEntryFileName(entry, name, ANIM_ENTRY_NAME_MAX);
Narayan Kamathafd31e02013-12-03 13:16:03 +0000711 if (foundEntryName > ANIM_ENTRY_NAME_MAX || foundEntryName == -1) {
712 ALOGE("Error fetching entry file name");
713 continue;
714 }
715
716 const String8 entryName(name);
717 const String8 path(entryName.getPathDir());
718 const String8 leaf(entryName.getPathLeaf());
719 if (leaf.size() > 0) {
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700720 if (entryName == CLOCK_FONT_ZIP_NAME) {
721 FileMap* map = zip->createEntryFileMap(entry);
722 if (map) {
723 animation.clockFont.map = map;
724 }
725 continue;
726 }
727
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400728 for (size_t j = 0; j < pcount; j++) {
Narayan Kamathafd31e02013-12-03 13:16:03 +0000729 if (path == animation.parts[j].path) {
Narayan Kamath4600dd02015-06-16 12:02:57 +0100730 uint16_t method;
Narayan Kamathafd31e02013-12-03 13:16:03 +0000731 // supports only stored png files
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400732 if (zip->getEntryInfo(entry, &method, NULL, NULL, NULL, NULL, NULL)) {
Narayan Kamathafd31e02013-12-03 13:16:03 +0000733 if (method == ZipFileRO::kCompressStored) {
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400734 FileMap* map = zip->createEntryFileMap(entry);
Narayan Kamathafd31e02013-12-03 13:16:03 +0000735 if (map) {
Narayan Kamathafd31e02013-12-03 13:16:03 +0000736 Animation::Part& part(animation.parts.editItemAt(j));
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700737 if (leaf == "audio.wav") {
738 // a part may have at most one audio file
Geoffrey Pitschd6d9a1d2016-06-08 00:38:58 -0700739 part.audioData = (uint8_t *)map->getDataPtr();
740 part.audioLength = map->getDataLength();
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400741 } else if (leaf == "trim.txt") {
742 part.trimData.setTo((char const*)map->getDataPtr(),
743 map->getDataLength());
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700744 } else {
745 Animation::Frame frame;
746 frame.name = leaf;
747 frame.map = map;
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400748 frame.trimWidth = animation.width;
749 frame.trimHeight = animation.height;
750 frame.trimX = 0;
751 frame.trimY = 0;
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700752 part.frames.add(frame);
753 }
Mathias Agopiana8826d62009-10-01 03:10:14 -0700754 }
Geoffrey Pitschd6d9a1d2016-06-08 00:38:58 -0700755 } else {
756 ALOGE("bootanimation.zip is compressed; must be only stored");
Mathias Agopiana8826d62009-10-01 03:10:14 -0700757 }
758 }
759 }
760 }
761 }
762 }
763
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400764 // If there is trimData present, override the positioning defaults.
765 for (Animation::Part& part : animation.parts) {
766 const char* trimDataStr = part.trimData.string();
767 for (size_t frameIdx = 0; frameIdx < part.frames.size(); frameIdx++) {
768 const char* endl = strstr(trimDataStr, "\n");
769 // No more trimData for this part.
770 if (endl == NULL) {
771 break;
772 }
773 String8 line(trimDataStr, endl - trimDataStr);
774 const char* lineStr = line.string();
775 trimDataStr = ++endl;
776 int width = 0, height = 0, x = 0, y = 0;
777 if (sscanf(lineStr, "%dx%d+%d+%d", &width, &height, &x, &y) == 4) {
778 Animation::Frame& frame(part.frames.editItemAt(frameIdx));
779 frame.trimWidth = width;
780 frame.trimHeight = height;
781 frame.trimX = x;
782 frame.trimY = y;
783 } else {
784 ALOGE("Error parsing trim.txt, line: %s", lineStr);
785 break;
786 }
787 }
788 }
789
Ed Coyne7464ac92017-06-08 12:26:48 -0700790 mCallbacks->init(animation.parts);
Geoffrey Pitschd6d9a1d2016-06-08 00:38:58 -0700791
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400792 zip->endIteration(cookie);
Narayan Kamathafd31e02013-12-03 13:16:03 +0000793
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700794 return true;
795}
796
797bool BootAnimation::movie()
798{
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700799 Animation* animation = loadAnimation(mZipFileName);
800 if (animation == NULL)
801 return false;
802
Damien Bargiacchi97480862016-03-29 14:55:55 -0700803 bool anyPartHasClock = false;
804 for (size_t i=0; i < animation->parts.size(); i++) {
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700805 if(validClock(animation->parts[i])) {
Damien Bargiacchi97480862016-03-29 14:55:55 -0700806 anyPartHasClock = true;
807 break;
808 }
809 }
810 if (!anyPartHasClock) {
811 mClockEnabled = false;
812 }
813
Sai Kiran Korwar27167492015-07-07 20:00:06 +0530814 // Check if npot textures are supported
815 mUseNpotTextures = false;
816 String8 gl_extensions;
817 const char* exts = reinterpret_cast<const char*>(glGetString(GL_EXTENSIONS));
818 if (!exts) {
819 glGetError();
820 } else {
821 gl_extensions.setTo(exts);
822 if ((gl_extensions.find("GL_ARB_texture_non_power_of_two") != -1) ||
823 (gl_extensions.find("GL_OES_texture_npot") != -1)) {
824 mUseNpotTextures = true;
825 }
826 }
827
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800828 // Blend required to draw time on top of animation frames.
829 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700830 glShadeModel(GL_FLAT);
831 glDisable(GL_DITHER);
832 glDisable(GL_SCISSOR_TEST);
833 glDisable(GL_BLEND);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700834
835 glBindTexture(GL_TEXTURE_2D, 0);
836 glEnable(GL_TEXTURE_2D);
837 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
838 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
839 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
840 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
841 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
842
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700843 bool clockFontInitialized = false;
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800844 if (mClockEnabled) {
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700845 clockFontInitialized =
846 (initFont(&animation->clockFont, CLOCK_FONT_ASSET) == NO_ERROR);
847 mClockEnabled = clockFontInitialized;
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800848 }
849
Damien Bargiacchi97480862016-03-29 14:55:55 -0700850 if (mClockEnabled && !updateIsTimeAccurate()) {
851 mTimeCheckThread = new TimeCheckThread(this);
852 mTimeCheckThread->run("BootAnimation::TimeCheckThread", PRIORITY_NORMAL);
853 }
854
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700855 playAnimation(*animation);
Damien Bargiacchi97480862016-03-29 14:55:55 -0700856
Geoffrey Pitscha9173532016-07-19 14:56:39 -0400857 if (mTimeCheckThread != nullptr) {
Damien Bargiacchi97480862016-03-29 14:55:55 -0700858 mTimeCheckThread->requestExit();
Geoffrey Pitscha9173532016-07-19 14:56:39 -0400859 mTimeCheckThread = nullptr;
860 }
861
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700862 releaseAnimation(animation);
863
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700864 if (clockFontInitialized) {
865 glDeleteTextures(1, &animation->clockFont.texture.name);
Andriy Naborskyy815e51d2016-03-24 16:43:34 -0700866 }
867
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700868 return false;
869}
870
871bool BootAnimation::playAnimation(const Animation& animation)
872{
873 const size_t pcount = animation.parts.size();
Mathias Agopiana8826d62009-10-01 03:10:14 -0700874 nsecs_t frameDuration = s2ns(1) / animation.fps;
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400875 const int animationX = (mWidth - animation.width) / 2;
876 const int animationY = (mHeight - animation.height) / 2;
Mathias Agopian9f3020d2009-11-06 16:30:18 -0800877
Keun-young Parkd1794cd2017-04-04 12:21:19 -0700878 ALOGD("%sAnimationShownTiming start time: %" PRId64 "ms", mShuttingDown ? "Shutdown" : "Boot",
879 elapsedRealtime());
Narayan Kamathafd31e02013-12-03 13:16:03 +0000880 for (size_t i=0 ; i<pcount ; i++) {
Mathias Agopiana8826d62009-10-01 03:10:14 -0700881 const Animation::Part& part(animation.parts[i]);
882 const size_t fcount = part.frames.size();
883 glBindTexture(GL_TEXTURE_2D, 0);
884
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700885 // Handle animation package
886 if (part.animation != NULL) {
887 playAnimation(*part.animation);
888 if (exitPending())
889 break;
890 continue; //to next part
891 }
892
Mathias Agopiana8826d62009-10-01 03:10:14 -0700893 for (int r=0 ; !part.count || r<part.count ; r++) {
Kevin Hesterd3782b22012-04-26 10:38:55 -0700894 // Exit any non playuntil complete parts immediately
895 if(exitPending() && !part.playUntilComplete)
896 break;
897
Ed Coyne7464ac92017-06-08 12:26:48 -0700898 mCallbacks->playPart(i, part, r);
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700899
Jesse Hall083b84c2014-09-22 10:51:09 -0700900 glClearColor(
901 part.backgroundColor[0],
902 part.backgroundColor[1],
903 part.backgroundColor[2],
904 1.0f);
905
Narayan Kamathafd31e02013-12-03 13:16:03 +0000906 for (size_t j=0 ; j<fcount && (!exitPending() || part.playUntilComplete) ; j++) {
Mathias Agopiana8826d62009-10-01 03:10:14 -0700907 const Animation::Frame& frame(part.frames[j]);
Mathias Agopiandb7dd2a2012-05-12 15:08:21 -0700908 nsecs_t lastFrame = systemTime();
Mathias Agopiana8826d62009-10-01 03:10:14 -0700909
910 if (r > 0) {
911 glBindTexture(GL_TEXTURE_2D, frame.tid);
912 } else {
913 if (part.count != 1) {
914 glGenTextures(1, &frame.tid);
915 glBindTexture(GL_TEXTURE_2D, frame.tid);
916 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
917 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
918 }
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700919 int w, h;
920 initTexture(frame.map, &w, &h);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700921 }
922
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400923 const int xc = animationX + frame.trimX;
924 const int yc = animationY + frame.trimY;
925 Region clearReg(Rect(mWidth, mHeight));
926 clearReg.subtractSelf(Rect(xc, yc, xc+frame.trimWidth, yc+frame.trimHeight));
Mathias Agopian9f3020d2009-11-06 16:30:18 -0800927 if (!clearReg.isEmpty()) {
928 Region::const_iterator head(clearReg.begin());
929 Region::const_iterator tail(clearReg.end());
930 glEnable(GL_SCISSOR_TEST);
931 while (head != tail) {
Andreas Gampecfedceb2014-09-30 21:48:18 -0700932 const Rect& r2(*head++);
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400933 glScissor(r2.left, mHeight - r2.bottom, r2.width(), r2.height());
Mathias Agopian9f3020d2009-11-06 16:30:18 -0800934 glClear(GL_COLOR_BUFFER_BIT);
935 }
936 glDisable(GL_SCISSOR_TEST);
937 }
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400938 // specify the y center as ceiling((mHeight - frame.trimHeight) / 2)
939 // which is equivalent to mHeight - (yc + frame.trimHeight)
940 glDrawTexiOES(xc, mHeight - (yc + frame.trimHeight),
941 0, frame.trimWidth, frame.trimHeight);
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700942 if (mClockEnabled && mTimeIsAccurate && validClock(part)) {
943 drawClock(animation.clockFont, part.clockPosX, part.clockPosY);
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800944 }
945
Mathias Agopiana8826d62009-10-01 03:10:14 -0700946 eglSwapBuffers(mDisplay, mSurface);
947
948 nsecs_t now = systemTime();
949 nsecs_t delay = frameDuration - (now - lastFrame);
Mathias Agopiandb7dd2a2012-05-12 15:08:21 -0700950 //ALOGD("%lld, %lld", ns2ms(now - lastFrame), ns2ms(delay));
Mathias Agopiana8826d62009-10-01 03:10:14 -0700951 lastFrame = now;
Mathias Agopiandb7dd2a2012-05-12 15:08:21 -0700952
953 if (delay > 0) {
954 struct timespec spec;
955 spec.tv_sec = (now + delay) / 1000000000;
956 spec.tv_nsec = (now + delay) % 1000000000;
957 int err;
958 do {
959 err = clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &spec, NULL);
960 } while (err<0 && errno == EINTR);
961 }
Kevin Hesterd3782b22012-04-26 10:38:55 -0700962
963 checkExit();
Mathias Agopiana8826d62009-10-01 03:10:14 -0700964 }
Kevin Hesterd3782b22012-04-26 10:38:55 -0700965
Mathias Agopiana8826d62009-10-01 03:10:14 -0700966 usleep(part.pause * ns2us(frameDuration));
Kevin Hesterd3782b22012-04-26 10:38:55 -0700967
968 // For infinite parts, we've now played them at least once, so perhaps exit
969 if(exitPending() && !part.count)
970 break;
Mathias Agopiana8826d62009-10-01 03:10:14 -0700971 }
972
Geoffrey Pitsch2fb30fb2016-07-06 16:16:20 -0400973 }
974
975 // Free textures created for looping parts now that the animation is done.
976 for (const Animation::Part& part : animation.parts) {
Mathias Agopiana8826d62009-10-01 03:10:14 -0700977 if (part.count != 1) {
Geoffrey Pitsch2fb30fb2016-07-06 16:16:20 -0400978 const size_t fcount = part.frames.size();
979 for (size_t j = 0; j < fcount; j++) {
Mathias Agopiana8826d62009-10-01 03:10:14 -0700980 const Animation::Frame& frame(part.frames[j]);
981 glDeleteTextures(1, &frame.tid);
982 }
983 }
984 }
Geoffrey Pitschd6d9a1d2016-06-08 00:38:58 -0700985
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700986 return true;
Mathias Agopiana8826d62009-10-01 03:10:14 -0700987}
988
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700989void BootAnimation::releaseAnimation(Animation* animation) const
990{
991 for (Vector<Animation::Part>::iterator it = animation->parts.begin(),
992 e = animation->parts.end(); it != e; ++it) {
993 if (it->animation)
994 releaseAnimation(it->animation);
995 }
996 if (animation->zip)
997 delete animation->zip;
998 delete animation;
999}
1000
1001BootAnimation::Animation* BootAnimation::loadAnimation(const String8& fn)
1002{
1003 if (mLoadedFiles.indexOf(fn) >= 0) {
1004 ALOGE("File \"%s\" is already loaded. Cyclic ref is not allowed",
1005 fn.string());
1006 return NULL;
1007 }
1008 ZipFileRO *zip = ZipFileRO::open(fn);
1009 if (zip == NULL) {
1010 ALOGE("Failed to open animation zip \"%s\": %s",
1011 fn.string(), strerror(errno));
1012 return NULL;
1013 }
1014
1015 Animation *animation = new Animation;
1016 animation->fileName = fn;
1017 animation->zip = zip;
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -07001018 animation->clockFont.map = nullptr;
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001019 mLoadedFiles.add(animation->fileName);
1020
1021 parseAnimationDesc(*animation);
Geoffrey Pitscha91a2d72016-07-12 14:46:19 -04001022 if (!preloadZip(*animation)) {
1023 return NULL;
1024 }
1025
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001026
1027 mLoadedFiles.remove(fn);
1028 return animation;
1029}
Damien Bargiacchi97480862016-03-29 14:55:55 -07001030
1031bool BootAnimation::updateIsTimeAccurate() {
1032 static constexpr long long MAX_TIME_IN_PAST = 60000LL * 60LL * 24LL * 30LL; // 30 days
1033 static constexpr long long MAX_TIME_IN_FUTURE = 60000LL * 90LL; // 90 minutes
1034
1035 if (mTimeIsAccurate) {
1036 return true;
1037 }
Keun-young Parkb5938422017-03-23 13:46:24 -07001038 if (mShuttingDown) return true;
Damien Bargiacchi97480862016-03-29 14:55:55 -07001039 struct stat statResult;
Damien Bargiacchi9071db12016-10-28 17:38:22 -07001040
1041 if(stat(TIME_FORMAT_12_HOUR_FLAG_FILE_PATH, &statResult) == 0) {
1042 mTimeFormat12Hour = true;
1043 }
1044
Damien Bargiacchi97480862016-03-29 14:55:55 -07001045 if(stat(ACCURATE_TIME_FLAG_FILE_PATH, &statResult) == 0) {
1046 mTimeIsAccurate = true;
1047 return true;
1048 }
1049
1050 FILE* file = fopen(LAST_TIME_CHANGED_FILE_PATH, "r");
1051 if (file != NULL) {
1052 long long lastChangedTime = 0;
1053 fscanf(file, "%lld", &lastChangedTime);
1054 fclose(file);
1055 if (lastChangedTime > 0) {
1056 struct timespec now;
1057 clock_gettime(CLOCK_REALTIME, &now);
1058 // Match the Java timestamp format
1059 long long rtcNow = (now.tv_sec * 1000LL) + (now.tv_nsec / 1000000LL);
Damien Bargiacchi96762812016-07-12 15:53:40 -07001060 if (ACCURATE_TIME_EPOCH < rtcNow
1061 && lastChangedTime > (rtcNow - MAX_TIME_IN_PAST)
1062 && lastChangedTime < (rtcNow + MAX_TIME_IN_FUTURE)) {
Damien Bargiacchi97480862016-03-29 14:55:55 -07001063 mTimeIsAccurate = true;
1064 }
1065 }
1066 }
1067
1068 return mTimeIsAccurate;
1069}
1070
1071BootAnimation::TimeCheckThread::TimeCheckThread(BootAnimation* bootAnimation) : Thread(false),
1072 mInotifyFd(-1), mSystemWd(-1), mTimeWd(-1), mBootAnimation(bootAnimation) {}
1073
1074BootAnimation::TimeCheckThread::~TimeCheckThread() {
1075 // mInotifyFd may be -1 but that's ok since we're not at risk of attempting to close a valid FD.
1076 close(mInotifyFd);
1077}
1078
1079bool BootAnimation::TimeCheckThread::threadLoop() {
1080 bool shouldLoop = doThreadLoop() && !mBootAnimation->mTimeIsAccurate
1081 && mBootAnimation->mClockEnabled;
1082 if (!shouldLoop) {
1083 close(mInotifyFd);
1084 mInotifyFd = -1;
1085 }
1086 return shouldLoop;
1087}
1088
1089bool BootAnimation::TimeCheckThread::doThreadLoop() {
1090 static constexpr int BUFF_LEN (10 * (sizeof(struct inotify_event) + NAME_MAX + 1));
1091
1092 // Poll instead of doing a blocking read so the Thread can exit if requested.
1093 struct pollfd pfd = { mInotifyFd, POLLIN, 0 };
1094 ssize_t pollResult = poll(&pfd, 1, 1000);
1095
1096 if (pollResult == 0) {
1097 return true;
1098 } else if (pollResult < 0) {
1099 ALOGE("Could not poll inotify events");
1100 return false;
1101 }
1102
1103 char buff[BUFF_LEN] __attribute__ ((aligned(__alignof__(struct inotify_event))));;
1104 ssize_t length = read(mInotifyFd, buff, BUFF_LEN);
1105 if (length == 0) {
1106 return true;
1107 } else if (length < 0) {
1108 ALOGE("Could not read inotify events");
1109 return false;
1110 }
1111
1112 const struct inotify_event *event;
1113 for (char* ptr = buff; ptr < buff + length; ptr += sizeof(struct inotify_event) + event->len) {
1114 event = (const struct inotify_event *) ptr;
1115 if (event->wd == mSystemWd && strcmp(SYSTEM_TIME_DIR_NAME, event->name) == 0) {
1116 addTimeDirWatch();
1117 } else if (event->wd == mTimeWd && (strcmp(LAST_TIME_CHANGED_FILE_NAME, event->name) == 0
1118 || strcmp(ACCURATE_TIME_FLAG_FILE_NAME, event->name) == 0)) {
1119 return !mBootAnimation->updateIsTimeAccurate();
1120 }
1121 }
1122
1123 return true;
1124}
1125
1126void BootAnimation::TimeCheckThread::addTimeDirWatch() {
1127 mTimeWd = inotify_add_watch(mInotifyFd, SYSTEM_TIME_DIR_PATH,
1128 IN_CLOSE_WRITE | IN_MOVED_TO | IN_ATTRIB);
1129 if (mTimeWd > 0) {
1130 // No need to watch for the time directory to be created if it already exists
1131 inotify_rm_watch(mInotifyFd, mSystemWd);
1132 mSystemWd = -1;
1133 }
1134}
1135
1136status_t BootAnimation::TimeCheckThread::readyToRun() {
1137 mInotifyFd = inotify_init();
1138 if (mInotifyFd < 0) {
1139 ALOGE("Could not initialize inotify fd");
1140 return NO_INIT;
1141 }
1142
1143 mSystemWd = inotify_add_watch(mInotifyFd, SYSTEM_DATA_DIR_PATH, IN_CREATE | IN_ATTRIB);
1144 if (mSystemWd < 0) {
1145 close(mInotifyFd);
1146 mInotifyFd = -1;
1147 ALOGE("Could not add watch for %s", SYSTEM_DATA_DIR_PATH);
1148 return NO_INIT;
1149 }
1150
1151 addTimeDirWatch();
1152
1153 if (mBootAnimation->updateIsTimeAccurate()) {
1154 close(mInotifyFd);
1155 mInotifyFd = -1;
1156 return ALREADY_EXISTS;
1157 }
1158
1159 return NO_ERROR;
1160}
1161
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001162// ---------------------------------------------------------------------------
1163
1164}
1165; // namespace android