blob: 495e56ce64b3dea4a7e6c27c493b625f1e0dec29 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -070017#define LOG_NDEBUG 0
Mathias Agopianbc726112009-09-23 15:44:05 -070018#define LOG_TAG "BootAnimation"
19
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080020#include <stdint.h>
Damien Bargiacchi97480862016-03-29 14:55:55 -070021#include <sys/inotify.h>
22#include <sys/poll.h>
23#include <sys/stat.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080024#include <sys/types.h>
25#include <math.h>
26#include <fcntl.h>
27#include <utils/misc.h>
Mathias Agopianb4d5a722009-09-23 17:05:19 -070028#include <signal.h>
Elliott Hughesbb94f312014-10-21 10:41:33 -070029#include <time.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080030
Jason parksbd9a08d2011-01-31 15:04:34 -060031#include <cutils/properties.h>
32
Mathias Agopianb13b9bd2012-02-17 18:27:36 -080033#include <androidfw/AssetManager.h>
Mathias Agopianac31a3b2009-05-21 19:59:24 -070034#include <binder/IPCThreadState.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080035#include <utils/Atomic.h>
36#include <utils/Errors.h>
37#include <utils/Log.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080038
39#include <ui/PixelFormat.h>
40#include <ui/Rect.h>
41#include <ui/Region.h>
42#include <ui/DisplayInfo.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080043
Jeff Brown0b722fe2012-08-24 22:40:14 -070044#include <gui/ISurfaceComposer.h>
Mathias Agopian8335f1c2012-02-25 18:48:35 -080045#include <gui/Surface.h>
46#include <gui/SurfaceComposerClient.h>
Mathias Agopian000479f2010-02-09 17:46:37 -080047
Andreas Gampecfedceb2014-09-30 21:48:18 -070048// TODO: Fix Skia.
49#pragma GCC diagnostic push
50#pragma GCC diagnostic ignored "-Wunused-parameter"
Derek Sollenbergereece0dd2014-02-27 14:31:29 -050051#include <SkBitmap.h>
52#include <SkStream.h>
53#include <SkImageDecoder.h>
Andreas Gampecfedceb2014-09-30 21:48:18 -070054#pragma GCC diagnostic pop
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080055
56#include <GLES/gl.h>
57#include <GLES/glext.h>
58#include <EGL/eglext.h>
59
60#include "BootAnimation.h"
Geoffrey Pitschd6d9a1d2016-06-08 00:38:58 -070061#include "audioplay.h"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080062
63namespace android {
64
Damien Bargiacchi97480862016-03-29 14:55:55 -070065static const char OEM_BOOTANIMATION_FILE[] = "/oem/media/bootanimation.zip";
66static const char SYSTEM_BOOTANIMATION_FILE[] = "/system/media/bootanimation.zip";
67static const char SYSTEM_ENCRYPTED_BOOTANIMATION_FILE[] = "/system/media/bootanimation-encrypted.zip";
68static const char SYSTEM_DATA_DIR_PATH[] = "/data/system";
69static const char SYSTEM_TIME_DIR_NAME[] = "time";
70static const char SYSTEM_TIME_DIR_PATH[] = "/data/system/time";
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;
80
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080081// ---------------------------------------------------------------------------
82
Damien Bargiacchi97480862016-03-29 14:55:55 -070083BootAnimation::BootAnimation() : Thread(false), mClockEnabled(true), mTimeIsAccurate(false),
84 mTimeCheckThread(NULL) {
Mathias Agopian627e7b52009-05-21 19:21:59 -070085 mSession = new SurfaceComposerClient();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080086}
87
Damien Bargiacchi97480862016-03-29 14:55:55 -070088BootAnimation::~BootAnimation() {}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080089
90void BootAnimation::onFirstRef() {
Mathias Agopianbc726112009-09-23 15:44:05 -070091 status_t err = mSession->linkToComposerDeath(this);
Steve Block3762c312012-01-06 19:20:56 +000092 ALOGE_IF(err, "linkToComposerDeath failed (%s) ", strerror(-err));
Mathias Agopian8434c532009-09-23 18:52:49 -070093 if (err == NO_ERROR) {
Mathias Agopianbc726112009-09-23 15:44:05 -070094 run("BootAnimation", PRIORITY_DISPLAY);
95 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080096}
97
Mathias Agopianbc726112009-09-23 15:44:05 -070098sp<SurfaceComposerClient> BootAnimation::session() const {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080099 return mSession;
100}
101
Mathias Agopianbc726112009-09-23 15:44:05 -0700102
Narayan Kamathafd31e02013-12-03 13:16:03 +0000103void BootAnimation::binderDied(const wp<IBinder>&)
Mathias Agopianbc726112009-09-23 15:44:05 -0700104{
105 // woah, surfaceflinger died!
Steve Block5baa3a62011-12-20 16:23:08 +0000106 ALOGD("SurfaceFlinger died, exiting...");
Mathias Agopianbc726112009-09-23 15:44:05 -0700107
108 // calling requestExit() is not enough here because the Surface code
109 // might be blocked on a condition variable that will never be updated.
110 kill( getpid(), SIGKILL );
111 requestExit();
Geoffrey Pitschd6d9a1d2016-06-08 00:38:58 -0700112 audioplay::destroy();
Mathias Agopianbc726112009-09-23 15:44:05 -0700113}
114
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800115status_t BootAnimation::initTexture(Texture* texture, AssetManager& assets,
116 const char* name) {
117 Asset* asset = assets.open(name, Asset::ACCESS_BUFFER);
Andreas Gampecfedceb2014-09-30 21:48:18 -0700118 if (asset == NULL)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800119 return NO_INIT;
120 SkBitmap bitmap;
121 SkImageDecoder::DecodeMemory(asset->getBuffer(false), asset->getLength(),
Mike Reed42a1d082014-07-07 18:06:18 -0400122 &bitmap, kUnknown_SkColorType, SkImageDecoder::kDecodePixels_Mode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800123 asset->close();
124 delete asset;
125
126 // ensure we can call getPixels(). No need to call unlock, since the
127 // bitmap will go out of scope when we return from this method.
128 bitmap.lockPixels();
129
130 const int w = bitmap.width();
131 const int h = bitmap.height();
132 const void* p = bitmap.getPixels();
133
134 GLint crop[4] = { 0, h, w, -h };
135 texture->w = w;
136 texture->h = h;
137
138 glGenTextures(1, &texture->name);
139 glBindTexture(GL_TEXTURE_2D, texture->name);
140
Mike Reed42a1d082014-07-07 18:06:18 -0400141 switch (bitmap.colorType()) {
142 case kAlpha_8_SkColorType:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800143 glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, w, h, 0, GL_ALPHA,
144 GL_UNSIGNED_BYTE, p);
145 break;
Mike Reed42a1d082014-07-07 18:06:18 -0400146 case kARGB_4444_SkColorType:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800147 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA,
148 GL_UNSIGNED_SHORT_4_4_4_4, p);
149 break;
Mike Reed42a1d082014-07-07 18:06:18 -0400150 case kN32_SkColorType:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800151 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA,
152 GL_UNSIGNED_BYTE, p);
153 break;
Mike Reed42a1d082014-07-07 18:06:18 -0400154 case kRGB_565_SkColorType:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800155 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, w, h, 0, GL_RGB,
156 GL_UNSIGNED_SHORT_5_6_5, p);
157 break;
158 default:
159 break;
160 }
161
162 glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_CROP_RECT_OES, crop);
163 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
164 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
165 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
166 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
167 return NO_ERROR;
168}
169
Mykola Kondratenko0c1eeb32014-04-15 09:35:44 +0200170status_t BootAnimation::initTexture(const Animation::Frame& frame)
Mathias Agopiana8826d62009-10-01 03:10:14 -0700171{
172 //StopWatch watch("blah");
173
174 SkBitmap bitmap;
Mykola Kondratenko0c1eeb32014-04-15 09:35:44 +0200175 SkMemoryStream stream(frame.map->getDataPtr(), frame.map->getDataLength());
Mathias Agopian2b99e552011-11-10 15:59:07 -0800176 SkImageDecoder* codec = SkImageDecoder::Factory(&stream);
Andreas Gampecfedceb2014-09-30 21:48:18 -0700177 if (codec != NULL) {
Elliott Hughesc367d482013-10-29 13:12:55 -0700178 codec->setDitherImage(false);
Mathias Agopian2b99e552011-11-10 15:59:07 -0800179 codec->decode(&stream, &bitmap,
Mike Reed42a1d082014-07-07 18:06:18 -0400180 kN32_SkColorType,
Mathias Agopian2b99e552011-11-10 15:59:07 -0800181 SkImageDecoder::kDecodePixels_Mode);
182 delete codec;
183 }
Mathias Agopiana8826d62009-10-01 03:10:14 -0700184
Mykola Kondratenko0c1eeb32014-04-15 09:35:44 +0200185 // FileMap memory is never released until application exit.
186 // Release it now as the texture is already loaded and the memory used for
187 // the packed resource can be released.
Narayan Kamath688ff4c2015-02-23 15:47:54 +0000188 delete frame.map;
Mykola Kondratenko0c1eeb32014-04-15 09:35:44 +0200189
Mathias Agopiana8826d62009-10-01 03:10:14 -0700190 // ensure we can call getPixels(). No need to call unlock, since the
191 // bitmap will go out of scope when we return from this method.
192 bitmap.lockPixels();
193
194 const int w = bitmap.width();
195 const int h = bitmap.height();
196 const void* p = bitmap.getPixels();
197
198 GLint crop[4] = { 0, h, w, -h };
199 int tw = 1 << (31 - __builtin_clz(w));
200 int th = 1 << (31 - __builtin_clz(h));
201 if (tw < w) tw <<= 1;
202 if (th < h) th <<= 1;
203
Mike Reed42a1d082014-07-07 18:06:18 -0400204 switch (bitmap.colorType()) {
205 case kN32_SkColorType:
Sai Kiran Korwar27167492015-07-07 20:00:06 +0530206 if (!mUseNpotTextures && (tw != w || th != h)) {
Mathias Agopiana8826d62009-10-01 03:10:14 -0700207 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, tw, th, 0, GL_RGBA,
208 GL_UNSIGNED_BYTE, 0);
209 glTexSubImage2D(GL_TEXTURE_2D, 0,
210 0, 0, w, h, GL_RGBA, GL_UNSIGNED_BYTE, p);
211 } else {
Sai Kiran Korwar27167492015-07-07 20:00:06 +0530212 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA,
Mathias Agopiana8826d62009-10-01 03:10:14 -0700213 GL_UNSIGNED_BYTE, p);
214 }
215 break;
216
Mike Reed42a1d082014-07-07 18:06:18 -0400217 case kRGB_565_SkColorType:
Sai Kiran Korwar27167492015-07-07 20:00:06 +0530218 if (!mUseNpotTextures && (tw != w || th != h)) {
Mathias Agopiana8826d62009-10-01 03:10:14 -0700219 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, tw, th, 0, GL_RGB,
220 GL_UNSIGNED_SHORT_5_6_5, 0);
221 glTexSubImage2D(GL_TEXTURE_2D, 0,
222 0, 0, w, h, GL_RGB, GL_UNSIGNED_SHORT_5_6_5, p);
223 } else {
Sai Kiran Korwar27167492015-07-07 20:00:06 +0530224 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, w, h, 0, GL_RGB,
Mathias Agopiana8826d62009-10-01 03:10:14 -0700225 GL_UNSIGNED_SHORT_5_6_5, p);
226 }
227 break;
228 default:
229 break;
230 }
231
232 glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_CROP_RECT_OES, crop);
233
234 return NO_ERROR;
235}
236
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800237status_t BootAnimation::readyToRun() {
238 mAssets.addDefaultAssets();
239
Jeff Brown0b722fe2012-08-24 22:40:14 -0700240 sp<IBinder> dtoken(SurfaceComposerClient::getBuiltInDisplay(
241 ISurfaceComposer::eDisplayIdMain));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800242 DisplayInfo dinfo;
Jeff Brown0b722fe2012-08-24 22:40:14 -0700243 status_t status = SurfaceComposerClient::getDisplayInfo(dtoken, &dinfo);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800244 if (status)
245 return -1;
246
247 // create the native surface
Jeff Brown0b722fe2012-08-24 22:40:14 -0700248 sp<SurfaceControl> control = session()->createSurface(String8("BootAnimation"),
249 dinfo.w, dinfo.h, PIXEL_FORMAT_RGB_565);
Mathias Agopian439863f2011-06-28 19:09:31 -0700250
251 SurfaceComposerClient::openGlobalTransaction();
Mathias Agopian17f638b2009-04-16 20:04:08 -0700252 control->setLayer(0x40000000);
Mathias Agopian439863f2011-06-28 19:09:31 -0700253 SurfaceComposerClient::closeGlobalTransaction();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800254
Mathias Agopian17f638b2009-04-16 20:04:08 -0700255 sp<Surface> s = control->getSurface();
256
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800257 // initialize opengl and egl
Mathias Agopian738b9a42009-08-06 16:41:02 -0700258 const EGLint attribs[] = {
Mathias Agopian1b253b72011-08-15 15:20:22 -0700259 EGL_RED_SIZE, 8,
260 EGL_GREEN_SIZE, 8,
261 EGL_BLUE_SIZE, 8,
Mathias Agopiana8826d62009-10-01 03:10:14 -0700262 EGL_DEPTH_SIZE, 0,
263 EGL_NONE
Mathias Agopian738b9a42009-08-06 16:41:02 -0700264 };
Andreas Gampecfedceb2014-09-30 21:48:18 -0700265 EGLint w, h;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800266 EGLint numConfigs;
267 EGLConfig config;
268 EGLSurface surface;
269 EGLContext context;
Mathias Agopian627e7b52009-05-21 19:21:59 -0700270
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800271 EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
Mathias Agopian627e7b52009-05-21 19:21:59 -0700272
273 eglInitialize(display, 0, 0);
Mathias Agopian1b253b72011-08-15 15:20:22 -0700274 eglChooseConfig(display, attribs, &config, 1, &numConfigs);
Mathias Agopian1473f462009-04-10 14:24:30 -0700275 surface = eglCreateWindowSurface(display, config, s.get(), NULL);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800276 context = eglCreateContext(display, config, NULL, NULL);
277 eglQuerySurface(display, surface, EGL_WIDTH, &w);
278 eglQuerySurface(display, surface, EGL_HEIGHT, &h);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700279
Mathias Agopianabac0102009-07-31 14:47:00 -0700280 if (eglMakeCurrent(display, surface, surface, context) == EGL_FALSE)
281 return NO_INIT;
Mathias Agopiana8826d62009-10-01 03:10:14 -0700282
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800283 mDisplay = display;
284 mContext = context;
285 mSurface = surface;
286 mWidth = w;
287 mHeight = h;
Mathias Agopian17f638b2009-04-16 20:04:08 -0700288 mFlingerSurfaceControl = control;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800289 mFlingerSurface = s;
290
Elliott Hughesc367d482013-10-29 13:12:55 -0700291 // If the device has encryption turned on or is in process
Jason parksbd9a08d2011-01-31 15:04:34 -0600292 // of being encrypted we show the encrypted boot animation.
293 char decrypt[PROPERTY_VALUE_MAX];
294 property_get("vold.decrypt", decrypt, "");
295
296 bool encryptedAnimation = atoi(decrypt) != 0 || !strcmp("trigger_restart_min_framework", decrypt);
297
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700298 if (encryptedAnimation && (access(SYSTEM_ENCRYPTED_BOOTANIMATION_FILE, R_OK) == 0)) {
299 mZipFileName = SYSTEM_ENCRYPTED_BOOTANIMATION_FILE;
Jason parksbd9a08d2011-01-31 15:04:34 -0600300 }
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700301 else if (access(OEM_BOOTANIMATION_FILE, R_OK) == 0) {
302 mZipFileName = OEM_BOOTANIMATION_FILE;
303 }
304 else if (access(SYSTEM_BOOTANIMATION_FILE, R_OK) == 0) {
305 mZipFileName = SYSTEM_BOOTANIMATION_FILE;
306 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800307 return NO_ERROR;
308}
309
Mathias Agopiana8826d62009-10-01 03:10:14 -0700310bool BootAnimation::threadLoop()
311{
312 bool r;
Narayan Kamathafd31e02013-12-03 13:16:03 +0000313 // We have no bootanimation file, so we use the stock android logo
314 // animation.
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700315 if (mZipFileName.isEmpty()) {
Mathias Agopiana8826d62009-10-01 03:10:14 -0700316 r = android();
317 } else {
318 r = movie();
319 }
320
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800321 eglMakeCurrent(mDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
322 eglDestroyContext(mDisplay, mContext);
323 eglDestroySurface(mDisplay, mSurface);
Mathias Agopian6cf0db22009-04-17 19:36:26 -0700324 mFlingerSurface.clear();
Mathias Agopian17f638b2009-04-16 20:04:08 -0700325 mFlingerSurfaceControl.clear();
Mathias Agopian627e7b52009-05-21 19:21:59 -0700326 eglTerminate(mDisplay);
327 IPCThreadState::self()->stopProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800328 return r;
329}
330
Mathias Agopiana8826d62009-10-01 03:10:14 -0700331bool BootAnimation::android()
332{
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700333 initTexture(&mAndroid[0], mAssets, "images/android-logo-mask.png");
334 initTexture(&mAndroid[1], mAssets, "images/android-logo-shine.png");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800335
336 // clear screen
Mathias Agopiana8826d62009-10-01 03:10:14 -0700337 glShadeModel(GL_FLAT);
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700338 glDisable(GL_DITHER);
339 glDisable(GL_SCISSOR_TEST);
Mathias Agopian59f19e42011-05-06 19:22:12 -0700340 glClearColor(0,0,0,1);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800341 glClear(GL_COLOR_BUFFER_BIT);
342 eglSwapBuffers(mDisplay, mSurface);
343
Mathias Agopiana8826d62009-10-01 03:10:14 -0700344 glEnable(GL_TEXTURE_2D);
345 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
346
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700347 const GLint xc = (mWidth - mAndroid[0].w) / 2;
348 const GLint yc = (mHeight - mAndroid[0].h) / 2;
349 const Rect updateRect(xc, yc, xc + mAndroid[0].w, yc + mAndroid[0].h);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800350
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800351 glScissor(updateRect.left, mHeight - updateRect.bottom, updateRect.width(),
352 updateRect.height());
353
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700354 // Blend state
355 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
356 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
357
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800358 const nsecs_t startTime = systemTime();
359 do {
Mathias Agopian13796652009-03-24 22:49:21 -0700360 nsecs_t now = systemTime();
361 double time = now - startTime;
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700362 float t = 4.0f * float(time / us2ns(16667)) / mAndroid[1].w;
363 GLint offset = (1 - (t - floorf(t))) * mAndroid[1].w;
364 GLint x = xc - offset;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800365
Mathias Agopian81668642009-07-28 11:41:30 -0700366 glDisable(GL_SCISSOR_TEST);
367 glClear(GL_COLOR_BUFFER_BIT);
368
369 glEnable(GL_SCISSOR_TEST);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800370 glDisable(GL_BLEND);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800371 glBindTexture(GL_TEXTURE_2D, mAndroid[1].name);
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700372 glDrawTexiOES(x, yc, 0, mAndroid[1].w, mAndroid[1].h);
373 glDrawTexiOES(x + mAndroid[1].w, yc, 0, mAndroid[1].w, mAndroid[1].h);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800374
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700375 glEnable(GL_BLEND);
376 glBindTexture(GL_TEXTURE_2D, mAndroid[0].name);
377 glDrawTexiOES(xc, yc, 0, mAndroid[0].w, mAndroid[0].h);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800378
Mathias Agopian627e7b52009-05-21 19:21:59 -0700379 EGLBoolean res = eglSwapBuffers(mDisplay, mSurface);
380 if (res == EGL_FALSE)
381 break;
382
Mathias Agopian13796652009-03-24 22:49:21 -0700383 // 12fps: don't animate too fast to preserve CPU
384 const nsecs_t sleepTime = 83333 - ns2us(systemTime() - now);
385 if (sleepTime > 0)
Mathias Agopiana8826d62009-10-01 03:10:14 -0700386 usleep(sleepTime);
Kevin Hesterd3782b22012-04-26 10:38:55 -0700387
388 checkExit();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800389 } while (!exitPending());
390
391 glDeleteTextures(1, &mAndroid[0].name);
392 glDeleteTextures(1, &mAndroid[1].name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800393 return false;
394}
395
Mathias Agopiana8826d62009-10-01 03:10:14 -0700396
Kevin Hesterd3782b22012-04-26 10:38:55 -0700397void BootAnimation::checkExit() {
398 // Allow surface flinger to gracefully request shutdown
399 char value[PROPERTY_VALUE_MAX];
400 property_get(EXIT_PROP_NAME, value, "0");
401 int exitnow = atoi(value);
402 if (exitnow) {
403 requestExit();
404 }
405}
406
Jesse Hall083b84c2014-09-22 10:51:09 -0700407// Parse a color represented as an HTML-style 'RRGGBB' string: each pair of
408// characters in str is a hex number in [0, 255], which are converted to
409// floating point values in the range [0.0, 1.0] and placed in the
410// corresponding elements of color.
411//
412// If the input string isn't valid, parseColor returns false and color is
413// left unchanged.
414static bool parseColor(const char str[7], float color[3]) {
415 float tmpColor[3];
416 for (int i = 0; i < 3; i++) {
417 int val = 0;
418 for (int j = 0; j < 2; j++) {
419 val *= 16;
420 char c = str[2*i + j];
421 if (c >= '0' && c <= '9') val += c - '0';
422 else if (c >= 'A' && c <= 'F') val += (c - 'A') + 10;
423 else if (c >= 'a' && c <= 'f') val += (c - 'a') + 10;
424 else return false;
425 }
426 tmpColor[i] = static_cast<float>(val) / 255.0f;
427 }
428 memcpy(color, tmpColor, sizeof(tmpColor));
429 return true;
430}
431
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700432
433static bool readFile(ZipFileRO* zip, const char* name, String8& outString)
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700434{
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700435 ZipEntryRO entry = zip->findEntryByName(name);
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700436 ALOGE_IF(!entry, "couldn't find %s", name);
437 if (!entry) {
438 return false;
439 }
440
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700441 FileMap* entryMap = zip->createEntryFileMap(entry);
442 zip->releaseEntry(entry);
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700443 ALOGE_IF(!entryMap, "entryMap is null");
444 if (!entryMap) {
445 return false;
446 }
447
448 outString.setTo((char const*)entryMap->getDataPtr(), entryMap->getDataLength());
Narayan Kamath688ff4c2015-02-23 15:47:54 +0000449 delete entryMap;
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700450 return true;
451}
452
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800453// The time glyphs are stored in a single image of height 64 pixels. Each digit is 40 pixels wide,
454// and the colon character is half that at 20 pixels. The glyph order is '0123456789:'.
455// We render 24 hour time.
456void BootAnimation::drawTime(const Texture& clockTex, const int yPos) {
457 static constexpr char TIME_FORMAT[] = "%H:%M";
458 static constexpr int TIME_LENGTH = sizeof(TIME_FORMAT);
459
460 static constexpr int DIGIT_HEIGHT = 64;
461 static constexpr int DIGIT_WIDTH = 40;
462 static constexpr int COLON_WIDTH = DIGIT_WIDTH / 2;
463 static constexpr int TIME_WIDTH = (DIGIT_WIDTH * 4) + COLON_WIDTH;
464
465 if (clockTex.h < DIGIT_HEIGHT || clockTex.w < (10 * DIGIT_WIDTH + COLON_WIDTH)) {
466 ALOGE("Clock texture is too small; abandoning boot animation clock");
467 mClockEnabled = false;
468 return;
469 }
470
471 time_t rawtime;
472 time(&rawtime);
473 struct tm* timeInfo = localtime(&rawtime);
474
475 char timeBuff[TIME_LENGTH];
476 size_t length = strftime(timeBuff, TIME_LENGTH, TIME_FORMAT, timeInfo);
477
478 if (length != TIME_LENGTH - 1) {
479 ALOGE("Couldn't format time; abandoning boot animation clock");
480 mClockEnabled = false;
481 return;
482 }
483
484 glEnable(GL_BLEND); // Allow us to draw on top of the animation
485 glBindTexture(GL_TEXTURE_2D, clockTex.name);
486
487 int xPos = (mWidth - TIME_WIDTH) / 2;
488 int cropRect[4] = { 0, DIGIT_HEIGHT, DIGIT_WIDTH, -DIGIT_HEIGHT };
489
490 for (int i = 0; i < TIME_LENGTH - 1; i++) {
491 char c = timeBuff[i];
492 int width = DIGIT_WIDTH;
493 int pos = c - '0'; // Position in the character list
494 if (pos < 0 || pos > 10) {
495 continue;
496 }
497 if (c == ':') {
498 width = COLON_WIDTH;
499 }
500
501 // Crop the texture to only the pixels in the current glyph
502 int left = pos * DIGIT_WIDTH;
503 cropRect[0] = left;
504 cropRect[2] = width;
505 glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_CROP_RECT_OES, cropRect);
506
507 glDrawTexiOES(xPos, yPos, 0, width, DIGIT_HEIGHT);
508
509 xPos += width;
510 }
511
512 glDisable(GL_BLEND); // Return to the animation's default behaviour
513 glBindTexture(GL_TEXTURE_2D, 0);
514}
515
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700516bool BootAnimation::parseAnimationDesc(Animation& animation)
Mathias Agopiana8826d62009-10-01 03:10:14 -0700517{
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700518 String8 desString;
519
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700520 if (!readFile(animation.zip, "desc.txt", desString)) {
Narayan Kamathafd31e02013-12-03 13:16:03 +0000521 return false;
522 }
Mathias Agopiana8826d62009-10-01 03:10:14 -0700523 char const* s = desString.string();
524
Mathias Agopiana8826d62009-10-01 03:10:14 -0700525 // Parse the description file
526 for (;;) {
527 const char* endl = strstr(s, "\n");
Andreas Gampecfedceb2014-09-30 21:48:18 -0700528 if (endl == NULL) break;
Mathias Agopiana8826d62009-10-01 03:10:14 -0700529 String8 line(s, endl - s);
530 const char* l = line.string();
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800531 int fps = 0;
532 int width = 0;
533 int height = 0;
534 int count = 0;
535 int pause = 0;
536 int clockPosY = -1;
Narayan Kamathafd31e02013-12-03 13:16:03 +0000537 char path[ANIM_ENTRY_NAME_MAX];
Jesse Hall083b84c2014-09-22 10:51:09 -0700538 char color[7] = "000000"; // default to black if unspecified
539
Kevin Hesterd3782b22012-04-26 10:38:55 -0700540 char pathType;
Mathias Agopiana8826d62009-10-01 03:10:14 -0700541 if (sscanf(l, "%d %d %d", &width, &height, &fps) == 3) {
Jesse Hall083b84c2014-09-22 10:51:09 -0700542 // ALOGD("> w=%d, h=%d, fps=%d", width, height, fps);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700543 animation.width = width;
544 animation.height = height;
545 animation.fps = fps;
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800546 } else if (sscanf(l, " %c %d %d %s #%6s %d",
547 &pathType, &count, &pause, path, color, &clockPosY) >= 4) {
548 // 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 -0700549 Animation::Part part;
Kevin Hesterd3782b22012-04-26 10:38:55 -0700550 part.playUntilComplete = pathType == 'c';
Mathias Agopiana8826d62009-10-01 03:10:14 -0700551 part.count = count;
552 part.pause = pause;
553 part.path = path;
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800554 part.clockPosY = clockPosY;
Geoffrey Pitschd6d9a1d2016-06-08 00:38:58 -0700555 part.audioData = NULL;
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700556 part.animation = NULL;
Jesse Hall083b84c2014-09-22 10:51:09 -0700557 if (!parseColor(color, part.backgroundColor)) {
558 ALOGE("> invalid color '#%s'", color);
559 part.backgroundColor[0] = 0.0f;
560 part.backgroundColor[1] = 0.0f;
561 part.backgroundColor[2] = 0.0f;
562 }
Mathias Agopiana8826d62009-10-01 03:10:14 -0700563 animation.parts.add(part);
564 }
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700565 else if (strcmp(l, "$SYSTEM") == 0) {
566 // ALOGD("> SYSTEM");
567 Animation::Part part;
568 part.playUntilComplete = false;
569 part.count = 1;
570 part.pause = 0;
Geoffrey Pitschd6d9a1d2016-06-08 00:38:58 -0700571 part.audioData = NULL;
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700572 part.animation = loadAnimation(String8(SYSTEM_BOOTANIMATION_FILE));
573 if (part.animation != NULL)
574 animation.parts.add(part);
575 }
Mathias Agopiana8826d62009-10-01 03:10:14 -0700576 s = ++endl;
577 }
578
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700579 return true;
580}
581
582bool BootAnimation::preloadZip(Animation& animation)
583{
Mathias Agopiana8826d62009-10-01 03:10:14 -0700584 // read all the data structures
585 const size_t pcount = animation.parts.size();
Narayan Kamathafd31e02013-12-03 13:16:03 +0000586 void *cookie = NULL;
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400587 ZipFileRO* zip = animation.zip;
588 if (!zip->startIteration(&cookie)) {
Narayan Kamathafd31e02013-12-03 13:16:03 +0000589 return false;
590 }
591
Geoffrey Pitscha91a2d72016-07-12 14:46:19 -0400592 Animation::Part* partWithAudio = NULL;
Narayan Kamathafd31e02013-12-03 13:16:03 +0000593 ZipEntryRO entry;
594 char name[ANIM_ENTRY_NAME_MAX];
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400595 while ((entry = zip->nextEntry(cookie)) != NULL) {
596 const int foundEntryName = zip->getEntryFileName(entry, name, ANIM_ENTRY_NAME_MAX);
Narayan Kamathafd31e02013-12-03 13:16:03 +0000597 if (foundEntryName > ANIM_ENTRY_NAME_MAX || foundEntryName == -1) {
598 ALOGE("Error fetching entry file name");
599 continue;
600 }
601
602 const String8 entryName(name);
603 const String8 path(entryName.getPathDir());
604 const String8 leaf(entryName.getPathLeaf());
605 if (leaf.size() > 0) {
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400606 for (size_t j = 0; j < pcount; j++) {
Narayan Kamathafd31e02013-12-03 13:16:03 +0000607 if (path == animation.parts[j].path) {
Narayan Kamath4600dd02015-06-16 12:02:57 +0100608 uint16_t method;
Narayan Kamathafd31e02013-12-03 13:16:03 +0000609 // supports only stored png files
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400610 if (zip->getEntryInfo(entry, &method, NULL, NULL, NULL, NULL, NULL)) {
Narayan Kamathafd31e02013-12-03 13:16:03 +0000611 if (method == ZipFileRO::kCompressStored) {
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400612 FileMap* map = zip->createEntryFileMap(entry);
Narayan Kamathafd31e02013-12-03 13:16:03 +0000613 if (map) {
Narayan Kamathafd31e02013-12-03 13:16:03 +0000614 Animation::Part& part(animation.parts.editItemAt(j));
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700615 if (leaf == "audio.wav") {
616 // a part may have at most one audio file
Geoffrey Pitschd6d9a1d2016-06-08 00:38:58 -0700617 part.audioData = (uint8_t *)map->getDataPtr();
618 part.audioLength = map->getDataLength();
Geoffrey Pitscha91a2d72016-07-12 14:46:19 -0400619 partWithAudio = &part;
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400620 } else if (leaf == "trim.txt") {
621 part.trimData.setTo((char const*)map->getDataPtr(),
622 map->getDataLength());
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700623 } else {
624 Animation::Frame frame;
625 frame.name = leaf;
626 frame.map = map;
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400627 frame.trimWidth = animation.width;
628 frame.trimHeight = animation.height;
629 frame.trimX = 0;
630 frame.trimY = 0;
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700631 part.frames.add(frame);
632 }
Mathias Agopiana8826d62009-10-01 03:10:14 -0700633 }
Geoffrey Pitschd6d9a1d2016-06-08 00:38:58 -0700634 } else {
635 ALOGE("bootanimation.zip is compressed; must be only stored");
Mathias Agopiana8826d62009-10-01 03:10:14 -0700636 }
637 }
638 }
639 }
640 }
641 }
642
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400643 // If there is trimData present, override the positioning defaults.
644 for (Animation::Part& part : animation.parts) {
645 const char* trimDataStr = part.trimData.string();
646 for (size_t frameIdx = 0; frameIdx < part.frames.size(); frameIdx++) {
647 const char* endl = strstr(trimDataStr, "\n");
648 // No more trimData for this part.
649 if (endl == NULL) {
650 break;
651 }
652 String8 line(trimDataStr, endl - trimDataStr);
653 const char* lineStr = line.string();
654 trimDataStr = ++endl;
655 int width = 0, height = 0, x = 0, y = 0;
656 if (sscanf(lineStr, "%dx%d+%d+%d", &width, &height, &x, &y) == 4) {
657 Animation::Frame& frame(part.frames.editItemAt(frameIdx));
658 frame.trimWidth = width;
659 frame.trimHeight = height;
660 frame.trimX = x;
661 frame.trimY = y;
662 } else {
663 ALOGE("Error parsing trim.txt, line: %s", lineStr);
664 break;
665 }
666 }
667 }
668
Geoffrey Pitschd6d9a1d2016-06-08 00:38:58 -0700669 // Create and initialize audioplay if there is a wav file in any of the animations.
Geoffrey Pitscha91a2d72016-07-12 14:46:19 -0400670 if (partWithAudio != NULL) {
Geoffrey Pitschd6d9a1d2016-06-08 00:38:58 -0700671 ALOGD("found audio.wav, creating playback engine");
Geoffrey Pitscha91a2d72016-07-12 14:46:19 -0400672 if (!audioplay::create(partWithAudio->audioData, partWithAudio->audioLength)) {
673 return false;
674 }
Geoffrey Pitschd6d9a1d2016-06-08 00:38:58 -0700675 }
676
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400677 zip->endIteration(cookie);
Narayan Kamathafd31e02013-12-03 13:16:03 +0000678
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700679 return true;
680}
681
682bool BootAnimation::movie()
683{
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700684 Animation* animation = loadAnimation(mZipFileName);
685 if (animation == NULL)
686 return false;
687
Damien Bargiacchi97480862016-03-29 14:55:55 -0700688 bool anyPartHasClock = false;
689 for (size_t i=0; i < animation->parts.size(); i++) {
690 if(animation->parts[i].clockPosY >= 0) {
691 anyPartHasClock = true;
692 break;
693 }
694 }
695 if (!anyPartHasClock) {
696 mClockEnabled = false;
697 }
698
Sai Kiran Korwar27167492015-07-07 20:00:06 +0530699 // Check if npot textures are supported
700 mUseNpotTextures = false;
701 String8 gl_extensions;
702 const char* exts = reinterpret_cast<const char*>(glGetString(GL_EXTENSIONS));
703 if (!exts) {
704 glGetError();
705 } else {
706 gl_extensions.setTo(exts);
707 if ((gl_extensions.find("GL_ARB_texture_non_power_of_two") != -1) ||
708 (gl_extensions.find("GL_OES_texture_npot") != -1)) {
709 mUseNpotTextures = true;
710 }
711 }
712
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800713 // Blend required to draw time on top of animation frames.
714 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700715 glShadeModel(GL_FLAT);
716 glDisable(GL_DITHER);
717 glDisable(GL_SCISSOR_TEST);
718 glDisable(GL_BLEND);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700719
720 glBindTexture(GL_TEXTURE_2D, 0);
721 glEnable(GL_TEXTURE_2D);
722 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
723 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
724 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
725 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
726 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
727
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800728 bool clockTextureInitialized = false;
729 if (mClockEnabled) {
730 clockTextureInitialized = (initTexture(&mClock, mAssets, "images/clock64.png") == NO_ERROR);
731 mClockEnabled = clockTextureInitialized;
732 }
733
Damien Bargiacchi97480862016-03-29 14:55:55 -0700734 if (mClockEnabled && !updateIsTimeAccurate()) {
735 mTimeCheckThread = new TimeCheckThread(this);
736 mTimeCheckThread->run("BootAnimation::TimeCheckThread", PRIORITY_NORMAL);
737 }
738
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700739 playAnimation(*animation);
Damien Bargiacchi97480862016-03-29 14:55:55 -0700740
741 if (mTimeCheckThread != NULL) {
742 mTimeCheckThread->requestExit();
743 mTimeCheckThread = NULL;
744 }
745
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700746 releaseAnimation(animation);
747
Andriy Naborskyy815e51d2016-03-24 16:43:34 -0700748 if (clockTextureInitialized) {
749 glDeleteTextures(1, &mClock.name);
750 }
751
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700752 return false;
753}
754
755bool BootAnimation::playAnimation(const Animation& animation)
756{
757 const size_t pcount = animation.parts.size();
Mathias Agopiana8826d62009-10-01 03:10:14 -0700758 nsecs_t frameDuration = s2ns(1) / animation.fps;
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400759 const int animationX = (mWidth - animation.width) / 2;
760 const int animationY = (mHeight - animation.height) / 2;
Mathias Agopian9f3020d2009-11-06 16:30:18 -0800761
Narayan Kamathafd31e02013-12-03 13:16:03 +0000762 for (size_t i=0 ; i<pcount ; i++) {
Mathias Agopiana8826d62009-10-01 03:10:14 -0700763 const Animation::Part& part(animation.parts[i]);
764 const size_t fcount = part.frames.size();
765 glBindTexture(GL_TEXTURE_2D, 0);
766
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700767 // Handle animation package
768 if (part.animation != NULL) {
769 playAnimation(*part.animation);
770 if (exitPending())
771 break;
772 continue; //to next part
773 }
774
Mathias Agopiana8826d62009-10-01 03:10:14 -0700775 for (int r=0 ; !part.count || r<part.count ; r++) {
Kevin Hesterd3782b22012-04-26 10:38:55 -0700776 // Exit any non playuntil complete parts immediately
777 if(exitPending() && !part.playUntilComplete)
778 break;
779
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700780 // only play audio file the first time we animate the part
Geoffrey Pitschd6d9a1d2016-06-08 00:38:58 -0700781 if (r == 0 && part.audioData) {
Geoffrey Pitsch30508792016-07-22 17:04:21 -0400782 // Read the system property to see if we should play the sound.
783 // If not present, default to playing it.
784 if (property_get_bool(PLAY_SOUND_PROP_NAME, 1)) {
785 ALOGD("playing clip for part%d, size=%d", (int) i, part.audioLength);
786 audioplay::playClip(part.audioData, part.audioLength);
787 }
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700788 }
789
Jesse Hall083b84c2014-09-22 10:51:09 -0700790 glClearColor(
791 part.backgroundColor[0],
792 part.backgroundColor[1],
793 part.backgroundColor[2],
794 1.0f);
795
Narayan Kamathafd31e02013-12-03 13:16:03 +0000796 for (size_t j=0 ; j<fcount && (!exitPending() || part.playUntilComplete) ; j++) {
Mathias Agopiana8826d62009-10-01 03:10:14 -0700797 const Animation::Frame& frame(part.frames[j]);
Mathias Agopiandb7dd2a2012-05-12 15:08:21 -0700798 nsecs_t lastFrame = systemTime();
Mathias Agopiana8826d62009-10-01 03:10:14 -0700799
800 if (r > 0) {
801 glBindTexture(GL_TEXTURE_2D, frame.tid);
802 } else {
803 if (part.count != 1) {
804 glGenTextures(1, &frame.tid);
805 glBindTexture(GL_TEXTURE_2D, frame.tid);
806 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
807 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
808 }
Mykola Kondratenko0c1eeb32014-04-15 09:35:44 +0200809 initTexture(frame);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700810 }
811
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400812 const int xc = animationX + frame.trimX;
813 const int yc = animationY + frame.trimY;
814 Region clearReg(Rect(mWidth, mHeight));
815 clearReg.subtractSelf(Rect(xc, yc, xc+frame.trimWidth, yc+frame.trimHeight));
Mathias Agopian9f3020d2009-11-06 16:30:18 -0800816 if (!clearReg.isEmpty()) {
817 Region::const_iterator head(clearReg.begin());
818 Region::const_iterator tail(clearReg.end());
819 glEnable(GL_SCISSOR_TEST);
820 while (head != tail) {
Andreas Gampecfedceb2014-09-30 21:48:18 -0700821 const Rect& r2(*head++);
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400822 glScissor(r2.left, mHeight - r2.bottom, r2.width(), r2.height());
Mathias Agopian9f3020d2009-11-06 16:30:18 -0800823 glClear(GL_COLOR_BUFFER_BIT);
824 }
825 glDisable(GL_SCISSOR_TEST);
826 }
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400827 // specify the y center as ceiling((mHeight - frame.trimHeight) / 2)
828 // which is equivalent to mHeight - (yc + frame.trimHeight)
829 glDrawTexiOES(xc, mHeight - (yc + frame.trimHeight),
830 0, frame.trimWidth, frame.trimHeight);
Damien Bargiacchi97480862016-03-29 14:55:55 -0700831 if (mClockEnabled && mTimeIsAccurate && part.clockPosY >= 0) {
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800832 drawTime(mClock, part.clockPosY);
833 }
834
Mathias Agopiana8826d62009-10-01 03:10:14 -0700835 eglSwapBuffers(mDisplay, mSurface);
836
837 nsecs_t now = systemTime();
838 nsecs_t delay = frameDuration - (now - lastFrame);
Mathias Agopiandb7dd2a2012-05-12 15:08:21 -0700839 //ALOGD("%lld, %lld", ns2ms(now - lastFrame), ns2ms(delay));
Mathias Agopiana8826d62009-10-01 03:10:14 -0700840 lastFrame = now;
Mathias Agopiandb7dd2a2012-05-12 15:08:21 -0700841
842 if (delay > 0) {
843 struct timespec spec;
844 spec.tv_sec = (now + delay) / 1000000000;
845 spec.tv_nsec = (now + delay) % 1000000000;
846 int err;
847 do {
848 err = clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &spec, NULL);
849 } while (err<0 && errno == EINTR);
850 }
Kevin Hesterd3782b22012-04-26 10:38:55 -0700851
852 checkExit();
Mathias Agopiana8826d62009-10-01 03:10:14 -0700853 }
Kevin Hesterd3782b22012-04-26 10:38:55 -0700854
Mathias Agopiana8826d62009-10-01 03:10:14 -0700855 usleep(part.pause * ns2us(frameDuration));
Kevin Hesterd3782b22012-04-26 10:38:55 -0700856
857 // For infinite parts, we've now played them at least once, so perhaps exit
858 if(exitPending() && !part.count)
859 break;
Mathias Agopiana8826d62009-10-01 03:10:14 -0700860 }
861
Geoffrey Pitsch2fb30fb2016-07-06 16:16:20 -0400862 }
863
864 // Free textures created for looping parts now that the animation is done.
865 for (const Animation::Part& part : animation.parts) {
Mathias Agopiana8826d62009-10-01 03:10:14 -0700866 if (part.count != 1) {
Geoffrey Pitsch2fb30fb2016-07-06 16:16:20 -0400867 const size_t fcount = part.frames.size();
868 for (size_t j = 0; j < fcount; j++) {
Mathias Agopiana8826d62009-10-01 03:10:14 -0700869 const Animation::Frame& frame(part.frames[j]);
870 glDeleteTextures(1, &frame.tid);
871 }
872 }
873 }
Geoffrey Pitschd6d9a1d2016-06-08 00:38:58 -0700874
875 // we've finally played everything we're going to play
876 audioplay::setPlaying(false);
877 audioplay::destroy();
878
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700879 return true;
Mathias Agopiana8826d62009-10-01 03:10:14 -0700880}
881
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700882void BootAnimation::releaseAnimation(Animation* animation) const
883{
884 for (Vector<Animation::Part>::iterator it = animation->parts.begin(),
885 e = animation->parts.end(); it != e; ++it) {
886 if (it->animation)
887 releaseAnimation(it->animation);
888 }
889 if (animation->zip)
890 delete animation->zip;
891 delete animation;
892}
893
894BootAnimation::Animation* BootAnimation::loadAnimation(const String8& fn)
895{
896 if (mLoadedFiles.indexOf(fn) >= 0) {
897 ALOGE("File \"%s\" is already loaded. Cyclic ref is not allowed",
898 fn.string());
899 return NULL;
900 }
901 ZipFileRO *zip = ZipFileRO::open(fn);
902 if (zip == NULL) {
903 ALOGE("Failed to open animation zip \"%s\": %s",
904 fn.string(), strerror(errno));
905 return NULL;
906 }
907
908 Animation *animation = new Animation;
909 animation->fileName = fn;
910 animation->zip = zip;
911 mLoadedFiles.add(animation->fileName);
912
913 parseAnimationDesc(*animation);
Geoffrey Pitscha91a2d72016-07-12 14:46:19 -0400914 if (!preloadZip(*animation)) {
915 return NULL;
916 }
917
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700918
919 mLoadedFiles.remove(fn);
920 return animation;
921}
Damien Bargiacchi97480862016-03-29 14:55:55 -0700922
923bool BootAnimation::updateIsTimeAccurate() {
924 static constexpr long long MAX_TIME_IN_PAST = 60000LL * 60LL * 24LL * 30LL; // 30 days
925 static constexpr long long MAX_TIME_IN_FUTURE = 60000LL * 90LL; // 90 minutes
926
927 if (mTimeIsAccurate) {
928 return true;
929 }
930
931 struct stat statResult;
932 if(stat(ACCURATE_TIME_FLAG_FILE_PATH, &statResult) == 0) {
933 mTimeIsAccurate = true;
934 return true;
935 }
936
937 FILE* file = fopen(LAST_TIME_CHANGED_FILE_PATH, "r");
938 if (file != NULL) {
939 long long lastChangedTime = 0;
940 fscanf(file, "%lld", &lastChangedTime);
941 fclose(file);
942 if (lastChangedTime > 0) {
943 struct timespec now;
944 clock_gettime(CLOCK_REALTIME, &now);
945 // Match the Java timestamp format
946 long long rtcNow = (now.tv_sec * 1000LL) + (now.tv_nsec / 1000000LL);
Damien Bargiacchi96762812016-07-12 15:53:40 -0700947 if (ACCURATE_TIME_EPOCH < rtcNow
948 && lastChangedTime > (rtcNow - MAX_TIME_IN_PAST)
949 && lastChangedTime < (rtcNow + MAX_TIME_IN_FUTURE)) {
Damien Bargiacchi97480862016-03-29 14:55:55 -0700950 mTimeIsAccurate = true;
951 }
952 }
953 }
954
955 return mTimeIsAccurate;
956}
957
958BootAnimation::TimeCheckThread::TimeCheckThread(BootAnimation* bootAnimation) : Thread(false),
959 mInotifyFd(-1), mSystemWd(-1), mTimeWd(-1), mBootAnimation(bootAnimation) {}
960
961BootAnimation::TimeCheckThread::~TimeCheckThread() {
962 // mInotifyFd may be -1 but that's ok since we're not at risk of attempting to close a valid FD.
963 close(mInotifyFd);
964}
965
966bool BootAnimation::TimeCheckThread::threadLoop() {
967 bool shouldLoop = doThreadLoop() && !mBootAnimation->mTimeIsAccurate
968 && mBootAnimation->mClockEnabled;
969 if (!shouldLoop) {
970 close(mInotifyFd);
971 mInotifyFd = -1;
972 }
973 return shouldLoop;
974}
975
976bool BootAnimation::TimeCheckThread::doThreadLoop() {
977 static constexpr int BUFF_LEN (10 * (sizeof(struct inotify_event) + NAME_MAX + 1));
978
979 // Poll instead of doing a blocking read so the Thread can exit if requested.
980 struct pollfd pfd = { mInotifyFd, POLLIN, 0 };
981 ssize_t pollResult = poll(&pfd, 1, 1000);
982
983 if (pollResult == 0) {
984 return true;
985 } else if (pollResult < 0) {
986 ALOGE("Could not poll inotify events");
987 return false;
988 }
989
990 char buff[BUFF_LEN] __attribute__ ((aligned(__alignof__(struct inotify_event))));;
991 ssize_t length = read(mInotifyFd, buff, BUFF_LEN);
992 if (length == 0) {
993 return true;
994 } else if (length < 0) {
995 ALOGE("Could not read inotify events");
996 return false;
997 }
998
999 const struct inotify_event *event;
1000 for (char* ptr = buff; ptr < buff + length; ptr += sizeof(struct inotify_event) + event->len) {
1001 event = (const struct inotify_event *) ptr;
1002 if (event->wd == mSystemWd && strcmp(SYSTEM_TIME_DIR_NAME, event->name) == 0) {
1003 addTimeDirWatch();
1004 } else if (event->wd == mTimeWd && (strcmp(LAST_TIME_CHANGED_FILE_NAME, event->name) == 0
1005 || strcmp(ACCURATE_TIME_FLAG_FILE_NAME, event->name) == 0)) {
1006 return !mBootAnimation->updateIsTimeAccurate();
1007 }
1008 }
1009
1010 return true;
1011}
1012
1013void BootAnimation::TimeCheckThread::addTimeDirWatch() {
1014 mTimeWd = inotify_add_watch(mInotifyFd, SYSTEM_TIME_DIR_PATH,
1015 IN_CLOSE_WRITE | IN_MOVED_TO | IN_ATTRIB);
1016 if (mTimeWd > 0) {
1017 // No need to watch for the time directory to be created if it already exists
1018 inotify_rm_watch(mInotifyFd, mSystemWd);
1019 mSystemWd = -1;
1020 }
1021}
1022
1023status_t BootAnimation::TimeCheckThread::readyToRun() {
1024 mInotifyFd = inotify_init();
1025 if (mInotifyFd < 0) {
1026 ALOGE("Could not initialize inotify fd");
1027 return NO_INIT;
1028 }
1029
1030 mSystemWd = inotify_add_watch(mInotifyFd, SYSTEM_DATA_DIR_PATH, IN_CREATE | IN_ATTRIB);
1031 if (mSystemWd < 0) {
1032 close(mInotifyFd);
1033 mInotifyFd = -1;
1034 ALOGE("Could not add watch for %s", SYSTEM_DATA_DIR_PATH);
1035 return NO_INIT;
1036 }
1037
1038 addTimeDirWatch();
1039
1040 if (mBootAnimation->updateIsTimeAccurate()) {
1041 close(mInotifyFd);
1042 mInotifyFd = -1;
1043 return ALREADY_EXISTS;
1044 }
1045
1046 return NO_ERROR;
1047}
1048
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001049// ---------------------------------------------------------------------------
1050
1051}
1052; // namespace android