 |
ThorVG
v0.8
|
A module for managing and drawing graphical elements.
More...
|
| SwCanvas |
| A module for rendering the graphical elements using the software engine.
|
|
A module for managing and drawing graphical elements.
A canvas is an entity responsible for drawing the target. It sets up the drawing engine and the buffer, which can be drawn on the screen. It also manages given Paint objects.
- Note
- A Canvas behavior depends on the raster engine though the final content of the buffer is expected to be identical.
- Warning
- The Paint objects belonging to one Canvas can't be shared among multiple Canvases.
◆ tvg_canvas_clear()
Sets the total number of the paints pushed into the canvas to be zero. Tvg_Paint objects stored in the canvas are released if free
is set to true
, otherwise the memory is not deallocated and all paints should be released manually in order to avoid memory leaks.
- Parameters
-
[in] | canvas | The Tvg_Canvas object to be cleared. |
[in] | free | If true the memory occupied by paints is deallocated, otherwise it is not. |
- Returns
- Tvg_Result enumeration.
- Return values
-
TVG_RESULT_SUCCESS | Succeed. |
TVG_RESULT_INVALID_ARGUMENT | An invalid Tvg_Canvas pointer. |
TVG_RESULT_INSUFFICIENT_CONDITION | An internal error. |
- Warning
- Please use the
free
argument only when you know how it works, otherwise it's not recommended.
- See also
- tvg_canvas_destroy()
◆ tvg_canvas_destroy()
Clears the canvas internal data, releases all paints stored by the canvas and destroys the canvas object itself.
static uint32_t *buffer = NULL;
static void _init() {
buffer = (uint32_t*) malloc(sizeof(uint32_t) * 100 * 100);
}
static void _job(const int cmd) {
switch (cmd) {
case CMD_EXIT: return 0;
case CMD_ADD_RECT:
break;
case CMD_DEL_RECT:
break;
default:
break;
}
}
int main(int argc, char **argv) {
int cmd = 0;
int stop = 1;
while (stop) {
stop = _job(cmd);
}
return 0;
}
- Parameters
-
[in] | canvas | The Tvg_Canvas object to be destroyed. |
- Returns
- Tvg_Result enumeration.
- Return values
-
TVG_RESULT_SUCCESS | Succeed. |
TVG_RESULT_INVALID_ARGUMENT | An invalid pointer to the Tvg_Canvas object is passed. |
- Note
- If the paints from the canvas should not be released, the tvg_canvas_clear() with a
free
argument value set to false
should be called. Please be aware that in such a case TVG is not responsible for the paints release anymore and it has to be done manually in order to avoid memory leaks.
- See also
- tvg_paint_del(), tvg_canvas_clear()
◆ tvg_canvas_draw()
Requests the canvas to draw the Tvg_Paint objects.
All paints from the given canvas will be rasterized to the buffer.
- Parameters
-
[in] | canvas | The Tvg_Canvas object containing elements to be drawn. |
- Returns
- Tvg_Result enumeration.
- Return values
-
TVG_RESULT_SUCCESS | Succeed. |
TVG_RESULT_INVALID_ARGUMENT | An invalid Tvg_Canvas pointer. |
TVG_RESULT_INSUFFICIENT_CONDITION | An internal error. |
- Note
- Drawing can be asynchronous based on the assigned thread number. To guarantee the drawing is done, call tvg_canvas_sync() afterwards.
- See also
- tvg_canvas_sync()
◆ tvg_canvas_push()
Inserts a drawing element into the canvas using a Tvg_Paint object.
- Parameters
-
[in] | canvas | The Tvg_Canvas object managing the paint . |
[in] | paint | The Tvg_Paint object to be drawn. |
Only the paints pushed into the canvas will be drawing targets. They are retained by the canvas until you call tvg_canvas_clear(). If you know the number of the pushed objects in advance, please call tvg_canvas_reserve().
- Returns
- Tvg_Result return values:
- Return values
-
TVG_RESULT_SUCCESS | Succeed. |
TVG_RESULT_INVALID_ARGUMENT | In case a nullptr is passed as the argument. |
TVG_RESULT_INSUFFICIENT_CONDITION | An internal error. |
- Note
- The rendering order of the paints is the same as the order as they were pushed. Consider sorting the paints before pushing them if you intend to use layering.
- See also
- tvg_canvas_reserve(), tvg_canvas_clear()
◆ tvg_canvas_reserve()
Reserves a memory block where the objects pushed into a canvas are stored.
If the number of Tvg_Paints to be stored in a canvas is known in advance, calling this function reduces the multiple memory allocations thus improves the performance.
uint32_t *buffer = NULL;
buffer = (uint32_t*) malloc(sizeof(uint32_t) * 100 * 100);
if (!buffer) return;
- Parameters
-
[in] | canvas | The Tvg_Canvas object managing the reserved memory. |
[in] | n | The number of objects for which the memory is to be reserved. |
- Returns
- Tvg_Result enumeration.
- Return values
-
TVG_RESULT_SUCCESS | Succeed. |
TVG_RESULT_INVALID_ARGUMENT | An invalid Tvg_Canvas pointer. |
TVG_RESULT_FAILED_ALLOCATION | An internal error with memory allocation. |
◆ tvg_canvas_sync()
Guarantees that the drawing process is finished.
Since the canvas rendering can be performed asynchronously, it should be called after the tvg_canvas_draw().
- Parameters
-
[in] | canvas | The Tvg_Canvas object containing elements which were drawn. |
- Returns
- Tvg_Result enumeration.
- Return values
-
TVG_RESULT_SUCCESS | Succeed. |
TVG_RESULT_INVALID_ARGUMENT | An invalid Tvg_Canvas pointer. |
TVG_RESULT_INSUFFICIENT_CONDITION | An internal error. |
- See also
- tvg_canvas_draw()
◆ tvg_canvas_update()
Updates all paints in a canvas.
Should be called before drawing in order to prepare paints for the rendering.
int _frame_render(void) {
}
void _event_handler(event *event_data) {
if (!event_data) return NULL;
switch(event_data.type) {
case EVENT_RECT_ADD:
if (!rect) {
}
break;
case EVENT_RECT_MOVE:
break;
default:
break;
}
}
int main(int argc, char **argv) {
event_handler_add(handler, _event_handler);
app_loop_begin(_frame_render);
app_loop_finish();
cleanup();
}
- Parameters
-
[in] | canvas | The Tvg_Canvas object to be updated. |
- Returns
- Tvg_Result enumeration.
- Return values
-
TVG_RESULT_SUCCESS | Succeed. |
TVG_RESULT_INVALID_ARGUMENT | An invalid Tvg_Canvas pointer. |
TVG_RESULT_INSUFFICIENT_CONDITION | An internal error. |
- See also
- tvg_canvas_update_paint()
◆ tvg_canvas_update_paint()
Updates the given Tvg_Paint object from the canvas before the rendering.
If a client application using the TVG library does not update the entire canvas with tvg_canvas_update() in the frame rendering process, Tvg_Paint objects previously added to the canvas should be updated manually with this function.
- Parameters
-
[in] | canvas | The Tvg_Canvas object to which the paint belongs. |
[in] | paint | The Tvg_Paint object to be updated. |
- Returns
- Tvg_Result enumeration.
- Return values
-
TVG_RESULT_SUCCESS | Succeed. |
TVG_RESULT_INVALID_ARGUMENT | In case a nullptr is passed as the argument. |
- See also
- tvg_canvas_update()
TVG_EXPORT Tvg_Result tvg_shape_append_rect(Tvg_Paint *paint, float x, float y, float w, float h, float rx, float ry)
Appends a rectangle to the path.
TVG_EXPORT Tvg_Result tvg_canvas_push(Tvg_Canvas *canvas, Tvg_Paint *paint)
Inserts a drawing element into the canvas using a Tvg_Paint object.
TVG_EXPORT Tvg_Result tvg_swcanvas_set_target(Tvg_Canvas *canvas, uint32_t *buffer, uint32_t stride, uint32_t w, uint32_t h, Tvg_Colorspace cs)
Sets the buffer used in the rasterization process and defines the used colorspace.
TVG_EXPORT Tvg_Result tvg_canvas_sync(Tvg_Canvas *canvas)
Guarantees that the drawing process is finished.
TVG_EXPORT Tvg_Result tvg_canvas_clear(Tvg_Canvas *canvas, bool free)
Sets the total number of the paints pushed into the canvas to be zero. Tvg_Paint objects stored in th...
@ TVG_ENGINE_SW
CPU rasterizer.
Definition: thorvg_capi.h:84
TVG_EXPORT Tvg_Result tvg_paint_del(Tvg_Paint *paint)
Releases the given Tvg_Paint object.
struct _Tvg_Canvas Tvg_Canvas
A structure responsible for managing and drawing graphical elements.
Definition: thorvg_capi.h:55
TVG_EXPORT Tvg_Result tvg_canvas_update(Tvg_Canvas *canvas)
Updates all paints in a canvas.
TVG_EXPORT Tvg_Result tvg_canvas_draw(Tvg_Canvas *canvas)
Requests the canvas to draw the Tvg_Paint objects.
TVG_EXPORT Tvg_Result tvg_paint_translate(Tvg_Paint *paint, float x, float y)
Moves the given Tvg_Paint in a two-dimensional space.
TVG_EXPORT Tvg_Canvas * tvg_swcanvas_create()
Creates a Canvas object.
TVG_EXPORT Tvg_Result tvg_engine_init(Tvg_Engine engine_method, unsigned threads)
Initializes TVG engines.
@ TVG_COLORSPACE_ARGB8888
The 8-bit color channels are combined into 32-bit color in the order: alpha, red, green,...
Definition: thorvg_capi.h:332
struct _Tvg_Paint Tvg_Paint
A structure representing a graphical element.
Definition: thorvg_capi.h:63
TVG_EXPORT Tvg_Result tvg_canvas_destroy(Tvg_Canvas *canvas)
Clears the canvas internal data, releases all paints stored by the canvas and destroys the canvas obj...
TVG_EXPORT Tvg_Result tvg_shape_set_stroke_width(Tvg_Paint *paint, float width)
Sets the stroke width for all of the figures from the paint.
TVG_EXPORT Tvg_Result tvg_shape_set_stroke_color(Tvg_Paint *paint, uint8_t r, uint8_t g, uint8_t b, uint8_t a)
Sets the shape's stroke color.
TVG_EXPORT Tvg_Result tvg_engine_term(Tvg_Engine engine_method)
Terminates TVG engines.
TVG_EXPORT Tvg_Result tvg_canvas_reserve(Tvg_Canvas *canvas, uint32_t n)
Reserves a memory block where the objects pushed into a canvas are stored.