| 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 <malloc.h>
|
|---|
| 38 | #include <mem.h>
|
|---|
| 39 | #include <as.h>
|
|---|
| 40 | #include <assert.h>
|
|---|
| 41 | #include "surface.h"
|
|---|
| 42 |
|
|---|
| 43 | struct 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 |
|
|---|
| 54 | surface_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 |
|
|---|
| 95 | void 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 |
|
|---|
| 107 | bool surface_is_shared(surface_t *surface)
|
|---|
| 108 | {
|
|---|
| 109 | return ((surface->flags & SURFACE_FLAG_SHARED) == SURFACE_FLAG_SHARED);
|
|---|
| 110 | }
|
|---|
| 111 |
|
|---|
| 112 | pixel_t *surface_direct_access(surface_t *surface)
|
|---|
| 113 | {
|
|---|
| 114 | return surface->pixmap.data;
|
|---|
| 115 | }
|
|---|
| 116 |
|
|---|
| 117 | pixelmap_t *surface_pixmap_access(surface_t *surface)
|
|---|
| 118 | {
|
|---|
| 119 | return &surface->pixmap;
|
|---|
| 120 | }
|
|---|
| 121 |
|
|---|
| 122 | void 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 |
|
|---|
| 131 | void 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 |
|
|---|
| 147 | void 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 |
|
|---|
| 160 | void 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 |
|
|---|
| 168 | void 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 |
|
|---|
| 178 | pixel_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 | */
|
|---|