summaryrefslogtreecommitdiff
path: root/src/vg/Context.cpp
blob: f1fee49cc06a23cef66d61daf1420ff010af457e (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
/*------------------------------------------------------------------------
 *
 * 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	Implementation of VGContext functions.
 * \note	
 *//*-------------------------------------------------------------------*/

#include "Context.h"

namespace tgOpenVG
{

/*-------------------------------------------------------------------*//*!
* \brief	VGContext constructor.
* \param	
* \return	
* \note		
*//*-------------------------------------------------------------------*/

VGContext::VGContext(VGContext* shareContext) :
	// Mode settings
	m_matrixMode(VG_MATRIX_PATH_USER_TO_SURFACE),
	m_fillRule(VG_EVEN_ODD),
	m_imageQuality(VG_IMAGE_QUALITY_FASTER),
	m_renderingQuality(VG_RENDERING_QUALITY_BETTER),
	m_blendMode(VG_BLEND_SRC_OVER),
	m_imageMode(VG_DRAW_IMAGE_NORMAL),

	// Scissor rectangles
	m_scissor(),
		
	// Stroke parameters
	m_strokeLineWidth(1.0f),
	m_inputStrokeLineWidth(1.0f),
	m_strokeCapStyle(VG_CAP_BUTT),
	m_strokeJoinStyle(VG_JOIN_MITER),
	m_strokeMiterLimit(4.0f),
	m_inputStrokeMiterLimit(4.0f),
	m_strokeDashPattern(),
	m_inputStrokeDashPattern(),
	m_strokeDashPhase(0.0f),
	m_inputStrokeDashPhase(0.0f),
	m_strokeDashPhaseReset(VG_FALSE),
		
	// Edge fill color for vgConvolve and pattern paint
	m_tileFillColor(0,0,0,0, Color::sRGBA),
	m_inputTileFillColor(0,0,0,0, Color::sRGBA),
		
	// Color for vgClear
	m_clearColor(0,0,0,0, Color::sRGBA),
	m_inputClearColor(0,0,0,0, Color::sRGBA),

    m_glyphOrigin(0.0f, 0.0f),
    m_inputGlyphOrigin(0.0f, 0.0f),

	m_masking(VG_FALSE),
	m_scissoring(VG_FALSE),

	m_pixelLayout(VG_PIXEL_LAYOUT_UNKNOWN),

	m_filterFormatLinear(VG_FALSE),
	m_filterFormatPremultiplied(VG_FALSE),
	m_filterChannelMask(VG_RED|VG_GREEN|VG_BLUE|VG_ALPHA),

	// Matrices
	m_pathUserToSurface(),
	m_imageUserToSurface(),
	m_glyphUserToSurface(),
	m_fillPaintToUser(),
	m_strokePaintToUser(),

	m_fillPaint(VG_INVALID_HANDLE),
	m_strokePaint(VG_INVALID_HANDLE),

    m_colorTransform(VG_FALSE),
    m_colorTransformValues(),
    m_inputColorTransformValues(),

	m_error(VG_NO_ERROR),

	m_imageManager(NULL),
	m_pathManager(NULL),
	m_paintManager(NULL),
	m_fontManager(NULL),
	m_maskLayerManager(NULL),

    m_eglDrawable(NULL)
{
	if(shareContext)
	{
		m_imageManager = shareContext->m_imageManager;
		m_pathManager = shareContext->m_pathManager;
		m_paintManager = shareContext->m_paintManager;
		m_fontManager = shareContext->m_fontManager;
		m_maskLayerManager = shareContext->m_maskLayerManager;
	}
	else
	{
		try
		{
			m_imageManager = RI_NEW(tgOpenVG::ResourceManager<Image>, ());	//throws bad_alloc
			m_pathManager = RI_NEW(tgOpenVG::ResourceManager<Path>, ());	//throws bad_alloc
			m_paintManager = RI_NEW(tgOpenVG::ResourceManager<Paint>, ());	//throws bad_alloc
			m_fontManager = RI_NEW(tgOpenVG::ResourceManager<Font>, ());	//throws bad_alloc
			m_maskLayerManager = RI_NEW(tgOpenVG::ResourceManager<Surface>, ());	//throws bad_alloc
		}
		catch(std::bad_alloc)
		{
			RI_DELETE(m_imageManager);
			RI_DELETE(m_pathManager);
			RI_DELETE(m_paintManager);
			RI_DELETE(m_fontManager);
			RI_DELETE(m_maskLayerManager);
			throw;
		}
	}
	RI_ASSERT(m_imageManager);
	RI_ASSERT(m_pathManager);
	RI_ASSERT(m_paintManager);
	RI_ASSERT(m_fontManager);
	RI_ASSERT(m_maskLayerManager);
	m_imageManager->addReference();
	m_pathManager->addReference();
	m_paintManager->addReference();
	m_fontManager->addReference();
	m_maskLayerManager->addReference();

    m_inputColorTransformValues[0] = 1.0f;
    m_inputColorTransformValues[1] = 1.0f;
    m_inputColorTransformValues[2] = 1.0f;
    m_inputColorTransformValues[3] = 1.0f;
    m_inputColorTransformValues[4] = 0.0f;
    m_inputColorTransformValues[5] = 0.0f;
    m_inputColorTransformValues[6] = 0.0f;
    m_inputColorTransformValues[7] = 0.0f;
    m_colorTransformValues[0] = 1.0f;
    m_colorTransformValues[1] = 1.0f;
    m_colorTransformValues[2] = 1.0f;
    m_colorTransformValues[3] = 1.0f;
    m_colorTransformValues[4] = 0.0f;
    m_colorTransformValues[5] = 0.0f;
    m_colorTransformValues[6] = 0.0f;
    m_colorTransformValues[7] = 0.0f;
}

/*-------------------------------------------------------------------*//*!
* \brief	VGContext destructor.
* \param	
* \return	
* \note		
*//*-------------------------------------------------------------------*/

VGContext::~VGContext()
{
	releasePaint(VG_FILL_PATH | VG_STROKE_PATH);
    setDefaultDrawable(NULL);

	//destroy own images, paths and paints
	while(Image* i = m_imageManager->getFirstResource(this))
		m_imageManager->removeResource(i);
	while(Path* p = m_pathManager->getFirstResource(this))
		m_pathManager->removeResource(p);
	while(Paint* t = m_paintManager->getFirstResource(this))
		m_paintManager->removeResource(t);
	while(Font* t = m_fontManager->getFirstResource(this))
		m_fontManager->removeResource(t);
	while(Surface* t = m_maskLayerManager->getFirstResource(this))
		m_maskLayerManager->removeResource(t);

	//decrease the reference count of resource managers
	if(!m_imageManager->removeReference())
		RI_DELETE(m_imageManager);
	if(!m_pathManager->removeReference())
		RI_DELETE(m_pathManager);
	if(!m_paintManager->removeReference())
		RI_DELETE(m_paintManager);
	if(!m_fontManager->removeReference())
		RI_DELETE(m_fontManager);
	if(!m_maskLayerManager->removeReference())
		RI_DELETE(m_maskLayerManager);
}

/*-------------------------------------------------------------------*//*!
* \brief	Sets new default drawable.
* \param	drawable New drawable or NULL when context is unbound
* \return	
* \note		
*//*-------------------------------------------------------------------*/

void VGContext::setDefaultDrawable(Drawable* drawable)
{
	if(m_eglDrawable)
	{
		if(!m_eglDrawable->removeReference())
			RI_DELETE(m_eglDrawable);
	}
	m_eglDrawable = drawable;
	if(m_eglDrawable)
	{
		m_eglDrawable->addReference();
	}
}

/*-------------------------------------------------------------------*//*!
* \brief	Returns true if the given image is generated through any
*			context that is shared with this one.
* \param	
* \return	
* \note		
*//*-------------------------------------------------------------------*/

bool VGContext::isValidImage(VGImage image)
{
	return m_imageManager->isValid((Image*)image);
}

/*-------------------------------------------------------------------*//*!
* \brief	Returns true if the given path is generated through any
*			context that is shared with this one.
* \param	
* \return	
* \note		
*//*-------------------------------------------------------------------*/

bool VGContext::isValidPath(VGPath path)
{
	return m_pathManager->isValid((Path*)path);
}

/*-------------------------------------------------------------------*//*!
* \brief	Returns true if the given paint is generated through any
*			context that is shared with this one.
* \param	
* \return	
* \note		
*//*-------------------------------------------------------------------*/

bool VGContext::isValidPaint(VGPaint paint)
{
	return m_paintManager->isValid((Paint*)paint);
}

/*-------------------------------------------------------------------*//*!
* \brief	Returns true if the given font is generated through any
*			context that is shared with this one.
* \param	
* \return	
* \note		
*//*-------------------------------------------------------------------*/

bool VGContext::isValidFont(VGFont font)
{
	return m_fontManager->isValid((Font*)font);
}

/*-------------------------------------------------------------------*//*!
* \brief	Returns true if the given mask layer is generated through any
*			context that is shared with this one.
* \param	
* \return	
* \note		
*//*-------------------------------------------------------------------*/

bool VGContext::isValidMaskLayer(VGMaskLayer layer)
{
	return m_maskLayerManager->isValid((Surface*)layer);
}

/*-------------------------------------------------------------------*//*!
* \brief	Releases the given paint objects of the context.
* \param	
* \return	
* \note		
*//*-------------------------------------------------------------------*/

void VGContext::releasePaint(VGbitfield paintModes)
{
	if(paintModes & VG_FILL_PATH)
	{
		//release previous paint
		Paint* prev = (Paint*)m_fillPaint;
		if(prev)
		{
			if(!prev->removeReference())
				RI_DELETE(prev);
		}
		m_fillPaint = VG_INVALID_HANDLE;
	}
	if(paintModes & VG_STROKE_PATH)
	{
		//release previous paint
		Paint* prev = (Paint*)m_strokePaint;
		if(prev)
		{
			if(!prev->removeReference())
				RI_DELETE(prev);
		}
		m_strokePaint = VG_INVALID_HANDLE;
	}
}

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

}	//namespace tgOpenVG