1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/gfx/2d/DrawTargetD2D1.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,226 @@ 1.4 +/* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*- 1.5 + * This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +#ifndef MOZILLA_GFX_DRAWTARGETD2D1_H_ 1.10 +#define MOZILLA_GFX_DRAWTARGETD2D1_H_ 1.11 + 1.12 +#include "2D.h" 1.13 +#include <d3d11.h> 1.14 +#include <d2d1_1.h> 1.15 +#include "PathD2D.h" 1.16 +#include "HelpersD2D.h" 1.17 + 1.18 +#include <vector> 1.19 +#include <sstream> 1.20 + 1.21 +#ifdef _MSC_VER 1.22 +#include <hash_set> 1.23 +#else 1.24 +#include <unordered_set> 1.25 +#endif 1.26 + 1.27 +struct IDWriteFactory; 1.28 + 1.29 +namespace mozilla { 1.30 +namespace gfx { 1.31 + 1.32 +class SourceSurfaceD2D1; 1.33 +class GradientStopsD2D; 1.34 +class ScaledFontDWrite; 1.35 + 1.36 +const int32_t kLayerCacheSize1 = 5; 1.37 + 1.38 +class DrawTargetD2D1 : public DrawTarget 1.39 +{ 1.40 +public: 1.41 + MOZ_DECLARE_REFCOUNTED_VIRTUAL_TYPENAME(DrawTargetD2D1) 1.42 + DrawTargetD2D1(); 1.43 + virtual ~DrawTargetD2D1(); 1.44 + 1.45 + virtual BackendType GetType() const { return BackendType::DIRECT2D1_1; } 1.46 + virtual TemporaryRef<SourceSurface> Snapshot(); 1.47 + virtual IntSize GetSize() { return mSize; } 1.48 + 1.49 + virtual void Flush(); 1.50 + virtual void DrawSurface(SourceSurface *aSurface, 1.51 + const Rect &aDest, 1.52 + const Rect &aSource, 1.53 + const DrawSurfaceOptions &aSurfOptions, 1.54 + const DrawOptions &aOptions); 1.55 + virtual void DrawFilter(FilterNode *aNode, 1.56 + const Rect &aSourceRect, 1.57 + const Point &aDestPoint, 1.58 + const DrawOptions &aOptions = DrawOptions()); 1.59 + virtual void DrawSurfaceWithShadow(SourceSurface *aSurface, 1.60 + const Point &aDest, 1.61 + const Color &aColor, 1.62 + const Point &aOffset, 1.63 + Float aSigma, 1.64 + CompositionOp aOperator); 1.65 + virtual void ClearRect(const Rect &aRect); 1.66 + virtual void MaskSurface(const Pattern &aSource, 1.67 + SourceSurface *aMask, 1.68 + Point aOffset, 1.69 + const DrawOptions &aOptions = DrawOptions()); 1.70 + 1.71 + virtual void CopySurface(SourceSurface *aSurface, 1.72 + const IntRect &aSourceRect, 1.73 + const IntPoint &aDestination); 1.74 + 1.75 + virtual void FillRect(const Rect &aRect, 1.76 + const Pattern &aPattern, 1.77 + const DrawOptions &aOptions = DrawOptions()); 1.78 + virtual void StrokeRect(const Rect &aRect, 1.79 + const Pattern &aPattern, 1.80 + const StrokeOptions &aStrokeOptions = StrokeOptions(), 1.81 + const DrawOptions &aOptions = DrawOptions()); 1.82 + virtual void StrokeLine(const Point &aStart, 1.83 + const Point &aEnd, 1.84 + const Pattern &aPattern, 1.85 + const StrokeOptions &aStrokeOptions = StrokeOptions(), 1.86 + const DrawOptions &aOptions = DrawOptions()); 1.87 + virtual void Stroke(const Path *aPath, 1.88 + const Pattern &aPattern, 1.89 + const StrokeOptions &aStrokeOptions = StrokeOptions(), 1.90 + const DrawOptions &aOptions = DrawOptions()); 1.91 + virtual void Fill(const Path *aPath, 1.92 + const Pattern &aPattern, 1.93 + const DrawOptions &aOptions = DrawOptions()); 1.94 + virtual void FillGlyphs(ScaledFont *aFont, 1.95 + const GlyphBuffer &aBuffer, 1.96 + const Pattern &aPattern, 1.97 + const DrawOptions &aOptions = DrawOptions(), 1.98 + const GlyphRenderingOptions *aRenderingOptions = nullptr); 1.99 + virtual void Mask(const Pattern &aSource, 1.100 + const Pattern &aMask, 1.101 + const DrawOptions &aOptions = DrawOptions()); 1.102 + virtual void PushClip(const Path *aPath); 1.103 + virtual void PushClipRect(const Rect &aRect); 1.104 + virtual void PopClip(); 1.105 + 1.106 + virtual TemporaryRef<SourceSurface> CreateSourceSurfaceFromData(unsigned char *aData, 1.107 + const IntSize &aSize, 1.108 + int32_t aStride, 1.109 + SurfaceFormat aFormat) const; 1.110 + virtual TemporaryRef<SourceSurface> OptimizeSourceSurface(SourceSurface *aSurface) const; 1.111 + 1.112 + virtual TemporaryRef<SourceSurface> 1.113 + CreateSourceSurfaceFromNativeSurface(const NativeSurface &aSurface) const { return nullptr; } 1.114 + 1.115 + virtual TemporaryRef<DrawTarget> 1.116 + CreateSimilarDrawTarget(const IntSize &aSize, SurfaceFormat aFormat) const; 1.117 + 1.118 + virtual TemporaryRef<PathBuilder> CreatePathBuilder(FillRule aFillRule = FillRule::FILL_WINDING) const; 1.119 + 1.120 + virtual TemporaryRef<GradientStops> 1.121 + CreateGradientStops(GradientStop *aStops, 1.122 + uint32_t aNumStops, 1.123 + ExtendMode aExtendMode = ExtendMode::CLAMP) const; 1.124 + 1.125 + virtual TemporaryRef<FilterNode> CreateFilter(FilterType aType); 1.126 + 1.127 + virtual void *GetNativeSurface(NativeSurfaceType aType) { return nullptr; } 1.128 + 1.129 + bool Init(const IntSize &aSize, SurfaceFormat aFormat); 1.130 + uint32_t GetByteSize() const; 1.131 + 1.132 + TemporaryRef<ID2D1Image> GetImageForSurface(SourceSurface *aSurface, Matrix &aSourceTransform, 1.133 + ExtendMode aExtendMode); 1.134 + 1.135 + TemporaryRef<ID2D1Image> GetImageForSurface(SourceSurface *aSurface, ExtendMode aExtendMode) { 1.136 + Matrix mat; 1.137 + return GetImageForSurface(aSurface, mat, aExtendMode); 1.138 + } 1.139 + 1.140 + static ID2D1Factory1 *factory(); 1.141 + static void CleanupD2D(); 1.142 + static IDWriteFactory *GetDWriteFactory(); 1.143 + 1.144 + operator std::string() const { 1.145 + std::stringstream stream; 1.146 + stream << "DrawTargetD2D 1.1 (" << this << ")"; 1.147 + return stream.str(); 1.148 + } 1.149 + 1.150 + static uint64_t mVRAMUsageDT; 1.151 + static uint64_t mVRAMUsageSS; 1.152 + 1.153 +private: 1.154 + friend class SourceSurfaceD2D1; 1.155 + 1.156 +#ifdef _MSC_VER 1.157 + typedef stdext::hash_set<DrawTargetD2D1*> TargetSet; 1.158 +#else 1.159 + typedef std::unordered_set<DrawTargetD2D1*> TargetSet; 1.160 +#endif 1.161 + 1.162 + // This function will mark the surface as changing, and make sure any 1.163 + // copy-on-write snapshots are notified. 1.164 + void MarkChanged(); 1.165 + void PrepareForDrawing(CompositionOp aOp, const Pattern &aPattern); 1.166 + void FinalizeDrawing(CompositionOp aOp, const Pattern &aPattern); 1.167 + void FlushTransformToDC() { 1.168 + if (mTransformDirty) { 1.169 + mDC->SetTransform(D2DMatrix(mTransform)); 1.170 + mTransformDirty = false; 1.171 + } 1.172 + } 1.173 + void AddDependencyOnSource(SourceSurfaceD2D1* aSource); 1.174 + 1.175 + void PopAllClips(); 1.176 + void PushClipsToDC(ID2D1DeviceContext *aDC); 1.177 + void PopClipsFromDC(ID2D1DeviceContext *aDC); 1.178 + 1.179 + TemporaryRef<ID2D1Brush> CreateBrushForPattern(const Pattern &aPattern, Float aAlpha = 1.0f); 1.180 + 1.181 + void PushD2DLayer(ID2D1DeviceContext *aDC, ID2D1Geometry *aGeometry, const D2D1_MATRIX_3X2_F &aTransform); 1.182 + 1.183 + IntSize mSize; 1.184 + 1.185 + RefPtr<ID3D11Device> mDevice; 1.186 + RefPtr<ID3D11Texture2D> mTexture; 1.187 + // This is only valid if mCurrentClippedGeometry is non-null. And will 1.188 + // only be the intersection of all pixel-aligned retangular clips. This is in 1.189 + // device space. 1.190 + IntRect mCurrentClipBounds; 1.191 + mutable RefPtr<ID2D1DeviceContext> mDC; 1.192 + RefPtr<ID2D1Bitmap1> mBitmap; 1.193 + RefPtr<ID2D1Bitmap1> mTempBitmap; 1.194 + 1.195 + // We store this to prevent excessive SetTextRenderingParams calls. 1.196 + RefPtr<IDWriteRenderingParams> mTextRenderingParams; 1.197 + 1.198 + // List of pushed clips. 1.199 + struct PushedClip 1.200 + { 1.201 + D2D1_RECT_F mBounds; 1.202 + union { 1.203 + // If mPath is non-null, the mTransform member will be used, otherwise 1.204 + // the mIsPixelAligned member is valid. 1.205 + D2D1_MATRIX_3X2_F mTransform; 1.206 + bool mIsPixelAligned; 1.207 + }; 1.208 + RefPtr<PathD2D> mPath; 1.209 + }; 1.210 + std::vector<PushedClip> mPushedClips; 1.211 + 1.212 + // The latest snapshot of this surface. This needs to be told when this 1.213 + // target is modified. We keep it alive as a cache. 1.214 + RefPtr<SourceSurfaceD2D1> mSnapshot; 1.215 + // A list of targets we need to flush when we're modified. 1.216 + TargetSet mDependentTargets; 1.217 + // A list of targets which have this object in their mDependentTargets set 1.218 + TargetSet mDependingOnTargets; 1.219 + 1.220 + // True of the current clip stack is pushed to the main RT. 1.221 + bool mClipsArePushed; 1.222 + static ID2D1Factory1 *mFactory; 1.223 + static IDWriteFactory *mDWriteFactory; 1.224 +}; 1.225 + 1.226 +} 1.227 +} 1.228 + 1.229 +#endif /* MOZILLA_GFX_DRAWTARGETD2D_H_ */