blob: 3e94e1b151735cd48d55a6add1e1dca2c609e6f7 [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";
Narayan Kamathafd31e02013-12-03 13:16:03 +000078static const int ANIM_ENTRY_NAME_MAX = 256;
79
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080080// ---------------------------------------------------------------------------
81
Damien Bargiacchi97480862016-03-29 14:55:55 -070082BootAnimation::BootAnimation() : Thread(false), mClockEnabled(true), mTimeIsAccurate(false),
83 mTimeCheckThread(NULL) {
Mathias Agopian627e7b52009-05-21 19:21:59 -070084 mSession = new SurfaceComposerClient();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080085}
86
Damien Bargiacchi97480862016-03-29 14:55:55 -070087BootAnimation::~BootAnimation() {}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080088
89void BootAnimation::onFirstRef() {
Mathias Agopianbc726112009-09-23 15:44:05 -070090 status_t err = mSession->linkToComposerDeath(this);
Steve Block3762c312012-01-06 19:20:56 +000091 ALOGE_IF(err, "linkToComposerDeath failed (%s) ", strerror(-err));
Mathias Agopian8434c532009-09-23 18:52:49 -070092 if (err == NO_ERROR) {
Mathias Agopianbc726112009-09-23 15:44:05 -070093 run("BootAnimation", PRIORITY_DISPLAY);
94 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080095}
96
Mathias Agopianbc726112009-09-23 15:44:05 -070097sp<SurfaceComposerClient> BootAnimation::session() const {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080098 return mSession;
99}
100
Mathias Agopianbc726112009-09-23 15:44:05 -0700101
Narayan Kamathafd31e02013-12-03 13:16:03 +0000102void BootAnimation::binderDied(const wp<IBinder>&)
Mathias Agopianbc726112009-09-23 15:44:05 -0700103{
104 // woah, surfaceflinger died!
Steve Block5baa3a62011-12-20 16:23:08 +0000105 ALOGD("SurfaceFlinger died, exiting...");
Mathias Agopianbc726112009-09-23 15:44:05 -0700106
107 // calling requestExit() is not enough here because the Surface code
108 // might be blocked on a condition variable that will never be updated.
109 kill( getpid(), SIGKILL );
110 requestExit();
Geoffrey Pitschd6d9a1d2016-06-08 00:38:58 -0700111 audioplay::destroy();
Mathias Agopianbc726112009-09-23 15:44:05 -0700112}
113
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800114status_t BootAnimation::initTexture(Texture* texture, AssetManager& assets,
115 const char* name) {
116 Asset* asset = assets.open(name, Asset::ACCESS_BUFFER);
Andreas Gampecfedceb2014-09-30 21:48:18 -0700117 if (asset == NULL)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800118 return NO_INIT;
119 SkBitmap bitmap;
Matt Sarett8898c162016-03-24 16:11:01 -0400120 sk_sp<SkData> data = SkData::MakeWithoutCopy(asset->getBuffer(false),
121 asset->getLength());
122 sk_sp<SkImage> image = SkImage::MakeFromEncoded(data);
123 image->asLegacyBitmap(&bitmap, SkImage::kRO_LegacyBitmapMode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800124 asset->close();
125 delete asset;
126
127 // ensure we can call getPixels(). No need to call unlock, since the
128 // bitmap will go out of scope when we return from this method.
129 bitmap.lockPixels();
130
131 const int w = bitmap.width();
132 const int h = bitmap.height();
133 const void* p = bitmap.getPixels();
134
135 GLint crop[4] = { 0, h, w, -h };
136 texture->w = w;
137 texture->h = h;
138
139 glGenTextures(1, &texture->name);
140 glBindTexture(GL_TEXTURE_2D, texture->name);
141
Mike Reed42a1d082014-07-07 18:06:18 -0400142 switch (bitmap.colorType()) {
143 case kAlpha_8_SkColorType:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800144 glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, w, h, 0, GL_ALPHA,
145 GL_UNSIGNED_BYTE, p);
146 break;
Mike Reed42a1d082014-07-07 18:06:18 -0400147 case kARGB_4444_SkColorType:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800148 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA,
149 GL_UNSIGNED_SHORT_4_4_4_4, p);
150 break;
Mike Reed42a1d082014-07-07 18:06:18 -0400151 case kN32_SkColorType:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800152 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA,
153 GL_UNSIGNED_BYTE, p);
154 break;
Mike Reed42a1d082014-07-07 18:06:18 -0400155 case kRGB_565_SkColorType:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800156 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, w, h, 0, GL_RGB,
157 GL_UNSIGNED_SHORT_5_6_5, p);
158 break;
159 default:
160 break;
161 }
162
163 glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_CROP_RECT_OES, crop);
164 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
165 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
166 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
167 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
168 return NO_ERROR;
169}
170
Mykola Kondratenko0c1eeb32014-04-15 09:35:44 +0200171status_t BootAnimation::initTexture(const Animation::Frame& frame)
Mathias Agopiana8826d62009-10-01 03:10:14 -0700172{
173 //StopWatch watch("blah");
174
175 SkBitmap bitmap;
Matt Sarett8898c162016-03-24 16:11:01 -0400176 sk_sp<SkData> data = SkData::MakeWithoutCopy(frame.map->getDataPtr(),
177 frame.map->getDataLength());
178 sk_sp<SkImage> image = SkImage::MakeFromEncoded(data);
179 image->asLegacyBitmap(&bitmap, SkImage::kRO_LegacyBitmapMode);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700180
Mykola Kondratenko0c1eeb32014-04-15 09:35:44 +0200181 // FileMap memory is never released until application exit.
182 // Release it now as the texture is already loaded and the memory used for
183 // the packed resource can be released.
Narayan Kamath688ff4c2015-02-23 15:47:54 +0000184 delete frame.map;
Mykola Kondratenko0c1eeb32014-04-15 09:35:44 +0200185
Mathias Agopiana8826d62009-10-01 03:10:14 -0700186 // ensure we can call getPixels(). No need to call unlock, since the
187 // bitmap will go out of scope when we return from this method.
188 bitmap.lockPixels();
189
190 const int w = bitmap.width();
191 const int h = bitmap.height();
192 const void* p = bitmap.getPixels();
193
194 GLint crop[4] = { 0, h, w, -h };
195 int tw = 1 << (31 - __builtin_clz(w));
196 int th = 1 << (31 - __builtin_clz(h));
197 if (tw < w) tw <<= 1;
198 if (th < h) th <<= 1;
199
Mike Reed42a1d082014-07-07 18:06:18 -0400200 switch (bitmap.colorType()) {
201 case kN32_SkColorType:
Sai Kiran Korwar27167492015-07-07 20:00:06 +0530202 if (!mUseNpotTextures && (tw != w || th != h)) {
Mathias Agopiana8826d62009-10-01 03:10:14 -0700203 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, tw, th, 0, GL_RGBA,
204 GL_UNSIGNED_BYTE, 0);
205 glTexSubImage2D(GL_TEXTURE_2D, 0,
206 0, 0, w, h, GL_RGBA, GL_UNSIGNED_BYTE, p);
207 } else {
Sai Kiran Korwar27167492015-07-07 20:00:06 +0530208 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA,
Mathias Agopiana8826d62009-10-01 03:10:14 -0700209 GL_UNSIGNED_BYTE, p);
210 }
211 break;
212
Mike Reed42a1d082014-07-07 18:06:18 -0400213 case kRGB_565_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_RGB, tw, th, 0, GL_RGB,
216 GL_UNSIGNED_SHORT_5_6_5, 0);
217 glTexSubImage2D(GL_TEXTURE_2D, 0,
218 0, 0, w, h, GL_RGB, GL_UNSIGNED_SHORT_5_6_5, p);
219 } else {
Sai Kiran Korwar27167492015-07-07 20:00:06 +0530220 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, w, h, 0, GL_RGB,
Mathias Agopiana8826d62009-10-01 03:10:14 -0700221 GL_UNSIGNED_SHORT_5_6_5, p);
222 }
223 break;
224 default:
225 break;
226 }
227
228 glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_CROP_RECT_OES, crop);
229
230 return NO_ERROR;
231}
232
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800233status_t BootAnimation::readyToRun() {
234 mAssets.addDefaultAssets();
235
Jeff Brown0b722fe2012-08-24 22:40:14 -0700236 sp<IBinder> dtoken(SurfaceComposerClient::getBuiltInDisplay(
237 ISurfaceComposer::eDisplayIdMain));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800238 DisplayInfo dinfo;
Jeff Brown0b722fe2012-08-24 22:40:14 -0700239 status_t status = SurfaceComposerClient::getDisplayInfo(dtoken, &dinfo);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800240 if (status)
241 return -1;
242
243 // create the native surface
Jeff Brown0b722fe2012-08-24 22:40:14 -0700244 sp<SurfaceControl> control = session()->createSurface(String8("BootAnimation"),
245 dinfo.w, dinfo.h, PIXEL_FORMAT_RGB_565);
Mathias Agopian439863f2011-06-28 19:09:31 -0700246
247 SurfaceComposerClient::openGlobalTransaction();
Mathias Agopian17f638b2009-04-16 20:04:08 -0700248 control->setLayer(0x40000000);
Mathias Agopian439863f2011-06-28 19:09:31 -0700249 SurfaceComposerClient::closeGlobalTransaction();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800250
Mathias Agopian17f638b2009-04-16 20:04:08 -0700251 sp<Surface> s = control->getSurface();
252
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800253 // initialize opengl and egl
Mathias Agopian738b9a42009-08-06 16:41:02 -0700254 const EGLint attribs[] = {
Mathias Agopian1b253b72011-08-15 15:20:22 -0700255 EGL_RED_SIZE, 8,
256 EGL_GREEN_SIZE, 8,
257 EGL_BLUE_SIZE, 8,
Mathias Agopiana8826d62009-10-01 03:10:14 -0700258 EGL_DEPTH_SIZE, 0,
259 EGL_NONE
Mathias Agopian738b9a42009-08-06 16:41:02 -0700260 };
Andreas Gampecfedceb2014-09-30 21:48:18 -0700261 EGLint w, h;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800262 EGLint numConfigs;
263 EGLConfig config;
264 EGLSurface surface;
265 EGLContext context;
Mathias Agopian627e7b52009-05-21 19:21:59 -0700266
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800267 EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
Mathias Agopian627e7b52009-05-21 19:21:59 -0700268
269 eglInitialize(display, 0, 0);
Mathias Agopian1b253b72011-08-15 15:20:22 -0700270 eglChooseConfig(display, attribs, &config, 1, &numConfigs);
Mathias Agopian1473f462009-04-10 14:24:30 -0700271 surface = eglCreateWindowSurface(display, config, s.get(), NULL);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800272 context = eglCreateContext(display, config, NULL, NULL);
273 eglQuerySurface(display, surface, EGL_WIDTH, &w);
274 eglQuerySurface(display, surface, EGL_HEIGHT, &h);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700275
Mathias Agopianabac0102009-07-31 14:47:00 -0700276 if (eglMakeCurrent(display, surface, surface, context) == EGL_FALSE)
277 return NO_INIT;
Mathias Agopiana8826d62009-10-01 03:10:14 -0700278
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800279 mDisplay = display;
280 mContext = context;
281 mSurface = surface;
282 mWidth = w;
283 mHeight = h;
Mathias Agopian17f638b2009-04-16 20:04:08 -0700284 mFlingerSurfaceControl = control;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800285 mFlingerSurface = s;
286
Elliott Hughesc367d482013-10-29 13:12:55 -0700287 // If the device has encryption turned on or is in process
Jason parksbd9a08d2011-01-31 15:04:34 -0600288 // of being encrypted we show the encrypted boot animation.
289 char decrypt[PROPERTY_VALUE_MAX];
290 property_get("vold.decrypt", decrypt, "");
291
292 bool encryptedAnimation = atoi(decrypt) != 0 || !strcmp("trigger_restart_min_framework", decrypt);
293
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700294 if (encryptedAnimation && (access(SYSTEM_ENCRYPTED_BOOTANIMATION_FILE, R_OK) == 0)) {
295 mZipFileName = SYSTEM_ENCRYPTED_BOOTANIMATION_FILE;
Jason parksbd9a08d2011-01-31 15:04:34 -0600296 }
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700297 else if (access(OEM_BOOTANIMATION_FILE, R_OK) == 0) {
298 mZipFileName = OEM_BOOTANIMATION_FILE;
299 }
300 else if (access(SYSTEM_BOOTANIMATION_FILE, R_OK) == 0) {
301 mZipFileName = SYSTEM_BOOTANIMATION_FILE;
302 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800303 return NO_ERROR;
304}
305
Mathias Agopiana8826d62009-10-01 03:10:14 -0700306bool BootAnimation::threadLoop()
307{
308 bool r;
Narayan Kamathafd31e02013-12-03 13:16:03 +0000309 // We have no bootanimation file, so we use the stock android logo
310 // animation.
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700311 if (mZipFileName.isEmpty()) {
Mathias Agopiana8826d62009-10-01 03:10:14 -0700312 r = android();
313 } else {
314 r = movie();
315 }
316
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800317 eglMakeCurrent(mDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
318 eglDestroyContext(mDisplay, mContext);
319 eglDestroySurface(mDisplay, mSurface);
Mathias Agopian6cf0db22009-04-17 19:36:26 -0700320 mFlingerSurface.clear();
Mathias Agopian17f638b2009-04-16 20:04:08 -0700321 mFlingerSurfaceControl.clear();
Mathias Agopian627e7b52009-05-21 19:21:59 -0700322 eglTerminate(mDisplay);
323 IPCThreadState::self()->stopProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800324 return r;
325}
326
Mathias Agopiana8826d62009-10-01 03:10:14 -0700327bool BootAnimation::android()
328{
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700329 initTexture(&mAndroid[0], mAssets, "images/android-logo-mask.png");
330 initTexture(&mAndroid[1], mAssets, "images/android-logo-shine.png");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800331
332 // clear screen
Mathias Agopiana8826d62009-10-01 03:10:14 -0700333 glShadeModel(GL_FLAT);
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700334 glDisable(GL_DITHER);
335 glDisable(GL_SCISSOR_TEST);
Mathias Agopian59f19e42011-05-06 19:22:12 -0700336 glClearColor(0,0,0,1);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800337 glClear(GL_COLOR_BUFFER_BIT);
338 eglSwapBuffers(mDisplay, mSurface);
339
Mathias Agopiana8826d62009-10-01 03:10:14 -0700340 glEnable(GL_TEXTURE_2D);
341 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
342
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700343 const GLint xc = (mWidth - mAndroid[0].w) / 2;
344 const GLint yc = (mHeight - mAndroid[0].h) / 2;
345 const Rect updateRect(xc, yc, xc + mAndroid[0].w, yc + mAndroid[0].h);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800346
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800347 glScissor(updateRect.left, mHeight - updateRect.bottom, updateRect.width(),
348 updateRect.height());
349
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700350 // Blend state
351 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
352 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
353
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800354 const nsecs_t startTime = systemTime();
355 do {
Mathias Agopian13796652009-03-24 22:49:21 -0700356 nsecs_t now = systemTime();
357 double time = now - startTime;
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700358 float t = 4.0f * float(time / us2ns(16667)) / mAndroid[1].w;
359 GLint offset = (1 - (t - floorf(t))) * mAndroid[1].w;
360 GLint x = xc - offset;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800361
Mathias Agopian81668642009-07-28 11:41:30 -0700362 glDisable(GL_SCISSOR_TEST);
363 glClear(GL_COLOR_BUFFER_BIT);
364
365 glEnable(GL_SCISSOR_TEST);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800366 glDisable(GL_BLEND);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800367 glBindTexture(GL_TEXTURE_2D, mAndroid[1].name);
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700368 glDrawTexiOES(x, yc, 0, mAndroid[1].w, mAndroid[1].h);
369 glDrawTexiOES(x + mAndroid[1].w, yc, 0, mAndroid[1].w, mAndroid[1].h);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800370
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700371 glEnable(GL_BLEND);
372 glBindTexture(GL_TEXTURE_2D, mAndroid[0].name);
373 glDrawTexiOES(xc, yc, 0, mAndroid[0].w, mAndroid[0].h);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800374
Mathias Agopian627e7b52009-05-21 19:21:59 -0700375 EGLBoolean res = eglSwapBuffers(mDisplay, mSurface);
376 if (res == EGL_FALSE)
377 break;
378
Mathias Agopian13796652009-03-24 22:49:21 -0700379 // 12fps: don't animate too fast to preserve CPU
380 const nsecs_t sleepTime = 83333 - ns2us(systemTime() - now);
381 if (sleepTime > 0)
Mathias Agopiana8826d62009-10-01 03:10:14 -0700382 usleep(sleepTime);
Kevin Hesterd3782b22012-04-26 10:38:55 -0700383
384 checkExit();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800385 } while (!exitPending());
386
387 glDeleteTextures(1, &mAndroid[0].name);
388 glDeleteTextures(1, &mAndroid[1].name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800389 return false;
390}
391
Mathias Agopiana8826d62009-10-01 03:10:14 -0700392
Kevin Hesterd3782b22012-04-26 10:38:55 -0700393void BootAnimation::checkExit() {
394 // Allow surface flinger to gracefully request shutdown
395 char value[PROPERTY_VALUE_MAX];
396 property_get(EXIT_PROP_NAME, value, "0");
397 int exitnow = atoi(value);
398 if (exitnow) {
399 requestExit();
400 }
401}
402
Jesse Hall083b84c2014-09-22 10:51:09 -0700403// Parse a color represented as an HTML-style 'RRGGBB' string: each pair of
404// characters in str is a hex number in [0, 255], which are converted to
405// floating point values in the range [0.0, 1.0] and placed in the
406// corresponding elements of color.
407//
408// If the input string isn't valid, parseColor returns false and color is
409// left unchanged.
410static bool parseColor(const char str[7], float color[3]) {
411 float tmpColor[3];
412 for (int i = 0; i < 3; i++) {
413 int val = 0;
414 for (int j = 0; j < 2; j++) {
415 val *= 16;
416 char c = str[2*i + j];
417 if (c >= '0' && c <= '9') val += c - '0';
418 else if (c >= 'A' && c <= 'F') val += (c - 'A') + 10;
419 else if (c >= 'a' && c <= 'f') val += (c - 'a') + 10;
420 else return false;
421 }
422 tmpColor[i] = static_cast<float>(val) / 255.0f;
423 }
424 memcpy(color, tmpColor, sizeof(tmpColor));
425 return true;
426}
427
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700428
429static bool readFile(ZipFileRO* zip, const char* name, String8& outString)
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700430{
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700431 ZipEntryRO entry = zip->findEntryByName(name);
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700432 ALOGE_IF(!entry, "couldn't find %s", name);
433 if (!entry) {
434 return false;
435 }
436
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700437 FileMap* entryMap = zip->createEntryFileMap(entry);
438 zip->releaseEntry(entry);
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700439 ALOGE_IF(!entryMap, "entryMap is null");
440 if (!entryMap) {
441 return false;
442 }
443
444 outString.setTo((char const*)entryMap->getDataPtr(), entryMap->getDataLength());
Narayan Kamath688ff4c2015-02-23 15:47:54 +0000445 delete entryMap;
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700446 return true;
447}
448
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800449// The time glyphs are stored in a single image of height 64 pixels. Each digit is 40 pixels wide,
450// and the colon character is half that at 20 pixels. The glyph order is '0123456789:'.
451// We render 24 hour time.
452void BootAnimation::drawTime(const Texture& clockTex, const int yPos) {
453 static constexpr char TIME_FORMAT[] = "%H:%M";
454 static constexpr int TIME_LENGTH = sizeof(TIME_FORMAT);
455
456 static constexpr int DIGIT_HEIGHT = 64;
457 static constexpr int DIGIT_WIDTH = 40;
458 static constexpr int COLON_WIDTH = DIGIT_WIDTH / 2;
459 static constexpr int TIME_WIDTH = (DIGIT_WIDTH * 4) + COLON_WIDTH;
460
461 if (clockTex.h < DIGIT_HEIGHT || clockTex.w < (10 * DIGIT_WIDTH + COLON_WIDTH)) {
462 ALOGE("Clock texture is too small; abandoning boot animation clock");
463 mClockEnabled = false;
464 return;
465 }
466
467 time_t rawtime;
468 time(&rawtime);
469 struct tm* timeInfo = localtime(&rawtime);
470
471 char timeBuff[TIME_LENGTH];
472 size_t length = strftime(timeBuff, TIME_LENGTH, TIME_FORMAT, timeInfo);
473
474 if (length != TIME_LENGTH - 1) {
475 ALOGE("Couldn't format time; abandoning boot animation clock");
476 mClockEnabled = false;
477 return;
478 }
479
480 glEnable(GL_BLEND); // Allow us to draw on top of the animation
481 glBindTexture(GL_TEXTURE_2D, clockTex.name);
482
483 int xPos = (mWidth - TIME_WIDTH) / 2;
484 int cropRect[4] = { 0, DIGIT_HEIGHT, DIGIT_WIDTH, -DIGIT_HEIGHT };
485
486 for (int i = 0; i < TIME_LENGTH - 1; i++) {
487 char c = timeBuff[i];
488 int width = DIGIT_WIDTH;
489 int pos = c - '0'; // Position in the character list
490 if (pos < 0 || pos > 10) {
491 continue;
492 }
493 if (c == ':') {
494 width = COLON_WIDTH;
495 }
496
497 // Crop the texture to only the pixels in the current glyph
498 int left = pos * DIGIT_WIDTH;
499 cropRect[0] = left;
500 cropRect[2] = width;
501 glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_CROP_RECT_OES, cropRect);
502
503 glDrawTexiOES(xPos, yPos, 0, width, DIGIT_HEIGHT);
504
505 xPos += width;
506 }
507
508 glDisable(GL_BLEND); // Return to the animation's default behaviour
509 glBindTexture(GL_TEXTURE_2D, 0);
510}
511
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700512bool BootAnimation::parseAnimationDesc(Animation& animation)
Mathias Agopiana8826d62009-10-01 03:10:14 -0700513{
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700514 String8 desString;
515
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700516 if (!readFile(animation.zip, "desc.txt", desString)) {
Narayan Kamathafd31e02013-12-03 13:16:03 +0000517 return false;
518 }
Mathias Agopiana8826d62009-10-01 03:10:14 -0700519 char const* s = desString.string();
520
Mathias Agopiana8826d62009-10-01 03:10:14 -0700521 // Parse the description file
522 for (;;) {
523 const char* endl = strstr(s, "\n");
Andreas Gampecfedceb2014-09-30 21:48:18 -0700524 if (endl == NULL) break;
Mathias Agopiana8826d62009-10-01 03:10:14 -0700525 String8 line(s, endl - s);
526 const char* l = line.string();
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800527 int fps = 0;
528 int width = 0;
529 int height = 0;
530 int count = 0;
531 int pause = 0;
532 int clockPosY = -1;
Narayan Kamathafd31e02013-12-03 13:16:03 +0000533 char path[ANIM_ENTRY_NAME_MAX];
Jesse Hall083b84c2014-09-22 10:51:09 -0700534 char color[7] = "000000"; // default to black if unspecified
535
Kevin Hesterd3782b22012-04-26 10:38:55 -0700536 char pathType;
Mathias Agopiana8826d62009-10-01 03:10:14 -0700537 if (sscanf(l, "%d %d %d", &width, &height, &fps) == 3) {
Jesse Hall083b84c2014-09-22 10:51:09 -0700538 // ALOGD("> w=%d, h=%d, fps=%d", width, height, fps);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700539 animation.width = width;
540 animation.height = height;
541 animation.fps = fps;
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800542 } else if (sscanf(l, " %c %d %d %s #%6s %d",
543 &pathType, &count, &pause, path, color, &clockPosY) >= 4) {
544 // 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 -0700545 Animation::Part part;
Kevin Hesterd3782b22012-04-26 10:38:55 -0700546 part.playUntilComplete = pathType == 'c';
Mathias Agopiana8826d62009-10-01 03:10:14 -0700547 part.count = count;
548 part.pause = pause;
549 part.path = path;
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800550 part.clockPosY = clockPosY;
Geoffrey Pitschd6d9a1d2016-06-08 00:38:58 -0700551 part.audioData = NULL;
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700552 part.animation = NULL;
Jesse Hall083b84c2014-09-22 10:51:09 -0700553 if (!parseColor(color, part.backgroundColor)) {
554 ALOGE("> invalid color '#%s'", color);
555 part.backgroundColor[0] = 0.0f;
556 part.backgroundColor[1] = 0.0f;
557 part.backgroundColor[2] = 0.0f;
558 }
Mathias Agopiana8826d62009-10-01 03:10:14 -0700559 animation.parts.add(part);
560 }
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700561 else if (strcmp(l, "$SYSTEM") == 0) {
562 // ALOGD("> SYSTEM");
563 Animation::Part part;
564 part.playUntilComplete = false;
565 part.count = 1;
566 part.pause = 0;
Geoffrey Pitschd6d9a1d2016-06-08 00:38:58 -0700567 part.audioData = NULL;
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700568 part.animation = loadAnimation(String8(SYSTEM_BOOTANIMATION_FILE));
569 if (part.animation != NULL)
570 animation.parts.add(part);
571 }
Mathias Agopiana8826d62009-10-01 03:10:14 -0700572 s = ++endl;
573 }
574
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700575 return true;
576}
577
578bool BootAnimation::preloadZip(Animation& animation)
579{
Mathias Agopiana8826d62009-10-01 03:10:14 -0700580 // read all the data structures
581 const size_t pcount = animation.parts.size();
Narayan Kamathafd31e02013-12-03 13:16:03 +0000582 void *cookie = NULL;
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400583 ZipFileRO* zip = animation.zip;
584 if (!zip->startIteration(&cookie)) {
Narayan Kamathafd31e02013-12-03 13:16:03 +0000585 return false;
586 }
587
Geoffrey Pitschd6d9a1d2016-06-08 00:38:58 -0700588 bool hasAudio = false;
Narayan Kamathafd31e02013-12-03 13:16:03 +0000589 ZipEntryRO entry;
590 char name[ANIM_ENTRY_NAME_MAX];
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400591 while ((entry = zip->nextEntry(cookie)) != NULL) {
592 const int foundEntryName = zip->getEntryFileName(entry, name, ANIM_ENTRY_NAME_MAX);
Narayan Kamathafd31e02013-12-03 13:16:03 +0000593 if (foundEntryName > ANIM_ENTRY_NAME_MAX || foundEntryName == -1) {
594 ALOGE("Error fetching entry file name");
595 continue;
596 }
597
598 const String8 entryName(name);
599 const String8 path(entryName.getPathDir());
600 const String8 leaf(entryName.getPathLeaf());
601 if (leaf.size() > 0) {
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400602 for (size_t j = 0; j < pcount; j++) {
Narayan Kamathafd31e02013-12-03 13:16:03 +0000603 if (path == animation.parts[j].path) {
Narayan Kamath4600dd02015-06-16 12:02:57 +0100604 uint16_t method;
Narayan Kamathafd31e02013-12-03 13:16:03 +0000605 // supports only stored png files
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400606 if (zip->getEntryInfo(entry, &method, NULL, NULL, NULL, NULL, NULL)) {
Narayan Kamathafd31e02013-12-03 13:16:03 +0000607 if (method == ZipFileRO::kCompressStored) {
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400608 FileMap* map = zip->createEntryFileMap(entry);
Narayan Kamathafd31e02013-12-03 13:16:03 +0000609 if (map) {
Narayan Kamathafd31e02013-12-03 13:16:03 +0000610 Animation::Part& part(animation.parts.editItemAt(j));
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700611 if (leaf == "audio.wav") {
Geoffrey Pitschd6d9a1d2016-06-08 00:38:58 -0700612 hasAudio = true;
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700613 // a part may have at most one audio file
Geoffrey Pitschd6d9a1d2016-06-08 00:38:58 -0700614 part.audioData = (uint8_t *)map->getDataPtr();
615 part.audioLength = map->getDataLength();
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400616 } else if (leaf == "trim.txt") {
617 part.trimData.setTo((char const*)map->getDataPtr(),
618 map->getDataLength());
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700619 } else {
620 Animation::Frame frame;
621 frame.name = leaf;
622 frame.map = map;
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400623 frame.trimWidth = animation.width;
624 frame.trimHeight = animation.height;
625 frame.trimX = 0;
626 frame.trimY = 0;
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700627 part.frames.add(frame);
628 }
Mathias Agopiana8826d62009-10-01 03:10:14 -0700629 }
Geoffrey Pitschd6d9a1d2016-06-08 00:38:58 -0700630 } else {
631 ALOGE("bootanimation.zip is compressed; must be only stored");
Mathias Agopiana8826d62009-10-01 03:10:14 -0700632 }
633 }
634 }
635 }
636 }
637 }
638
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400639 // If there is trimData present, override the positioning defaults.
640 for (Animation::Part& part : animation.parts) {
641 const char* trimDataStr = part.trimData.string();
642 for (size_t frameIdx = 0; frameIdx < part.frames.size(); frameIdx++) {
643 const char* endl = strstr(trimDataStr, "\n");
644 // No more trimData for this part.
645 if (endl == NULL) {
646 break;
647 }
648 String8 line(trimDataStr, endl - trimDataStr);
649 const char* lineStr = line.string();
650 trimDataStr = ++endl;
651 int width = 0, height = 0, x = 0, y = 0;
652 if (sscanf(lineStr, "%dx%d+%d+%d", &width, &height, &x, &y) == 4) {
653 Animation::Frame& frame(part.frames.editItemAt(frameIdx));
654 frame.trimWidth = width;
655 frame.trimHeight = height;
656 frame.trimX = x;
657 frame.trimY = y;
658 } else {
659 ALOGE("Error parsing trim.txt, line: %s", lineStr);
660 break;
661 }
662 }
663 }
664
Geoffrey Pitschd6d9a1d2016-06-08 00:38:58 -0700665 // Create and initialize audioplay if there is a wav file in any of the animations.
666 if (hasAudio) {
667 ALOGD("found audio.wav, creating playback engine");
668 audioplay::create();
669 }
670
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400671 zip->endIteration(cookie);
Narayan Kamathafd31e02013-12-03 13:16:03 +0000672
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700673 return true;
674}
675
676bool BootAnimation::movie()
677{
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700678 Animation* animation = loadAnimation(mZipFileName);
679 if (animation == NULL)
680 return false;
681
Damien Bargiacchi97480862016-03-29 14:55:55 -0700682 bool anyPartHasClock = false;
683 for (size_t i=0; i < animation->parts.size(); i++) {
684 if(animation->parts[i].clockPosY >= 0) {
685 anyPartHasClock = true;
686 break;
687 }
688 }
689 if (!anyPartHasClock) {
690 mClockEnabled = false;
691 }
692
Sai Kiran Korwar27167492015-07-07 20:00:06 +0530693 // Check if npot textures are supported
694 mUseNpotTextures = false;
695 String8 gl_extensions;
696 const char* exts = reinterpret_cast<const char*>(glGetString(GL_EXTENSIONS));
697 if (!exts) {
698 glGetError();
699 } else {
700 gl_extensions.setTo(exts);
701 if ((gl_extensions.find("GL_ARB_texture_non_power_of_two") != -1) ||
702 (gl_extensions.find("GL_OES_texture_npot") != -1)) {
703 mUseNpotTextures = true;
704 }
705 }
706
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800707 // Blend required to draw time on top of animation frames.
708 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700709 glShadeModel(GL_FLAT);
710 glDisable(GL_DITHER);
711 glDisable(GL_SCISSOR_TEST);
712 glDisable(GL_BLEND);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700713
714 glBindTexture(GL_TEXTURE_2D, 0);
715 glEnable(GL_TEXTURE_2D);
716 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
717 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
718 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
719 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
720 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
721
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800722 bool clockTextureInitialized = false;
723 if (mClockEnabled) {
724 clockTextureInitialized = (initTexture(&mClock, mAssets, "images/clock64.png") == NO_ERROR);
725 mClockEnabled = clockTextureInitialized;
726 }
727
Damien Bargiacchi97480862016-03-29 14:55:55 -0700728 if (mClockEnabled && !updateIsTimeAccurate()) {
729 mTimeCheckThread = new TimeCheckThread(this);
730 mTimeCheckThread->run("BootAnimation::TimeCheckThread", PRIORITY_NORMAL);
731 }
732
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700733 playAnimation(*animation);
Damien Bargiacchi97480862016-03-29 14:55:55 -0700734
735 if (mTimeCheckThread != NULL) {
736 mTimeCheckThread->requestExit();
737 mTimeCheckThread = NULL;
738 }
739
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700740 releaseAnimation(animation);
741
Andriy Naborskyy815e51d2016-03-24 16:43:34 -0700742 if (clockTextureInitialized) {
743 glDeleteTextures(1, &mClock.name);
744 }
745
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700746 return false;
747}
748
749bool BootAnimation::playAnimation(const Animation& animation)
750{
751 const size_t pcount = animation.parts.size();
Mathias Agopiana8826d62009-10-01 03:10:14 -0700752 nsecs_t frameDuration = s2ns(1) / animation.fps;
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400753 const int animationX = (mWidth - animation.width) / 2;
754 const int animationY = (mHeight - animation.height) / 2;
Mathias Agopian9f3020d2009-11-06 16:30:18 -0800755
Narayan Kamathafd31e02013-12-03 13:16:03 +0000756 for (size_t i=0 ; i<pcount ; i++) {
Mathias Agopiana8826d62009-10-01 03:10:14 -0700757 const Animation::Part& part(animation.parts[i]);
758 const size_t fcount = part.frames.size();
759 glBindTexture(GL_TEXTURE_2D, 0);
760
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700761 // Handle animation package
762 if (part.animation != NULL) {
763 playAnimation(*part.animation);
764 if (exitPending())
765 break;
766 continue; //to next part
767 }
768
Mathias Agopiana8826d62009-10-01 03:10:14 -0700769 for (int r=0 ; !part.count || r<part.count ; r++) {
Kevin Hesterd3782b22012-04-26 10:38:55 -0700770 // Exit any non playuntil complete parts immediately
771 if(exitPending() && !part.playUntilComplete)
772 break;
773
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700774 // only play audio file the first time we animate the part
Geoffrey Pitschd6d9a1d2016-06-08 00:38:58 -0700775 if (r == 0 && part.audioData) {
776 ALOGD("playing clip for part%d, size=%d", (int) i, part.audioLength);
777 audioplay::playClip(part.audioData, part.audioLength);
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700778 }
779
Jesse Hall083b84c2014-09-22 10:51:09 -0700780 glClearColor(
781 part.backgroundColor[0],
782 part.backgroundColor[1],
783 part.backgroundColor[2],
784 1.0f);
785
Narayan Kamathafd31e02013-12-03 13:16:03 +0000786 for (size_t j=0 ; j<fcount && (!exitPending() || part.playUntilComplete) ; j++) {
Mathias Agopiana8826d62009-10-01 03:10:14 -0700787 const Animation::Frame& frame(part.frames[j]);
Mathias Agopiandb7dd2a2012-05-12 15:08:21 -0700788 nsecs_t lastFrame = systemTime();
Mathias Agopiana8826d62009-10-01 03:10:14 -0700789
790 if (r > 0) {
791 glBindTexture(GL_TEXTURE_2D, frame.tid);
792 } else {
793 if (part.count != 1) {
794 glGenTextures(1, &frame.tid);
795 glBindTexture(GL_TEXTURE_2D, frame.tid);
796 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
797 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
798 }
Mykola Kondratenko0c1eeb32014-04-15 09:35:44 +0200799 initTexture(frame);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700800 }
801
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400802 const int xc = animationX + frame.trimX;
803 const int yc = animationY + frame.trimY;
804 Region clearReg(Rect(mWidth, mHeight));
805 clearReg.subtractSelf(Rect(xc, yc, xc+frame.trimWidth, yc+frame.trimHeight));
Mathias Agopian9f3020d2009-11-06 16:30:18 -0800806 if (!clearReg.isEmpty()) {
807 Region::const_iterator head(clearReg.begin());
808 Region::const_iterator tail(clearReg.end());
809 glEnable(GL_SCISSOR_TEST);
810 while (head != tail) {
Andreas Gampecfedceb2014-09-30 21:48:18 -0700811 const Rect& r2(*head++);
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400812 glScissor(r2.left, mHeight - r2.bottom, r2.width(), r2.height());
Mathias Agopian9f3020d2009-11-06 16:30:18 -0800813 glClear(GL_COLOR_BUFFER_BIT);
814 }
815 glDisable(GL_SCISSOR_TEST);
816 }
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400817 // specify the y center as ceiling((mHeight - frame.trimHeight) / 2)
818 // which is equivalent to mHeight - (yc + frame.trimHeight)
819 glDrawTexiOES(xc, mHeight - (yc + frame.trimHeight),
820 0, frame.trimWidth, frame.trimHeight);
Damien Bargiacchi97480862016-03-29 14:55:55 -0700821 if (mClockEnabled && mTimeIsAccurate && part.clockPosY >= 0) {
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800822 drawTime(mClock, part.clockPosY);
823 }
824
Mathias Agopiana8826d62009-10-01 03:10:14 -0700825 eglSwapBuffers(mDisplay, mSurface);
826
827 nsecs_t now = systemTime();
828 nsecs_t delay = frameDuration - (now - lastFrame);
Mathias Agopiandb7dd2a2012-05-12 15:08:21 -0700829 //ALOGD("%lld, %lld", ns2ms(now - lastFrame), ns2ms(delay));
Mathias Agopiana8826d62009-10-01 03:10:14 -0700830 lastFrame = now;
Mathias Agopiandb7dd2a2012-05-12 15:08:21 -0700831
832 if (delay > 0) {
833 struct timespec spec;
834 spec.tv_sec = (now + delay) / 1000000000;
835 spec.tv_nsec = (now + delay) % 1000000000;
836 int err;
837 do {
838 err = clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &spec, NULL);
839 } while (err<0 && errno == EINTR);
840 }
Kevin Hesterd3782b22012-04-26 10:38:55 -0700841
842 checkExit();
Mathias Agopiana8826d62009-10-01 03:10:14 -0700843 }
Kevin Hesterd3782b22012-04-26 10:38:55 -0700844
Mathias Agopiana8826d62009-10-01 03:10:14 -0700845 usleep(part.pause * ns2us(frameDuration));
Kevin Hesterd3782b22012-04-26 10:38:55 -0700846
847 // For infinite parts, we've now played them at least once, so perhaps exit
848 if(exitPending() && !part.count)
849 break;
Mathias Agopiana8826d62009-10-01 03:10:14 -0700850 }
851
Geoffrey Pitsch2fb30fb2016-07-06 16:16:20 -0400852 }
853
854 // Free textures created for looping parts now that the animation is done.
855 for (const Animation::Part& part : animation.parts) {
Mathias Agopiana8826d62009-10-01 03:10:14 -0700856 if (part.count != 1) {
Geoffrey Pitsch2fb30fb2016-07-06 16:16:20 -0400857 const size_t fcount = part.frames.size();
858 for (size_t j = 0; j < fcount; j++) {
Mathias Agopiana8826d62009-10-01 03:10:14 -0700859 const Animation::Frame& frame(part.frames[j]);
860 glDeleteTextures(1, &frame.tid);
861 }
862 }
863 }
Geoffrey Pitschd6d9a1d2016-06-08 00:38:58 -0700864
865 // we've finally played everything we're going to play
866 audioplay::setPlaying(false);
867 audioplay::destroy();
868
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700869 return true;
Mathias Agopiana8826d62009-10-01 03:10:14 -0700870}
871
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700872void BootAnimation::releaseAnimation(Animation* animation) const
873{
874 for (Vector<Animation::Part>::iterator it = animation->parts.begin(),
875 e = animation->parts.end(); it != e; ++it) {
876 if (it->animation)
877 releaseAnimation(it->animation);
878 }
879 if (animation->zip)
880 delete animation->zip;
881 delete animation;
882}
883
884BootAnimation::Animation* BootAnimation::loadAnimation(const String8& fn)
885{
886 if (mLoadedFiles.indexOf(fn) >= 0) {
887 ALOGE("File \"%s\" is already loaded. Cyclic ref is not allowed",
888 fn.string());
889 return NULL;
890 }
891 ZipFileRO *zip = ZipFileRO::open(fn);
892 if (zip == NULL) {
893 ALOGE("Failed to open animation zip \"%s\": %s",
894 fn.string(), strerror(errno));
895 return NULL;
896 }
897
898 Animation *animation = new Animation;
899 animation->fileName = fn;
900 animation->zip = zip;
901 mLoadedFiles.add(animation->fileName);
902
903 parseAnimationDesc(*animation);
904 preloadZip(*animation);
905
906 mLoadedFiles.remove(fn);
907 return animation;
908}
Damien Bargiacchi97480862016-03-29 14:55:55 -0700909
910bool BootAnimation::updateIsTimeAccurate() {
911 static constexpr long long MAX_TIME_IN_PAST = 60000LL * 60LL * 24LL * 30LL; // 30 days
912 static constexpr long long MAX_TIME_IN_FUTURE = 60000LL * 90LL; // 90 minutes
913
914 if (mTimeIsAccurate) {
915 return true;
916 }
917
918 struct stat statResult;
919 if(stat(ACCURATE_TIME_FLAG_FILE_PATH, &statResult) == 0) {
920 mTimeIsAccurate = true;
921 return true;
922 }
923
924 FILE* file = fopen(LAST_TIME_CHANGED_FILE_PATH, "r");
925 if (file != NULL) {
926 long long lastChangedTime = 0;
927 fscanf(file, "%lld", &lastChangedTime);
928 fclose(file);
929 if (lastChangedTime > 0) {
930 struct timespec now;
931 clock_gettime(CLOCK_REALTIME, &now);
932 // Match the Java timestamp format
933 long long rtcNow = (now.tv_sec * 1000LL) + (now.tv_nsec / 1000000LL);
Damien Bargiacchi96762812016-07-12 15:53:40 -0700934 if (ACCURATE_TIME_EPOCH < rtcNow
935 && lastChangedTime > (rtcNow - MAX_TIME_IN_PAST)
936 && lastChangedTime < (rtcNow + MAX_TIME_IN_FUTURE)) {
Damien Bargiacchi97480862016-03-29 14:55:55 -0700937 mTimeIsAccurate = true;
938 }
939 }
940 }
941
942 return mTimeIsAccurate;
943}
944
945BootAnimation::TimeCheckThread::TimeCheckThread(BootAnimation* bootAnimation) : Thread(false),
946 mInotifyFd(-1), mSystemWd(-1), mTimeWd(-1), mBootAnimation(bootAnimation) {}
947
948BootAnimation::TimeCheckThread::~TimeCheckThread() {
949 // mInotifyFd may be -1 but that's ok since we're not at risk of attempting to close a valid FD.
950 close(mInotifyFd);
951}
952
953bool BootAnimation::TimeCheckThread::threadLoop() {
954 bool shouldLoop = doThreadLoop() && !mBootAnimation->mTimeIsAccurate
955 && mBootAnimation->mClockEnabled;
956 if (!shouldLoop) {
957 close(mInotifyFd);
958 mInotifyFd = -1;
959 }
960 return shouldLoop;
961}
962
963bool BootAnimation::TimeCheckThread::doThreadLoop() {
964 static constexpr int BUFF_LEN (10 * (sizeof(struct inotify_event) + NAME_MAX + 1));
965
966 // Poll instead of doing a blocking read so the Thread can exit if requested.
967 struct pollfd pfd = { mInotifyFd, POLLIN, 0 };
968 ssize_t pollResult = poll(&pfd, 1, 1000);
969
970 if (pollResult == 0) {
971 return true;
972 } else if (pollResult < 0) {
973 ALOGE("Could not poll inotify events");
974 return false;
975 }
976
977 char buff[BUFF_LEN] __attribute__ ((aligned(__alignof__(struct inotify_event))));;
978 ssize_t length = read(mInotifyFd, buff, BUFF_LEN);
979 if (length == 0) {
980 return true;
981 } else if (length < 0) {
982 ALOGE("Could not read inotify events");
983 return false;
984 }
985
986 const struct inotify_event *event;
987 for (char* ptr = buff; ptr < buff + length; ptr += sizeof(struct inotify_event) + event->len) {
988 event = (const struct inotify_event *) ptr;
989 if (event->wd == mSystemWd && strcmp(SYSTEM_TIME_DIR_NAME, event->name) == 0) {
990 addTimeDirWatch();
991 } else if (event->wd == mTimeWd && (strcmp(LAST_TIME_CHANGED_FILE_NAME, event->name) == 0
992 || strcmp(ACCURATE_TIME_FLAG_FILE_NAME, event->name) == 0)) {
993 return !mBootAnimation->updateIsTimeAccurate();
994 }
995 }
996
997 return true;
998}
999
1000void BootAnimation::TimeCheckThread::addTimeDirWatch() {
1001 mTimeWd = inotify_add_watch(mInotifyFd, SYSTEM_TIME_DIR_PATH,
1002 IN_CLOSE_WRITE | IN_MOVED_TO | IN_ATTRIB);
1003 if (mTimeWd > 0) {
1004 // No need to watch for the time directory to be created if it already exists
1005 inotify_rm_watch(mInotifyFd, mSystemWd);
1006 mSystemWd = -1;
1007 }
1008}
1009
1010status_t BootAnimation::TimeCheckThread::readyToRun() {
1011 mInotifyFd = inotify_init();
1012 if (mInotifyFd < 0) {
1013 ALOGE("Could not initialize inotify fd");
1014 return NO_INIT;
1015 }
1016
1017 mSystemWd = inotify_add_watch(mInotifyFd, SYSTEM_DATA_DIR_PATH, IN_CREATE | IN_ATTRIB);
1018 if (mSystemWd < 0) {
1019 close(mInotifyFd);
1020 mInotifyFd = -1;
1021 ALOGE("Could not add watch for %s", SYSTEM_DATA_DIR_PATH);
1022 return NO_INIT;
1023 }
1024
1025 addTimeDirWatch();
1026
1027 if (mBootAnimation->updateIsTimeAccurate()) {
1028 close(mInotifyFd);
1029 mInotifyFd = -1;
1030 return ALREADY_EXISTS;
1031 }
1032
1033 return NO_ERROR;
1034}
1035
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001036// ---------------------------------------------------------------------------
1037
1038}
1039; // namespace android