michael@0: /* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*- michael@0: * This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #ifndef MOZILLA_GFX_DRAWTARGETD2D1_H_ michael@0: #define MOZILLA_GFX_DRAWTARGETD2D1_H_ michael@0: michael@0: #include "2D.h" michael@0: #include michael@0: #include michael@0: #include "PathD2D.h" michael@0: #include "HelpersD2D.h" michael@0: michael@0: #include michael@0: #include michael@0: michael@0: #ifdef _MSC_VER michael@0: #include michael@0: #else michael@0: #include michael@0: #endif michael@0: michael@0: struct IDWriteFactory; michael@0: michael@0: namespace mozilla { michael@0: namespace gfx { michael@0: michael@0: class SourceSurfaceD2D1; michael@0: class GradientStopsD2D; michael@0: class ScaledFontDWrite; michael@0: michael@0: const int32_t kLayerCacheSize1 = 5; michael@0: michael@0: class DrawTargetD2D1 : public DrawTarget michael@0: { michael@0: public: michael@0: MOZ_DECLARE_REFCOUNTED_VIRTUAL_TYPENAME(DrawTargetD2D1) michael@0: DrawTargetD2D1(); michael@0: virtual ~DrawTargetD2D1(); michael@0: michael@0: virtual BackendType GetType() const { return BackendType::DIRECT2D1_1; } michael@0: virtual TemporaryRef Snapshot(); michael@0: virtual IntSize GetSize() { return mSize; } michael@0: michael@0: virtual void Flush(); michael@0: virtual void DrawSurface(SourceSurface *aSurface, michael@0: const Rect &aDest, michael@0: const Rect &aSource, michael@0: const DrawSurfaceOptions &aSurfOptions, michael@0: const DrawOptions &aOptions); michael@0: virtual void DrawFilter(FilterNode *aNode, michael@0: const Rect &aSourceRect, michael@0: const Point &aDestPoint, michael@0: const DrawOptions &aOptions = DrawOptions()); michael@0: virtual void DrawSurfaceWithShadow(SourceSurface *aSurface, michael@0: const Point &aDest, michael@0: const Color &aColor, michael@0: const Point &aOffset, michael@0: Float aSigma, michael@0: CompositionOp aOperator); michael@0: virtual void ClearRect(const Rect &aRect); michael@0: virtual void MaskSurface(const Pattern &aSource, michael@0: SourceSurface *aMask, michael@0: Point aOffset, michael@0: const DrawOptions &aOptions = DrawOptions()); michael@0: michael@0: virtual void CopySurface(SourceSurface *aSurface, michael@0: const IntRect &aSourceRect, michael@0: const IntPoint &aDestination); michael@0: michael@0: virtual void FillRect(const Rect &aRect, michael@0: const Pattern &aPattern, michael@0: const DrawOptions &aOptions = DrawOptions()); michael@0: virtual void StrokeRect(const Rect &aRect, michael@0: const Pattern &aPattern, michael@0: const StrokeOptions &aStrokeOptions = StrokeOptions(), michael@0: const DrawOptions &aOptions = DrawOptions()); michael@0: virtual void StrokeLine(const Point &aStart, michael@0: const Point &aEnd, michael@0: const Pattern &aPattern, michael@0: const StrokeOptions &aStrokeOptions = StrokeOptions(), michael@0: const DrawOptions &aOptions = DrawOptions()); michael@0: virtual void Stroke(const Path *aPath, michael@0: const Pattern &aPattern, michael@0: const StrokeOptions &aStrokeOptions = StrokeOptions(), michael@0: const DrawOptions &aOptions = DrawOptions()); michael@0: virtual void Fill(const Path *aPath, michael@0: const Pattern &aPattern, michael@0: const DrawOptions &aOptions = DrawOptions()); michael@0: virtual void FillGlyphs(ScaledFont *aFont, michael@0: const GlyphBuffer &aBuffer, michael@0: const Pattern &aPattern, michael@0: const DrawOptions &aOptions = DrawOptions(), michael@0: const GlyphRenderingOptions *aRenderingOptions = nullptr); michael@0: virtual void Mask(const Pattern &aSource, michael@0: const Pattern &aMask, michael@0: const DrawOptions &aOptions = DrawOptions()); michael@0: virtual void PushClip(const Path *aPath); michael@0: virtual void PushClipRect(const Rect &aRect); michael@0: virtual void PopClip(); michael@0: michael@0: virtual TemporaryRef CreateSourceSurfaceFromData(unsigned char *aData, michael@0: const IntSize &aSize, michael@0: int32_t aStride, michael@0: SurfaceFormat aFormat) const; michael@0: virtual TemporaryRef OptimizeSourceSurface(SourceSurface *aSurface) const; michael@0: michael@0: virtual TemporaryRef michael@0: CreateSourceSurfaceFromNativeSurface(const NativeSurface &aSurface) const { return nullptr; } michael@0: michael@0: virtual TemporaryRef michael@0: CreateSimilarDrawTarget(const IntSize &aSize, SurfaceFormat aFormat) const; michael@0: michael@0: virtual TemporaryRef CreatePathBuilder(FillRule aFillRule = FillRule::FILL_WINDING) const; michael@0: michael@0: virtual TemporaryRef michael@0: CreateGradientStops(GradientStop *aStops, michael@0: uint32_t aNumStops, michael@0: ExtendMode aExtendMode = ExtendMode::CLAMP) const; michael@0: michael@0: virtual TemporaryRef CreateFilter(FilterType aType); michael@0: michael@0: virtual void *GetNativeSurface(NativeSurfaceType aType) { return nullptr; } michael@0: michael@0: bool Init(const IntSize &aSize, SurfaceFormat aFormat); michael@0: uint32_t GetByteSize() const; michael@0: michael@0: TemporaryRef GetImageForSurface(SourceSurface *aSurface, Matrix &aSourceTransform, michael@0: ExtendMode aExtendMode); michael@0: michael@0: TemporaryRef GetImageForSurface(SourceSurface *aSurface, ExtendMode aExtendMode) { michael@0: Matrix mat; michael@0: return GetImageForSurface(aSurface, mat, aExtendMode); michael@0: } michael@0: michael@0: static ID2D1Factory1 *factory(); michael@0: static void CleanupD2D(); michael@0: static IDWriteFactory *GetDWriteFactory(); michael@0: michael@0: operator std::string() const { michael@0: std::stringstream stream; michael@0: stream << "DrawTargetD2D 1.1 (" << this << ")"; michael@0: return stream.str(); michael@0: } michael@0: michael@0: static uint64_t mVRAMUsageDT; michael@0: static uint64_t mVRAMUsageSS; michael@0: michael@0: private: michael@0: friend class SourceSurfaceD2D1; michael@0: michael@0: #ifdef _MSC_VER michael@0: typedef stdext::hash_set TargetSet; michael@0: #else michael@0: typedef std::unordered_set TargetSet; michael@0: #endif michael@0: michael@0: // This function will mark the surface as changing, and make sure any michael@0: // copy-on-write snapshots are notified. michael@0: void MarkChanged(); michael@0: void PrepareForDrawing(CompositionOp aOp, const Pattern &aPattern); michael@0: void FinalizeDrawing(CompositionOp aOp, const Pattern &aPattern); michael@0: void FlushTransformToDC() { michael@0: if (mTransformDirty) { michael@0: mDC->SetTransform(D2DMatrix(mTransform)); michael@0: mTransformDirty = false; michael@0: } michael@0: } michael@0: void AddDependencyOnSource(SourceSurfaceD2D1* aSource); michael@0: michael@0: void PopAllClips(); michael@0: void PushClipsToDC(ID2D1DeviceContext *aDC); michael@0: void PopClipsFromDC(ID2D1DeviceContext *aDC); michael@0: michael@0: TemporaryRef CreateBrushForPattern(const Pattern &aPattern, Float aAlpha = 1.0f); michael@0: michael@0: void PushD2DLayer(ID2D1DeviceContext *aDC, ID2D1Geometry *aGeometry, const D2D1_MATRIX_3X2_F &aTransform); michael@0: michael@0: IntSize mSize; michael@0: michael@0: RefPtr mDevice; michael@0: RefPtr mTexture; michael@0: // This is only valid if mCurrentClippedGeometry is non-null. And will michael@0: // only be the intersection of all pixel-aligned retangular clips. This is in michael@0: // device space. michael@0: IntRect mCurrentClipBounds; michael@0: mutable RefPtr mDC; michael@0: RefPtr mBitmap; michael@0: RefPtr mTempBitmap; michael@0: michael@0: // We store this to prevent excessive SetTextRenderingParams calls. michael@0: RefPtr mTextRenderingParams; michael@0: michael@0: // List of pushed clips. michael@0: struct PushedClip michael@0: { michael@0: D2D1_RECT_F mBounds; michael@0: union { michael@0: // If mPath is non-null, the mTransform member will be used, otherwise michael@0: // the mIsPixelAligned member is valid. michael@0: D2D1_MATRIX_3X2_F mTransform; michael@0: bool mIsPixelAligned; michael@0: }; michael@0: RefPtr mPath; michael@0: }; michael@0: std::vector mPushedClips; michael@0: michael@0: // The latest snapshot of this surface. This needs to be told when this michael@0: // target is modified. We keep it alive as a cache. michael@0: RefPtr mSnapshot; michael@0: // A list of targets we need to flush when we're modified. michael@0: TargetSet mDependentTargets; michael@0: // A list of targets which have this object in their mDependentTargets set michael@0: TargetSet mDependingOnTargets; michael@0: michael@0: // True of the current clip stack is pushed to the main RT. michael@0: bool mClipsArePushed; michael@0: static ID2D1Factory1 *mFactory; michael@0: static IDWriteFactory *mDWriteFactory; michael@0: }; michael@0: michael@0: } michael@0: } michael@0: michael@0: #endif /* MOZILLA_GFX_DRAWTARGETD2D_H_ */