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