no bug - Bumping Firefox l10n changesets r=release a=l10n-bump DONTBUILD CLOSED TREE
[gecko.git] / gfx / thebes / gfxImageSurface.h
blobeefc23373877142a8ff913c50078d69cb6fa2f12
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 GFX_IMAGESURFACE_H
7 #define GFX_IMAGESURFACE_H
9 #include "mozilla/MemoryReporting.h"
10 #include "mozilla/RefPtr.h"
11 #include "gfxASurface.h"
12 #include "nsSize.h"
14 // ARGB -- raw buffer.. wont be changed.. good for storing data.
16 class gfxSubimageSurface;
18 namespace mozilla {
19 namespace gfx {
20 class DataSourceSurface;
21 class SourceSurface;
22 } // namespace gfx
23 } // namespace mozilla
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.
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
39 * @see gfxImageFormat
41 gfxImageSurface(unsigned char* aData, const mozilla::gfx::IntSize& aSize,
42 long aStride, gfxImageFormat aFormat);
44 /**
45 * Construct an image surface.
46 * @param aSize The size of the buffer
47 * @param format Format of the data
49 * @see gfxImageFormat
51 gfxImageSurface(const mozilla::gfx::IntSize& size, gfxImageFormat format,
52 bool aClear = true);
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.
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
67 * @see gfxImageFormat
69 gfxImageSurface(const mozilla::gfx::IntSize& aSize, gfxImageFormat aFormat,
70 long aStride, int32_t aMinimalAllocation, bool aClear);
72 explicit gfxImageSurface(cairo_surface_t* csurf);
74 virtual ~gfxImageSurface();
76 // ImageSurface methods
77 gfxImageFormat Format() const { return mFormat; }
79 virtual const mozilla::gfx::IntSize GetSize() const override { return mSize; }
80 int32_t Width() const {
81 if (mSize.width < 0) {
82 return 0;
84 return mSize.width;
86 int32_t Height() const {
87 if (mSize.height < 0) {
88 return 0;
90 return mSize.height;
93 /**
94 * Distance in bytes between the start of a line and the start of the
95 * next line.
97 int32_t Stride() const { return mStride; }
98 /**
99 * Returns a pointer for the image data. Users of this function can
100 * write to it, but must not attempt to free the buffer.
102 unsigned char* Data() const {
103 return mData;
104 } // delete this data under us and die.
106 * Returns the total size of the image data.
108 int32_t GetDataSize() const {
109 if (mStride < 0 || mSize.height < 0) {
110 return 0;
112 return mStride * mSize.height;
115 /* Fast copy from another image surface; returns TRUE if successful, FALSE
116 * otherwise */
117 bool CopyFrom(gfxImageSurface* other);
120 * Fast copy from a source surface; returns TRUE if successful, FALSE
121 * otherwise Assumes that the format of this surface is compatable with
122 * aSurface
124 bool CopyFrom(mozilla::gfx::SourceSurface* aSurface);
127 * Fast copy to a source surface; returns TRUE if successful, FALSE otherwise
128 * Assumes that the format of this surface is compatible with aSurface
130 bool CopyTo(mozilla::gfx::SourceSurface* aSurface);
133 * Copy to a Moz2D DataSourceSurface.
134 * Marked as virtual so that browsercomps can access this method.
136 virtual already_AddRefed<mozilla::gfx::DataSourceSurface>
137 CopyToB8G8R8A8DataSourceSurface();
139 /* return new Subimage with pointing to original image starting from aRect.pos
140 * and size of aRect.size. New subimage keeping current image reference
142 already_AddRefed<gfxSubimageSurface> GetSubimage(const gfxRect& aRect);
144 virtual already_AddRefed<gfxImageSurface> GetAsImageSurface() override;
146 /** See gfxASurface.h. */
147 static long ComputeStride(const mozilla::gfx::IntSize&, gfxImageFormat);
149 virtual size_t SizeOfExcludingThis(
150 mozilla::MallocSizeOf aMallocSizeOf) const override;
151 virtual size_t SizeOfIncludingThis(
152 mozilla::MallocSizeOf aMallocSizeOf) const override;
153 virtual bool SizeOfIsMeasured() const override;
155 protected:
156 gfxImageSurface();
157 void InitWithData(unsigned char* aData, const mozilla::gfx::IntSize& aSize,
158 long aStride, gfxImageFormat aFormat);
160 * See the parameters to the matching constructor. This should only
161 * be called once, in the constructor, which has already set mSize
162 * and mFormat.
164 void AllocateAndInit(long aStride, int32_t aMinimalAllocation, bool aClear);
165 void InitFromSurface(cairo_surface_t* csurf);
167 long ComputeStride() const {
168 if (mSize.height < 0 || mSize.width < 0) {
169 return 0;
171 return ComputeStride(mSize, mFormat);
174 void MakeInvalid();
176 mozilla::gfx::IntSize mSize;
177 bool mOwnsData;
178 unsigned char* mData;
179 gfxImageFormat mFormat;
180 long mStride;
183 class gfxSubimageSurface : public gfxImageSurface {
184 protected:
185 friend class gfxImageSurface;
186 gfxSubimageSurface(gfxImageSurface* aParent, unsigned char* aData,
187 const mozilla::gfx::IntSize& aSize,
188 gfxImageFormat aFormat);
190 private:
191 RefPtr<gfxImageSurface> mParent;
194 #endif /* GFX_IMAGESURFACE_H */