blob: 09c97d80bc3b15264da5ed3765d7f076b228c15c [file] [log] [blame]
[email protected]94f206c12012-08-25 00:09:141// 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.
4
5#include "config.h"
6
7#if USE(ACCELERATED_COMPOSITING)
8
9#include "ImageLayerChromium.h"
10
11#include "CCLayerTreeHost.h"
12#include "LayerTextureUpdater.h"
13#include "PlatformColor.h"
14
15namespace WebCore {
16
17class ImageLayerTextureUpdater : public LayerTextureUpdater {
18public:
19 class Texture : public LayerTextureUpdater::Texture {
20 public:
21 Texture(ImageLayerTextureUpdater* textureUpdater, PassOwnPtr<CCPrioritizedTexture> texture)
22 : LayerTextureUpdater::Texture(texture)
23 , m_textureUpdater(textureUpdater)
24 {
25 }
26
27 virtual void updateRect(CCResourceProvider* resourceProvider, const IntRect& sourceRect, const IntSize& destOffset) OVERRIDE
28 {
29 textureUpdater()->updateTextureRect(resourceProvider, texture(), sourceRect, destOffset);
30 }
31
32 private:
33 ImageLayerTextureUpdater* textureUpdater() { return m_textureUpdater; }
34
35 ImageLayerTextureUpdater* m_textureUpdater;
36 };
37
38 static PassRefPtr<ImageLayerTextureUpdater> create()
39 {
40 return adoptRef(new ImageLayerTextureUpdater());
41 }
42
43 virtual ~ImageLayerTextureUpdater() { }
44
45 virtual PassOwnPtr<LayerTextureUpdater::Texture> createTexture(CCPrioritizedTextureManager* manager)
46 {
47 return adoptPtr(new Texture(this, CCPrioritizedTexture::create(manager)));
48 }
49
50 virtual SampledTexelFormat sampledTexelFormat(GC3Denum textureFormat) OVERRIDE
51 {
52 return PlatformColor::sameComponentOrder(textureFormat) ?
53 LayerTextureUpdater::SampledTexelFormatRGBA : LayerTextureUpdater::SampledTexelFormatBGRA;
54 }
55
56 void updateTextureRect(CCResourceProvider* resourceProvider, CCPrioritizedTexture* texture, const IntRect& sourceRect, const IntSize& destOffset)
57 {
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);
63
64 IntSize clippedDestOffset = destOffset + IntSize(clippedSourceRect.location() - sourceRect.location());
65
66 SkAutoLockPixels lock(m_bitmap);
67 texture->upload(resourceProvider, static_cast<const uint8_t*>(m_bitmap.getPixels()), imageRect, clippedSourceRect, clippedDestOffset);
68 }
69
70 void setBitmap(const SkBitmap& bitmap)
71 {
72 m_bitmap = bitmap;
73 }
74
75private:
76 ImageLayerTextureUpdater() { }
77
78 SkBitmap m_bitmap;
79};
80
81PassRefPtr<ImageLayerChromium> ImageLayerChromium::create()
82{
83 return adoptRef(new ImageLayerChromium());
84}
85
86ImageLayerChromium::ImageLayerChromium()
87 : TiledLayerChromium()
88{
89}
90
91ImageLayerChromium::~ImageLayerChromium()
92{
93}
94
95void ImageLayerChromium::setBitmap(const SkBitmap& bitmap)
96{
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())
102 return;
103
104 m_bitmap = bitmap;
105 setNeedsDisplay();
106}
107
108void ImageLayerChromium::setTexturePriorities(const CCPriorityCalculator& priorityCalc)
109{
110 // Update the tile data before creating all the layer's tiles.
111 updateTileSizeAndTilingOption();
112
113 TiledLayerChromium::setTexturePriorities(priorityCalc);
114}
115
116void ImageLayerChromium::update(CCTextureUpdateQueue& queue, const CCOcclusionTracker* occlusion, CCRenderingStats& stats)
117{
118 createTextureUpdaterIfNeeded();
119 if (m_needsDisplay) {
120 m_textureUpdater->setBitmap(m_bitmap);
121 updateTileSizeAndTilingOption();
122 invalidateContentRect(IntRect(IntPoint(), contentBounds()));
123 m_needsDisplay = false;
124 }
125 TiledLayerChromium::update(queue, occlusion, stats);
126}
127
128void ImageLayerChromium::createTextureUpdaterIfNeeded()
129{
130 if (m_textureUpdater)
131 return;
132
133 m_textureUpdater = ImageLayerTextureUpdater::create();
134 GC3Denum textureFormat = layerTreeHost()->rendererCapabilities().bestTextureFormat;
135 setTextureFormat(textureFormat);
136 setSampledTexelFormat(textureUpdater()->sampledTexelFormat(textureFormat));
137}
138
139LayerTextureUpdater* ImageLayerChromium::textureUpdater() const
140{
141 return m_textureUpdater.get();
142}
143
144IntSize ImageLayerChromium::contentBounds() const
145{
146 return IntSize(m_bitmap.width(), m_bitmap.height());
147}
148
149bool ImageLayerChromium::drawsContent() const
150{
151 return !m_bitmap.isNull() && TiledLayerChromium::drawsContent();
152}
153
154bool ImageLayerChromium::needsContentsScale() const
155{
156 // Contents scale is not need for image layer because this can be done in compositor more efficiently.
157 return false;
158}
159
160}
161#endif // USE(ACCELERATED_COMPOSITING)