Bug 1073336 part 14a - Update animation generation when changing animations via the...
[gecko.git] / gfx / 2d / SourceSurfaceCG.h
blob9e1798490e715504b1ecdeb5894850a2242242aa
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>
11 #include "2D.h"
13 class MacIOSurface;
15 namespace mozilla {
16 namespace gfx {
18 CGImageRef
19 CreateCGImage(CGDataProviderReleaseDataCallback aCallback,
20 void *aInfo,
21 const void *aData,
22 const IntSize &aSize,
23 int32_t aStride,
24 SurfaceFormat aFormat);
26 CGImageRef
27 CreateCGImage(void *aInfo,
28 const void *aData,
29 const IntSize &aSize,
30 int32_t aStride,
31 SurfaceFormat aFormat);
33 class DrawTargetCG;
35 class SourceSurfaceCG : public SourceSurface
37 public:
38 MOZ_DECLARE_REFCOUNTED_VIRTUAL_TYPENAME(SourceSurfaceCG)
39 SourceSurfaceCG() {}
40 explicit SourceSurfaceCG(CGImageRef aImage) : mImage(aImage) {}
41 ~SourceSurfaceCG();
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,
51 const IntSize &aSize,
52 int32_t aStride,
53 SurfaceFormat aFormat);
55 private:
56 CGImageRef mImage;
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
66 public:
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,
79 const IntSize &aSize,
80 int32_t aStride,
81 SurfaceFormat aFormat);
83 virtual unsigned char *GetData();
85 virtual int32_t Stride() { return CGImageGetBytesPerRow(mImage); }
88 private:
89 CGContextRef mCg;
90 CGImageRef mImage;
91 SurfaceFormat mFormat;
92 //XXX: we don't need to store mData we can just get it from the CGContext
93 void *mData;
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
101 public:
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
110 public:
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();
130 return this;
133 CGImageRef GetImage() { EnsureImage(); return mImage; }
135 virtual unsigned char *GetData() { return static_cast<unsigned char*>(mData); }
137 virtual int32_t Stride() { return mStride; }
139 private:
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;
149 CGContextRef mCg;
150 SurfaceFormat mFormat;
152 mutable CGImageRef mImage;
154 // mData can be owned by three different things:
155 // mImage, mCg or SourceSurfaceCGBitmapContext
156 void *mData;
158 // The image buffer, if the buffer is owned by this class.
159 AlignedArray<uint8_t> mDataHolder;
161 int32_t mStride;
162 IntSize mSize;
165 class SourceSurfaceCGIOSurfaceContext : public SourceSurfaceCGContext
167 public:
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; }
182 private:
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;
193 void *mData;
194 int32_t mStride;
196 IntSize mSize;
203 #endif // _MOZILLA_GFX_SOURCESURFACECG_H