blob: 3e4035e9fc1929e8d7b2a8eb21f69049f303dca9 [file] [log] [blame]
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -08001/*
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 Agopian0d1318b2009-03-27 17:58:20 -070025#include <utils/StopWatch.h>
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080026
27#include <core/SkBitmap.h>
28
29#include <ui/EGLDisplaySurface.h>
30
Mathias Agopian0d1318b2009-03-27 17:58:20 -070031#include "BlurFilter.h"
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080032#include "LayerBase.h"
33#include "LayerOrientationAnim.h"
34#include "SurfaceFlinger.h"
35#include "DisplayHardware/DisplayHardware.h"
36#include "OrientationAnimation.h"
37
38namespace android {
39// ---------------------------------------------------------------------------
40
41const uint32_t LayerOrientationAnim::typeInfo = LayerBase::typeInfo | 0x80;
42const char* const LayerOrientationAnim::typeID = "LayerOrientationAnim";
43
44// ---------------------------------------------------------------------------
45
Mathias Agopian0d1318b2009-03-27 17:58:20 -070046// Animation...
47const float DURATION = ms2ns(200);
48const float BOUNCES_PER_SECOND = 0.5f;
49//const float BOUNCES_AMPLITUDE = 1.0f/16.0f;
50const float BOUNCES_AMPLITUDE = 0;
51const 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 Projectedbf3b62009-03-03 19:31:44 -080057LayerOrientationAnim::LayerOrientationAnim(
58 SurfaceFlinger* flinger, DisplayID display,
59 OrientationAnimation* anim,
Mathias Agopian0d1318b2009-03-27 17:58:20 -070060 const LayerBitmap& bitmapIn,
61 const LayerBitmap& bitmapOut)
62 : LayerOrientationAnimBase(flinger, display), mAnim(anim),
63 mBitmapIn(bitmapIn), mBitmapOut(bitmapOut),
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080064 mTextureName(-1), mTextureNameIn(-1)
65{
Mathias Agopian0d1318b2009-03-27 17:58:20 -070066 // blur that texture.
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080067 mStartTime = systemTime();
68 mFinishTime = 0;
69 mOrientationCompleted = false;
70 mFirstRedraw = false;
71 mLastNormalizedTime = 0;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080072 mNeedsBlending = false;
Mathias Agopian0d1318b2009-03-27 17:58:20 -070073 mAlphaInLerp.set(1.0f, DIM_TARGET);
74 mAlphaOutLerp.set(0.5f, 1.0f);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -080075}
76
77LayerOrientationAnim::~LayerOrientationAnim()
78{
79 if (mTextureName != -1U) {
80 LayerBase::deletedTextures.add(mTextureName);
81 }
82 if (mTextureNameIn != -1U) {
83 LayerBase::deletedTextures.add(mTextureNameIn);
84 }
85}
86
87bool LayerOrientationAnim::needsBlending() const
88{
89 return mNeedsBlending;
90}
91
92Point 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
99void 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 Agopian7c143aa2009-03-24 22:50:50 -0700112 copybit_device_t* copybit = mFlinger->getBlitEngine();
113 if (copybit) {
114 mCanUseCopyBit = true;
115 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800116}
117
118void LayerOrientationAnim::onOrientationCompleted()
119{
120 mFinishTime = systemTime();
121 mOrientationCompleted = true;
122 mFirstRedraw = true;
123 mNeedsBlending = true;
124 mFlinger->invalidateLayerVisibility(this);
125}
126
127void LayerOrientationAnim::onDraw(const Region& clip) const
128{
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800129 const nsecs_t now = systemTime();
Mathias Agopian0d1318b2009-03-27 17:58:20 -0700130 float alphaIn, alphaOut;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800131
132 if (mOrientationCompleted) {
133 if (mFirstRedraw) {
Mathias Agopian7c143aa2009-03-24 22:50:50 -0700134 mFirstRedraw = false;
135
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800136 // make a copy of what's on screen
137 copybit_image_t image;
Mathias Agopian0d1318b2009-03-27 17:58:20 -0700138 mBitmapOut.getBitmapSurface(&image);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800139 const DisplayHardware& hw(graphicPlane(0).displayHardware());
140 hw.copyBackToImage(image);
Mathias Agopian7c143aa2009-03-24 22:50:50 -0700141
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 Projectedbf3b62009-03-03 19:31:44 -0800148
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 Agopian0d1318b2009-03-27 17:58:20 -0700159 const float interpolatedTime = INTERPOLATED_TIME(normalizedTime);
160 alphaIn = mAlphaInLerp.getOut();
161 alphaOut = mAlphaOutLerp(interpolatedTime);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800162 } else {
163 mAnim->onAnimationFinished();
Mathias Agopian0d1318b2009-03-27 17:58:20 -0700164 alphaIn = mAlphaInLerp.getOut();
165 alphaOut = mAlphaOutLerp.getOut();
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800166 }
167 } else {
168 const float normalizedTime = float(now - mStartTime) / DURATION;
169 if (normalizedTime <= 1.0f) {
170 mLastNormalizedTime = normalizedTime;
Mathias Agopian0d1318b2009-03-27 17:58:20 -0700171 const float interpolatedTime = INTERPOLATED_TIME(normalizedTime);
172 alphaIn = mAlphaInLerp(interpolatedTime);
173 alphaOut = 0.0f;
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800174 } else {
175 mLastNormalizedTime = 1.0f;
176 const float to_seconds = DURATION / seconds(1);
Mathias Agopian0d1318b2009-03-27 17:58:20 -0700177 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 Projectedbf3b62009-03-03 19:31:44 -0800186 }
Mathias Agopian0d1318b2009-03-27 17:58:20 -0700187 mAlphaOutLerp.setIn(alphaIn);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800188 }
Mathias Agopian0d1318b2009-03-27 17:58:20 -0700189 drawScaled(1.0f, alphaIn, alphaOut);
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800190}
191
Mathias Agopian0d1318b2009-03-27 17:58:20 -0700192void LayerOrientationAnim::drawScaled(float scale, float alphaIn, float alphaOut) const
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800193{
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 Agopian7c143aa2009-03-24 22:50:50 -0700202 if (!mOrientationCompleted) {
Mathias Agopian0d1318b2009-03-27 17:58:20 -0700203 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 Agopian7c143aa2009-03-24 22:50:50 -0700212 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800213
Mathias Agopian0d1318b2009-03-27 17:58:20 -0700214 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 Agopian7c143aa2009-03-24 22:50:50 -0700222 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 Projectedbf3b62009-03-03 19:31:44 -0800225 const copybit_rect_t srect = { 0, 0, src.w, src.h };
Mathias Agopian0d1318b2009-03-27 17:58:20 -0700226 const Region reg(Rect( drect.l, drect.t, drect.r, drect.b ));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800227
Mathias Agopian7c143aa2009-03-24 22:50:50 -0700228 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 Agopian0d1318b2009-03-27 17:58:20 -0700234
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 Agopian7c143aa2009-03-24 22:50:50 -0700240 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800241
Mathias Agopian0d1318b2009-03-27 17:58:20 -0700242 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 Agopian7c143aa2009-03-24 22:50:50 -0700247 }
248 LOGE_IF(err != NO_ERROR, "copybit failed (%s)", strerror(err));
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800249 }
Mathias Agopian7c143aa2009-03-24 22:50:50 -0700250 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 Projectedbf3b62009-03-03 19:31:44 -0800259
Mathias Agopian7c143aa2009-03-24 22:50:50 -0700260 Transform tr;
Mathias Agopian0d1318b2009-03-27 17:58:20 -0700261 tr.set(scale,0,0,scale);
Mathias Agopian7c143aa2009-03-24 22:50:50 -0700262 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 Projectedbf3b62009-03-03 19:31:44 -0800268 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 Agopian7c143aa2009-03-24 22:50:50 -0700272 if (!(mFlags & DisplayHardware::SLOW_CONFIG)) {
273 // Too slow to do this in software
274 self.mDrawingState.flags |= ISurfaceComposer::eLayerFilter;
275 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800276
Mathias Agopian0d1318b2009-03-27 17:58:20 -0700277 if (alphaIn > 0.0f) {
Mathias Agopian7c143aa2009-03-24 22:50:50 -0700278 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 Agopian0d1318b2009-03-27 17:58:20 -0700285 self.mDrawingState.alpha = int(alphaIn*255);
286 drawWithOpenGL(reg, mTextureNameIn, t);
Mathias Agopian7c143aa2009-03-24 22:50:50 -0700287 }
288
Mathias Agopian0d1318b2009-03-27 17:58:20 -0700289 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 Projectedbf3b62009-03-03 19:31:44 -0800299 }
The Android Open Source Projectedbf3b62009-03-03 19:31:44 -0800300 }
301}
302
303// ---------------------------------------------------------------------------
304
305}; // namespace android