summaryrefslogtreecommitdiff
path: root/src/vg/Context.h
blob: 05aad3a798493c77f0bca5d4055dd31b85de45f9 (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
#ifndef __CONTEXT_H
#define __CONTEXT_H

/*------------------------------------------------------------------------
 *
 * OpenVG 1.1 Reference Implementation
 * -----------------------------------
 *
 * Copyright (c) 2007 The Khronos Group Inc.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and /or associated documentation files
 * (the "Materials "), to deal in the Materials without restriction,
 * including without limitation the rights to use, copy, modify, merge,
 * publish, distribute, sublicense, and/or sell copies of the Materials,
 * and to permit persons to whom the Materials are furnished to do so,
 * subject to the following conditions: 
 *
 * The above copyright notice and this permission notice shall be included 
 * in all copies or substantial portions of the Materials. 
 *
 * THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE MATERIALS OR
 * THE USE OR OTHER DEALINGS IN THE MATERIALS.
 *
 *//**
 * \file
 * \brief	VGContext class. Used for storing OpenVG state.
 * \note	
 *//*-------------------------------------------------------------------*/

#include <VG/openvg.h>
#include "Defs.h"
#include "Math.h"
#include "Image.h"
#include "Path.h"
#include "Font.h"
#include "Array.h"

//==============================================================================================

namespace tgOpenVG
{

class VGContext;

/*-------------------------------------------------------------------*//*!
* \brief	A list of resources (Images, Paths, or Paints) shared by a
*			set of contexts.
* \param	
* \return	
* \note		
*//*-------------------------------------------------------------------*/

template <class Resource> class ResourceManager
{
public:
	ResourceManager() :
		m_referenceCount(0),
		m_resources()
	{
	}

	~ResourceManager()
	{
		RI_ASSERT(m_referenceCount == 0);
		RI_ASSERT(m_resources.size() == 0);
	}

	void			addReference()
	{
		m_referenceCount++;
	}

	int				removeReference()
	{
		m_referenceCount--;
		RI_ASSERT(m_referenceCount >= 0);
		return m_referenceCount;
	}

	void			addResource(Resource* resource, VGContext* context)
	{
		Entry r;
		r.resource = resource;
		r.context = context;
		m_resources.push_back(r);	//throws bad_alloc
		resource->addReference();
	}

	void			removeResource(Resource* resource)
	{
		if(!resource->removeReference())
			RI_DELETE(resource);

		int i=0;
		bool found = false;
		for(;i<m_resources.size();i++)
		{
			if(m_resources[i].resource == resource)
			{
				found = true;
				break;
			}
		}
		RI_ASSERT(found);

		for(;i<m_resources.size()-1;i++)
		{
			m_resources[i] = m_resources[i+1];
		}
		m_resources.resize(m_resources.size()-1);
	}

	bool			isValid(Resource* resource)
	{
		for(int i=0;i<m_resources.size();i++)
		{
			if(m_resources[i].resource == resource)
				return true;
		}
		return false;
	}

	Resource*		getFirstResource(VGContext* context)
	{
		for(int i=0;i<m_resources.size();i++)
		{
			if(m_resources[i].context == context)
				return m_resources[i].resource;
		}
		return NULL;
	}

private:
	ResourceManager(const ResourceManager&);
	ResourceManager operator=(const ResourceManager&);

	struct Entry
	{
		Resource*	resource;
		VGContext*	context;
	};

	int				m_referenceCount;
	Array<Entry>	m_resources;
};

/*-------------------------------------------------------------------*//*!
* \brief	
* \param	
* \return	
* \note		
*//*-------------------------------------------------------------------*/

class VGContext
{
public:
	VGContext(VGContext* shareContext);	//throws bad_alloc
	~VGContext();

    void            setDefaultDrawable(Drawable* drawable); //called from EGL
    Drawable*       getCurrentDrawable()        { return m_eglDrawable; }

	bool			isValidImage(VGImage image);
	bool			isValidPath(VGPath path);
	bool			isValidPaint(VGPaint paint);
	bool			isValidFont(VGFont font);
	bool			isValidMaskLayer(VGMaskLayer layer);

	void			releasePaint(VGbitfield paintModes);

	void			setError(VGErrorCode error)		{ if(m_error == VG_NO_ERROR) m_error = error; }

	// Mode settings
	VGMatrixMode					m_matrixMode;
	VGFillRule						m_fillRule;
	VGImageQuality					m_imageQuality;
	VGRenderingQuality				m_renderingQuality;
	VGBlendMode						m_blendMode;
	VGImageMode						m_imageMode;
	
	// Scissor rectangles
	Array<Rectangle>				m_scissor;

	// Stroke parameters
	RIfloat							m_strokeLineWidth;
	RIfloat							m_inputStrokeLineWidth;
	VGCapStyle						m_strokeCapStyle;
	VGJoinStyle						m_strokeJoinStyle;
	RIfloat							m_strokeMiterLimit;
	RIfloat							m_inputStrokeMiterLimit;
	Array<RIfloat>					m_strokeDashPattern;
	Array<RIfloat>					m_inputStrokeDashPattern;
	RIfloat							m_strokeDashPhase;
	RIfloat							m_inputStrokeDashPhase;
	VGboolean						m_strokeDashPhaseReset;

	// Edge fill color for vgConvolve and pattern paint
	Color							m_tileFillColor;
	Color							m_inputTileFillColor;

	// Color for vgClear
	Color							m_clearColor;
	Color							m_inputClearColor;

    Vector2                         m_glyphOrigin;
    Vector2                         m_inputGlyphOrigin;

	VGboolean						m_masking;
	VGboolean						m_scissoring;

	VGPixelLayout					m_pixelLayout;

	VGboolean						m_filterFormatLinear;
	VGboolean						m_filterFormatPremultiplied;
	VGbitfield						m_filterChannelMask;

	// Matrices
	Matrix3x3						m_pathUserToSurface;
	Matrix3x3						m_imageUserToSurface;
	Matrix3x3						m_glyphUserToSurface;
	Matrix3x3						m_fillPaintToUser;
	Matrix3x3						m_strokePaintToUser;

	VGPaint							m_fillPaint;
	VGPaint							m_strokePaint;

    VGboolean                       m_colorTransform;
    RIfloat                         m_colorTransformValues[8];
    RIfloat                         m_inputColorTransformValues[8];

	VGErrorCode						m_error;

	ResourceManager<Image>*			m_imageManager;
	ResourceManager<Path>*			m_pathManager;
	ResourceManager<Paint>*			m_paintManager;
	ResourceManager<Font>*			m_fontManager;
	ResourceManager<Surface>*		m_maskLayerManager;
private:
	Drawable*                       m_eglDrawable;

	VGContext(const VGContext&);			//!< Not allowed.
	void operator=(const VGContext&);		//!< Not allowed.
};

//==============================================================================================

}	//namespace tgOpenVG

//==============================================================================================

#endif /* __CONTEXT_H */