-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Shape: Offscreen alpha mask with render callback
Wrap rendering of alpha shapes in a class caching the picture instances by comparing the render function and its data. Use this in EmojiButton to avoid countless instances of the same triangle picture.
- Loading branch information
Showing
4 changed files
with
167 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
#include "shape.h" | ||
|
||
#include "x11adapter.h" | ||
|
||
#include <poser/core.h> | ||
#include <stdlib.h> | ||
#include <string.h> | ||
|
||
struct Shape | ||
{ | ||
ShapeRenderer renderer; | ||
const void *data; | ||
size_t datasz; | ||
xcb_render_picture_t picture; | ||
unsigned refcnt; | ||
}; | ||
|
||
static PSC_List *shapes; | ||
|
||
Shape *Shape_create(ShapeRenderer renderer, size_t datasz, const void *data) | ||
{ | ||
if (!shapes) shapes = PSC_List_create(); | ||
for (size_t i = 0; i < PSC_List_size(shapes); ++i) | ||
{ | ||
Shape *shape = PSC_List_at(shapes, i); | ||
if (shape->renderer == renderer && shape->datasz == datasz | ||
&& !memcmp(shape->data, data, datasz)) | ||
{ | ||
++shape->refcnt; | ||
return shape; | ||
} | ||
} | ||
Shape *self = PSC_malloc(sizeof *self); | ||
self->renderer = renderer; | ||
self->data = data; | ||
self->datasz = datasz; | ||
self->picture = 0; | ||
self->refcnt = 1; | ||
PSC_List_append(shapes, self, 0); | ||
return self; | ||
} | ||
|
||
void Shape_render(Shape *self, xcb_render_picture_t ownerpic) | ||
{ | ||
if (self->picture) return; | ||
self->picture = self->renderer(ownerpic, self->data); | ||
} | ||
|
||
xcb_render_picture_t Shape_picture(const Shape *self) | ||
{ | ||
return self->picture; | ||
} | ||
|
||
void Shape_destroy(Shape *self) | ||
{ | ||
if (!self) return; | ||
if (--self->refcnt) return; | ||
PSC_List_remove(shapes, self); | ||
if (!PSC_List_size(shapes)) | ||
{ | ||
PSC_List_destroy(shapes); | ||
shapes = 0; | ||
} | ||
if (self->picture) | ||
{ | ||
xcb_render_free_picture(X11Adapter_connection(), self->picture); | ||
} | ||
free(self); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
#ifndef XMOJI_SHAPE_H | ||
#define XMOJI_SHAPE_H | ||
|
||
#include <poser/decl.h> | ||
#include <xcb/render.h> | ||
|
||
C_CLASS_DECL(Shape); | ||
|
||
typedef xcb_render_picture_t (*ShapeRenderer)( | ||
xcb_render_picture_t ownerpic, const void *data); | ||
|
||
Shape *Shape_create(ShapeRenderer renderer, | ||
size_t datasz, const void *data) ATTR_RETNONNULL; | ||
void Shape_render(Shape *self, xcb_render_picture_t ownerpic) CMETHOD; | ||
xcb_render_picture_t Shape_picture(const Shape *self) CMETHOD ATTR_PURE; | ||
void Shape_destroy(Shape *self); | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,6 +21,7 @@ xmoji_MODULES= button \ | |
object \ | ||
pixmap \ | ||
scrollbox \ | ||
shape \ | ||
surface \ | ||
tabbox \ | ||
textbox \ | ||
|