blob: cc49cf3b61e684000e1566a2e1551c7361201b3f [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Mathias Agopianbc726112009-09-23 15:44:05 -070017#define LOG_TAG "BootAnimation"
18
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080019#include <stdint.h>
20#include <sys/types.h>
21#include <math.h>
22#include <fcntl.h>
23#include <utils/misc.h>
Mathias Agopianb4d5a722009-09-23 17:05:19 -070024#include <signal.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080025
Jason parksbd9a08d2011-01-31 15:04:34 -060026#include <cutils/properties.h>
27
Mathias Agopianb13b9bd2012-02-17 18:27:36 -080028#include <androidfw/AssetManager.h>
Mathias Agopianac31a3b2009-05-21 19:59:24 -070029#include <binder/IPCThreadState.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080030#include <utils/Atomic.h>
31#include <utils/Errors.h>
32#include <utils/Log.h>
Mathias Agopianb13b9bd2012-02-17 18:27:36 -080033#include <utils/threads.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080034
35#include <ui/PixelFormat.h>
36#include <ui/Rect.h>
37#include <ui/Region.h>
38#include <ui/DisplayInfo.h>
Mathias Agopiandff8e582009-05-04 14:17:04 -070039#include <ui/FramebufferNativeWindow.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080040
Jeff Brown0b722fe2012-08-24 22:40:14 -070041#include <gui/ISurfaceComposer.h>
Mathias Agopian8335f1c2012-02-25 18:48:35 -080042#include <gui/Surface.h>
43#include <gui/SurfaceComposerClient.h>
Mathias Agopian000479f2010-02-09 17:46:37 -080044
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080045#include <core/SkBitmap.h>
Mathias Agopian2b99e552011-11-10 15:59:07 -080046#include <core/SkStream.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080047#include <images/SkImageDecoder.h>
48
49#include <GLES/gl.h>
50#include <GLES/glext.h>
51#include <EGL/eglext.h>
52
53#include "BootAnimation.h"
54
Jim Huangc11f4622010-08-10 03:12:15 +080055#define USER_BOOTANIMATION_FILE "/data/local/bootanimation.zip"
56#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
66// ---------------------------------------------------------------------------
67
Mathias Agopian627e7b52009-05-21 19:21:59 -070068BootAnimation::BootAnimation() : Thread(false)
Mathias Agopiana8826d62009-10-01 03:10:14 -070069{
Mathias Agopian627e7b52009-05-21 19:21:59 -070070 mSession = new SurfaceComposerClient();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080071}
72
73BootAnimation::~BootAnimation() {
74}
75
76void BootAnimation::onFirstRef() {
Mathias Agopianbc726112009-09-23 15:44:05 -070077 status_t err = mSession->linkToComposerDeath(this);
Steve Block3762c312012-01-06 19:20:56 +000078 ALOGE_IF(err, "linkToComposerDeath failed (%s) ", strerror(-err));
Mathias Agopian8434c532009-09-23 18:52:49 -070079 if (err == NO_ERROR) {
Mathias Agopianbc726112009-09-23 15:44:05 -070080 run("BootAnimation", PRIORITY_DISPLAY);
81 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080082}
83
Mathias Agopianbc726112009-09-23 15:44:05 -070084sp<SurfaceComposerClient> BootAnimation::session() const {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080085 return mSession;
86}
87
Mathias Agopianbc726112009-09-23 15:44:05 -070088
89void BootAnimation::binderDied(const wp<IBinder>& who)
90{
91 // woah, surfaceflinger died!
Steve Block5baa3a62011-12-20 16:23:08 +000092 ALOGD("SurfaceFlinger died, exiting...");
Mathias Agopianbc726112009-09-23 15:44:05 -070093
94 // calling requestExit() is not enough here because the Surface code
95 // might be blocked on a condition variable that will never be updated.
96 kill( getpid(), SIGKILL );
97 requestExit();
98}
99
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800100status_t BootAnimation::initTexture(Texture* texture, AssetManager& assets,
101 const char* name) {
102 Asset* asset = assets.open(name, Asset::ACCESS_BUFFER);
103 if (!asset)
104 return NO_INIT;
105 SkBitmap bitmap;
106 SkImageDecoder::DecodeMemory(asset->getBuffer(false), asset->getLength(),
107 &bitmap, SkBitmap::kNo_Config, SkImageDecoder::kDecodePixels_Mode);
108 asset->close();
109 delete asset;
110
111 // ensure we can call getPixels(). No need to call unlock, since the
112 // bitmap will go out of scope when we return from this method.
113 bitmap.lockPixels();
114
115 const int w = bitmap.width();
116 const int h = bitmap.height();
117 const void* p = bitmap.getPixels();
118
119 GLint crop[4] = { 0, h, w, -h };
120 texture->w = w;
121 texture->h = h;
122
123 glGenTextures(1, &texture->name);
124 glBindTexture(GL_TEXTURE_2D, texture->name);
125
126 switch (bitmap.getConfig()) {
127 case SkBitmap::kA8_Config:
128 glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, w, h, 0, GL_ALPHA,
129 GL_UNSIGNED_BYTE, p);
130 break;
131 case SkBitmap::kARGB_4444_Config:
132 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA,
133 GL_UNSIGNED_SHORT_4_4_4_4, p);
134 break;
135 case SkBitmap::kARGB_8888_Config:
136 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA,
137 GL_UNSIGNED_BYTE, p);
138 break;
139 case SkBitmap::kRGB_565_Config:
140 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, w, h, 0, GL_RGB,
141 GL_UNSIGNED_SHORT_5_6_5, p);
142 break;
143 default:
144 break;
145 }
146
147 glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_CROP_RECT_OES, crop);
148 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
149 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
150 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
151 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
152 return NO_ERROR;
153}
154
Mathias Agopiana8826d62009-10-01 03:10:14 -0700155status_t BootAnimation::initTexture(void* buffer, size_t len)
156{
157 //StopWatch watch("blah");
158
159 SkBitmap bitmap;
Mathias Agopian2b99e552011-11-10 15:59:07 -0800160 SkMemoryStream stream(buffer, len);
161 SkImageDecoder* codec = SkImageDecoder::Factory(&stream);
Mathias Agopian2b99e552011-11-10 15:59:07 -0800162 if (codec) {
Elliott Hughesc367d482013-10-29 13:12:55 -0700163 codec->setDitherImage(false);
Mathias Agopian2b99e552011-11-10 15:59:07 -0800164 codec->decode(&stream, &bitmap,
Mathias Agopian60691ce2012-06-11 14:08:02 -0700165 SkBitmap::kARGB_8888_Config,
Mathias Agopian2b99e552011-11-10 15:59:07 -0800166 SkImageDecoder::kDecodePixels_Mode);
167 delete codec;
168 }
Mathias Agopiana8826d62009-10-01 03:10:14 -0700169
170 // ensure we can call getPixels(). No need to call unlock, since the
171 // bitmap will go out of scope when we return from this method.
172 bitmap.lockPixels();
173
174 const int w = bitmap.width();
175 const int h = bitmap.height();
176 const void* p = bitmap.getPixels();
177
178 GLint crop[4] = { 0, h, w, -h };
179 int tw = 1 << (31 - __builtin_clz(w));
180 int th = 1 << (31 - __builtin_clz(h));
181 if (tw < w) tw <<= 1;
182 if (th < h) th <<= 1;
183
184 switch (bitmap.getConfig()) {
185 case SkBitmap::kARGB_8888_Config:
186 if (tw != w || th != h) {
187 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, tw, th, 0, GL_RGBA,
188 GL_UNSIGNED_BYTE, 0);
189 glTexSubImage2D(GL_TEXTURE_2D, 0,
190 0, 0, w, h, GL_RGBA, GL_UNSIGNED_BYTE, p);
191 } else {
192 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, tw, th, 0, GL_RGBA,
193 GL_UNSIGNED_BYTE, p);
194 }
195 break;
196
197 case SkBitmap::kRGB_565_Config:
198 if (tw != w || th != h) {
199 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, tw, th, 0, GL_RGB,
200 GL_UNSIGNED_SHORT_5_6_5, 0);
201 glTexSubImage2D(GL_TEXTURE_2D, 0,
202 0, 0, w, h, GL_RGB, GL_UNSIGNED_SHORT_5_6_5, p);
203 } else {
204 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, tw, th, 0, GL_RGB,
205 GL_UNSIGNED_SHORT_5_6_5, p);
206 }
207 break;
208 default:
209 break;
210 }
211
212 glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_CROP_RECT_OES, crop);
213
214 return NO_ERROR;
215}
216
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800217status_t BootAnimation::readyToRun() {
218 mAssets.addDefaultAssets();
219
Jeff Brown0b722fe2012-08-24 22:40:14 -0700220 sp<IBinder> dtoken(SurfaceComposerClient::getBuiltInDisplay(
221 ISurfaceComposer::eDisplayIdMain));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800222 DisplayInfo dinfo;
Jeff Brown0b722fe2012-08-24 22:40:14 -0700223 status_t status = SurfaceComposerClient::getDisplayInfo(dtoken, &dinfo);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800224 if (status)
225 return -1;
226
227 // create the native surface
Jeff Brown0b722fe2012-08-24 22:40:14 -0700228 sp<SurfaceControl> control = session()->createSurface(String8("BootAnimation"),
229 dinfo.w, dinfo.h, PIXEL_FORMAT_RGB_565);
Mathias Agopian439863f2011-06-28 19:09:31 -0700230
231 SurfaceComposerClient::openGlobalTransaction();
Mathias Agopian17f638b2009-04-16 20:04:08 -0700232 control->setLayer(0x40000000);
Mathias Agopian439863f2011-06-28 19:09:31 -0700233 SurfaceComposerClient::closeGlobalTransaction();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800234
Mathias Agopian17f638b2009-04-16 20:04:08 -0700235 sp<Surface> s = control->getSurface();
236
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800237 // initialize opengl and egl
Mathias Agopian738b9a42009-08-06 16:41:02 -0700238 const EGLint attribs[] = {
Mathias Agopian1b253b72011-08-15 15:20:22 -0700239 EGL_RED_SIZE, 8,
240 EGL_GREEN_SIZE, 8,
241 EGL_BLUE_SIZE, 8,
Mathias Agopiana8826d62009-10-01 03:10:14 -0700242 EGL_DEPTH_SIZE, 0,
243 EGL_NONE
Mathias Agopian738b9a42009-08-06 16:41:02 -0700244 };
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800245 EGLint w, h, dummy;
246 EGLint numConfigs;
247 EGLConfig config;
248 EGLSurface surface;
249 EGLContext context;
Mathias Agopian627e7b52009-05-21 19:21:59 -0700250
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800251 EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
Mathias Agopian627e7b52009-05-21 19:21:59 -0700252
253 eglInitialize(display, 0, 0);
Mathias Agopian1b253b72011-08-15 15:20:22 -0700254 eglChooseConfig(display, attribs, &config, 1, &numConfigs);
Mathias Agopian1473f462009-04-10 14:24:30 -0700255 surface = eglCreateWindowSurface(display, config, s.get(), NULL);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800256 context = eglCreateContext(display, config, NULL, NULL);
257 eglQuerySurface(display, surface, EGL_WIDTH, &w);
258 eglQuerySurface(display, surface, EGL_HEIGHT, &h);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700259
Mathias Agopianabac0102009-07-31 14:47:00 -0700260 if (eglMakeCurrent(display, surface, surface, context) == EGL_FALSE)
261 return NO_INIT;
Mathias Agopiana8826d62009-10-01 03:10:14 -0700262
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800263 mDisplay = display;
264 mContext = context;
265 mSurface = surface;
266 mWidth = w;
267 mHeight = h;
Mathias Agopian17f638b2009-04-16 20:04:08 -0700268 mFlingerSurfaceControl = control;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800269 mFlingerSurface = s;
270
Jim Huangc11f4622010-08-10 03:12:15 +0800271 mAndroidAnimation = true;
Jason parksbd9a08d2011-01-31 15:04:34 -0600272
Elliott Hughesc367d482013-10-29 13:12:55 -0700273 // If the device has encryption turned on or is in process
Jason parksbd9a08d2011-01-31 15:04:34 -0600274 // of being encrypted we show the encrypted boot animation.
275 char decrypt[PROPERTY_VALUE_MAX];
276 property_get("vold.decrypt", decrypt, "");
277
278 bool encryptedAnimation = atoi(decrypt) != 0 || !strcmp("trigger_restart_min_framework", decrypt);
279
280 if ((encryptedAnimation &&
281 (access(SYSTEM_ENCRYPTED_BOOTANIMATION_FILE, R_OK) == 0) &&
282 (mZip.open(SYSTEM_ENCRYPTED_BOOTANIMATION_FILE) == NO_ERROR)) ||
283
284 ((access(USER_BOOTANIMATION_FILE, R_OK) == 0) &&
285 (mZip.open(USER_BOOTANIMATION_FILE) == NO_ERROR)) ||
286
287 ((access(SYSTEM_BOOTANIMATION_FILE, R_OK) == 0) &&
288 (mZip.open(SYSTEM_BOOTANIMATION_FILE) == NO_ERROR))) {
Chih-Wei Huang29787512010-11-03 15:33:00 +0800289 mAndroidAnimation = false;
Jason parksbd9a08d2011-01-31 15:04:34 -0600290 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800291
292 return NO_ERROR;
293}
294
Mathias Agopiana8826d62009-10-01 03:10:14 -0700295bool BootAnimation::threadLoop()
296{
297 bool r;
298 if (mAndroidAnimation) {
299 r = android();
300 } else {
301 r = movie();
302 }
303
Kevin Hesterd3782b22012-04-26 10:38:55 -0700304 // No need to force exit anymore
305 property_set(EXIT_PROP_NAME, "0");
306
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800307 eglMakeCurrent(mDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
308 eglDestroyContext(mDisplay, mContext);
309 eglDestroySurface(mDisplay, mSurface);
Mathias Agopian6cf0db22009-04-17 19:36:26 -0700310 mFlingerSurface.clear();
Mathias Agopian17f638b2009-04-16 20:04:08 -0700311 mFlingerSurfaceControl.clear();
Mathias Agopian627e7b52009-05-21 19:21:59 -0700312 eglTerminate(mDisplay);
313 IPCThreadState::self()->stopProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800314 return r;
315}
316
Mathias Agopiana8826d62009-10-01 03:10:14 -0700317bool BootAnimation::android()
318{
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700319 initTexture(&mAndroid[0], mAssets, "images/android-logo-mask.png");
320 initTexture(&mAndroid[1], mAssets, "images/android-logo-shine.png");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800321
322 // clear screen
Mathias Agopiana8826d62009-10-01 03:10:14 -0700323 glShadeModel(GL_FLAT);
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700324 glDisable(GL_DITHER);
325 glDisable(GL_SCISSOR_TEST);
Mathias Agopian59f19e42011-05-06 19:22:12 -0700326 glClearColor(0,0,0,1);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800327 glClear(GL_COLOR_BUFFER_BIT);
328 eglSwapBuffers(mDisplay, mSurface);
329
Mathias Agopiana8826d62009-10-01 03:10:14 -0700330 glEnable(GL_TEXTURE_2D);
331 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
332
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700333 const GLint xc = (mWidth - mAndroid[0].w) / 2;
334 const GLint yc = (mHeight - mAndroid[0].h) / 2;
335 const Rect updateRect(xc, yc, xc + mAndroid[0].w, yc + mAndroid[0].h);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800336
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800337 glScissor(updateRect.left, mHeight - updateRect.bottom, updateRect.width(),
338 updateRect.height());
339
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700340 // Blend state
341 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
342 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
343
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800344 const nsecs_t startTime = systemTime();
345 do {
Mathias Agopian13796652009-03-24 22:49:21 -0700346 nsecs_t now = systemTime();
347 double time = now - startTime;
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700348 float t = 4.0f * float(time / us2ns(16667)) / mAndroid[1].w;
349 GLint offset = (1 - (t - floorf(t))) * mAndroid[1].w;
350 GLint x = xc - offset;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800351
Mathias Agopian81668642009-07-28 11:41:30 -0700352 glDisable(GL_SCISSOR_TEST);
353 glClear(GL_COLOR_BUFFER_BIT);
354
355 glEnable(GL_SCISSOR_TEST);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800356 glDisable(GL_BLEND);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800357 glBindTexture(GL_TEXTURE_2D, mAndroid[1].name);
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700358 glDrawTexiOES(x, yc, 0, mAndroid[1].w, mAndroid[1].h);
359 glDrawTexiOES(x + mAndroid[1].w, yc, 0, mAndroid[1].w, mAndroid[1].h);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800360
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700361 glEnable(GL_BLEND);
362 glBindTexture(GL_TEXTURE_2D, mAndroid[0].name);
363 glDrawTexiOES(xc, yc, 0, mAndroid[0].w, mAndroid[0].h);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800364
Mathias Agopian627e7b52009-05-21 19:21:59 -0700365 EGLBoolean res = eglSwapBuffers(mDisplay, mSurface);
366 if (res == EGL_FALSE)
367 break;
368
Mathias Agopian13796652009-03-24 22:49:21 -0700369 // 12fps: don't animate too fast to preserve CPU
370 const nsecs_t sleepTime = 83333 - ns2us(systemTime() - now);
371 if (sleepTime > 0)
Mathias Agopiana8826d62009-10-01 03:10:14 -0700372 usleep(sleepTime);
Kevin Hesterd3782b22012-04-26 10:38:55 -0700373
374 checkExit();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800375 } while (!exitPending());
376
377 glDeleteTextures(1, &mAndroid[0].name);
378 glDeleteTextures(1, &mAndroid[1].name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800379 return false;
380}
381
Mathias Agopiana8826d62009-10-01 03:10:14 -0700382
Kevin Hesterd3782b22012-04-26 10:38:55 -0700383void BootAnimation::checkExit() {
384 // Allow surface flinger to gracefully request shutdown
385 char value[PROPERTY_VALUE_MAX];
386 property_get(EXIT_PROP_NAME, value, "0");
387 int exitnow = atoi(value);
388 if (exitnow) {
389 requestExit();
390 }
391}
392
Mathias Agopiana8826d62009-10-01 03:10:14 -0700393bool BootAnimation::movie()
394{
395 ZipFileRO& zip(mZip);
396
397 size_t numEntries = zip.getNumEntries();
398 ZipEntryRO desc = zip.findEntryByName("desc.txt");
399 FileMap* descMap = zip.createEntryFileMap(desc);
Steve Block3762c312012-01-06 19:20:56 +0000400 ALOGE_IF(!descMap, "descMap is null");
Mathias Agopiana8826d62009-10-01 03:10:14 -0700401 if (!descMap) {
402 return false;
403 }
404
405 String8 desString((char const*)descMap->getDataPtr(),
406 descMap->getDataLength());
407 char const* s = desString.string();
408
409 Animation animation;
410
411 // Parse the description file
412 for (;;) {
413 const char* endl = strstr(s, "\n");
414 if (!endl) break;
415 String8 line(s, endl - s);
416 const char* l = line.string();
417 int fps, width, height, count, pause;
418 char path[256];
Kevin Hesterd3782b22012-04-26 10:38:55 -0700419 char pathType;
Mathias Agopiana8826d62009-10-01 03:10:14 -0700420 if (sscanf(l, "%d %d %d", &width, &height, &fps) == 3) {
Kevin Hesterd3782b22012-04-26 10:38:55 -0700421 //LOGD("> w=%d, h=%d, fps=%d", width, height, fps);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700422 animation.width = width;
423 animation.height = height;
424 animation.fps = fps;
425 }
Kevin Hesterd3782b22012-04-26 10:38:55 -0700426 else if (sscanf(l, " %c %d %d %s", &pathType, &count, &pause, path) == 4) {
427 //LOGD("> type=%c, count=%d, pause=%d, path=%s", pathType, count, pause, path);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700428 Animation::Part part;
Kevin Hesterd3782b22012-04-26 10:38:55 -0700429 part.playUntilComplete = pathType == 'c';
Mathias Agopiana8826d62009-10-01 03:10:14 -0700430 part.count = count;
431 part.pause = pause;
432 part.path = path;
433 animation.parts.add(part);
434 }
Kevin Hesterd3782b22012-04-26 10:38:55 -0700435
Mathias Agopiana8826d62009-10-01 03:10:14 -0700436 s = ++endl;
437 }
438
439 // read all the data structures
440 const size_t pcount = animation.parts.size();
441 for (size_t i=0 ; i<numEntries ; i++) {
442 char name[256];
443 ZipEntryRO entry = zip.findEntryByIndex(i);
444 if (zip.getEntryFileName(entry, name, 256) == 0) {
445 const String8 entryName(name);
446 const String8 path(entryName.getPathDir());
447 const String8 leaf(entryName.getPathLeaf());
448 if (leaf.size() > 0) {
449 for (int j=0 ; j<pcount ; j++) {
450 if (path == animation.parts[j].path) {
451 int method;
452 // supports only stored png files
453 if (zip.getEntryInfo(entry, &method, 0, 0, 0, 0, 0)) {
454 if (method == ZipFileRO::kCompressStored) {
455 FileMap* map = zip.createEntryFileMap(entry);
456 if (map) {
457 Animation::Frame frame;
458 frame.name = leaf;
459 frame.map = map;
460 Animation::Part& part(animation.parts.editItemAt(j));
461 part.frames.add(frame);
462 }
463 }
464 }
465 }
466 }
467 }
468 }
469 }
470
471 // clear screen
472 glShadeModel(GL_FLAT);
473 glDisable(GL_DITHER);
474 glDisable(GL_SCISSOR_TEST);
475 glDisable(GL_BLEND);
Mathias Agopian59f19e42011-05-06 19:22:12 -0700476 glClearColor(0,0,0,1);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700477 glClear(GL_COLOR_BUFFER_BIT);
478
479 eglSwapBuffers(mDisplay, mSurface);
480
481 glBindTexture(GL_TEXTURE_2D, 0);
482 glEnable(GL_TEXTURE_2D);
483 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
484 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
485 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
486 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
487 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
488
489 const int xc = (mWidth - animation.width) / 2;
490 const int yc = ((mHeight - animation.height) / 2);
491 nsecs_t lastFrame = systemTime();
492 nsecs_t frameDuration = s2ns(1) / animation.fps;
493
Mathias Agopian9f3020d2009-11-06 16:30:18 -0800494 Region clearReg(Rect(mWidth, mHeight));
495 clearReg.subtractSelf(Rect(xc, yc, xc+animation.width, yc+animation.height));
496
Kevin Hesterd3782b22012-04-26 10:38:55 -0700497 for (int i=0 ; i<pcount ; i++) {
Mathias Agopiana8826d62009-10-01 03:10:14 -0700498 const Animation::Part& part(animation.parts[i]);
499 const size_t fcount = part.frames.size();
500 glBindTexture(GL_TEXTURE_2D, 0);
501
502 for (int r=0 ; !part.count || r<part.count ; r++) {
Kevin Hesterd3782b22012-04-26 10:38:55 -0700503 // Exit any non playuntil complete parts immediately
504 if(exitPending() && !part.playUntilComplete)
505 break;
506
507 for (int j=0 ; j<fcount && (!exitPending() || part.playUntilComplete) ; j++) {
Mathias Agopiana8826d62009-10-01 03:10:14 -0700508 const Animation::Frame& frame(part.frames[j]);
Mathias Agopiandb7dd2a2012-05-12 15:08:21 -0700509 nsecs_t lastFrame = systemTime();
Mathias Agopiana8826d62009-10-01 03:10:14 -0700510
511 if (r > 0) {
512 glBindTexture(GL_TEXTURE_2D, frame.tid);
513 } else {
514 if (part.count != 1) {
515 glGenTextures(1, &frame.tid);
516 glBindTexture(GL_TEXTURE_2D, frame.tid);
517 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
518 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
519 }
520 initTexture(
521 frame.map->getDataPtr(),
522 frame.map->getDataLength());
523 }
524
Mathias Agopian9f3020d2009-11-06 16:30:18 -0800525 if (!clearReg.isEmpty()) {
526 Region::const_iterator head(clearReg.begin());
527 Region::const_iterator tail(clearReg.end());
528 glEnable(GL_SCISSOR_TEST);
529 while (head != tail) {
530 const Rect& r(*head++);
531 glScissor(r.left, mHeight - r.bottom,
532 r.width(), r.height());
533 glClear(GL_COLOR_BUFFER_BIT);
534 }
535 glDisable(GL_SCISSOR_TEST);
536 }
Mathias Agopiana8826d62009-10-01 03:10:14 -0700537 glDrawTexiOES(xc, yc, 0, animation.width, animation.height);
538 eglSwapBuffers(mDisplay, mSurface);
539
540 nsecs_t now = systemTime();
541 nsecs_t delay = frameDuration - (now - lastFrame);
Mathias Agopiandb7dd2a2012-05-12 15:08:21 -0700542 //ALOGD("%lld, %lld", ns2ms(now - lastFrame), ns2ms(delay));
Mathias Agopiana8826d62009-10-01 03:10:14 -0700543 lastFrame = now;
Mathias Agopiandb7dd2a2012-05-12 15:08:21 -0700544
545 if (delay > 0) {
546 struct timespec spec;
547 spec.tv_sec = (now + delay) / 1000000000;
548 spec.tv_nsec = (now + delay) % 1000000000;
549 int err;
550 do {
551 err = clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &spec, NULL);
552 } while (err<0 && errno == EINTR);
553 }
Kevin Hesterd3782b22012-04-26 10:38:55 -0700554
555 checkExit();
Mathias Agopiana8826d62009-10-01 03:10:14 -0700556 }
Kevin Hesterd3782b22012-04-26 10:38:55 -0700557
Mathias Agopiana8826d62009-10-01 03:10:14 -0700558 usleep(part.pause * ns2us(frameDuration));
Kevin Hesterd3782b22012-04-26 10:38:55 -0700559
560 // For infinite parts, we've now played them at least once, so perhaps exit
561 if(exitPending() && !part.count)
562 break;
Mathias Agopiana8826d62009-10-01 03:10:14 -0700563 }
564
565 // free the textures for this part
566 if (part.count != 1) {
567 for (int j=0 ; j<fcount ; j++) {
568 const Animation::Frame& frame(part.frames[j]);
569 glDeleteTextures(1, &frame.tid);
570 }
571 }
572 }
573
574 return false;
575}
576
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800577// ---------------------------------------------------------------------------
578
579}
580; // namespace android