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/. */
6 #ifndef _MOZILLA_GFX_SOURCESURFACECG_H
7 #define _MOZILLA_GFX_SOURCESURFACECG_H
9 #include <ApplicationServices/ApplicationServices.h>
19 CreateCGImage(CGDataProviderReleaseDataCallback aCallback
,
24 SurfaceFormat aFormat
);
27 CreateCGImage(void *aInfo
,
31 SurfaceFormat aFormat
);
35 class SourceSurfaceCG
: public SourceSurface
38 MOZ_DECLARE_REFCOUNTED_VIRTUAL_TYPENAME(SourceSurfaceCG
)
40 explicit SourceSurfaceCG(CGImageRef aImage
) : mImage(aImage
) {}
43 virtual SurfaceType
GetType() const { return SurfaceType::COREGRAPHICS_IMAGE
; }
44 virtual IntSize
GetSize() const;
45 virtual SurfaceFormat
GetFormat() const;
46 virtual TemporaryRef
<DataSourceSurface
> GetDataSurface();
48 CGImageRef
GetImage() { return mImage
; }
50 bool InitFromData(unsigned char *aData
,
53 SurfaceFormat aFormat
);
58 /* It might be better to just use the bitmap info from the CGImageRef to
59 * deduce the format to save space in SourceSurfaceCG,
60 * for now we just store it in mFormat */
61 SurfaceFormat mFormat
;
64 class DataSourceSurfaceCG
: public DataSourceSurface
67 MOZ_DECLARE_REFCOUNTED_VIRTUAL_TYPENAME(DataSourceSurfaceCG
)
68 DataSourceSurfaceCG() {}
69 explicit DataSourceSurfaceCG(CGImageRef aImage
);
70 ~DataSourceSurfaceCG();
72 virtual SurfaceType
GetType() const { return SurfaceType::DATA
; }
73 virtual IntSize
GetSize() const;
74 virtual SurfaceFormat
GetFormat() const { return mFormat
; }
76 CGImageRef
GetImage() { return mImage
; }
78 bool InitFromData(unsigned char *aData
,
81 SurfaceFormat aFormat
);
83 virtual unsigned char *GetData();
85 virtual int32_t Stride() { return CGImageGetBytesPerRow(mImage
); }
91 SurfaceFormat mFormat
;
92 //XXX: we don't need to store mData we can just get it from the CGContext
94 /* It might be better to just use the bitmap info from the CGImageRef to
95 * deduce the format to save space in SourceSurfaceCG,
96 * for now we just store it in mFormat */
99 class SourceSurfaceCGContext
: public DataSourceSurface
102 MOZ_DECLARE_REFCOUNTED_VIRTUAL_TYPENAME(DataSourceSurfaceCGContext
)
103 virtual void DrawTargetWillChange() = 0;
104 virtual void DrawTargetWillGoAway() = 0;
105 virtual CGImageRef
GetImage() = 0;
108 class SourceSurfaceCGBitmapContext
: public SourceSurfaceCGContext
111 MOZ_DECLARE_REFCOUNTED_VIRTUAL_TYPENAME(DataSourceSurfaceCGBitmapContext
)
112 explicit SourceSurfaceCGBitmapContext(DrawTargetCG
*);
113 ~SourceSurfaceCGBitmapContext();
115 virtual SurfaceType
GetType() const { return SurfaceType::COREGRAPHICS_CGCONTEXT
; }
116 virtual IntSize
GetSize() const;
117 virtual SurfaceFormat
GetFormat() const { return mFormat
; }
118 virtual TemporaryRef
<DataSourceSurface
> GetDataSurface()
120 // This call to DrawTargetWillChange() is needed to make a local copy of
121 // the data from mDrawTarget. If we don't do that, the data can end up
122 // getting deleted before the CGImageRef it belongs to.
124 // Another reason we need a local copy of the data is that the data in
125 // mDrawTarget could change when someone touches the original DrawTargetCG
126 // object. But a SourceSurface object should be immutable.
128 // For more information see bug 925448.
129 DrawTargetWillChange();
133 CGImageRef
GetImage() { EnsureImage(); return mImage
; }
135 virtual unsigned char *GetData() { return static_cast<unsigned char*>(mData
); }
137 virtual int32_t Stride() { return mStride
; }
140 //XXX: do the other backends friend their DrawTarget?
141 friend class DrawTargetCG
;
142 virtual void DrawTargetWillChange();
143 virtual void DrawTargetWillGoAway();
144 void EnsureImage() const;
146 // We hold a weak reference to these two objects.
147 // The cycle is broken by DrawTargetWillChange
148 DrawTargetCG
*mDrawTarget
;
150 SurfaceFormat mFormat
;
152 mutable CGImageRef mImage
;
154 // mData can be owned by three different things:
155 // mImage, mCg or SourceSurfaceCGBitmapContext
158 // The image buffer, if the buffer is owned by this class.
159 AlignedArray
<uint8_t> mDataHolder
;
165 class SourceSurfaceCGIOSurfaceContext
: public SourceSurfaceCGContext
168 MOZ_DECLARE_REFCOUNTED_VIRTUAL_TYPENAME(DataSourceSurfaceCGIOSurfaceContext
)
169 explicit SourceSurfaceCGIOSurfaceContext(DrawTargetCG
*);
170 ~SourceSurfaceCGIOSurfaceContext();
172 virtual SurfaceType
GetType() const { return SurfaceType::COREGRAPHICS_CGCONTEXT
; }
173 virtual IntSize
GetSize() const;
174 virtual SurfaceFormat
GetFormat() const { return mFormat
; }
176 CGImageRef
GetImage() { EnsureImage(); return mImage
; }
178 virtual unsigned char *GetData();
180 virtual int32_t Stride() { return mStride
; }
183 //XXX: do the other backends friend their DrawTarget?
184 friend class DrawTargetCG
;
185 virtual void DrawTargetWillChange();
186 virtual void DrawTargetWillGoAway() { DrawTargetWillChange(); }
187 void EnsureImage() const;
189 SurfaceFormat mFormat
;
190 mutable CGImageRef mImage
;
191 MacIOSurface
* mIOSurface
;
203 #endif // _MOZILLA_GFX_SOURCESURFACECG_H