blob: 69c459705e2e204dedd5c304d3d4023419728dde [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 Agopianac31a3b2009-05-21 19:59:24 -070028#include <binder/IPCThreadState.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080029#include <utils/threads.h>
30#include <utils/Atomic.h>
31#include <utils/Errors.h>
32#include <utils/Log.h>
33#include <utils/AssetManager.h>
34
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>
Mathias Agopian738b9a42009-08-06 16:41:02 -070040#include <ui/EGLUtils.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080041
Mathias Agopian000479f2010-02-09 17:46:37 -080042#include <surfaceflinger/ISurfaceComposer.h>
Mathias Agopian770492c2010-05-28 14:22:23 -070043#include <surfaceflinger/ISurfaceComposerClient.h>
Mathias Agopian000479f2010-02-09 17:46:37 -080044
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080045#include <core/SkBitmap.h>
46#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
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080058namespace android {
59
60// ---------------------------------------------------------------------------
61
Mathias Agopian627e7b52009-05-21 19:21:59 -070062BootAnimation::BootAnimation() : Thread(false)
Mathias Agopiana8826d62009-10-01 03:10:14 -070063{
Mathias Agopian627e7b52009-05-21 19:21:59 -070064 mSession = new SurfaceComposerClient();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080065}
66
67BootAnimation::~BootAnimation() {
68}
69
70void BootAnimation::onFirstRef() {
Mathias Agopianbc726112009-09-23 15:44:05 -070071 status_t err = mSession->linkToComposerDeath(this);
72 LOGE_IF(err, "linkToComposerDeath failed (%s) ", strerror(-err));
Mathias Agopian8434c532009-09-23 18:52:49 -070073 if (err == NO_ERROR) {
Mathias Agopianbc726112009-09-23 15:44:05 -070074 run("BootAnimation", PRIORITY_DISPLAY);
75 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080076}
77
Mathias Agopianbc726112009-09-23 15:44:05 -070078sp<SurfaceComposerClient> BootAnimation::session() const {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080079 return mSession;
80}
81
Mathias Agopianbc726112009-09-23 15:44:05 -070082
83void BootAnimation::binderDied(const wp<IBinder>& who)
84{
85 // woah, surfaceflinger died!
86 LOGD("SurfaceFlinger died, exiting...");
87
88 // calling requestExit() is not enough here because the Surface code
89 // might be blocked on a condition variable that will never be updated.
90 kill( getpid(), SIGKILL );
91 requestExit();
92}
93
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080094status_t BootAnimation::initTexture(Texture* texture, AssetManager& assets,
95 const char* name) {
96 Asset* asset = assets.open(name, Asset::ACCESS_BUFFER);
97 if (!asset)
98 return NO_INIT;
99 SkBitmap bitmap;
100 SkImageDecoder::DecodeMemory(asset->getBuffer(false), asset->getLength(),
101 &bitmap, SkBitmap::kNo_Config, SkImageDecoder::kDecodePixels_Mode);
102 asset->close();
103 delete asset;
104
105 // ensure we can call getPixels(). No need to call unlock, since the
106 // bitmap will go out of scope when we return from this method.
107 bitmap.lockPixels();
108
109 const int w = bitmap.width();
110 const int h = bitmap.height();
111 const void* p = bitmap.getPixels();
112
113 GLint crop[4] = { 0, h, w, -h };
114 texture->w = w;
115 texture->h = h;
116
117 glGenTextures(1, &texture->name);
118 glBindTexture(GL_TEXTURE_2D, texture->name);
119
120 switch (bitmap.getConfig()) {
121 case SkBitmap::kA8_Config:
122 glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, w, h, 0, GL_ALPHA,
123 GL_UNSIGNED_BYTE, p);
124 break;
125 case SkBitmap::kARGB_4444_Config:
126 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA,
127 GL_UNSIGNED_SHORT_4_4_4_4, p);
128 break;
129 case SkBitmap::kARGB_8888_Config:
130 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA,
131 GL_UNSIGNED_BYTE, p);
132 break;
133 case SkBitmap::kRGB_565_Config:
134 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, w, h, 0, GL_RGB,
135 GL_UNSIGNED_SHORT_5_6_5, p);
136 break;
137 default:
138 break;
139 }
140
141 glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_CROP_RECT_OES, crop);
142 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
143 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
144 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
145 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
146 return NO_ERROR;
147}
148
Mathias Agopiana8826d62009-10-01 03:10:14 -0700149status_t BootAnimation::initTexture(void* buffer, size_t len)
150{
151 //StopWatch watch("blah");
152
153 SkBitmap bitmap;
154 SkImageDecoder::DecodeMemory(buffer, len,
155 &bitmap, SkBitmap::kRGB_565_Config,
156 SkImageDecoder::kDecodePixels_Mode);
157
158 // ensure we can call getPixels(). No need to call unlock, since the
159 // bitmap will go out of scope when we return from this method.
160 bitmap.lockPixels();
161
162 const int w = bitmap.width();
163 const int h = bitmap.height();
164 const void* p = bitmap.getPixels();
165
166 GLint crop[4] = { 0, h, w, -h };
167 int tw = 1 << (31 - __builtin_clz(w));
168 int th = 1 << (31 - __builtin_clz(h));
169 if (tw < w) tw <<= 1;
170 if (th < h) th <<= 1;
171
172 switch (bitmap.getConfig()) {
173 case SkBitmap::kARGB_8888_Config:
174 if (tw != w || th != h) {
175 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, tw, th, 0, GL_RGBA,
176 GL_UNSIGNED_BYTE, 0);
177 glTexSubImage2D(GL_TEXTURE_2D, 0,
178 0, 0, w, h, GL_RGBA, GL_UNSIGNED_BYTE, p);
179 } else {
180 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, tw, th, 0, GL_RGBA,
181 GL_UNSIGNED_BYTE, p);
182 }
183 break;
184
185 case SkBitmap::kRGB_565_Config:
186 if (tw != w || th != h) {
187 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, tw, th, 0, GL_RGB,
188 GL_UNSIGNED_SHORT_5_6_5, 0);
189 glTexSubImage2D(GL_TEXTURE_2D, 0,
190 0, 0, w, h, GL_RGB, GL_UNSIGNED_SHORT_5_6_5, p);
191 } else {
192 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, tw, th, 0, GL_RGB,
193 GL_UNSIGNED_SHORT_5_6_5, p);
194 }
195 break;
196 default:
197 break;
198 }
199
200 glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_CROP_RECT_OES, crop);
201
202 return NO_ERROR;
203}
204
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800205status_t BootAnimation::readyToRun() {
206 mAssets.addDefaultAssets();
207
208 DisplayInfo dinfo;
209 status_t status = session()->getDisplayInfo(0, &dinfo);
210 if (status)
211 return -1;
212
213 // create the native surface
Mathias Agopian17f638b2009-04-16 20:04:08 -0700214 sp<SurfaceControl> control = session()->createSurface(
Mathias Agopian9638e5c2011-04-20 14:19:32 -0700215 0, dinfo.w, dinfo.h, PIXEL_FORMAT_RGB_565);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800216 session()->openTransaction();
Mathias Agopian17f638b2009-04-16 20:04:08 -0700217 control->setLayer(0x40000000);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800218 session()->closeTransaction();
219
Mathias Agopian17f638b2009-04-16 20:04:08 -0700220 sp<Surface> s = control->getSurface();
221
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800222 // initialize opengl and egl
Mathias Agopian738b9a42009-08-06 16:41:02 -0700223 const EGLint attribs[] = {
Mathias Agopiana8826d62009-10-01 03:10:14 -0700224 EGL_DEPTH_SIZE, 0,
225 EGL_NONE
Mathias Agopian738b9a42009-08-06 16:41:02 -0700226 };
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800227 EGLint w, h, dummy;
228 EGLint numConfigs;
229 EGLConfig config;
230 EGLSurface surface;
231 EGLContext context;
Mathias Agopian627e7b52009-05-21 19:21:59 -0700232
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800233 EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
Mathias Agopian627e7b52009-05-21 19:21:59 -0700234
235 eglInitialize(display, 0, 0);
Mathias Agopian738b9a42009-08-06 16:41:02 -0700236 EGLUtils::selectConfigForNativeWindow(display, attribs, s.get(), &config);
Mathias Agopian1473f462009-04-10 14:24:30 -0700237 surface = eglCreateWindowSurface(display, config, s.get(), NULL);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800238 context = eglCreateContext(display, config, NULL, NULL);
239 eglQuerySurface(display, surface, EGL_WIDTH, &w);
240 eglQuerySurface(display, surface, EGL_HEIGHT, &h);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700241
Mathias Agopianabac0102009-07-31 14:47:00 -0700242 if (eglMakeCurrent(display, surface, surface, context) == EGL_FALSE)
243 return NO_INIT;
Mathias Agopiana8826d62009-10-01 03:10:14 -0700244
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800245 mDisplay = display;
246 mContext = context;
247 mSurface = surface;
248 mWidth = w;
249 mHeight = h;
Mathias Agopian17f638b2009-04-16 20:04:08 -0700250 mFlingerSurfaceControl = control;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800251 mFlingerSurface = s;
252
Jim Huangc11f4622010-08-10 03:12:15 +0800253 mAndroidAnimation = true;
Jason parksbd9a08d2011-01-31 15:04:34 -0600254
255 // If the device has encryption turned on or is in process
256 // of being encrypted we show the encrypted boot animation.
257 char decrypt[PROPERTY_VALUE_MAX];
258 property_get("vold.decrypt", decrypt, "");
259
260 bool encryptedAnimation = atoi(decrypt) != 0 || !strcmp("trigger_restart_min_framework", decrypt);
261
262 if ((encryptedAnimation &&
263 (access(SYSTEM_ENCRYPTED_BOOTANIMATION_FILE, R_OK) == 0) &&
264 (mZip.open(SYSTEM_ENCRYPTED_BOOTANIMATION_FILE) == NO_ERROR)) ||
265
266 ((access(USER_BOOTANIMATION_FILE, R_OK) == 0) &&
267 (mZip.open(USER_BOOTANIMATION_FILE) == NO_ERROR)) ||
268
269 ((access(SYSTEM_BOOTANIMATION_FILE, R_OK) == 0) &&
270 (mZip.open(SYSTEM_BOOTANIMATION_FILE) == NO_ERROR))) {
Chih-Wei Huang29787512010-11-03 15:33:00 +0800271 mAndroidAnimation = false;
Jason parksbd9a08d2011-01-31 15:04:34 -0600272 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800273
274 return NO_ERROR;
275}
276
Mathias Agopiana8826d62009-10-01 03:10:14 -0700277bool BootAnimation::threadLoop()
278{
279 bool r;
280 if (mAndroidAnimation) {
281 r = android();
282 } else {
283 r = movie();
284 }
285
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800286 eglMakeCurrent(mDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
287 eglDestroyContext(mDisplay, mContext);
288 eglDestroySurface(mDisplay, mSurface);
Mathias Agopian6cf0db22009-04-17 19:36:26 -0700289 mFlingerSurface.clear();
Mathias Agopian17f638b2009-04-16 20:04:08 -0700290 mFlingerSurfaceControl.clear();
Mathias Agopian627e7b52009-05-21 19:21:59 -0700291 eglTerminate(mDisplay);
292 IPCThreadState::self()->stopProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800293 return r;
294}
295
Mathias Agopiana8826d62009-10-01 03:10:14 -0700296bool BootAnimation::android()
297{
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700298 initTexture(&mAndroid[0], mAssets, "images/android-logo-mask.png");
299 initTexture(&mAndroid[1], mAssets, "images/android-logo-shine.png");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800300
301 // clear screen
Mathias Agopiana8826d62009-10-01 03:10:14 -0700302 glShadeModel(GL_FLAT);
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700303 glDisable(GL_DITHER);
304 glDisable(GL_SCISSOR_TEST);
Mathias Agopian59f19e42011-05-06 19:22:12 -0700305 glClearColor(0,0,0,1);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800306 glClear(GL_COLOR_BUFFER_BIT);
307 eglSwapBuffers(mDisplay, mSurface);
308
Mathias Agopiana8826d62009-10-01 03:10:14 -0700309 glEnable(GL_TEXTURE_2D);
310 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
311
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700312 const GLint xc = (mWidth - mAndroid[0].w) / 2;
313 const GLint yc = (mHeight - mAndroid[0].h) / 2;
314 const Rect updateRect(xc, yc, xc + mAndroid[0].w, yc + mAndroid[0].h);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800315
316 // draw and update only what we need
Mathias Agopian1473f462009-04-10 14:24:30 -0700317 mFlingerSurface->setSwapRectangle(updateRect);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800318
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800319 glScissor(updateRect.left, mHeight - updateRect.bottom, updateRect.width(),
320 updateRect.height());
321
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700322 // Blend state
323 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
324 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
325
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800326 const nsecs_t startTime = systemTime();
327 do {
Mathias Agopian13796652009-03-24 22:49:21 -0700328 nsecs_t now = systemTime();
329 double time = now - startTime;
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700330 float t = 4.0f * float(time / us2ns(16667)) / mAndroid[1].w;
331 GLint offset = (1 - (t - floorf(t))) * mAndroid[1].w;
332 GLint x = xc - offset;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800333
Mathias Agopian81668642009-07-28 11:41:30 -0700334 glDisable(GL_SCISSOR_TEST);
335 glClear(GL_COLOR_BUFFER_BIT);
336
337 glEnable(GL_SCISSOR_TEST);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800338 glDisable(GL_BLEND);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800339 glBindTexture(GL_TEXTURE_2D, mAndroid[1].name);
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700340 glDrawTexiOES(x, yc, 0, mAndroid[1].w, mAndroid[1].h);
341 glDrawTexiOES(x + mAndroid[1].w, yc, 0, mAndroid[1].w, mAndroid[1].h);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800342
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700343 glEnable(GL_BLEND);
344 glBindTexture(GL_TEXTURE_2D, mAndroid[0].name);
345 glDrawTexiOES(xc, yc, 0, mAndroid[0].w, mAndroid[0].h);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800346
Mathias Agopian627e7b52009-05-21 19:21:59 -0700347 EGLBoolean res = eglSwapBuffers(mDisplay, mSurface);
348 if (res == EGL_FALSE)
349 break;
350
Mathias Agopian13796652009-03-24 22:49:21 -0700351 // 12fps: don't animate too fast to preserve CPU
352 const nsecs_t sleepTime = 83333 - ns2us(systemTime() - now);
353 if (sleepTime > 0)
Mathias Agopiana8826d62009-10-01 03:10:14 -0700354 usleep(sleepTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800355 } while (!exitPending());
356
357 glDeleteTextures(1, &mAndroid[0].name);
358 glDeleteTextures(1, &mAndroid[1].name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800359 return false;
360}
361
Mathias Agopiana8826d62009-10-01 03:10:14 -0700362
363bool BootAnimation::movie()
364{
365 ZipFileRO& zip(mZip);
366
367 size_t numEntries = zip.getNumEntries();
368 ZipEntryRO desc = zip.findEntryByName("desc.txt");
369 FileMap* descMap = zip.createEntryFileMap(desc);
370 LOGE_IF(!descMap, "descMap is null");
371 if (!descMap) {
372 return false;
373 }
374
375 String8 desString((char const*)descMap->getDataPtr(),
376 descMap->getDataLength());
377 char const* s = desString.string();
378
379 Animation animation;
380
381 // Parse the description file
382 for (;;) {
383 const char* endl = strstr(s, "\n");
384 if (!endl) break;
385 String8 line(s, endl - s);
386 const char* l = line.string();
387 int fps, width, height, count, pause;
388 char path[256];
389 if (sscanf(l, "%d %d %d", &width, &height, &fps) == 3) {
390 //LOGD("> w=%d, h=%d, fps=%d", fps, width, height);
391 animation.width = width;
392 animation.height = height;
393 animation.fps = fps;
394 }
395 if (sscanf(l, "p %d %d %s", &count, &pause, path) == 3) {
396 //LOGD("> count=%d, pause=%d, path=%s", count, pause, path);
397 Animation::Part part;
398 part.count = count;
399 part.pause = pause;
400 part.path = path;
401 animation.parts.add(part);
402 }
403 s = ++endl;
404 }
405
406 // read all the data structures
407 const size_t pcount = animation.parts.size();
408 for (size_t i=0 ; i<numEntries ; i++) {
409 char name[256];
410 ZipEntryRO entry = zip.findEntryByIndex(i);
411 if (zip.getEntryFileName(entry, name, 256) == 0) {
412 const String8 entryName(name);
413 const String8 path(entryName.getPathDir());
414 const String8 leaf(entryName.getPathLeaf());
415 if (leaf.size() > 0) {
416 for (int j=0 ; j<pcount ; j++) {
417 if (path == animation.parts[j].path) {
418 int method;
419 // supports only stored png files
420 if (zip.getEntryInfo(entry, &method, 0, 0, 0, 0, 0)) {
421 if (method == ZipFileRO::kCompressStored) {
422 FileMap* map = zip.createEntryFileMap(entry);
423 if (map) {
424 Animation::Frame frame;
425 frame.name = leaf;
426 frame.map = map;
427 Animation::Part& part(animation.parts.editItemAt(j));
428 part.frames.add(frame);
429 }
430 }
431 }
432 }
433 }
434 }
435 }
436 }
437
438 // clear screen
439 glShadeModel(GL_FLAT);
440 glDisable(GL_DITHER);
441 glDisable(GL_SCISSOR_TEST);
442 glDisable(GL_BLEND);
Mathias Agopian59f19e42011-05-06 19:22:12 -0700443 glClearColor(0,0,0,1);
Mathias Agopiana8826d62009-10-01 03:10:14 -0700444 glClear(GL_COLOR_BUFFER_BIT);
445
446 eglSwapBuffers(mDisplay, mSurface);
447
448 glBindTexture(GL_TEXTURE_2D, 0);
449 glEnable(GL_TEXTURE_2D);
450 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
451 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
452 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
453 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
454 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
455
456 const int xc = (mWidth - animation.width) / 2;
457 const int yc = ((mHeight - animation.height) / 2);
458 nsecs_t lastFrame = systemTime();
459 nsecs_t frameDuration = s2ns(1) / animation.fps;
460
Mathias Agopian9f3020d2009-11-06 16:30:18 -0800461 Region clearReg(Rect(mWidth, mHeight));
462 clearReg.subtractSelf(Rect(xc, yc, xc+animation.width, yc+animation.height));
463
Mathias Agopiana8826d62009-10-01 03:10:14 -0700464 for (int i=0 ; i<pcount && !exitPending() ; i++) {
465 const Animation::Part& part(animation.parts[i]);
466 const size_t fcount = part.frames.size();
467 glBindTexture(GL_TEXTURE_2D, 0);
468
469 for (int r=0 ; !part.count || r<part.count ; r++) {
470 for (int j=0 ; j<fcount && !exitPending(); j++) {
471 const Animation::Frame& frame(part.frames[j]);
472
473 if (r > 0) {
474 glBindTexture(GL_TEXTURE_2D, frame.tid);
475 } else {
476 if (part.count != 1) {
477 glGenTextures(1, &frame.tid);
478 glBindTexture(GL_TEXTURE_2D, frame.tid);
479 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
480 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
481 }
482 initTexture(
483 frame.map->getDataPtr(),
484 frame.map->getDataLength());
485 }
486
Mathias Agopian9f3020d2009-11-06 16:30:18 -0800487 if (!clearReg.isEmpty()) {
488 Region::const_iterator head(clearReg.begin());
489 Region::const_iterator tail(clearReg.end());
490 glEnable(GL_SCISSOR_TEST);
491 while (head != tail) {
492 const Rect& r(*head++);
493 glScissor(r.left, mHeight - r.bottom,
494 r.width(), r.height());
495 glClear(GL_COLOR_BUFFER_BIT);
496 }
497 glDisable(GL_SCISSOR_TEST);
498 }
Mathias Agopiana8826d62009-10-01 03:10:14 -0700499 glDrawTexiOES(xc, yc, 0, animation.width, animation.height);
500 eglSwapBuffers(mDisplay, mSurface);
501
502 nsecs_t now = systemTime();
503 nsecs_t delay = frameDuration - (now - lastFrame);
504 lastFrame = now;
505 long wait = ns2us(frameDuration);
506 if (wait > 0)
507 usleep(wait);
508 }
509 usleep(part.pause * ns2us(frameDuration));
510 }
511
512 // free the textures for this part
513 if (part.count != 1) {
514 for (int j=0 ; j<fcount ; j++) {
515 const Animation::Frame& frame(part.frames[j]);
516 glDeleteTextures(1, &frame.tid);
517 }
518 }
519 }
520
521 return false;
522}
523
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800524// ---------------------------------------------------------------------------
525
526}
527; // namespace android