Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1820)

Unified Diff: Source/platform/graphics/GraphicsContext.cpp

Issue 169283008: Maintain SkPaint in GraphicsContextState. (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: Ready for review. Created 6 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: Source/platform/graphics/GraphicsContext.cpp
diff --git a/Source/platform/graphics/GraphicsContext.cpp b/Source/platform/graphics/GraphicsContext.cpp
index 1e96dcadd941c7139f92b2a77a45f73cfff6a59c..d4df9d549fe242b5fa219a92c737832c4ec24d1d 100644
--- a/Source/platform/graphics/GraphicsContext.cpp
+++ b/Source/platform/graphics/GraphicsContext.cpp
@@ -138,7 +138,7 @@ GraphicsContext::GraphicsContext(SkCanvas* canvas)
GraphicsContext::~GraphicsContext()
{
ASSERT(!m_paintStateIndex);
- ASSERT(!m_paintState->m_saveCount);
+ ASSERT(!m_paintState->saveCount());
ASSERT(!m_annotationCount);
ASSERT(!m_layerCount);
ASSERT(m_recordingStateStack.isEmpty());
@@ -160,7 +160,7 @@ void GraphicsContext::save()
if (paintingDisabled())
return;
- m_paintState->m_saveCount++;
+ m_paintState->incrementSaveCount();
m_canvasStateStack.append(CanvasSaveState(m_canvasSaveFlags, m_canvas->getSaveCount()));
m_canvasSaveFlags |= SkCanvas::kMatrixClip_SaveFlag;
@@ -171,13 +171,13 @@ void GraphicsContext::restore()
if (paintingDisabled())
return;
- if (!m_paintStateIndex && !m_paintState->m_saveCount) {
+ if (!m_paintStateIndex && !m_paintState->saveCount()) {
WTF_LOG_ERROR("ERROR void GraphicsContext::restore() stack is empty");
return;
}
- if (m_paintState->m_saveCount) {
- m_paintState->m_saveCount--;
+ if (m_paintState->saveCount()) {
+ m_paintState->decrementSaveCount();
} else {
m_paintStateIndex--;
m_paintState = m_paintStateStack[m_paintStateIndex].get();
@@ -247,14 +247,6 @@ void GraphicsContext::endAnnotation()
#endif
}
-void GraphicsContext::setStrokeColor(const Color& color)
-{
- GraphicsContextState* stateToSet = mutableState();
- stateToSet->m_strokeData.setColor(color);
- stateToSet->m_strokeData.clearGradient();
- stateToSet->m_strokeData.clearPattern();
-}
-
void GraphicsContext::setStrokePattern(PassRefPtr<Pattern> pattern)
{
if (paintingDisabled())
@@ -265,9 +257,7 @@ void GraphicsContext::setStrokePattern(PassRefPtr<Pattern> pattern)
setStrokeColor(Color::black);
return;
}
- GraphicsContextState* stateToSet = mutableState();
- stateToSet->m_strokeData.clearGradient();
- stateToSet->m_strokeData.setPattern(pattern);
+ mutableState()->setStrokePattern(pattern);
}
void GraphicsContext::setStrokeGradient(PassRefPtr<Gradient> gradient)
@@ -280,17 +270,7 @@ void GraphicsContext::setStrokeGradient(PassRefPtr<Gradient> gradient)
setStrokeColor(Color::black);
return;
}
- GraphicsContextState* stateToSet = mutableState();
- stateToSet->m_strokeData.setGradient(gradient);
- stateToSet->m_strokeData.clearPattern();
-}
-
-void GraphicsContext::setFillColor(const Color& color)
-{
- GraphicsContextState* stateToSet = mutableState();
- stateToSet->m_fillColor = color;
- stateToSet->m_fillGradient.clear();
- stateToSet->m_fillPattern.clear();
+ mutableState()->setStrokeGradient(gradient);
}
void GraphicsContext::setFillPattern(PassRefPtr<Pattern> pattern)
@@ -304,9 +284,7 @@ void GraphicsContext::setFillPattern(PassRefPtr<Pattern> pattern)
return;
}
- GraphicsContextState* stateToSet = mutableState();
- stateToSet->m_fillGradient.clear();
- stateToSet->m_fillPattern = pattern;
+ mutableState()->setFillPattern(pattern);
}
void GraphicsContext::setFillGradient(PassRefPtr<Gradient> gradient)
@@ -320,9 +298,7 @@ void GraphicsContext::setFillGradient(PassRefPtr<Gradient> gradient)
return;
}
- GraphicsContextState* stateToSet = mutableState();
- stateToSet->m_fillGradient = gradient;
- stateToSet->m_fillPattern.clear();
+ mutableState()->setFillGradient(gradient);
}
void GraphicsContext::setShadow(const FloatSize& offset, float blur, const Color& color,
@@ -348,7 +324,7 @@ void GraphicsContext::setDrawLooper(PassOwnPtr<DrawLooperBuilder> drawLooperBuil
if (paintingDisabled())
return;
- mutableState()->m_looper = drawLooperBuilder->detachDrawLooper();
+ mutableState()->setDrawLooper(drawLooperBuilder->detachDrawLooper());
}
void GraphicsContext::clearDrawLooper()
@@ -356,22 +332,12 @@ void GraphicsContext::clearDrawLooper()
if (paintingDisabled())
return;
- mutableState()->m_looper.clear();
+ mutableState()->clearDrawLooper();
}
bool GraphicsContext::hasShadow() const
{
- return !!immutableState()->m_looper;
-}
-
-int GraphicsContext::getNormalizedAlpha() const
-{
- int alpha = roundf(immutableState()->m_alpha * 256);
- if (alpha > 255)
- alpha = 255;
- else if (alpha < 0)
- alpha = 0;
- return alpha;
+ return !!immutableState()->drawLooper();
}
FloatRect GraphicsContext::getClipBounds() const
@@ -443,15 +409,12 @@ bool GraphicsContext::couldUseLCDRenderedText()
void GraphicsContext::setCompositeOperation(CompositeOperator compositeOperation, WebBlendMode blendMode)
{
- GraphicsContextState* stateToSet = mutableState();
- stateToSet->m_compositeOperator = compositeOperation;
- stateToSet->m_blendMode = blendMode;
- stateToSet->m_xferMode = WebCoreCompositeToSkiaComposite(compositeOperation, blendMode);
+ mutableState()->setCompositeOperation(compositeOperation, blendMode);
}
SkColorFilter* GraphicsContext::colorFilter()
{
- return immutableState()->m_colorFilter.get();
+ return immutableState()->colorFilter();
}
void GraphicsContext::setColorFilter(ColorFilter colorFilter)
@@ -460,8 +423,8 @@ void GraphicsContext::setColorFilter(ColorFilter colorFilter)
// We only support one active color filter at the moment. If (when) this becomes a problem,
// we should switch to using color filter chains (Skia work in progress).
- ASSERT(!stateToSet->m_colorFilter);
- stateToSet->m_colorFilter = WebCoreColorFilterToSkiaColorFilter(colorFilter);
+ ASSERT(!stateToSet->colorFilter());
+ stateToSet->setColorFilter(WebCoreColorFilterToSkiaColorFilter(colorFilter));
}
bool GraphicsContext::readPixels(SkBitmap* bitmap, int x, int y, SkCanvas::Config8888 config8888)
@@ -498,7 +461,7 @@ bool GraphicsContext::concat(const SkMatrix& matrix)
void GraphicsContext::beginTransparencyLayer(float opacity, const FloatRect* bounds)
{
- beginLayer(opacity, immutableState()->m_compositeOperator, bounds);
+ beginLayer(opacity, immutableState()->compositeOperator(), bounds);
}
void GraphicsContext::beginLayer(float opacity, CompositeOperator op, const FloatRect* bounds, ColorFilter colorFilter, ImageFilter* imageFilter)
@@ -514,7 +477,7 @@ void GraphicsContext::beginLayer(float opacity, CompositeOperator op, const Floa
SkPaint layerPaint;
layerPaint.setAlpha(static_cast<unsigned char>(opacity * 255));
- layerPaint.setXfermode(WebCoreCompositeToSkiaComposite(op, m_paintState->m_blendMode).get());
+ layerPaint.setXfermode(WebCoreCompositeToSkiaComposite(op, m_paintState->blendMode()).get());
layerPaint.setColorFilter(WebCoreColorFilterToSkiaColorFilter(colorFilter).get());
layerPaint.setImageFilter(imageFilter);
@@ -617,22 +580,16 @@ void GraphicsContext::setupPaintForFilling(SkPaint* paint) const
if (paintingDisabled())
return;
- setupPaintCommon(paint);
-
- setupShader(paint, immutableState()->m_fillGradient.get(), immutableState()->m_fillPattern.get(), immutableState()->m_fillColor.rgb());
+ *paint = immutableState()->fillPaint();
}
-float GraphicsContext::setupPaintForStroking(SkPaint* paint, int length) const
+void GraphicsContext::setupPaintForStroking(SkPaint* paint, int length) const
{
if (paintingDisabled())
- return 0.0f;
-
- setupPaintCommon(paint);
-
- setupShader(paint, immutableState()->m_strokeData.gradient(), immutableState()->m_strokeData.pattern(),
- immutableState()->m_strokeData.color().rgb());
+ return;
- return immutableState()->m_strokeData.setupPaint(paint, length);
+ *paint = immutableState()->strokePaint();
+ immutableState()->strokeData().setupPaintDashPathEffect(paint, length);
Stephen White 2014/03/04 15:24:13 Seems a little surprising not to handle this autom
}
void GraphicsContext::drawConvexPolygon(size_t numPoints, const FloatPoint* points, bool shouldAntialias)
@@ -1010,19 +967,16 @@ void GraphicsContext::drawRect(const IntRect& rect)
return;
SkRect skRect = rect;
- SkPaint paint;
- int fillcolorNotTransparent = m_paintState->m_fillColor.rgb() & 0xFF000000;
- if (fillcolorNotTransparent) {
- setupPaintForFilling(&paint);
- drawRect(skRect, paint);
- }
+ int fillcolorNotTransparent = immutableState()->fillColor().rgb() & 0xFF000000;
+ if (fillcolorNotTransparent)
+ drawRect(skRect, immutableState()->fillPaint());
- if (m_paintState->m_strokeData.style() != NoStroke && (m_paintState->m_strokeData.color().rgb() & 0xFF000000)) {
+ if (immutableState()->strokeData().style() != NoStroke && (immutableState()->strokeData().color().rgb() & 0xFF000000)) {
// We do a fill of four rects to simulate the stroke of a border.
- paint.reset();
+ SkPaint paint;
setupPaintForFilling(&paint);
Stephen White 2014/03/04 15:24:13 IIRC, Florin's patch should get rid of this one; m
// need to jam in the strokeColor
- paint.setColor(this->effectiveStrokeColor());
+ paint.setColor(immutableState()->effectiveStrokeColor());
SkRect topBorder = { skRect.fLeft, skRect.fTop, skRect.fRight, skRect.fTop + 1 };
drawRect(topBorder, paint);
@@ -1265,7 +1219,8 @@ void GraphicsContext::drawBitmapRect(const SkBitmap& bitmap, const SkRect* src,
if (paintingDisabled())
return;
- SkCanvas::DrawBitmapRectFlags flags = m_paintState->m_shouldClampToSourceRect ? SkCanvas::kNone_DrawBitmapRectFlag : SkCanvas::kBleed_DrawBitmapRectFlag;
+ SkCanvas::DrawBitmapRectFlags flags =
+ immutableState()->shouldClampToSourceRect() ? SkCanvas::kNone_DrawBitmapRectFlag : SkCanvas::kBleed_DrawBitmapRectFlag;
m_canvas->drawBitmapRectToRect(bitmap, src, dst, paint, flags);
@@ -1363,12 +1318,11 @@ void GraphicsContext::fillPath(const Path& pathToFill)
SkPath& path = const_cast<SkPath&>(pathToFill.skPath());
SkPath::FillType previousFillType = path.getFillType();
- SkPath::FillType temporaryFillType = m_paintState->m_fillRule == RULE_EVENODD ? SkPath::kEvenOdd_FillType : SkPath::kWinding_FillType;
+ SkPath::FillType temporaryFillType =
+ immutableState()->fillRule() == RULE_EVENODD ? SkPath::kEvenOdd_FillType : SkPath::kWinding_FillType;
path.setFillType(temporaryFillType);
- SkPaint paint;
- setupPaintForFilling(&paint);
- drawPath(path, paint);
+ drawPath(path, immutableState()->fillPaint());
path.setFillType(previousFillType);
}
@@ -1380,9 +1334,7 @@ void GraphicsContext::fillRect(const FloatRect& rect)
SkRect r = rect;
- SkPaint paint;
- setupPaintForFilling(&paint);
- drawRect(r, paint);
+ drawRect(r, immutableState()->fillPaint());
}
void GraphicsContext::fillRect(const FloatRect& rect, const Color& color)
@@ -1392,7 +1344,12 @@ void GraphicsContext::fillRect(const FloatRect& rect, const Color& color)
SkRect r = rect;
SkPaint paint;
- setupPaintCommon(&paint);
+ paint.setAntiAlias(immutableState()->shouldAntialias());
+ if (!SkXfermode::IsMode(immutableState()->xferMode(), SkXfermode::kSrcOver_Mode))
+ paint.setXfermode(immutableState()->xferMode());
+ if (immutableState()->drawLooper())
+ paint.setLooper(immutableState()->drawLooper());
+ paint.setColorFilter(immutableState()->colorFilter());
f(malita) 2014/03/03 22:46:21 Why does this paint need special handling? (why no
Stephen Chennney 2014/03/04 13:20:36 It doesn't (apart from the color at the end). I mu
Stephen Chennney 2014/03/04 14:47:10 Done.
paint.setColor(color.rgb());
drawRect(r, paint);
}
@@ -1436,9 +1393,7 @@ void GraphicsContext::fillEllipse(const FloatRect& ellipse)
return;
SkRect rect = ellipse;
- SkPaint paint;
- setupPaintForFilling(&paint);
- drawOval(rect, paint);
+ drawOval(rect, immutableState()->fillPaint());
}
void GraphicsContext::strokePath(const Path& pathToStroke)
@@ -1840,26 +1795,6 @@ void GraphicsContext::setPathFromConvexPoints(SkPath* path, size_t numPoints, co
path->setConvexity(convexity);
}
-void GraphicsContext::setupPaintCommon(SkPaint* paint) const
-{
-#if defined(SK_DEBUG)
- {
- SkPaint defaultPaint;
- SkASSERT(*paint == defaultPaint);
- }
-#endif
-
- paint->setAntiAlias(m_paintState->m_shouldAntialias);
-
- if (!SkXfermode::IsMode(m_paintState->m_xferMode.get(), SkXfermode::kSrcOver_Mode))
- paint->setXfermode(m_paintState->m_xferMode.get());
-
- if (m_paintState->m_looper)
- paint->setLooper(m_paintState->m_looper.get());
-
- paint->setColorFilter(m_paintState->m_colorFilter.get());
-}
-
void GraphicsContext::drawOuterPath(const SkPath& path, SkPaint& paint, int width)
{
#if OS(MACOSX)
@@ -2011,27 +1946,6 @@ const SkPMColor GraphicsContext::antiColors2(int index)
}
#endif
-void GraphicsContext::setupShader(SkPaint* paint, Gradient* grad, Pattern* pat, SkColor color) const
-{
- RefPtr<SkShader> shader;
-
- if (grad) {
- shader = grad->shader();
- color = SK_ColorBLACK;
- } else if (pat) {
- shader = pat->shader();
- color = SK_ColorBLACK;
- paint->setFilterBitmap(imageInterpolationQuality() != InterpolationNone);
- }
-
- paint->setColor(m_paintState->applyAlpha(color));
-
- if (!shader)
- return;
-
- paint->setShader(shader.get());
-}
-
void GraphicsContext::didDrawTextInRect(const SkRect& textRect)
{
if (m_trackTextRegion) {

Powered by Google App Engine
This is Rietveld 408576698