blob: 6be40d46ce31b65fd993027ae921fd3f90c13147 [file] [log] [blame]
[email protected]94f206c12012-08-25 00:09:141// 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.
4
[email protected]d50c6862012-10-23 02:08:315#include "cc/render_surface_impl.h"
[email protected]94f206c12012-08-25 00:09:146
[email protected]ac7c7f52012-11-08 06:26:507#include <algorithm>
8
[email protected]4456eee22012-10-19 18:16:389#include "base/logging.h"
10#include "base/stringprintf.h"
[email protected]aa0a9d32012-10-24 01:58:1011#include "cc/damage_tracker.h"
12#include "cc/debug_border_draw_quad.h"
[email protected]08d06fa2012-11-16 00:06:3813#include "cc/debug_colors.h"
[email protected]aa0a9d32012-10-24 01:58:1014#include "cc/delegated_renderer_layer_impl.h"
[email protected]d50c6862012-10-23 02:08:3115#include "cc/layer_impl.h"
[email protected]55a124d02012-10-22 03:07:1316#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"
[email protected]4456eee22012-10-19 18:16:3821#include "cc/shared_quad_state.h"
[email protected]55761e62012-11-21 18:55:5822#include "third_party/skia/include/core/SkImageFilter.h"
[email protected]aad0a0072012-11-01 18:15:5823#include "ui/gfx/rect_conversions.h"
[email protected]c8686a02012-11-27 08:29:0024#include "ui/gfx/transform.h"
[email protected]94f206c12012-08-25 00:09:1425
[email protected]9c88e562012-09-14 22:21:3026namespace cc {
[email protected]94f206c12012-08-25 00:09:1427
[email protected]96baf3e2012-10-22 23:09:5528RenderSurfaceImpl::RenderSurfaceImpl(LayerImpl* owningLayer)
[email protected]94f206c12012-08-25 00:09:1429 : m_owningLayer(owningLayer)
30 , m_surfacePropertyChanged(false)
31 , m_drawOpacity(1)
32 , m_drawOpacityIsAnimating(false)
33 , m_targetSurfaceTransformsAreAnimating(false)
34 , m_screenSpaceTransformsAreAnimating(false)
[email protected]dc462d782012-11-21 21:43:0135 , m_isClipped(false)
[email protected]94f206c12012-08-25 00:09:1436 , m_nearestAncestorThatMovesPixels(0)
37 , m_targetRenderSurfaceLayerIndexHistory(0)
38 , m_currentLayerIndexHistory(0)
39{
[email protected]96baf3e2012-10-22 23:09:5540 m_damageTracker = DamageTracker::create();
[email protected]94f206c12012-08-25 00:09:1441}
42
[email protected]96baf3e2012-10-22 23:09:5543RenderSurfaceImpl::~RenderSurfaceImpl()
[email protected]94f206c12012-08-25 00:09:1444{
45}
46
[email protected]aad0a0072012-11-01 18:15:5847gfx::RectF RenderSurfaceImpl::drawableContentRect() const
[email protected]94f206c12012-08-25 00:09:1448{
[email protected]aad0a0072012-11-01 18:15:5849 gfx::RectF drawableContentRect = MathUtil::mapClippedRect(m_drawTransform, m_contentRect);
[email protected]94f206c12012-08-25 00:09:1450 if (m_owningLayer->hasReplica())
[email protected]aad0a0072012-11-01 18:15:5851 drawableContentRect.Union(MathUtil::mapClippedRect(m_replicaDrawTransform, m_contentRect));
[email protected]94f206c12012-08-25 00:09:1452
53 return drawableContentRect;
54}
55
[email protected]96baf3e2012-10-22 23:09:5556std::string RenderSurfaceImpl::name() const
[email protected]94f206c12012-08-25 00:09:1457{
[email protected]96baf3e2012-10-22 23:09:5558 return base::StringPrintf("RenderSurfaceImpl(id=%i,owner=%s)", m_owningLayer->id(), m_owningLayer->debugName().data());
[email protected]94f206c12012-08-25 00:09:1459}
60
[email protected]515e8d232012-09-10 19:15:2761static std::string indentString(int indent)
[email protected]94f206c12012-08-25 00:09:1462{
[email protected]515e8d232012-09-10 19:15:2763 std::string str;
[email protected]94f206c12012-08-25 00:09:1464 for (int i = 0; i != indent; ++i)
[email protected]515e8d232012-09-10 19:15:2765 str.append(" ");
66 return str;
[email protected]94f206c12012-08-25 00:09:1467}
68
[email protected]96baf3e2012-10-22 23:09:5569void RenderSurfaceImpl::dumpSurface(std::string* str, int indent) const
[email protected]94f206c12012-08-25 00:09:1470{
[email protected]515e8d232012-09-10 19:15:2771 std::string indentStr = indentString(indent);
72 str->append(indentStr);
73 base::StringAppendF(str, "%s\n", name().data());
[email protected]94f206c12012-08-25 00:09:1474
[email protected]515e8d232012-09-10 19:15:2775 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());
[email protected]94f206c12012-08-25 00:09:1478
[email protected]515e8d232012-09-10 19:15:2779 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",
[email protected]c8686a02012-11-27 08:29:0081 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));
[email protected]94f206c12012-08-25 00:09:1485
[email protected]515e8d232012-09-10 19:15:2786 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());
[email protected]94f206c12012-08-25 00:09:1490}
91
[email protected]96baf3e2012-10-22 23:09:5592int RenderSurfaceImpl::owningLayerId() const
[email protected]94f206c12012-08-25 00:09:1493{
94 return m_owningLayer ? m_owningLayer->id() : 0;
95}
96
97
[email protected]aad0a0072012-11-01 18:15:5898void RenderSurfaceImpl::setClipRect(const gfx::Rect& clipRect)
[email protected]94f206c12012-08-25 00:09:1499{
100 if (m_clipRect == clipRect)
101 return;
102
103 m_surfacePropertyChanged = true;
104 m_clipRect = clipRect;
105}
106
[email protected]96baf3e2012-10-22 23:09:55107bool RenderSurfaceImpl::contentsChanged() const
[email protected]94f206c12012-08-25 00:09:14108{
[email protected]aad0a0072012-11-01 18:15:58109 return !m_damageTracker->currentDamageRect().IsEmpty();
[email protected]94f206c12012-08-25 00:09:14110}
111
[email protected]aad0a0072012-11-01 18:15:58112void RenderSurfaceImpl::setContentRect(const gfx::Rect& contentRect)
[email protected]94f206c12012-08-25 00:09:14113{
114 if (m_contentRect == contentRect)
115 return;
116
117 m_surfacePropertyChanged = true;
118 m_contentRect = contentRect;
119}
120
[email protected]96baf3e2012-10-22 23:09:55121bool RenderSurfaceImpl::surfacePropertyChanged() const
[email protected]94f206c12012-08-25 00:09:14122{
123 // Surface property changes are tracked as follows:
124 //
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.
127 //
128 // - all other property changes come from the owning layer (or some ancestor layer
129 // that propagates its change to the owning layer).
130 //
[email protected]1d993172012-10-18 18:15:04131 DCHECK(m_owningLayer);
[email protected]94f206c12012-08-25 00:09:14132 return m_surfacePropertyChanged || m_owningLayer->layerPropertyChanged();
133}
134
[email protected]96baf3e2012-10-22 23:09:55135bool RenderSurfaceImpl::surfacePropertyChangedOnlyFromDescendant() const
[email protected]94f206c12012-08-25 00:09:14136{
137 return m_surfacePropertyChanged && !m_owningLayer->layerPropertyChanged();
138}
139
[email protected]96baf3e2012-10-22 23:09:55140void RenderSurfaceImpl::addContributingDelegatedRenderPassLayer(LayerImpl* layer)
[email protected]7d929c02012-09-20 17:26:57141{
[email protected]1d993172012-10-18 18:15:04142 DCHECK(std::find(m_layerList.begin(), m_layerList.end(), layer) != m_layerList.end());
[email protected]96baf3e2012-10-22 23:09:55143 DelegatedRendererLayerImpl* delegatedRendererLayer = static_cast<DelegatedRendererLayerImpl*>(layer);
[email protected]d58499a2012-10-09 22:27:47144 m_contributingDelegatedRenderPassLayerList.push_back(delegatedRendererLayer);
[email protected]7d929c02012-09-20 17:26:57145}
146
[email protected]96baf3e2012-10-22 23:09:55147void RenderSurfaceImpl::clearLayerLists()
[email protected]7d929c02012-09-20 17:26:57148{
149 m_layerList.clear();
150 m_contributingDelegatedRenderPassLayerList.clear();
151}
152
[email protected]aad0a0072012-11-01 18:15:58153static inline gfx::Rect computeClippedRectInTarget(const LayerImpl* owningLayer)
[email protected]94f206c12012-08-25 00:09:14154{
[email protected]1d993172012-10-18 18:15:04155 DCHECK(owningLayer->parent());
[email protected]94f206c12012-08-25 00:09:14156
[email protected]96baf3e2012-10-22 23:09:55157 const LayerImpl* renderTarget = owningLayer->parent()->renderTarget();
158 const RenderSurfaceImpl* self = owningLayer->renderSurface();
[email protected]94f206c12012-08-25 00:09:14159
[email protected]aad0a0072012-11-01 18:15:58160 gfx::Rect clippedRectInTarget = self->clipRect();
[email protected]94f206c12012-08-25 00:09:14161 if (owningLayer->backgroundFilters().hasFilterThatMovesPixels()) {
162 // If the layer has background filters that move pixels, we cannot scissor as tightly.
163 // FIXME: this should be able to be a tighter scissor, perhaps expanded by the filter outsets?
164 clippedRectInTarget = renderTarget->renderSurface()->contentRect();
[email protected]aad0a0072012-11-01 18:15:58165 } else if (clippedRectInTarget.IsEmpty()) {
[email protected]94f206c12012-08-25 00:09:14166 // For surfaces, empty clipRect means that the surface does not clip anything.
[email protected]aad0a0072012-11-01 18:15:58167 clippedRectInTarget = renderTarget->renderSurface()->contentRect();
168 clippedRectInTarget.Intersect(gfx::ToEnclosingRect(self->drawableContentRect()));
[email protected]94f206c12012-08-25 00:09:14169 } else
[email protected]aad0a0072012-11-01 18:15:58170 clippedRectInTarget.Intersect(gfx::ToEnclosingRect(self->drawableContentRect()));
[email protected]94f206c12012-08-25 00:09:14171 return clippedRectInTarget;
172}
173
[email protected]96baf3e2012-10-22 23:09:55174RenderPass::Id RenderSurfaceImpl::renderPassId()
[email protected]0f077a52012-09-08 01:45:24175{
176 int layerId = m_owningLayer->id();
177 int subId = 0;
[email protected]1d993172012-10-18 18:15:04178 DCHECK(layerId > 0);
[email protected]96baf3e2012-10-22 23:09:55179 return RenderPass::Id(layerId, subId);
[email protected]0f077a52012-09-08 01:45:24180}
181
[email protected]96baf3e2012-10-22 23:09:55182void RenderSurfaceImpl::appendRenderPasses(RenderPassSink& passSink)
[email protected]467b3612012-08-28 07:41:16183{
[email protected]7d929c02012-09-20 17:26:57184 for (size_t i = 0; i < m_contributingDelegatedRenderPassLayerList.size(); ++i)
185 m_contributingDelegatedRenderPassLayerList[i]->appendContributingRenderPasses(passSink);
186
[email protected]f57bbc02012-11-21 07:02:15187 scoped_ptr<RenderPass> pass = RenderPass::Create();
188 pass->SetNew(renderPassId(), m_contentRect, m_damageTracker->currentDamageRect(), m_screenSpaceTransform);
189 pass->filters = m_owningLayer->filters();
[email protected]4c3432b2012-11-21 18:47:13190 SkRefCnt_SafeAssign(pass->filter, m_owningLayer->filter());
[email protected]f57bbc02012-11-21 07:02:15191 pass->background_filters = m_owningLayer->backgroundFilters();
[email protected]87cea5372012-09-26 18:59:56192 passSink.appendRenderPass(pass.Pass());
[email protected]467b3612012-08-28 07:41:16193}
194
[email protected]96baf3e2012-10-22 23:09:55195void RenderSurfaceImpl::appendQuads(QuadSink& quadSink, AppendQuadsData& appendQuadsData, bool forReplica, RenderPass::Id renderPassId)
[email protected]94f206c12012-08-25 00:09:14196{
[email protected]1d993172012-10-18 18:15:04197 DCHECK(!forReplica || m_owningLayer->hasReplica());
[email protected]94f206c12012-08-25 00:09:14198
[email protected]aad0a0072012-11-01 18:15:58199 gfx::Rect clippedRectInTarget = computeClippedRectInTarget(m_owningLayer);
[email protected]c8686a02012-11-27 08:29:00200 const gfx::Transform& drawTransform = forReplica ? m_replicaDrawTransform : m_drawTransform;
[email protected]cb7af742012-11-21 04:02:24201 SharedQuadState* sharedQuadState = quadSink.useSharedQuadState(SharedQuadState::Create());
[email protected]dc462d782012-11-21 21:43:01202 sharedQuadState->SetAll(drawTransform, m_contentRect, clippedRectInTarget, m_clipRect, m_isClipped, m_drawOpacity);
[email protected]94f206c12012-08-25 00:09:14203
[email protected]3dce37232012-11-15 01:47:44204 if (m_owningLayer->showDebugBorders()) {
[email protected]08d06fa2012-11-16 00:06:38205 SkColor color = forReplica ? DebugColors::SurfaceReplicaBorderColor() : DebugColors::SurfaceBorderColor();
206 float width = forReplica ? DebugColors::SurfaceReplicaBorderWidth(m_owningLayer->layerTreeHostImpl()) : DebugColors::SurfaceBorderWidth(m_owningLayer->layerTreeHostImpl());
[email protected]c22418b2012-11-20 23:06:26207 scoped_ptr<DebugBorderDrawQuad> debugBorderQuad = DebugBorderDrawQuad::Create();
208 debugBorderQuad->SetNew(sharedQuadState, contentRect(), color, width);
209 quadSink.append(debugBorderQuad.PassAs<DrawQuad>(), appendQuadsData);
[email protected]94f206c12012-08-25 00:09:14210 }
211
[email protected]96baf3e2012-10-22 23:09:55212 // FIXME: By using the same RenderSurfaceImpl for both the content and its reflection,
[email protected]94f206c12012-08-25 00:09:14213 // it's currently not possible to apply a separate mask to the reflection layer
214 // or correctly handle opacity in reflections (opacity must be applied after drawing
[email protected]96baf3e2012-10-22 23:09:55215 // both the layer and its reflection). The solution is to introduce yet another RenderSurfaceImpl
[email protected]94f206c12012-08-25 00:09:14216 // to draw the layer and its reflection in. For now we only apply a separate reflection
217 // mask if the contents don't have a mask of their own.
[email protected]96baf3e2012-10-22 23:09:55218 LayerImpl* maskLayer = m_owningLayer->maskLayer();
[email protected]aad0a0072012-11-01 18:15:58219 if (maskLayer && (!maskLayer->drawsContent() || maskLayer->bounds().IsEmpty()))
[email protected]94f206c12012-08-25 00:09:14220 maskLayer = 0;
221
222 if (!maskLayer && forReplica) {
223 maskLayer = m_owningLayer->replicaLayer()->maskLayer();
[email protected]aad0a0072012-11-01 18:15:58224 if (maskLayer && (!maskLayer->drawsContent() || maskLayer->bounds().IsEmpty()))
[email protected]94f206c12012-08-25 00:09:14225 maskLayer = 0;
226 }
227
228 float maskTexCoordScaleX = 1;
229 float maskTexCoordScaleY = 1;
[email protected]058a724a2012-10-30 18:37:02230 float maskTexCoordOffsetX = 0;
231 float maskTexCoordOffsetY = 0;
[email protected]94f206c12012-08-25 00:09:14232 if (maskLayer) {
[email protected]904e9132012-11-01 00:12:47233 maskTexCoordScaleX = contentRect().width() / maskLayer->contentsScaleX() / maskLayer->bounds().width();
234 maskTexCoordScaleY = contentRect().height() / maskLayer->contentsScaleY() / maskLayer->bounds().height();
[email protected]94f206c12012-08-25 00:09:14235 maskTexCoordOffsetX = static_cast<float>(contentRect().x()) / contentRect().width() * maskTexCoordScaleX;
236 maskTexCoordOffsetY = static_cast<float>(contentRect().y()) / contentRect().height() * maskTexCoordScaleY;
237 }
238
[email protected]96baf3e2012-10-22 23:09:55239 ResourceProvider::ResourceId maskResourceId = maskLayer ? maskLayer->contentsResourceId() : 0;
[email protected]aad0a0072012-11-01 18:15:58240 gfx::Rect contentsChangedSinceLastFrame = contentsChanged() ? m_contentRect : gfx::Rect();
[email protected]94f206c12012-08-25 00:09:14241
[email protected]c22418b2012-11-20 23:06:26242 scoped_ptr<RenderPassDrawQuad> quad = RenderPassDrawQuad::Create();
243 quad->SetNew(sharedQuadState, contentRect(), renderPassId, forReplica, maskResourceId, contentsChangedSinceLastFrame,
244 maskTexCoordScaleX, maskTexCoordScaleY, maskTexCoordOffsetX, maskTexCoordOffsetY);
245 quadSink.append(quad.PassAs<DrawQuad>(), appendQuadsData);
[email protected]94f206c12012-08-25 00:09:14246}
247
[email protected]bc5e77c2012-11-05 20:00:49248} // namespace cc