aboutsummaryrefslogtreecommitdiff
path: root/test/canvas.c
blob: 6186c9aa56bc86eb3b49d7d22124437c45f0852f (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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
/*
 * 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 <stdio.h>
#include <stdatomic.h>
#include <inttypes.h>

#include <lv2/lv2plug.in/ns/ext/patch/patch.h>
#include <lv2/lv2plug.in/ns/ext/log/log.h>
#include <lv2/lv2plug.in/ns/ext/log/logger.h>

#include <canvas.lv2/forge.h>
#include <canvas.lv2/render.h>

#include <lv2_extensions.h>

#include <cairo/cairo.h>

//#define DEBUG
#define MAX_GRAPH_BUF 0x10000

typedef struct _plughandle_t plughandle_t;

struct _plughandle_t {
	LV2_URID_Map *map;
	LV2_Atom_Forge forge;
	LV2_Atom_Forge forge_through;

	LV2_Log_Log *log;
	LV2_Log_Logger logger;

	const LV2_Atom_Sequence *control;
	LV2_Atom_Sequence *notify;

	LV2_URID patch_Get;
	LV2_URID patch_Set;
	LV2_URID patch_Put;
	LV2_URID patch_property;
	LV2_URID patch_value;
	LV2_URID patch_body;

	atomic_flag lock;

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

	LV2_Canvas canvas;
	bool dirty;

	float aspect_ratio;
	union {
		LV2_Atom_Tuple graph;
		uint8_t buf [MAX_GRAPH_BUF];
	};
};

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(atomic_flag *lock)
{
	atomic_flag_clear_explicit(lock, memory_order_release);
}

static const float lv2_L [] = {
	0.05, 0.275,
	0.05, 0.73463521816969,
	0.39996786383766, 0.73463521816969,
	0.35805418792799, 0.61981755929103,
	0.16950515672412, 0.61981755929103,
	0.16950515672412, 0.275,
	0.05, 0.275
};

static const float lv2_V [] = {
	0.44035674587458, 0.73463521816969,
	0.27321237521861, 0.275,
	0.39612954205777, 0.275,
	0.5215250619933, 0.61980400005209,
	0.64678627651808, 0.275,
	0.76999411666921, 0.275,
	0.60269884777111, 0.73463521816969,
	0.44035674587458, 0.73463521816969
};

static inline LV2_Atom_Forge_Ref
_lv2_logo_forge(LV2_Atom_Forge *forge, LV2_Canvas_URID *urid)
{
	const uint32_t fg = 0xbb6600ff;
	LV2_Atom_Forge_Ref ref;

	if(  (ref = lv2_canvas_forge_beginPath(forge, urid))
		&& (ref = lv2_canvas_forge_polyLine(forge, urid, sizeof(lv2_L)/sizeof(float), lv2_L))
		&& (ref = lv2_canvas_forge_closePath(forge, urid))
		&& (ref = lv2_canvas_forge_style(forge, urid, fg))
		&& (ref = lv2_canvas_forge_stroke(forge, urid))

		&& (ref = lv2_canvas_forge_beginPath(forge, urid))
		&& (ref = lv2_canvas_forge_polyLine(forge, urid, sizeof(lv2_L)/sizeof(float), lv2_V))
		&& (ref = lv2_canvas_forge_closePath(forge, urid))
		&& (ref = lv2_canvas_forge_style(forge, urid, fg))
		&& (ref = lv2_canvas_forge_stroke(forge, urid))

		&& (ref = lv2_canvas_forge_beginPath(forge, urid))
		&& (ref = lv2_canvas_forge_moveTo(forge, urid, 0.92679577564592, 0.33745757758451))
		&& (ref = lv2_canvas_forge_curveTo(forge, urid, 0.95, 0.37544661222032, 0.9486097413556,
			0.42890103900541, 0.91866073788306, 0.46581025262318))
		&& (ref = lv2_canvas_forge_curveTo(forge, urid, 0.87662774067075, 0.51761178520021,
			0.84865149155459, 0.52351773004551, 0.8188709443895, 0.55088574387747))
		&& (ref = lv2_canvas_forge_lineTo(forge, urid, 0.93798338878322, 0.55088574387747))
		&& (ref = lv2_canvas_forge_lineTo(forge, urid, 0.93798338878322, 0.61972641362727))
		&& (ref = lv2_canvas_forge_lineTo(forge, urid, 0.68857649440815, 0.61972641362727))
		&& (ref = lv2_canvas_forge_curveTo(forge, urid, 0.70410821191941, 0.57897193773781,
			0.71568706655441, 0.55649255812279, 0.73505227967577, 0.53436493734023))
		&& (ref = lv2_canvas_forge_curveTo(forge, urid, 0.78431409785481, 0.47807598612821,
			0.88073913173375, 0.44149338929647, 0.87483180798279, 0.39074363998918))
		&& (ref = lv2_canvas_forge_curveTo(forge, urid, 0.8731729385169, 0.37649219041461,
			0.86900905711197, 0.34385128732334, 0.80655313421425, 0.34385128732334))
		&& (ref = lv2_canvas_forge_lineTo(forge, urid, 0.7834998081023, 0.34385128732334))
		&& (ref = lv2_canvas_forge_lineTo(forge, urid, 0.80849192152801, 0.275))
		&& (ref = lv2_canvas_forge_curveTo(forge, urid, 0.88098903540187, 0.275,
			0.90879494370618, 0.30798728419169, 0.92679577564592, 0.33745757758451))
		&& (ref = lv2_canvas_forge_style(forge, urid, fg))
		&& (ref = lv2_canvas_forge_stroke(forge, urid)) )
		{
			return ref;
		}

	return 0;
}

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_INLINEDISPLAY__queue_draw))
			handle->queue_draw = features[i]->data;
		else if(!strcmp(features[i]->URI, LV2_LOG__log))
			handle->log = 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);

	handle->patch_Get = handle->map->map(handle->map->handle, LV2_PATCH__Get);
	handle->patch_Set = handle->map->map(handle->map->handle, LV2_PATCH__Set);
	handle->patch_Put = handle->map->map(handle->map->handle, LV2_PATCH__Put);
	handle->patch_property = handle->map->map(handle->map->handle, LV2_PATCH__property);
	handle->patch_value = handle->map->map(handle->map->handle, LV2_PATCH__value);
	handle->patch_body = handle->map->map(handle->map->handle, LV2_PATCH__body);

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

	handle->lock = (atomic_flag)ATOMIC_FLAG_INIT;

	lv2_canvas_init(&handle->canvas, handle->map);

	handle->aspect_ratio = 1.f;

	LV2_Atom_Forge_Frame frame;
	lv2_atom_forge_set_buffer(&handle->forge, handle->buf, MAX_GRAPH_BUF);
	LV2_Atom_Forge_Ref ref = lv2_atom_forge_tuple(&handle->forge, &frame);
	if(ref)
		ref = _lv2_logo_forge(&handle->forge, &handle->canvas.urid);
	if(ref)
		lv2_atom_forge_pop(&handle->forge, &frame);

	handle->dirty = true;

	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 LV2_Atom_Forge_Ref
_forge_graph(plughandle_t *handle, int64_t frames)
{
	const uint32_t szg = lv2_atom_total_size(&handle->graph.atom);

	LV2_Atom_Forge_Frame obj_frame;
	LV2_Atom_Forge_Ref ref = lv2_atom_forge_frame_time(&handle->forge, frames);
	if(ref)
		ref = lv2_atom_forge_object(&handle->forge, &obj_frame, 0, handle->patch_Set);
	if(ref)
		ref = lv2_atom_forge_key(&handle->forge, handle->patch_property);
	if(ref)
		ref = lv2_atom_forge_urid(&handle->forge, handle->canvas.urid.Canvas_graph);
	if(ref)
		ref = lv2_atom_forge_key(&handle->forge, handle->patch_value);
	if(ref)
		ref = lv2_atom_forge_write(&handle->forge, &handle->graph, szg);
	if(ref)
		lv2_atom_forge_pop(&handle->forge, &obj_frame);

	return ref;
}

static LV2_Atom_Forge_Ref
_forge_aspect(plughandle_t *handle, int64_t frames)
{
	LV2_Atom_Forge_Frame obj_frame;
	LV2_Atom_Forge_Ref ref = lv2_atom_forge_frame_time(&handle->forge, frames);
	if(ref)
		ref = lv2_atom_forge_object(&handle->forge, &obj_frame, 0, handle->patch_Set);
	if(ref)
		ref = lv2_atom_forge_key(&handle->forge, handle->patch_property);
	if(ref)
		ref = lv2_atom_forge_urid(&handle->forge, handle->canvas.urid.Canvas_aspectRatio);
	if(ref)
		ref = lv2_atom_forge_key(&handle->forge, handle->patch_value);
	if(ref)
		ref = lv2_atom_forge_float(&handle->forge, handle->aspect_ratio);
	if(ref)
		lv2_atom_forge_pop(&handle->forge, &obj_frame);

	return ref;
}

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

	const uint32_t capacity = handle->notify->atom.size;
	lv2_atom_forge_set_buffer(&handle->forge, (uint8_t *)handle->notify, capacity);
	LV2_Atom_Forge_Frame frame;
	LV2_Atom_Forge_Ref ref = lv2_atom_forge_sequence_head(&handle->forge, &frame, 0);

	LV2_ATOM_SEQUENCE_FOREACH(handle->control, ev)
	{
		const LV2_Atom_Object *obj = (const LV2_Atom_Object *)&ev->body;
		const uint32_t szo = lv2_atom_total_size(&obj->atom);
		bool route = false;

		if(lv2_atom_forge_is_object_type(&handle->forge, obj->atom.type))
		{
			if(obj->body.otype == handle->patch_Get)
			{
				const LV2_Atom_URID *property = NULL;

				lv2_atom_object_get(obj, handle->patch_property, &property,
					0);

				if(!property)
				{
					if(ref)
						ref = _forge_aspect(handle, ev->time.frames);
					if(ref)
						ref = _forge_graph(handle, ev->time.frames);
				}
				if(  property
					&& (property->atom.type == handle->forge.URID)
					&& (property->body == handle->canvas.urid.Canvas_graph) )
				{
					if(ref)
						ref = _forge_graph(handle, ev->time.frames);
				}
				else if(  property
					&& (property->atom.type == handle->forge.URID)
					&& (property->body == handle->canvas.urid.Canvas_aspectRatio) )
				{
					if(ref)
						ref = _forge_aspect(handle, ev->time.frames);
				}
			}
			else if(obj->body.otype == handle->patch_Set)
			{
				const LV2_Atom_URID *property = NULL;
				const LV2_Atom *value = NULL;

				lv2_atom_object_get(obj, handle->patch_property, &property,
					handle->patch_value, &value,
					0);

				if(  property
					&& (property->atom.type == handle->forge.URID)
					&& value)
				{
					if(  (property->body == handle->canvas.urid.Canvas_graph)
						&& (value->type == handle->forge.Tuple) )
					{
						const LV2_Atom_Tuple *graph = (const LV2_Atom_Tuple *)value;
						const uint32_t szg = lv2_atom_total_size(&graph->atom);

						if(  (szg <= MAX_GRAPH_BUF)
							&&  _try_lock(&handle->lock) )
						{
							memcpy(&handle->graph, graph, szg);

							_unlock(&handle->lock);

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

						route = true;
					}
					else if(  (property->body == handle->canvas.urid.Canvas_aspectRatio)
						&& (value->type == handle->forge.Float) )
					{
						if( _try_lock(&handle->lock) )
						{
							handle->aspect_ratio = ((const LV2_Atom_Float *)value)->body;

							_unlock(&handle->lock);

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

						route = true;
					}
					else if( (property->body == handle->canvas.urid.Canvas_mouseButtonLeft)
						&& (value->type == handle->forge.Bool) )
					{
#ifdef DEBUG
						if(handle->log)
						{
							lv2_log_trace(&handle->logger, "\tcanvas:mouseButtonLeft: %"PRIi32"\n",
								((const LV2_Atom_Bool *)value)->body);
						}
#endif

						route = true;
					}
					else if( (property->body == handle->canvas.urid.Canvas_mouseButtonMiddle)
						&& (value->type == handle->forge.Bool) )
					{
#ifdef DEBUG
						if(handle->log)
						{
							lv2_log_trace(&handle->logger, "\tcanvas:mouseButtonMiddle: %"PRIi32"\n",
								((const LV2_Atom_Bool *)value)->body);
						}
#endif

						route = true;
					}
					else if( (property->body == handle->canvas.urid.Canvas_mouseButtonRight)
						&& (value->type == handle->forge.Bool) )
					{
#ifdef DEBUG
						if(handle->log)
						{
							lv2_log_trace(&handle->logger, "\tcanvas:mouseButtonRight: %"PRIi32"\n",
								((const LV2_Atom_Bool *)value)->body);
						}
#endif

						route = true;
					}
					else if( (property->body == handle->canvas.urid.Canvas_mousePositionX)
						&& (value->type == handle->forge.Double) )
					{
#ifdef DEBUG
						if(handle->log)
						{
							lv2_log_trace(&handle->logger, "\tcanvas:mousePositionX: %lf\n",
								((const LV2_Atom_Double *)value)->body);
						}
#endif

						route = true;
					}
					else if( (property->body == handle->canvas.urid.Canvas_mousePositionY)
						&& (value->type == handle->forge.Double) )
					{
#ifdef DEBUG
						if(handle->log)
						{
							lv2_log_trace(&handle->logger, "\tcanvas:mousePositionY: %lf\n",
								((const LV2_Atom_Double *)value)->body);
						}
#endif

						route = true;
					}
					else if( (property->body == handle->canvas.urid.Canvas_mouseWheelX)
						&& (value->type == handle->forge.Double) )
					{
#ifdef DEBUG
						if(handle->log)
						{
							lv2_log_trace(&handle->logger, "\tcanvas:mouseWheelX: %lf\n",
								((const LV2_Atom_Double *)value)->body);
						}
#endif

						route = true;
					}
					else if( (property->body == handle->canvas.urid.Canvas_mouseWheelY)
						&& (value->type == handle->forge.Double) )
					{
#ifdef DEBUG
						if(handle->log)
						{
							lv2_log_trace(&handle->logger, "\tcanvas:mouseWheelY: %lf\n",
								((const LV2_Atom_Double *)value)->body);
						}
#endif

						route = true;
					}
					else if( (property->body == handle->canvas.urid.Canvas_mouseFocus)
						&& (value->type == handle->forge.Bool) )
					{
#ifdef DEBUG
						if(handle->log)
						{
							lv2_log_trace(&handle->logger, "\tcanvas:mouseFocus: %"PRIi32"\n",
								((const LV2_Atom_Bool *)value)->body);
						}
#endif

						route = true;
					}
				}
			}
			else if(obj->body.otype == handle->patch_Put)
			{
				const LV2_Atom_Object *body = NULL;

				lv2_atom_object_get(obj, handle->patch_body, &body,
					0);

				if(  body
					&& lv2_atom_forge_is_object_type(&handle->forge, body->atom.type) )
				{
					const LV2_Atom_Bool *l = NULL;
					const LV2_Atom_Bool *m = NULL;
					const LV2_Atom_Bool *r = NULL;
					const LV2_Atom_Double *dx = NULL;
					const LV2_Atom_Double *dy = NULL;
					const LV2_Atom_Double *x = NULL;
					const LV2_Atom_Double *y = NULL;
					const LV2_Atom_Bool *f = NULL;

					lv2_atom_object_get(body,
						handle->canvas.urid.Canvas_mouseButtonLeft, &l,
						handle->canvas.urid.Canvas_mouseButtonMiddle, &m,
						handle->canvas.urid.Canvas_mouseButtonRight, &r,
						handle->canvas.urid.Canvas_mouseWheelX, &dx,
						handle->canvas.urid.Canvas_mouseWheelY, &dy,
						handle->canvas.urid.Canvas_mousePositionX, &x,
						handle->canvas.urid.Canvas_mousePositionY, &y,
						handle->canvas.urid.Canvas_mouseFocus, &f,
						0);

#ifdef DEBUG
					if(handle->log)
					{
						lv2_log_trace(&handle->logger, "{\n");
						if(l && (l->atom.type == handle->forge.Bool) )
							lv2_log_trace(&handle->logger, "\tcanvas:mousebuttonLeft: %"PRIi32"\n", l->body);
						if(m && (m->atom.type == handle->forge.Bool) )
							lv2_log_trace(&handle->logger, "\tcanvas:mousebuttonMiddle: %"PRIi32"\n", m->body);
						if(r && (r->atom.type == handle->forge.Bool) )
							lv2_log_trace(&handle->logger, "\tcanvas:mousebuttonRight: %"PRIi32"\n", r->body);
						if(x && (x->atom.type == handle->forge.Double) )
							lv2_log_trace(&handle->logger, "\tcanvas:mousePositionX: %lf\n", x->body);
						if(y && (y->atom.type == handle->forge.Double) )
							lv2_log_trace(&handle->logger, "\tcanvas:mousePositionY: %lf\n", y->body);
						if(dx && (dx->atom.type == handle->forge.Double) )
							lv2_log_trace(&handle->logger, "\tcanvas:mouseWheelX: %lf\n", dx->body);
						if(dy && (dy->atom.type == handle->forge.Double) )
							lv2_log_trace(&handle->logger, "\tcanvas:mouseWheelY: %lf\n", dy->body);
						if(f && (f->atom.type == handle->forge.Bool) )
							lv2_log_trace(&handle->logger, "\tcanvas:mouseFocus: %"PRIi32"\n", f->body);
						lv2_log_trace(&handle->logger, "}\n");
					}
#endif

					if(  x && (x->atom.type == handle->forge.Double)
						&& y && (y->atom.type == handle->forge.Double) )
						route = true; // a valid input event at least sets x and y
				}
			}
		}

		if(route) // should we route to UI?
		{
			if(ref)
				ref = lv2_atom_forge_frame_time(&handle->forge, ev->time.frames);
			if(ref)
				ref = lv2_atom_forge_write(&handle->forge, obj, szo);
		}
	}

	if(handle->dirty)
	{
		if(handle->queue_draw)
			handle->queue_draw->queue_draw(handle->queue_draw->handle);

		if(ref)
			ref = _forge_aspect(handle, nsamples-1);
		if(ref)
			ref = _forge_graph(handle, nsamples-1);

		handle->dirty = false;
	}

	if(ref)
		lv2_atom_forge_pop(&handle->forge, &frame);
	else
		lv2_atom_sequence_clear(handle->notify);
}

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;

	int W;
	int H;

	if(handle->aspect_ratio == 0.f)
	{
		W = w;
		H = h;
	}
	else if(handle->aspect_ratio <= 1.f)
	{
		W = h * handle->aspect_ratio;
		H = h;
	}
	else if(handle->aspect_ratio > 1.f)
	{
		W = w;
		H = w / handle->aspect_ratio;
	}

	if( (surf->width != W) || (surf->height != H) || !surf->data)
	{
		_cairo_deinit(handle);
		surf = _cairo_init(handle, W, H);
	}

	if(!surf)
		return NULL;

	_spin_lock(&handle->lock);

	lv2_canvas_render(&handle->canvas, handle->cairo.ctx, &handle->graph);

	_unlock(&handle->lock);

	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);

	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_PREFIX"canvas",
	.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;
	}
}