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

Unified Diff: third_party/WebKit/Source/modules/canvas2d/BaseRenderingContext2D.h

Issue 1710633002: Pull up a subset of CanvasRenderingContext2D into BaseRenderingContext2D. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: rebase Created 4 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
« no previous file with comments | « no previous file | third_party/WebKit/Source/modules/canvas2d/BaseRenderingContext2D.cpp » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: third_party/WebKit/Source/modules/canvas2d/BaseRenderingContext2D.h
diff --git a/third_party/WebKit/Source/modules/canvas2d/CanvasRenderingContext2D.h b/third_party/WebKit/Source/modules/canvas2d/BaseRenderingContext2D.h
similarity index 47%
copy from third_party/WebKit/Source/modules/canvas2d/CanvasRenderingContext2D.h
copy to third_party/WebKit/Source/modules/canvas2d/BaseRenderingContext2D.h
index 5106883c4caa24ce6e4ae154d4dff9a9a1c52525..9cccce49960514a23052e79ef0233ea0def6b92e 100644
--- a/third_party/WebKit/Source/modules/canvas2d/CanvasRenderingContext2D.h
+++ b/third_party/WebKit/Source/modules/canvas2d/BaseRenderingContext2D.h
@@ -1,87 +1,34 @@
-/*
- * Copyright (C) 2006, 2007, 2009, 2010, 2011, 2012 Apple Inc. All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * 1. Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
- * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
- * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
- * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
- * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
- * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
- * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
- * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
- * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
-
-#ifndef CanvasRenderingContext2D_h
-#define CanvasRenderingContext2D_h
-
-#include "bindings/core/v8/ScriptWrappable.h"
+// Copyright 2016 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef BaseRenderingContext2D_h
+#define BaseRenderingContext2D_h
+
+#include "bindings/core/v8/UnionTypesCore.h"
#include "bindings/modules/v8/UnionTypesModules.h"
-#include "core/html/canvas/CanvasContextCreationAttributes.h"
#include "core/html/canvas/CanvasRenderingContext.h"
-#include "core/html/canvas/CanvasRenderingContextFactory.h"
-#include "core/svg/SVGResourceClient.h"
#include "modules/ModulesExport.h"
-#include "modules/canvas2d/Canvas2DContextAttributes.h"
+#include "modules/canvas2d/CanvasGradient.h"
#include "modules/canvas2d/CanvasPathMethods.h"
#include "modules/canvas2d/CanvasRenderingContext2DState.h"
-#include "platform/graphics/GraphicsTypes.h"
-#include "platform/heap/GarbageCollected.h"
-#include "public/platform/WebThread.h"
-#include "wtf/Vector.h"
-#include "wtf/text/WTFString.h"
-
-namespace blink { class WebLayer; }
+#include "modules/canvas2d/CanvasStyle.h"
+#include "third_party/skia/include/core/SkCanvas.h"
namespace blink {
class CanvasImageSource;
-class Element;
-class ExceptionState;
-class FloatRect;
-class FloatSize;
-class Font;
-class FontMetrics;
-class HitRegion;
-class HitRegionOptions;
-class HitRegionManager;
-class ImageData;
+class Color;
+class Image;
+class ImageBuffer;
class Path2D;
class SVGMatrixTearOff;
-class TextMetrics;
typedef HTMLImageElementOrHTMLVideoElementOrHTMLCanvasElementOrImageBitmap CanvasImageSourceUnion;
-class MODULES_EXPORT CanvasRenderingContext2D final : public CanvasRenderingContext, public CanvasPathMethods, public WebThread::TaskObserver, public SVGResourceClient {
- DEFINE_WRAPPERTYPEINFO();
- WILL_BE_USING_PRE_FINALIZER(CanvasRenderingContext2D, dispose);
+class MODULES_EXPORT BaseRenderingContext2D : public WillBeGarbageCollectedMixin, public CanvasPathMethods {
public:
- class Factory : public CanvasRenderingContextFactory {
- WTF_MAKE_NONCOPYABLE(Factory);
- public:
- Factory() {}
- ~Factory() override {}
-
- PassOwnPtrWillBeRawPtr<CanvasRenderingContext> create(HTMLCanvasElement* canvas, const CanvasContextCreationAttributes& attrs, Document& document) override
- {
- return adoptPtrWillBeNoop(new CanvasRenderingContext2D(canvas, attrs, document));
- }
- CanvasRenderingContext::ContextType contextType() const override { return CanvasRenderingContext::Context2d; }
- void onError(HTMLCanvasElement*, const String& error) override { }
- };
-
- ~CanvasRenderingContext2D() override;
+ ~BaseRenderingContext2D() override;
void strokeStyle(StringOrCanvasGradientOrCanvasPattern&) const;
void setStrokeStyle(const StringOrCanvasGradientOrCanvasPattern&);
@@ -122,11 +69,6 @@ public:
double globalAlpha() const;
void setGlobalAlpha(double);
- bool isContextLost() const override;
-
- bool shouldAntialias() const;
- void setShouldAntialias(bool) override;
-
String globalCompositeOperation() const;
void setGlobalCompositeOperation(const String&);
@@ -160,10 +102,7 @@ public:
bool isPointInStroke(const double x, const double y);
bool isPointInStroke(Path2D*, const double x, const double y);
- void scrollPathIntoView();
- void scrollPathIntoView(Path2D*);
-
- void clearRect(double x, double y, double width, double height) override;
+ void clearRect(double x, double y, double width, double height);
void fillRect(double x, double y, double width, double height);
void strokeRect(double x, double y, double width, double height);
@@ -182,88 +121,71 @@ public:
void putImageData(ImageData*, double dx, double dy, ExceptionState&);
void putImageData(ImageData*, double dx, double dy, double dirtyX, double dirtyY, double dirtyWidth, double dirtyHeight, ExceptionState&);
- void reset() override;
-
- String font() const;
- void setFont(const String&) override;
-
- String textAlign() const;
- void setTextAlign(const String&);
-
- String textBaseline() const;
- void setTextBaseline(const String&);
-
- String direction() const;
- void setDirection(const String&);
-
- void fillText(const String& text, double x, double y);
- void fillText(const String& text, double x, double y, double maxWidth);
- void strokeText(const String& text, double x, double y);
- void strokeText(const String& text, double x, double y, double maxWidth);
- TextMetrics* measureText(const String& text);
-
bool imageSmoothingEnabled() const;
void setImageSmoothingEnabled(bool);
String imageSmoothingQuality() const;
void setImageSmoothingQuality(const String&);
- void getContextAttributes(Canvas2DContextAttributes&) const;
+ virtual bool originClean() const = 0;
+ virtual void setOriginTainted() = 0;
+ virtual bool wouldTaintOrigin(CanvasImageSource*) = 0;
- void drawFocusIfNeeded(Element*);
- void drawFocusIfNeeded(Path2D*, Element*);
+ virtual int width() const = 0;
+ virtual int height() const = 0;
- void addHitRegion(const HitRegionOptions&, ExceptionState&);
- void removeHitRegion(const String& id);
- void clearHitRegions();
- HitRegion* hitRegionAtPoint(const FloatPoint&);
- unsigned hitRegionsCount() const override;
+ virtual bool hasImageBuffer() const = 0;
+ virtual ImageBuffer* imageBuffer() const = 0;
- void loseContext(LostContextMode) override;
- void didSetSurfaceSize() override;
+ virtual bool parseColorOrCurrentColor(Color&, const String& colorString) const = 0;
- void restoreCanvasMatrixClipStack(SkCanvas*) const override;
+ virtual SkCanvas* drawingCanvas() const = 0;
+ virtual SkCanvas* existingDrawingCanvas() const = 0;
+ virtual void disableDeferral(DisableDeferralReason) = 0;
- // TaskObserver implementation
- void didProcessTask() override;
- void willProcessTask() override { }
+ virtual AffineTransform baseTransform() const = 0;
- void styleDidChange(const ComputedStyle* oldStyle, const ComputedStyle& newStyle) override;
+ virtual void didDraw(const SkIRect& dirtyRect) = 0;
- // SVGResourceClient implementation
- void filterNeedsInvalidation() override;
+ virtual bool stateHasFilter() = 0;
+ virtual SkImageFilter* stateGetFilter() = 0;
-private:
- friend class CanvasRenderingContext2DAutoRestoreSkCanvas;
+ virtual void validateStateStack() = 0;
- CanvasRenderingContext2D(HTMLCanvasElement*, const CanvasContextCreationAttributes& attrs, Document&);
+ virtual bool hasAlpha() const = 0;
- void dispose();
+ virtual bool isContextLost() const = 0;
- CanvasRenderingContext2DState& modifiableState();
- const CanvasRenderingContext2DState& state() const { return *m_stateStack.last(); }
+ DECLARE_VIRTUAL_TRACE();
- void setShadow(const FloatSize& offset, double blur, RGBA32 color);
+protected:
+ BaseRenderingContext2D();
- void dispatchContextLostEvent(Timer<CanvasRenderingContext2D>*);
- void dispatchContextRestoredEvent(Timer<CanvasRenderingContext2D>*);
- void tryRestoreContextEvent(Timer<CanvasRenderingContext2D>*);
+ CanvasRenderingContext2DState& modifiableState();
+ const CanvasRenderingContext2DState& state() const { return *m_stateStack.last(); }
bool computeDirtyRect(const FloatRect& localBounds, SkIRect*);
bool computeDirtyRect(const FloatRect& localBounds, const SkIRect& transformedClipBounds, SkIRect*);
- void didDraw(const SkIRect&);
- SkCanvas* drawingCanvas() const;
+ template<typename DrawFunc, typename ContainsFunc>
+ bool draw(const DrawFunc&, const ContainsFunc&, const SkRect& bounds, CanvasRenderingContext2DState::PaintType, CanvasRenderingContext2DState::ImageType = CanvasRenderingContext2DState::NoImage);
- void unwindStateStack();
- void realizeSaves();
+ void inflateStrokeRect(FloatRect&) const;
- void pruneLocalFontCache(size_t targetSize);
- void schedulePruneLocalFontCacheIfNeeded();
+ enum DrawType {
+ ClipFill, // Fill that is already known to cover the current clip
+ UntransformedUnclippedFill
+ };
+
+ void checkOverdraw(const SkRect&, const SkPaint*, CanvasRenderingContext2DState::ImageType, DrawType);
+
+ WillBeHeapVector<OwnPtrWillBeMember<CanvasRenderingContext2DState>> m_stateStack;
sof 2016/02/24 15:56:46 This runs into MSVC compilation breakage non-Oilpa
+ AntiAliasingMode m_clipAntialiasing;
+
+private:
+ void realizeSaves();
bool shouldDrawImageAntialiased(const FloatRect& destRect) const;
- template<typename DrawFunc, typename ContainsFunc>
- bool draw(const DrawFunc&, const ContainsFunc&, const SkRect& bounds, CanvasRenderingContext2DState::PaintType, CanvasRenderingContext2DState::ImageType = CanvasRenderingContext2DState::NoImage);
void drawPathInternal(const Path&, CanvasRenderingContext2DState::PaintType, SkPath::FillType = SkPath::kWinding_FillType);
void drawImageInternal(SkCanvas*, CanvasImageSource*, Image*, const FloatRect& srcRect, const FloatRect& dstRect, const SkPaint*);
void clipInternal(const Path&, const String& windingRuleString);
@@ -271,65 +193,99 @@ private:
bool isPointInPathInternal(const Path&, const double x, const double y, const String& windingRuleString);
bool isPointInStrokeInternal(const Path&, const double x, const double y);
- void scrollPathIntoViewInternal(const Path&);
-
- void drawTextInternal(const String&, double x, double y, CanvasRenderingContext2DState::PaintType, double* maxWidth = nullptr);
-
- const Font& accessFont();
- int getFontBaseline(const FontMetrics&) const;
-
- void clearCanvas();
- bool rectContainsTransformedRect(const FloatRect&, const SkIRect&) const;
-
- void inflateStrokeRect(FloatRect&) const;
+ static bool isFullCanvasCompositeMode(SkXfermode::Mode);
template<typename DrawFunc>
void compositedDraw(const DrawFunc&, SkCanvas*, CanvasRenderingContext2DState::PaintType, CanvasRenderingContext2DState::ImageType);
- void drawFocusIfNeededInternal(const Path&, Element*);
- bool focusRingCallIsValid(const Path&, Element*);
- void drawFocusRing(const Path&);
- void updateElementAccessibility(const Path&, Element*);
-
- void validateStateStack();
-
- enum DrawType {
- ClipFill, // Fill that is already known to cover the current clip
- UntransformedUnclippedFill
- };
-
- void checkOverdraw(const SkRect&, const SkPaint*, CanvasRenderingContext2DState::ImageType, DrawType);
-
- CanvasRenderingContext::ContextType contextType() const override { return CanvasRenderingContext::Context2d; }
- bool is2d() const override { return true; }
- bool isAccelerated() const override;
- bool hasAlpha() const override { return m_hasAlpha; }
- void setIsHidden(bool) override;
- void stop() final;
- DECLARE_VIRTUAL_TRACE();
-
- virtual bool isTransformInvertible() const;
-
- WebLayer* platformLayer() const override;
-
- WillBeHeapVector<OwnPtrWillBeMember<CanvasRenderingContext2DState>> m_stateStack;
- PersistentWillBeMember<HitRegionManager> m_hitRegionManager;
- AntiAliasingMode m_clipAntialiasing;
- bool m_hasAlpha;
- LostContextMode m_contextLostMode;
- bool m_contextRestorable;
- unsigned m_tryRestoreContextAttemptCount;
- Timer<CanvasRenderingContext2D> m_dispatchContextLostEventTimer;
- Timer<CanvasRenderingContext2D> m_dispatchContextRestoredEventTimer;
- Timer<CanvasRenderingContext2D> m_tryRestoreContextEventTimer;
-
- HashMap<String, Font> m_fontsResolvedUsingCurrentStyle;
- bool m_pruneLocalFontCacheScheduled;
- ListHashSet<String> m_fontLRUList;
+ void clearCanvas();
+ bool rectContainsTransformedRect(const FloatRect&, const SkIRect&) const;
};
-DEFINE_TYPE_CASTS(CanvasRenderingContext2D, CanvasRenderingContext, context, context->is2d(), context.is2d());
+template<typename DrawFunc, typename ContainsFunc>
+bool BaseRenderingContext2D::draw(const DrawFunc& drawFunc, const ContainsFunc& drawCoversClipBounds, const SkRect& bounds, CanvasRenderingContext2DState::PaintType paintType, CanvasRenderingContext2DState::ImageType imageType)
+{
+ if (!state().isTransformInvertible())
+ return false;
+
+ SkIRect clipBounds;
+ if (!drawingCanvas() || !drawingCanvas()->getClipDeviceBounds(&clipBounds))
+ return false;
+
+ // If gradient size is zero, then paint nothing.
+ CanvasStyle* style = state().style(paintType);
+ if (style) {
+ CanvasGradient* gradient = style->canvasGradient();
+ if (gradient && gradient->gradient()->isZeroSize())
+ return false;
+ }
+
+ if (isFullCanvasCompositeMode(state().globalComposite()) || stateHasFilter()) {
+ compositedDraw(drawFunc, drawingCanvas(), paintType, imageType);
+ didDraw(clipBounds);
+ } else if (state().globalComposite() == SkXfermode::kSrc_Mode) {
+ clearCanvas(); // takes care of checkOverdraw()
+ const SkPaint* paint = state().getPaint(paintType, DrawForegroundOnly, imageType);
+ drawFunc(drawingCanvas(), paint);
+ didDraw(clipBounds);
+ } else {
+ SkIRect dirtyRect;
+ if (computeDirtyRect(bounds, clipBounds, &dirtyRect)) {
+ const SkPaint* paint = state().getPaint(paintType, DrawShadowAndForeground, imageType);
+ if (paintType != CanvasRenderingContext2DState::StrokePaintType && drawCoversClipBounds(clipBounds))
+ checkOverdraw(bounds, paint, imageType, ClipFill);
+ drawFunc(drawingCanvas(), paint);
+ didDraw(dirtyRect);
+ }
+ }
+ return true;
+}
+
+template<typename DrawFunc>
+void BaseRenderingContext2D::compositedDraw(const DrawFunc& drawFunc, SkCanvas* c, CanvasRenderingContext2DState::PaintType paintType, CanvasRenderingContext2DState::ImageType imageType)
+{
+ SkImageFilter* filter = stateGetFilter();
+ ASSERT(isFullCanvasCompositeMode(state().globalComposite()) || filter);
+ SkMatrix ctm = c->getTotalMatrix();
+ c->resetMatrix();
+ SkPaint compositePaint;
+ compositePaint.setXfermodeMode(state().globalComposite());
+ if (state().shouldDrawShadows()) {
+ // unroll into two independently composited passes if drawing shadows
+ SkPaint shadowPaint = *state().getPaint(paintType, DrawShadowOnly, imageType);
+ int saveCount = c->getSaveCount();
+ if (filter) {
+ SkPaint filterPaint;
+ filterPaint.setImageFilter(filter);
+ // TODO(junov): crbug.com/502921 We could use primitive bounds if we knew that the filter
+ // does not affect transparent black regions.
+ c->saveLayer(nullptr, &shadowPaint);
+ c->saveLayer(nullptr, &filterPaint);
+ SkPaint foregroundPaint = *state().getPaint(paintType, DrawForegroundOnly, imageType);
+ c->setMatrix(ctm);
+ drawFunc(c, &foregroundPaint);
+ } else {
+ ASSERT(isFullCanvasCompositeMode(state().globalComposite()));
+ c->saveLayer(nullptr, &compositePaint);
+ shadowPaint.setXfermodeMode(SkXfermode::kSrcOver_Mode);
+ c->setMatrix(ctm);
+ drawFunc(c, &shadowPaint);
+ }
+ c->restoreToCount(saveCount);
+ }
+
+ compositePaint.setImageFilter(filter);
+ // TODO(junov): crbug.com/502921 We could use primitive bounds if we knew that the filter
+ // does not affect transparent black regions *and* !isFullCanvasCompositeMode
+ c->saveLayer(nullptr, &compositePaint);
+ SkPaint foregroundPaint = *state().getPaint(paintType, DrawForegroundOnly, imageType);
+ foregroundPaint.setXfermodeMode(SkXfermode::kSrcOver_Mode);
+ c->setMatrix(ctm);
+ drawFunc(c, &foregroundPaint);
+ c->restore();
+ c->setMatrix(ctm);
+}
} // namespace blink
-#endif // CanvasRenderingContext2D_h
+#endif // BaseRenderingContext2D_h
« no previous file with comments | « no previous file | third_party/WebKit/Source/modules/canvas2d/BaseRenderingContext2D.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698