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