main wld-private.h
  1/* wld: wld-private.h
  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#ifndef WLD_PRIVATE_H
 25#define WLD_PRIVATE_H
 26
 27#include "wld.h"
 28
 29#include <assert.h>
 30#include <ft2build.h>
 31#include <stdbool.h>
 32#include <stdint.h>
 33#include FT_FREETYPE_H
 34#include FT_BITMAP_H
 35
 36#define ARRAY_LENGTH(array) (sizeof(array) / sizeof(array)[0])
 37#if ENABLE_DEBUG
 38#define DEBUG(format, ...) \
 39	fprintf(stderr, "# %s: " format, __func__, ##__VA_ARGS__)
 40#else
 41#define DEBUG(format, ...)
 42#endif
 43
 44#define EXPORT __attribute__((visibility("default")))
 45#define CONTAINER_OF(ptr, type, member) \
 46	((type *)((uintptr_t)ptr - offsetof(type, member)))
 47#define IMPL(impl_type, base_type)                                          \
 48	static inline struct impl_type *impl_type(struct base_type *object) \
 49	{                                                                   \
 50		assert(object->impl == &base_type##_impl);                  \
 51		return (struct impl_type *)object;                          \
 52	}
 53
 54struct wld_font_context {
 55	FT_Library library;
 56};
 57
 58struct glyph {
 59	FT_Bitmap bitmap;
 60
 61	/**
 62	 * The offset from the origin to the top left corner of the bitmap.
 63	 */
 64	int16_t x, y;
 65
 66	/**
 67	 * The width to advance to the origin of the next character.
 68	 */
 69	uint16_t advance;
 70};
 71
 72struct font {
 73	struct wld_font base;
 74
 75	struct wld_font_context *context;
 76	FT_Face face;
 77	struct glyph **glyphs;
 78};
 79
 80struct wld_context_impl {
 81	struct wld_renderer *(*create_renderer)(struct wld_context *context);
 82	struct buffer *(*create_buffer)(struct wld_context *context,
 83	                                uint32_t width, uint32_t height,
 84	                                uint32_t format, uint32_t flags);
 85	struct buffer *(*import_buffer)(struct wld_context *context,
 86	                                uint32_t type, union wld_object object,
 87	                                uint32_t width, uint32_t height,
 88	                                uint32_t format, uint32_t pitch);
 89	struct wld_surface *(*create_surface)(struct wld_context *context,
 90	                                      uint32_t width, uint32_t height,
 91	                                      uint32_t format, uint32_t flags);
 92	void (*destroy)(struct wld_context *context);
 93};
 94
 95struct wld_renderer_impl {
 96	uint32_t (*capabilities)(struct wld_renderer *renderer,
 97	                         struct buffer *buffer);
 98	bool (*set_target)(struct wld_renderer *renderer, struct buffer *buffer);
 99	void (*fill_rectangle)(struct wld_renderer *renderer,
100	                       uint32_t color, int32_t x, int32_t y,
101	                       uint32_t width, uint32_t height);
102	void (*fill_region)(struct wld_renderer *renderer,
103	                    uint32_t color, pixman_region32_t *region);
104	void (*copy_rectangle)(struct wld_renderer *renderer, struct buffer *src,
105	                       int32_t dst_x, int32_t dst_y,
106	                       int32_t src_x, int32_t src_y,
107	                       uint32_t width, uint32_t height);
108	void (*copy_region)(struct wld_renderer *renderer, struct buffer *src,
109	                    int32_t dst_x, int32_t dst_y,
110	                    pixman_region32_t *region);
111	void (*draw_circle)(struct wld_renderer *renderer, uint32_t color,
112				int32_t x, int32_t y, uint32_t r, bool fill);
113	void (*draw_line)(struct wld_renderer *renderer, uint32_t color,
114			 int32_t x1, int32_t y1, int32_t x2, int32_t y2);
115	void (*draw_text)(struct wld_renderer *renderer,
116	                  struct font *font, uint32_t color,
117	                  int32_t x, int32_t y, const char *text, uint32_t length,
118	                  struct wld_extents *extents);
119	void (*flush)(struct wld_renderer *renderer);
120	void (*destroy)(struct wld_renderer *renderer);
121};
122
123struct buffer {
124	struct wld_buffer base;
125
126	unsigned ref, map_ref;
127	struct wld_exporter *exporters;
128	struct wld_destructor *destructors;
129};
130
131struct wld_buffer_impl {
132	bool (*map)(struct buffer *buffer);
133	bool (*unmap)(struct buffer *buffer);
134	void (*flush)(struct buffer *buffer);
135	void (*destroy)(struct buffer *buffer);
136};
137
138struct wld_surface_impl {
139	pixman_region32_t *(*damage)(struct wld_surface *surface,
140	                             pixman_region32_t *damage);
141	struct buffer *(*back)(struct wld_surface *surface);
142	struct buffer *(*take)(struct wld_surface *surface);
143	bool (*release)(struct wld_surface *surface, struct buffer *buffer);
144	bool (*swap)(struct wld_surface *surface);
145	void (*destroy)(struct wld_surface *surface);
146};
147
148struct buffer_socket {
149	const struct buffer_socket_impl *impl;
150};
151
152struct buffer_socket_impl {
153	bool (*attach)(struct buffer_socket *socket, struct buffer *buffer);
154	void (*process)(struct buffer_socket *socket);
155	void (*destroy)(struct buffer_socket *socket);
156};
157
158bool font_ensure_glyph(struct font *font, FT_UInt glyph_index);
159
160/**
161 * Returns the number of bytes per pixel for the given format.
162 */
163static inline uint8_t
164format_bytes_per_pixel(enum wld_format format)
165{
166	switch (format) {
167	case WLD_FORMAT_ARGB8888:
168	case WLD_FORMAT_XRGB8888:
169		return 4;
170	default:
171		return 0;
172	}
173}
174
175static inline pixman_format_code_t
176format_wld_to_pixman(uint32_t format)
177{
178	switch (format) {
179	case WLD_FORMAT_ARGB8888:
180		return PIXMAN_a8r8g8b8;
181	case WLD_FORMAT_XRGB8888:
182		return PIXMAN_x8r8g8b8;
183	default:
184		return 0;
185	}
186}
187
188static inline uint32_t
189format_pixman_to_wld(pixman_format_code_t format)
190{
191	switch (format) {
192	case PIXMAN_a8r8g8b8:
193		return WLD_FORMAT_ARGB8888;
194	case PIXMAN_x8r8g8b8:
195		return WLD_FORMAT_XRGB8888;
196	default:
197		return 0;
198	}
199}
200
201/**
202 * This default fill_region method is implemented in terms of fill_rectangle.
203 */
204void default_fill_region(struct wld_renderer *renderer, uint32_t color,
205                         pixman_region32_t *region);
206
207/**
208 * This default copy_region method is implemented in terms of copy_rectangle.
209 */
210void default_copy_region(struct wld_renderer *renderer, struct buffer *buffer,
211                         int32_t dst_x, int32_t dst_y,
212                         pixman_region32_t *region);
213
214struct wld_surface *default_create_surface(struct wld_context *context,
215                                           uint32_t width, uint32_t height,
216                                           uint32_t format, uint32_t flags);
217
218struct wld_surface *buffered_surface_create(struct wld_context *context,
219                                            uint32_t width, uint32_t height,
220                                            uint32_t format, uint32_t flags,
221                                            struct buffer_socket *socket);
222
223void context_initialize(struct wld_context *context,
224                        const struct wld_context_impl *impl);
225
226void renderer_initialize(struct wld_renderer *renderer,
227                         const struct wld_renderer_impl *impl);
228
229void buffer_initialize(struct buffer *buffer,
230                       const struct wld_buffer_impl *impl,
231                       uint32_t width, uint32_t height,
232                       uint32_t format, uint32_t pitch);
233
234void surface_initialize(struct wld_surface *surface,
235                        const struct wld_surface_impl *impl);
236
237#endif