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