19066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project/*
29066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * Copyright (C) 2007 The Android Open Source Project
39066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project *
49066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * Licensed under the Apache License, Version 2.0 (the "License");
59066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * you may not use this file except in compliance with the License.
69066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * You may obtain a copy of the License at
79066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project *
89066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project *      http://www.apache.org/licenses/LICENSE-2.0
99066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project *
109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * Unless required by applicable law or agreed to in writing, software
119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * distributed under the License is distributed on an "AS IS" BASIS,
129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * See the License for the specific language governing permissions and
149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * limitations under the License.
159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project */
169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
17bc7261130a51dc9f3461d3970eee1b923bcbf193Mathias Agopian#define LOG_TAG "BootAnimation"
18bc7261130a51dc9f3461d3970eee1b923bcbf193Mathias Agopian
199066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#include <stdint.h>
209066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#include <sys/types.h>
219066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#include <math.h>
229066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#include <fcntl.h>
239066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#include <utils/misc.h>
24b4d5a72691846bce5779cc3db056f09a5031d7ccMathias Agopian#include <signal.h>
259066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
26bd9a08d318f94a3d0b2f8f9d7b16b10736d80c5cJason parks#include <cutils/properties.h>
27bd9a08d318f94a3d0b2f8f9d7b16b10736d80c5cJason parks
28b13b9bdad2baf6ad1ec2e56b6b7598fa20f55fc4Mathias Agopian#include <androidfw/AssetManager.h>
29ac31a3b8b09aba1c5ebc73f0cf65cac2210aa6b7Mathias Agopian#include <binder/IPCThreadState.h>
309066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#include <utils/Atomic.h>
319066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#include <utils/Errors.h>
329066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#include <utils/Log.h>
33b13b9bdad2baf6ad1ec2e56b6b7598fa20f55fc4Mathias Agopian#include <utils/threads.h>
349066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
359066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#include <ui/PixelFormat.h>
369066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#include <ui/Rect.h>
379066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#include <ui/Region.h>
389066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#include <ui/DisplayInfo.h>
39dff8e58d47ede6e748c0b02e128ca33b42a4f362Mathias Agopian#include <ui/FramebufferNativeWindow.h>
409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
410b722fe9ce98d97dbcb6fefd170b85ab7037e528Jeff Brown#include <gui/ISurfaceComposer.h>
428335f1ccccedb6655d96d9d5b697a7f0938235ddMathias Agopian#include <gui/Surface.h>
438335f1ccccedb6655d96d9d5b697a7f0938235ddMathias Agopian#include <gui/SurfaceComposerClient.h>
44000479f9e325b4e426a67033abd92d47da412725Mathias Agopian
459066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#include <core/SkBitmap.h>
462b99e55ee3a0956a43a276f8018356ff9d08eafcMathias Agopian#include <core/SkStream.h>
479066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#include <images/SkImageDecoder.h>
489066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
499066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#include <GLES/gl.h>
509066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#include <GLES/glext.h>
519066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#include <EGL/eglext.h>
529066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
539066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project#include "BootAnimation.h"
549066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
55c11f46259a1e8f4e7e58925aefd1ed9eaf57a7fcJim Huang#define USER_BOOTANIMATION_FILE "/data/local/bootanimation.zip"
56c11f46259a1e8f4e7e58925aefd1ed9eaf57a7fcJim Huang#define SYSTEM_BOOTANIMATION_FILE "/system/media/bootanimation.zip"
57bd9a08d318f94a3d0b2f8f9d7b16b10736d80c5cJason parks#define SYSTEM_ENCRYPTED_BOOTANIMATION_FILE "/system/media/bootanimation-encrypted.zip"
58d3782b26b2026e60a8e0d4b967a156369f2a46f8Kevin Hester#define EXIT_PROP_NAME "service.bootanim.exit"
59c11f46259a1e8f4e7e58925aefd1ed9eaf57a7fcJim Huang
60db7dd2af93ae96e5749cfbe978c2695049d9d782Mathias Agopianextern "C" int clock_nanosleep(clockid_t clock_id, int flags,
61db7dd2af93ae96e5749cfbe978c2695049d9d782Mathias Agopian                           const struct timespec *request,
62db7dd2af93ae96e5749cfbe978c2695049d9d782Mathias Agopian                           struct timespec *remain);
63db7dd2af93ae96e5749cfbe978c2695049d9d782Mathias Agopian
649066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectnamespace android {
659066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
669066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project// ---------------------------------------------------------------------------
679066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
68627e7b50be41e4fdee758a1bfad3a55de56b4e27Mathias AgopianBootAnimation::BootAnimation() : Thread(false)
69a8826d67097b12cf9d6b14a7e32c0862f17b189aMathias Agopian{
70627e7b50be41e4fdee758a1bfad3a55de56b4e27Mathias Agopian    mSession = new SurfaceComposerClient();
719066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
729066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
739066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source ProjectBootAnimation::~BootAnimation() {
749066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
759066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
769066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectvoid BootAnimation::onFirstRef() {
77bc7261130a51dc9f3461d3970eee1b923bcbf193Mathias Agopian    status_t err = mSession->linkToComposerDeath(this);
783762c311729fe9f3af085c14c5c1fb471d994c03Steve Block    ALOGE_IF(err, "linkToComposerDeath failed (%s) ", strerror(-err));
798434c5369304e639efe8eab368ca410c589d87c2Mathias Agopian    if (err == NO_ERROR) {
80bc7261130a51dc9f3461d3970eee1b923bcbf193Mathias Agopian        run("BootAnimation", PRIORITY_DISPLAY);
81bc7261130a51dc9f3461d3970eee1b923bcbf193Mathias Agopian    }
829066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
839066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
84bc7261130a51dc9f3461d3970eee1b923bcbf193Mathias Agopiansp<SurfaceComposerClient> BootAnimation::session() const {
859066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    return mSession;
869066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
879066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
88bc7261130a51dc9f3461d3970eee1b923bcbf193Mathias Agopian
89bc7261130a51dc9f3461d3970eee1b923bcbf193Mathias Agopianvoid BootAnimation::binderDied(const wp<IBinder>& who)
90bc7261130a51dc9f3461d3970eee1b923bcbf193Mathias Agopian{
91bc7261130a51dc9f3461d3970eee1b923bcbf193Mathias Agopian    // woah, surfaceflinger died!
925baa3a62a97544669fba6d65a11c07f252e654ddSteve Block    ALOGD("SurfaceFlinger died, exiting...");
93bc7261130a51dc9f3461d3970eee1b923bcbf193Mathias Agopian
94bc7261130a51dc9f3461d3970eee1b923bcbf193Mathias Agopian    // calling requestExit() is not enough here because the Surface code
95bc7261130a51dc9f3461d3970eee1b923bcbf193Mathias Agopian    // might be blocked on a condition variable that will never be updated.
96bc7261130a51dc9f3461d3970eee1b923bcbf193Mathias Agopian    kill( getpid(), SIGKILL );
97bc7261130a51dc9f3461d3970eee1b923bcbf193Mathias Agopian    requestExit();
98bc7261130a51dc9f3461d3970eee1b923bcbf193Mathias Agopian}
99bc7261130a51dc9f3461d3970eee1b923bcbf193Mathias Agopian
1009066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatus_t BootAnimation::initTexture(Texture* texture, AssetManager& assets,
1019066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        const char* name) {
1029066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    Asset* asset = assets.open(name, Asset::ACCESS_BUFFER);
1039066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (!asset)
1049066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return NO_INIT;
1059066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    SkBitmap bitmap;
1069066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    SkImageDecoder::DecodeMemory(asset->getBuffer(false), asset->getLength(),
1079066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            &bitmap, SkBitmap::kNo_Config, SkImageDecoder::kDecodePixels_Mode);
1089066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    asset->close();
1099066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    delete asset;
1109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    // ensure we can call getPixels(). No need to call unlock, since the
1129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    // bitmap will go out of scope when we return from this method.
1139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    bitmap.lockPixels();
1149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    const int w = bitmap.width();
1169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    const int h = bitmap.height();
1179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    const void* p = bitmap.getPixels();
1189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1199066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    GLint crop[4] = { 0, h, w, -h };
1209066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    texture->w = w;
1219066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    texture->h = h;
1229066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1239066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    glGenTextures(1, &texture->name);
1249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    glBindTexture(GL_TEXTURE_2D, texture->name);
1259066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1269066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    switch (bitmap.getConfig()) {
1279066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        case SkBitmap::kA8_Config:
1289066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, w, h, 0, GL_ALPHA,
1299066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    GL_UNSIGNED_BYTE, p);
1309066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            break;
1319066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        case SkBitmap::kARGB_4444_Config:
1329066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA,
1339066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    GL_UNSIGNED_SHORT_4_4_4_4, p);
1349066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            break;
1359066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        case SkBitmap::kARGB_8888_Config:
1369066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA,
1379066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    GL_UNSIGNED_BYTE, p);
1389066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            break;
1399066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        case SkBitmap::kRGB_565_Config:
1409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, w, h, 0, GL_RGB,
1419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project                    GL_UNSIGNED_SHORT_5_6_5, p);
1429066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            break;
1439066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        default:
1449066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            break;
1459066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    }
1469066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
1479066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_CROP_RECT_OES, crop);
1489066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
1499066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
1509066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
1519066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
1529066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    return NO_ERROR;
1539066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
1549066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
155a8826d67097b12cf9d6b14a7e32c0862f17b189aMathias Agopianstatus_t BootAnimation::initTexture(void* buffer, size_t len)
156a8826d67097b12cf9d6b14a7e32c0862f17b189aMathias Agopian{
157a8826d67097b12cf9d6b14a7e32c0862f17b189aMathias Agopian    //StopWatch watch("blah");
158a8826d67097b12cf9d6b14a7e32c0862f17b189aMathias Agopian
159a8826d67097b12cf9d6b14a7e32c0862f17b189aMathias Agopian    SkBitmap bitmap;
1602b99e55ee3a0956a43a276f8018356ff9d08eafcMathias Agopian    SkMemoryStream  stream(buffer, len);
1612b99e55ee3a0956a43a276f8018356ff9d08eafcMathias Agopian    SkImageDecoder* codec = SkImageDecoder::Factory(&stream);
1622b99e55ee3a0956a43a276f8018356ff9d08eafcMathias Agopian    codec->setDitherImage(false);
1632b99e55ee3a0956a43a276f8018356ff9d08eafcMathias Agopian    if (codec) {
1642b99e55ee3a0956a43a276f8018356ff9d08eafcMathias Agopian        codec->decode(&stream, &bitmap,
16560691ce1600cb79ec9bb9957eee29be7a6a5e5d2Mathias Agopian                SkBitmap::kARGB_8888_Config,
1662b99e55ee3a0956a43a276f8018356ff9d08eafcMathias Agopian                SkImageDecoder::kDecodePixels_Mode);
1672b99e55ee3a0956a43a276f8018356ff9d08eafcMathias Agopian        delete codec;
1682b99e55ee3a0956a43a276f8018356ff9d08eafcMathias Agopian    }
169a8826d67097b12cf9d6b14a7e32c0862f17b189aMathias Agopian
170a8826d67097b12cf9d6b14a7e32c0862f17b189aMathias Agopian    // ensure we can call getPixels(). No need to call unlock, since the
171a8826d67097b12cf9d6b14a7e32c0862f17b189aMathias Agopian    // bitmap will go out of scope when we return from this method.
172a8826d67097b12cf9d6b14a7e32c0862f17b189aMathias Agopian    bitmap.lockPixels();
173a8826d67097b12cf9d6b14a7e32c0862f17b189aMathias Agopian
174a8826d67097b12cf9d6b14a7e32c0862f17b189aMathias Agopian    const int w = bitmap.width();
175a8826d67097b12cf9d6b14a7e32c0862f17b189aMathias Agopian    const int h = bitmap.height();
176a8826d67097b12cf9d6b14a7e32c0862f17b189aMathias Agopian    const void* p = bitmap.getPixels();
177a8826d67097b12cf9d6b14a7e32c0862f17b189aMathias Agopian
178a8826d67097b12cf9d6b14a7e32c0862f17b189aMathias Agopian    GLint crop[4] = { 0, h, w, -h };
179a8826d67097b12cf9d6b14a7e32c0862f17b189aMathias Agopian    int tw = 1 << (31 - __builtin_clz(w));
180a8826d67097b12cf9d6b14a7e32c0862f17b189aMathias Agopian    int th = 1 << (31 - __builtin_clz(h));
181a8826d67097b12cf9d6b14a7e32c0862f17b189aMathias Agopian    if (tw < w) tw <<= 1;
182a8826d67097b12cf9d6b14a7e32c0862f17b189aMathias Agopian    if (th < h) th <<= 1;
183a8826d67097b12cf9d6b14a7e32c0862f17b189aMathias Agopian
184a8826d67097b12cf9d6b14a7e32c0862f17b189aMathias Agopian    switch (bitmap.getConfig()) {
185a8826d67097b12cf9d6b14a7e32c0862f17b189aMathias Agopian        case SkBitmap::kARGB_8888_Config:
186a8826d67097b12cf9d6b14a7e32c0862f17b189aMathias Agopian            if (tw != w || th != h) {
187a8826d67097b12cf9d6b14a7e32c0862f17b189aMathias Agopian                glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, tw, th, 0, GL_RGBA,
188a8826d67097b12cf9d6b14a7e32c0862f17b189aMathias Agopian                        GL_UNSIGNED_BYTE, 0);
189a8826d67097b12cf9d6b14a7e32c0862f17b189aMathias Agopian                glTexSubImage2D(GL_TEXTURE_2D, 0,
190a8826d67097b12cf9d6b14a7e32c0862f17b189aMathias Agopian                        0, 0, w, h, GL_RGBA, GL_UNSIGNED_BYTE, p);
191a8826d67097b12cf9d6b14a7e32c0862f17b189aMathias Agopian            } else {
192a8826d67097b12cf9d6b14a7e32c0862f17b189aMathias Agopian                glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, tw, th, 0, GL_RGBA,
193a8826d67097b12cf9d6b14a7e32c0862f17b189aMathias Agopian                        GL_UNSIGNED_BYTE, p);
194a8826d67097b12cf9d6b14a7e32c0862f17b189aMathias Agopian            }
195a8826d67097b12cf9d6b14a7e32c0862f17b189aMathias Agopian            break;
196a8826d67097b12cf9d6b14a7e32c0862f17b189aMathias Agopian
197a8826d67097b12cf9d6b14a7e32c0862f17b189aMathias Agopian        case SkBitmap::kRGB_565_Config:
198a8826d67097b12cf9d6b14a7e32c0862f17b189aMathias Agopian            if (tw != w || th != h) {
199a8826d67097b12cf9d6b14a7e32c0862f17b189aMathias Agopian                glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, tw, th, 0, GL_RGB,
200a8826d67097b12cf9d6b14a7e32c0862f17b189aMathias Agopian                        GL_UNSIGNED_SHORT_5_6_5, 0);
201a8826d67097b12cf9d6b14a7e32c0862f17b189aMathias Agopian                glTexSubImage2D(GL_TEXTURE_2D, 0,
202a8826d67097b12cf9d6b14a7e32c0862f17b189aMathias Agopian                        0, 0, w, h, GL_RGB, GL_UNSIGNED_SHORT_5_6_5, p);
203a8826d67097b12cf9d6b14a7e32c0862f17b189aMathias Agopian            } else {
204a8826d67097b12cf9d6b14a7e32c0862f17b189aMathias Agopian                glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, tw, th, 0, GL_RGB,
205a8826d67097b12cf9d6b14a7e32c0862f17b189aMathias Agopian                        GL_UNSIGNED_SHORT_5_6_5, p);
206a8826d67097b12cf9d6b14a7e32c0862f17b189aMathias Agopian            }
207a8826d67097b12cf9d6b14a7e32c0862f17b189aMathias Agopian            break;
208a8826d67097b12cf9d6b14a7e32c0862f17b189aMathias Agopian        default:
209a8826d67097b12cf9d6b14a7e32c0862f17b189aMathias Agopian            break;
210a8826d67097b12cf9d6b14a7e32c0862f17b189aMathias Agopian    }
211a8826d67097b12cf9d6b14a7e32c0862f17b189aMathias Agopian
212a8826d67097b12cf9d6b14a7e32c0862f17b189aMathias Agopian    glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_CROP_RECT_OES, crop);
213a8826d67097b12cf9d6b14a7e32c0862f17b189aMathias Agopian
214a8826d67097b12cf9d6b14a7e32c0862f17b189aMathias Agopian    return NO_ERROR;
215a8826d67097b12cf9d6b14a7e32c0862f17b189aMathias Agopian}
216a8826d67097b12cf9d6b14a7e32c0862f17b189aMathias Agopian
2179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectstatus_t BootAnimation::readyToRun() {
2189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    mAssets.addDefaultAssets();
2199066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
2200b722fe9ce98d97dbcb6fefd170b85ab7037e528Jeff Brown    sp<IBinder> dtoken(SurfaceComposerClient::getBuiltInDisplay(
2210b722fe9ce98d97dbcb6fefd170b85ab7037e528Jeff Brown            ISurfaceComposer::eDisplayIdMain));
2229066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    DisplayInfo dinfo;
2230b722fe9ce98d97dbcb6fefd170b85ab7037e528Jeff Brown    status_t status = SurfaceComposerClient::getDisplayInfo(dtoken, &dinfo);
2249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    if (status)
2259066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        return -1;
2269066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
2279066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    // create the native surface
2280b722fe9ce98d97dbcb6fefd170b85ab7037e528Jeff Brown    sp<SurfaceControl> control = session()->createSurface(String8("BootAnimation"),
2290b722fe9ce98d97dbcb6fefd170b85ab7037e528Jeff Brown            dinfo.w, dinfo.h, PIXEL_FORMAT_RGB_565);
230439863f3b3e725b5de1cba4940a21900369961c0Mathias Agopian
231439863f3b3e725b5de1cba4940a21900369961c0Mathias Agopian    SurfaceComposerClient::openGlobalTransaction();
23217f638b39f2e8b610ecfa1290e5bc42ab7700c98Mathias Agopian    control->setLayer(0x40000000);
233439863f3b3e725b5de1cba4940a21900369961c0Mathias Agopian    SurfaceComposerClient::closeGlobalTransaction();
2349066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
23517f638b39f2e8b610ecfa1290e5bc42ab7700c98Mathias Agopian    sp<Surface> s = control->getSurface();
23617f638b39f2e8b610ecfa1290e5bc42ab7700c98Mathias Agopian
2379066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    // initialize opengl and egl
238738b9a45399485893fa9541fae19c47e01bc4af4Mathias Agopian    const EGLint attribs[] = {
2391b253b702a356619ae3c5e68ee92062b5305c5d7Mathias Agopian            EGL_RED_SIZE,   8,
2401b253b702a356619ae3c5e68ee92062b5305c5d7Mathias Agopian            EGL_GREEN_SIZE, 8,
2411b253b702a356619ae3c5e68ee92062b5305c5d7Mathias Agopian            EGL_BLUE_SIZE,  8,
242a8826d67097b12cf9d6b14a7e32c0862f17b189aMathias Agopian            EGL_DEPTH_SIZE, 0,
243a8826d67097b12cf9d6b14a7e32c0862f17b189aMathias Agopian            EGL_NONE
244738b9a45399485893fa9541fae19c47e01bc4af4Mathias Agopian    };
2459066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    EGLint w, h, dummy;
2469066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    EGLint numConfigs;
2479066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    EGLConfig config;
2489066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    EGLSurface surface;
2499066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    EGLContext context;
250627e7b50be41e4fdee758a1bfad3a55de56b4e27Mathias Agopian
2519066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
252627e7b50be41e4fdee758a1bfad3a55de56b4e27Mathias Agopian
253627e7b50be41e4fdee758a1bfad3a55de56b4e27Mathias Agopian    eglInitialize(display, 0, 0);
2541b253b702a356619ae3c5e68ee92062b5305c5d7Mathias Agopian    eglChooseConfig(display, attribs, &config, 1, &numConfigs);
2551473f46cbc82aa6f0ba744cc896a36923823d55bMathias Agopian    surface = eglCreateWindowSurface(display, config, s.get(), NULL);
2569066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    context = eglCreateContext(display, config, NULL, NULL);
2579066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    eglQuerySurface(display, surface, EGL_WIDTH, &w);
2589066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    eglQuerySurface(display, surface, EGL_HEIGHT, &h);
259a8826d67097b12cf9d6b14a7e32c0862f17b189aMathias Agopian
260abac01052c1c40f8582fe9f010c2efe6013e25c6Mathias Agopian    if (eglMakeCurrent(display, surface, surface, context) == EGL_FALSE)
261abac01052c1c40f8582fe9f010c2efe6013e25c6Mathias Agopian        return NO_INIT;
262a8826d67097b12cf9d6b14a7e32c0862f17b189aMathias Agopian
2639066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    mDisplay = display;
2649066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    mContext = context;
2659066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    mSurface = surface;
2669066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    mWidth = w;
2679066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    mHeight = h;
26817f638b39f2e8b610ecfa1290e5bc42ab7700c98Mathias Agopian    mFlingerSurfaceControl = control;
2699066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    mFlingerSurface = s;
2709066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
271c11f46259a1e8f4e7e58925aefd1ed9eaf57a7fcJim Huang    mAndroidAnimation = true;
272bd9a08d318f94a3d0b2f8f9d7b16b10736d80c5cJason parks
273bd9a08d318f94a3d0b2f8f9d7b16b10736d80c5cJason parks    // If the device has encryption turned on or is in process
274bd9a08d318f94a3d0b2f8f9d7b16b10736d80c5cJason parks    // of being encrypted we show the encrypted boot animation.
275bd9a08d318f94a3d0b2f8f9d7b16b10736d80c5cJason parks    char decrypt[PROPERTY_VALUE_MAX];
276bd9a08d318f94a3d0b2f8f9d7b16b10736d80c5cJason parks    property_get("vold.decrypt", decrypt, "");
277bd9a08d318f94a3d0b2f8f9d7b16b10736d80c5cJason parks
278bd9a08d318f94a3d0b2f8f9d7b16b10736d80c5cJason parks    bool encryptedAnimation = atoi(decrypt) != 0 || !strcmp("trigger_restart_min_framework", decrypt);
279bd9a08d318f94a3d0b2f8f9d7b16b10736d80c5cJason parks
280bd9a08d318f94a3d0b2f8f9d7b16b10736d80c5cJason parks    if ((encryptedAnimation &&
281bd9a08d318f94a3d0b2f8f9d7b16b10736d80c5cJason parks            (access(SYSTEM_ENCRYPTED_BOOTANIMATION_FILE, R_OK) == 0) &&
282bd9a08d318f94a3d0b2f8f9d7b16b10736d80c5cJason parks            (mZip.open(SYSTEM_ENCRYPTED_BOOTANIMATION_FILE) == NO_ERROR)) ||
283bd9a08d318f94a3d0b2f8f9d7b16b10736d80c5cJason parks
284bd9a08d318f94a3d0b2f8f9d7b16b10736d80c5cJason parks            ((access(USER_BOOTANIMATION_FILE, R_OK) == 0) &&
285bd9a08d318f94a3d0b2f8f9d7b16b10736d80c5cJason parks            (mZip.open(USER_BOOTANIMATION_FILE) == NO_ERROR)) ||
286bd9a08d318f94a3d0b2f8f9d7b16b10736d80c5cJason parks
287bd9a08d318f94a3d0b2f8f9d7b16b10736d80c5cJason parks            ((access(SYSTEM_BOOTANIMATION_FILE, R_OK) == 0) &&
288bd9a08d318f94a3d0b2f8f9d7b16b10736d80c5cJason parks            (mZip.open(SYSTEM_BOOTANIMATION_FILE) == NO_ERROR))) {
2892978751310b4efef1faa87b116fcaee9423c007fChih-Wei Huang        mAndroidAnimation = false;
290bd9a08d318f94a3d0b2f8f9d7b16b10736d80c5cJason parks    }
2919066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
2929066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    return NO_ERROR;
2939066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
2949066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
295a8826d67097b12cf9d6b14a7e32c0862f17b189aMathias Agopianbool BootAnimation::threadLoop()
296a8826d67097b12cf9d6b14a7e32c0862f17b189aMathias Agopian{
297a8826d67097b12cf9d6b14a7e32c0862f17b189aMathias Agopian    bool r;
298a8826d67097b12cf9d6b14a7e32c0862f17b189aMathias Agopian    if (mAndroidAnimation) {
299a8826d67097b12cf9d6b14a7e32c0862f17b189aMathias Agopian        r = android();
300a8826d67097b12cf9d6b14a7e32c0862f17b189aMathias Agopian    } else {
301a8826d67097b12cf9d6b14a7e32c0862f17b189aMathias Agopian        r = movie();
302a8826d67097b12cf9d6b14a7e32c0862f17b189aMathias Agopian    }
303a8826d67097b12cf9d6b14a7e32c0862f17b189aMathias Agopian
304d3782b26b2026e60a8e0d4b967a156369f2a46f8Kevin Hester    // No need to force exit anymore
305d3782b26b2026e60a8e0d4b967a156369f2a46f8Kevin Hester    property_set(EXIT_PROP_NAME, "0");
306d3782b26b2026e60a8e0d4b967a156369f2a46f8Kevin Hester
3079066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    eglMakeCurrent(mDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
3089066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    eglDestroyContext(mDisplay, mContext);
3099066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    eglDestroySurface(mDisplay, mSurface);
3106cf0db228ca275dfcda57d79c55e5fa306809632Mathias Agopian    mFlingerSurface.clear();
31117f638b39f2e8b610ecfa1290e5bc42ab7700c98Mathias Agopian    mFlingerSurfaceControl.clear();
312627e7b50be41e4fdee758a1bfad3a55de56b4e27Mathias Agopian    eglTerminate(mDisplay);
313627e7b50be41e4fdee758a1bfad3a55de56b4e27Mathias Agopian    IPCThreadState::self()->stopProcess();
3149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    return r;
3159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
3169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
317a8826d67097b12cf9d6b14a7e32c0862f17b189aMathias Agopianbool BootAnimation::android()
318a8826d67097b12cf9d6b14a7e32c0862f17b189aMathias Agopian{
319b2cf954cb47a73065aa4b7fed753877615614f41Mathias Agopian    initTexture(&mAndroid[0], mAssets, "images/android-logo-mask.png");
320b2cf954cb47a73065aa4b7fed753877615614f41Mathias Agopian    initTexture(&mAndroid[1], mAssets, "images/android-logo-shine.png");
3219066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
3229066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    // clear screen
323a8826d67097b12cf9d6b14a7e32c0862f17b189aMathias Agopian    glShadeModel(GL_FLAT);
324b2cf954cb47a73065aa4b7fed753877615614f41Mathias Agopian    glDisable(GL_DITHER);
325b2cf954cb47a73065aa4b7fed753877615614f41Mathias Agopian    glDisable(GL_SCISSOR_TEST);
32659f19e48c1c043bb9debdc35d166e397e2125d33Mathias Agopian    glClearColor(0,0,0,1);
3279066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    glClear(GL_COLOR_BUFFER_BIT);
3289066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    eglSwapBuffers(mDisplay, mSurface);
3299066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
330a8826d67097b12cf9d6b14a7e32c0862f17b189aMathias Agopian    glEnable(GL_TEXTURE_2D);
331a8826d67097b12cf9d6b14a7e32c0862f17b189aMathias Agopian    glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
332a8826d67097b12cf9d6b14a7e32c0862f17b189aMathias Agopian
333b2cf954cb47a73065aa4b7fed753877615614f41Mathias Agopian    const GLint xc = (mWidth  - mAndroid[0].w) / 2;
334b2cf954cb47a73065aa4b7fed753877615614f41Mathias Agopian    const GLint yc = (mHeight - mAndroid[0].h) / 2;
335b2cf954cb47a73065aa4b7fed753877615614f41Mathias Agopian    const Rect updateRect(xc, yc, xc + mAndroid[0].w, yc + mAndroid[0].h);
3369066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
3379066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    glScissor(updateRect.left, mHeight - updateRect.bottom, updateRect.width(),
3389066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project            updateRect.height());
3399066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
340b2cf954cb47a73065aa4b7fed753877615614f41Mathias Agopian    // Blend state
341b2cf954cb47a73065aa4b7fed753877615614f41Mathias Agopian    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
342b2cf954cb47a73065aa4b7fed753877615614f41Mathias Agopian    glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
343b2cf954cb47a73065aa4b7fed753877615614f41Mathias Agopian
3449066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    const nsecs_t startTime = systemTime();
3459066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    do {
3461379665abdb8330caf813fb77902914d1545bcd9Mathias Agopian        nsecs_t now = systemTime();
3471379665abdb8330caf813fb77902914d1545bcd9Mathias Agopian        double time = now - startTime;
348b2cf954cb47a73065aa4b7fed753877615614f41Mathias Agopian        float t = 4.0f * float(time / us2ns(16667)) / mAndroid[1].w;
349b2cf954cb47a73065aa4b7fed753877615614f41Mathias Agopian        GLint offset = (1 - (t - floorf(t))) * mAndroid[1].w;
350b2cf954cb47a73065aa4b7fed753877615614f41Mathias Agopian        GLint x = xc - offset;
3519066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
3528166864e42bc20c1544b2028e8dd65b90f29a0eaMathias Agopian        glDisable(GL_SCISSOR_TEST);
3538166864e42bc20c1544b2028e8dd65b90f29a0eaMathias Agopian        glClear(GL_COLOR_BUFFER_BIT);
3548166864e42bc20c1544b2028e8dd65b90f29a0eaMathias Agopian
3558166864e42bc20c1544b2028e8dd65b90f29a0eaMathias Agopian        glEnable(GL_SCISSOR_TEST);
3569066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        glDisable(GL_BLEND);
3579066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project        glBindTexture(GL_TEXTURE_2D, mAndroid[1].name);
358b2cf954cb47a73065aa4b7fed753877615614f41Mathias Agopian        glDrawTexiOES(x,                 yc, 0, mAndroid[1].w, mAndroid[1].h);
359b2cf954cb47a73065aa4b7fed753877615614f41Mathias Agopian        glDrawTexiOES(x + mAndroid[1].w, yc, 0, mAndroid[1].w, mAndroid[1].h);
3609066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
361b2cf954cb47a73065aa4b7fed753877615614f41Mathias Agopian        glEnable(GL_BLEND);
362b2cf954cb47a73065aa4b7fed753877615614f41Mathias Agopian        glBindTexture(GL_TEXTURE_2D, mAndroid[0].name);
363b2cf954cb47a73065aa4b7fed753877615614f41Mathias Agopian        glDrawTexiOES(xc, yc, 0, mAndroid[0].w, mAndroid[0].h);
3649066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
365627e7b50be41e4fdee758a1bfad3a55de56b4e27Mathias Agopian        EGLBoolean res = eglSwapBuffers(mDisplay, mSurface);
366627e7b50be41e4fdee758a1bfad3a55de56b4e27Mathias Agopian        if (res == EGL_FALSE)
367627e7b50be41e4fdee758a1bfad3a55de56b4e27Mathias Agopian            break;
368627e7b50be41e4fdee758a1bfad3a55de56b4e27Mathias Agopian
3691379665abdb8330caf813fb77902914d1545bcd9Mathias Agopian        // 12fps: don't animate too fast to preserve CPU
3701379665abdb8330caf813fb77902914d1545bcd9Mathias Agopian        const nsecs_t sleepTime = 83333 - ns2us(systemTime() - now);
3711379665abdb8330caf813fb77902914d1545bcd9Mathias Agopian        if (sleepTime > 0)
372a8826d67097b12cf9d6b14a7e32c0862f17b189aMathias Agopian            usleep(sleepTime);
373d3782b26b2026e60a8e0d4b967a156369f2a46f8Kevin Hester
374d3782b26b2026e60a8e0d4b967a156369f2a46f8Kevin Hester        checkExit();
3759066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    } while (!exitPending());
3769066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
3779066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    glDeleteTextures(1, &mAndroid[0].name);
3789066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    glDeleteTextures(1, &mAndroid[1].name);
3799066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project    return false;
3809066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
3819066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
382a8826d67097b12cf9d6b14a7e32c0862f17b189aMathias Agopian
383d3782b26b2026e60a8e0d4b967a156369f2a46f8Kevin Hestervoid BootAnimation::checkExit() {
384d3782b26b2026e60a8e0d4b967a156369f2a46f8Kevin Hester    // Allow surface flinger to gracefully request shutdown
385d3782b26b2026e60a8e0d4b967a156369f2a46f8Kevin Hester    char value[PROPERTY_VALUE_MAX];
386d3782b26b2026e60a8e0d4b967a156369f2a46f8Kevin Hester    property_get(EXIT_PROP_NAME, value, "0");
387d3782b26b2026e60a8e0d4b967a156369f2a46f8Kevin Hester    int exitnow = atoi(value);
388d3782b26b2026e60a8e0d4b967a156369f2a46f8Kevin Hester    if (exitnow) {
389d3782b26b2026e60a8e0d4b967a156369f2a46f8Kevin Hester        requestExit();
390d3782b26b2026e60a8e0d4b967a156369f2a46f8Kevin Hester    }
391d3782b26b2026e60a8e0d4b967a156369f2a46f8Kevin Hester}
392d3782b26b2026e60a8e0d4b967a156369f2a46f8Kevin Hester
393a8826d67097b12cf9d6b14a7e32c0862f17b189aMathias Agopianbool BootAnimation::movie()
394a8826d67097b12cf9d6b14a7e32c0862f17b189aMathias Agopian{
395a8826d67097b12cf9d6b14a7e32c0862f17b189aMathias Agopian    ZipFileRO& zip(mZip);
396a8826d67097b12cf9d6b14a7e32c0862f17b189aMathias Agopian
397a8826d67097b12cf9d6b14a7e32c0862f17b189aMathias Agopian    size_t numEntries = zip.getNumEntries();
398a8826d67097b12cf9d6b14a7e32c0862f17b189aMathias Agopian    ZipEntryRO desc = zip.findEntryByName("desc.txt");
399a8826d67097b12cf9d6b14a7e32c0862f17b189aMathias Agopian    FileMap* descMap = zip.createEntryFileMap(desc);
4003762c311729fe9f3af085c14c5c1fb471d994c03Steve Block    ALOGE_IF(!descMap, "descMap is null");
401a8826d67097b12cf9d6b14a7e32c0862f17b189aMathias Agopian    if (!descMap) {
402a8826d67097b12cf9d6b14a7e32c0862f17b189aMathias Agopian        return false;
403a8826d67097b12cf9d6b14a7e32c0862f17b189aMathias Agopian    }
404a8826d67097b12cf9d6b14a7e32c0862f17b189aMathias Agopian
405a8826d67097b12cf9d6b14a7e32c0862f17b189aMathias Agopian    String8 desString((char const*)descMap->getDataPtr(),
406a8826d67097b12cf9d6b14a7e32c0862f17b189aMathias Agopian            descMap->getDataLength());
407a8826d67097b12cf9d6b14a7e32c0862f17b189aMathias Agopian    char const* s = desString.string();
408a8826d67097b12cf9d6b14a7e32c0862f17b189aMathias Agopian
409a8826d67097b12cf9d6b14a7e32c0862f17b189aMathias Agopian    Animation animation;
410a8826d67097b12cf9d6b14a7e32c0862f17b189aMathias Agopian
411a8826d67097b12cf9d6b14a7e32c0862f17b189aMathias Agopian    // Parse the description file
412a8826d67097b12cf9d6b14a7e32c0862f17b189aMathias Agopian    for (;;) {
413a8826d67097b12cf9d6b14a7e32c0862f17b189aMathias Agopian        const char* endl = strstr(s, "\n");
414a8826d67097b12cf9d6b14a7e32c0862f17b189aMathias Agopian        if (!endl) break;
415a8826d67097b12cf9d6b14a7e32c0862f17b189aMathias Agopian        String8 line(s, endl - s);
416a8826d67097b12cf9d6b14a7e32c0862f17b189aMathias Agopian        const char* l = line.string();
417a8826d67097b12cf9d6b14a7e32c0862f17b189aMathias Agopian        int fps, width, height, count, pause;
418a8826d67097b12cf9d6b14a7e32c0862f17b189aMathias Agopian        char path[256];
419d3782b26b2026e60a8e0d4b967a156369f2a46f8Kevin Hester        char pathType;
420a8826d67097b12cf9d6b14a7e32c0862f17b189aMathias Agopian        if (sscanf(l, "%d %d %d", &width, &height, &fps) == 3) {
421d3782b26b2026e60a8e0d4b967a156369f2a46f8Kevin Hester            //LOGD("> w=%d, h=%d, fps=%d", width, height, fps);
422a8826d67097b12cf9d6b14a7e32c0862f17b189aMathias Agopian            animation.width = width;
423a8826d67097b12cf9d6b14a7e32c0862f17b189aMathias Agopian            animation.height = height;
424a8826d67097b12cf9d6b14a7e32c0862f17b189aMathias Agopian            animation.fps = fps;
425a8826d67097b12cf9d6b14a7e32c0862f17b189aMathias Agopian        }
426d3782b26b2026e60a8e0d4b967a156369f2a46f8Kevin Hester        else if (sscanf(l, " %c %d %d %s", &pathType, &count, &pause, path) == 4) {
427d3782b26b2026e60a8e0d4b967a156369f2a46f8Kevin Hester            //LOGD("> type=%c, count=%d, pause=%d, path=%s", pathType, count, pause, path);
428a8826d67097b12cf9d6b14a7e32c0862f17b189aMathias Agopian            Animation::Part part;
429d3782b26b2026e60a8e0d4b967a156369f2a46f8Kevin Hester            part.playUntilComplete = pathType == 'c';
430a8826d67097b12cf9d6b14a7e32c0862f17b189aMathias Agopian            part.count = count;
431a8826d67097b12cf9d6b14a7e32c0862f17b189aMathias Agopian            part.pause = pause;
432a8826d67097b12cf9d6b14a7e32c0862f17b189aMathias Agopian            part.path = path;
433a8826d67097b12cf9d6b14a7e32c0862f17b189aMathias Agopian            animation.parts.add(part);
434a8826d67097b12cf9d6b14a7e32c0862f17b189aMathias Agopian        }
435d3782b26b2026e60a8e0d4b967a156369f2a46f8Kevin Hester
436a8826d67097b12cf9d6b14a7e32c0862f17b189aMathias Agopian        s = ++endl;
437a8826d67097b12cf9d6b14a7e32c0862f17b189aMathias Agopian    }
438a8826d67097b12cf9d6b14a7e32c0862f17b189aMathias Agopian
439a8826d67097b12cf9d6b14a7e32c0862f17b189aMathias Agopian    // read all the data structures
440a8826d67097b12cf9d6b14a7e32c0862f17b189aMathias Agopian    const size_t pcount = animation.parts.size();
441a8826d67097b12cf9d6b14a7e32c0862f17b189aMathias Agopian    for (size_t i=0 ; i<numEntries ; i++) {
442a8826d67097b12cf9d6b14a7e32c0862f17b189aMathias Agopian        char name[256];
443a8826d67097b12cf9d6b14a7e32c0862f17b189aMathias Agopian        ZipEntryRO entry = zip.findEntryByIndex(i);
444a8826d67097b12cf9d6b14a7e32c0862f17b189aMathias Agopian        if (zip.getEntryFileName(entry, name, 256) == 0) {
445a8826d67097b12cf9d6b14a7e32c0862f17b189aMathias Agopian            const String8 entryName(name);
446a8826d67097b12cf9d6b14a7e32c0862f17b189aMathias Agopian            const String8 path(entryName.getPathDir());
447a8826d67097b12cf9d6b14a7e32c0862f17b189aMathias Agopian            const String8 leaf(entryName.getPathLeaf());
448a8826d67097b12cf9d6b14a7e32c0862f17b189aMathias Agopian            if (leaf.size() > 0) {
449a8826d67097b12cf9d6b14a7e32c0862f17b189aMathias Agopian                for (int j=0 ; j<pcount ; j++) {
450a8826d67097b12cf9d6b14a7e32c0862f17b189aMathias Agopian                    if (path == animation.parts[j].path) {
451a8826d67097b12cf9d6b14a7e32c0862f17b189aMathias Agopian                        int method;
452a8826d67097b12cf9d6b14a7e32c0862f17b189aMathias Agopian                        // supports only stored png files
453a8826d67097b12cf9d6b14a7e32c0862f17b189aMathias Agopian                        if (zip.getEntryInfo(entry, &method, 0, 0, 0, 0, 0)) {
454a8826d67097b12cf9d6b14a7e32c0862f17b189aMathias Agopian                            if (method == ZipFileRO::kCompressStored) {
455a8826d67097b12cf9d6b14a7e32c0862f17b189aMathias Agopian                                FileMap* map = zip.createEntryFileMap(entry);
456a8826d67097b12cf9d6b14a7e32c0862f17b189aMathias Agopian                                if (map) {
457a8826d67097b12cf9d6b14a7e32c0862f17b189aMathias Agopian                                    Animation::Frame frame;
458a8826d67097b12cf9d6b14a7e32c0862f17b189aMathias Agopian                                    frame.name = leaf;
459a8826d67097b12cf9d6b14a7e32c0862f17b189aMathias Agopian                                    frame.map = map;
460a8826d67097b12cf9d6b14a7e32c0862f17b189aMathias Agopian                                    Animation::Part& part(animation.parts.editItemAt(j));
461a8826d67097b12cf9d6b14a7e32c0862f17b189aMathias Agopian                                    part.frames.add(frame);
462a8826d67097b12cf9d6b14a7e32c0862f17b189aMathias Agopian                                }
463a8826d67097b12cf9d6b14a7e32c0862f17b189aMathias Agopian                            }
464a8826d67097b12cf9d6b14a7e32c0862f17b189aMathias Agopian                        }
465a8826d67097b12cf9d6b14a7e32c0862f17b189aMathias Agopian                    }
466a8826d67097b12cf9d6b14a7e32c0862f17b189aMathias Agopian                }
467a8826d67097b12cf9d6b14a7e32c0862f17b189aMathias Agopian            }
468a8826d67097b12cf9d6b14a7e32c0862f17b189aMathias Agopian        }
469a8826d67097b12cf9d6b14a7e32c0862f17b189aMathias Agopian    }
470a8826d67097b12cf9d6b14a7e32c0862f17b189aMathias Agopian
471a8826d67097b12cf9d6b14a7e32c0862f17b189aMathias Agopian    // clear screen
472a8826d67097b12cf9d6b14a7e32c0862f17b189aMathias Agopian    glShadeModel(GL_FLAT);
473a8826d67097b12cf9d6b14a7e32c0862f17b189aMathias Agopian    glDisable(GL_DITHER);
474a8826d67097b12cf9d6b14a7e32c0862f17b189aMathias Agopian    glDisable(GL_SCISSOR_TEST);
475a8826d67097b12cf9d6b14a7e32c0862f17b189aMathias Agopian    glDisable(GL_BLEND);
47659f19e48c1c043bb9debdc35d166e397e2125d33Mathias Agopian    glClearColor(0,0,0,1);
477a8826d67097b12cf9d6b14a7e32c0862f17b189aMathias Agopian    glClear(GL_COLOR_BUFFER_BIT);
478a8826d67097b12cf9d6b14a7e32c0862f17b189aMathias Agopian
479a8826d67097b12cf9d6b14a7e32c0862f17b189aMathias Agopian    eglSwapBuffers(mDisplay, mSurface);
480a8826d67097b12cf9d6b14a7e32c0862f17b189aMathias Agopian
481a8826d67097b12cf9d6b14a7e32c0862f17b189aMathias Agopian    glBindTexture(GL_TEXTURE_2D, 0);
482a8826d67097b12cf9d6b14a7e32c0862f17b189aMathias Agopian    glEnable(GL_TEXTURE_2D);
483a8826d67097b12cf9d6b14a7e32c0862f17b189aMathias Agopian    glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
484a8826d67097b12cf9d6b14a7e32c0862f17b189aMathias Agopian    glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
485a8826d67097b12cf9d6b14a7e32c0862f17b189aMathias Agopian    glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
486a8826d67097b12cf9d6b14a7e32c0862f17b189aMathias Agopian    glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
487a8826d67097b12cf9d6b14a7e32c0862f17b189aMathias Agopian    glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
488a8826d67097b12cf9d6b14a7e32c0862f17b189aMathias Agopian
489a8826d67097b12cf9d6b14a7e32c0862f17b189aMathias Agopian    const int xc = (mWidth - animation.width) / 2;
490a8826d67097b12cf9d6b14a7e32c0862f17b189aMathias Agopian    const int yc = ((mHeight - animation.height) / 2);
491a8826d67097b12cf9d6b14a7e32c0862f17b189aMathias Agopian    nsecs_t lastFrame = systemTime();
492a8826d67097b12cf9d6b14a7e32c0862f17b189aMathias Agopian    nsecs_t frameDuration = s2ns(1) / animation.fps;
493a8826d67097b12cf9d6b14a7e32c0862f17b189aMathias Agopian
4949f3020db15e50d07ee1ace42e47d90b076f2ce54Mathias Agopian    Region clearReg(Rect(mWidth, mHeight));
4959f3020db15e50d07ee1ace42e47d90b076f2ce54Mathias Agopian    clearReg.subtractSelf(Rect(xc, yc, xc+animation.width, yc+animation.height));
4969f3020db15e50d07ee1ace42e47d90b076f2ce54Mathias Agopian
497d3782b26b2026e60a8e0d4b967a156369f2a46f8Kevin Hester    for (int i=0 ; i<pcount ; i++) {
498a8826d67097b12cf9d6b14a7e32c0862f17b189aMathias Agopian        const Animation::Part& part(animation.parts[i]);
499a8826d67097b12cf9d6b14a7e32c0862f17b189aMathias Agopian        const size_t fcount = part.frames.size();
500a8826d67097b12cf9d6b14a7e32c0862f17b189aMathias Agopian        glBindTexture(GL_TEXTURE_2D, 0);
501a8826d67097b12cf9d6b14a7e32c0862f17b189aMathias Agopian
502a8826d67097b12cf9d6b14a7e32c0862f17b189aMathias Agopian        for (int r=0 ; !part.count || r<part.count ; r++) {
503d3782b26b2026e60a8e0d4b967a156369f2a46f8Kevin Hester            // Exit any non playuntil complete parts immediately
504d3782b26b2026e60a8e0d4b967a156369f2a46f8Kevin Hester            if(exitPending() && !part.playUntilComplete)
505d3782b26b2026e60a8e0d4b967a156369f2a46f8Kevin Hester                break;
506d3782b26b2026e60a8e0d4b967a156369f2a46f8Kevin Hester
507d3782b26b2026e60a8e0d4b967a156369f2a46f8Kevin Hester            for (int j=0 ; j<fcount && (!exitPending() || part.playUntilComplete) ; j++) {
508a8826d67097b12cf9d6b14a7e32c0862f17b189aMathias Agopian                const Animation::Frame& frame(part.frames[j]);
509db7dd2af93ae96e5749cfbe978c2695049d9d782Mathias Agopian                nsecs_t lastFrame = systemTime();
510a8826d67097b12cf9d6b14a7e32c0862f17b189aMathias Agopian
511a8826d67097b12cf9d6b14a7e32c0862f17b189aMathias Agopian                if (r > 0) {
512a8826d67097b12cf9d6b14a7e32c0862f17b189aMathias Agopian                    glBindTexture(GL_TEXTURE_2D, frame.tid);
513a8826d67097b12cf9d6b14a7e32c0862f17b189aMathias Agopian                } else {
514a8826d67097b12cf9d6b14a7e32c0862f17b189aMathias Agopian                    if (part.count != 1) {
515a8826d67097b12cf9d6b14a7e32c0862f17b189aMathias Agopian                        glGenTextures(1, &frame.tid);
516a8826d67097b12cf9d6b14a7e32c0862f17b189aMathias Agopian                        glBindTexture(GL_TEXTURE_2D, frame.tid);
517a8826d67097b12cf9d6b14a7e32c0862f17b189aMathias Agopian                        glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
518a8826d67097b12cf9d6b14a7e32c0862f17b189aMathias Agopian                        glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
519a8826d67097b12cf9d6b14a7e32c0862f17b189aMathias Agopian                    }
520a8826d67097b12cf9d6b14a7e32c0862f17b189aMathias Agopian                    initTexture(
521a8826d67097b12cf9d6b14a7e32c0862f17b189aMathias Agopian                            frame.map->getDataPtr(),
522a8826d67097b12cf9d6b14a7e32c0862f17b189aMathias Agopian                            frame.map->getDataLength());
523a8826d67097b12cf9d6b14a7e32c0862f17b189aMathias Agopian                }
524a8826d67097b12cf9d6b14a7e32c0862f17b189aMathias Agopian
5259f3020db15e50d07ee1ace42e47d90b076f2ce54Mathias Agopian                if (!clearReg.isEmpty()) {
5269f3020db15e50d07ee1ace42e47d90b076f2ce54Mathias Agopian                    Region::const_iterator head(clearReg.begin());
5279f3020db15e50d07ee1ace42e47d90b076f2ce54Mathias Agopian                    Region::const_iterator tail(clearReg.end());
5289f3020db15e50d07ee1ace42e47d90b076f2ce54Mathias Agopian                    glEnable(GL_SCISSOR_TEST);
5299f3020db15e50d07ee1ace42e47d90b076f2ce54Mathias Agopian                    while (head != tail) {
5309f3020db15e50d07ee1ace42e47d90b076f2ce54Mathias Agopian                        const Rect& r(*head++);
5319f3020db15e50d07ee1ace42e47d90b076f2ce54Mathias Agopian                        glScissor(r.left, mHeight - r.bottom,
5329f3020db15e50d07ee1ace42e47d90b076f2ce54Mathias Agopian                                r.width(), r.height());
5339f3020db15e50d07ee1ace42e47d90b076f2ce54Mathias Agopian                        glClear(GL_COLOR_BUFFER_BIT);
5349f3020db15e50d07ee1ace42e47d90b076f2ce54Mathias Agopian                    }
5359f3020db15e50d07ee1ace42e47d90b076f2ce54Mathias Agopian                    glDisable(GL_SCISSOR_TEST);
5369f3020db15e50d07ee1ace42e47d90b076f2ce54Mathias Agopian                }
537a8826d67097b12cf9d6b14a7e32c0862f17b189aMathias Agopian                glDrawTexiOES(xc, yc, 0, animation.width, animation.height);
538a8826d67097b12cf9d6b14a7e32c0862f17b189aMathias Agopian                eglSwapBuffers(mDisplay, mSurface);
539a8826d67097b12cf9d6b14a7e32c0862f17b189aMathias Agopian
540a8826d67097b12cf9d6b14a7e32c0862f17b189aMathias Agopian                nsecs_t now = systemTime();
541a8826d67097b12cf9d6b14a7e32c0862f17b189aMathias Agopian                nsecs_t delay = frameDuration - (now - lastFrame);
542db7dd2af93ae96e5749cfbe978c2695049d9d782Mathias Agopian                //ALOGD("%lld, %lld", ns2ms(now - lastFrame), ns2ms(delay));
543a8826d67097b12cf9d6b14a7e32c0862f17b189aMathias Agopian                lastFrame = now;
544db7dd2af93ae96e5749cfbe978c2695049d9d782Mathias Agopian
545db7dd2af93ae96e5749cfbe978c2695049d9d782Mathias Agopian                if (delay > 0) {
546db7dd2af93ae96e5749cfbe978c2695049d9d782Mathias Agopian                    struct timespec spec;
547db7dd2af93ae96e5749cfbe978c2695049d9d782Mathias Agopian                    spec.tv_sec  = (now + delay) / 1000000000;
548db7dd2af93ae96e5749cfbe978c2695049d9d782Mathias Agopian                    spec.tv_nsec = (now + delay) % 1000000000;
549db7dd2af93ae96e5749cfbe978c2695049d9d782Mathias Agopian                    int err;
550db7dd2af93ae96e5749cfbe978c2695049d9d782Mathias Agopian                    do {
551db7dd2af93ae96e5749cfbe978c2695049d9d782Mathias Agopian                        err = clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &spec, NULL);
552db7dd2af93ae96e5749cfbe978c2695049d9d782Mathias Agopian                    } while (err<0 && errno == EINTR);
553db7dd2af93ae96e5749cfbe978c2695049d9d782Mathias Agopian                }
554d3782b26b2026e60a8e0d4b967a156369f2a46f8Kevin Hester
555d3782b26b2026e60a8e0d4b967a156369f2a46f8Kevin Hester                checkExit();
556a8826d67097b12cf9d6b14a7e32c0862f17b189aMathias Agopian            }
557d3782b26b2026e60a8e0d4b967a156369f2a46f8Kevin Hester
558a8826d67097b12cf9d6b14a7e32c0862f17b189aMathias Agopian            usleep(part.pause * ns2us(frameDuration));
559d3782b26b2026e60a8e0d4b967a156369f2a46f8Kevin Hester
560d3782b26b2026e60a8e0d4b967a156369f2a46f8Kevin Hester            // For infinite parts, we've now played them at least once, so perhaps exit
561d3782b26b2026e60a8e0d4b967a156369f2a46f8Kevin Hester            if(exitPending() && !part.count)
562d3782b26b2026e60a8e0d4b967a156369f2a46f8Kevin Hester                break;
563a8826d67097b12cf9d6b14a7e32c0862f17b189aMathias Agopian        }
564a8826d67097b12cf9d6b14a7e32c0862f17b189aMathias Agopian
565a8826d67097b12cf9d6b14a7e32c0862f17b189aMathias Agopian        // free the textures for this part
566a8826d67097b12cf9d6b14a7e32c0862f17b189aMathias Agopian        if (part.count != 1) {
567a8826d67097b12cf9d6b14a7e32c0862f17b189aMathias Agopian            for (int j=0 ; j<fcount ; j++) {
568a8826d67097b12cf9d6b14a7e32c0862f17b189aMathias Agopian                const Animation::Frame& frame(part.frames[j]);
569a8826d67097b12cf9d6b14a7e32c0862f17b189aMathias Agopian                glDeleteTextures(1, &frame.tid);
570a8826d67097b12cf9d6b14a7e32c0862f17b189aMathias Agopian            }
571a8826d67097b12cf9d6b14a7e32c0862f17b189aMathias Agopian        }
572a8826d67097b12cf9d6b14a7e32c0862f17b189aMathias Agopian    }
573a8826d67097b12cf9d6b14a7e32c0862f17b189aMathias Agopian
574a8826d67097b12cf9d6b14a7e32c0862f17b189aMathias Agopian    return false;
575a8826d67097b12cf9d6b14a7e32c0862f17b189aMathias Agopian}
576a8826d67097b12cf9d6b14a7e32c0862f17b189aMathias Agopian
5779066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project// ---------------------------------------------------------------------------
5789066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project
5799066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project}
5809066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project; // namespace android
581