Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | /* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 4 -*- |
michael@0 | 2 | * This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 5 | |
michael@0 | 6 | #ifndef GFX_DRAWABLE_H |
michael@0 | 7 | #define GFX_DRAWABLE_H |
michael@0 | 8 | |
michael@0 | 9 | #include "nsAutoPtr.h" |
michael@0 | 10 | #include "gfxRect.h" |
michael@0 | 11 | #include "gfxMatrix.h" |
michael@0 | 12 | #include "GraphicsFilter.h" |
michael@0 | 13 | #include "mozilla/gfx/2D.h" |
michael@0 | 14 | |
michael@0 | 15 | class gfxASurface; |
michael@0 | 16 | class gfxImageSurface; |
michael@0 | 17 | class gfxContext; |
michael@0 | 18 | class gfxPattern; |
michael@0 | 19 | |
michael@0 | 20 | /** |
michael@0 | 21 | * gfxDrawable |
michael@0 | 22 | * An Interface representing something that has an intrinsic size and can draw |
michael@0 | 23 | * itself repeatedly. |
michael@0 | 24 | */ |
michael@0 | 25 | class gfxDrawable { |
michael@0 | 26 | NS_INLINE_DECL_REFCOUNTING(gfxDrawable) |
michael@0 | 27 | public: |
michael@0 | 28 | gfxDrawable(const gfxIntSize aSize) |
michael@0 | 29 | : mSize(aSize) {} |
michael@0 | 30 | |
michael@0 | 31 | /** |
michael@0 | 32 | * Draw into aContext filling aFillRect, possibly repeating, using aFilter. |
michael@0 | 33 | * aTransform is a userspace to "image"space matrix. For example, if Draw |
michael@0 | 34 | * draws using a gfxPattern, this is the matrix that should be set on the |
michael@0 | 35 | * pattern prior to rendering it. |
michael@0 | 36 | * @return whether drawing was successful |
michael@0 | 37 | */ |
michael@0 | 38 | virtual bool Draw(gfxContext* aContext, |
michael@0 | 39 | const gfxRect& aFillRect, |
michael@0 | 40 | bool aRepeat, |
michael@0 | 41 | const GraphicsFilter& aFilter, |
michael@0 | 42 | const gfxMatrix& aTransform = gfxMatrix()) = 0; |
michael@0 | 43 | virtual already_AddRefed<gfxImageSurface> GetAsImageSurface() { return nullptr; } |
michael@0 | 44 | virtual gfxIntSize Size() { return mSize; } |
michael@0 | 45 | |
michael@0 | 46 | protected: |
michael@0 | 47 | // Protected destructor, to discourage deletion outside of Release(): |
michael@0 | 48 | virtual ~gfxDrawable() {} |
michael@0 | 49 | |
michael@0 | 50 | const gfxIntSize mSize; |
michael@0 | 51 | }; |
michael@0 | 52 | |
michael@0 | 53 | /** |
michael@0 | 54 | * gfxSurfaceDrawable |
michael@0 | 55 | * A convenience implementation of gfxDrawable for surfaces. |
michael@0 | 56 | */ |
michael@0 | 57 | class gfxSurfaceDrawable : public gfxDrawable { |
michael@0 | 58 | public: |
michael@0 | 59 | gfxSurfaceDrawable(gfxASurface* aSurface, const gfxIntSize aSize, |
michael@0 | 60 | const gfxMatrix aTransform = gfxMatrix()); |
michael@0 | 61 | gfxSurfaceDrawable(mozilla::gfx::DrawTarget* aDT, const gfxIntSize aSize, |
michael@0 | 62 | const gfxMatrix aTransform = gfxMatrix()); |
michael@0 | 63 | gfxSurfaceDrawable(mozilla::gfx::SourceSurface* aSurface, const gfxIntSize aSize, |
michael@0 | 64 | const gfxMatrix aTransform = gfxMatrix()); |
michael@0 | 65 | virtual ~gfxSurfaceDrawable() {} |
michael@0 | 66 | |
michael@0 | 67 | virtual bool Draw(gfxContext* aContext, |
michael@0 | 68 | const gfxRect& aFillRect, |
michael@0 | 69 | bool aRepeat, |
michael@0 | 70 | const GraphicsFilter& aFilter, |
michael@0 | 71 | const gfxMatrix& aTransform = gfxMatrix()); |
michael@0 | 72 | |
michael@0 | 73 | virtual already_AddRefed<gfxImageSurface> GetAsImageSurface(); |
michael@0 | 74 | |
michael@0 | 75 | protected: |
michael@0 | 76 | nsRefPtr<gfxASurface> mSurface; |
michael@0 | 77 | mozilla::RefPtr<mozilla::gfx::DrawTarget> mDrawTarget; |
michael@0 | 78 | mozilla::RefPtr<mozilla::gfx::SourceSurface> mSourceSurface; |
michael@0 | 79 | const gfxMatrix mTransform; |
michael@0 | 80 | }; |
michael@0 | 81 | |
michael@0 | 82 | /** |
michael@0 | 83 | * gfxDrawingCallback |
michael@0 | 84 | * A simple drawing functor. |
michael@0 | 85 | */ |
michael@0 | 86 | class gfxDrawingCallback { |
michael@0 | 87 | NS_INLINE_DECL_REFCOUNTING(gfxDrawingCallback) |
michael@0 | 88 | protected: |
michael@0 | 89 | // Protected destructor, to discourage deletion outside of Release(): |
michael@0 | 90 | virtual ~gfxDrawingCallback() {} |
michael@0 | 91 | |
michael@0 | 92 | public: |
michael@0 | 93 | /** |
michael@0 | 94 | * Draw into aContext filling aFillRect using aFilter. |
michael@0 | 95 | * aTransform is a userspace to "image"space matrix. For example, if Draw |
michael@0 | 96 | * draws using a gfxPattern, this is the matrix that should be set on the |
michael@0 | 97 | * pattern prior to rendering it. |
michael@0 | 98 | * @return whether drawing was successful |
michael@0 | 99 | */ |
michael@0 | 100 | virtual bool operator()(gfxContext* aContext, |
michael@0 | 101 | const gfxRect& aFillRect, |
michael@0 | 102 | const GraphicsFilter& aFilter, |
michael@0 | 103 | const gfxMatrix& aTransform = gfxMatrix()) = 0; |
michael@0 | 104 | |
michael@0 | 105 | }; |
michael@0 | 106 | |
michael@0 | 107 | /** |
michael@0 | 108 | * gfxCallbackDrawable |
michael@0 | 109 | * A convenience implementation of gfxDrawable for callbacks. |
michael@0 | 110 | */ |
michael@0 | 111 | class gfxCallbackDrawable : public gfxDrawable { |
michael@0 | 112 | public: |
michael@0 | 113 | gfxCallbackDrawable(gfxDrawingCallback* aCallback, const gfxIntSize aSize); |
michael@0 | 114 | virtual ~gfxCallbackDrawable() {} |
michael@0 | 115 | |
michael@0 | 116 | virtual bool Draw(gfxContext* aContext, |
michael@0 | 117 | const gfxRect& aFillRect, |
michael@0 | 118 | bool aRepeat, |
michael@0 | 119 | const GraphicsFilter& aFilter, |
michael@0 | 120 | const gfxMatrix& aTransform = gfxMatrix()); |
michael@0 | 121 | |
michael@0 | 122 | protected: |
michael@0 | 123 | already_AddRefed<gfxSurfaceDrawable> MakeSurfaceDrawable(const GraphicsFilter aFilter = GraphicsFilter::FILTER_FAST); |
michael@0 | 124 | |
michael@0 | 125 | nsRefPtr<gfxDrawingCallback> mCallback; |
michael@0 | 126 | nsRefPtr<gfxSurfaceDrawable> mSurfaceDrawable; |
michael@0 | 127 | }; |
michael@0 | 128 | |
michael@0 | 129 | /** |
michael@0 | 130 | * gfxPatternDrawable |
michael@0 | 131 | * A convenience implementation of gfxDrawable for patterns. |
michael@0 | 132 | */ |
michael@0 | 133 | class gfxPatternDrawable : public gfxDrawable { |
michael@0 | 134 | public: |
michael@0 | 135 | gfxPatternDrawable(gfxPattern* aPattern, |
michael@0 | 136 | const gfxIntSize aSize); |
michael@0 | 137 | virtual ~gfxPatternDrawable(); |
michael@0 | 138 | |
michael@0 | 139 | virtual bool Draw(gfxContext* aContext, |
michael@0 | 140 | const gfxRect& aFillRect, |
michael@0 | 141 | bool aRepeat, |
michael@0 | 142 | const GraphicsFilter& aFilter, |
michael@0 | 143 | const gfxMatrix& aTransform = gfxMatrix()); |
michael@0 | 144 | |
michael@0 | 145 | protected: |
michael@0 | 146 | already_AddRefed<gfxCallbackDrawable> MakeCallbackDrawable(); |
michael@0 | 147 | |
michael@0 | 148 | nsRefPtr<gfxPattern> mPattern; |
michael@0 | 149 | }; |
michael@0 | 150 | |
michael@0 | 151 | #endif /* GFX_DRAWABLE_H */ |