summaryrefslogtreecommitdiff
path: root/test/image-formats.c
blob: 6abe075057e1ab5fa4b6a1c5c20568a447b15b79 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/*
 * Copyright 2010 Red Hat Inc.
 *
 * Permission to use, copy, modify, distribute, and sell this software
 * and its documentation for any purpose is hereby granted without
 * fee, provided that the above copyright notice appear in all copies
 * and that both that copyright notice and this permission notice
 * appear in supporting documentation, and that the name of
 * Benjamin Otte not be used in advertising or publicity pertaining to
 * distribution of the software without specific, written prior
 * permission. Benjamin Otte makes no representations about the
 * suitability of this software for any purpose.  It is provided "as
 * is" without express or implied warranty.
 *
 * BENJAMIN OTTE DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
 * FITNESS, IN NO EVENT SHALL BENJAMIN OTTE BE LIABLE FOR ANY SPECIAL,
 * INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
 * IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 *
 * Author: Benjamin Otte <otte@redhat.com>
 */

#include "cairo-test.h"
#include <pthread.h>
#include <stdlib.h>

#define SPACE 5
#define BLOCK SPACE
#define IMAGE (2 * BLOCK)

#define N_FORMATS (CAIRO_FORMAT_PLANAR_420 + 1)
#define N_COLOR_SPACES (CAIRO_COLOR_SPACE_YCBCR_JPEG + 1)

#define WIDTH (SPACE + IMAGE) * N_FORMATS + SPACE
#define HEIGHT (SPACE + IMAGE) * N_COLOR_SPACES + SPACE

static void
draw_surface (cairo_t *cr)
{
  cairo_set_source_rgb (cr, 0.75, 0, 0);
  cairo_rectangle (cr, 0, 0, BLOCK, BLOCK);
  cairo_fill (cr);

  cairo_set_source_rgb (cr, 0, 0.75, 0);
  cairo_rectangle (cr, 0, BLOCK, BLOCK, BLOCK);
  cairo_fill (cr);

  cairo_set_source_rgb (cr, 0, 0, 0.75);
  cairo_rectangle (cr, BLOCK, 0, BLOCK, BLOCK);
  cairo_fill (cr);

  cairo_set_source_rgb (cr, 0.75, 0.75, 0.75);
  cairo_rectangle (cr, BLOCK, BLOCK, BLOCK, BLOCK);
  cairo_fill (cr);
}

static int
get_planes_for_format (cairo_format_t format)
{
    switch (format) {
    case CAIRO_FORMAT_PLANAR_444:
    case CAIRO_FORMAT_PLANAR_422:
    case CAIRO_FORMAT_PLANAR_420:
        return 3;
    case CAIRO_FORMAT_ARGB32:
    case CAIRO_FORMAT_RGB24:
    case CAIRO_FORMAT_A8:
    case CAIRO_FORMAT_A1:
    case CAIRO_FORMAT_RGB16_565:
    case CAIRO_FORMAT_RGBA32:
    case CAIRO_FORMAT_ABGR32:
    case CAIRO_FORMAT_BGRA32:
    case CAIRO_FORMAT_PACKED_YUYV:
    case CAIRO_FORMAT_PACKED_YVYU:
    case CAIRO_FORMAT_PACKED_UYVY:
    case CAIRO_FORMAT_PACKED_VYUY:
    case CAIRO_FORMAT_INVALID:
    default:
        return 1;
  }
}

static cairo_surface_t *
create_surface (cairo_format_t format, cairo_color_space_t color_space)
{
  cairo_surface_t *surface;
  static const cairo_user_data_key_t key;
  char *planes[4];
  int strides[4];
  int i;

  planes[0] = calloc (4, IMAGE * IMAGE * 4);
  strides[0] = IMAGE * 4;
  if (planes[0] == NULL)
      return NULL;

  for (i = 1; i < 4; i++) {
      planes[i] = planes[i - 1] + IMAGE * IMAGE * 4;
      strides[i] = strides[i - 1];
  }

  surface = cairo_image_surface_create_planar (format,
                                               color_space,
                                               IMAGE,
                                               IMAGE,
                                               get_planes_for_format (format),
                                               planes,
                                               strides);
  cairo_surface_set_user_data (surface, &key, planes[0], free);

  return surface;
}

static cairo_test_status_t
draw (cairo_t *cr, int width, int height)
{
    int format, color_space;
    cairo_surface_t *surface;
    cairo_t *cr2;

    cairo_set_source_rgb (cr, 0.25, 0.25, 0.25);
    cairo_paint (cr);

    for (format = 0; format < N_FORMATS; format++) {
        for (color_space = 0; color_space < N_COLOR_SPACES; color_space++) {
            surface = create_surface (format, color_space);
            if (surface == NULL)
                return CAIRO_TEST_NO_MEMORY;

            cr2 = cairo_create (surface);
            draw_surface (cr2);
            cairo_destroy (cr2);

            cairo_set_source_surface (cr, surface, (IMAGE + SPACE) * format + SPACE, (IMAGE + SPACE) * color_space + SPACE);
            cairo_paint (cr);

            cairo_surface_destroy (surface);
        }
    }

    return CAIRO_TEST_SUCCESS;
}

CAIRO_TEST (image_formats,
	    "Draw lots of 1x1 rectangles on similar surfaces in lots of threads",
	    "", /* keywords */
	    NULL, /* requirements */
	    WIDTH, HEIGHT,
	    NULL, draw)