aboutsummaryrefslogtreecommitdiff
path: root/canvas.c
blob: cc3c311f78f9b5d680837913479fcf1096458f48 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
/*
 * Copyright (c) 2016 Hanspeter Portner (dev@open-music-kontrollers.ch)
 *
 * This is free software: you can redistribute it and/or modify
 * it under the terms of the Artistic License 2.0 as published by
 * The Perl Foundation.
 *
 * This source is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * Artistic License 2.0 for more details.
 *
 * You should have received a copy of the Artistic License 2.0
 * along the source as a COPYING file. If not, obtain it from
 * http://www.perlfoundation.org/artistic_license_2_0.
 */

#include <stdlib.h>
#include <stdatomic.h>
#include <math.h>

#include <canvas.h>

#include <lv2_extensions.h>

#include <cairo/cairo.h>

typedef struct _plughandle_t plughandle_t;

struct _plughandle_t {
	LV2_URID_Map *map;
	LV2_Atom_Forge forge;

	LV2_Log_Log *log;
	LV2_Log_Logger logger;

	const LV2_Atom_Sequence *control;
	LV2_Atom_Sequence *notify;

	atomic_flag lock;

	LV2_Inline_Display *queue_draw;
	LV2_Inline_Display_Image_Surface image_surface;
	struct {
		cairo_surface_t *surface;
		cairo_t *ctx;
	} cairo;

	union {
		LV2_Atom_Tuple tup;
		uint8_t buf [0x10000];
	};
};

static inline void
_spin_lock(atomic_flag *lock)
{
	while(atomic_flag_test_and_set_explicit(lock, memory_order_acquire))
	{
		// spin
	}
}

static inline bool
_try_lock(atomic_flag *lock)
{
	return atomic_flag_test_and_set_explicit(lock, memory_order_acquire);
}

static inline void
_unlock_lock(atomic_flag *lock)
{
	atomic_flag_clear_explicit(lock, memory_order_release);
}

static inline void
_init_lock(atomic_flag *lock)
{
	atomic_flag_clear_explicit(lock, memory_order_relaxed);
}

static LV2_Handle
instantiate(const LV2_Descriptor* descriptor, double rate,
	const char *bundle_path, const LV2_Feature *const *features)
{
	plughandle_t *handle = calloc(1, sizeof(plughandle_t));
	if(!handle)
		return NULL;

	for(unsigned i=0; features[i]; i++)
	{
		if(!strcmp(features[i]->URI, LV2_URID__map))
			handle->map = features[i]->data;
		else if(!strcmp(features[i]->URI, LV2_LOG__log))
			handle->log = features[i]->data;
		else if(!strcmp(features[i]->URI, LV2_INLINEDISPLAY__queue_draw))
			handle->queue_draw = features[i]->data;
	}

	if(!handle->map)
	{
		fprintf(stderr,
			"%s: Host does not support urid:map\n", descriptor->URI);
		free(handle);
		return NULL;
	}

	if(handle->log)
		lv2_log_logger_init(&handle->logger, handle->map, handle->log);

	lv2_atom_forge_init(&handle->forge, handle->map);

	handle->tup.atom.size = 0;
	handle->tup.atom.type = handle->forge.Tuple;

	_init_lock(&handle->lock);

	return handle;
}

static void
connect_port(LV2_Handle instance, uint32_t port, void *data)
{
	plughandle_t *handle = instance;

	switch(port)
	{
		case 0:
			handle->control = data;
			break;
		case 1:
			handle->notify = data;
			break;
		default:
			break;
	}
}

static void
run(LV2_Handle instance, uint32_t nsamples)
{
	plughandle_t *handle = instance;

	const uint32_t sz = lv2_atom_total_size(&handle->control->atom);
	memcpy(handle->notify, handle->control, sz);

	if(_try_lock(&handle->lock))
	{
		//FIXME

		_unlock_lock(&handle->lock);

		if(handle->queue_draw)
			handle->queue_draw->queue_draw(handle->queue_draw->handle);
	}
}

static inline LV2_Inline_Display_Image_Surface *
_cairo_init(plughandle_t *handle, int w, int h)
{
	LV2_Inline_Display_Image_Surface *surf = &handle->image_surface;

	surf->width = w;
	surf->height = w > h ? h : w; // try to use 1:1 ratio
	surf->stride = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, surf->width);
	surf->data = realloc(surf->data, surf->stride * surf->height);
	if(!surf->data)
		return NULL;

	handle->cairo.surface = cairo_image_surface_create_for_data(
		surf->data, CAIRO_FORMAT_ARGB32, surf->width, surf->height, surf->stride);

	if(handle->cairo.surface)
	{
		cairo_surface_set_device_scale(handle->cairo.surface, surf->width, surf->height);

		handle->cairo.ctx = cairo_create(handle->cairo.surface);
		if(handle->cairo.ctx)
		{
			cairo_select_font_face(handle->cairo.ctx, "cairo:monospace", CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_NORMAL);
		}
	}

	return surf;
}

static inline void
_cairo_deinit(plughandle_t *handle)
{
	LV2_Inline_Display_Image_Surface *surf = &handle->image_surface;

	if(handle->cairo.ctx)
	{
		cairo_destroy(handle->cairo.ctx);
		handle->cairo.ctx = NULL;
	}

	if(handle->cairo.surface)
	{
		cairo_surface_finish(handle->cairo.surface);
		cairo_surface_destroy(handle->cairo.surface);
		handle->cairo.surface = NULL;
	}

	if(surf->data)
	{
		free(surf->data);
		surf->data = NULL;
	}
}

// non-rt
static LV2_Inline_Display_Image_Surface *
_render(LV2_Handle instance, uint32_t w, uint32_t h)
{
	plughandle_t *handle = instance;
	LV2_Inline_Display_Image_Surface *surf = &handle->image_surface;

	if( (surf->width != (int)w) || (surf->height > (int)h) || !surf->data)
	{
		_cairo_deinit(handle);
		surf = _cairo_init(handle, w, h);
	}

	if(!surf)
		return NULL;

	// save state
	cairo_save(handle->cairo.ctx);

	// clear surface
	cairo_set_operator(handle->cairo.ctx, CAIRO_OPERATOR_CLEAR);
	cairo_paint(handle->cairo.ctx);

	// default attributes
	cairo_set_operator(handle->cairo.ctx, CAIRO_OPERATOR_SOURCE);
	cairo_set_font_size(handle->cairo.ctx, 0.1);
	cairo_set_line_width(handle->cairo.ctx, 0.01);
	cairo_set_source_rgba(handle->cairo.ctx, 1.0, 1.0, 1.0, 1.0);

	_spin_lock(&handle->lock);

	LV2_ATOM_TUPLE_FOREACH(&handle->tup, itm)
	{
		if(lv2_atom_forge_is_object_type(&handle->forge, itm->type))
			;//_render_cmd(handle, handle->cairo.ctx, (const LV2_Atom_Object *)itm);
	}

	_unlock_lock(&handle->lock);

	//FIXME
	{
		cairo_translate(handle->cairo.ctx, 0.5, 0.5);
		cairo_rotate(handle->cairo.ctx, M_PI/4);
		cairo_rectangle(handle->cairo.ctx, -0.3, -0.3, 0.6, 0.6);
		cairo_set_source_rgba(handle->cairo.ctx, 0.8, 0.8, 0.0, 1.0);
		cairo_fill(handle->cairo.ctx);
	}

	// save state
	cairo_restore(handle->cairo.ctx);

	// flush
	cairo_surface_flush(handle->cairo.surface);

	return surf;
}

static const LV2_Inline_Display_Interface idisp_iface = {
	.render = _render
};

static void
cleanup(LV2_Handle instance)
{
	plughandle_t *handle = instance;

	_cairo_deinit(handle);

	if(handle->image_surface.data)
		free(handle->image_surface.data);
	handle->image_surface.data = NULL;

	free(handle);
}

static const void *
extension_data(const char *uri)
{
	if(!strcmp(uri, LV2_INLINEDISPLAY__interface))
		return &idisp_iface;

	return NULL;
}

const LV2_Descriptor canvas_canvas = {
	.URI            = CANVAS_CANVAS_URI,
	.instantiate    = instantiate,
	.connect_port   = connect_port,
	.activate       = NULL,
	.run            = run,
	.deactivate     = NULL,
	.cleanup        = cleanup,
	.extension_data = extension_data
};

LV2_SYMBOL_EXPORT const LV2_Descriptor*
lv2_descriptor(uint32_t index)
{
	switch(index)
	{
		case 0:
			return &canvas_canvas;
		default:
			return NULL;
	}
}