1 : /* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 : * ***** BEGIN LICENSE BLOCK *****
3 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 : *
5 : * The contents of this file are subject to the Mozilla Public License Version
6 : * 1.1 (the "License"); you may not use this file except in compliance with
7 : * the License. You may obtain a copy of the License at
8 : * http://www.mozilla.org/MPL/
9 : *
10 : * Software distributed under the License is distributed on an "AS IS" basis,
11 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 : * for the specific language governing rights and limitations under the
13 : * License.
14 : *
15 : * The Original Code is Mozilla Corporation code.
16 : *
17 : * The Initial Developer of the Original Code is Mozilla Foundation.
18 : * Portions created by the Initial Developer are Copyright (C) 2009
19 : * the Initial Developer. All Rights Reserved.
20 : *
21 : * Contributor(s):
22 : * Bas Schouten <bschouten@mozilla.com>
23 : *
24 : * Alternatively, the contents of this file may be used under the terms of
25 : * either the GNU General Public License Version 2 or later (the "GPL"), or
26 : * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 : * in which case the provisions of the GPL or the LGPL are applicable instead
28 : * of those above. If you wish to allow use of your version of this file only
29 : * under the terms of either the GPL or the LGPL, and not to allow others to
30 : * use your version of this file under the terms of the MPL, indicate your
31 : * decision by deleting the provisions above and replace them with the notice
32 : * and other provisions required by the GPL or the LGPL. If you do not delete
33 : * the provisions above, a recipient may use your version of this file under
34 : * the terms of any one of the MPL, the GPL or the LGPL.
35 : *
36 : * ***** END LICENSE BLOCK ***** */
37 :
38 : #include "SourceSurfaceCairo.h"
39 : #include "DrawTargetCairo.h"
40 : #include "HelpersCairo.h"
41 :
42 : #include "cairo.h"
43 :
44 : namespace mozilla {
45 : namespace gfx {
46 :
47 : static SurfaceFormat
48 0 : CairoFormatToSurfaceFormat(cairo_format_t format)
49 : {
50 0 : switch (format)
51 : {
52 : case CAIRO_FORMAT_ARGB32:
53 0 : return FORMAT_B8G8R8A8;
54 : case CAIRO_FORMAT_RGB24:
55 0 : return FORMAT_B8G8R8X8;
56 : case CAIRO_FORMAT_A8:
57 0 : return FORMAT_A8;
58 : default:
59 0 : return FORMAT_B8G8R8A8;
60 : }
61 : }
62 :
63 0 : SourceSurfaceCairo::SourceSurfaceCairo(cairo_surface_t* aSurface,
64 : const IntSize& aSize,
65 : const SurfaceFormat& aFormat,
66 : DrawTargetCairo* aDrawTarget /* = NULL */)
67 : : mSize(aSize)
68 : , mFormat(aFormat)
69 : , mSurface(aSurface)
70 0 : , mDrawTarget(aDrawTarget)
71 : {
72 0 : cairo_surface_reference(mSurface);
73 0 : }
74 :
75 0 : SourceSurfaceCairo::~SourceSurfaceCairo()
76 : {
77 0 : MarkIndependent();
78 0 : cairo_surface_destroy(mSurface);
79 0 : }
80 :
81 : IntSize
82 0 : SourceSurfaceCairo::GetSize() const
83 : {
84 0 : return mSize;
85 : }
86 :
87 : SurfaceFormat
88 0 : SourceSurfaceCairo::GetFormat() const
89 : {
90 0 : return mFormat;
91 : }
92 :
93 : TemporaryRef<DataSourceSurface>
94 0 : SourceSurfaceCairo::GetDataSurface()
95 : {
96 0 : RefPtr<DataSourceSurfaceCairo> dataSurf;
97 :
98 0 : if (cairo_surface_get_type(mSurface) == CAIRO_SURFACE_TYPE_IMAGE) {
99 0 : dataSurf = new DataSourceSurfaceCairo(mSurface);
100 : } else {
101 : cairo_surface_t* imageSurf = cairo_image_surface_create(GfxFormatToCairoFormat(mFormat),
102 0 : mSize.width, mSize.height);
103 :
104 : // Fill the new image surface with the contents of our surface.
105 0 : cairo_t* ctx = cairo_create(imageSurf);
106 0 : cairo_set_source_surface(ctx, mSurface, 0, 0);
107 0 : cairo_paint(ctx);
108 0 : cairo_destroy(ctx);
109 :
110 0 : dataSurf = new DataSourceSurfaceCairo(imageSurf);
111 0 : cairo_surface_destroy(imageSurf);
112 : }
113 :
114 0 : return dataSurf;
115 : }
116 :
117 : cairo_surface_t*
118 0 : SourceSurfaceCairo::GetSurface() const
119 : {
120 0 : return mSurface;
121 : }
122 :
123 : void
124 0 : SourceSurfaceCairo::DrawTargetWillChange()
125 : {
126 0 : if (mDrawTarget) {
127 0 : mDrawTarget = NULL;
128 :
129 : // We're about to lose our version of the surface, so make a copy of it.
130 : cairo_surface_t* surface = cairo_surface_create_similar(mSurface,
131 : GfxFormatToCairoContent(mFormat),
132 0 : mSize.width, mSize.height);
133 0 : cairo_t* ctx = cairo_create(surface);
134 0 : cairo_pattern_t* pat = cairo_pattern_create_for_surface(mSurface);
135 0 : cairo_set_source(ctx, pat);
136 0 : cairo_paint(ctx);
137 0 : cairo_destroy(ctx);
138 :
139 : // Swap in this new surface.
140 0 : cairo_surface_destroy(mSurface);
141 0 : mSurface = surface;
142 : }
143 0 : }
144 :
145 : void
146 0 : SourceSurfaceCairo::MarkIndependent()
147 : {
148 0 : if (mDrawTarget) {
149 0 : mDrawTarget->RemoveSnapshot(this);
150 0 : mDrawTarget = NULL;
151 : }
152 0 : }
153 :
154 0 : DataSourceSurfaceCairo::DataSourceSurfaceCairo(cairo_surface_t* imageSurf)
155 0 : : mImageSurface(imageSurf)
156 : {
157 0 : cairo_surface_reference(mImageSurface);
158 0 : }
159 :
160 0 : DataSourceSurfaceCairo::~DataSourceSurfaceCairo()
161 : {
162 0 : cairo_surface_destroy(mImageSurface);
163 0 : }
164 :
165 : unsigned char *
166 0 : DataSourceSurfaceCairo::GetData()
167 : {
168 0 : return cairo_image_surface_get_data(mImageSurface);
169 : }
170 :
171 : int32_t
172 0 : DataSourceSurfaceCairo::Stride()
173 : {
174 0 : return cairo_image_surface_get_stride(mImageSurface);
175 : }
176 :
177 : IntSize
178 0 : DataSourceSurfaceCairo::GetSize() const
179 : {
180 0 : IntSize size;
181 0 : size.width = cairo_image_surface_get_width(mImageSurface);
182 0 : size.height = cairo_image_surface_get_height(mImageSurface);
183 :
184 : return size;
185 : }
186 :
187 : SurfaceFormat
188 0 : DataSourceSurfaceCairo::GetFormat() const
189 : {
190 0 : return CairoFormatToSurfaceFormat(cairo_image_surface_get_format(mImageSurface));
191 : }
192 :
193 : cairo_surface_t*
194 0 : DataSourceSurfaceCairo::GetSurface() const
195 : {
196 0 : return mImageSurface;
197 : }
198 :
199 : }
200 : }
|