Skip to content

Instantly share code, notes, and snippets.

@noamr
Created June 5, 2013 14:59
Show Gist options
  • Save noamr/5714517 to your computer and use it in GitHub Desktop.
Save noamr/5714517 to your computer and use it in GitHub Desktop.
diff --git a/Source/WebCore/platform/graphics/texmap/coordinated/CoordinatedGraphicsLayer.cpp b/Source/WebCore/platform/graphics/texmap/coordinated/CoordinatedGraphicsLayer.cpp
index a0e916f..f173da7 100644
--- a/Source/WebCore/platform/graphics/texmap/coordinated/CoordinatedGraphicsLayer.cpp
+++ b/Source/WebCore/platform/graphics/texmap/coordinated/CoordinatedGraphicsLayer.cpp
@@ -954,11 +954,11 @@ Color CoordinatedGraphicsLayer::tiledBackingStoreBackgroundColor() const
return contentsOpaque() ? Color::white : Color::transparent;
}
-PassOwnPtr<GraphicsContext> CoordinatedGraphicsLayer::beginContentUpdate(const IntSize& size, uint32_t& atlas, IntPoint& offset)
+bool CoordinatedGraphicsLayer::paintToSurface(const IntSize& size, uint32_t& atlas, IntPoint& offset, CoordinatedSurface::Client* client)
{
ASSERT(m_coordinator);
ASSERT(m_coordinator->isFlushingLayerChanges());
- return m_coordinator->beginContentUpdate(size, contentsOpaque() ? CoordinatedSurface::NoFlags : CoordinatedSurface::SupportsAlpha, atlas, offset);
+ return m_coordinator->paintToSurface(size, contentsOpaque() ? CoordinatedSurface::NoFlags : CoordinatedSurface::SupportsAlpha, atlas, offset, client);
}
void CoordinatedGraphicsLayer::createTile(uint32_t tileID, const SurfaceUpdateInfo& updateInfo, const IntRect& tileRect)
diff --git a/Source/WebCore/platform/graphics/texmap/coordinated/CoordinatedGraphicsLayer.h b/Source/WebCore/platform/graphics/texmap/coordinated/CoordinatedGraphicsLayer.h
index 4d70cd2..fd4446b 100644
--- a/Source/WebCore/platform/graphics/texmap/coordinated/CoordinatedGraphicsLayer.h
+++ b/Source/WebCore/platform/graphics/texmap/coordinated/CoordinatedGraphicsLayer.h
@@ -52,7 +52,7 @@ public:
virtual FloatRect visibleContentsRect() const = 0;
virtual PassRefPtr<CoordinatedImageBacking> createImageBackingIfNeeded(Image*) = 0;
virtual void detachLayer(CoordinatedGraphicsLayer*) = 0;
- virtual PassOwnPtr<GraphicsContext> beginContentUpdate(const IntSize&, CoordinatedSurface::Flags, uint32_t& atlasID, IntPoint&) = 0;
+ virtual bool paintToSurface(const IntSize&, CoordinatedSurface::Flags, uint32_t& atlasID, IntPoint&, CoordinatedSurface::Client*) = 0;
virtual void syncLayerState(CoordinatedLayerID, CoordinatedGraphicsLayerState&) = 0;
};
@@ -139,7 +139,7 @@ public:
virtual void createTile(uint32_t tileID, const SurfaceUpdateInfo&, const IntRect&) OVERRIDE;
virtual void updateTile(uint32_t tileID, const SurfaceUpdateInfo&, const IntRect&) OVERRIDE;
virtual void removeTile(uint32_t tileID) OVERRIDE;
- virtual PassOwnPtr<GraphicsContext> beginContentUpdate(const IntSize&, uint32_t& atlasID, IntPoint&) OVERRIDE;
+ virtual bool paintToSurface(const IntSize&, uint32_t& atlasID, IntPoint&, CoordinatedSurface::Client*) OVERRIDE;
void setCoordinator(CoordinatedGraphicsLayerClient*);
diff --git a/Source/WebCore/platform/graphics/texmap/coordinated/CoordinatedImageBacking.cpp b/Source/WebCore/platform/graphics/texmap/coordinated/CoordinatedImageBacking.cpp
index 128babe..fe5e256 100644
--- a/Source/WebCore/platform/graphics/texmap/coordinated/CoordinatedImageBacking.cpp
+++ b/Source/WebCore/platform/graphics/texmap/coordinated/CoordinatedImageBacking.cpp
@@ -82,6 +82,26 @@ void CoordinatedImageBacking::markDirty()
m_isDirty = true;
}
+namespace {
+class ImageBackingSurfaceClient : public CoordinatedSurface::Client
+{
+public:
+ virtual void paintToSurfaceContext(GraphicsContext* context)
+ {
+ context->drawImage(m_image, ColorSpaceDeviceRGB, m_rect, m_rect);
+ }
+
+ ImageBackingSurfaceClient(Image* image, const IntRect& rect)
+ : m_image(image)
+ , m_rect(rect)
+ {
+ }
+
+private:
+ IntRect m_rect;
+ Image* m_image;
+};
+}
void CoordinatedImageBacking::update()
{
releaseSurfaceIfNeeded();
@@ -108,9 +128,8 @@ void CoordinatedImageBacking::update()
}
IntRect rect(IntPoint::zero(), m_image->size());
- OwnPtr<GraphicsContext> context = m_surface->createGraphicsContext(rect);
- context->drawImage(m_image.get(), ColorSpaceDeviceRGB, rect, rect);
-
+ ImageBackingSurfaceClient surfaceClient(m_image.get(), rect);
+ m_surface->paintToSurface(rect, &surfaceClient);
m_nativeImagePtr = m_image->nativeImageForCurrentFrame();
// If sending the message fails, try again in the next update.
diff --git a/Source/WebCore/platform/graphics/texmap/coordinated/CoordinatedSurface.h b/Source/WebCore/platform/graphics/texmap/coordinated/CoordinatedSurface.h
index 3077c3c..f47aae0 100644
--- a/Source/WebCore/platform/graphics/texmap/coordinated/CoordinatedSurface.h
+++ b/Source/WebCore/platform/graphics/texmap/coordinated/CoordinatedSurface.h
@@ -37,6 +37,13 @@ public:
NoFlags = 0,
SupportsAlpha = 1 << 0,
};
+
+ struct Client
+ {
+ virtual void paintToSurfaceContext(GraphicsContext*) = 0;
+ virtual ~Client() { }
+ };
+
typedef unsigned Flags;
typedef PassRefPtr<CoordinatedSurface> Factory(const IntSize&, Flags);
@@ -49,7 +56,7 @@ public:
virtual IntSize size() const = 0;
// Create a graphics context that can be used to paint into the backing store.
- virtual PassOwnPtr<GraphicsContext> createGraphicsContext(const IntRect&) = 0;
+ virtual void paintToSurface(const IntRect&, Client*) = 0;
#if USE(TEXTURE_MAPPER)
virtual void copyToTexture(PassRefPtr<BitmapTexture>, const IntRect& target, const IntPoint& sourceOffset) = 0;
diff --git a/Source/WebCore/platform/graphics/texmap/coordinated/CoordinatedTile.cpp b/Source/WebCore/platform/graphics/texmap/coordinated/CoordinatedTile.cpp
index 9f9d34e..83d03c5 100644
--- a/Source/WebCore/platform/graphics/texmap/coordinated/CoordinatedTile.cpp
+++ b/Source/WebCore/platform/graphics/texmap/coordinated/CoordinatedTile.cpp
@@ -67,23 +67,26 @@ void CoordinatedTile::invalidate(const IntRect& dirtyRect)
m_dirtyRect.unite(tileDirtyRect);
}
+void CoordinatedTile::paintToSurfaceContext(GraphicsContext* context)
+{
+ context->translate(-m_dirtyRect.x(), -m_dirtyRect.y());
+ context->scale(FloatSize(m_tiledBackingStore->contentsScale(), m_tiledBackingStore->contentsScale()));
+ m_tiledBackingStore->client()->tiledBackingStorePaint(context, m_tiledBackingStore->mapToContents(m_dirtyRect));
+}
+
+
Vector<IntRect> CoordinatedTile::updateBackBuffer()
{
if (!isDirty())
return Vector<IntRect>();
SurfaceUpdateInfo updateInfo;
- OwnPtr<GraphicsContext> graphicsContext = m_client->beginContentUpdate(m_dirtyRect.size(), updateInfo.atlasID, updateInfo.surfaceOffset);
- if (!graphicsContext)
+ if (!m_client->paintToSurface(m_dirtyRect.size(), updateInfo.atlasID, updateInfo.surfaceOffset, this))
return Vector<IntRect>();
- graphicsContext->translate(-m_dirtyRect.x(), -m_dirtyRect.y());
- graphicsContext->scale(FloatSize(m_tiledBackingStore->contentsScale(), m_tiledBackingStore->contentsScale()));
- m_tiledBackingStore->client()->tiledBackingStorePaint(graphicsContext.get(), m_tiledBackingStore->mapToContents(m_dirtyRect));
updateInfo.updateRect = m_dirtyRect;
updateInfo.updateRect.move(-m_rect.x(), -m_rect.y());
updateInfo.scaleFactor = m_tiledBackingStore->contentsScale();
- graphicsContext.release();
static uint32_t id = 1;
if (m_ID == InvalidCoordinatedTileID) {
diff --git a/Source/WebCore/platform/graphics/texmap/coordinated/CoordinatedTile.h b/Source/WebCore/platform/graphics/texmap/coordinated/CoordinatedTile.h
index be99bfd..165921b 100644
--- a/Source/WebCore/platform/graphics/texmap/coordinated/CoordinatedTile.h
+++ b/Source/WebCore/platform/graphics/texmap/coordinated/CoordinatedTile.h
@@ -27,7 +27,7 @@
#define CoordinatedTile_h
#if USE(TILED_BACKING_STORE)
-
+#include "CoordinatedSurface.h"
#include "IntRect.h"
#include "Tile.h"
#include "TiledBackingStore.h"
@@ -39,7 +39,7 @@ class ImageBuffer;
class SurfaceUpdateInfo;
class TiledBackingStore;
-class CoordinatedTile : public Tile {
+class CoordinatedTile : public Tile, public CoordinatedSurface::Client {
public:
static PassRefPtr<Tile> create(CoordinatedTileClient* client, TiledBackingStore* tiledBackingStore, const Coordinate& tileCoordinate) { return adoptRef(new CoordinatedTile(client, tiledBackingStore, tileCoordinate)); }
~CoordinatedTile();
@@ -55,6 +55,9 @@ public:
const IntRect& rect() const { return m_rect; }
void resize(const IntSize&);
+protected:
+ virtual void paintToSurfaceContext(GraphicsContext*) OVERRIDE;
+
private:
CoordinatedTile(CoordinatedTileClient*, TiledBackingStore*, const Coordinate&);
@@ -75,7 +78,7 @@ public:
virtual void createTile(uint32_t tileID, const SurfaceUpdateInfo&, const IntRect&) = 0;
virtual void updateTile(uint32_t tileID, const SurfaceUpdateInfo&, const IntRect&) = 0;
virtual void removeTile(uint32_t tileID) = 0;
- virtual PassOwnPtr<GraphicsContext> beginContentUpdate(const IntSize&, uint32_t& atlasID, IntPoint&) = 0;
+ virtual bool paintToSurface(const IntSize&, uint32_t& atlasID, IntPoint&, CoordinatedSurface::Client*) = 0;
};
class CoordinatedTileBackend : public TiledBackingStoreBackend {
diff --git a/Source/WebCore/platform/graphics/texmap/coordinated/UpdateAtlas.cpp b/Source/WebCore/platform/graphics/texmap/coordinated/UpdateAtlas.cpp
index 364f068..61bf931 100644
--- a/Source/WebCore/platform/graphics/texmap/coordinated/UpdateAtlas.cpp
+++ b/Source/WebCore/platform/graphics/texmap/coordinated/UpdateAtlas.cpp
@@ -62,7 +62,36 @@ void UpdateAtlas::didSwapBuffers()
m_areaAllocator.clear();
}
-PassOwnPtr<GraphicsContext> UpdateAtlas::beginPaintingOnAvailableBuffer(uint32_t& atlasID, const IntSize& size, IntPoint& offset)
+namespace {
+class UpdateAtlasSurfaceClient : public CoordinatedSurface::Client
+{
+public:
+ virtual void paintToSurfaceContext(GraphicsContext* context) OVERRIDE
+ {
+ if (m_hasAlpha) {
+ context->setCompositeOperation(CompositeCopy);
+ context->fillRect(IntRect(IntPoint::zero(), m_size), Color::transparent, ColorSpaceDeviceRGB);
+ context->setCompositeOperation(CompositeSourceOver);
+ }
+
+ m_client->paintToSurfaceContext(context);
+ }
+
+ UpdateAtlasSurfaceClient(CoordinatedSurface::Client* client, bool alpha, const IntSize& size)
+ : m_client(client)
+ , m_hasAlpha(alpha)
+ , m_size(size)
+ {
+ }
+
+private:
+ CoordinatedSurface::Client* m_client;
+ bool m_hasAlpha;
+ IntSize m_size;
+};
+}
+
+bool UpdateAtlas::paintOnAvailableBuffer(uint32_t& atlasID, const IntSize& size, IntPoint& offset, CoordinatedSurface::Client* client)
{
m_inactivityInSeconds = 0;
buildLayoutIfNeeded();
@@ -70,24 +99,18 @@ PassOwnPtr<GraphicsContext> UpdateAtlas::beginPaintingOnAvailableBuffer(uint32_t
// No available buffer was found, returning null.
if (rect.isEmpty())
- return PassOwnPtr<GraphicsContext>();
+ return false;
if (!m_surface)
- return PassOwnPtr<GraphicsContext>();
+ return false;
atlasID = m_ID;
// FIXME: Use tri-state buffers, to allow faster updates.
offset = rect.location();
- OwnPtr<GraphicsContext> graphicsContext = m_surface->createGraphicsContext(rect);
-
- if (supportsAlpha()) {
- graphicsContext->setCompositeOperation(CompositeCopy);
- graphicsContext->fillRect(IntRect(IntPoint::zero(), size), Color::transparent, ColorSpaceDeviceRGB);
- graphicsContext->setCompositeOperation(CompositeSourceOver);
- }
-
- return graphicsContext.release();
+ UpdateAtlasSurfaceClient surfaceClient(client, supportsAlpha(), size);
+ m_surface->paintToSurface(rect, &surfaceClient);
+ return true;
}
} // namespace WebCore
diff --git a/Source/WebCore/platform/graphics/texmap/coordinated/UpdateAtlas.h b/Source/WebCore/platform/graphics/texmap/coordinated/UpdateAtlas.h
index 48a6bf8..72bb87f 100644
--- a/Source/WebCore/platform/graphics/texmap/coordinated/UpdateAtlas.h
+++ b/Source/WebCore/platform/graphics/texmap/coordinated/UpdateAtlas.h
@@ -46,7 +46,7 @@ public:
inline IntSize size() const { return m_surface->size(); }
// Returns a null pointer of there is no available buffer.
- PassOwnPtr<GraphicsContext> beginPaintingOnAvailableBuffer(uint32_t& atlasID, const IntSize&, IntPoint& offset);
+ bool paintOnAvailableBuffer(uint32_t& atlasID, const IntSize&, IntPoint& offset, CoordinatedSurface::Client*);
void didSwapBuffers();
bool supportsAlpha() const { return m_surface->supportsAlpha(); }
diff --git a/Source/WebKit2/Shared/CoordinatedGraphics/WebCoordinatedSurface.cpp b/Source/WebKit2/Shared/CoordinatedGraphics/WebCoordinatedSurface.cpp
index 52b7f6d..4aa3bc9 100644
--- a/Source/WebKit2/Shared/CoordinatedGraphics/WebCoordinatedSurface.cpp
+++ b/Source/WebKit2/Shared/CoordinatedGraphics/WebCoordinatedSurface.cpp
@@ -103,6 +103,15 @@ PassRefPtr<WebCoordinatedSurface> WebCoordinatedSurface::createWithSurface(const
}
#endif
+void WebCoordinatedSurface::paintToSurface(const IntRect& rect, CoordinatedSurface::Client* client)
+{
+ if (!client)
+ return;
+
+ OwnPtr<GraphicsContext> context = createGraphicsContext(rect);
+ client->paintToSurfaceContext(context.get());
+}
+
PassOwnPtr<WebCore::GraphicsContext> WebCoordinatedSurface::createGraphicsContext(const IntRect& rect)
{
#if USE(GRAPHICS_SURFACE)
diff --git a/Source/WebKit2/Shared/CoordinatedGraphics/WebCoordinatedSurface.h b/Source/WebKit2/Shared/CoordinatedGraphics/WebCoordinatedSurface.h
index 5386a91..98474b4 100644
--- a/Source/WebKit2/Shared/CoordinatedGraphics/WebCoordinatedSurface.h
+++ b/Source/WebKit2/Shared/CoordinatedGraphics/WebCoordinatedSurface.h
@@ -73,13 +73,15 @@ public:
virtual WebCore::IntSize size() const OVERRIDE { return m_size; }
- virtual PassOwnPtr<WebCore::GraphicsContext> createGraphicsContext(const WebCore::IntRect&) OVERRIDE;
+ virtual void paintToSurface(const WebCore::IntRect&, WebCore::CoordinatedSurface::Client*) OVERRIDE;
+
#if USE(TEXTURE_MAPPER)
virtual void copyToTexture(PassRefPtr<WebCore::BitmapTexture>, const WebCore::IntRect& target, const WebCore::IntPoint& sourceOffset) OVERRIDE;
#endif
private:
+ PassOwnPtr<WebCore::GraphicsContext> createGraphicsContext(const WebCore::IntRect&);
WebCoordinatedSurface(const WebCore::IntSize&, Flags, PassRefPtr<ShareableBitmap>);
virtual Flags flags() const OVERRIDE { return m_flags; }
diff --git a/Source/WebKit2/WebProcess/WebPage/CoordinatedGraphics/CoordinatedLayerTreeHost.cpp b/Source/WebKit2/WebProcess/WebPage/CoordinatedGraphics/CoordinatedLayerTreeHost.cpp
index 422ae5d..7da7d3e 100644
--- a/Source/WebKit2/WebProcess/WebPage/CoordinatedGraphics/CoordinatedLayerTreeHost.cpp
+++ b/Source/WebKit2/WebProcess/WebPage/CoordinatedGraphics/CoordinatedLayerTreeHost.cpp
@@ -692,23 +692,20 @@ void CoordinatedLayerTreeHost::purgeBackingStores()
m_updateAtlases.clear();
}
-PassOwnPtr<GraphicsContext> CoordinatedLayerTreeHost::beginContentUpdate(const IntSize& size, CoordinatedSurface::Flags flags, uint32_t& atlasID, IntPoint& offset)
+bool CoordinatedLayerTreeHost::paintToSurface(const IntSize& size, CoordinatedSurface::Flags flags, uint32_t& atlasID, IntPoint& offset, CoordinatedSurface::Client* client)
{
- OwnPtr<GraphicsContext> graphicsContext;
for (unsigned i = 0; i < m_updateAtlases.size(); ++i) {
UpdateAtlas* atlas = m_updateAtlases[i].get();
if (atlas->supportsAlpha() == (flags & CoordinatedSurface::SupportsAlpha)) {
// This will return null if there is no available buffer space.
- graphicsContext = atlas->beginPaintingOnAvailableBuffer(atlasID, size, offset);
- if (graphicsContext)
- return graphicsContext.release();
+ return atlas->paintOnAvailableBuffer(atlasID, size, offset, client);
}
}
static const int ScratchBufferDimension = 1024; // Should be a power of two.
m_updateAtlases.append(adoptPtr(new UpdateAtlas(this, ScratchBufferDimension, flags)));
scheduleReleaseInactiveAtlases();
- return m_updateAtlases.last()->beginPaintingOnAvailableBuffer(atlasID, size, offset);
+ return m_updateAtlases.last()->paintOnAvailableBuffer(atlasID, size, offset, client);
}
const double ReleaseInactiveAtlasesTimerInterval = 0.5;
diff --git a/Source/WebKit2/WebProcess/WebPage/CoordinatedGraphics/CoordinatedLayerTreeHost.h b/Source/WebKit2/WebProcess/WebPage/CoordinatedGraphics/CoordinatedLayerTreeHost.h
index 05d61ba..d72dee2 100644
--- a/Source/WebKit2/WebProcess/WebPage/CoordinatedGraphics/CoordinatedLayerTreeHost.h
+++ b/Source/WebKit2/WebProcess/WebPage/CoordinatedGraphics/CoordinatedLayerTreeHost.h
@@ -123,7 +123,7 @@ private:
virtual WebCore::FloatRect visibleContentsRect() const;
virtual PassRefPtr<WebCore::CoordinatedImageBacking> createImageBackingIfNeeded(WebCore::Image*) OVERRIDE;
virtual void detachLayer(WebCore::CoordinatedGraphicsLayer*);
- virtual PassOwnPtr<WebCore::GraphicsContext> beginContentUpdate(const WebCore::IntSize&, WebCore::CoordinatedSurface::Flags, uint32_t& atlasID, WebCore::IntPoint&);
+ virtual bool paintToSurface(const WebCore::IntSize&, WebCore::CoordinatedSurface::Flags, uint32_t& atlasID, WebCore::IntPoint&, WebCore::CoordinatedSurface::Client*);
virtual void syncLayerState(WebCore::CoordinatedLayerID, WebCore::CoordinatedGraphicsLayerState&);
// UpdateAtlasClient
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment