blob: 5b904cb27f8d6beabeeef4f81880dc2d1b5f167c [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
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080017#include <stdint.h>
18#include <sys/types.h>
19#include <math.h>
20#include <fcntl.h>
21#include <utils/misc.h>
22
Mathias Agopianac31a3b2009-05-21 19:59:24 -070023#include <binder/IPCThreadState.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080024#include <utils/threads.h>
25#include <utils/Atomic.h>
26#include <utils/Errors.h>
27#include <utils/Log.h>
28#include <utils/AssetManager.h>
29
30#include <ui/PixelFormat.h>
31#include <ui/Rect.h>
32#include <ui/Region.h>
33#include <ui/DisplayInfo.h>
34#include <ui/ISurfaceComposer.h>
35#include <ui/ISurfaceFlingerClient.h>
Mathias Agopiandff8e582009-05-04 14:17:04 -070036#include <ui/FramebufferNativeWindow.h>
Mathias Agopian738b9a42009-08-06 16:41:02 -070037#include <ui/EGLUtils.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080038
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
Mathias Agopian627e7b52009-05-21 19:21:59 -070052BootAnimation::BootAnimation() : Thread(false)
53{
54 mSession = new SurfaceComposerClient();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080055}
56
57BootAnimation::~BootAnimation() {
58}
59
60void BootAnimation::onFirstRef() {
61 run("BootAnimation", PRIORITY_DISPLAY);
62}
63
64const sp<SurfaceComposerClient>& BootAnimation::session() const {
65 return mSession;
66}
67
68status_t BootAnimation::initTexture(Texture* texture, AssetManager& assets,
69 const char* name) {
70 Asset* asset = assets.open(name, Asset::ACCESS_BUFFER);
71 if (!asset)
72 return NO_INIT;
73 SkBitmap bitmap;
74 SkImageDecoder::DecodeMemory(asset->getBuffer(false), asset->getLength(),
75 &bitmap, SkBitmap::kNo_Config, SkImageDecoder::kDecodePixels_Mode);
76 asset->close();
77 delete asset;
78
79 // ensure we can call getPixels(). No need to call unlock, since the
80 // bitmap will go out of scope when we return from this method.
81 bitmap.lockPixels();
82
83 const int w = bitmap.width();
84 const int h = bitmap.height();
85 const void* p = bitmap.getPixels();
86
87 GLint crop[4] = { 0, h, w, -h };
88 texture->w = w;
89 texture->h = h;
90
91 glGenTextures(1, &texture->name);
92 glBindTexture(GL_TEXTURE_2D, texture->name);
93
94 switch (bitmap.getConfig()) {
95 case SkBitmap::kA8_Config:
96 glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, w, h, 0, GL_ALPHA,
97 GL_UNSIGNED_BYTE, p);
98 break;
99 case SkBitmap::kARGB_4444_Config:
100 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA,
101 GL_UNSIGNED_SHORT_4_4_4_4, p);
102 break;
103 case SkBitmap::kARGB_8888_Config:
104 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA,
105 GL_UNSIGNED_BYTE, p);
106 break;
107 case SkBitmap::kRGB_565_Config:
108 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, w, h, 0, GL_RGB,
109 GL_UNSIGNED_SHORT_5_6_5, p);
110 break;
111 default:
112 break;
113 }
114
115 glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_CROP_RECT_OES, crop);
116 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
117 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
118 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
119 glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
120 return NO_ERROR;
121}
122
123status_t BootAnimation::readyToRun() {
124 mAssets.addDefaultAssets();
125
126 DisplayInfo dinfo;
127 status_t status = session()->getDisplayInfo(0, &dinfo);
128 if (status)
129 return -1;
130
131 // create the native surface
Mathias Agopian17f638b2009-04-16 20:04:08 -0700132 sp<SurfaceControl> control = session()->createSurface(
Mathias Agopian947f4f42009-05-22 01:27:01 -0700133 getpid(), 0, dinfo.w, dinfo.h, PIXEL_FORMAT_RGB_565,
134 ISurfaceComposer::eGPU);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800135 session()->openTransaction();
Mathias Agopian17f638b2009-04-16 20:04:08 -0700136 control->setLayer(0x40000000);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800137 session()->closeTransaction();
138
Mathias Agopian17f638b2009-04-16 20:04:08 -0700139 sp<Surface> s = control->getSurface();
140
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800141 // initialize opengl and egl
Mathias Agopian738b9a42009-08-06 16:41:02 -0700142 const EGLint attribs[] = {
143 EGL_DEPTH_SIZE, 0,
144 EGL_NONE
145 };
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800146 EGLint w, h, dummy;
147 EGLint numConfigs;
148 EGLConfig config;
149 EGLSurface surface;
150 EGLContext context;
Mathias Agopian627e7b52009-05-21 19:21:59 -0700151
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800152 EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
Mathias Agopian627e7b52009-05-21 19:21:59 -0700153
154 eglInitialize(display, 0, 0);
Mathias Agopian738b9a42009-08-06 16:41:02 -0700155 EGLUtils::selectConfigForNativeWindow(display, attribs, s.get(), &config);
Mathias Agopian1473f462009-04-10 14:24:30 -0700156 surface = eglCreateWindowSurface(display, config, s.get(), NULL);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800157 context = eglCreateContext(display, config, NULL, NULL);
158 eglQuerySurface(display, surface, EGL_WIDTH, &w);
159 eglQuerySurface(display, surface, EGL_HEIGHT, &h);
Mathias Agopianabac0102009-07-31 14:47:00 -0700160
161 if (eglMakeCurrent(display, surface, surface, context) == EGL_FALSE)
162 return NO_INIT;
163
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800164 mDisplay = display;
165 mContext = context;
166 mSurface = surface;
167 mWidth = w;
168 mHeight = h;
Mathias Agopian17f638b2009-04-16 20:04:08 -0700169 mFlingerSurfaceControl = control;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800170 mFlingerSurface = s;
171
172 // initialize GL
173 glShadeModel(GL_FLAT);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800174 glEnable(GL_TEXTURE_2D);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800175 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
176
177 return NO_ERROR;
178}
179
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800180bool 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);
Mathias Agopian6cf0db22009-04-17 19:36:26 -0700185 mFlingerSurface.clear();
Mathias Agopian17f638b2009-04-16 20:04:08 -0700186 mFlingerSurfaceControl.clear();
Mathias Agopian627e7b52009-05-21 19:21:59 -0700187 eglTerminate(mDisplay);
188 IPCThreadState::self()->stopProcess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800189 return r;
190}
191
192bool BootAnimation::android() {
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700193 initTexture(&mAndroid[0], mAssets, "images/android-logo-mask.png");
194 initTexture(&mAndroid[1], mAssets, "images/android-logo-shine.png");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800195
196 // clear screen
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700197 glDisable(GL_DITHER);
198 glDisable(GL_SCISSOR_TEST);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800199 glClear(GL_COLOR_BUFFER_BIT);
200 eglSwapBuffers(mDisplay, mSurface);
201
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700202 const GLint xc = (mWidth - mAndroid[0].w) / 2;
203 const GLint yc = (mHeight - mAndroid[0].h) / 2;
204 const Rect updateRect(xc, yc, xc + mAndroid[0].w, yc + mAndroid[0].h);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800205
206 // draw and update only what we need
Mathias Agopian1473f462009-04-10 14:24:30 -0700207 mFlingerSurface->setSwapRectangle(updateRect);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800208
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800209 glScissor(updateRect.left, mHeight - updateRect.bottom, updateRect.width(),
210 updateRect.height());
211
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700212 // Blend state
213 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
214 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
215
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800216 const nsecs_t startTime = systemTime();
217 do {
Mathias Agopian13796652009-03-24 22:49:21 -0700218 nsecs_t now = systemTime();
219 double time = now - startTime;
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700220 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
Mathias Agopian81668642009-07-28 11:41:30 -0700224 glDisable(GL_SCISSOR_TEST);
225 glClear(GL_COLOR_BUFFER_BIT);
226
227 glEnable(GL_SCISSOR_TEST);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800228 glDisable(GL_BLEND);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800229 glBindTexture(GL_TEXTURE_2D, mAndroid[1].name);
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700230 glDrawTexiOES(x, yc, 0, mAndroid[1].w, mAndroid[1].h);
231 glDrawTexiOES(x + mAndroid[1].w, yc, 0, mAndroid[1].w, mAndroid[1].h);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800232
Mathias Agopianb2cf9542009-03-24 18:34:16 -0700233 glEnable(GL_BLEND);
234 glBindTexture(GL_TEXTURE_2D, mAndroid[0].name);
235 glDrawTexiOES(xc, yc, 0, mAndroid[0].w, mAndroid[0].h);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800236
Mathias Agopian627e7b52009-05-21 19:21:59 -0700237 EGLBoolean res = eglSwapBuffers(mDisplay, mSurface);
238 if (res == EGL_FALSE)
239 break;
240
Mathias Agopian13796652009-03-24 22:49:21 -0700241 // 12fps: don't animate too fast to preserve CPU
242 const nsecs_t sleepTime = 83333 - ns2us(systemTime() - now);
243 if (sleepTime > 0)
244 usleep(sleepTime);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800245 } while (!exitPending());
246
247 glDeleteTextures(1, &mAndroid[0].name);
248 glDeleteTextures(1, &mAndroid[1].name);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800249 return false;
250}
251
252// ---------------------------------------------------------------------------
253
254}
255; // namespace android