neuwld

patched neuwld library (tracked)
git clone https://git.daat.foo/neuwld.git
Log | Files | Refs | README | LICENSE

pixman.c (14229B)


      1 /* wld: pixman.c
      2  *
      3  * Copyright (c) 2013, 2014 Michael Forney
      4  *
      5  * Permission is hereby granted, free of charge, to any person obtaining a copy
      6  * of this software and associated documentation files (the "Software"), to deal
      7  * in the Software without restriction, including without limitation the rights
      8  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
      9  * copies 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 included in
     13  * all copies or substantial portions of the Software.
     14  *
     15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
     18  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
     19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
     20  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
     21  * SOFTWARE.
     22  */
     23 
     24 #include "pixman.h"
     25 #include "wld-private.h"
     26 
     27 #include <string.h>
     28 
     29 #define PIXMAN_COLOR(c)                              \
     30 	{                                            \
     31 		.alpha = ((c >> 24) & 0xff) * 0x101, \
     32 		.red = ((c >> 16) & 0xff) * 0x101,   \
     33 		.green = ((c >> 8) & 0xff) * 0x101,  \
     34 		.blue = ((c >> 0) & 0xff) * 0x101,   \
     35 	}
     36 
     37 struct pixman_renderer {
     38 	struct wld_renderer base;
     39 	pixman_image_t *target;
     40 	uint32_t target_width, target_height;
     41 	pixman_glyph_cache_t *glyph_cache;
     42 };
     43 
     44 struct pixman_buffer {
     45 	struct buffer base;
     46 	pixman_image_t *image;
     47 };
     48 
     49 struct pixman_map {
     50 	struct wld_exporter exporter;
     51 	struct wld_destructor destructor;
     52 	pixman_image_t *image;
     53 };
     54 
     55 #include "interface/context.h"
     56 #define RENDERER_IMPLEMENTS_REGION
     57 #define RENDERER_IMPLEMENTS_CLIP
     58 #include "interface/buffer.h"
     59 #include "interface/renderer.h"
     60 IMPL(pixman_renderer, wld_renderer)
     61 IMPL(pixman_buffer, wld_buffer)
     62 
     63 static struct wld_context context = { .impl = &wld_context_impl };
     64 
     65 EXPORT
     66 struct wld_context *wld_pixman_context = &context;
     67 
     68 struct wld_renderer *
     69 context_create_renderer(struct wld_context *context)
     70 {
     71 	struct pixman_renderer *renderer;
     72 
     73 	if (!(renderer = malloc(sizeof *renderer)))
     74 		goto error0;
     75 
     76 	if (!(renderer->glyph_cache = pixman_glyph_cache_create()))
     77 		goto error1;
     78 
     79 	renderer_initialize(&renderer->base, &wld_renderer_impl);
     80 	renderer->target = NULL;
     81 
     82 	return &renderer->base;
     83 
     84 error1:
     85 	free(renderer);
     86 error0:
     87 	return NULL;
     88 }
     89 
     90 static struct buffer *
     91 new_buffer(pixman_image_t *image)
     92 {
     93 	struct pixman_buffer *buffer;
     94 
     95 	if (!(buffer = malloc(sizeof *buffer)))
     96 		return NULL;
     97 
     98 	buffer_initialize(&buffer->base, &wld_buffer_impl,
     99 	                  pixman_image_get_width(image),
    100 	                  pixman_image_get_height(image),
    101 	                  format_pixman_to_wld(pixman_image_get_format(image)),
    102 	                  pixman_image_get_stride(image));
    103 	buffer->base.base.map = pixman_image_get_data(image);
    104 	buffer->image = image;
    105 
    106 	return &buffer->base;
    107 }
    108 
    109 struct buffer *
    110 context_create_buffer(struct wld_context *context,
    111                       uint32_t width, uint32_t height,
    112                       uint32_t format, uint32_t flags)
    113 {
    114 	struct buffer *buffer;
    115 	pixman_image_t *image;
    116 
    117 	image = pixman_image_create_bits(format_wld_to_pixman(format),
    118 	                                 width, height, NULL, 0);
    119 
    120 	if (!image)
    121 		goto error0;
    122 
    123 	if (!(buffer = new_buffer(image)))
    124 		goto error1;
    125 
    126 	return buffer;
    127 
    128 error1:
    129 	pixman_image_unref(image);
    130 error0:
    131 	return NULL;
    132 }
    133 
    134 struct buffer *
    135 context_import_buffer(struct wld_context *context,
    136                       uint32_t type, union wld_object object,
    137                       uint32_t width, uint32_t height,
    138                       uint32_t format, uint32_t pitch)
    139 {
    140 	struct buffer *buffer;
    141 	pixman_image_t *image;
    142 
    143 	switch (type) {
    144 	case WLD_OBJECT_DATA:
    145 		image = pixman_image_create_bits(format_wld_to_pixman(format),
    146 		                                 width, height, object.ptr, pitch);
    147 		break;
    148 	default:
    149 		image = NULL;
    150 	}
    151 
    152 	if (!image)
    153 		goto error0;
    154 
    155 	if (!(buffer = new_buffer(image)))
    156 		goto error1;
    157 
    158 	return buffer;
    159 
    160 error1:
    161 	pixman_image_unref(image);
    162 error0:
    163 	return NULL;
    164 }
    165 
    166 void
    167 context_destroy(struct wld_context *context)
    168 {
    169 }
    170 
    171 uint32_t
    172 renderer_capabilities(struct wld_renderer *renderer, struct buffer *buffer)
    173 {
    174 	/* The pixman renderer can read and write to any buffer using it's map
    175 	 * implementation. */
    176 	return WLD_CAPABILITY_READ | WLD_CAPABILITY_WRITE;
    177 }
    178 
    179 static void
    180 destroy_image(pixman_image_t *image, void *data)
    181 {
    182 	struct buffer *buffer = data;
    183 
    184 	wld_unmap(&buffer->base);
    185 }
    186 
    187 bool
    188 map_export(struct wld_exporter *exporter, struct wld_buffer *buffer,
    189            uint32_t type, union wld_object *object)
    190 {
    191 	struct pixman_map *map = CONTAINER_OF(exporter, struct pixman_map, exporter);
    192 
    193 	switch (type) {
    194 	case WLD_PIXMAN_OBJECT_IMAGE:
    195 		object->ptr = pixman_image_ref(map->image);
    196 		return true;
    197 	default:
    198 		return false;
    199 	}
    200 }
    201 
    202 void
    203 map_destroy(struct wld_destructor *destructor)
    204 {
    205 	struct pixman_map *map = CONTAINER_OF(destructor, struct pixman_map, destructor);
    206 
    207 	pixman_image_unref(map->image);
    208 	free(map);
    209 }
    210 
    211 static pixman_image_t *
    212 pixman_image(struct buffer *buffer)
    213 {
    214 	if (buffer->base.impl == &wld_buffer_impl)
    215 		return pixman_image_ref(pixman_buffer(&buffer->base)->image);
    216 
    217 	union wld_object object;
    218 
    219 	if (wld_export(&buffer->base, WLD_PIXMAN_OBJECT_IMAGE, &object))
    220 		return object.ptr;
    221 
    222 	struct pixman_map *map;
    223 	pixman_image_t *image;
    224 
    225 	if (!wld_map(&buffer->base))
    226 		goto error0;
    227 
    228 	image = pixman_image_create_bits(format_wld_to_pixman(buffer->base.format),
    229 	                                 buffer->base.width, buffer->base.height,
    230 	                                 buffer->base.map, buffer->base.pitch);
    231 
    232 	if (!image)
    233 		goto error1;
    234 
    235 	if (!(map = malloc(sizeof *map)))
    236 		goto error2;
    237 
    238 	map->image = image;
    239 	map->exporter.export = &map_export;
    240 	wld_buffer_add_exporter(&buffer->base, &map->exporter);
    241 	map->destructor.destroy = &map_destroy;
    242 	wld_buffer_add_destructor(&buffer->base, &map->destructor);
    243 	pixman_image_set_destroy_function(image, &destroy_image, buffer);
    244 
    245 	return pixman_image_ref(image);
    246 
    247 error2:
    248 	pixman_image_unref(image);
    249 error1:
    250 	wld_unmap(&buffer->base);
    251 error0:
    252 	return NULL;
    253 }
    254 
    255 bool
    256 renderer_set_target(struct wld_renderer *base, struct buffer *buffer)
    257 {
    258 	struct pixman_renderer *renderer = pixman_renderer(base);
    259 
    260 	if (renderer->target)
    261 		pixman_image_unref(renderer->target);
    262 
    263 	if (buffer) {
    264 		if (!(renderer->target = pixman_image(buffer)))
    265 			return false;
    266 
    267 		/* solid fills only clip against an explicit region */
    268 		renderer->target_width = buffer->base.width;
    269 		renderer->target_height = buffer->base.height;
    270 		renderer_set_clip(base, 0, 0, buffer->base.width, buffer->base.height);
    271 		return true;
    272 	}
    273 
    274 	renderer->target = NULL;
    275 	return true;
    276 }
    277 
    278 void
    279 renderer_set_clip(struct wld_renderer *base, int32_t x, int32_t y,
    280                   uint32_t width, uint32_t height)
    281 {
    282 	struct pixman_renderer *renderer = pixman_renderer(base);
    283 	pixman_region32_t clip, bounds;
    284 
    285 	if (!renderer->target)
    286 		return;
    287 	pixman_region32_init_rect(&bounds, 0, 0, renderer->target_width,
    288 	                          renderer->target_height);
    289 	pixman_region32_init_rect(&clip, x, y, width, height);
    290 	pixman_region32_intersect(&clip, &clip, &bounds);
    291 	pixman_image_set_clip_region32(renderer->target, &clip);
    292 	pixman_region32_fini(&clip);
    293 	pixman_region32_fini(&bounds);
    294 }
    295 
    296 void
    297 renderer_fill_rectangle(struct wld_renderer *base, uint32_t color,
    298                         int32_t x, int32_t y,
    299                         uint32_t width, uint32_t height)
    300 {
    301 	struct pixman_renderer *renderer = pixman_renderer(base);
    302 	pixman_color_t pixman_color = PIXMAN_COLOR(color);
    303 	pixman_box32_t box = { x, y, x + width, y + height };
    304 
    305 	pixman_image_fill_boxes(PIXMAN_OP_SRC, renderer->target,
    306 	                        &pixman_color, 1, &box);
    307 }
    308 
    309 void
    310 renderer_fill_region(struct wld_renderer *base, uint32_t color,
    311                      pixman_region32_t *region)
    312 {
    313 	struct pixman_renderer *renderer = pixman_renderer(base);
    314 	pixman_color_t pixman_color = PIXMAN_COLOR(color);
    315 	pixman_box32_t *boxes;
    316 	int num_boxes;
    317 
    318 	boxes = pixman_region32_rectangles(region, &num_boxes);
    319 	pixman_image_fill_boxes(PIXMAN_OP_SRC, renderer->target,
    320 	                        &pixman_color, num_boxes, boxes);
    321 }
    322 
    323 void
    324 renderer_copy_rectangle(struct wld_renderer *base, struct buffer *buffer,
    325                         int32_t dst_x, int32_t dst_y,
    326                         int32_t src_x, int32_t src_y,
    327                         uint32_t width, uint32_t height)
    328 {
    329 	struct pixman_renderer *renderer = pixman_renderer(base);
    330 	pixman_image_t *src = pixman_image(buffer), *dst = renderer->target;
    331 
    332 	if (!src)
    333 		return;
    334 
    335 	pixman_image_composite32(PIXMAN_OP_SRC, src, NULL, dst,
    336 	                         src_x, src_y, 0, 0, dst_x, dst_y, width, height);
    337 	pixman_image_unref(src);
    338 }
    339 
    340 void
    341 renderer_copy_region(struct wld_renderer *base, struct buffer *buffer,
    342                      int32_t dst_x, int32_t dst_y,
    343                      pixman_region32_t *region)
    344 {
    345 	struct pixman_renderer *renderer = pixman_renderer(base);
    346 	pixman_image_t *src = pixman_image(buffer), *dst = renderer->target;
    347 	pixman_region32_t clip;
    348 
    349 	if (!src)
    350 		return;
    351 
    352 	pixman_region32_init(&clip);
    353 	pixman_region32_copy(&clip, region);
    354 	pixman_region32_translate(&clip, dst_x, dst_y);
    355 
    356 	pixman_image_set_clip_region32(dst, &clip);
    357 	pixman_image_composite32(PIXMAN_OP_SRC, src, NULL, dst,
    358 	                         region->extents.x1, region->extents.y1, 0, 0,
    359 	                         region->extents.x1 + dst_x,
    360 	                         region->extents.y1 + dst_y,
    361 	                         region->extents.x2 - region->extents.x1,
    362 	                         region->extents.y2 - region->extents.y1);
    363 	pixman_image_unref(src);
    364 	pixman_image_set_clip_region32(dst, NULL);
    365 
    366 	pixman_region32_fini(&clip);
    367 }
    368 
    369 static inline uint8_t
    370 reverse(uint8_t byte)
    371 {
    372 	byte = ((byte << 1) & 0xaa) | ((byte >> 1) & 0x55);
    373 	byte = ((byte << 2) & 0xcc) | ((byte >> 2) & 0x33);
    374 	byte = ((byte << 4) & 0xf0) | ((byte >> 4) & 0x0f);
    375 
    376 	return byte;
    377 }
    378 
    379 static pixman_image_t *
    380 glyph_bitmap_to_pixman_image(struct glyph *glyph)
    381 {
    382 	FT_Bitmap *bitmap = &glyph->bitmap;
    383 	pixman_image_t *image;
    384 	uint8_t *src, *dst;
    385 	uint32_t row, byte_index, bytes_per_row, pitch;
    386 
    387 	switch (bitmap->pixel_mode) {
    388 	case FT_PIXEL_MODE_MONO:
    389 		image = pixman_image_create_bits(PIXMAN_a1, bitmap->width,
    390 		                                 bitmap->rows, NULL, bitmap->pitch);
    391 		if (!image)
    392 			return NULL;
    393 
    394 		pitch = pixman_image_get_stride(image);
    395 		bytes_per_row = (bitmap->width + 7) / 8;
    396 		src = bitmap->buffer;
    397 		dst = (uint8_t *)pixman_image_get_data(image);
    398 
    399 		for (row = 0; row < bitmap->rows; ++row) {
    400 			/* Pixman's A1 format expects the bits in the opposite order
    401 			 * that Freetype gives us. */
    402 			for (byte_index = 0; byte_index < bytes_per_row; ++byte_index)
    403 				dst[byte_index] = reverse(src[byte_index]);
    404 
    405 			dst += pitch;
    406 			src += bitmap->pitch;
    407 		}
    408 		return image;
    409 	case FT_PIXEL_MODE_GRAY:
    410 		image = pixman_image_create_bits(PIXMAN_a8, bitmap->width,
    411 		                                 bitmap->rows, NULL, 0);
    412 		if (!image)
    413 			return NULL;
    414 
    415 		pitch = pixman_image_get_stride(image);
    416 		src = bitmap->buffer;
    417 		dst = (uint8_t *)pixman_image_get_data(image);
    418 
    419 		for (row = 0; row < bitmap->rows; ++row) {
    420 			memset(dst, 0, pitch);
    421 			memcpy(dst, src, bitmap->width);
    422 			dst += pitch;
    423 			src += bitmap->pitch;
    424 		}
    425 		return image;
    426 	default:
    427 		return NULL;
    428 	}
    429 }
    430 
    431 void
    432 renderer_draw_text(struct wld_renderer *base,
    433                    struct font *font, uint32_t color,
    434                    int32_t x, int32_t y, const char *text,
    435                    uint32_t length, struct wld_extents *extents)
    436 {
    437 	struct pixman_renderer *renderer = pixman_renderer(base);
    438 	int ret;
    439 	uint32_t c;
    440 	struct glyph *glyph;
    441 	FT_UInt glyph_index;
    442 	pixman_glyph_t *glyphs;
    443 	uint32_t index = 0, origin_x = 0;
    444 	pixman_color_t pixman_color = PIXMAN_COLOR(color);
    445 	pixman_image_t *solid;
    446 
    447 	if (length == -1)
    448 		length = strlen(text);
    449 	glyphs = malloc(length * sizeof(glyphs[0]));
    450 	if (!glyphs)
    451 		return;
    452 	solid = pixman_image_create_solid_fill(&pixman_color);
    453 
    454 	while ((ret = wld_utf8_decode(text, length, &c)) > 0 && c != '\0') {
    455 		text += ret;
    456 		length -= ret;
    457 		glyph_index = font_char_index(font, c);
    458 
    459 		if (!font_ensure_glyph(font, glyph_index))
    460 			continue;
    461 
    462 		glyph = font->glyphs[glyph_index];
    463 
    464 		glyphs[index].x = origin_x;
    465 		glyphs[index].y = 0;
    466 		glyphs[index].glyph = pixman_glyph_cache_lookup(renderer->glyph_cache,
    467 		                                                font, glyph);
    468 
    469 		/* If we don't have the glyph in our cache, do some conversions to make
    470 		 * pixman happy, and then insert it. */
    471 		if (!glyphs[index].glyph) {
    472 			pixman_image_t *image;
    473 			image = glyph_bitmap_to_pixman_image(glyph);
    474 
    475 			if (!image)
    476 				goto advance;
    477 
    478 			/* Insert the glyph into the cache. */
    479 			pixman_glyph_cache_freeze(renderer->glyph_cache);
    480 			glyphs[index].glyph = pixman_glyph_cache_insert(renderer->glyph_cache, font, glyph,
    481 			                                                -glyph->x, -glyph->y, image);
    482 			pixman_glyph_cache_thaw(renderer->glyph_cache);
    483 
    484 			/* The glyph cache copies the contents of the glyph bitmap. */
    485 			pixman_image_unref(image);
    486 		}
    487 
    488 		++index;
    489 
    490 	advance:
    491 		origin_x += glyph->advance;
    492 	}
    493 
    494 	pixman_composite_glyphs_no_mask(PIXMAN_OP_OVER, solid, renderer->target,
    495 	                                0, 0, x, y, renderer->glyph_cache,
    496 	                                index, glyphs);
    497 
    498 	free(glyphs);
    499 	pixman_image_unref(solid);
    500 
    501 	if (extents)
    502 		extents->advance = origin_x;
    503 }
    504 
    505 void
    506 renderer_flush(struct wld_renderer *renderer)
    507 {
    508 }
    509 
    510 void
    511 renderer_destroy(struct wld_renderer *base)
    512 {
    513 	struct pixman_renderer *renderer = pixman_renderer(base);
    514 
    515 	pixman_glyph_cache_destroy(renderer->glyph_cache);
    516 	free(renderer);
    517 }
    518 
    519 bool
    520 buffer_map(struct buffer *buffer)
    521 {
    522 	return true;
    523 }
    524 
    525 bool
    526 buffer_unmap(struct buffer *buffer)
    527 {
    528 	return true;
    529 }
    530 
    531 void
    532 buffer_destroy(struct buffer *base)
    533 {
    534 	struct pixman_buffer *buffer = pixman_buffer(&base->base);
    535 
    536 	pixman_image_unref(buffer->image);
    537 	free(buffer);
    538 }