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