WebKit merge 128969:128981
[chromium-blink-merge.git] / cc / ImageLayerChromium.cpp
blobd728bceb23a0768c94c5d7effe7955cce9557fdb
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.
5 #include "config.h"
7 #if USE(ACCELERATED_COMPOSITING)
9 #include "ImageLayerChromium.h"
11 #include "CCLayerTreeHost.h"
12 #include "LayerTextureUpdater.h"
13 #include "PlatformColor.h"
15 namespace cc {
17 class ImageLayerTextureUpdater : public LayerTextureUpdater {
18 public:
19 class Texture : public LayerTextureUpdater::Texture {
20 public:
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);
32 private:
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)
72 m_bitmap = bitmap;
75 private:
76 ImageLayerTextureUpdater() { }
78 SkBitmap m_bitmap;
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())
102 return;
104 m_bitmap = bitmap;
105 setNeedsDisplay();
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)
131 return;
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.
157 return false;
161 #endif // USE(ACCELERATED_COMPOSITING)