Line data Source code
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 "SourceSurfaceCairo.h"
7 : #include "DrawTargetCairo.h"
8 : #include "HelpersCairo.h"
9 : #include "DataSourceSurfaceWrapper.h"
10 :
11 : #include "cairo.h"
12 :
13 : namespace mozilla {
14 : namespace gfx {
15 :
16 : static SurfaceFormat
17 0 : CairoFormatToSurfaceFormat(cairo_format_t format)
18 : {
19 0 : switch (format)
20 : {
21 : case CAIRO_FORMAT_ARGB32:
22 0 : return SurfaceFormat::B8G8R8A8;
23 : case CAIRO_FORMAT_RGB24:
24 0 : return SurfaceFormat::B8G8R8X8;
25 : case CAIRO_FORMAT_RGB16_565:
26 0 : return SurfaceFormat::R5G6B5_UINT16;
27 : case CAIRO_FORMAT_A8:
28 0 : return SurfaceFormat::A8;
29 : default:
30 0 : return SurfaceFormat::B8G8R8A8;
31 : }
32 : }
33 :
34 0 : SourceSurfaceCairo::SourceSurfaceCairo(cairo_surface_t* aSurface,
35 : const IntSize& aSize,
36 : const SurfaceFormat& aFormat,
37 0 : DrawTargetCairo* aDrawTarget /* = nullptr */)
38 : : mSize(aSize)
39 0 : , mFormat(aFormat)
40 : , mSurface(aSurface)
41 0 : , mDrawTarget(aDrawTarget)
42 : {
43 0 : cairo_surface_reference(mSurface);
44 0 : }
45 :
46 0 : SourceSurfaceCairo::~SourceSurfaceCairo()
47 : {
48 0 : cairo_surface_destroy(mSurface);
49 0 : }
50 :
51 : IntSize
52 0 : SourceSurfaceCairo::GetSize() const
53 : {
54 0 : return mSize;
55 : }
56 :
57 : SurfaceFormat
58 0 : SourceSurfaceCairo::GetFormat() const
59 : {
60 0 : return mFormat;
61 : }
62 :
63 : already_AddRefed<DataSourceSurface>
64 0 : SourceSurfaceCairo::GetDataSurface()
65 : {
66 0 : RefPtr<DataSourceSurface> dataSurf;
67 :
68 0 : if (cairo_surface_get_type(mSurface) == CAIRO_SURFACE_TYPE_IMAGE) {
69 0 : dataSurf = new DataSourceSurfaceCairo(mSurface);
70 : } else {
71 0 : cairo_surface_t* imageSurf = cairo_image_surface_create(GfxFormatToCairoFormat(mFormat),
72 0 : mSize.width, mSize.height);
73 :
74 : // Fill the new image surface with the contents of our surface.
75 0 : cairo_t* ctx = cairo_create(imageSurf);
76 0 : cairo_set_source_surface(ctx, mSurface, 0, 0);
77 0 : cairo_paint(ctx);
78 0 : cairo_destroy(ctx);
79 :
80 0 : dataSurf = new DataSourceSurfaceCairo(imageSurf);
81 0 : cairo_surface_destroy(imageSurf);
82 : }
83 :
84 : // We also need to make sure that the returned surface has
85 : // surface->GetType() == SurfaceType::DATA.
86 0 : return MakeAndAddRef<DataSourceSurfaceWrapper>(dataSurf);
87 : }
88 :
89 : cairo_surface_t*
90 0 : SourceSurfaceCairo::GetSurface() const
91 : {
92 0 : return mSurface;
93 : }
94 :
95 : void
96 0 : SourceSurfaceCairo::DrawTargetWillChange()
97 : {
98 0 : if (mDrawTarget) {
99 0 : mDrawTarget = nullptr;
100 :
101 : // We're about to lose our version of the surface, so make a copy of it.
102 0 : cairo_surface_t* surface = cairo_surface_create_similar(mSurface,
103 : GfxFormatToCairoContent(mFormat),
104 0 : mSize.width, mSize.height);
105 0 : cairo_t* ctx = cairo_create(surface);
106 0 : cairo_pattern_t* pat = cairo_pattern_create_for_surface(mSurface);
107 0 : cairo_set_source(ctx, pat);
108 0 : cairo_paint(ctx);
109 0 : cairo_destroy(ctx);
110 0 : cairo_pattern_destroy(pat);
111 :
112 : // Swap in this new surface.
113 0 : cairo_surface_destroy(mSurface);
114 0 : mSurface = surface;
115 : }
116 0 : }
117 :
118 0 : DataSourceSurfaceCairo::DataSourceSurfaceCairo(cairo_surface_t* imageSurf)
119 0 : : mImageSurface(imageSurf)
120 : {
121 0 : cairo_surface_reference(mImageSurface);
122 0 : }
123 :
124 0 : DataSourceSurfaceCairo::~DataSourceSurfaceCairo()
125 : {
126 0 : cairo_surface_destroy(mImageSurface);
127 0 : }
128 :
129 : unsigned char *
130 0 : DataSourceSurfaceCairo::GetData()
131 : {
132 0 : return cairo_image_surface_get_data(mImageSurface);
133 : }
134 :
135 : int32_t
136 0 : DataSourceSurfaceCairo::Stride()
137 : {
138 0 : return cairo_image_surface_get_stride(mImageSurface);
139 : }
140 :
141 : IntSize
142 0 : DataSourceSurfaceCairo::GetSize() const
143 : {
144 0 : IntSize size;
145 0 : size.width = cairo_image_surface_get_width(mImageSurface);
146 0 : size.height = cairo_image_surface_get_height(mImageSurface);
147 :
148 0 : return size;
149 : }
150 :
151 : SurfaceFormat
152 0 : DataSourceSurfaceCairo::GetFormat() const
153 : {
154 0 : return CairoFormatToSurfaceFormat(cairo_image_surface_get_format(mImageSurface));
155 : }
156 :
157 : cairo_surface_t*
158 0 : DataSourceSurfaceCairo::GetSurface() const
159 : {
160 0 : return mImageSurface;
161 : }
162 :
163 : } // namespace gfx
164 : } // namespace mozilla
|