blob: 9cfef47b2ea1e5e23aab9fd5d3825921bac916c2 [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>
Damien Bargiacchi97480862016-03-29 14:55:55 -070021#include <sys/inotify.h>
22#include <sys/poll.h>
23#include <sys/stat.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080024#include <sys/types.h>
25#include <math.h>
26#include <fcntl.h>
27#include <utils/misc.h>
Mathias Agopianb4d5a722009-09-23 17:05:19 -070028#include <signal.h>
Elliott Hughesbb94f312014-10-21 10:41:33 -070029#include <time.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080030
Jason parksbd9a08d2011-01-31 15:04:34 -060031#include <cutils/properties.h>
32
Mathias Agopianb13b9bd2012-02-17 18:27:36 -080033#include <androidfw/AssetManager.h>
Mathias Agopianac31a3b2009-05-21 19:59:24 -070034#include <binder/IPCThreadState.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080035#include <utils/Atomic.h>
36#include <utils/Errors.h>
37#include <utils/Log.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080038
39#include <ui/PixelFormat.h>
40#include <ui/Rect.h>
41#include <ui/Region.h>
42#include <ui/DisplayInfo.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080043
Jeff Brown0b722fe2012-08-24 22:40:14 -070044#include <gui/ISurfaceComposer.h>
Mathias Agopian8335f1c2012-02-25 18:48:35 -080045#include <gui/Surface.h>
46#include <gui/SurfaceComposerClient.h>
Mathias Agopian000479f2010-02-09 17:46:37 -080047
Andreas Gampecfedceb2014-09-30 21:48:18 -070048// TODO: Fix Skia.
49#pragma GCC diagnostic push
50#pragma GCC diagnostic ignored "-Wunused-parameter"
Derek Sollenbergereece0dd2014-02-27 14:31:29 -050051#include <SkBitmap.h>
52#include <SkStream.h>
53#include <SkImageDecoder.h>
Andreas Gampecfedceb2014-09-30 21:48:18 -070054#pragma GCC diagnostic pop
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080055
56#include <GLES/gl.h>
57#include <GLES/glext.h>
58#include <EGL/eglext.h>
59
60#include "BootAnimation.h"
Geoffrey Pitschd6d9a1d2016-06-08 00:38:58 -070061#include "audioplay.h"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080062
63namespace android {
64
Damien Bargiacchi97480862016-03-29 14:55:55 -070065static const char OEM_BOOTANIMATION_FILE[] = "/oem/media/bootanimation.zip";
66static const char SYSTEM_BOOTANIMATION_FILE[] = "/system/media/bootanimation.zip";
67static const char SYSTEM_ENCRYPTED_BOOTANIMATION_FILE[] = "/system/media/bootanimation-encrypted.zip";
68static const char SYSTEM_DATA_DIR_PATH[] = "/data/system";
69static const char SYSTEM_TIME_DIR_NAME[] = "time";
70static const char SYSTEM_TIME_DIR_PATH[] = "/data/system/time";
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -070071static const char CLOCK_FONT_ASSET[] = "images/clock_font.png";
72static const char CLOCK_FONT_ZIP_NAME[] = "clock_font.png";
Damien Bargiacchi97480862016-03-29 14:55:55 -070073static const char LAST_TIME_CHANGED_FILE_NAME[] = "last_time_change";
74static const char LAST_TIME_CHANGED_FILE_PATH[] = "/data/system/time/last_time_change";
75static const char ACCURATE_TIME_FLAG_FILE_NAME[] = "time_is_accurate";
76static const char ACCURATE_TIME_FLAG_FILE_PATH[] = "/data/system/time/time_is_accurate";
Damien Bargiacchi9071db12016-10-28 17:38:22 -070077static const char TIME_FORMAT_12_HOUR_FLAG_FILE_PATH[] = "/data/system/time/time_format_12_hour";
Damien Bargiacchi96762812016-07-12 15:53:40 -070078// Java timestamp format. Don't show the clock if the date is before 2000-01-01 00:00:00.
79static const long long ACCURATE_TIME_EPOCH = 946684800000;
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -070080static constexpr char FONT_BEGIN_CHAR = ' ';
81static constexpr char FONT_END_CHAR = '~' + 1;
82static constexpr size_t FONT_NUM_CHARS = FONT_END_CHAR - FONT_BEGIN_CHAR + 1;
83static constexpr size_t FONT_NUM_COLS = 16;
84static constexpr size_t FONT_NUM_ROWS = FONT_NUM_CHARS / FONT_NUM_COLS;
85static const int TEXT_CENTER_VALUE = INT_MAX;
86static const int TEXT_MISSING_VALUE = INT_MIN;
Damien Bargiacchi97480862016-03-29 14:55:55 -070087static const char EXIT_PROP_NAME[] = "service.bootanim.exit";
Geoffrey Pitsch30508792016-07-22 17:04:21 -040088static const char PLAY_SOUND_PROP_NAME[] = "persist.sys.bootanim.play_sound";
Narayan Kamathafd31e02013-12-03 13:16:03 +000089static const int ANIM_ENTRY_NAME_MAX = 256;
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -070090static constexpr size_t TEXT_POS_LEN_MAX = 16;
Geoffrey Pitsch290c4352016-08-09 14:35:10 -040091static const char BOOT_COMPLETED_PROP_NAME[] = "sys.boot_completed";
92static const char BOOTREASON_PROP_NAME[] = "ro.boot.bootreason";
93// bootreasons list in "system/core/bootstat/bootstat.cpp".
94static const std::vector<std::string> PLAY_SOUND_BOOTREASON_BLACKLIST {
95 "kernel_panic",
96 "Panic",
97 "Watchdog",
98};
Narayan Kamathafd31e02013-12-03 13:16:03 +000099
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800100// ---------------------------------------------------------------------------
101
Damien Bargiacchi97480862016-03-29 14:55:55 -0700102BootAnimation::BootAnimation() : Thread(false), mClockEnabled(true), mTimeIsAccurate(false),
Damien Bargiacchi9071db12016-10-28 17:38:22 -0700103 mTimeFormat12Hour(false), mTimeCheckThread(NULL) {
Mathias Agopian627e7b52009-05-21 19:21:59 -0700104 mSession = new SurfaceComposerClient();
Geoffrey Pitsch290c4352016-08-09 14:35:10 -0400105
106 // If the system has already booted, the animation is not being used for a boot.
107 mSystemBoot = !property_get_bool(BOOT_COMPLETED_PROP_NAME, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800108}
109
Damien Bargiacchi97480862016-03-29 14:55:55 -0700110BootAnimation::~BootAnimation() {}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800111
112void 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();
Geoffrey Pitschd6d9a1d2016-06-08 00:38:58 -0700134 audioplay::destroy();
Mathias Agopianbc726112009-09-23 15:44:05 -0700135}
136
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800137status_t BootAnimation::initTexture(Texture* texture, AssetManager& assets,
138 const char* name) {
139 Asset* asset = assets.open(name, Asset::ACCESS_BUFFER);
Andreas Gampecfedceb2014-09-30 21:48:18 -0700140 if (asset == NULL)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800141 return NO_INIT;
142 SkBitmap bitmap;
143 SkImageDecoder::DecodeMemory(asset->getBuffer(false), asset->getLength(),
Mike Reed42a1d082014-07-07 18:06:18 -0400144 &bitmap, kUnknown_SkColorType, SkImageDecoder::kDecodePixels_Mode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800145 asset->close();
146 delete asset;
147
148 // ensure we can call getPixels(). No need to call unlock, since the
149 // bitmap will go out of scope when we return from this method.
150 bitmap.lockPixels();
151
152 const int w = bitmap.width();
153 const int h = bitmap.height();
154 const void* p = bitmap.getPixels();
155
156 GLint crop[4] = { 0, h, w, -h };
157 texture->w = w;
158 texture->h = h;
159
160 glGenTextures(1, &texture->name);
161 glBindTexture(GL_TEXTURE_2D, texture->name);
162
Mike Reed42a1d082014-07-07 18:06:18 -0400163 switch (bitmap.colorType()) {
164 case kAlpha_8_SkColorType:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800165 glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, w, h, 0, GL_ALPHA,
166 GL_UNSIGNED_BYTE, p);
167 break;
Mike Reed42a1d082014-07-07 18:06:18 -0400168 case kARGB_4444_SkColorType:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800169 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA,
170 GL_UNSIGNED_SHORT_4_4_4_4, p);
171 break;
Mike Reed42a1d082014-07-07 18:06:18 -0400172 case kN32_SkColorType:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800173 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA,
174 GL_UNSIGNED_BYTE, p);
175 break;
Mike Reed42a1d082014-07-07 18:06:18 -0400176 case kRGB_565_SkColorType:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800177 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, w, h, 0, GL_RGB,
178 GL_UNSIGNED_SHORT_5_6_5, p);
179 break;
180 default:
181 break;
182 }
183
184 glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_CROP_RECT_OES, crop);
185 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
186 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
187 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
188 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700189
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800190 return NO_ERROR;
191}
192
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700193status_t BootAnimation::initTexture(FileMap* map, int* width, int* height)
Mathias Agopiana8826d62009-10-01 03:10:14 -0700194{
Mathias Agopiana8826d62009-10-01 03:10:14 -0700195 SkBitmap bitmap;
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700196 SkMemoryStream stream(map->getDataPtr(), map->getDataLength());
Mathias Agopian2b99e552011-11-10 15:59:07 -0800197 SkImageDecoder* codec = SkImageDecoder::Factory(&stream);
Andreas Gampecfedceb2014-09-30 21:48:18 -0700198 if (codec != NULL) {
Elliott Hughesc367d482013-10-29 13:12:55 -0700199 codec->setDitherImage(false);
Mathias Agopian2b99e552011-11-10 15:59:07 -0800200 codec->decode(&stream, &bitmap,
Mike Reed42a1d082014-07-07 18:06:18 -0400201 kN32_SkColorType,
Mathias Agopian2b99e552011-11-10 15:59:07 -0800202 SkImageDecoder::kDecodePixels_Mode);
203 delete codec;
204 }
Mathias Agopiana8826d62009-10-01 03:10:14 -0700205
Mykola Kondratenko0c1eeb32014-04-15 09:35:44 +0200206 // FileMap memory is never released until application exit.
207 // Release it now as the texture is already loaded and the memory used for
208 // the packed resource can be released.
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700209 delete map;
Mykola Kondratenko0c1eeb32014-04-15 09:35:44 +0200210
Mathias Agopiana8826d62009-10-01 03:10:14 -0700211 // ensure we can call getPixels(). No need to call unlock, since the
212 // bitmap will go out of scope when we return from this method.
213 bitmap.lockPixels();
214
215 const int w = bitmap.width();
216 const int h = bitmap.height();
217 const void* p = bitmap.getPixels();
218
219 GLint crop[4] = { 0, h, w, -h };
220 int tw = 1 << (31 - __builtin_clz(w));
221 int th = 1 << (31 - __builtin_clz(h));
222 if (tw < w) tw <<= 1;
223 if (th < h) th <<= 1;
224
Mike Reed42a1d082014-07-07 18:06:18 -0400225 switch (bitmap.colorType()) {
226 case kN32_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_RGBA, tw, th, 0, GL_RGBA,
229 GL_UNSIGNED_BYTE, 0);
230 glTexSubImage2D(GL_TEXTURE_2D, 0,
231 0, 0, w, h, GL_RGBA, GL_UNSIGNED_BYTE, p);
232 } else {
Sai Kiran Korwar27167492015-07-07 20:00:06 +0530233 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA,
Mathias Agopiana8826d62009-10-01 03:10:14 -0700234 GL_UNSIGNED_BYTE, p);
235 }
236 break;
237
Mike Reed42a1d082014-07-07 18:06:18 -0400238 case kRGB_565_SkColorType:
Sai Kiran Korwar27167492015-07-07 20:00:06 +0530239 if (!mUseNpotTextures && (tw != w || th != h)) {
Mathias Agopiana8826d62009-10-01 03:10:14 -0700240 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, tw, th, 0, GL_RGB,
241 GL_UNSIGNED_SHORT_5_6_5, 0);
242 glTexSubImage2D(GL_TEXTURE_2D, 0,
243 0, 0, w, h, GL_RGB, GL_UNSIGNED_SHORT_5_6_5, p);
244 } else {
Sai Kiran Korwar27167492015-07-07 20:00:06 +0530245 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, w, h, 0, GL_RGB,
Mathias Agopiana8826d62009-10-01 03:10:14 -0700246 GL_UNSIGNED_SHORT_5_6_5, p);
247 }
248 break;
249 default:
250 break;
251 }
252
253 glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_CROP_RECT_OES, crop);
254
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700255 *width = w;
256 *height = h;
257
Mathias Agopiana8826d62009-10-01 03:10:14 -0700258 return NO_ERROR;
259}
260
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800261status_t BootAnimation::readyToRun() {
262 mAssets.addDefaultAssets();
263
Jeff Brown0b722fe2012-08-24 22:40:14 -0700264 sp<IBinder> dtoken(SurfaceComposerClient::getBuiltInDisplay(
265 ISurfaceComposer::eDisplayIdMain));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800266 DisplayInfo dinfo;
Jeff Brown0b722fe2012-08-24 22:40:14 -0700267 status_t status = SurfaceComposerClient::getDisplayInfo(dtoken, &dinfo);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800268 if (status)
269 return -1;
270
271 // create the native surface
Jeff Brown0b722fe2012-08-24 22:40:14 -0700272 sp<SurfaceControl> control = session()->createSurface(String8("BootAnimation"),
273 dinfo.w, dinfo.h, PIXEL_FORMAT_RGB_565);
Mathias Agopian439863f2011-06-28 19:09:31 -0700274
275 SurfaceComposerClient::openGlobalTransaction();
Mathias Agopian17f638b2009-04-16 20:04:08 -0700276 control->setLayer(0x40000000);
Mathias Agopian439863f2011-06-28 19:09:31 -0700277 SurfaceComposerClient::closeGlobalTransaction();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800278
Mathias Agopian17f638b2009-04-16 20:04:08 -0700279 sp<Surface> s = control->getSurface();
280
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800281 // initialize opengl and egl
Mathias Agopian738b9a42009-08-06 16:41:02 -0700282 const EGLint attribs[] = {
Mathias Agopian1b253b72011-08-15 15:20:22 -0700283 EGL_RED_SIZE, 8,
284 EGL_GREEN_SIZE, 8,
285 EGL_BLUE_SIZE, 8,
Mathias Agopiana8826d62009-10-01 03:10:14 -0700286 EGL_DEPTH_SIZE, 0,
287 EGL_NONE
Mathias Agopian738b9a42009-08-06 16:41:02 -0700288 };
Andreas Gampecfedceb2014-09-30 21:48:18 -0700289 EGLint w, h;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800290 EGLint numConfigs;
291 EGLConfig config;
292 EGLSurface surface;
293 EGLContext context;
Mathias Agopian627e7b52009-05-21 19:21:59 -0700294
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800295 EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
Mathias Agopian627e7b52009-05-21 19:21:59 -0700296
297 eglInitialize(display, 0, 0);
Mathias Agopian1b253b72011-08-15 15:20:22 -0700298 eglChooseConfig(display, attribs, &config, 1, &numConfigs);
Mathias Agopian1473f462009-04-10 14:24:30 -0700299 surface = eglCreateWindowSurface(display, config, s.get(), NULL);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800300 context = eglCreateContext(display, config, NULL, NULL);
301 eglQuerySurface(display, surface, EGL_WIDTH, &w);
302 eglQuerySurface(display, surface, EGL_HEIGHT, &h);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700303
Mathias Agopianabac0102009-07-31 14:47:00 -0700304 if (eglMakeCurrent(display, surface, surface, context) == EGL_FALSE)
305 return NO_INIT;
Mathias Agopiana8826d62009-10-01 03:10:14 -0700306
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800307 mDisplay = display;
308 mContext = context;
309 mSurface = surface;
310 mWidth = w;
311 mHeight = h;
Mathias Agopian17f638b2009-04-16 20:04:08 -0700312 mFlingerSurfaceControl = control;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800313 mFlingerSurface = s;
314
Elliott Hughesc367d482013-10-29 13:12:55 -0700315 // If the device has encryption turned on or is in process
Jason parksbd9a08d2011-01-31 15:04:34 -0600316 // of being encrypted we show the encrypted boot animation.
317 char decrypt[PROPERTY_VALUE_MAX];
318 property_get("vold.decrypt", decrypt, "");
319
320 bool encryptedAnimation = atoi(decrypt) != 0 || !strcmp("trigger_restart_min_framework", decrypt);
321
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700322 if (encryptedAnimation && (access(SYSTEM_ENCRYPTED_BOOTANIMATION_FILE, R_OK) == 0)) {
323 mZipFileName = SYSTEM_ENCRYPTED_BOOTANIMATION_FILE;
Jason parksbd9a08d2011-01-31 15:04:34 -0600324 }
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700325 else if (access(OEM_BOOTANIMATION_FILE, R_OK) == 0) {
326 mZipFileName = OEM_BOOTANIMATION_FILE;
327 }
328 else if (access(SYSTEM_BOOTANIMATION_FILE, R_OK) == 0) {
329 mZipFileName = SYSTEM_BOOTANIMATION_FILE;
330 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800331 return NO_ERROR;
332}
333
Mathias Agopiana8826d62009-10-01 03:10:14 -0700334bool BootAnimation::threadLoop()
335{
336 bool r;
Narayan Kamathafd31e02013-12-03 13:16:03 +0000337 // We have no bootanimation file, so we use the stock android logo
338 // animation.
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700339 if (mZipFileName.isEmpty()) {
Mathias Agopiana8826d62009-10-01 03:10:14 -0700340 r = android();
341 } else {
342 r = movie();
343 }
344
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800345 eglMakeCurrent(mDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
346 eglDestroyContext(mDisplay, mContext);
347 eglDestroySurface(mDisplay, mSurface);
Mathias Agopian6cf0db22009-04-17 19:36:26 -0700348 mFlingerSurface.clear();
Mathias Agopian17f638b2009-04-16 20:04:08 -0700349 mFlingerSurfaceControl.clear();
Mathias Agopian627e7b52009-05-21 19:21:59 -0700350 eglTerminate(mDisplay);
Sai Kiran Korwar3eee9fb2015-06-16 17:13:35 +0530351 eglReleaseThread();
Mathias Agopian627e7b52009-05-21 19:21:59 -0700352 IPCThreadState::self()->stopProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800353 return r;
354}
355
Mathias Agopiana8826d62009-10-01 03:10:14 -0700356bool BootAnimation::android()
357{
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700358 initTexture(&mAndroid[0], mAssets, "images/android-logo-mask.png");
359 initTexture(&mAndroid[1], mAssets, "images/android-logo-shine.png");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800360
361 // clear screen
Mathias Agopiana8826d62009-10-01 03:10:14 -0700362 glShadeModel(GL_FLAT);
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700363 glDisable(GL_DITHER);
364 glDisable(GL_SCISSOR_TEST);
Mathias Agopian59f19e42011-05-06 19:22:12 -0700365 glClearColor(0,0,0,1);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800366 glClear(GL_COLOR_BUFFER_BIT);
367 eglSwapBuffers(mDisplay, mSurface);
368
Mathias Agopiana8826d62009-10-01 03:10:14 -0700369 glEnable(GL_TEXTURE_2D);
370 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
371
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700372 const GLint xc = (mWidth - mAndroid[0].w) / 2;
373 const GLint yc = (mHeight - mAndroid[0].h) / 2;
374 const Rect updateRect(xc, yc, xc + mAndroid[0].w, yc + mAndroid[0].h);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800375
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800376 glScissor(updateRect.left, mHeight - updateRect.bottom, updateRect.width(),
377 updateRect.height());
378
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700379 // Blend state
380 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
381 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
382
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800383 const nsecs_t startTime = systemTime();
384 do {
Mathias Agopian13796652009-03-24 22:49:21 -0700385 nsecs_t now = systemTime();
386 double time = now - startTime;
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700387 float t = 4.0f * float(time / us2ns(16667)) / mAndroid[1].w;
388 GLint offset = (1 - (t - floorf(t))) * mAndroid[1].w;
389 GLint x = xc - offset;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800390
Mathias Agopian81668642009-07-28 11:41:30 -0700391 glDisable(GL_SCISSOR_TEST);
392 glClear(GL_COLOR_BUFFER_BIT);
393
394 glEnable(GL_SCISSOR_TEST);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800395 glDisable(GL_BLEND);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800396 glBindTexture(GL_TEXTURE_2D, mAndroid[1].name);
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700397 glDrawTexiOES(x, yc, 0, mAndroid[1].w, mAndroid[1].h);
398 glDrawTexiOES(x + mAndroid[1].w, yc, 0, mAndroid[1].w, mAndroid[1].h);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800399
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700400 glEnable(GL_BLEND);
401 glBindTexture(GL_TEXTURE_2D, mAndroid[0].name);
402 glDrawTexiOES(xc, yc, 0, mAndroid[0].w, mAndroid[0].h);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800403
Mathias Agopian627e7b52009-05-21 19:21:59 -0700404 EGLBoolean res = eglSwapBuffers(mDisplay, mSurface);
405 if (res == EGL_FALSE)
406 break;
407
Mathias Agopian13796652009-03-24 22:49:21 -0700408 // 12fps: don't animate too fast to preserve CPU
409 const nsecs_t sleepTime = 83333 - ns2us(systemTime() - now);
410 if (sleepTime > 0)
Mathias Agopiana8826d62009-10-01 03:10:14 -0700411 usleep(sleepTime);
Kevin Hesterd3782b22012-04-26 10:38:55 -0700412
413 checkExit();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800414 } while (!exitPending());
415
416 glDeleteTextures(1, &mAndroid[0].name);
417 glDeleteTextures(1, &mAndroid[1].name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800418 return false;
419}
420
Kevin Hesterd3782b22012-04-26 10:38:55 -0700421void BootAnimation::checkExit() {
422 // Allow surface flinger to gracefully request shutdown
423 char value[PROPERTY_VALUE_MAX];
424 property_get(EXIT_PROP_NAME, value, "0");
425 int exitnow = atoi(value);
426 if (exitnow) {
427 requestExit();
428 }
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
Geoffrey Pitscha91a2d72016-07-12 14:46:19 -0400698 Animation::Part* partWithAudio = NULL;
Narayan Kamathafd31e02013-12-03 13:16:03 +0000699 ZipEntryRO entry;
700 char name[ANIM_ENTRY_NAME_MAX];
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400701 while ((entry = zip->nextEntry(cookie)) != NULL) {
702 const int foundEntryName = zip->getEntryFileName(entry, name, ANIM_ENTRY_NAME_MAX);
Narayan Kamathafd31e02013-12-03 13:16:03 +0000703 if (foundEntryName > ANIM_ENTRY_NAME_MAX || foundEntryName == -1) {
704 ALOGE("Error fetching entry file name");
705 continue;
706 }
707
708 const String8 entryName(name);
709 const String8 path(entryName.getPathDir());
710 const String8 leaf(entryName.getPathLeaf());
711 if (leaf.size() > 0) {
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700712 if (entryName == CLOCK_FONT_ZIP_NAME) {
713 FileMap* map = zip->createEntryFileMap(entry);
714 if (map) {
715 animation.clockFont.map = map;
716 }
717 continue;
718 }
719
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400720 for (size_t j = 0; j < pcount; j++) {
Narayan Kamathafd31e02013-12-03 13:16:03 +0000721 if (path == animation.parts[j].path) {
Narayan Kamath4600dd02015-06-16 12:02:57 +0100722 uint16_t method;
Narayan Kamathafd31e02013-12-03 13:16:03 +0000723 // supports only stored png files
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400724 if (zip->getEntryInfo(entry, &method, NULL, NULL, NULL, NULL, NULL)) {
Narayan Kamathafd31e02013-12-03 13:16:03 +0000725 if (method == ZipFileRO::kCompressStored) {
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400726 FileMap* map = zip->createEntryFileMap(entry);
Narayan Kamathafd31e02013-12-03 13:16:03 +0000727 if (map) {
Narayan Kamathafd31e02013-12-03 13:16:03 +0000728 Animation::Part& part(animation.parts.editItemAt(j));
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700729 if (leaf == "audio.wav") {
730 // a part may have at most one audio file
Geoffrey Pitschd6d9a1d2016-06-08 00:38:58 -0700731 part.audioData = (uint8_t *)map->getDataPtr();
732 part.audioLength = map->getDataLength();
Geoffrey Pitscha91a2d72016-07-12 14:46:19 -0400733 partWithAudio = &part;
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400734 } else if (leaf == "trim.txt") {
735 part.trimData.setTo((char const*)map->getDataPtr(),
736 map->getDataLength());
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700737 } else {
738 Animation::Frame frame;
739 frame.name = leaf;
740 frame.map = map;
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400741 frame.trimWidth = animation.width;
742 frame.trimHeight = animation.height;
743 frame.trimX = 0;
744 frame.trimY = 0;
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700745 part.frames.add(frame);
746 }
Mathias Agopiana8826d62009-10-01 03:10:14 -0700747 }
Geoffrey Pitschd6d9a1d2016-06-08 00:38:58 -0700748 } else {
749 ALOGE("bootanimation.zip is compressed; must be only stored");
Mathias Agopiana8826d62009-10-01 03:10:14 -0700750 }
751 }
752 }
753 }
754 }
755 }
756
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400757 // If there is trimData present, override the positioning defaults.
758 for (Animation::Part& part : animation.parts) {
759 const char* trimDataStr = part.trimData.string();
760 for (size_t frameIdx = 0; frameIdx < part.frames.size(); frameIdx++) {
761 const char* endl = strstr(trimDataStr, "\n");
762 // No more trimData for this part.
763 if (endl == NULL) {
764 break;
765 }
766 String8 line(trimDataStr, endl - trimDataStr);
767 const char* lineStr = line.string();
768 trimDataStr = ++endl;
769 int width = 0, height = 0, x = 0, y = 0;
770 if (sscanf(lineStr, "%dx%d+%d+%d", &width, &height, &x, &y) == 4) {
771 Animation::Frame& frame(part.frames.editItemAt(frameIdx));
772 frame.trimWidth = width;
773 frame.trimHeight = height;
774 frame.trimX = x;
775 frame.trimY = y;
776 } else {
777 ALOGE("Error parsing trim.txt, line: %s", lineStr);
778 break;
779 }
780 }
781 }
782
Geoffrey Pitschd6d9a1d2016-06-08 00:38:58 -0700783 // Create and initialize audioplay if there is a wav file in any of the animations.
Geoffrey Pitscha91a2d72016-07-12 14:46:19 -0400784 if (partWithAudio != NULL) {
Geoffrey Pitschd6d9a1d2016-06-08 00:38:58 -0700785 ALOGD("found audio.wav, creating playback engine");
Geoffrey Pitscha91a2d72016-07-12 14:46:19 -0400786 if (!audioplay::create(partWithAudio->audioData, partWithAudio->audioLength)) {
787 return false;
788 }
Geoffrey Pitschd6d9a1d2016-06-08 00:38:58 -0700789 }
790
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400791 zip->endIteration(cookie);
Narayan Kamathafd31e02013-12-03 13:16:03 +0000792
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700793 return true;
794}
795
796bool BootAnimation::movie()
797{
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700798 Animation* animation = loadAnimation(mZipFileName);
799 if (animation == NULL)
800 return false;
801
Damien Bargiacchi97480862016-03-29 14:55:55 -0700802 bool anyPartHasClock = false;
803 for (size_t i=0; i < animation->parts.size(); i++) {
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700804 if(validClock(animation->parts[i])) {
Damien Bargiacchi97480862016-03-29 14:55:55 -0700805 anyPartHasClock = true;
806 break;
807 }
808 }
809 if (!anyPartHasClock) {
810 mClockEnabled = false;
811 }
812
Sai Kiran Korwar27167492015-07-07 20:00:06 +0530813 // Check if npot textures are supported
814 mUseNpotTextures = false;
815 String8 gl_extensions;
816 const char* exts = reinterpret_cast<const char*>(glGetString(GL_EXTENSIONS));
817 if (!exts) {
818 glGetError();
819 } else {
820 gl_extensions.setTo(exts);
821 if ((gl_extensions.find("GL_ARB_texture_non_power_of_two") != -1) ||
822 (gl_extensions.find("GL_OES_texture_npot") != -1)) {
823 mUseNpotTextures = true;
824 }
825 }
826
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800827 // Blend required to draw time on top of animation frames.
828 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700829 glShadeModel(GL_FLAT);
830 glDisable(GL_DITHER);
831 glDisable(GL_SCISSOR_TEST);
832 glDisable(GL_BLEND);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700833
834 glBindTexture(GL_TEXTURE_2D, 0);
835 glEnable(GL_TEXTURE_2D);
836 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
837 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
838 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
839 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
840 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
841
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700842 bool clockFontInitialized = false;
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800843 if (mClockEnabled) {
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700844 clockFontInitialized =
845 (initFont(&animation->clockFont, CLOCK_FONT_ASSET) == NO_ERROR);
846 mClockEnabled = clockFontInitialized;
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800847 }
848
Damien Bargiacchi97480862016-03-29 14:55:55 -0700849 if (mClockEnabled && !updateIsTimeAccurate()) {
850 mTimeCheckThread = new TimeCheckThread(this);
851 mTimeCheckThread->run("BootAnimation::TimeCheckThread", PRIORITY_NORMAL);
852 }
853
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700854 playAnimation(*animation);
Damien Bargiacchi97480862016-03-29 14:55:55 -0700855
856 if (mTimeCheckThread != NULL) {
857 mTimeCheckThread->requestExit();
858 mTimeCheckThread = NULL;
859 }
860
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700861 releaseAnimation(animation);
862
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700863 if (clockFontInitialized) {
864 glDeleteTextures(1, &animation->clockFont.texture.name);
Andriy Naborskyy815e51d2016-03-24 16:43:34 -0700865 }
866
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700867 return false;
868}
869
870bool BootAnimation::playAnimation(const Animation& animation)
871{
872 const size_t pcount = animation.parts.size();
Mathias Agopiana8826d62009-10-01 03:10:14 -0700873 nsecs_t frameDuration = s2ns(1) / animation.fps;
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400874 const int animationX = (mWidth - animation.width) / 2;
875 const int animationY = (mHeight - animation.height) / 2;
Mathias Agopian9f3020d2009-11-06 16:30:18 -0800876
Narayan Kamathafd31e02013-12-03 13:16:03 +0000877 for (size_t i=0 ; i<pcount ; i++) {
Mathias Agopiana8826d62009-10-01 03:10:14 -0700878 const Animation::Part& part(animation.parts[i]);
879 const size_t fcount = part.frames.size();
880 glBindTexture(GL_TEXTURE_2D, 0);
881
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700882 // Handle animation package
883 if (part.animation != NULL) {
884 playAnimation(*part.animation);
885 if (exitPending())
886 break;
887 continue; //to next part
888 }
889
Mathias Agopiana8826d62009-10-01 03:10:14 -0700890 for (int r=0 ; !part.count || r<part.count ; r++) {
Kevin Hesterd3782b22012-04-26 10:38:55 -0700891 // Exit any non playuntil complete parts immediately
892 if(exitPending() && !part.playUntilComplete)
893 break;
894
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700895 // only play audio file the first time we animate the part
Geoffrey Pitsch290c4352016-08-09 14:35:10 -0400896 if (r == 0 && part.audioData && playSoundsAllowed()) {
897 ALOGD("playing clip for part%d, size=%d", (int) i, part.audioLength);
898 audioplay::playClip(part.audioData, part.audioLength);
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700899 }
900
Jesse Hall083b84c2014-09-22 10:51:09 -0700901 glClearColor(
902 part.backgroundColor[0],
903 part.backgroundColor[1],
904 part.backgroundColor[2],
905 1.0f);
906
Narayan Kamathafd31e02013-12-03 13:16:03 +0000907 for (size_t j=0 ; j<fcount && (!exitPending() || part.playUntilComplete) ; j++) {
Mathias Agopiana8826d62009-10-01 03:10:14 -0700908 const Animation::Frame& frame(part.frames[j]);
Mathias Agopiandb7dd2a2012-05-12 15:08:21 -0700909 nsecs_t lastFrame = systemTime();
Mathias Agopiana8826d62009-10-01 03:10:14 -0700910
911 if (r > 0) {
912 glBindTexture(GL_TEXTURE_2D, frame.tid);
913 } else {
914 if (part.count != 1) {
915 glGenTextures(1, &frame.tid);
916 glBindTexture(GL_TEXTURE_2D, frame.tid);
917 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
918 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
919 }
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700920 int w, h;
921 initTexture(frame.map, &w, &h);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700922 }
923
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400924 const int xc = animationX + frame.trimX;
925 const int yc = animationY + frame.trimY;
926 Region clearReg(Rect(mWidth, mHeight));
927 clearReg.subtractSelf(Rect(xc, yc, xc+frame.trimWidth, yc+frame.trimHeight));
Mathias Agopian9f3020d2009-11-06 16:30:18 -0800928 if (!clearReg.isEmpty()) {
929 Region::const_iterator head(clearReg.begin());
930 Region::const_iterator tail(clearReg.end());
931 glEnable(GL_SCISSOR_TEST);
932 while (head != tail) {
Andreas Gampecfedceb2014-09-30 21:48:18 -0700933 const Rect& r2(*head++);
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400934 glScissor(r2.left, mHeight - r2.bottom, r2.width(), r2.height());
Mathias Agopian9f3020d2009-11-06 16:30:18 -0800935 glClear(GL_COLOR_BUFFER_BIT);
936 }
937 glDisable(GL_SCISSOR_TEST);
938 }
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400939 // specify the y center as ceiling((mHeight - frame.trimHeight) / 2)
940 // which is equivalent to mHeight - (yc + frame.trimHeight)
941 glDrawTexiOES(xc, mHeight - (yc + frame.trimHeight),
942 0, frame.trimWidth, frame.trimHeight);
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700943 if (mClockEnabled && mTimeIsAccurate && validClock(part)) {
944 drawClock(animation.clockFont, part.clockPosX, part.clockPosY);
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800945 }
946
Mathias Agopiana8826d62009-10-01 03:10:14 -0700947 eglSwapBuffers(mDisplay, mSurface);
948
949 nsecs_t now = systemTime();
950 nsecs_t delay = frameDuration - (now - lastFrame);
Mathias Agopiandb7dd2a2012-05-12 15:08:21 -0700951 //ALOGD("%lld, %lld", ns2ms(now - lastFrame), ns2ms(delay));
Mathias Agopiana8826d62009-10-01 03:10:14 -0700952 lastFrame = now;
Mathias Agopiandb7dd2a2012-05-12 15:08:21 -0700953
954 if (delay > 0) {
955 struct timespec spec;
956 spec.tv_sec = (now + delay) / 1000000000;
957 spec.tv_nsec = (now + delay) % 1000000000;
958 int err;
959 do {
960 err = clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &spec, NULL);
961 } while (err<0 && errno == EINTR);
962 }
Kevin Hesterd3782b22012-04-26 10:38:55 -0700963
964 checkExit();
Mathias Agopiana8826d62009-10-01 03:10:14 -0700965 }
Kevin Hesterd3782b22012-04-26 10:38:55 -0700966
Mathias Agopiana8826d62009-10-01 03:10:14 -0700967 usleep(part.pause * ns2us(frameDuration));
Kevin Hesterd3782b22012-04-26 10:38:55 -0700968
969 // For infinite parts, we've now played them at least once, so perhaps exit
970 if(exitPending() && !part.count)
971 break;
Mathias Agopiana8826d62009-10-01 03:10:14 -0700972 }
973
Geoffrey Pitsch2fb30fb2016-07-06 16:16:20 -0400974 }
975
976 // Free textures created for looping parts now that the animation is done.
977 for (const Animation::Part& part : animation.parts) {
Mathias Agopiana8826d62009-10-01 03:10:14 -0700978 if (part.count != 1) {
Geoffrey Pitsch2fb30fb2016-07-06 16:16:20 -0400979 const size_t fcount = part.frames.size();
980 for (size_t j = 0; j < fcount; j++) {
Mathias Agopiana8826d62009-10-01 03:10:14 -0700981 const Animation::Frame& frame(part.frames[j]);
982 glDeleteTextures(1, &frame.tid);
983 }
984 }
985 }
Geoffrey Pitschd6d9a1d2016-06-08 00:38:58 -0700986
987 // we've finally played everything we're going to play
988 audioplay::setPlaying(false);
989 audioplay::destroy();
990
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700991 return true;
Mathias Agopiana8826d62009-10-01 03:10:14 -0700992}
993
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700994void BootAnimation::releaseAnimation(Animation* animation) const
995{
996 for (Vector<Animation::Part>::iterator it = animation->parts.begin(),
997 e = animation->parts.end(); it != e; ++it) {
998 if (it->animation)
999 releaseAnimation(it->animation);
1000 }
1001 if (animation->zip)
1002 delete animation->zip;
1003 delete animation;
1004}
1005
1006BootAnimation::Animation* BootAnimation::loadAnimation(const String8& fn)
1007{
1008 if (mLoadedFiles.indexOf(fn) >= 0) {
1009 ALOGE("File \"%s\" is already loaded. Cyclic ref is not allowed",
1010 fn.string());
1011 return NULL;
1012 }
1013 ZipFileRO *zip = ZipFileRO::open(fn);
1014 if (zip == NULL) {
1015 ALOGE("Failed to open animation zip \"%s\": %s",
1016 fn.string(), strerror(errno));
1017 return NULL;
1018 }
1019
1020 Animation *animation = new Animation;
1021 animation->fileName = fn;
1022 animation->zip = zip;
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -07001023 animation->clockFont.map = nullptr;
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001024 mLoadedFiles.add(animation->fileName);
1025
1026 parseAnimationDesc(*animation);
Geoffrey Pitscha91a2d72016-07-12 14:46:19 -04001027 if (!preloadZip(*animation)) {
1028 return NULL;
1029 }
1030
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001031
1032 mLoadedFiles.remove(fn);
1033 return animation;
1034}
Damien Bargiacchi97480862016-03-29 14:55:55 -07001035
Geoffrey Pitsch290c4352016-08-09 14:35:10 -04001036bool BootAnimation::playSoundsAllowed() const {
1037 // Only play sounds for system boots, not runtime restarts.
1038 if (!mSystemBoot) {
1039 return false;
1040 }
1041
1042 // Read the system property to see if we should play the sound.
1043 // If it's not present, default to allowed.
1044 if (!property_get_bool(PLAY_SOUND_PROP_NAME, 1)) {
1045 return false;
1046 }
1047
1048 // Don't play sounds if this is a reboot due to an error.
1049 char bootreason[PROPERTY_VALUE_MAX];
1050 if (property_get(BOOTREASON_PROP_NAME, bootreason, nullptr) > 0) {
1051 for (const auto& str : PLAY_SOUND_BOOTREASON_BLACKLIST) {
1052 if (strcasecmp(str.c_str(), bootreason) == 0) {
1053 return false;
1054 }
1055 }
1056 }
1057 return true;
1058}
1059
Damien Bargiacchi97480862016-03-29 14:55:55 -07001060bool BootAnimation::updateIsTimeAccurate() {
1061 static constexpr long long MAX_TIME_IN_PAST = 60000LL * 60LL * 24LL * 30LL; // 30 days
1062 static constexpr long long MAX_TIME_IN_FUTURE = 60000LL * 90LL; // 90 minutes
1063
1064 if (mTimeIsAccurate) {
1065 return true;
1066 }
1067
1068 struct stat statResult;
Damien Bargiacchi9071db12016-10-28 17:38:22 -07001069
1070 if(stat(TIME_FORMAT_12_HOUR_FLAG_FILE_PATH, &statResult) == 0) {
1071 mTimeFormat12Hour = true;
1072 }
1073
Damien Bargiacchi97480862016-03-29 14:55:55 -07001074 if(stat(ACCURATE_TIME_FLAG_FILE_PATH, &statResult) == 0) {
1075 mTimeIsAccurate = true;
1076 return true;
1077 }
1078
1079 FILE* file = fopen(LAST_TIME_CHANGED_FILE_PATH, "r");
1080 if (file != NULL) {
1081 long long lastChangedTime = 0;
1082 fscanf(file, "%lld", &lastChangedTime);
1083 fclose(file);
1084 if (lastChangedTime > 0) {
1085 struct timespec now;
1086 clock_gettime(CLOCK_REALTIME, &now);
1087 // Match the Java timestamp format
1088 long long rtcNow = (now.tv_sec * 1000LL) + (now.tv_nsec / 1000000LL);
Damien Bargiacchi96762812016-07-12 15:53:40 -07001089 if (ACCURATE_TIME_EPOCH < rtcNow
1090 && lastChangedTime > (rtcNow - MAX_TIME_IN_PAST)
1091 && lastChangedTime < (rtcNow + MAX_TIME_IN_FUTURE)) {
Damien Bargiacchi97480862016-03-29 14:55:55 -07001092 mTimeIsAccurate = true;
1093 }
1094 }
1095 }
1096
1097 return mTimeIsAccurate;
1098}
1099
1100BootAnimation::TimeCheckThread::TimeCheckThread(BootAnimation* bootAnimation) : Thread(false),
1101 mInotifyFd(-1), mSystemWd(-1), mTimeWd(-1), mBootAnimation(bootAnimation) {}
1102
1103BootAnimation::TimeCheckThread::~TimeCheckThread() {
1104 // mInotifyFd may be -1 but that's ok since we're not at risk of attempting to close a valid FD.
1105 close(mInotifyFd);
1106}
1107
1108bool BootAnimation::TimeCheckThread::threadLoop() {
1109 bool shouldLoop = doThreadLoop() && !mBootAnimation->mTimeIsAccurate
1110 && mBootAnimation->mClockEnabled;
1111 if (!shouldLoop) {
1112 close(mInotifyFd);
1113 mInotifyFd = -1;
1114 }
1115 return shouldLoop;
1116}
1117
1118bool BootAnimation::TimeCheckThread::doThreadLoop() {
1119 static constexpr int BUFF_LEN (10 * (sizeof(struct inotify_event) + NAME_MAX + 1));
1120
1121 // Poll instead of doing a blocking read so the Thread can exit if requested.
1122 struct pollfd pfd = { mInotifyFd, POLLIN, 0 };
1123 ssize_t pollResult = poll(&pfd, 1, 1000);
1124
1125 if (pollResult == 0) {
1126 return true;
1127 } else if (pollResult < 0) {
1128 ALOGE("Could not poll inotify events");
1129 return false;
1130 }
1131
1132 char buff[BUFF_LEN] __attribute__ ((aligned(__alignof__(struct inotify_event))));;
1133 ssize_t length = read(mInotifyFd, buff, BUFF_LEN);
1134 if (length == 0) {
1135 return true;
1136 } else if (length < 0) {
1137 ALOGE("Could not read inotify events");
1138 return false;
1139 }
1140
1141 const struct inotify_event *event;
1142 for (char* ptr = buff; ptr < buff + length; ptr += sizeof(struct inotify_event) + event->len) {
1143 event = (const struct inotify_event *) ptr;
1144 if (event->wd == mSystemWd && strcmp(SYSTEM_TIME_DIR_NAME, event->name) == 0) {
1145 addTimeDirWatch();
1146 } else if (event->wd == mTimeWd && (strcmp(LAST_TIME_CHANGED_FILE_NAME, event->name) == 0
1147 || strcmp(ACCURATE_TIME_FLAG_FILE_NAME, event->name) == 0)) {
1148 return !mBootAnimation->updateIsTimeAccurate();
1149 }
1150 }
1151
1152 return true;
1153}
1154
1155void BootAnimation::TimeCheckThread::addTimeDirWatch() {
1156 mTimeWd = inotify_add_watch(mInotifyFd, SYSTEM_TIME_DIR_PATH,
1157 IN_CLOSE_WRITE | IN_MOVED_TO | IN_ATTRIB);
1158 if (mTimeWd > 0) {
1159 // No need to watch for the time directory to be created if it already exists
1160 inotify_rm_watch(mInotifyFd, mSystemWd);
1161 mSystemWd = -1;
1162 }
1163}
1164
1165status_t BootAnimation::TimeCheckThread::readyToRun() {
1166 mInotifyFd = inotify_init();
1167 if (mInotifyFd < 0) {
1168 ALOGE("Could not initialize inotify fd");
1169 return NO_INIT;
1170 }
1171
1172 mSystemWd = inotify_add_watch(mInotifyFd, SYSTEM_DATA_DIR_PATH, IN_CREATE | IN_ATTRIB);
1173 if (mSystemWd < 0) {
1174 close(mInotifyFd);
1175 mInotifyFd = -1;
1176 ALOGE("Could not add watch for %s", SYSTEM_DATA_DIR_PATH);
1177 return NO_INIT;
1178 }
1179
1180 addTimeDirWatch();
1181
1182 if (mBootAnimation->updateIsTimeAccurate()) {
1183 close(mInotifyFd);
1184 mInotifyFd = -1;
1185 return ALREADY_EXISTS;
1186 }
1187
1188 return NO_ERROR;
1189}
1190
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001191// ---------------------------------------------------------------------------
1192
1193}
1194; // namespace android