blob: 6b50486074df669c2eb46347f1fc8978f5a02290 [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
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
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080044#include <core/SkBitmap.h>
Mathias Agopian2b99e552011-11-10 15:59:07 -080045#include <core/SkStream.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080046#include <images/SkImageDecoder.h>
47
48#include <GLES/gl.h>
49#include <GLES/glext.h>
50#include <EGL/eglext.h>
51
52#include "BootAnimation.h"
53
Jim Huangc11f4622010-08-10 03:12:15 +080054#define USER_BOOTANIMATION_FILE "/data/local/bootanimation.zip"
55#define SYSTEM_BOOTANIMATION_FILE "/system/media/bootanimation.zip"
Jason parksbd9a08d2011-01-31 15:04:34 -060056#define SYSTEM_ENCRYPTED_BOOTANIMATION_FILE "/system/media/bootanimation-encrypted.zip"
Kevin Hesterd3782b22012-04-26 10:38:55 -070057#define EXIT_PROP_NAME "service.bootanim.exit"
Jim Huangc11f4622010-08-10 03:12:15 +080058
Mathias Agopiandb7dd2a2012-05-12 15:08:21 -070059extern "C" int clock_nanosleep(clockid_t clock_id, int flags,
60 const struct timespec *request,
61 struct timespec *remain);
62
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080063namespace android {
64
65// ---------------------------------------------------------------------------
66
Mathias Agopian627e7b52009-05-21 19:21:59 -070067BootAnimation::BootAnimation() : Thread(false)
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() {
73}
74
75void BootAnimation::onFirstRef() {
Mathias Agopianbc726112009-09-23 15:44:05 -070076 status_t err = mSession->linkToComposerDeath(this);
Steve Block3762c312012-01-06 19:20:56 +000077 ALOGE_IF(err, "linkToComposerDeath failed (%s) ", strerror(-err));
Mathias Agopian8434c532009-09-23 18:52:49 -070078 if (err == NO_ERROR) {
Mathias Agopianbc726112009-09-23 15:44:05 -070079 run("BootAnimation", PRIORITY_DISPLAY);
80 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080081}
82
Mathias Agopianbc726112009-09-23 15:44:05 -070083sp<SurfaceComposerClient> BootAnimation::session() const {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080084 return mSession;
85}
86
Mathias Agopianbc726112009-09-23 15:44:05 -070087
88void BootAnimation::binderDied(const wp<IBinder>& who)
89{
90 // woah, surfaceflinger died!
Steve Block5baa3a62011-12-20 16:23:08 +000091 ALOGD("SurfaceFlinger died, exiting...");
Mathias Agopianbc726112009-09-23 15:44:05 -070092
93 // calling requestExit() is not enough here because the Surface code
94 // might be blocked on a condition variable that will never be updated.
95 kill( getpid(), SIGKILL );
96 requestExit();
97}
98
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080099status_t BootAnimation::initTexture(Texture* texture, AssetManager& assets,
100 const char* name) {
101 Asset* asset = assets.open(name, Asset::ACCESS_BUFFER);
102 if (!asset)
103 return NO_INIT;
104 SkBitmap bitmap;
105 SkImageDecoder::DecodeMemory(asset->getBuffer(false), asset->getLength(),
106 &bitmap, SkBitmap::kNo_Config, SkImageDecoder::kDecodePixels_Mode);
107 asset->close();
108 delete asset;
109
110 // ensure we can call getPixels(). No need to call unlock, since the
111 // bitmap will go out of scope when we return from this method.
112 bitmap.lockPixels();
113
114 const int w = bitmap.width();
115 const int h = bitmap.height();
116 const void* p = bitmap.getPixels();
117
118 GLint crop[4] = { 0, h, w, -h };
119 texture->w = w;
120 texture->h = h;
121
122 glGenTextures(1, &texture->name);
123 glBindTexture(GL_TEXTURE_2D, texture->name);
124
125 switch (bitmap.getConfig()) {
126 case SkBitmap::kA8_Config:
127 glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, w, h, 0, GL_ALPHA,
128 GL_UNSIGNED_BYTE, p);
129 break;
130 case SkBitmap::kARGB_4444_Config:
131 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA,
132 GL_UNSIGNED_SHORT_4_4_4_4, p);
133 break;
134 case SkBitmap::kARGB_8888_Config:
135 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA,
136 GL_UNSIGNED_BYTE, p);
137 break;
138 case SkBitmap::kRGB_565_Config:
139 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, w, h, 0, GL_RGB,
140 GL_UNSIGNED_SHORT_5_6_5, p);
141 break;
142 default:
143 break;
144 }
145
146 glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_CROP_RECT_OES, crop);
147 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
148 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
149 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
150 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
151 return NO_ERROR;
152}
153
Mathias Agopiana8826d62009-10-01 03:10:14 -0700154status_t BootAnimation::initTexture(void* buffer, size_t len)
155{
156 //StopWatch watch("blah");
157
158 SkBitmap bitmap;
Mathias Agopian2b99e552011-11-10 15:59:07 -0800159 SkMemoryStream stream(buffer, len);
160 SkImageDecoder* codec = SkImageDecoder::Factory(&stream);
161 codec->setDitherImage(false);
162 if (codec) {
163 codec->decode(&stream, &bitmap,
Mathias Agopian60691ce2012-06-11 14:08:02 -0700164 SkBitmap::kARGB_8888_Config,
Mathias Agopian2b99e552011-11-10 15:59:07 -0800165 SkImageDecoder::kDecodePixels_Mode);
166 delete codec;
167 }
Mathias Agopiana8826d62009-10-01 03:10:14 -0700168
169 // ensure we can call getPixels(). No need to call unlock, since the
170 // bitmap will go out of scope when we return from this method.
171 bitmap.lockPixels();
172
173 const int w = bitmap.width();
174 const int h = bitmap.height();
175 const void* p = bitmap.getPixels();
176
177 GLint crop[4] = { 0, h, w, -h };
178 int tw = 1 << (31 - __builtin_clz(w));
179 int th = 1 << (31 - __builtin_clz(h));
180 if (tw < w) tw <<= 1;
181 if (th < h) th <<= 1;
182
183 switch (bitmap.getConfig()) {
184 case SkBitmap::kARGB_8888_Config:
185 if (tw != w || th != h) {
186 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, tw, th, 0, GL_RGBA,
187 GL_UNSIGNED_BYTE, 0);
188 glTexSubImage2D(GL_TEXTURE_2D, 0,
189 0, 0, w, h, GL_RGBA, GL_UNSIGNED_BYTE, p);
190 } else {
191 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, tw, th, 0, GL_RGBA,
192 GL_UNSIGNED_BYTE, p);
193 }
194 break;
195
196 case SkBitmap::kRGB_565_Config:
197 if (tw != w || th != h) {
198 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, tw, th, 0, GL_RGB,
199 GL_UNSIGNED_SHORT_5_6_5, 0);
200 glTexSubImage2D(GL_TEXTURE_2D, 0,
201 0, 0, w, h, GL_RGB, GL_UNSIGNED_SHORT_5_6_5, p);
202 } else {
203 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, tw, th, 0, GL_RGB,
204 GL_UNSIGNED_SHORT_5_6_5, p);
205 }
206 break;
207 default:
208 break;
209 }
210
211 glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_CROP_RECT_OES, crop);
212
213 return NO_ERROR;
214}
215
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800216status_t BootAnimation::readyToRun() {
217 mAssets.addDefaultAssets();
218
219 DisplayInfo dinfo;
220 status_t status = session()->getDisplayInfo(0, &dinfo);
221 if (status)
222 return -1;
223
224 // create the native surface
Mathias Agopian17f638b2009-04-16 20:04:08 -0700225 sp<SurfaceControl> control = session()->createSurface(
Mathias Agopian9638e5c2011-04-20 14:19:32 -0700226 0, dinfo.w, dinfo.h, PIXEL_FORMAT_RGB_565);
Mathias Agopian439863f2011-06-28 19:09:31 -0700227
228 SurfaceComposerClient::openGlobalTransaction();
Mathias Agopian17f638b2009-04-16 20:04:08 -0700229 control->setLayer(0x40000000);
Mathias Agopian439863f2011-06-28 19:09:31 -0700230 SurfaceComposerClient::closeGlobalTransaction();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800231
Mathias Agopian17f638b2009-04-16 20:04:08 -0700232 sp<Surface> s = control->getSurface();
233
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800234 // initialize opengl and egl
Mathias Agopian738b9a42009-08-06 16:41:02 -0700235 const EGLint attribs[] = {
Mathias Agopian1b253b72011-08-15 15:20:22 -0700236 EGL_RED_SIZE, 8,
237 EGL_GREEN_SIZE, 8,
238 EGL_BLUE_SIZE, 8,
Mathias Agopiana8826d62009-10-01 03:10:14 -0700239 EGL_DEPTH_SIZE, 0,
240 EGL_NONE
Mathias Agopian738b9a42009-08-06 16:41:02 -0700241 };
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800242 EGLint w, h, dummy;
243 EGLint numConfigs;
244 EGLConfig config;
245 EGLSurface surface;
246 EGLContext context;
Mathias Agopian627e7b52009-05-21 19:21:59 -0700247
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800248 EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
Mathias Agopian627e7b52009-05-21 19:21:59 -0700249
250 eglInitialize(display, 0, 0);
Mathias Agopian1b253b72011-08-15 15:20:22 -0700251 eglChooseConfig(display, attribs, &config, 1, &numConfigs);
Mathias Agopian1473f462009-04-10 14:24:30 -0700252 surface = eglCreateWindowSurface(display, config, s.get(), NULL);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800253 context = eglCreateContext(display, config, NULL, NULL);
254 eglQuerySurface(display, surface, EGL_WIDTH, &w);
255 eglQuerySurface(display, surface, EGL_HEIGHT, &h);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700256
Mathias Agopianabac0102009-07-31 14:47:00 -0700257 if (eglMakeCurrent(display, surface, surface, context) == EGL_FALSE)
258 return NO_INIT;
Mathias Agopiana8826d62009-10-01 03:10:14 -0700259
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800260 mDisplay = display;
261 mContext = context;
262 mSurface = surface;
263 mWidth = w;
264 mHeight = h;
Mathias Agopian17f638b2009-04-16 20:04:08 -0700265 mFlingerSurfaceControl = control;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800266 mFlingerSurface = s;
267
Jim Huangc11f4622010-08-10 03:12:15 +0800268 mAndroidAnimation = true;
Jason parksbd9a08d2011-01-31 15:04:34 -0600269
270 // If the device has encryption turned on or is in process
271 // of being encrypted we show the encrypted boot animation.
272 char decrypt[PROPERTY_VALUE_MAX];
273 property_get("vold.decrypt", decrypt, "");
274
275 bool encryptedAnimation = atoi(decrypt) != 0 || !strcmp("trigger_restart_min_framework", decrypt);
276
277 if ((encryptedAnimation &&
278 (access(SYSTEM_ENCRYPTED_BOOTANIMATION_FILE, R_OK) == 0) &&
279 (mZip.open(SYSTEM_ENCRYPTED_BOOTANIMATION_FILE) == NO_ERROR)) ||
280
281 ((access(USER_BOOTANIMATION_FILE, R_OK) == 0) &&
282 (mZip.open(USER_BOOTANIMATION_FILE) == NO_ERROR)) ||
283
284 ((access(SYSTEM_BOOTANIMATION_FILE, R_OK) == 0) &&
285 (mZip.open(SYSTEM_BOOTANIMATION_FILE) == NO_ERROR))) {
Chih-Wei Huang29787512010-11-03 15:33:00 +0800286 mAndroidAnimation = false;
Jason parksbd9a08d2011-01-31 15:04:34 -0600287 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800288
289 return NO_ERROR;
290}
291
Mathias Agopiana8826d62009-10-01 03:10:14 -0700292bool BootAnimation::threadLoop()
293{
294 bool r;
295 if (mAndroidAnimation) {
296 r = android();
297 } else {
298 r = movie();
299 }
300
Kevin Hesterd3782b22012-04-26 10:38:55 -0700301 // No need to force exit anymore
302 property_set(EXIT_PROP_NAME, "0");
303
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800304 eglMakeCurrent(mDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
305 eglDestroyContext(mDisplay, mContext);
306 eglDestroySurface(mDisplay, mSurface);
Mathias Agopian6cf0db22009-04-17 19:36:26 -0700307 mFlingerSurface.clear();
Mathias Agopian17f638b2009-04-16 20:04:08 -0700308 mFlingerSurfaceControl.clear();
Mathias Agopian627e7b52009-05-21 19:21:59 -0700309 eglTerminate(mDisplay);
310 IPCThreadState::self()->stopProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800311 return r;
312}
313
Mathias Agopiana8826d62009-10-01 03:10:14 -0700314bool BootAnimation::android()
315{
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700316 initTexture(&mAndroid[0], mAssets, "images/android-logo-mask.png");
317 initTexture(&mAndroid[1], mAssets, "images/android-logo-shine.png");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800318
319 // clear screen
Mathias Agopiana8826d62009-10-01 03:10:14 -0700320 glShadeModel(GL_FLAT);
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700321 glDisable(GL_DITHER);
322 glDisable(GL_SCISSOR_TEST);
Mathias Agopian59f19e42011-05-06 19:22:12 -0700323 glClearColor(0,0,0,1);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800324 glClear(GL_COLOR_BUFFER_BIT);
325 eglSwapBuffers(mDisplay, mSurface);
326
Mathias Agopiana8826d62009-10-01 03:10:14 -0700327 glEnable(GL_TEXTURE_2D);
328 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
329
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700330 const GLint xc = (mWidth - mAndroid[0].w) / 2;
331 const GLint yc = (mHeight - mAndroid[0].h) / 2;
332 const Rect updateRect(xc, yc, xc + mAndroid[0].w, yc + mAndroid[0].h);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800333
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800334 glScissor(updateRect.left, mHeight - updateRect.bottom, updateRect.width(),
335 updateRect.height());
336
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700337 // Blend state
338 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
339 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
340
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800341 const nsecs_t startTime = systemTime();
342 do {
Mathias Agopian13796652009-03-24 22:49:21 -0700343 nsecs_t now = systemTime();
344 double time = now - startTime;
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700345 float t = 4.0f * float(time / us2ns(16667)) / mAndroid[1].w;
346 GLint offset = (1 - (t - floorf(t))) * mAndroid[1].w;
347 GLint x = xc - offset;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800348
Mathias Agopian81668642009-07-28 11:41:30 -0700349 glDisable(GL_SCISSOR_TEST);
350 glClear(GL_COLOR_BUFFER_BIT);
351
352 glEnable(GL_SCISSOR_TEST);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800353 glDisable(GL_BLEND);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800354 glBindTexture(GL_TEXTURE_2D, mAndroid[1].name);
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700355 glDrawTexiOES(x, yc, 0, mAndroid[1].w, mAndroid[1].h);
356 glDrawTexiOES(x + mAndroid[1].w, yc, 0, mAndroid[1].w, mAndroid[1].h);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800357
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700358 glEnable(GL_BLEND);
359 glBindTexture(GL_TEXTURE_2D, mAndroid[0].name);
360 glDrawTexiOES(xc, yc, 0, mAndroid[0].w, mAndroid[0].h);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800361
Mathias Agopian627e7b52009-05-21 19:21:59 -0700362 EGLBoolean res = eglSwapBuffers(mDisplay, mSurface);
363 if (res == EGL_FALSE)
364 break;
365
Mathias Agopian13796652009-03-24 22:49:21 -0700366 // 12fps: don't animate too fast to preserve CPU
367 const nsecs_t sleepTime = 83333 - ns2us(systemTime() - now);
368 if (sleepTime > 0)
Mathias Agopiana8826d62009-10-01 03:10:14 -0700369 usleep(sleepTime);
Kevin Hesterd3782b22012-04-26 10:38:55 -0700370
371 checkExit();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800372 } while (!exitPending());
373
374 glDeleteTextures(1, &mAndroid[0].name);
375 glDeleteTextures(1, &mAndroid[1].name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800376 return false;
377}
378
Mathias Agopiana8826d62009-10-01 03:10:14 -0700379
Kevin Hesterd3782b22012-04-26 10:38:55 -0700380void BootAnimation::checkExit() {
381 // Allow surface flinger to gracefully request shutdown
382 char value[PROPERTY_VALUE_MAX];
383 property_get(EXIT_PROP_NAME, value, "0");
384 int exitnow = atoi(value);
385 if (exitnow) {
386 requestExit();
387 }
388}
389
Mathias Agopiana8826d62009-10-01 03:10:14 -0700390bool BootAnimation::movie()
391{
392 ZipFileRO& zip(mZip);
393
394 size_t numEntries = zip.getNumEntries();
395 ZipEntryRO desc = zip.findEntryByName("desc.txt");
396 FileMap* descMap = zip.createEntryFileMap(desc);
Steve Block3762c312012-01-06 19:20:56 +0000397 ALOGE_IF(!descMap, "descMap is null");
Mathias Agopiana8826d62009-10-01 03:10:14 -0700398 if (!descMap) {
399 return false;
400 }
401
402 String8 desString((char const*)descMap->getDataPtr(),
403 descMap->getDataLength());
404 char const* s = desString.string();
405
406 Animation animation;
407
408 // Parse the description file
409 for (;;) {
410 const char* endl = strstr(s, "\n");
411 if (!endl) break;
412 String8 line(s, endl - s);
413 const char* l = line.string();
414 int fps, width, height, count, pause;
415 char path[256];
Kevin Hesterd3782b22012-04-26 10:38:55 -0700416 char pathType;
Mathias Agopiana8826d62009-10-01 03:10:14 -0700417 if (sscanf(l, "%d %d %d", &width, &height, &fps) == 3) {
Kevin Hesterd3782b22012-04-26 10:38:55 -0700418 //LOGD("> w=%d, h=%d, fps=%d", width, height, fps);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700419 animation.width = width;
420 animation.height = height;
421 animation.fps = fps;
422 }
Kevin Hesterd3782b22012-04-26 10:38:55 -0700423 else if (sscanf(l, " %c %d %d %s", &pathType, &count, &pause, path) == 4) {
424 //LOGD("> type=%c, count=%d, pause=%d, path=%s", pathType, count, pause, path);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700425 Animation::Part part;
Kevin Hesterd3782b22012-04-26 10:38:55 -0700426 part.playUntilComplete = pathType == 'c';
Mathias Agopiana8826d62009-10-01 03:10:14 -0700427 part.count = count;
428 part.pause = pause;
429 part.path = path;
430 animation.parts.add(part);
431 }
Kevin Hesterd3782b22012-04-26 10:38:55 -0700432
Mathias Agopiana8826d62009-10-01 03:10:14 -0700433 s = ++endl;
434 }
435
436 // read all the data structures
437 const size_t pcount = animation.parts.size();
438 for (size_t i=0 ; i<numEntries ; i++) {
439 char name[256];
440 ZipEntryRO entry = zip.findEntryByIndex(i);
441 if (zip.getEntryFileName(entry, name, 256) == 0) {
442 const String8 entryName(name);
443 const String8 path(entryName.getPathDir());
444 const String8 leaf(entryName.getPathLeaf());
445 if (leaf.size() > 0) {
446 for (int j=0 ; j<pcount ; j++) {
447 if (path == animation.parts[j].path) {
448 int method;
449 // supports only stored png files
450 if (zip.getEntryInfo(entry, &method, 0, 0, 0, 0, 0)) {
451 if (method == ZipFileRO::kCompressStored) {
452 FileMap* map = zip.createEntryFileMap(entry);
453 if (map) {
454 Animation::Frame frame;
455 frame.name = leaf;
456 frame.map = map;
457 Animation::Part& part(animation.parts.editItemAt(j));
458 part.frames.add(frame);
459 }
460 }
461 }
462 }
463 }
464 }
465 }
466 }
467
468 // clear screen
469 glShadeModel(GL_FLAT);
470 glDisable(GL_DITHER);
471 glDisable(GL_SCISSOR_TEST);
472 glDisable(GL_BLEND);
Mathias Agopian59f19e42011-05-06 19:22:12 -0700473 glClearColor(0,0,0,1);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700474 glClear(GL_COLOR_BUFFER_BIT);
475
476 eglSwapBuffers(mDisplay, mSurface);
477
478 glBindTexture(GL_TEXTURE_2D, 0);
479 glEnable(GL_TEXTURE_2D);
480 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
481 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
482 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
483 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
484 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
485
486 const int xc = (mWidth - animation.width) / 2;
487 const int yc = ((mHeight - animation.height) / 2);
488 nsecs_t lastFrame = systemTime();
489 nsecs_t frameDuration = s2ns(1) / animation.fps;
490
Mathias Agopian9f3020d2009-11-06 16:30:18 -0800491 Region clearReg(Rect(mWidth, mHeight));
492 clearReg.subtractSelf(Rect(xc, yc, xc+animation.width, yc+animation.height));
493
Kevin Hesterd3782b22012-04-26 10:38:55 -0700494 for (int i=0 ; i<pcount ; i++) {
Mathias Agopiana8826d62009-10-01 03:10:14 -0700495 const Animation::Part& part(animation.parts[i]);
496 const size_t fcount = part.frames.size();
497 glBindTexture(GL_TEXTURE_2D, 0);
498
499 for (int r=0 ; !part.count || r<part.count ; r++) {
Kevin Hesterd3782b22012-04-26 10:38:55 -0700500 // Exit any non playuntil complete parts immediately
501 if(exitPending() && !part.playUntilComplete)
502 break;
503
504 for (int j=0 ; j<fcount && (!exitPending() || part.playUntilComplete) ; j++) {
Mathias Agopiana8826d62009-10-01 03:10:14 -0700505 const Animation::Frame& frame(part.frames[j]);
Mathias Agopiandb7dd2a2012-05-12 15:08:21 -0700506 nsecs_t lastFrame = systemTime();
Mathias Agopiana8826d62009-10-01 03:10:14 -0700507
508 if (r > 0) {
509 glBindTexture(GL_TEXTURE_2D, frame.tid);
510 } else {
511 if (part.count != 1) {
512 glGenTextures(1, &frame.tid);
513 glBindTexture(GL_TEXTURE_2D, frame.tid);
514 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
515 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
516 }
517 initTexture(
518 frame.map->getDataPtr(),
519 frame.map->getDataLength());
520 }
521
Mathias Agopian9f3020d2009-11-06 16:30:18 -0800522 if (!clearReg.isEmpty()) {
523 Region::const_iterator head(clearReg.begin());
524 Region::const_iterator tail(clearReg.end());
525 glEnable(GL_SCISSOR_TEST);
526 while (head != tail) {
527 const Rect& r(*head++);
528 glScissor(r.left, mHeight - r.bottom,
529 r.width(), r.height());
530 glClear(GL_COLOR_BUFFER_BIT);
531 }
532 glDisable(GL_SCISSOR_TEST);
533 }
Mathias Agopiana8826d62009-10-01 03:10:14 -0700534 glDrawTexiOES(xc, yc, 0, animation.width, animation.height);
535 eglSwapBuffers(mDisplay, mSurface);
536
537 nsecs_t now = systemTime();
538 nsecs_t delay = frameDuration - (now - lastFrame);
Mathias Agopiandb7dd2a2012-05-12 15:08:21 -0700539 //ALOGD("%lld, %lld", ns2ms(now - lastFrame), ns2ms(delay));
Mathias Agopiana8826d62009-10-01 03:10:14 -0700540 lastFrame = now;
Mathias Agopiandb7dd2a2012-05-12 15:08:21 -0700541
542 if (delay > 0) {
543 struct timespec spec;
544 spec.tv_sec = (now + delay) / 1000000000;
545 spec.tv_nsec = (now + delay) % 1000000000;
546 int err;
547 do {
548 err = clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &spec, NULL);
549 } while (err<0 && errno == EINTR);
550 }
Kevin Hesterd3782b22012-04-26 10:38:55 -0700551
552 checkExit();
Mathias Agopiana8826d62009-10-01 03:10:14 -0700553 }
Kevin Hesterd3782b22012-04-26 10:38:55 -0700554
Mathias Agopiana8826d62009-10-01 03:10:14 -0700555 usleep(part.pause * ns2us(frameDuration));
Kevin Hesterd3782b22012-04-26 10:38:55 -0700556
557 // For infinite parts, we've now played them at least once, so perhaps exit
558 if(exitPending() && !part.count)
559 break;
Mathias Agopiana8826d62009-10-01 03:10:14 -0700560 }
561
562 // free the textures for this part
563 if (part.count != 1) {
564 for (int j=0 ; j<fcount ; j++) {
565 const Animation::Frame& frame(part.frames[j]);
566 glDeleteTextures(1, &frame.tid);
567 }
568 }
569 }
570
571 return false;
572}
573
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800574// ---------------------------------------------------------------------------
575
576}
577; // namespace android