blob: 7f7ae2c2f150799003e18590efd11e82436ff8e0 [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 SYSTEM_BOOTANIMATION_FILE "/system/media/bootanimation.zip"
Jason parksbd9a08d2011-01-31 15:04:34 -060056#define SYSTEM_ENCRYPTED_BOOTANIMATION_FILE "/system/media/bootanimation-encrypted.zip"
Kevin Hesterd3782b22012-04-26 10:38:55 -070057#define EXIT_PROP_NAME "service.bootanim.exit"
Jim Huangc11f4622010-08-10 03:12:15 +080058
Mathias Agopiandb7dd2a2012-05-12 15:08:21 -070059extern "C" int clock_nanosleep(clockid_t clock_id, int flags,
60 const struct timespec *request,
61 struct timespec *remain);
62
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080063namespace android {
64
Narayan Kamathafd31e02013-12-03 13:16:03 +000065static const int ANIM_ENTRY_NAME_MAX = 256;
66
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080067// ---------------------------------------------------------------------------
68
Narayan Kamathafd31e02013-12-03 13:16:03 +000069BootAnimation::BootAnimation() : Thread(false), mZip(NULL)
Mathias Agopiana8826d62009-10-01 03:10:14 -070070{
Mathias Agopian627e7b52009-05-21 19:21:59 -070071 mSession = new SurfaceComposerClient();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080072}
73
74BootAnimation::~BootAnimation() {
Narayan Kamathafd31e02013-12-03 13:16:03 +000075 if (mZip != NULL) {
76 delete mZip;
77 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080078}
79
80void BootAnimation::onFirstRef() {
Mathias Agopianbc726112009-09-23 15:44:05 -070081 status_t err = mSession->linkToComposerDeath(this);
Steve Block3762c312012-01-06 19:20:56 +000082 ALOGE_IF(err, "linkToComposerDeath failed (%s) ", strerror(-err));
Mathias Agopian8434c532009-09-23 18:52:49 -070083 if (err == NO_ERROR) {
Mathias Agopianbc726112009-09-23 15:44:05 -070084 run("BootAnimation", PRIORITY_DISPLAY);
85 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080086}
87
Mathias Agopianbc726112009-09-23 15:44:05 -070088sp<SurfaceComposerClient> BootAnimation::session() const {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080089 return mSession;
90}
91
Mathias Agopianbc726112009-09-23 15:44:05 -070092
Narayan Kamathafd31e02013-12-03 13:16:03 +000093void BootAnimation::binderDied(const wp<IBinder>&)
Mathias Agopianbc726112009-09-23 15:44:05 -070094{
95 // woah, surfaceflinger died!
Steve Block5baa3a62011-12-20 16:23:08 +000096 ALOGD("SurfaceFlinger died, exiting...");
Mathias Agopianbc726112009-09-23 15:44:05 -070097
98 // calling requestExit() is not enough here because the Surface code
99 // might be blocked on a condition variable that will never be updated.
100 kill( getpid(), SIGKILL );
101 requestExit();
102}
103
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800104status_t BootAnimation::initTexture(Texture* texture, AssetManager& assets,
105 const char* name) {
106 Asset* asset = assets.open(name, Asset::ACCESS_BUFFER);
107 if (!asset)
108 return NO_INIT;
109 SkBitmap bitmap;
110 SkImageDecoder::DecodeMemory(asset->getBuffer(false), asset->getLength(),
111 &bitmap, SkBitmap::kNo_Config, SkImageDecoder::kDecodePixels_Mode);
112 asset->close();
113 delete asset;
114
115 // ensure we can call getPixels(). No need to call unlock, since the
116 // bitmap will go out of scope when we return from this method.
117 bitmap.lockPixels();
118
119 const int w = bitmap.width();
120 const int h = bitmap.height();
121 const void* p = bitmap.getPixels();
122
123 GLint crop[4] = { 0, h, w, -h };
124 texture->w = w;
125 texture->h = h;
126
127 glGenTextures(1, &texture->name);
128 glBindTexture(GL_TEXTURE_2D, texture->name);
129
130 switch (bitmap.getConfig()) {
131 case SkBitmap::kA8_Config:
132 glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, w, h, 0, GL_ALPHA,
133 GL_UNSIGNED_BYTE, p);
134 break;
135 case SkBitmap::kARGB_4444_Config:
136 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA,
137 GL_UNSIGNED_SHORT_4_4_4_4, p);
138 break;
139 case SkBitmap::kARGB_8888_Config:
140 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA,
141 GL_UNSIGNED_BYTE, p);
142 break;
143 case SkBitmap::kRGB_565_Config:
144 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, w, h, 0, GL_RGB,
145 GL_UNSIGNED_SHORT_5_6_5, p);
146 break;
147 default:
148 break;
149 }
150
151 glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_CROP_RECT_OES, crop);
152 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
153 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
154 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
155 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
156 return NO_ERROR;
157}
158
Mykola Kondratenko0c1eeb32014-04-15 09:35:44 +0200159status_t BootAnimation::initTexture(const Animation::Frame& frame)
Mathias Agopiana8826d62009-10-01 03:10:14 -0700160{
161 //StopWatch watch("blah");
162
163 SkBitmap bitmap;
Mykola Kondratenko0c1eeb32014-04-15 09:35:44 +0200164 SkMemoryStream stream(frame.map->getDataPtr(), frame.map->getDataLength());
Mathias Agopian2b99e552011-11-10 15:59:07 -0800165 SkImageDecoder* codec = SkImageDecoder::Factory(&stream);
Mathias Agopian2b99e552011-11-10 15:59:07 -0800166 if (codec) {
Elliott Hughesc367d482013-10-29 13:12:55 -0700167 codec->setDitherImage(false);
Mathias Agopian2b99e552011-11-10 15:59:07 -0800168 codec->decode(&stream, &bitmap,
Mathias Agopian60691ce2012-06-11 14:08:02 -0700169 SkBitmap::kARGB_8888_Config,
Mathias Agopian2b99e552011-11-10 15:59:07 -0800170 SkImageDecoder::kDecodePixels_Mode);
171 delete codec;
172 }
Mathias Agopiana8826d62009-10-01 03:10:14 -0700173
Mykola Kondratenko0c1eeb32014-04-15 09:35:44 +0200174 // FileMap memory is never released until application exit.
175 // Release it now as the texture is already loaded and the memory used for
176 // the packed resource can be released.
177 frame.map->release();
178
Mathias Agopiana8826d62009-10-01 03:10:14 -0700179 // ensure we can call getPixels(). No need to call unlock, since the
180 // bitmap will go out of scope when we return from this method.
181 bitmap.lockPixels();
182
183 const int w = bitmap.width();
184 const int h = bitmap.height();
185 const void* p = bitmap.getPixels();
186
187 GLint crop[4] = { 0, h, w, -h };
188 int tw = 1 << (31 - __builtin_clz(w));
189 int th = 1 << (31 - __builtin_clz(h));
190 if (tw < w) tw <<= 1;
191 if (th < h) th <<= 1;
192
193 switch (bitmap.getConfig()) {
194 case SkBitmap::kARGB_8888_Config:
195 if (tw != w || th != h) {
196 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, tw, th, 0, GL_RGBA,
197 GL_UNSIGNED_BYTE, 0);
198 glTexSubImage2D(GL_TEXTURE_2D, 0,
199 0, 0, w, h, GL_RGBA, GL_UNSIGNED_BYTE, p);
200 } else {
201 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, tw, th, 0, GL_RGBA,
202 GL_UNSIGNED_BYTE, p);
203 }
204 break;
205
206 case SkBitmap::kRGB_565_Config:
207 if (tw != w || th != h) {
208 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, tw, th, 0, GL_RGB,
209 GL_UNSIGNED_SHORT_5_6_5, 0);
210 glTexSubImage2D(GL_TEXTURE_2D, 0,
211 0, 0, w, h, GL_RGB, GL_UNSIGNED_SHORT_5_6_5, p);
212 } else {
213 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, tw, th, 0, GL_RGB,
214 GL_UNSIGNED_SHORT_5_6_5, p);
215 }
216 break;
217 default:
218 break;
219 }
220
221 glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_CROP_RECT_OES, crop);
222
223 return NO_ERROR;
224}
225
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800226status_t BootAnimation::readyToRun() {
227 mAssets.addDefaultAssets();
228
Jeff Brown0b722fe2012-08-24 22:40:14 -0700229 sp<IBinder> dtoken(SurfaceComposerClient::getBuiltInDisplay(
230 ISurfaceComposer::eDisplayIdMain));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800231 DisplayInfo dinfo;
Jeff Brown0b722fe2012-08-24 22:40:14 -0700232 status_t status = SurfaceComposerClient::getDisplayInfo(dtoken, &dinfo);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800233 if (status)
234 return -1;
235
236 // create the native surface
Jeff Brown0b722fe2012-08-24 22:40:14 -0700237 sp<SurfaceControl> control = session()->createSurface(String8("BootAnimation"),
238 dinfo.w, dinfo.h, PIXEL_FORMAT_RGB_565);
Mathias Agopian439863f2011-06-28 19:09:31 -0700239
240 SurfaceComposerClient::openGlobalTransaction();
Mathias Agopian17f638b2009-04-16 20:04:08 -0700241 control->setLayer(0x40000000);
Mathias Agopian439863f2011-06-28 19:09:31 -0700242 SurfaceComposerClient::closeGlobalTransaction();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800243
Mathias Agopian17f638b2009-04-16 20:04:08 -0700244 sp<Surface> s = control->getSurface();
245
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800246 // initialize opengl and egl
Mathias Agopian738b9a42009-08-06 16:41:02 -0700247 const EGLint attribs[] = {
Mathias Agopian1b253b72011-08-15 15:20:22 -0700248 EGL_RED_SIZE, 8,
249 EGL_GREEN_SIZE, 8,
250 EGL_BLUE_SIZE, 8,
Mathias Agopiana8826d62009-10-01 03:10:14 -0700251 EGL_DEPTH_SIZE, 0,
252 EGL_NONE
Mathias Agopian738b9a42009-08-06 16:41:02 -0700253 };
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800254 EGLint w, h, dummy;
255 EGLint numConfigs;
256 EGLConfig config;
257 EGLSurface surface;
258 EGLContext context;
Mathias Agopian627e7b52009-05-21 19:21:59 -0700259
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800260 EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
Mathias Agopian627e7b52009-05-21 19:21:59 -0700261
262 eglInitialize(display, 0, 0);
Mathias Agopian1b253b72011-08-15 15:20:22 -0700263 eglChooseConfig(display, attribs, &config, 1, &numConfigs);
Mathias Agopian1473f462009-04-10 14:24:30 -0700264 surface = eglCreateWindowSurface(display, config, s.get(), NULL);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800265 context = eglCreateContext(display, config, NULL, NULL);
266 eglQuerySurface(display, surface, EGL_WIDTH, &w);
267 eglQuerySurface(display, surface, EGL_HEIGHT, &h);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700268
Mathias Agopianabac0102009-07-31 14:47:00 -0700269 if (eglMakeCurrent(display, surface, surface, context) == EGL_FALSE)
270 return NO_INIT;
Mathias Agopiana8826d62009-10-01 03:10:14 -0700271
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800272 mDisplay = display;
273 mContext = context;
274 mSurface = surface;
275 mWidth = w;
276 mHeight = h;
Mathias Agopian17f638b2009-04-16 20:04:08 -0700277 mFlingerSurfaceControl = control;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800278 mFlingerSurface = s;
279
Elliott Hughesc367d482013-10-29 13:12:55 -0700280 // If the device has encryption turned on or is in process
Jason parksbd9a08d2011-01-31 15:04:34 -0600281 // of being encrypted we show the encrypted boot animation.
282 char decrypt[PROPERTY_VALUE_MAX];
283 property_get("vold.decrypt", decrypt, "");
284
285 bool encryptedAnimation = atoi(decrypt) != 0 || !strcmp("trigger_restart_min_framework", decrypt);
286
Narayan Kamathafd31e02013-12-03 13:16:03 +0000287 ZipFileRO* zipFile = NULL;
Jason parksbd9a08d2011-01-31 15:04:34 -0600288 if ((encryptedAnimation &&
289 (access(SYSTEM_ENCRYPTED_BOOTANIMATION_FILE, R_OK) == 0) &&
Narayan Kamathafd31e02013-12-03 13:16:03 +0000290 ((zipFile = ZipFileRO::open(SYSTEM_ENCRYPTED_BOOTANIMATION_FILE)) != NULL)) ||
Jason parksbd9a08d2011-01-31 15:04:34 -0600291
Jason parksbd9a08d2011-01-31 15:04:34 -0600292 ((access(SYSTEM_BOOTANIMATION_FILE, R_OK) == 0) &&
Narayan Kamathafd31e02013-12-03 13:16:03 +0000293 ((zipFile = ZipFileRO::open(SYSTEM_BOOTANIMATION_FILE)) != NULL))) {
294 mZip = zipFile;
Jason parksbd9a08d2011-01-31 15:04:34 -0600295 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800296
297 return NO_ERROR;
298}
299
Mathias Agopiana8826d62009-10-01 03:10:14 -0700300bool BootAnimation::threadLoop()
301{
302 bool r;
Narayan Kamathafd31e02013-12-03 13:16:03 +0000303 // We have no bootanimation file, so we use the stock android logo
304 // animation.
305 if (mZip == NULL) {
Mathias Agopiana8826d62009-10-01 03:10:14 -0700306 r = android();
307 } else {
308 r = movie();
309 }
310
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800311 eglMakeCurrent(mDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
312 eglDestroyContext(mDisplay, mContext);
313 eglDestroySurface(mDisplay, mSurface);
Mathias Agopian6cf0db22009-04-17 19:36:26 -0700314 mFlingerSurface.clear();
Mathias Agopian17f638b2009-04-16 20:04:08 -0700315 mFlingerSurfaceControl.clear();
Mathias Agopian627e7b52009-05-21 19:21:59 -0700316 eglTerminate(mDisplay);
317 IPCThreadState::self()->stopProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800318 return r;
319}
320
Mathias Agopiana8826d62009-10-01 03:10:14 -0700321bool BootAnimation::android()
322{
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700323 initTexture(&mAndroid[0], mAssets, "images/android-logo-mask.png");
324 initTexture(&mAndroid[1], mAssets, "images/android-logo-shine.png");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800325
326 // clear screen
Mathias Agopiana8826d62009-10-01 03:10:14 -0700327 glShadeModel(GL_FLAT);
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700328 glDisable(GL_DITHER);
329 glDisable(GL_SCISSOR_TEST);
Mathias Agopian59f19e42011-05-06 19:22:12 -0700330 glClearColor(0,0,0,1);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800331 glClear(GL_COLOR_BUFFER_BIT);
332 eglSwapBuffers(mDisplay, mSurface);
333
Mathias Agopiana8826d62009-10-01 03:10:14 -0700334 glEnable(GL_TEXTURE_2D);
335 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
336
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700337 const GLint xc = (mWidth - mAndroid[0].w) / 2;
338 const GLint yc = (mHeight - mAndroid[0].h) / 2;
339 const Rect updateRect(xc, yc, xc + mAndroid[0].w, yc + mAndroid[0].h);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800340
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800341 glScissor(updateRect.left, mHeight - updateRect.bottom, updateRect.width(),
342 updateRect.height());
343
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700344 // Blend state
345 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
346 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
347
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800348 const nsecs_t startTime = systemTime();
349 do {
Mathias Agopian13796652009-03-24 22:49:21 -0700350 nsecs_t now = systemTime();
351 double time = now - startTime;
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700352 float t = 4.0f * float(time / us2ns(16667)) / mAndroid[1].w;
353 GLint offset = (1 - (t - floorf(t))) * mAndroid[1].w;
354 GLint x = xc - offset;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800355
Mathias Agopian81668642009-07-28 11:41:30 -0700356 glDisable(GL_SCISSOR_TEST);
357 glClear(GL_COLOR_BUFFER_BIT);
358
359 glEnable(GL_SCISSOR_TEST);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800360 glDisable(GL_BLEND);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800361 glBindTexture(GL_TEXTURE_2D, mAndroid[1].name);
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700362 glDrawTexiOES(x, yc, 0, mAndroid[1].w, mAndroid[1].h);
363 glDrawTexiOES(x + mAndroid[1].w, yc, 0, mAndroid[1].w, mAndroid[1].h);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800364
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700365 glEnable(GL_BLEND);
366 glBindTexture(GL_TEXTURE_2D, mAndroid[0].name);
367 glDrawTexiOES(xc, yc, 0, mAndroid[0].w, mAndroid[0].h);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800368
Mathias Agopian627e7b52009-05-21 19:21:59 -0700369 EGLBoolean res = eglSwapBuffers(mDisplay, mSurface);
370 if (res == EGL_FALSE)
371 break;
372
Mathias Agopian13796652009-03-24 22:49:21 -0700373 // 12fps: don't animate too fast to preserve CPU
374 const nsecs_t sleepTime = 83333 - ns2us(systemTime() - now);
375 if (sleepTime > 0)
Mathias Agopiana8826d62009-10-01 03:10:14 -0700376 usleep(sleepTime);
Kevin Hesterd3782b22012-04-26 10:38:55 -0700377
378 checkExit();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800379 } while (!exitPending());
380
381 glDeleteTextures(1, &mAndroid[0].name);
382 glDeleteTextures(1, &mAndroid[1].name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800383 return false;
384}
385
Mathias Agopiana8826d62009-10-01 03:10:14 -0700386
Kevin Hesterd3782b22012-04-26 10:38:55 -0700387void BootAnimation::checkExit() {
388 // Allow surface flinger to gracefully request shutdown
389 char value[PROPERTY_VALUE_MAX];
390 property_get(EXIT_PROP_NAME, value, "0");
391 int exitnow = atoi(value);
392 if (exitnow) {
393 requestExit();
394 }
395}
396
Mathias Agopiana8826d62009-10-01 03:10:14 -0700397bool BootAnimation::movie()
398{
Narayan Kamathafd31e02013-12-03 13:16:03 +0000399 ZipEntryRO desc = mZip->findEntryByName("desc.txt");
400 ALOGE_IF(!desc, "couldn't find desc.txt");
401 if (!desc) {
402 return false;
403 }
Mathias Agopiana8826d62009-10-01 03:10:14 -0700404
Narayan Kamathafd31e02013-12-03 13:16:03 +0000405 FileMap* descMap = mZip->createEntryFileMap(desc);
406 mZip->releaseEntry(desc);
Steve Block3762c312012-01-06 19:20:56 +0000407 ALOGE_IF(!descMap, "descMap is null");
Mathias Agopiana8826d62009-10-01 03:10:14 -0700408 if (!descMap) {
409 return false;
410 }
411
412 String8 desString((char const*)descMap->getDataPtr(),
413 descMap->getDataLength());
Mykola Kondratenko0c1eeb32014-04-15 09:35:44 +0200414 descMap->release();
Mathias Agopiana8826d62009-10-01 03:10:14 -0700415 char const* s = desString.string();
416
417 Animation animation;
418
419 // Parse the description file
420 for (;;) {
421 const char* endl = strstr(s, "\n");
422 if (!endl) break;
423 String8 line(s, endl - s);
424 const char* l = line.string();
425 int fps, width, height, count, pause;
Narayan Kamathafd31e02013-12-03 13:16:03 +0000426 char path[ANIM_ENTRY_NAME_MAX];
Kevin Hesterd3782b22012-04-26 10:38:55 -0700427 char pathType;
Mathias Agopiana8826d62009-10-01 03:10:14 -0700428 if (sscanf(l, "%d %d %d", &width, &height, &fps) == 3) {
Kevin Hesterd3782b22012-04-26 10:38:55 -0700429 //LOGD("> w=%d, h=%d, fps=%d", width, height, fps);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700430 animation.width = width;
431 animation.height = height;
432 animation.fps = fps;
433 }
Kevin Hesterd3782b22012-04-26 10:38:55 -0700434 else if (sscanf(l, " %c %d %d %s", &pathType, &count, &pause, path) == 4) {
435 //LOGD("> type=%c, count=%d, pause=%d, path=%s", pathType, count, pause, path);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700436 Animation::Part part;
Kevin Hesterd3782b22012-04-26 10:38:55 -0700437 part.playUntilComplete = pathType == 'c';
Mathias Agopiana8826d62009-10-01 03:10:14 -0700438 part.count = count;
439 part.pause = pause;
440 part.path = path;
441 animation.parts.add(part);
442 }
Kevin Hesterd3782b22012-04-26 10:38:55 -0700443
Mathias Agopiana8826d62009-10-01 03:10:14 -0700444 s = ++endl;
445 }
446
447 // read all the data structures
448 const size_t pcount = animation.parts.size();
Narayan Kamathafd31e02013-12-03 13:16:03 +0000449 void *cookie = NULL;
450 if (!mZip->startIteration(&cookie)) {
451 return false;
452 }
453
454 ZipEntryRO entry;
455 char name[ANIM_ENTRY_NAME_MAX];
456 while ((entry = mZip->nextEntry(cookie)) != NULL) {
457 const int foundEntryName = mZip->getEntryFileName(entry, name, ANIM_ENTRY_NAME_MAX);
458 if (foundEntryName > ANIM_ENTRY_NAME_MAX || foundEntryName == -1) {
459 ALOGE("Error fetching entry file name");
460 continue;
461 }
462
463 const String8 entryName(name);
464 const String8 path(entryName.getPathDir());
465 const String8 leaf(entryName.getPathLeaf());
466 if (leaf.size() > 0) {
467 for (size_t j=0 ; j<pcount ; j++) {
468 if (path == animation.parts[j].path) {
469 int method;
470 // supports only stored png files
471 if (mZip->getEntryInfo(entry, &method, NULL, NULL, NULL, NULL, NULL)) {
472 if (method == ZipFileRO::kCompressStored) {
473 FileMap* map = mZip->createEntryFileMap(entry);
474 if (map) {
475 Animation::Frame frame;
476 frame.name = leaf;
477 frame.map = map;
478 Animation::Part& part(animation.parts.editItemAt(j));
479 part.frames.add(frame);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700480 }
481 }
482 }
483 }
484 }
485 }
486 }
487
Narayan Kamathafd31e02013-12-03 13:16:03 +0000488 mZip->endIteration(cookie);
489
Mathias Agopiana8826d62009-10-01 03:10:14 -0700490 // clear screen
491 glShadeModel(GL_FLAT);
492 glDisable(GL_DITHER);
493 glDisable(GL_SCISSOR_TEST);
494 glDisable(GL_BLEND);
Mathias Agopian59f19e42011-05-06 19:22:12 -0700495 glClearColor(0,0,0,1);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700496 glClear(GL_COLOR_BUFFER_BIT);
497
498 eglSwapBuffers(mDisplay, mSurface);
499
500 glBindTexture(GL_TEXTURE_2D, 0);
501 glEnable(GL_TEXTURE_2D);
502 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
503 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
504 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
505 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
506 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
507
508 const int xc = (mWidth - animation.width) / 2;
509 const int yc = ((mHeight - animation.height) / 2);
510 nsecs_t lastFrame = systemTime();
511 nsecs_t frameDuration = s2ns(1) / animation.fps;
512
Mathias Agopian9f3020d2009-11-06 16:30:18 -0800513 Region clearReg(Rect(mWidth, mHeight));
514 clearReg.subtractSelf(Rect(xc, yc, xc+animation.width, yc+animation.height));
515
Narayan Kamathafd31e02013-12-03 13:16:03 +0000516 for (size_t i=0 ; i<pcount ; i++) {
Mathias Agopiana8826d62009-10-01 03:10:14 -0700517 const Animation::Part& part(animation.parts[i]);
518 const size_t fcount = part.frames.size();
519 glBindTexture(GL_TEXTURE_2D, 0);
520
521 for (int r=0 ; !part.count || r<part.count ; r++) {
Kevin Hesterd3782b22012-04-26 10:38:55 -0700522 // Exit any non playuntil complete parts immediately
523 if(exitPending() && !part.playUntilComplete)
524 break;
525
Narayan Kamathafd31e02013-12-03 13:16:03 +0000526 for (size_t j=0 ; j<fcount && (!exitPending() || part.playUntilComplete) ; j++) {
Mathias Agopiana8826d62009-10-01 03:10:14 -0700527 const Animation::Frame& frame(part.frames[j]);
Mathias Agopiandb7dd2a2012-05-12 15:08:21 -0700528 nsecs_t lastFrame = systemTime();
Mathias Agopiana8826d62009-10-01 03:10:14 -0700529
530 if (r > 0) {
531 glBindTexture(GL_TEXTURE_2D, frame.tid);
532 } else {
533 if (part.count != 1) {
534 glGenTextures(1, &frame.tid);
535 glBindTexture(GL_TEXTURE_2D, frame.tid);
536 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
537 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
538 }
Mykola Kondratenko0c1eeb32014-04-15 09:35:44 +0200539 initTexture(frame);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700540 }
541
Mathias Agopian9f3020d2009-11-06 16:30:18 -0800542 if (!clearReg.isEmpty()) {
543 Region::const_iterator head(clearReg.begin());
544 Region::const_iterator tail(clearReg.end());
545 glEnable(GL_SCISSOR_TEST);
546 while (head != tail) {
547 const Rect& r(*head++);
548 glScissor(r.left, mHeight - r.bottom,
549 r.width(), r.height());
550 glClear(GL_COLOR_BUFFER_BIT);
551 }
552 glDisable(GL_SCISSOR_TEST);
553 }
Mathias Agopiana8826d62009-10-01 03:10:14 -0700554 glDrawTexiOES(xc, yc, 0, animation.width, animation.height);
555 eglSwapBuffers(mDisplay, mSurface);
556
557 nsecs_t now = systemTime();
558 nsecs_t delay = frameDuration - (now - lastFrame);
Mathias Agopiandb7dd2a2012-05-12 15:08:21 -0700559 //ALOGD("%lld, %lld", ns2ms(now - lastFrame), ns2ms(delay));
Mathias Agopiana8826d62009-10-01 03:10:14 -0700560 lastFrame = now;
Mathias Agopiandb7dd2a2012-05-12 15:08:21 -0700561
562 if (delay > 0) {
563 struct timespec spec;
564 spec.tv_sec = (now + delay) / 1000000000;
565 spec.tv_nsec = (now + delay) % 1000000000;
566 int err;
567 do {
568 err = clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &spec, NULL);
569 } while (err<0 && errno == EINTR);
570 }
Kevin Hesterd3782b22012-04-26 10:38:55 -0700571
572 checkExit();
Mathias Agopiana8826d62009-10-01 03:10:14 -0700573 }
Kevin Hesterd3782b22012-04-26 10:38:55 -0700574
Mathias Agopiana8826d62009-10-01 03:10:14 -0700575 usleep(part.pause * ns2us(frameDuration));
Kevin Hesterd3782b22012-04-26 10:38:55 -0700576
577 // For infinite parts, we've now played them at least once, so perhaps exit
578 if(exitPending() && !part.count)
579 break;
Mathias Agopiana8826d62009-10-01 03:10:14 -0700580 }
581
582 // free the textures for this part
583 if (part.count != 1) {
Narayan Kamathafd31e02013-12-03 13:16:03 +0000584 for (size_t j=0 ; j<fcount ; j++) {
Mathias Agopiana8826d62009-10-01 03:10:14 -0700585 const Animation::Frame& frame(part.frames[j]);
586 glDeleteTextures(1, &frame.tid);
587 }
588 }
589 }
590
591 return false;
592}
593
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800594// ---------------------------------------------------------------------------
595
596}
597; // namespace android