blob: fba462bfbe41094fa6073f7861ebd0e205298b0c [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
Andreas Gampecfedceb2014-09-30 21:48:18 -070045// TODO: Fix Skia.
46#pragma GCC diagnostic push
47#pragma GCC diagnostic ignored "-Wunused-parameter"
Derek Sollenbergereece0dd2014-02-27 14:31:29 -050048#include <SkBitmap.h>
49#include <SkStream.h>
50#include <SkImageDecoder.h>
Andreas Gampecfedceb2014-09-30 21:48:18 -070051#pragma GCC diagnostic pop
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080052
53#include <GLES/gl.h>
54#include <GLES/glext.h>
55#include <EGL/eglext.h>
56
57#include "BootAnimation.h"
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -070058#include "AudioPlayer.h"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080059
Jeff Sharkey28f08772014-04-16 09:41:58 -070060#define OEM_BOOTANIMATION_FILE "/oem/media/bootanimation.zip"
Jim Huangc11f4622010-08-10 03:12:15 +080061#define SYSTEM_BOOTANIMATION_FILE "/system/media/bootanimation.zip"
Jason parksbd9a08d2011-01-31 15:04:34 -060062#define SYSTEM_ENCRYPTED_BOOTANIMATION_FILE "/system/media/bootanimation-encrypted.zip"
Kevin Hesterd3782b22012-04-26 10:38:55 -070063#define EXIT_PROP_NAME "service.bootanim.exit"
Jim Huangc11f4622010-08-10 03:12:15 +080064
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080065namespace android {
66
Narayan Kamathafd31e02013-12-03 13:16:03 +000067static const int ANIM_ENTRY_NAME_MAX = 256;
68
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080069// ---------------------------------------------------------------------------
70
Narayan Kamathafd31e02013-12-03 13:16:03 +000071BootAnimation::BootAnimation() : Thread(false), mZip(NULL)
Mathias Agopiana8826d62009-10-01 03:10:14 -070072{
Mathias Agopian627e7b52009-05-21 19:21:59 -070073 mSession = new SurfaceComposerClient();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080074}
75
76BootAnimation::~BootAnimation() {
Narayan Kamathafd31e02013-12-03 13:16:03 +000077 if (mZip != NULL) {
78 delete mZip;
79 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080080}
81
82void BootAnimation::onFirstRef() {
Mathias Agopianbc726112009-09-23 15:44:05 -070083 status_t err = mSession->linkToComposerDeath(this);
Steve Block3762c312012-01-06 19:20:56 +000084 ALOGE_IF(err, "linkToComposerDeath failed (%s) ", strerror(-err));
Mathias Agopian8434c532009-09-23 18:52:49 -070085 if (err == NO_ERROR) {
Mathias Agopianbc726112009-09-23 15:44:05 -070086 run("BootAnimation", PRIORITY_DISPLAY);
87 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080088}
89
Mathias Agopianbc726112009-09-23 15:44:05 -070090sp<SurfaceComposerClient> BootAnimation::session() const {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080091 return mSession;
92}
93
Mathias Agopianbc726112009-09-23 15:44:05 -070094
Narayan Kamathafd31e02013-12-03 13:16:03 +000095void BootAnimation::binderDied(const wp<IBinder>&)
Mathias Agopianbc726112009-09-23 15:44:05 -070096{
97 // woah, surfaceflinger died!
Steve Block5baa3a62011-12-20 16:23:08 +000098 ALOGD("SurfaceFlinger died, exiting...");
Mathias Agopianbc726112009-09-23 15:44:05 -070099
100 // calling requestExit() is not enough here because the Surface code
101 // might be blocked on a condition variable that will never be updated.
102 kill( getpid(), SIGKILL );
103 requestExit();
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700104 if (mAudioPlayer != NULL) {
105 mAudioPlayer->requestExit();
106 }
Mathias Agopianbc726112009-09-23 15:44:05 -0700107}
108
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800109status_t BootAnimation::initTexture(Texture* texture, AssetManager& assets,
110 const char* name) {
111 Asset* asset = assets.open(name, Asset::ACCESS_BUFFER);
Andreas Gampecfedceb2014-09-30 21:48:18 -0700112 if (asset == NULL)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800113 return NO_INIT;
114 SkBitmap bitmap;
115 SkImageDecoder::DecodeMemory(asset->getBuffer(false), asset->getLength(),
Mike Reed42a1d082014-07-07 18:06:18 -0400116 &bitmap, kUnknown_SkColorType, SkImageDecoder::kDecodePixels_Mode);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800117 asset->close();
118 delete asset;
119
120 // ensure we can call getPixels(). No need to call unlock, since the
121 // bitmap will go out of scope when we return from this method.
122 bitmap.lockPixels();
123
124 const int w = bitmap.width();
125 const int h = bitmap.height();
126 const void* p = bitmap.getPixels();
127
128 GLint crop[4] = { 0, h, w, -h };
129 texture->w = w;
130 texture->h = h;
131
132 glGenTextures(1, &texture->name);
133 glBindTexture(GL_TEXTURE_2D, texture->name);
134
Mike Reed42a1d082014-07-07 18:06:18 -0400135 switch (bitmap.colorType()) {
136 case kAlpha_8_SkColorType:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800137 glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, w, h, 0, GL_ALPHA,
138 GL_UNSIGNED_BYTE, p);
139 break;
Mike Reed42a1d082014-07-07 18:06:18 -0400140 case kARGB_4444_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_SHORT_4_4_4_4, p);
143 break;
Mike Reed42a1d082014-07-07 18:06:18 -0400144 case kN32_SkColorType:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800145 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA,
146 GL_UNSIGNED_BYTE, p);
147 break;
Mike Reed42a1d082014-07-07 18:06:18 -0400148 case kRGB_565_SkColorType:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800149 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, w, h, 0, GL_RGB,
150 GL_UNSIGNED_SHORT_5_6_5, p);
151 break;
152 default:
153 break;
154 }
155
156 glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_CROP_RECT_OES, crop);
157 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
158 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
159 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
160 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
161 return NO_ERROR;
162}
163
Mykola Kondratenko0c1eeb32014-04-15 09:35:44 +0200164status_t BootAnimation::initTexture(const Animation::Frame& frame)
Mathias Agopiana8826d62009-10-01 03:10:14 -0700165{
166 //StopWatch watch("blah");
167
168 SkBitmap bitmap;
Mykola Kondratenko0c1eeb32014-04-15 09:35:44 +0200169 SkMemoryStream stream(frame.map->getDataPtr(), frame.map->getDataLength());
Mathias Agopian2b99e552011-11-10 15:59:07 -0800170 SkImageDecoder* codec = SkImageDecoder::Factory(&stream);
Andreas Gampecfedceb2014-09-30 21:48:18 -0700171 if (codec != NULL) {
Elliott Hughesc367d482013-10-29 13:12:55 -0700172 codec->setDitherImage(false);
Mathias Agopian2b99e552011-11-10 15:59:07 -0800173 codec->decode(&stream, &bitmap,
Mike Reed42a1d082014-07-07 18:06:18 -0400174 kN32_SkColorType,
Mathias Agopian2b99e552011-11-10 15:59:07 -0800175 SkImageDecoder::kDecodePixels_Mode);
176 delete codec;
177 }
Mathias Agopiana8826d62009-10-01 03:10:14 -0700178
Mykola Kondratenko0c1eeb32014-04-15 09:35:44 +0200179 // FileMap memory is never released until application exit.
180 // Release it now as the texture is already loaded and the memory used for
181 // the packed resource can be released.
Narayan Kamath688ff4c2015-02-23 15:47:54 +0000182 delete frame.map;
Mykola Kondratenko0c1eeb32014-04-15 09:35:44 +0200183
Mathias Agopiana8826d62009-10-01 03:10:14 -0700184 // ensure we can call getPixels(). No need to call unlock, since the
185 // bitmap will go out of scope when we return from this method.
186 bitmap.lockPixels();
187
188 const int w = bitmap.width();
189 const int h = bitmap.height();
190 const void* p = bitmap.getPixels();
191
192 GLint crop[4] = { 0, h, w, -h };
193 int tw = 1 << (31 - __builtin_clz(w));
194 int th = 1 << (31 - __builtin_clz(h));
195 if (tw < w) tw <<= 1;
196 if (th < h) th <<= 1;
197
Mike Reed42a1d082014-07-07 18:06:18 -0400198 switch (bitmap.colorType()) {
199 case kN32_SkColorType:
Mathias Agopiana8826d62009-10-01 03:10:14 -0700200 if (tw != w || th != h) {
201 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, tw, th, 0, GL_RGBA,
202 GL_UNSIGNED_BYTE, 0);
203 glTexSubImage2D(GL_TEXTURE_2D, 0,
204 0, 0, w, h, GL_RGBA, GL_UNSIGNED_BYTE, p);
205 } else {
206 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, tw, th, 0, GL_RGBA,
207 GL_UNSIGNED_BYTE, p);
208 }
209 break;
210
Mike Reed42a1d082014-07-07 18:06:18 -0400211 case kRGB_565_SkColorType:
Mathias Agopiana8826d62009-10-01 03:10:14 -0700212 if (tw != w || th != h) {
213 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, tw, th, 0, GL_RGB,
214 GL_UNSIGNED_SHORT_5_6_5, 0);
215 glTexSubImage2D(GL_TEXTURE_2D, 0,
216 0, 0, w, h, GL_RGB, GL_UNSIGNED_SHORT_5_6_5, p);
217 } else {
218 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, tw, th, 0, GL_RGB,
219 GL_UNSIGNED_SHORT_5_6_5, p);
220 }
221 break;
222 default:
223 break;
224 }
225
226 glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_CROP_RECT_OES, crop);
227
228 return NO_ERROR;
229}
230
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800231status_t BootAnimation::readyToRun() {
232 mAssets.addDefaultAssets();
233
Jeff Brown0b722fe2012-08-24 22:40:14 -0700234 sp<IBinder> dtoken(SurfaceComposerClient::getBuiltInDisplay(
235 ISurfaceComposer::eDisplayIdMain));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800236 DisplayInfo dinfo;
Jeff Brown0b722fe2012-08-24 22:40:14 -0700237 status_t status = SurfaceComposerClient::getDisplayInfo(dtoken, &dinfo);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800238 if (status)
239 return -1;
240
241 // create the native surface
Jeff Brown0b722fe2012-08-24 22:40:14 -0700242 sp<SurfaceControl> control = session()->createSurface(String8("BootAnimation"),
243 dinfo.w, dinfo.h, PIXEL_FORMAT_RGB_565);
Mathias Agopian439863f2011-06-28 19:09:31 -0700244
245 SurfaceComposerClient::openGlobalTransaction();
Mathias Agopian17f638b2009-04-16 20:04:08 -0700246 control->setLayer(0x40000000);
Mathias Agopian439863f2011-06-28 19:09:31 -0700247 SurfaceComposerClient::closeGlobalTransaction();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800248
Mathias Agopian17f638b2009-04-16 20:04:08 -0700249 sp<Surface> s = control->getSurface();
250
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800251 // initialize opengl and egl
Mathias Agopian738b9a42009-08-06 16:41:02 -0700252 const EGLint attribs[] = {
Mathias Agopian1b253b72011-08-15 15:20:22 -0700253 EGL_RED_SIZE, 8,
254 EGL_GREEN_SIZE, 8,
255 EGL_BLUE_SIZE, 8,
Mathias Agopiana8826d62009-10-01 03:10:14 -0700256 EGL_DEPTH_SIZE, 0,
257 EGL_NONE
Mathias Agopian738b9a42009-08-06 16:41:02 -0700258 };
Andreas Gampecfedceb2014-09-30 21:48:18 -0700259 EGLint w, h;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800260 EGLint numConfigs;
261 EGLConfig config;
262 EGLSurface surface;
263 EGLContext context;
Mathias Agopian627e7b52009-05-21 19:21:59 -0700264
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800265 EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
Mathias Agopian627e7b52009-05-21 19:21:59 -0700266
267 eglInitialize(display, 0, 0);
Mathias Agopian1b253b72011-08-15 15:20:22 -0700268 eglChooseConfig(display, attribs, &config, 1, &numConfigs);
Mathias Agopian1473f462009-04-10 14:24:30 -0700269 surface = eglCreateWindowSurface(display, config, s.get(), NULL);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800270 context = eglCreateContext(display, config, NULL, NULL);
271 eglQuerySurface(display, surface, EGL_WIDTH, &w);
272 eglQuerySurface(display, surface, EGL_HEIGHT, &h);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700273
Mathias Agopianabac0102009-07-31 14:47:00 -0700274 if (eglMakeCurrent(display, surface, surface, context) == EGL_FALSE)
275 return NO_INIT;
Mathias Agopiana8826d62009-10-01 03:10:14 -0700276
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800277 mDisplay = display;
278 mContext = context;
279 mSurface = surface;
280 mWidth = w;
281 mHeight = h;
Mathias Agopian17f638b2009-04-16 20:04:08 -0700282 mFlingerSurfaceControl = control;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800283 mFlingerSurface = s;
284
Elliott Hughesc367d482013-10-29 13:12:55 -0700285 // If the device has encryption turned on or is in process
Jason parksbd9a08d2011-01-31 15:04:34 -0600286 // of being encrypted we show the encrypted boot animation.
287 char decrypt[PROPERTY_VALUE_MAX];
288 property_get("vold.decrypt", decrypt, "");
289
290 bool encryptedAnimation = atoi(decrypt) != 0 || !strcmp("trigger_restart_min_framework", decrypt);
291
Narayan Kamathafd31e02013-12-03 13:16:03 +0000292 ZipFileRO* zipFile = NULL;
Jason parksbd9a08d2011-01-31 15:04:34 -0600293 if ((encryptedAnimation &&
294 (access(SYSTEM_ENCRYPTED_BOOTANIMATION_FILE, R_OK) == 0) &&
Narayan Kamathafd31e02013-12-03 13:16:03 +0000295 ((zipFile = ZipFileRO::open(SYSTEM_ENCRYPTED_BOOTANIMATION_FILE)) != NULL)) ||
Jason parksbd9a08d2011-01-31 15:04:34 -0600296
Jeff Sharkey28f08772014-04-16 09:41:58 -0700297 ((access(OEM_BOOTANIMATION_FILE, R_OK) == 0) &&
298 ((zipFile = ZipFileRO::open(OEM_BOOTANIMATION_FILE)) != NULL)) ||
299
Jason parksbd9a08d2011-01-31 15:04:34 -0600300 ((access(SYSTEM_BOOTANIMATION_FILE, R_OK) == 0) &&
Narayan Kamathafd31e02013-12-03 13:16:03 +0000301 ((zipFile = ZipFileRO::open(SYSTEM_BOOTANIMATION_FILE)) != NULL))) {
302 mZip = zipFile;
Jason parksbd9a08d2011-01-31 15:04:34 -0600303 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800304
305 return NO_ERROR;
306}
307
Mathias Agopiana8826d62009-10-01 03:10:14 -0700308bool BootAnimation::threadLoop()
309{
310 bool r;
Narayan Kamathafd31e02013-12-03 13:16:03 +0000311 // We have no bootanimation file, so we use the stock android logo
312 // animation.
313 if (mZip == NULL) {
Mathias Agopiana8826d62009-10-01 03:10:14 -0700314 r = android();
315 } else {
316 r = movie();
317 }
318
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800319 eglMakeCurrent(mDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
320 eglDestroyContext(mDisplay, mContext);
321 eglDestroySurface(mDisplay, mSurface);
Mathias Agopian6cf0db22009-04-17 19:36:26 -0700322 mFlingerSurface.clear();
Mathias Agopian17f638b2009-04-16 20:04:08 -0700323 mFlingerSurfaceControl.clear();
Mathias Agopian627e7b52009-05-21 19:21:59 -0700324 eglTerminate(mDisplay);
325 IPCThreadState::self()->stopProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800326 return r;
327}
328
Mathias Agopiana8826d62009-10-01 03:10:14 -0700329bool BootAnimation::android()
330{
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700331 initTexture(&mAndroid[0], mAssets, "images/android-logo-mask.png");
332 initTexture(&mAndroid[1], mAssets, "images/android-logo-shine.png");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800333
334 // clear screen
Mathias Agopiana8826d62009-10-01 03:10:14 -0700335 glShadeModel(GL_FLAT);
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700336 glDisable(GL_DITHER);
337 glDisable(GL_SCISSOR_TEST);
Mathias Agopian59f19e42011-05-06 19:22:12 -0700338 glClearColor(0,0,0,1);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800339 glClear(GL_COLOR_BUFFER_BIT);
340 eglSwapBuffers(mDisplay, mSurface);
341
Mathias Agopiana8826d62009-10-01 03:10:14 -0700342 glEnable(GL_TEXTURE_2D);
343 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
344
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700345 const GLint xc = (mWidth - mAndroid[0].w) / 2;
346 const GLint yc = (mHeight - mAndroid[0].h) / 2;
347 const Rect updateRect(xc, yc, xc + mAndroid[0].w, yc + mAndroid[0].h);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800348
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800349 glScissor(updateRect.left, mHeight - updateRect.bottom, updateRect.width(),
350 updateRect.height());
351
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700352 // Blend state
353 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
354 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
355
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800356 const nsecs_t startTime = systemTime();
357 do {
Mathias Agopian13796652009-03-24 22:49:21 -0700358 nsecs_t now = systemTime();
359 double time = now - startTime;
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700360 float t = 4.0f * float(time / us2ns(16667)) / mAndroid[1].w;
361 GLint offset = (1 - (t - floorf(t))) * mAndroid[1].w;
362 GLint x = xc - offset;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800363
Mathias Agopian81668642009-07-28 11:41:30 -0700364 glDisable(GL_SCISSOR_TEST);
365 glClear(GL_COLOR_BUFFER_BIT);
366
367 glEnable(GL_SCISSOR_TEST);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800368 glDisable(GL_BLEND);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800369 glBindTexture(GL_TEXTURE_2D, mAndroid[1].name);
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700370 glDrawTexiOES(x, yc, 0, mAndroid[1].w, mAndroid[1].h);
371 glDrawTexiOES(x + mAndroid[1].w, yc, 0, mAndroid[1].w, mAndroid[1].h);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800372
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700373 glEnable(GL_BLEND);
374 glBindTexture(GL_TEXTURE_2D, mAndroid[0].name);
375 glDrawTexiOES(xc, yc, 0, mAndroid[0].w, mAndroid[0].h);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800376
Mathias Agopian627e7b52009-05-21 19:21:59 -0700377 EGLBoolean res = eglSwapBuffers(mDisplay, mSurface);
378 if (res == EGL_FALSE)
379 break;
380
Mathias Agopian13796652009-03-24 22:49:21 -0700381 // 12fps: don't animate too fast to preserve CPU
382 const nsecs_t sleepTime = 83333 - ns2us(systemTime() - now);
383 if (sleepTime > 0)
Mathias Agopiana8826d62009-10-01 03:10:14 -0700384 usleep(sleepTime);
Kevin Hesterd3782b22012-04-26 10:38:55 -0700385
386 checkExit();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800387 } while (!exitPending());
388
389 glDeleteTextures(1, &mAndroid[0].name);
390 glDeleteTextures(1, &mAndroid[1].name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800391 return false;
392}
393
Mathias Agopiana8826d62009-10-01 03:10:14 -0700394
Kevin Hesterd3782b22012-04-26 10:38:55 -0700395void BootAnimation::checkExit() {
396 // Allow surface flinger to gracefully request shutdown
397 char value[PROPERTY_VALUE_MAX];
398 property_get(EXIT_PROP_NAME, value, "0");
399 int exitnow = atoi(value);
400 if (exitnow) {
401 requestExit();
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700402 if (mAudioPlayer != NULL) {
403 mAudioPlayer->requestExit();
404 }
Kevin Hesterd3782b22012-04-26 10:38:55 -0700405 }
406}
407
Jesse Hall083b84c2014-09-22 10:51:09 -0700408// Parse a color represented as an HTML-style 'RRGGBB' string: each pair of
409// characters in str is a hex number in [0, 255], which are converted to
410// floating point values in the range [0.0, 1.0] and placed in the
411// corresponding elements of color.
412//
413// If the input string isn't valid, parseColor returns false and color is
414// left unchanged.
415static bool parseColor(const char str[7], float color[3]) {
416 float tmpColor[3];
417 for (int i = 0; i < 3; i++) {
418 int val = 0;
419 for (int j = 0; j < 2; j++) {
420 val *= 16;
421 char c = str[2*i + j];
422 if (c >= '0' && c <= '9') val += c - '0';
423 else if (c >= 'A' && c <= 'F') val += (c - 'A') + 10;
424 else if (c >= 'a' && c <= 'f') val += (c - 'a') + 10;
425 else return false;
426 }
427 tmpColor[i] = static_cast<float>(val) / 255.0f;
428 }
429 memcpy(color, tmpColor, sizeof(tmpColor));
430 return true;
431}
432
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700433bool BootAnimation::readFile(const char* name, String8& outString)
434{
435 ZipEntryRO entry = mZip->findEntryByName(name);
436 ALOGE_IF(!entry, "couldn't find %s", name);
437 if (!entry) {
438 return false;
439 }
440
441 FileMap* entryMap = mZip->createEntryFileMap(entry);
442 mZip->releaseEntry(entry);
443 ALOGE_IF(!entryMap, "entryMap is null");
444 if (!entryMap) {
445 return false;
446 }
447
448 outString.setTo((char const*)entryMap->getDataPtr(), entryMap->getDataLength());
Narayan Kamath688ff4c2015-02-23 15:47:54 +0000449 delete entryMap;
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700450 return true;
451}
452
Mathias Agopiana8826d62009-10-01 03:10:14 -0700453bool BootAnimation::movie()
454{
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700455 String8 desString;
456
457 if (!readFile("desc.txt", desString)) {
Narayan Kamathafd31e02013-12-03 13:16:03 +0000458 return false;
459 }
Mathias Agopiana8826d62009-10-01 03:10:14 -0700460 char const* s = desString.string();
461
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700462 // Create and initialize an AudioPlayer if we have an audio_conf.txt file
463 String8 audioConf;
464 if (readFile("audio_conf.txt", audioConf)) {
465 mAudioPlayer = new AudioPlayer;
466 if (!mAudioPlayer->init(audioConf.string())) {
467 ALOGE("mAudioPlayer.init failed");
468 mAudioPlayer = NULL;
469 }
470 }
471
Mathias Agopiana8826d62009-10-01 03:10:14 -0700472 Animation animation;
473
474 // Parse the description file
475 for (;;) {
476 const char* endl = strstr(s, "\n");
Andreas Gampecfedceb2014-09-30 21:48:18 -0700477 if (endl == NULL) break;
Mathias Agopiana8826d62009-10-01 03:10:14 -0700478 String8 line(s, endl - s);
479 const char* l = line.string();
480 int fps, width, height, count, pause;
Narayan Kamathafd31e02013-12-03 13:16:03 +0000481 char path[ANIM_ENTRY_NAME_MAX];
Jesse Hall083b84c2014-09-22 10:51:09 -0700482 char color[7] = "000000"; // default to black if unspecified
483
Kevin Hesterd3782b22012-04-26 10:38:55 -0700484 char pathType;
Mathias Agopiana8826d62009-10-01 03:10:14 -0700485 if (sscanf(l, "%d %d %d", &width, &height, &fps) == 3) {
Jesse Hall083b84c2014-09-22 10:51:09 -0700486 // ALOGD("> w=%d, h=%d, fps=%d", width, height, fps);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700487 animation.width = width;
488 animation.height = height;
489 animation.fps = fps;
490 }
Jesse Hall083b84c2014-09-22 10:51:09 -0700491 else if (sscanf(l, " %c %d %d %s #%6s", &pathType, &count, &pause, path, color) >= 4) {
492 // ALOGD("> type=%c, count=%d, pause=%d, path=%s, color=%s", pathType, count, pause, path, color);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700493 Animation::Part part;
Kevin Hesterd3782b22012-04-26 10:38:55 -0700494 part.playUntilComplete = pathType == 'c';
Mathias Agopiana8826d62009-10-01 03:10:14 -0700495 part.count = count;
496 part.pause = pause;
497 part.path = path;
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700498 part.audioFile = NULL;
Jesse Hall083b84c2014-09-22 10:51:09 -0700499 if (!parseColor(color, part.backgroundColor)) {
500 ALOGE("> invalid color '#%s'", color);
501 part.backgroundColor[0] = 0.0f;
502 part.backgroundColor[1] = 0.0f;
503 part.backgroundColor[2] = 0.0f;
504 }
Mathias Agopiana8826d62009-10-01 03:10:14 -0700505 animation.parts.add(part);
506 }
Kevin Hesterd3782b22012-04-26 10:38:55 -0700507
Mathias Agopiana8826d62009-10-01 03:10:14 -0700508 s = ++endl;
509 }
510
511 // read all the data structures
512 const size_t pcount = animation.parts.size();
Narayan Kamathafd31e02013-12-03 13:16:03 +0000513 void *cookie = NULL;
514 if (!mZip->startIteration(&cookie)) {
515 return false;
516 }
517
518 ZipEntryRO entry;
519 char name[ANIM_ENTRY_NAME_MAX];
520 while ((entry = mZip->nextEntry(cookie)) != NULL) {
521 const int foundEntryName = mZip->getEntryFileName(entry, name, ANIM_ENTRY_NAME_MAX);
522 if (foundEntryName > ANIM_ENTRY_NAME_MAX || foundEntryName == -1) {
523 ALOGE("Error fetching entry file name");
524 continue;
525 }
526
527 const String8 entryName(name);
528 const String8 path(entryName.getPathDir());
529 const String8 leaf(entryName.getPathLeaf());
530 if (leaf.size() > 0) {
531 for (size_t j=0 ; j<pcount ; j++) {
532 if (path == animation.parts[j].path) {
Narayan Kamath4600dd02015-06-16 12:02:57 +0100533 uint16_t method;
Narayan Kamathafd31e02013-12-03 13:16:03 +0000534 // supports only stored png files
535 if (mZip->getEntryInfo(entry, &method, NULL, NULL, NULL, NULL, NULL)) {
536 if (method == ZipFileRO::kCompressStored) {
537 FileMap* map = mZip->createEntryFileMap(entry);
538 if (map) {
Narayan Kamathafd31e02013-12-03 13:16:03 +0000539 Animation::Part& part(animation.parts.editItemAt(j));
Mike Lockwoodebf9a0d2014-10-02 16:08:47 -0700540 if (leaf == "audio.wav") {
541 // a part may have at most one audio file
542 part.audioFile = map;
543 } else {
544 Animation::Frame frame;
545 frame.name = leaf;
546 frame.map = map;
547 part.frames.add(frame);
548 }
Mathias Agopiana8826d62009-10-01 03:10:14 -0700549 }
550 }
551 }
552 }
553 }
554 }
555 }
556
Narayan Kamathafd31e02013-12-03 13:16:03 +0000557 mZip->endIteration(cookie);
558
Mathias Agopiana8826d62009-10-01 03:10:14 -0700559 // clear screen
560 glShadeModel(GL_FLAT);
561 glDisable(GL_DITHER);
562 glDisable(GL_SCISSOR_TEST);
563 glDisable(GL_BLEND);
Mathias Agopian59f19e42011-05-06 19:22:12 -0700564 glClearColor(0,0,0,1);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700565 glClear(GL_COLOR_BUFFER_BIT);
566
567 eglSwapBuffers(mDisplay, mSurface);
568
569 glBindTexture(GL_TEXTURE_2D, 0);
570 glEnable(GL_TEXTURE_2D);
571 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
572 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
573 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
574 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
575 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
576
577 const int xc = (mWidth - animation.width) / 2;
578 const int yc = ((mHeight - animation.height) / 2);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700579 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 Kamathafd31e02013-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 Kamathafd31e02013-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) {
Andreas Gampecfedceb2014-09-30 21:48:18 -0700626 const Rect& r2(*head++);
627 glScissor(r2.left, mHeight - r2.bottom,
628 r2.width(), r2.height());
Mathias Agopian9f3020d2009-11-06 16:30:18 -0800629 glClear(GL_COLOR_BUFFER_BIT);
630 }
631 glDisable(GL_SCISSOR_TEST);
632 }
Chris Elliottd13d5042015-05-04 15:24:12 -0700633 // specify the y center as ceiling((mHeight - animation.height) / 2)
634 // which is equivalent to mHeight - (yc + animation.height)
635 glDrawTexiOES(xc, mHeight - (yc + animation.height),
636 0, animation.width, animation.height);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700637 eglSwapBuffers(mDisplay, mSurface);
638
639 nsecs_t now = systemTime();
640 nsecs_t delay = frameDuration - (now - lastFrame);
Mathias Agopiandb7dd2a2012-05-12 15:08:21 -0700641 //ALOGD("%lld, %lld", ns2ms(now - lastFrame), ns2ms(delay));
Mathias Agopiana8826d62009-10-01 03:10:14 -0700642 lastFrame = now;
Mathias Agopiandb7dd2a2012-05-12 15:08:21 -0700643
644 if (delay > 0) {
645 struct timespec spec;
646 spec.tv_sec = (now + delay) / 1000000000;
647 spec.tv_nsec = (now + delay) % 1000000000;
648 int err;
649 do {
650 err = clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &spec, NULL);
651 } while (err<0 && errno == EINTR);
652 }
Kevin Hesterd3782b22012-04-26 10:38:55 -0700653
654 checkExit();
Mathias Agopiana8826d62009-10-01 03:10:14 -0700655 }
Kevin Hesterd3782b22012-04-26 10:38:55 -0700656
Mathias Agopiana8826d62009-10-01 03:10:14 -0700657 usleep(part.pause * ns2us(frameDuration));
Kevin Hesterd3782b22012-04-26 10:38:55 -0700658
659 // For infinite parts, we've now played them at least once, so perhaps exit
660 if(exitPending() && !part.count)
661 break;
Mathias Agopiana8826d62009-10-01 03:10:14 -0700662 }
663
664 // free the textures for this part
665 if (part.count != 1) {
Narayan Kamathafd31e02013-12-03 13:16:03 +0000666 for (size_t j=0 ; j<fcount ; j++) {
Mathias Agopiana8826d62009-10-01 03:10:14 -0700667 const Animation::Frame& frame(part.frames[j]);
668 glDeleteTextures(1, &frame.tid);
669 }
670 }
671 }
672
673 return false;
674}
675
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800676// ---------------------------------------------------------------------------
677
678}
679; // namespace android