1 // Copyright 2010 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 #include "ImageLayerChromium.h"
9 #include "base/compiler_specific.h"
10 #include "CCLayerTreeHost.h"
11 #include "CCTextureUpdateQueue.h"
12 #include "LayerTextureUpdater.h"
13 #include "PlatformColor.h"
17 class ImageLayerTextureUpdater
: public LayerTextureUpdater
{
19 class Texture
: public LayerTextureUpdater::Texture
{
21 Texture(ImageLayerTextureUpdater
* textureUpdater
, scoped_ptr
<CCPrioritizedTexture
> texture
)
22 : LayerTextureUpdater::Texture(texture
.Pass())
23 , m_textureUpdater(textureUpdater
)
27 virtual void update(CCTextureUpdateQueue
& queue
, const IntRect
& sourceRect
, const IntSize
& destOffset
, bool partialUpdate
, CCRenderingStats
&) OVERRIDE
29 textureUpdater()->updateTexture(queue
, texture(), sourceRect
, destOffset
, partialUpdate
);
33 ImageLayerTextureUpdater
* textureUpdater() { return m_textureUpdater
; }
35 ImageLayerTextureUpdater
* m_textureUpdater
;
38 static PassRefPtr
<ImageLayerTextureUpdater
> create()
40 return adoptRef(new ImageLayerTextureUpdater());
43 virtual ~ImageLayerTextureUpdater() { }
45 virtual PassOwnPtr
<LayerTextureUpdater::Texture
> createTexture(
46 CCPrioritizedTextureManager
* manager
) OVERRIDE
48 return adoptPtr(new Texture(this, CCPrioritizedTexture::create(manager
)));
51 virtual SampledTexelFormat
sampledTexelFormat(GC3Denum textureFormat
) OVERRIDE
53 return PlatformColor::sameComponentOrder(textureFormat
) ?
54 LayerTextureUpdater::SampledTexelFormatRGBA
: LayerTextureUpdater::SampledTexelFormatBGRA
;
57 void updateTexture(CCTextureUpdateQueue
& queue
, CCPrioritizedTexture
* texture
, const IntRect
& sourceRect
, const IntSize
& destOffset
, bool partialUpdate
)
59 // Source rect should never go outside the image pixels, even if this
60 // is requested because the texture extends outside the image.
61 IntRect clippedSourceRect
= sourceRect
;
62 IntRect imageRect
= IntRect(0, 0, m_bitmap
.width(), m_bitmap
.height());
63 clippedSourceRect
.intersect(imageRect
);
65 IntSize clippedDestOffset
= destOffset
+ IntSize(clippedSourceRect
.location() - sourceRect
.location());
67 TextureUploader::Parameters upload
= { texture
, &m_bitmap
, NULL
, { imageRect
, clippedSourceRect
, clippedDestOffset
} };
69 queue
.appendPartialUpload(upload
);
71 queue
.appendFullUpload(upload
);
74 void setBitmap(const SkBitmap
& bitmap
)
80 ImageLayerTextureUpdater() { }
85 scoped_refptr
<ImageLayerChromium
> ImageLayerChromium::create()
87 return make_scoped_refptr(new ImageLayerChromium());
90 ImageLayerChromium::ImageLayerChromium()
91 : TiledLayerChromium()
95 ImageLayerChromium::~ImageLayerChromium()
99 void ImageLayerChromium::setBitmap(const SkBitmap
& bitmap
)
101 // setBitmap() currently gets called whenever there is any
102 // style change that affects the layer even if that change doesn't
103 // affect the actual contents of the image (e.g. a CSS animation).
104 // With this check in place we avoid unecessary texture uploads.
105 if (bitmap
.pixelRef() && bitmap
.pixelRef() == m_bitmap
.pixelRef())
112 void ImageLayerChromium::setTexturePriorities(const CCPriorityCalculator
& priorityCalc
)
114 // Update the tile data before creating all the layer's tiles.
115 updateTileSizeAndTilingOption();
117 TiledLayerChromium::setTexturePriorities(priorityCalc
);
120 void ImageLayerChromium::update(CCTextureUpdateQueue
& queue
, const CCOcclusionTracker
* occlusion
, CCRenderingStats
& stats
)
122 createTextureUpdaterIfNeeded();
123 if (m_needsDisplay
) {
124 m_textureUpdater
->setBitmap(m_bitmap
);
125 updateTileSizeAndTilingOption();
126 invalidateContentRect(IntRect(IntPoint(), contentBounds()));
127 m_needsDisplay
= false;
129 TiledLayerChromium::update(queue
, occlusion
, stats
);
132 void ImageLayerChromium::createTextureUpdaterIfNeeded()
134 if (m_textureUpdater
)
137 m_textureUpdater
= ImageLayerTextureUpdater::create();
138 GC3Denum textureFormat
= layerTreeHost()->rendererCapabilities().bestTextureFormat
;
139 setTextureFormat(textureFormat
);
140 setSampledTexelFormat(textureUpdater()->sampledTexelFormat(textureFormat
));
143 LayerTextureUpdater
* ImageLayerChromium::textureUpdater() const
145 return m_textureUpdater
.get();
148 IntSize
ImageLayerChromium::contentBounds() const
150 return IntSize(m_bitmap
.width(), m_bitmap
.height());
153 bool ImageLayerChromium::drawsContent() const
155 return !m_bitmap
.isNull() && TiledLayerChromium::drawsContent();
158 bool ImageLayerChromium::needsContentsScale() const
160 // Contents scale is not need for image layer because this can be done in compositor more efficiently.