blob: f2f7008452bb6978780331f88972a8dc7831d6ec [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"
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -070061#include "AudioPlayer.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";
75static const char EXIT_PROP_NAME[] = "service.bootanim.exit";
Narayan Kamathafd31e02013-12-03 13:16:03 +000076static const int ANIM_ENTRY_NAME_MAX = 256;
77
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080078// ---------------------------------------------------------------------------
79
Damien Bargiacchi97480862016-03-29 14:55:55 -070080BootAnimation::BootAnimation() : Thread(false), mClockEnabled(true), mTimeIsAccurate(false),
81 mTimeCheckThread(NULL) {
Mathias Agopian627e7b52009-05-21 19:21:59 -070082 mSession = new SurfaceComposerClient();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080083}
84
Damien Bargiacchi97480862016-03-29 14:55:55 -070085BootAnimation::~BootAnimation() {}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080086
87void BootAnimation::onFirstRef() {
Mathias Agopianbc726112009-09-23 15:44:05 -070088 status_t err = mSession->linkToComposerDeath(this);
Steve Block3762c312012-01-06 19:20:56 +000089 ALOGE_IF(err, "linkToComposerDeath failed (%s) ", strerror(-err));
Mathias Agopian8434c532009-09-23 18:52:49 -070090 if (err == NO_ERROR) {
Mathias Agopianbc726112009-09-23 15:44:05 -070091 run("BootAnimation", PRIORITY_DISPLAY);
92 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080093}
94
Mathias Agopianbc726112009-09-23 15:44:05 -070095sp<SurfaceComposerClient> BootAnimation::session() const {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080096 return mSession;
97}
98
Mathias Agopianbc726112009-09-23 15:44:05 -070099
Narayan Kamathafd31e02013-12-03 13:16:03 +0000100void BootAnimation::binderDied(const wp<IBinder>&)
Mathias Agopianbc726112009-09-23 15:44:05 -0700101{
102 // woah, surfaceflinger died!
Steve Block5baa3a62011-12-20 16:23:08 +0000103 ALOGD("SurfaceFlinger died, exiting...");
Mathias Agopianbc726112009-09-23 15:44:05 -0700104
105 // calling requestExit() is not enough here because the Surface code
106 // might be blocked on a condition variable that will never be updated.
107 kill( getpid(), SIGKILL );
108 requestExit();
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700109 if (mAudioPlayer != NULL) {
110 mAudioPlayer->requestExit();
111 }
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;
120 SkImageDecoder::DecodeMemory(asset->getBuffer(false), asset->getLength(),
Mike Reed42a1d082014-07-07 18:06:18 -0400121 &bitmap, kUnknown_SkColorType, SkImageDecoder::kDecodePixels_Mode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800122 asset->close();
123 delete asset;
124
125 // ensure we can call getPixels(). No need to call unlock, since the
126 // bitmap will go out of scope when we return from this method.
127 bitmap.lockPixels();
128
129 const int w = bitmap.width();
130 const int h = bitmap.height();
131 const void* p = bitmap.getPixels();
132
133 GLint crop[4] = { 0, h, w, -h };
134 texture->w = w;
135 texture->h = h;
136
137 glGenTextures(1, &texture->name);
138 glBindTexture(GL_TEXTURE_2D, texture->name);
139
Mike Reed42a1d082014-07-07 18:06:18 -0400140 switch (bitmap.colorType()) {
141 case kAlpha_8_SkColorType:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800142 glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, w, h, 0, GL_ALPHA,
143 GL_UNSIGNED_BYTE, p);
144 break;
Mike Reed42a1d082014-07-07 18:06:18 -0400145 case kARGB_4444_SkColorType:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800146 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA,
147 GL_UNSIGNED_SHORT_4_4_4_4, p);
148 break;
Mike Reed42a1d082014-07-07 18:06:18 -0400149 case kN32_SkColorType:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800150 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA,
151 GL_UNSIGNED_BYTE, p);
152 break;
Mike Reed42a1d082014-07-07 18:06:18 -0400153 case kRGB_565_SkColorType:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800154 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, w, h, 0, GL_RGB,
155 GL_UNSIGNED_SHORT_5_6_5, p);
156 break;
157 default:
158 break;
159 }
160
161 glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_CROP_RECT_OES, crop);
162 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
163 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
164 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
165 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
166 return NO_ERROR;
167}
168
Mykola Kondratenko0c1eeb32014-04-15 09:35:44 +0200169status_t BootAnimation::initTexture(const Animation::Frame& frame)
Mathias Agopiana8826d62009-10-01 03:10:14 -0700170{
171 //StopWatch watch("blah");
172
173 SkBitmap bitmap;
Mykola Kondratenko0c1eeb32014-04-15 09:35:44 +0200174 SkMemoryStream stream(frame.map->getDataPtr(), frame.map->getDataLength());
Mathias Agopian2b99e552011-11-10 15:59:07 -0800175 SkImageDecoder* codec = SkImageDecoder::Factory(&stream);
Andreas Gampecfedceb2014-09-30 21:48:18 -0700176 if (codec != NULL) {
Elliott Hughesc367d482013-10-29 13:12:55 -0700177 codec->setDitherImage(false);
Mathias Agopian2b99e552011-11-10 15:59:07 -0800178 codec->decode(&stream, &bitmap,
Mike Reed42a1d082014-07-07 18:06:18 -0400179 kN32_SkColorType,
Mathias Agopian2b99e552011-11-10 15:59:07 -0800180 SkImageDecoder::kDecodePixels_Mode);
181 delete codec;
182 }
Mathias Agopiana8826d62009-10-01 03:10:14 -0700183
Mykola Kondratenko0c1eeb32014-04-15 09:35:44 +0200184 // FileMap memory is never released until application exit.
185 // Release it now as the texture is already loaded and the memory used for
186 // the packed resource can be released.
Narayan Kamath688ff4c2015-02-23 15:47:54 +0000187 delete frame.map;
Mykola Kondratenko0c1eeb32014-04-15 09:35:44 +0200188
Mathias Agopiana8826d62009-10-01 03:10:14 -0700189 // ensure we can call getPixels(). No need to call unlock, since the
190 // bitmap will go out of scope when we return from this method.
191 bitmap.lockPixels();
192
193 const int w = bitmap.width();
194 const int h = bitmap.height();
195 const void* p = bitmap.getPixels();
196
197 GLint crop[4] = { 0, h, w, -h };
198 int tw = 1 << (31 - __builtin_clz(w));
199 int th = 1 << (31 - __builtin_clz(h));
200 if (tw < w) tw <<= 1;
201 if (th < h) th <<= 1;
202
Mike Reed42a1d082014-07-07 18:06:18 -0400203 switch (bitmap.colorType()) {
204 case kN32_SkColorType:
Sai Kiran Korwar27167492015-07-07 20:00:06 +0530205 if (!mUseNpotTextures && (tw != w || th != h)) {
Mathias Agopiana8826d62009-10-01 03:10:14 -0700206 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, tw, th, 0, GL_RGBA,
207 GL_UNSIGNED_BYTE, 0);
208 glTexSubImage2D(GL_TEXTURE_2D, 0,
209 0, 0, w, h, GL_RGBA, GL_UNSIGNED_BYTE, p);
210 } else {
Sai Kiran Korwar27167492015-07-07 20:00:06 +0530211 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA,
Mathias Agopiana8826d62009-10-01 03:10:14 -0700212 GL_UNSIGNED_BYTE, p);
213 }
214 break;
215
Mike Reed42a1d082014-07-07 18:06:18 -0400216 case kRGB_565_SkColorType:
Sai Kiran Korwar27167492015-07-07 20:00:06 +0530217 if (!mUseNpotTextures && (tw != w || th != h)) {
Mathias Agopiana8826d62009-10-01 03:10:14 -0700218 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, tw, th, 0, GL_RGB,
219 GL_UNSIGNED_SHORT_5_6_5, 0);
220 glTexSubImage2D(GL_TEXTURE_2D, 0,
221 0, 0, w, h, GL_RGB, GL_UNSIGNED_SHORT_5_6_5, p);
222 } else {
Sai Kiran Korwar27167492015-07-07 20:00:06 +0530223 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, w, h, 0, GL_RGB,
Mathias Agopiana8826d62009-10-01 03:10:14 -0700224 GL_UNSIGNED_SHORT_5_6_5, p);
225 }
226 break;
227 default:
228 break;
229 }
230
231 glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_CROP_RECT_OES, crop);
232
233 return NO_ERROR;
234}
235
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800236status_t BootAnimation::readyToRun() {
237 mAssets.addDefaultAssets();
238
Jeff Brown0b722fe2012-08-24 22:40:14 -0700239 sp<IBinder> dtoken(SurfaceComposerClient::getBuiltInDisplay(
240 ISurfaceComposer::eDisplayIdMain));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800241 DisplayInfo dinfo;
Jeff Brown0b722fe2012-08-24 22:40:14 -0700242 status_t status = SurfaceComposerClient::getDisplayInfo(dtoken, &dinfo);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800243 if (status)
244 return -1;
245
246 // create the native surface
Jeff Brown0b722fe2012-08-24 22:40:14 -0700247 sp<SurfaceControl> control = session()->createSurface(String8("BootAnimation"),
248 dinfo.w, dinfo.h, PIXEL_FORMAT_RGB_565);
Mathias Agopian439863f2011-06-28 19:09:31 -0700249
250 SurfaceComposerClient::openGlobalTransaction();
Mathias Agopian17f638b2009-04-16 20:04:08 -0700251 control->setLayer(0x40000000);
Mathias Agopian439863f2011-06-28 19:09:31 -0700252 SurfaceComposerClient::closeGlobalTransaction();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800253
Mathias Agopian17f638b2009-04-16 20:04:08 -0700254 sp<Surface> s = control->getSurface();
255
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800256 // initialize opengl and egl
Mathias Agopian738b9a42009-08-06 16:41:02 -0700257 const EGLint attribs[] = {
Mathias Agopian1b253b72011-08-15 15:20:22 -0700258 EGL_RED_SIZE, 8,
259 EGL_GREEN_SIZE, 8,
260 EGL_BLUE_SIZE, 8,
Mathias Agopiana8826d62009-10-01 03:10:14 -0700261 EGL_DEPTH_SIZE, 0,
262 EGL_NONE
Mathias Agopian738b9a42009-08-06 16:41:02 -0700263 };
Andreas Gampecfedceb2014-09-30 21:48:18 -0700264 EGLint w, h;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800265 EGLint numConfigs;
266 EGLConfig config;
267 EGLSurface surface;
268 EGLContext context;
Mathias Agopian627e7b52009-05-21 19:21:59 -0700269
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800270 EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
Mathias Agopian627e7b52009-05-21 19:21:59 -0700271
272 eglInitialize(display, 0, 0);
Mathias Agopian1b253b72011-08-15 15:20:22 -0700273 eglChooseConfig(display, attribs, &config, 1, &numConfigs);
Mathias Agopian1473f462009-04-10 14:24:30 -0700274 surface = eglCreateWindowSurface(display, config, s.get(), NULL);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800275 context = eglCreateContext(display, config, NULL, NULL);
276 eglQuerySurface(display, surface, EGL_WIDTH, &w);
277 eglQuerySurface(display, surface, EGL_HEIGHT, &h);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700278
Mathias Agopianabac0102009-07-31 14:47:00 -0700279 if (eglMakeCurrent(display, surface, surface, context) == EGL_FALSE)
280 return NO_INIT;
Mathias Agopiana8826d62009-10-01 03:10:14 -0700281
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800282 mDisplay = display;
283 mContext = context;
284 mSurface = surface;
285 mWidth = w;
286 mHeight = h;
Mathias Agopian17f638b2009-04-16 20:04:08 -0700287 mFlingerSurfaceControl = control;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800288 mFlingerSurface = s;
289
Elliott Hughesc367d482013-10-29 13:12:55 -0700290 // If the device has encryption turned on or is in process
Jason parksbd9a08d2011-01-31 15:04:34 -0600291 // of being encrypted we show the encrypted boot animation.
292 char decrypt[PROPERTY_VALUE_MAX];
293 property_get("vold.decrypt", decrypt, "");
294
295 bool encryptedAnimation = atoi(decrypt) != 0 || !strcmp("trigger_restart_min_framework", decrypt);
296
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700297 if (encryptedAnimation && (access(SYSTEM_ENCRYPTED_BOOTANIMATION_FILE, R_OK) == 0)) {
298 mZipFileName = SYSTEM_ENCRYPTED_BOOTANIMATION_FILE;
Jason parksbd9a08d2011-01-31 15:04:34 -0600299 }
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700300 else if (access(OEM_BOOTANIMATION_FILE, R_OK) == 0) {
301 mZipFileName = OEM_BOOTANIMATION_FILE;
302 }
303 else if (access(SYSTEM_BOOTANIMATION_FILE, R_OK) == 0) {
304 mZipFileName = SYSTEM_BOOTANIMATION_FILE;
305 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800306 return NO_ERROR;
307}
308
Mathias Agopiana8826d62009-10-01 03:10:14 -0700309bool BootAnimation::threadLoop()
310{
311 bool r;
Narayan Kamathafd31e02013-12-03 13:16:03 +0000312 // We have no bootanimation file, so we use the stock android logo
313 // animation.
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700314 if (mZipFileName.isEmpty()) {
Mathias Agopiana8826d62009-10-01 03:10:14 -0700315 r = android();
316 } else {
317 r = movie();
318 }
319
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800320 eglMakeCurrent(mDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
321 eglDestroyContext(mDisplay, mContext);
322 eglDestroySurface(mDisplay, mSurface);
Mathias Agopian6cf0db22009-04-17 19:36:26 -0700323 mFlingerSurface.clear();
Mathias Agopian17f638b2009-04-16 20:04:08 -0700324 mFlingerSurfaceControl.clear();
Mathias Agopian627e7b52009-05-21 19:21:59 -0700325 eglTerminate(mDisplay);
326 IPCThreadState::self()->stopProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800327 return r;
328}
329
Mathias Agopiana8826d62009-10-01 03:10:14 -0700330bool BootAnimation::android()
331{
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700332 initTexture(&mAndroid[0], mAssets, "images/android-logo-mask.png");
333 initTexture(&mAndroid[1], mAssets, "images/android-logo-shine.png");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800334
335 // clear screen
Mathias Agopiana8826d62009-10-01 03:10:14 -0700336 glShadeModel(GL_FLAT);
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700337 glDisable(GL_DITHER);
338 glDisable(GL_SCISSOR_TEST);
Mathias Agopian59f19e42011-05-06 19:22:12 -0700339 glClearColor(0,0,0,1);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800340 glClear(GL_COLOR_BUFFER_BIT);
341 eglSwapBuffers(mDisplay, mSurface);
342
Mathias Agopiana8826d62009-10-01 03:10:14 -0700343 glEnable(GL_TEXTURE_2D);
344 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
345
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700346 const GLint xc = (mWidth - mAndroid[0].w) / 2;
347 const GLint yc = (mHeight - mAndroid[0].h) / 2;
348 const Rect updateRect(xc, yc, xc + mAndroid[0].w, yc + mAndroid[0].h);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800349
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800350 glScissor(updateRect.left, mHeight - updateRect.bottom, updateRect.width(),
351 updateRect.height());
352
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700353 // Blend state
354 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
355 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
356
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800357 const nsecs_t startTime = systemTime();
358 do {
Mathias Agopian13796652009-03-24 22:49:21 -0700359 nsecs_t now = systemTime();
360 double time = now - startTime;
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700361 float t = 4.0f * float(time / us2ns(16667)) / mAndroid[1].w;
362 GLint offset = (1 - (t - floorf(t))) * mAndroid[1].w;
363 GLint x = xc - offset;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800364
Mathias Agopian81668642009-07-28 11:41:30 -0700365 glDisable(GL_SCISSOR_TEST);
366 glClear(GL_COLOR_BUFFER_BIT);
367
368 glEnable(GL_SCISSOR_TEST);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800369 glDisable(GL_BLEND);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800370 glBindTexture(GL_TEXTURE_2D, mAndroid[1].name);
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700371 glDrawTexiOES(x, yc, 0, mAndroid[1].w, mAndroid[1].h);
372 glDrawTexiOES(x + mAndroid[1].w, yc, 0, mAndroid[1].w, mAndroid[1].h);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800373
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700374 glEnable(GL_BLEND);
375 glBindTexture(GL_TEXTURE_2D, mAndroid[0].name);
376 glDrawTexiOES(xc, yc, 0, mAndroid[0].w, mAndroid[0].h);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800377
Mathias Agopian627e7b52009-05-21 19:21:59 -0700378 EGLBoolean res = eglSwapBuffers(mDisplay, mSurface);
379 if (res == EGL_FALSE)
380 break;
381
Mathias Agopian13796652009-03-24 22:49:21 -0700382 // 12fps: don't animate too fast to preserve CPU
383 const nsecs_t sleepTime = 83333 - ns2us(systemTime() - now);
384 if (sleepTime > 0)
Mathias Agopiana8826d62009-10-01 03:10:14 -0700385 usleep(sleepTime);
Kevin Hesterd3782b22012-04-26 10:38:55 -0700386
387 checkExit();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800388 } while (!exitPending());
389
390 glDeleteTextures(1, &mAndroid[0].name);
391 glDeleteTextures(1, &mAndroid[1].name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800392 return false;
393}
394
Mathias Agopiana8826d62009-10-01 03:10:14 -0700395
Kevin Hesterd3782b22012-04-26 10:38:55 -0700396void BootAnimation::checkExit() {
397 // Allow surface flinger to gracefully request shutdown
398 char value[PROPERTY_VALUE_MAX];
399 property_get(EXIT_PROP_NAME, value, "0");
400 int exitnow = atoi(value);
401 if (exitnow) {
402 requestExit();
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700403 if (mAudioPlayer != NULL) {
404 mAudioPlayer->requestExit();
405 }
Kevin Hesterd3782b22012-04-26 10:38:55 -0700406 }
407}
408
Jesse Hall083b84c2014-09-22 10:51:09 -0700409// Parse a color represented as an HTML-style 'RRGGBB' string: each pair of
410// characters in str is a hex number in [0, 255], which are converted to
411// floating point values in the range [0.0, 1.0] and placed in the
412// corresponding elements of color.
413//
414// If the input string isn't valid, parseColor returns false and color is
415// left unchanged.
416static bool parseColor(const char str[7], float color[3]) {
417 float tmpColor[3];
418 for (int i = 0; i < 3; i++) {
419 int val = 0;
420 for (int j = 0; j < 2; j++) {
421 val *= 16;
422 char c = str[2*i + j];
423 if (c >= '0' && c <= '9') val += c - '0';
424 else if (c >= 'A' && c <= 'F') val += (c - 'A') + 10;
425 else if (c >= 'a' && c <= 'f') val += (c - 'a') + 10;
426 else return false;
427 }
428 tmpColor[i] = static_cast<float>(val) / 255.0f;
429 }
430 memcpy(color, tmpColor, sizeof(tmpColor));
431 return true;
432}
433
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700434
435static bool readFile(ZipFileRO* zip, const char* name, String8& outString)
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700436{
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700437 ZipEntryRO entry = zip->findEntryByName(name);
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700438 ALOGE_IF(!entry, "couldn't find %s", name);
439 if (!entry) {
440 return false;
441 }
442
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700443 FileMap* entryMap = zip->createEntryFileMap(entry);
444 zip->releaseEntry(entry);
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700445 ALOGE_IF(!entryMap, "entryMap is null");
446 if (!entryMap) {
447 return false;
448 }
449
450 outString.setTo((char const*)entryMap->getDataPtr(), entryMap->getDataLength());
Narayan Kamath688ff4c2015-02-23 15:47:54 +0000451 delete entryMap;
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700452 return true;
453}
454
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800455// The time glyphs are stored in a single image of height 64 pixels. Each digit is 40 pixels wide,
456// and the colon character is half that at 20 pixels. The glyph order is '0123456789:'.
457// We render 24 hour time.
458void BootAnimation::drawTime(const Texture& clockTex, const int yPos) {
459 static constexpr char TIME_FORMAT[] = "%H:%M";
460 static constexpr int TIME_LENGTH = sizeof(TIME_FORMAT);
461
462 static constexpr int DIGIT_HEIGHT = 64;
463 static constexpr int DIGIT_WIDTH = 40;
464 static constexpr int COLON_WIDTH = DIGIT_WIDTH / 2;
465 static constexpr int TIME_WIDTH = (DIGIT_WIDTH * 4) + COLON_WIDTH;
466
467 if (clockTex.h < DIGIT_HEIGHT || clockTex.w < (10 * DIGIT_WIDTH + COLON_WIDTH)) {
468 ALOGE("Clock texture is too small; abandoning boot animation clock");
469 mClockEnabled = false;
470 return;
471 }
472
473 time_t rawtime;
474 time(&rawtime);
475 struct tm* timeInfo = localtime(&rawtime);
476
477 char timeBuff[TIME_LENGTH];
478 size_t length = strftime(timeBuff, TIME_LENGTH, TIME_FORMAT, timeInfo);
479
480 if (length != TIME_LENGTH - 1) {
481 ALOGE("Couldn't format time; abandoning boot animation clock");
482 mClockEnabled = false;
483 return;
484 }
485
486 glEnable(GL_BLEND); // Allow us to draw on top of the animation
487 glBindTexture(GL_TEXTURE_2D, clockTex.name);
488
489 int xPos = (mWidth - TIME_WIDTH) / 2;
490 int cropRect[4] = { 0, DIGIT_HEIGHT, DIGIT_WIDTH, -DIGIT_HEIGHT };
491
492 for (int i = 0; i < TIME_LENGTH - 1; i++) {
493 char c = timeBuff[i];
494 int width = DIGIT_WIDTH;
495 int pos = c - '0'; // Position in the character list
496 if (pos < 0 || pos > 10) {
497 continue;
498 }
499 if (c == ':') {
500 width = COLON_WIDTH;
501 }
502
503 // Crop the texture to only the pixels in the current glyph
504 int left = pos * DIGIT_WIDTH;
505 cropRect[0] = left;
506 cropRect[2] = width;
507 glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_CROP_RECT_OES, cropRect);
508
509 glDrawTexiOES(xPos, yPos, 0, width, DIGIT_HEIGHT);
510
511 xPos += width;
512 }
513
514 glDisable(GL_BLEND); // Return to the animation's default behaviour
515 glBindTexture(GL_TEXTURE_2D, 0);
516}
517
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700518bool BootAnimation::parseAnimationDesc(Animation& animation)
Mathias Agopiana8826d62009-10-01 03:10:14 -0700519{
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700520 String8 desString;
521
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700522 if (!readFile(animation.zip, "desc.txt", desString)) {
Narayan Kamathafd31e02013-12-03 13:16:03 +0000523 return false;
524 }
Mathias Agopiana8826d62009-10-01 03:10:14 -0700525 char const* s = desString.string();
526
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700527 // Create and initialize an AudioPlayer if we have an audio_conf.txt file
528 String8 audioConf;
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700529 if (readFile(animation.zip, "audio_conf.txt", audioConf)) {
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700530 mAudioPlayer = new AudioPlayer;
531 if (!mAudioPlayer->init(audioConf.string())) {
532 ALOGE("mAudioPlayer.init failed");
533 mAudioPlayer = NULL;
534 }
535 }
536
Mathias Agopiana8826d62009-10-01 03:10:14 -0700537 // Parse the description file
538 for (;;) {
539 const char* endl = strstr(s, "\n");
Andreas Gampecfedceb2014-09-30 21:48:18 -0700540 if (endl == NULL) break;
Mathias Agopiana8826d62009-10-01 03:10:14 -0700541 String8 line(s, endl - s);
542 const char* l = line.string();
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800543 int fps = 0;
544 int width = 0;
545 int height = 0;
546 int count = 0;
547 int pause = 0;
548 int clockPosY = -1;
Narayan Kamathafd31e02013-12-03 13:16:03 +0000549 char path[ANIM_ENTRY_NAME_MAX];
Jesse Hall083b84c2014-09-22 10:51:09 -0700550 char color[7] = "000000"; // default to black if unspecified
551
Kevin Hesterd3782b22012-04-26 10:38:55 -0700552 char pathType;
Mathias Agopiana8826d62009-10-01 03:10:14 -0700553 if (sscanf(l, "%d %d %d", &width, &height, &fps) == 3) {
Jesse Hall083b84c2014-09-22 10:51:09 -0700554 // ALOGD("> w=%d, h=%d, fps=%d", width, height, fps);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700555 animation.width = width;
556 animation.height = height;
557 animation.fps = fps;
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800558 } else if (sscanf(l, " %c %d %d %s #%6s %d",
559 &pathType, &count, &pause, path, color, &clockPosY) >= 4) {
560 // 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 -0700561 Animation::Part part;
Kevin Hesterd3782b22012-04-26 10:38:55 -0700562 part.playUntilComplete = pathType == 'c';
Mathias Agopiana8826d62009-10-01 03:10:14 -0700563 part.count = count;
564 part.pause = pause;
565 part.path = path;
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800566 part.clockPosY = clockPosY;
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700567 part.audioFile = NULL;
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700568 part.animation = NULL;
Jesse Hall083b84c2014-09-22 10:51:09 -0700569 if (!parseColor(color, part.backgroundColor)) {
570 ALOGE("> invalid color '#%s'", color);
571 part.backgroundColor[0] = 0.0f;
572 part.backgroundColor[1] = 0.0f;
573 part.backgroundColor[2] = 0.0f;
574 }
Mathias Agopiana8826d62009-10-01 03:10:14 -0700575 animation.parts.add(part);
576 }
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700577 else if (strcmp(l, "$SYSTEM") == 0) {
578 // ALOGD("> SYSTEM");
579 Animation::Part part;
580 part.playUntilComplete = false;
581 part.count = 1;
582 part.pause = 0;
583 part.audioFile = NULL;
584 part.animation = loadAnimation(String8(SYSTEM_BOOTANIMATION_FILE));
585 if (part.animation != NULL)
586 animation.parts.add(part);
587 }
Mathias Agopiana8826d62009-10-01 03:10:14 -0700588 s = ++endl;
589 }
590
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700591 return true;
592}
593
594bool BootAnimation::preloadZip(Animation& animation)
595{
Mathias Agopiana8826d62009-10-01 03:10:14 -0700596 // read all the data structures
597 const size_t pcount = animation.parts.size();
Narayan Kamathafd31e02013-12-03 13:16:03 +0000598 void *cookie = NULL;
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400599 ZipFileRO* zip = animation.zip;
600 if (!zip->startIteration(&cookie)) {
Narayan Kamathafd31e02013-12-03 13:16:03 +0000601 return false;
602 }
603
604 ZipEntryRO entry;
605 char name[ANIM_ENTRY_NAME_MAX];
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400606 while ((entry = zip->nextEntry(cookie)) != NULL) {
607 const int foundEntryName = zip->getEntryFileName(entry, name, ANIM_ENTRY_NAME_MAX);
Narayan Kamathafd31e02013-12-03 13:16:03 +0000608 if (foundEntryName > ANIM_ENTRY_NAME_MAX || foundEntryName == -1) {
609 ALOGE("Error fetching entry file name");
610 continue;
611 }
612
613 const String8 entryName(name);
614 const String8 path(entryName.getPathDir());
615 const String8 leaf(entryName.getPathLeaf());
616 if (leaf.size() > 0) {
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400617 for (size_t j = 0; j < pcount; j++) {
Narayan Kamathafd31e02013-12-03 13:16:03 +0000618 if (path == animation.parts[j].path) {
Narayan Kamath4600dd02015-06-16 12:02:57 +0100619 uint16_t method;
Narayan Kamathafd31e02013-12-03 13:16:03 +0000620 // supports only stored png files
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400621 if (zip->getEntryInfo(entry, &method, NULL, NULL, NULL, NULL, NULL)) {
Narayan Kamathafd31e02013-12-03 13:16:03 +0000622 if (method == ZipFileRO::kCompressStored) {
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400623 FileMap* map = zip->createEntryFileMap(entry);
Narayan Kamathafd31e02013-12-03 13:16:03 +0000624 if (map) {
Narayan Kamathafd31e02013-12-03 13:16:03 +0000625 Animation::Part& part(animation.parts.editItemAt(j));
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700626 if (leaf == "audio.wav") {
627 // a part may have at most one audio file
628 part.audioFile = map;
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400629 } else if (leaf == "trim.txt") {
630 part.trimData.setTo((char const*)map->getDataPtr(),
631 map->getDataLength());
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700632 } else {
633 Animation::Frame frame;
634 frame.name = leaf;
635 frame.map = map;
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400636 frame.trimWidth = animation.width;
637 frame.trimHeight = animation.height;
638 frame.trimX = 0;
639 frame.trimY = 0;
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700640 part.frames.add(frame);
641 }
Mathias Agopiana8826d62009-10-01 03:10:14 -0700642 }
643 }
644 }
645 }
646 }
647 }
648 }
649
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400650 // If there is trimData present, override the positioning defaults.
651 for (Animation::Part& part : animation.parts) {
652 const char* trimDataStr = part.trimData.string();
653 for (size_t frameIdx = 0; frameIdx < part.frames.size(); frameIdx++) {
654 const char* endl = strstr(trimDataStr, "\n");
655 // No more trimData for this part.
656 if (endl == NULL) {
657 break;
658 }
659 String8 line(trimDataStr, endl - trimDataStr);
660 const char* lineStr = line.string();
661 trimDataStr = ++endl;
662 int width = 0, height = 0, x = 0, y = 0;
663 if (sscanf(lineStr, "%dx%d+%d+%d", &width, &height, &x, &y) == 4) {
664 Animation::Frame& frame(part.frames.editItemAt(frameIdx));
665 frame.trimWidth = width;
666 frame.trimHeight = height;
667 frame.trimX = x;
668 frame.trimY = y;
669 } else {
670 ALOGE("Error parsing trim.txt, line: %s", lineStr);
671 break;
672 }
673 }
674 }
675
676 zip->endIteration(cookie);
Narayan Kamathafd31e02013-12-03 13:16:03 +0000677
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700678 return true;
679}
680
681bool BootAnimation::movie()
682{
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700683 Animation* animation = loadAnimation(mZipFileName);
684 if (animation == NULL)
685 return false;
686
Damien Bargiacchi97480862016-03-29 14:55:55 -0700687 bool anyPartHasClock = false;
688 for (size_t i=0; i < animation->parts.size(); i++) {
689 if(animation->parts[i].clockPosY >= 0) {
690 anyPartHasClock = true;
691 break;
692 }
693 }
694 if (!anyPartHasClock) {
695 mClockEnabled = false;
696 }
697
Sai Kiran Korwar27167492015-07-07 20:00:06 +0530698 // Check if npot textures are supported
699 mUseNpotTextures = false;
700 String8 gl_extensions;
701 const char* exts = reinterpret_cast<const char*>(glGetString(GL_EXTENSIONS));
702 if (!exts) {
703 glGetError();
704 } else {
705 gl_extensions.setTo(exts);
706 if ((gl_extensions.find("GL_ARB_texture_non_power_of_two") != -1) ||
707 (gl_extensions.find("GL_OES_texture_npot") != -1)) {
708 mUseNpotTextures = true;
709 }
710 }
711
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800712 // Blend required to draw time on top of animation frames.
713 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700714 glShadeModel(GL_FLAT);
715 glDisable(GL_DITHER);
716 glDisable(GL_SCISSOR_TEST);
717 glDisable(GL_BLEND);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700718
719 glBindTexture(GL_TEXTURE_2D, 0);
720 glEnable(GL_TEXTURE_2D);
721 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
722 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
723 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
724 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
725 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
726
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800727 bool clockTextureInitialized = false;
728 if (mClockEnabled) {
729 clockTextureInitialized = (initTexture(&mClock, mAssets, "images/clock64.png") == NO_ERROR);
730 mClockEnabled = clockTextureInitialized;
731 }
732
Damien Bargiacchi97480862016-03-29 14:55:55 -0700733 if (mClockEnabled && !updateIsTimeAccurate()) {
734 mTimeCheckThread = new TimeCheckThread(this);
735 mTimeCheckThread->run("BootAnimation::TimeCheckThread", PRIORITY_NORMAL);
736 }
737
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700738 playAnimation(*animation);
Damien Bargiacchi97480862016-03-29 14:55:55 -0700739
740 if (mTimeCheckThread != NULL) {
741 mTimeCheckThread->requestExit();
742 mTimeCheckThread = NULL;
743 }
744
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700745 releaseAnimation(animation);
746
Andriy Naborskyy815e51d2016-03-24 16:43:34 -0700747 if (clockTextureInitialized) {
748 glDeleteTextures(1, &mClock.name);
749 }
750
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700751 return false;
752}
753
754bool BootAnimation::playAnimation(const Animation& animation)
755{
756 const size_t pcount = animation.parts.size();
Mathias Agopiana8826d62009-10-01 03:10:14 -0700757 nsecs_t frameDuration = s2ns(1) / animation.fps;
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400758 const int animationX = (mWidth - animation.width) / 2;
759 const int animationY = (mHeight - animation.height) / 2;
Mathias Agopian9f3020d2009-11-06 16:30:18 -0800760
Narayan Kamathafd31e02013-12-03 13:16:03 +0000761 for (size_t i=0 ; i<pcount ; i++) {
Mathias Agopiana8826d62009-10-01 03:10:14 -0700762 const Animation::Part& part(animation.parts[i]);
763 const size_t fcount = part.frames.size();
764 glBindTexture(GL_TEXTURE_2D, 0);
765
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700766 // Handle animation package
767 if (part.animation != NULL) {
768 playAnimation(*part.animation);
769 if (exitPending())
770 break;
771 continue; //to next part
772 }
773
Mathias Agopiana8826d62009-10-01 03:10:14 -0700774 for (int r=0 ; !part.count || r<part.count ; r++) {
Kevin Hesterd3782b22012-04-26 10:38:55 -0700775 // Exit any non playuntil complete parts immediately
776 if(exitPending() && !part.playUntilComplete)
777 break;
778
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700779 // only play audio file the first time we animate the part
780 if (r == 0 && mAudioPlayer != NULL && part.audioFile) {
781 mAudioPlayer->playFile(part.audioFile);
782 }
783
Jesse Hall083b84c2014-09-22 10:51:09 -0700784 glClearColor(
785 part.backgroundColor[0],
786 part.backgroundColor[1],
787 part.backgroundColor[2],
788 1.0f);
789
Narayan Kamathafd31e02013-12-03 13:16:03 +0000790 for (size_t j=0 ; j<fcount && (!exitPending() || part.playUntilComplete) ; j++) {
Mathias Agopiana8826d62009-10-01 03:10:14 -0700791 const Animation::Frame& frame(part.frames[j]);
Mathias Agopiandb7dd2a2012-05-12 15:08:21 -0700792 nsecs_t lastFrame = systemTime();
Mathias Agopiana8826d62009-10-01 03:10:14 -0700793
794 if (r > 0) {
795 glBindTexture(GL_TEXTURE_2D, frame.tid);
796 } else {
797 if (part.count != 1) {
798 glGenTextures(1, &frame.tid);
799 glBindTexture(GL_TEXTURE_2D, frame.tid);
800 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
801 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
802 }
Mykola Kondratenko0c1eeb32014-04-15 09:35:44 +0200803 initTexture(frame);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700804 }
805
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400806 const int xc = animationX + frame.trimX;
807 const int yc = animationY + frame.trimY;
808 Region clearReg(Rect(mWidth, mHeight));
809 clearReg.subtractSelf(Rect(xc, yc, xc+frame.trimWidth, yc+frame.trimHeight));
Mathias Agopian9f3020d2009-11-06 16:30:18 -0800810 if (!clearReg.isEmpty()) {
811 Region::const_iterator head(clearReg.begin());
812 Region::const_iterator tail(clearReg.end());
813 glEnable(GL_SCISSOR_TEST);
814 while (head != tail) {
Andreas Gampecfedceb2014-09-30 21:48:18 -0700815 const Rect& r2(*head++);
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400816 glScissor(r2.left, mHeight - r2.bottom, r2.width(), r2.height());
Mathias Agopian9f3020d2009-11-06 16:30:18 -0800817 glClear(GL_COLOR_BUFFER_BIT);
818 }
819 glDisable(GL_SCISSOR_TEST);
820 }
Geoffrey Pitschdd214a72016-06-27 17:14:30 -0400821 // specify the y center as ceiling((mHeight - frame.trimHeight) / 2)
822 // which is equivalent to mHeight - (yc + frame.trimHeight)
823 glDrawTexiOES(xc, mHeight - (yc + frame.trimHeight),
824 0, frame.trimWidth, frame.trimHeight);
Damien Bargiacchi97480862016-03-29 14:55:55 -0700825 if (mClockEnabled && mTimeIsAccurate && part.clockPosY >= 0) {
Damien Bargiacchia704b7d2016-02-16 16:55:49 -0800826 drawTime(mClock, part.clockPosY);
827 }
828
Mathias Agopiana8826d62009-10-01 03:10:14 -0700829 eglSwapBuffers(mDisplay, mSurface);
830
831 nsecs_t now = systemTime();
832 nsecs_t delay = frameDuration - (now - lastFrame);
Mathias Agopiandb7dd2a2012-05-12 15:08:21 -0700833 //ALOGD("%lld, %lld", ns2ms(now - lastFrame), ns2ms(delay));
Mathias Agopiana8826d62009-10-01 03:10:14 -0700834 lastFrame = now;
Mathias Agopiandb7dd2a2012-05-12 15:08:21 -0700835
836 if (delay > 0) {
837 struct timespec spec;
838 spec.tv_sec = (now + delay) / 1000000000;
839 spec.tv_nsec = (now + delay) % 1000000000;
840 int err;
841 do {
842 err = clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &spec, NULL);
843 } while (err<0 && errno == EINTR);
844 }
Kevin Hesterd3782b22012-04-26 10:38:55 -0700845
846 checkExit();
Mathias Agopiana8826d62009-10-01 03:10:14 -0700847 }
Kevin Hesterd3782b22012-04-26 10:38:55 -0700848
Mathias Agopiana8826d62009-10-01 03:10:14 -0700849 usleep(part.pause * ns2us(frameDuration));
Kevin Hesterd3782b22012-04-26 10:38:55 -0700850
851 // For infinite parts, we've now played them at least once, so perhaps exit
852 if(exitPending() && !part.count)
853 break;
Mathias Agopiana8826d62009-10-01 03:10:14 -0700854 }
855
856 // free the textures for this part
857 if (part.count != 1) {
Narayan Kamathafd31e02013-12-03 13:16:03 +0000858 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 }
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700864 return true;
Mathias Agopiana8826d62009-10-01 03:10:14 -0700865}
866
Andriy Naborskyy39218ba2015-08-16 21:32:50 -0700867void BootAnimation::releaseAnimation(Animation* animation) const
868{
869 for (Vector<Animation::Part>::iterator it = animation->parts.begin(),
870 e = animation->parts.end(); it != e; ++it) {
871 if (it->animation)
872 releaseAnimation(it->animation);
873 }
874 if (animation->zip)
875 delete animation->zip;
876 delete animation;
877}
878
879BootAnimation::Animation* BootAnimation::loadAnimation(const String8& fn)
880{
881 if (mLoadedFiles.indexOf(fn) >= 0) {
882 ALOGE("File \"%s\" is already loaded. Cyclic ref is not allowed",
883 fn.string());
884 return NULL;
885 }
886 ZipFileRO *zip = ZipFileRO::open(fn);
887 if (zip == NULL) {
888 ALOGE("Failed to open animation zip \"%s\": %s",
889 fn.string(), strerror(errno));
890 return NULL;
891 }
892
893 Animation *animation = new Animation;
894 animation->fileName = fn;
895 animation->zip = zip;
896 mLoadedFiles.add(animation->fileName);
897
898 parseAnimationDesc(*animation);
899 preloadZip(*animation);
900
901 mLoadedFiles.remove(fn);
902 return animation;
903}
Damien Bargiacchi97480862016-03-29 14:55:55 -0700904
905bool BootAnimation::updateIsTimeAccurate() {
906 static constexpr long long MAX_TIME_IN_PAST = 60000LL * 60LL * 24LL * 30LL; // 30 days
907 static constexpr long long MAX_TIME_IN_FUTURE = 60000LL * 90LL; // 90 minutes
908
909 if (mTimeIsAccurate) {
910 return true;
911 }
912
913 struct stat statResult;
914 if(stat(ACCURATE_TIME_FLAG_FILE_PATH, &statResult) == 0) {
915 mTimeIsAccurate = true;
916 return true;
917 }
918
919 FILE* file = fopen(LAST_TIME_CHANGED_FILE_PATH, "r");
920 if (file != NULL) {
921 long long lastChangedTime = 0;
922 fscanf(file, "%lld", &lastChangedTime);
923 fclose(file);
924 if (lastChangedTime > 0) {
925 struct timespec now;
926 clock_gettime(CLOCK_REALTIME, &now);
927 // Match the Java timestamp format
928 long long rtcNow = (now.tv_sec * 1000LL) + (now.tv_nsec / 1000000LL);
929 if (lastChangedTime > rtcNow - MAX_TIME_IN_PAST
930 && lastChangedTime < rtcNow + MAX_TIME_IN_FUTURE) {
931 mTimeIsAccurate = true;
932 }
933 }
934 }
935
936 return mTimeIsAccurate;
937}
938
939BootAnimation::TimeCheckThread::TimeCheckThread(BootAnimation* bootAnimation) : Thread(false),
940 mInotifyFd(-1), mSystemWd(-1), mTimeWd(-1), mBootAnimation(bootAnimation) {}
941
942BootAnimation::TimeCheckThread::~TimeCheckThread() {
943 // mInotifyFd may be -1 but that's ok since we're not at risk of attempting to close a valid FD.
944 close(mInotifyFd);
945}
946
947bool BootAnimation::TimeCheckThread::threadLoop() {
948 bool shouldLoop = doThreadLoop() && !mBootAnimation->mTimeIsAccurate
949 && mBootAnimation->mClockEnabled;
950 if (!shouldLoop) {
951 close(mInotifyFd);
952 mInotifyFd = -1;
953 }
954 return shouldLoop;
955}
956
957bool BootAnimation::TimeCheckThread::doThreadLoop() {
958 static constexpr int BUFF_LEN (10 * (sizeof(struct inotify_event) + NAME_MAX + 1));
959
960 // Poll instead of doing a blocking read so the Thread can exit if requested.
961 struct pollfd pfd = { mInotifyFd, POLLIN, 0 };
962 ssize_t pollResult = poll(&pfd, 1, 1000);
963
964 if (pollResult == 0) {
965 return true;
966 } else if (pollResult < 0) {
967 ALOGE("Could not poll inotify events");
968 return false;
969 }
970
971 char buff[BUFF_LEN] __attribute__ ((aligned(__alignof__(struct inotify_event))));;
972 ssize_t length = read(mInotifyFd, buff, BUFF_LEN);
973 if (length == 0) {
974 return true;
975 } else if (length < 0) {
976 ALOGE("Could not read inotify events");
977 return false;
978 }
979
980 const struct inotify_event *event;
981 for (char* ptr = buff; ptr < buff + length; ptr += sizeof(struct inotify_event) + event->len) {
982 event = (const struct inotify_event *) ptr;
983 if (event->wd == mSystemWd && strcmp(SYSTEM_TIME_DIR_NAME, event->name) == 0) {
984 addTimeDirWatch();
985 } else if (event->wd == mTimeWd && (strcmp(LAST_TIME_CHANGED_FILE_NAME, event->name) == 0
986 || strcmp(ACCURATE_TIME_FLAG_FILE_NAME, event->name) == 0)) {
987 return !mBootAnimation->updateIsTimeAccurate();
988 }
989 }
990
991 return true;
992}
993
994void BootAnimation::TimeCheckThread::addTimeDirWatch() {
995 mTimeWd = inotify_add_watch(mInotifyFd, SYSTEM_TIME_DIR_PATH,
996 IN_CLOSE_WRITE | IN_MOVED_TO | IN_ATTRIB);
997 if (mTimeWd > 0) {
998 // No need to watch for the time directory to be created if it already exists
999 inotify_rm_watch(mInotifyFd, mSystemWd);
1000 mSystemWd = -1;
1001 }
1002}
1003
1004status_t BootAnimation::TimeCheckThread::readyToRun() {
1005 mInotifyFd = inotify_init();
1006 if (mInotifyFd < 0) {
1007 ALOGE("Could not initialize inotify fd");
1008 return NO_INIT;
1009 }
1010
1011 mSystemWd = inotify_add_watch(mInotifyFd, SYSTEM_DATA_DIR_PATH, IN_CREATE | IN_ATTRIB);
1012 if (mSystemWd < 0) {
1013 close(mInotifyFd);
1014 mInotifyFd = -1;
1015 ALOGE("Could not add watch for %s", SYSTEM_DATA_DIR_PATH);
1016 return NO_INIT;
1017 }
1018
1019 addTimeDirWatch();
1020
1021 if (mBootAnimation->updateIsTimeAccurate()) {
1022 close(mInotifyFd);
1023 mInotifyFd = -1;
1024 return ALREADY_EXISTS;
1025 }
1026
1027 return NO_ERROR;
1028}
1029
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001030// ---------------------------------------------------------------------------
1031
1032}
1033; // namespace android