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/. */
5
6 #include "gfxQuartzSurface.h"
7 #include "gfxContext.h"
8 #include "gfxImageSurface.h"
9 #include "mozilla/gfx/2D.h"
10 #include "mozilla/gfx/HelpersCairo.h"
11
12 #include "cairo-quartz.h"
13
MakeInvalid()14 void gfxQuartzSurface::MakeInvalid() { mSize = mozilla::gfx::IntSize(-1, -1); }
15
gfxQuartzSurface(const mozilla::gfx::IntSize & desiredSize,gfxImageFormat format)16 gfxQuartzSurface::gfxQuartzSurface(const mozilla::gfx::IntSize& desiredSize,
17 gfxImageFormat format)
18 : mCGContext(nullptr), mSize(desiredSize) {
19 if (!mozilla::gfx::Factory::CheckSurfaceSize(desiredSize)) MakeInvalid();
20
21 unsigned int width = static_cast<unsigned int>(mSize.width);
22 unsigned int height = static_cast<unsigned int>(mSize.height);
23
24 cairo_format_t cformat = GfxFormatToCairoFormat(format);
25 cairo_surface_t* surf = cairo_quartz_surface_create(cformat, width, height);
26
27 mCGContext = cairo_quartz_surface_get_cg_context(surf);
28
29 CGContextRetain(mCGContext);
30
31 Init(surf);
32 if (mSurfaceValid) {
33 RecordMemoryUsed(mSize.height * 4 + sizeof(gfxQuartzSurface));
34 }
35 }
36
gfxQuartzSurface(CGContextRef context,const mozilla::gfx::IntSize & size)37 gfxQuartzSurface::gfxQuartzSurface(CGContextRef context,
38 const mozilla::gfx::IntSize& size)
39 : mCGContext(context), mSize(size) {
40 if (!mozilla::gfx::Factory::CheckSurfaceSize(size)) MakeInvalid();
41
42 unsigned int width = static_cast<unsigned int>(mSize.width);
43 unsigned int height = static_cast<unsigned int>(mSize.height);
44
45 cairo_surface_t* surf =
46 cairo_quartz_surface_create_for_cg_context(context, width, height);
47
48 CGContextRetain(mCGContext);
49
50 Init(surf);
51 if (mSurfaceValid) {
52 RecordMemoryUsed(mSize.height * 4 + sizeof(gfxQuartzSurface));
53 }
54 }
55
gfxQuartzSurface(cairo_surface_t * csurf,const mozilla::gfx::IntSize & aSize)56 gfxQuartzSurface::gfxQuartzSurface(cairo_surface_t* csurf,
57 const mozilla::gfx::IntSize& aSize)
58 : mSize(aSize) {
59 mCGContext = cairo_quartz_surface_get_cg_context(csurf);
60 CGContextRetain(mCGContext);
61
62 Init(csurf, true);
63 }
64
gfxQuartzSurface(unsigned char * data,const mozilla::gfx::IntSize & aSize,long stride,gfxImageFormat format)65 gfxQuartzSurface::gfxQuartzSurface(unsigned char* data,
66 const mozilla::gfx::IntSize& aSize,
67 long stride, gfxImageFormat format)
68 : mCGContext(nullptr), mSize(aSize.width, aSize.height) {
69 if (!mozilla::gfx::Factory::CheckSurfaceSize(aSize)) MakeInvalid();
70
71 cairo_format_t cformat = GfxFormatToCairoFormat(format);
72 cairo_surface_t* surf = cairo_quartz_surface_create_for_data(
73 data, cformat, aSize.width, aSize.height, stride);
74
75 mCGContext = cairo_quartz_surface_get_cg_context(surf);
76
77 CGContextRetain(mCGContext);
78
79 Init(surf);
80 if (mSurfaceValid) {
81 RecordMemoryUsed(mSize.height * stride + sizeof(gfxQuartzSurface));
82 }
83 }
84
GetAsImageSurface()85 already_AddRefed<gfxImageSurface> gfxQuartzSurface::GetAsImageSurface() {
86 cairo_surface_t* surface = cairo_quartz_surface_get_image(mSurface);
87 if (!surface || cairo_surface_status(surface)) return nullptr;
88
89 RefPtr<gfxASurface> img = Wrap(surface);
90
91 // cairo_quartz_surface_get_image returns a referenced image, and thebes
92 // shares the refcounts of Cairo surfaces. However, Wrap also adds a
93 // reference to the image. We need to remove one of these references
94 // explicitly so we don't leak.
95 img.get()->Release();
96
97 img->SetOpaqueRect(GetOpaqueRect());
98
99 return img.forget().downcast<gfxImageSurface>();
100 }
101
~gfxQuartzSurface()102 gfxQuartzSurface::~gfxQuartzSurface() { CGContextRelease(mCGContext); }
103