source: mainline/uspace/lib/draw/surface.c@ cc36562b

Last change on this file since cc36562b was a35b458, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 7 years ago

style: Remove trailing whitespace on _all_ lines, including empty ones, for particular file types.

Command used: tools/srepl '\s\+$' '' -- *.c *.h *.py *.sh *.s *.S *.ag

Currently, whitespace on empty lines is very inconsistent.
There are two basic choices: Either remove the whitespace, or keep empty lines
indented to the level of surrounding code. The former is AFAICT more common,
and also much easier to do automatically.

Alternatively, we could write script for automatic indentation, and use that
instead. However, if such a script exists, it's possible to use the indented
style locally, by having the editor apply relevant conversions on load/save,
without affecting remote repository. IMO, it makes more sense to adopt
the simpler rule.

  • Property mode set to 100644
File size: 5.2 KB
Line 
1/*
2 * Copyright (c) 2011 Martin Decky
3 * Copyright (c) 2012 Petr Koupy
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * - Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * - Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * - The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30/** @addtogroup draw
31 * @{
32 */
33/**
34 * @file
35 */
36
37#include <mem.h>
38#include <as.h>
39#include <assert.h>
40#include <stdlib.h>
41#include "surface.h"
42
43struct surface {
44 surface_flags_t flags;
45
46 surface_coord_t dirty_x_lo;
47 surface_coord_t dirty_x_hi;
48 surface_coord_t dirty_y_lo;
49 surface_coord_t dirty_y_hi;
50
51 pixelmap_t pixmap;
52};
53
54surface_t *surface_create(surface_coord_t width, surface_coord_t height,
55 pixel_t *pixbuf, surface_flags_t flags)
56{
57 surface_t *surface = (surface_t *) malloc(sizeof(surface_t));
58 if (!surface) {
59 return NULL;
60 }
61
62 size_t pixbuf_size = width * height * sizeof(pixel_t);
63
64 if (!pixbuf) {
65 if ((flags & SURFACE_FLAG_SHARED) == SURFACE_FLAG_SHARED) {
66 pixbuf = (pixel_t *) as_area_create(AS_AREA_ANY,
67 pixbuf_size,
68 AS_AREA_READ | AS_AREA_WRITE | AS_AREA_CACHEABLE,
69 AS_AREA_UNPAGED);
70 if (pixbuf == AS_MAP_FAILED) {
71 free(surface);
72 return NULL;
73 }
74 } else {
75 pixbuf = (pixel_t *) malloc(pixbuf_size);
76 if (pixbuf == NULL) {
77 free(surface);
78 return NULL;
79 }
80 }
81
82 memset(pixbuf, 0, pixbuf_size);
83 }
84
85 surface->flags = flags;
86 surface->pixmap.width = width;
87 surface->pixmap.height = height;
88 surface->pixmap.data = pixbuf;
89
90 surface_reset_damaged_region(surface);
91
92 return surface;
93}
94
95void surface_destroy(surface_t *surface)
96{
97 pixel_t *pixbuf = surface->pixmap.data;
98
99 if ((surface->flags & SURFACE_FLAG_SHARED) == SURFACE_FLAG_SHARED)
100 as_area_destroy((void *) pixbuf);
101 else
102 free(pixbuf);
103
104 free(surface);
105}
106
107bool surface_is_shared(surface_t *surface)
108{
109 return ((surface->flags & SURFACE_FLAG_SHARED) == SURFACE_FLAG_SHARED);
110}
111
112pixel_t *surface_direct_access(surface_t *surface)
113{
114 return surface->pixmap.data;
115}
116
117pixelmap_t *surface_pixmap_access(surface_t *surface)
118{
119 return &surface->pixmap;
120}
121
122void surface_get_resolution(surface_t *surface, surface_coord_t *width, surface_coord_t *height)
123{
124 assert(width);
125 assert(height);
126
127 *width = surface->pixmap.width;
128 *height = surface->pixmap.height;
129}
130
131void surface_get_damaged_region(surface_t *surface, surface_coord_t *x, surface_coord_t *y,
132 surface_coord_t *width, surface_coord_t *height)
133{
134 assert(x);
135 assert(y);
136 assert(width);
137 assert(height);
138
139 *x = surface->dirty_x_lo;
140 *y = surface->dirty_y_lo;
141 *width = surface->dirty_x_lo <= surface->dirty_x_hi ?
142 (surface->dirty_x_hi - surface->dirty_x_lo) + 1 : 0;
143 *height = surface->dirty_y_lo <= surface->dirty_y_hi ?
144 (surface->dirty_y_hi - surface->dirty_y_lo) + 1 : 0;
145}
146
147void surface_add_damaged_region(surface_t *surface, surface_coord_t x, surface_coord_t y,
148 surface_coord_t width, surface_coord_t height)
149{
150 surface->dirty_x_lo = surface->dirty_x_lo > x ? x : surface->dirty_x_lo;
151 surface->dirty_y_lo = surface->dirty_y_lo > y ? y : surface->dirty_y_lo;
152
153 surface_coord_t x_hi = x + width - 1;
154 surface_coord_t y_hi = y + height - 1;
155
156 surface->dirty_x_hi = surface->dirty_x_hi < x_hi ? x_hi : surface->dirty_x_hi;
157 surface->dirty_y_hi = surface->dirty_y_hi < y_hi ? y_hi : surface->dirty_y_hi;
158}
159
160void surface_reset_damaged_region(surface_t *surface)
161{
162 surface->dirty_x_lo = surface->pixmap.width - 1;
163 surface->dirty_x_hi = 0;
164 surface->dirty_y_lo = surface->pixmap.height - 1;
165 surface->dirty_y_hi = 0;
166}
167
168void surface_put_pixel(surface_t *surface, surface_coord_t x, surface_coord_t y, pixel_t pixel)
169{
170 surface->dirty_x_lo = surface->dirty_x_lo > x ? x : surface->dirty_x_lo;
171 surface->dirty_x_hi = surface->dirty_x_hi < x ? x : surface->dirty_x_hi;
172 surface->dirty_y_lo = surface->dirty_y_lo > y ? y : surface->dirty_y_lo;
173 surface->dirty_y_hi = surface->dirty_y_hi < y ? y : surface->dirty_y_hi;
174
175 pixelmap_put_pixel(&surface->pixmap, x, y, pixel);
176}
177
178pixel_t surface_get_pixel(surface_t *surface, surface_coord_t x, surface_coord_t y)
179{
180 return pixelmap_get_pixel(&surface->pixmap, x, y);
181}
182
183/** @}
184 */
Note: See TracBrowser for help on using the repository browser.