blob: b2474f2f18bcb27bdfe83c477422210e542fa1df [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>
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>
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>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080039
Jeff Brown0b722fe2012-08-24 22:40:14 -070040#include <gui/ISurfaceComposer.h>
Mathias Agopian8335f1c2012-02-25 18:48:35 -080041#include <gui/Surface.h>
42#include <gui/SurfaceComposerClient.h>
Mathias Agopian000479f2010-02-09 17:46:37 -080043
Derek Sollenbergereece0dd2014-02-27 14:31:29 -050044#include <SkBitmap.h>
45#include <SkStream.h>
46#include <SkImageDecoder.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080047
48#include <GLES/gl.h>
49#include <GLES/glext.h>
50#include <EGL/eglext.h>
51
52#include "BootAnimation.h"
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -070053#include "AudioPlayer.h"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080054
Jeff Sharkey28f08772014-04-16 09:41:58 -070055#define OEM_BOOTANIMATION_FILE "/oem/media/bootanimation.zip"
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
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 Kamath5a74e2d2013-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 Kamath5a74e2d2013-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 Kamath5a74e2d2013-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 Kamath5a74e2d2013-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();
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700103 if (mAudioPlayer != NULL) {
104 mAudioPlayer->requestExit();
105 }
Mathias Agopianbc726112009-09-23 15:44:05 -0700106}
107
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800108status_t BootAnimation::initTexture(Texture* texture, AssetManager& assets,
109 const char* name) {
110 Asset* asset = assets.open(name, Asset::ACCESS_BUFFER);
111 if (!asset)
112 return NO_INIT;
113 SkBitmap bitmap;
114 SkImageDecoder::DecodeMemory(asset->getBuffer(false), asset->getLength(),
Mike Reed42a1d082014-07-07 18:06:18 -0400115 &bitmap, kUnknown_SkColorType, SkImageDecoder::kDecodePixels_Mode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800116 asset->close();
117 delete asset;
118
119 // ensure we can call getPixels(). No need to call unlock, since the
120 // bitmap will go out of scope when we return from this method.
121 bitmap.lockPixels();
122
123 const int w = bitmap.width();
124 const int h = bitmap.height();
125 const void* p = bitmap.getPixels();
126
127 GLint crop[4] = { 0, h, w, -h };
128 texture->w = w;
129 texture->h = h;
130
131 glGenTextures(1, &texture->name);
132 glBindTexture(GL_TEXTURE_2D, texture->name);
133
Mike Reed42a1d082014-07-07 18:06:18 -0400134 switch (bitmap.colorType()) {
135 case kAlpha_8_SkColorType:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800136 glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, w, h, 0, GL_ALPHA,
137 GL_UNSIGNED_BYTE, p);
138 break;
Mike Reed42a1d082014-07-07 18:06:18 -0400139 case kARGB_4444_SkColorType:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800140 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA,
141 GL_UNSIGNED_SHORT_4_4_4_4, p);
142 break;
Mike Reed42a1d082014-07-07 18:06:18 -0400143 case kN32_SkColorType:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800144 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA,
145 GL_UNSIGNED_BYTE, p);
146 break;
Mike Reed42a1d082014-07-07 18:06:18 -0400147 case kRGB_565_SkColorType:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800148 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, w, h, 0, GL_RGB,
149 GL_UNSIGNED_SHORT_5_6_5, p);
150 break;
151 default:
152 break;
153 }
154
155 glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_CROP_RECT_OES, crop);
156 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
157 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
158 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
159 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
160 return NO_ERROR;
161}
162
Mykola Kondratenko0c1eeb32014-04-15 09:35:44 +0200163status_t BootAnimation::initTexture(const Animation::Frame& frame)
Mathias Agopiana8826d62009-10-01 03:10:14 -0700164{
165 //StopWatch watch("blah");
166
167 SkBitmap bitmap;
Mykola Kondratenko0c1eeb32014-04-15 09:35:44 +0200168 SkMemoryStream stream(frame.map->getDataPtr(), frame.map->getDataLength());
Mathias Agopian2b99e552011-11-10 15:59:07 -0800169 SkImageDecoder* codec = SkImageDecoder::Factory(&stream);
Mathias Agopian2b99e552011-11-10 15:59:07 -0800170 if (codec) {
Elliott Hughesc367d482013-10-29 13:12:55 -0700171 codec->setDitherImage(false);
Mathias Agopian2b99e552011-11-10 15:59:07 -0800172 codec->decode(&stream, &bitmap,
Mike Reed42a1d082014-07-07 18:06:18 -0400173 kN32_SkColorType,
Mathias Agopian2b99e552011-11-10 15:59:07 -0800174 SkImageDecoder::kDecodePixels_Mode);
175 delete codec;
176 }
Mathias Agopiana8826d62009-10-01 03:10:14 -0700177
Mykola Kondratenko0c1eeb32014-04-15 09:35:44 +0200178 // FileMap memory is never released until application exit.
179 // Release it now as the texture is already loaded and the memory used for
180 // the packed resource can be released.
181 frame.map->release();
182
Mathias Agopiana8826d62009-10-01 03:10:14 -0700183 // ensure we can call getPixels(). No need to call unlock, since the
184 // bitmap will go out of scope when we return from this method.
185 bitmap.lockPixels();
186
187 const int w = bitmap.width();
188 const int h = bitmap.height();
189 const void* p = bitmap.getPixels();
190
191 GLint crop[4] = { 0, h, w, -h };
192 int tw = 1 << (31 - __builtin_clz(w));
193 int th = 1 << (31 - __builtin_clz(h));
194 if (tw < w) tw <<= 1;
195 if (th < h) th <<= 1;
196
Mike Reed42a1d082014-07-07 18:06:18 -0400197 switch (bitmap.colorType()) {
198 case kN32_SkColorType:
Mathias Agopiana8826d62009-10-01 03:10:14 -0700199 if (tw != w || th != h) {
200 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, tw, th, 0, GL_RGBA,
201 GL_UNSIGNED_BYTE, 0);
202 glTexSubImage2D(GL_TEXTURE_2D, 0,
203 0, 0, w, h, GL_RGBA, GL_UNSIGNED_BYTE, p);
204 } else {
205 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, tw, th, 0, GL_RGBA,
206 GL_UNSIGNED_BYTE, p);
207 }
208 break;
209
Mike Reed42a1d082014-07-07 18:06:18 -0400210 case kRGB_565_SkColorType:
Mathias Agopiana8826d62009-10-01 03:10:14 -0700211 if (tw != w || th != h) {
212 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, tw, th, 0, GL_RGB,
213 GL_UNSIGNED_SHORT_5_6_5, 0);
214 glTexSubImage2D(GL_TEXTURE_2D, 0,
215 0, 0, w, h, GL_RGB, GL_UNSIGNED_SHORT_5_6_5, p);
216 } else {
217 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, tw, th, 0, GL_RGB,
218 GL_UNSIGNED_SHORT_5_6_5, p);
219 }
220 break;
221 default:
222 break;
223 }
224
225 glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_CROP_RECT_OES, crop);
226
227 return NO_ERROR;
228}
229
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800230status_t BootAnimation::readyToRun() {
231 mAssets.addDefaultAssets();
232
Jeff Brown0b722fe2012-08-24 22:40:14 -0700233 sp<IBinder> dtoken(SurfaceComposerClient::getBuiltInDisplay(
234 ISurfaceComposer::eDisplayIdMain));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800235 DisplayInfo dinfo;
Jeff Brown0b722fe2012-08-24 22:40:14 -0700236 status_t status = SurfaceComposerClient::getDisplayInfo(dtoken, &dinfo);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800237 if (status)
238 return -1;
239
240 // create the native surface
Jeff Brown0b722fe2012-08-24 22:40:14 -0700241 sp<SurfaceControl> control = session()->createSurface(String8("BootAnimation"),
242 dinfo.w, dinfo.h, PIXEL_FORMAT_RGB_565);
Mathias Agopian439863f2011-06-28 19:09:31 -0700243
244 SurfaceComposerClient::openGlobalTransaction();
Mathias Agopian17f638b2009-04-16 20:04:08 -0700245 control->setLayer(0x40000000);
Mathias Agopian439863f2011-06-28 19:09:31 -0700246 SurfaceComposerClient::closeGlobalTransaction();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800247
Mathias Agopian17f638b2009-04-16 20:04:08 -0700248 sp<Surface> s = control->getSurface();
249
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800250 // initialize opengl and egl
Mathias Agopian738b9a42009-08-06 16:41:02 -0700251 const EGLint attribs[] = {
Mathias Agopian1b253b72011-08-15 15:20:22 -0700252 EGL_RED_SIZE, 8,
253 EGL_GREEN_SIZE, 8,
254 EGL_BLUE_SIZE, 8,
Mathias Agopiana8826d62009-10-01 03:10:14 -0700255 EGL_DEPTH_SIZE, 0,
256 EGL_NONE
Mathias Agopian738b9a42009-08-06 16:41:02 -0700257 };
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800258 EGLint w, h, dummy;
259 EGLint numConfigs;
260 EGLConfig config;
261 EGLSurface surface;
262 EGLContext context;
Mathias Agopian627e7b52009-05-21 19:21:59 -0700263
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800264 EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
Mathias Agopian627e7b52009-05-21 19:21:59 -0700265
266 eglInitialize(display, 0, 0);
Mathias Agopian1b253b72011-08-15 15:20:22 -0700267 eglChooseConfig(display, attribs, &config, 1, &numConfigs);
Mathias Agopian1473f462009-04-10 14:24:30 -0700268 surface = eglCreateWindowSurface(display, config, s.get(), NULL);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800269 context = eglCreateContext(display, config, NULL, NULL);
270 eglQuerySurface(display, surface, EGL_WIDTH, &w);
271 eglQuerySurface(display, surface, EGL_HEIGHT, &h);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700272
Mathias Agopianabac0102009-07-31 14:47:00 -0700273 if (eglMakeCurrent(display, surface, surface, context) == EGL_FALSE)
274 return NO_INIT;
Mathias Agopiana8826d62009-10-01 03:10:14 -0700275
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800276 mDisplay = display;
277 mContext = context;
278 mSurface = surface;
279 mWidth = w;
280 mHeight = h;
Mathias Agopian17f638b2009-04-16 20:04:08 -0700281 mFlingerSurfaceControl = control;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800282 mFlingerSurface = s;
283
Elliott Hughesc367d482013-10-29 13:12:55 -0700284 // If the device has encryption turned on or is in process
Jason parksbd9a08d2011-01-31 15:04:34 -0600285 // of being encrypted we show the encrypted boot animation.
286 char decrypt[PROPERTY_VALUE_MAX];
287 property_get("vold.decrypt", decrypt, "");
288
289 bool encryptedAnimation = atoi(decrypt) != 0 || !strcmp("trigger_restart_min_framework", decrypt);
290
Narayan Kamath5a74e2d2013-12-03 13:16:03 +0000291 ZipFileRO* zipFile = NULL;
Jason parksbd9a08d2011-01-31 15:04:34 -0600292 if ((encryptedAnimation &&
293 (access(SYSTEM_ENCRYPTED_BOOTANIMATION_FILE, R_OK) == 0) &&
Narayan Kamath5a74e2d2013-12-03 13:16:03 +0000294 ((zipFile = ZipFileRO::open(SYSTEM_ENCRYPTED_BOOTANIMATION_FILE)) != NULL)) ||
Jason parksbd9a08d2011-01-31 15:04:34 -0600295
Jeff Sharkey28f08772014-04-16 09:41:58 -0700296 ((access(OEM_BOOTANIMATION_FILE, R_OK) == 0) &&
297 ((zipFile = ZipFileRO::open(OEM_BOOTANIMATION_FILE)) != NULL)) ||
298
Jason parksbd9a08d2011-01-31 15:04:34 -0600299 ((access(SYSTEM_BOOTANIMATION_FILE, R_OK) == 0) &&
Narayan Kamath5a74e2d2013-12-03 13:16:03 +0000300 ((zipFile = ZipFileRO::open(SYSTEM_BOOTANIMATION_FILE)) != NULL))) {
301 mZip = zipFile;
Jason parksbd9a08d2011-01-31 15:04:34 -0600302 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800303
304 return NO_ERROR;
305}
306
Mathias Agopiana8826d62009-10-01 03:10:14 -0700307bool BootAnimation::threadLoop()
308{
309 bool r;
Narayan Kamath5a74e2d2013-12-03 13:16:03 +0000310 // We have no bootanimation file, so we use the stock android logo
311 // animation.
312 if (mZip == NULL) {
Mathias Agopiana8826d62009-10-01 03:10:14 -0700313 r = android();
314 } else {
315 r = movie();
316 }
317
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800318 eglMakeCurrent(mDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
319 eglDestroyContext(mDisplay, mContext);
320 eglDestroySurface(mDisplay, mSurface);
Mathias Agopian6cf0db22009-04-17 19:36:26 -0700321 mFlingerSurface.clear();
Mathias Agopian17f638b2009-04-16 20:04:08 -0700322 mFlingerSurfaceControl.clear();
Mathias Agopian627e7b52009-05-21 19:21:59 -0700323 eglTerminate(mDisplay);
324 IPCThreadState::self()->stopProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800325 return r;
326}
327
Mathias Agopiana8826d62009-10-01 03:10:14 -0700328bool BootAnimation::android()
329{
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700330 initTexture(&mAndroid[0], mAssets, "images/android-logo-mask.png");
331 initTexture(&mAndroid[1], mAssets, "images/android-logo-shine.png");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800332
333 // clear screen
Mathias Agopiana8826d62009-10-01 03:10:14 -0700334 glShadeModel(GL_FLAT);
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700335 glDisable(GL_DITHER);
336 glDisable(GL_SCISSOR_TEST);
Mathias Agopian59f19e42011-05-06 19:22:12 -0700337 glClearColor(0,0,0,1);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800338 glClear(GL_COLOR_BUFFER_BIT);
339 eglSwapBuffers(mDisplay, mSurface);
340
Mathias Agopiana8826d62009-10-01 03:10:14 -0700341 glEnable(GL_TEXTURE_2D);
342 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
343
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700344 const GLint xc = (mWidth - mAndroid[0].w) / 2;
345 const GLint yc = (mHeight - mAndroid[0].h) / 2;
346 const Rect updateRect(xc, yc, xc + mAndroid[0].w, yc + mAndroid[0].h);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800347
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800348 glScissor(updateRect.left, mHeight - updateRect.bottom, updateRect.width(),
349 updateRect.height());
350
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700351 // Blend state
352 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
353 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
354
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800355 const nsecs_t startTime = systemTime();
356 do {
Mathias Agopian13796652009-03-24 22:49:21 -0700357 nsecs_t now = systemTime();
358 double time = now - startTime;
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700359 float t = 4.0f * float(time / us2ns(16667)) / mAndroid[1].w;
360 GLint offset = (1 - (t - floorf(t))) * mAndroid[1].w;
361 GLint x = xc - offset;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800362
Mathias Agopian81668642009-07-28 11:41:30 -0700363 glDisable(GL_SCISSOR_TEST);
364 glClear(GL_COLOR_BUFFER_BIT);
365
366 glEnable(GL_SCISSOR_TEST);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800367 glDisable(GL_BLEND);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800368 glBindTexture(GL_TEXTURE_2D, mAndroid[1].name);
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700369 glDrawTexiOES(x, yc, 0, mAndroid[1].w, mAndroid[1].h);
370 glDrawTexiOES(x + mAndroid[1].w, yc, 0, mAndroid[1].w, mAndroid[1].h);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800371
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700372 glEnable(GL_BLEND);
373 glBindTexture(GL_TEXTURE_2D, mAndroid[0].name);
374 glDrawTexiOES(xc, yc, 0, mAndroid[0].w, mAndroid[0].h);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800375
Mathias Agopian627e7b52009-05-21 19:21:59 -0700376 EGLBoolean res = eglSwapBuffers(mDisplay, mSurface);
377 if (res == EGL_FALSE)
378 break;
379
Mathias Agopian13796652009-03-24 22:49:21 -0700380 // 12fps: don't animate too fast to preserve CPU
381 const nsecs_t sleepTime = 83333 - ns2us(systemTime() - now);
382 if (sleepTime > 0)
Mathias Agopiana8826d62009-10-01 03:10:14 -0700383 usleep(sleepTime);
Kevin Hesterd3782b22012-04-26 10:38:55 -0700384
385 checkExit();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800386 } while (!exitPending());
387
388 glDeleteTextures(1, &mAndroid[0].name);
389 glDeleteTextures(1, &mAndroid[1].name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800390 return false;
391}
392
Mathias Agopiana8826d62009-10-01 03:10:14 -0700393
Kevin Hesterd3782b22012-04-26 10:38:55 -0700394void BootAnimation::checkExit() {
395 // Allow surface flinger to gracefully request shutdown
396 char value[PROPERTY_VALUE_MAX];
397 property_get(EXIT_PROP_NAME, value, "0");
398 int exitnow = atoi(value);
399 if (exitnow) {
400 requestExit();
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700401 if (mAudioPlayer != NULL) {
402 mAudioPlayer->requestExit();
403 }
Kevin Hesterd3782b22012-04-26 10:38:55 -0700404 }
405}
406
Jesse Hall083b84c2014-09-22 10:51:09 -0700407// Parse a color represented as an HTML-style 'RRGGBB' string: each pair of
408// characters in str is a hex number in [0, 255], which are converted to
409// floating point values in the range [0.0, 1.0] and placed in the
410// corresponding elements of color.
411//
412// If the input string isn't valid, parseColor returns false and color is
413// left unchanged.
414static bool parseColor(const char str[7], float color[3]) {
415 float tmpColor[3];
416 for (int i = 0; i < 3; i++) {
417 int val = 0;
418 for (int j = 0; j < 2; j++) {
419 val *= 16;
420 char c = str[2*i + j];
421 if (c >= '0' && c <= '9') val += c - '0';
422 else if (c >= 'A' && c <= 'F') val += (c - 'A') + 10;
423 else if (c >= 'a' && c <= 'f') val += (c - 'a') + 10;
424 else return false;
425 }
426 tmpColor[i] = static_cast<float>(val) / 255.0f;
427 }
428 memcpy(color, tmpColor, sizeof(tmpColor));
429 return true;
430}
431
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700432bool BootAnimation::readFile(const char* name, String8& outString)
433{
434 ZipEntryRO entry = mZip->findEntryByName(name);
435 ALOGE_IF(!entry, "couldn't find %s", name);
436 if (!entry) {
437 return false;
438 }
439
440 FileMap* entryMap = mZip->createEntryFileMap(entry);
441 mZip->releaseEntry(entry);
442 ALOGE_IF(!entryMap, "entryMap is null");
443 if (!entryMap) {
444 return false;
445 }
446
447 outString.setTo((char const*)entryMap->getDataPtr(), entryMap->getDataLength());
448 entryMap->release();
449 return true;
450}
451
Mathias Agopiana8826d62009-10-01 03:10:14 -0700452bool BootAnimation::movie()
453{
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700454 String8 desString;
455
456 if (!readFile("desc.txt", desString)) {
Narayan Kamath5a74e2d2013-12-03 13:16:03 +0000457 return false;
458 }
Mathias Agopiana8826d62009-10-01 03:10:14 -0700459 char const* s = desString.string();
460
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700461 // Create and initialize an AudioPlayer if we have an audio_conf.txt file
462 String8 audioConf;
463 if (readFile("audio_conf.txt", audioConf)) {
464 mAudioPlayer = new AudioPlayer;
465 if (!mAudioPlayer->init(audioConf.string())) {
466 ALOGE("mAudioPlayer.init failed");
467 mAudioPlayer = NULL;
468 }
469 }
470
Mathias Agopiana8826d62009-10-01 03:10:14 -0700471 Animation animation;
472
473 // Parse the description file
474 for (;;) {
475 const char* endl = strstr(s, "\n");
476 if (!endl) break;
477 String8 line(s, endl - s);
478 const char* l = line.string();
479 int fps, width, height, count, pause;
Narayan Kamath5a74e2d2013-12-03 13:16:03 +0000480 char path[ANIM_ENTRY_NAME_MAX];
Jesse Hall083b84c2014-09-22 10:51:09 -0700481 char color[7] = "000000"; // default to black if unspecified
482
Kevin Hesterd3782b22012-04-26 10:38:55 -0700483 char pathType;
Mathias Agopiana8826d62009-10-01 03:10:14 -0700484 if (sscanf(l, "%d %d %d", &width, &height, &fps) == 3) {
Jesse Hall083b84c2014-09-22 10:51:09 -0700485 // ALOGD("> w=%d, h=%d, fps=%d", width, height, fps);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700486 animation.width = width;
487 animation.height = height;
488 animation.fps = fps;
489 }
Jesse Hall083b84c2014-09-22 10:51:09 -0700490 else if (sscanf(l, " %c %d %d %s #%6s", &pathType, &count, &pause, path, color) >= 4) {
491 // ALOGD("> type=%c, count=%d, pause=%d, path=%s, color=%s", pathType, count, pause, path, color);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700492 Animation::Part part;
Kevin Hesterd3782b22012-04-26 10:38:55 -0700493 part.playUntilComplete = pathType == 'c';
Mathias Agopiana8826d62009-10-01 03:10:14 -0700494 part.count = count;
495 part.pause = pause;
496 part.path = path;
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700497 part.audioFile = NULL;
Jesse Hall083b84c2014-09-22 10:51:09 -0700498 if (!parseColor(color, part.backgroundColor)) {
499 ALOGE("> invalid color '#%s'", color);
500 part.backgroundColor[0] = 0.0f;
501 part.backgroundColor[1] = 0.0f;
502 part.backgroundColor[2] = 0.0f;
503 }
Mathias Agopiana8826d62009-10-01 03:10:14 -0700504 animation.parts.add(part);
505 }
Kevin Hesterd3782b22012-04-26 10:38:55 -0700506
Mathias Agopiana8826d62009-10-01 03:10:14 -0700507 s = ++endl;
508 }
509
510 // read all the data structures
511 const size_t pcount = animation.parts.size();
Narayan Kamath5a74e2d2013-12-03 13:16:03 +0000512 void *cookie = NULL;
513 if (!mZip->startIteration(&cookie)) {
514 return false;
515 }
516
517 ZipEntryRO entry;
518 char name[ANIM_ENTRY_NAME_MAX];
519 while ((entry = mZip->nextEntry(cookie)) != NULL) {
520 const int foundEntryName = mZip->getEntryFileName(entry, name, ANIM_ENTRY_NAME_MAX);
521 if (foundEntryName > ANIM_ENTRY_NAME_MAX || foundEntryName == -1) {
522 ALOGE("Error fetching entry file name");
523 continue;
524 }
525
526 const String8 entryName(name);
527 const String8 path(entryName.getPathDir());
528 const String8 leaf(entryName.getPathLeaf());
529 if (leaf.size() > 0) {
530 for (size_t j=0 ; j<pcount ; j++) {
531 if (path == animation.parts[j].path) {
532 int method;
533 // supports only stored png files
534 if (mZip->getEntryInfo(entry, &method, NULL, NULL, NULL, NULL, NULL)) {
535 if (method == ZipFileRO::kCompressStored) {
536 FileMap* map = mZip->createEntryFileMap(entry);
537 if (map) {
Narayan Kamath5a74e2d2013-12-03 13:16:03 +0000538 Animation::Part& part(animation.parts.editItemAt(j));
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700539 if (leaf == "audio.wav") {
540 // a part may have at most one audio file
541 part.audioFile = map;
542 } else {
543 Animation::Frame frame;
544 frame.name = leaf;
545 frame.map = map;
546 part.frames.add(frame);
547 }
Mathias Agopiana8826d62009-10-01 03:10:14 -0700548 }
549 }
550 }
551 }
552 }
553 }
554 }
555
Narayan Kamath5a74e2d2013-12-03 13:16:03 +0000556 mZip->endIteration(cookie);
557
Mathias Agopiana8826d62009-10-01 03:10:14 -0700558 // clear screen
559 glShadeModel(GL_FLAT);
560 glDisable(GL_DITHER);
561 glDisable(GL_SCISSOR_TEST);
562 glDisable(GL_BLEND);
Mathias Agopian59f19e42011-05-06 19:22:12 -0700563 glClearColor(0,0,0,1);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700564 glClear(GL_COLOR_BUFFER_BIT);
565
566 eglSwapBuffers(mDisplay, mSurface);
567
568 glBindTexture(GL_TEXTURE_2D, 0);
569 glEnable(GL_TEXTURE_2D);
570 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
571 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
572 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
573 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
574 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
575
576 const int xc = (mWidth - animation.width) / 2;
577 const int yc = ((mHeight - animation.height) / 2);
578 nsecs_t lastFrame = systemTime();
579 nsecs_t frameDuration = s2ns(1) / animation.fps;
580
Mathias Agopian9f3020d2009-11-06 16:30:18 -0800581 Region clearReg(Rect(mWidth, mHeight));
582 clearReg.subtractSelf(Rect(xc, yc, xc+animation.width, yc+animation.height));
583
Narayan Kamath5a74e2d2013-12-03 13:16:03 +0000584 for (size_t i=0 ; i<pcount ; i++) {
Mathias Agopiana8826d62009-10-01 03:10:14 -0700585 const Animation::Part& part(animation.parts[i]);
586 const size_t fcount = part.frames.size();
587 glBindTexture(GL_TEXTURE_2D, 0);
588
589 for (int r=0 ; !part.count || r<part.count ; r++) {
Kevin Hesterd3782b22012-04-26 10:38:55 -0700590 // Exit any non playuntil complete parts immediately
591 if(exitPending() && !part.playUntilComplete)
592 break;
593
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700594 // only play audio file the first time we animate the part
595 if (r == 0 && mAudioPlayer != NULL && part.audioFile) {
596 mAudioPlayer->playFile(part.audioFile);
597 }
598
Jesse Hall083b84c2014-09-22 10:51:09 -0700599 glClearColor(
600 part.backgroundColor[0],
601 part.backgroundColor[1],
602 part.backgroundColor[2],
603 1.0f);
604
Narayan Kamath5a74e2d2013-12-03 13:16:03 +0000605 for (size_t j=0 ; j<fcount && (!exitPending() || part.playUntilComplete) ; j++) {
Mathias Agopiana8826d62009-10-01 03:10:14 -0700606 const Animation::Frame& frame(part.frames[j]);
Mathias Agopiandb7dd2a2012-05-12 15:08:21 -0700607 nsecs_t lastFrame = systemTime();
Mathias Agopiana8826d62009-10-01 03:10:14 -0700608
609 if (r > 0) {
610 glBindTexture(GL_TEXTURE_2D, frame.tid);
611 } else {
612 if (part.count != 1) {
613 glGenTextures(1, &frame.tid);
614 glBindTexture(GL_TEXTURE_2D, frame.tid);
615 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
616 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
617 }
Mykola Kondratenko0c1eeb32014-04-15 09:35:44 +0200618 initTexture(frame);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700619 }
620
Mathias Agopian9f3020d2009-11-06 16:30:18 -0800621 if (!clearReg.isEmpty()) {
622 Region::const_iterator head(clearReg.begin());
623 Region::const_iterator tail(clearReg.end());
624 glEnable(GL_SCISSOR_TEST);
625 while (head != tail) {
626 const Rect& r(*head++);
627 glScissor(r.left, mHeight - r.bottom,
628 r.width(), r.height());
629 glClear(GL_COLOR_BUFFER_BIT);
630 }
631 glDisable(GL_SCISSOR_TEST);
632 }
Mathias Agopiana8826d62009-10-01 03:10:14 -0700633 glDrawTexiOES(xc, yc, 0, animation.width, animation.height);
634 eglSwapBuffers(mDisplay, mSurface);
635
636 nsecs_t now = systemTime();
637 nsecs_t delay = frameDuration - (now - lastFrame);
Mathias Agopiandb7dd2a2012-05-12 15:08:21 -0700638 //ALOGD("%lld, %lld", ns2ms(now - lastFrame), ns2ms(delay));
Mathias Agopiana8826d62009-10-01 03:10:14 -0700639 lastFrame = now;
Mathias Agopiandb7dd2a2012-05-12 15:08:21 -0700640
641 if (delay > 0) {
642 struct timespec spec;
643 spec.tv_sec = (now + delay) / 1000000000;
644 spec.tv_nsec = (now + delay) % 1000000000;
645 int err;
646 do {
647 err = clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &spec, NULL);
648 } while (err<0 && errno == EINTR);
649 }
Kevin Hesterd3782b22012-04-26 10:38:55 -0700650
651 checkExit();
Mathias Agopiana8826d62009-10-01 03:10:14 -0700652 }
Kevin Hesterd3782b22012-04-26 10:38:55 -0700653
Mathias Agopiana8826d62009-10-01 03:10:14 -0700654 usleep(part.pause * ns2us(frameDuration));
Kevin Hesterd3782b22012-04-26 10:38:55 -0700655
656 // For infinite parts, we've now played them at least once, so perhaps exit
657 if(exitPending() && !part.count)
658 break;
Mathias Agopiana8826d62009-10-01 03:10:14 -0700659 }
660
661 // free the textures for this part
662 if (part.count != 1) {
Narayan Kamath5a74e2d2013-12-03 13:16:03 +0000663 for (size_t j=0 ; j<fcount ; j++) {
Mathias Agopiana8826d62009-10-01 03:10:14 -0700664 const Animation::Frame& frame(part.frames[j]);
665 glDeleteTextures(1, &frame.tid);
666 }
667 }
668 }
669
670 return false;
671}
672
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800673// ---------------------------------------------------------------------------
674
675}
676; // namespace android