commit 4ee9dac535a3093227199e52853a63826bc62ab3
parent 1c0784bb39b3c66b0a69bd341050874348e85b2f
Author: Michael Forney <mforney@mforney.org>
Date: Mon, 13 Jan 2014 17:50:47 -0800
Separate actual rendering to renderer
Diffstat:
| M | Makefile | | | 7 | ++++++- |
| M | context.c | | | 6 | ++++++ |
| M | drawable.c | | | 91 | ++----------------------------------------------------------------------------- |
| M | dumb.c | | | 19 | ++++++++----------- |
| M | intel.c | | | 137 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++------------------------ |
| M | interface/context.h | | | 3 | +++ |
| M | interface/drawable.h | | | 33 | --------------------------------- |
| A | interface/renderer.h | | | 68 | ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| M | pixman.c | | | 161 | +++++++++++++++++++++++++++++++++++++++++++++++++------------------------------ |
| M | pixman.h | | | 11 | +++++------ |
| A | renderer.c | | | 145 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| M | wayland-drm.c | | | 7 | +++++++ |
| M | wayland-shm.c | | | 12 | ++++++------ |
| M | wld-private.h | | | 49 | ++++++++++++++++++++++++++++++++----------------- |
| M | wld.h | | | 54 | ++++++++++++++++++++++++++++++++++++++++-------------- |
15 files changed, 525 insertions(+), 278 deletions(-)
diff --git a/Makefile b/Makefile
@@ -15,7 +15,12 @@ CLEAN_FILES := $(TARGETS)
WLD_REQUIRES = fontconfig pixman-1
WLD_REQUIRES_PRIVATE = freetype2
-WLD_SOURCES = context.c drawable.c color.c font.c
+WLD_SOURCES = \
+ color.c \
+ context.c \
+ drawable.c \
+ font.c \
+ renderer.c
WLD_HEADERS = wld.h
ifeq ($(ENABLE_DEBUG),1)
diff --git a/context.c b/context.c
@@ -30,6 +30,12 @@ void context_initialize(struct wld_context * context,
}
EXPORT
+struct wld_renderer * wld_create_renderer(struct wld_context * context)
+{
+ return context->impl->create_renderer(context);
+}
+
+EXPORT
struct wld_drawable * wld_create_drawable(struct wld_context * context,
uint32_t width, uint32_t height,
uint32_t format)
diff --git a/drawable.c b/drawable.c
@@ -25,41 +25,6 @@
#include <assert.h>
-void default_fill_region(struct wld_drawable * drawable, uint32_t color,
- pixman_region32_t * region)
-{
- pixman_box32_t * box;
- int num_boxes;
-
- box = pixman_region32_rectangles(region, &num_boxes);
-
- while (num_boxes--)
- {
- drawable->impl->fill_rectangle(drawable, color, box->x1, box->y1,
- box->x2 - box->x1,
- box->y2 - box->y1);
- ++box;
- }
-}
-
-void default_copy_region(struct wld_drawable * src, struct wld_drawable * dst,
- pixman_region32_t * region,
- int32_t dst_x, int32_t dst_y)
-{
- pixman_box32_t * box;
- int num_boxes;
-
- box = pixman_region32_rectangles(region, &num_boxes);
-
- while (num_boxes--)
- {
- dst->impl->copy_rectangle(src, dst, box->x1, box->y1,
- dst_x + box->x1, dst_y + box->y1,
- box->x2 - box->x1, box->y2 - box->y1);
- ++box;
- }
-}
-
void drawable_initialize(struct wld_drawable * drawable,
const struct wld_drawable_impl * impl,
uint32_t width, uint32_t height,
@@ -88,58 +53,6 @@ void exporter_initialize(struct wld_exporter * exporter,
}
EXPORT
-void wld_fill_rectangle(struct wld_drawable * drawable, uint32_t color,
- int32_t x, int32_t y, uint32_t width, uint32_t height)
-{
- drawable->impl->fill_rectangle(drawable, color, x, y, width, height);
-}
-
-EXPORT
-void wld_fill_region(struct wld_drawable * drawable, uint32_t color,
- pixman_region32_t * region)
-{
- drawable->impl->fill_region(drawable, color, region);
-}
-
-EXPORT
-void wld_copy_rectangle(struct wld_drawable * src, struct wld_drawable * dst,
- int32_t src_x, int32_t src_y,
- int32_t dst_x, int32_t dst_y,
- uint32_t width, uint32_t height)
-{
- assert(src->impl == dst->impl);
- dst->impl->copy_rectangle(src, dst, src_x, src_y, dst_x, dst_y,
- width, height);
-}
-
-EXPORT
-void wld_copy_region(struct wld_drawable * src, struct wld_drawable * dst,
- pixman_region32_t * region, int32_t dst_x, int32_t dst_y)
-{
- assert(src->impl == dst->impl);
- dst->impl->copy_region(src, dst, region, dst_x, dst_y);
-}
-
-EXPORT
-void wld_draw_text_n(struct wld_drawable * drawable,
- struct wld_font * font_base, uint32_t color,
- int32_t x, int32_t y,
- const char * text, int32_t length,
- struct wld_extents * extents)
-{
- struct font * font = (void *) font_base;
-
- drawable->impl->draw_text(drawable, font, color, x, y,
- text, length, extents);
-}
-
-EXPORT
-void wld_destroy_drawable(struct wld_drawable * drawable)
-{
- drawable->impl->destroy(drawable);
-}
-
-EXPORT
void wld_write(struct wld_drawable * drawable, const void * data, size_t size)
{
drawable->impl->write(drawable, data, size);
@@ -167,8 +80,8 @@ bool wld_export(struct wld_drawable * drawable,
}
EXPORT
-void wld_flush(struct wld_drawable * drawable)
+void wld_destroy_drawable(struct wld_drawable * drawable)
{
- drawable->impl->flush(drawable);
+ drawable->impl->destroy(drawable);
}
diff --git a/dumb.c b/dumb.c
@@ -27,6 +27,7 @@
#include "drm.h"
#include <fcntl.h>
+#include <unistd.h>
#include <sys/mman.h>
#include <xf86drm.h>
@@ -35,7 +36,6 @@
struct dumb_context
{
struct wld_context base;
- struct wld_context * pixman;
int fd;
};
@@ -65,20 +65,17 @@ struct wld_context * driver_create_context(int drm_fd)
struct dumb_context * context;
if (!(context = malloc(sizeof *context)))
- goto error0;
-
- if (!(context->pixman = wld_pixman_create_context()))
- goto error1;
+ return NULL;
context_initialize(&context->base, &context_impl);
context->fd = drm_fd;
return &context->base;
+}
- error1:
- free(context);
- error0:
- return NULL;
+struct wld_renderer * context_create_renderer(struct wld_context * context)
+{
+ return wld_create_renderer(wld_pixman_context);
}
static struct wld_drawable * new_drawable(struct dumb_context * context,
@@ -106,7 +103,7 @@ static struct wld_drawable * new_drawable(struct dumb_context * context,
if (data == MAP_FAILED)
goto error1;
- if (!pixman_initialize_drawable(context->pixman, &drawable->pixman,
+ if (!pixman_initialize_drawable(wld_pixman_context, &drawable->pixman,
width, height, data, pitch, format))
{
goto error2;
@@ -198,7 +195,7 @@ void context_destroy(struct wld_context * base)
{
struct dumb_context * context = dumb_context(base);
- wld_destroy_context(context->pixman);
+ close(context->fd);
free(context);
}
diff --git a/intel.c b/intel.c
@@ -34,25 +34,31 @@ struct intel_context
{
struct wld_context base;
drm_intel_bufmgr * bufmgr;
+};
+
+struct intel_renderer
+{
+ struct wld_renderer base;
struct intel_batch * batch;
+ struct intel_drawable * target;
};
struct intel_drawable
{
struct wld_drawable base;
struct wld_exporter exporter;
-
- struct intel_context * context;
drm_intel_bo * bo;
pixman_image_t * virtual;
};
#include "interface/context.h"
+#include "interface/renderer.h"
#include "interface/drawable.h"
#include "interface/exporter.h"
#define DRM_DRIVER_NAME intel
#include "interface/drm.h"
IMPL(intel, context)
+IMPL(intel, renderer)
IMPL(intel, drawable)
bool driver_device_supported(uint32_t vendor_id, uint32_t device_id)
@@ -75,23 +81,37 @@ struct wld_context * driver_create_context(int drm_fd)
if (!context->bufmgr)
goto error1;
- context->batch = intel_batch_new(context->bufmgr);
-
- if (!context->batch)
- goto error2;
-
return &context->base;
- error2:
- drm_intel_bufmgr_destroy(context->bufmgr);
error1:
free(context);
error0:
return NULL;
}
-static struct intel_drawable * new_drawable(struct intel_context * context,
- uint32_t width, uint32_t height,
+/**** Context ****/
+struct wld_renderer * context_create_renderer(struct wld_context * base)
+{
+ struct intel_context * context = intel_context(base);
+ struct intel_renderer * renderer;
+
+ if (!(renderer = malloc(sizeof *renderer)))
+ goto error0;
+
+ if (!(renderer->batch = intel_batch_new(context->bufmgr)))
+ goto error1;
+
+ renderer_initialize(&renderer->base, &renderer_impl);
+
+ return &renderer->base;
+
+ error1:
+ free(renderer);
+ error0:
+ return NULL;
+}
+
+static struct intel_drawable * new_drawable(uint32_t width, uint32_t height,
uint32_t format)
{
struct intel_drawable * intel;
@@ -101,7 +121,6 @@ static struct intel_drawable * new_drawable(struct intel_context * context,
drawable_initialize(&intel->base, &drawable_impl,
width, height, format, 0);
- intel->context = context;
intel->virtual = NULL;
exporter_initialize(&intel->exporter, &exporter_impl);
drawable_add_exporter(&intel->base, &intel->exporter);
@@ -117,7 +136,7 @@ struct wld_drawable * context_create_drawable(struct wld_context * base,
struct intel_drawable * intel;
uint32_t tiling_mode = width >= 128 ? I915_TILING_X : I915_TILING_NONE;
- if (!(intel = new_drawable(context, width, height, format)))
+ if (!(intel = new_drawable(width, height, format)))
return NULL;
intel->bo = drm_intel_bo_alloc_tiled(context->bufmgr, "drawable",
@@ -155,7 +174,7 @@ struct wld_drawable * context_import(struct wld_context * base,
if (!bo)
goto error0;
- if (!(intel = new_drawable(context, width, height, format)))
+ if (!(intel = new_drawable(width, height, format)))
goto error1;
intel->bo = bo;
@@ -173,41 +192,69 @@ void context_destroy(struct wld_context * base)
{
struct intel_context * context = intel_context(base);
- intel_batch_destroy(context->batch);
drm_intel_bufmgr_destroy(context->bufmgr);
free(context);
}
-void drawable_fill_rectangle(struct wld_drawable * drawable, uint32_t color,
+/**** Renderer ****/
+uint32_t renderer_capabilities(struct wld_renderer * renderer,
+ struct wld_drawable * drawable)
+{
+ if (drawable->impl == &drawable_impl)
+ return WLD_CAPABILITY_READ | WLD_CAPABILITY_WRITE;
+
+ return 0;
+}
+
+bool renderer_set_target(struct wld_renderer * base,
+ struct wld_drawable * drawable)
+{
+ struct intel_renderer * renderer = intel_renderer(base);
+
+ if (drawable && drawable->impl != &drawable_impl)
+ return false;
+
+ renderer->target = drawable ? intel_drawable(drawable) : NULL;
+
+ return true;
+}
+
+void renderer_fill_rectangle(struct wld_renderer * base, uint32_t color,
int32_t x, int32_t y,
uint32_t width, uint32_t height)
{
- struct intel_drawable * intel = (void *) drawable;
+ struct intel_renderer * renderer = intel_renderer(base);
- xy_color_blt(intel->context->batch, intel->bo, intel->base.pitch,
+ xy_color_blt(renderer->batch, renderer->target->bo,
+ renderer->target->base.pitch,
x, y, x + width, y + height, color);
}
-void drawable_copy_rectangle(struct wld_drawable * src_drawable,
- struct wld_drawable * dst_drawable,
- int32_t src_x, int32_t src_y,
+void renderer_copy_rectangle(struct wld_renderer * base,
+ struct wld_drawable * drawable_base,
int32_t dst_x, int32_t dst_y,
+ int32_t src_x, int32_t src_y,
uint32_t width, uint32_t height)
{
- struct intel_drawable * src = (void *) src_drawable;
- struct intel_drawable * dst = (void *) dst_drawable;
+ struct intel_renderer * renderer = intel_renderer(base);
+
+ if (drawable_base->impl != &drawable_impl)
+ return;
- xy_src_copy_blt(dst->context->batch,
- src->bo, src->base.pitch, src_x, src_y,
- dst->bo, dst->base.pitch, dst_x, dst_y, width, height);
+ struct intel_drawable * drawable = intel_drawable(drawable_base);
+
+ xy_src_copy_blt(renderer->batch, drawable->bo, drawable->base.pitch,
+ src_x, src_y,
+ renderer->target->bo, renderer->target->base.pitch,
+ dst_x, dst_y, width, height);
}
-void drawable_draw_text(struct wld_drawable * drawable,
+void renderer_draw_text(struct wld_renderer * base,
struct font * font, uint32_t color,
int32_t x, int32_t y, const char * text, int32_t length,
struct wld_extents * extents)
{
- struct intel_drawable * intel = (void *) drawable;
+ struct intel_renderer * renderer = intel_renderer(base);
int ret;
struct glyph * glyph;
uint32_t row;
@@ -217,8 +264,8 @@ void drawable_draw_text(struct wld_drawable * drawable,
uint8_t * byte;
int32_t origin_x = x;
- xy_setup_blt(intel->context->batch, true, INTEL_BLT_RASTER_OPERATION_SRC,
- 0, color, intel->bo, intel->base.pitch);
+ xy_setup_blt(renderer->batch, true, INTEL_BLT_RASTER_OPERATION_SRC,
+ 0, color, renderer->target->bo, renderer->target->base.pitch);
while ((ret = FcUtf8ToUcs4((FcChar8 *) text, &c, length)) > 0 && c != '\0')
{
@@ -245,7 +292,7 @@ void drawable_draw_text(struct wld_drawable * drawable,
}
retry:
- ret = xy_text_immediate_blt(intel->context->batch, intel->bo,
+ ret = xy_text_immediate_blt(renderer->batch, renderer->target->bo,
origin_x + glyph->x, y + glyph->y,
origin_x + glyph->x + glyph->bitmap.width,
y + glyph->y + glyph->bitmap.rows,
@@ -254,10 +301,10 @@ void drawable_draw_text(struct wld_drawable * drawable,
if (ret == INTEL_BATCH_NO_SPACE)
{
- intel_batch_flush(intel->context->batch);
- xy_setup_blt(intel->context->batch, true,
+ intel_batch_flush(renderer->batch);
+ xy_setup_blt(renderer->batch, true,
INTEL_BLT_RASTER_OPERATION_SRC, 0, color,
- intel->bo, intel->base.pitch);
+ renderer->target->bo, renderer->target->base.pitch);
goto retry;
}
@@ -269,6 +316,21 @@ void drawable_draw_text(struct wld_drawable * drawable,
extents->advance = origin_x - x;
}
+void renderer_flush(struct wld_renderer * base)
+{
+ struct intel_renderer * renderer = intel_renderer(base);
+
+ intel_batch_flush(renderer->batch);
+}
+
+void renderer_destroy(struct wld_renderer * base)
+{
+ struct intel_renderer * renderer = intel_renderer(base);
+
+ intel_batch_destroy(renderer->batch);
+ free(renderer);
+}
+
void drawable_write(struct wld_drawable * drawable, const void * data, size_t size)
{
struct intel_drawable * intel = (void *) drawable;
@@ -304,13 +366,6 @@ pixman_image_t * drawable_map(struct wld_drawable * drawable)
return intel->virtual;
}
-void drawable_flush(struct wld_drawable * drawable)
-{
- struct intel_drawable * intel = (void *) drawable;
-
- intel_batch_flush(intel->context->batch);
-}
-
void drawable_destroy(struct wld_drawable * drawable)
{
struct intel_drawable * intel = (void *) drawable;
diff --git a/interface/context.h b/interface/context.h
@@ -21,6 +21,8 @@
* SOFTWARE.
*/
+static struct wld_renderer * context_create_renderer
+ (struct wld_context * context);
static struct wld_drawable * context_create_drawable
(struct wld_context * context,
uint32_t width, uint32_t height, uint32_t format);
@@ -30,6 +32,7 @@ static struct wld_drawable * context_import
static void context_destroy(struct wld_context * context);
static const struct wld_context_impl context_impl = {
+ .create_renderer = &context_create_renderer,
.create_drawable = &context_create_drawable,
.import = &context_import,
.destroy = &context_destroy
diff --git a/interface/drawable.h b/interface/drawable.h
@@ -21,47 +21,14 @@
* SOFTWARE.
*/
-static void drawable_fill_rectangle(struct wld_drawable * drawable,
- uint32_t color, int32_t x, int32_t y,
- uint32_t width, uint32_t height);
-static void drawable_copy_rectangle(struct wld_drawable * src,
- struct wld_drawable * dst,
- int32_t src_x, int32_t src_y,
- int32_t dst_x, int32_t dst_y,
- uint32_t width, uint32_t height);
-#ifdef DRAWABLE_IMPLEMENTS_REGION
-static void drawable_fill_region(struct wld_drawable * drawable, uint32_t color,
- pixman_region32_t * region);
-static void drawable_copy_region(struct wld_drawable * src,
- struct wld_drawable * drawable,
- pixman_region32_t * region,
- int32_t dst_x, int32_t dst_y);
-#endif
-static void drawable_draw_text(struct wld_drawable * drawable,
- struct font * font, uint32_t color,
- int32_t x, int32_t y,
- const char * text, int32_t length,
- struct wld_extents * extents);
static void drawable_write(struct wld_drawable * drawable,
const void * data, size_t size);
static pixman_image_t * drawable_map(struct wld_drawable * drawable);
-static void drawable_flush(struct wld_drawable * drawable);
static void drawable_destroy(struct wld_drawable * drawable);
static const struct wld_drawable_impl drawable_impl = {
- .fill_rectangle = &drawable_fill_rectangle,
- .copy_rectangle = &drawable_copy_rectangle,
-#ifdef DRAWABLE_IMPLEMENTS_REGION
- .fill_region = &drawable_fill_region,
- .copy_region = &drawable_copy_region,
-#else
- .fill_region = &default_fill_region,
- .copy_region = &default_copy_region,
-#endif
- .draw_text = &drawable_draw_text,
.write = &drawable_write,
.map = &drawable_map,
- .flush = &drawable_flush,
.destroy = &drawable_destroy
};
diff --git a/interface/renderer.h b/interface/renderer.h
@@ -0,0 +1,68 @@
+/* wld: interface/drawable.h
+ *
+ * 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.
+ */
+
+static uint32_t renderer_capabilities(struct wld_renderer * renderer,
+ struct wld_drawable * drawable);
+static bool renderer_set_target(struct wld_renderer * renderer,
+ struct wld_drawable * drawable);
+static void renderer_fill_rectangle(struct wld_renderer * renderer,
+ uint32_t color, int32_t x, int32_t y,
+ uint32_t width, uint32_t height);
+static void renderer_copy_rectangle(struct wld_renderer * renderer,
+ struct wld_drawable * drawable,
+ int32_t dst_x, int32_t dst_y,
+ int32_t src_x, int32_t src_y,
+ uint32_t width, uint32_t height);
+#ifdef RENDERER_IMPLEMENTS_REGION
+static void renderer_fill_region(struct wld_renderer * base, uint32_t color,
+ pixman_region32_t * region);
+static void renderer_copy_region(struct wld_renderer * base,
+ struct wld_drawable * drawable,
+ int32_t dst_x, int32_t dst_y,
+ pixman_region32_t * region);
+#endif
+static void renderer_draw_text(struct wld_renderer * renderer,
+ struct font * font, uint32_t color,
+ int32_t x, int32_t y,
+ const char * text, int32_t length,
+ struct wld_extents * extents);
+static void renderer_flush(struct wld_renderer * renderer);
+static void renderer_destroy(struct wld_renderer * renderer);
+
+static const struct wld_renderer_impl renderer_impl = {
+ .capabilities = &renderer_capabilities,
+ .set_target = &renderer_set_target,
+ .fill_rectangle = &renderer_fill_rectangle,
+ .copy_rectangle = &renderer_copy_rectangle,
+#ifdef RENDERER_IMPLEMENTS_REGION
+ .fill_region = &renderer_fill_region,
+ .copy_region = &renderer_copy_region,
+#else
+ .fill_region = &default_fill_region,
+ .copy_region = &default_copy_region,
+#endif
+ .draw_text = &renderer_draw_text,
+ .flush = &renderer_flush,
+ .destroy = &renderer_destroy
+};
+
diff --git a/pixman.c b/pixman.c
@@ -32,33 +32,43 @@
.blue = ((c >> 0) & 0xff) * 0x101, \
}
-struct pixman_context
+struct pixman_renderer
{
- struct wld_context base;
+ struct wld_renderer base;
+ pixman_image_t * target;
pixman_glyph_cache_t * glyph_cache;
};
#include "interface/context.h"
-#define DRAWABLE_IMPLEMENTS_REGION
+#define RENDERER_IMPLEMENTS_REGION
+#include "interface/renderer.h"
#include "interface/drawable.h"
+IMPL(pixman, renderer)
+IMPL(pixman, drawable)
+static struct wld_context context = { .impl = &context_impl };
const struct wld_drawable_impl * const pixman_drawable_impl = &drawable_impl;
EXPORT
-struct wld_context * wld_pixman_create_context()
+struct wld_context * wld_pixman_context = &context;
+
+struct wld_renderer * context_create_renderer(struct wld_context * context)
{
- struct pixman_context * context;
+ struct pixman_renderer * renderer;
- if (!(context = malloc(sizeof *context)))
+ if (!(renderer = malloc(sizeof *renderer)))
goto error0;
- if (!(context->glyph_cache = pixman_glyph_cache_create()))
+ if (!(renderer->glyph_cache = pixman_glyph_cache_create()))
goto error1;
- return &context->base;
+ renderer_initialize(&renderer->base, &renderer_impl);
+ renderer->target = NULL;
+
+ return &renderer->base;
error1:
- free(context);
+ free(renderer);
error0:
return NULL;
}
@@ -78,8 +88,7 @@ bool pixman_initialize_drawable
return drawable->image != NULL;
}
-struct wld_drawable * new_drawable(struct pixman_context * context,
- pixman_image_t * image)
+struct wld_drawable * new_drawable(pixman_image_t * image)
{
struct pixman_drawable * drawable;
@@ -91,17 +100,15 @@ struct wld_drawable * new_drawable(struct pixman_context * context,
pixman_image_get_height(image),
format_pixman_to_wld(pixman_image_get_format(image)),
pixman_image_get_stride(image));
- drawable->context = context;
drawable->image = image;
return &drawable->base;
}
-struct wld_drawable * context_create_drawable(struct wld_context * base,
+struct wld_drawable * context_create_drawable(struct wld_context * context,
uint32_t width, uint32_t height,
uint32_t format)
{
- struct pixman_context * context = (void *) base;
struct wld_drawable * drawable;
pixman_image_t * image;
@@ -111,7 +118,7 @@ struct wld_drawable * context_create_drawable(struct wld_context * base,
if (!image)
goto error0;
- if (!(drawable = new_drawable(context, image)))
+ if (!(drawable = new_drawable(image)))
goto error1;
return drawable;
@@ -122,11 +129,11 @@ struct wld_drawable * context_create_drawable(struct wld_context * base,
return NULL;
}
-struct wld_drawable * context_import
- (struct wld_context * base, uint32_t type, union wld_object object,
- uint32_t width, uint32_t height, uint32_t format, uint32_t pitch)
+struct wld_drawable * context_import(struct wld_context * context,
+ uint32_t type, union wld_object object,
+ uint32_t width, uint32_t height,
+ uint32_t format, uint32_t pitch)
{
- struct pixman_context * context = (void *) base;
struct wld_drawable * drawable;
pixman_image_t * image;
@@ -142,7 +149,7 @@ struct wld_drawable * context_import
if (!image)
goto error0;
- if (!(drawable = new_drawable(context, image)))
+ if (!(drawable = new_drawable(image)))
goto error1;
return drawable;
@@ -154,68 +161,94 @@ struct wld_drawable * context_import
}
-void context_destroy(struct wld_context * base)
+void context_destroy(struct wld_context * context)
+{
+}
+
+uint32_t renderer_capabilities(struct wld_renderer * renderer,
+ struct wld_drawable * drawable)
{
- struct pixman_context * context = (void *) base;
+ if (drawable->impl == &drawable_impl)
+ return WLD_CAPABILITY_READ | WLD_CAPABILITY_WRITE;
- pixman_glyph_cache_destroy(context->glyph_cache);
- free(context);
+ return 0;
}
-void drawable_fill_rectangle(struct wld_drawable * drawable, uint32_t color,
+bool renderer_set_target(struct wld_renderer * base,
+ struct wld_drawable * drawable)
+{
+ struct pixman_renderer * renderer = pixman_renderer(base);
+
+ if (drawable && drawable->impl != &drawable_impl)
+ return false;
+
+ renderer->target = drawable ? pixman_drawable(drawable)->image : NULL;
+
+ return true;
+}
+
+void renderer_fill_rectangle(struct wld_renderer * base, uint32_t color,
int32_t x, int32_t y,
uint32_t width, uint32_t height)
{
- struct pixman_drawable * pixman = (void *) drawable;
+ struct pixman_renderer * renderer = pixman_renderer(base);
pixman_color_t pixman_color = PIXMAN_COLOR(color);
pixman_box32_t box = { x, y, x + width, y + height };
- pixman_image_fill_boxes(PIXMAN_OP_SRC, pixman->image, &pixman_color,
- 1, &box);
+ pixman_image_fill_boxes(PIXMAN_OP_SRC, renderer->target,
+ &pixman_color, 1, &box);
}
-void drawable_fill_region(struct wld_drawable * drawable, uint32_t color,
+void renderer_fill_region(struct wld_renderer * base, uint32_t color,
pixman_region32_t * region)
{
- struct pixman_drawable * pixman = (void *) drawable;
+ struct pixman_renderer * renderer = pixman_renderer(base);
pixman_color_t pixman_color = PIXMAN_COLOR(color);
pixman_box32_t * boxes;
int num_boxes;
boxes = pixman_region32_rectangles(region, &num_boxes);
- pixman_image_fill_boxes(PIXMAN_OP_SRC, pixman->image, &pixman_color,
- num_boxes, boxes);
+ pixman_image_fill_boxes(PIXMAN_OP_SRC, renderer->target,
+ &pixman_color, num_boxes, boxes);
}
-void drawable_copy_rectangle(struct wld_drawable * src_drawable,
- struct wld_drawable * dst_drawable,
- int32_t src_x, int32_t src_y,
+void renderer_copy_rectangle(struct wld_renderer * base,
+ struct wld_drawable * drawable,
int32_t dst_x, int32_t dst_y,
+ int32_t src_x, int32_t src_y,
uint32_t width, uint32_t height)
{
- struct pixman_drawable * src = (void *) src_drawable;
- struct pixman_drawable * dst = (void *) dst_drawable;
+ struct pixman_renderer * renderer = pixman_renderer(base);
+ pixman_image_t * dst = renderer->target, * src;
- pixman_image_composite32(PIXMAN_OP_SRC, src->image, NULL, dst->image,
+ if (drawable->impl != &drawable_impl)
+ return;
+
+ src = pixman_drawable(drawable)->image;
+ pixman_image_composite32(PIXMAN_OP_SRC, src, NULL, dst,
src_x, src_y, 0, 0, dst_x, dst_y, width, height);
}
-void drawable_copy_region(struct wld_drawable * src_drawable,
- struct wld_drawable * dst_drawable,
- pixman_region32_t * region,
- int32_t dst_x, int32_t dst_y)
+void renderer_copy_region(struct wld_renderer * base,
+ struct wld_drawable * drawable,
+ int32_t dst_x, int32_t dst_y,
+ pixman_region32_t * region)
{
- struct pixman_drawable * src = (void *) src_drawable;
- struct pixman_drawable * dst = (void *) dst_drawable;
+ struct pixman_renderer * renderer = pixman_renderer(base);
+ pixman_image_t * dst = renderer->target, * src;
- pixman_image_set_clip_region32(src->image, region);
- pixman_image_composite32(PIXMAN_OP_SRC, src->image, NULL, dst->image,
+ if (drawable->impl != &drawable_impl)
+ return;
+
+ src = pixman_drawable(drawable)->image;
+ pixman_image_set_clip_region32(src, region);
+ pixman_image_composite32(PIXMAN_OP_SRC, src, NULL, dst,
region->extents.x1, region->extents.y1, 0, 0,
region->extents.x1 + dst_x,
region->extents.y1 + dst_y,
region->extents.x2 - region->extents.x1,
region->extents.y2 - region->extents.y1);
- pixman_image_set_clip_region32(src->image, NULL);
+ pixman_image_set_clip_region32(src, NULL);
}
static inline uint8_t reverse(uint8_t byte)
@@ -227,12 +260,12 @@ static inline uint8_t reverse(uint8_t byte)
return byte;
}
-void drawable_draw_text(struct wld_drawable * drawable,
+void renderer_draw_text(struct wld_renderer * base,
struct font * font, uint32_t color,
int32_t x, int32_t y, const char * text, int32_t length,
struct wld_extents * extents)
{
- struct pixman_drawable * pixman = (void *) drawable;
+ struct pixman_renderer * renderer = pixman_renderer(base);
int ret;
uint32_t c;
struct glyph * glyph;
@@ -257,8 +290,8 @@ void drawable_draw_text(struct wld_drawable * drawable,
glyphs[index].x = origin_x;
glyphs[index].y = 0;
- glyphs[index].glyph = pixman_glyph_cache_lookup
- (pixman->context->glyph_cache, font, glyph);
+ glyphs[index].glyph = pixman_glyph_cache_lookup(renderer->glyph_cache,
+ font, glyph);
/* If we don't have the glyph in our cache, do some conversions to make
* pixman happy, and then insert it. */
@@ -293,11 +326,11 @@ void drawable_draw_text(struct wld_drawable * drawable,
}
/* Insert the glyph into the cache. */
- pixman_glyph_cache_freeze(pixman->context->glyph_cache);
+ pixman_glyph_cache_freeze(renderer->glyph_cache);
glyphs[index].glyph = pixman_glyph_cache_insert
- (pixman->context->glyph_cache, font, glyph,
+ (renderer->glyph_cache, font, glyph,
-glyph->x, -glyph->y, image);
- pixman_glyph_cache_thaw(pixman->context->glyph_cache);
+ pixman_glyph_cache_thaw(renderer->glyph_cache);
/* The glyph cache copies the contents of the glyph bitmap. */
pixman_image_unref(image);
@@ -309,8 +342,8 @@ void drawable_draw_text(struct wld_drawable * drawable,
origin_x += glyph->advance;
}
- pixman_composite_glyphs_no_mask(PIXMAN_OP_OVER, solid, pixman->image, 0, 0,
- x, y, pixman->context->glyph_cache,
+ pixman_composite_glyphs_no_mask(PIXMAN_OP_OVER, solid, renderer->target,
+ 0, 0, x, y, renderer->glyph_cache,
index, glyphs);
pixman_image_unref(solid);
@@ -319,6 +352,18 @@ void drawable_draw_text(struct wld_drawable * drawable,
extents->advance = origin_x;
}
+void renderer_flush(struct wld_renderer * renderer)
+{
+}
+
+void renderer_destroy(struct wld_renderer * base)
+{
+ struct pixman_renderer * renderer = pixman_renderer(base);
+
+ pixman_glyph_cache_destroy(renderer->glyph_cache);
+ free(renderer);
+}
+
void drawable_write(struct wld_drawable * drawable,
const void * data, size_t size)
{
@@ -334,10 +379,6 @@ pixman_image_t * drawable_map(struct wld_drawable * drawable)
return pixman_image_ref(pixman->image);
}
-void drawable_flush(struct wld_drawable * drawable)
-{
-}
-
void drawable_destroy(struct wld_drawable * drawable)
{
struct pixman_drawable * pixman = (void *) drawable;
diff --git a/pixman.h b/pixman.h
@@ -26,13 +26,12 @@
#include <stdint.h>
-struct wld_context;
+extern struct wld_context * wld_pixman_context;
-/**
- * Create a new drawing context which uses Pixman to do software rendering on
- * buffers in memory.
- */
-struct wld_context * wld_pixman_create_context();
+static inline struct wld_context * wld_pixman_create_context()
+{
+ return wld_pixman_context;
+}
#endif
diff --git a/renderer.c b/renderer.c
@@ -0,0 +1,145 @@
+/* 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 wld_drawable * drawable,
+ 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, drawable,
+ 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_drawable * drawable)
+{
+ return renderer->impl->capabilities(renderer, drawable);
+}
+
+EXPORT
+bool wld_set_target_drawable(struct wld_renderer * renderer,
+ struct wld_drawable * drawable)
+{
+ if (!renderer->impl->set_target(renderer, drawable))
+ return false;
+
+ renderer->target = drawable;
+
+ return true;
+}
+
+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_drawable * drawable,
+ 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, drawable, dst_x, dst_y,
+ src_x, src_y, width, height);
+}
+
+EXPORT
+void wld_copy_region(struct wld_renderer * renderer,
+ struct wld_drawable * drawable,
+ int32_t dst_x, int32_t dst_y, pixman_region32_t * region)
+{
+ renderer->impl->copy_region(renderer, drawable, dst_x, dst_y, region);
+}
+
+EXPORT
+void wld_draw_text_n(struct wld_renderer * renderer,
+ struct wld_font * font_base, uint32_t color,
+ int32_t x, int32_t y, const char * text, int32_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);
+}
+
diff --git a/wayland-drm.c b/wayland-drm.c
@@ -184,6 +184,13 @@ int wld_wayland_drm_get_fd(struct wld_context * base)
return context->authenticated ? context->fd : -1;
}
+struct wld_renderer * context_create_renderer(struct wld_context * base)
+{
+ struct drm_context * context = drm_context(base);
+
+ return wld_create_renderer(context->driver_context);
+}
+
struct wld_drawable * context_create_drawable(struct wld_context * base,
uint32_t width, uint32_t height,
uint32_t format)
diff --git a/wayland-shm.c b/wayland-shm.c
@@ -43,8 +43,6 @@ struct shm_context
struct wl_registry * registry;
struct wl_shm * wl;
struct wl_array formats;
-
- struct wld_context * pixman_context;
};
#include "interface/context.h"
@@ -106,8 +104,6 @@ struct wld_context * wld_shm_create_context(struct wl_display * display,
wl_registry_add_listener(context->registry, ®istry_listener, context);
wl_proxy_set_queue((struct wl_proxy *) context->registry, queue);
- context->pixman_context = wld_pixman_create_context();
-
/* Wait for wl_shm global. */
wayland_roundtrip(display, queue);
@@ -148,6 +144,11 @@ bool wld_shm_has_format(struct wld_context * base, uint32_t format)
return false;
}
+struct wld_renderer * context_create_renderer(struct wld_context * context)
+{
+ return wld_create_renderer(wld_pixman_context);
+}
+
struct wld_drawable * context_create_drawable(struct wld_context * base,
uint32_t width, uint32_t height,
uint32_t format)
@@ -181,7 +182,7 @@ struct wld_drawable * context_create_drawable(struct wld_context * base,
goto error1;
object.ptr = data;
- drawable = wld_import(context->pixman_context, WLD_OBJECT_DATA, object,
+ drawable = wld_import(wld_pixman_context, WLD_OBJECT_DATA, object,
width, height, format, pitch);
if (!drawable)
@@ -228,7 +229,6 @@ void context_destroy(struct wld_context * base)
{
struct shm_context * context = shm_context(base);
- wld_destroy_context(context->pixman_context);
wl_shm_destroy(context->wl);
wl_registry_destroy(context->registry);
wl_array_release(&context->formats);
diff --git a/wld-private.h b/wld-private.h
@@ -85,6 +85,7 @@ struct font
struct wld_context_impl
{
+ struct wld_renderer * (* create_renderer)(struct wld_context * context);
struct wld_drawable * (* create_drawable)(struct wld_context * context,
uint32_t width, uint32_t height,
uint32_t format);
@@ -95,29 +96,39 @@ struct wld_context_impl
void (* destroy)(struct wld_context * context);
};
-struct wld_drawable_impl
+struct wld_renderer_impl
{
- void (* fill_rectangle)(struct wld_drawable * drawable, uint32_t color,
- int32_t x, int32_t y,
+ uint32_t (* capabilities)(struct wld_renderer * renderer,
+ struct wld_drawable * buffer);
+ bool (* set_target)(struct wld_renderer * renderer,
+ struct wld_drawable * drawable);
+ void (* fill_rectangle)(struct wld_renderer * renderer,
+ uint32_t color, int32_t x, int32_t y,
uint32_t width, uint32_t height);
- void (* fill_region)(struct wld_drawable * drawable, uint32_t color,
- pixman_region32_t * region);
- void (* copy_rectangle)(struct wld_drawable * src,
- struct wld_drawable * dst,
- int32_t src_x, int32_t src_y,
+ void (* fill_region)(struct wld_renderer * renderer,
+ uint32_t color, pixman_region32_t * region);
+ void (* copy_rectangle)(struct wld_renderer * renderer,
+ struct wld_drawable * src,
int32_t dst_x, int32_t dst_y,
+ int32_t src_x, int32_t src_y,
uint32_t width, uint32_t height);
- void (* copy_region)(struct wld_drawable * src, struct wld_drawable * dst,
- pixman_region32_t * region,
- int32_t dst_x, int32_t dst_y);
- void (* draw_text)(struct wld_drawable * drawable,
+ void (* copy_region)(struct wld_renderer * renderer,
+ struct wld_drawable * src,
+ int32_t dst_x, int32_t dst_y,
+ pixman_region32_t * region);
+ void (* draw_text)(struct wld_renderer * renderer,
struct font * font, uint32_t color,
int32_t x, int32_t y, const char * text, int32_t length,
struct wld_extents * extents);
+ void (* flush)(struct wld_renderer * renderer);
+ void (* destroy)(struct wld_renderer * renderer);
+};
+
+struct wld_drawable_impl
+{
void (* write)(struct wld_drawable * drawable,
const void * data, size_t size);
pixman_image_t * (* map)(struct wld_drawable * drawable);
- void (* flush)(struct wld_drawable * drawable);
void (* destroy)(struct wld_drawable * drawable);
};
@@ -181,19 +192,23 @@ static inline uint32_t format_pixman_to_wld(pixman_format_code_t format)
/**
* This default fill_region method is implemented in terms of fill_rectangle.
*/
-void default_fill_region(struct wld_drawable * drawable, uint32_t color,
+void default_fill_region(struct wld_renderer * renderer, uint32_t color,
pixman_region32_t * region);
/**
* This default copy_region method is implemented in terms of copy_rectangle.
*/
-void default_copy_region(struct wld_drawable * src, struct wld_drawable * dst,
- pixman_region32_t * region,
- int32_t dst_x, int32_t dst_y);
+void default_copy_region(struct wld_renderer * renderer,
+ struct wld_drawable * drawable,
+ int32_t dst_x, int32_t dst_y,
+ pixman_region32_t * region);
void context_initialize(struct wld_context * context,
const struct wld_context_impl * impl);
+void renderer_initialize(struct wld_renderer * renderer,
+ const struct wld_renderer_impl * impl);
+
void drawable_initialize(struct wld_drawable * drawable,
const struct wld_drawable_impl * impl,
uint32_t width, uint32_t height,
diff --git a/wld.h b/wld.h
@@ -66,6 +66,8 @@ struct wld_context
const struct wld_context_impl * const impl;
};
+struct wld_renderer * wld_create_renderer(struct wld_context * context);
+
struct wld_drawable * wld_create_drawable(struct wld_context * context,
uint32_t width, uint32_t height,
uint32_t format);
@@ -158,24 +160,52 @@ struct wld_drawable
bool wld_export(struct wld_drawable * drawable,
uint32_t type, union wld_object * object);
+void wld_write(struct wld_drawable * drawable, const void * data, size_t size);
+
+pixman_image_t * wld_map(struct wld_drawable * drawable);
+
/**
* Destroy a drawable (created with any context).
*/
void wld_destroy_drawable(struct wld_drawable * drawable);
-void wld_fill_rectangle(struct wld_drawable * drawable, uint32_t color,
+/**** Renderers ****/
+
+struct wld_renderer
+{
+ const struct wld_renderer_impl * const impl;
+ struct wld_drawable * target;
+};
+
+enum wld_capability
+{
+ WLD_CAPABILITY_READ = 1<<0,
+ WLD_CAPABILITY_WRITE = 1<<1,
+};
+
+void wld_destroy_renderer(struct wld_renderer * renderer);
+
+uint32_t wld_capabilities(struct wld_renderer * renderer,
+ struct wld_drawable * drawable);
+
+bool wld_set_target_buffer(struct wld_renderer * renderer,
+ struct wld_drawable * drawable);
+
+void wld_fill_rectangle(struct wld_renderer * renderer, uint32_t color,
int32_t x, int32_t y, uint32_t width, uint32_t height);
-void wld_fill_region(struct wld_drawable * drawable, uint32_t color,
+void wld_fill_region(struct wld_renderer * renderer, uint32_t color,
pixman_region32_t * region);
-void wld_copy_rectangle(struct wld_drawable * src, struct wld_drawable * dst,
- int32_t src_x, int32_t src_y,
+void wld_copy_rectangle(struct wld_renderer * renderer,
+ struct wld_drawable * drawable,
int32_t dst_x, int32_t dst_y,
+ int32_t src_x, int32_t src_y,
uint32_t width, uint32_t height);
-void wld_copy_region(struct wld_drawable * src, struct wld_drawable * dst,
- pixman_region32_t * region, int32_t dst_x, int32_t dst_y);
+void wld_copy_region(struct wld_renderer * renderer,
+ struct wld_drawable * drawable,
+ int32_t dst_x, int32_t dst_y, pixman_region32_t * region);
/**
* Draw a UTF-8 text string to the given drawable.
@@ -184,24 +214,20 @@ void wld_copy_region(struct wld_drawable * src, struct wld_drawable * dst,
* @param extents If not NULL, will be initialized to the extents of the
* drawn text
*/
-void wld_draw_text_n(struct wld_drawable * drawable,
+void wld_draw_text_n(struct wld_renderer * renderer,
struct wld_font * font, uint32_t color,
int32_t x, int32_t y, const char * text, int32_t length,
struct wld_extents * extents);
-static inline void wld_draw_text(struct wld_drawable * drawable,
+static inline void wld_draw_text(struct wld_renderer * renderer,
struct wld_font * font, uint32_t color,
int32_t x, int32_t y, const char * text,
struct wld_extents * extents)
{
- wld_draw_text_n(drawable, font, color, x, y, text, INT32_MAX, extents);
+ wld_draw_text_n(renderer, font, color, x, y, text, INT32_MAX, extents);
}
-void wld_write(struct wld_drawable * drawable, const void * data, size_t size);
-
-pixman_image_t * wld_map(struct wld_drawable * drawable);
-
-void wld_flush(struct wld_drawable * drawable);
+void wld_flush(struct wld_renderer * renderer);
#endif