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 #if USE(ACCELERATED_COMPOSITING)
9 #include "ImageLayerChromium.h"
11 #include "CCLayerTreeHost.h"
12 #include "LayerTextureUpdater.h"
13 #include "PlatformColor.h"
17 class ImageLayerTextureUpdater
: public LayerTextureUpdater
{
19 class Texture
: public LayerTextureUpdater::Texture
{
21 Texture(ImageLayerTextureUpdater
* textureUpdater
, PassOwnPtr
<CCPrioritizedTexture
> texture
)
22 : LayerTextureUpdater::Texture(texture
)
23 , m_textureUpdater(textureUpdater
)
27 virtual void updateRect(CCResourceProvider
* resourceProvider
, const IntRect
& sourceRect
, const IntSize
& destOffset
) OVERRIDE
29 textureUpdater()->updateTextureRect(resourceProvider
, texture(), sourceRect
, destOffset
);
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(CCPrioritizedTextureManager
* manager
)
47 return adoptPtr(new Texture(this, CCPrioritizedTexture::create(manager
)));
50 virtual SampledTexelFormat
sampledTexelFormat(GC3Denum textureFormat
) OVERRIDE
52 return PlatformColor::sameComponentOrder(textureFormat
) ?
53 LayerTextureUpdater::SampledTexelFormatRGBA
: LayerTextureUpdater::SampledTexelFormatBGRA
;
56 void updateTextureRect(CCResourceProvider
* resourceProvider
, CCPrioritizedTexture
* texture
, const IntRect
& sourceRect
, const IntSize
& destOffset
)
58 // Source rect should never go outside the image pixels, even if this
59 // is requested because the texture extends outside the image.
60 IntRect clippedSourceRect
= sourceRect
;
61 IntRect imageRect
= IntRect(0, 0, m_bitmap
.width(), m_bitmap
.height());
62 clippedSourceRect
.intersect(imageRect
);
64 IntSize clippedDestOffset
= destOffset
+ IntSize(clippedSourceRect
.location() - sourceRect
.location());
66 SkAutoLockPixels
lock(m_bitmap
);
67 texture
->upload(resourceProvider
, static_cast<const uint8_t*>(m_bitmap
.getPixels()), imageRect
, clippedSourceRect
, clippedDestOffset
);
70 void setBitmap(const SkBitmap
& bitmap
)
76 ImageLayerTextureUpdater() { }
81 PassRefPtr
<ImageLayerChromium
> ImageLayerChromium::create()
83 return adoptRef(new ImageLayerChromium());
86 ImageLayerChromium::ImageLayerChromium()
87 : TiledLayerChromium()
91 ImageLayerChromium::~ImageLayerChromium()
95 void ImageLayerChromium::setBitmap(const SkBitmap
& bitmap
)
97 // setBitmap() currently gets called whenever there is any
98 // style change that affects the layer even if that change doesn't
99 // affect the actual contents of the image (e.g. a CSS animation).
100 // With this check in place we avoid unecessary texture uploads.
101 if (bitmap
.pixelRef() && bitmap
.pixelRef() == m_bitmap
.pixelRef())
108 void ImageLayerChromium::setTexturePriorities(const CCPriorityCalculator
& priorityCalc
)
110 // Update the tile data before creating all the layer's tiles.
111 updateTileSizeAndTilingOption();
113 TiledLayerChromium::setTexturePriorities(priorityCalc
);
116 void ImageLayerChromium::update(CCTextureUpdateQueue
& queue
, const CCOcclusionTracker
* occlusion
, CCRenderingStats
& stats
)
118 createTextureUpdaterIfNeeded();
119 if (m_needsDisplay
) {
120 m_textureUpdater
->setBitmap(m_bitmap
);
121 updateTileSizeAndTilingOption();
122 invalidateContentRect(IntRect(IntPoint(), contentBounds()));
123 m_needsDisplay
= false;
125 TiledLayerChromium::update(queue
, occlusion
, stats
);
128 void ImageLayerChromium::createTextureUpdaterIfNeeded()
130 if (m_textureUpdater
)
133 m_textureUpdater
= ImageLayerTextureUpdater::create();
134 GC3Denum textureFormat
= layerTreeHost()->rendererCapabilities().bestTextureFormat
;
135 setTextureFormat(textureFormat
);
136 setSampledTexelFormat(textureUpdater()->sampledTexelFormat(textureFormat
));
139 LayerTextureUpdater
* ImageLayerChromium::textureUpdater() const
141 return m_textureUpdater
.get();
144 IntSize
ImageLayerChromium::contentBounds() const
146 return IntSize(m_bitmap
.width(), m_bitmap
.height());
149 bool ImageLayerChromium::drawsContent() const
151 return !m_bitmap
.isNull() && TiledLayerChromium::drawsContent();
154 bool ImageLayerChromium::needsContentsScale() const
156 // Contents scale is not need for image layer because this can be done in compositor more efficiently.
161 #endif // USE(ACCELERATED_COMPOSITING)