blob: 3c2efd891e1a5df4394b137143a4aa596f56ad4f [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>
Matt Sarett8898c162016-03-24 16:11:01 -040052#include <SkImage.h>
Derek Sollenbergereece0dd2014-02-27 14:31:29 -050053#include <SkStream.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 Bargiacchi96762812016-07-12 15:53:40 -070077// Java timestamp format. Don't show the clock if the date is before 2000-01-01 00:00:00.
78static const long long ACCURATE_TIME_EPOCH = 946684800000;
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -070079static constexpr char FONT_BEGIN_CHAR = ' ';
80static constexpr char FONT_END_CHAR = '~' + 1;
81static constexpr size_t FONT_NUM_CHARS = FONT_END_CHAR - FONT_BEGIN_CHAR + 1;
82static constexpr size_t FONT_NUM_COLS = 16;
83static constexpr size_t FONT_NUM_ROWS = FONT_NUM_CHARS / FONT_NUM_COLS;
84static const int TEXT_CENTER_VALUE = INT_MAX;
85static const int TEXT_MISSING_VALUE = INT_MIN;
Damien Bargiacchi97480862016-03-29 14:55:55 -070086static const char EXIT_PROP_NAME[] = "service.bootanim.exit";
Geoffrey Pitsch30508792016-07-22 17:04:21 -040087static const char PLAY_SOUND_PROP_NAME[] = "persist.sys.bootanim.play_sound";
Narayan Kamathafd31e02013-12-03 13:16:03 +000088static const int ANIM_ENTRY_NAME_MAX = 256;
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -070089static constexpr size_t TEXT_POS_LEN_MAX = 16;
Geoffrey Pitsch290c4352016-08-09 14:35:10 -040090static const char BOOT_COMPLETED_PROP_NAME[] = "sys.boot_completed";
91static const char BOOTREASON_PROP_NAME[] = "ro.boot.bootreason";
92// bootreasons list in "system/core/bootstat/bootstat.cpp".
93static const std::vector<std::string> PLAY_SOUND_BOOTREASON_BLACKLIST {
94 "kernel_panic",
95 "Panic",
96 "Watchdog",
97};
Narayan Kamathafd31e02013-12-03 13:16:03 +000098
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080099// ---------------------------------------------------------------------------
100
Damien Bargiacchi97480862016-03-29 14:55:55 -0700101BootAnimation::BootAnimation() : Thread(false), mClockEnabled(true), mTimeIsAccurate(false),
102 mTimeCheckThread(NULL) {
Mathias Agopian627e7b52009-05-21 19:21:59 -0700103 mSession = new SurfaceComposerClient();
Geoffrey Pitsch290c4352016-08-09 14:35:10 -0400104
105 // If the system has already booted, the animation is not being used for a boot.
106 mSystemBoot = !property_get_bool(BOOT_COMPLETED_PROP_NAME, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800107}
108
Damien Bargiacchi97480862016-03-29 14:55:55 -0700109BootAnimation::~BootAnimation() {}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800110
111void BootAnimation::onFirstRef() {
Mathias Agopianbc726112009-09-23 15:44:05 -0700112 status_t err = mSession->linkToComposerDeath(this);
Steve Block3762c312012-01-06 19:20:56 +0000113 ALOGE_IF(err, "linkToComposerDeath failed (%s) ", strerror(-err));
Mathias Agopian8434c532009-09-23 18:52:49 -0700114 if (err == NO_ERROR) {
Mathias Agopianbc726112009-09-23 15:44:05 -0700115 run("BootAnimation", PRIORITY_DISPLAY);
116 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800117}
118
Mathias Agopianbc726112009-09-23 15:44:05 -0700119sp<SurfaceComposerClient> BootAnimation::session() const {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800120 return mSession;
121}
122
Mathias Agopianbc726112009-09-23 15:44:05 -0700123
Narayan Kamathafd31e02013-12-03 13:16:03 +0000124void BootAnimation::binderDied(const wp<IBinder>&)
Mathias Agopianbc726112009-09-23 15:44:05 -0700125{
126 // woah, surfaceflinger died!
Steve Block5baa3a62011-12-20 16:23:08 +0000127 ALOGD("SurfaceFlinger died, exiting...");
Mathias Agopianbc726112009-09-23 15:44:05 -0700128
129 // calling requestExit() is not enough here because the Surface code
130 // might be blocked on a condition variable that will never be updated.
131 kill( getpid(), SIGKILL );
132 requestExit();
Geoffrey Pitschd6d9a1d2016-06-08 00:38:58 -0700133 audioplay::destroy();
Mathias Agopianbc726112009-09-23 15:44:05 -0700134}
135
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800136status_t BootAnimation::initTexture(Texture* texture, AssetManager& assets,
137 const char* name) {
138 Asset* asset = assets.open(name, Asset::ACCESS_BUFFER);
Andreas Gampecfedceb2014-09-30 21:48:18 -0700139 if (asset == NULL)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800140 return NO_INIT;
141 SkBitmap bitmap;
Matt Sarett8898c162016-03-24 16:11:01 -0400142 sk_sp<SkData> data = SkData::MakeWithoutCopy(asset->getBuffer(false),
143 asset->getLength());
144 sk_sp<SkImage> image = SkImage::MakeFromEncoded(data);
145 image->asLegacyBitmap(&bitmap, SkImage::kRO_LegacyBitmapMode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800146 asset->close();
147 delete asset;
148
149 // ensure we can call getPixels(). No need to call unlock, since the
150 // bitmap will go out of scope when we return from this method.
151 bitmap.lockPixels();
152
153 const int w = bitmap.width();
154 const int h = bitmap.height();
155 const void* p = bitmap.getPixels();
156
157 GLint crop[4] = { 0, h, w, -h };
158 texture->w = w;
159 texture->h = h;
160
161 glGenTextures(1, &texture->name);
162 glBindTexture(GL_TEXTURE_2D, texture->name);
163
Mike Reed42a1d082014-07-07 18:06:18 -0400164 switch (bitmap.colorType()) {
165 case kAlpha_8_SkColorType:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800166 glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, w, h, 0, GL_ALPHA,
167 GL_UNSIGNED_BYTE, p);
168 break;
Mike Reed42a1d082014-07-07 18:06:18 -0400169 case kARGB_4444_SkColorType:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800170 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA,
171 GL_UNSIGNED_SHORT_4_4_4_4, p);
172 break;
Mike Reed42a1d082014-07-07 18:06:18 -0400173 case kN32_SkColorType:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800174 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA,
175 GL_UNSIGNED_BYTE, p);
176 break;
Mike Reed42a1d082014-07-07 18:06:18 -0400177 case kRGB_565_SkColorType:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800178 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, w, h, 0, GL_RGB,
179 GL_UNSIGNED_SHORT_5_6_5, p);
180 break;
181 default:
182 break;
183 }
184
185 glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_CROP_RECT_OES, crop);
186 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
187 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
188 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
189 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700190
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800191 return NO_ERROR;
192}
193
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700194status_t BootAnimation::initTexture(FileMap* map, int* width, int* height)
Mathias Agopiana8826d62009-10-01 03:10:14 -0700195{
Mathias Agopiana8826d62009-10-01 03:10:14 -0700196 SkBitmap bitmap;
Damien Bargiacchif67b3172016-09-07 20:17:14 -0700197 sk_sp<SkData> data = SkData::MakeWithoutCopy(map->getDataPtr(),
198 map->getDataLength());
Matt Sarett8898c162016-03-24 16:11:01 -0400199 sk_sp<SkImage> image = SkImage::MakeFromEncoded(data);
200 image->asLegacyBitmap(&bitmap, SkImage::kRO_LegacyBitmapMode);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700201
Mykola Kondratenko0c1eeb32014-04-15 09:35:44 +0200202 // FileMap memory is never released until application exit.
203 // Release it now as the texture is already loaded and the memory used for
204 // the packed resource can be released.
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700205 delete map;
Mykola Kondratenko0c1eeb32014-04-15 09:35:44 +0200206
Mathias Agopiana8826d62009-10-01 03:10:14 -0700207 // ensure we can call getPixels(). No need to call unlock, since the
208 // bitmap will go out of scope when we return from this method.
209 bitmap.lockPixels();
210
211 const int w = bitmap.width();
212 const int h = bitmap.height();
213 const void* p = bitmap.getPixels();
214
215 GLint crop[4] = { 0, h, w, -h };
216 int tw = 1 << (31 - __builtin_clz(w));
217 int th = 1 << (31 - __builtin_clz(h));
218 if (tw < w) tw <<= 1;
219 if (th < h) th <<= 1;
220
Mike Reed42a1d082014-07-07 18:06:18 -0400221 switch (bitmap.colorType()) {
222 case kN32_SkColorType:
Sai Kiran Korwar27167492015-07-07 20:00:06 +0530223 if (!mUseNpotTextures && (tw != w || th != h)) {
Mathias Agopiana8826d62009-10-01 03:10:14 -0700224 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, tw, th, 0, GL_RGBA,
225 GL_UNSIGNED_BYTE, 0);
226 glTexSubImage2D(GL_TEXTURE_2D, 0,
227 0, 0, w, h, GL_RGBA, GL_UNSIGNED_BYTE, p);
228 } else {
Sai Kiran Korwar27167492015-07-07 20:00:06 +0530229 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA,
Mathias Agopiana8826d62009-10-01 03:10:14 -0700230 GL_UNSIGNED_BYTE, p);
231 }
232 break;
233
Mike Reed42a1d082014-07-07 18:06:18 -0400234 case kRGB_565_SkColorType:
Sai Kiran Korwar27167492015-07-07 20:00:06 +0530235 if (!mUseNpotTextures && (tw != w || th != h)) {
Mathias Agopiana8826d62009-10-01 03:10:14 -0700236 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, tw, th, 0, GL_RGB,
237 GL_UNSIGNED_SHORT_5_6_5, 0);
238 glTexSubImage2D(GL_TEXTURE_2D, 0,
239 0, 0, w, h, GL_RGB, GL_UNSIGNED_SHORT_5_6_5, p);
240 } else {
Sai Kiran Korwar27167492015-07-07 20:00:06 +0530241 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, w, h, 0, GL_RGB,
Mathias Agopiana8826d62009-10-01 03:10:14 -0700242 GL_UNSIGNED_SHORT_5_6_5, p);
243 }
244 break;
245 default:
246 break;
247 }
248
249 glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_CROP_RECT_OES, crop);
250
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700251 *width = w;
252 *height = h;
253
Mathias Agopiana8826d62009-10-01 03:10:14 -0700254 return NO_ERROR;
255}
256
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800257status_t BootAnimation::readyToRun() {
258 mAssets.addDefaultAssets();
259
Jeff Brown0b722fe2012-08-24 22:40:14 -0700260 sp<IBinder> dtoken(SurfaceComposerClient::getBuiltInDisplay(
261 ISurfaceComposer::eDisplayIdMain));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800262 DisplayInfo dinfo;
Jeff Brown0b722fe2012-08-24 22:40:14 -0700263 status_t status = SurfaceComposerClient::getDisplayInfo(dtoken, &dinfo);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800264 if (status)
265 return -1;
266
267 // create the native surface
Jeff Brown0b722fe2012-08-24 22:40:14 -0700268 sp<SurfaceControl> control = session()->createSurface(String8("BootAnimation"),
269 dinfo.w, dinfo.h, PIXEL_FORMAT_RGB_565);
Mathias Agopian439863f2011-06-28 19:09:31 -0700270
271 SurfaceComposerClient::openGlobalTransaction();
Mathias Agopian17f638b2009-04-16 20:04:08 -0700272 control->setLayer(0x40000000);
Mathias Agopian439863f2011-06-28 19:09:31 -0700273 SurfaceComposerClient::closeGlobalTransaction();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800274
Mathias Agopian17f638b2009-04-16 20:04:08 -0700275 sp<Surface> s = control->getSurface();
276
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800277 // initialize opengl and egl
Mathias Agopian738b9a42009-08-06 16:41:02 -0700278 const EGLint attribs[] = {
Mathias Agopian1b253b72011-08-15 15:20:22 -0700279 EGL_RED_SIZE, 8,
280 EGL_GREEN_SIZE, 8,
281 EGL_BLUE_SIZE, 8,
Mathias Agopiana8826d62009-10-01 03:10:14 -0700282 EGL_DEPTH_SIZE, 0,
283 EGL_NONE
Mathias Agopian738b9a42009-08-06 16:41:02 -0700284 };
Andreas Gampecfedceb2014-09-30 21:48:18 -0700285 EGLint w, h;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800286 EGLint numConfigs;
287 EGLConfig config;
288 EGLSurface surface;
289 EGLContext context;
Mathias Agopian627e7b52009-05-21 19:21:59 -0700290
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800291 EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
Mathias Agopian627e7b52009-05-21 19:21:59 -0700292
293 eglInitialize(display, 0, 0);
Mathias Agopian1b253b72011-08-15 15:20:22 -0700294 eglChooseConfig(display, attribs, &config, 1, &numConfigs);
Mathias Agopian1473f462009-04-10 14:24:30 -0700295 surface = eglCreateWindowSurface(display, config, s.get(), NULL);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800296 context = eglCreateContext(display, config, NULL, NULL);
297 eglQuerySurface(display, surface, EGL_WIDTH, &w);
298 eglQuerySurface(display, surface, EGL_HEIGHT, &h);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700299
Mathias Agopianabac0102009-07-31 14:47:00 -0700300 if (eglMakeCurrent(display, surface, surface, context) == EGL_FALSE)
301 return NO_INIT;
Mathias Agopiana8826d62009-10-01 03:10:14 -0700302
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800303 mDisplay = display;
304 mContext = context;
305 mSurface = surface;
306 mWidth = w;
307 mHeight = h;
Mathias Agopian17f638b2009-04-16 20:04:08 -0700308 mFlingerSurfaceControl = control;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800309 mFlingerSurface = s;
310
Elliott Hughesc367d482013-10-29 13:12:55 -0700311 // If the device has encryption turned on or is in process
Jason parksbd9a08d2011-01-31 15:04:34 -0600312 // of being encrypted we show the encrypted boot animation.
313 char decrypt[PROPERTY_VALUE_MAX];
314 property_get("vold.decrypt", decrypt, "");
315
316 bool encryptedAnimation = atoi(decrypt) != 0 || !strcmp("trigger_restart_min_framework", decrypt);
317
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700318 if (encryptedAnimation && (access(SYSTEM_ENCRYPTED_BOOTANIMATION_FILE, R_OK) == 0)) {
319 mZipFileName = SYSTEM_ENCRYPTED_BOOTANIMATION_FILE;
Jason parksbd9a08d2011-01-31 15:04:34 -0600320 }
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700321 else if (access(OEM_BOOTANIMATION_FILE, R_OK) == 0) {
322 mZipFileName = OEM_BOOTANIMATION_FILE;
323 }
324 else if (access(SYSTEM_BOOTANIMATION_FILE, R_OK) == 0) {
325 mZipFileName = SYSTEM_BOOTANIMATION_FILE;
326 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800327 return NO_ERROR;
328}
329
Mathias Agopiana8826d62009-10-01 03:10:14 -0700330bool BootAnimation::threadLoop()
331{
332 bool r;
Narayan Kamathafd31e02013-12-03 13:16:03 +0000333 // We have no bootanimation file, so we use the stock android logo
334 // animation.
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700335 if (mZipFileName.isEmpty()) {
Mathias Agopiana8826d62009-10-01 03:10:14 -0700336 r = android();
337 } else {
338 r = movie();
339 }
340
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800341 eglMakeCurrent(mDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
342 eglDestroyContext(mDisplay, mContext);
343 eglDestroySurface(mDisplay, mSurface);
Mathias Agopian6cf0db22009-04-17 19:36:26 -0700344 mFlingerSurface.clear();
Mathias Agopian17f638b2009-04-16 20:04:08 -0700345 mFlingerSurfaceControl.clear();
Mathias Agopian627e7b52009-05-21 19:21:59 -0700346 eglTerminate(mDisplay);
Sai Kiran Korwar3eee9fb2015-06-16 17:13:35 +0530347 eglReleaseThread();
Mathias Agopian627e7b52009-05-21 19:21:59 -0700348 IPCThreadState::self()->stopProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800349 return r;
350}
351
Mathias Agopiana8826d62009-10-01 03:10:14 -0700352bool BootAnimation::android()
353{
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700354 initTexture(&mAndroid[0], mAssets, "images/android-logo-mask.png");
355 initTexture(&mAndroid[1], mAssets, "images/android-logo-shine.png");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800356
357 // clear screen
Mathias Agopiana8826d62009-10-01 03:10:14 -0700358 glShadeModel(GL_FLAT);
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700359 glDisable(GL_DITHER);
360 glDisable(GL_SCISSOR_TEST);
Mathias Agopian59f19e42011-05-06 19:22:12 -0700361 glClearColor(0,0,0,1);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800362 glClear(GL_COLOR_BUFFER_BIT);
363 eglSwapBuffers(mDisplay, mSurface);
364
Mathias Agopiana8826d62009-10-01 03:10:14 -0700365 glEnable(GL_TEXTURE_2D);
366 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
367
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700368 const GLint xc = (mWidth - mAndroid[0].w) / 2;
369 const GLint yc = (mHeight - mAndroid[0].h) / 2;
370 const Rect updateRect(xc, yc, xc + mAndroid[0].w, yc + mAndroid[0].h);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800371
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800372 glScissor(updateRect.left, mHeight - updateRect.bottom, updateRect.width(),
373 updateRect.height());
374
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700375 // Blend state
376 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
377 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
378
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800379 const nsecs_t startTime = systemTime();
380 do {
Mathias Agopian13796652009-03-24 22:49:21 -0700381 nsecs_t now = systemTime();
382 double time = now - startTime;
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700383 float t = 4.0f * float(time / us2ns(16667)) / mAndroid[1].w;
384 GLint offset = (1 - (t - floorf(t))) * mAndroid[1].w;
385 GLint x = xc - offset;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800386
Mathias Agopian81668642009-07-28 11:41:30 -0700387 glDisable(GL_SCISSOR_TEST);
388 glClear(GL_COLOR_BUFFER_BIT);
389
390 glEnable(GL_SCISSOR_TEST);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800391 glDisable(GL_BLEND);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800392 glBindTexture(GL_TEXTURE_2D, mAndroid[1].name);
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700393 glDrawTexiOES(x, yc, 0, mAndroid[1].w, mAndroid[1].h);
394 glDrawTexiOES(x + mAndroid[1].w, yc, 0, mAndroid[1].w, mAndroid[1].h);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800395
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700396 glEnable(GL_BLEND);
397 glBindTexture(GL_TEXTURE_2D, mAndroid[0].name);
398 glDrawTexiOES(xc, yc, 0, mAndroid[0].w, mAndroid[0].h);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800399
Mathias Agopian627e7b52009-05-21 19:21:59 -0700400 EGLBoolean res = eglSwapBuffers(mDisplay, mSurface);
401 if (res == EGL_FALSE)
402 break;
403
Mathias Agopian13796652009-03-24 22:49:21 -0700404 // 12fps: don't animate too fast to preserve CPU
405 const nsecs_t sleepTime = 83333 - ns2us(systemTime() - now);
406 if (sleepTime > 0)
Mathias Agopiana8826d62009-10-01 03:10:14 -0700407 usleep(sleepTime);
Kevin Hesterd3782b22012-04-26 10:38:55 -0700408
409 checkExit();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800410 } while (!exitPending());
411
412 glDeleteTextures(1, &mAndroid[0].name);
413 glDeleteTextures(1, &mAndroid[1].name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800414 return false;
415}
416
Kevin Hesterd3782b22012-04-26 10:38:55 -0700417void BootAnimation::checkExit() {
418 // Allow surface flinger to gracefully request shutdown
419 char value[PROPERTY_VALUE_MAX];
420 property_get(EXIT_PROP_NAME, value, "0");
421 int exitnow = atoi(value);
422 if (exitnow) {
423 requestExit();
424 }
425}
426
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700427bool BootAnimation::validClock(const Animation::Part& part) {
428 return part.clockPosX != TEXT_MISSING_VALUE && part.clockPosY != TEXT_MISSING_VALUE;
429}
430
431bool parseTextCoord(const char* str, int* dest) {
432 if (strcmp("c", str) == 0) {
433 *dest = TEXT_CENTER_VALUE;
434 return true;
435 }
436
437 char* end;
438 int val = (int) strtol(str, &end, 0);
439 if (end == str || *end != '\0' || val == INT_MAX || val == INT_MIN) {
440 return false;
441 }
442 *dest = val;
443 return true;
444}
445
446// Parse two position coordinates. If only string is non-empty, treat it as the y value.
447void parsePosition(const char* str1, const char* str2, int* x, int* y) {
448 bool success = false;
449 if (strlen(str1) == 0) { // No values were specified
450 // success = false
451 } else if (strlen(str2) == 0) { // we have only one value
452 if (parseTextCoord(str1, y)) {
453 *x = TEXT_CENTER_VALUE;
454 success = true;
455 }
456 } else {
457 if (parseTextCoord(str1, x) && parseTextCoord(str2, y)) {
458 success = true;
459 }
460 }
461
462 if (!success) {
463 *x = TEXT_MISSING_VALUE;
464 *y = TEXT_MISSING_VALUE;
465 }
466}
467
Jesse Hall083b84c2014-09-22 10:51:09 -0700468// Parse a color represented as an HTML-style 'RRGGBB' string: each pair of
469// characters in str is a hex number in [0, 255], which are converted to
470// floating point values in the range [0.0, 1.0] and placed in the
471// corresponding elements of color.
472//
473// If the input string isn't valid, parseColor returns false and color is
474// left unchanged.
475static bool parseColor(const char str[7], float color[3]) {
476 float tmpColor[3];
477 for (int i = 0; i < 3; i++) {
478 int val = 0;
479 for (int j = 0; j < 2; j++) {
480 val *= 16;
481 char c = str[2*i + j];
482 if (c >= '0' && c <= '9') val += c - '0';
483 else if (c >= 'A' && c <= 'F') val += (c - 'A') + 10;
484 else if (c >= 'a' && c <= 'f') val += (c - 'a') + 10;
485 else return false;
486 }
487 tmpColor[i] = static_cast<float>(val) / 255.0f;
488 }
489 memcpy(color, tmpColor, sizeof(tmpColor));
490 return true;
491}
492
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700493
494static bool readFile(ZipFileRO* zip, const char* name, String8& outString)
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700495{
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700496 ZipEntryRO entry = zip->findEntryByName(name);
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700497 ALOGE_IF(!entry, "couldn't find %s", name);
498 if (!entry) {
499 return false;
500 }
501
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700502 FileMap* entryMap = zip->createEntryFileMap(entry);
503 zip->releaseEntry(entry);
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700504 ALOGE_IF(!entryMap, "entryMap is null");
505 if (!entryMap) {
506 return false;
507 }
508
509 outString.setTo((char const*)entryMap->getDataPtr(), entryMap->getDataLength());
Narayan Kamath688ff4c2015-02-23 15:47:54 +0000510 delete entryMap;
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700511 return true;
512}
513
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700514// The font image should be a 96x2 array of character images. The
515// columns are the printable ASCII characters 0x20 - 0x7f. The
516// top row is regular text; the bottom row is bold.
517status_t BootAnimation::initFont(Font* font, const char* fallback) {
518 status_t status = NO_ERROR;
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800519
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700520 if (font->map != nullptr) {
521 glGenTextures(1, &font->texture.name);
522 glBindTexture(GL_TEXTURE_2D, font->texture.name);
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800523
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700524 status = initTexture(font->map, &font->texture.w, &font->texture.h);
525
526 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
527 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
528 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
529 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
530 } else if (fallback != nullptr) {
531 status = initTexture(&font->texture, mAssets, fallback);
532 } else {
533 return NO_INIT;
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800534 }
535
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700536 if (status == NO_ERROR) {
537 font->char_width = font->texture.w / FONT_NUM_COLS;
538 font->char_height = font->texture.h / FONT_NUM_ROWS / 2; // There are bold and regular rows
539 }
540
541 return status;
542}
543
544void BootAnimation::drawText(const char* str, const Font& font, bool bold, int* x, int* y) {
545 glEnable(GL_BLEND); // Allow us to draw on top of the animation
546 glBindTexture(GL_TEXTURE_2D, font.texture.name);
547
548 const int len = strlen(str);
549 const int strWidth = font.char_width * len;
550
551 if (*x == TEXT_CENTER_VALUE) {
552 *x = (mWidth - strWidth) / 2;
553 } else if (*x < 0) {
554 *x = mWidth + *x - strWidth;
555 }
556 if (*y == TEXT_CENTER_VALUE) {
557 *y = (mHeight - font.char_height) / 2;
558 } else if (*y < 0) {
559 *y = mHeight + *y - font.char_height;
560 }
561
562 int cropRect[4] = { 0, 0, font.char_width, -font.char_height };
563
564 for (int i = 0; i < len; i++) {
565 char c = str[i];
566
567 if (c < FONT_BEGIN_CHAR || c > FONT_END_CHAR) {
568 c = '?';
569 }
570
571 // Crop the texture to only the pixels in the current glyph
572 const int charPos = (c - FONT_BEGIN_CHAR); // Position in the list of valid characters
573 const int row = charPos / FONT_NUM_COLS;
574 const int col = charPos % FONT_NUM_COLS;
575 cropRect[0] = col * font.char_width; // Left of column
576 cropRect[1] = row * font.char_height * 2; // Top of row
577 // Move down to bottom of regular (one char_heigh) or bold (two char_heigh) line
578 cropRect[1] += bold ? 2 * font.char_height : font.char_height;
579 glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_CROP_RECT_OES, cropRect);
580
581 glDrawTexiOES(*x, *y, 0, font.char_width, font.char_height);
582
583 *x += font.char_width;
584 }
585
586 glDisable(GL_BLEND); // Return to the animation's default behaviour
587 glBindTexture(GL_TEXTURE_2D, 0);
588}
589
590// We render 24 hour time.
591void BootAnimation::drawClock(const Font& font, const int xPos, const int yPos) {
592 static constexpr char TIME_FORMAT[] = "%H:%M";
593 static constexpr int TIME_LENGTH = 6;
594
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800595 time_t rawtime;
596 time(&rawtime);
597 struct tm* timeInfo = localtime(&rawtime);
598
599 char timeBuff[TIME_LENGTH];
600 size_t length = strftime(timeBuff, TIME_LENGTH, TIME_FORMAT, timeInfo);
601
602 if (length != TIME_LENGTH - 1) {
603 ALOGE("Couldn't format time; abandoning boot animation clock");
604 mClockEnabled = false;
605 return;
606 }
607
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700608 int x = xPos;
609 int y = yPos;
610 drawText(timeBuff, font, false, &x, &y);
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800611}
612
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700613bool BootAnimation::parseAnimationDesc(Animation& animation)
Mathias Agopiana8826d62009-10-01 03:10:14 -0700614{
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700615 String8 desString;
616
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700617 if (!readFile(animation.zip, "desc.txt", desString)) {
Narayan Kamathafd31e02013-12-03 13:16:03 +0000618 return false;
619 }
Mathias Agopiana8826d62009-10-01 03:10:14 -0700620 char const* s = desString.string();
621
Mathias Agopiana8826d62009-10-01 03:10:14 -0700622 // Parse the description file
623 for (;;) {
624 const char* endl = strstr(s, "\n");
Andreas Gampecfedceb2014-09-30 21:48:18 -0700625 if (endl == NULL) break;
Mathias Agopiana8826d62009-10-01 03:10:14 -0700626 String8 line(s, endl - s);
627 const char* l = line.string();
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800628 int fps = 0;
629 int width = 0;
630 int height = 0;
631 int count = 0;
632 int pause = 0;
Narayan Kamathafd31e02013-12-03 13:16:03 +0000633 char path[ANIM_ENTRY_NAME_MAX];
Jesse Hall083b84c2014-09-22 10:51:09 -0700634 char color[7] = "000000"; // default to black if unspecified
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700635 char clockPos1[TEXT_POS_LEN_MAX + 1] = "";
636 char clockPos2[TEXT_POS_LEN_MAX + 1] = "";
Jesse Hall083b84c2014-09-22 10:51:09 -0700637
Kevin Hesterd3782b22012-04-26 10:38:55 -0700638 char pathType;
Mathias Agopiana8826d62009-10-01 03:10:14 -0700639 if (sscanf(l, "%d %d %d", &width, &height, &fps) == 3) {
Jesse Hall083b84c2014-09-22 10:51:09 -0700640 // ALOGD("> w=%d, h=%d, fps=%d", width, height, fps);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700641 animation.width = width;
642 animation.height = height;
643 animation.fps = fps;
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700644 } else if (sscanf(l, " %c %d %d %s #%6s %16s %16s",
645 &pathType, &count, &pause, path, color, clockPos1, clockPos2) >= 4) {
646 //ALOGD("> type=%c, count=%d, pause=%d, path=%s, color=%s, clockPos1=%s, clockPos2=%s",
647 // pathType, count, pause, path, color, clockPos1, clockPos2);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700648 Animation::Part part;
Kevin Hesterd3782b22012-04-26 10:38:55 -0700649 part.playUntilComplete = pathType == 'c';
Mathias Agopiana8826d62009-10-01 03:10:14 -0700650 part.count = count;
651 part.pause = pause;
652 part.path = path;
Geoffrey Pitschd6d9a1d2016-06-08 00:38:58 -0700653 part.audioData = NULL;
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700654 part.animation = NULL;
Jesse Hall083b84c2014-09-22 10:51:09 -0700655 if (!parseColor(color, part.backgroundColor)) {
656 ALOGE("> invalid color '#%s'", color);
657 part.backgroundColor[0] = 0.0f;
658 part.backgroundColor[1] = 0.0f;
659 part.backgroundColor[2] = 0.0f;
660 }
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700661 parsePosition(clockPos1, clockPos2, &part.clockPosX, &part.clockPosY);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700662 animation.parts.add(part);
663 }
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700664 else if (strcmp(l, "$SYSTEM") == 0) {
665 // ALOGD("> SYSTEM");
666 Animation::Part part;
667 part.playUntilComplete = false;
668 part.count = 1;
669 part.pause = 0;
Geoffrey Pitschd6d9a1d2016-06-08 00:38:58 -0700670 part.audioData = NULL;
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700671 part.animation = loadAnimation(String8(SYSTEM_BOOTANIMATION_FILE));
672 if (part.animation != NULL)
673 animation.parts.add(part);
674 }
Mathias Agopiana8826d62009-10-01 03:10:14 -0700675 s = ++endl;
676 }
677
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700678 return true;
679}
680
681bool BootAnimation::preloadZip(Animation& animation)
682{
Mathias Agopiana8826d62009-10-01 03:10:14 -0700683 // read all the data structures
684 const size_t pcount = animation.parts.size();
Narayan Kamathafd31e02013-12-03 13:16:03 +0000685 void *cookie = NULL;
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400686 ZipFileRO* zip = animation.zip;
687 if (!zip->startIteration(&cookie)) {
Narayan Kamathafd31e02013-12-03 13:16:03 +0000688 return false;
689 }
690
Geoffrey Pitscha91a2d72016-07-12 14:46:19 -0400691 Animation::Part* partWithAudio = NULL;
Narayan Kamathafd31e02013-12-03 13:16:03 +0000692 ZipEntryRO entry;
693 char name[ANIM_ENTRY_NAME_MAX];
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400694 while ((entry = zip->nextEntry(cookie)) != NULL) {
695 const int foundEntryName = zip->getEntryFileName(entry, name, ANIM_ENTRY_NAME_MAX);
Narayan Kamathafd31e02013-12-03 13:16:03 +0000696 if (foundEntryName > ANIM_ENTRY_NAME_MAX || foundEntryName == -1) {
697 ALOGE("Error fetching entry file name");
698 continue;
699 }
700
701 const String8 entryName(name);
702 const String8 path(entryName.getPathDir());
703 const String8 leaf(entryName.getPathLeaf());
704 if (leaf.size() > 0) {
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -0700705 if (entryName == CLOCK_FONT_ZIP_NAME) {
706 FileMap* map = zip->createEntryFileMap(entry);
707 if (map) {
708 animation.clockFont.map = map;
709 }
710 continue;
711 }
712
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400713 for (size_t j = 0; j < pcount; j++) {
Narayan Kamathafd31e02013-12-03 13:16:03 +0000714 if (path == animation.parts[j].path) {
Narayan Kamath4600dd02015-06-16 12:02:57 +0100715 uint16_t method;
Narayan Kamathafd31e02013-12-03 13:16:03 +0000716 // supports only stored png files
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400717 if (zip->getEntryInfo(entry, &method, NULL, NULL, NULL, NULL, NULL)) {
Narayan Kamathafd31e02013-12-03 13:16:03 +0000718 if (method == ZipFileRO::kCompressStored) {
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400719 FileMap* map = zip->createEntryFileMap(entry);
Narayan Kamathafd31e02013-12-03 13:16:03 +0000720 if (map) {
Narayan Kamathafd31e02013-12-03 13:16:03 +0000721 Animation::Part& part(animation.parts.editItemAt(j));
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700722 if (leaf == "audio.wav") {
723 // a part may have at most one audio file
Geoffrey Pitschd6d9a1d2016-06-08 00:38:58 -0700724 part.audioData = (uint8_t *)map->getDataPtr();
725 part.audioLength = map->getDataLength();
Geoffrey Pitscha91a2d72016-07-12 14:46:19 -0400726 partWithAudio = &part;
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400727 } else if (leaf == "trim.txt") {
728 part.trimData.setTo((char const*)map->getDataPtr(),
729 map->getDataLength());
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700730 } else {
731 Animation::Frame frame;
732 frame.name = leaf;
733 frame.map = map;
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400734 frame.trimWidth = animation.width;
735 frame.trimHeight = animation.height;
736 frame.trimX = 0;
737 frame.trimY = 0;
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700738 part.frames.add(frame);
739 }
Mathias Agopiana8826d62009-10-01 03:10:14 -0700740 }
Geoffrey Pitschd6d9a1d2016-06-08 00:38:58 -0700741 } else {
742 ALOGE("bootanimation.zip is compressed; must be only stored");
Mathias Agopiana8826d62009-10-01 03:10:14 -0700743 }
744 }
745 }
746 }
747 }
748 }
749
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400750 // If there is trimData present, override the positioning defaults.
751 for (Animation::Part& part : animation.parts) {
752 const char* trimDataStr = part.trimData.string();
753 for (size_t frameIdx = 0; frameIdx < part.frames.size(); frameIdx++) {
754 const char* endl = strstr(trimDataStr, "\n");
755 // No more trimData for this part.
756 if (endl == NULL) {
757 break;
758 }
759 String8 line(trimDataStr, endl - trimDataStr);
760 const char* lineStr = line.string();
761 trimDataStr = ++endl;
762 int width = 0, height = 0, x = 0, y = 0;
763 if (sscanf(lineStr, "%dx%d+%d+%d", &width, &height, &x, &y) == 4) {
764 Animation::Frame& frame(part.frames.editItemAt(frameIdx));
765 frame.trimWidth = width;
766 frame.trimHeight = height;
767 frame.trimX = x;
768 frame.trimY = y;
769 } else {
770 ALOGE("Error parsing trim.txt, line: %s", lineStr);
771 break;
772 }
773 }
774 }
775
Geoffrey Pitschd6d9a1d2016-06-08 00:38:58 -0700776 // Create and initialize audioplay if there is a wav file in any of the animations.
Geoffrey Pitscha91a2d72016-07-12 14:46:19 -0400777 if (partWithAudio != NULL) {
Geoffrey Pitschd6d9a1d2016-06-08 00:38:58 -0700778 ALOGD("found audio.wav, creating playback engine");
Geoffrey Pitscha91a2d72016-07-12 14:46:19 -0400779 if (!audioplay::create(partWithAudio->audioData, partWithAudio->audioLength)) {
780 return false;
781 }
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
849 if (mTimeCheckThread != NULL) {
850 mTimeCheckThread->requestExit();
851 mTimeCheckThread = NULL;
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
Narayan Kamathafd31e02013-12-03 13:16:03 +0000870 for (size_t i=0 ; i<pcount ; i++) {
Mathias Agopiana8826d62009-10-01 03:10:14 -0700871 const Animation::Part& part(animation.parts[i]);
872 const size_t fcount = part.frames.size();
873 glBindTexture(GL_TEXTURE_2D, 0);
874
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700875 // Handle animation package
876 if (part.animation != NULL) {
877 playAnimation(*part.animation);
878 if (exitPending())
879 break;
880 continue; //to next part
881 }
882
Mathias Agopiana8826d62009-10-01 03:10:14 -0700883 for (int r=0 ; !part.count || r<part.count ; r++) {
Kevin Hesterd3782b22012-04-26 10:38:55 -0700884 // Exit any non playuntil complete parts immediately
885 if(exitPending() && !part.playUntilComplete)
886 break;
887
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700888 // only play audio file the first time we animate the part
Geoffrey Pitsch290c4352016-08-09 14:35:10 -0400889 if (r == 0 && part.audioData && playSoundsAllowed()) {
890 ALOGD("playing clip for part%d, size=%d", (int) i, part.audioLength);
891 audioplay::playClip(part.audioData, part.audioLength);
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
980 // we've finally played everything we're going to play
981 audioplay::setPlaying(false);
982 audioplay::destroy();
983
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700984 return true;
Mathias Agopiana8826d62009-10-01 03:10:14 -0700985}
986
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700987void BootAnimation::releaseAnimation(Animation* animation) const
988{
989 for (Vector<Animation::Part>::iterator it = animation->parts.begin(),
990 e = animation->parts.end(); it != e; ++it) {
991 if (it->animation)
992 releaseAnimation(it->animation);
993 }
994 if (animation->zip)
995 delete animation->zip;
996 delete animation;
997}
998
999BootAnimation::Animation* BootAnimation::loadAnimation(const String8& fn)
1000{
1001 if (mLoadedFiles.indexOf(fn) >= 0) {
1002 ALOGE("File \"%s\" is already loaded. Cyclic ref is not allowed",
1003 fn.string());
1004 return NULL;
1005 }
1006 ZipFileRO *zip = ZipFileRO::open(fn);
1007 if (zip == NULL) {
1008 ALOGE("Failed to open animation zip \"%s\": %s",
1009 fn.string(), strerror(errno));
1010 return NULL;
1011 }
1012
1013 Animation *animation = new Animation;
1014 animation->fileName = fn;
1015 animation->zip = zip;
Damien Bargiacchi0e3d2ab2016-08-29 04:11:19 -07001016 animation->clockFont.map = nullptr;
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001017 mLoadedFiles.add(animation->fileName);
1018
1019 parseAnimationDesc(*animation);
Geoffrey Pitscha91a2d72016-07-12 14:46:19 -04001020 if (!preloadZip(*animation)) {
1021 return NULL;
1022 }
1023
Andriy Naborskyy39218ba2015-08-16 21:32:50 -07001024
1025 mLoadedFiles.remove(fn);
1026 return animation;
1027}
Damien Bargiacchi97480862016-03-29 14:55:55 -07001028
Geoffrey Pitsch290c4352016-08-09 14:35:10 -04001029bool BootAnimation::playSoundsAllowed() const {
1030 // Only play sounds for system boots, not runtime restarts.
1031 if (!mSystemBoot) {
1032 return false;
1033 }
1034
1035 // Read the system property to see if we should play the sound.
1036 // If it's not present, default to allowed.
1037 if (!property_get_bool(PLAY_SOUND_PROP_NAME, 1)) {
1038 return false;
1039 }
1040
1041 // Don't play sounds if this is a reboot due to an error.
1042 char bootreason[PROPERTY_VALUE_MAX];
1043 if (property_get(BOOTREASON_PROP_NAME, bootreason, nullptr) > 0) {
1044 for (const auto& str : PLAY_SOUND_BOOTREASON_BLACKLIST) {
1045 if (strcasecmp(str.c_str(), bootreason) == 0) {
1046 return false;
1047 }
1048 }
1049 }
1050 return true;
1051}
1052
Damien Bargiacchi97480862016-03-29 14:55:55 -07001053bool BootAnimation::updateIsTimeAccurate() {
1054 static constexpr long long MAX_TIME_IN_PAST = 60000LL * 60LL * 24LL * 30LL; // 30 days
1055 static constexpr long long MAX_TIME_IN_FUTURE = 60000LL * 90LL; // 90 minutes
1056
1057 if (mTimeIsAccurate) {
1058 return true;
1059 }
1060
1061 struct stat statResult;
1062 if(stat(ACCURATE_TIME_FLAG_FILE_PATH, &statResult) == 0) {
1063 mTimeIsAccurate = true;
1064 return true;
1065 }
1066
1067 FILE* file = fopen(LAST_TIME_CHANGED_FILE_PATH, "r");
1068 if (file != NULL) {
1069 long long lastChangedTime = 0;
1070 fscanf(file, "%lld", &lastChangedTime);
1071 fclose(file);
1072 if (lastChangedTime > 0) {
1073 struct timespec now;
1074 clock_gettime(CLOCK_REALTIME, &now);
1075 // Match the Java timestamp format
1076 long long rtcNow = (now.tv_sec * 1000LL) + (now.tv_nsec / 1000000LL);
Damien Bargiacchi96762812016-07-12 15:53:40 -07001077 if (ACCURATE_TIME_EPOCH < rtcNow
1078 && lastChangedTime > (rtcNow - MAX_TIME_IN_PAST)
1079 && lastChangedTime < (rtcNow + MAX_TIME_IN_FUTURE)) {
Damien Bargiacchi97480862016-03-29 14:55:55 -07001080 mTimeIsAccurate = true;
1081 }
1082 }
1083 }
1084
1085 return mTimeIsAccurate;
1086}
1087
1088BootAnimation::TimeCheckThread::TimeCheckThread(BootAnimation* bootAnimation) : Thread(false),
1089 mInotifyFd(-1), mSystemWd(-1), mTimeWd(-1), mBootAnimation(bootAnimation) {}
1090
1091BootAnimation::TimeCheckThread::~TimeCheckThread() {
1092 // mInotifyFd may be -1 but that's ok since we're not at risk of attempting to close a valid FD.
1093 close(mInotifyFd);
1094}
1095
1096bool BootAnimation::TimeCheckThread::threadLoop() {
1097 bool shouldLoop = doThreadLoop() && !mBootAnimation->mTimeIsAccurate
1098 && mBootAnimation->mClockEnabled;
1099 if (!shouldLoop) {
1100 close(mInotifyFd);
1101 mInotifyFd = -1;
1102 }
1103 return shouldLoop;
1104}
1105
1106bool BootAnimation::TimeCheckThread::doThreadLoop() {
1107 static constexpr int BUFF_LEN (10 * (sizeof(struct inotify_event) + NAME_MAX + 1));
1108
1109 // Poll instead of doing a blocking read so the Thread can exit if requested.
1110 struct pollfd pfd = { mInotifyFd, POLLIN, 0 };
1111 ssize_t pollResult = poll(&pfd, 1, 1000);
1112
1113 if (pollResult == 0) {
1114 return true;
1115 } else if (pollResult < 0) {
1116 ALOGE("Could not poll inotify events");
1117 return false;
1118 }
1119
1120 char buff[BUFF_LEN] __attribute__ ((aligned(__alignof__(struct inotify_event))));;
1121 ssize_t length = read(mInotifyFd, buff, BUFF_LEN);
1122 if (length == 0) {
1123 return true;
1124 } else if (length < 0) {
1125 ALOGE("Could not read inotify events");
1126 return false;
1127 }
1128
1129 const struct inotify_event *event;
1130 for (char* ptr = buff; ptr < buff + length; ptr += sizeof(struct inotify_event) + event->len) {
1131 event = (const struct inotify_event *) ptr;
1132 if (event->wd == mSystemWd && strcmp(SYSTEM_TIME_DIR_NAME, event->name) == 0) {
1133 addTimeDirWatch();
1134 } else if (event->wd == mTimeWd && (strcmp(LAST_TIME_CHANGED_FILE_NAME, event->name) == 0
1135 || strcmp(ACCURATE_TIME_FLAG_FILE_NAME, event->name) == 0)) {
1136 return !mBootAnimation->updateIsTimeAccurate();
1137 }
1138 }
1139
1140 return true;
1141}
1142
1143void BootAnimation::TimeCheckThread::addTimeDirWatch() {
1144 mTimeWd = inotify_add_watch(mInotifyFd, SYSTEM_TIME_DIR_PATH,
1145 IN_CLOSE_WRITE | IN_MOVED_TO | IN_ATTRIB);
1146 if (mTimeWd > 0) {
1147 // No need to watch for the time directory to be created if it already exists
1148 inotify_rm_watch(mInotifyFd, mSystemWd);
1149 mSystemWd = -1;
1150 }
1151}
1152
1153status_t BootAnimation::TimeCheckThread::readyToRun() {
1154 mInotifyFd = inotify_init();
1155 if (mInotifyFd < 0) {
1156 ALOGE("Could not initialize inotify fd");
1157 return NO_INIT;
1158 }
1159
1160 mSystemWd = inotify_add_watch(mInotifyFd, SYSTEM_DATA_DIR_PATH, IN_CREATE | IN_ATTRIB);
1161 if (mSystemWd < 0) {
1162 close(mInotifyFd);
1163 mInotifyFd = -1;
1164 ALOGE("Could not add watch for %s", SYSTEM_DATA_DIR_PATH);
1165 return NO_INIT;
1166 }
1167
1168 addTimeDirWatch();
1169
1170 if (mBootAnimation->updateIsTimeAccurate()) {
1171 close(mInotifyFd);
1172 mInotifyFd = -1;
1173 return ALREADY_EXISTS;
1174 }
1175
1176 return NO_ERROR;
1177}
1178
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001179// ---------------------------------------------------------------------------
1180
1181}
1182; // namespace android