BootAnimation.cpp revision 8434c5369304e639efe8eab368ca410c589d87c2
1/* 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#include <signal.h> 25 26#include <binder/IPCThreadState.h> 27#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> 39#include <ui/FramebufferNativeWindow.h> 40#include <ui/EGLUtils.h> 41 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 55BootAnimation::BootAnimation() : Thread(false) 56{ 57 mSession = new SurfaceComposerClient(); 58} 59 60BootAnimation::~BootAnimation() { 61} 62 63void BootAnimation::onFirstRef() { 64 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 } 69} 70 71sp<SurfaceComposerClient> BootAnimation::session() const { 72 return mSession; 73} 74 75 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 87status_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 151 sp<SurfaceControl> control = session()->createSurface( 152 getpid(), 0, dinfo.w, dinfo.h, PIXEL_FORMAT_RGB_565); 153 session()->openTransaction(); 154 control->setLayer(0x40000000); 155 session()->closeTransaction(); 156 157 sp<Surface> s = control->getSurface(); 158 159 // initialize opengl and egl 160 const EGLint attribs[] = { 161 EGL_DEPTH_SIZE, 0, 162 EGL_NONE 163 }; 164 EGLint w, h, dummy; 165 EGLint numConfigs; 166 EGLConfig config; 167 EGLSurface surface; 168 EGLContext context; 169 170 EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY); 171 172 eglInitialize(display, 0, 0); 173 EGLUtils::selectConfigForNativeWindow(display, attribs, s.get(), &config); 174 surface = eglCreateWindowSurface(display, config, s.get(), NULL); 175 context = eglCreateContext(display, config, NULL, NULL); 176 eglQuerySurface(display, surface, EGL_WIDTH, &w); 177 eglQuerySurface(display, surface, EGL_HEIGHT, &h); 178 179 if (eglMakeCurrent(display, surface, surface, context) == EGL_FALSE) 180 return NO_INIT; 181 182 mDisplay = display; 183 mContext = context; 184 mSurface = surface; 185 mWidth = w; 186 mHeight = h; 187 mFlingerSurfaceControl = control; 188 mFlingerSurface = s; 189 190 // initialize GL 191 glShadeModel(GL_FLAT); 192 glEnable(GL_TEXTURE_2D); 193 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); 194 195 return NO_ERROR; 196} 197 198bool 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); 203 mFlingerSurface.clear(); 204 mFlingerSurfaceControl.clear(); 205 eglTerminate(mDisplay); 206 IPCThreadState::self()->stopProcess(); 207 return r; 208} 209 210bool BootAnimation::android() { 211 initTexture(&mAndroid[0], mAssets, "images/android-logo-mask.png"); 212 initTexture(&mAndroid[1], mAssets, "images/android-logo-shine.png"); 213 214 // clear screen 215 glDisable(GL_DITHER); 216 glDisable(GL_SCISSOR_TEST); 217 glClear(GL_COLOR_BUFFER_BIT); 218 eglSwapBuffers(mDisplay, mSurface); 219 220 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); 223 224 // draw and update only what we need 225 mFlingerSurface->setSwapRectangle(updateRect); 226 227 glScissor(updateRect.left, mHeight - updateRect.bottom, updateRect.width(), 228 updateRect.height()); 229 230 // Blend state 231 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); 232 glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); 233 234 const nsecs_t startTime = systemTime(); 235 do { 236 nsecs_t now = systemTime(); 237 double time = now - startTime; 238 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; 241 242 glDisable(GL_SCISSOR_TEST); 243 glClear(GL_COLOR_BUFFER_BIT); 244 245 glEnable(GL_SCISSOR_TEST); 246 glDisable(GL_BLEND); 247 glBindTexture(GL_TEXTURE_2D, mAndroid[1].name); 248 glDrawTexiOES(x, yc, 0, mAndroid[1].w, mAndroid[1].h); 249 glDrawTexiOES(x + mAndroid[1].w, yc, 0, mAndroid[1].w, mAndroid[1].h); 250 251 glEnable(GL_BLEND); 252 glBindTexture(GL_TEXTURE_2D, mAndroid[0].name); 253 glDrawTexiOES(xc, yc, 0, mAndroid[0].w, mAndroid[0].h); 254 255 EGLBoolean res = eglSwapBuffers(mDisplay, mSurface); 256 if (res == EGL_FALSE) 257 break; 258 259 // 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); 263 } while (!exitPending()); 264 265 glDeleteTextures(1, &mAndroid[0].name); 266 glDeleteTextures(1, &mAndroid[1].name); 267 return false; 268} 269 270// --------------------------------------------------------------------------- 271 272} 273; // namespace android 274