blob: 6b2de4b7f1fff92a6089915a20fe397d73fa74da [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 Coyne2c9e94a2017-05-31 10:08:28 -070099BootAnimation::BootAnimation(InitCallback initCallback,
100 PlayPartCallback partCallback)
101 : Thread(false), mClockEnabled(true), mTimeIsAccurate(false),
102 mTimeFormat12Hour(false), mTimeCheckThread(NULL),
103 mInitCallback(initCallback), mPlayPartCallback(partCallback) {
Mathias Agopian627e7b52009-05-21 19:21:59 -0700104 mSession = new SurfaceComposerClient();
Geoffrey Pitsch290c4352016-08-09 14:35:10 -0400105
Keun-young Parkb5938422017-03-23 13:46:24 -0700106 std::string powerCtl = android::base::GetProperty("sys.powerctl", "");
107 if (powerCtl.empty()) {
108 mShuttingDown = false;
109 } else {
110 mShuttingDown = true;
111 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800112}
113
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800114void BootAnimation::onFirstRef() {
Mathias Agopianbc726112009-09-23 15:44:05 -0700115 status_t err = mSession->linkToComposerDeath(this);
Steve Block3762c312012-01-06 19:20:56 +0000116 ALOGE_IF(err, "linkToComposerDeath failed (%s) ", strerror(-err));
Mathias Agopian8434c532009-09-23 18:52:49 -0700117 if (err == NO_ERROR) {
Mathias Agopianbc726112009-09-23 15:44:05 -0700118 run("BootAnimation", PRIORITY_DISPLAY);
119 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800120}
121
Mathias Agopianbc726112009-09-23 15:44:05 -0700122sp<SurfaceComposerClient> BootAnimation::session() const {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800123 return mSession;
124}
125
Mathias Agopianbc726112009-09-23 15:44:05 -0700126
Narayan Kamathafd31e02013-12-03 13:16:03 +0000127void BootAnimation::binderDied(const wp<IBinder>&)
Mathias Agopianbc726112009-09-23 15:44:05 -0700128{
129 // woah, surfaceflinger died!
Steve Block5baa3a62011-12-20 16:23:08 +0000130 ALOGD("SurfaceFlinger died, exiting...");
Mathias Agopianbc726112009-09-23 15:44:05 -0700131
132 // calling requestExit() is not enough here because the Surface code
133 // might be blocked on a condition variable that will never be updated.
134 kill( getpid(), SIGKILL );
135 requestExit();
136}
137
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800138status_t BootAnimation::initTexture(Texture* texture, AssetManager& assets,
139 const char* name) {
140 Asset* asset = assets.open(name, Asset::ACCESS_BUFFER);
Andreas Gampecfedceb2014-09-30 21:48:18 -0700141 if (asset == NULL)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800142 return NO_INIT;
143 SkBitmap bitmap;
Matt Sarett8898c162016-03-24 16:11:01 -0400144 sk_sp<SkData> data = SkData::MakeWithoutCopy(asset->getBuffer(false),
145 asset->getLength());
146 sk_sp<SkImage> image = SkImage::MakeFromEncoded(data);
147 image->asLegacyBitmap(&bitmap, SkImage::kRO_LegacyBitmapMode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800148 asset->close();
149 delete asset;
150
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800151 const int w = bitmap.width();
152 const int h = bitmap.height();
153 const void* p = bitmap.getPixels();
154
155 GLint crop[4] = { 0, h, w, -h };
156 texture->w = w;
157 texture->h = h;
158
159 glGenTextures(1, &texture->name);
160 glBindTexture(GL_TEXTURE_2D, texture->name);
161
Mike Reed42a1d082014-07-07 18:06:18 -0400162 switch (bitmap.colorType()) {
163 case kAlpha_8_SkColorType:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800164 glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, w, h, 0, GL_ALPHA,
165 GL_UNSIGNED_BYTE, p);
166 break;
Mike Reed42a1d082014-07-07 18:06:18 -0400167 case kARGB_4444_SkColorType:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800168 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA,
169 GL_UNSIGNED_SHORT_4_4_4_4, p);
170 break;
Mike Reed42a1d082014-07-07 18:06:18 -0400171 case kN32_SkColorType:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800172 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA,
173 GL_UNSIGNED_BYTE, p);
174 break;
Mike Reed42a1d082014-07-07 18:06:18 -0400175 case kRGB_565_SkColorType:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800176 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, w, h, 0, GL_RGB,
177 GL_UNSIGNED_SHORT_5_6_5, p);
178 break;
179 default:
180 break;
181 }
182
183 glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_CROP_RECT_OES, crop);
184 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
185 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
186 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
187 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700188
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800189 return NO_ERROR;
190}
191
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700192status_t BootAnimation::initTexture(FileMap* map, int* width, int* height)
Mathias Agopiana8826d62009-10-01 03:10:14 -0700193{
Mathias Agopiana8826d62009-10-01 03:10:14 -0700194 SkBitmap bitmap;
Damien Bargiacchif67b3172016-09-07 20:17:14 -0700195 sk_sp<SkData> data = SkData::MakeWithoutCopy(map->getDataPtr(),
196 map->getDataLength());
Matt Sarett8898c162016-03-24 16:11:01 -0400197 sk_sp<SkImage> image = SkImage::MakeFromEncoded(data);
198 image->asLegacyBitmap(&bitmap, SkImage::kRO_LegacyBitmapMode);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700199
Mykola Kondratenko0c1eeb32014-04-15 09:35:44 +0200200 // FileMap memory is never released until application exit.
201 // Release it now as the texture is already loaded and the memory used for
202 // the packed resource can be released.
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700203 delete map;
Mykola Kondratenko0c1eeb32014-04-15 09:35:44 +0200204
Mathias Agopiana8826d62009-10-01 03:10:14 -0700205 const int w = bitmap.width();
206 const int h = bitmap.height();
207 const void* p = bitmap.getPixels();
208
209 GLint crop[4] = { 0, h, w, -h };
210 int tw = 1 << (31 - __builtin_clz(w));
211 int th = 1 << (31 - __builtin_clz(h));
212 if (tw < w) tw <<= 1;
213 if (th < h) th <<= 1;
214
Mike Reed42a1d082014-07-07 18:06:18 -0400215 switch (bitmap.colorType()) {
216 case kN32_SkColorType:
Sai Kiran Korwar27167492015-07-07 20:00:06 +0530217 if (!mUseNpotTextures && (tw != w || th != h)) {
Mathias Agopiana8826d62009-10-01 03:10:14 -0700218 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, tw, th, 0, GL_RGBA,
219 GL_UNSIGNED_BYTE, 0);
220 glTexSubImage2D(GL_TEXTURE_2D, 0,
221 0, 0, w, h, GL_RGBA, GL_UNSIGNED_BYTE, p);
222 } else {
Sai Kiran Korwar27167492015-07-07 20:00:06 +0530223 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA,
Mathias Agopiana8826d62009-10-01 03:10:14 -0700224 GL_UNSIGNED_BYTE, p);
225 }
226 break;
227
Mike Reed42a1d082014-07-07 18:06:18 -0400228 case kRGB_565_SkColorType:
Sai Kiran Korwar27167492015-07-07 20:00:06 +0530229 if (!mUseNpotTextures && (tw != w || th != h)) {
Mathias Agopiana8826d62009-10-01 03:10:14 -0700230 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, tw, th, 0, GL_RGB,
231 GL_UNSIGNED_SHORT_5_6_5, 0);
232 glTexSubImage2D(GL_TEXTURE_2D, 0,
233 0, 0, w, h, GL_RGB, GL_UNSIGNED_SHORT_5_6_5, p);
234 } else {
Sai Kiran Korwar27167492015-07-07 20:00:06 +0530235 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, w, h, 0, GL_RGB,
Mathias Agopiana8826d62009-10-01 03:10:14 -0700236 GL_UNSIGNED_SHORT_5_6_5, p);
237 }
238 break;
239 default:
240 break;
241 }
242
243 glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_CROP_RECT_OES, crop);
244
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700245 *width = w;
246 *height = h;
247
Mathias Agopiana8826d62009-10-01 03:10:14 -0700248 return NO_ERROR;
249}
250
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800251status_t BootAnimation::readyToRun() {
252 mAssets.addDefaultAssets();
253
Jeff Brown0b722fe2012-08-24 22:40:14 -0700254 sp<IBinder> dtoken(SurfaceComposerClient::getBuiltInDisplay(
255 ISurfaceComposer::eDisplayIdMain));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800256 DisplayInfo dinfo;
Jeff Brown0b722fe2012-08-24 22:40:14 -0700257 status_t status = SurfaceComposerClient::getDisplayInfo(dtoken, &dinfo);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800258 if (status)
259 return -1;
260
261 // create the native surface
Jeff Brown0b722fe2012-08-24 22:40:14 -0700262 sp<SurfaceControl> control = session()->createSurface(String8("BootAnimation"),
263 dinfo.w, dinfo.h, PIXEL_FORMAT_RGB_565);
Mathias Agopian439863f2011-06-28 19:09:31 -0700264
265 SurfaceComposerClient::openGlobalTransaction();
Mathias Agopian17f638b2009-04-16 20:04:08 -0700266 control->setLayer(0x40000000);
Mathias Agopian439863f2011-06-28 19:09:31 -0700267 SurfaceComposerClient::closeGlobalTransaction();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800268
Mathias Agopian17f638b2009-04-16 20:04:08 -0700269 sp<Surface> s = control->getSurface();
270
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800271 // initialize opengl and egl
Mathias Agopian738b9a42009-08-06 16:41:02 -0700272 const EGLint attribs[] = {
Mathias Agopian1b253b72011-08-15 15:20:22 -0700273 EGL_RED_SIZE, 8,
274 EGL_GREEN_SIZE, 8,
275 EGL_BLUE_SIZE, 8,
Mathias Agopiana8826d62009-10-01 03:10:14 -0700276 EGL_DEPTH_SIZE, 0,
277 EGL_NONE
Mathias Agopian738b9a42009-08-06 16:41:02 -0700278 };
Andreas Gampecfedceb2014-09-30 21:48:18 -0700279 EGLint w, h;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800280 EGLint numConfigs;
281 EGLConfig config;
282 EGLSurface surface;
283 EGLContext context;
Mathias Agopian627e7b52009-05-21 19:21:59 -0700284
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800285 EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
Mathias Agopian627e7b52009-05-21 19:21:59 -0700286
287 eglInitialize(display, 0, 0);
Mathias Agopian1b253b72011-08-15 15:20:22 -0700288 eglChooseConfig(display, attribs, &config, 1, &numConfigs);
Mathias Agopian1473f462009-04-10 14:24:30 -0700289 surface = eglCreateWindowSurface(display, config, s.get(), NULL);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800290 context = eglCreateContext(display, config, NULL, NULL);
291 eglQuerySurface(display, surface, EGL_WIDTH, &w);
292 eglQuerySurface(display, surface, EGL_HEIGHT, &h);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700293
Mathias Agopianabac0102009-07-31 14:47:00 -0700294 if (eglMakeCurrent(display, surface, surface, context) == EGL_FALSE)
295 return NO_INIT;
Mathias Agopiana8826d62009-10-01 03:10:14 -0700296
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800297 mDisplay = display;
298 mContext = context;
299 mSurface = surface;
300 mWidth = w;
301 mHeight = h;
Mathias Agopian17f638b2009-04-16 20:04:08 -0700302 mFlingerSurfaceControl = control;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800303 mFlingerSurface = s;
304
Elliott Hughesc367d482013-10-29 13:12:55 -0700305 // If the device has encryption turned on or is in process
Jason parksbd9a08d2011-01-31 15:04:34 -0600306 // of being encrypted we show the encrypted boot animation.
307 char decrypt[PROPERTY_VALUE_MAX];
308 property_get("vold.decrypt", decrypt, "");
309
Keun-young Parkb5938422017-03-23 13:46:24 -0700310 bool encryptedAnimation = atoi(decrypt) != 0 ||
311 !strcmp("trigger_restart_min_framework", decrypt);
Jason parksbd9a08d2011-01-31 15:04:34 -0600312
Keun-young Parkb5938422017-03-23 13:46:24 -0700313 if (!mShuttingDown && encryptedAnimation &&
314 (access(SYSTEM_ENCRYPTED_BOOTANIMATION_FILE, R_OK) == 0)) {
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700315 mZipFileName = SYSTEM_ENCRYPTED_BOOTANIMATION_FILE;
Keun-young Parkb5938422017-03-23 13:46:24 -0700316 return NO_ERROR;
Jason parksbd9a08d2011-01-31 15:04:34 -0600317 }
Keun-young Parkb5938422017-03-23 13:46:24 -0700318 static const char* bootFiles[] = {OEM_BOOTANIMATION_FILE, SYSTEM_BOOTANIMATION_FILE};
319 static const char* shutdownFiles[] =
320 {OEM_SHUTDOWNANIMATION_FILE, SYSTEM_SHUTDOWNANIMATION_FILE};
321
322 for (const char* f : (!mShuttingDown ? bootFiles : shutdownFiles)) {
323 if (access(f, R_OK) == 0) {
324 mZipFileName = f;
325 return NO_ERROR;
326 }
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700327 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800328 return NO_ERROR;
329}
330
Mathias Agopiana8826d62009-10-01 03:10:14 -0700331bool BootAnimation::threadLoop()
332{
333 bool r;
Narayan Kamathafd31e02013-12-03 13:16:03 +0000334 // We have no bootanimation file, so we use the stock android logo
335 // animation.
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700336 if (mZipFileName.isEmpty()) {
Mathias Agopiana8826d62009-10-01 03:10:14 -0700337 r = android();
338 } else {
339 r = movie();
340 }
341
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800342 eglMakeCurrent(mDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
343 eglDestroyContext(mDisplay, mContext);
344 eglDestroySurface(mDisplay, mSurface);
Mathias Agopian6cf0db22009-04-17 19:36:26 -0700345 mFlingerSurface.clear();
Mathias Agopian17f638b2009-04-16 20:04:08 -0700346 mFlingerSurfaceControl.clear();
Mathias Agopian627e7b52009-05-21 19:21:59 -0700347 eglTerminate(mDisplay);
Sai Kiran Korwar3eee9fb2015-06-16 17:13:35 +0530348 eglReleaseThread();
Mathias Agopian627e7b52009-05-21 19:21:59 -0700349 IPCThreadState::self()->stopProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800350 return r;
351}
352
Mathias Agopiana8826d62009-10-01 03:10:14 -0700353bool BootAnimation::android()
354{
Keun-young Parkd1794cd2017-04-04 12:21:19 -0700355 ALOGD("%sAnimationShownTiming start time: %" PRId64 "ms", mShuttingDown ? "Shutdown" : "Boot",
356 elapsedRealtime());
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700357 initTexture(&mAndroid[0], mAssets, "images/android-logo-mask.png");
358 initTexture(&mAndroid[1], mAssets, "images/android-logo-shine.png");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800359
360 // 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();
427 }
428}
429
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700430bool BootAnimation::validClock(const Animation::Part& part) {
431 return part.clockPosX != TEXT_MISSING_VALUE && part.clockPosY != TEXT_MISSING_VALUE;
432}
433
434bool parseTextCoord(const char* str, int* dest) {
435 if (strcmp("c", str) == 0) {
436 *dest = TEXT_CENTER_VALUE;
437 return true;
438 }
439
440 char* end;
441 int val = (int) strtol(str, &end, 0);
442 if (end == str || *end != '\0' || val == INT_MAX || val == INT_MIN) {
443 return false;
444 }
445 *dest = val;
446 return true;
447}
448
449// Parse two position coordinates. If only string is non-empty, treat it as the y value.
450void parsePosition(const char* str1, const char* str2, int* x, int* y) {
451 bool success = false;
452 if (strlen(str1) == 0) { // No values were specified
453 // success = false
454 } else if (strlen(str2) == 0) { // we have only one value
455 if (parseTextCoord(str1, y)) {
456 *x = TEXT_CENTER_VALUE;
457 success = true;
458 }
459 } else {
460 if (parseTextCoord(str1, x) && parseTextCoord(str2, y)) {
461 success = true;
462 }
463 }
464
465 if (!success) {
466 *x = TEXT_MISSING_VALUE;
467 *y = TEXT_MISSING_VALUE;
468 }
469}
470
Jesse Hall083b84c2014-09-22 10:51:09 -0700471// Parse a color represented as an HTML-style 'RRGGBB' string: each pair of
472// characters in str is a hex number in [0, 255], which are converted to
473// floating point values in the range [0.0, 1.0] and placed in the
474// corresponding elements of color.
475//
476// If the input string isn't valid, parseColor returns false and color is
477// left unchanged.
478static bool parseColor(const char str[7], float color[3]) {
479 float tmpColor[3];
480 for (int i = 0; i < 3; i++) {
481 int val = 0;
482 for (int j = 0; j < 2; j++) {
483 val *= 16;
484 char c = str[2*i + j];
485 if (c >= '0' && c <= '9') val += c - '0';
486 else if (c >= 'A' && c <= 'F') val += (c - 'A') + 10;
487 else if (c >= 'a' && c <= 'f') val += (c - 'a') + 10;
488 else return false;
489 }
490 tmpColor[i] = static_cast<float>(val) / 255.0f;
491 }
492 memcpy(color, tmpColor, sizeof(tmpColor));
493 return true;
494}
495
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700496
497static bool readFile(ZipFileRO* zip, const char* name, String8& outString)
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700498{
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700499 ZipEntryRO entry = zip->findEntryByName(name);
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700500 ALOGE_IF(!entry, "couldn't find %s", name);
501 if (!entry) {
502 return false;
503 }
504
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700505 FileMap* entryMap = zip->createEntryFileMap(entry);
506 zip->releaseEntry(entry);
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700507 ALOGE_IF(!entryMap, "entryMap is null");
508 if (!entryMap) {
509 return false;
510 }
511
512 outString.setTo((char const*)entryMap->getDataPtr(), entryMap->getDataLength());
Narayan Kamath688ff4c2015-02-23 15:47:54 +0000513 delete entryMap;
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700514 return true;
515}
516
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700517// The font image should be a 96x2 array of character images. The
518// columns are the printable ASCII characters 0x20 - 0x7f. The
519// top row is regular text; the bottom row is bold.
520status_t BootAnimation::initFont(Font* font, const char* fallback) {
521 status_t status = NO_ERROR;
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800522
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700523 if (font->map != nullptr) {
524 glGenTextures(1, &font->texture.name);
525 glBindTexture(GL_TEXTURE_2D, font->texture.name);
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800526
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700527 status = initTexture(font->map, &font->texture.w, &font->texture.h);
528
529 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
530 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
531 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
532 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
533 } else if (fallback != nullptr) {
534 status = initTexture(&font->texture, mAssets, fallback);
535 } else {
536 return NO_INIT;
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800537 }
538
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700539 if (status == NO_ERROR) {
540 font->char_width = font->texture.w / FONT_NUM_COLS;
541 font->char_height = font->texture.h / FONT_NUM_ROWS / 2; // There are bold and regular rows
542 }
543
544 return status;
545}
546
547void BootAnimation::drawText(const char* str, const Font& font, bool bold, int* x, int* y) {
548 glEnable(GL_BLEND); // Allow us to draw on top of the animation
549 glBindTexture(GL_TEXTURE_2D, font.texture.name);
550
551 const int len = strlen(str);
552 const int strWidth = font.char_width * len;
553
554 if (*x == TEXT_CENTER_VALUE) {
555 *x = (mWidth - strWidth) / 2;
556 } else if (*x < 0) {
557 *x = mWidth + *x - strWidth;
558 }
559 if (*y == TEXT_CENTER_VALUE) {
560 *y = (mHeight - font.char_height) / 2;
561 } else if (*y < 0) {
562 *y = mHeight + *y - font.char_height;
563 }
564
565 int cropRect[4] = { 0, 0, font.char_width, -font.char_height };
566
567 for (int i = 0; i < len; i++) {
568 char c = str[i];
569
570 if (c < FONT_BEGIN_CHAR || c > FONT_END_CHAR) {
571 c = '?';
572 }
573
574 // Crop the texture to only the pixels in the current glyph
575 const int charPos = (c - FONT_BEGIN_CHAR); // Position in the list of valid characters
576 const int row = charPos / FONT_NUM_COLS;
577 const int col = charPos % FONT_NUM_COLS;
578 cropRect[0] = col * font.char_width; // Left of column
579 cropRect[1] = row * font.char_height * 2; // Top of row
580 // Move down to bottom of regular (one char_heigh) or bold (two char_heigh) line
581 cropRect[1] += bold ? 2 * font.char_height : font.char_height;
582 glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_CROP_RECT_OES, cropRect);
583
584 glDrawTexiOES(*x, *y, 0, font.char_width, font.char_height);
585
586 *x += font.char_width;
587 }
588
589 glDisable(GL_BLEND); // Return to the animation's default behaviour
590 glBindTexture(GL_TEXTURE_2D, 0);
591}
592
Damien Bargiacchi9071db12016-10-28 17:38:22 -0700593// We render 12 or 24 hour time.
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700594void BootAnimation::drawClock(const Font& font, const int xPos, const int yPos) {
Damien Bargiacchi9071db12016-10-28 17:38:22 -0700595 static constexpr char TIME_FORMAT_12[] = "%l:%M";
596 static constexpr char TIME_FORMAT_24[] = "%H:%M";
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700597 static constexpr int TIME_LENGTH = 6;
598
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800599 time_t rawtime;
600 time(&rawtime);
601 struct tm* timeInfo = localtime(&rawtime);
602
603 char timeBuff[TIME_LENGTH];
Damien Bargiacchi9071db12016-10-28 17:38:22 -0700604 const char* timeFormat = mTimeFormat12Hour ? TIME_FORMAT_12 : TIME_FORMAT_24;
605 size_t length = strftime(timeBuff, TIME_LENGTH, timeFormat, timeInfo);
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800606
607 if (length != TIME_LENGTH - 1) {
608 ALOGE("Couldn't format time; abandoning boot animation clock");
609 mClockEnabled = false;
610 return;
611 }
612
Damien Bargiacchi45a76442016-12-05 18:02:18 -0800613 char* out = timeBuff[0] == ' ' ? &timeBuff[1] : &timeBuff[0];
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700614 int x = xPos;
615 int y = yPos;
Damien Bargiacchi45a76442016-12-05 18:02:18 -0800616 drawText(out, font, false, &x, &y);
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800617}
618
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700619bool BootAnimation::parseAnimationDesc(Animation& animation)
Mathias Agopiana8826d62009-10-01 03:10:14 -0700620{
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700621 String8 desString;
622
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700623 if (!readFile(animation.zip, "desc.txt", desString)) {
Narayan Kamathafd31e02013-12-03 13:16:03 +0000624 return false;
625 }
Mathias Agopiana8826d62009-10-01 03:10:14 -0700626 char const* s = desString.string();
627
Mathias Agopiana8826d62009-10-01 03:10:14 -0700628 // Parse the description file
629 for (;;) {
630 const char* endl = strstr(s, "\n");
Andreas Gampecfedceb2014-09-30 21:48:18 -0700631 if (endl == NULL) break;
Mathias Agopiana8826d62009-10-01 03:10:14 -0700632 String8 line(s, endl - s);
633 const char* l = line.string();
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800634 int fps = 0;
635 int width = 0;
636 int height = 0;
637 int count = 0;
638 int pause = 0;
Narayan Kamathafd31e02013-12-03 13:16:03 +0000639 char path[ANIM_ENTRY_NAME_MAX];
Jesse Hall083b84c2014-09-22 10:51:09 -0700640 char color[7] = "000000"; // default to black if unspecified
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700641 char clockPos1[TEXT_POS_LEN_MAX + 1] = "";
642 char clockPos2[TEXT_POS_LEN_MAX + 1] = "";
Jesse Hall083b84c2014-09-22 10:51:09 -0700643
Kevin Hesterd3782b22012-04-26 10:38:55 -0700644 char pathType;
Mathias Agopiana8826d62009-10-01 03:10:14 -0700645 if (sscanf(l, "%d %d %d", &width, &height, &fps) == 3) {
Jesse Hall083b84c2014-09-22 10:51:09 -0700646 // ALOGD("> w=%d, h=%d, fps=%d", width, height, fps);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700647 animation.width = width;
648 animation.height = height;
649 animation.fps = fps;
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700650 } else if (sscanf(l, " %c %d %d %s #%6s %16s %16s",
651 &pathType, &count, &pause, path, color, clockPos1, clockPos2) >= 4) {
652 //ALOGD("> type=%c, count=%d, pause=%d, path=%s, color=%s, clockPos1=%s, clockPos2=%s",
653 // pathType, count, pause, path, color, clockPos1, clockPos2);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700654 Animation::Part part;
Kevin Hesterd3782b22012-04-26 10:38:55 -0700655 part.playUntilComplete = pathType == 'c';
Mathias Agopiana8826d62009-10-01 03:10:14 -0700656 part.count = count;
657 part.pause = pause;
658 part.path = path;
Geoffrey Pitschd6d9a1d2016-06-08 00:38:58 -0700659 part.audioData = NULL;
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700660 part.animation = NULL;
Jesse Hall083b84c2014-09-22 10:51:09 -0700661 if (!parseColor(color, part.backgroundColor)) {
662 ALOGE("> invalid color '#%s'", color);
663 part.backgroundColor[0] = 0.0f;
664 part.backgroundColor[1] = 0.0f;
665 part.backgroundColor[2] = 0.0f;
666 }
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700667 parsePosition(clockPos1, clockPos2, &part.clockPosX, &part.clockPosY);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700668 animation.parts.add(part);
669 }
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700670 else if (strcmp(l, "$SYSTEM") == 0) {
671 // ALOGD("> SYSTEM");
672 Animation::Part part;
673 part.playUntilComplete = false;
674 part.count = 1;
675 part.pause = 0;
Geoffrey Pitschd6d9a1d2016-06-08 00:38:58 -0700676 part.audioData = NULL;
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700677 part.animation = loadAnimation(String8(SYSTEM_BOOTANIMATION_FILE));
678 if (part.animation != NULL)
679 animation.parts.add(part);
680 }
Mathias Agopiana8826d62009-10-01 03:10:14 -0700681 s = ++endl;
682 }
683
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700684 return true;
685}
686
687bool BootAnimation::preloadZip(Animation& animation)
688{
Mathias Agopiana8826d62009-10-01 03:10:14 -0700689 // read all the data structures
690 const size_t pcount = animation.parts.size();
Narayan Kamathafd31e02013-12-03 13:16:03 +0000691 void *cookie = NULL;
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400692 ZipFileRO* zip = animation.zip;
693 if (!zip->startIteration(&cookie)) {
Narayan Kamathafd31e02013-12-03 13:16:03 +0000694 return false;
695 }
696
697 ZipEntryRO entry;
698 char name[ANIM_ENTRY_NAME_MAX];
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400699 while ((entry = zip->nextEntry(cookie)) != NULL) {
700 const int foundEntryName = zip->getEntryFileName(entry, name, ANIM_ENTRY_NAME_MAX);
Narayan Kamathafd31e02013-12-03 13:16:03 +0000701 if (foundEntryName > ANIM_ENTRY_NAME_MAX || foundEntryName == -1) {
702 ALOGE("Error fetching entry file name");
703 continue;
704 }
705
706 const String8 entryName(name);
707 const String8 path(entryName.getPathDir());
708 const String8 leaf(entryName.getPathLeaf());
709 if (leaf.size() > 0) {
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700710 if (entryName == CLOCK_FONT_ZIP_NAME) {
711 FileMap* map = zip->createEntryFileMap(entry);
712 if (map) {
713 animation.clockFont.map = map;
714 }
715 continue;
716 }
717
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400718 for (size_t j = 0; j < pcount; j++) {
Narayan Kamathafd31e02013-12-03 13:16:03 +0000719 if (path == animation.parts[j].path) {
Narayan Kamath4600dd02015-06-16 12:02:57 +0100720 uint16_t method;
Narayan Kamathafd31e02013-12-03 13:16:03 +0000721 // supports only stored png files
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400722 if (zip->getEntryInfo(entry, &method, NULL, NULL, NULL, NULL, NULL)) {
Narayan Kamathafd31e02013-12-03 13:16:03 +0000723 if (method == ZipFileRO::kCompressStored) {
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400724 FileMap* map = zip->createEntryFileMap(entry);
Narayan Kamathafd31e02013-12-03 13:16:03 +0000725 if (map) {
Narayan Kamathafd31e02013-12-03 13:16:03 +0000726 Animation::Part& part(animation.parts.editItemAt(j));
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700727 if (leaf == "audio.wav") {
728 // a part may have at most one audio file
Geoffrey Pitschd6d9a1d2016-06-08 00:38:58 -0700729 part.audioData = (uint8_t *)map->getDataPtr();
730 part.audioLength = map->getDataLength();
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400731 } else if (leaf == "trim.txt") {
732 part.trimData.setTo((char const*)map->getDataPtr(),
733 map->getDataLength());
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700734 } else {
735 Animation::Frame frame;
736 frame.name = leaf;
737 frame.map = map;
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400738 frame.trimWidth = animation.width;
739 frame.trimHeight = animation.height;
740 frame.trimX = 0;
741 frame.trimY = 0;
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700742 part.frames.add(frame);
743 }
Mathias Agopiana8826d62009-10-01 03:10:14 -0700744 }
Geoffrey Pitschd6d9a1d2016-06-08 00:38:58 -0700745 } else {
746 ALOGE("bootanimation.zip is compressed; must be only stored");
Mathias Agopiana8826d62009-10-01 03:10:14 -0700747 }
748 }
749 }
750 }
751 }
752 }
753
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400754 // If there is trimData present, override the positioning defaults.
755 for (Animation::Part& part : animation.parts) {
756 const char* trimDataStr = part.trimData.string();
757 for (size_t frameIdx = 0; frameIdx < part.frames.size(); frameIdx++) {
758 const char* endl = strstr(trimDataStr, "\n");
759 // No more trimData for this part.
760 if (endl == NULL) {
761 break;
762 }
763 String8 line(trimDataStr, endl - trimDataStr);
764 const char* lineStr = line.string();
765 trimDataStr = ++endl;
766 int width = 0, height = 0, x = 0, y = 0;
767 if (sscanf(lineStr, "%dx%d+%d+%d", &width, &height, &x, &y) == 4) {
768 Animation::Frame& frame(part.frames.editItemAt(frameIdx));
769 frame.trimWidth = width;
770 frame.trimHeight = height;
771 frame.trimX = x;
772 frame.trimY = y;
773 } else {
774 ALOGE("Error parsing trim.txt, line: %s", lineStr);
775 break;
776 }
777 }
778 }
779
Ed Coyne2c9e94a2017-05-31 10:08:28 -0700780 if (mInitCallback != nullptr) {
781 mInitCallback(animation.parts);
Geoffrey Pitschd6d9a1d2016-06-08 00:38:58 -0700782 }
783
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400784 zip->endIteration(cookie);
Narayan Kamathafd31e02013-12-03 13:16:03 +0000785
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700786 return true;
787}
788
789bool BootAnimation::movie()
790{
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700791 Animation* animation = loadAnimation(mZipFileName);
792 if (animation == NULL)
793 return false;
794
Damien Bargiacchi97480862016-03-29 14:55:55 -0700795 bool anyPartHasClock = false;
796 for (size_t i=0; i < animation->parts.size(); i++) {
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700797 if(validClock(animation->parts[i])) {
Damien Bargiacchi97480862016-03-29 14:55:55 -0700798 anyPartHasClock = true;
799 break;
800 }
801 }
802 if (!anyPartHasClock) {
803 mClockEnabled = false;
804 }
805
Sai Kiran Korwar27167492015-07-07 20:00:06 +0530806 // Check if npot textures are supported
807 mUseNpotTextures = false;
808 String8 gl_extensions;
809 const char* exts = reinterpret_cast<const char*>(glGetString(GL_EXTENSIONS));
810 if (!exts) {
811 glGetError();
812 } else {
813 gl_extensions.setTo(exts);
814 if ((gl_extensions.find("GL_ARB_texture_non_power_of_two") != -1) ||
815 (gl_extensions.find("GL_OES_texture_npot") != -1)) {
816 mUseNpotTextures = true;
817 }
818 }
819
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800820 // Blend required to draw time on top of animation frames.
821 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700822 glShadeModel(GL_FLAT);
823 glDisable(GL_DITHER);
824 glDisable(GL_SCISSOR_TEST);
825 glDisable(GL_BLEND);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700826
827 glBindTexture(GL_TEXTURE_2D, 0);
828 glEnable(GL_TEXTURE_2D);
829 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
830 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
831 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
832 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
833 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
834
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700835 bool clockFontInitialized = false;
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800836 if (mClockEnabled) {
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700837 clockFontInitialized =
838 (initFont(&animation->clockFont, CLOCK_FONT_ASSET) == NO_ERROR);
839 mClockEnabled = clockFontInitialized;
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800840 }
841
Damien Bargiacchi97480862016-03-29 14:55:55 -0700842 if (mClockEnabled && !updateIsTimeAccurate()) {
843 mTimeCheckThread = new TimeCheckThread(this);
844 mTimeCheckThread->run("BootAnimation::TimeCheckThread", PRIORITY_NORMAL);
845 }
846
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700847 playAnimation(*animation);
Damien Bargiacchi97480862016-03-29 14:55:55 -0700848
Geoffrey Pitscha9173532016-07-19 14:56:39 -0400849 if (mTimeCheckThread != nullptr) {
Damien Bargiacchi97480862016-03-29 14:55:55 -0700850 mTimeCheckThread->requestExit();
Geoffrey Pitscha9173532016-07-19 14:56:39 -0400851 mTimeCheckThread = nullptr;
852 }
853
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700854 releaseAnimation(animation);
855
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700856 if (clockFontInitialized) {
857 glDeleteTextures(1, &animation->clockFont.texture.name);
Andriy Naborskyy815e51d2016-03-24 16:43:34 -0700858 }
859
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700860 return false;
861}
862
863bool BootAnimation::playAnimation(const Animation& animation)
864{
865 const size_t pcount = animation.parts.size();
Mathias Agopiana8826d62009-10-01 03:10:14 -0700866 nsecs_t frameDuration = s2ns(1) / animation.fps;
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400867 const int animationX = (mWidth - animation.width) / 2;
868 const int animationY = (mHeight - animation.height) / 2;
Mathias Agopian9f3020d2009-11-06 16:30:18 -0800869
Keun-young Parkd1794cd2017-04-04 12:21:19 -0700870 ALOGD("%sAnimationShownTiming start time: %" PRId64 "ms", mShuttingDown ? "Shutdown" : "Boot",
871 elapsedRealtime());
Narayan Kamathafd31e02013-12-03 13:16:03 +0000872 for (size_t i=0 ; i<pcount ; i++) {
Mathias Agopiana8826d62009-10-01 03:10:14 -0700873 const Animation::Part& part(animation.parts[i]);
874 const size_t fcount = part.frames.size();
875 glBindTexture(GL_TEXTURE_2D, 0);
876
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700877 // Handle animation package
878 if (part.animation != NULL) {
879 playAnimation(*part.animation);
880 if (exitPending())
881 break;
882 continue; //to next part
883 }
884
Mathias Agopiana8826d62009-10-01 03:10:14 -0700885 for (int r=0 ; !part.count || r<part.count ; r++) {
Kevin Hesterd3782b22012-04-26 10:38:55 -0700886 // Exit any non playuntil complete parts immediately
887 if(exitPending() && !part.playUntilComplete)
888 break;
889
Ed Coyne2c9e94a2017-05-31 10:08:28 -0700890 if (mPlayPartCallback != nullptr) {
891 mPlayPartCallback(i, part, r);
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700892 }
893
Jesse Hall083b84c2014-09-22 10:51:09 -0700894 glClearColor(
895 part.backgroundColor[0],
896 part.backgroundColor[1],
897 part.backgroundColor[2],
898 1.0f);
899
Narayan Kamathafd31e02013-12-03 13:16:03 +0000900 for (size_t j=0 ; j<fcount && (!exitPending() || part.playUntilComplete) ; j++) {
Mathias Agopiana8826d62009-10-01 03:10:14 -0700901 const Animation::Frame& frame(part.frames[j]);
Mathias Agopiandb7dd2a2012-05-12 15:08:21 -0700902 nsecs_t lastFrame = systemTime();
Mathias Agopiana8826d62009-10-01 03:10:14 -0700903
904 if (r > 0) {
905 glBindTexture(GL_TEXTURE_2D, frame.tid);
906 } else {
907 if (part.count != 1) {
908 glGenTextures(1, &frame.tid);
909 glBindTexture(GL_TEXTURE_2D, frame.tid);
910 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
911 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
912 }
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700913 int w, h;
914 initTexture(frame.map, &w, &h);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700915 }
916
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400917 const int xc = animationX + frame.trimX;
918 const int yc = animationY + frame.trimY;
919 Region clearReg(Rect(mWidth, mHeight));
920 clearReg.subtractSelf(Rect(xc, yc, xc+frame.trimWidth, yc+frame.trimHeight));
Mathias Agopian9f3020d2009-11-06 16:30:18 -0800921 if (!clearReg.isEmpty()) {
922 Region::const_iterator head(clearReg.begin());
923 Region::const_iterator tail(clearReg.end());
924 glEnable(GL_SCISSOR_TEST);
925 while (head != tail) {
Andreas Gampecfedceb2014-09-30 21:48:18 -0700926 const Rect& r2(*head++);
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400927 glScissor(r2.left, mHeight - r2.bottom, r2.width(), r2.height());
Mathias Agopian9f3020d2009-11-06 16:30:18 -0800928 glClear(GL_COLOR_BUFFER_BIT);
929 }
930 glDisable(GL_SCISSOR_TEST);
931 }
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400932 // specify the y center as ceiling((mHeight - frame.trimHeight) / 2)
933 // which is equivalent to mHeight - (yc + frame.trimHeight)
934 glDrawTexiOES(xc, mHeight - (yc + frame.trimHeight),
935 0, frame.trimWidth, frame.trimHeight);
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700936 if (mClockEnabled && mTimeIsAccurate && validClock(part)) {
937 drawClock(animation.clockFont, part.clockPosX, part.clockPosY);
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800938 }
939
Mathias Agopiana8826d62009-10-01 03:10:14 -0700940 eglSwapBuffers(mDisplay, mSurface);
941
942 nsecs_t now = systemTime();
943 nsecs_t delay = frameDuration - (now - lastFrame);
Mathias Agopiandb7dd2a2012-05-12 15:08:21 -0700944 //ALOGD("%lld, %lld", ns2ms(now - lastFrame), ns2ms(delay));
Mathias Agopiana8826d62009-10-01 03:10:14 -0700945 lastFrame = now;
Mathias Agopiandb7dd2a2012-05-12 15:08:21 -0700946
947 if (delay > 0) {
948 struct timespec spec;
949 spec.tv_sec = (now + delay) / 1000000000;
950 spec.tv_nsec = (now + delay) % 1000000000;
951 int err;
952 do {
953 err = clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &spec, NULL);
954 } while (err<0 && errno == EINTR);
955 }
Kevin Hesterd3782b22012-04-26 10:38:55 -0700956
957 checkExit();
Mathias Agopiana8826d62009-10-01 03:10:14 -0700958 }
Kevin Hesterd3782b22012-04-26 10:38:55 -0700959
Mathias Agopiana8826d62009-10-01 03:10:14 -0700960 usleep(part.pause * ns2us(frameDuration));
Kevin Hesterd3782b22012-04-26 10:38:55 -0700961
962 // For infinite parts, we've now played them at least once, so perhaps exit
963 if(exitPending() && !part.count)
964 break;
Mathias Agopiana8826d62009-10-01 03:10:14 -0700965 }
966
Geoffrey Pitsch2fb30fb2016-07-06 16:16:20 -0400967 }
968
969 // Free textures created for looping parts now that the animation is done.
970 for (const Animation::Part& part : animation.parts) {
Mathias Agopiana8826d62009-10-01 03:10:14 -0700971 if (part.count != 1) {
Geoffrey Pitsch2fb30fb2016-07-06 16:16:20 -0400972 const size_t fcount = part.frames.size();
973 for (size_t j = 0; j < fcount; j++) {
Mathias Agopiana8826d62009-10-01 03:10:14 -0700974 const Animation::Frame& frame(part.frames[j]);
975 glDeleteTextures(1, &frame.tid);
976 }
977 }
978 }
Geoffrey Pitschd6d9a1d2016-06-08 00:38:58 -0700979
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700980 return true;
Mathias Agopiana8826d62009-10-01 03:10:14 -0700981}
982
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700983void BootAnimation::releaseAnimation(Animation* animation) const
984{
985 for (Vector<Animation::Part>::iterator it = animation->parts.begin(),
986 e = animation->parts.end(); it != e; ++it) {
987 if (it->animation)
988 releaseAnimation(it->animation);
989 }
990 if (animation->zip)
991 delete animation->zip;
992 delete animation;
993}
994
995BootAnimation::Animation* BootAnimation::loadAnimation(const String8& fn)
996{
997 if (mLoadedFiles.indexOf(fn) >= 0) {
998 ALOGE("File \"%s\" is already loaded. Cyclic ref is not allowed",
999 fn.string());
1000 return NULL;
1001 }
1002 ZipFileRO *zip = ZipFileRO::open(fn);
1003 if (zip == NULL) {
1004 ALOGE("Failed to open animation zip \"%s\": %s",
1005 fn.string(), strerror(errno));
1006 return NULL;
1007 }
1008
1009 Animation *animation = new Animation;
1010 animation->fileName = fn;
1011 animation->zip = zip;
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -07001012 animation->clockFont.map = nullptr;
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001013 mLoadedFiles.add(animation->fileName);
1014
1015 parseAnimationDesc(*animation);
Geoffrey Pitscha91a2d72016-07-12 14:46:19 -04001016 if (!preloadZip(*animation)) {
1017 return NULL;
1018 }
1019
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001020
1021 mLoadedFiles.remove(fn);
1022 return animation;
1023}
Damien Bargiacchi97480862016-03-29 14:55:55 -07001024
1025bool BootAnimation::updateIsTimeAccurate() {
1026 static constexpr long long MAX_TIME_IN_PAST = 60000LL * 60LL * 24LL * 30LL; // 30 days
1027 static constexpr long long MAX_TIME_IN_FUTURE = 60000LL * 90LL; // 90 minutes
1028
1029 if (mTimeIsAccurate) {
1030 return true;
1031 }
Keun-young Parkb5938422017-03-23 13:46:24 -07001032 if (mShuttingDown) return true;
Damien Bargiacchi97480862016-03-29 14:55:55 -07001033 struct stat statResult;
Damien Bargiacchi9071db12016-10-28 17:38:22 -07001034
1035 if(stat(TIME_FORMAT_12_HOUR_FLAG_FILE_PATH, &statResult) == 0) {
1036 mTimeFormat12Hour = true;
1037 }
1038
Damien Bargiacchi97480862016-03-29 14:55:55 -07001039 if(stat(ACCURATE_TIME_FLAG_FILE_PATH, &statResult) == 0) {
1040 mTimeIsAccurate = true;
1041 return true;
1042 }
1043
1044 FILE* file = fopen(LAST_TIME_CHANGED_FILE_PATH, "r");
1045 if (file != NULL) {
1046 long long lastChangedTime = 0;
1047 fscanf(file, "%lld", &lastChangedTime);
1048 fclose(file);
1049 if (lastChangedTime > 0) {
1050 struct timespec now;
1051 clock_gettime(CLOCK_REALTIME, &now);
1052 // Match the Java timestamp format
1053 long long rtcNow = (now.tv_sec * 1000LL) + (now.tv_nsec / 1000000LL);
Damien Bargiacchi96762812016-07-12 15:53:40 -07001054 if (ACCURATE_TIME_EPOCH < rtcNow
1055 && lastChangedTime > (rtcNow - MAX_TIME_IN_PAST)
1056 && lastChangedTime < (rtcNow + MAX_TIME_IN_FUTURE)) {
Damien Bargiacchi97480862016-03-29 14:55:55 -07001057 mTimeIsAccurate = true;
1058 }
1059 }
1060 }
1061
1062 return mTimeIsAccurate;
1063}
1064
1065BootAnimation::TimeCheckThread::TimeCheckThread(BootAnimation* bootAnimation) : Thread(false),
1066 mInotifyFd(-1), mSystemWd(-1), mTimeWd(-1), mBootAnimation(bootAnimation) {}
1067
1068BootAnimation::TimeCheckThread::~TimeCheckThread() {
1069 // mInotifyFd may be -1 but that's ok since we're not at risk of attempting to close a valid FD.
1070 close(mInotifyFd);
1071}
1072
1073bool BootAnimation::TimeCheckThread::threadLoop() {
1074 bool shouldLoop = doThreadLoop() && !mBootAnimation->mTimeIsAccurate
1075 && mBootAnimation->mClockEnabled;
1076 if (!shouldLoop) {
1077 close(mInotifyFd);
1078 mInotifyFd = -1;
1079 }
1080 return shouldLoop;
1081}
1082
1083bool BootAnimation::TimeCheckThread::doThreadLoop() {
1084 static constexpr int BUFF_LEN (10 * (sizeof(struct inotify_event) + NAME_MAX + 1));
1085
1086 // Poll instead of doing a blocking read so the Thread can exit if requested.
1087 struct pollfd pfd = { mInotifyFd, POLLIN, 0 };
1088 ssize_t pollResult = poll(&pfd, 1, 1000);
1089
1090 if (pollResult == 0) {
1091 return true;
1092 } else if (pollResult < 0) {
1093 ALOGE("Could not poll inotify events");
1094 return false;
1095 }
1096
1097 char buff[BUFF_LEN] __attribute__ ((aligned(__alignof__(struct inotify_event))));;
1098 ssize_t length = read(mInotifyFd, buff, BUFF_LEN);
1099 if (length == 0) {
1100 return true;
1101 } else if (length < 0) {
1102 ALOGE("Could not read inotify events");
1103 return false;
1104 }
1105
1106 const struct inotify_event *event;
1107 for (char* ptr = buff; ptr < buff + length; ptr += sizeof(struct inotify_event) + event->len) {
1108 event = (const struct inotify_event *) ptr;
1109 if (event->wd == mSystemWd && strcmp(SYSTEM_TIME_DIR_NAME, event->name) == 0) {
1110 addTimeDirWatch();
1111 } else if (event->wd == mTimeWd && (strcmp(LAST_TIME_CHANGED_FILE_NAME, event->name) == 0
1112 || strcmp(ACCURATE_TIME_FLAG_FILE_NAME, event->name) == 0)) {
1113 return !mBootAnimation->updateIsTimeAccurate();
1114 }
1115 }
1116
1117 return true;
1118}
1119
1120void BootAnimation::TimeCheckThread::addTimeDirWatch() {
1121 mTimeWd = inotify_add_watch(mInotifyFd, SYSTEM_TIME_DIR_PATH,
1122 IN_CLOSE_WRITE | IN_MOVED_TO | IN_ATTRIB);
1123 if (mTimeWd > 0) {
1124 // No need to watch for the time directory to be created if it already exists
1125 inotify_rm_watch(mInotifyFd, mSystemWd);
1126 mSystemWd = -1;
1127 }
1128}
1129
1130status_t BootAnimation::TimeCheckThread::readyToRun() {
1131 mInotifyFd = inotify_init();
1132 if (mInotifyFd < 0) {
1133 ALOGE("Could not initialize inotify fd");
1134 return NO_INIT;
1135 }
1136
1137 mSystemWd = inotify_add_watch(mInotifyFd, SYSTEM_DATA_DIR_PATH, IN_CREATE | IN_ATTRIB);
1138 if (mSystemWd < 0) {
1139 close(mInotifyFd);
1140 mInotifyFd = -1;
1141 ALOGE("Could not add watch for %s", SYSTEM_DATA_DIR_PATH);
1142 return NO_INIT;
1143 }
1144
1145 addTimeDirWatch();
1146
1147 if (mBootAnimation->updateIsTimeAccurate()) {
1148 close(mInotifyFd);
1149 mInotifyFd = -1;
1150 return ALREADY_EXISTS;
1151 }
1152
1153 return NO_ERROR;
1154}
1155
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001156// ---------------------------------------------------------------------------
1157
1158}
1159; // namespace android