The Android Open Source Project | edbf3b6 | 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 | |
| 25 | #include <utils/threads.h> |
| 26 | #include <utils/Atomic.h> |
| 27 | #include <utils/Errors.h> |
| 28 | #include <utils/Log.h> |
| 29 | #include <utils/AssetManager.h> |
| 30 | |
| 31 | #include <ui/PixelFormat.h> |
| 32 | #include <ui/Rect.h> |
| 33 | #include <ui/Region.h> |
| 34 | #include <ui/DisplayInfo.h> |
| 35 | #include <ui/ISurfaceComposer.h> |
| 36 | #include <ui/ISurfaceFlingerClient.h> |
| 37 | #include <ui/EGLNativeWindowSurface.h> |
| 38 | |
| 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 | |
| 48 | namespace android { |
| 49 | |
| 50 | // --------------------------------------------------------------------------- |
| 51 | |
| 52 | BootAnimation::BootAnimation(const sp<ISurfaceComposer>& composer) : |
| 53 | Thread(false) { |
| 54 | mSession = SurfaceComposerClient::clientForConnection( |
| 55 | composer->createConnection()->asBinder()); |
| 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, |
| 134 | PIXEL_FORMAT_RGB_565); |
| 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; |
| 147 | EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY); |
| 148 | eglChooseConfig(display, attribs, &config, 1, &numConfigs); |
| 149 | |
| 150 | mNativeWindowSurface = new EGLNativeWindowSurface(s); |
| 151 | surface = eglCreateWindowSurface(display, config, |
| 152 | mNativeWindowSurface.get(), NULL); |
| 153 | |
| 154 | context = eglCreateContext(display, config, NULL, NULL); |
| 155 | eglQuerySurface(display, surface, EGL_WIDTH, &w); |
| 156 | eglQuerySurface(display, surface, EGL_HEIGHT, &h); |
| 157 | eglMakeCurrent(display, surface, surface, context); |
| 158 | mDisplay = display; |
| 159 | mContext = context; |
| 160 | mSurface = surface; |
| 161 | mWidth = w; |
| 162 | mHeight = h; |
| 163 | mFlingerSurface = s; |
| 164 | |
| 165 | // initialize GL |
| 166 | glShadeModel(GL_FLAT); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 167 | glEnable(GL_TEXTURE_2D); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 168 | glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); |
| 169 | |
| 170 | return NO_ERROR; |
| 171 | } |
| 172 | |
| 173 | void BootAnimation::requestExit() { |
| 174 | mBarrier.open(); |
| 175 | Thread::requestExit(); |
| 176 | } |
| 177 | |
| 178 | bool BootAnimation::threadLoop() { |
| 179 | bool r = android(); |
| 180 | eglMakeCurrent(mDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT); |
| 181 | eglDestroyContext(mDisplay, mContext); |
| 182 | eglDestroySurface(mDisplay, mSurface); |
| 183 | mNativeWindowSurface.clear(); |
| 184 | return r; |
| 185 | } |
| 186 | |
| 187 | bool BootAnimation::android() { |
Mathias Agopian | 7a7af5c | 2009-03-24 18:34:16 -0700 | [diff] [blame] | 188 | initTexture(&mAndroid[0], mAssets, "images/android-logo-mask.png"); |
| 189 | initTexture(&mAndroid[1], mAssets, "images/android-logo-shine.png"); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 190 | |
| 191 | // clear screen |
Mathias Agopian | 7a7af5c | 2009-03-24 18:34:16 -0700 | [diff] [blame] | 192 | glDisable(GL_DITHER); |
| 193 | glDisable(GL_SCISSOR_TEST); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 194 | glClear(GL_COLOR_BUFFER_BIT); |
| 195 | eglSwapBuffers(mDisplay, mSurface); |
| 196 | |
Mathias Agopian | 7a7af5c | 2009-03-24 18:34:16 -0700 | [diff] [blame] | 197 | const GLint xc = (mWidth - mAndroid[0].w) / 2; |
| 198 | const GLint yc = (mHeight - mAndroid[0].h) / 2; |
| 199 | const Rect updateRect(xc, yc, xc + mAndroid[0].w, yc + mAndroid[0].h); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 200 | |
| 201 | // draw and update only what we need |
| 202 | mNativeWindowSurface->setSwapRectangle(updateRect.left, |
| 203 | updateRect.top, updateRect.width(), updateRect.height()); |
| 204 | |
| 205 | glEnable(GL_SCISSOR_TEST); |
| 206 | glScissor(updateRect.left, mHeight - updateRect.bottom, updateRect.width(), |
| 207 | updateRect.height()); |
| 208 | |
Mathias Agopian | 7a7af5c | 2009-03-24 18:34:16 -0700 | [diff] [blame] | 209 | // Blend state |
| 210 | glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); |
| 211 | glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); |
| 212 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 213 | const nsecs_t startTime = systemTime(); |
| 214 | do { |
Mathias Agopian | b535e58 | 2009-03-24 22:49:21 -0700 | [diff] [blame^] | 215 | nsecs_t now = systemTime(); |
| 216 | double time = now - startTime; |
Mathias Agopian | 7a7af5c | 2009-03-24 18:34:16 -0700 | [diff] [blame] | 217 | float t = 4.0f * float(time / us2ns(16667)) / mAndroid[1].w; |
| 218 | GLint offset = (1 - (t - floorf(t))) * mAndroid[1].w; |
| 219 | GLint x = xc - offset; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 220 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 221 | glDisable(GL_BLEND); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 222 | glBindTexture(GL_TEXTURE_2D, mAndroid[1].name); |
Mathias Agopian | 7a7af5c | 2009-03-24 18:34:16 -0700 | [diff] [blame] | 223 | glDrawTexiOES(x, yc, 0, mAndroid[1].w, mAndroid[1].h); |
| 224 | glDrawTexiOES(x + mAndroid[1].w, yc, 0, mAndroid[1].w, mAndroid[1].h); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 225 | |
Mathias Agopian | 7a7af5c | 2009-03-24 18:34:16 -0700 | [diff] [blame] | 226 | glEnable(GL_BLEND); |
| 227 | glBindTexture(GL_TEXTURE_2D, mAndroid[0].name); |
| 228 | glDrawTexiOES(xc, yc, 0, mAndroid[0].w, mAndroid[0].h); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 229 | |
| 230 | eglSwapBuffers(mDisplay, mSurface); |
Mathias Agopian | b535e58 | 2009-03-24 22:49:21 -0700 | [diff] [blame^] | 231 | |
| 232 | // 12fps: don't animate too fast to preserve CPU |
| 233 | const nsecs_t sleepTime = 83333 - ns2us(systemTime() - now); |
| 234 | if (sleepTime > 0) |
| 235 | usleep(sleepTime); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 236 | } while (!exitPending()); |
| 237 | |
| 238 | glDeleteTextures(1, &mAndroid[0].name); |
| 239 | glDeleteTextures(1, &mAndroid[1].name); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 240 | return false; |
| 241 | } |
| 242 | |
| 243 | // --------------------------------------------------------------------------- |
| 244 | |
| 245 | } |
| 246 | ; // namespace android |