blob: 1d5f20766c06bfc9b02fa80e5b74606a04b2c648 [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"
Jim Huangc11f4622010-08-10 03:12:15 +080057
Mathias Agopiandb7dd2a2012-05-12 15:08:21 -070058extern "C" int clock_nanosleep(clockid_t clock_id, int flags,
59 const struct timespec *request,
60 struct timespec *remain);
61
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080062namespace android {
63
64// ---------------------------------------------------------------------------
65
Mathias Agopian627e7b52009-05-21 19:21:59 -070066BootAnimation::BootAnimation() : Thread(false)
Mathias Agopiana8826d62009-10-01 03:10:14 -070067{
Mathias Agopian627e7b52009-05-21 19:21:59 -070068 mSession = new SurfaceComposerClient();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080069}
70
71BootAnimation::~BootAnimation() {
72}
73
74void BootAnimation::onFirstRef() {
Mathias Agopianbc726112009-09-23 15:44:05 -070075 status_t err = mSession->linkToComposerDeath(this);
Steve Block3762c312012-01-06 19:20:56 +000076 ALOGE_IF(err, "linkToComposerDeath failed (%s) ", strerror(-err));
Mathias Agopian8434c532009-09-23 18:52:49 -070077 if (err == NO_ERROR) {
Mathias Agopianbc726112009-09-23 15:44:05 -070078 run("BootAnimation", PRIORITY_DISPLAY);
79 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080080}
81
Mathias Agopianbc726112009-09-23 15:44:05 -070082sp<SurfaceComposerClient> BootAnimation::session() const {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080083 return mSession;
84}
85
Mathias Agopianbc726112009-09-23 15:44:05 -070086
87void BootAnimation::binderDied(const wp<IBinder>& who)
88{
89 // woah, surfaceflinger died!
Steve Block5baa3a62011-12-20 16:23:08 +000090 ALOGD("SurfaceFlinger died, exiting...");
Mathias Agopianbc726112009-09-23 15:44:05 -070091
92 // calling requestExit() is not enough here because the Surface code
93 // might be blocked on a condition variable that will never be updated.
94 kill( getpid(), SIGKILL );
95 requestExit();
96}
97
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080098status_t BootAnimation::initTexture(Texture* texture, AssetManager& assets,
99 const char* name) {
100 Asset* asset = assets.open(name, Asset::ACCESS_BUFFER);
101 if (!asset)
102 return NO_INIT;
103 SkBitmap bitmap;
104 SkImageDecoder::DecodeMemory(asset->getBuffer(false), asset->getLength(),
105 &bitmap, SkBitmap::kNo_Config, SkImageDecoder::kDecodePixels_Mode);
106 asset->close();
107 delete asset;
108
109 // ensure we can call getPixels(). No need to call unlock, since the
110 // bitmap will go out of scope when we return from this method.
111 bitmap.lockPixels();
112
113 const int w = bitmap.width();
114 const int h = bitmap.height();
115 const void* p = bitmap.getPixels();
116
117 GLint crop[4] = { 0, h, w, -h };
118 texture->w = w;
119 texture->h = h;
120
121 glGenTextures(1, &texture->name);
122 glBindTexture(GL_TEXTURE_2D, texture->name);
123
124 switch (bitmap.getConfig()) {
125 case SkBitmap::kA8_Config:
126 glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, w, h, 0, GL_ALPHA,
127 GL_UNSIGNED_BYTE, p);
128 break;
129 case SkBitmap::kARGB_4444_Config:
130 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA,
131 GL_UNSIGNED_SHORT_4_4_4_4, p);
132 break;
133 case SkBitmap::kARGB_8888_Config:
134 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA,
135 GL_UNSIGNED_BYTE, p);
136 break;
137 case SkBitmap::kRGB_565_Config:
138 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, w, h, 0, GL_RGB,
139 GL_UNSIGNED_SHORT_5_6_5, p);
140 break;
141 default:
142 break;
143 }
144
145 glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_CROP_RECT_OES, crop);
146 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
147 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
148 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
149 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
150 return NO_ERROR;
151}
152
Mathias Agopiana8826d62009-10-01 03:10:14 -0700153status_t BootAnimation::initTexture(void* buffer, size_t len)
154{
155 //StopWatch watch("blah");
156
157 SkBitmap bitmap;
Mathias Agopian2b99e552011-11-10 15:59:07 -0800158 SkMemoryStream stream(buffer, len);
159 SkImageDecoder* codec = SkImageDecoder::Factory(&stream);
160 codec->setDitherImage(false);
161 if (codec) {
162 codec->decode(&stream, &bitmap,
163 SkBitmap::kRGB_565_Config,
164 SkImageDecoder::kDecodePixels_Mode);
165 delete codec;
166 }
Mathias Agopiana8826d62009-10-01 03:10:14 -0700167
168 // ensure we can call getPixels(). No need to call unlock, since the
169 // bitmap will go out of scope when we return from this method.
170 bitmap.lockPixels();
171
172 const int w = bitmap.width();
173 const int h = bitmap.height();
174 const void* p = bitmap.getPixels();
175
176 GLint crop[4] = { 0, h, w, -h };
177 int tw = 1 << (31 - __builtin_clz(w));
178 int th = 1 << (31 - __builtin_clz(h));
179 if (tw < w) tw <<= 1;
180 if (th < h) th <<= 1;
181
182 switch (bitmap.getConfig()) {
183 case SkBitmap::kARGB_8888_Config:
184 if (tw != w || th != h) {
185 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, tw, th, 0, GL_RGBA,
186 GL_UNSIGNED_BYTE, 0);
187 glTexSubImage2D(GL_TEXTURE_2D, 0,
188 0, 0, w, h, GL_RGBA, GL_UNSIGNED_BYTE, p);
189 } else {
190 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, tw, th, 0, GL_RGBA,
191 GL_UNSIGNED_BYTE, p);
192 }
193 break;
194
195 case SkBitmap::kRGB_565_Config:
196 if (tw != w || th != h) {
197 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, tw, th, 0, GL_RGB,
198 GL_UNSIGNED_SHORT_5_6_5, 0);
199 glTexSubImage2D(GL_TEXTURE_2D, 0,
200 0, 0, w, h, GL_RGB, GL_UNSIGNED_SHORT_5_6_5, p);
201 } else {
202 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, tw, th, 0, GL_RGB,
203 GL_UNSIGNED_SHORT_5_6_5, p);
204 }
205 break;
206 default:
207 break;
208 }
209
210 glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_CROP_RECT_OES, crop);
211
212 return NO_ERROR;
213}
214
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800215status_t BootAnimation::readyToRun() {
216 mAssets.addDefaultAssets();
217
218 DisplayInfo dinfo;
219 status_t status = session()->getDisplayInfo(0, &dinfo);
220 if (status)
221 return -1;
222
223 // create the native surface
Mathias Agopian17f638b2009-04-16 20:04:08 -0700224 sp<SurfaceControl> control = session()->createSurface(
Mathias Agopian9638e5c2011-04-20 14:19:32 -0700225 0, dinfo.w, dinfo.h, PIXEL_FORMAT_RGB_565);
Mathias Agopian439863f2011-06-28 19:09:31 -0700226
227 SurfaceComposerClient::openGlobalTransaction();
Mathias Agopian17f638b2009-04-16 20:04:08 -0700228 control->setLayer(0x40000000);
Mathias Agopian439863f2011-06-28 19:09:31 -0700229 SurfaceComposerClient::closeGlobalTransaction();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800230
Mathias Agopian17f638b2009-04-16 20:04:08 -0700231 sp<Surface> s = control->getSurface();
232
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800233 // initialize opengl and egl
Mathias Agopian738b9a42009-08-06 16:41:02 -0700234 const EGLint attribs[] = {
Mathias Agopian1b253b72011-08-15 15:20:22 -0700235 EGL_RED_SIZE, 8,
236 EGL_GREEN_SIZE, 8,
237 EGL_BLUE_SIZE, 8,
Mathias Agopiana8826d62009-10-01 03:10:14 -0700238 EGL_DEPTH_SIZE, 0,
239 EGL_NONE
Mathias Agopian738b9a42009-08-06 16:41:02 -0700240 };
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800241 EGLint w, h, dummy;
242 EGLint numConfigs;
243 EGLConfig config;
244 EGLSurface surface;
245 EGLContext context;
Mathias Agopian627e7b52009-05-21 19:21:59 -0700246
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800247 EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
Mathias Agopian627e7b52009-05-21 19:21:59 -0700248
249 eglInitialize(display, 0, 0);
Mathias Agopian1b253b72011-08-15 15:20:22 -0700250 eglChooseConfig(display, attribs, &config, 1, &numConfigs);
Mathias Agopian1473f462009-04-10 14:24:30 -0700251 surface = eglCreateWindowSurface(display, config, s.get(), NULL);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800252 context = eglCreateContext(display, config, NULL, NULL);
253 eglQuerySurface(display, surface, EGL_WIDTH, &w);
254 eglQuerySurface(display, surface, EGL_HEIGHT, &h);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700255
Mathias Agopianabac0102009-07-31 14:47:00 -0700256 if (eglMakeCurrent(display, surface, surface, context) == EGL_FALSE)
257 return NO_INIT;
Mathias Agopiana8826d62009-10-01 03:10:14 -0700258
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800259 mDisplay = display;
260 mContext = context;
261 mSurface = surface;
262 mWidth = w;
263 mHeight = h;
Mathias Agopian17f638b2009-04-16 20:04:08 -0700264 mFlingerSurfaceControl = control;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800265 mFlingerSurface = s;
266
Jim Huangc11f4622010-08-10 03:12:15 +0800267 mAndroidAnimation = true;
Jason parksbd9a08d2011-01-31 15:04:34 -0600268
269 // If the device has encryption turned on or is in process
270 // of being encrypted we show the encrypted boot animation.
271 char decrypt[PROPERTY_VALUE_MAX];
272 property_get("vold.decrypt", decrypt, "");
273
274 bool encryptedAnimation = atoi(decrypt) != 0 || !strcmp("trigger_restart_min_framework", decrypt);
275
276 if ((encryptedAnimation &&
277 (access(SYSTEM_ENCRYPTED_BOOTANIMATION_FILE, R_OK) == 0) &&
278 (mZip.open(SYSTEM_ENCRYPTED_BOOTANIMATION_FILE) == NO_ERROR)) ||
279
280 ((access(USER_BOOTANIMATION_FILE, R_OK) == 0) &&
281 (mZip.open(USER_BOOTANIMATION_FILE) == NO_ERROR)) ||
282
283 ((access(SYSTEM_BOOTANIMATION_FILE, R_OK) == 0) &&
284 (mZip.open(SYSTEM_BOOTANIMATION_FILE) == NO_ERROR))) {
Chih-Wei Huang29787512010-11-03 15:33:00 +0800285 mAndroidAnimation = false;
Jason parksbd9a08d2011-01-31 15:04:34 -0600286 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800287
288 return NO_ERROR;
289}
290
Mathias Agopiana8826d62009-10-01 03:10:14 -0700291bool BootAnimation::threadLoop()
292{
293 bool r;
294 if (mAndroidAnimation) {
295 r = android();
296 } else {
297 r = movie();
298 }
299
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800300 eglMakeCurrent(mDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
301 eglDestroyContext(mDisplay, mContext);
302 eglDestroySurface(mDisplay, mSurface);
Mathias Agopian6cf0db22009-04-17 19:36:26 -0700303 mFlingerSurface.clear();
Mathias Agopian17f638b2009-04-16 20:04:08 -0700304 mFlingerSurfaceControl.clear();
Mathias Agopian627e7b52009-05-21 19:21:59 -0700305 eglTerminate(mDisplay);
306 IPCThreadState::self()->stopProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800307 return r;
308}
309
Mathias Agopiana8826d62009-10-01 03:10:14 -0700310bool BootAnimation::android()
311{
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700312 initTexture(&mAndroid[0], mAssets, "images/android-logo-mask.png");
313 initTexture(&mAndroid[1], mAssets, "images/android-logo-shine.png");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800314
315 // clear screen
Mathias Agopiana8826d62009-10-01 03:10:14 -0700316 glShadeModel(GL_FLAT);
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700317 glDisable(GL_DITHER);
318 glDisable(GL_SCISSOR_TEST);
Mathias Agopian59f19e42011-05-06 19:22:12 -0700319 glClearColor(0,0,0,1);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800320 glClear(GL_COLOR_BUFFER_BIT);
321 eglSwapBuffers(mDisplay, mSurface);
322
Mathias Agopiana8826d62009-10-01 03:10:14 -0700323 glEnable(GL_TEXTURE_2D);
324 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
325
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700326 const GLint xc = (mWidth - mAndroid[0].w) / 2;
327 const GLint yc = (mHeight - mAndroid[0].h) / 2;
328 const Rect updateRect(xc, yc, xc + mAndroid[0].w, yc + mAndroid[0].h);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800329
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800330 glScissor(updateRect.left, mHeight - updateRect.bottom, updateRect.width(),
331 updateRect.height());
332
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700333 // Blend state
334 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
335 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
336
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800337 const nsecs_t startTime = systemTime();
338 do {
Mathias Agopian13796652009-03-24 22:49:21 -0700339 nsecs_t now = systemTime();
340 double time = now - startTime;
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700341 float t = 4.0f * float(time / us2ns(16667)) / mAndroid[1].w;
342 GLint offset = (1 - (t - floorf(t))) * mAndroid[1].w;
343 GLint x = xc - offset;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800344
Mathias Agopian81668642009-07-28 11:41:30 -0700345 glDisable(GL_SCISSOR_TEST);
346 glClear(GL_COLOR_BUFFER_BIT);
347
348 glEnable(GL_SCISSOR_TEST);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800349 glDisable(GL_BLEND);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800350 glBindTexture(GL_TEXTURE_2D, mAndroid[1].name);
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700351 glDrawTexiOES(x, yc, 0, mAndroid[1].w, mAndroid[1].h);
352 glDrawTexiOES(x + mAndroid[1].w, yc, 0, mAndroid[1].w, mAndroid[1].h);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800353
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700354 glEnable(GL_BLEND);
355 glBindTexture(GL_TEXTURE_2D, mAndroid[0].name);
356 glDrawTexiOES(xc, yc, 0, mAndroid[0].w, mAndroid[0].h);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800357
Mathias Agopian627e7b52009-05-21 19:21:59 -0700358 EGLBoolean res = eglSwapBuffers(mDisplay, mSurface);
359 if (res == EGL_FALSE)
360 break;
361
Mathias Agopian13796652009-03-24 22:49:21 -0700362 // 12fps: don't animate too fast to preserve CPU
363 const nsecs_t sleepTime = 83333 - ns2us(systemTime() - now);
364 if (sleepTime > 0)
Mathias Agopiana8826d62009-10-01 03:10:14 -0700365 usleep(sleepTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800366 } while (!exitPending());
367
368 glDeleteTextures(1, &mAndroid[0].name);
369 glDeleteTextures(1, &mAndroid[1].name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800370 return false;
371}
372
Mathias Agopiana8826d62009-10-01 03:10:14 -0700373
374bool BootAnimation::movie()
375{
376 ZipFileRO& zip(mZip);
377
378 size_t numEntries = zip.getNumEntries();
379 ZipEntryRO desc = zip.findEntryByName("desc.txt");
380 FileMap* descMap = zip.createEntryFileMap(desc);
Steve Block3762c312012-01-06 19:20:56 +0000381 ALOGE_IF(!descMap, "descMap is null");
Mathias Agopiana8826d62009-10-01 03:10:14 -0700382 if (!descMap) {
383 return false;
384 }
385
386 String8 desString((char const*)descMap->getDataPtr(),
387 descMap->getDataLength());
388 char const* s = desString.string();
389
390 Animation animation;
391
392 // Parse the description file
393 for (;;) {
394 const char* endl = strstr(s, "\n");
395 if (!endl) break;
396 String8 line(s, endl - s);
397 const char* l = line.string();
398 int fps, width, height, count, pause;
399 char path[256];
400 if (sscanf(l, "%d %d %d", &width, &height, &fps) == 3) {
Steve Block5baa3a62011-12-20 16:23:08 +0000401 //ALOGD("> w=%d, h=%d, fps=%d", fps, width, height);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700402 animation.width = width;
403 animation.height = height;
404 animation.fps = fps;
405 }
406 if (sscanf(l, "p %d %d %s", &count, &pause, path) == 3) {
Steve Block5baa3a62011-12-20 16:23:08 +0000407 //ALOGD("> count=%d, pause=%d, path=%s", count, pause, path);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700408 Animation::Part part;
409 part.count = count;
410 part.pause = pause;
411 part.path = path;
412 animation.parts.add(part);
413 }
414 s = ++endl;
415 }
416
417 // read all the data structures
418 const size_t pcount = animation.parts.size();
419 for (size_t i=0 ; i<numEntries ; i++) {
420 char name[256];
421 ZipEntryRO entry = zip.findEntryByIndex(i);
422 if (zip.getEntryFileName(entry, name, 256) == 0) {
423 const String8 entryName(name);
424 const String8 path(entryName.getPathDir());
425 const String8 leaf(entryName.getPathLeaf());
426 if (leaf.size() > 0) {
427 for (int j=0 ; j<pcount ; j++) {
428 if (path == animation.parts[j].path) {
429 int method;
430 // supports only stored png files
431 if (zip.getEntryInfo(entry, &method, 0, 0, 0, 0, 0)) {
432 if (method == ZipFileRO::kCompressStored) {
433 FileMap* map = zip.createEntryFileMap(entry);
434 if (map) {
435 Animation::Frame frame;
436 frame.name = leaf;
437 frame.map = map;
438 Animation::Part& part(animation.parts.editItemAt(j));
439 part.frames.add(frame);
440 }
441 }
442 }
443 }
444 }
445 }
446 }
447 }
448
449 // clear screen
450 glShadeModel(GL_FLAT);
451 glDisable(GL_DITHER);
452 glDisable(GL_SCISSOR_TEST);
453 glDisable(GL_BLEND);
Mathias Agopian59f19e42011-05-06 19:22:12 -0700454 glClearColor(0,0,0,1);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700455 glClear(GL_COLOR_BUFFER_BIT);
456
457 eglSwapBuffers(mDisplay, mSurface);
458
459 glBindTexture(GL_TEXTURE_2D, 0);
460 glEnable(GL_TEXTURE_2D);
461 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
462 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
463 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
464 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
465 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
466
467 const int xc = (mWidth - animation.width) / 2;
468 const int yc = ((mHeight - animation.height) / 2);
469 nsecs_t lastFrame = systemTime();
470 nsecs_t frameDuration = s2ns(1) / animation.fps;
471
Mathias Agopian9f3020d2009-11-06 16:30:18 -0800472 Region clearReg(Rect(mWidth, mHeight));
473 clearReg.subtractSelf(Rect(xc, yc, xc+animation.width, yc+animation.height));
474
Mathias Agopiana8826d62009-10-01 03:10:14 -0700475 for (int i=0 ; i<pcount && !exitPending() ; i++) {
476 const Animation::Part& part(animation.parts[i]);
477 const size_t fcount = part.frames.size();
478 glBindTexture(GL_TEXTURE_2D, 0);
479
480 for (int r=0 ; !part.count || r<part.count ; r++) {
481 for (int j=0 ; j<fcount && !exitPending(); j++) {
482 const Animation::Frame& frame(part.frames[j]);
Mathias Agopiandb7dd2a2012-05-12 15:08:21 -0700483 nsecs_t lastFrame = systemTime();
Mathias Agopiana8826d62009-10-01 03:10:14 -0700484
485 if (r > 0) {
486 glBindTexture(GL_TEXTURE_2D, frame.tid);
487 } else {
488 if (part.count != 1) {
489 glGenTextures(1, &frame.tid);
490 glBindTexture(GL_TEXTURE_2D, frame.tid);
491 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
492 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
493 }
494 initTexture(
495 frame.map->getDataPtr(),
496 frame.map->getDataLength());
497 }
498
Mathias Agopian9f3020d2009-11-06 16:30:18 -0800499 if (!clearReg.isEmpty()) {
500 Region::const_iterator head(clearReg.begin());
501 Region::const_iterator tail(clearReg.end());
502 glEnable(GL_SCISSOR_TEST);
503 while (head != tail) {
504 const Rect& r(*head++);
505 glScissor(r.left, mHeight - r.bottom,
506 r.width(), r.height());
507 glClear(GL_COLOR_BUFFER_BIT);
508 }
509 glDisable(GL_SCISSOR_TEST);
510 }
Mathias Agopiana8826d62009-10-01 03:10:14 -0700511 glDrawTexiOES(xc, yc, 0, animation.width, animation.height);
512 eglSwapBuffers(mDisplay, mSurface);
513
514 nsecs_t now = systemTime();
515 nsecs_t delay = frameDuration - (now - lastFrame);
Mathias Agopiandb7dd2a2012-05-12 15:08:21 -0700516 //ALOGD("%lld, %lld", ns2ms(now - lastFrame), ns2ms(delay));
Mathias Agopiana8826d62009-10-01 03:10:14 -0700517 lastFrame = now;
Mathias Agopiandb7dd2a2012-05-12 15:08:21 -0700518
519 if (delay > 0) {
520 struct timespec spec;
521 spec.tv_sec = (now + delay) / 1000000000;
522 spec.tv_nsec = (now + delay) % 1000000000;
523 int err;
524 do {
525 err = clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &spec, NULL);
526 } while (err<0 && errno == EINTR);
527 }
Mathias Agopiana8826d62009-10-01 03:10:14 -0700528 }
529 usleep(part.pause * ns2us(frameDuration));
530 }
531
532 // free the textures for this part
533 if (part.count != 1) {
534 for (int j=0 ; j<fcount ; j++) {
535 const Animation::Frame& frame(part.frames[j]);
536 glDeleteTextures(1, &frame.tid);
537 }
538 }
539 }
540
541 return false;
542}
543
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800544// ---------------------------------------------------------------------------
545
546}
547; // namespace android