summaryrefslogtreecommitdiff
path: root/ovgosd.c
blob: 6574658ea723d74bf57ebe896f9557be40b060b4 (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
/*
 * See the README file for copyright information and how to reach the author.
 *
 * $Id$
 */

#include <VG/openvg.h>
#include <VG/vgu.h>
#include <EGL/egl.h>
#include <GLES/gl.h>

#include "ovgosd.h"
#include "display.h"
#include "omxdevice.h"
#include "setup.h"
#include "tools.h"

class cOvgOsd : public cOsd
{

public:

	cOvgOsd(int Left, int Top, uint Level, cOvg *ovg);
	virtual ~cOvgOsd();

	virtual void Flush(void);
	virtual eOsdError SetAreas(const tArea *Areas, int NumAreas);

protected:

	virtual void SetActive(bool On);

private:

	cOvg *m_ovg;

};

/* ------------------------------------------------------------------------- */

class cOvg : public cThread
{
public:

	cOvg() :
		cThread(),
		m_do(0),
		m_done(0),
		m_width(0),
		m_height(0),
		m_aspect(0),
		m_pixmap(0),
		m_d(0),
		m_x(0),
		m_y(0),
		m_w(0),
		m_h(0),
		m_clear(false)
	{
		cRpiSetup::GetDisplaySize(m_width, m_height, m_aspect);

		m_do = new cCondWait();
		m_done = new cCondWait();

		Start();
	}

	~cOvg()
	{
		Cancel(-1);
		Clear();
		while (Active())
			cCondWait::SleepMs(50);

		delete m_do;
		delete m_done;
	}

	void GetDisplaySize(int &width, int &height, double &aspect)
	{
		width = m_width;
		height = m_height;
		aspect = m_aspect;
	}

	void DrawPixmap(int x, int y, int w, int h, int d, const uint8_t *data)
	{
		Lock();
		m_pixmap = data;
		m_d = d;
		m_x = x;
		m_y = y;
		m_w = w;
		m_h = h;
		m_do->Signal();
		m_done->Wait();
		Unlock();
	}

	void Clear()
	{
		Lock();
		m_clear = true;
		m_do->Signal();
		m_done->Wait();
		Unlock();
	}

protected:

	virtual void Action(void)
	{
		DLOG("cOvg() thread started");

		EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
		if (display == EGL_NO_DISPLAY)
			ELOG("failed to get EGL display connection!");

		if (eglInitialize(display, NULL, NULL) == EGL_FALSE)
			ELOG("failed to init EGL display connection!");

		eglBindAPI(EGL_OPENVG_API);

		const EGLint attr[] = {
			EGL_RED_SIZE, 8,
			EGL_GREEN_SIZE, 8,
			EGL_BLUE_SIZE, 8,
			EGL_ALPHA_SIZE, 8,
			EGL_SURFACE_TYPE, EGL_WINDOW_BIT | EGL_PBUFFER_BIT,
			EGL_CONFORMANT, EGL_OPENVG_BIT,
			EGL_NONE
		};

		EGLConfig config;
		EGLint nConfig;

		// get an appropriate EGL frame buffer configuration
		if (eglChooseConfig(display, attr, &config, 1, &nConfig) == EGL_FALSE)
			ELOG("failed to get EGL frame buffer config!");

		// create an EGL rendering context
		EGLContext context = eglCreateContext(display, config, NULL, NULL);
		if (context == EGL_NO_CONTEXT)
			ELOG("failed to create EGL rendering context!");

		cRpiDisplay::Open(0 /* LCD */);
		DISPMANX_ELEMENT_HANDLE_T dispmanElement;
		cRpiDisplay::AddElement(dispmanElement, m_width, m_height, 2);

		EGL_DISPMANX_WINDOW_T nativewindow;
		nativewindow.element = dispmanElement;
		nativewindow.width = m_width;
		nativewindow.height = m_height;

		const EGLint windowAttr[] = {
			EGL_RENDER_BUFFER, EGL_SINGLE_BUFFER,
			EGL_NONE
		};

		EGLSurface surface = eglCreateWindowSurface(display, config,
				&nativewindow, windowAttr);
		if (surface == EGL_NO_SURFACE)
			ELOG("failed to create EGL window surface!");

		// connect the context to the surface
		if (eglMakeCurrent(display, surface, surface, context) == EGL_FALSE)
			ELOG("failed to connect context to surface!");

		float color[4] = {0.0f, 0.0f, 0.0f, 0.0f};
	    vgSetfv(VG_CLEAR_COLOR, 4, color);
	    vgClear(0, 0, m_width, m_height);
		eglSwapBuffers(display, surface);

		vgSeti(VG_MATRIX_MODE, VG_MATRIX_IMAGE_USER_TO_SURFACE);
		vgSeti(VG_IMAGE_MODE, VG_DRAW_IMAGE_NORMAL);
		vgSeti(VG_IMAGE_QUALITY, VG_IMAGE_QUALITY_BETTER);
		vgSeti(VG_BLEND_MODE, VG_BLEND_SRC);

		vgLoadIdentity();
		vgScale(1.0f, -1.0f);
		vgTranslate(0.0f, -m_height);

		VGImage image = vgCreateImage(VG_sARGB_8888, m_width, m_height,
				VG_IMAGE_QUALITY_BETTER);

		while (Running())
		{
			m_do->Wait();
			if (m_pixmap)
			{
				vgClearImage(image, m_x, m_y, m_w, m_h);
				vgImageSubData(image, m_pixmap, m_d, VG_sARGB_8888,
						m_x, m_y, m_w, m_h);
				vgDrawImage(image);
				m_pixmap = 0;
			}
			if (m_clear)
			{
				vgClearImage(image, 0, 0, m_width, m_height);
				vgDrawImage(image);
				m_clear = false;
			}
			eglSwapBuffers(display, surface);
			m_done->Signal();
		}

		vgDestroyImage(image);

		// clear screen
		glClear(GL_COLOR_BUFFER_BIT);
		eglSwapBuffers(display, surface);

		// Release OpenGL resources
		eglMakeCurrent(display,
				EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
		eglDestroySurface(display, surface);
		eglDestroyContext(display, context);
		eglTerminate(display);

		cRpiDisplay::Close();

		DLOG("cOvg() thread ended");
	}

private:

	cCondWait* m_do;
	cCondWait* m_done;

	int m_width;
	int m_height;
	double m_aspect;

	const uint8_t *m_pixmap;
	int m_d;
	int m_x;
	int m_y;
	int m_w;
	int m_h;

	bool m_clear;

};

cRpiOsdProvider::cRpiOsdProvider() :
	cOsdProvider(),
	m_ovg(0)
{
	DLOG("new cOsdProvider()");
	m_ovg = new cOvg();
}

cRpiOsdProvider::~cRpiOsdProvider()
{
	DLOG("delete cOsdProvider()");
	delete m_ovg;
}

cOsd *cRpiOsdProvider::CreateOsd(int Left, int Top, uint Level)
{
	return new cOvgOsd(Left, Top, Level, m_ovg);
}

/* ------------------------------------------------------------------------- */

cOvgOsd::cOvgOsd(int Left, int Top, uint Level, cOvg *ovg) :
	cOsd(Left, Top, Level),
	m_ovg(ovg)
{
}

cOvgOsd::~cOvgOsd()
{
	m_ovg->Clear();
}

void cOvgOsd::Flush(void)
{
	if (IsTrueColor())
	{
		LOCK_PIXMAPS;

		while (cPixmapMemory *pm = RenderPixmaps()) {
			m_ovg->DrawPixmap(
					Left() + pm->ViewPort().X(), Top() + pm->ViewPort().Y(),
					pm->ViewPort().Width(),	pm->ViewPort().Height(),
					pm->ViewPort().Width() * sizeof(tColor), pm->Data());
			delete pm;
		}
		return;
	}

	for (int i = 0; cBitmap *bitmap = GetBitmap(i); ++i)
	{
		int x1, y1, x2, y2;
		if (bitmap->Dirty(x1, y1, x2, y2))
		{
			int w = x2 - x1 + 1;
			int h = y2 - y1 + 1;
			uint8_t *argb = (uint8_t *) malloc(w * h * sizeof(uint32_t));

			for (int y = y1; y <= y2; ++y)
			{
				for (int x = x1; x <= x2; ++x)
				{
					((uint32_t *) argb)[x - x1 + (y - y1) * w] =
					bitmap->GetColor(x, y);
				}
			}
			m_ovg->DrawPixmap(Left() + bitmap->X0() + x1, 
				Top() + bitmap->Y0() + y1, w, h, w * sizeof(tColor), argb);
			bitmap->Clean();
			free(argb);
		}
	}
}

eOsdError cOvgOsd::SetAreas(const tArea *Areas, int NumAreas)
{
	eOsdError error;
	cBitmap * bitmap;

	if (Active())
		m_ovg->Clear();

	error = cOsd::SetAreas(Areas, NumAreas);

	for (int i = 0; (bitmap = GetBitmap(i)) != NULL; i++)
		bitmap->Clean();

	return error;
}

void cOvgOsd::SetActive(bool On)
{
	if (On != Active())
	{
		cOsd::SetActive(On);
		if (!On)
			m_ovg->Clear();
		else
			if (GetBitmap(0))
				Flush();
	}
}