gfx/thebes/gfxImageSurface.h

changeset 0
6474c204b198
equal deleted inserted replaced
-1:000000000000 0:015c7a35f471
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_IMAGESURFACE_H
7 #define GFX_IMAGESURFACE_H
8
9 #include "mozilla/MemoryReporting.h"
10 #include "mozilla/RefPtr.h"
11 #include "gfxASurface.h"
12 #include "nsAutoPtr.h"
13 #include "nsSize.h"
14
15 // ARGB -- raw buffer.. wont be changed.. good for storing data.
16
17 class gfxSubimageSurface;
18
19 namespace mozilla {
20 namespace gfx {
21 class DataSourceSurface;
22 class SourceSurface;
23 }
24 }
25
26 /**
27 * A raw image buffer. The format can be set in the constructor. Its main
28 * purpose is for storing read-only images and using it as a source surface,
29 * but it can also be drawn to.
30 */
31 class gfxImageSurface : public gfxASurface {
32 public:
33 /**
34 * Construct an image surface around an existing buffer of image data.
35 * @param aData A buffer containing the image data
36 * @param aSize The size of the buffer
37 * @param aStride The stride of the buffer
38 * @param format Format of the data
39 *
40 * @see gfxImageFormat
41 */
42 gfxImageSurface(unsigned char *aData, const gfxIntSize& aSize,
43 long aStride, gfxImageFormat aFormat);
44
45 /**
46 * Construct an image surface.
47 * @param aSize The size of the buffer
48 * @param format Format of the data
49 *
50 * @see gfxImageFormat
51 */
52 gfxImageSurface(const gfxIntSize& size, gfxImageFormat format, bool aClear = true);
53
54 /**
55 * Construct an image surface, with a specified stride and allowing the
56 * allocation of more memory than required for the storage of the surface
57 * itself. When aStride and aMinimalAllocation are <=0, this constructor
58 * is the equivalent of the preceeding one.
59 *
60 * @param format Format of the data
61 * @param aSize The size of the buffer
62 * @param aStride The stride of the buffer - if <=0, use ComputeStride()
63 * @param aMinimalAllocation Allocate at least this many bytes. If smaller
64 * than width * stride, or width*stride <=0, this value is ignored.
65 * @param aClear
66 *
67 * @see gfxImageFormat
68 */
69 gfxImageSurface(const gfxIntSize& aSize, gfxImageFormat aFormat,
70 long aStride, int32_t aMinimalAllocation, bool aClear);
71
72 gfxImageSurface(cairo_surface_t *csurf);
73
74 virtual ~gfxImageSurface();
75
76 // ImageSurface methods
77 gfxImageFormat Format() const { return mFormat; }
78
79 virtual const gfxIntSize GetSize() const { return mSize; }
80 int32_t Width() const { return mSize.width; }
81 int32_t Height() const { return mSize.height; }
82
83 /**
84 * Distance in bytes between the start of a line and the start of the
85 * next line.
86 */
87 int32_t Stride() const { return mStride; }
88 /**
89 * Returns a pointer for the image data. Users of this function can
90 * write to it, but must not attempt to free the buffer.
91 */
92 unsigned char* Data() const { return mData; } // delete this data under us and die.
93 /**
94 * Returns the total size of the image data.
95 */
96 int32_t GetDataSize() const { return mStride*mSize.height; }
97
98 /* Fast copy from another image surface; returns TRUE if successful, FALSE otherwise */
99 bool CopyFrom (gfxImageSurface *other);
100
101 /**
102 * Fast copy from a source surface; returns TRUE if successful, FALSE otherwise
103 * Assumes that the format of this surface is compatable with aSurface
104 */
105 bool CopyFrom (mozilla::gfx::SourceSurface *aSurface);
106
107 /**
108 * Fast copy to a source surface; returns TRUE if successful, FALSE otherwise
109 * Assumes that the format of this surface is compatible with aSurface
110 */
111 bool CopyTo (mozilla::gfx::SourceSurface *aSurface);
112
113 /**
114 * Copy to a Moz2D DataSourceSurface.
115 * Marked as virtual so that browsercomps can access this method.
116 */
117 virtual mozilla::TemporaryRef<mozilla::gfx::DataSourceSurface> CopyToB8G8R8A8DataSourceSurface();
118
119 /* return new Subimage with pointing to original image starting from aRect.pos
120 * and size of aRect.size. New subimage keeping current image reference
121 */
122 already_AddRefed<gfxSubimageSurface> GetSubimage(const gfxRect& aRect);
123
124 virtual already_AddRefed<gfxImageSurface> GetAsImageSurface();
125
126 /** See gfxASurface.h. */
127 virtual void MovePixels(const nsIntRect& aSourceRect,
128 const nsIntPoint& aDestTopLeft) MOZ_OVERRIDE;
129
130 static long ComputeStride(const gfxIntSize&, gfxImageFormat);
131
132 virtual size_t SizeOfExcludingThis(mozilla::MallocSizeOf aMallocSizeOf) const
133 MOZ_OVERRIDE;
134 virtual size_t SizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf) const
135 MOZ_OVERRIDE;
136 virtual bool SizeOfIsMeasured() const MOZ_OVERRIDE;
137
138 protected:
139 gfxImageSurface();
140 void InitWithData(unsigned char *aData, const gfxIntSize& aSize,
141 long aStride, gfxImageFormat aFormat);
142 /**
143 * See the parameters to the matching constructor. This should only
144 * be called once, in the constructor, which has already set mSize
145 * and mFormat.
146 */
147 void AllocateAndInit(long aStride, int32_t aMinimalAllocation, bool aClear);
148 void InitFromSurface(cairo_surface_t *csurf);
149
150 long ComputeStride() const { return ComputeStride(mSize, mFormat); }
151
152
153 void MakeInvalid();
154
155 gfxIntSize mSize;
156 bool mOwnsData;
157 unsigned char *mData;
158 gfxImageFormat mFormat;
159 long mStride;
160 };
161
162 class gfxSubimageSurface : public gfxImageSurface {
163 protected:
164 friend class gfxImageSurface;
165 gfxSubimageSurface(gfxImageSurface* aParent,
166 unsigned char* aData,
167 const gfxIntSize& aSize,
168 gfxImageFormat aFormat);
169 private:
170 nsRefPtr<gfxImageSurface> mParent;
171 };
172
173 #endif /* GFX_IMAGESURFACE_H */

mercurial