1
/*
2
 * Copyright © 2011 Uli Schlachter
3
 *
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:
11
 *
12
 * The above copyright notice and this permission notice shall be
13
 * included in all copies or substantial portions of the Software.
14
 *
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
22
 * SOFTWARE.
23
 *
24
 * Author: Uli Schlachter <psychon@znc.in>
25
 */
26

            
27
#include "cairo-test.h"
28

            
29
#define WIDTH 512
30
#define HEIGHT 512
31

            
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
35

            
36
/* This tries to trigger a bug in the xcb backend where a Picture is freed to
37
 * early. It goes something like this:
38
 *
39
 * - _composite_mask calls _cairo_xcb_picture_for_pattern to get a xcb_picture_t
40
 *   for the source.
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
46
 *   for the mask.
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.
55
 *
56
 * So this depends on the screen's surface cache to be full which is why we do
57
 * all this looping.
58
 */
59

            
60
static cairo_surface_t *
61
600
create_image (void)
62
{
63
    cairo_surface_t *surface;
64
    cairo_t *cr;
65

            
66
600
    surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, WIDTH, HEIGHT);
67
    /* Paint something random to the image */
68
600
    cr = cairo_create (surface);
69
600
    cairo_paint (cr);
70
600
    cairo_set_source_rgb (cr, 0, 1, 1);
71
600
    cairo_rectangle (cr, 0, 0, WIDTH/2.0, HEIGHT/2.0);
72
600
    cairo_fill (cr);
73
600
    cairo_set_source_rgb (cr, 1, 0, 1);
74
600
    cairo_rectangle (cr, WIDTH/2.0, HEIGHT/2.0, WIDTH/2.0, HEIGHT/2.0);
75
600
    cairo_fill (cr);
76
600
    cairo_destroy (cr);
77

            
78
600
    return surface;
79
}
80

            
81
static cairo_surface_t *
82
300
dirty_cache (cairo_t *cr)
83
{
84
    cairo_surface_t *surface;
85

            
86
    /* Set a source surface... */
87
300
    surface = create_image ();
88
300
    cairo_set_source_surface (cr, surface, 0, 0);
89
300
    cairo_surface_destroy (surface);
90
    /* ...and create a mask surface, so that we can hit the early FreePicture */
91
300
    surface = create_image ();
92
300
    cairo_mask_surface (cr, surface, 0, 0);
93

            
94
300
    return surface;
95
}
96

            
97
static cairo_test_status_t
98
3
draw (cairo_t *cr, int width, int height)
99
{
100
    int i;
101
    cairo_surface_t *array[ITERATIONS];
102

            
103
    /* We have to keep the associated cairo_surface_t alive so that they aren't
104
     * removed from the cache */
105
303
    for (i = 0; i < ITERATIONS; i++)
106
300
	array[i] = dirty_cache (cr);
107
303
    for (i = 0; i < ITERATIONS; i++)
108
300
	cairo_surface_destroy (array[i]);
109

            
110
3
    return CAIRO_TEST_SUCCESS;
111
}
112

            
113
1
CAIRO_TEST (xcb_stress_cache,
114
	    "Stress test for a image surface cache in cairo-xcb",
115
	    "xcb, stress", /* keywords */
116
	    NULL, /* requirements */
117
	    2, 2,
118
	    NULL, draw)