summaryrefslogtreecommitdiff
path: root/libs/cairo-1.16.0/src/cairo-cogl-gradient.c
blob: 0923e3b6d953501dca8ff61fe5b8c07ce551d02a (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
/* cairo - a vector graphics library with display and print output
 *
 * Copyright © 2011 Intel Corporation.
 *
 * This library is free software; you can redistribute it and/or
 * modify it either under the terms of the GNU Lesser General Public
 * License version 2.1 as published by the Free Software Foundation
 * (the "LGPL") or, at your option, under the terms of the Mozilla
 * Public License Version 1.1 (the "MPL"). If you do not alter this
 * notice, a recipient may use your version of this file under either
 * the MPL or the LGPL.
 *
 * You should have received a copy of the LGPL along with this library
 * in the file COPYING-LGPL-2.1; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA
 * You should have received a copy of the MPL along with this library
 * in the file COPYING-MPL-1.1
 *
 * The contents of this file are subject to the Mozilla Public License
 * Version 1.1 (the "License"); you may not use this file except in
 * compliance with the License. You may obtain a copy of the License at
 * http://www.mozilla.og/MPL/
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY
 * OF ANY KIND, either express or implied. See the LGPL or the MPL for
 * the specific language governing rights and limitations.
 *
 * Contributor(s):
 *      Robert Bragg <robert@linux.intel.com>
 */
//#include "cairoint.h"

#include "cairo-cogl-private.h"
#include "cairo-cogl-gradient-private.h"
#include "cairo-image-surface-private.h"

#include <cogl/cogl2-experimental.h>
#include <glib.h>

#define DUMP_GRADIENTS_TO_PNG

static unsigned long
_cairo_cogl_linear_gradient_hash (unsigned int                  n_stops,
				  const cairo_gradient_stop_t  *stops)
{
    return _cairo_hash_bytes (n_stops, stops,
                              sizeof (cairo_gradient_stop_t) * n_stops);
}

static cairo_cogl_linear_gradient_t *
_cairo_cogl_linear_gradient_lookup (cairo_cogl_device_t         *ctx,
				    unsigned long                 hash,
				    unsigned int                  n_stops,
				    const cairo_gradient_stop_t  *stops)
{
    cairo_cogl_linear_gradient_t lookup;

    lookup.cache_entry.hash = hash,
    lookup.n_stops = n_stops;
    lookup.stops = stops;

    return _cairo_cache_lookup (&ctx->linear_cache, &lookup.cache_entry);
}

cairo_bool_t
_cairo_cogl_linear_gradient_equal (const void *key_a, const void *key_b)
{
    const cairo_cogl_linear_gradient_t *a = key_a;
    const cairo_cogl_linear_gradient_t *b = key_b;

    if (a->n_stops != b->n_stops)
        return FALSE;

    return memcmp (a->stops, b->stops, a->n_stops * sizeof (cairo_gradient_stop_t)) == 0;
}

cairo_cogl_linear_gradient_t *
_cairo_cogl_linear_gradient_reference (cairo_cogl_linear_gradient_t *gradient)
{
    assert (CAIRO_REFERENCE_COUNT_HAS_REFERENCE (&gradient->ref_count));

    _cairo_reference_count_inc (&gradient->ref_count);

    return gradient;
}

void
_cairo_cogl_linear_gradient_destroy (cairo_cogl_linear_gradient_t *gradient)
{
    GList *l;

    assert (CAIRO_REFERENCE_COUNT_HAS_REFERENCE (&gradient->ref_count));

    if (! _cairo_reference_count_dec_and_test (&gradient->ref_count))
	return;

    for (l = gradient->textures; l; l = l->next) {
	cairo_cogl_linear_texture_entry_t *entry = l->data;
	cogl_object_unref (entry->texture);
	free (entry);
    }
    g_list_free (gradient->textures);

    free (gradient);
}

static int
_cairo_cogl_util_next_p2 (int a)
{
  int rval = 1;

  while (rval < a)
    rval <<= 1;

  return rval;
}

static float
get_max_color_component_range (const cairo_color_stop_t *color0, const cairo_color_stop_t *color1)
{
    float range;
    float max = 0;

    range = fabs (color0->red - color1->red);
    max = MAX (range, max);
    range = fabs (color0->green - color1->green);
    max = MAX (range, max);
    range = fabs (color0->blue - color1->blue);
    max = MAX (range, max);
    range = fabs (color0->alpha - color1->alpha);
    max = MAX (range, max);

    return max;
}

static int
_cairo_cogl_linear_gradient_width_for_stops (cairo_extend_t		  extend,
					     unsigned int                 n_stops,
					     const cairo_gradient_stop_t *stops)
{
    unsigned int n;
    float max_texels_per_unit_offset = 0;
    float total_offset_range;

    /* Find the stop pair demanding the most precision because we are
     * interpolating the largest color-component range.
     *
     * From that we can define the relative sizes of all the other
     * stop pairs within our texture and thus the overall size.
     *
     * To determine the maximum number of texels for a given gap we
     * look at the range of colors we are expected to interpolate (so
     * long as the stop offsets are not degenerate) and we simply
     * assume we want one texel for each unique color value possible
     * for a one byte-per-component representation.
     * XXX: maybe this is overkill and just allowing 128 levels
     * instead of 256 would be enough and then we'd rely on the
     * bilinear filtering to give the full range.
     *
     * XXX: potentially we could try and map offsets to pixels to come
     * up with a more precise mapping, but we are aiming to cache
     * the gradients so we can't make assumptions about how it will be
     * scaled in the future.
     */
    for (n = 1; n < n_stops; n++) {
	float color_range;
	float offset_range;
	float texels;
	float texels_per_unit_offset;

	/* note: degenerate stops don't need to be represented in the
	 * texture but we want to be sure that solid gaps get at least
	 * one texel and all other gaps get at least 2 texels.
	 */

	if (stops[n].offset == stops[n-1].offset)
	    continue;

	color_range = get_max_color_component_range (&stops[n].color, &stops[n-1].color);
	if (color_range == 0)
	    texels = 1;
	else
	    texels = MAX (2, 256.0f * color_range);

	/* So how many texels would we need to map over the full [0,1]
	 * gradient range so this gap would have enough texels? ... */
	offset_range = stops[n].offset - stops[n - 1].offset;
	texels_per_unit_offset = texels / offset_range;

	if (texels_per_unit_offset > max_texels_per_unit_offset)
	    max_texels_per_unit_offset = texels_per_unit_offset;
    }

    total_offset_range = fabs (stops[n_stops - 1].offset - stops[0].offset);
    return max_texels_per_unit_offset * total_offset_range;
}

/* Aim to create gradient textures without an alpha component so we can avoid
 * needing to use blending... */
static CoglPixelFormat
_cairo_cogl_linear_gradient_format_for_stops (cairo_extend_t		   extend,
					      unsigned int                 n_stops,
					      const cairo_gradient_stop_t *stops)
{
    unsigned int n;

    /* We have to add extra transparent texels to the end of the gradient to
     * handle CAIRO_EXTEND_NONE... */
    if (extend == CAIRO_EXTEND_NONE)
	return COGL_PIXEL_FORMAT_BGRA_8888_PRE;

    for (n = 1; n < n_stops; n++) {
	if (stops[n].color.alpha != 1.0)
	    return COGL_PIXEL_FORMAT_BGRA_8888_PRE;
    }

    return COGL_PIXEL_FORMAT_BGR_888;
}

static cairo_cogl_gradient_compatibility_t
_cairo_cogl_compatibility_from_extend_mode (cairo_extend_t extend_mode)
{
    switch (extend_mode)
    {
    case CAIRO_EXTEND_NONE:
	return CAIRO_COGL_GRADIENT_CAN_EXTEND_NONE;
    case CAIRO_EXTEND_PAD:
	return CAIRO_COGL_GRADIENT_CAN_EXTEND_PAD;
    case CAIRO_EXTEND_REPEAT:
	return CAIRO_COGL_GRADIENT_CAN_EXTEND_REPEAT;
    case CAIRO_EXTEND_REFLECT:
	return CAIRO_COGL_GRADIENT_CAN_EXTEND_REFLECT;
    }

    assert (0); /* not reached */
    return CAIRO_EXTEND_NONE;
}

cairo_cogl_linear_texture_entry_t *
_cairo_cogl_linear_gradient_texture_for_extend (cairo_cogl_linear_gradient_t *gradient,
						cairo_extend_t extend_mode)
{
    GList *l;
    cairo_cogl_gradient_compatibility_t compatibility =
	_cairo_cogl_compatibility_from_extend_mode (extend_mode);
    for (l = gradient->textures; l; l = l->next) {
	cairo_cogl_linear_texture_entry_t *entry = l->data;
	if (entry->compatibility & compatibility)
	    return entry;
    }
    return NULL;
}

static void
color_stop_lerp (const cairo_color_stop_t *c0,
		 const cairo_color_stop_t *c1,
		 float factor,
		 cairo_color_stop_t *dest)
{
    /* NB: we always ignore the short members in this file so we don't need to
     * worry about initializing them here. */
    dest->red = c0->red * (1.0f-factor) + c1->red * factor;
    dest->green = c0->green * (1.0f-factor) + c1->green * factor;
    dest->blue = c0->blue * (1.0f-factor) + c1->blue * factor;
    dest->alpha = c0->alpha * (1.0f-factor) + c1->alpha * factor;
}

static size_t
_cairo_cogl_linear_gradient_size (cairo_cogl_linear_gradient_t *gradient)
{
    GList *l;
    size_t size = 0;
    for (l = gradient->textures; l; l = l->next) {
	cairo_cogl_linear_texture_entry_t *entry = l->data;
	size += cogl_texture_get_width (entry->texture) * 4;
    }
    return size;
}

static void
emit_stop (CoglVertexP2C4 **position,
	   float left,
	   float right,
	   const cairo_color_stop_t *left_color,
	   const cairo_color_stop_t *right_color)
{
    CoglVertexP2C4 *p = *position;

    guint8 lr = left_color->red * 255;
    guint8 lg = left_color->green * 255;
    guint8 lb = left_color->blue * 255;
    guint8 la = left_color->alpha * 255;

    guint8 rr = right_color->red * 255;
    guint8 rg = right_color->green * 255;
    guint8 rb = right_color->blue * 255;
    guint8 ra = right_color->alpha * 255;

    p[0].x = left;
    p[0].y = 0;
    p[0].r = lr; p[0].g = lg; p[0].b = lb; p[0].a = la;
    p[1].x = left;
    p[1].y = 1;
    p[1].r = lr; p[1].g = lg; p[1].b = lb; p[1].a = la;
    p[2].x = right;
    p[2].y = 1;
    p[2].r = rr; p[2].g = rg; p[2].b = rb; p[2].a = ra;

    p[3].x = left;
    p[3].y = 0;
    p[3].r = lr; p[3].g = lg; p[3].b = lb; p[3].a = la;
    p[4].x = right;
    p[4].y = 1;
    p[4].r = rr; p[4].g = rg; p[4].b = rb; p[4].a = ra;
    p[5].x = right;
    p[5].y = 0;
    p[5].r = rr; p[5].g = rg; p[5].b = rb; p[5].a = ra;

    *position = &p[6];
}

#ifdef DUMP_GRADIENTS_TO_PNG
static void
dump_gradient_to_png (CoglTexture *texture)
{
    cairo_image_surface_t *image = (cairo_image_surface_t *)
	cairo_image_surface_create (CAIRO_FORMAT_ARGB32,
				    cogl_texture_get_width (texture),
				    cogl_texture_get_height (texture));
    CoglPixelFormat format;
    static int gradient_id = 0;
    char *gradient_name;

    if (image->base.status)
	return;

#if G_BYTE_ORDER == G_LITTLE_ENDIAN
    format = COGL_PIXEL_FORMAT_BGRA_8888_PRE;
#else
    format = COGL_PIXEL_FORMAT_ARGB_8888_PRE;
#endif
    cogl_texture_get_data (texture,
			   format,
			   0,
			   image->data);
    gradient_name = g_strdup_printf ("./gradient%d.png", gradient_id++);
    g_print ("writing gradient: %s\n", gradient_name);
    cairo_surface_write_to_png ((cairo_surface_t *)image, gradient_name);
    g_free (gradient_name);
}
#endif

cairo_int_status_t
_cairo_cogl_get_linear_gradient (cairo_cogl_device_t *device,
				 cairo_extend_t extend_mode,
				 int n_stops,
				 const cairo_gradient_stop_t *stops,
				 cairo_cogl_linear_gradient_t **gradient_out)
{
    unsigned long hash;
    cairo_cogl_linear_gradient_t *gradient;
    cairo_cogl_linear_texture_entry_t *entry;
    cairo_gradient_stop_t *internal_stops;
    int stop_offset;
    int n_internal_stops;
    int n;
    cairo_cogl_gradient_compatibility_t compatibilities;
    int width;
    int left_padding = 0;
    cairo_color_stop_t left_padding_color;
    int right_padding = 0;
    cairo_color_stop_t right_padding_color;
    CoglPixelFormat format;
    CoglTexture2D *tex;
    GError *error = NULL;
    int un_padded_width;
    CoglHandle offscreen;
    cairo_int_status_t status;
    int n_quads;
    int n_vertices;
    float prev;
    float right;
    CoglVertexP2C4 *vertices;
    CoglVertexP2C4 *p;
    CoglPrimitive *prim;

    hash = _cairo_cogl_linear_gradient_hash (n_stops, stops);

    gradient = _cairo_cogl_linear_gradient_lookup (device, hash, n_stops, stops);
    if (gradient) {
	cairo_cogl_linear_texture_entry_t *entry =
	    _cairo_cogl_linear_gradient_texture_for_extend (gradient, extend_mode);
	if (entry) {
	    *gradient_out = _cairo_cogl_linear_gradient_reference (gradient);
	    return CAIRO_INT_STATUS_SUCCESS;
	}
    }

    if (!gradient) {
	gradient = _cairo_malloc (sizeof (cairo_cogl_linear_gradient_t) +
			   sizeof (cairo_gradient_stop_t) * (n_stops - 1));
	if (!gradient)
	    return CAIRO_INT_STATUS_NO_MEMORY;

	CAIRO_REFERENCE_COUNT_INIT (&gradient->ref_count, 1);
	/* NB: we update the cache_entry size at the end before
	 * [re]adding it to the cache. */
	gradient->cache_entry.hash = hash;
	gradient->textures = NULL;
	gradient->n_stops = n_stops;
	gradient->stops = gradient->stops_embedded;
	memcpy (gradient->stops_embedded, stops, sizeof (cairo_gradient_stop_t) * n_stops);
    } else
	_cairo_cogl_linear_gradient_reference (gradient);

    entry = _cairo_malloc (sizeof (cairo_cogl_linear_texture_entry_t));
    if (!entry) {
	status = CAIRO_INT_STATUS_NO_MEMORY;
	goto BAIL;
    }

    compatibilities = _cairo_cogl_compatibility_from_extend_mode (extend_mode);

    n_internal_stops = n_stops;
    stop_offset = 0;

    /* We really need stops covering the full [0,1] range for repeat/reflect
     * if we want to use sampler REPEAT/MIRROR wrap modes so we may need
     * to add some extra stops... */
    if (extend_mode == CAIRO_EXTEND_REPEAT || extend_mode == CAIRO_EXTEND_REFLECT)
    {
	/* If we don't need any extra stops then actually the texture
	 * will be shareable for repeat and reflect... */
	compatibilities = (CAIRO_COGL_GRADIENT_CAN_EXTEND_REPEAT |
			   CAIRO_COGL_GRADIENT_CAN_EXTEND_REFLECT);

	if (stops[0].offset != 0) {
	    n_internal_stops++;
	    stop_offset++;
	}

	if (stops[n_stops - 1].offset != 1)
	    n_internal_stops++;
    }

    internal_stops = alloca (n_internal_stops * sizeof (cairo_gradient_stop_t));
    memcpy (&internal_stops[stop_offset], stops, sizeof (cairo_gradient_stop_t) * n_stops);

    /* cairo_color_stop_t values are all unpremultiplied but we need to
     * interpolate premultiplied colors so we premultiply all the double
     * components now. (skipping any extra stops added for repeat/reflect)
     *
     * Anothing thing to note is that by premultiplying the colors
     * early we'll also reduce the range of colors to interpolate
     * which can result in smaller gradient textures.
     */
    for (n = stop_offset; n < n_stops; n++) {
	cairo_color_stop_t *color = &internal_stops[n].color;
	color->red *= color->alpha;
	color->green *= color->alpha;
	color->blue *= color->alpha;
    }

    if (n_internal_stops != n_stops)
    {
	if (extend_mode == CAIRO_EXTEND_REPEAT) {
	    compatibilities &= ~CAIRO_COGL_GRADIENT_CAN_EXTEND_REFLECT;
	    if (stops[0].offset != 0) {
		/* what's the wrap-around distance between the user's end-stops? */
		double dx = (1.0 - stops[n_stops - 1].offset) + stops[0].offset;
		internal_stops[0].offset = 0;
		color_stop_lerp (&stops[0].color,
				 &stops[n_stops - 1].color,
				 stops[0].offset / dx,
				 &internal_stops[0].color);
	    }
	    if (stops[n_stops - 1].offset != 1) {
		internal_stops[n_internal_stops - 1].offset = 1;
		internal_stops[n_internal_stops - 1].color = internal_stops[0].color;
	    }
	} else if (extend_mode == CAIRO_EXTEND_REFLECT) {
	    compatibilities &= ~CAIRO_COGL_GRADIENT_CAN_EXTEND_REPEAT;
	    if (stops[0].offset != 0) {
		internal_stops[0].offset = 0;
		internal_stops[0].color = stops[n_stops - 1].color;
	    }
	    if (stops[n_stops - 1].offset != 1) {
		internal_stops[n_internal_stops - 1].offset = 1;
		internal_stops[n_internal_stops - 1].color = stops[0].color;
	    }
	}
    }

    stops = internal_stops;
    n_stops = n_internal_stops;

    width = _cairo_cogl_linear_gradient_width_for_stops (extend_mode, n_stops, stops);

    if (extend_mode == CAIRO_EXTEND_PAD) {

	/* Here we need to guarantee that the edge texels of our
	 * texture correspond to the desired padding color so we
	 * can use CLAMP_TO_EDGE.
	 *
	 * For short stop-gaps and especially for degenerate stops
	 * it's possible that without special consideration the
	 * user's end stop colors would not be present in our final
	 * texture.
	 *
	 * To handle this we forcibly add two extra padding texels
	 * at the edges which extend beyond the [0,1] range of the
	 * gradient itself and we will later report a translate and
	 * scale transform to compensate for this.
	 */

	/* XXX: If we consider generating a mipmap for our 1d texture
	 * at some point then we also need to consider how much
	 * padding to add to be sure lower mipmap levels still have
	 * the desired edge color (as opposed to a linear blend with
	 * other colors of the gradient).
	 */

	left_padding = 1;
	left_padding_color = stops[0].color;
	right_padding = 1;
	right_padding_color = stops[n_stops - 1].color;
    } else if (extend_mode == CAIRO_EXTEND_NONE) {
	/* We handle EXTEND_NONE by adding two extra, transparent, texels at
	 * the ends of the texture and use CLAMP_TO_EDGE.
	 *
	 * We add a scale and translate transform so to account for our texels
	 * extending beyond the [0,1] range. */

	left_padding = 1;
	left_padding_color.red = 0;
	left_padding_color.green = 0;
	left_padding_color.blue = 0;
	left_padding_color.alpha = 0;
	right_padding = 1;
	right_padding_color = left_padding_color;
    }

    /* If we still have stops that don't cover the full [0,1] range
     * then we need to define a texture-coordinate scale + translate
     * transform to account for that... */
    if (stops[n_stops - 1].offset - stops[0].offset < 1) {
	float range = stops[n_stops - 1].offset - stops[0].offset;
	entry->scale_x = 1.0 / range;
	entry->translate_x = -(stops[0].offset * entry->scale_x);
    }

    width += left_padding + right_padding;

    width = _cairo_cogl_util_next_p2 (width);
    width = MIN (4096, width); /* lets not go too stupidly big! */
    format = _cairo_cogl_linear_gradient_format_for_stops (extend_mode, n_stops, stops);

    do {
	tex = cogl_texture_2d_new_with_size (device->cogl_context,
					     width,
					     1,
					     format,
					     &error);
	if (!tex)
	    g_error_free (error);
    } while (tex == NULL && width >> 1);

    if (!tex) {
	status = CAIRO_INT_STATUS_NO_MEMORY;
	goto BAIL;
    }

    entry->texture = COGL_TEXTURE (tex);
    entry->compatibility = compatibilities;

    un_padded_width = width - left_padding - right_padding;

    /* XXX: only when we know the final texture width can we calculate the
     * scale and translate factors needed to account for padding... */
    if (un_padded_width != width)
	entry->scale_x *= (float)un_padded_width / (float)width;
    if (left_padding)
	entry->translate_x += (entry->scale_x / (float)un_padded_width) * (float)left_padding;

    offscreen = cogl_offscreen_new_to_texture (tex);
    cogl_push_framebuffer (COGL_FRAMEBUFFER (offscreen));
    cogl_ortho (0, width, 1, 0, -1, 100);
    cogl_framebuffer_clear4f (COGL_FRAMEBUFFER (offscreen),
			      COGL_BUFFER_BIT_COLOR,
			      0, 0, 0, 0);

    n_quads = n_stops - 1 + !!left_padding + !!right_padding;
    n_vertices = 6 * n_quads;
    vertices = alloca (sizeof (CoglVertexP2C4) * n_vertices);
    p = vertices;
    if (left_padding)
	emit_stop (&p, 0, left_padding, &left_padding_color, &left_padding_color);
    prev = (float)left_padding;
    for (n = 1; n < n_stops; n++) {
	right = (float)left_padding + (float)un_padded_width * stops[n].offset;
	emit_stop (&p, prev, right, &stops[n-1].color, &stops[n].color);
	prev = right;
    }
    if (right_padding)
	emit_stop (&p, prev, width, &right_padding_color, &right_padding_color);

    prim = cogl_primitive_new_p2c4 (COGL_VERTICES_MODE_TRIANGLES,
				    n_vertices,
				    vertices);
    /* Just use this as the simplest way to setup a default pipeline... */
    cogl_set_source_color4f (0, 0, 0, 0);
    cogl_primitive_draw (prim);
    cogl_object_unref (prim);

    cogl_pop_framebuffer ();
    cogl_object_unref (offscreen);

    gradient->textures = g_list_prepend (gradient->textures, entry);
    gradient->cache_entry.size = _cairo_cogl_linear_gradient_size (gradient);

#ifdef DUMP_GRADIENTS_TO_PNG
    dump_gradient_to_png (COGL_TEXTURE (tex));
#endif

#warning "FIXME:"
    /* XXX: it seems the documentation of _cairo_cache_insert isn't true - it
     * doesn't handle re-adding the same entry gracefully - the cache will
     * just keep on growing and then it will start randomly evicting things
     * pointlessly */
    /* we ignore errors here and just return an uncached gradient */
    if (likely (! _cairo_cache_insert (&device->linear_cache, &gradient->cache_entry)))
        _cairo_cogl_linear_gradient_reference (gradient);

    *gradient_out = gradient;
    return CAIRO_INT_STATUS_SUCCESS;

BAIL:
    free (entry);
    if (gradient)
	_cairo_cogl_linear_gradient_destroy (gradient);
    return status;
}