2011-06-24 17:41:16 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
2012-05-21 11:12:37 +00:00
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2011-06-24 17:41:16 +00:00
|
|
|
|
|
|
|
#ifndef MOZILLA_GFX_DRAWTARGETD2D_H_
|
|
|
|
#define MOZILLA_GFX_DRAWTARGETD2D_H_
|
|
|
|
|
|
|
|
#include "2D.h"
|
|
|
|
#include "PathD2D.h"
|
|
|
|
#include <d3d10_1.h>
|
|
|
|
#include "HelpersD2D.h"
|
|
|
|
|
|
|
|
#include <vector>
|
|
|
|
#include <sstream>
|
2011-12-19 13:06:10 +00:00
|
|
|
|
|
|
|
#ifdef _MSC_VER
|
2011-12-09 09:51:57 +00:00
|
|
|
#include <hash_set>
|
2011-12-19 13:06:10 +00:00
|
|
|
#else
|
|
|
|
#include <unordered_set>
|
|
|
|
#endif
|
2011-06-24 17:41:16 +00:00
|
|
|
|
2012-05-15 14:57:51 +00:00
|
|
|
struct IDWriteFactory;
|
|
|
|
|
2011-06-24 17:41:16 +00:00
|
|
|
namespace mozilla {
|
|
|
|
namespace gfx {
|
|
|
|
|
|
|
|
class SourceSurfaceD2DTarget;
|
2011-12-28 05:56:11 +00:00
|
|
|
class SourceSurfaceD2D;
|
2011-07-08 18:49:35 +00:00
|
|
|
class GradientStopsD2D;
|
2012-05-15 14:57:51 +00:00
|
|
|
class ScaledFontDWrite;
|
2011-06-24 17:41:16 +00:00
|
|
|
|
2012-06-20 21:41:16 +00:00
|
|
|
const int32_t kLayerCacheSize = 5;
|
|
|
|
|
2011-06-24 17:41:16 +00:00
|
|
|
struct PrivateD3D10DataD2D
|
|
|
|
{
|
|
|
|
RefPtr<ID3D10Effect> mEffect;
|
|
|
|
RefPtr<ID3D10InputLayout> mInputLayout;
|
|
|
|
RefPtr<ID3D10Buffer> mVB;
|
|
|
|
RefPtr<ID3D10BlendState> mBlendStates[OP_COUNT];
|
|
|
|
};
|
|
|
|
|
|
|
|
class DrawTargetD2D : public DrawTarget
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
DrawTargetD2D();
|
|
|
|
virtual ~DrawTargetD2D();
|
|
|
|
|
|
|
|
virtual BackendType GetType() const { return BACKEND_DIRECT2D; }
|
|
|
|
virtual TemporaryRef<SourceSurface> Snapshot();
|
|
|
|
virtual IntSize GetSize() { return mSize; }
|
|
|
|
|
|
|
|
virtual void Flush();
|
|
|
|
virtual void DrawSurface(SourceSurface *aSurface,
|
|
|
|
const Rect &aDest,
|
|
|
|
const Rect &aSource,
|
|
|
|
const DrawSurfaceOptions &aSurfOptions = DrawSurfaceOptions(),
|
|
|
|
const DrawOptions &aOptions = DrawOptions());
|
|
|
|
virtual void DrawSurfaceWithShadow(SourceSurface *aSurface,
|
|
|
|
const Point &aDest,
|
|
|
|
const Color &aColor,
|
|
|
|
const Point &aOffset,
|
2011-07-05 06:28:14 +00:00
|
|
|
Float aSigma,
|
|
|
|
CompositionOp aOperator);
|
2011-06-24 17:41:16 +00:00
|
|
|
virtual void ClearRect(const Rect &aRect);
|
2013-06-13 03:57:51 +00:00
|
|
|
virtual void MaskSurface(const Pattern &aSource,
|
|
|
|
SourceSurface *aMask,
|
|
|
|
Point aOffset,
|
|
|
|
const DrawOptions &aOptions = DrawOptions());
|
|
|
|
|
2011-06-24 17:41:16 +00:00
|
|
|
|
|
|
|
virtual void CopySurface(SourceSurface *aSurface,
|
|
|
|
const IntRect &aSourceRect,
|
|
|
|
const IntPoint &aDestination);
|
|
|
|
|
|
|
|
virtual void FillRect(const Rect &aRect,
|
|
|
|
const Pattern &aPattern,
|
|
|
|
const DrawOptions &aOptions = DrawOptions());
|
|
|
|
virtual void StrokeRect(const Rect &aRect,
|
|
|
|
const Pattern &aPattern,
|
|
|
|
const StrokeOptions &aStrokeOptions = StrokeOptions(),
|
|
|
|
const DrawOptions &aOptions = DrawOptions());
|
|
|
|
virtual void StrokeLine(const Point &aStart,
|
|
|
|
const Point &aEnd,
|
|
|
|
const Pattern &aPattern,
|
|
|
|
const StrokeOptions &aStrokeOptions = StrokeOptions(),
|
|
|
|
const DrawOptions &aOptions = DrawOptions());
|
|
|
|
virtual void Stroke(const Path *aPath,
|
|
|
|
const Pattern &aPattern,
|
|
|
|
const StrokeOptions &aStrokeOptions = StrokeOptions(),
|
|
|
|
const DrawOptions &aOptions = DrawOptions());
|
|
|
|
virtual void Fill(const Path *aPath,
|
|
|
|
const Pattern &aPattern,
|
|
|
|
const DrawOptions &aOptions = DrawOptions());
|
|
|
|
virtual void FillGlyphs(ScaledFont *aFont,
|
|
|
|
const GlyphBuffer &aBuffer,
|
|
|
|
const Pattern &aPattern,
|
2012-03-19 19:20:17 +00:00
|
|
|
const DrawOptions &aOptions = DrawOptions(),
|
2012-08-14 18:06:12 +00:00
|
|
|
const GlyphRenderingOptions *aRenderingOptions = nullptr);
|
2011-12-28 05:56:11 +00:00
|
|
|
virtual void Mask(const Pattern &aSource,
|
|
|
|
const Pattern &aMask,
|
|
|
|
const DrawOptions &aOptions = DrawOptions());
|
2011-06-24 17:41:16 +00:00
|
|
|
virtual void PushClip(const Path *aPath);
|
2011-12-28 05:56:11 +00:00
|
|
|
virtual void PushClipRect(const Rect &aRect);
|
2011-06-24 17:41:16 +00:00
|
|
|
virtual void PopClip();
|
|
|
|
|
|
|
|
virtual TemporaryRef<SourceSurface> CreateSourceSurfaceFromData(unsigned char *aData,
|
|
|
|
const IntSize &aSize,
|
|
|
|
int32_t aStride,
|
|
|
|
SurfaceFormat aFormat) const;
|
|
|
|
virtual TemporaryRef<SourceSurface> OptimizeSourceSurface(SourceSurface *aSurface) const;
|
|
|
|
|
|
|
|
virtual TemporaryRef<SourceSurface>
|
|
|
|
CreateSourceSurfaceFromNativeSurface(const NativeSurface &aSurface) const;
|
|
|
|
|
|
|
|
virtual TemporaryRef<DrawTarget>
|
|
|
|
CreateSimilarDrawTarget(const IntSize &aSize, SurfaceFormat aFormat) const;
|
|
|
|
|
|
|
|
virtual TemporaryRef<PathBuilder> CreatePathBuilder(FillRule aFillRule = FILL_WINDING) const;
|
|
|
|
|
2011-12-28 05:56:11 +00:00
|
|
|
virtual TemporaryRef<GradientStops>
|
|
|
|
CreateGradientStops(GradientStop *aStops,
|
|
|
|
uint32_t aNumStops,
|
|
|
|
ExtendMode aExtendMode = EXTEND_CLAMP) const;
|
2011-06-24 17:41:16 +00:00
|
|
|
|
|
|
|
virtual void *GetNativeSurface(NativeSurfaceType aType);
|
|
|
|
|
|
|
|
bool Init(const IntSize &aSize, SurfaceFormat aFormat);
|
|
|
|
bool Init(ID3D10Texture2D *aTexture, SurfaceFormat aFormat);
|
|
|
|
bool InitD3D10Data();
|
2012-06-20 21:41:16 +00:00
|
|
|
uint32_t GetByteSize() const;
|
2012-06-20 21:41:16 +00:00
|
|
|
TemporaryRef<ID2D1Layer> GetCachedLayer();
|
|
|
|
void PopCachedLayer(ID2D1RenderTarget *aRT);
|
2011-06-24 17:41:16 +00:00
|
|
|
|
|
|
|
static ID2D1Factory *factory();
|
2012-11-25 19:00:31 +00:00
|
|
|
static void CleanupD2D();
|
2012-05-15 14:57:51 +00:00
|
|
|
static IDWriteFactory *GetDWriteFactory();
|
2011-06-24 17:41:16 +00:00
|
|
|
|
|
|
|
operator std::string() const {
|
|
|
|
std::stringstream stream;
|
|
|
|
stream << "DrawTargetD2D(" << this << ")";
|
|
|
|
return stream.str();
|
|
|
|
}
|
2012-06-20 21:41:16 +00:00
|
|
|
|
|
|
|
static uint64_t mVRAMUsageDT;
|
|
|
|
static uint64_t mVRAMUsageSS;
|
|
|
|
|
2011-06-24 17:41:16 +00:00
|
|
|
private:
|
2013-06-13 03:57:51 +00:00
|
|
|
TemporaryRef<ID2D1Bitmap>
|
2013-06-29 10:13:17 +00:00
|
|
|
GetBitmapForSurface(SourceSurface *aSurface,
|
|
|
|
Rect &aSource);
|
2011-06-24 17:41:16 +00:00
|
|
|
friend class AutoSaveRestoreClippedOut;
|
|
|
|
friend class SourceSurfaceD2DTarget;
|
|
|
|
|
2011-12-19 13:06:10 +00:00
|
|
|
#ifdef _MSC_VER
|
2011-12-09 09:51:57 +00:00
|
|
|
typedef stdext::hash_set<DrawTargetD2D*> TargetSet;
|
2011-12-19 13:06:10 +00:00
|
|
|
#else
|
|
|
|
typedef std::unordered_set<DrawTargetD2D*> TargetSet;
|
|
|
|
#endif
|
2011-12-09 09:51:57 +00:00
|
|
|
|
2011-06-24 17:41:16 +00:00
|
|
|
bool InitD2DRenderTarget();
|
|
|
|
void PrepareForDrawing(ID2D1RenderTarget *aRT);
|
|
|
|
|
|
|
|
// This function will mark the surface as changing, and make sure any
|
|
|
|
// copy-on-write snapshots are notified.
|
|
|
|
void MarkChanged();
|
2011-12-09 09:53:14 +00:00
|
|
|
void FlushTransformToRT() {
|
|
|
|
if (mTransformDirty) {
|
|
|
|
mRT->SetTransform(D2DMatrix(mTransform));
|
|
|
|
mTransformDirty = false;
|
|
|
|
}
|
|
|
|
}
|
2011-12-09 09:51:57 +00:00
|
|
|
void AddDependencyOnSource(SourceSurfaceD2DTarget* aSource);
|
2011-06-24 17:41:16 +00:00
|
|
|
|
|
|
|
ID3D10BlendState *GetBlendStateForOperator(CompositionOp aOperator);
|
2011-07-08 18:49:35 +00:00
|
|
|
ID2D1RenderTarget *GetRTForOperation(CompositionOp aOperator, const Pattern &aPattern);
|
|
|
|
void FinalizeRTForOperation(CompositionOp aOperator, const Pattern &aPattern, const Rect &aBounds); void EnsureViews();
|
2011-06-24 17:41:16 +00:00
|
|
|
void PopAllClips();
|
2012-05-15 14:57:51 +00:00
|
|
|
void PushClipsToRT(ID2D1RenderTarget *aRT);
|
|
|
|
void PopClipsFromRT(ID2D1RenderTarget *aRT);
|
2011-06-24 17:41:16 +00:00
|
|
|
|
2012-05-15 14:57:51 +00:00
|
|
|
// This function ensures mCurrentClipMaskTexture contains a texture containing
|
2012-09-18 23:04:42 +00:00
|
|
|
// a mask corresponding with the current DrawTarget clip. See
|
|
|
|
// GetClippedGeometry for a description of aClipBounds.
|
|
|
|
void EnsureClipMaskTexture(IntRect *aClipBounds);
|
2012-05-15 14:57:51 +00:00
|
|
|
|
2012-05-15 14:57:51 +00:00
|
|
|
bool FillGlyphsManual(ScaledFontDWrite *aFont,
|
|
|
|
const GlyphBuffer &aBuffer,
|
|
|
|
const Color &aColor,
|
|
|
|
IDWriteRenderingParams *aParams,
|
|
|
|
const DrawOptions &aOptions = DrawOptions());
|
|
|
|
|
2012-05-15 14:57:51 +00:00
|
|
|
TemporaryRef<ID2D1RenderTarget> CreateRTForTexture(ID3D10Texture2D *aTexture, SurfaceFormat aFormat);
|
2012-09-18 23:04:42 +00:00
|
|
|
|
|
|
|
// This returns the clipped geometry, in addition it returns aClipBounds which
|
|
|
|
// represents the intersection of all pixel-aligned rectangular clips that
|
|
|
|
// are currently set. The returned clipped geometry must be clipped by these
|
|
|
|
// bounds to correctly reflect the total clip. This is in device space.
|
|
|
|
TemporaryRef<ID2D1Geometry> GetClippedGeometry(IntRect *aClipBounds);
|
2011-06-24 17:41:16 +00:00
|
|
|
|
|
|
|
TemporaryRef<ID2D1Brush> CreateBrushForPattern(const Pattern &aPattern, Float aAlpha = 1.0f);
|
|
|
|
|
2012-05-21 15:24:05 +00:00
|
|
|
TemporaryRef<ID3D10Texture2D> CreateGradientTexture(const GradientStopsD2D *aStops);
|
2012-05-15 14:57:51 +00:00
|
|
|
TemporaryRef<ID3D10Texture2D> CreateTextureForAnalysis(IDWriteGlyphRunAnalysis *aAnalysis, const IntRect &aBounds);
|
2011-07-08 18:49:35 +00:00
|
|
|
|
|
|
|
void SetupEffectForRadialGradient(const RadialGradientPattern *aPattern);
|
2012-05-15 14:57:51 +00:00
|
|
|
void SetupStateForRendering();
|
2011-07-08 18:49:35 +00:00
|
|
|
|
2012-09-18 23:04:42 +00:00
|
|
|
// Set the scissor rect to a certain IntRects, resets the scissor rect to
|
2013-07-20 08:48:55 +00:00
|
|
|
// surface bounds when nullptr is specified.
|
2012-09-18 23:04:42 +00:00
|
|
|
void SetScissorToRect(IntRect *aRect);
|
|
|
|
|
2012-10-08 10:17:17 +00:00
|
|
|
void PushD2DLayer(ID2D1RenderTarget *aRT, ID2D1Geometry *aGeometry, ID2D1Layer *aLayer, const D2D1_MATRIX_3X2_F &aTransform);
|
2012-10-06 23:43:16 +00:00
|
|
|
|
2011-07-08 18:49:35 +00:00
|
|
|
static const uint32_t test = 4;
|
|
|
|
|
2011-06-24 17:41:16 +00:00
|
|
|
IntSize mSize;
|
|
|
|
|
|
|
|
RefPtr<ID3D10Device1> mDevice;
|
|
|
|
RefPtr<ID3D10Texture2D> mTexture;
|
2012-05-15 14:57:51 +00:00
|
|
|
RefPtr<ID3D10Texture2D> mCurrentClipMaskTexture;
|
2012-08-10 11:18:36 +00:00
|
|
|
RefPtr<ID2D1Geometry> mCurrentClippedGeometry;
|
2012-09-18 23:04:42 +00:00
|
|
|
// This is only valid if mCurrentClippedGeometry is non-null. And will
|
|
|
|
// only be the intersection of all pixel-aligned retangular clips. This is in
|
|
|
|
// device space.
|
|
|
|
IntRect mCurrentClipBounds;
|
2011-06-24 17:41:16 +00:00
|
|
|
mutable RefPtr<ID2D1RenderTarget> mRT;
|
|
|
|
|
2012-06-26 00:59:18 +00:00
|
|
|
// We store this to prevent excessive SetTextRenderingParams calls.
|
|
|
|
RefPtr<IDWriteRenderingParams> mTextRenderingParams;
|
|
|
|
|
2011-06-24 17:41:16 +00:00
|
|
|
// Temporary texture and render target used for supporting alternative operators.
|
|
|
|
RefPtr<ID3D10Texture2D> mTempTexture;
|
|
|
|
RefPtr<ID3D10RenderTargetView> mRTView;
|
|
|
|
RefPtr<ID3D10ShaderResourceView> mSRView;
|
|
|
|
RefPtr<ID2D1RenderTarget> mTempRT;
|
|
|
|
RefPtr<ID3D10RenderTargetView> mTempRTView;
|
|
|
|
|
|
|
|
// List of pushed clips.
|
|
|
|
struct PushedClip
|
|
|
|
{
|
|
|
|
RefPtr<ID2D1Layer> mLayer;
|
|
|
|
D2D1_RECT_F mBounds;
|
2012-09-18 23:04:42 +00:00
|
|
|
union {
|
2013-07-20 08:48:55 +00:00
|
|
|
// If mPath is non-nullptr, the mTransform member will be used, otherwise
|
2012-09-18 23:04:42 +00:00
|
|
|
// the mIsPixelAligned member is valid.
|
|
|
|
D2D1_MATRIX_3X2_F mTransform;
|
|
|
|
bool mIsPixelAligned;
|
|
|
|
};
|
2011-06-24 17:41:16 +00:00
|
|
|
RefPtr<PathD2D> mPath;
|
|
|
|
};
|
|
|
|
std::vector<PushedClip> mPushedClips;
|
2012-06-20 21:41:16 +00:00
|
|
|
|
|
|
|
// We cache ID2D1Layer objects as it causes D2D to keep around textures that
|
|
|
|
// serve as the temporary surfaces for these operations. As texture creation
|
|
|
|
// is quite expensive this considerably improved performance.
|
|
|
|
// Careful here, RAII will not ensure destruction of the RefPtrs.
|
|
|
|
RefPtr<ID2D1Layer> mCachedLayers[kLayerCacheSize];
|
|
|
|
uint32_t mCurrentCachedLayer;
|
2011-06-24 17:41:16 +00:00
|
|
|
|
2011-12-09 09:51:57 +00:00
|
|
|
// The latest snapshot of this surface. This needs to be told when this
|
|
|
|
// target is modified. We keep it alive as a cache.
|
|
|
|
RefPtr<SourceSurfaceD2DTarget> mSnapshot;
|
2011-06-24 17:41:16 +00:00
|
|
|
// A list of targets we need to flush when we're modified.
|
2011-12-09 09:51:57 +00:00
|
|
|
TargetSet mDependentTargets;
|
|
|
|
// A list of targets which have this object in their mDependentTargets set
|
|
|
|
TargetSet mDependingOnTargets;
|
2011-06-24 17:41:16 +00:00
|
|
|
|
|
|
|
// True of the current clip stack is pushed to the main RT.
|
|
|
|
bool mClipsArePushed;
|
|
|
|
PrivateD3D10DataD2D *mPrivateData;
|
|
|
|
static ID2D1Factory *mFactory;
|
2012-05-15 14:57:51 +00:00
|
|
|
static IDWriteFactory *mDWriteFactory;
|
2011-06-24 17:41:16 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* MOZILLA_GFX_DRAWTARGETD2D_H_ */
|