aboutsummaryrefslogtreecommitdiff
path: root/src/wld/renderer.c
blob: a038095f27ef6561b66775f54376bd34fd6b7768 (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
153
154
155
156
157
/* wld: renderer.c
 *
 * Copyright (c) 2013, 2014 Michael Forney
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

#include "wld-private.h"

void default_fill_region(struct wld_renderer * renderer, uint32_t color,
                         pixman_region32_t * region)
{
    pixman_box32_t * box;
    int num_boxes;

    box = pixman_region32_rectangles(region, &num_boxes);

    while (num_boxes--)
    {
        renderer->impl->fill_rectangle(renderer, color, box->x1, box->y1,
                                       box->x2 - box->x1, box->y2 - box->y1);
        ++box;
    }
}

void default_copy_region(struct wld_renderer * renderer, struct buffer * buffer,
                         int32_t dst_x, int32_t dst_y,
                         pixman_region32_t * region)
{
    pixman_box32_t * box;
    int num_boxes;

    box = pixman_region32_rectangles(region, &num_boxes);

    while (num_boxes--)
    {
        renderer->impl->copy_rectangle(renderer, buffer,
                                       dst_x + box->x1, dst_y + box->y1,
                                       box->x1, box->y1,
                                       box->x2 - box->x1, box->y2 - box->y1);
        ++box;
    }
}

void renderer_initialize(struct wld_renderer * renderer,
                         const struct wld_renderer_impl * impl)
{
    *((const struct wld_renderer_impl **) &renderer->impl) = impl;
    renderer->target = NULL;
}

EXPORT
void wld_destroy_renderer(struct wld_renderer * renderer)
{
    renderer->impl->destroy(renderer);
}

EXPORT
uint32_t wld_capabilities(struct wld_renderer * renderer,
                          struct wld_buffer * buffer)
{
    return renderer->impl->capabilities(renderer, (struct buffer *) buffer);
}

EXPORT
bool wld_set_target_buffer(struct wld_renderer * renderer,
                           struct wld_buffer * buffer)
{
    if (!renderer->impl->set_target(renderer, (struct buffer *) buffer))
        return false;

    renderer->target = buffer;

    return true;
}

EXPORT
bool wld_set_target_surface(struct wld_renderer * renderer,
                            struct wld_surface * surface)
{
    struct buffer * back_buffer;

    if (!(back_buffer = surface->impl->back(surface)))
        return false;

    return renderer->impl->set_target(renderer, back_buffer);
}

EXPORT
void wld_fill_rectangle(struct wld_renderer * renderer, uint32_t color,
                        int32_t x, int32_t y, uint32_t width, uint32_t height)
{
    renderer->impl->fill_rectangle(renderer, color, x, y, width, height);
}

EXPORT
void wld_fill_region(struct wld_renderer * renderer, uint32_t color,
                     pixman_region32_t * region)
{
    renderer->impl->fill_region(renderer, color, region);
}

EXPORT
void wld_copy_rectangle(struct wld_renderer * renderer,
                        struct wld_buffer * buffer,
                        int32_t dst_x, int32_t dst_y,
                        int32_t src_x, int32_t src_y,
                        uint32_t width, uint32_t height)
{
    renderer->impl->copy_rectangle(renderer, (struct buffer *) buffer,
                                   dst_x, dst_y, src_x, src_y, width, height);
}

EXPORT
void wld_copy_region(struct wld_renderer * renderer,
                     struct wld_buffer * buffer,
                     int32_t dst_x, int32_t dst_y, pixman_region32_t * region)
{
    renderer->impl->copy_region(renderer, (struct buffer *) buffer,
                                dst_x, dst_y, region);
}

EXPORT
void wld_draw_text(struct wld_renderer * renderer,
                   struct wld_font * font_base, uint32_t color,
                   int32_t x, int32_t y, const char * text, uint32_t length,
                   struct wld_extents * extents)
{
    struct font * font = (void *) font_base;

    renderer->impl->draw_text(renderer, font, color, x, y, text, length,
                              extents);
}

EXPORT
void wld_flush(struct wld_renderer * renderer)
{
    renderer->impl->flush(renderer);
    renderer->impl->set_target(renderer, NULL);
}