1 /* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*- 2 * This Source Code Form is subject to the terms of the Mozilla Public 3 * License, v. 2.0. If a copy of the MPL was not distributed with this 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 5 6 #ifndef GFX_DRAWABLE_H 7 #define GFX_DRAWABLE_H 8 9 #include "gfxRect.h" 10 #include "gfxMatrix.h" 11 #include "gfxTypes.h" 12 #include "mozilla/gfx/2D.h" 13 #include "mozilla/gfx/Types.h" 14 #include "nsISupportsImpl.h" 15 16 class gfxContext; 17 class gfxPattern; 18 19 /** 20 * gfxDrawable 21 * An Interface representing something that has an intrinsic size and can draw 22 * itself repeatedly. 23 */ 24 class gfxDrawable { 25 NS_INLINE_DECL_REFCOUNTING(gfxDrawable) 26 public: 27 typedef mozilla::gfx::AntialiasMode AntialiasMode; 28 typedef mozilla::gfx::CompositionOp CompositionOp; 29 typedef mozilla::gfx::DrawTarget DrawTarget; 30 gfxDrawable(const mozilla::gfx::IntSize aSize)31 explicit gfxDrawable(const mozilla::gfx::IntSize aSize) : mSize(aSize) {} 32 33 /** 34 * Draw into aContext filling aFillRect, possibly repeating, using 35 * aSamplingFilter. aTransform is a userspace to "image"space matrix. For 36 * example, if Draw draws using a gfxPattern, this is the matrix that should 37 * be set on the pattern prior to rendering it. 38 * @return whether drawing was successful 39 */ 40 virtual bool Draw(gfxContext* aContext, const gfxRect& aFillRect, 41 mozilla::gfx::ExtendMode aExtendMode, 42 const mozilla::gfx::SamplingFilter aSamplingFilter, 43 gfxFloat aOpacity = 1.0, 44 const gfxMatrix& aTransform = gfxMatrix()) = 0; 45 46 virtual bool DrawWithSamplingRect( 47 DrawTarget* aDrawTarget, CompositionOp aOp, AntialiasMode aAntialiasMode, 48 const gfxRect& aFillRect, const gfxRect& aSamplingRect, 49 mozilla::gfx::ExtendMode aExtendMode, 50 const mozilla::gfx::SamplingFilter aSamplingFilter, 51 gfxFloat aOpacity = 1.0) { 52 return false; 53 } 54 Size()55 virtual mozilla::gfx::IntSize Size() { return mSize; } 56 57 protected: 58 // Protected destructor, to discourage deletion outside of Release(): 59 virtual ~gfxDrawable() = default; 60 61 const mozilla::gfx::IntSize mSize; 62 }; 63 64 /** 65 * gfxSurfaceDrawable 66 * A convenience implementation of gfxDrawable for surfaces. 67 */ 68 class gfxSurfaceDrawable : public gfxDrawable { 69 public: 70 gfxSurfaceDrawable(mozilla::gfx::SourceSurface* aSurface, 71 const mozilla::gfx::IntSize aSize, 72 const gfxMatrix aTransform = gfxMatrix()); 73 virtual ~gfxSurfaceDrawable() = default; 74 75 virtual bool Draw(gfxContext* aContext, const gfxRect& aFillRect, 76 mozilla::gfx::ExtendMode aExtendMode, 77 const mozilla::gfx::SamplingFilter aSamplingFilter, 78 gfxFloat aOpacity = 1.0, 79 const gfxMatrix& aTransform = gfxMatrix()) override; 80 81 virtual bool DrawWithSamplingRect( 82 DrawTarget* aDrawTarget, CompositionOp aOp, AntialiasMode aAntialiasMode, 83 const gfxRect& aFillRect, const gfxRect& aSamplingRect, 84 mozilla::gfx::ExtendMode aExtendMode, 85 const mozilla::gfx::SamplingFilter aSamplingFilter, 86 gfxFloat aOpacity = 1.0) override; 87 88 protected: 89 void DrawInternal(DrawTarget* aDrawTarget, CompositionOp aOp, 90 AntialiasMode aAntialiasMode, const gfxRect& aFillRect, 91 const mozilla::gfx::IntRect& aSamplingRect, 92 mozilla::gfx::ExtendMode aExtendMode, 93 const mozilla::gfx::SamplingFilter aSamplingFilter, 94 gfxFloat aOpacity, 95 const gfxMatrix& aTransform = gfxMatrix()); 96 97 RefPtr<mozilla::gfx::SourceSurface> mSourceSurface; 98 const gfxMatrix mTransform; 99 }; 100 101 /** 102 * gfxDrawingCallback 103 * A simple drawing functor. 104 */ 105 class gfxDrawingCallback { 106 NS_INLINE_DECL_REFCOUNTING(gfxDrawingCallback) 107 protected: 108 // Protected destructor, to discourage deletion outside of Release(): 109 virtual ~gfxDrawingCallback() = default; 110 111 public: 112 /** 113 * Draw into aContext filling aFillRect using aSamplingFilter. 114 * aTransform is a userspace to "image"space matrix. For example, if Draw 115 * draws using a gfxPattern, this is the matrix that should be set on the 116 * pattern prior to rendering it. 117 * @return whether drawing was successful 118 */ 119 virtual bool operator()(gfxContext* aContext, const gfxRect& aFillRect, 120 const mozilla::gfx::SamplingFilter aSamplingFilter, 121 const gfxMatrix& aTransform = gfxMatrix()) = 0; 122 }; 123 124 /** 125 * gfxCallbackDrawable 126 * A convenience implementation of gfxDrawable for callbacks. 127 */ 128 class gfxCallbackDrawable : public gfxDrawable { 129 public: 130 gfxCallbackDrawable(gfxDrawingCallback* aCallback, 131 const mozilla::gfx::IntSize aSize); 132 virtual ~gfxCallbackDrawable() = default; 133 134 virtual bool Draw(gfxContext* aContext, const gfxRect& aFillRect, 135 mozilla::gfx::ExtendMode aExtendMode, 136 const mozilla::gfx::SamplingFilter aSamplingFilter, 137 gfxFloat aOpacity = 1.0, 138 const gfxMatrix& aTransform = gfxMatrix()) override; 139 140 protected: 141 already_AddRefed<gfxSurfaceDrawable> MakeSurfaceDrawable( 142 gfxContext* aContext, mozilla::gfx::SamplingFilter aSamplingFilter = 143 mozilla::gfx::SamplingFilter::LINEAR); 144 145 RefPtr<gfxDrawingCallback> mCallback; 146 RefPtr<gfxSurfaceDrawable> mSurfaceDrawable; 147 }; 148 149 /** 150 * gfxPatternDrawable 151 * A convenience implementation of gfxDrawable for patterns. 152 */ 153 class gfxPatternDrawable : public gfxDrawable { 154 public: 155 gfxPatternDrawable(gfxPattern* aPattern, const mozilla::gfx::IntSize aSize); 156 virtual ~gfxPatternDrawable(); 157 158 virtual bool Draw(gfxContext* aContext, const gfxRect& aFillRect, 159 mozilla::gfx::ExtendMode aExtendMode, 160 const mozilla::gfx::SamplingFilter aSamplingFilter, 161 gfxFloat aOpacity = 1.0, 162 const gfxMatrix& aTransform = gfxMatrix()) override; 163 164 protected: 165 already_AddRefed<gfxCallbackDrawable> MakeCallbackDrawable(); 166 167 RefPtr<gfxPattern> mPattern; 168 }; 169 170 #endif /* GFX_DRAWABLE_H */ 171