egl/sfopenvg/riContext.cpp
branchEGL_MERGE
changeset 180 f767bd5f4cfc
parent 119 5f371025658c
child 181 c1509651cd2b
equal deleted inserted replaced
119:5f371025658c 180:f767bd5f4cfc
     1 /*------------------------------------------------------------------------
       
     2  *
       
     3  * OpenVG 1.1 Reference Implementation
       
     4  * -----------------------------------
       
     5  *
       
     6  * Copyright (c) 2007 The Khronos Group Inc.
       
     7  *
       
     8  * Permission is hereby granted, free of charge, to any person obtaining a
       
     9  * copy of this software and /or associated documentation files
       
    10  * (the "Materials "), to deal in the Materials without restriction,
       
    11  * including without limitation the rights to use, copy, modify, merge,
       
    12  * publish, distribute, sublicense, and/or sell copies of the Materials,
       
    13  * and to permit persons to whom the Materials are furnished to do so,
       
    14  * subject to the following conditions: 
       
    15  *
       
    16  * The above copyright notice and this permission notice shall be included 
       
    17  * in all copies or substantial portions of the Materials. 
       
    18  *
       
    19  * THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
       
    20  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
       
    21  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
       
    22  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
       
    23  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
       
    24  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE MATERIALS OR
       
    25  * THE USE OR OTHER DEALINGS IN THE MATERIALS.
       
    26  *
       
    27  *//**
       
    28  * \file
       
    29  * \brief	Implementation of VGContext functions.
       
    30  * \note	
       
    31  *//*-------------------------------------------------------------------*/
       
    32 
       
    33 #include "riContext.h"
       
    34 
       
    35 namespace OpenVGRI
       
    36 {
       
    37 
       
    38 /*-------------------------------------------------------------------*//*!
       
    39 * \brief	VGContext constructor.
       
    40 * \param	
       
    41 * \return	
       
    42 * \note		
       
    43 *//*-------------------------------------------------------------------*/
       
    44 
       
    45 VGContext::VGContext(VGContext* shareContext) :
       
    46 	// Mode settings
       
    47 	m_matrixMode(VG_MATRIX_PATH_USER_TO_SURFACE),
       
    48 	m_fillRule(VG_EVEN_ODD),
       
    49 	m_imageQuality(VG_IMAGE_QUALITY_FASTER),
       
    50 	m_renderingQuality(VG_RENDERING_QUALITY_BETTER),
       
    51 	m_blendMode(VG_BLEND_SRC_OVER),
       
    52 	m_imageMode(VG_DRAW_IMAGE_NORMAL),
       
    53 
       
    54 	// Scissor rectangles
       
    55 	m_scissor(),
       
    56 		
       
    57 	// Stroke parameters
       
    58 	m_strokeLineWidth(1.0f),
       
    59 	m_inputStrokeLineWidth(1.0f),
       
    60 	m_strokeCapStyle(VG_CAP_BUTT),
       
    61 	m_strokeJoinStyle(VG_JOIN_MITER),
       
    62 	m_strokeMiterLimit(4.0f),
       
    63 	m_inputStrokeMiterLimit(4.0f),
       
    64 	m_strokeDashPattern(),
       
    65 	m_inputStrokeDashPattern(),
       
    66 	m_strokeDashPhase(0.0f),
       
    67 	m_inputStrokeDashPhase(0.0f),
       
    68 	m_strokeDashPhaseReset(VG_FALSE),
       
    69 		
       
    70 	// Edge fill color for vgConvolve and pattern paint
       
    71 	m_tileFillColor(0,0,0,0, Color::sRGBA),
       
    72 	m_inputTileFillColor(0,0,0,0, Color::sRGBA),
       
    73 		
       
    74 	// Color for vgClear
       
    75 	m_clearColor(0,0,0,0, Color::sRGBA),
       
    76 	m_inputClearColor(0,0,0,0, Color::sRGBA),
       
    77 
       
    78     m_glyphOrigin(0.0f, 0.0f),
       
    79     m_inputGlyphOrigin(0.0f, 0.0f),
       
    80 
       
    81 	m_masking(VG_FALSE),
       
    82 	m_scissoring(VG_FALSE),
       
    83 
       
    84 	m_pixelLayout(VG_PIXEL_LAYOUT_UNKNOWN),
       
    85 
       
    86 	m_filterFormatLinear(VG_FALSE),
       
    87 	m_filterFormatPremultiplied(VG_FALSE),
       
    88 	m_filterChannelMask(VG_RED|VG_GREEN|VG_BLUE|VG_ALPHA),
       
    89 
       
    90 	// Matrices
       
    91 	m_pathUserToSurface(),
       
    92 	m_imageUserToSurface(),
       
    93 	m_glyphUserToSurface(),
       
    94 	m_fillPaintToUser(),
       
    95 	m_strokePaintToUser(),
       
    96 
       
    97 	m_fillPaint(VG_INVALID_HANDLE),
       
    98 	m_strokePaint(VG_INVALID_HANDLE),
       
    99 
       
   100     m_colorTransform(VG_FALSE),
       
   101     m_colorTransformValues(),
       
   102     m_inputColorTransformValues(),
       
   103 
       
   104 	m_error(VG_NO_ERROR),
       
   105 
       
   106 	m_imageManager(NULL),
       
   107 	m_pathManager(NULL),
       
   108 	m_paintManager(NULL),
       
   109 	m_fontManager(NULL),
       
   110 	m_maskLayerManager(NULL),
       
   111 
       
   112     m_eglDrawable(NULL)
       
   113 {
       
   114 	if(shareContext)
       
   115 	{
       
   116 		m_imageManager = shareContext->m_imageManager;
       
   117 		m_pathManager = shareContext->m_pathManager;
       
   118 		m_paintManager = shareContext->m_paintManager;
       
   119 		m_fontManager = shareContext->m_fontManager;
       
   120 		m_maskLayerManager = shareContext->m_maskLayerManager;
       
   121 	}
       
   122 	else
       
   123 	{
       
   124 		try
       
   125 		{
       
   126 			m_imageManager = RI_NEW(OpenVGRI::ResourceManager<Image>, ());	//throws bad_alloc
       
   127 			m_pathManager = RI_NEW(OpenVGRI::ResourceManager<Path>, ());	//throws bad_alloc
       
   128 			m_paintManager = RI_NEW(OpenVGRI::ResourceManager<Paint>, ());	//throws bad_alloc
       
   129 			m_fontManager = RI_NEW(OpenVGRI::ResourceManager<Font>, ());	//throws bad_alloc
       
   130 			m_maskLayerManager = RI_NEW(OpenVGRI::ResourceManager<Surface>, ());	//throws bad_alloc
       
   131 		}
       
   132 		catch(std::bad_alloc)
       
   133 		{
       
   134 			RI_DELETE(m_imageManager);
       
   135 			RI_DELETE(m_pathManager);
       
   136 			RI_DELETE(m_paintManager);
       
   137 			RI_DELETE(m_fontManager);
       
   138 			RI_DELETE(m_maskLayerManager);
       
   139 			throw;
       
   140 		}
       
   141 	}
       
   142 	RI_ASSERT(m_imageManager);
       
   143 	RI_ASSERT(m_pathManager);
       
   144 	RI_ASSERT(m_paintManager);
       
   145 	RI_ASSERT(m_fontManager);
       
   146 	RI_ASSERT(m_maskLayerManager);
       
   147 	m_imageManager->addReference();
       
   148 	m_pathManager->addReference();
       
   149 	m_paintManager->addReference();
       
   150 	m_fontManager->addReference();
       
   151 	m_maskLayerManager->addReference();
       
   152 
       
   153     m_inputColorTransformValues[0] = 1.0f;
       
   154     m_inputColorTransformValues[1] = 1.0f;
       
   155     m_inputColorTransformValues[2] = 1.0f;
       
   156     m_inputColorTransformValues[3] = 1.0f;
       
   157     m_inputColorTransformValues[4] = 0.0f;
       
   158     m_inputColorTransformValues[5] = 0.0f;
       
   159     m_inputColorTransformValues[6] = 0.0f;
       
   160     m_inputColorTransformValues[7] = 0.0f;
       
   161     m_colorTransformValues[0] = 1.0f;
       
   162     m_colorTransformValues[1] = 1.0f;
       
   163     m_colorTransformValues[2] = 1.0f;
       
   164     m_colorTransformValues[3] = 1.0f;
       
   165     m_colorTransformValues[4] = 0.0f;
       
   166     m_colorTransformValues[5] = 0.0f;
       
   167     m_colorTransformValues[6] = 0.0f;
       
   168     m_colorTransformValues[7] = 0.0f;
       
   169 }
       
   170 
       
   171 /*-------------------------------------------------------------------*//*!
       
   172 * \brief	VGContext destructor.
       
   173 * \param	
       
   174 * \return	
       
   175 * \note		
       
   176 *//*-------------------------------------------------------------------*/
       
   177 
       
   178 VGContext::~VGContext()
       
   179 {
       
   180 	releasePaint(VG_FILL_PATH | VG_STROKE_PATH);
       
   181     setDefaultDrawable(NULL);
       
   182 
       
   183 	//destroy own images, paths and paints
       
   184 	while(Image* i = m_imageManager->getFirstResource(this))
       
   185 		m_imageManager->removeResource(i);
       
   186 	while(Path* p = m_pathManager->getFirstResource(this))
       
   187 		m_pathManager->removeResource(p);
       
   188 	while(Paint* t = m_paintManager->getFirstResource(this))
       
   189 		m_paintManager->removeResource(t);
       
   190 	while(Font* t = m_fontManager->getFirstResource(this))
       
   191 		m_fontManager->removeResource(t);
       
   192 	while(Surface* t = m_maskLayerManager->getFirstResource(this))
       
   193 		m_maskLayerManager->removeResource(t);
       
   194 
       
   195 	//decrease the reference count of resource managers
       
   196 	if(!m_imageManager->removeReference())
       
   197 		RI_DELETE(m_imageManager);
       
   198 	if(!m_pathManager->removeReference())
       
   199 		RI_DELETE(m_pathManager);
       
   200 	if(!m_paintManager->removeReference())
       
   201 		RI_DELETE(m_paintManager);
       
   202 	if(!m_fontManager->removeReference())
       
   203 		RI_DELETE(m_fontManager);
       
   204 	if(!m_maskLayerManager->removeReference())
       
   205 		RI_DELETE(m_maskLayerManager);
       
   206 }
       
   207 
       
   208 /*-------------------------------------------------------------------*//*!
       
   209 * \brief	Sets new default drawable.
       
   210 * \param	drawable New drawable or NULL when context is unbound
       
   211 * \return	
       
   212 * \note		
       
   213 *//*-------------------------------------------------------------------*/
       
   214 
       
   215 void VGContext::setDefaultDrawable(Drawable* drawable)
       
   216 {
       
   217 	if(m_eglDrawable)
       
   218 	{
       
   219 		if(!m_eglDrawable->removeReference())
       
   220 			RI_DELETE(m_eglDrawable);
       
   221 	}
       
   222 	m_eglDrawable = drawable;
       
   223 	if(m_eglDrawable)
       
   224 	{
       
   225 		m_eglDrawable->addReference();
       
   226 	}
       
   227 }
       
   228 
       
   229 /*-------------------------------------------------------------------*//*!
       
   230 * \brief	Returns true if the given image is generated through any
       
   231 *			context that is shared with this one.
       
   232 * \param	
       
   233 * \return	
       
   234 * \note		
       
   235 *//*-------------------------------------------------------------------*/
       
   236 
       
   237 bool VGContext::isValidImage(VGImage image)
       
   238 {
       
   239 	return m_imageManager->isValid((Image*)image);
       
   240 }
       
   241 
       
   242 /*-------------------------------------------------------------------*//*!
       
   243 * \brief	Returns true if the given path is generated through any
       
   244 *			context that is shared with this one.
       
   245 * \param	
       
   246 * \return	
       
   247 * \note		
       
   248 *//*-------------------------------------------------------------------*/
       
   249 
       
   250 bool VGContext::isValidPath(VGPath path)
       
   251 {
       
   252 	return m_pathManager->isValid((Path*)path);
       
   253 }
       
   254 
       
   255 /*-------------------------------------------------------------------*//*!
       
   256 * \brief	Returns true if the given paint is generated through any
       
   257 *			context that is shared with this one.
       
   258 * \param	
       
   259 * \return	
       
   260 * \note		
       
   261 *//*-------------------------------------------------------------------*/
       
   262 
       
   263 bool VGContext::isValidPaint(VGPaint paint)
       
   264 {
       
   265 	return m_paintManager->isValid((Paint*)paint);
       
   266 }
       
   267 
       
   268 /*-------------------------------------------------------------------*//*!
       
   269 * \brief	Returns true if the given font is generated through any
       
   270 *			context that is shared with this one.
       
   271 * \param	
       
   272 * \return	
       
   273 * \note		
       
   274 *//*-------------------------------------------------------------------*/
       
   275 
       
   276 bool VGContext::isValidFont(VGFont font)
       
   277 {
       
   278 	return m_fontManager->isValid((Font*)font);
       
   279 }
       
   280 
       
   281 /*-------------------------------------------------------------------*//*!
       
   282 * \brief	Returns true if the given mask layer is generated through any
       
   283 *			context that is shared with this one.
       
   284 * \param	
       
   285 * \return	
       
   286 * \note		
       
   287 *//*-------------------------------------------------------------------*/
       
   288 
       
   289 bool VGContext::isValidMaskLayer(VGMaskLayer layer)
       
   290 {
       
   291 	return m_maskLayerManager->isValid((Surface*)layer);
       
   292 }
       
   293 
       
   294 /*-------------------------------------------------------------------*//*!
       
   295 * \brief	Releases the given paint objects of the context.
       
   296 * \param	
       
   297 * \return	
       
   298 * \note		
       
   299 *//*-------------------------------------------------------------------*/
       
   300 
       
   301 void VGContext::releasePaint(VGbitfield paintModes)
       
   302 {
       
   303 	if(paintModes & VG_FILL_PATH)
       
   304 	{
       
   305 		//release previous paint
       
   306 		Paint* prev = (Paint*)m_fillPaint;
       
   307 		if(prev)
       
   308 		{
       
   309 			if(!prev->removeReference())
       
   310 				RI_DELETE(prev);
       
   311 		}
       
   312 		m_fillPaint = VG_INVALID_HANDLE;
       
   313 	}
       
   314 	if(paintModes & VG_STROKE_PATH)
       
   315 	{
       
   316 		//release previous paint
       
   317 		Paint* prev = (Paint*)m_strokePaint;
       
   318 		if(prev)
       
   319 		{
       
   320 			if(!prev->removeReference())
       
   321 				RI_DELETE(prev);
       
   322 		}
       
   323 		m_strokePaint = VG_INVALID_HANDLE;
       
   324 	}
       
   325 }
       
   326 
       
   327 //==============================================================================================
       
   328 
       
   329 }	//namespace OpenVGRI