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.
7 #if USE(ACCELERATED_COMPOSITING)
9 #include "CCTiledLayerImpl.h"
11 #include "base/stringprintf.h"
12 #include "CCAppendQuadsData.h"
13 #include "CCCheckerboardDrawQuad.h"
14 #include "CCDebugBorderDrawQuad.h"
15 #include "CCLayerTilingData.h"
16 #include "CCMathUtil.h"
17 #include "CCQuadSink.h"
18 #include "CCSolidColorDrawQuad.h"
19 #include "CCTileDrawQuad.h"
20 #include "FloatQuad.h"
21 #include "GraphicsContext3D.h"
25 using WebKit::WebTransformationMatrix
;
29 static const int debugTileBorderWidth
= 1;
30 static const int debugTileBorderAlpha
= 100;
31 static const int debugTileBorderColorRed
= 80;
32 static const int debugTileBorderColorGreen
= 200;
33 static const int debugTileBorderColorBlue
= 200;
34 static const int debugTileBorderMissingTileColorRed
= 255;
35 static const int debugTileBorderMissingTileColorGreen
= 0;
36 static const int debugTileBorderMissingTileColorBlue
= 0;
38 class DrawableTile
: public CCLayerTilingData::Tile
{
39 WTF_MAKE_NONCOPYABLE(DrawableTile
);
41 static PassOwnPtr
<DrawableTile
> create() { return adoptPtr(new DrawableTile()); }
43 CCResourceProvider::ResourceId
resourceId() const { return m_resourceId
; }
44 void setResourceId(CCResourceProvider::ResourceId resourceId
) { m_resourceId
= resourceId
; }
47 DrawableTile() : m_resourceId(0) { }
49 CCResourceProvider::ResourceId m_resourceId
;
52 CCTiledLayerImpl::CCTiledLayerImpl(int id
)
55 , m_contentsSwizzled(false)
59 CCTiledLayerImpl::~CCTiledLayerImpl()
63 CCResourceProvider::ResourceId
CCTiledLayerImpl::contentsResourceId() const
65 // This function is only valid for single texture layers, e.g. masks.
67 ASSERT(m_tiler
->numTilesX() == 1);
68 ASSERT(m_tiler
->numTilesY() == 1);
70 DrawableTile
* tile
= tileAt(0, 0);
71 CCResourceProvider::ResourceId resourceId
= tile
? tile
->resourceId() : 0;
77 void CCTiledLayerImpl::dumpLayerProperties(std::string
* str
, int indent
) const
79 str
->append(indentString(indent
));
80 base::StringAppendF(str
, "skipsDraw: %d\n", (!m_tiler
|| m_skipsDraw
));
81 CCLayerImpl::dumpLayerProperties(str
, indent
);
84 bool CCTiledLayerImpl::hasTileAt(int i
, int j
) const
86 return m_tiler
->tileAt(i
, j
);
89 bool CCTiledLayerImpl::hasTextureIdForTileAt(int i
, int j
) const
91 return hasTileAt(i
, j
) && tileAt(i
, j
)->resourceId();
94 DrawableTile
* CCTiledLayerImpl::tileAt(int i
, int j
) const
96 return static_cast<DrawableTile
*>(m_tiler
->tileAt(i
, j
));
99 DrawableTile
* CCTiledLayerImpl::createTile(int i
, int j
)
101 OwnPtr
<DrawableTile
> tile(DrawableTile::create());
102 DrawableTile
* addedTile
= tile
.get();
103 m_tiler
->addTile(tile
.release(), i
, j
);
107 void CCTiledLayerImpl::appendQuads(CCQuadSink
& quadSink
, CCAppendQuadsData
& appendQuadsData
)
109 const IntRect
& contentRect
= visibleContentRect();
111 if (!m_tiler
|| m_tiler
->hasEmptyBounds() || contentRect
.isEmpty())
114 CCSharedQuadState
* sharedQuadState
= quadSink
.useSharedQuadState(createSharedQuadState());
115 appendDebugBorderQuad(quadSink
, sharedQuadState
, appendQuadsData
);
117 int left
, top
, right
, bottom
;
118 m_tiler
->contentRectToTileIndices(contentRect
, left
, top
, right
, bottom
);
120 if (hasDebugBorders()) {
121 for (int j
= top
; j
<= bottom
; ++j
) {
122 for (int i
= left
; i
<= right
; ++i
) {
123 DrawableTile
* tile
= tileAt(i
, j
);
124 IntRect tileRect
= m_tiler
->tileBounds(i
, j
);
127 if (m_skipsDraw
|| !tile
|| !tile
->resourceId())
128 borderColor
= SkColorSetARGB(debugTileBorderAlpha
, debugTileBorderMissingTileColorRed
, debugTileBorderMissingTileColorGreen
, debugTileBorderMissingTileColorBlue
);
130 borderColor
= SkColorSetARGB(debugTileBorderAlpha
, debugTileBorderColorRed
, debugTileBorderColorGreen
, debugTileBorderColorBlue
);
131 quadSink
.append(CCDebugBorderDrawQuad::create(sharedQuadState
, tileRect
, borderColor
, debugTileBorderWidth
), appendQuadsData
);
139 for (int j
= top
; j
<= bottom
; ++j
) {
140 for (int i
= left
; i
<= right
; ++i
) {
141 DrawableTile
* tile
= tileAt(i
, j
);
142 IntRect tileRect
= m_tiler
->tileBounds(i
, j
);
143 IntRect displayRect
= tileRect
;
144 tileRect
.intersect(contentRect
);
147 if (tileRect
.isEmpty())
150 if (!tile
|| !tile
->resourceId()) {
151 if (drawCheckerboardForMissingTiles())
152 appendQuadsData
.hadMissingTiles
|= quadSink
.append(CCCheckerboardDrawQuad::create(sharedQuadState
, tileRect
), appendQuadsData
);
154 appendQuadsData
.hadMissingTiles
|= quadSink
.append(CCSolidColorDrawQuad::create(sharedQuadState
, tileRect
, backgroundColor()), appendQuadsData
);
158 IntRect tileOpaqueRect
= tile
->opaqueRect();
159 tileOpaqueRect
.intersect(contentRect
);
161 // Keep track of how the top left has moved, so the texture can be
162 // offset the same amount.
163 IntSize displayOffset
= tileRect
.minXMinYCorner() - displayRect
.minXMinYCorner();
164 IntPoint textureOffset
= m_tiler
->textureOffset(i
, j
) + displayOffset
;
165 float tileWidth
= static_cast<float>(m_tiler
->tileSize().width());
166 float tileHeight
= static_cast<float>(m_tiler
->tileSize().height());
167 IntSize
textureSize(tileWidth
, tileHeight
);
169 bool clipped
= false;
170 FloatQuad visibleContentInTargetQuad
= CCMathUtil::mapQuad(drawTransform(), FloatQuad(visibleContentRect()), clipped
);
171 bool isAxisAlignedInTarget
= !clipped
&& visibleContentInTargetQuad
.isRectilinear();
172 bool useAA
= m_tiler
->hasBorderTexels() && !isAxisAlignedInTarget
;
174 bool leftEdgeAA
= !i
&& useAA
;
175 bool topEdgeAA
= !j
&& useAA
;
176 bool rightEdgeAA
= i
== m_tiler
->numTilesX() - 1 && useAA
;
177 bool bottomEdgeAA
= j
== m_tiler
->numTilesY() - 1 && useAA
;
179 const GC3Dint textureFilter
= m_tiler
->hasBorderTexels() ? GraphicsContext3D::LINEAR
: GraphicsContext3D::NEAREST
;
180 quadSink
.append(CCTileDrawQuad::create(sharedQuadState
, tileRect
, tileOpaqueRect
, tile
->resourceId(), textureOffset
, textureSize
, textureFilter
, contentsSwizzled(), leftEdgeAA
, topEdgeAA
, rightEdgeAA
, bottomEdgeAA
), appendQuadsData
);
185 void CCTiledLayerImpl::setTilingData(const CCLayerTilingData
& tiler
)
190 m_tiler
= CCLayerTilingData::create(tiler
.tileSize(), tiler
.hasBorderTexels() ? CCLayerTilingData::HasBorderTexels
: CCLayerTilingData::NoBorderTexels
);
194 void CCTiledLayerImpl::pushTileProperties(int i
, int j
, CCResourceProvider::ResourceId resourceId
, const IntRect
& opaqueRect
)
196 DrawableTile
* tile
= tileAt(i
, j
);
198 tile
= createTile(i
, j
);
199 tile
->setResourceId(resourceId
);
200 tile
->setOpaqueRect(opaqueRect
);
203 Region
CCTiledLayerImpl::visibleContentOpaqueRegion() const
208 return visibleContentRect();
209 return m_tiler
->opaqueRegionInContentRect(visibleContentRect());
212 void CCTiledLayerImpl::didLoseContext()
217 } // namespace WebCore
219 #endif // USE(ACCELERATED_COMPOSITING)