BootAnimation.cpp 18.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*
 * Copyright (C) 2007 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

17 18
#define LOG_TAG "BootAnimation"

19 20 21 22 23
#include <stdint.h>
#include <sys/types.h>
#include <math.h>
#include <fcntl.h>
#include <utils/misc.h>
Mathias Agopian's avatar
Mathias Agopian committed
24
#include <signal.h>
25

26 27
#include <cutils/properties.h>

Mathias Agopian's avatar
Mathias Agopian committed
28
#include <androidfw/AssetManager.h>
Mathias Agopian's avatar
Mathias Agopian committed
29
#include <binder/IPCThreadState.h>
30 31 32
#include <utils/Atomic.h>
#include <utils/Errors.h>
#include <utils/Log.h>
Mathias Agopian's avatar
Mathias Agopian committed
33
#include <utils/threads.h>
34 35 36 37 38

#include <ui/PixelFormat.h>
#include <ui/Rect.h>
#include <ui/Region.h>
#include <ui/DisplayInfo.h>
39
#include <ui/FramebufferNativeWindow.h>
40

Jeff Brown's avatar
Jeff Brown committed
41
#include <gui/ISurfaceComposer.h>
Mathias Agopian's avatar
Mathias Agopian committed
42 43
#include <gui/Surface.h>
#include <gui/SurfaceComposerClient.h>
44

45
#include <core/SkBitmap.h>
46
#include <core/SkStream.h>
47 48 49 50 51 52 53 54
#include <images/SkImageDecoder.h>

#include <GLES/gl.h>
#include <GLES/glext.h>
#include <EGL/eglext.h>

#include "BootAnimation.h"

55 56
#define USER_BOOTANIMATION_FILE "/data/local/bootanimation.zip"
#define SYSTEM_BOOTANIMATION_FILE "/system/media/bootanimation.zip"
57
#define SYSTEM_ENCRYPTED_BOOTANIMATION_FILE "/system/media/bootanimation-encrypted.zip"
Kevin Hester's avatar
Kevin Hester committed
58
#define EXIT_PROP_NAME "service.bootanim.exit"
59

Mathias Agopian's avatar
Mathias Agopian committed
60 61 62 63
extern "C" int clock_nanosleep(clockid_t clock_id, int flags,
                           const struct timespec *request,
                           struct timespec *remain);

64 65 66 67
namespace android {

// ---------------------------------------------------------------------------

68
BootAnimation::BootAnimation() : Thread(false)
Mathias Agopian's avatar
Mathias Agopian committed
69
{
70
    mSession = new SurfaceComposerClient();
71 72 73 74 75 76
}

BootAnimation::~BootAnimation() {
}

void BootAnimation::onFirstRef() {
77
    status_t err = mSession->linkToComposerDeath(this);
78
    ALOGE_IF(err, "linkToComposerDeath failed (%s) ", strerror(-err));
79
    if (err == NO_ERROR) {
80 81
        run("BootAnimation", PRIORITY_DISPLAY);
    }
82 83
}

84
sp<SurfaceComposerClient> BootAnimation::session() const {
85 86 87
    return mSession;
}

88 89 90 91

void BootAnimation::binderDied(const wp<IBinder>& who)
{
    // woah, surfaceflinger died!
92
    ALOGD("SurfaceFlinger died, exiting...");
93 94 95 96 97 98 99

    // calling requestExit() is not enough here because the Surface code
    // might be blocked on a condition variable that will never be updated.
    kill( getpid(), SIGKILL );
    requestExit();
}

100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
status_t BootAnimation::initTexture(Texture* texture, AssetManager& assets,
        const char* name) {
    Asset* asset = assets.open(name, Asset::ACCESS_BUFFER);
    if (!asset)
        return NO_INIT;
    SkBitmap bitmap;
    SkImageDecoder::DecodeMemory(asset->getBuffer(false), asset->getLength(),
            &bitmap, SkBitmap::kNo_Config, SkImageDecoder::kDecodePixels_Mode);
    asset->close();
    delete asset;

    // ensure we can call getPixels(). No need to call unlock, since the
    // bitmap will go out of scope when we return from this method.
    bitmap.lockPixels();

    const int w = bitmap.width();
    const int h = bitmap.height();
    const void* p = bitmap.getPixels();

    GLint crop[4] = { 0, h, w, -h };
    texture->w = w;
    texture->h = h;

    glGenTextures(1, &texture->name);
    glBindTexture(GL_TEXTURE_2D, texture->name);

    switch (bitmap.getConfig()) {
        case SkBitmap::kA8_Config:
            glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, w, h, 0, GL_ALPHA,
                    GL_UNSIGNED_BYTE, p);
            break;
        case SkBitmap::kARGB_4444_Config:
            glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA,
                    GL_UNSIGNED_SHORT_4_4_4_4, p);
            break;
        case SkBitmap::kARGB_8888_Config:
            glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA,
                    GL_UNSIGNED_BYTE, p);
            break;
        case SkBitmap::kRGB_565_Config:
            glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, w, h, 0, GL_RGB,
                    GL_UNSIGNED_SHORT_5_6_5, p);
            break;
        default:
            break;
    }

    glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_CROP_RECT_OES, crop);
    glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
    glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
    glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
    return NO_ERROR;
}

Mathias Agopian's avatar
Mathias Agopian committed
155 156 157 158 159
status_t BootAnimation::initTexture(void* buffer, size_t len)
{
    //StopWatch watch("blah");

    SkBitmap bitmap;
160 161 162
    SkMemoryStream  stream(buffer, len);
    SkImageDecoder* codec = SkImageDecoder::Factory(&stream);
    if (codec) {
163
        codec->setDitherImage(false);
164
        codec->decode(&stream, &bitmap,
165
                SkBitmap::kARGB_8888_Config,
166 167 168
                SkImageDecoder::kDecodePixels_Mode);
        delete codec;
    }
Mathias Agopian's avatar
Mathias Agopian committed
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216

    // ensure we can call getPixels(). No need to call unlock, since the
    // bitmap will go out of scope when we return from this method.
    bitmap.lockPixels();

    const int w = bitmap.width();
    const int h = bitmap.height();
    const void* p = bitmap.getPixels();

    GLint crop[4] = { 0, h, w, -h };
    int tw = 1 << (31 - __builtin_clz(w));
    int th = 1 << (31 - __builtin_clz(h));
    if (tw < w) tw <<= 1;
    if (th < h) th <<= 1;

    switch (bitmap.getConfig()) {
        case SkBitmap::kARGB_8888_Config:
            if (tw != w || th != h) {
                glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, tw, th, 0, GL_RGBA,
                        GL_UNSIGNED_BYTE, 0);
                glTexSubImage2D(GL_TEXTURE_2D, 0,
                        0, 0, w, h, GL_RGBA, GL_UNSIGNED_BYTE, p);
            } else {
                glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, tw, th, 0, GL_RGBA,
                        GL_UNSIGNED_BYTE, p);
            }
            break;

        case SkBitmap::kRGB_565_Config:
            if (tw != w || th != h) {
                glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, tw, th, 0, GL_RGB,
                        GL_UNSIGNED_SHORT_5_6_5, 0);
                glTexSubImage2D(GL_TEXTURE_2D, 0,
                        0, 0, w, h, GL_RGB, GL_UNSIGNED_SHORT_5_6_5, p);
            } else {
                glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, tw, th, 0, GL_RGB,
                        GL_UNSIGNED_SHORT_5_6_5, p);
            }
            break;
        default:
            break;
    }

    glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_CROP_RECT_OES, crop);

    return NO_ERROR;
}

217 218 219
status_t BootAnimation::readyToRun() {
    mAssets.addDefaultAssets();

Jeff Brown's avatar
Jeff Brown committed
220 221
    sp<IBinder> dtoken(SurfaceComposerClient::getBuiltInDisplay(
            ISurfaceComposer::eDisplayIdMain));
222
    DisplayInfo dinfo;
Jeff Brown's avatar
Jeff Brown committed
223
    status_t status = SurfaceComposerClient::getDisplayInfo(dtoken, &dinfo);
224 225 226 227
    if (status)
        return -1;

    // create the native surface
Jeff Brown's avatar
Jeff Brown committed
228 229
    sp<SurfaceControl> control = session()->createSurface(String8("BootAnimation"),
            dinfo.w, dinfo.h, PIXEL_FORMAT_RGB_565);
230 231

    SurfaceComposerClient::openGlobalTransaction();
232
    control->setLayer(0x40000000);
233
    SurfaceComposerClient::closeGlobalTransaction();
234

235 236
    sp<Surface> s = control->getSurface();

237
    // initialize opengl and egl
238
    const EGLint attribs[] = {
239 240 241
            EGL_RED_SIZE,   8,
            EGL_GREEN_SIZE, 8,
            EGL_BLUE_SIZE,  8,
Mathias Agopian's avatar
Mathias Agopian committed
242 243
            EGL_DEPTH_SIZE, 0,
            EGL_NONE
244
    };
245 246 247 248 249
    EGLint w, h, dummy;
    EGLint numConfigs;
    EGLConfig config;
    EGLSurface surface;
    EGLContext context;
250

251
    EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
252 253

    eglInitialize(display, 0, 0);
254
    eglChooseConfig(display, attribs, &config, 1, &numConfigs);
255
    surface = eglCreateWindowSurface(display, config, s.get(), NULL);
256 257 258
    context = eglCreateContext(display, config, NULL, NULL);
    eglQuerySurface(display, surface, EGL_WIDTH, &w);
    eglQuerySurface(display, surface, EGL_HEIGHT, &h);
Mathias Agopian's avatar
Mathias Agopian committed
259

260 261
    if (eglMakeCurrent(display, surface, surface, context) == EGL_FALSE)
        return NO_INIT;
Mathias Agopian's avatar
Mathias Agopian committed
262

263 264 265 266 267
    mDisplay = display;
    mContext = context;
    mSurface = surface;
    mWidth = w;
    mHeight = h;
268
    mFlingerSurfaceControl = control;
269 270
    mFlingerSurface = s;

271
    mAndroidAnimation = true;
272

273
    // If the device has encryption turned on or is in process
274 275 276 277 278 279 280 281 282 283 284 285 286 287 288
    // of being encrypted we show the encrypted boot animation.
    char decrypt[PROPERTY_VALUE_MAX];
    property_get("vold.decrypt", decrypt, "");

    bool encryptedAnimation = atoi(decrypt) != 0 || !strcmp("trigger_restart_min_framework", decrypt);

    if ((encryptedAnimation &&
            (access(SYSTEM_ENCRYPTED_BOOTANIMATION_FILE, R_OK) == 0) &&
            (mZip.open(SYSTEM_ENCRYPTED_BOOTANIMATION_FILE) == NO_ERROR)) ||

            ((access(USER_BOOTANIMATION_FILE, R_OK) == 0) &&
            (mZip.open(USER_BOOTANIMATION_FILE) == NO_ERROR)) ||

            ((access(SYSTEM_BOOTANIMATION_FILE, R_OK) == 0) &&
            (mZip.open(SYSTEM_BOOTANIMATION_FILE) == NO_ERROR))) {
289
        mAndroidAnimation = false;
290
    }
291 292 293 294

    return NO_ERROR;
}

Mathias Agopian's avatar
Mathias Agopian committed
295 296 297 298 299 300 301 302 303
bool BootAnimation::threadLoop()
{
    bool r;
    if (mAndroidAnimation) {
        r = android();
    } else {
        r = movie();
    }

Kevin Hester's avatar
Kevin Hester committed
304 305 306
    // No need to force exit anymore
    property_set(EXIT_PROP_NAME, "0");

307 308 309
    eglMakeCurrent(mDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
    eglDestroyContext(mDisplay, mContext);
    eglDestroySurface(mDisplay, mSurface);
310
    mFlingerSurface.clear();
311
    mFlingerSurfaceControl.clear();
312 313
    eglTerminate(mDisplay);
    IPCThreadState::self()->stopProcess();
314 315 316
    return r;
}

Mathias Agopian's avatar
Mathias Agopian committed
317 318
bool BootAnimation::android()
{
319 320
    initTexture(&mAndroid[0], mAssets, "images/android-logo-mask.png");
    initTexture(&mAndroid[1], mAssets, "images/android-logo-shine.png");
321 322

    // clear screen
Mathias Agopian's avatar
Mathias Agopian committed
323
    glShadeModel(GL_FLAT);
324 325
    glDisable(GL_DITHER);
    glDisable(GL_SCISSOR_TEST);
326
    glClearColor(0,0,0,1);
327 328 329
    glClear(GL_COLOR_BUFFER_BIT);
    eglSwapBuffers(mDisplay, mSurface);

Mathias Agopian's avatar
Mathias Agopian committed
330 331 332
    glEnable(GL_TEXTURE_2D);
    glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);

333 334 335
    const GLint xc = (mWidth  - mAndroid[0].w) / 2;
    const GLint yc = (mHeight - mAndroid[0].h) / 2;
    const Rect updateRect(xc, yc, xc + mAndroid[0].w, yc + mAndroid[0].h);
336 337 338 339

    glScissor(updateRect.left, mHeight - updateRect.bottom, updateRect.width(),
            updateRect.height());

340 341 342 343
    // Blend state
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);

344 345
    const nsecs_t startTime = systemTime();
    do {
346 347
        nsecs_t now = systemTime();
        double time = now - startTime;
348 349 350
        float t = 4.0f * float(time / us2ns(16667)) / mAndroid[1].w;
        GLint offset = (1 - (t - floorf(t))) * mAndroid[1].w;
        GLint x = xc - offset;
351

352 353 354 355
        glDisable(GL_SCISSOR_TEST);
        glClear(GL_COLOR_BUFFER_BIT);

        glEnable(GL_SCISSOR_TEST);
356 357
        glDisable(GL_BLEND);
        glBindTexture(GL_TEXTURE_2D, mAndroid[1].name);
358 359
        glDrawTexiOES(x,                 yc, 0, mAndroid[1].w, mAndroid[1].h);
        glDrawTexiOES(x + mAndroid[1].w, yc, 0, mAndroid[1].w, mAndroid[1].h);
360

361 362 363
        glEnable(GL_BLEND);
        glBindTexture(GL_TEXTURE_2D, mAndroid[0].name);
        glDrawTexiOES(xc, yc, 0, mAndroid[0].w, mAndroid[0].h);
364

365 366 367 368
        EGLBoolean res = eglSwapBuffers(mDisplay, mSurface);
        if (res == EGL_FALSE)
            break;

369 370 371
        // 12fps: don't animate too fast to preserve CPU
        const nsecs_t sleepTime = 83333 - ns2us(systemTime() - now);
        if (sleepTime > 0)
Mathias Agopian's avatar
Mathias Agopian committed
372
            usleep(sleepTime);
Kevin Hester's avatar
Kevin Hester committed
373 374

        checkExit();
375 376 377 378 379 380 381
    } while (!exitPending());

    glDeleteTextures(1, &mAndroid[0].name);
    glDeleteTextures(1, &mAndroid[1].name);
    return false;
}

Mathias Agopian's avatar
Mathias Agopian committed
382

Kevin Hester's avatar
Kevin Hester committed
383 384 385 386 387 388 389 390 391 392
void BootAnimation::checkExit() {
    // Allow surface flinger to gracefully request shutdown
    char value[PROPERTY_VALUE_MAX];
    property_get(EXIT_PROP_NAME, value, "0");
    int exitnow = atoi(value);
    if (exitnow) {
        requestExit();
    }
}

Mathias Agopian's avatar
Mathias Agopian committed
393 394 395 396 397 398 399
bool BootAnimation::movie()
{
    ZipFileRO& zip(mZip);

    size_t numEntries = zip.getNumEntries();
    ZipEntryRO desc = zip.findEntryByName("desc.txt");
    FileMap* descMap = zip.createEntryFileMap(desc);
400
    ALOGE_IF(!descMap, "descMap is null");
Mathias Agopian's avatar
Mathias Agopian committed
401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418
    if (!descMap) {
        return false;
    }

    String8 desString((char const*)descMap->getDataPtr(),
            descMap->getDataLength());
    char const* s = desString.string();

    Animation animation;

    // Parse the description file
    for (;;) {
        const char* endl = strstr(s, "\n");
        if (!endl) break;
        String8 line(s, endl - s);
        const char* l = line.string();
        int fps, width, height, count, pause;
        char path[256];
Kevin Hester's avatar
Kevin Hester committed
419
        char pathType;
Mathias Agopian's avatar
Mathias Agopian committed
420
        if (sscanf(l, "%d %d %d", &width, &height, &fps) == 3) {
Kevin Hester's avatar
Kevin Hester committed
421
            //LOGD("> w=%d, h=%d, fps=%d", width, height, fps);
Mathias Agopian's avatar
Mathias Agopian committed
422 423 424 425
            animation.width = width;
            animation.height = height;
            animation.fps = fps;
        }
Kevin Hester's avatar
Kevin Hester committed
426 427
        else if (sscanf(l, " %c %d %d %s", &pathType, &count, &pause, path) == 4) {
            //LOGD("> type=%c, count=%d, pause=%d, path=%s", pathType, count, pause, path);
Mathias Agopian's avatar
Mathias Agopian committed
428
            Animation::Part part;
Kevin Hester's avatar
Kevin Hester committed
429
            part.playUntilComplete = pathType == 'c';
Mathias Agopian's avatar
Mathias Agopian committed
430 431 432 433 434
            part.count = count;
            part.pause = pause;
            part.path = path;
            animation.parts.add(part);
        }
Kevin Hester's avatar
Kevin Hester committed
435

Mathias Agopian's avatar
Mathias Agopian committed
436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475
        s = ++endl;
    }

    // read all the data structures
    const size_t pcount = animation.parts.size();
    for (size_t i=0 ; i<numEntries ; i++) {
        char name[256];
        ZipEntryRO entry = zip.findEntryByIndex(i);
        if (zip.getEntryFileName(entry, name, 256) == 0) {
            const String8 entryName(name);
            const String8 path(entryName.getPathDir());
            const String8 leaf(entryName.getPathLeaf());
            if (leaf.size() > 0) {
                for (int j=0 ; j<pcount ; j++) {
                    if (path == animation.parts[j].path) {
                        int method;
                        // supports only stored png files
                        if (zip.getEntryInfo(entry, &method, 0, 0, 0, 0, 0)) {
                            if (method == ZipFileRO::kCompressStored) {
                                FileMap* map = zip.createEntryFileMap(entry);
                                if (map) {
                                    Animation::Frame frame;
                                    frame.name = leaf;
                                    frame.map = map;
                                    Animation::Part& part(animation.parts.editItemAt(j));
                                    part.frames.add(frame);
                                }
                            }
                        }
                    }
                }
            }
        }
    }

    // clear screen
    glShadeModel(GL_FLAT);
    glDisable(GL_DITHER);
    glDisable(GL_SCISSOR_TEST);
    glDisable(GL_BLEND);
476
    glClearColor(0,0,0,1);
Mathias Agopian's avatar
Mathias Agopian committed
477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493
    glClear(GL_COLOR_BUFFER_BIT);

    eglSwapBuffers(mDisplay, mSurface);

    glBindTexture(GL_TEXTURE_2D, 0);
    glEnable(GL_TEXTURE_2D);
    glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
    glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
    glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
    glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

    const int xc = (mWidth - animation.width) / 2;
    const int yc = ((mHeight - animation.height) / 2);
    nsecs_t lastFrame = systemTime();
    nsecs_t frameDuration = s2ns(1) / animation.fps;

494 495 496
    Region clearReg(Rect(mWidth, mHeight));
    clearReg.subtractSelf(Rect(xc, yc, xc+animation.width, yc+animation.height));

Kevin Hester's avatar
Kevin Hester committed
497
    for (int i=0 ; i<pcount ; i++) {
Mathias Agopian's avatar
Mathias Agopian committed
498 499 500 501 502
        const Animation::Part& part(animation.parts[i]);
        const size_t fcount = part.frames.size();
        glBindTexture(GL_TEXTURE_2D, 0);

        for (int r=0 ; !part.count || r<part.count ; r++) {
Kevin Hester's avatar
Kevin Hester committed
503 504 505 506 507
            // Exit any non playuntil complete parts immediately
            if(exitPending() && !part.playUntilComplete)
                break;

            for (int j=0 ; j<fcount && (!exitPending() || part.playUntilComplete) ; j++) {
Mathias Agopian's avatar
Mathias Agopian committed
508
                const Animation::Frame& frame(part.frames[j]);
Mathias Agopian's avatar
Mathias Agopian committed
509
                nsecs_t lastFrame = systemTime();
Mathias Agopian's avatar
Mathias Agopian committed
510 511 512 513 514 515 516 517 518 519 520 521 522 523 524

                if (r > 0) {
                    glBindTexture(GL_TEXTURE_2D, frame.tid);
                } else {
                    if (part.count != 1) {
                        glGenTextures(1, &frame.tid);
                        glBindTexture(GL_TEXTURE_2D, frame.tid);
                        glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
                        glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
                    }
                    initTexture(
                            frame.map->getDataPtr(),
                            frame.map->getDataLength());
                }

525 526 527 528 529 530 531 532 533 534 535 536
                if (!clearReg.isEmpty()) {
                    Region::const_iterator head(clearReg.begin());
                    Region::const_iterator tail(clearReg.end());
                    glEnable(GL_SCISSOR_TEST);
                    while (head != tail) {
                        const Rect& r(*head++);
                        glScissor(r.left, mHeight - r.bottom,
                                r.width(), r.height());
                        glClear(GL_COLOR_BUFFER_BIT);
                    }
                    glDisable(GL_SCISSOR_TEST);
                }
Mathias Agopian's avatar
Mathias Agopian committed
537 538 539 540 541
                glDrawTexiOES(xc, yc, 0, animation.width, animation.height);
                eglSwapBuffers(mDisplay, mSurface);

                nsecs_t now = systemTime();
                nsecs_t delay = frameDuration - (now - lastFrame);
Mathias Agopian's avatar
Mathias Agopian committed
542
                //ALOGD("%lld, %lld", ns2ms(now - lastFrame), ns2ms(delay));
Mathias Agopian's avatar
Mathias Agopian committed
543
                lastFrame = now;
Mathias Agopian's avatar
Mathias Agopian committed
544 545 546 547 548 549 550 551 552 553

                if (delay > 0) {
                    struct timespec spec;
                    spec.tv_sec  = (now + delay) / 1000000000;
                    spec.tv_nsec = (now + delay) % 1000000000;
                    int err;
                    do {
                        err = clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &spec, NULL);
                    } while (err<0 && errno == EINTR);
                }
Kevin Hester's avatar
Kevin Hester committed
554 555

                checkExit();
Mathias Agopian's avatar
Mathias Agopian committed
556
            }
Kevin Hester's avatar
Kevin Hester committed
557

Mathias Agopian's avatar
Mathias Agopian committed
558
            usleep(part.pause * ns2us(frameDuration));
Kevin Hester's avatar
Kevin Hester committed
559 560 561 562

            // For infinite parts, we've now played them at least once, so perhaps exit
            if(exitPending() && !part.count)
                break;
Mathias Agopian's avatar
Mathias Agopian committed
563 564 565 566 567 568 569 570 571 572 573 574 575 576
        }

        // free the textures for this part
        if (part.count != 1) {
            for (int j=0 ; j<fcount ; j++) {
                const Animation::Frame& frame(part.frames[j]);
                glDeleteTextures(1, &frame.tid);
            }
        }
    }

    return false;
}

577 578 579 580
// ---------------------------------------------------------------------------

}
; // namespace android