2 * Copyright © 2011 Uli Schlachter
4 * Permission is hereby granted, free of charge, to any person
5 * obtaining a copy of this software and associated documentation
6 * files (the "Software"), to deal in the Software without
7 * restriction, including without limitation the rights to use, copy,
8 * modify, merge, publish, distribute, sublicense, and/or sell copies
9 * of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
19 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 * Author: Uli Schlachter <psychon@znc.in>
27 #include "cairo-test.h"
32 /* This is a random pick. This results in 512 (width) * 512 (height) *
33 * 2 (surfaces per run) * 4 (ARGB32) * ITERATIONS = 200 MiB of image data. */
34 #define ITERATIONS 100
36 /* This tries to trigger a bug in the xcb backend where a Picture is freed to
37 * early. It goes something like this:
39 * - _composite_mask calls _cairo_xcb_picture_for_pattern to get a xcb_picture_t
41 * - _cairo_xcb_picture_for_pattern calls _cairo_xcb_surface_picture which calls
42 * _cairo_xcb_screen_store_surface_picture which adds the picture to a cache.
43 * - _cairo_xcb_surface_picture also attached the picture as a snapshot to
44 * the source surface using cairo_surface_finish as detach_func.
45 * - _composite_mask calls _cairo_xcb_picture_for_pattern to get a xcb_picture_t
47 * - The resulting picture surface is added to the cache again, but the cache is
48 * already full, so a random cache entry is picked and removed.
49 * - The surface that was added before is picked and gets fed to
50 * _surface_cache_entry_destroy.
51 * - This calls _cairo_surface_detach_snapshot which causes the
52 * detach_func from above to be called, so the surface is finished and the
53 * associated picture is FreePicture'd.
54 * - _composite_mask now uses a Picture that was already freed.
56 * So this depends on the screen's surface cache to be full which is why we do
60 static cairo_surface_t *
63 cairo_surface_t *surface;
66 surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, WIDTH, HEIGHT);
67 /* Paint something random to the image */
68 cr = cairo_create (surface);
70 cairo_set_source_rgb (cr, 0, 1, 1);
71 cairo_rectangle (cr, 0, 0, WIDTH/2.0, HEIGHT/2.0);
73 cairo_set_source_rgb (cr, 1, 0, 1);
74 cairo_rectangle (cr, WIDTH/2.0, HEIGHT/2.0, WIDTH/2.0, HEIGHT/2.0);
81 static cairo_surface_t *
82 dirty_cache (cairo_t *cr)
84 cairo_surface_t *surface;
86 /* Set a source surface... */
87 surface = create_image ();
88 cairo_set_source_surface (cr, surface, 0, 0);
89 cairo_surface_destroy (surface);
90 /* ...and create a mask surface, so that we can hit the early FreePicture */
91 surface = create_image ();
92 cairo_mask_surface (cr, surface, 0, 0);
97 static cairo_test_status_t
98 draw (cairo_t *cr, int width, int height)
101 cairo_surface_t *array[ITERATIONS];
103 /* We have to keep the associated cairo_surface_t alive so that they aren't
104 * removed from the cache */
105 for (i = 0; i < ITERATIONS; i++)
106 array[i] = dirty_cache (cr);
107 for (i = 0; i < ITERATIONS; i++)
108 cairo_surface_destroy (array[i]);
110 return CAIRO_TEST_SUCCESS;
113 CAIRO_TEST (xcb_stress_cache,
114 "Stress test for a image surface cache in cairo-xcb",
115 "xcb, stress", /* keywords */
116 NULL, /* requirements */