The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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 | |
Mathias Agopian | 627e7b5 | 2009-05-21 19:21:59 -0700 | [diff] [blame] | 25 | #include <utils/IPCThreadState.h> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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/EGLNativeWindowSurface.h> |
| 39 | |
| 40 | #include <core/SkBitmap.h> |
| 41 | #include <images/SkImageDecoder.h> |
| 42 | |
| 43 | #include <GLES/gl.h> |
| 44 | #include <GLES/glext.h> |
| 45 | #include <EGL/eglext.h> |
| 46 | |
| 47 | #include "BootAnimation.h" |
| 48 | |
| 49 | namespace android { |
| 50 | |
| 51 | // --------------------------------------------------------------------------- |
| 52 | |
Mathias Agopian | 627e7b5 | 2009-05-21 19:21:59 -0700 | [diff] [blame] | 53 | BootAnimation::BootAnimation() : Thread(false) |
| 54 | { |
| 55 | mSession = new SurfaceComposerClient(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | BootAnimation::~BootAnimation() { |
| 59 | } |
| 60 | |
| 61 | void BootAnimation::onFirstRef() { |
| 62 | run("BootAnimation", PRIORITY_DISPLAY); |
| 63 | } |
| 64 | |
| 65 | const sp<SurfaceComposerClient>& BootAnimation::session() const { |
| 66 | return mSession; |
| 67 | } |
| 68 | |
| 69 | status_t BootAnimation::initTexture(Texture* texture, AssetManager& assets, |
| 70 | const char* name) { |
| 71 | Asset* asset = assets.open(name, Asset::ACCESS_BUFFER); |
| 72 | if (!asset) |
| 73 | return NO_INIT; |
| 74 | SkBitmap bitmap; |
| 75 | SkImageDecoder::DecodeMemory(asset->getBuffer(false), asset->getLength(), |
| 76 | &bitmap, SkBitmap::kNo_Config, SkImageDecoder::kDecodePixels_Mode); |
| 77 | asset->close(); |
| 78 | delete asset; |
| 79 | |
| 80 | // ensure we can call getPixels(). No need to call unlock, since the |
| 81 | // bitmap will go out of scope when we return from this method. |
| 82 | bitmap.lockPixels(); |
| 83 | |
| 84 | const int w = bitmap.width(); |
| 85 | const int h = bitmap.height(); |
| 86 | const void* p = bitmap.getPixels(); |
| 87 | |
| 88 | GLint crop[4] = { 0, h, w, -h }; |
| 89 | texture->w = w; |
| 90 | texture->h = h; |
| 91 | |
| 92 | glGenTextures(1, &texture->name); |
| 93 | glBindTexture(GL_TEXTURE_2D, texture->name); |
| 94 | |
| 95 | switch (bitmap.getConfig()) { |
| 96 | case SkBitmap::kA8_Config: |
| 97 | glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, w, h, 0, GL_ALPHA, |
| 98 | GL_UNSIGNED_BYTE, p); |
| 99 | break; |
| 100 | case SkBitmap::kARGB_4444_Config: |
| 101 | glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA, |
| 102 | GL_UNSIGNED_SHORT_4_4_4_4, p); |
| 103 | break; |
| 104 | case SkBitmap::kARGB_8888_Config: |
| 105 | glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA, |
| 106 | GL_UNSIGNED_BYTE, p); |
| 107 | break; |
| 108 | case SkBitmap::kRGB_565_Config: |
| 109 | glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, w, h, 0, GL_RGB, |
| 110 | GL_UNSIGNED_SHORT_5_6_5, p); |
| 111 | break; |
| 112 | default: |
| 113 | break; |
| 114 | } |
| 115 | |
| 116 | glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_CROP_RECT_OES, crop); |
| 117 | glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); |
| 118 | glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); |
| 119 | glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); |
| 120 | glTexParameterx(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); |
| 121 | return NO_ERROR; |
| 122 | } |
| 123 | |
| 124 | status_t BootAnimation::readyToRun() { |
| 125 | mAssets.addDefaultAssets(); |
| 126 | |
| 127 | DisplayInfo dinfo; |
| 128 | status_t status = session()->getDisplayInfo(0, &dinfo); |
| 129 | if (status) |
| 130 | return -1; |
| 131 | |
| 132 | // create the native surface |
| 133 | sp<Surface> s = session()->createSurface(getpid(), 0, dinfo.w, dinfo.h, |
Mathias Agopian | 627e7b5 | 2009-05-21 19:21:59 -0700 | [diff] [blame] | 134 | PIXEL_FORMAT_RGB_565, ISurfaceComposer::eGPU); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 135 | session()->openTransaction(); |
| 136 | s->setLayer(0x40000000); |
| 137 | session()->closeTransaction(); |
| 138 | |
| 139 | // initialize opengl and egl |
| 140 | const EGLint attribs[] = { EGL_RED_SIZE, 5, EGL_GREEN_SIZE, 6, |
| 141 | EGL_BLUE_SIZE, 5, EGL_DEPTH_SIZE, 0, EGL_NONE }; |
| 142 | EGLint w, h, dummy; |
| 143 | EGLint numConfigs; |
| 144 | EGLConfig config; |
| 145 | EGLSurface surface; |
| 146 | EGLContext context; |
Mathias Agopian | 627e7b5 | 2009-05-21 19:21:59 -0700 | [diff] [blame] | 147 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 148 | EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY); |
Mathias Agopian | 627e7b5 | 2009-05-21 19:21:59 -0700 | [diff] [blame] | 149 | |
| 150 | eglInitialize(display, 0, 0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 151 | eglChooseConfig(display, attribs, &config, 1, &numConfigs); |
| 152 | |
| 153 | mNativeWindowSurface = new EGLNativeWindowSurface(s); |
| 154 | surface = eglCreateWindowSurface(display, config, |
| 155 | mNativeWindowSurface.get(), NULL); |
| 156 | |
| 157 | context = eglCreateContext(display, config, NULL, NULL); |
| 158 | eglQuerySurface(display, surface, EGL_WIDTH, &w); |
| 159 | eglQuerySurface(display, surface, EGL_HEIGHT, &h); |
| 160 | eglMakeCurrent(display, surface, surface, context); |
| 161 | mDisplay = display; |
| 162 | mContext = context; |
| 163 | mSurface = surface; |
| 164 | mWidth = w; |
| 165 | mHeight = h; |
| 166 | mFlingerSurface = s; |
| 167 | |
| 168 | // initialize GL |
| 169 | glShadeModel(GL_FLAT); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 170 | glEnable(GL_TEXTURE_2D); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 171 | glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); |
| 172 | |
| 173 | return NO_ERROR; |
| 174 | } |
| 175 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 176 | bool BootAnimation::threadLoop() { |
| 177 | bool r = android(); |
| 178 | eglMakeCurrent(mDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT); |
| 179 | eglDestroyContext(mDisplay, mContext); |
| 180 | eglDestroySurface(mDisplay, mSurface); |
| 181 | mNativeWindowSurface.clear(); |
Mathias Agopian | 627e7b5 | 2009-05-21 19:21:59 -0700 | [diff] [blame] | 182 | mFlingerSurface.clear(); |
| 183 | eglTerminate(mDisplay); |
| 184 | IPCThreadState::self()->stopProcess(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 185 | return r; |
| 186 | } |
| 187 | |
| 188 | bool BootAnimation::android() { |
Mathias Agopian | b24b297 | 2009-03-24 18:34:22 -0700 | [diff] [blame] | 189 | initTexture(&mAndroid[0], mAssets, "images/android-logo-mask.png"); |
| 190 | initTexture(&mAndroid[1], mAssets, "images/android-logo-shine.png"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 191 | |
| 192 | // clear screen |
Mathias Agopian | b24b297 | 2009-03-24 18:34:22 -0700 | [diff] [blame] | 193 | glDisable(GL_DITHER); |
| 194 | glDisable(GL_SCISSOR_TEST); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 195 | glClear(GL_COLOR_BUFFER_BIT); |
| 196 | eglSwapBuffers(mDisplay, mSurface); |
| 197 | |
Mathias Agopian | b24b297 | 2009-03-24 18:34:22 -0700 | [diff] [blame] | 198 | const GLint xc = (mWidth - mAndroid[0].w) / 2; |
| 199 | const GLint yc = (mHeight - mAndroid[0].h) / 2; |
| 200 | const Rect updateRect(xc, yc, xc + mAndroid[0].w, yc + mAndroid[0].h); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 201 | |
| 202 | // draw and update only what we need |
| 203 | mNativeWindowSurface->setSwapRectangle(updateRect.left, |
| 204 | updateRect.top, updateRect.width(), updateRect.height()); |
| 205 | |
| 206 | glEnable(GL_SCISSOR_TEST); |
| 207 | glScissor(updateRect.left, mHeight - updateRect.bottom, updateRect.width(), |
| 208 | updateRect.height()); |
| 209 | |
Mathias Agopian | b24b297 | 2009-03-24 18:34:22 -0700 | [diff] [blame] | 210 | // Blend state |
| 211 | glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); |
| 212 | glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); |
| 213 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 214 | const nsecs_t startTime = systemTime(); |
| 215 | do { |
Mathias Agopian | 00f5eca | 2009-03-24 22:42:15 -0700 | [diff] [blame] | 216 | nsecs_t now = systemTime(); |
| 217 | double time = now - startTime; |
Mathias Agopian | b24b297 | 2009-03-24 18:34:22 -0700 | [diff] [blame] | 218 | float t = 4.0f * float(time / us2ns(16667)) / mAndroid[1].w; |
| 219 | GLint offset = (1 - (t - floorf(t))) * mAndroid[1].w; |
| 220 | GLint x = xc - offset; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 221 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 222 | glDisable(GL_BLEND); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 223 | glBindTexture(GL_TEXTURE_2D, mAndroid[1].name); |
Mathias Agopian | b24b297 | 2009-03-24 18:34:22 -0700 | [diff] [blame] | 224 | glDrawTexiOES(x, yc, 0, mAndroid[1].w, mAndroid[1].h); |
| 225 | glDrawTexiOES(x + mAndroid[1].w, yc, 0, mAndroid[1].w, mAndroid[1].h); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 226 | |
Mathias Agopian | b24b297 | 2009-03-24 18:34:22 -0700 | [diff] [blame] | 227 | glEnable(GL_BLEND); |
| 228 | glBindTexture(GL_TEXTURE_2D, mAndroid[0].name); |
| 229 | glDrawTexiOES(xc, yc, 0, mAndroid[0].w, mAndroid[0].h); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 230 | |
Mathias Agopian | 627e7b5 | 2009-05-21 19:21:59 -0700 | [diff] [blame] | 231 | EGLBoolean res = eglSwapBuffers(mDisplay, mSurface); |
| 232 | if (res == EGL_FALSE) |
| 233 | break; |
| 234 | |
Mathias Agopian | 00f5eca | 2009-03-24 22:42:15 -0700 | [diff] [blame] | 235 | // 12fps: don't animate too fast to preserve CPU |
| 236 | const nsecs_t sleepTime = 83333 - ns2us(systemTime() - now); |
| 237 | if (sleepTime > 0) |
| 238 | usleep(sleepTime); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 239 | } while (!exitPending()); |
| 240 | |
| 241 | glDeleteTextures(1, &mAndroid[0].name); |
| 242 | glDeleteTextures(1, &mAndroid[1].name); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 243 | return false; |
| 244 | } |
| 245 | |
| 246 | // --------------------------------------------------------------------------- |
| 247 | |
| 248 | } |
| 249 | ; // namespace android |