blob: 6526123aba13116532beb2a768b7c5818339c5a0 [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";
69static const char SYSTEM_BOOTANIMATION_FILE[] = "/system/media/bootanimation.zip";
70static const char SYSTEM_ENCRYPTED_BOOTANIMATION_FILE[] = "/system/media/bootanimation-encrypted.zip";
Keun-young Parkb5938422017-03-23 13:46:24 -070071static const char OEM_SHUTDOWNANIMATION_FILE[] = "/oem/media/shutdownanimation.zip";
72static const char SYSTEM_SHUTDOWNANIMATION_FILE[] = "/system/media/shutdownanimation.zip";
73
Damien Bargiacchi97480862016-03-29 14:55:55 -070074static const char SYSTEM_DATA_DIR_PATH[] = "/data/system";
75static const char SYSTEM_TIME_DIR_NAME[] = "time";
76static const char SYSTEM_TIME_DIR_PATH[] = "/data/system/time";
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -070077static const char CLOCK_FONT_ASSET[] = "images/clock_font.png";
78static const char CLOCK_FONT_ZIP_NAME[] = "clock_font.png";
Damien Bargiacchi97480862016-03-29 14:55:55 -070079static const char LAST_TIME_CHANGED_FILE_NAME[] = "last_time_change";
80static const char LAST_TIME_CHANGED_FILE_PATH[] = "/data/system/time/last_time_change";
81static const char ACCURATE_TIME_FLAG_FILE_NAME[] = "time_is_accurate";
82static const char ACCURATE_TIME_FLAG_FILE_PATH[] = "/data/system/time/time_is_accurate";
Damien Bargiacchi9071db12016-10-28 17:38:22 -070083static const char TIME_FORMAT_12_HOUR_FLAG_FILE_PATH[] = "/data/system/time/time_format_12_hour";
Damien Bargiacchi96762812016-07-12 15:53:40 -070084// Java timestamp format. Don't show the clock if the date is before 2000-01-01 00:00:00.
85static const long long ACCURATE_TIME_EPOCH = 946684800000;
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -070086static constexpr char FONT_BEGIN_CHAR = ' ';
87static constexpr char FONT_END_CHAR = '~' + 1;
88static constexpr size_t FONT_NUM_CHARS = FONT_END_CHAR - FONT_BEGIN_CHAR + 1;
89static constexpr size_t FONT_NUM_COLS = 16;
90static constexpr size_t FONT_NUM_ROWS = FONT_NUM_CHARS / FONT_NUM_COLS;
91static const int TEXT_CENTER_VALUE = INT_MAX;
92static const int TEXT_MISSING_VALUE = INT_MIN;
Damien Bargiacchi97480862016-03-29 14:55:55 -070093static const char EXIT_PROP_NAME[] = "service.bootanim.exit";
Narayan Kamathafd31e02013-12-03 13:16:03 +000094static const int ANIM_ENTRY_NAME_MAX = 256;
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -070095static constexpr size_t TEXT_POS_LEN_MAX = 16;
Narayan Kamathafd31e02013-12-03 13:16:03 +000096
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080097// ---------------------------------------------------------------------------
98
Ed Coyne7464ac92017-06-08 12:26:48 -070099BootAnimation::BootAnimation(sp<Callbacks> callbacks)
Ed Coyne2c9e94a2017-05-31 10:08:28 -0700100 : Thread(false), mClockEnabled(true), mTimeIsAccurate(false),
Ed Coyne7464ac92017-06-08 12:26:48 -0700101 mTimeFormat12Hour(false), mTimeCheckThread(NULL), mCallbacks(callbacks) {
Mathias Agopian627e7b52009-05-21 19:21:59 -0700102 mSession = new SurfaceComposerClient();
Geoffrey Pitsch290c4352016-08-09 14:35:10 -0400103
Keun-young Parkb5938422017-03-23 13:46:24 -0700104 std::string powerCtl = android::base::GetProperty("sys.powerctl", "");
105 if (powerCtl.empty()) {
106 mShuttingDown = false;
107 } else {
108 mShuttingDown = true;
109 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800110}
111
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800112void BootAnimation::onFirstRef() {
Mathias Agopianbc726112009-09-23 15:44:05 -0700113 status_t err = mSession->linkToComposerDeath(this);
Steve Block3762c312012-01-06 19:20:56 +0000114 ALOGE_IF(err, "linkToComposerDeath failed (%s) ", strerror(-err));
Mathias Agopian8434c532009-09-23 18:52:49 -0700115 if (err == NO_ERROR) {
Mathias Agopianbc726112009-09-23 15:44:05 -0700116 run("BootAnimation", PRIORITY_DISPLAY);
117 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800118}
119
Mathias Agopianbc726112009-09-23 15:44:05 -0700120sp<SurfaceComposerClient> BootAnimation::session() const {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800121 return mSession;
122}
123
Mathias Agopianbc726112009-09-23 15:44:05 -0700124
Narayan Kamathafd31e02013-12-03 13:16:03 +0000125void BootAnimation::binderDied(const wp<IBinder>&)
Mathias Agopianbc726112009-09-23 15:44:05 -0700126{
127 // woah, surfaceflinger died!
Steve Block5baa3a62011-12-20 16:23:08 +0000128 ALOGD("SurfaceFlinger died, exiting...");
Mathias Agopianbc726112009-09-23 15:44:05 -0700129
130 // calling requestExit() is not enough here because the Surface code
131 // might be blocked on a condition variable that will never be updated.
132 kill( getpid(), SIGKILL );
133 requestExit();
134}
135
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800136status_t BootAnimation::initTexture(Texture* texture, AssetManager& assets,
137 const char* name) {
138 Asset* asset = assets.open(name, Asset::ACCESS_BUFFER);
Andreas Gampecfedceb2014-09-30 21:48:18 -0700139 if (asset == NULL)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800140 return NO_INIT;
141 SkBitmap bitmap;
Matt Sarett8898c162016-03-24 16:11:01 -0400142 sk_sp<SkData> data = SkData::MakeWithoutCopy(asset->getBuffer(false),
143 asset->getLength());
144 sk_sp<SkImage> image = SkImage::MakeFromEncoded(data);
145 image->asLegacyBitmap(&bitmap, SkImage::kRO_LegacyBitmapMode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800146 asset->close();
147 delete asset;
148
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800149 const int w = bitmap.width();
150 const int h = bitmap.height();
151 const void* p = bitmap.getPixels();
152
153 GLint crop[4] = { 0, h, w, -h };
154 texture->w = w;
155 texture->h = h;
156
157 glGenTextures(1, &texture->name);
158 glBindTexture(GL_TEXTURE_2D, texture->name);
159
Mike Reed42a1d082014-07-07 18:06:18 -0400160 switch (bitmap.colorType()) {
161 case kAlpha_8_SkColorType:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800162 glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, w, h, 0, GL_ALPHA,
163 GL_UNSIGNED_BYTE, p);
164 break;
Mike Reed42a1d082014-07-07 18:06:18 -0400165 case kARGB_4444_SkColorType:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800166 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA,
167 GL_UNSIGNED_SHORT_4_4_4_4, p);
168 break;
Mike Reed42a1d082014-07-07 18:06:18 -0400169 case kN32_SkColorType:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800170 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA,
171 GL_UNSIGNED_BYTE, p);
172 break;
Mike Reed42a1d082014-07-07 18:06:18 -0400173 case kRGB_565_SkColorType:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800174 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, w, h, 0, GL_RGB,
175 GL_UNSIGNED_SHORT_5_6_5, p);
176 break;
177 default:
178 break;
179 }
180
181 glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_CROP_RECT_OES, crop);
182 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
183 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
184 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
185 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700186
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800187 return NO_ERROR;
188}
189
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700190status_t BootAnimation::initTexture(FileMap* map, int* width, int* height)
Mathias Agopiana8826d62009-10-01 03:10:14 -0700191{
Mathias Agopiana8826d62009-10-01 03:10:14 -0700192 SkBitmap bitmap;
Damien Bargiacchif67b3172016-09-07 20:17:14 -0700193 sk_sp<SkData> data = SkData::MakeWithoutCopy(map->getDataPtr(),
194 map->getDataLength());
Matt Sarett8898c162016-03-24 16:11:01 -0400195 sk_sp<SkImage> image = SkImage::MakeFromEncoded(data);
196 image->asLegacyBitmap(&bitmap, SkImage::kRO_LegacyBitmapMode);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700197
Mykola Kondratenko0c1eeb32014-04-15 09:35:44 +0200198 // FileMap memory is never released until application exit.
199 // Release it now as the texture is already loaded and the memory used for
200 // the packed resource can be released.
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700201 delete map;
Mykola Kondratenko0c1eeb32014-04-15 09:35:44 +0200202
Mathias Agopiana8826d62009-10-01 03:10:14 -0700203 const int w = bitmap.width();
204 const int h = bitmap.height();
205 const void* p = bitmap.getPixels();
206
207 GLint crop[4] = { 0, h, w, -h };
208 int tw = 1 << (31 - __builtin_clz(w));
209 int th = 1 << (31 - __builtin_clz(h));
210 if (tw < w) tw <<= 1;
211 if (th < h) th <<= 1;
212
Mike Reed42a1d082014-07-07 18:06:18 -0400213 switch (bitmap.colorType()) {
214 case kN32_SkColorType:
Sai Kiran Korwar27167492015-07-07 20:00:06 +0530215 if (!mUseNpotTextures && (tw != w || th != h)) {
Mathias Agopiana8826d62009-10-01 03:10:14 -0700216 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, tw, th, 0, GL_RGBA,
217 GL_UNSIGNED_BYTE, 0);
218 glTexSubImage2D(GL_TEXTURE_2D, 0,
219 0, 0, w, h, GL_RGBA, GL_UNSIGNED_BYTE, p);
220 } else {
Sai Kiran Korwar27167492015-07-07 20:00:06 +0530221 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA,
Mathias Agopiana8826d62009-10-01 03:10:14 -0700222 GL_UNSIGNED_BYTE, p);
223 }
224 break;
225
Mike Reed42a1d082014-07-07 18:06:18 -0400226 case kRGB_565_SkColorType:
Sai Kiran Korwar27167492015-07-07 20:00:06 +0530227 if (!mUseNpotTextures && (tw != w || th != h)) {
Mathias Agopiana8826d62009-10-01 03:10:14 -0700228 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, tw, th, 0, GL_RGB,
229 GL_UNSIGNED_SHORT_5_6_5, 0);
230 glTexSubImage2D(GL_TEXTURE_2D, 0,
231 0, 0, w, h, GL_RGB, GL_UNSIGNED_SHORT_5_6_5, p);
232 } else {
Sai Kiran Korwar27167492015-07-07 20:00:06 +0530233 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, w, h, 0, GL_RGB,
Mathias Agopiana8826d62009-10-01 03:10:14 -0700234 GL_UNSIGNED_SHORT_5_6_5, p);
235 }
236 break;
237 default:
238 break;
239 }
240
241 glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_CROP_RECT_OES, crop);
242
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700243 *width = w;
244 *height = h;
245
Mathias Agopiana8826d62009-10-01 03:10:14 -0700246 return NO_ERROR;
247}
248
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800249status_t BootAnimation::readyToRun() {
250 mAssets.addDefaultAssets();
251
Jeff Brown0b722fe2012-08-24 22:40:14 -0700252 sp<IBinder> dtoken(SurfaceComposerClient::getBuiltInDisplay(
253 ISurfaceComposer::eDisplayIdMain));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800254 DisplayInfo dinfo;
Jeff Brown0b722fe2012-08-24 22:40:14 -0700255 status_t status = SurfaceComposerClient::getDisplayInfo(dtoken, &dinfo);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800256 if (status)
257 return -1;
258
259 // create the native surface
Jeff Brown0b722fe2012-08-24 22:40:14 -0700260 sp<SurfaceControl> control = session()->createSurface(String8("BootAnimation"),
261 dinfo.w, dinfo.h, PIXEL_FORMAT_RGB_565);
Mathias Agopian439863f2011-06-28 19:09:31 -0700262
263 SurfaceComposerClient::openGlobalTransaction();
Mathias Agopian17f638b2009-04-16 20:04:08 -0700264 control->setLayer(0x40000000);
Mathias Agopian439863f2011-06-28 19:09:31 -0700265 SurfaceComposerClient::closeGlobalTransaction();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800266
Mathias Agopian17f638b2009-04-16 20:04:08 -0700267 sp<Surface> s = control->getSurface();
268
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800269 // initialize opengl and egl
Mathias Agopian738b9a42009-08-06 16:41:02 -0700270 const EGLint attribs[] = {
Mathias Agopian1b253b72011-08-15 15:20:22 -0700271 EGL_RED_SIZE, 8,
272 EGL_GREEN_SIZE, 8,
273 EGL_BLUE_SIZE, 8,
Mathias Agopiana8826d62009-10-01 03:10:14 -0700274 EGL_DEPTH_SIZE, 0,
275 EGL_NONE
Mathias Agopian738b9a42009-08-06 16:41:02 -0700276 };
Andreas Gampecfedceb2014-09-30 21:48:18 -0700277 EGLint w, h;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800278 EGLint numConfigs;
279 EGLConfig config;
280 EGLSurface surface;
281 EGLContext context;
Mathias Agopian627e7b52009-05-21 19:21:59 -0700282
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800283 EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
Mathias Agopian627e7b52009-05-21 19:21:59 -0700284
285 eglInitialize(display, 0, 0);
Mathias Agopian1b253b72011-08-15 15:20:22 -0700286 eglChooseConfig(display, attribs, &config, 1, &numConfigs);
Mathias Agopian1473f462009-04-10 14:24:30 -0700287 surface = eglCreateWindowSurface(display, config, s.get(), NULL);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800288 context = eglCreateContext(display, config, NULL, NULL);
289 eglQuerySurface(display, surface, EGL_WIDTH, &w);
290 eglQuerySurface(display, surface, EGL_HEIGHT, &h);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700291
Mathias Agopianabac0102009-07-31 14:47:00 -0700292 if (eglMakeCurrent(display, surface, surface, context) == EGL_FALSE)
293 return NO_INIT;
Mathias Agopiana8826d62009-10-01 03:10:14 -0700294
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800295 mDisplay = display;
296 mContext = context;
297 mSurface = surface;
298 mWidth = w;
299 mHeight = h;
Mathias Agopian17f638b2009-04-16 20:04:08 -0700300 mFlingerSurfaceControl = control;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800301 mFlingerSurface = s;
302
Elliott Hughesc367d482013-10-29 13:12:55 -0700303 // If the device has encryption turned on or is in process
Jason parksbd9a08d2011-01-31 15:04:34 -0600304 // of being encrypted we show the encrypted boot animation.
305 char decrypt[PROPERTY_VALUE_MAX];
306 property_get("vold.decrypt", decrypt, "");
307
Keun-young Parkb5938422017-03-23 13:46:24 -0700308 bool encryptedAnimation = atoi(decrypt) != 0 ||
309 !strcmp("trigger_restart_min_framework", decrypt);
Jason parksbd9a08d2011-01-31 15:04:34 -0600310
Keun-young Parkb5938422017-03-23 13:46:24 -0700311 if (!mShuttingDown && encryptedAnimation &&
312 (access(SYSTEM_ENCRYPTED_BOOTANIMATION_FILE, R_OK) == 0)) {
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700313 mZipFileName = SYSTEM_ENCRYPTED_BOOTANIMATION_FILE;
Keun-young Parkb5938422017-03-23 13:46:24 -0700314 return NO_ERROR;
Jason parksbd9a08d2011-01-31 15:04:34 -0600315 }
Keun-young Parkb5938422017-03-23 13:46:24 -0700316 static const char* bootFiles[] = {OEM_BOOTANIMATION_FILE, SYSTEM_BOOTANIMATION_FILE};
317 static const char* shutdownFiles[] =
318 {OEM_SHUTDOWNANIMATION_FILE, SYSTEM_SHUTDOWNANIMATION_FILE};
319
320 for (const char* f : (!mShuttingDown ? bootFiles : shutdownFiles)) {
321 if (access(f, R_OK) == 0) {
322 mZipFileName = f;
323 return NO_ERROR;
324 }
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700325 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800326 return NO_ERROR;
327}
328
Mathias Agopiana8826d62009-10-01 03:10:14 -0700329bool BootAnimation::threadLoop()
330{
331 bool r;
Narayan Kamathafd31e02013-12-03 13:16:03 +0000332 // We have no bootanimation file, so we use the stock android logo
333 // animation.
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700334 if (mZipFileName.isEmpty()) {
Mathias Agopiana8826d62009-10-01 03:10:14 -0700335 r = android();
336 } else {
337 r = movie();
338 }
339
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800340 eglMakeCurrent(mDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
341 eglDestroyContext(mDisplay, mContext);
342 eglDestroySurface(mDisplay, mSurface);
Mathias Agopian6cf0db22009-04-17 19:36:26 -0700343 mFlingerSurface.clear();
Mathias Agopian17f638b2009-04-16 20:04:08 -0700344 mFlingerSurfaceControl.clear();
Mathias Agopian627e7b52009-05-21 19:21:59 -0700345 eglTerminate(mDisplay);
Sai Kiran Korwar3eee9fb2015-06-16 17:13:35 +0530346 eglReleaseThread();
Mathias Agopian627e7b52009-05-21 19:21:59 -0700347 IPCThreadState::self()->stopProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800348 return r;
349}
350
Mathias Agopiana8826d62009-10-01 03:10:14 -0700351bool BootAnimation::android()
352{
Keun-young Parkd1794cd2017-04-04 12:21:19 -0700353 ALOGD("%sAnimationShownTiming start time: %" PRId64 "ms", mShuttingDown ? "Shutdown" : "Boot",
354 elapsedRealtime());
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700355 initTexture(&mAndroid[0], mAssets, "images/android-logo-mask.png");
356 initTexture(&mAndroid[1], mAssets, "images/android-logo-shine.png");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800357
Ed Coyne7464ac92017-06-08 12:26:48 -0700358 mCallbacks->init({});
359
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800360 // clear screen
Mathias Agopiana8826d62009-10-01 03:10:14 -0700361 glShadeModel(GL_FLAT);
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700362 glDisable(GL_DITHER);
363 glDisable(GL_SCISSOR_TEST);
Mathias Agopian59f19e42011-05-06 19:22:12 -0700364 glClearColor(0,0,0,1);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800365 glClear(GL_COLOR_BUFFER_BIT);
366 eglSwapBuffers(mDisplay, mSurface);
367
Mathias Agopiana8826d62009-10-01 03:10:14 -0700368 glEnable(GL_TEXTURE_2D);
369 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
370
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700371 const GLint xc = (mWidth - mAndroid[0].w) / 2;
372 const GLint yc = (mHeight - mAndroid[0].h) / 2;
373 const Rect updateRect(xc, yc, xc + mAndroid[0].w, yc + mAndroid[0].h);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800374
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800375 glScissor(updateRect.left, mHeight - updateRect.bottom, updateRect.width(),
376 updateRect.height());
377
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700378 // Blend state
379 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
380 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
381
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800382 const nsecs_t startTime = systemTime();
383 do {
Mathias Agopian13796652009-03-24 22:49:21 -0700384 nsecs_t now = systemTime();
385 double time = now - startTime;
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700386 float t = 4.0f * float(time / us2ns(16667)) / mAndroid[1].w;
387 GLint offset = (1 - (t - floorf(t))) * mAndroid[1].w;
388 GLint x = xc - offset;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800389
Mathias Agopian81668642009-07-28 11:41:30 -0700390 glDisable(GL_SCISSOR_TEST);
391 glClear(GL_COLOR_BUFFER_BIT);
392
393 glEnable(GL_SCISSOR_TEST);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800394 glDisable(GL_BLEND);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800395 glBindTexture(GL_TEXTURE_2D, mAndroid[1].name);
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700396 glDrawTexiOES(x, yc, 0, mAndroid[1].w, mAndroid[1].h);
397 glDrawTexiOES(x + mAndroid[1].w, yc, 0, mAndroid[1].w, mAndroid[1].h);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800398
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700399 glEnable(GL_BLEND);
400 glBindTexture(GL_TEXTURE_2D, mAndroid[0].name);
401 glDrawTexiOES(xc, yc, 0, mAndroid[0].w, mAndroid[0].h);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800402
Mathias Agopian627e7b52009-05-21 19:21:59 -0700403 EGLBoolean res = eglSwapBuffers(mDisplay, mSurface);
404 if (res == EGL_FALSE)
405 break;
406
Mathias Agopian13796652009-03-24 22:49:21 -0700407 // 12fps: don't animate too fast to preserve CPU
408 const nsecs_t sleepTime = 83333 - ns2us(systemTime() - now);
409 if (sleepTime > 0)
Mathias Agopiana8826d62009-10-01 03:10:14 -0700410 usleep(sleepTime);
Kevin Hesterd3782b22012-04-26 10:38:55 -0700411
412 checkExit();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800413 } while (!exitPending());
414
415 glDeleteTextures(1, &mAndroid[0].name);
416 glDeleteTextures(1, &mAndroid[1].name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800417 return false;
418}
419
Kevin Hesterd3782b22012-04-26 10:38:55 -0700420void BootAnimation::checkExit() {
421 // Allow surface flinger to gracefully request shutdown
422 char value[PROPERTY_VALUE_MAX];
423 property_get(EXIT_PROP_NAME, value, "0");
424 int exitnow = atoi(value);
425 if (exitnow) {
426 requestExit();
Ed Coyne7464ac92017-06-08 12:26:48 -0700427 mCallbacks->shutdown();
Kevin Hesterd3782b22012-04-26 10:38:55 -0700428 }
429}
430
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700431bool BootAnimation::validClock(const Animation::Part& part) {
432 return part.clockPosX != TEXT_MISSING_VALUE && part.clockPosY != TEXT_MISSING_VALUE;
433}
434
435bool parseTextCoord(const char* str, int* dest) {
436 if (strcmp("c", str) == 0) {
437 *dest = TEXT_CENTER_VALUE;
438 return true;
439 }
440
441 char* end;
442 int val = (int) strtol(str, &end, 0);
443 if (end == str || *end != '\0' || val == INT_MAX || val == INT_MIN) {
444 return false;
445 }
446 *dest = val;
447 return true;
448}
449
450// Parse two position coordinates. If only string is non-empty, treat it as the y value.
451void parsePosition(const char* str1, const char* str2, int* x, int* y) {
452 bool success = false;
453 if (strlen(str1) == 0) { // No values were specified
454 // success = false
455 } else if (strlen(str2) == 0) { // we have only one value
456 if (parseTextCoord(str1, y)) {
457 *x = TEXT_CENTER_VALUE;
458 success = true;
459 }
460 } else {
461 if (parseTextCoord(str1, x) && parseTextCoord(str2, y)) {
462 success = true;
463 }
464 }
465
466 if (!success) {
467 *x = TEXT_MISSING_VALUE;
468 *y = TEXT_MISSING_VALUE;
469 }
470}
471
Jesse Hall083b84c2014-09-22 10:51:09 -0700472// Parse a color represented as an HTML-style 'RRGGBB' string: each pair of
473// characters in str is a hex number in [0, 255], which are converted to
474// floating point values in the range [0.0, 1.0] and placed in the
475// corresponding elements of color.
476//
477// If the input string isn't valid, parseColor returns false and color is
478// left unchanged.
479static bool parseColor(const char str[7], float color[3]) {
480 float tmpColor[3];
481 for (int i = 0; i < 3; i++) {
482 int val = 0;
483 for (int j = 0; j < 2; j++) {
484 val *= 16;
485 char c = str[2*i + j];
486 if (c >= '0' && c <= '9') val += c - '0';
487 else if (c >= 'A' && c <= 'F') val += (c - 'A') + 10;
488 else if (c >= 'a' && c <= 'f') val += (c - 'a') + 10;
489 else return false;
490 }
491 tmpColor[i] = static_cast<float>(val) / 255.0f;
492 }
493 memcpy(color, tmpColor, sizeof(tmpColor));
494 return true;
495}
496
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700497
498static bool readFile(ZipFileRO* zip, const char* name, String8& outString)
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700499{
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700500 ZipEntryRO entry = zip->findEntryByName(name);
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700501 ALOGE_IF(!entry, "couldn't find %s", name);
502 if (!entry) {
503 return false;
504 }
505
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700506 FileMap* entryMap = zip->createEntryFileMap(entry);
507 zip->releaseEntry(entry);
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700508 ALOGE_IF(!entryMap, "entryMap is null");
509 if (!entryMap) {
510 return false;
511 }
512
513 outString.setTo((char const*)entryMap->getDataPtr(), entryMap->getDataLength());
Narayan Kamath688ff4c2015-02-23 15:47:54 +0000514 delete entryMap;
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700515 return true;
516}
517
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700518// The font image should be a 96x2 array of character images. The
519// columns are the printable ASCII characters 0x20 - 0x7f. The
520// top row is regular text; the bottom row is bold.
521status_t BootAnimation::initFont(Font* font, const char* fallback) {
522 status_t status = NO_ERROR;
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800523
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700524 if (font->map != nullptr) {
525 glGenTextures(1, &font->texture.name);
526 glBindTexture(GL_TEXTURE_2D, font->texture.name);
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800527
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700528 status = initTexture(font->map, &font->texture.w, &font->texture.h);
529
530 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
531 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
532 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
533 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
534 } else if (fallback != nullptr) {
535 status = initTexture(&font->texture, mAssets, fallback);
536 } else {
537 return NO_INIT;
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800538 }
539
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700540 if (status == NO_ERROR) {
541 font->char_width = font->texture.w / FONT_NUM_COLS;
542 font->char_height = font->texture.h / FONT_NUM_ROWS / 2; // There are bold and regular rows
543 }
544
545 return status;
546}
547
548void BootAnimation::drawText(const char* str, const Font& font, bool bold, int* x, int* y) {
549 glEnable(GL_BLEND); // Allow us to draw on top of the animation
550 glBindTexture(GL_TEXTURE_2D, font.texture.name);
551
552 const int len = strlen(str);
553 const int strWidth = font.char_width * len;
554
555 if (*x == TEXT_CENTER_VALUE) {
556 *x = (mWidth - strWidth) / 2;
557 } else if (*x < 0) {
558 *x = mWidth + *x - strWidth;
559 }
560 if (*y == TEXT_CENTER_VALUE) {
561 *y = (mHeight - font.char_height) / 2;
562 } else if (*y < 0) {
563 *y = mHeight + *y - font.char_height;
564 }
565
566 int cropRect[4] = { 0, 0, font.char_width, -font.char_height };
567
568 for (int i = 0; i < len; i++) {
569 char c = str[i];
570
571 if (c < FONT_BEGIN_CHAR || c > FONT_END_CHAR) {
572 c = '?';
573 }
574
575 // Crop the texture to only the pixels in the current glyph
576 const int charPos = (c - FONT_BEGIN_CHAR); // Position in the list of valid characters
577 const int row = charPos / FONT_NUM_COLS;
578 const int col = charPos % FONT_NUM_COLS;
579 cropRect[0] = col * font.char_width; // Left of column
580 cropRect[1] = row * font.char_height * 2; // Top of row
581 // Move down to bottom of regular (one char_heigh) or bold (two char_heigh) line
582 cropRect[1] += bold ? 2 * font.char_height : font.char_height;
583 glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_CROP_RECT_OES, cropRect);
584
585 glDrawTexiOES(*x, *y, 0, font.char_width, font.char_height);
586
587 *x += font.char_width;
588 }
589
590 glDisable(GL_BLEND); // Return to the animation's default behaviour
591 glBindTexture(GL_TEXTURE_2D, 0);
592}
593
Damien Bargiacchi9071db12016-10-28 17:38:22 -0700594// We render 12 or 24 hour time.
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700595void BootAnimation::drawClock(const Font& font, const int xPos, const int yPos) {
Damien Bargiacchi9071db12016-10-28 17:38:22 -0700596 static constexpr char TIME_FORMAT_12[] = "%l:%M";
597 static constexpr char TIME_FORMAT_24[] = "%H:%M";
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700598 static constexpr int TIME_LENGTH = 6;
599
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800600 time_t rawtime;
601 time(&rawtime);
602 struct tm* timeInfo = localtime(&rawtime);
603
604 char timeBuff[TIME_LENGTH];
Damien Bargiacchi9071db12016-10-28 17:38:22 -0700605 const char* timeFormat = mTimeFormat12Hour ? TIME_FORMAT_12 : TIME_FORMAT_24;
606 size_t length = strftime(timeBuff, TIME_LENGTH, timeFormat, timeInfo);
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800607
608 if (length != TIME_LENGTH - 1) {
609 ALOGE("Couldn't format time; abandoning boot animation clock");
610 mClockEnabled = false;
611 return;
612 }
613
Damien Bargiacchi45a76442016-12-05 18:02:18 -0800614 char* out = timeBuff[0] == ' ' ? &timeBuff[1] : &timeBuff[0];
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700615 int x = xPos;
616 int y = yPos;
Damien Bargiacchi45a76442016-12-05 18:02:18 -0800617 drawText(out, font, false, &x, &y);
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800618}
619
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700620bool BootAnimation::parseAnimationDesc(Animation& animation)
Mathias Agopiana8826d62009-10-01 03:10:14 -0700621{
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700622 String8 desString;
623
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700624 if (!readFile(animation.zip, "desc.txt", desString)) {
Narayan Kamathafd31e02013-12-03 13:16:03 +0000625 return false;
626 }
Mathias Agopiana8826d62009-10-01 03:10:14 -0700627 char const* s = desString.string();
628
Mathias Agopiana8826d62009-10-01 03:10:14 -0700629 // Parse the description file
630 for (;;) {
631 const char* endl = strstr(s, "\n");
Andreas Gampecfedceb2014-09-30 21:48:18 -0700632 if (endl == NULL) break;
Mathias Agopiana8826d62009-10-01 03:10:14 -0700633 String8 line(s, endl - s);
634 const char* l = line.string();
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800635 int fps = 0;
636 int width = 0;
637 int height = 0;
638 int count = 0;
639 int pause = 0;
Narayan Kamathafd31e02013-12-03 13:16:03 +0000640 char path[ANIM_ENTRY_NAME_MAX];
Jesse Hall083b84c2014-09-22 10:51:09 -0700641 char color[7] = "000000"; // default to black if unspecified
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700642 char clockPos1[TEXT_POS_LEN_MAX + 1] = "";
643 char clockPos2[TEXT_POS_LEN_MAX + 1] = "";
Jesse Hall083b84c2014-09-22 10:51:09 -0700644
Kevin Hesterd3782b22012-04-26 10:38:55 -0700645 char pathType;
Mathias Agopiana8826d62009-10-01 03:10:14 -0700646 if (sscanf(l, "%d %d %d", &width, &height, &fps) == 3) {
Jesse Hall083b84c2014-09-22 10:51:09 -0700647 // ALOGD("> w=%d, h=%d, fps=%d", width, height, fps);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700648 animation.width = width;
649 animation.height = height;
650 animation.fps = fps;
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700651 } else if (sscanf(l, " %c %d %d %s #%6s %16s %16s",
652 &pathType, &count, &pause, path, color, clockPos1, clockPos2) >= 4) {
653 //ALOGD("> type=%c, count=%d, pause=%d, path=%s, color=%s, clockPos1=%s, clockPos2=%s",
654 // pathType, count, pause, path, color, clockPos1, clockPos2);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700655 Animation::Part part;
Kevin Hesterd3782b22012-04-26 10:38:55 -0700656 part.playUntilComplete = pathType == 'c';
Mathias Agopiana8826d62009-10-01 03:10:14 -0700657 part.count = count;
658 part.pause = pause;
659 part.path = path;
Geoffrey Pitschd6d9a1d2016-06-08 00:38:58 -0700660 part.audioData = NULL;
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700661 part.animation = NULL;
Jesse Hall083b84c2014-09-22 10:51:09 -0700662 if (!parseColor(color, part.backgroundColor)) {
663 ALOGE("> invalid color '#%s'", color);
664 part.backgroundColor[0] = 0.0f;
665 part.backgroundColor[1] = 0.0f;
666 part.backgroundColor[2] = 0.0f;
667 }
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700668 parsePosition(clockPos1, clockPos2, &part.clockPosX, &part.clockPosY);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700669 animation.parts.add(part);
670 }
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700671 else if (strcmp(l, "$SYSTEM") == 0) {
672 // ALOGD("> SYSTEM");
673 Animation::Part part;
674 part.playUntilComplete = false;
675 part.count = 1;
676 part.pause = 0;
Geoffrey Pitschd6d9a1d2016-06-08 00:38:58 -0700677 part.audioData = NULL;
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700678 part.animation = loadAnimation(String8(SYSTEM_BOOTANIMATION_FILE));
679 if (part.animation != NULL)
680 animation.parts.add(part);
681 }
Mathias Agopiana8826d62009-10-01 03:10:14 -0700682 s = ++endl;
683 }
684
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700685 return true;
686}
687
688bool BootAnimation::preloadZip(Animation& animation)
689{
Mathias Agopiana8826d62009-10-01 03:10:14 -0700690 // read all the data structures
691 const size_t pcount = animation.parts.size();
Narayan Kamathafd31e02013-12-03 13:16:03 +0000692 void *cookie = NULL;
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400693 ZipFileRO* zip = animation.zip;
694 if (!zip->startIteration(&cookie)) {
Narayan Kamathafd31e02013-12-03 13:16:03 +0000695 return false;
696 }
697
698 ZipEntryRO entry;
699 char name[ANIM_ENTRY_NAME_MAX];
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400700 while ((entry = zip->nextEntry(cookie)) != NULL) {
701 const int foundEntryName = zip->getEntryFileName(entry, name, ANIM_ENTRY_NAME_MAX);
Narayan Kamathafd31e02013-12-03 13:16:03 +0000702 if (foundEntryName > ANIM_ENTRY_NAME_MAX || foundEntryName == -1) {
703 ALOGE("Error fetching entry file name");
704 continue;
705 }
706
707 const String8 entryName(name);
708 const String8 path(entryName.getPathDir());
709 const String8 leaf(entryName.getPathLeaf());
710 if (leaf.size() > 0) {
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700711 if (entryName == CLOCK_FONT_ZIP_NAME) {
712 FileMap* map = zip->createEntryFileMap(entry);
713 if (map) {
714 animation.clockFont.map = map;
715 }
716 continue;
717 }
718
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400719 for (size_t j = 0; j < pcount; j++) {
Narayan Kamathafd31e02013-12-03 13:16:03 +0000720 if (path == animation.parts[j].path) {
Narayan Kamath4600dd02015-06-16 12:02:57 +0100721 uint16_t method;
Narayan Kamathafd31e02013-12-03 13:16:03 +0000722 // supports only stored png files
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400723 if (zip->getEntryInfo(entry, &method, NULL, NULL, NULL, NULL, NULL)) {
Narayan Kamathafd31e02013-12-03 13:16:03 +0000724 if (method == ZipFileRO::kCompressStored) {
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400725 FileMap* map = zip->createEntryFileMap(entry);
Narayan Kamathafd31e02013-12-03 13:16:03 +0000726 if (map) {
Narayan Kamathafd31e02013-12-03 13:16:03 +0000727 Animation::Part& part(animation.parts.editItemAt(j));
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700728 if (leaf == "audio.wav") {
729 // a part may have at most one audio file
Geoffrey Pitschd6d9a1d2016-06-08 00:38:58 -0700730 part.audioData = (uint8_t *)map->getDataPtr();
731 part.audioLength = map->getDataLength();
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400732 } else if (leaf == "trim.txt") {
733 part.trimData.setTo((char const*)map->getDataPtr(),
734 map->getDataLength());
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700735 } else {
736 Animation::Frame frame;
737 frame.name = leaf;
738 frame.map = map;
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400739 frame.trimWidth = animation.width;
740 frame.trimHeight = animation.height;
741 frame.trimX = 0;
742 frame.trimY = 0;
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700743 part.frames.add(frame);
744 }
Mathias Agopiana8826d62009-10-01 03:10:14 -0700745 }
Geoffrey Pitschd6d9a1d2016-06-08 00:38:58 -0700746 } else {
747 ALOGE("bootanimation.zip is compressed; must be only stored");
Mathias Agopiana8826d62009-10-01 03:10:14 -0700748 }
749 }
750 }
751 }
752 }
753 }
754
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400755 // If there is trimData present, override the positioning defaults.
756 for (Animation::Part& part : animation.parts) {
757 const char* trimDataStr = part.trimData.string();
758 for (size_t frameIdx = 0; frameIdx < part.frames.size(); frameIdx++) {
759 const char* endl = strstr(trimDataStr, "\n");
760 // No more trimData for this part.
761 if (endl == NULL) {
762 break;
763 }
764 String8 line(trimDataStr, endl - trimDataStr);
765 const char* lineStr = line.string();
766 trimDataStr = ++endl;
767 int width = 0, height = 0, x = 0, y = 0;
768 if (sscanf(lineStr, "%dx%d+%d+%d", &width, &height, &x, &y) == 4) {
769 Animation::Frame& frame(part.frames.editItemAt(frameIdx));
770 frame.trimWidth = width;
771 frame.trimHeight = height;
772 frame.trimX = x;
773 frame.trimY = y;
774 } else {
775 ALOGE("Error parsing trim.txt, line: %s", lineStr);
776 break;
777 }
778 }
779 }
780
Ed Coyne7464ac92017-06-08 12:26:48 -0700781 mCallbacks->init(animation.parts);
Geoffrey Pitschd6d9a1d2016-06-08 00:38:58 -0700782
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400783 zip->endIteration(cookie);
Narayan Kamathafd31e02013-12-03 13:16:03 +0000784
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700785 return true;
786}
787
788bool BootAnimation::movie()
789{
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700790 Animation* animation = loadAnimation(mZipFileName);
791 if (animation == NULL)
792 return false;
793
Damien Bargiacchi97480862016-03-29 14:55:55 -0700794 bool anyPartHasClock = false;
795 for (size_t i=0; i < animation->parts.size(); i++) {
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700796 if(validClock(animation->parts[i])) {
Damien Bargiacchi97480862016-03-29 14:55:55 -0700797 anyPartHasClock = true;
798 break;
799 }
800 }
801 if (!anyPartHasClock) {
802 mClockEnabled = false;
803 }
804
Sai Kiran Korwar27167492015-07-07 20:00:06 +0530805 // Check if npot textures are supported
806 mUseNpotTextures = false;
807 String8 gl_extensions;
808 const char* exts = reinterpret_cast<const char*>(glGetString(GL_EXTENSIONS));
809 if (!exts) {
810 glGetError();
811 } else {
812 gl_extensions.setTo(exts);
813 if ((gl_extensions.find("GL_ARB_texture_non_power_of_two") != -1) ||
814 (gl_extensions.find("GL_OES_texture_npot") != -1)) {
815 mUseNpotTextures = true;
816 }
817 }
818
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800819 // Blend required to draw time on top of animation frames.
820 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700821 glShadeModel(GL_FLAT);
822 glDisable(GL_DITHER);
823 glDisable(GL_SCISSOR_TEST);
824 glDisable(GL_BLEND);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700825
826 glBindTexture(GL_TEXTURE_2D, 0);
827 glEnable(GL_TEXTURE_2D);
828 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
829 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
830 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
831 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
832 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
833
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700834 bool clockFontInitialized = false;
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800835 if (mClockEnabled) {
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700836 clockFontInitialized =
837 (initFont(&animation->clockFont, CLOCK_FONT_ASSET) == NO_ERROR);
838 mClockEnabled = clockFontInitialized;
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800839 }
840
Damien Bargiacchi97480862016-03-29 14:55:55 -0700841 if (mClockEnabled && !updateIsTimeAccurate()) {
842 mTimeCheckThread = new TimeCheckThread(this);
843 mTimeCheckThread->run("BootAnimation::TimeCheckThread", PRIORITY_NORMAL);
844 }
845
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700846 playAnimation(*animation);
Damien Bargiacchi97480862016-03-29 14:55:55 -0700847
Geoffrey Pitscha9173532016-07-19 14:56:39 -0400848 if (mTimeCheckThread != nullptr) {
Damien Bargiacchi97480862016-03-29 14:55:55 -0700849 mTimeCheckThread->requestExit();
Geoffrey Pitscha9173532016-07-19 14:56:39 -0400850 mTimeCheckThread = nullptr;
851 }
852
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700853 releaseAnimation(animation);
854
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700855 if (clockFontInitialized) {
856 glDeleteTextures(1, &animation->clockFont.texture.name);
Andriy Naborskyy815e51d2016-03-24 16:43:34 -0700857 }
858
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700859 return false;
860}
861
862bool BootAnimation::playAnimation(const Animation& animation)
863{
864 const size_t pcount = animation.parts.size();
Mathias Agopiana8826d62009-10-01 03:10:14 -0700865 nsecs_t frameDuration = s2ns(1) / animation.fps;
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400866 const int animationX = (mWidth - animation.width) / 2;
867 const int animationY = (mHeight - animation.height) / 2;
Mathias Agopian9f3020d2009-11-06 16:30:18 -0800868
Keun-young Parkd1794cd2017-04-04 12:21:19 -0700869 ALOGD("%sAnimationShownTiming start time: %" PRId64 "ms", mShuttingDown ? "Shutdown" : "Boot",
870 elapsedRealtime());
Narayan Kamathafd31e02013-12-03 13:16:03 +0000871 for (size_t i=0 ; i<pcount ; i++) {
Mathias Agopiana8826d62009-10-01 03:10:14 -0700872 const Animation::Part& part(animation.parts[i]);
873 const size_t fcount = part.frames.size();
874 glBindTexture(GL_TEXTURE_2D, 0);
875
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700876 // Handle animation package
877 if (part.animation != NULL) {
878 playAnimation(*part.animation);
879 if (exitPending())
880 break;
881 continue; //to next part
882 }
883
Mathias Agopiana8826d62009-10-01 03:10:14 -0700884 for (int r=0 ; !part.count || r<part.count ; r++) {
Kevin Hesterd3782b22012-04-26 10:38:55 -0700885 // Exit any non playuntil complete parts immediately
886 if(exitPending() && !part.playUntilComplete)
887 break;
888
Ed Coyne7464ac92017-06-08 12:26:48 -0700889 mCallbacks->playPart(i, part, r);
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700890
Jesse Hall083b84c2014-09-22 10:51:09 -0700891 glClearColor(
892 part.backgroundColor[0],
893 part.backgroundColor[1],
894 part.backgroundColor[2],
895 1.0f);
896
Narayan Kamathafd31e02013-12-03 13:16:03 +0000897 for (size_t j=0 ; j<fcount && (!exitPending() || part.playUntilComplete) ; j++) {
Mathias Agopiana8826d62009-10-01 03:10:14 -0700898 const Animation::Frame& frame(part.frames[j]);
Mathias Agopiandb7dd2a2012-05-12 15:08:21 -0700899 nsecs_t lastFrame = systemTime();
Mathias Agopiana8826d62009-10-01 03:10:14 -0700900
901 if (r > 0) {
902 glBindTexture(GL_TEXTURE_2D, frame.tid);
903 } else {
904 if (part.count != 1) {
905 glGenTextures(1, &frame.tid);
906 glBindTexture(GL_TEXTURE_2D, frame.tid);
907 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
908 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
909 }
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700910 int w, h;
911 initTexture(frame.map, &w, &h);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700912 }
913
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400914 const int xc = animationX + frame.trimX;
915 const int yc = animationY + frame.trimY;
916 Region clearReg(Rect(mWidth, mHeight));
917 clearReg.subtractSelf(Rect(xc, yc, xc+frame.trimWidth, yc+frame.trimHeight));
Mathias Agopian9f3020d2009-11-06 16:30:18 -0800918 if (!clearReg.isEmpty()) {
919 Region::const_iterator head(clearReg.begin());
920 Region::const_iterator tail(clearReg.end());
921 glEnable(GL_SCISSOR_TEST);
922 while (head != tail) {
Andreas Gampecfedceb2014-09-30 21:48:18 -0700923 const Rect& r2(*head++);
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400924 glScissor(r2.left, mHeight - r2.bottom, r2.width(), r2.height());
Mathias Agopian9f3020d2009-11-06 16:30:18 -0800925 glClear(GL_COLOR_BUFFER_BIT);
926 }
927 glDisable(GL_SCISSOR_TEST);
928 }
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400929 // specify the y center as ceiling((mHeight - frame.trimHeight) / 2)
930 // which is equivalent to mHeight - (yc + frame.trimHeight)
931 glDrawTexiOES(xc, mHeight - (yc + frame.trimHeight),
932 0, frame.trimWidth, frame.trimHeight);
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700933 if (mClockEnabled && mTimeIsAccurate && validClock(part)) {
934 drawClock(animation.clockFont, part.clockPosX, part.clockPosY);
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800935 }
936
Mathias Agopiana8826d62009-10-01 03:10:14 -0700937 eglSwapBuffers(mDisplay, mSurface);
938
939 nsecs_t now = systemTime();
940 nsecs_t delay = frameDuration - (now - lastFrame);
Mathias Agopiandb7dd2a2012-05-12 15:08:21 -0700941 //ALOGD("%lld, %lld", ns2ms(now - lastFrame), ns2ms(delay));
Mathias Agopiana8826d62009-10-01 03:10:14 -0700942 lastFrame = now;
Mathias Agopiandb7dd2a2012-05-12 15:08:21 -0700943
944 if (delay > 0) {
945 struct timespec spec;
946 spec.tv_sec = (now + delay) / 1000000000;
947 spec.tv_nsec = (now + delay) % 1000000000;
948 int err;
949 do {
950 err = clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &spec, NULL);
951 } while (err<0 && errno == EINTR);
952 }
Kevin Hesterd3782b22012-04-26 10:38:55 -0700953
954 checkExit();
Mathias Agopiana8826d62009-10-01 03:10:14 -0700955 }
Kevin Hesterd3782b22012-04-26 10:38:55 -0700956
Mathias Agopiana8826d62009-10-01 03:10:14 -0700957 usleep(part.pause * ns2us(frameDuration));
Kevin Hesterd3782b22012-04-26 10:38:55 -0700958
959 // For infinite parts, we've now played them at least once, so perhaps exit
960 if(exitPending() && !part.count)
961 break;
Mathias Agopiana8826d62009-10-01 03:10:14 -0700962 }
963
Geoffrey Pitsch2fb30fb2016-07-06 16:16:20 -0400964 }
965
966 // Free textures created for looping parts now that the animation is done.
967 for (const Animation::Part& part : animation.parts) {
Mathias Agopiana8826d62009-10-01 03:10:14 -0700968 if (part.count != 1) {
Geoffrey Pitsch2fb30fb2016-07-06 16:16:20 -0400969 const size_t fcount = part.frames.size();
970 for (size_t j = 0; j < fcount; j++) {
Mathias Agopiana8826d62009-10-01 03:10:14 -0700971 const Animation::Frame& frame(part.frames[j]);
972 glDeleteTextures(1, &frame.tid);
973 }
974 }
975 }
Geoffrey Pitschd6d9a1d2016-06-08 00:38:58 -0700976
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700977 return true;
Mathias Agopiana8826d62009-10-01 03:10:14 -0700978}
979
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700980void BootAnimation::releaseAnimation(Animation* animation) const
981{
982 for (Vector<Animation::Part>::iterator it = animation->parts.begin(),
983 e = animation->parts.end(); it != e; ++it) {
984 if (it->animation)
985 releaseAnimation(it->animation);
986 }
987 if (animation->zip)
988 delete animation->zip;
989 delete animation;
990}
991
992BootAnimation::Animation* BootAnimation::loadAnimation(const String8& fn)
993{
994 if (mLoadedFiles.indexOf(fn) >= 0) {
995 ALOGE("File \"%s\" is already loaded. Cyclic ref is not allowed",
996 fn.string());
997 return NULL;
998 }
999 ZipFileRO *zip = ZipFileRO::open(fn);
1000 if (zip == NULL) {
1001 ALOGE("Failed to open animation zip \"%s\": %s",
1002 fn.string(), strerror(errno));
1003 return NULL;
1004 }
1005
1006 Animation *animation = new Animation;
1007 animation->fileName = fn;
1008 animation->zip = zip;
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -07001009 animation->clockFont.map = nullptr;
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001010 mLoadedFiles.add(animation->fileName);
1011
1012 parseAnimationDesc(*animation);
Geoffrey Pitscha91a2d72016-07-12 14:46:19 -04001013 if (!preloadZip(*animation)) {
1014 return NULL;
1015 }
1016
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001017
1018 mLoadedFiles.remove(fn);
1019 return animation;
1020}
Damien Bargiacchi97480862016-03-29 14:55:55 -07001021
1022bool BootAnimation::updateIsTimeAccurate() {
1023 static constexpr long long MAX_TIME_IN_PAST = 60000LL * 60LL * 24LL * 30LL; // 30 days
1024 static constexpr long long MAX_TIME_IN_FUTURE = 60000LL * 90LL; // 90 minutes
1025
1026 if (mTimeIsAccurate) {
1027 return true;
1028 }
Keun-young Parkb5938422017-03-23 13:46:24 -07001029 if (mShuttingDown) return true;
Damien Bargiacchi97480862016-03-29 14:55:55 -07001030 struct stat statResult;
Damien Bargiacchi9071db12016-10-28 17:38:22 -07001031
1032 if(stat(TIME_FORMAT_12_HOUR_FLAG_FILE_PATH, &statResult) == 0) {
1033 mTimeFormat12Hour = true;
1034 }
1035
Damien Bargiacchi97480862016-03-29 14:55:55 -07001036 if(stat(ACCURATE_TIME_FLAG_FILE_PATH, &statResult) == 0) {
1037 mTimeIsAccurate = true;
1038 return true;
1039 }
1040
1041 FILE* file = fopen(LAST_TIME_CHANGED_FILE_PATH, "r");
1042 if (file != NULL) {
1043 long long lastChangedTime = 0;
1044 fscanf(file, "%lld", &lastChangedTime);
1045 fclose(file);
1046 if (lastChangedTime > 0) {
1047 struct timespec now;
1048 clock_gettime(CLOCK_REALTIME, &now);
1049 // Match the Java timestamp format
1050 long long rtcNow = (now.tv_sec * 1000LL) + (now.tv_nsec / 1000000LL);
Damien Bargiacchi96762812016-07-12 15:53:40 -07001051 if (ACCURATE_TIME_EPOCH < rtcNow
1052 && lastChangedTime > (rtcNow - MAX_TIME_IN_PAST)
1053 && lastChangedTime < (rtcNow + MAX_TIME_IN_FUTURE)) {
Damien Bargiacchi97480862016-03-29 14:55:55 -07001054 mTimeIsAccurate = true;
1055 }
1056 }
1057 }
1058
1059 return mTimeIsAccurate;
1060}
1061
1062BootAnimation::TimeCheckThread::TimeCheckThread(BootAnimation* bootAnimation) : Thread(false),
1063 mInotifyFd(-1), mSystemWd(-1), mTimeWd(-1), mBootAnimation(bootAnimation) {}
1064
1065BootAnimation::TimeCheckThread::~TimeCheckThread() {
1066 // mInotifyFd may be -1 but that's ok since we're not at risk of attempting to close a valid FD.
1067 close(mInotifyFd);
1068}
1069
1070bool BootAnimation::TimeCheckThread::threadLoop() {
1071 bool shouldLoop = doThreadLoop() && !mBootAnimation->mTimeIsAccurate
1072 && mBootAnimation->mClockEnabled;
1073 if (!shouldLoop) {
1074 close(mInotifyFd);
1075 mInotifyFd = -1;
1076 }
1077 return shouldLoop;
1078}
1079
1080bool BootAnimation::TimeCheckThread::doThreadLoop() {
1081 static constexpr int BUFF_LEN (10 * (sizeof(struct inotify_event) + NAME_MAX + 1));
1082
1083 // Poll instead of doing a blocking read so the Thread can exit if requested.
1084 struct pollfd pfd = { mInotifyFd, POLLIN, 0 };
1085 ssize_t pollResult = poll(&pfd, 1, 1000);
1086
1087 if (pollResult == 0) {
1088 return true;
1089 } else if (pollResult < 0) {
1090 ALOGE("Could not poll inotify events");
1091 return false;
1092 }
1093
1094 char buff[BUFF_LEN] __attribute__ ((aligned(__alignof__(struct inotify_event))));;
1095 ssize_t length = read(mInotifyFd, buff, BUFF_LEN);
1096 if (length == 0) {
1097 return true;
1098 } else if (length < 0) {
1099 ALOGE("Could not read inotify events");
1100 return false;
1101 }
1102
1103 const struct inotify_event *event;
1104 for (char* ptr = buff; ptr < buff + length; ptr += sizeof(struct inotify_event) + event->len) {
1105 event = (const struct inotify_event *) ptr;
1106 if (event->wd == mSystemWd && strcmp(SYSTEM_TIME_DIR_NAME, event->name) == 0) {
1107 addTimeDirWatch();
1108 } else if (event->wd == mTimeWd && (strcmp(LAST_TIME_CHANGED_FILE_NAME, event->name) == 0
1109 || strcmp(ACCURATE_TIME_FLAG_FILE_NAME, event->name) == 0)) {
1110 return !mBootAnimation->updateIsTimeAccurate();
1111 }
1112 }
1113
1114 return true;
1115}
1116
1117void BootAnimation::TimeCheckThread::addTimeDirWatch() {
1118 mTimeWd = inotify_add_watch(mInotifyFd, SYSTEM_TIME_DIR_PATH,
1119 IN_CLOSE_WRITE | IN_MOVED_TO | IN_ATTRIB);
1120 if (mTimeWd > 0) {
1121 // No need to watch for the time directory to be created if it already exists
1122 inotify_rm_watch(mInotifyFd, mSystemWd);
1123 mSystemWd = -1;
1124 }
1125}
1126
1127status_t BootAnimation::TimeCheckThread::readyToRun() {
1128 mInotifyFd = inotify_init();
1129 if (mInotifyFd < 0) {
1130 ALOGE("Could not initialize inotify fd");
1131 return NO_INIT;
1132 }
1133
1134 mSystemWd = inotify_add_watch(mInotifyFd, SYSTEM_DATA_DIR_PATH, IN_CREATE | IN_ATTRIB);
1135 if (mSystemWd < 0) {
1136 close(mInotifyFd);
1137 mInotifyFd = -1;
1138 ALOGE("Could not add watch for %s", SYSTEM_DATA_DIR_PATH);
1139 return NO_INIT;
1140 }
1141
1142 addTimeDirWatch();
1143
1144 if (mBootAnimation->updateIsTimeAccurate()) {
1145 close(mInotifyFd);
1146 mInotifyFd = -1;
1147 return ALREADY_EXISTS;
1148 }
1149
1150 return NO_ERROR;
1151}
1152
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001153// ---------------------------------------------------------------------------
1154
1155}
1156; // namespace android