1 /* Cairo - a vector graphics library with display and print output
3 * Copyright © 2008 Chris Wilson
4 * Copyright © 2009 Intel Corporation
6 * This library is free software; you can redistribute it and/or
7 * modify it either under the terms of the GNU Lesser General Public
8 * License version 2.1 as published by the Free Software Foundation
9 * (the "LGPL") or, at your option, under the terms of the Mozilla
10 * Public License Version 1.1 (the "MPL"). If you do not alter this
11 * notice, a recipient may use your version of this file under either
12 * the MPL or the LGPL.
14 * You should have received a copy of the LGPL along with this library
15 * in the file COPYING-LGPL-2.1; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA
17 * You should have received a copy of the MPL along with this library
18 * in the file COPYING-MPL-1.1
20 * The contents of this file are subject to the Mozilla Public License
21 * Version 1.1 (the "License"); you may not use this file except in
22 * compliance with the License. You may obtain a copy of the License at
23 * http://www.mozilla.org/MPL/
25 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY
26 * OF ANY KIND, either express or implied. See the LGPL or the MPL for
27 * the specific language governing rights and limitations.
30 * Chris Wilson <chris@chris-wilson.co.uk>
35 #include "cairo-xcb-private.h"
36 #include "cairo-list-inline.h"
38 struct pattern_cache_entry {
39 cairo_cache_entry_t key;
40 cairo_xcb_screen_t *screen;
41 cairo_pattern_union_t pattern;
42 cairo_surface_t *picture;
46 _cairo_xcb_screen_finish (cairo_xcb_screen_t *screen)
50 CAIRO_MUTEX_LOCK (screen->connection->screens_mutex);
51 cairo_list_del (&screen->link);
52 CAIRO_MUTEX_UNLOCK (screen->connection->screens_mutex);
54 while (! cairo_list_is_empty (&screen->surfaces)) {
55 cairo_surface_t *surface;
57 surface = &cairo_list_first_entry (&screen->surfaces,
61 cairo_surface_finish (surface);
64 while (! cairo_list_is_empty (&screen->pictures)) {
65 cairo_surface_t *surface;
67 surface = &cairo_list_first_entry (&screen->pictures,
71 cairo_surface_finish (surface);
74 for (i = 0; i < screen->solid_cache_size; i++)
75 cairo_surface_destroy (screen->solid_cache[i].picture);
77 for (i = 0; i < ARRAY_LENGTH (screen->stock_colors); i++)
78 cairo_surface_destroy (screen->stock_colors[i]);
80 for (i = 0; i < ARRAY_LENGTH (screen->gc); i++) {
81 if (screen->gc_depths[i] != 0)
82 _cairo_xcb_connection_free_gc (screen->connection, screen->gc[i]);
85 _cairo_cache_fini (&screen->linear_pattern_cache);
86 _cairo_cache_fini (&screen->radial_pattern_cache);
87 _cairo_freelist_fini (&screen->pattern_cache_entry_freelist);
93 _linear_pattern_cache_entry_equal (const void *A, const void *B)
95 const struct pattern_cache_entry *a = A, *b = B;
97 return _cairo_linear_pattern_equal (&a->pattern.gradient.linear,
98 &b->pattern.gradient.linear);
102 _radial_pattern_cache_entry_equal (const void *A, const void *B)
104 const struct pattern_cache_entry *a = A, *b = B;
106 return _cairo_radial_pattern_equal (&a->pattern.gradient.radial,
107 &b->pattern.gradient.radial);
111 _pattern_cache_entry_destroy (void *closure)
113 struct pattern_cache_entry *entry = closure;
115 _cairo_pattern_fini (&entry->pattern.base);
116 cairo_surface_destroy (entry->picture);
117 _cairo_freelist_free (&entry->screen->pattern_cache_entry_freelist, entry);
121 _cairo_xcb_screen_get (xcb_connection_t *xcb_connection,
122 xcb_screen_t *xcb_screen)
124 cairo_xcb_connection_t *connection;
125 cairo_xcb_screen_t *screen;
126 cairo_status_t status;
129 connection = _cairo_xcb_connection_get (xcb_connection);
130 if (unlikely (connection == NULL))
133 CAIRO_MUTEX_LOCK (connection->screens_mutex);
135 cairo_list_foreach_entry (screen,
137 &connection->screens,
140 if (screen->xcb_screen == xcb_screen) {
141 /* Maintain list in MRU order */
142 if (&screen->link != connection->screens.next)
143 cairo_list_move (&screen->link, &connection->screens);
149 screen = malloc (sizeof (cairo_xcb_screen_t));
150 if (unlikely (screen == NULL))
153 screen->connection = connection;
154 screen->xcb_screen = xcb_screen;
156 _cairo_freelist_init (&screen->pattern_cache_entry_freelist,
157 sizeof (struct pattern_cache_entry));
158 cairo_list_init (&screen->link);
159 cairo_list_init (&screen->surfaces);
160 cairo_list_init (&screen->pictures);
162 memset (screen->gc_depths, 0, sizeof (screen->gc_depths));
163 memset (screen->gc, 0, sizeof (screen->gc));
165 screen->solid_cache_size = 0;
166 for (i = 0; i < ARRAY_LENGTH (screen->stock_colors); i++)
167 screen->stock_colors[i] = NULL;
169 status = _cairo_cache_init (&screen->linear_pattern_cache,
170 _linear_pattern_cache_entry_equal,
172 _pattern_cache_entry_destroy,
174 if (unlikely (status))
177 status = _cairo_cache_init (&screen->radial_pattern_cache,
178 _radial_pattern_cache_entry_equal,
180 _pattern_cache_entry_destroy,
182 if (unlikely (status))
185 cairo_list_add (&screen->link, &connection->screens);
188 CAIRO_MUTEX_UNLOCK (connection->screens_mutex);
193 _cairo_cache_fini (&screen->linear_pattern_cache);
195 CAIRO_MUTEX_UNLOCK (connection->screens_mutex);
201 static xcb_gcontext_t
202 _create_gc (cairo_xcb_screen_t *screen,
203 xcb_drawable_t drawable)
205 uint32_t values[] = { 0 };
207 return _cairo_xcb_connection_create_gc (screen->connection, drawable,
208 XCB_GC_GRAPHICS_EXPOSURES,
213 _cairo_xcb_screen_get_gc (cairo_xcb_screen_t *screen,
214 xcb_drawable_t drawable,
219 assert (CAIRO_MUTEX_IS_LOCKED (screen->connection->device.mutex));
221 for (i = 0; i < ARRAY_LENGTH (screen->gc); i++) {
222 if (screen->gc_depths[i] == depth) {
223 screen->gc_depths[i] = 0;
224 return screen->gc[i];
228 return _create_gc (screen, drawable);
232 _cairo_xcb_screen_put_gc (cairo_xcb_screen_t *screen, int depth, xcb_gcontext_t gc)
236 assert (CAIRO_MUTEX_IS_LOCKED (screen->connection->device.mutex));
238 for (i = 0; i < ARRAY_LENGTH (screen->gc); i++) {
239 if (screen->gc_depths[i] == 0)
243 if (i == ARRAY_LENGTH (screen->gc)) {
244 /* perform random substitution to ensure fair caching over depths */
245 i = rand () % ARRAY_LENGTH (screen->gc);
246 _cairo_xcb_connection_free_gc (screen->connection, screen->gc[i]);
250 screen->gc_depths[i] = depth;
254 _cairo_xcb_screen_store_linear_picture (cairo_xcb_screen_t *screen,
255 const cairo_linear_pattern_t *linear,
256 cairo_surface_t *picture)
258 struct pattern_cache_entry *entry;
259 cairo_status_t status;
261 assert (CAIRO_MUTEX_IS_LOCKED (screen->connection->device.mutex));
263 entry = _cairo_freelist_alloc (&screen->pattern_cache_entry_freelist);
264 if (unlikely (entry == NULL))
265 return _cairo_error (CAIRO_STATUS_NO_MEMORY);
267 entry->key.hash = _cairo_linear_pattern_hash (_CAIRO_HASH_INIT_VALUE, linear);
270 status = _cairo_pattern_init_copy (&entry->pattern.base, &linear->base.base);
271 if (unlikely (status)) {
272 _cairo_freelist_free (&screen->pattern_cache_entry_freelist, entry);
276 entry->picture = cairo_surface_reference (picture);
277 entry->screen = screen;
279 status = _cairo_cache_insert (&screen->linear_pattern_cache,
281 if (unlikely (status)) {
282 cairo_surface_destroy (picture);
283 _cairo_freelist_free (&screen->pattern_cache_entry_freelist, entry);
287 return CAIRO_STATUS_SUCCESS;
291 _cairo_xcb_screen_lookup_linear_picture (cairo_xcb_screen_t *screen,
292 const cairo_linear_pattern_t *linear)
294 cairo_surface_t *picture = NULL;
295 struct pattern_cache_entry tmpl;
296 struct pattern_cache_entry *entry;
298 assert (CAIRO_MUTEX_IS_LOCKED (screen->connection->device.mutex));
300 tmpl.key.hash = _cairo_linear_pattern_hash (_CAIRO_HASH_INIT_VALUE, linear);
301 _cairo_pattern_init_static_copy (&tmpl.pattern.base, &linear->base.base);
303 entry = _cairo_cache_lookup (&screen->linear_pattern_cache, &tmpl.key);
305 picture = cairo_surface_reference (entry->picture);
311 _cairo_xcb_screen_store_radial_picture (cairo_xcb_screen_t *screen,
312 const cairo_radial_pattern_t *radial,
313 cairo_surface_t *picture)
315 struct pattern_cache_entry *entry;
316 cairo_status_t status;
318 assert (CAIRO_MUTEX_IS_LOCKED (screen->connection->device.mutex));
320 entry = _cairo_freelist_alloc (&screen->pattern_cache_entry_freelist);
321 if (unlikely (entry == NULL))
322 return _cairo_error (CAIRO_STATUS_NO_MEMORY);
324 entry->key.hash = _cairo_radial_pattern_hash (_CAIRO_HASH_INIT_VALUE, radial);
327 status = _cairo_pattern_init_copy (&entry->pattern.base, &radial->base.base);
328 if (unlikely (status)) {
329 _cairo_freelist_free (&screen->pattern_cache_entry_freelist, entry);
333 entry->picture = cairo_surface_reference (picture);
334 entry->screen = screen;
336 status = _cairo_cache_insert (&screen->radial_pattern_cache, &entry->key);
337 if (unlikely (status)) {
338 cairo_surface_destroy (picture);
339 _cairo_freelist_free (&screen->pattern_cache_entry_freelist, entry);
343 return CAIRO_STATUS_SUCCESS;
347 _cairo_xcb_screen_lookup_radial_picture (cairo_xcb_screen_t *screen,
348 const cairo_radial_pattern_t *radial)
350 cairo_surface_t *picture = NULL;
351 struct pattern_cache_entry tmpl;
352 struct pattern_cache_entry *entry;
354 assert (CAIRO_MUTEX_IS_LOCKED (screen->connection->device.mutex));
356 tmpl.key.hash = _cairo_radial_pattern_hash (_CAIRO_HASH_INIT_VALUE, radial);
357 _cairo_pattern_init_static_copy (&tmpl.pattern.base, &radial->base.base);
359 entry = _cairo_cache_lookup (&screen->radial_pattern_cache, &tmpl.key);
361 picture = cairo_surface_reference (entry->picture);