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