summaryrefslogtreecommitdiff
path: root/pugl/cairo_gl.h
blob: fe296bbcf30feddc08e33b6fae119f84b1b93038 (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
/*
  Copyright 2016 David Robillard <http://drobilla.net>

  Permission to use, copy, modify, and/or distribute this software for any
  purpose with or without fee is hereby granted, provided that the above
  copyright notice and this permission notice appear in all copies.

  THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/

#if defined(PUGL_HAVE_GL) && defined(PUGL_HAVE_CAIRO)

#include <cairo.h>
#include <stdint.h>

#include "pugl/gl.h"

typedef struct {
	unsigned texture_id;
	uint8_t* buffer;
} PuglCairoGL;

static cairo_surface_t*
pugl_cairo_gl_create(PuglCairoGL* ctx, int width, int height, int bpp)
{
	free(ctx->buffer);
	ctx->buffer = (uint8_t*)calloc(bpp * width * height, sizeof(uint8_t));
	if (!ctx->buffer) {
		fprintf(stderr, "failed to allocate surface buffer\n");
		return NULL;
	}

	return cairo_image_surface_create_for_data(
		ctx->buffer, CAIRO_FORMAT_ARGB32, width, height, bpp * width);
}

static void
pugl_cairo_gl_free(PuglCairoGL* ctx)
{
	free(ctx->buffer);
	ctx->buffer = NULL;
}

static void
pugl_cairo_gl_configure(PuglCairoGL* ctx, int width, int height)
{
	glDisable(GL_DEPTH_TEST);
	glEnable(GL_BLEND);
	glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
	glEnable(GL_TEXTURE_RECTANGLE_ARB);

	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	glOrtho(-1.0f, 1.0f, -1.0f, 1.0f, -1.0f, 1.0f);

	glClear(GL_COLOR_BUFFER_BIT);

	glDeleteTextures(1, &ctx->texture_id);
	glGenTextures(1, &ctx->texture_id);
	glBindTexture(GL_TEXTURE_RECTANGLE_ARB, ctx->texture_id);
	glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
}

static void
pugl_cairo_gl_draw(PuglCairoGL* ctx, int width, int height)
{
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
	glViewport(0, 0, width, height);
	glClear(GL_COLOR_BUFFER_BIT);

	glPushMatrix();
	glEnable(GL_TEXTURE_RECTANGLE_ARB);
	glEnable(GL_TEXTURE_2D);

	glTexImage2D(GL_TEXTURE_RECTANGLE_ARB, 0, GL_RGBA8,
	             width, height, 0,
	             GL_BGRA, GL_UNSIGNED_BYTE, ctx->buffer);

	glBegin(GL_QUADS);
	glTexCoord2f(0.0f, (GLfloat)height);
	glVertex2f(-1.0f, -1.0f);

	glTexCoord2f((GLfloat)width, (GLfloat)height);
	glVertex2f(1.0f, -1.0f);

	glTexCoord2f((GLfloat)width, 0.0f);
	glVertex2f(1.0f, 1.0f);

	glTexCoord2f(0.0f, 0.0f);
	glVertex2f(-1.0f, 1.0f);
	glEnd();

	glDisable(GL_TEXTURE_2D);
	glDisable(GL_TEXTURE_RECTANGLE_ARB);
	glPopMatrix();
}

#endif