- Revision
- 136435
- Author
- [email protected]
- Date
- 2012-12-03 13:42:39 -0800 (Mon, 03 Dec 2012)
Log Message
[Skia] Deferred SkCanvas save()
https://bugs.webkit.org/show_bug.cgi?id=102272
Reviewed by Stephen White.
Implement a lazy save() strategy: instead of delegating to the canvas upfront, defer and
only commit when a clip/matrix mutation is observed.
The patch yields a 1-20% SkCanvas save/restore reduction for heavy pages, with much larger
gains observed for deeply nested SVG content.
No new tests: no behaviour changes, coverage provided by existing tests.
* platform/graphics/skia/GraphicsContextSkia.cpp:
(WebCore::GraphicsContext::canvasClip):
* platform/graphics/skia/PlatformContextSkia.cpp:
(WebCore):
(WebCore::PlatformContextSkia::DeferredSaveState::DeferredSaveState):
(PlatformContextSkia::DeferredSaveState):
(WebCore::PlatformContextSkia::PlatformContextSkia):
(WebCore::PlatformContextSkia::save):
(WebCore::PlatformContextSkia::saveLayer):
(WebCore::PlatformContextSkia::beginLayerClippedToImage):
(WebCore::PlatformContextSkia::restore):
(WebCore::PlatformContextSkia::applyClipFromImage):
* platform/graphics/skia/PlatformContextSkia.h:
(PlatformContextSkia):
(WebCore::PlatformContextSkia::realizeSave):
(WebCore):
(WebCore::PlatformContextSkia::canvas):
(WebCore::PlatformContextSkia::clipPath):
(WebCore::PlatformContextSkia::clipRect):
(WebCore::PlatformContextSkia::setMatrix):
(WebCore::PlatformContextSkia::concat):
(WebCore::PlatformContextSkia::rotate):
(WebCore::PlatformContextSkia::scale):
(WebCore::PlatformContextSkia::translate):
Modified Paths
Diff
Modified: trunk/Source/WebCore/ChangeLog (136434 => 136435)
--- trunk/Source/WebCore/ChangeLog 2012-12-03 21:22:41 UTC (rev 136434)
+++ trunk/Source/WebCore/ChangeLog 2012-12-03 21:42:39 UTC (rev 136435)
@@ -1,3 +1,43 @@
+2012-12-03 Florin Malita <[email protected]>
+
+ [Skia] Deferred SkCanvas save()
+ https://bugs.webkit.org/show_bug.cgi?id=102272
+
+ Reviewed by Stephen White.
+
+ Implement a lazy save() strategy: instead of delegating to the canvas upfront, defer and
+ only commit when a clip/matrix mutation is observed.
+
+ The patch yields a 1-20% SkCanvas save/restore reduction for heavy pages, with much larger
+ gains observed for deeply nested SVG content.
+
+ No new tests: no behaviour changes, coverage provided by existing tests.
+
+ * platform/graphics/skia/GraphicsContextSkia.cpp:
+ (WebCore::GraphicsContext::canvasClip):
+ * platform/graphics/skia/PlatformContextSkia.cpp:
+ (WebCore):
+ (WebCore::PlatformContextSkia::DeferredSaveState::DeferredSaveState):
+ (PlatformContextSkia::DeferredSaveState):
+ (WebCore::PlatformContextSkia::PlatformContextSkia):
+ (WebCore::PlatformContextSkia::save):
+ (WebCore::PlatformContextSkia::saveLayer):
+ (WebCore::PlatformContextSkia::beginLayerClippedToImage):
+ (WebCore::PlatformContextSkia::restore):
+ (WebCore::PlatformContextSkia::applyClipFromImage):
+ * platform/graphics/skia/PlatformContextSkia.h:
+ (PlatformContextSkia):
+ (WebCore::PlatformContextSkia::realizeSave):
+ (WebCore):
+ (WebCore::PlatformContextSkia::canvas):
+ (WebCore::PlatformContextSkia::clipPath):
+ (WebCore::PlatformContextSkia::clipRect):
+ (WebCore::PlatformContextSkia::setMatrix):
+ (WebCore::PlatformContextSkia::concat):
+ (WebCore::PlatformContextSkia::rotate):
+ (WebCore::PlatformContextSkia::scale):
+ (WebCore::PlatformContextSkia::translate):
+
2012-12-03 Simon Fraser <[email protected]>
When animating mask-postion on a composited layer, element renders incorrectly
Modified: trunk/Source/WebCore/platform/graphics/skia/GraphicsContextSkia.cpp (136434 => 136435)
--- trunk/Source/WebCore/platform/graphics/skia/GraphicsContextSkia.cpp 2012-12-03 21:22:41 UTC (rev 136434)
+++ trunk/Source/WebCore/platform/graphics/skia/GraphicsContextSkia.cpp 2012-12-03 21:42:39 UTC (rev 136435)
@@ -334,7 +334,7 @@
if (paintingDisabled())
return;
- platformContext()->canvasClipPath(path.isNull() ? SkPath() : *path.platformPath());
+ platformContext()->clipPath(path.isNull() ? SkPath() : *path.platformPath());
}
void GraphicsContext::clipOut(const IntRect& rect)
Modified: trunk/Source/WebCore/platform/graphics/skia/PlatformContextSkia.cpp (136434 => 136435)
--- trunk/Source/WebCore/platform/graphics/skia/PlatformContextSkia.cpp 2012-12-03 21:22:41 UTC (rev 136434)
+++ trunk/Source/WebCore/platform/graphics/skia/PlatformContextSkia.cpp 2012-12-03 21:42:39 UTC (rev 136435)
@@ -58,6 +58,13 @@
namespace WebCore {
+struct PlatformContextSkia::DeferredSaveState {
+ DeferredSaveState(unsigned mask, int count) : m_flags(mask), m_restoreCount(count) { }
+
+ unsigned m_flags;
+ int m_restoreCount;
+};
+
// State -----------------------------------------------------------------------
// Encapsulates the additional painting state information we store for each
@@ -183,6 +190,7 @@
// Danger: canvas can be NULL.
PlatformContextSkia::PlatformContextSkia(SkCanvas* canvas)
: m_canvas(canvas)
+ , m_deferredSaveFlags(0)
, m_trackOpaqueRegion(false)
, m_printing(false)
, m_accelerated(false)
@@ -228,21 +236,14 @@
// don't attempt to clip multiple times.
m_state->m_imageBufferClip.reset();
- // Save our native canvas.
- m_canvas->save();
+ m_saveStateStack.append(DeferredSaveState(m_deferredSaveFlags, m_canvas->getSaveCount()));
+ m_deferredSaveFlags |= SkCanvas::kMatrixClip_SaveFlag;
}
-void PlatformContextSkia::saveLayer(const SkRect* bounds, const SkPaint* paint)
-{
- m_canvas->saveLayer(bounds, paint);
- if (bounds)
- m_canvas->clipRect(*bounds);
- if (m_trackOpaqueRegion)
- m_opaqueRegion.pushCanvasLayer(paint);
-}
-
void PlatformContextSkia::saveLayer(const SkRect* bounds, const SkPaint* paint, SkCanvas::SaveFlags saveFlags)
{
+ realizeSave(SkCanvas::kMatrixClip_SaveFlag);
+
m_canvas->saveLayer(bounds, paint, saveFlags);
if (bounds)
m_canvas->clipRect(*bounds);
@@ -264,7 +265,7 @@
SkFloatToScalar(rect.maxX()), SkFloatToScalar(rect.maxY()) };
if (imageBuffer->internalSize().isEmpty()) {
- m_canvas->clipRect(bounds);
+ clipRect(bounds);
return;
}
@@ -307,8 +308,10 @@
m_stateStack.removeLast();
m_state = &m_stateStack.last();
- // Restore our native canvas.
- m_canvas->restore();
+ DeferredSaveState savedState = m_saveStateStack.last();
+ m_saveStateStack.removeLast();
+ m_deferredSaveFlags = savedState.m_flags;
+ m_canvas->restoreToCount(savedState.m_restoreCount);
}
void PlatformContextSkia::drawRect(SkRect rect)
@@ -548,11 +551,6 @@
return m_state->applyAlpha(m_state->m_strokeColor);
}
-void PlatformContextSkia::canvasClipPath(const SkPath& path)
-{
- m_canvas->clipPath(path);
-}
-
InterpolationQuality PlatformContextSkia::interpolationQuality() const
{
return m_state->m_interpolationQuality;
@@ -596,6 +594,7 @@
// only look at the alpha when compositing. I'm not 100% sure this is what WebKit expects for image clipping.
SkPaint paint;
paint.setXfermodeMode(SkXfermode::kDstIn_Mode);
+ realizeSave(SkCanvas::kMatrixClip_SaveFlag);
m_canvas->save(SkCanvas::kMatrix_SaveFlag);
m_canvas->resetMatrix();
m_canvas->drawBitmapRect(imageBuffer, 0, rect, &paint);
Modified: trunk/Source/WebCore/platform/graphics/skia/PlatformContextSkia.h (136434 => 136435)
--- trunk/Source/WebCore/platform/graphics/skia/PlatformContextSkia.h 2012-12-03 21:22:41 UTC (rev 136434)
+++ trunk/Source/WebCore/platform/graphics/skia/PlatformContextSkia.h 2012-12-03 21:42:39 UTC (rev 136435)
@@ -93,8 +93,7 @@
void save();
void restore();
- void saveLayer(const SkRect* bounds, const SkPaint*);
- void saveLayer(const SkRect* bounds, const SkPaint*, SkCanvas::SaveFlags);
+ void saveLayer(const SkRect* bounds, const SkPaint*, SkCanvas::SaveFlags = SkCanvas::kARGB_ClipLayer_SaveFlag);
void restoreLayer();
// Begins a layer that is clipped to the image |imageBuffer| at the location
@@ -141,8 +140,6 @@
int getNormalizedAlpha() const;
SkXfermode::Mode getXfermodeMode() const;
- void canvasClipPath(const SkPath&);
-
// Returns the fill color. The returned color has it's alpha adjusted
// by the current alpha.
SkColor effectiveFillColor() const;
@@ -152,7 +149,10 @@
SkColor effectiveStrokeColor() const;
// Returns the canvas used for painting, NOT guaranteed to be non-null.
- SkCanvas* canvas() { return m_canvas; }
+ // Accessing the backing canvas this way flushes all queued save ops,
+ // so it should be avoided. Use the corresponding PlatformContextSkia
+ // draw/matrix/clip methods instead.
+ SkCanvas* canvas();
const SkCanvas* canvas() const { return m_canvas; }
InterpolationQuality interpolationQuality() const;
@@ -259,9 +259,13 @@
// common code between setupPaintFor[Filling,Stroking]
void setupShader(SkPaint*, Gradient*, Pattern*, SkColor) const;
+ void realizeSave(SkCanvas::SaveFlags);
+
// Defines drawing style.
struct State;
+ struct DeferredSaveState;
+
// NULL indicates painting is disabled. Never delete this object.
SkCanvas* m_canvas;
const GraphicsContext* m_gc;
@@ -273,6 +277,13 @@
// mStateStack.back().
State* m_state;
+ WTF::Vector<DeferredSaveState> m_saveStateStack;
+
+ // Currently pending save flags.
+ // FIXME: While defined as a bitmask of SkCanvas::SaveFlags, this is mostly used as a bool.
+ // It will come in handy when adding granular save() support (clip vs. matrix vs. paint).
+ unsigned m_deferredSaveFlags;
+
// Tracks the region painted opaque via the GraphicsContext.
OpaqueRegionSkia m_opaqueRegion;
bool m_trackOpaqueRegion;
@@ -287,6 +298,22 @@
#endif
};
+inline void PlatformContextSkia::realizeSave(SkCanvas::SaveFlags flags)
+{
+ if (m_deferredSaveFlags & flags) {
+ m_canvas->save((SkCanvas::SaveFlags)m_deferredSaveFlags);
+ m_deferredSaveFlags = 0;
+ }
+}
+
+inline SkCanvas* PlatformContextSkia::canvas()
+{
+ // Flush any pending saves.
+ realizeSave(SkCanvas::kMatrixClip_SaveFlag);
+
+ return m_canvas;
+}
+
inline const SkBitmap& PlatformContextSkia::layerBitmap(AccessMode access) const
{
return m_canvas->getTopDevice()->accessBitmap(access == ReadWrite);
@@ -324,11 +351,15 @@
inline bool PlatformContextSkia::clipPath(const SkPath& path, AntiAliasingMode aa, SkRegion::Op op)
{
+ realizeSave(SkCanvas::kClip_SaveFlag);
+
return m_canvas->clipPath(path, op, aa == AntiAliased);
}
inline bool PlatformContextSkia::clipRect(const SkRect& rect, AntiAliasingMode aa, SkRegion::Op op)
{
+ realizeSave(SkCanvas::kClip_SaveFlag);
+
return m_canvas->clipRect(rect, op, aa == AntiAliased);
}
@@ -339,6 +370,8 @@
inline void PlatformContextSkia::setMatrix(const SkMatrix& matrix)
{
+ realizeSave(SkCanvas::kMatrix_SaveFlag);
+
m_canvas->setMatrix(matrix);
}
@@ -349,21 +382,29 @@
inline bool PlatformContextSkia::concat(const SkMatrix& matrix)
{
+ realizeSave(SkCanvas::kMatrix_SaveFlag);
+
return m_canvas->concat(matrix);
}
inline bool PlatformContextSkia::rotate(SkScalar degrees)
{
+ realizeSave(SkCanvas::kMatrix_SaveFlag);
+
return m_canvas->rotate(degrees);
}
inline bool PlatformContextSkia::scale(SkScalar sx, SkScalar sy)
{
+ realizeSave(SkCanvas::kMatrix_SaveFlag);
+
return m_canvas->scale(sx, sy);
}
inline bool PlatformContextSkia::translate(SkScalar dx, SkScalar dy)
{
+ realizeSave(SkCanvas::kMatrix_SaveFlag);
+
return m_canvas->translate(dx, dy);
}