blob: 80c8ee4dcff694708de9592047063c9b7a4aa6d4 [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>
24
Mathias Agopianac31a3b2009-05-21 19:59:24 -070025#include <binder/IPCThreadState.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080026#include <utils/threads.h>
27#include <utils/Atomic.h>
28#include <utils/Errors.h>
29#include <utils/Log.h>
30#include <utils/AssetManager.h>
31
32#include <ui/PixelFormat.h>
33#include <ui/Rect.h>
34#include <ui/Region.h>
35#include <ui/DisplayInfo.h>
36#include <ui/ISurfaceComposer.h>
37#include <ui/ISurfaceFlingerClient.h>
Mathias Agopiandff8e582009-05-04 14:17:04 -070038#include <ui/FramebufferNativeWindow.h>
Mathias Agopian738b9a42009-08-06 16:41:02 -070039#include <ui/EGLUtils.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080040
41#include <core/SkBitmap.h>
42#include <images/SkImageDecoder.h>
43
44#include <GLES/gl.h>
45#include <GLES/glext.h>
46#include <EGL/eglext.h>
47
48#include "BootAnimation.h"
49
50namespace android {
51
52// ---------------------------------------------------------------------------
53
Mathias Agopian627e7b52009-05-21 19:21:59 -070054BootAnimation::BootAnimation() : Thread(false)
55{
56 mSession = new SurfaceComposerClient();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080057}
58
59BootAnimation::~BootAnimation() {
60}
61
62void BootAnimation::onFirstRef() {
Mathias Agopianbc726112009-09-23 15:44:05 -070063 status_t err = mSession->linkToComposerDeath(this);
64 LOGE_IF(err, "linkToComposerDeath failed (%s) ", strerror(-err));
65 if (err != NO_ERROR) {
66 run("BootAnimation", PRIORITY_DISPLAY);
67 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080068}
69
Mathias Agopianbc726112009-09-23 15:44:05 -070070sp<SurfaceComposerClient> BootAnimation::session() const {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080071 return mSession;
72}
73
Mathias Agopianbc726112009-09-23 15:44:05 -070074
75void BootAnimation::binderDied(const wp<IBinder>& who)
76{
77 // woah, surfaceflinger died!
78 LOGD("SurfaceFlinger died, exiting...");
79
80 // calling requestExit() is not enough here because the Surface code
81 // might be blocked on a condition variable that will never be updated.
82 kill( getpid(), SIGKILL );
83 requestExit();
84}
85
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080086status_t BootAnimation::initTexture(Texture* texture, AssetManager& assets,
87 const char* name) {
88 Asset* asset = assets.open(name, Asset::ACCESS_BUFFER);
89 if (!asset)
90 return NO_INIT;
91 SkBitmap bitmap;
92 SkImageDecoder::DecodeMemory(asset->getBuffer(false), asset->getLength(),
93 &bitmap, SkBitmap::kNo_Config, SkImageDecoder::kDecodePixels_Mode);
94 asset->close();
95 delete asset;
96
97 // ensure we can call getPixels(). No need to call unlock, since the
98 // bitmap will go out of scope when we return from this method.
99 bitmap.lockPixels();
100
101 const int w = bitmap.width();
102 const int h = bitmap.height();
103 const void* p = bitmap.getPixels();
104
105 GLint crop[4] = { 0, h, w, -h };
106 texture->w = w;
107 texture->h = h;
108
109 glGenTextures(1, &texture->name);
110 glBindTexture(GL_TEXTURE_2D, texture->name);
111
112 switch (bitmap.getConfig()) {
113 case SkBitmap::kA8_Config:
114 glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, w, h, 0, GL_ALPHA,
115 GL_UNSIGNED_BYTE, p);
116 break;
117 case SkBitmap::kARGB_4444_Config:
118 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA,
119 GL_UNSIGNED_SHORT_4_4_4_4, p);
120 break;
121 case SkBitmap::kARGB_8888_Config:
122 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA,
123 GL_UNSIGNED_BYTE, p);
124 break;
125 case SkBitmap::kRGB_565_Config:
126 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, w, h, 0, GL_RGB,
127 GL_UNSIGNED_SHORT_5_6_5, p);
128 break;
129 default:
130 break;
131 }
132
133 glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_CROP_RECT_OES, crop);
134 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
135 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
136 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
137 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
138 return NO_ERROR;
139}
140
141status_t BootAnimation::readyToRun() {
142 mAssets.addDefaultAssets();
143
144 DisplayInfo dinfo;
145 status_t status = session()->getDisplayInfo(0, &dinfo);
146 if (status)
147 return -1;
148
149 // create the native surface
Mathias Agopian17f638b2009-04-16 20:04:08 -0700150 sp<SurfaceControl> control = session()->createSurface(
Mathias Agopian317a6282009-08-13 17:29:02 -0700151 getpid(), 0, dinfo.w, dinfo.h, PIXEL_FORMAT_RGB_565);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800152 session()->openTransaction();
Mathias Agopian17f638b2009-04-16 20:04:08 -0700153 control->setLayer(0x40000000);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800154 session()->closeTransaction();
155
Mathias Agopian17f638b2009-04-16 20:04:08 -0700156 sp<Surface> s = control->getSurface();
157
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800158 // initialize opengl and egl
Mathias Agopian738b9a42009-08-06 16:41:02 -0700159 const EGLint attribs[] = {
160 EGL_DEPTH_SIZE, 0,
161 EGL_NONE
162 };
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800163 EGLint w, h, dummy;
164 EGLint numConfigs;
165 EGLConfig config;
166 EGLSurface surface;
167 EGLContext context;
Mathias Agopian627e7b52009-05-21 19:21:59 -0700168
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800169 EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
Mathias Agopian627e7b52009-05-21 19:21:59 -0700170
171 eglInitialize(display, 0, 0);
Mathias Agopian738b9a42009-08-06 16:41:02 -0700172 EGLUtils::selectConfigForNativeWindow(display, attribs, s.get(), &config);
Mathias Agopian1473f462009-04-10 14:24:30 -0700173 surface = eglCreateWindowSurface(display, config, s.get(), NULL);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800174 context = eglCreateContext(display, config, NULL, NULL);
175 eglQuerySurface(display, surface, EGL_WIDTH, &w);
176 eglQuerySurface(display, surface, EGL_HEIGHT, &h);
Mathias Agopianabac0102009-07-31 14:47:00 -0700177
178 if (eglMakeCurrent(display, surface, surface, context) == EGL_FALSE)
179 return NO_INIT;
180
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800181 mDisplay = display;
182 mContext = context;
183 mSurface = surface;
184 mWidth = w;
185 mHeight = h;
Mathias Agopian17f638b2009-04-16 20:04:08 -0700186 mFlingerSurfaceControl = control;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800187 mFlingerSurface = s;
188
189 // initialize GL
190 glShadeModel(GL_FLAT);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800191 glEnable(GL_TEXTURE_2D);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800192 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
193
194 return NO_ERROR;
195}
196
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800197bool BootAnimation::threadLoop() {
198 bool r = android();
199 eglMakeCurrent(mDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
200 eglDestroyContext(mDisplay, mContext);
201 eglDestroySurface(mDisplay, mSurface);
Mathias Agopian6cf0db22009-04-17 19:36:26 -0700202 mFlingerSurface.clear();
Mathias Agopian17f638b2009-04-16 20:04:08 -0700203 mFlingerSurfaceControl.clear();
Mathias Agopian627e7b52009-05-21 19:21:59 -0700204 eglTerminate(mDisplay);
205 IPCThreadState::self()->stopProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800206 return r;
207}
208
209bool BootAnimation::android() {
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700210 initTexture(&mAndroid[0], mAssets, "images/android-logo-mask.png");
211 initTexture(&mAndroid[1], mAssets, "images/android-logo-shine.png");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800212
213 // clear screen
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700214 glDisable(GL_DITHER);
215 glDisable(GL_SCISSOR_TEST);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800216 glClear(GL_COLOR_BUFFER_BIT);
217 eglSwapBuffers(mDisplay, mSurface);
218
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700219 const GLint xc = (mWidth - mAndroid[0].w) / 2;
220 const GLint yc = (mHeight - mAndroid[0].h) / 2;
221 const Rect updateRect(xc, yc, xc + mAndroid[0].w, yc + mAndroid[0].h);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800222
223 // draw and update only what we need
Mathias Agopian1473f462009-04-10 14:24:30 -0700224 mFlingerSurface->setSwapRectangle(updateRect);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800225
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800226 glScissor(updateRect.left, mHeight - updateRect.bottom, updateRect.width(),
227 updateRect.height());
228
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700229 // Blend state
230 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
231 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
232
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800233 const nsecs_t startTime = systemTime();
234 do {
Mathias Agopian13796652009-03-24 22:49:21 -0700235 nsecs_t now = systemTime();
236 double time = now - startTime;
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700237 float t = 4.0f * float(time / us2ns(16667)) / mAndroid[1].w;
238 GLint offset = (1 - (t - floorf(t))) * mAndroid[1].w;
239 GLint x = xc - offset;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800240
Mathias Agopian81668642009-07-28 11:41:30 -0700241 glDisable(GL_SCISSOR_TEST);
242 glClear(GL_COLOR_BUFFER_BIT);
243
244 glEnable(GL_SCISSOR_TEST);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800245 glDisable(GL_BLEND);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800246 glBindTexture(GL_TEXTURE_2D, mAndroid[1].name);
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700247 glDrawTexiOES(x, yc, 0, mAndroid[1].w, mAndroid[1].h);
248 glDrawTexiOES(x + mAndroid[1].w, yc, 0, mAndroid[1].w, mAndroid[1].h);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800249
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700250 glEnable(GL_BLEND);
251 glBindTexture(GL_TEXTURE_2D, mAndroid[0].name);
252 glDrawTexiOES(xc, yc, 0, mAndroid[0].w, mAndroid[0].h);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800253
Mathias Agopian627e7b52009-05-21 19:21:59 -0700254 EGLBoolean res = eglSwapBuffers(mDisplay, mSurface);
255 if (res == EGL_FALSE)
256 break;
257
Mathias Agopian13796652009-03-24 22:49:21 -0700258 // 12fps: don't animate too fast to preserve CPU
259 const nsecs_t sleepTime = 83333 - ns2us(systemTime() - now);
260 if (sleepTime > 0)
261 usleep(sleepTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800262 } while (!exitPending());
263
264 glDeleteTextures(1, &mAndroid[0].name);
265 glDeleteTextures(1, &mAndroid[1].name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800266 return false;
267}
268
269// ---------------------------------------------------------------------------
270
271}
272; // namespace android