|
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 _MOZILLA_GFX_SOURCESURFACECG_H |
|
7 #define _MOZILLA_GFX_SOURCESURFACECG_H |
|
8 |
|
9 #include <ApplicationServices/ApplicationServices.h> |
|
10 |
|
11 #include "2D.h" |
|
12 |
|
13 class MacIOSurface; |
|
14 |
|
15 namespace mozilla { |
|
16 namespace gfx { |
|
17 |
|
18 CGImageRef |
|
19 CreateCGImage(void *aInfo, |
|
20 const void *aData, |
|
21 const IntSize &aSize, |
|
22 int32_t aStride, |
|
23 SurfaceFormat aFormat); |
|
24 |
|
25 class DrawTargetCG; |
|
26 |
|
27 class SourceSurfaceCG : public SourceSurface |
|
28 { |
|
29 public: |
|
30 MOZ_DECLARE_REFCOUNTED_VIRTUAL_TYPENAME(SourceSurfaceCG) |
|
31 SourceSurfaceCG() {} |
|
32 SourceSurfaceCG(CGImageRef aImage) : mImage(aImage) {} |
|
33 ~SourceSurfaceCG(); |
|
34 |
|
35 virtual SurfaceType GetType() const { return SurfaceType::COREGRAPHICS_IMAGE; } |
|
36 virtual IntSize GetSize() const; |
|
37 virtual SurfaceFormat GetFormat() const; |
|
38 virtual TemporaryRef<DataSourceSurface> GetDataSurface(); |
|
39 |
|
40 CGImageRef GetImage() { return mImage; } |
|
41 |
|
42 bool InitFromData(unsigned char *aData, |
|
43 const IntSize &aSize, |
|
44 int32_t aStride, |
|
45 SurfaceFormat aFormat); |
|
46 |
|
47 private: |
|
48 CGImageRef mImage; |
|
49 |
|
50 /* It might be better to just use the bitmap info from the CGImageRef to |
|
51 * deduce the format to save space in SourceSurfaceCG, |
|
52 * for now we just store it in mFormat */ |
|
53 SurfaceFormat mFormat; |
|
54 }; |
|
55 |
|
56 class DataSourceSurfaceCG : public DataSourceSurface |
|
57 { |
|
58 public: |
|
59 MOZ_DECLARE_REFCOUNTED_VIRTUAL_TYPENAME(DataSourceSurfaceCG) |
|
60 DataSourceSurfaceCG() {} |
|
61 DataSourceSurfaceCG(CGImageRef aImage); |
|
62 ~DataSourceSurfaceCG(); |
|
63 |
|
64 virtual SurfaceType GetType() const { return SurfaceType::DATA; } |
|
65 virtual IntSize GetSize() const; |
|
66 virtual SurfaceFormat GetFormat() const { return mFormat; } |
|
67 |
|
68 CGImageRef GetImage() { return mImage; } |
|
69 |
|
70 bool InitFromData(unsigned char *aData, |
|
71 const IntSize &aSize, |
|
72 int32_t aStride, |
|
73 SurfaceFormat aFormat); |
|
74 |
|
75 virtual unsigned char *GetData(); |
|
76 |
|
77 virtual int32_t Stride() { return CGImageGetBytesPerRow(mImage); } |
|
78 |
|
79 |
|
80 private: |
|
81 CGContextRef mCg; |
|
82 CGImageRef mImage; |
|
83 SurfaceFormat mFormat; |
|
84 //XXX: we don't need to store mData we can just get it from the CGContext |
|
85 void *mData; |
|
86 /* It might be better to just use the bitmap info from the CGImageRef to |
|
87 * deduce the format to save space in SourceSurfaceCG, |
|
88 * for now we just store it in mFormat */ |
|
89 }; |
|
90 |
|
91 class SourceSurfaceCGContext : public DataSourceSurface |
|
92 { |
|
93 public: |
|
94 MOZ_DECLARE_REFCOUNTED_VIRTUAL_TYPENAME(DataSourceSurfaceCGContext) |
|
95 virtual void DrawTargetWillChange() = 0; |
|
96 virtual CGImageRef GetImage() = 0; |
|
97 }; |
|
98 |
|
99 class SourceSurfaceCGBitmapContext : public SourceSurfaceCGContext |
|
100 { |
|
101 public: |
|
102 MOZ_DECLARE_REFCOUNTED_VIRTUAL_TYPENAME(DataSourceSurfaceCGBitmapContext) |
|
103 SourceSurfaceCGBitmapContext(DrawTargetCG *); |
|
104 ~SourceSurfaceCGBitmapContext(); |
|
105 |
|
106 virtual SurfaceType GetType() const { return SurfaceType::COREGRAPHICS_CGCONTEXT; } |
|
107 virtual IntSize GetSize() const; |
|
108 virtual SurfaceFormat GetFormat() const { return mFormat; } |
|
109 virtual TemporaryRef<DataSourceSurface> GetDataSurface() |
|
110 { |
|
111 // This call to DrawTargetWillChange() is needed to make a local copy of |
|
112 // the data from mDrawTarget. If we don't do that, the data can end up |
|
113 // getting deleted before the CGImageRef it belongs to. |
|
114 // |
|
115 // Another reason we need a local copy of the data is that the data in |
|
116 // mDrawTarget could change when someone touches the original DrawTargetCG |
|
117 // object. But a SourceSurface object should be immutable. |
|
118 // |
|
119 // For more information see bug 925448. |
|
120 DrawTargetWillChange(); |
|
121 return this; |
|
122 } |
|
123 |
|
124 CGImageRef GetImage() { EnsureImage(); return mImage; } |
|
125 |
|
126 virtual unsigned char *GetData() { return static_cast<unsigned char*>(mData); } |
|
127 |
|
128 virtual int32_t Stride() { return mStride; } |
|
129 |
|
130 private: |
|
131 //XXX: do the other backends friend their DrawTarget? |
|
132 friend class DrawTargetCG; |
|
133 virtual void DrawTargetWillChange(); |
|
134 void EnsureImage() const; |
|
135 |
|
136 // We hold a weak reference to these two objects. |
|
137 // The cycle is broken by DrawTargetWillChange |
|
138 DrawTargetCG *mDrawTarget; |
|
139 CGContextRef mCg; |
|
140 SurfaceFormat mFormat; |
|
141 |
|
142 mutable CGImageRef mImage; |
|
143 |
|
144 // mData can be owned by three different things: |
|
145 // mImage, mCg or SourceSurfaceCGBitmapContext |
|
146 void *mData; |
|
147 |
|
148 // The image buffer, if the buffer is owned by this class. |
|
149 AlignedArray<uint8_t> mDataHolder; |
|
150 |
|
151 int32_t mStride; |
|
152 IntSize mSize; |
|
153 }; |
|
154 |
|
155 class SourceSurfaceCGIOSurfaceContext : public SourceSurfaceCGContext |
|
156 { |
|
157 public: |
|
158 MOZ_DECLARE_REFCOUNTED_VIRTUAL_TYPENAME(DataSourceSurfaceCGIOSurfaceContext) |
|
159 SourceSurfaceCGIOSurfaceContext(DrawTargetCG *); |
|
160 ~SourceSurfaceCGIOSurfaceContext(); |
|
161 |
|
162 virtual SurfaceType GetType() const { return SurfaceType::COREGRAPHICS_CGCONTEXT; } |
|
163 virtual IntSize GetSize() const; |
|
164 virtual SurfaceFormat GetFormat() const { return mFormat; } |
|
165 |
|
166 CGImageRef GetImage() { EnsureImage(); return mImage; } |
|
167 |
|
168 virtual unsigned char *GetData(); |
|
169 |
|
170 virtual int32_t Stride() { return mStride; } |
|
171 |
|
172 private: |
|
173 //XXX: do the other backends friend their DrawTarget? |
|
174 friend class DrawTargetCG; |
|
175 virtual void DrawTargetWillChange(); |
|
176 void EnsureImage() const; |
|
177 |
|
178 SurfaceFormat mFormat; |
|
179 mutable CGImageRef mImage; |
|
180 MacIOSurface* mIOSurface; |
|
181 |
|
182 void *mData; |
|
183 int32_t mStride; |
|
184 |
|
185 IntSize mSize; |
|
186 }; |
|
187 |
|
188 |
|
189 } |
|
190 } |
|
191 |
|
192 #endif // _MOZILLA_GFX_SOURCESURFACECG_H |