1 // Copyright 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "cc/render_surface_impl.h"
9 #include "base/logging.h"
10 #include "base/stringprintf.h"
11 #include "cc/damage_tracker.h"
12 #include "cc/debug_border_draw_quad.h"
13 #include "cc/debug_colors.h"
14 #include "cc/delegated_renderer_layer_impl.h"
15 #include "cc/layer_impl.h"
16 #include "cc/math_util.h"
17 #include "cc/quad_sink.h"
18 #include "cc/render_pass.h"
19 #include "cc/render_pass_draw_quad.h"
20 #include "cc/render_pass_sink.h"
21 #include "cc/shared_quad_state.h"
22 #include "third_party/skia/include/core/SkImageFilter.h"
23 #include "ui/gfx/rect_conversions.h"
24 #include "ui/gfx/transform.h"
28 RenderSurfaceImpl::RenderSurfaceImpl(LayerImpl
* owningLayer
)
29 : m_owningLayer(owningLayer
)
30 , m_surfacePropertyChanged(false)
32 , m_drawOpacityIsAnimating(false)
33 , m_targetSurfaceTransformsAreAnimating(false)
34 , m_screenSpaceTransformsAreAnimating(false)
36 , m_nearestAncestorThatMovesPixels(0)
37 , m_targetRenderSurfaceLayerIndexHistory(0)
38 , m_currentLayerIndexHistory(0)
40 m_damageTracker
= DamageTracker::create();
43 RenderSurfaceImpl::~RenderSurfaceImpl()
47 gfx::RectF
RenderSurfaceImpl::drawableContentRect() const
49 gfx::RectF drawableContentRect
= MathUtil::mapClippedRect(m_drawTransform
, m_contentRect
);
50 if (m_owningLayer
->hasReplica())
51 drawableContentRect
.Union(MathUtil::mapClippedRect(m_replicaDrawTransform
, m_contentRect
));
53 return drawableContentRect
;
56 std::string
RenderSurfaceImpl::name() const
58 return base::StringPrintf("RenderSurfaceImpl(id=%i,owner=%s)", m_owningLayer
->id(), m_owningLayer
->debugName().data());
61 static std::string
indentString(int indent
)
64 for (int i
= 0; i
!= indent
; ++i
)
69 void RenderSurfaceImpl::dumpSurface(std::string
* str
, int indent
) const
71 std::string indentStr
= indentString(indent
);
72 str
->append(indentStr
);
73 base::StringAppendF(str
, "%s\n", name().data());
75 indentStr
.append(" ");
76 str
->append(indentStr
);
77 base::StringAppendF(str
, "contentRect: (%d, %d, %d, %d)\n", m_contentRect
.x(), m_contentRect
.y(), m_contentRect
.width(), m_contentRect
.height());
79 str
->append(indentStr
);
80 base::StringAppendF(str
, "drawTransform: %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f\n",
81 m_drawTransform
.matrix().getDouble(0, 0), m_drawTransform
.matrix().getDouble(0, 1), m_drawTransform
.matrix().getDouble(0, 2), m_drawTransform
.matrix().getDouble(0, 3),
82 m_drawTransform
.matrix().getDouble(1, 0), m_drawTransform
.matrix().getDouble(1, 1), m_drawTransform
.matrix().getDouble(1, 2), m_drawTransform
.matrix().getDouble(1, 3),
83 m_drawTransform
.matrix().getDouble(2, 0), m_drawTransform
.matrix().getDouble(2, 1), m_drawTransform
.matrix().getDouble(2, 2), m_drawTransform
.matrix().getDouble(2, 3),
84 m_drawTransform
.matrix().getDouble(3, 0), m_drawTransform
.matrix().getDouble(3, 1), m_drawTransform
.matrix().getDouble(3, 2), m_drawTransform
.matrix().getDouble(3, 3));
86 str
->append(indentStr
);
87 base::StringAppendF(str
, "damageRect is pos(%f, %f), size(%f, %f)\n",
88 m_damageTracker
->currentDamageRect().x(), m_damageTracker
->currentDamageRect().y(),
89 m_damageTracker
->currentDamageRect().width(), m_damageTracker
->currentDamageRect().height());
92 int RenderSurfaceImpl::owningLayerId() const
94 return m_owningLayer
? m_owningLayer
->id() : 0;
98 void RenderSurfaceImpl::setClipRect(const gfx::Rect
& clipRect
)
100 if (m_clipRect
== clipRect
)
103 m_surfacePropertyChanged
= true;
104 m_clipRect
= clipRect
;
107 bool RenderSurfaceImpl::contentsChanged() const
109 return !m_damageTracker
->currentDamageRect().IsEmpty();
112 void RenderSurfaceImpl::setContentRect(const gfx::Rect
& contentRect
)
114 if (m_contentRect
== contentRect
)
117 m_surfacePropertyChanged
= true;
118 m_contentRect
= contentRect
;
121 bool RenderSurfaceImpl::surfacePropertyChanged() const
123 // Surface property changes are tracked as follows:
125 // - m_surfacePropertyChanged is flagged when the clipRect or contentRect change. As
126 // of now, these are the only two properties that can be affected by descendant layers.
128 // - all other property changes come from the owning layer (or some ancestor layer
129 // that propagates its change to the owning layer).
131 DCHECK(m_owningLayer
);
132 return m_surfacePropertyChanged
|| m_owningLayer
->layerPropertyChanged();
135 bool RenderSurfaceImpl::surfacePropertyChangedOnlyFromDescendant() const
137 return m_surfacePropertyChanged
&& !m_owningLayer
->layerPropertyChanged();
140 void RenderSurfaceImpl::addContributingDelegatedRenderPassLayer(LayerImpl
* layer
)
142 DCHECK(std::find(m_layerList
.begin(), m_layerList
.end(), layer
) != m_layerList
.end());
143 DelegatedRendererLayerImpl
* delegatedRendererLayer
= static_cast<DelegatedRendererLayerImpl
*>(layer
);
144 m_contributingDelegatedRenderPassLayerList
.push_back(delegatedRendererLayer
);
147 void RenderSurfaceImpl::clearLayerLists()
150 m_contributingDelegatedRenderPassLayerList
.clear();
153 RenderPass::Id
RenderSurfaceImpl::renderPassId()
155 int layerId
= m_owningLayer
->id();
158 return RenderPass::Id(layerId
, subId
);
161 void RenderSurfaceImpl::appendRenderPasses(RenderPassSink
& passSink
)
163 for (size_t i
= 0; i
< m_contributingDelegatedRenderPassLayerList
.size(); ++i
)
164 m_contributingDelegatedRenderPassLayerList
[i
]->AppendContributingRenderPasses(&passSink
);
166 scoped_ptr
<RenderPass
> pass
= RenderPass::Create();
167 pass
->SetNew(renderPassId(), m_contentRect
, m_damageTracker
->currentDamageRect(), m_screenSpaceTransform
);
168 passSink
.appendRenderPass(pass
.Pass());
171 void RenderSurfaceImpl::appendQuads(QuadSink
& quadSink
, AppendQuadsData
& appendQuadsData
, bool forReplica
, RenderPass::Id renderPassId
)
173 DCHECK(!forReplica
|| m_owningLayer
->hasReplica());
175 const gfx::Transform
& drawTransform
= forReplica
? m_replicaDrawTransform
: m_drawTransform
;
176 SharedQuadState
* sharedQuadState
= quadSink
.useSharedQuadState(SharedQuadState::Create());
177 sharedQuadState
->SetAll(drawTransform
, m_contentRect
, m_clipRect
, m_isClipped
, m_drawOpacity
);
179 if (m_owningLayer
->showDebugBorders()) {
180 SkColor color
= forReplica
? DebugColors::SurfaceReplicaBorderColor() : DebugColors::SurfaceBorderColor();
181 float width
= forReplica
? DebugColors::SurfaceReplicaBorderWidth(m_owningLayer
->layerTreeImpl()) : DebugColors::SurfaceBorderWidth(m_owningLayer
->layerTreeImpl());
182 scoped_ptr
<DebugBorderDrawQuad
> debugBorderQuad
= DebugBorderDrawQuad::Create();
183 debugBorderQuad
->SetNew(sharedQuadState
, contentRect(), color
, width
);
184 quadSink
.append(debugBorderQuad
.PassAs
<DrawQuad
>(), appendQuadsData
);
187 // FIXME: By using the same RenderSurfaceImpl for both the content and its reflection,
188 // it's currently not possible to apply a separate mask to the reflection layer
189 // or correctly handle opacity in reflections (opacity must be applied after drawing
190 // both the layer and its reflection). The solution is to introduce yet another RenderSurfaceImpl
191 // to draw the layer and its reflection in. For now we only apply a separate reflection
192 // mask if the contents don't have a mask of their own.
193 LayerImpl
* maskLayer
= m_owningLayer
->maskLayer();
194 if (maskLayer
&& (!maskLayer
->drawsContent() || maskLayer
->bounds().IsEmpty()))
197 if (!maskLayer
&& forReplica
) {
198 maskLayer
= m_owningLayer
->replicaLayer()->maskLayer();
199 if (maskLayer
&& (!maskLayer
->drawsContent() || maskLayer
->bounds().IsEmpty()))
203 gfx::RectF
maskUVRect(0.0f
, 0.0f
, 1.0f
, 1.0f
);
205 gfx::Vector2dF owningLayerDrawScale
= MathUtil::computeTransform2dScaleComponents(m_owningLayer
->drawTransform(), 1.f
);
206 gfx::SizeF unclippedSurfaceSize
= gfx::ScaleSize(
207 m_owningLayer
->contentBounds(),
208 owningLayerDrawScale
.x(),
209 owningLayerDrawScale
.y());
210 // This assumes that the owning layer clips its subtree when a mask is
212 DCHECK(gfx::RectF(unclippedSurfaceSize
).Contains(contentRect()));
214 float uvScaleX
= contentRect().width() / unclippedSurfaceSize
.width();
215 float uvScaleY
= contentRect().height() / unclippedSurfaceSize
.height();
217 maskUVRect
= gfx::RectF(
218 static_cast<float>(contentRect().x()) / contentRect().width() * uvScaleX
,
219 static_cast<float>(contentRect().y()) / contentRect().height() * uvScaleY
,
224 ResourceProvider::ResourceId maskResourceId
= maskLayer
? maskLayer
->contentsResourceId() : 0;
225 gfx::Rect contentsChangedSinceLastFrame
= contentsChanged() ? m_contentRect
: gfx::Rect();
227 scoped_ptr
<RenderPassDrawQuad
> quad
= RenderPassDrawQuad::Create();
228 quad
->SetNew(sharedQuadState
, contentRect(), renderPassId
, forReplica
, maskResourceId
, contentsChangedSinceLastFrame
, maskUVRect
, m_owningLayer
->filters(), m_owningLayer
->filter(), m_owningLayer
->backgroundFilters());
229 quadSink
.append(quad
.PassAs
<DrawQuad
>(), appendQuadsData
);