blob: 03edbf380f9467dfbb99cc4a61069379905bc077 [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
17#define LOG_TAG "BootAnimation"
18
19#include <stdint.h>
20#include <sys/types.h>
21#include <math.h>
22#include <fcntl.h>
23#include <utils/misc.h>
24
25#include <utils/threads.h>
26#include <utils/Atomic.h>
27#include <utils/Errors.h>
28#include <utils/Log.h>
29#include <utils/AssetManager.h>
30
31#include <ui/PixelFormat.h>
32#include <ui/Rect.h>
33#include <ui/Region.h>
34#include <ui/DisplayInfo.h>
35#include <ui/ISurfaceComposer.h>
36#include <ui/ISurfaceFlingerClient.h>
37#include <ui/EGLNativeWindowSurface.h>
38
39#include <core/SkBitmap.h>
40#include <images/SkImageDecoder.h>
41
42#include <GLES/gl.h>
43#include <GLES/glext.h>
44#include <EGL/eglext.h>
45
46#include "BootAnimation.h"
47
48namespace android {
49
50// ---------------------------------------------------------------------------
51
52BootAnimation::BootAnimation(const sp<ISurfaceComposer>& composer) :
53 Thread(false) {
54 mSession = SurfaceComposerClient::clientForConnection(
55 composer->createConnection()->asBinder());
56}
57
58BootAnimation::~BootAnimation() {
59}
60
61void BootAnimation::onFirstRef() {
62 run("BootAnimation", PRIORITY_DISPLAY);
63}
64
65const sp<SurfaceComposerClient>& BootAnimation::session() const {
66 return mSession;
67}
68
69status_t BootAnimation::initTexture(Texture* texture, AssetManager& assets,
70 const char* name) {
71 Asset* asset = assets.open(name, Asset::ACCESS_BUFFER);
72 if (!asset)
73 return NO_INIT;
74 SkBitmap bitmap;
75 SkImageDecoder::DecodeMemory(asset->getBuffer(false), asset->getLength(),
76 &bitmap, SkBitmap::kNo_Config, SkImageDecoder::kDecodePixels_Mode);
77 asset->close();
78 delete asset;
79
80 // ensure we can call getPixels(). No need to call unlock, since the
81 // bitmap will go out of scope when we return from this method.
82 bitmap.lockPixels();
83
84 const int w = bitmap.width();
85 const int h = bitmap.height();
86 const void* p = bitmap.getPixels();
87
88 GLint crop[4] = { 0, h, w, -h };
89 texture->w = w;
90 texture->h = h;
91
92 glGenTextures(1, &texture->name);
93 glBindTexture(GL_TEXTURE_2D, texture->name);
94
95 switch (bitmap.getConfig()) {
96 case SkBitmap::kA8_Config:
97 glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, w, h, 0, GL_ALPHA,
98 GL_UNSIGNED_BYTE, p);
99 break;
100 case SkBitmap::kARGB_4444_Config:
101 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA,
102 GL_UNSIGNED_SHORT_4_4_4_4, p);
103 break;
104 case SkBitmap::kARGB_8888_Config:
105 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA,
106 GL_UNSIGNED_BYTE, p);
107 break;
108 case SkBitmap::kRGB_565_Config:
109 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, w, h, 0, GL_RGB,
110 GL_UNSIGNED_SHORT_5_6_5, p);
111 break;
112 default:
113 break;
114 }
115
116 glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_CROP_RECT_OES, crop);
117 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
118 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
119 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
120 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
121 return NO_ERROR;
122}
123
124status_t BootAnimation::readyToRun() {
125 mAssets.addDefaultAssets();
126
127 DisplayInfo dinfo;
128 status_t status = session()->getDisplayInfo(0, &dinfo);
129 if (status)
130 return -1;
131
132 // create the native surface
133 sp<Surface> s = session()->createSurface(getpid(), 0, dinfo.w, dinfo.h,
134 PIXEL_FORMAT_RGB_565);
135 session()->openTransaction();
136 s->setLayer(0x40000000);
137 session()->closeTransaction();
138
139 // initialize opengl and egl
140 const EGLint attribs[] = { EGL_RED_SIZE, 5, EGL_GREEN_SIZE, 6,
141 EGL_BLUE_SIZE, 5, EGL_DEPTH_SIZE, 0, EGL_NONE };
142 EGLint w, h, dummy;
143 EGLint numConfigs;
144 EGLConfig config;
145 EGLSurface surface;
146 EGLContext context;
147 EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
148 eglChooseConfig(display, attribs, &config, 1, &numConfigs);
149
150 mNativeWindowSurface = new EGLNativeWindowSurface(s);
151 surface = eglCreateWindowSurface(display, config,
152 mNativeWindowSurface.get(), NULL);
153
154 context = eglCreateContext(display, config, NULL, NULL);
155 eglQuerySurface(display, surface, EGL_WIDTH, &w);
156 eglQuerySurface(display, surface, EGL_HEIGHT, &h);
157 eglMakeCurrent(display, surface, surface, context);
158 mDisplay = display;
159 mContext = context;
160 mSurface = surface;
161 mWidth = w;
162 mHeight = h;
163 mFlingerSurface = s;
164
165 // initialize GL
166 glShadeModel(GL_FLAT);
167 glEnable(GL_DITHER);
168 glEnable(GL_TEXTURE_2D);
169 glEnable(GL_SCISSOR_TEST);
170 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
171
172 return NO_ERROR;
173}
174
175void BootAnimation::requestExit() {
176 mBarrier.open();
177 Thread::requestExit();
178}
179
180bool BootAnimation::threadLoop() {
181 bool r = android();
182 eglMakeCurrent(mDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
183 eglDestroyContext(mDisplay, mContext);
184 eglDestroySurface(mDisplay, mSurface);
185 mNativeWindowSurface.clear();
186 return r;
187}
188
189bool BootAnimation::android() {
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700190 initTexture(&mAndroid[0], mAssets, "images/android-logo-mask.png");
191 initTexture(&mAndroid[1], mAssets, "images/android-logo-shine.png");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800192
193 // clear screen
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700194 glDisable(GL_DITHER);
195 glDisable(GL_SCISSOR_TEST);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800196 glClear(GL_COLOR_BUFFER_BIT);
197 eglSwapBuffers(mDisplay, mSurface);
198
199 // wait ~1s
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800200
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700201 const GLint xc = (mWidth - mAndroid[0].w) / 2;
202 const GLint yc = (mHeight - mAndroid[0].h) / 2;
203 const Rect updateRect(xc, yc, xc + mAndroid[0].w, yc + mAndroid[0].h);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800204
205 // draw and update only what we need
206 mNativeWindowSurface->setSwapRectangle(updateRect.left,
207 updateRect.top, updateRect.width(), updateRect.height());
208
209 glEnable(GL_SCISSOR_TEST);
210 glScissor(updateRect.left, mHeight - updateRect.bottom, updateRect.width(),
211 updateRect.height());
212
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700213 // Blend state
214 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
215 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
216
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800217 const nsecs_t startTime = systemTime();
218 do {
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700219 double time = systemTime() - startTime;
220 float t = 4.0f * float(time / us2ns(16667)) / mAndroid[1].w;
221 GLint offset = (1 - (t - floorf(t))) * mAndroid[1].w;
222 GLint x = xc - offset;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800223
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800224 glDisable(GL_BLEND);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800225 glBindTexture(GL_TEXTURE_2D, mAndroid[1].name);
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700226 glDrawTexiOES(x, yc, 0, mAndroid[1].w, mAndroid[1].h);
227 glDrawTexiOES(x + mAndroid[1].w, yc, 0, mAndroid[1].w, mAndroid[1].h);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800228
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700229 glEnable(GL_BLEND);
230 glBindTexture(GL_TEXTURE_2D, mAndroid[0].name);
231 glDrawTexiOES(xc, yc, 0, mAndroid[0].w, mAndroid[0].h);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800232
233 eglSwapBuffers(mDisplay, mSurface);
234 } while (!exitPending());
235
236 glDeleteTextures(1, &mAndroid[0].name);
237 glDeleteTextures(1, &mAndroid[1].name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800238 return false;
239}
240
241// ---------------------------------------------------------------------------
242
243}
244; // namespace android