blob: 7b4e6eed60940c06fb17278e1ca9eea800fa7a38 [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
Mathias Agopianbc726112009-09-23 15:44:05 -070017#define LOG_TAG "BootAnimation"
18
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080019#include <stdint.h>
20#include <sys/types.h>
21#include <math.h>
22#include <fcntl.h>
23#include <utils/misc.h>
Mathias Agopianb4d5a722009-09-23 17:05:19 -070024#include <signal.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080025
Jason parksbd9a08d2011-01-31 15:04:34 -060026#include <cutils/properties.h>
27
Mathias Agopianb13b9bd2012-02-17 18:27:36 -080028#include <androidfw/AssetManager.h>
Mathias Agopianac31a3b2009-05-21 19:59:24 -070029#include <binder/IPCThreadState.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080030#include <utils/Atomic.h>
31#include <utils/Errors.h>
32#include <utils/Log.h>
Mathias Agopianb13b9bd2012-02-17 18:27:36 -080033#include <utils/threads.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080034
35#include <ui/PixelFormat.h>
36#include <ui/Rect.h>
37#include <ui/Region.h>
38#include <ui/DisplayInfo.h>
Mathias Agopiandff8e582009-05-04 14:17:04 -070039#include <ui/FramebufferNativeWindow.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080040
Jeff Brown0b722fe2012-08-24 22:40:14 -070041#include <gui/ISurfaceComposer.h>
Mathias Agopian8335f1c2012-02-25 18:48:35 -080042#include <gui/Surface.h>
43#include <gui/SurfaceComposerClient.h>
Mathias Agopian000479f2010-02-09 17:46:37 -080044
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080045#include <core/SkBitmap.h>
Mathias Agopian2b99e552011-11-10 15:59:07 -080046#include <core/SkStream.h>
Derek Sollenberger5827cb52013-07-26 14:58:06 -040047#include <core/SkImageDecoder.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080048
49#include <GLES/gl.h>
50#include <GLES/glext.h>
51#include <EGL/eglext.h>
52
53#include "BootAnimation.h"
54
Jim Huangc11f4622010-08-10 03:12:15 +080055#define USER_BOOTANIMATION_FILE "/data/local/bootanimation.zip"
56#define SYSTEM_BOOTANIMATION_FILE "/system/media/bootanimation.zip"
Jason parksbd9a08d2011-01-31 15:04:34 -060057#define SYSTEM_ENCRYPTED_BOOTANIMATION_FILE "/system/media/bootanimation-encrypted.zip"
Kevin Hesterd3782b22012-04-26 10:38:55 -070058#define EXIT_PROP_NAME "service.bootanim.exit"
Jim Huangc11f4622010-08-10 03:12:15 +080059
Mathias Agopiandb7dd2a2012-05-12 15:08:21 -070060extern "C" int clock_nanosleep(clockid_t clock_id, int flags,
61 const struct timespec *request,
62 struct timespec *remain);
63
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080064namespace android {
65
Narayan Kamathafd31e02013-12-03 13:16:03 +000066static const int ANIM_ENTRY_NAME_MAX = 256;
67
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080068// ---------------------------------------------------------------------------
69
Narayan Kamathafd31e02013-12-03 13:16:03 +000070BootAnimation::BootAnimation() : Thread(false), mZip(NULL)
Mathias Agopiana8826d62009-10-01 03:10:14 -070071{
Mathias Agopian627e7b52009-05-21 19:21:59 -070072 mSession = new SurfaceComposerClient();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080073}
74
75BootAnimation::~BootAnimation() {
Narayan Kamathafd31e02013-12-03 13:16:03 +000076 if (mZip != NULL) {
77 delete mZip;
78 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080079}
80
81void BootAnimation::onFirstRef() {
Mathias Agopianbc726112009-09-23 15:44:05 -070082 status_t err = mSession->linkToComposerDeath(this);
Steve Block3762c312012-01-06 19:20:56 +000083 ALOGE_IF(err, "linkToComposerDeath failed (%s) ", strerror(-err));
Mathias Agopian8434c532009-09-23 18:52:49 -070084 if (err == NO_ERROR) {
Mathias Agopianbc726112009-09-23 15:44:05 -070085 run("BootAnimation", PRIORITY_DISPLAY);
86 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080087}
88
Mathias Agopianbc726112009-09-23 15:44:05 -070089sp<SurfaceComposerClient> BootAnimation::session() const {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080090 return mSession;
91}
92
Mathias Agopianbc726112009-09-23 15:44:05 -070093
Narayan Kamathafd31e02013-12-03 13:16:03 +000094void BootAnimation::binderDied(const wp<IBinder>&)
Mathias Agopianbc726112009-09-23 15:44:05 -070095{
96 // woah, surfaceflinger died!
Steve Block5baa3a62011-12-20 16:23:08 +000097 ALOGD("SurfaceFlinger died, exiting...");
Mathias Agopianbc726112009-09-23 15:44:05 -070098
99 // calling requestExit() is not enough here because the Surface code
100 // might be blocked on a condition variable that will never be updated.
101 kill( getpid(), SIGKILL );
102 requestExit();
103}
104
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800105status_t BootAnimation::initTexture(Texture* texture, AssetManager& assets,
106 const char* name) {
107 Asset* asset = assets.open(name, Asset::ACCESS_BUFFER);
108 if (!asset)
109 return NO_INIT;
110 SkBitmap bitmap;
111 SkImageDecoder::DecodeMemory(asset->getBuffer(false), asset->getLength(),
112 &bitmap, SkBitmap::kNo_Config, SkImageDecoder::kDecodePixels_Mode);
113 asset->close();
114 delete asset;
115
116 // ensure we can call getPixels(). No need to call unlock, since the
117 // bitmap will go out of scope when we return from this method.
118 bitmap.lockPixels();
119
120 const int w = bitmap.width();
121 const int h = bitmap.height();
122 const void* p = bitmap.getPixels();
123
124 GLint crop[4] = { 0, h, w, -h };
125 texture->w = w;
126 texture->h = h;
127
128 glGenTextures(1, &texture->name);
129 glBindTexture(GL_TEXTURE_2D, texture->name);
130
131 switch (bitmap.getConfig()) {
132 case SkBitmap::kA8_Config:
133 glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, w, h, 0, GL_ALPHA,
134 GL_UNSIGNED_BYTE, p);
135 break;
136 case SkBitmap::kARGB_4444_Config:
137 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA,
138 GL_UNSIGNED_SHORT_4_4_4_4, p);
139 break;
140 case SkBitmap::kARGB_8888_Config:
141 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA,
142 GL_UNSIGNED_BYTE, p);
143 break;
144 case SkBitmap::kRGB_565_Config:
145 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, w, h, 0, GL_RGB,
146 GL_UNSIGNED_SHORT_5_6_5, p);
147 break;
148 default:
149 break;
150 }
151
152 glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_CROP_RECT_OES, crop);
153 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
154 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
155 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
156 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
157 return NO_ERROR;
158}
159
Mathias Agopiana8826d62009-10-01 03:10:14 -0700160status_t BootAnimation::initTexture(void* buffer, size_t len)
161{
162 //StopWatch watch("blah");
163
164 SkBitmap bitmap;
Mathias Agopian2b99e552011-11-10 15:59:07 -0800165 SkMemoryStream stream(buffer, len);
166 SkImageDecoder* codec = SkImageDecoder::Factory(&stream);
Mathias Agopian2b99e552011-11-10 15:59:07 -0800167 if (codec) {
Elliott Hughesc367d482013-10-29 13:12:55 -0700168 codec->setDitherImage(false);
Mathias Agopian2b99e552011-11-10 15:59:07 -0800169 codec->decode(&stream, &bitmap,
Mathias Agopian60691ce2012-06-11 14:08:02 -0700170 SkBitmap::kARGB_8888_Config,
Mathias Agopian2b99e552011-11-10 15:59:07 -0800171 SkImageDecoder::kDecodePixels_Mode);
172 delete codec;
173 }
Mathias Agopiana8826d62009-10-01 03:10:14 -0700174
175 // ensure we can call getPixels(). No need to call unlock, since the
176 // bitmap will go out of scope when we return from this method.
177 bitmap.lockPixels();
178
179 const int w = bitmap.width();
180 const int h = bitmap.height();
181 const void* p = bitmap.getPixels();
182
183 GLint crop[4] = { 0, h, w, -h };
184 int tw = 1 << (31 - __builtin_clz(w));
185 int th = 1 << (31 - __builtin_clz(h));
186 if (tw < w) tw <<= 1;
187 if (th < h) th <<= 1;
188
189 switch (bitmap.getConfig()) {
190 case SkBitmap::kARGB_8888_Config:
191 if (tw != w || th != h) {
192 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, tw, th, 0, GL_RGBA,
193 GL_UNSIGNED_BYTE, 0);
194 glTexSubImage2D(GL_TEXTURE_2D, 0,
195 0, 0, w, h, GL_RGBA, GL_UNSIGNED_BYTE, p);
196 } else {
197 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, tw, th, 0, GL_RGBA,
198 GL_UNSIGNED_BYTE, p);
199 }
200 break;
201
202 case SkBitmap::kRGB_565_Config:
203 if (tw != w || th != h) {
204 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, tw, th, 0, GL_RGB,
205 GL_UNSIGNED_SHORT_5_6_5, 0);
206 glTexSubImage2D(GL_TEXTURE_2D, 0,
207 0, 0, w, h, GL_RGB, GL_UNSIGNED_SHORT_5_6_5, p);
208 } else {
209 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, tw, th, 0, GL_RGB,
210 GL_UNSIGNED_SHORT_5_6_5, p);
211 }
212 break;
213 default:
214 break;
215 }
216
217 glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_CROP_RECT_OES, crop);
218
219 return NO_ERROR;
220}
221
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800222status_t BootAnimation::readyToRun() {
223 mAssets.addDefaultAssets();
224
Jeff Brown0b722fe2012-08-24 22:40:14 -0700225 sp<IBinder> dtoken(SurfaceComposerClient::getBuiltInDisplay(
226 ISurfaceComposer::eDisplayIdMain));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800227 DisplayInfo dinfo;
Jeff Brown0b722fe2012-08-24 22:40:14 -0700228 status_t status = SurfaceComposerClient::getDisplayInfo(dtoken, &dinfo);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800229 if (status)
230 return -1;
231
232 // create the native surface
Jeff Brown0b722fe2012-08-24 22:40:14 -0700233 sp<SurfaceControl> control = session()->createSurface(String8("BootAnimation"),
234 dinfo.w, dinfo.h, PIXEL_FORMAT_RGB_565);
Mathias Agopian439863f2011-06-28 19:09:31 -0700235
236 SurfaceComposerClient::openGlobalTransaction();
Mathias Agopian17f638b2009-04-16 20:04:08 -0700237 control->setLayer(0x40000000);
Mathias Agopian439863f2011-06-28 19:09:31 -0700238 SurfaceComposerClient::closeGlobalTransaction();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800239
Mathias Agopian17f638b2009-04-16 20:04:08 -0700240 sp<Surface> s = control->getSurface();
241
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800242 // initialize opengl and egl
Mathias Agopian738b9a42009-08-06 16:41:02 -0700243 const EGLint attribs[] = {
Mathias Agopian1b253b72011-08-15 15:20:22 -0700244 EGL_RED_SIZE, 8,
245 EGL_GREEN_SIZE, 8,
246 EGL_BLUE_SIZE, 8,
Mathias Agopiana8826d62009-10-01 03:10:14 -0700247 EGL_DEPTH_SIZE, 0,
248 EGL_NONE
Mathias Agopian738b9a42009-08-06 16:41:02 -0700249 };
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800250 EGLint w, h, dummy;
251 EGLint numConfigs;
252 EGLConfig config;
253 EGLSurface surface;
254 EGLContext context;
Mathias Agopian627e7b52009-05-21 19:21:59 -0700255
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800256 EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
Mathias Agopian627e7b52009-05-21 19:21:59 -0700257
258 eglInitialize(display, 0, 0);
Mathias Agopian1b253b72011-08-15 15:20:22 -0700259 eglChooseConfig(display, attribs, &config, 1, &numConfigs);
Mathias Agopian1473f462009-04-10 14:24:30 -0700260 surface = eglCreateWindowSurface(display, config, s.get(), NULL);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800261 context = eglCreateContext(display, config, NULL, NULL);
262 eglQuerySurface(display, surface, EGL_WIDTH, &w);
263 eglQuerySurface(display, surface, EGL_HEIGHT, &h);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700264
Mathias Agopianabac0102009-07-31 14:47:00 -0700265 if (eglMakeCurrent(display, surface, surface, context) == EGL_FALSE)
266 return NO_INIT;
Mathias Agopiana8826d62009-10-01 03:10:14 -0700267
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800268 mDisplay = display;
269 mContext = context;
270 mSurface = surface;
271 mWidth = w;
272 mHeight = h;
Mathias Agopian17f638b2009-04-16 20:04:08 -0700273 mFlingerSurfaceControl = control;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800274 mFlingerSurface = s;
275
Elliott Hughesc367d482013-10-29 13:12:55 -0700276 // If the device has encryption turned on or is in process
Jason parksbd9a08d2011-01-31 15:04:34 -0600277 // of being encrypted we show the encrypted boot animation.
278 char decrypt[PROPERTY_VALUE_MAX];
279 property_get("vold.decrypt", decrypt, "");
280
281 bool encryptedAnimation = atoi(decrypt) != 0 || !strcmp("trigger_restart_min_framework", decrypt);
282
Narayan Kamathafd31e02013-12-03 13:16:03 +0000283 ZipFileRO* zipFile = NULL;
Jason parksbd9a08d2011-01-31 15:04:34 -0600284 if ((encryptedAnimation &&
285 (access(SYSTEM_ENCRYPTED_BOOTANIMATION_FILE, R_OK) == 0) &&
Narayan Kamathafd31e02013-12-03 13:16:03 +0000286 ((zipFile = ZipFileRO::open(SYSTEM_ENCRYPTED_BOOTANIMATION_FILE)) != NULL)) ||
Jason parksbd9a08d2011-01-31 15:04:34 -0600287
288 ((access(USER_BOOTANIMATION_FILE, R_OK) == 0) &&
Narayan Kamathafd31e02013-12-03 13:16:03 +0000289 ((zipFile = ZipFileRO::open(USER_BOOTANIMATION_FILE)) != NULL)) ||
Jason parksbd9a08d2011-01-31 15:04:34 -0600290
291 ((access(SYSTEM_BOOTANIMATION_FILE, R_OK) == 0) &&
Narayan Kamathafd31e02013-12-03 13:16:03 +0000292 ((zipFile = ZipFileRO::open(SYSTEM_BOOTANIMATION_FILE)) != NULL))) {
293 mZip = zipFile;
Jason parksbd9a08d2011-01-31 15:04:34 -0600294 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800295
296 return NO_ERROR;
297}
298
Mathias Agopiana8826d62009-10-01 03:10:14 -0700299bool BootAnimation::threadLoop()
300{
301 bool r;
Narayan Kamathafd31e02013-12-03 13:16:03 +0000302 // We have no bootanimation file, so we use the stock android logo
303 // animation.
304 if (mZip == NULL) {
Mathias Agopiana8826d62009-10-01 03:10:14 -0700305 r = android();
306 } else {
307 r = movie();
308 }
309
Kevin Hesterd3782b22012-04-26 10:38:55 -0700310 // No need to force exit anymore
311 property_set(EXIT_PROP_NAME, "0");
312
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800313 eglMakeCurrent(mDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
314 eglDestroyContext(mDisplay, mContext);
315 eglDestroySurface(mDisplay, mSurface);
Mathias Agopian6cf0db22009-04-17 19:36:26 -0700316 mFlingerSurface.clear();
Mathias Agopian17f638b2009-04-16 20:04:08 -0700317 mFlingerSurfaceControl.clear();
Mathias Agopian627e7b52009-05-21 19:21:59 -0700318 eglTerminate(mDisplay);
319 IPCThreadState::self()->stopProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800320 return r;
321}
322
Mathias Agopiana8826d62009-10-01 03:10:14 -0700323bool BootAnimation::android()
324{
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700325 initTexture(&mAndroid[0], mAssets, "images/android-logo-mask.png");
326 initTexture(&mAndroid[1], mAssets, "images/android-logo-shine.png");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800327
328 // clear screen
Mathias Agopiana8826d62009-10-01 03:10:14 -0700329 glShadeModel(GL_FLAT);
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700330 glDisable(GL_DITHER);
331 glDisable(GL_SCISSOR_TEST);
Mathias Agopian59f19e42011-05-06 19:22:12 -0700332 glClearColor(0,0,0,1);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800333 glClear(GL_COLOR_BUFFER_BIT);
334 eglSwapBuffers(mDisplay, mSurface);
335
Mathias Agopiana8826d62009-10-01 03:10:14 -0700336 glEnable(GL_TEXTURE_2D);
337 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
338
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700339 const GLint xc = (mWidth - mAndroid[0].w) / 2;
340 const GLint yc = (mHeight - mAndroid[0].h) / 2;
341 const Rect updateRect(xc, yc, xc + mAndroid[0].w, yc + mAndroid[0].h);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800342
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800343 glScissor(updateRect.left, mHeight - updateRect.bottom, updateRect.width(),
344 updateRect.height());
345
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700346 // Blend state
347 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
348 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
349
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800350 const nsecs_t startTime = systemTime();
351 do {
Mathias Agopian13796652009-03-24 22:49:21 -0700352 nsecs_t now = systemTime();
353 double time = now - startTime;
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700354 float t = 4.0f * float(time / us2ns(16667)) / mAndroid[1].w;
355 GLint offset = (1 - (t - floorf(t))) * mAndroid[1].w;
356 GLint x = xc - offset;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800357
Mathias Agopian81668642009-07-28 11:41:30 -0700358 glDisable(GL_SCISSOR_TEST);
359 glClear(GL_COLOR_BUFFER_BIT);
360
361 glEnable(GL_SCISSOR_TEST);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800362 glDisable(GL_BLEND);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800363 glBindTexture(GL_TEXTURE_2D, mAndroid[1].name);
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700364 glDrawTexiOES(x, yc, 0, mAndroid[1].w, mAndroid[1].h);
365 glDrawTexiOES(x + mAndroid[1].w, yc, 0, mAndroid[1].w, mAndroid[1].h);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800366
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700367 glEnable(GL_BLEND);
368 glBindTexture(GL_TEXTURE_2D, mAndroid[0].name);
369 glDrawTexiOES(xc, yc, 0, mAndroid[0].w, mAndroid[0].h);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800370
Mathias Agopian627e7b52009-05-21 19:21:59 -0700371 EGLBoolean res = eglSwapBuffers(mDisplay, mSurface);
372 if (res == EGL_FALSE)
373 break;
374
Mathias Agopian13796652009-03-24 22:49:21 -0700375 // 12fps: don't animate too fast to preserve CPU
376 const nsecs_t sleepTime = 83333 - ns2us(systemTime() - now);
377 if (sleepTime > 0)
Mathias Agopiana8826d62009-10-01 03:10:14 -0700378 usleep(sleepTime);
Kevin Hesterd3782b22012-04-26 10:38:55 -0700379
380 checkExit();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800381 } while (!exitPending());
382
383 glDeleteTextures(1, &mAndroid[0].name);
384 glDeleteTextures(1, &mAndroid[1].name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800385 return false;
386}
387
Mathias Agopiana8826d62009-10-01 03:10:14 -0700388
Kevin Hesterd3782b22012-04-26 10:38:55 -0700389void BootAnimation::checkExit() {
390 // Allow surface flinger to gracefully request shutdown
391 char value[PROPERTY_VALUE_MAX];
392 property_get(EXIT_PROP_NAME, value, "0");
393 int exitnow = atoi(value);
394 if (exitnow) {
395 requestExit();
396 }
397}
398
Mathias Agopiana8826d62009-10-01 03:10:14 -0700399bool BootAnimation::movie()
400{
Narayan Kamathafd31e02013-12-03 13:16:03 +0000401 ZipEntryRO desc = mZip->findEntryByName("desc.txt");
402 ALOGE_IF(!desc, "couldn't find desc.txt");
403 if (!desc) {
404 return false;
405 }
Mathias Agopiana8826d62009-10-01 03:10:14 -0700406
Narayan Kamathafd31e02013-12-03 13:16:03 +0000407 FileMap* descMap = mZip->createEntryFileMap(desc);
408 mZip->releaseEntry(desc);
Steve Block3762c312012-01-06 19:20:56 +0000409 ALOGE_IF(!descMap, "descMap is null");
Mathias Agopiana8826d62009-10-01 03:10:14 -0700410 if (!descMap) {
411 return false;
412 }
413
414 String8 desString((char const*)descMap->getDataPtr(),
415 descMap->getDataLength());
416 char const* s = desString.string();
417
418 Animation animation;
419
420 // Parse the description file
421 for (;;) {
422 const char* endl = strstr(s, "\n");
423 if (!endl) break;
424 String8 line(s, endl - s);
425 const char* l = line.string();
426 int fps, width, height, count, pause;
Narayan Kamathafd31e02013-12-03 13:16:03 +0000427 char path[ANIM_ENTRY_NAME_MAX];
Kevin Hesterd3782b22012-04-26 10:38:55 -0700428 char pathType;
Mathias Agopiana8826d62009-10-01 03:10:14 -0700429 if (sscanf(l, "%d %d %d", &width, &height, &fps) == 3) {
Kevin Hesterd3782b22012-04-26 10:38:55 -0700430 //LOGD("> w=%d, h=%d, fps=%d", width, height, fps);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700431 animation.width = width;
432 animation.height = height;
433 animation.fps = fps;
434 }
Kevin Hesterd3782b22012-04-26 10:38:55 -0700435 else if (sscanf(l, " %c %d %d %s", &pathType, &count, &pause, path) == 4) {
436 //LOGD("> type=%c, count=%d, pause=%d, path=%s", pathType, count, pause, path);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700437 Animation::Part part;
Kevin Hesterd3782b22012-04-26 10:38:55 -0700438 part.playUntilComplete = pathType == 'c';
Mathias Agopiana8826d62009-10-01 03:10:14 -0700439 part.count = count;
440 part.pause = pause;
441 part.path = path;
442 animation.parts.add(part);
443 }
Kevin Hesterd3782b22012-04-26 10:38:55 -0700444
Mathias Agopiana8826d62009-10-01 03:10:14 -0700445 s = ++endl;
446 }
447
448 // read all the data structures
449 const size_t pcount = animation.parts.size();
Narayan Kamathafd31e02013-12-03 13:16:03 +0000450 void *cookie = NULL;
451 if (!mZip->startIteration(&cookie)) {
452 return false;
453 }
454
455 ZipEntryRO entry;
456 char name[ANIM_ENTRY_NAME_MAX];
457 while ((entry = mZip->nextEntry(cookie)) != NULL) {
458 const int foundEntryName = mZip->getEntryFileName(entry, name, ANIM_ENTRY_NAME_MAX);
459 if (foundEntryName > ANIM_ENTRY_NAME_MAX || foundEntryName == -1) {
460 ALOGE("Error fetching entry file name");
461 continue;
462 }
463
464 const String8 entryName(name);
465 const String8 path(entryName.getPathDir());
466 const String8 leaf(entryName.getPathLeaf());
467 if (leaf.size() > 0) {
468 for (size_t j=0 ; j<pcount ; j++) {
469 if (path == animation.parts[j].path) {
470 int method;
471 // supports only stored png files
472 if (mZip->getEntryInfo(entry, &method, NULL, NULL, NULL, NULL, NULL)) {
473 if (method == ZipFileRO::kCompressStored) {
474 FileMap* map = mZip->createEntryFileMap(entry);
475 if (map) {
476 Animation::Frame frame;
477 frame.name = leaf;
478 frame.map = map;
479 Animation::Part& part(animation.parts.editItemAt(j));
480 part.frames.add(frame);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700481 }
482 }
483 }
484 }
485 }
486 }
487 }
488
Narayan Kamathafd31e02013-12-03 13:16:03 +0000489 mZip->endIteration(cookie);
490
Mathias Agopiana8826d62009-10-01 03:10:14 -0700491 // clear screen
492 glShadeModel(GL_FLAT);
493 glDisable(GL_DITHER);
494 glDisable(GL_SCISSOR_TEST);
495 glDisable(GL_BLEND);
Mathias Agopian59f19e42011-05-06 19:22:12 -0700496 glClearColor(0,0,0,1);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700497 glClear(GL_COLOR_BUFFER_BIT);
498
499 eglSwapBuffers(mDisplay, mSurface);
500
501 glBindTexture(GL_TEXTURE_2D, 0);
502 glEnable(GL_TEXTURE_2D);
503 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
504 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
505 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
506 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
507 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
508
509 const int xc = (mWidth - animation.width) / 2;
510 const int yc = ((mHeight - animation.height) / 2);
511 nsecs_t lastFrame = systemTime();
512 nsecs_t frameDuration = s2ns(1) / animation.fps;
513
Mathias Agopian9f3020d2009-11-06 16:30:18 -0800514 Region clearReg(Rect(mWidth, mHeight));
515 clearReg.subtractSelf(Rect(xc, yc, xc+animation.width, yc+animation.height));
516
Narayan Kamathafd31e02013-12-03 13:16:03 +0000517 for (size_t i=0 ; i<pcount ; i++) {
Mathias Agopiana8826d62009-10-01 03:10:14 -0700518 const Animation::Part& part(animation.parts[i]);
519 const size_t fcount = part.frames.size();
520 glBindTexture(GL_TEXTURE_2D, 0);
521
522 for (int r=0 ; !part.count || r<part.count ; r++) {
Kevin Hesterd3782b22012-04-26 10:38:55 -0700523 // Exit any non playuntil complete parts immediately
524 if(exitPending() && !part.playUntilComplete)
525 break;
526
Narayan Kamathafd31e02013-12-03 13:16:03 +0000527 for (size_t j=0 ; j<fcount && (!exitPending() || part.playUntilComplete) ; j++) {
Mathias Agopiana8826d62009-10-01 03:10:14 -0700528 const Animation::Frame& frame(part.frames[j]);
Mathias Agopiandb7dd2a2012-05-12 15:08:21 -0700529 nsecs_t lastFrame = systemTime();
Mathias Agopiana8826d62009-10-01 03:10:14 -0700530
531 if (r > 0) {
532 glBindTexture(GL_TEXTURE_2D, frame.tid);
533 } else {
534 if (part.count != 1) {
535 glGenTextures(1, &frame.tid);
536 glBindTexture(GL_TEXTURE_2D, frame.tid);
537 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
538 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
539 }
540 initTexture(
541 frame.map->getDataPtr(),
542 frame.map->getDataLength());
543 }
544
Mathias Agopian9f3020d2009-11-06 16:30:18 -0800545 if (!clearReg.isEmpty()) {
546 Region::const_iterator head(clearReg.begin());
547 Region::const_iterator tail(clearReg.end());
548 glEnable(GL_SCISSOR_TEST);
549 while (head != tail) {
550 const Rect& r(*head++);
551 glScissor(r.left, mHeight - r.bottom,
552 r.width(), r.height());
553 glClear(GL_COLOR_BUFFER_BIT);
554 }
555 glDisable(GL_SCISSOR_TEST);
556 }
Mathias Agopiana8826d62009-10-01 03:10:14 -0700557 glDrawTexiOES(xc, yc, 0, animation.width, animation.height);
558 eglSwapBuffers(mDisplay, mSurface);
559
560 nsecs_t now = systemTime();
561 nsecs_t delay = frameDuration - (now - lastFrame);
Mathias Agopiandb7dd2a2012-05-12 15:08:21 -0700562 //ALOGD("%lld, %lld", ns2ms(now - lastFrame), ns2ms(delay));
Mathias Agopiana8826d62009-10-01 03:10:14 -0700563 lastFrame = now;
Mathias Agopiandb7dd2a2012-05-12 15:08:21 -0700564
565 if (delay > 0) {
566 struct timespec spec;
567 spec.tv_sec = (now + delay) / 1000000000;
568 spec.tv_nsec = (now + delay) % 1000000000;
569 int err;
570 do {
571 err = clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &spec, NULL);
572 } while (err<0 && errno == EINTR);
573 }
Kevin Hesterd3782b22012-04-26 10:38:55 -0700574
575 checkExit();
Mathias Agopiana8826d62009-10-01 03:10:14 -0700576 }
Kevin Hesterd3782b22012-04-26 10:38:55 -0700577
Mathias Agopiana8826d62009-10-01 03:10:14 -0700578 usleep(part.pause * ns2us(frameDuration));
Kevin Hesterd3782b22012-04-26 10:38:55 -0700579
580 // For infinite parts, we've now played them at least once, so perhaps exit
581 if(exitPending() && !part.count)
582 break;
Mathias Agopiana8826d62009-10-01 03:10:14 -0700583 }
584
585 // free the textures for this part
586 if (part.count != 1) {
Narayan Kamathafd31e02013-12-03 13:16:03 +0000587 for (size_t j=0 ; j<fcount ; j++) {
Mathias Agopiana8826d62009-10-01 03:10:14 -0700588 const Animation::Frame& frame(part.frames[j]);
589 glDeleteTextures(1, &frame.tid);
590 }
591 }
592 }
593
594 return false;
595}
596
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800597// ---------------------------------------------------------------------------
598
599}
600; // namespace android