blob: beb550cef47632e823cab5c0845e25585f450773 [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";
71static const char LAST_TIME_CHANGED_FILE_NAME[] = "last_time_change";
72static const char LAST_TIME_CHANGED_FILE_PATH[] = "/data/system/time/last_time_change";
73static const char ACCURATE_TIME_FLAG_FILE_NAME[] = "time_is_accurate";
74static const char ACCURATE_TIME_FLAG_FILE_PATH[] = "/data/system/time/time_is_accurate";
Damien Bargiacchi96762812016-07-12 15:53:40 -070075// Java timestamp format. Don't show the clock if the date is before 2000-01-01 00:00:00.
76static const long long ACCURATE_TIME_EPOCH = 946684800000;
Damien Bargiacchi97480862016-03-29 14:55:55 -070077static const char EXIT_PROP_NAME[] = "service.bootanim.exit";
Geoffrey Pitsch30508792016-07-22 17:04:21 -040078static const char PLAY_SOUND_PROP_NAME[] = "persist.sys.bootanim.play_sound";
Narayan Kamathafd31e02013-12-03 13:16:03 +000079static const int ANIM_ENTRY_NAME_MAX = 256;
Geoffrey Pitsch290c4352016-08-09 14:35:10 -040080static const char BOOT_COMPLETED_PROP_NAME[] = "sys.boot_completed";
81static const char BOOTREASON_PROP_NAME[] = "ro.boot.bootreason";
82// bootreasons list in "system/core/bootstat/bootstat.cpp".
83static const std::vector<std::string> PLAY_SOUND_BOOTREASON_BLACKLIST {
84 "kernel_panic",
85 "Panic",
86 "Watchdog",
87};
Narayan Kamathafd31e02013-12-03 13:16:03 +000088
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080089// ---------------------------------------------------------------------------
90
Damien Bargiacchi97480862016-03-29 14:55:55 -070091BootAnimation::BootAnimation() : Thread(false), mClockEnabled(true), mTimeIsAccurate(false),
92 mTimeCheckThread(NULL) {
Mathias Agopian627e7b52009-05-21 19:21:59 -070093 mSession = new SurfaceComposerClient();
Geoffrey Pitsch290c4352016-08-09 14:35:10 -040094
95 // If the system has already booted, the animation is not being used for a boot.
96 mSystemBoot = !property_get_bool(BOOT_COMPLETED_PROP_NAME, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080097}
98
Damien Bargiacchi97480862016-03-29 14:55:55 -070099BootAnimation::~BootAnimation() {}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800100
101void BootAnimation::onFirstRef() {
Mathias Agopianbc726112009-09-23 15:44:05 -0700102 status_t err = mSession->linkToComposerDeath(this);
Steve Block3762c312012-01-06 19:20:56 +0000103 ALOGE_IF(err, "linkToComposerDeath failed (%s) ", strerror(-err));
Mathias Agopian8434c532009-09-23 18:52:49 -0700104 if (err == NO_ERROR) {
Mathias Agopianbc726112009-09-23 15:44:05 -0700105 run("BootAnimation", PRIORITY_DISPLAY);
106 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800107}
108
Mathias Agopianbc726112009-09-23 15:44:05 -0700109sp<SurfaceComposerClient> BootAnimation::session() const {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800110 return mSession;
111}
112
Mathias Agopianbc726112009-09-23 15:44:05 -0700113
Narayan Kamathafd31e02013-12-03 13:16:03 +0000114void BootAnimation::binderDied(const wp<IBinder>&)
Mathias Agopianbc726112009-09-23 15:44:05 -0700115{
116 // woah, surfaceflinger died!
Steve Block5baa3a62011-12-20 16:23:08 +0000117 ALOGD("SurfaceFlinger died, exiting...");
Mathias Agopianbc726112009-09-23 15:44:05 -0700118
119 // calling requestExit() is not enough here because the Surface code
120 // might be blocked on a condition variable that will never be updated.
121 kill( getpid(), SIGKILL );
122 requestExit();
Geoffrey Pitschd6d9a1d2016-06-08 00:38:58 -0700123 audioplay::destroy();
Mathias Agopianbc726112009-09-23 15:44:05 -0700124}
125
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800126status_t BootAnimation::initTexture(Texture* texture, AssetManager& assets,
127 const char* name) {
128 Asset* asset = assets.open(name, Asset::ACCESS_BUFFER);
Andreas Gampecfedceb2014-09-30 21:48:18 -0700129 if (asset == NULL)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800130 return NO_INIT;
131 SkBitmap bitmap;
Matt Sarett8898c162016-03-24 16:11:01 -0400132 sk_sp<SkData> data = SkData::MakeWithoutCopy(asset->getBuffer(false),
133 asset->getLength());
134 sk_sp<SkImage> image = SkImage::MakeFromEncoded(data);
135 image->asLegacyBitmap(&bitmap, SkImage::kRO_LegacyBitmapMode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800136 asset->close();
137 delete asset;
138
139 // ensure we can call getPixels(). No need to call unlock, since the
140 // bitmap will go out of scope when we return from this method.
141 bitmap.lockPixels();
142
143 const int w = bitmap.width();
144 const int h = bitmap.height();
145 const void* p = bitmap.getPixels();
146
147 GLint crop[4] = { 0, h, w, -h };
148 texture->w = w;
149 texture->h = h;
150
151 glGenTextures(1, &texture->name);
152 glBindTexture(GL_TEXTURE_2D, texture->name);
153
Mike Reed42a1d082014-07-07 18:06:18 -0400154 switch (bitmap.colorType()) {
155 case kAlpha_8_SkColorType:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800156 glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, w, h, 0, GL_ALPHA,
157 GL_UNSIGNED_BYTE, p);
158 break;
Mike Reed42a1d082014-07-07 18:06:18 -0400159 case kARGB_4444_SkColorType:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800160 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA,
161 GL_UNSIGNED_SHORT_4_4_4_4, p);
162 break;
Mike Reed42a1d082014-07-07 18:06:18 -0400163 case kN32_SkColorType:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800164 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA,
165 GL_UNSIGNED_BYTE, p);
166 break;
Mike Reed42a1d082014-07-07 18:06:18 -0400167 case kRGB_565_SkColorType:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800168 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, w, h, 0, GL_RGB,
169 GL_UNSIGNED_SHORT_5_6_5, p);
170 break;
171 default:
172 break;
173 }
174
175 glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_CROP_RECT_OES, crop);
176 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
177 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
178 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
179 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
180 return NO_ERROR;
181}
182
Mykola Kondratenko0c1eeb32014-04-15 09:35:44 +0200183status_t BootAnimation::initTexture(const Animation::Frame& frame)
Mathias Agopiana8826d62009-10-01 03:10:14 -0700184{
185 //StopWatch watch("blah");
186
187 SkBitmap bitmap;
Matt Sarett8898c162016-03-24 16:11:01 -0400188 sk_sp<SkData> data = SkData::MakeWithoutCopy(frame.map->getDataPtr(),
189 frame.map->getDataLength());
190 sk_sp<SkImage> image = SkImage::MakeFromEncoded(data);
191 image->asLegacyBitmap(&bitmap, SkImage::kRO_LegacyBitmapMode);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700192
Mykola Kondratenko0c1eeb32014-04-15 09:35:44 +0200193 // FileMap memory is never released until application exit.
194 // Release it now as the texture is already loaded and the memory used for
195 // the packed resource can be released.
Narayan Kamath688ff4c2015-02-23 15:47:54 +0000196 delete frame.map;
Mykola Kondratenko0c1eeb32014-04-15 09:35:44 +0200197
Mathias Agopiana8826d62009-10-01 03:10:14 -0700198 // ensure we can call getPixels(). No need to call unlock, since the
199 // bitmap will go out of scope when we return from this method.
200 bitmap.lockPixels();
201
202 const int w = bitmap.width();
203 const int h = bitmap.height();
204 const void* p = bitmap.getPixels();
205
206 GLint crop[4] = { 0, h, w, -h };
207 int tw = 1 << (31 - __builtin_clz(w));
208 int th = 1 << (31 - __builtin_clz(h));
209 if (tw < w) tw <<= 1;
210 if (th < h) th <<= 1;
211
Mike Reed42a1d082014-07-07 18:06:18 -0400212 switch (bitmap.colorType()) {
213 case kN32_SkColorType:
Sai Kiran Korwar27167492015-07-07 20:00:06 +0530214 if (!mUseNpotTextures && (tw != w || th != h)) {
Mathias Agopiana8826d62009-10-01 03:10:14 -0700215 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, tw, th, 0, GL_RGBA,
216 GL_UNSIGNED_BYTE, 0);
217 glTexSubImage2D(GL_TEXTURE_2D, 0,
218 0, 0, w, h, GL_RGBA, GL_UNSIGNED_BYTE, p);
219 } else {
Sai Kiran Korwar27167492015-07-07 20:00:06 +0530220 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA,
Mathias Agopiana8826d62009-10-01 03:10:14 -0700221 GL_UNSIGNED_BYTE, p);
222 }
223 break;
224
Mike Reed42a1d082014-07-07 18:06:18 -0400225 case kRGB_565_SkColorType:
Sai Kiran Korwar27167492015-07-07 20:00:06 +0530226 if (!mUseNpotTextures && (tw != w || th != h)) {
Mathias Agopiana8826d62009-10-01 03:10:14 -0700227 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, tw, th, 0, GL_RGB,
228 GL_UNSIGNED_SHORT_5_6_5, 0);
229 glTexSubImage2D(GL_TEXTURE_2D, 0,
230 0, 0, w, h, GL_RGB, GL_UNSIGNED_SHORT_5_6_5, p);
231 } else {
Sai Kiran Korwar27167492015-07-07 20:00:06 +0530232 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, w, h, 0, GL_RGB,
Mathias Agopiana8826d62009-10-01 03:10:14 -0700233 GL_UNSIGNED_SHORT_5_6_5, p);
234 }
235 break;
236 default:
237 break;
238 }
239
240 glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_CROP_RECT_OES, crop);
241
242 return NO_ERROR;
243}
244
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800245status_t BootAnimation::readyToRun() {
246 mAssets.addDefaultAssets();
247
Jeff Brown0b722fe2012-08-24 22:40:14 -0700248 sp<IBinder> dtoken(SurfaceComposerClient::getBuiltInDisplay(
249 ISurfaceComposer::eDisplayIdMain));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800250 DisplayInfo dinfo;
Jeff Brown0b722fe2012-08-24 22:40:14 -0700251 status_t status = SurfaceComposerClient::getDisplayInfo(dtoken, &dinfo);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800252 if (status)
253 return -1;
254
255 // create the native surface
Jeff Brown0b722fe2012-08-24 22:40:14 -0700256 sp<SurfaceControl> control = session()->createSurface(String8("BootAnimation"),
257 dinfo.w, dinfo.h, PIXEL_FORMAT_RGB_565);
Mathias Agopian439863f2011-06-28 19:09:31 -0700258
259 SurfaceComposerClient::openGlobalTransaction();
Mathias Agopian17f638b2009-04-16 20:04:08 -0700260 control->setLayer(0x40000000);
Mathias Agopian439863f2011-06-28 19:09:31 -0700261 SurfaceComposerClient::closeGlobalTransaction();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800262
Mathias Agopian17f638b2009-04-16 20:04:08 -0700263 sp<Surface> s = control->getSurface();
264
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800265 // initialize opengl and egl
Mathias Agopian738b9a42009-08-06 16:41:02 -0700266 const EGLint attribs[] = {
Mathias Agopian1b253b72011-08-15 15:20:22 -0700267 EGL_RED_SIZE, 8,
268 EGL_GREEN_SIZE, 8,
269 EGL_BLUE_SIZE, 8,
Mathias Agopiana8826d62009-10-01 03:10:14 -0700270 EGL_DEPTH_SIZE, 0,
271 EGL_NONE
Mathias Agopian738b9a42009-08-06 16:41:02 -0700272 };
Andreas Gampecfedceb2014-09-30 21:48:18 -0700273 EGLint w, h;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800274 EGLint numConfigs;
275 EGLConfig config;
276 EGLSurface surface;
277 EGLContext context;
Mathias Agopian627e7b52009-05-21 19:21:59 -0700278
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800279 EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
Mathias Agopian627e7b52009-05-21 19:21:59 -0700280
281 eglInitialize(display, 0, 0);
Mathias Agopian1b253b72011-08-15 15:20:22 -0700282 eglChooseConfig(display, attribs, &config, 1, &numConfigs);
Mathias Agopian1473f462009-04-10 14:24:30 -0700283 surface = eglCreateWindowSurface(display, config, s.get(), NULL);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800284 context = eglCreateContext(display, config, NULL, NULL);
285 eglQuerySurface(display, surface, EGL_WIDTH, &w);
286 eglQuerySurface(display, surface, EGL_HEIGHT, &h);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700287
Mathias Agopianabac0102009-07-31 14:47:00 -0700288 if (eglMakeCurrent(display, surface, surface, context) == EGL_FALSE)
289 return NO_INIT;
Mathias Agopiana8826d62009-10-01 03:10:14 -0700290
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800291 mDisplay = display;
292 mContext = context;
293 mSurface = surface;
294 mWidth = w;
295 mHeight = h;
Mathias Agopian17f638b2009-04-16 20:04:08 -0700296 mFlingerSurfaceControl = control;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800297 mFlingerSurface = s;
298
Elliott Hughesc367d482013-10-29 13:12:55 -0700299 // If the device has encryption turned on or is in process
Jason parksbd9a08d2011-01-31 15:04:34 -0600300 // of being encrypted we show the encrypted boot animation.
301 char decrypt[PROPERTY_VALUE_MAX];
302 property_get("vold.decrypt", decrypt, "");
303
304 bool encryptedAnimation = atoi(decrypt) != 0 || !strcmp("trigger_restart_min_framework", decrypt);
305
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700306 if (encryptedAnimation && (access(SYSTEM_ENCRYPTED_BOOTANIMATION_FILE, R_OK) == 0)) {
307 mZipFileName = SYSTEM_ENCRYPTED_BOOTANIMATION_FILE;
Jason parksbd9a08d2011-01-31 15:04:34 -0600308 }
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700309 else if (access(OEM_BOOTANIMATION_FILE, R_OK) == 0) {
310 mZipFileName = OEM_BOOTANIMATION_FILE;
311 }
312 else if (access(SYSTEM_BOOTANIMATION_FILE, R_OK) == 0) {
313 mZipFileName = SYSTEM_BOOTANIMATION_FILE;
314 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800315 return NO_ERROR;
316}
317
Mathias Agopiana8826d62009-10-01 03:10:14 -0700318bool BootAnimation::threadLoop()
319{
320 bool r;
Narayan Kamathafd31e02013-12-03 13:16:03 +0000321 // We have no bootanimation file, so we use the stock android logo
322 // animation.
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700323 if (mZipFileName.isEmpty()) {
Mathias Agopiana8826d62009-10-01 03:10:14 -0700324 r = android();
325 } else {
326 r = movie();
327 }
328
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800329 eglMakeCurrent(mDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
330 eglDestroyContext(mDisplay, mContext);
331 eglDestroySurface(mDisplay, mSurface);
Mathias Agopian6cf0db22009-04-17 19:36:26 -0700332 mFlingerSurface.clear();
Mathias Agopian17f638b2009-04-16 20:04:08 -0700333 mFlingerSurfaceControl.clear();
Mathias Agopian627e7b52009-05-21 19:21:59 -0700334 eglTerminate(mDisplay);
Sai Kiran Korwar3eee9fb2015-06-16 17:13:35 +0530335 eglReleaseThread();
Mathias Agopian627e7b52009-05-21 19:21:59 -0700336 IPCThreadState::self()->stopProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800337 return r;
338}
339
Mathias Agopiana8826d62009-10-01 03:10:14 -0700340bool BootAnimation::android()
341{
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700342 initTexture(&mAndroid[0], mAssets, "images/android-logo-mask.png");
343 initTexture(&mAndroid[1], mAssets, "images/android-logo-shine.png");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800344
345 // clear screen
Mathias Agopiana8826d62009-10-01 03:10:14 -0700346 glShadeModel(GL_FLAT);
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700347 glDisable(GL_DITHER);
348 glDisable(GL_SCISSOR_TEST);
Mathias Agopian59f19e42011-05-06 19:22:12 -0700349 glClearColor(0,0,0,1);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800350 glClear(GL_COLOR_BUFFER_BIT);
351 eglSwapBuffers(mDisplay, mSurface);
352
Mathias Agopiana8826d62009-10-01 03:10:14 -0700353 glEnable(GL_TEXTURE_2D);
354 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
355
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700356 const GLint xc = (mWidth - mAndroid[0].w) / 2;
357 const GLint yc = (mHeight - mAndroid[0].h) / 2;
358 const Rect updateRect(xc, yc, xc + mAndroid[0].w, yc + mAndroid[0].h);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800359
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800360 glScissor(updateRect.left, mHeight - updateRect.bottom, updateRect.width(),
361 updateRect.height());
362
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700363 // Blend state
364 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
365 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
366
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800367 const nsecs_t startTime = systemTime();
368 do {
Mathias Agopian13796652009-03-24 22:49:21 -0700369 nsecs_t now = systemTime();
370 double time = now - startTime;
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700371 float t = 4.0f * float(time / us2ns(16667)) / mAndroid[1].w;
372 GLint offset = (1 - (t - floorf(t))) * mAndroid[1].w;
373 GLint x = xc - offset;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800374
Mathias Agopian81668642009-07-28 11:41:30 -0700375 glDisable(GL_SCISSOR_TEST);
376 glClear(GL_COLOR_BUFFER_BIT);
377
378 glEnable(GL_SCISSOR_TEST);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800379 glDisable(GL_BLEND);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800380 glBindTexture(GL_TEXTURE_2D, mAndroid[1].name);
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700381 glDrawTexiOES(x, yc, 0, mAndroid[1].w, mAndroid[1].h);
382 glDrawTexiOES(x + mAndroid[1].w, yc, 0, mAndroid[1].w, mAndroid[1].h);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800383
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700384 glEnable(GL_BLEND);
385 glBindTexture(GL_TEXTURE_2D, mAndroid[0].name);
386 glDrawTexiOES(xc, yc, 0, mAndroid[0].w, mAndroid[0].h);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800387
Mathias Agopian627e7b52009-05-21 19:21:59 -0700388 EGLBoolean res = eglSwapBuffers(mDisplay, mSurface);
389 if (res == EGL_FALSE)
390 break;
391
Mathias Agopian13796652009-03-24 22:49:21 -0700392 // 12fps: don't animate too fast to preserve CPU
393 const nsecs_t sleepTime = 83333 - ns2us(systemTime() - now);
394 if (sleepTime > 0)
Mathias Agopiana8826d62009-10-01 03:10:14 -0700395 usleep(sleepTime);
Kevin Hesterd3782b22012-04-26 10:38:55 -0700396
397 checkExit();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800398 } while (!exitPending());
399
400 glDeleteTextures(1, &mAndroid[0].name);
401 glDeleteTextures(1, &mAndroid[1].name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800402 return false;
403}
404
Mathias Agopiana8826d62009-10-01 03:10:14 -0700405
Kevin Hesterd3782b22012-04-26 10:38:55 -0700406void BootAnimation::checkExit() {
407 // Allow surface flinger to gracefully request shutdown
408 char value[PROPERTY_VALUE_MAX];
409 property_get(EXIT_PROP_NAME, value, "0");
410 int exitnow = atoi(value);
411 if (exitnow) {
412 requestExit();
413 }
414}
415
Jesse Hall083b84c2014-09-22 10:51:09 -0700416// Parse a color represented as an HTML-style 'RRGGBB' string: each pair of
417// characters in str is a hex number in [0, 255], which are converted to
418// floating point values in the range [0.0, 1.0] and placed in the
419// corresponding elements of color.
420//
421// If the input string isn't valid, parseColor returns false and color is
422// left unchanged.
423static bool parseColor(const char str[7], float color[3]) {
424 float tmpColor[3];
425 for (int i = 0; i < 3; i++) {
426 int val = 0;
427 for (int j = 0; j < 2; j++) {
428 val *= 16;
429 char c = str[2*i + j];
430 if (c >= '0' && c <= '9') val += c - '0';
431 else if (c >= 'A' && c <= 'F') val += (c - 'A') + 10;
432 else if (c >= 'a' && c <= 'f') val += (c - 'a') + 10;
433 else return false;
434 }
435 tmpColor[i] = static_cast<float>(val) / 255.0f;
436 }
437 memcpy(color, tmpColor, sizeof(tmpColor));
438 return true;
439}
440
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700441
442static bool readFile(ZipFileRO* zip, const char* name, String8& outString)
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700443{
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700444 ZipEntryRO entry = zip->findEntryByName(name);
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700445 ALOGE_IF(!entry, "couldn't find %s", name);
446 if (!entry) {
447 return false;
448 }
449
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700450 FileMap* entryMap = zip->createEntryFileMap(entry);
451 zip->releaseEntry(entry);
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700452 ALOGE_IF(!entryMap, "entryMap is null");
453 if (!entryMap) {
454 return false;
455 }
456
457 outString.setTo((char const*)entryMap->getDataPtr(), entryMap->getDataLength());
Narayan Kamath688ff4c2015-02-23 15:47:54 +0000458 delete entryMap;
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700459 return true;
460}
461
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800462// The time glyphs are stored in a single image of height 64 pixels. Each digit is 40 pixels wide,
463// and the colon character is half that at 20 pixels. The glyph order is '0123456789:'.
464// We render 24 hour time.
465void BootAnimation::drawTime(const Texture& clockTex, const int yPos) {
466 static constexpr char TIME_FORMAT[] = "%H:%M";
467 static constexpr int TIME_LENGTH = sizeof(TIME_FORMAT);
468
469 static constexpr int DIGIT_HEIGHT = 64;
470 static constexpr int DIGIT_WIDTH = 40;
471 static constexpr int COLON_WIDTH = DIGIT_WIDTH / 2;
472 static constexpr int TIME_WIDTH = (DIGIT_WIDTH * 4) + COLON_WIDTH;
473
474 if (clockTex.h < DIGIT_HEIGHT || clockTex.w < (10 * DIGIT_WIDTH + COLON_WIDTH)) {
475 ALOGE("Clock texture is too small; abandoning boot animation clock");
476 mClockEnabled = false;
477 return;
478 }
479
480 time_t rawtime;
481 time(&rawtime);
482 struct tm* timeInfo = localtime(&rawtime);
483
484 char timeBuff[TIME_LENGTH];
485 size_t length = strftime(timeBuff, TIME_LENGTH, TIME_FORMAT, timeInfo);
486
487 if (length != TIME_LENGTH - 1) {
488 ALOGE("Couldn't format time; abandoning boot animation clock");
489 mClockEnabled = false;
490 return;
491 }
492
493 glEnable(GL_BLEND); // Allow us to draw on top of the animation
494 glBindTexture(GL_TEXTURE_2D, clockTex.name);
495
496 int xPos = (mWidth - TIME_WIDTH) / 2;
497 int cropRect[4] = { 0, DIGIT_HEIGHT, DIGIT_WIDTH, -DIGIT_HEIGHT };
498
499 for (int i = 0; i < TIME_LENGTH - 1; i++) {
500 char c = timeBuff[i];
501 int width = DIGIT_WIDTH;
502 int pos = c - '0'; // Position in the character list
503 if (pos < 0 || pos > 10) {
504 continue;
505 }
506 if (c == ':') {
507 width = COLON_WIDTH;
508 }
509
510 // Crop the texture to only the pixels in the current glyph
511 int left = pos * DIGIT_WIDTH;
512 cropRect[0] = left;
513 cropRect[2] = width;
514 glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_CROP_RECT_OES, cropRect);
515
516 glDrawTexiOES(xPos, yPos, 0, width, DIGIT_HEIGHT);
517
518 xPos += width;
519 }
520
521 glDisable(GL_BLEND); // Return to the animation's default behaviour
522 glBindTexture(GL_TEXTURE_2D, 0);
523}
524
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700525bool BootAnimation::parseAnimationDesc(Animation& animation)
Mathias Agopiana8826d62009-10-01 03:10:14 -0700526{
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700527 String8 desString;
528
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700529 if (!readFile(animation.zip, "desc.txt", desString)) {
Narayan Kamathafd31e02013-12-03 13:16:03 +0000530 return false;
531 }
Mathias Agopiana8826d62009-10-01 03:10:14 -0700532 char const* s = desString.string();
533
Mathias Agopiana8826d62009-10-01 03:10:14 -0700534 // Parse the description file
535 for (;;) {
536 const char* endl = strstr(s, "\n");
Andreas Gampecfedceb2014-09-30 21:48:18 -0700537 if (endl == NULL) break;
Mathias Agopiana8826d62009-10-01 03:10:14 -0700538 String8 line(s, endl - s);
539 const char* l = line.string();
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800540 int fps = 0;
541 int width = 0;
542 int height = 0;
543 int count = 0;
544 int pause = 0;
545 int clockPosY = -1;
Narayan Kamathafd31e02013-12-03 13:16:03 +0000546 char path[ANIM_ENTRY_NAME_MAX];
Jesse Hall083b84c2014-09-22 10:51:09 -0700547 char color[7] = "000000"; // default to black if unspecified
548
Kevin Hesterd3782b22012-04-26 10:38:55 -0700549 char pathType;
Mathias Agopiana8826d62009-10-01 03:10:14 -0700550 if (sscanf(l, "%d %d %d", &width, &height, &fps) == 3) {
Jesse Hall083b84c2014-09-22 10:51:09 -0700551 // ALOGD("> w=%d, h=%d, fps=%d", width, height, fps);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700552 animation.width = width;
553 animation.height = height;
554 animation.fps = fps;
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800555 } else if (sscanf(l, " %c %d %d %s #%6s %d",
556 &pathType, &count, &pause, path, color, &clockPosY) >= 4) {
557 // ALOGD("> type=%c, count=%d, pause=%d, path=%s, color=%s, clockPosY=%d", pathType, count, pause, path, color, clockPosY);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700558 Animation::Part part;
Kevin Hesterd3782b22012-04-26 10:38:55 -0700559 part.playUntilComplete = pathType == 'c';
Mathias Agopiana8826d62009-10-01 03:10:14 -0700560 part.count = count;
561 part.pause = pause;
562 part.path = path;
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800563 part.clockPosY = clockPosY;
Geoffrey Pitschd6d9a1d2016-06-08 00:38:58 -0700564 part.audioData = NULL;
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700565 part.animation = NULL;
Jesse Hall083b84c2014-09-22 10:51:09 -0700566 if (!parseColor(color, part.backgroundColor)) {
567 ALOGE("> invalid color '#%s'", color);
568 part.backgroundColor[0] = 0.0f;
569 part.backgroundColor[1] = 0.0f;
570 part.backgroundColor[2] = 0.0f;
571 }
Mathias Agopiana8826d62009-10-01 03:10:14 -0700572 animation.parts.add(part);
573 }
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700574 else if (strcmp(l, "$SYSTEM") == 0) {
575 // ALOGD("> SYSTEM");
576 Animation::Part part;
577 part.playUntilComplete = false;
578 part.count = 1;
579 part.pause = 0;
Geoffrey Pitschd6d9a1d2016-06-08 00:38:58 -0700580 part.audioData = NULL;
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700581 part.animation = loadAnimation(String8(SYSTEM_BOOTANIMATION_FILE));
582 if (part.animation != NULL)
583 animation.parts.add(part);
584 }
Mathias Agopiana8826d62009-10-01 03:10:14 -0700585 s = ++endl;
586 }
587
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700588 return true;
589}
590
591bool BootAnimation::preloadZip(Animation& animation)
592{
Mathias Agopiana8826d62009-10-01 03:10:14 -0700593 // read all the data structures
594 const size_t pcount = animation.parts.size();
Narayan Kamathafd31e02013-12-03 13:16:03 +0000595 void *cookie = NULL;
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400596 ZipFileRO* zip = animation.zip;
597 if (!zip->startIteration(&cookie)) {
Narayan Kamathafd31e02013-12-03 13:16:03 +0000598 return false;
599 }
600
Geoffrey Pitscha91a2d72016-07-12 14:46:19 -0400601 Animation::Part* partWithAudio = NULL;
Narayan Kamathafd31e02013-12-03 13:16:03 +0000602 ZipEntryRO entry;
603 char name[ANIM_ENTRY_NAME_MAX];
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400604 while ((entry = zip->nextEntry(cookie)) != NULL) {
605 const int foundEntryName = zip->getEntryFileName(entry, name, ANIM_ENTRY_NAME_MAX);
Narayan Kamathafd31e02013-12-03 13:16:03 +0000606 if (foundEntryName > ANIM_ENTRY_NAME_MAX || foundEntryName == -1) {
607 ALOGE("Error fetching entry file name");
608 continue;
609 }
610
611 const String8 entryName(name);
612 const String8 path(entryName.getPathDir());
613 const String8 leaf(entryName.getPathLeaf());
614 if (leaf.size() > 0) {
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400615 for (size_t j = 0; j < pcount; j++) {
Narayan Kamathafd31e02013-12-03 13:16:03 +0000616 if (path == animation.parts[j].path) {
Narayan Kamath4600dd02015-06-16 12:02:57 +0100617 uint16_t method;
Narayan Kamathafd31e02013-12-03 13:16:03 +0000618 // supports only stored png files
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400619 if (zip->getEntryInfo(entry, &method, NULL, NULL, NULL, NULL, NULL)) {
Narayan Kamathafd31e02013-12-03 13:16:03 +0000620 if (method == ZipFileRO::kCompressStored) {
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400621 FileMap* map = zip->createEntryFileMap(entry);
Narayan Kamathafd31e02013-12-03 13:16:03 +0000622 if (map) {
Narayan Kamathafd31e02013-12-03 13:16:03 +0000623 Animation::Part& part(animation.parts.editItemAt(j));
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700624 if (leaf == "audio.wav") {
625 // a part may have at most one audio file
Geoffrey Pitschd6d9a1d2016-06-08 00:38:58 -0700626 part.audioData = (uint8_t *)map->getDataPtr();
627 part.audioLength = map->getDataLength();
Geoffrey Pitscha91a2d72016-07-12 14:46:19 -0400628 partWithAudio = &part;
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400629 } else if (leaf == "trim.txt") {
630 part.trimData.setTo((char const*)map->getDataPtr(),
631 map->getDataLength());
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700632 } else {
633 Animation::Frame frame;
634 frame.name = leaf;
635 frame.map = map;
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400636 frame.trimWidth = animation.width;
637 frame.trimHeight = animation.height;
638 frame.trimX = 0;
639 frame.trimY = 0;
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700640 part.frames.add(frame);
641 }
Mathias Agopiana8826d62009-10-01 03:10:14 -0700642 }
Geoffrey Pitschd6d9a1d2016-06-08 00:38:58 -0700643 } else {
644 ALOGE("bootanimation.zip is compressed; must be only stored");
Mathias Agopiana8826d62009-10-01 03:10:14 -0700645 }
646 }
647 }
648 }
649 }
650 }
651
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400652 // If there is trimData present, override the positioning defaults.
653 for (Animation::Part& part : animation.parts) {
654 const char* trimDataStr = part.trimData.string();
655 for (size_t frameIdx = 0; frameIdx < part.frames.size(); frameIdx++) {
656 const char* endl = strstr(trimDataStr, "\n");
657 // No more trimData for this part.
658 if (endl == NULL) {
659 break;
660 }
661 String8 line(trimDataStr, endl - trimDataStr);
662 const char* lineStr = line.string();
663 trimDataStr = ++endl;
664 int width = 0, height = 0, x = 0, y = 0;
665 if (sscanf(lineStr, "%dx%d+%d+%d", &width, &height, &x, &y) == 4) {
666 Animation::Frame& frame(part.frames.editItemAt(frameIdx));
667 frame.trimWidth = width;
668 frame.trimHeight = height;
669 frame.trimX = x;
670 frame.trimY = y;
671 } else {
672 ALOGE("Error parsing trim.txt, line: %s", lineStr);
673 break;
674 }
675 }
676 }
677
Geoffrey Pitschd6d9a1d2016-06-08 00:38:58 -0700678 // Create and initialize audioplay if there is a wav file in any of the animations.
Geoffrey Pitscha91a2d72016-07-12 14:46:19 -0400679 if (partWithAudio != NULL) {
Geoffrey Pitschd6d9a1d2016-06-08 00:38:58 -0700680 ALOGD("found audio.wav, creating playback engine");
Geoffrey Pitscha91a2d72016-07-12 14:46:19 -0400681 if (!audioplay::create(partWithAudio->audioData, partWithAudio->audioLength)) {
682 return false;
683 }
Geoffrey Pitschd6d9a1d2016-06-08 00:38:58 -0700684 }
685
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400686 zip->endIteration(cookie);
Narayan Kamathafd31e02013-12-03 13:16:03 +0000687
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700688 return true;
689}
690
691bool BootAnimation::movie()
692{
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700693 Animation* animation = loadAnimation(mZipFileName);
694 if (animation == NULL)
695 return false;
696
Damien Bargiacchi97480862016-03-29 14:55:55 -0700697 bool anyPartHasClock = false;
698 for (size_t i=0; i < animation->parts.size(); i++) {
699 if(animation->parts[i].clockPosY >= 0) {
700 anyPartHasClock = true;
701 break;
702 }
703 }
704 if (!anyPartHasClock) {
705 mClockEnabled = false;
706 }
707
Sai Kiran Korwar27167492015-07-07 20:00:06 +0530708 // Check if npot textures are supported
709 mUseNpotTextures = false;
710 String8 gl_extensions;
711 const char* exts = reinterpret_cast<const char*>(glGetString(GL_EXTENSIONS));
712 if (!exts) {
713 glGetError();
714 } else {
715 gl_extensions.setTo(exts);
716 if ((gl_extensions.find("GL_ARB_texture_non_power_of_two") != -1) ||
717 (gl_extensions.find("GL_OES_texture_npot") != -1)) {
718 mUseNpotTextures = true;
719 }
720 }
721
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800722 // Blend required to draw time on top of animation frames.
723 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700724 glShadeModel(GL_FLAT);
725 glDisable(GL_DITHER);
726 glDisable(GL_SCISSOR_TEST);
727 glDisable(GL_BLEND);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700728
729 glBindTexture(GL_TEXTURE_2D, 0);
730 glEnable(GL_TEXTURE_2D);
731 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
732 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
733 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
734 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
735 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
736
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800737 bool clockTextureInitialized = false;
738 if (mClockEnabled) {
739 clockTextureInitialized = (initTexture(&mClock, mAssets, "images/clock64.png") == NO_ERROR);
740 mClockEnabled = clockTextureInitialized;
741 }
742
Damien Bargiacchi97480862016-03-29 14:55:55 -0700743 if (mClockEnabled && !updateIsTimeAccurate()) {
744 mTimeCheckThread = new TimeCheckThread(this);
745 mTimeCheckThread->run("BootAnimation::TimeCheckThread", PRIORITY_NORMAL);
746 }
747
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700748 playAnimation(*animation);
Damien Bargiacchi97480862016-03-29 14:55:55 -0700749
750 if (mTimeCheckThread != NULL) {
751 mTimeCheckThread->requestExit();
752 mTimeCheckThread = NULL;
753 }
754
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700755 releaseAnimation(animation);
756
Andriy Naborskyy815e51d2016-03-24 16:43:34 -0700757 if (clockTextureInitialized) {
758 glDeleteTextures(1, &mClock.name);
759 }
760
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700761 return false;
762}
763
764bool BootAnimation::playAnimation(const Animation& animation)
765{
766 const size_t pcount = animation.parts.size();
Mathias Agopiana8826d62009-10-01 03:10:14 -0700767 nsecs_t frameDuration = s2ns(1) / animation.fps;
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400768 const int animationX = (mWidth - animation.width) / 2;
769 const int animationY = (mHeight - animation.height) / 2;
Mathias Agopian9f3020d2009-11-06 16:30:18 -0800770
Narayan Kamathafd31e02013-12-03 13:16:03 +0000771 for (size_t i=0 ; i<pcount ; i++) {
Mathias Agopiana8826d62009-10-01 03:10:14 -0700772 const Animation::Part& part(animation.parts[i]);
773 const size_t fcount = part.frames.size();
774 glBindTexture(GL_TEXTURE_2D, 0);
775
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700776 // Handle animation package
777 if (part.animation != NULL) {
778 playAnimation(*part.animation);
779 if (exitPending())
780 break;
781 continue; //to next part
782 }
783
Mathias Agopiana8826d62009-10-01 03:10:14 -0700784 for (int r=0 ; !part.count || r<part.count ; r++) {
Kevin Hesterd3782b22012-04-26 10:38:55 -0700785 // Exit any non playuntil complete parts immediately
786 if(exitPending() && !part.playUntilComplete)
787 break;
788
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700789 // only play audio file the first time we animate the part
Geoffrey Pitsch290c4352016-08-09 14:35:10 -0400790 if (r == 0 && part.audioData && playSoundsAllowed()) {
791 ALOGD("playing clip for part%d, size=%d", (int) i, part.audioLength);
792 audioplay::playClip(part.audioData, part.audioLength);
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700793 }
794
Jesse Hall083b84c2014-09-22 10:51:09 -0700795 glClearColor(
796 part.backgroundColor[0],
797 part.backgroundColor[1],
798 part.backgroundColor[2],
799 1.0f);
800
Narayan Kamathafd31e02013-12-03 13:16:03 +0000801 for (size_t j=0 ; j<fcount && (!exitPending() || part.playUntilComplete) ; j++) {
Mathias Agopiana8826d62009-10-01 03:10:14 -0700802 const Animation::Frame& frame(part.frames[j]);
Mathias Agopiandb7dd2a2012-05-12 15:08:21 -0700803 nsecs_t lastFrame = systemTime();
Mathias Agopiana8826d62009-10-01 03:10:14 -0700804
805 if (r > 0) {
806 glBindTexture(GL_TEXTURE_2D, frame.tid);
807 } else {
808 if (part.count != 1) {
809 glGenTextures(1, &frame.tid);
810 glBindTexture(GL_TEXTURE_2D, frame.tid);
811 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
812 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
813 }
Mykola Kondratenko0c1eeb32014-04-15 09:35:44 +0200814 initTexture(frame);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700815 }
816
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400817 const int xc = animationX + frame.trimX;
818 const int yc = animationY + frame.trimY;
819 Region clearReg(Rect(mWidth, mHeight));
820 clearReg.subtractSelf(Rect(xc, yc, xc+frame.trimWidth, yc+frame.trimHeight));
Mathias Agopian9f3020d2009-11-06 16:30:18 -0800821 if (!clearReg.isEmpty()) {
822 Region::const_iterator head(clearReg.begin());
823 Region::const_iterator tail(clearReg.end());
824 glEnable(GL_SCISSOR_TEST);
825 while (head != tail) {
Andreas Gampecfedceb2014-09-30 21:48:18 -0700826 const Rect& r2(*head++);
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400827 glScissor(r2.left, mHeight - r2.bottom, r2.width(), r2.height());
Mathias Agopian9f3020d2009-11-06 16:30:18 -0800828 glClear(GL_COLOR_BUFFER_BIT);
829 }
830 glDisable(GL_SCISSOR_TEST);
831 }
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400832 // specify the y center as ceiling((mHeight - frame.trimHeight) / 2)
833 // which is equivalent to mHeight - (yc + frame.trimHeight)
834 glDrawTexiOES(xc, mHeight - (yc + frame.trimHeight),
835 0, frame.trimWidth, frame.trimHeight);
Damien Bargiacchi97480862016-03-29 14:55:55 -0700836 if (mClockEnabled && mTimeIsAccurate && part.clockPosY >= 0) {
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800837 drawTime(mClock, part.clockPosY);
838 }
839
Mathias Agopiana8826d62009-10-01 03:10:14 -0700840 eglSwapBuffers(mDisplay, mSurface);
841
842 nsecs_t now = systemTime();
843 nsecs_t delay = frameDuration - (now - lastFrame);
Mathias Agopiandb7dd2a2012-05-12 15:08:21 -0700844 //ALOGD("%lld, %lld", ns2ms(now - lastFrame), ns2ms(delay));
Mathias Agopiana8826d62009-10-01 03:10:14 -0700845 lastFrame = now;
Mathias Agopiandb7dd2a2012-05-12 15:08:21 -0700846
847 if (delay > 0) {
848 struct timespec spec;
849 spec.tv_sec = (now + delay) / 1000000000;
850 spec.tv_nsec = (now + delay) % 1000000000;
851 int err;
852 do {
853 err = clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &spec, NULL);
854 } while (err<0 && errno == EINTR);
855 }
Kevin Hesterd3782b22012-04-26 10:38:55 -0700856
857 checkExit();
Mathias Agopiana8826d62009-10-01 03:10:14 -0700858 }
Kevin Hesterd3782b22012-04-26 10:38:55 -0700859
Mathias Agopiana8826d62009-10-01 03:10:14 -0700860 usleep(part.pause * ns2us(frameDuration));
Kevin Hesterd3782b22012-04-26 10:38:55 -0700861
862 // For infinite parts, we've now played them at least once, so perhaps exit
863 if(exitPending() && !part.count)
864 break;
Mathias Agopiana8826d62009-10-01 03:10:14 -0700865 }
866
Geoffrey Pitsch2fb30fb2016-07-06 16:16:20 -0400867 }
868
869 // Free textures created for looping parts now that the animation is done.
870 for (const Animation::Part& part : animation.parts) {
Mathias Agopiana8826d62009-10-01 03:10:14 -0700871 if (part.count != 1) {
Geoffrey Pitsch2fb30fb2016-07-06 16:16:20 -0400872 const size_t fcount = part.frames.size();
873 for (size_t j = 0; j < fcount; j++) {
Mathias Agopiana8826d62009-10-01 03:10:14 -0700874 const Animation::Frame& frame(part.frames[j]);
875 glDeleteTextures(1, &frame.tid);
876 }
877 }
878 }
Geoffrey Pitschd6d9a1d2016-06-08 00:38:58 -0700879
880 // we've finally played everything we're going to play
881 audioplay::setPlaying(false);
882 audioplay::destroy();
883
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700884 return true;
Mathias Agopiana8826d62009-10-01 03:10:14 -0700885}
886
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700887void BootAnimation::releaseAnimation(Animation* animation) const
888{
889 for (Vector<Animation::Part>::iterator it = animation->parts.begin(),
890 e = animation->parts.end(); it != e; ++it) {
891 if (it->animation)
892 releaseAnimation(it->animation);
893 }
894 if (animation->zip)
895 delete animation->zip;
896 delete animation;
897}
898
899BootAnimation::Animation* BootAnimation::loadAnimation(const String8& fn)
900{
901 if (mLoadedFiles.indexOf(fn) >= 0) {
902 ALOGE("File \"%s\" is already loaded. Cyclic ref is not allowed",
903 fn.string());
904 return NULL;
905 }
906 ZipFileRO *zip = ZipFileRO::open(fn);
907 if (zip == NULL) {
908 ALOGE("Failed to open animation zip \"%s\": %s",
909 fn.string(), strerror(errno));
910 return NULL;
911 }
912
913 Animation *animation = new Animation;
914 animation->fileName = fn;
915 animation->zip = zip;
916 mLoadedFiles.add(animation->fileName);
917
918 parseAnimationDesc(*animation);
Geoffrey Pitscha91a2d72016-07-12 14:46:19 -0400919 if (!preloadZip(*animation)) {
920 return NULL;
921 }
922
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700923
924 mLoadedFiles.remove(fn);
925 return animation;
926}
Damien Bargiacchi97480862016-03-29 14:55:55 -0700927
Geoffrey Pitsch290c4352016-08-09 14:35:10 -0400928bool BootAnimation::playSoundsAllowed() const {
929 // Only play sounds for system boots, not runtime restarts.
930 if (!mSystemBoot) {
931 return false;
932 }
933
934 // Read the system property to see if we should play the sound.
935 // If it's not present, default to allowed.
936 if (!property_get_bool(PLAY_SOUND_PROP_NAME, 1)) {
937 return false;
938 }
939
940 // Don't play sounds if this is a reboot due to an error.
941 char bootreason[PROPERTY_VALUE_MAX];
942 if (property_get(BOOTREASON_PROP_NAME, bootreason, nullptr) > 0) {
943 for (const auto& str : PLAY_SOUND_BOOTREASON_BLACKLIST) {
944 if (strcasecmp(str.c_str(), bootreason) == 0) {
945 return false;
946 }
947 }
948 }
949 return true;
950}
951
Damien Bargiacchi97480862016-03-29 14:55:55 -0700952bool BootAnimation::updateIsTimeAccurate() {
953 static constexpr long long MAX_TIME_IN_PAST = 60000LL * 60LL * 24LL * 30LL; // 30 days
954 static constexpr long long MAX_TIME_IN_FUTURE = 60000LL * 90LL; // 90 minutes
955
956 if (mTimeIsAccurate) {
957 return true;
958 }
959
960 struct stat statResult;
961 if(stat(ACCURATE_TIME_FLAG_FILE_PATH, &statResult) == 0) {
962 mTimeIsAccurate = true;
963 return true;
964 }
965
966 FILE* file = fopen(LAST_TIME_CHANGED_FILE_PATH, "r");
967 if (file != NULL) {
968 long long lastChangedTime = 0;
969 fscanf(file, "%lld", &lastChangedTime);
970 fclose(file);
971 if (lastChangedTime > 0) {
972 struct timespec now;
973 clock_gettime(CLOCK_REALTIME, &now);
974 // Match the Java timestamp format
975 long long rtcNow = (now.tv_sec * 1000LL) + (now.tv_nsec / 1000000LL);
Damien Bargiacchi96762812016-07-12 15:53:40 -0700976 if (ACCURATE_TIME_EPOCH < rtcNow
977 && lastChangedTime > (rtcNow - MAX_TIME_IN_PAST)
978 && lastChangedTime < (rtcNow + MAX_TIME_IN_FUTURE)) {
Damien Bargiacchi97480862016-03-29 14:55:55 -0700979 mTimeIsAccurate = true;
980 }
981 }
982 }
983
984 return mTimeIsAccurate;
985}
986
987BootAnimation::TimeCheckThread::TimeCheckThread(BootAnimation* bootAnimation) : Thread(false),
988 mInotifyFd(-1), mSystemWd(-1), mTimeWd(-1), mBootAnimation(bootAnimation) {}
989
990BootAnimation::TimeCheckThread::~TimeCheckThread() {
991 // mInotifyFd may be -1 but that's ok since we're not at risk of attempting to close a valid FD.
992 close(mInotifyFd);
993}
994
995bool BootAnimation::TimeCheckThread::threadLoop() {
996 bool shouldLoop = doThreadLoop() && !mBootAnimation->mTimeIsAccurate
997 && mBootAnimation->mClockEnabled;
998 if (!shouldLoop) {
999 close(mInotifyFd);
1000 mInotifyFd = -1;
1001 }
1002 return shouldLoop;
1003}
1004
1005bool BootAnimation::TimeCheckThread::doThreadLoop() {
1006 static constexpr int BUFF_LEN (10 * (sizeof(struct inotify_event) + NAME_MAX + 1));
1007
1008 // Poll instead of doing a blocking read so the Thread can exit if requested.
1009 struct pollfd pfd = { mInotifyFd, POLLIN, 0 };
1010 ssize_t pollResult = poll(&pfd, 1, 1000);
1011
1012 if (pollResult == 0) {
1013 return true;
1014 } else if (pollResult < 0) {
1015 ALOGE("Could not poll inotify events");
1016 return false;
1017 }
1018
1019 char buff[BUFF_LEN] __attribute__ ((aligned(__alignof__(struct inotify_event))));;
1020 ssize_t length = read(mInotifyFd, buff, BUFF_LEN);
1021 if (length == 0) {
1022 return true;
1023 } else if (length < 0) {
1024 ALOGE("Could not read inotify events");
1025 return false;
1026 }
1027
1028 const struct inotify_event *event;
1029 for (char* ptr = buff; ptr < buff + length; ptr += sizeof(struct inotify_event) + event->len) {
1030 event = (const struct inotify_event *) ptr;
1031 if (event->wd == mSystemWd && strcmp(SYSTEM_TIME_DIR_NAME, event->name) == 0) {
1032 addTimeDirWatch();
1033 } else if (event->wd == mTimeWd && (strcmp(LAST_TIME_CHANGED_FILE_NAME, event->name) == 0
1034 || strcmp(ACCURATE_TIME_FLAG_FILE_NAME, event->name) == 0)) {
1035 return !mBootAnimation->updateIsTimeAccurate();
1036 }
1037 }
1038
1039 return true;
1040}
1041
1042void BootAnimation::TimeCheckThread::addTimeDirWatch() {
1043 mTimeWd = inotify_add_watch(mInotifyFd, SYSTEM_TIME_DIR_PATH,
1044 IN_CLOSE_WRITE | IN_MOVED_TO | IN_ATTRIB);
1045 if (mTimeWd > 0) {
1046 // No need to watch for the time directory to be created if it already exists
1047 inotify_rm_watch(mInotifyFd, mSystemWd);
1048 mSystemWd = -1;
1049 }
1050}
1051
1052status_t BootAnimation::TimeCheckThread::readyToRun() {
1053 mInotifyFd = inotify_init();
1054 if (mInotifyFd < 0) {
1055 ALOGE("Could not initialize inotify fd");
1056 return NO_INIT;
1057 }
1058
1059 mSystemWd = inotify_add_watch(mInotifyFd, SYSTEM_DATA_DIR_PATH, IN_CREATE | IN_ATTRIB);
1060 if (mSystemWd < 0) {
1061 close(mInotifyFd);
1062 mInotifyFd = -1;
1063 ALOGE("Could not add watch for %s", SYSTEM_DATA_DIR_PATH);
1064 return NO_INIT;
1065 }
1066
1067 addTimeDirWatch();
1068
1069 if (mBootAnimation->updateIsTimeAccurate()) {
1070 close(mInotifyFd);
1071 mInotifyFd = -1;
1072 return ALREADY_EXISTS;
1073 }
1074
1075 return NO_ERROR;
1076}
1077
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001078// ---------------------------------------------------------------------------
1079
1080}
1081; // namespace android