commit f61ad4e6277f7bb45ce44bfe466a480616cec45f
parent 6fb42cc33b69227b82f4a95597e11a3b2eb03beb
Author: Michael Forney <mforney@mforney.org>
Date: Tue, 31 Dec 2013 22:53:22 -0800
Add context abstraction
Diffstat:
| M | Makefile | | | 2 | +- |
| A | context.c | | | 55 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| M | drm-private.h | | | 31 | +++---------------------------- |
| M | drm.c | | | 73 | +++++++------------------------------------------------------------------ |
| M | drm.h | | | 44 | ++++++++------------------------------------ |
| M | dumb.c | | | 64 | +++++++++++++++++++++++++++++++--------------------------------- |
| M | intel.c | | | 86 | ++++++++++++++++++++++++++++++++++++++++++++----------------------------------- |
| A | interface/context.h | | | 37 | +++++++++++++++++++++++++++++++++++++ |
| M | interface/drm.h | | | 17 | +---------------- |
| M | pixman-private.h | | | 6 | +++--- |
| M | pixman.c | | | 104 | ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++------------------- |
| M | pixman.h | | | 19 | ++----------------- |
| M | wayland-drm.c | | | 15 | +++++++-------- |
| M | wayland-shm.c | | | 10 | ++++++---- |
| M | wld-private.h | | | 28 | ++++++++++++++++++++++++++++ |
| M | wld.h | | | 30 | ++++++++++++++++++++++++++++++ |
16 files changed, 346 insertions(+), 275 deletions(-)
diff --git a/Makefile b/Makefile
@@ -15,7 +15,7 @@ CLEAN_FILES := $(TARGETS)
WLD_REQUIRES = fontconfig pixman-1
WLD_REQUIRES_PRIVATE = freetype2
-WLD_SOURCES = drawable.c color.c font.c
+WLD_SOURCES = context.c drawable.c color.c font.c
WLD_HEADERS = wld.h
ifeq ($(ENABLE_DEBUG),1)
diff --git a/context.c b/context.c
@@ -0,0 +1,55 @@
+/* wld: context.c
+ *
+ * Copyright (c) 2013 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 context_initialize(struct wld_context * context,
+ const struct wld_context_impl * impl)
+{
+ *((const struct wld_context_impl **) &context->impl) = impl;
+}
+
+EXPORT
+struct wld_drawable * wld_create_drawable(struct wld_context * context,
+ uint32_t width, uint32_t height,
+ uint32_t format)
+{
+ return context->impl->create_drawable(context, width, height, format);
+}
+
+EXPORT
+struct wld_drawable * wld_import(struct wld_context * context,
+ uint32_t type, union wld_object object,
+ uint32_t width, uint32_t height,
+ uint32_t format, uint32_t pitch)
+{
+ return context->impl->import(context, type, object,
+ width, height, format, pitch);
+}
+
+EXPORT
+void wld_destroy_context(struct wld_context * context)
+{
+ context->impl->destroy(context);
+}
+
diff --git a/drm-private.h b/drm-private.h
@@ -26,30 +26,13 @@
#include "wld-private.h"
-typedef bool (* drm_device_supported_func_t)(uint32_t vendor_id,
- uint32_t device_id);
-typedef void * (* drm_create_context_func_t)(int drm_fd);
-typedef void (* drm_destroy_context_func_t)(void * context);
-typedef struct wld_drawable * (* drm_create_drawable_func_t)
- (void * context, uint32_t width, uint32_t height, uint32_t format);
-typedef struct wld_drawable * (* drm_import_func_t)
- (void * context, uint32_t width, uint32_t height, uint32_t format,
- int prime_fd, unsigned long pitch);
-typedef struct wld_drawable * (* drm_import_gem_func_t)
- (void * context, uint32_t width, uint32_t height, uint32_t format,
- uint32_t gem_name, unsigned long pitch);
-
typedef int (* drm_export_func_t)(struct wld_drawable * drawable);
typedef uint32_t (* drm_get_handle_func_t)(struct wld_drawable * drawable);
struct wld_drm_interface
{
- drm_device_supported_func_t device_supported;
- drm_create_context_func_t create_context;
- drm_destroy_context_func_t destroy_context;
- drm_create_drawable_func_t create_drawable;
- drm_import_func_t import;
- drm_import_gem_func_t import_gem;
+ bool (* device_supported)(uint32_t vendor_id, uint32_t device_id);
+ struct wld_context * (* create_context)(int drm_fd);
};
struct drm_draw_interface
@@ -59,19 +42,11 @@ struct drm_draw_interface
drm_get_handle_func_t get_handle;
};
-struct wld_drm_context
-{
- const struct wld_drm_interface * interface;
- void * context;
-};
-
#if WITH_DRM_INTEL
extern const struct wld_drm_interface intel_drm;
#endif
extern const struct wld_drm_interface dumb_drm;
-
-bool drm_initialize_context(struct wld_drm_context * context, int fd);
-void drm_finalize_context(struct wld_drm_context * context);
+extern const struct wld_context_impl * dumb_context_impl;
#endif
diff --git a/drm.c b/drm.c
@@ -68,80 +68,21 @@ static const struct wld_drm_interface * find_drm_interface(int fd)
return NULL;
}
-bool drm_initialize_context(struct wld_drm_context * drm, int fd)
-{
- if (!(drm->interface = find_drm_interface(fd)))
- return false;
-
- if (!(drm->context = drm->interface->create_context(fd)))
- return false;
-
- return true;
-}
-
-void drm_finalize_context(struct wld_drm_context * drm)
-{
- drm->interface->destroy_context(drm->context);
-}
-
-EXPORT
-struct wld_drm_context * wld_drm_create_context(int fd)
-{
- struct wld_drm_context * drm;
-
- if (!(drm = malloc(sizeof *drm)))
- goto error0;
-
- if (!drm_initialize_context(drm, fd))
- goto error1;
-
- return drm;
-
- error1:
- free(drm);
- error0:
- return NULL;
-}
-
-EXPORT
-void wld_drm_destroy_context(struct wld_drm_context * drm)
-{
- drm_finalize_context(drm);
- free(drm);
-}
-
EXPORT
-bool wld_drm_is_dumb(struct wld_drm_context * drm)
+struct wld_context * wld_drm_create_context(int fd)
{
- return drm->interface == &dumb_drm;
-}
+ const struct wld_drm_interface * interface;
-EXPORT
-struct wld_drawable * wld_drm_create_drawable(struct wld_drm_context * drm,
- uint32_t width, uint32_t height,
- uint32_t format)
-{
- return drm->interface->create_drawable(drm->context, width, height, format);
-}
+ if (!(interface = find_drm_interface(fd)))
+ return NULL;
-EXPORT
-struct wld_drawable * wld_drm_import(struct wld_drm_context * drm,
- uint32_t width, uint32_t height,
- uint32_t format,
- int prime_fd, unsigned long pitch)
-{
- return drm->interface->import(drm->context, width, height, format,
- prime_fd, pitch);
+ return interface->create_context(fd);
}
EXPORT
-struct wld_drawable * wld_drm_import_gem(struct wld_drm_context * drm,
- uint32_t width, uint32_t height,
- uint32_t format,
- uint32_t gem_name, unsigned long pitch)
+bool wld_drm_is_dumb(struct wld_context * context)
{
- return drm->interface->import_gem(drm->context, width, height, format,
- gem_name, pitch);
+ return context->impl == dumb_context_impl;
}
EXPORT
diff --git a/drm.h b/drm.h
@@ -27,46 +27,18 @@
#include <stdbool.h>
#include <stdint.h>
-struct wld_drm_context;
-struct wld_drawable;
+enum wld_drm_object_type
+{
+ WLD_DRM_OBJECT_PRIME_FD = 0x00010000,
+ WLD_DRM_OBJECT_GEM_NAME = 0x00010001
+};
/**
- * Create a new DRM context from an opened DRM device file descriptor.
+ * Create a new WLD context from an opened DRM device file descriptor.
*/
-struct wld_drm_context * wld_drm_create_context(int fd);
+struct wld_context * wld_drm_create_context(int fd);
-/**
- * Destroy a DRM context.
- */
-void wld_drm_destroy_context(struct wld_drm_context * drm);
-
-bool wld_drm_is_dumb(struct wld_drm_context * drm);
-
-/**
- * Create a new DRM drawable.
- */
-struct wld_drawable * wld_drm_create_drawable(struct wld_drm_context * drm,
- uint32_t width, uint32_t height,
- uint32_t format);
-
-/**
- * Create a new DRM drawable by importing a PRIME file descriptor.
- */
-struct wld_drawable * wld_drm_import(struct wld_drm_context * context,
- uint32_t width, uint32_t height,
- uint32_t format,
- int prime_fd, unsigned long pitch);
-
-/**
- * Create a new DRM drawable by importing a GEM name.
- *
- * This is provided for compatibility with clients that don't support PRIME.
- * You should use wld_drm_import_drawable and PRIME if possible.
- */
-struct wld_drawable * wld_drm_import_gem(struct wld_drm_context * context,
- uint32_t width, uint32_t height,
- uint32_t format, uint32_t gem_name,
- unsigned long pitch);
+bool wld_drm_is_dumb(struct wld_context * context);
/**
* Export a DRM drawable to a PRIME file descriptor.
diff --git a/dumb.c b/dumb.c
@@ -24,6 +24,7 @@
#include "pixman.h"
#include "pixman-private.h"
#include "drm-private.h"
+#include "drm.h"
#include <fcntl.h>
#include <sys/mman.h>
@@ -33,7 +34,8 @@
struct dumb_context
{
- struct wld_pixman_context * pixman;
+ struct wld_context base;
+ struct wld_context * pixman;
int fd;
};
@@ -45,7 +47,11 @@ struct dumb_drawable
};
#define DRM_DRIVER_NAME dumb
+#include "interface/context.h"
#include "interface/drm.h"
+IMPL(dumb, context)
+
+const struct wld_context_impl * dumb_context_impl = &context_impl;
/* DRM drawable */
static int drawable_export(struct wld_drawable * drawable);
@@ -62,7 +68,7 @@ bool drm_device_supported(uint32_t vendor_id, uint32_t device_id)
return true;
}
-void * drm_create_context(int drm_fd)
+struct wld_context * drm_create_context(int drm_fd)
{
struct dumb_context * context;
@@ -72,6 +78,7 @@ void * drm_create_context(int drm_fd)
if (!(context->pixman = wld_pixman_create_context()))
goto error1;
+ context_initialize(&context->base, &context_impl);
context->fd = drm_fd;
if (!draw_initialized)
@@ -80,7 +87,7 @@ void * drm_create_context(int drm_fd)
draw_initialized = true;
}
- return context;
+ return &context->base;
error1:
free(context);
@@ -88,14 +95,6 @@ void * drm_create_context(int drm_fd)
return NULL;
}
-void drm_destroy_context(void * base)
-{
- struct dumb_context * context = base;
-
- wld_pixman_destroy_context(context->pixman);
- free(context);
-}
-
static struct wld_drawable * new_drawable(struct dumb_context * context,
uint32_t width, uint32_t height,
uint32_t format, uint32_t handle,
@@ -141,11 +140,11 @@ static struct wld_drawable * new_drawable(struct dumb_context * context,
return NULL;
}
-struct wld_drawable * drm_create_drawable(void * base,
- uint32_t width, uint32_t height,
- uint32_t format)
+struct wld_drawable * context_create_drawable(struct wld_context * base,
+ uint32_t width, uint32_t height,
+ uint32_t format)
{
- struct dumb_context * context = base;
+ struct dumb_context * context = dumb_context(base);
struct wld_drawable * drawable;
struct drm_mode_create_dumb create_dumb_arg = {
.height = height, .width = width,
@@ -178,33 +177,32 @@ struct wld_drawable * drm_create_drawable(void * base,
return NULL;
}
-struct wld_drawable * drm_import(void * base, uint32_t width, uint32_t height,
- uint32_t format, int prime_fd,
- unsigned long pitch)
+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 dumb_context * context = base;
- int ret;
+ struct dumb_context * context = dumb_context(base);
uint32_t handle;
- ret = drmPrimeFDToHandle(context->fd, prime_fd, &handle);
-
- if (ret != 0)
- goto error0;
+ switch (type)
+ {
+ case WLD_DRM_OBJECT_PRIME_FD:
+ if (drmPrimeFDToHandle(context->fd, object.i, &handle) != 0)
+ return NULL;
+ break;
+ default: return NULL;
+ }
return new_drawable(context, width, height, format, handle, pitch);
-
- error0:
- return NULL;
}
-struct wld_drawable * drm_import_gem(void * context,
- uint32_t width, uint32_t height,
- uint32_t format,
- uint32_t gem_name, unsigned long pitch)
+void context_destroy(struct wld_context * base)
{
- DEBUG("dumb: import_gem is not supported\n");
+ struct dumb_context * context = dumb_context(base);
- return NULL;
+ wld_destroy_context(context->pixman);
+ free(context);
}
static int drawable_export(struct wld_drawable * drawable)
diff --git a/intel.c b/intel.c
@@ -22,6 +22,7 @@
*/
#include "drm-private.h"
+#include "drm.h"
#include "wld-private.h"
#include <unistd.h>
@@ -31,6 +32,7 @@
struct intel_context
{
+ struct wld_context base;
drm_intel_bufmgr * bufmgr;
struct intel_batch * batch;
};
@@ -44,16 +46,18 @@ struct intel_drawable
pixman_image_t * virtual;
};
+#include "interface/context.h"
#define DRM_DRIVER_NAME intel
#include "interface/drm.h"
#include "interface/drm_drawable.h"
+IMPL(intel, context)
bool drm_device_supported(uint32_t vendor_id, uint32_t device_id)
{
return vendor_id == 0x8086;
}
-void * drm_create_context(int drm_fd)
+struct wld_context * drm_create_context(int drm_fd)
{
struct intel_context * context;
@@ -62,6 +66,7 @@ void * drm_create_context(int drm_fd)
if (!context)
goto error0;
+ context_initialize(&context->base, &context_impl);
context->bufmgr = drm_intel_bufmgr_gem_init(drm_fd, INTEL_BATCH_SIZE);
if (!context->bufmgr)
@@ -72,7 +77,7 @@ void * drm_create_context(int drm_fd)
if (!context->batch)
goto error2;
- return context;
+ return &context->base;
error2:
drm_intel_bufmgr_destroy(context->bufmgr);
@@ -82,15 +87,6 @@ void * drm_create_context(int drm_fd)
return NULL;
}
-void drm_destroy_context(void * base)
-{
- struct intel_context * context = base;
-
- intel_batch_destroy(context->batch);
- drm_intel_bufmgr_destroy(context->bufmgr);
- free(context);
-}
-
static struct intel_drawable * new_drawable(struct intel_context * context,
uint32_t width, uint32_t height,
uint32_t format)
@@ -110,11 +106,11 @@ static struct intel_drawable * new_drawable(struct intel_context * context,
return intel;
}
-struct wld_drawable * drm_create_drawable(void * base,
- uint32_t width, uint32_t height,
- uint32_t format)
+struct wld_drawable * context_create_drawable(struct wld_context * base,
+ uint32_t width, uint32_t height,
+ uint32_t format)
{
- struct intel_context * context = base;
+ struct intel_context * context = intel_context(base);
struct intel_drawable * intel;
uint32_t tiling_mode = width >= 128 ? I915_TILING_X : I915_TILING_NONE;
@@ -128,41 +124,55 @@ struct wld_drawable * drm_create_drawable(void * base,
return &intel->base;
}
-struct wld_drawable * drm_import(void * base,
- uint32_t width, uint32_t height,
- uint32_t format,
- int prime_fd, unsigned long pitch)
+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 intel_context * context = base;
+ struct intel_context * context = intel_context(base);
struct intel_drawable * intel;
- uint32_t size = width * height * 4;
+ drm_intel_bo * bo;
+
+ switch (type)
+ {
+ case WLD_DRM_OBJECT_PRIME_FD:
+ {
+ uint32_t size = width * height * format_bytes_per_pixel(format);
+ bo = drm_intel_bo_gem_create_from_prime(context->bufmgr,
+ object.i, size);
+ break;
+ }
+ case WLD_DRM_OBJECT_GEM_NAME:
+ bo = drm_intel_bo_gem_create_from_name(context->bufmgr, "buffer",
+ object.u32);
+ break;
+ default: bo = NULL;
+ };
+
+ if (!bo)
+ goto error0;
if (!(intel = new_drawable(context, width, height, format)))
- return NULL;
+ goto error1;
- intel->bo = drm_intel_bo_gem_create_from_prime(context->bufmgr,
- prime_fd, size);
+ intel->bo = bo;
intel->base.pitch = pitch;
return &intel->base;
+
+ error1:
+ drm_intel_bo_unreference(bo);
+ error0:
+ return NULL;
}
-struct wld_drawable * drm_import_gem(void * base,
- uint32_t width, uint32_t height,
- uint32_t format,
- uint32_t gem_name, unsigned long pitch)
+void context_destroy(struct wld_context * base)
{
- struct intel_context * context = base;
- struct intel_drawable * intel;
+ struct intel_context * context = intel_context(base);
- if (!(intel = new_drawable(context, width, height, format)))
- return NULL;
-
- intel->bo = drm_intel_bo_gem_create_from_name(context->bufmgr, "drawable",
- gem_name);
- intel->base.pitch = pitch;
-
- return &intel->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,
diff --git a/interface/context.h b/interface/context.h
@@ -0,0 +1,37 @@
+/* wld: interface/context.h
+ *
+ * Copyright (c) 2013 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 struct wld_drawable * context_create_drawable
+ (struct wld_context * context,
+ uint32_t width, uint32_t height, uint32_t format);
+static 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);
+static void context_destroy(struct wld_context * context);
+
+static const struct wld_context_impl context_impl = {
+ .create_drawable = &context_create_drawable,
+ .import = &context_import,
+ .destroy = &context_destroy
+};
+
diff --git a/interface/drm.h b/interface/drm.h
@@ -27,28 +27,13 @@
/* DRM implementation */
static bool drm_device_supported(uint32_t vendor_id, uint32_t device_id);
-static void * drm_create_context(int drm_fd);
-static void drm_destroy_context(void * context);
-static struct wld_drawable * drm_create_drawable
- (void * context, uint32_t width, uint32_t height, uint32_t format);
-static struct wld_drawable * drm_import(void * context,
- uint32_t width, uint32_t height,
- uint32_t format, int prime_fd,
- unsigned long pitch);
-static struct wld_drawable * drm_import_gem(void * context,
- uint32_t width, uint32_t height,
- uint32_t format, uint32_t gem_name,
- unsigned long pitch);
+static struct wld_context * drm_create_context(int drm_fd);
#define EXPAND(f, x) f(x)
#define VAR(name) name ## _drm
const struct wld_drm_interface EXPAND(VAR, DRM_DRIVER_NAME) = {
.device_supported = &drm_device_supported,
.create_context = &drm_create_context,
- .destroy_context = &drm_destroy_context,
- .create_drawable = &drm_create_drawable,
- .import = &drm_import,
- .import_gem = &drm_import_gem,
};
#undef VAR
#undef EXPAND
diff --git a/pixman-private.h b/pixman-private.h
@@ -26,19 +26,19 @@
#include "wld.h"
-struct wld_pixman_context;
+struct wld_context;
struct pixman_drawable
{
struct wld_drawable base;
pixman_image_t * image;
- struct wld_pixman_context * context;
+ struct pixman_context * context;
};
extern const struct wld_draw_interface * const pixman_draw;
bool pixman_initialize_drawable
- (struct wld_pixman_context * context, struct pixman_drawable * drawable,
+ (struct wld_context * context, struct pixman_drawable * drawable,
uint32_t width, uint32_t height,
void * data, uint32_t pitch, uint32_t format);
diff --git a/pixman.c b/pixman.c
@@ -32,20 +32,22 @@
.blue = ((c >> 0) & 0xff) * 0x101, \
}
-struct wld_pixman_context
+struct pixman_context
{
+ struct wld_context base;
pixman_glyph_cache_t * glyph_cache;
};
+#include "interface/context.h"
#define DRAWABLE_IMPLEMENTS_REGION
#include "interface/drawable.h"
const struct wld_draw_interface * const pixman_draw = &draw_interface;
EXPORT
-struct wld_pixman_context * wld_pixman_create_context()
+struct wld_context * wld_pixman_create_context()
{
- struct wld_pixman_context * context;
+ struct pixman_context * context;
if (!(context = malloc(sizeof *context)))
goto error0;
@@ -53,7 +55,7 @@ struct wld_pixman_context * wld_pixman_create_context()
if (!(context->glyph_cache = pixman_glyph_cache_create()))
goto error1;
- return context;
+ return &context->base;
error1:
free(context);
@@ -61,15 +63,8 @@ struct wld_pixman_context * wld_pixman_create_context()
return NULL;
}
-EXPORT
-void wld_pixman_destroy_context(struct wld_pixman_context * context)
-{
- pixman_glyph_cache_destroy(context->glyph_cache);
- free(context);
-}
-
bool pixman_initialize_drawable
- (struct wld_pixman_context * context, struct pixman_drawable * drawable,
+ (struct wld_context * context, struct pixman_drawable * drawable,
uint32_t width, uint32_t height,
void * data, uint32_t pitch, uint32_t format)
{
@@ -79,7 +74,7 @@ bool pixman_initialize_drawable
drawable->base.format = format;
drawable->base.pitch = pitch;
- drawable->context = context;
+ drawable->context = (void *) context;
drawable->image = pixman_image_create_bits(format_wld_to_pixman(format),
width, height,
(uint32_t *) data, pitch);
@@ -87,30 +82,89 @@ bool pixman_initialize_drawable
return drawable->image != NULL;
}
-EXPORT
-struct wld_drawable * wld_pixman_create_drawable
- (struct wld_pixman_context * context, uint32_t width, uint32_t height,
- void * data, uint32_t pitch, uint32_t format)
+struct wld_drawable * new_drawable(struct pixman_context * context,
+ pixman_image_t * image)
+{
+ struct pixman_drawable * drawable;
+
+ if (!(drawable = malloc(sizeof *drawable)))
+ return NULL;
+
+ drawable->base.interface = &draw_interface;
+ drawable->base.width = pixman_image_get_width(image);
+ drawable->base.height = pixman_image_get_height(image);
+ drawable->base.format = format_pixman_to_wld
+ (pixman_image_get_format(image));
+ drawable->base.pitch = pixman_image_get_stride(image);
+ drawable->context = context;
+ drawable->image = image;
+
+ return &drawable->base;
+}
+
+struct wld_drawable * context_create_drawable(struct wld_context * base,
+ uint32_t width, uint32_t height,
+ uint32_t format)
{
- struct pixman_drawable * pixman;
+ struct pixman_context * context = (void *) base;
+ struct wld_drawable * drawable;
+ pixman_image_t * image;
- pixman = malloc(sizeof *pixman);
+ image = pixman_image_create_bits(format_wld_to_pixman(format),
+ width, height, NULL, 0);
- if (!pixman)
+ if (!image)
goto error0;
- if (!pixman_initialize_drawable(context, pixman, width, height,
- data, pitch, format))
- {
+ if (!(drawable = new_drawable(context, image)))
goto error1;
+
+ return drawable;
+
+ error1:
+ pixman_image_unref(image);
+ error0:
+ 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 pixman_context * context = (void *) base;
+ struct wld_drawable * drawable;
+ pixman_image_t * image;
+
+ switch (type)
+ {
+ case WLD_OBJECT_DATA:
+ image = pixman_image_create_bits(format_wld_to_pixman(format),
+ width, height, object.ptr, pitch);
+ break;
+ default: image = NULL;
}
- return &pixman->base;
+ if (!image)
+ goto error0;
+
+ if (!(drawable = new_drawable(context, image)))
+ goto error1;
+
+ return drawable;
error1:
- free(pixman);
+ pixman_image_unref(image);
error0:
return NULL;
+
+}
+
+void context_destroy(struct wld_context * base)
+{
+ struct pixman_context * context = (void *) base;
+
+ pixman_glyph_cache_destroy(context->glyph_cache);
+ free(context);
}
void drawable_fill_rectangle(struct wld_drawable * drawable, uint32_t color,
diff --git a/pixman.h b/pixman.h
@@ -26,28 +26,13 @@
#include <stdint.h>
-struct wld_pixman_context;
-struct wld_drawable;
+struct wld_context;
/**
* Create a new drawing context which uses Pixman to do software rendering on
* buffers in memory.
*/
-struct wld_pixman_context * wld_pixman_create_context();
-
-/**
- * Destroy a Pixman context.
- */
-void wld_pixman_destroy_context(struct wld_pixman_context * context);
-
-/**
- * Create a new Pixman drawable with the specified dimensions.
- *
- * If data is NULL, new backing memory will be allocated.
- */
-struct wld_drawable * wld_pixman_create_drawable
- (struct wld_pixman_context * context, uint32_t width, uint32_t height,
- void * data, uint32_t pitch, uint32_t format);
+struct wld_context * wld_pixman_create_context();
#endif
diff --git a/wayland-drm.c b/wayland-drm.c
@@ -39,8 +39,7 @@
struct wld_wayland_drm_context
{
- struct wld_drm_context context;
-
+ struct wld_context * driver_context;
struct wl_drm * wl;
struct wl_registry * registry;
struct wl_array formats;
@@ -141,7 +140,7 @@ struct wld_wayland_drm_context * wld_wayland_drm_create_context
goto error4;
}
- if (!drm_initialize_context(&drm->context, drm->fd))
+ if (!(drm->driver_context = wld_drm_create_context(drm->fd)))
{
DEBUG("Couldn't initialize context for DRM device\n");
goto error4;
@@ -165,7 +164,7 @@ struct wld_wayland_drm_context * wld_wayland_drm_create_context
EXPORT
void wld_wayland_drm_destroy_context(struct wld_wayland_drm_context * drm)
{
- drm_finalize_context(&drm->context);
+ wld_destroy_context(&drm->driver_context);
close(drm->fd);
wl_drm_destroy(drm->wl);
wl_registry_destroy(drm->registry);
@@ -205,17 +204,17 @@ struct wld_drawable * wld_wayland_drm_create_drawable
if (buffer && !wld_wayland_drm_has_format(drm, format))
return NULL;
- drawable = wld_drm_create_drawable(&drm->context, width, height, format);
+ drawable = wld_create_drawable(drm->driver_context, width, height, format);
if (!drawable)
return NULL;
if (buffer)
{
- int prime_fd;
+ union wld_object object;
- prime_fd = wld_drm_export(drawable);
- *buffer = wl_drm_create_prime_buffer(drm->wl, prime_fd,
+ wld_export(drawable, WLD_DRM_OBJECT_PRIME_FD, &object);
+ *buffer = wl_drm_create_prime_buffer(drm->wl, object.i,
width, height, format,
0, drawable->pitch, 0, 0, 0, 0);
close(prime_fd);
diff --git a/wayland-shm.c b/wayland-shm.c
@@ -42,7 +42,7 @@ struct wld_shm_context
struct wl_shm * wl;
struct wl_array formats;
- struct wld_pixman_context * pixman_context;
+ struct wld_context * pixman_context;
};
static void registry_global(void * data, struct wl_registry * registry,
@@ -137,7 +137,7 @@ struct wld_shm_context * wld_shm_create_context(struct wl_display * display,
EXPORT
void wld_shm_destroy_context(struct wld_shm_context * shm)
{
- wld_pixman_destroy_context(shm->pixman_context);
+ wld_destroy_context(shm->pixman_context);
wl_shm_destroy(shm->wl);
wl_registry_destroy(shm->registry);
wl_array_release(&shm->formats);
@@ -173,6 +173,7 @@ struct wld_drawable * wld_shm_create_drawable(struct wld_shm_context * shm,
struct wl_shm_pool * pool;
struct wld_drawable * drawable;
uint32_t shm_format = wayland_format(format);
+ union wld_object object;
fd = mkostemp(name, O_CLOEXEC);
@@ -189,8 +190,9 @@ struct wld_drawable * wld_shm_create_drawable(struct wld_shm_context * shm,
if (data == MAP_FAILED)
goto error1;
- drawable = wld_pixman_create_drawable(shm->pixman_context, width, height,
- data, pitch, format);
+ object.ptr = data;
+ drawable = wld_import(shm->pixman_context, WLD_OBJECT_DATA, object,
+ width, height, format, pitch);
pool = wl_shm_create_pool(shm->wl, fd, size);
*buffer = wl_shm_pool_create_buffer(pool, 0, width, height, pitch,
diff --git a/wld-private.h b/wld-private.h
@@ -83,6 +83,18 @@ struct font
struct glyph ** glyphs;
};
+struct wld_context_impl
+{
+ struct wld_drawable * (* create_drawable)(struct wld_context * context,
+ uint32_t width, uint32_t height,
+ uint32_t format);
+ struct wld_drawable * (* import)(struct wld_context * context,
+ uint32_t type, union wld_object object,
+ uint32_t width, uint32_t height,
+ uint32_t format, uint32_t pitch);
+ void (* destroy)(struct wld_context * context);
+};
+
struct wld_draw_interface
{
void (* fill_rectangle)(struct wld_drawable * drawable, uint32_t color,
@@ -140,6 +152,19 @@ static inline pixman_format_code_t format_wld_to_pixman(uint32_t format)
}
}
+static inline uint32_t format_pixman_to_wld(pixman_format_code_t format)
+{
+ switch (format)
+ {
+ case PIXMAN_a8r8g8b8:
+ return WLD_FORMAT_ARGB8888;
+ case PIXMAN_x8r8g8b8:
+ return WLD_FORMAT_XRGB8888;
+ default:
+ return 0;
+ }
+}
+
/**
* This default fill_region method is implemented in terms of fill_rectangle.
*/
@@ -153,5 +178,8 @@ 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 context_initialize(struct wld_context * context,
+ const struct wld_context_impl * impl);
+
#endif
diff --git a/wld.h b/wld.h
@@ -47,6 +47,36 @@ enum wld_format
bool wld_lookup_named_color(const char * name, uint32_t * color);
+/**** WLD Context ****/
+
+enum wld_object_type
+{
+ WLD_OBJECT_DATA = 0x0
+};
+
+union wld_object
+{
+ void * ptr;
+ uint32_t u32;
+ int i;
+};
+
+struct wld_context
+{
+ const struct wld_context_impl * const impl;
+};
+
+struct wld_drawable * wld_create_drawable(struct wld_context * context,
+ uint32_t width, uint32_t height,
+ uint32_t format);
+
+struct wld_drawable * wld_import(struct wld_context * context, uint32_t type,
+ union wld_object object,
+ uint32_t width, uint32_t height,
+ uint32_t format, uint32_t pitch);
+
+void wld_destroy_context(struct wld_context * context);
+
/**** Font Handling ****/
struct wld_extents