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 
7 #include "mozilla/MemoryReporting.h"
8 #if defined(HAVE_POSIX_MEMALIGN)
9 #include "gfxAlphaRecovery.h"
10 #endif
11 #include "gfxImageSurface.h"
12 
13 #include "cairo.h"
14 #include "mozilla/gfx/2D.h"
15 #include "mozilla/gfx/HelpersCairo.h"
16 #include "gfx2DGlue.h"
17 #include <algorithm>
18 
19 using namespace mozilla;
20 using namespace mozilla::gfx;
21 
gfxImageSurface()22 gfxImageSurface::gfxImageSurface()
23   : mSize(0, 0),
24     mOwnsData(false),
25     mFormat(SurfaceFormat::UNKNOWN),
26     mStride(0)
27 {
28 }
29 
30 void
InitFromSurface(cairo_surface_t * csurf)31 gfxImageSurface::InitFromSurface(cairo_surface_t *csurf)
32 {
33     if (!csurf || cairo_surface_status(csurf)) {
34         MakeInvalid();
35         return;
36     }
37 
38     mSize.width = cairo_image_surface_get_width(csurf);
39     mSize.height = cairo_image_surface_get_height(csurf);
40     mData = cairo_image_surface_get_data(csurf);
41     mFormat = CairoFormatToGfxFormat(cairo_image_surface_get_format(csurf));
42     mOwnsData = false;
43     mStride = cairo_image_surface_get_stride(csurf);
44 
45     Init(csurf, true);
46 }
47 
gfxImageSurface(unsigned char * aData,const IntSize & aSize,long aStride,gfxImageFormat aFormat)48 gfxImageSurface::gfxImageSurface(unsigned char *aData, const IntSize& aSize,
49                                  long aStride, gfxImageFormat aFormat)
50 {
51     InitWithData(aData, aSize, aStride, aFormat);
52 }
53 
54 void
MakeInvalid()55 gfxImageSurface::MakeInvalid()
56 {
57     mSize = IntSize(-1, -1);
58     mData = nullptr;
59     mStride = 0;
60 }
61 
62 void
InitWithData(unsigned char * aData,const IntSize & aSize,long aStride,gfxImageFormat aFormat)63 gfxImageSurface::InitWithData(unsigned char *aData, const IntSize& aSize,
64                               long aStride, gfxImageFormat aFormat)
65 {
66     mSize = aSize;
67     mOwnsData = false;
68     mData = aData;
69     mFormat = aFormat;
70     mStride = aStride;
71 
72     if (!Factory::CheckSurfaceSize(aSize))
73         MakeInvalid();
74 
75     cairo_format_t cformat = GfxFormatToCairoFormat(mFormat);
76     cairo_surface_t *surface =
77         cairo_image_surface_create_for_data((unsigned char*)mData,
78                                             cformat,
79                                             mSize.width,
80                                             mSize.height,
81                                             mStride);
82 
83     // cairo_image_surface_create_for_data can return a 'null' surface
84     // in out of memory conditions. The gfxASurface::Init call checks
85     // the surface it receives to see if there is an error with the
86     // surface and handles it appropriately. That is why there is
87     // no check here.
88     Init(surface);
89 }
90 
91 static void*
TryAllocAlignedBytes(size_t aSize)92 TryAllocAlignedBytes(size_t aSize)
93 {
94     // Use fallible allocators here
95 #if defined(HAVE_POSIX_MEMALIGN)
96     void* ptr;
97     // Try to align for fast alpha recovery.  This should only help
98     // cairo too, can't hurt.
99     return moz_posix_memalign(&ptr,
100                               1 << gfxAlphaRecovery::GoodAlignmentLog2(),
101                               aSize) ?
102              nullptr : ptr;
103 #else
104     // Oh well, hope that luck is with us in the allocator
105     return malloc(aSize);
106 #endif
107 }
108 
gfxImageSurface(const IntSize & size,gfxImageFormat format,bool aClear)109 gfxImageSurface::gfxImageSurface(const IntSize& size, gfxImageFormat format, bool aClear)
110  : mSize(size), mData(nullptr), mFormat(format)
111 {
112     AllocateAndInit(0, 0, aClear);
113 }
114 
115 void
AllocateAndInit(long aStride,int32_t aMinimalAllocation,bool aClear)116 gfxImageSurface::AllocateAndInit(long aStride, int32_t aMinimalAllocation,
117                                  bool aClear)
118 {
119     // The callers should set mSize and mFormat.
120     MOZ_ASSERT(!mData);
121     mData = nullptr;
122     mOwnsData = false;
123 
124     mStride = aStride > 0 ? aStride : ComputeStride();
125     if (aMinimalAllocation < mSize.height * mStride)
126         aMinimalAllocation = mSize.height * mStride;
127 
128     if (!Factory::CheckSurfaceSize(mSize))
129         MakeInvalid();
130 
131     // if we have a zero-sized surface, just leave mData nullptr
132     if (mSize.height * mStride > 0) {
133 
134         // This can fail to allocate memory aligned as we requested,
135         // or it can fail to allocate any memory at all.
136         mData = (unsigned char *) TryAllocAlignedBytes(aMinimalAllocation);
137         if (!mData)
138             return;
139         if (aClear)
140             memset(mData, 0, aMinimalAllocation);
141     }
142 
143     mOwnsData = true;
144 
145     cairo_format_t cformat = GfxFormatToCairoFormat(mFormat);
146     cairo_surface_t *surface =
147         cairo_image_surface_create_for_data((unsigned char*)mData,
148                                             cformat,
149                                             mSize.width,
150                                             mSize.height,
151                                             mStride);
152 
153     Init(surface);
154 
155     if (mSurfaceValid) {
156         RecordMemoryUsed(mSize.height * ComputeStride() +
157                          sizeof(gfxImageSurface));
158     }
159 }
160 
gfxImageSurface(const IntSize & size,gfxImageFormat format,long aStride,int32_t aExtraBytes,bool aClear)161 gfxImageSurface::gfxImageSurface(const IntSize& size, gfxImageFormat format,
162                                  long aStride, int32_t aExtraBytes, bool aClear)
163  : mSize(size), mData(nullptr), mFormat(format)
164 {
165     AllocateAndInit(aStride, aExtraBytes, aClear);
166 }
167 
gfxImageSurface(cairo_surface_t * csurf)168 gfxImageSurface::gfxImageSurface(cairo_surface_t *csurf)
169 {
170     mSize.width = cairo_image_surface_get_width(csurf);
171     mSize.height = cairo_image_surface_get_height(csurf);
172     mData = cairo_image_surface_get_data(csurf);
173     mFormat = CairoFormatToGfxFormat(cairo_image_surface_get_format(csurf));
174     mOwnsData = false;
175     mStride = cairo_image_surface_get_stride(csurf);
176 
177     Init(csurf, true);
178 }
179 
~gfxImageSurface()180 gfxImageSurface::~gfxImageSurface()
181 {
182     if (mOwnsData)
183         free(mData);
184 }
185 
186 /*static*/ long
ComputeStride(const IntSize & aSize,gfxImageFormat aFormat)187 gfxImageSurface::ComputeStride(const IntSize& aSize, gfxImageFormat aFormat)
188 {
189     long stride;
190 
191     if (aFormat == SurfaceFormat::A8R8G8B8_UINT32)
192         stride = aSize.width * 4;
193     else if (aFormat == SurfaceFormat::X8R8G8B8_UINT32)
194         stride = aSize.width * 4;
195     else if (aFormat == SurfaceFormat::R5G6B5_UINT16)
196         stride = aSize.width * 2;
197     else if (aFormat == SurfaceFormat::A8)
198         stride = aSize.width;
199     else {
200         NS_WARNING("Unknown format specified to gfxImageSurface!");
201         stride = aSize.width * 4;
202     }
203 
204     stride = ((stride + 3) / 4) * 4;
205 
206     return stride;
207 }
208 
209 size_t
SizeOfExcludingThis(mozilla::MallocSizeOf aMallocSizeOf) const210 gfxImageSurface::SizeOfExcludingThis(mozilla::MallocSizeOf aMallocSizeOf) const
211 {
212     size_t n = gfxASurface::SizeOfExcludingThis(aMallocSizeOf);
213     if (mOwnsData) {
214         n += aMallocSizeOf(mData);
215     }
216     return n;
217 }
218 
219 size_t
SizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf) const220 gfxImageSurface::SizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf) const
221 {
222     return aMallocSizeOf(this) + SizeOfExcludingThis(aMallocSizeOf);
223 }
224 
225 bool
SizeOfIsMeasured() const226 gfxImageSurface::SizeOfIsMeasured() const
227 {
228     return true;
229 }
230 
231 // helper function for the CopyFrom methods
232 static void
CopyForStride(unsigned char * aDest,unsigned char * aSrc,const IntSize & aSize,long aDestStride,long aSrcStride)233 CopyForStride(unsigned char* aDest, unsigned char* aSrc, const IntSize& aSize, long aDestStride, long aSrcStride)
234 {
235     if (aDestStride == aSrcStride) {
236         memcpy (aDest, aSrc, aSrcStride * aSize.height);
237     } else {
238         int lineSize = std::min(aDestStride, aSrcStride);
239         for (int i = 0; i < aSize.height; i++) {
240             unsigned char* src = aSrc + aSrcStride * i;
241             unsigned char* dst = aDest + aDestStride * i;
242 
243             memcpy (dst, src, lineSize);
244         }
245     }
246 }
247 
248 // helper function for the CopyFrom methods
249 static bool
FormatsAreCompatible(gfxImageFormat a1,gfxImageFormat a2)250 FormatsAreCompatible(gfxImageFormat a1, gfxImageFormat a2)
251 {
252     if (a1 != a2 &&
253         !(a1 == SurfaceFormat::A8R8G8B8_UINT32 &&
254           a2 == SurfaceFormat::X8R8G8B8_UINT32) &&
255         !(a1 == SurfaceFormat::X8R8G8B8_UINT32 &&
256           a2 == SurfaceFormat::A8R8G8B8_UINT32)) {
257         return false;
258     }
259 
260     return true;
261 }
262 
263 bool
CopyFrom(SourceSurface * aSurface)264 gfxImageSurface::CopyFrom (SourceSurface *aSurface)
265 {
266     RefPtr<DataSourceSurface> data = aSurface->GetDataSurface();
267 
268     if (!data) {
269         return false;
270     }
271 
272     IntSize size(data->GetSize().width, data->GetSize().height);
273     if (size != mSize) {
274         return false;
275     }
276 
277     if (!FormatsAreCompatible(SurfaceFormatToImageFormat(aSurface->GetFormat()),
278                               mFormat)) {
279         return false;
280     }
281 
282     CopyForStride(mData, data->GetData(), size, mStride, data->Stride());
283 
284     return true;
285 }
286 
287 
288 bool
CopyFrom(gfxImageSurface * other)289 gfxImageSurface::CopyFrom(gfxImageSurface *other)
290 {
291     if (other->mSize != mSize) {
292         return false;
293     }
294 
295     if (!FormatsAreCompatible(other->mFormat, mFormat)) {
296         return false;
297     }
298 
299     CopyForStride(mData, other->mData, mSize, mStride, other->mStride);
300 
301     return true;
302 }
303 
304 bool
CopyTo(SourceSurface * aSurface)305 gfxImageSurface::CopyTo(SourceSurface *aSurface) {
306     RefPtr<DataSourceSurface> data = aSurface->GetDataSurface();
307 
308     if (!data) {
309         return false;
310     }
311 
312     IntSize size(data->GetSize().width, data->GetSize().height);
313     if (size != mSize) {
314         return false;
315     }
316 
317     if (!FormatsAreCompatible(SurfaceFormatToImageFormat(aSurface->GetFormat()),
318                               mFormat)) {
319         return false;
320     }
321 
322     CopyForStride(data->GetData(), mData, size, data->Stride(), mStride);
323 
324     return true;
325 }
326 
327 already_AddRefed<DataSourceSurface>
CopyToB8G8R8A8DataSourceSurface()328 gfxImageSurface::CopyToB8G8R8A8DataSourceSurface()
329 {
330   RefPtr<DataSourceSurface> dataSurface =
331     Factory::CreateDataSourceSurface(IntSize(GetSize().width, GetSize().height),
332                                      SurfaceFormat::B8G8R8A8);
333   if (dataSurface) {
334     CopyTo(dataSurface);
335   }
336   return dataSurface.forget();
337 }
338 
339 already_AddRefed<gfxSubimageSurface>
GetSubimage(const gfxRect & aRect)340 gfxImageSurface::GetSubimage(const gfxRect& aRect)
341 {
342     gfxRect r(aRect);
343     r.Round();
344     MOZ_ASSERT(gfxRect(0, 0, mSize.width, mSize.height).Contains(r));
345 
346     gfxImageFormat format = Format();
347 
348     unsigned char* subData = Data() +
349         (Stride() * (int)r.Y()) +
350         (int)r.X() * gfxASurface::BytePerPixelFromFormat(Format());
351 
352     if (format == SurfaceFormat::A8R8G8B8_UINT32 &&
353         GetOpaqueRect().Contains(aRect)) {
354         format = SurfaceFormat::X8R8G8B8_UINT32;
355     }
356 
357     RefPtr<gfxSubimageSurface> image =
358         new gfxSubimageSurface(this, subData,
359                                IntSize((int)r.Width(), (int)r.Height()),
360                                format);
361 
362     return image.forget();
363 }
364 
gfxSubimageSurface(gfxImageSurface * aParent,unsigned char * aData,const IntSize & aSize,gfxImageFormat aFormat)365 gfxSubimageSurface::gfxSubimageSurface(gfxImageSurface* aParent,
366                                        unsigned char* aData,
367                                        const IntSize& aSize,
368                                        gfxImageFormat aFormat)
369   : gfxImageSurface(aData, aSize, aParent->Stride(), aFormat)
370   , mParent(aParent)
371 {
372 }
373 
374 already_AddRefed<gfxImageSurface>
GetAsImageSurface()375 gfxImageSurface::GetAsImageSurface()
376 {
377   RefPtr<gfxImageSurface> surface = this;
378   return surface.forget();
379 }
380