blob: c8c142d7918141edf457035f550305ac9ab1f92f [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
Mathias Agopianac31a3b2009-05-21 19:59:24 -070026#include <binder/IPCThreadState.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080027#include <utils/threads.h>
28#include <utils/Atomic.h>
29#include <utils/Errors.h>
30#include <utils/Log.h>
31#include <utils/AssetManager.h>
32
33#include <ui/PixelFormat.h>
34#include <ui/Rect.h>
35#include <ui/Region.h>
36#include <ui/DisplayInfo.h>
37#include <ui/ISurfaceComposer.h>
38#include <ui/ISurfaceFlingerClient.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
42#include <core/SkBitmap.h>
43#include <images/SkImageDecoder.h>
44
45#include <GLES/gl.h>
46#include <GLES/glext.h>
47#include <EGL/eglext.h>
48
49#include "BootAnimation.h"
50
51namespace android {
52
53// ---------------------------------------------------------------------------
54
Mathias Agopian627e7b52009-05-21 19:21:59 -070055BootAnimation::BootAnimation() : Thread(false)
56{
57 mSession = new SurfaceComposerClient();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080058}
59
60BootAnimation::~BootAnimation() {
61}
62
63void BootAnimation::onFirstRef() {
Mathias Agopianbc726112009-09-23 15:44:05 -070064 status_t err = mSession->linkToComposerDeath(this);
65 LOGE_IF(err, "linkToComposerDeath failed (%s) ", strerror(-err));
66 if (err != NO_ERROR) {
67 run("BootAnimation", PRIORITY_DISPLAY);
68 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080069}
70
Mathias Agopianbc726112009-09-23 15:44:05 -070071sp<SurfaceComposerClient> BootAnimation::session() const {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080072 return mSession;
73}
74
Mathias Agopianbc726112009-09-23 15:44:05 -070075
76void BootAnimation::binderDied(const wp<IBinder>& who)
77{
78 // woah, surfaceflinger died!
79 LOGD("SurfaceFlinger died, exiting...");
80
81 // calling requestExit() is not enough here because the Surface code
82 // might be blocked on a condition variable that will never be updated.
83 kill( getpid(), SIGKILL );
84 requestExit();
85}
86
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080087status_t BootAnimation::initTexture(Texture* texture, AssetManager& assets,
88 const char* name) {
89 Asset* asset = assets.open(name, Asset::ACCESS_BUFFER);
90 if (!asset)
91 return NO_INIT;
92 SkBitmap bitmap;
93 SkImageDecoder::DecodeMemory(asset->getBuffer(false), asset->getLength(),
94 &bitmap, SkBitmap::kNo_Config, SkImageDecoder::kDecodePixels_Mode);
95 asset->close();
96 delete asset;
97
98 // ensure we can call getPixels(). No need to call unlock, since the
99 // bitmap will go out of scope when we return from this method.
100 bitmap.lockPixels();
101
102 const int w = bitmap.width();
103 const int h = bitmap.height();
104 const void* p = bitmap.getPixels();
105
106 GLint crop[4] = { 0, h, w, -h };
107 texture->w = w;
108 texture->h = h;
109
110 glGenTextures(1, &texture->name);
111 glBindTexture(GL_TEXTURE_2D, texture->name);
112
113 switch (bitmap.getConfig()) {
114 case SkBitmap::kA8_Config:
115 glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, w, h, 0, GL_ALPHA,
116 GL_UNSIGNED_BYTE, p);
117 break;
118 case SkBitmap::kARGB_4444_Config:
119 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA,
120 GL_UNSIGNED_SHORT_4_4_4_4, p);
121 break;
122 case SkBitmap::kARGB_8888_Config:
123 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA,
124 GL_UNSIGNED_BYTE, p);
125 break;
126 case SkBitmap::kRGB_565_Config:
127 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, w, h, 0, GL_RGB,
128 GL_UNSIGNED_SHORT_5_6_5, p);
129 break;
130 default:
131 break;
132 }
133
134 glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_CROP_RECT_OES, crop);
135 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
136 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
137 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
138 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
139 return NO_ERROR;
140}
141
142status_t BootAnimation::readyToRun() {
143 mAssets.addDefaultAssets();
144
145 DisplayInfo dinfo;
146 status_t status = session()->getDisplayInfo(0, &dinfo);
147 if (status)
148 return -1;
149
150 // create the native surface
Mathias Agopian17f638b2009-04-16 20:04:08 -0700151 sp<SurfaceControl> control = session()->createSurface(
Mathias Agopian317a6282009-08-13 17:29:02 -0700152 getpid(), 0, dinfo.w, dinfo.h, PIXEL_FORMAT_RGB_565);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800153 session()->openTransaction();
Mathias Agopian17f638b2009-04-16 20:04:08 -0700154 control->setLayer(0x40000000);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800155 session()->closeTransaction();
156
Mathias Agopian17f638b2009-04-16 20:04:08 -0700157 sp<Surface> s = control->getSurface();
158
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800159 // initialize opengl and egl
Mathias Agopian738b9a42009-08-06 16:41:02 -0700160 const EGLint attribs[] = {
161 EGL_DEPTH_SIZE, 0,
162 EGL_NONE
163 };
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800164 EGLint w, h, dummy;
165 EGLint numConfigs;
166 EGLConfig config;
167 EGLSurface surface;
168 EGLContext context;
Mathias Agopian627e7b52009-05-21 19:21:59 -0700169
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800170 EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
Mathias Agopian627e7b52009-05-21 19:21:59 -0700171
172 eglInitialize(display, 0, 0);
Mathias Agopian738b9a42009-08-06 16:41:02 -0700173 EGLUtils::selectConfigForNativeWindow(display, attribs, s.get(), &config);
Mathias Agopian1473f462009-04-10 14:24:30 -0700174 surface = eglCreateWindowSurface(display, config, s.get(), NULL);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800175 context = eglCreateContext(display, config, NULL, NULL);
176 eglQuerySurface(display, surface, EGL_WIDTH, &w);
177 eglQuerySurface(display, surface, EGL_HEIGHT, &h);
Mathias Agopianabac0102009-07-31 14:47:00 -0700178
179 if (eglMakeCurrent(display, surface, surface, context) == EGL_FALSE)
180 return NO_INIT;
181
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800182 mDisplay = display;
183 mContext = context;
184 mSurface = surface;
185 mWidth = w;
186 mHeight = h;
Mathias Agopian17f638b2009-04-16 20:04:08 -0700187 mFlingerSurfaceControl = control;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800188 mFlingerSurface = s;
189
190 // initialize GL
191 glShadeModel(GL_FLAT);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800192 glEnable(GL_TEXTURE_2D);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800193 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
194
195 return NO_ERROR;
196}
197
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800198bool BootAnimation::threadLoop() {
199 bool r = android();
200 eglMakeCurrent(mDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
201 eglDestroyContext(mDisplay, mContext);
202 eglDestroySurface(mDisplay, mSurface);
Mathias Agopian6cf0db22009-04-17 19:36:26 -0700203 mFlingerSurface.clear();
Mathias Agopian17f638b2009-04-16 20:04:08 -0700204 mFlingerSurfaceControl.clear();
Mathias Agopian627e7b52009-05-21 19:21:59 -0700205 eglTerminate(mDisplay);
206 IPCThreadState::self()->stopProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800207 return r;
208}
209
210bool BootAnimation::android() {
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700211 initTexture(&mAndroid[0], mAssets, "images/android-logo-mask.png");
212 initTexture(&mAndroid[1], mAssets, "images/android-logo-shine.png");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800213
214 // clear screen
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700215 glDisable(GL_DITHER);
216 glDisable(GL_SCISSOR_TEST);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800217 glClear(GL_COLOR_BUFFER_BIT);
218 eglSwapBuffers(mDisplay, mSurface);
219
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700220 const GLint xc = (mWidth - mAndroid[0].w) / 2;
221 const GLint yc = (mHeight - mAndroid[0].h) / 2;
222 const Rect updateRect(xc, yc, xc + mAndroid[0].w, yc + mAndroid[0].h);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800223
224 // draw and update only what we need
Mathias Agopian1473f462009-04-10 14:24:30 -0700225 mFlingerSurface->setSwapRectangle(updateRect);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800226
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800227 glScissor(updateRect.left, mHeight - updateRect.bottom, updateRect.width(),
228 updateRect.height());
229
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700230 // Blend state
231 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
232 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
233
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800234 const nsecs_t startTime = systemTime();
235 do {
Mathias Agopian13796652009-03-24 22:49:21 -0700236 nsecs_t now = systemTime();
237 double time = now - startTime;
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700238 float t = 4.0f * float(time / us2ns(16667)) / mAndroid[1].w;
239 GLint offset = (1 - (t - floorf(t))) * mAndroid[1].w;
240 GLint x = xc - offset;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800241
Mathias Agopian81668642009-07-28 11:41:30 -0700242 glDisable(GL_SCISSOR_TEST);
243 glClear(GL_COLOR_BUFFER_BIT);
244
245 glEnable(GL_SCISSOR_TEST);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800246 glDisable(GL_BLEND);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800247 glBindTexture(GL_TEXTURE_2D, mAndroid[1].name);
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700248 glDrawTexiOES(x, yc, 0, mAndroid[1].w, mAndroid[1].h);
249 glDrawTexiOES(x + mAndroid[1].w, yc, 0, mAndroid[1].w, mAndroid[1].h);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800250
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700251 glEnable(GL_BLEND);
252 glBindTexture(GL_TEXTURE_2D, mAndroid[0].name);
253 glDrawTexiOES(xc, yc, 0, mAndroid[0].w, mAndroid[0].h);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800254
Mathias Agopian627e7b52009-05-21 19:21:59 -0700255 EGLBoolean res = eglSwapBuffers(mDisplay, mSurface);
256 if (res == EGL_FALSE)
257 break;
258
Mathias Agopian13796652009-03-24 22:49:21 -0700259 // 12fps: don't animate too fast to preserve CPU
260 const nsecs_t sleepTime = 83333 - ns2us(systemTime() - now);
261 if (sleepTime > 0)
262 usleep(sleepTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800263 } while (!exitPending());
264
265 glDeleteTextures(1, &mAndroid[0].name);
266 glDeleteTextures(1, &mAndroid[1].name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800267 return false;
268}
269
270// ---------------------------------------------------------------------------
271
272}
273; // namespace android