blob: f3e3affe37939a89acaaf1b2da04ab0cc4e1d80c [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>
Elliott Hughesbb94f312014-10-21 10:41:33 -070025#include <time.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080026
Jason parksbd9a08d2011-01-31 15:04:34 -060027#include <cutils/properties.h>
28
Mathias Agopianb13b9bd2012-02-17 18:27:36 -080029#include <androidfw/AssetManager.h>
Mathias Agopianac31a3b2009-05-21 19:59:24 -070030#include <binder/IPCThreadState.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080031#include <utils/Atomic.h>
32#include <utils/Errors.h>
33#include <utils/Log.h>
Mathias Agopianb13b9bd2012-02-17 18:27:36 -080034#include <utils/threads.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080035
36#include <ui/PixelFormat.h>
37#include <ui/Rect.h>
38#include <ui/Region.h>
39#include <ui/DisplayInfo.h>
Mathias Agopiandff8e582009-05-04 14:17:04 -070040#include <ui/FramebufferNativeWindow.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080041
Jeff Brown0b722fe2012-08-24 22:40:14 -070042#include <gui/ISurfaceComposer.h>
Mathias Agopian8335f1c2012-02-25 18:48:35 -080043#include <gui/Surface.h>
44#include <gui/SurfaceComposerClient.h>
Mathias Agopian000479f2010-02-09 17:46:37 -080045
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080046#include <core/SkBitmap.h>
Mathias Agopian2b99e552011-11-10 15:59:07 -080047#include <core/SkStream.h>
Derek Sollenberger5827cb52013-07-26 14:58:06 -040048#include <core/SkImageDecoder.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080049
50#include <GLES/gl.h>
51#include <GLES/glext.h>
52#include <EGL/eglext.h>
53
54#include "BootAnimation.h"
55
Jim Huangc11f4622010-08-10 03:12:15 +080056#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
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080060namespace android {
61
Narayan Kamathafd31e02013-12-03 13:16:03 +000062static const int ANIM_ENTRY_NAME_MAX = 256;
63
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080064// ---------------------------------------------------------------------------
65
Narayan Kamathafd31e02013-12-03 13:16:03 +000066BootAnimation::BootAnimation() : Thread(false), mZip(NULL)
Mathias Agopiana8826d62009-10-01 03:10:14 -070067{
Mathias Agopian627e7b52009-05-21 19:21:59 -070068 mSession = new SurfaceComposerClient();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080069}
70
71BootAnimation::~BootAnimation() {
Narayan Kamathafd31e02013-12-03 13:16:03 +000072 if (mZip != NULL) {
73 delete mZip;
74 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080075}
76
77void BootAnimation::onFirstRef() {
Mathias Agopianbc726112009-09-23 15:44:05 -070078 status_t err = mSession->linkToComposerDeath(this);
Steve Block3762c312012-01-06 19:20:56 +000079 ALOGE_IF(err, "linkToComposerDeath failed (%s) ", strerror(-err));
Mathias Agopian8434c532009-09-23 18:52:49 -070080 if (err == NO_ERROR) {
Mathias Agopianbc726112009-09-23 15:44:05 -070081 run("BootAnimation", PRIORITY_DISPLAY);
82 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080083}
84
Mathias Agopianbc726112009-09-23 15:44:05 -070085sp<SurfaceComposerClient> BootAnimation::session() const {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080086 return mSession;
87}
88
Mathias Agopianbc726112009-09-23 15:44:05 -070089
Narayan Kamathafd31e02013-12-03 13:16:03 +000090void BootAnimation::binderDied(const wp<IBinder>&)
Mathias Agopianbc726112009-09-23 15:44:05 -070091{
92 // woah, surfaceflinger died!
Steve Block5baa3a62011-12-20 16:23:08 +000093 ALOGD("SurfaceFlinger died, exiting...");
Mathias Agopianbc726112009-09-23 15:44:05 -070094
95 // calling requestExit() is not enough here because the Surface code
96 // might be blocked on a condition variable that will never be updated.
97 kill( getpid(), SIGKILL );
98 requestExit();
99}
100
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800101status_t BootAnimation::initTexture(Texture* texture, AssetManager& assets,
102 const char* name) {
103 Asset* asset = assets.open(name, Asset::ACCESS_BUFFER);
104 if (!asset)
105 return NO_INIT;
106 SkBitmap bitmap;
107 SkImageDecoder::DecodeMemory(asset->getBuffer(false), asset->getLength(),
108 &bitmap, SkBitmap::kNo_Config, SkImageDecoder::kDecodePixels_Mode);
109 asset->close();
110 delete asset;
111
112 // ensure we can call getPixels(). No need to call unlock, since the
113 // bitmap will go out of scope when we return from this method.
114 bitmap.lockPixels();
115
116 const int w = bitmap.width();
117 const int h = bitmap.height();
118 const void* p = bitmap.getPixels();
119
120 GLint crop[4] = { 0, h, w, -h };
121 texture->w = w;
122 texture->h = h;
123
124 glGenTextures(1, &texture->name);
125 glBindTexture(GL_TEXTURE_2D, texture->name);
126
127 switch (bitmap.getConfig()) {
128 case SkBitmap::kA8_Config:
129 glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, w, h, 0, GL_ALPHA,
130 GL_UNSIGNED_BYTE, p);
131 break;
132 case SkBitmap::kARGB_4444_Config:
133 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA,
134 GL_UNSIGNED_SHORT_4_4_4_4, p);
135 break;
136 case SkBitmap::kARGB_8888_Config:
137 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA,
138 GL_UNSIGNED_BYTE, p);
139 break;
140 case SkBitmap::kRGB_565_Config:
141 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, w, h, 0, GL_RGB,
142 GL_UNSIGNED_SHORT_5_6_5, p);
143 break;
144 default:
145 break;
146 }
147
148 glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_CROP_RECT_OES, crop);
149 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
150 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
151 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
152 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
153 return NO_ERROR;
154}
155
Mykola Kondratenko0c1eeb32014-04-15 09:35:44 +0200156status_t BootAnimation::initTexture(const Animation::Frame& frame)
Mathias Agopiana8826d62009-10-01 03:10:14 -0700157{
158 //StopWatch watch("blah");
159
160 SkBitmap bitmap;
Mykola Kondratenko0c1eeb32014-04-15 09:35:44 +0200161 SkMemoryStream stream(frame.map->getDataPtr(), frame.map->getDataLength());
Mathias Agopian2b99e552011-11-10 15:59:07 -0800162 SkImageDecoder* codec = SkImageDecoder::Factory(&stream);
Mathias Agopian2b99e552011-11-10 15:59:07 -0800163 if (codec) {
Elliott Hughesc367d482013-10-29 13:12:55 -0700164 codec->setDitherImage(false);
Mathias Agopian2b99e552011-11-10 15:59:07 -0800165 codec->decode(&stream, &bitmap,
Mathias Agopian60691ce2012-06-11 14:08:02 -0700166 SkBitmap::kARGB_8888_Config,
Mathias Agopian2b99e552011-11-10 15:59:07 -0800167 SkImageDecoder::kDecodePixels_Mode);
168 delete codec;
169 }
Mathias Agopiana8826d62009-10-01 03:10:14 -0700170
Mykola Kondratenko0c1eeb32014-04-15 09:35:44 +0200171 // FileMap memory is never released until application exit.
172 // Release it now as the texture is already loaded and the memory used for
173 // the packed resource can be released.
174 frame.map->release();
175
Mathias Agopiana8826d62009-10-01 03:10:14 -0700176 // ensure we can call getPixels(). No need to call unlock, since the
177 // bitmap will go out of scope when we return from this method.
178 bitmap.lockPixels();
179
180 const int w = bitmap.width();
181 const int h = bitmap.height();
182 const void* p = bitmap.getPixels();
183
184 GLint crop[4] = { 0, h, w, -h };
185 int tw = 1 << (31 - __builtin_clz(w));
186 int th = 1 << (31 - __builtin_clz(h));
187 if (tw < w) tw <<= 1;
188 if (th < h) th <<= 1;
189
190 switch (bitmap.getConfig()) {
191 case SkBitmap::kARGB_8888_Config:
192 if (tw != w || th != h) {
193 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, tw, th, 0, GL_RGBA,
194 GL_UNSIGNED_BYTE, 0);
195 glTexSubImage2D(GL_TEXTURE_2D, 0,
196 0, 0, w, h, GL_RGBA, GL_UNSIGNED_BYTE, p);
197 } else {
198 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, tw, th, 0, GL_RGBA,
199 GL_UNSIGNED_BYTE, p);
200 }
201 break;
202
203 case SkBitmap::kRGB_565_Config:
204 if (tw != w || th != h) {
205 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, tw, th, 0, GL_RGB,
206 GL_UNSIGNED_SHORT_5_6_5, 0);
207 glTexSubImage2D(GL_TEXTURE_2D, 0,
208 0, 0, w, h, GL_RGB, GL_UNSIGNED_SHORT_5_6_5, p);
209 } else {
210 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, tw, th, 0, GL_RGB,
211 GL_UNSIGNED_SHORT_5_6_5, p);
212 }
213 break;
214 default:
215 break;
216 }
217
218 glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_CROP_RECT_OES, crop);
219
220 return NO_ERROR;
221}
222
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800223status_t BootAnimation::readyToRun() {
224 mAssets.addDefaultAssets();
225
Jeff Brown0b722fe2012-08-24 22:40:14 -0700226 sp<IBinder> dtoken(SurfaceComposerClient::getBuiltInDisplay(
227 ISurfaceComposer::eDisplayIdMain));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800228 DisplayInfo dinfo;
Jeff Brown0b722fe2012-08-24 22:40:14 -0700229 status_t status = SurfaceComposerClient::getDisplayInfo(dtoken, &dinfo);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800230 if (status)
231 return -1;
232
233 // create the native surface
Jeff Brown0b722fe2012-08-24 22:40:14 -0700234 sp<SurfaceControl> control = session()->createSurface(String8("BootAnimation"),
235 dinfo.w, dinfo.h, PIXEL_FORMAT_RGB_565);
Mathias Agopian439863f2011-06-28 19:09:31 -0700236
237 SurfaceComposerClient::openGlobalTransaction();
Mathias Agopian17f638b2009-04-16 20:04:08 -0700238 control->setLayer(0x40000000);
Mathias Agopian439863f2011-06-28 19:09:31 -0700239 SurfaceComposerClient::closeGlobalTransaction();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800240
Mathias Agopian17f638b2009-04-16 20:04:08 -0700241 sp<Surface> s = control->getSurface();
242
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800243 // initialize opengl and egl
Mathias Agopian738b9a42009-08-06 16:41:02 -0700244 const EGLint attribs[] = {
Mathias Agopian1b253b72011-08-15 15:20:22 -0700245 EGL_RED_SIZE, 8,
246 EGL_GREEN_SIZE, 8,
247 EGL_BLUE_SIZE, 8,
Mathias Agopiana8826d62009-10-01 03:10:14 -0700248 EGL_DEPTH_SIZE, 0,
249 EGL_NONE
Mathias Agopian738b9a42009-08-06 16:41:02 -0700250 };
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800251 EGLint w, h, dummy;
252 EGLint numConfigs;
253 EGLConfig config;
254 EGLSurface surface;
255 EGLContext context;
Mathias Agopian627e7b52009-05-21 19:21:59 -0700256
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800257 EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
Mathias Agopian627e7b52009-05-21 19:21:59 -0700258
259 eglInitialize(display, 0, 0);
Mathias Agopian1b253b72011-08-15 15:20:22 -0700260 eglChooseConfig(display, attribs, &config, 1, &numConfigs);
Mathias Agopian1473f462009-04-10 14:24:30 -0700261 surface = eglCreateWindowSurface(display, config, s.get(), NULL);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800262 context = eglCreateContext(display, config, NULL, NULL);
263 eglQuerySurface(display, surface, EGL_WIDTH, &w);
264 eglQuerySurface(display, surface, EGL_HEIGHT, &h);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700265
Mathias Agopianabac0102009-07-31 14:47:00 -0700266 if (eglMakeCurrent(display, surface, surface, context) == EGL_FALSE)
267 return NO_INIT;
Mathias Agopiana8826d62009-10-01 03:10:14 -0700268
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800269 mDisplay = display;
270 mContext = context;
271 mSurface = surface;
272 mWidth = w;
273 mHeight = h;
Mathias Agopian17f638b2009-04-16 20:04:08 -0700274 mFlingerSurfaceControl = control;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800275 mFlingerSurface = s;
276
Elliott Hughesc367d482013-10-29 13:12:55 -0700277 // If the device has encryption turned on or is in process
Jason parksbd9a08d2011-01-31 15:04:34 -0600278 // of being encrypted we show the encrypted boot animation.
279 char decrypt[PROPERTY_VALUE_MAX];
280 property_get("vold.decrypt", decrypt, "");
281
282 bool encryptedAnimation = atoi(decrypt) != 0 || !strcmp("trigger_restart_min_framework", decrypt);
283
Narayan Kamathafd31e02013-12-03 13:16:03 +0000284 ZipFileRO* zipFile = NULL;
Jason parksbd9a08d2011-01-31 15:04:34 -0600285 if ((encryptedAnimation &&
286 (access(SYSTEM_ENCRYPTED_BOOTANIMATION_FILE, R_OK) == 0) &&
Narayan Kamathafd31e02013-12-03 13:16:03 +0000287 ((zipFile = ZipFileRO::open(SYSTEM_ENCRYPTED_BOOTANIMATION_FILE)) != NULL)) ||
Jason parksbd9a08d2011-01-31 15:04:34 -0600288
Jason parksbd9a08d2011-01-31 15:04:34 -0600289 ((access(SYSTEM_BOOTANIMATION_FILE, R_OK) == 0) &&
Narayan Kamathafd31e02013-12-03 13:16:03 +0000290 ((zipFile = ZipFileRO::open(SYSTEM_BOOTANIMATION_FILE)) != NULL))) {
291 mZip = zipFile;
Jason parksbd9a08d2011-01-31 15:04:34 -0600292 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800293
294 return NO_ERROR;
295}
296
Mathias Agopiana8826d62009-10-01 03:10:14 -0700297bool BootAnimation::threadLoop()
298{
299 bool r;
Narayan Kamathafd31e02013-12-03 13:16:03 +0000300 // We have no bootanimation file, so we use the stock android logo
301 // animation.
302 if (mZip == NULL) {
Mathias Agopiana8826d62009-10-01 03:10:14 -0700303 r = android();
304 } else {
305 r = movie();
306 }
307
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800308 eglMakeCurrent(mDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
309 eglDestroyContext(mDisplay, mContext);
310 eglDestroySurface(mDisplay, mSurface);
Mathias Agopian6cf0db22009-04-17 19:36:26 -0700311 mFlingerSurface.clear();
Mathias Agopian17f638b2009-04-16 20:04:08 -0700312 mFlingerSurfaceControl.clear();
Mathias Agopian627e7b52009-05-21 19:21:59 -0700313 eglTerminate(mDisplay);
314 IPCThreadState::self()->stopProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800315 return r;
316}
317
Mathias Agopiana8826d62009-10-01 03:10:14 -0700318bool BootAnimation::android()
319{
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700320 initTexture(&mAndroid[0], mAssets, "images/android-logo-mask.png");
321 initTexture(&mAndroid[1], mAssets, "images/android-logo-shine.png");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800322
323 // clear screen
Mathias Agopiana8826d62009-10-01 03:10:14 -0700324 glShadeModel(GL_FLAT);
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700325 glDisable(GL_DITHER);
326 glDisable(GL_SCISSOR_TEST);
Mathias Agopian59f19e42011-05-06 19:22:12 -0700327 glClearColor(0,0,0,1);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800328 glClear(GL_COLOR_BUFFER_BIT);
329 eglSwapBuffers(mDisplay, mSurface);
330
Mathias Agopiana8826d62009-10-01 03:10:14 -0700331 glEnable(GL_TEXTURE_2D);
332 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
333
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700334 const GLint xc = (mWidth - mAndroid[0].w) / 2;
335 const GLint yc = (mHeight - mAndroid[0].h) / 2;
336 const Rect updateRect(xc, yc, xc + mAndroid[0].w, yc + mAndroid[0].h);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800337
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800338 glScissor(updateRect.left, mHeight - updateRect.bottom, updateRect.width(),
339 updateRect.height());
340
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700341 // Blend state
342 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
343 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
344
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800345 const nsecs_t startTime = systemTime();
346 do {
Mathias Agopian13796652009-03-24 22:49:21 -0700347 nsecs_t now = systemTime();
348 double time = now - startTime;
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700349 float t = 4.0f * float(time / us2ns(16667)) / mAndroid[1].w;
350 GLint offset = (1 - (t - floorf(t))) * mAndroid[1].w;
351 GLint x = xc - offset;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800352
Mathias Agopian81668642009-07-28 11:41:30 -0700353 glDisable(GL_SCISSOR_TEST);
354 glClear(GL_COLOR_BUFFER_BIT);
355
356 glEnable(GL_SCISSOR_TEST);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800357 glDisable(GL_BLEND);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800358 glBindTexture(GL_TEXTURE_2D, mAndroid[1].name);
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700359 glDrawTexiOES(x, yc, 0, mAndroid[1].w, mAndroid[1].h);
360 glDrawTexiOES(x + mAndroid[1].w, yc, 0, mAndroid[1].w, mAndroid[1].h);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800361
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700362 glEnable(GL_BLEND);
363 glBindTexture(GL_TEXTURE_2D, mAndroid[0].name);
364 glDrawTexiOES(xc, yc, 0, mAndroid[0].w, mAndroid[0].h);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800365
Mathias Agopian627e7b52009-05-21 19:21:59 -0700366 EGLBoolean res = eglSwapBuffers(mDisplay, mSurface);
367 if (res == EGL_FALSE)
368 break;
369
Mathias Agopian13796652009-03-24 22:49:21 -0700370 // 12fps: don't animate too fast to preserve CPU
371 const nsecs_t sleepTime = 83333 - ns2us(systemTime() - now);
372 if (sleepTime > 0)
Mathias Agopiana8826d62009-10-01 03:10:14 -0700373 usleep(sleepTime);
Kevin Hesterd3782b22012-04-26 10:38:55 -0700374
375 checkExit();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800376 } while (!exitPending());
377
378 glDeleteTextures(1, &mAndroid[0].name);
379 glDeleteTextures(1, &mAndroid[1].name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800380 return false;
381}
382
Mathias Agopiana8826d62009-10-01 03:10:14 -0700383
Kevin Hesterd3782b22012-04-26 10:38:55 -0700384void BootAnimation::checkExit() {
385 // Allow surface flinger to gracefully request shutdown
386 char value[PROPERTY_VALUE_MAX];
387 property_get(EXIT_PROP_NAME, value, "0");
388 int exitnow = atoi(value);
389 if (exitnow) {
390 requestExit();
391 }
392}
393
Mathias Agopiana8826d62009-10-01 03:10:14 -0700394bool BootAnimation::movie()
395{
Narayan Kamathafd31e02013-12-03 13:16:03 +0000396 ZipEntryRO desc = mZip->findEntryByName("desc.txt");
397 ALOGE_IF(!desc, "couldn't find desc.txt");
398 if (!desc) {
399 return false;
400 }
Mathias Agopiana8826d62009-10-01 03:10:14 -0700401
Narayan Kamathafd31e02013-12-03 13:16:03 +0000402 FileMap* descMap = mZip->createEntryFileMap(desc);
403 mZip->releaseEntry(desc);
Steve Block3762c312012-01-06 19:20:56 +0000404 ALOGE_IF(!descMap, "descMap is null");
Mathias Agopiana8826d62009-10-01 03:10:14 -0700405 if (!descMap) {
406 return false;
407 }
408
409 String8 desString((char const*)descMap->getDataPtr(),
410 descMap->getDataLength());
Mykola Kondratenko0c1eeb32014-04-15 09:35:44 +0200411 descMap->release();
Mathias Agopiana8826d62009-10-01 03:10:14 -0700412 char const* s = desString.string();
413
414 Animation animation;
415
416 // Parse the description file
417 for (;;) {
418 const char* endl = strstr(s, "\n");
419 if (!endl) break;
420 String8 line(s, endl - s);
421 const char* l = line.string();
422 int fps, width, height, count, pause;
Narayan Kamathafd31e02013-12-03 13:16:03 +0000423 char path[ANIM_ENTRY_NAME_MAX];
Kevin Hesterd3782b22012-04-26 10:38:55 -0700424 char pathType;
Mathias Agopiana8826d62009-10-01 03:10:14 -0700425 if (sscanf(l, "%d %d %d", &width, &height, &fps) == 3) {
Kevin Hesterd3782b22012-04-26 10:38:55 -0700426 //LOGD("> w=%d, h=%d, fps=%d", width, height, fps);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700427 animation.width = width;
428 animation.height = height;
429 animation.fps = fps;
430 }
Kevin Hesterd3782b22012-04-26 10:38:55 -0700431 else if (sscanf(l, " %c %d %d %s", &pathType, &count, &pause, path) == 4) {
432 //LOGD("> type=%c, count=%d, pause=%d, path=%s", pathType, count, pause, path);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700433 Animation::Part part;
Kevin Hesterd3782b22012-04-26 10:38:55 -0700434 part.playUntilComplete = pathType == 'c';
Mathias Agopiana8826d62009-10-01 03:10:14 -0700435 part.count = count;
436 part.pause = pause;
437 part.path = path;
438 animation.parts.add(part);
439 }
Kevin Hesterd3782b22012-04-26 10:38:55 -0700440
Mathias Agopiana8826d62009-10-01 03:10:14 -0700441 s = ++endl;
442 }
443
444 // read all the data structures
445 const size_t pcount = animation.parts.size();
Narayan Kamathafd31e02013-12-03 13:16:03 +0000446 void *cookie = NULL;
447 if (!mZip->startIteration(&cookie)) {
448 return false;
449 }
450
451 ZipEntryRO entry;
452 char name[ANIM_ENTRY_NAME_MAX];
453 while ((entry = mZip->nextEntry(cookie)) != NULL) {
454 const int foundEntryName = mZip->getEntryFileName(entry, name, ANIM_ENTRY_NAME_MAX);
455 if (foundEntryName > ANIM_ENTRY_NAME_MAX || foundEntryName == -1) {
456 ALOGE("Error fetching entry file name");
457 continue;
458 }
459
460 const String8 entryName(name);
461 const String8 path(entryName.getPathDir());
462 const String8 leaf(entryName.getPathLeaf());
463 if (leaf.size() > 0) {
464 for (size_t j=0 ; j<pcount ; j++) {
465 if (path == animation.parts[j].path) {
466 int method;
467 // supports only stored png files
468 if (mZip->getEntryInfo(entry, &method, NULL, NULL, NULL, NULL, NULL)) {
469 if (method == ZipFileRO::kCompressStored) {
470 FileMap* map = mZip->createEntryFileMap(entry);
471 if (map) {
472 Animation::Frame frame;
473 frame.name = leaf;
474 frame.map = map;
475 Animation::Part& part(animation.parts.editItemAt(j));
476 part.frames.add(frame);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700477 }
478 }
479 }
480 }
481 }
482 }
483 }
484
Narayan Kamathafd31e02013-12-03 13:16:03 +0000485 mZip->endIteration(cookie);
486
Mathias Agopiana8826d62009-10-01 03:10:14 -0700487 // clear screen
488 glShadeModel(GL_FLAT);
489 glDisable(GL_DITHER);
490 glDisable(GL_SCISSOR_TEST);
491 glDisable(GL_BLEND);
Mathias Agopian59f19e42011-05-06 19:22:12 -0700492 glClearColor(0,0,0,1);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700493 glClear(GL_COLOR_BUFFER_BIT);
494
495 eglSwapBuffers(mDisplay, mSurface);
496
497 glBindTexture(GL_TEXTURE_2D, 0);
498 glEnable(GL_TEXTURE_2D);
499 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
500 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
501 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
502 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
503 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
504
505 const int xc = (mWidth - animation.width) / 2;
506 const int yc = ((mHeight - animation.height) / 2);
507 nsecs_t lastFrame = systemTime();
508 nsecs_t frameDuration = s2ns(1) / animation.fps;
509
Mathias Agopian9f3020d2009-11-06 16:30:18 -0800510 Region clearReg(Rect(mWidth, mHeight));
511 clearReg.subtractSelf(Rect(xc, yc, xc+animation.width, yc+animation.height));
512
Narayan Kamathafd31e02013-12-03 13:16:03 +0000513 for (size_t i=0 ; i<pcount ; i++) {
Mathias Agopiana8826d62009-10-01 03:10:14 -0700514 const Animation::Part& part(animation.parts[i]);
515 const size_t fcount = part.frames.size();
516 glBindTexture(GL_TEXTURE_2D, 0);
517
518 for (int r=0 ; !part.count || r<part.count ; r++) {
Kevin Hesterd3782b22012-04-26 10:38:55 -0700519 // Exit any non playuntil complete parts immediately
520 if(exitPending() && !part.playUntilComplete)
521 break;
522
Narayan Kamathafd31e02013-12-03 13:16:03 +0000523 for (size_t j=0 ; j<fcount && (!exitPending() || part.playUntilComplete) ; j++) {
Mathias Agopiana8826d62009-10-01 03:10:14 -0700524 const Animation::Frame& frame(part.frames[j]);
Mathias Agopiandb7dd2a2012-05-12 15:08:21 -0700525 nsecs_t lastFrame = systemTime();
Mathias Agopiana8826d62009-10-01 03:10:14 -0700526
527 if (r > 0) {
528 glBindTexture(GL_TEXTURE_2D, frame.tid);
529 } else {
530 if (part.count != 1) {
531 glGenTextures(1, &frame.tid);
532 glBindTexture(GL_TEXTURE_2D, frame.tid);
533 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
534 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
535 }
Mykola Kondratenko0c1eeb32014-04-15 09:35:44 +0200536 initTexture(frame);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700537 }
538
Mathias Agopian9f3020d2009-11-06 16:30:18 -0800539 if (!clearReg.isEmpty()) {
540 Region::const_iterator head(clearReg.begin());
541 Region::const_iterator tail(clearReg.end());
542 glEnable(GL_SCISSOR_TEST);
543 while (head != tail) {
544 const Rect& r(*head++);
545 glScissor(r.left, mHeight - r.bottom,
546 r.width(), r.height());
547 glClear(GL_COLOR_BUFFER_BIT);
548 }
549 glDisable(GL_SCISSOR_TEST);
550 }
Mathias Agopiana8826d62009-10-01 03:10:14 -0700551 glDrawTexiOES(xc, yc, 0, animation.width, animation.height);
552 eglSwapBuffers(mDisplay, mSurface);
553
554 nsecs_t now = systemTime();
555 nsecs_t delay = frameDuration - (now - lastFrame);
Mathias Agopiandb7dd2a2012-05-12 15:08:21 -0700556 //ALOGD("%lld, %lld", ns2ms(now - lastFrame), ns2ms(delay));
Mathias Agopiana8826d62009-10-01 03:10:14 -0700557 lastFrame = now;
Mathias Agopiandb7dd2a2012-05-12 15:08:21 -0700558
559 if (delay > 0) {
560 struct timespec spec;
561 spec.tv_sec = (now + delay) / 1000000000;
562 spec.tv_nsec = (now + delay) % 1000000000;
563 int err;
564 do {
565 err = clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &spec, NULL);
566 } while (err<0 && errno == EINTR);
567 }
Kevin Hesterd3782b22012-04-26 10:38:55 -0700568
569 checkExit();
Mathias Agopiana8826d62009-10-01 03:10:14 -0700570 }
Kevin Hesterd3782b22012-04-26 10:38:55 -0700571
Mathias Agopiana8826d62009-10-01 03:10:14 -0700572 usleep(part.pause * ns2us(frameDuration));
Kevin Hesterd3782b22012-04-26 10:38:55 -0700573
574 // For infinite parts, we've now played them at least once, so perhaps exit
575 if(exitPending() && !part.count)
576 break;
Mathias Agopiana8826d62009-10-01 03:10:14 -0700577 }
578
579 // free the textures for this part
580 if (part.count != 1) {
Narayan Kamathafd31e02013-12-03 13:16:03 +0000581 for (size_t j=0 ; j<fcount ; j++) {
Mathias Agopiana8826d62009-10-01 03:10:14 -0700582 const Animation::Frame& frame(part.frames[j]);
583 glDeleteTextures(1, &frame.tid);
584 }
585 }
586 }
587
588 return false;
589}
590
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800591// ---------------------------------------------------------------------------
592
593}
594; // namespace android