blob: 167014e7c7a2f33705c3b8d60db4e1a8e840667f [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>
21#include <sys/types.h>
22#include <math.h>
23#include <fcntl.h>
24#include <utils/misc.h>
Mathias Agopianb4d5a722009-09-23 17:05:19 -070025#include <signal.h>
Elliott Hughesbb94f312014-10-21 10:41:33 -070026#include <time.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080027
Jason parksbd9a08d2011-01-31 15:04:34 -060028#include <cutils/properties.h>
29
Mathias Agopianb13b9bd2012-02-17 18:27:36 -080030#include <androidfw/AssetManager.h>
Mathias Agopianac31a3b2009-05-21 19:59:24 -070031#include <binder/IPCThreadState.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080032#include <utils/Atomic.h>
33#include <utils/Errors.h>
34#include <utils/Log.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>
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
Derek Sollenbergereece0dd2014-02-27 14:31:29 -050045#include <SkBitmap.h>
46#include <SkStream.h>
47#include <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"
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -070054#include "AudioPlayer.h"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080055
Jeff Sharkey28f08772014-04-16 09:41:58 -070056#define OEM_BOOTANIMATION_FILE "/oem/media/bootanimation.zip"
Jim Huangc11f4622010-08-10 03:12:15 +080057#define SYSTEM_BOOTANIMATION_FILE "/system/media/bootanimation.zip"
Jason parksbd9a08d2011-01-31 15:04:34 -060058#define SYSTEM_ENCRYPTED_BOOTANIMATION_FILE "/system/media/bootanimation-encrypted.zip"
Kevin Hesterd3782b22012-04-26 10:38:55 -070059#define EXIT_PROP_NAME "service.bootanim.exit"
Jim Huangc11f4622010-08-10 03:12:15 +080060
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080061namespace android {
62
Narayan Kamathafd31e02013-12-03 13:16:03 +000063static const int ANIM_ENTRY_NAME_MAX = 256;
64
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080065// ---------------------------------------------------------------------------
66
Narayan Kamathafd31e02013-12-03 13:16:03 +000067BootAnimation::BootAnimation() : Thread(false), mZip(NULL)
Mathias Agopiana8826d62009-10-01 03:10:14 -070068{
Mathias Agopian627e7b52009-05-21 19:21:59 -070069 mSession = new SurfaceComposerClient();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080070}
71
72BootAnimation::~BootAnimation() {
Narayan Kamathafd31e02013-12-03 13:16:03 +000073 if (mZip != NULL) {
74 delete mZip;
75 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080076}
77
78void BootAnimation::onFirstRef() {
Mathias Agopianbc726112009-09-23 15:44:05 -070079 status_t err = mSession->linkToComposerDeath(this);
Steve Block3762c312012-01-06 19:20:56 +000080 ALOGE_IF(err, "linkToComposerDeath failed (%s) ", strerror(-err));
Mathias Agopian8434c532009-09-23 18:52:49 -070081 if (err == NO_ERROR) {
Mathias Agopianbc726112009-09-23 15:44:05 -070082 run("BootAnimation", PRIORITY_DISPLAY);
83 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080084}
85
Mathias Agopianbc726112009-09-23 15:44:05 -070086sp<SurfaceComposerClient> BootAnimation::session() const {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080087 return mSession;
88}
89
Mathias Agopianbc726112009-09-23 15:44:05 -070090
Narayan Kamathafd31e02013-12-03 13:16:03 +000091void BootAnimation::binderDied(const wp<IBinder>&)
Mathias Agopianbc726112009-09-23 15:44:05 -070092{
93 // woah, surfaceflinger died!
Steve Block5baa3a62011-12-20 16:23:08 +000094 ALOGD("SurfaceFlinger died, exiting...");
Mathias Agopianbc726112009-09-23 15:44:05 -070095
96 // calling requestExit() is not enough here because the Surface code
97 // might be blocked on a condition variable that will never be updated.
98 kill( getpid(), SIGKILL );
99 requestExit();
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700100 if (mAudioPlayer != NULL) {
101 mAudioPlayer->requestExit();
102 }
Mathias Agopianbc726112009-09-23 15:44:05 -0700103}
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(),
Mike Reed42a1d082014-07-07 18:06:18 -0400112 &bitmap, kUnknown_SkColorType, SkImageDecoder::kDecodePixels_Mode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800113 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
Mike Reed42a1d082014-07-07 18:06:18 -0400131 switch (bitmap.colorType()) {
132 case kAlpha_8_SkColorType:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800133 glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, w, h, 0, GL_ALPHA,
134 GL_UNSIGNED_BYTE, p);
135 break;
Mike Reed42a1d082014-07-07 18:06:18 -0400136 case kARGB_4444_SkColorType:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800137 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA,
138 GL_UNSIGNED_SHORT_4_4_4_4, p);
139 break;
Mike Reed42a1d082014-07-07 18:06:18 -0400140 case kN32_SkColorType:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800141 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA,
142 GL_UNSIGNED_BYTE, p);
143 break;
Mike Reed42a1d082014-07-07 18:06:18 -0400144 case kRGB_565_SkColorType:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800145 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
Mykola Kondratenko0c1eeb32014-04-15 09:35:44 +0200160status_t BootAnimation::initTexture(const Animation::Frame& frame)
Mathias Agopiana8826d62009-10-01 03:10:14 -0700161{
162 //StopWatch watch("blah");
163
164 SkBitmap bitmap;
Mykola Kondratenko0c1eeb32014-04-15 09:35:44 +0200165 SkMemoryStream stream(frame.map->getDataPtr(), frame.map->getDataLength());
Mathias Agopian2b99e552011-11-10 15:59:07 -0800166 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,
Mike Reed42a1d082014-07-07 18:06:18 -0400170 kN32_SkColorType,
Mathias Agopian2b99e552011-11-10 15:59:07 -0800171 SkImageDecoder::kDecodePixels_Mode);
172 delete codec;
173 }
Mathias Agopiana8826d62009-10-01 03:10:14 -0700174
Mykola Kondratenko0c1eeb32014-04-15 09:35:44 +0200175 // FileMap memory is never released until application exit.
176 // Release it now as the texture is already loaded and the memory used for
177 // the packed resource can be released.
178 frame.map->release();
179
Mathias Agopiana8826d62009-10-01 03:10:14 -0700180 // ensure we can call getPixels(). No need to call unlock, since the
181 // bitmap will go out of scope when we return from this method.
182 bitmap.lockPixels();
183
184 const int w = bitmap.width();
185 const int h = bitmap.height();
186 const void* p = bitmap.getPixels();
187
188 GLint crop[4] = { 0, h, w, -h };
189 int tw = 1 << (31 - __builtin_clz(w));
190 int th = 1 << (31 - __builtin_clz(h));
191 if (tw < w) tw <<= 1;
192 if (th < h) th <<= 1;
193
Mike Reed42a1d082014-07-07 18:06:18 -0400194 switch (bitmap.colorType()) {
195 case kN32_SkColorType:
Mathias Agopiana8826d62009-10-01 03:10:14 -0700196 if (tw != w || th != h) {
197 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, tw, th, 0, GL_RGBA,
198 GL_UNSIGNED_BYTE, 0);
199 glTexSubImage2D(GL_TEXTURE_2D, 0,
200 0, 0, w, h, GL_RGBA, GL_UNSIGNED_BYTE, p);
201 } else {
202 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, tw, th, 0, GL_RGBA,
203 GL_UNSIGNED_BYTE, p);
204 }
205 break;
206
Mike Reed42a1d082014-07-07 18:06:18 -0400207 case kRGB_565_SkColorType:
Mathias Agopiana8826d62009-10-01 03:10:14 -0700208 if (tw != w || th != h) {
209 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, tw, th, 0, GL_RGB,
210 GL_UNSIGNED_SHORT_5_6_5, 0);
211 glTexSubImage2D(GL_TEXTURE_2D, 0,
212 0, 0, w, h, GL_RGB, GL_UNSIGNED_SHORT_5_6_5, p);
213 } else {
214 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, tw, th, 0, GL_RGB,
215 GL_UNSIGNED_SHORT_5_6_5, p);
216 }
217 break;
218 default:
219 break;
220 }
221
222 glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_CROP_RECT_OES, crop);
223
224 return NO_ERROR;
225}
226
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800227status_t BootAnimation::readyToRun() {
228 mAssets.addDefaultAssets();
229
Jeff Brown0b722fe2012-08-24 22:40:14 -0700230 sp<IBinder> dtoken(SurfaceComposerClient::getBuiltInDisplay(
231 ISurfaceComposer::eDisplayIdMain));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800232 DisplayInfo dinfo;
Jeff Brown0b722fe2012-08-24 22:40:14 -0700233 status_t status = SurfaceComposerClient::getDisplayInfo(dtoken, &dinfo);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800234 if (status)
235 return -1;
236
237 // create the native surface
Jeff Brown0b722fe2012-08-24 22:40:14 -0700238 sp<SurfaceControl> control = session()->createSurface(String8("BootAnimation"),
239 dinfo.w, dinfo.h, PIXEL_FORMAT_RGB_565);
Mathias Agopian439863f2011-06-28 19:09:31 -0700240
241 SurfaceComposerClient::openGlobalTransaction();
Mathias Agopian17f638b2009-04-16 20:04:08 -0700242 control->setLayer(0x40000000);
Mathias Agopian439863f2011-06-28 19:09:31 -0700243 SurfaceComposerClient::closeGlobalTransaction();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800244
Mathias Agopian17f638b2009-04-16 20:04:08 -0700245 sp<Surface> s = control->getSurface();
246
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800247 // initialize opengl and egl
Mathias Agopian738b9a42009-08-06 16:41:02 -0700248 const EGLint attribs[] = {
Mathias Agopian1b253b72011-08-15 15:20:22 -0700249 EGL_RED_SIZE, 8,
250 EGL_GREEN_SIZE, 8,
251 EGL_BLUE_SIZE, 8,
Mathias Agopiana8826d62009-10-01 03:10:14 -0700252 EGL_DEPTH_SIZE, 0,
253 EGL_NONE
Mathias Agopian738b9a42009-08-06 16:41:02 -0700254 };
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800255 EGLint w, h, dummy;
256 EGLint numConfigs;
257 EGLConfig config;
258 EGLSurface surface;
259 EGLContext context;
Mathias Agopian627e7b52009-05-21 19:21:59 -0700260
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800261 EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
Mathias Agopian627e7b52009-05-21 19:21:59 -0700262
263 eglInitialize(display, 0, 0);
Mathias Agopian1b253b72011-08-15 15:20:22 -0700264 eglChooseConfig(display, attribs, &config, 1, &numConfigs);
Mathias Agopian1473f462009-04-10 14:24:30 -0700265 surface = eglCreateWindowSurface(display, config, s.get(), NULL);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800266 context = eglCreateContext(display, config, NULL, NULL);
267 eglQuerySurface(display, surface, EGL_WIDTH, &w);
268 eglQuerySurface(display, surface, EGL_HEIGHT, &h);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700269
Mathias Agopianabac0102009-07-31 14:47:00 -0700270 if (eglMakeCurrent(display, surface, surface, context) == EGL_FALSE)
271 return NO_INIT;
Mathias Agopiana8826d62009-10-01 03:10:14 -0700272
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800273 mDisplay = display;
274 mContext = context;
275 mSurface = surface;
276 mWidth = w;
277 mHeight = h;
Mathias Agopian17f638b2009-04-16 20:04:08 -0700278 mFlingerSurfaceControl = control;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800279 mFlingerSurface = s;
280
Elliott Hughesc367d482013-10-29 13:12:55 -0700281 // If the device has encryption turned on or is in process
Jason parksbd9a08d2011-01-31 15:04:34 -0600282 // of being encrypted we show the encrypted boot animation.
283 char decrypt[PROPERTY_VALUE_MAX];
284 property_get("vold.decrypt", decrypt, "");
285
286 bool encryptedAnimation = atoi(decrypt) != 0 || !strcmp("trigger_restart_min_framework", decrypt);
287
Narayan Kamathafd31e02013-12-03 13:16:03 +0000288 ZipFileRO* zipFile = NULL;
Jason parksbd9a08d2011-01-31 15:04:34 -0600289 if ((encryptedAnimation &&
290 (access(SYSTEM_ENCRYPTED_BOOTANIMATION_FILE, R_OK) == 0) &&
Narayan Kamathafd31e02013-12-03 13:16:03 +0000291 ((zipFile = ZipFileRO::open(SYSTEM_ENCRYPTED_BOOTANIMATION_FILE)) != NULL)) ||
Jason parksbd9a08d2011-01-31 15:04:34 -0600292
Jeff Sharkey28f08772014-04-16 09:41:58 -0700293 ((access(OEM_BOOTANIMATION_FILE, R_OK) == 0) &&
294 ((zipFile = ZipFileRO::open(OEM_BOOTANIMATION_FILE)) != NULL)) ||
295
Jason parksbd9a08d2011-01-31 15:04:34 -0600296 ((access(SYSTEM_BOOTANIMATION_FILE, R_OK) == 0) &&
Narayan Kamathafd31e02013-12-03 13:16:03 +0000297 ((zipFile = ZipFileRO::open(SYSTEM_BOOTANIMATION_FILE)) != NULL))) {
298 mZip = zipFile;
Jason parksbd9a08d2011-01-31 15:04:34 -0600299 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800300
301 return NO_ERROR;
302}
303
Mathias Agopiana8826d62009-10-01 03:10:14 -0700304bool BootAnimation::threadLoop()
305{
306 bool r;
Narayan Kamathafd31e02013-12-03 13:16:03 +0000307 // We have no bootanimation file, so we use the stock android logo
308 // animation.
309 if (mZip == NULL) {
Mathias Agopiana8826d62009-10-01 03:10:14 -0700310 r = android();
311 } else {
312 r = movie();
313 }
314
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800315 eglMakeCurrent(mDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
316 eglDestroyContext(mDisplay, mContext);
317 eglDestroySurface(mDisplay, mSurface);
Mathias Agopian6cf0db22009-04-17 19:36:26 -0700318 mFlingerSurface.clear();
Mathias Agopian17f638b2009-04-16 20:04:08 -0700319 mFlingerSurfaceControl.clear();
Mathias Agopian627e7b52009-05-21 19:21:59 -0700320 eglTerminate(mDisplay);
321 IPCThreadState::self()->stopProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800322 return r;
323}
324
Mathias Agopiana8826d62009-10-01 03:10:14 -0700325bool BootAnimation::android()
326{
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700327 initTexture(&mAndroid[0], mAssets, "images/android-logo-mask.png");
328 initTexture(&mAndroid[1], mAssets, "images/android-logo-shine.png");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800329
330 // clear screen
Mathias Agopiana8826d62009-10-01 03:10:14 -0700331 glShadeModel(GL_FLAT);
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700332 glDisable(GL_DITHER);
333 glDisable(GL_SCISSOR_TEST);
Mathias Agopian59f19e42011-05-06 19:22:12 -0700334 glClearColor(0,0,0,1);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800335 glClear(GL_COLOR_BUFFER_BIT);
336 eglSwapBuffers(mDisplay, mSurface);
337
Mathias Agopiana8826d62009-10-01 03:10:14 -0700338 glEnable(GL_TEXTURE_2D);
339 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
340
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700341 const GLint xc = (mWidth - mAndroid[0].w) / 2;
342 const GLint yc = (mHeight - mAndroid[0].h) / 2;
343 const Rect updateRect(xc, yc, xc + mAndroid[0].w, yc + mAndroid[0].h);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800344
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800345 glScissor(updateRect.left, mHeight - updateRect.bottom, updateRect.width(),
346 updateRect.height());
347
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700348 // Blend state
349 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
350 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
351
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800352 const nsecs_t startTime = systemTime();
353 do {
Mathias Agopian13796652009-03-24 22:49:21 -0700354 nsecs_t now = systemTime();
355 double time = now - startTime;
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700356 float t = 4.0f * float(time / us2ns(16667)) / mAndroid[1].w;
357 GLint offset = (1 - (t - floorf(t))) * mAndroid[1].w;
358 GLint x = xc - offset;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800359
Mathias Agopian81668642009-07-28 11:41:30 -0700360 glDisable(GL_SCISSOR_TEST);
361 glClear(GL_COLOR_BUFFER_BIT);
362
363 glEnable(GL_SCISSOR_TEST);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800364 glDisable(GL_BLEND);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800365 glBindTexture(GL_TEXTURE_2D, mAndroid[1].name);
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700366 glDrawTexiOES(x, yc, 0, mAndroid[1].w, mAndroid[1].h);
367 glDrawTexiOES(x + mAndroid[1].w, yc, 0, mAndroid[1].w, mAndroid[1].h);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800368
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700369 glEnable(GL_BLEND);
370 glBindTexture(GL_TEXTURE_2D, mAndroid[0].name);
371 glDrawTexiOES(xc, yc, 0, mAndroid[0].w, mAndroid[0].h);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800372
Mathias Agopian627e7b52009-05-21 19:21:59 -0700373 EGLBoolean res = eglSwapBuffers(mDisplay, mSurface);
374 if (res == EGL_FALSE)
375 break;
376
Mathias Agopian13796652009-03-24 22:49:21 -0700377 // 12fps: don't animate too fast to preserve CPU
378 const nsecs_t sleepTime = 83333 - ns2us(systemTime() - now);
379 if (sleepTime > 0)
Mathias Agopiana8826d62009-10-01 03:10:14 -0700380 usleep(sleepTime);
Kevin Hesterd3782b22012-04-26 10:38:55 -0700381
382 checkExit();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800383 } while (!exitPending());
384
385 glDeleteTextures(1, &mAndroid[0].name);
386 glDeleteTextures(1, &mAndroid[1].name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800387 return false;
388}
389
Mathias Agopiana8826d62009-10-01 03:10:14 -0700390
Kevin Hesterd3782b22012-04-26 10:38:55 -0700391void BootAnimation::checkExit() {
392 // Allow surface flinger to gracefully request shutdown
393 char value[PROPERTY_VALUE_MAX];
394 property_get(EXIT_PROP_NAME, value, "0");
395 int exitnow = atoi(value);
396 if (exitnow) {
397 requestExit();
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700398 if (mAudioPlayer != NULL) {
399 mAudioPlayer->requestExit();
400 }
Kevin Hesterd3782b22012-04-26 10:38:55 -0700401 }
402}
403
Jesse Hall083b84c2014-09-22 10:51:09 -0700404// Parse a color represented as an HTML-style 'RRGGBB' string: each pair of
405// characters in str is a hex number in [0, 255], which are converted to
406// floating point values in the range [0.0, 1.0] and placed in the
407// corresponding elements of color.
408//
409// If the input string isn't valid, parseColor returns false and color is
410// left unchanged.
411static bool parseColor(const char str[7], float color[3]) {
412 float tmpColor[3];
413 for (int i = 0; i < 3; i++) {
414 int val = 0;
415 for (int j = 0; j < 2; j++) {
416 val *= 16;
417 char c = str[2*i + j];
418 if (c >= '0' && c <= '9') val += c - '0';
419 else if (c >= 'A' && c <= 'F') val += (c - 'A') + 10;
420 else if (c >= 'a' && c <= 'f') val += (c - 'a') + 10;
421 else return false;
422 }
423 tmpColor[i] = static_cast<float>(val) / 255.0f;
424 }
425 memcpy(color, tmpColor, sizeof(tmpColor));
426 return true;
427}
428
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700429bool BootAnimation::readFile(const char* name, String8& outString)
430{
431 ZipEntryRO entry = mZip->findEntryByName(name);
432 ALOGE_IF(!entry, "couldn't find %s", name);
433 if (!entry) {
434 return false;
435 }
436
437 FileMap* entryMap = mZip->createEntryFileMap(entry);
438 mZip->releaseEntry(entry);
439 ALOGE_IF(!entryMap, "entryMap is null");
440 if (!entryMap) {
441 return false;
442 }
443
444 outString.setTo((char const*)entryMap->getDataPtr(), entryMap->getDataLength());
445 entryMap->release();
446 return true;
447}
448
Mathias Agopiana8826d62009-10-01 03:10:14 -0700449bool BootAnimation::movie()
450{
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700451 String8 desString;
452
453 if (!readFile("desc.txt", desString)) {
Narayan Kamathafd31e02013-12-03 13:16:03 +0000454 return false;
455 }
Mathias Agopiana8826d62009-10-01 03:10:14 -0700456 char const* s = desString.string();
457
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700458 // Create and initialize an AudioPlayer if we have an audio_conf.txt file
459 String8 audioConf;
460 if (readFile("audio_conf.txt", audioConf)) {
461 mAudioPlayer = new AudioPlayer;
462 if (!mAudioPlayer->init(audioConf.string())) {
463 ALOGE("mAudioPlayer.init failed");
464 mAudioPlayer = NULL;
465 }
466 }
467
Mathias Agopiana8826d62009-10-01 03:10:14 -0700468 Animation animation;
469
470 // Parse the description file
471 for (;;) {
472 const char* endl = strstr(s, "\n");
473 if (!endl) break;
474 String8 line(s, endl - s);
475 const char* l = line.string();
476 int fps, width, height, count, pause;
Narayan Kamathafd31e02013-12-03 13:16:03 +0000477 char path[ANIM_ENTRY_NAME_MAX];
Jesse Hall083b84c2014-09-22 10:51:09 -0700478 char color[7] = "000000"; // default to black if unspecified
479
Kevin Hesterd3782b22012-04-26 10:38:55 -0700480 char pathType;
Mathias Agopiana8826d62009-10-01 03:10:14 -0700481 if (sscanf(l, "%d %d %d", &width, &height, &fps) == 3) {
Jesse Hall083b84c2014-09-22 10:51:09 -0700482 // ALOGD("> w=%d, h=%d, fps=%d", width, height, fps);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700483 animation.width = width;
484 animation.height = height;
485 animation.fps = fps;
486 }
Jesse Hall083b84c2014-09-22 10:51:09 -0700487 else if (sscanf(l, " %c %d %d %s #%6s", &pathType, &count, &pause, path, color) >= 4) {
488 // ALOGD("> type=%c, count=%d, pause=%d, path=%s, color=%s", pathType, count, pause, path, color);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700489 Animation::Part part;
Kevin Hesterd3782b22012-04-26 10:38:55 -0700490 part.playUntilComplete = pathType == 'c';
Mathias Agopiana8826d62009-10-01 03:10:14 -0700491 part.count = count;
492 part.pause = pause;
493 part.path = path;
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700494 part.audioFile = NULL;
Jesse Hall083b84c2014-09-22 10:51:09 -0700495 if (!parseColor(color, part.backgroundColor)) {
496 ALOGE("> invalid color '#%s'", color);
497 part.backgroundColor[0] = 0.0f;
498 part.backgroundColor[1] = 0.0f;
499 part.backgroundColor[2] = 0.0f;
500 }
Mathias Agopiana8826d62009-10-01 03:10:14 -0700501 animation.parts.add(part);
502 }
Kevin Hesterd3782b22012-04-26 10:38:55 -0700503
Mathias Agopiana8826d62009-10-01 03:10:14 -0700504 s = ++endl;
505 }
506
507 // read all the data structures
508 const size_t pcount = animation.parts.size();
Narayan Kamathafd31e02013-12-03 13:16:03 +0000509 void *cookie = NULL;
510 if (!mZip->startIteration(&cookie)) {
511 return false;
512 }
513
514 ZipEntryRO entry;
515 char name[ANIM_ENTRY_NAME_MAX];
516 while ((entry = mZip->nextEntry(cookie)) != NULL) {
517 const int foundEntryName = mZip->getEntryFileName(entry, name, ANIM_ENTRY_NAME_MAX);
518 if (foundEntryName > ANIM_ENTRY_NAME_MAX || foundEntryName == -1) {
519 ALOGE("Error fetching entry file name");
520 continue;
521 }
522
523 const String8 entryName(name);
524 const String8 path(entryName.getPathDir());
525 const String8 leaf(entryName.getPathLeaf());
526 if (leaf.size() > 0) {
527 for (size_t j=0 ; j<pcount ; j++) {
528 if (path == animation.parts[j].path) {
529 int method;
530 // supports only stored png files
531 if (mZip->getEntryInfo(entry, &method, NULL, NULL, NULL, NULL, NULL)) {
532 if (method == ZipFileRO::kCompressStored) {
533 FileMap* map = mZip->createEntryFileMap(entry);
534 if (map) {
Narayan Kamathafd31e02013-12-03 13:16:03 +0000535 Animation::Part& part(animation.parts.editItemAt(j));
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700536 if (leaf == "audio.wav") {
537 // a part may have at most one audio file
538 part.audioFile = map;
539 } else {
540 Animation::Frame frame;
541 frame.name = leaf;
542 frame.map = map;
543 part.frames.add(frame);
544 }
Mathias Agopiana8826d62009-10-01 03:10:14 -0700545 }
546 }
547 }
548 }
549 }
550 }
551 }
552
Narayan Kamathafd31e02013-12-03 13:16:03 +0000553 mZip->endIteration(cookie);
554
Mathias Agopiana8826d62009-10-01 03:10:14 -0700555 // clear screen
556 glShadeModel(GL_FLAT);
557 glDisable(GL_DITHER);
558 glDisable(GL_SCISSOR_TEST);
559 glDisable(GL_BLEND);
Mathias Agopian59f19e42011-05-06 19:22:12 -0700560 glClearColor(0,0,0,1);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700561 glClear(GL_COLOR_BUFFER_BIT);
562
563 eglSwapBuffers(mDisplay, mSurface);
564
565 glBindTexture(GL_TEXTURE_2D, 0);
566 glEnable(GL_TEXTURE_2D);
567 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
568 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
569 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
570 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
571 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
572
573 const int xc = (mWidth - animation.width) / 2;
574 const int yc = ((mHeight - animation.height) / 2);
575 nsecs_t lastFrame = systemTime();
576 nsecs_t frameDuration = s2ns(1) / animation.fps;
577
Mathias Agopian9f3020d2009-11-06 16:30:18 -0800578 Region clearReg(Rect(mWidth, mHeight));
579 clearReg.subtractSelf(Rect(xc, yc, xc+animation.width, yc+animation.height));
580
Narayan Kamathafd31e02013-12-03 13:16:03 +0000581 for (size_t i=0 ; i<pcount ; i++) {
Mathias Agopiana8826d62009-10-01 03:10:14 -0700582 const Animation::Part& part(animation.parts[i]);
583 const size_t fcount = part.frames.size();
584 glBindTexture(GL_TEXTURE_2D, 0);
585
586 for (int r=0 ; !part.count || r<part.count ; r++) {
Kevin Hesterd3782b22012-04-26 10:38:55 -0700587 // Exit any non playuntil complete parts immediately
588 if(exitPending() && !part.playUntilComplete)
589 break;
590
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700591 // only play audio file the first time we animate the part
592 if (r == 0 && mAudioPlayer != NULL && part.audioFile) {
593 mAudioPlayer->playFile(part.audioFile);
594 }
595
Jesse Hall083b84c2014-09-22 10:51:09 -0700596 glClearColor(
597 part.backgroundColor[0],
598 part.backgroundColor[1],
599 part.backgroundColor[2],
600 1.0f);
601
Narayan Kamathafd31e02013-12-03 13:16:03 +0000602 for (size_t j=0 ; j<fcount && (!exitPending() || part.playUntilComplete) ; j++) {
Mathias Agopiana8826d62009-10-01 03:10:14 -0700603 const Animation::Frame& frame(part.frames[j]);
Mathias Agopiandb7dd2a2012-05-12 15:08:21 -0700604 nsecs_t lastFrame = systemTime();
Mathias Agopiana8826d62009-10-01 03:10:14 -0700605
606 if (r > 0) {
607 glBindTexture(GL_TEXTURE_2D, frame.tid);
608 } else {
609 if (part.count != 1) {
610 glGenTextures(1, &frame.tid);
611 glBindTexture(GL_TEXTURE_2D, frame.tid);
612 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
613 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
614 }
Mykola Kondratenko0c1eeb32014-04-15 09:35:44 +0200615 initTexture(frame);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700616 }
617
Mathias Agopian9f3020d2009-11-06 16:30:18 -0800618 if (!clearReg.isEmpty()) {
619 Region::const_iterator head(clearReg.begin());
620 Region::const_iterator tail(clearReg.end());
621 glEnable(GL_SCISSOR_TEST);
622 while (head != tail) {
623 const Rect& r(*head++);
624 glScissor(r.left, mHeight - r.bottom,
625 r.width(), r.height());
626 glClear(GL_COLOR_BUFFER_BIT);
627 }
628 glDisable(GL_SCISSOR_TEST);
629 }
Mathias Agopiana8826d62009-10-01 03:10:14 -0700630 glDrawTexiOES(xc, yc, 0, animation.width, animation.height);
631 eglSwapBuffers(mDisplay, mSurface);
632
633 nsecs_t now = systemTime();
634 nsecs_t delay = frameDuration - (now - lastFrame);
Mathias Agopiandb7dd2a2012-05-12 15:08:21 -0700635 //ALOGD("%lld, %lld", ns2ms(now - lastFrame), ns2ms(delay));
Mathias Agopiana8826d62009-10-01 03:10:14 -0700636 lastFrame = now;
Mathias Agopiandb7dd2a2012-05-12 15:08:21 -0700637
638 if (delay > 0) {
639 struct timespec spec;
640 spec.tv_sec = (now + delay) / 1000000000;
641 spec.tv_nsec = (now + delay) % 1000000000;
642 int err;
643 do {
644 err = clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &spec, NULL);
645 } while (err<0 && errno == EINTR);
646 }
Kevin Hesterd3782b22012-04-26 10:38:55 -0700647
648 checkExit();
Mathias Agopiana8826d62009-10-01 03:10:14 -0700649 }
Kevin Hesterd3782b22012-04-26 10:38:55 -0700650
Mathias Agopiana8826d62009-10-01 03:10:14 -0700651 usleep(part.pause * ns2us(frameDuration));
Kevin Hesterd3782b22012-04-26 10:38:55 -0700652
653 // For infinite parts, we've now played them at least once, so perhaps exit
654 if(exitPending() && !part.count)
655 break;
Mathias Agopiana8826d62009-10-01 03:10:14 -0700656 }
657
658 // free the textures for this part
659 if (part.count != 1) {
Narayan Kamathafd31e02013-12-03 13:16:03 +0000660 for (size_t j=0 ; j<fcount ; j++) {
Mathias Agopiana8826d62009-10-01 03:10:14 -0700661 const Animation::Frame& frame(part.frames[j]);
662 glDeleteTextures(1, &frame.tid);
663 }
664 }
665 }
666
667 return false;
668}
669
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800670// ---------------------------------------------------------------------------
671
672}
673; // namespace android