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 "SurfaceFlinger" |
| 18 | |
| 19 | #include <stdlib.h> |
| 20 | #include <stdint.h> |
| 21 | #include <sys/types.h> |
| 22 | |
| 23 | #include <utils/Errors.h> |
| 24 | #include <utils/Log.h> |
Mathias Agopian | 0d1318b | 2009-03-27 17:58:20 -0700 | [diff] [blame] | 25 | #include <utils/StopWatch.h> |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 26 | |
| 27 | #include <core/SkBitmap.h> |
| 28 | |
| 29 | #include <ui/EGLDisplaySurface.h> |
| 30 | |
Mathias Agopian | 0d1318b | 2009-03-27 17:58:20 -0700 | [diff] [blame] | 31 | #include "BlurFilter.h" |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 32 | #include "LayerBase.h" |
| 33 | #include "LayerOrientationAnim.h" |
| 34 | #include "SurfaceFlinger.h" |
| 35 | #include "DisplayHardware/DisplayHardware.h" |
| 36 | #include "OrientationAnimation.h" |
| 37 | |
| 38 | namespace android { |
| 39 | // --------------------------------------------------------------------------- |
| 40 | |
| 41 | const uint32_t LayerOrientationAnim::typeInfo = LayerBase::typeInfo | 0x80; |
| 42 | const char* const LayerOrientationAnim::typeID = "LayerOrientationAnim"; |
| 43 | |
| 44 | // --------------------------------------------------------------------------- |
| 45 | |
Mathias Agopian | 0d1318b | 2009-03-27 17:58:20 -0700 | [diff] [blame] | 46 | // Animation... |
| 47 | const float DURATION = ms2ns(200); |
| 48 | const float BOUNCES_PER_SECOND = 0.5f; |
| 49 | //const float BOUNCES_AMPLITUDE = 1.0f/16.0f; |
| 50 | const float BOUNCES_AMPLITUDE = 0; |
| 51 | const float DIM_TARGET = 0.40f; |
| 52 | //#define INTERPOLATED_TIME(_t) ((_t)*(_t)) |
| 53 | #define INTERPOLATED_TIME(_t) (_t) |
| 54 | |
| 55 | // --------------------------------------------------------------------------- |
| 56 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 57 | LayerOrientationAnim::LayerOrientationAnim( |
| 58 | SurfaceFlinger* flinger, DisplayID display, |
| 59 | OrientationAnimation* anim, |
Mathias Agopian | 0d1318b | 2009-03-27 17:58:20 -0700 | [diff] [blame] | 60 | const LayerBitmap& bitmapIn, |
| 61 | const LayerBitmap& bitmapOut) |
| 62 | : LayerOrientationAnimBase(flinger, display), mAnim(anim), |
| 63 | mBitmapIn(bitmapIn), mBitmapOut(bitmapOut), |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 64 | mTextureName(-1), mTextureNameIn(-1) |
| 65 | { |
Mathias Agopian | 0d1318b | 2009-03-27 17:58:20 -0700 | [diff] [blame] | 66 | // blur that texture. |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 67 | mStartTime = systemTime(); |
| 68 | mFinishTime = 0; |
| 69 | mOrientationCompleted = false; |
| 70 | mFirstRedraw = false; |
| 71 | mLastNormalizedTime = 0; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 72 | mNeedsBlending = false; |
Mathias Agopian | 0d1318b | 2009-03-27 17:58:20 -0700 | [diff] [blame] | 73 | mAlphaInLerp.set(1.0f, DIM_TARGET); |
| 74 | mAlphaOutLerp.set(0.5f, 1.0f); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | LayerOrientationAnim::~LayerOrientationAnim() |
| 78 | { |
| 79 | if (mTextureName != -1U) { |
| 80 | LayerBase::deletedTextures.add(mTextureName); |
| 81 | } |
| 82 | if (mTextureNameIn != -1U) { |
| 83 | LayerBase::deletedTextures.add(mTextureNameIn); |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | bool LayerOrientationAnim::needsBlending() const |
| 88 | { |
| 89 | return mNeedsBlending; |
| 90 | } |
| 91 | |
| 92 | Point LayerOrientationAnim::getPhysicalSize() const |
| 93 | { |
| 94 | const GraphicPlane& plane(graphicPlane(0)); |
| 95 | const DisplayHardware& hw(plane.displayHardware()); |
| 96 | return Point(hw.getWidth(), hw.getHeight()); |
| 97 | } |
| 98 | |
| 99 | void LayerOrientationAnim::validateVisibility(const Transform&) |
| 100 | { |
| 101 | const Layer::State& s(drawingState()); |
| 102 | const Transform tr(s.transform); |
| 103 | const Point size(getPhysicalSize()); |
| 104 | uint32_t w = size.x; |
| 105 | uint32_t h = size.y; |
| 106 | mTransformedBounds = tr.makeBounds(w, h); |
| 107 | mLeft = tr.tx(); |
| 108 | mTop = tr.ty(); |
| 109 | transparentRegionScreen.clear(); |
| 110 | mTransformed = true; |
| 111 | mCanUseCopyBit = false; |
Mathias Agopian | 7c143aa | 2009-03-24 22:50:50 -0700 | [diff] [blame] | 112 | copybit_device_t* copybit = mFlinger->getBlitEngine(); |
| 113 | if (copybit) { |
| 114 | mCanUseCopyBit = true; |
| 115 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | void LayerOrientationAnim::onOrientationCompleted() |
| 119 | { |
| 120 | mFinishTime = systemTime(); |
| 121 | mOrientationCompleted = true; |
| 122 | mFirstRedraw = true; |
| 123 | mNeedsBlending = true; |
| 124 | mFlinger->invalidateLayerVisibility(this); |
| 125 | } |
| 126 | |
| 127 | void LayerOrientationAnim::onDraw(const Region& clip) const |
| 128 | { |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 129 | const nsecs_t now = systemTime(); |
Mathias Agopian | 0d1318b | 2009-03-27 17:58:20 -0700 | [diff] [blame] | 130 | float alphaIn, alphaOut; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 131 | |
| 132 | if (mOrientationCompleted) { |
| 133 | if (mFirstRedraw) { |
Mathias Agopian | 7c143aa | 2009-03-24 22:50:50 -0700 | [diff] [blame] | 134 | mFirstRedraw = false; |
| 135 | |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 136 | // make a copy of what's on screen |
| 137 | copybit_image_t image; |
Mathias Agopian | 0d1318b | 2009-03-27 17:58:20 -0700 | [diff] [blame] | 138 | mBitmapOut.getBitmapSurface(&image); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 139 | const DisplayHardware& hw(graphicPlane(0).displayHardware()); |
| 140 | hw.copyBackToImage(image); |
Mathias Agopian | 7c143aa | 2009-03-24 22:50:50 -0700 | [diff] [blame] | 141 | |
| 142 | // and erase the screen for this round |
| 143 | glDisable(GL_BLEND); |
| 144 | glDisable(GL_DITHER); |
| 145 | glDisable(GL_SCISSOR_TEST); |
| 146 | glClearColor(0,0,0,0); |
| 147 | glClear(GL_COLOR_BUFFER_BIT); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 148 | |
| 149 | // FIXME: code below is gross |
| 150 | mNeedsBlending = false; |
| 151 | LayerOrientationAnim* self(const_cast<LayerOrientationAnim*>(this)); |
| 152 | mFlinger->invalidateLayerVisibility(self); |
| 153 | } |
| 154 | |
| 155 | // make sure pick-up where we left off |
| 156 | const float duration = DURATION * mLastNormalizedTime; |
| 157 | const float normalizedTime = (float(now - mFinishTime) / duration); |
| 158 | if (normalizedTime <= 1.0f) { |
Mathias Agopian | 0d1318b | 2009-03-27 17:58:20 -0700 | [diff] [blame] | 159 | const float interpolatedTime = INTERPOLATED_TIME(normalizedTime); |
| 160 | alphaIn = mAlphaInLerp.getOut(); |
| 161 | alphaOut = mAlphaOutLerp(interpolatedTime); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 162 | } else { |
| 163 | mAnim->onAnimationFinished(); |
Mathias Agopian | 0d1318b | 2009-03-27 17:58:20 -0700 | [diff] [blame] | 164 | alphaIn = mAlphaInLerp.getOut(); |
| 165 | alphaOut = mAlphaOutLerp.getOut(); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 166 | } |
| 167 | } else { |
| 168 | const float normalizedTime = float(now - mStartTime) / DURATION; |
| 169 | if (normalizedTime <= 1.0f) { |
| 170 | mLastNormalizedTime = normalizedTime; |
Mathias Agopian | 0d1318b | 2009-03-27 17:58:20 -0700 | [diff] [blame] | 171 | const float interpolatedTime = INTERPOLATED_TIME(normalizedTime); |
| 172 | alphaIn = mAlphaInLerp(interpolatedTime); |
| 173 | alphaOut = 0.0f; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 174 | } else { |
| 175 | mLastNormalizedTime = 1.0f; |
| 176 | const float to_seconds = DURATION / seconds(1); |
Mathias Agopian | 0d1318b | 2009-03-27 17:58:20 -0700 | [diff] [blame] | 177 | alphaIn = mAlphaInLerp.getOut(); |
| 178 | if (BOUNCES_AMPLITUDE > 0.0f) { |
| 179 | const float phi = BOUNCES_PER_SECOND * |
| 180 | (((normalizedTime - 1.0f) * to_seconds)*M_PI*2); |
| 181 | if (alphaIn > 1.0f) alphaIn = 1.0f; |
| 182 | else if (alphaIn < 0.0f) alphaIn = 0.0f; |
| 183 | alphaIn += BOUNCES_AMPLITUDE * (1.0f - cosf(phi)); |
| 184 | } |
| 185 | alphaOut = 0.0f; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 186 | } |
Mathias Agopian | 0d1318b | 2009-03-27 17:58:20 -0700 | [diff] [blame] | 187 | mAlphaOutLerp.setIn(alphaIn); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 188 | } |
Mathias Agopian | 0d1318b | 2009-03-27 17:58:20 -0700 | [diff] [blame] | 189 | drawScaled(1.0f, alphaIn, alphaOut); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 190 | } |
| 191 | |
Mathias Agopian | 0d1318b | 2009-03-27 17:58:20 -0700 | [diff] [blame] | 192 | void LayerOrientationAnim::drawScaled(float scale, float alphaIn, float alphaOut) const |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 193 | { |
| 194 | copybit_image_t dst; |
| 195 | const GraphicPlane& plane(graphicPlane(0)); |
| 196 | const DisplayHardware& hw(plane.displayHardware()); |
| 197 | hw.getDisplaySurface(&dst); |
| 198 | |
| 199 | // clear screen |
| 200 | // TODO: with update on demand, we may be able |
| 201 | // to not erase the screen at all during the animation |
Mathias Agopian | 7c143aa | 2009-03-24 22:50:50 -0700 | [diff] [blame] | 202 | if (!mOrientationCompleted) { |
Mathias Agopian | 0d1318b | 2009-03-27 17:58:20 -0700 | [diff] [blame] | 203 | if (scale==1.0f && (alphaIn>=1.0f || alphaOut>=1.0f)) { |
| 204 | // we don't need to erase the screen in that case |
| 205 | } else { |
| 206 | glDisable(GL_BLEND); |
| 207 | glDisable(GL_DITHER); |
| 208 | glDisable(GL_SCISSOR_TEST); |
| 209 | glClearColor(0,0,0,0); |
| 210 | glClear(GL_COLOR_BUFFER_BIT); |
| 211 | } |
Mathias Agopian | 7c143aa | 2009-03-24 22:50:50 -0700 | [diff] [blame] | 212 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 213 | |
Mathias Agopian | 0d1318b | 2009-03-27 17:58:20 -0700 | [diff] [blame] | 214 | copybit_image_t src; |
| 215 | mBitmapIn.getBitmapSurface(&src); |
| 216 | |
| 217 | copybit_image_t srcOut; |
| 218 | mBitmapOut.getBitmapSurface(&srcOut); |
| 219 | |
| 220 | const int w = dst.w*scale; |
| 221 | const int h = dst.h*scale; |
Mathias Agopian | 7c143aa | 2009-03-24 22:50:50 -0700 | [diff] [blame] | 222 | const int xc = uint32_t(dst.w-w)/2; |
| 223 | const int yc = uint32_t(dst.h-h)/2; |
| 224 | const copybit_rect_t drect = { xc, yc, xc+w, yc+h }; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 225 | const copybit_rect_t srect = { 0, 0, src.w, src.h }; |
Mathias Agopian | 0d1318b | 2009-03-27 17:58:20 -0700 | [diff] [blame] | 226 | const Region reg(Rect( drect.l, drect.t, drect.r, drect.b )); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 227 | |
Mathias Agopian | 7c143aa | 2009-03-24 22:50:50 -0700 | [diff] [blame] | 228 | int err = NO_ERROR; |
| 229 | const int can_use_copybit = canUseCopybit(); |
| 230 | if (can_use_copybit) { |
| 231 | copybit_device_t* copybit = mFlinger->getBlitEngine(); |
| 232 | copybit->set_parameter(copybit, COPYBIT_TRANSFORM, 0); |
| 233 | copybit->set_parameter(copybit, COPYBIT_DITHER, COPYBIT_ENABLE); |
Mathias Agopian | 0d1318b | 2009-03-27 17:58:20 -0700 | [diff] [blame] | 234 | |
| 235 | if (alphaIn > 0) { |
| 236 | region_iterator it(reg); |
| 237 | copybit->set_parameter(copybit, COPYBIT_BLUR, COPYBIT_ENABLE); |
| 238 | copybit->set_parameter(copybit, COPYBIT_PLANE_ALPHA, int(alphaIn*255)); |
| 239 | err = copybit->stretch(copybit, &dst, &src, &drect, &srect, &it); |
Mathias Agopian | 7c143aa | 2009-03-24 22:50:50 -0700 | [diff] [blame] | 240 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 241 | |
Mathias Agopian | 0d1318b | 2009-03-27 17:58:20 -0700 | [diff] [blame] | 242 | if (!err && alphaOut > 0.0f) { |
| 243 | region_iterator it(reg); |
| 244 | copybit->set_parameter(copybit, COPYBIT_BLUR, COPYBIT_DISABLE); |
| 245 | copybit->set_parameter(copybit, COPYBIT_PLANE_ALPHA, int(alphaOut*255)); |
| 246 | err = copybit->stretch(copybit, &dst, &srcOut, &drect, &srect, &it); |
Mathias Agopian | 7c143aa | 2009-03-24 22:50:50 -0700 | [diff] [blame] | 247 | } |
| 248 | LOGE_IF(err != NO_ERROR, "copybit failed (%s)", strerror(err)); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 249 | } |
Mathias Agopian | 7c143aa | 2009-03-24 22:50:50 -0700 | [diff] [blame] | 250 | if (!can_use_copybit || err) { |
| 251 | GGLSurface t; |
| 252 | t.version = sizeof(GGLSurface); |
| 253 | t.width = src.w; |
| 254 | t.height = src.h; |
| 255 | t.stride = src.w; |
| 256 | t.vstride= src.h; |
| 257 | t.format = src.format; |
| 258 | t.data = (GGLubyte*)(intptr_t(src.base) + src.offset); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 259 | |
Mathias Agopian | 7c143aa | 2009-03-24 22:50:50 -0700 | [diff] [blame] | 260 | Transform tr; |
Mathias Agopian | 0d1318b | 2009-03-27 17:58:20 -0700 | [diff] [blame] | 261 | tr.set(scale,0,0,scale); |
Mathias Agopian | 7c143aa | 2009-03-24 22:50:50 -0700 | [diff] [blame] | 262 | tr.set(xc, yc); |
| 263 | |
| 264 | // FIXME: we should not access mVertices and mDrawingState like that, |
| 265 | // but since we control the animation, we know it's going to work okay. |
| 266 | // eventually we'd need a more formal way of doing things like this. |
| 267 | LayerOrientationAnim& self(const_cast<LayerOrientationAnim&>(*this)); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 268 | tr.transform(self.mVertices[0], 0, 0); |
| 269 | tr.transform(self.mVertices[1], 0, src.h); |
| 270 | tr.transform(self.mVertices[2], src.w, src.h); |
| 271 | tr.transform(self.mVertices[3], src.w, 0); |
Mathias Agopian | 7c143aa | 2009-03-24 22:50:50 -0700 | [diff] [blame] | 272 | if (!(mFlags & DisplayHardware::SLOW_CONFIG)) { |
| 273 | // Too slow to do this in software |
| 274 | self.mDrawingState.flags |= ISurfaceComposer::eLayerFilter; |
| 275 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 276 | |
Mathias Agopian | 0d1318b | 2009-03-27 17:58:20 -0700 | [diff] [blame] | 277 | if (alphaIn > 0.0f) { |
Mathias Agopian | 7c143aa | 2009-03-24 22:50:50 -0700 | [diff] [blame] | 278 | t.data = (GGLubyte*)(intptr_t(src.base) + src.offset); |
| 279 | if (UNLIKELY(mTextureNameIn == -1LU)) { |
| 280 | mTextureNameIn = createTexture(); |
| 281 | GLuint w=0, h=0; |
| 282 | const Region dirty(Rect(t.width, t.height)); |
| 283 | loadTexture(dirty, mTextureNameIn, t, w, h); |
| 284 | } |
Mathias Agopian | 0d1318b | 2009-03-27 17:58:20 -0700 | [diff] [blame] | 285 | self.mDrawingState.alpha = int(alphaIn*255); |
| 286 | drawWithOpenGL(reg, mTextureNameIn, t); |
Mathias Agopian | 7c143aa | 2009-03-24 22:50:50 -0700 | [diff] [blame] | 287 | } |
| 288 | |
Mathias Agopian | 0d1318b | 2009-03-27 17:58:20 -0700 | [diff] [blame] | 289 | if (alphaOut > 0.0f) { |
| 290 | t.data = (GGLubyte*)(intptr_t(srcOut.base) + srcOut.offset); |
| 291 | if (UNLIKELY(mTextureName == -1LU)) { |
| 292 | mTextureName = createTexture(); |
| 293 | GLuint w=0, h=0; |
| 294 | const Region dirty(Rect(t.width, t.height)); |
| 295 | loadTexture(dirty, mTextureName, t, w, h); |
| 296 | } |
| 297 | self.mDrawingState.alpha = int(alphaOut*255); |
| 298 | drawWithOpenGL(reg, mTextureName, t); |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 299 | } |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 300 | } |
| 301 | } |
| 302 | |
| 303 | // --------------------------------------------------------------------------- |
| 304 | |
| 305 | }; // namespace android |