xref: /trunk/main/canvas/source/tools/surface.cxx (revision cdf0e10c)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
29*cdf0e10cSrcweir #include "precompiled_canvas.hxx"
30*cdf0e10cSrcweir 
31*cdf0e10cSrcweir #include "surface.hxx"
32*cdf0e10cSrcweir #include <basegfx/polygon/b2dpolygonclipper.hxx>
33*cdf0e10cSrcweir #include <basegfx/matrix/b2dhommatrixtools.hxx>
34*cdf0e10cSrcweir #include <comphelper/scopeguard.hxx>
35*cdf0e10cSrcweir #include <boost/bind.hpp>
36*cdf0e10cSrcweir 
37*cdf0e10cSrcweir namespace canvas
38*cdf0e10cSrcweir {
39*cdf0e10cSrcweir 
40*cdf0e10cSrcweir 	//////////////////////////////////////////////////////////////////////////////////
41*cdf0e10cSrcweir 	// Surface::Surface
42*cdf0e10cSrcweir 	//////////////////////////////////////////////////////////////////////////////////
43*cdf0e10cSrcweir 
44*cdf0e10cSrcweir 	Surface::Surface( const PageManagerSharedPtr&  rPageManager,
45*cdf0e10cSrcweir 					  const IColorBufferSharedPtr& rColorBuffer,
46*cdf0e10cSrcweir 					  const ::basegfx::B2IPoint&   rPos,
47*cdf0e10cSrcweir 					  const ::basegfx::B2ISize&    rSize ) :
48*cdf0e10cSrcweir 		mpColorBuffer(rColorBuffer),
49*cdf0e10cSrcweir 		mpPageManager(rPageManager),
50*cdf0e10cSrcweir 		mpFragment(),
51*cdf0e10cSrcweir 		maSourceOffset(rPos),
52*cdf0e10cSrcweir 		maSize(rSize),
53*cdf0e10cSrcweir 		mbIsDirty(true)
54*cdf0e10cSrcweir 	{
55*cdf0e10cSrcweir 	}
56*cdf0e10cSrcweir 
57*cdf0e10cSrcweir 	//////////////////////////////////////////////////////////////////////////////////
58*cdf0e10cSrcweir 	// Surface::~Surface
59*cdf0e10cSrcweir 	//////////////////////////////////////////////////////////////////////////////////
60*cdf0e10cSrcweir 
61*cdf0e10cSrcweir 	Surface::~Surface()
62*cdf0e10cSrcweir 	{
63*cdf0e10cSrcweir 		if(mpFragment)
64*cdf0e10cSrcweir 			mpPageManager->free(mpFragment);
65*cdf0e10cSrcweir 	}
66*cdf0e10cSrcweir 
67*cdf0e10cSrcweir 	//////////////////////////////////////////////////////////////////////////////////
68*cdf0e10cSrcweir 	// Surface::getUVCoords
69*cdf0e10cSrcweir 	//////////////////////////////////////////////////////////////////////////////////
70*cdf0e10cSrcweir 
71*cdf0e10cSrcweir 	void Surface::setColorBufferDirty()
72*cdf0e10cSrcweir 	{
73*cdf0e10cSrcweir 		mbIsDirty=true;
74*cdf0e10cSrcweir 	}
75*cdf0e10cSrcweir 
76*cdf0e10cSrcweir 	//////////////////////////////////////////////////////////////////////////////////
77*cdf0e10cSrcweir 	// Surface::getUVCoords
78*cdf0e10cSrcweir 	//////////////////////////////////////////////////////////////////////////////////
79*cdf0e10cSrcweir 
80*cdf0e10cSrcweir 	basegfx::B2DRectangle Surface::getUVCoords() const
81*cdf0e10cSrcweir 	{
82*cdf0e10cSrcweir 		::basegfx::B2ISize aPageSize(mpPageManager->getPageSize());
83*cdf0e10cSrcweir 		::basegfx::B2IPoint aDestOffset;
84*cdf0e10cSrcweir         if( mpFragment )
85*cdf0e10cSrcweir             aDestOffset = mpFragment->getPos();
86*cdf0e10cSrcweir 
87*cdf0e10cSrcweir 		const double pw( aPageSize.getX() );
88*cdf0e10cSrcweir 		const double ph( aPageSize.getY() );
89*cdf0e10cSrcweir 		const double ox( aDestOffset.getX() );
90*cdf0e10cSrcweir 		const double oy( aDestOffset.getY() );
91*cdf0e10cSrcweir 		const double sx( maSize.getX() );
92*cdf0e10cSrcweir 		const double sy( maSize.getY() );
93*cdf0e10cSrcweir 
94*cdf0e10cSrcweir 		return ::basegfx::B2DRectangle( ox/pw,
95*cdf0e10cSrcweir                                         oy/ph,
96*cdf0e10cSrcweir                                         (ox+sx)/pw,
97*cdf0e10cSrcweir                                         (oy+sy)/ph );
98*cdf0e10cSrcweir 	}
99*cdf0e10cSrcweir 
100*cdf0e10cSrcweir 	//////////////////////////////////////////////////////////////////////////////////
101*cdf0e10cSrcweir 	// Surface::getUVCoords
102*cdf0e10cSrcweir 	//////////////////////////////////////////////////////////////////////////////////
103*cdf0e10cSrcweir 
104*cdf0e10cSrcweir 	basegfx::B2DRectangle Surface::getUVCoords( const ::basegfx::B2IPoint& rPos,
105*cdf0e10cSrcweir 												const ::basegfx::B2ISize&  rSize ) const
106*cdf0e10cSrcweir 	{
107*cdf0e10cSrcweir 		::basegfx::B2ISize aPageSize(mpPageManager->getPageSize());
108*cdf0e10cSrcweir 
109*cdf0e10cSrcweir 		const double pw( aPageSize.getX() );
110*cdf0e10cSrcweir 		const double ph( aPageSize.getY() );
111*cdf0e10cSrcweir 		const double ox( rPos.getX() );
112*cdf0e10cSrcweir 		const double oy( rPos.getY() );
113*cdf0e10cSrcweir 		const double sx( rSize.getX() );
114*cdf0e10cSrcweir 		const double sy( rSize.getY() );
115*cdf0e10cSrcweir 
116*cdf0e10cSrcweir 		return ::basegfx::B2DRectangle( ox/pw,
117*cdf0e10cSrcweir                                         oy/ph,
118*cdf0e10cSrcweir                                         (ox+sx)/pw,
119*cdf0e10cSrcweir                                         (oy+sy)/ph );
120*cdf0e10cSrcweir 	}
121*cdf0e10cSrcweir 
122*cdf0e10cSrcweir 	//////////////////////////////////////////////////////////////////////////////////
123*cdf0e10cSrcweir 	// Surface::draw
124*cdf0e10cSrcweir 	//////////////////////////////////////////////////////////////////////////////////
125*cdf0e10cSrcweir 
126*cdf0e10cSrcweir 	bool Surface::draw( double                          fAlpha,
127*cdf0e10cSrcweir 						const ::basegfx::B2DPoint&      rPos,
128*cdf0e10cSrcweir 						const ::basegfx::B2DHomMatrix&  rTransform )
129*cdf0e10cSrcweir 	{
130*cdf0e10cSrcweir 		IRenderModuleSharedPtr pRenderModule(mpPageManager->getRenderModule());
131*cdf0e10cSrcweir 
132*cdf0e10cSrcweir         RenderModuleGuard aGuard( pRenderModule );
133*cdf0e10cSrcweir 
134*cdf0e10cSrcweir 		prepareRendering();
135*cdf0e10cSrcweir 
136*cdf0e10cSrcweir 		// convert size to normalized device coordinates
137*cdf0e10cSrcweir 		const ::basegfx::B2DRectangle& rUV( getUVCoords() );
138*cdf0e10cSrcweir 
139*cdf0e10cSrcweir 		const double u1(rUV.getMinX());
140*cdf0e10cSrcweir 		const double v1(rUV.getMinY());
141*cdf0e10cSrcweir 		const double u2(rUV.getMaxX());
142*cdf0e10cSrcweir 		const double v2(rUV.getMaxY());
143*cdf0e10cSrcweir 
144*cdf0e10cSrcweir 		// concat transforms
145*cdf0e10cSrcweir         // 1) offset of surface subarea
146*cdf0e10cSrcweir         // 2) surface transform
147*cdf0e10cSrcweir 		// 3) translation to output position [rPos]
148*cdf0e10cSrcweir 		// 4) scale to normalized device coordinates
149*cdf0e10cSrcweir 		// 5) flip y-axis
150*cdf0e10cSrcweir 		// 6) translate to account for viewport transform
151*cdf0e10cSrcweir         basegfx::B2DHomMatrix aTransform(basegfx::tools::createTranslateB2DHomMatrix(
152*cdf0e10cSrcweir             maSourceOffset.getX(), maSourceOffset.getY()));
153*cdf0e10cSrcweir         aTransform = aTransform * rTransform;
154*cdf0e10cSrcweir 		aTransform.translate(::basegfx::fround(rPos.getX()),
155*cdf0e10cSrcweir                              ::basegfx::fround(rPos.getY()));
156*cdf0e10cSrcweir 
157*cdf0e10cSrcweir 		/*
158*cdf0e10cSrcweir 			######################################
159*cdf0e10cSrcweir 			######################################
160*cdf0e10cSrcweir 			######################################
161*cdf0e10cSrcweir 
162*cdf0e10cSrcweir 			    		   Y
163*cdf0e10cSrcweir 			    		   ^+1
164*cdf0e10cSrcweir 			    		   |
165*cdf0e10cSrcweir 			       2	   |	   3
166*cdf0e10cSrcweir 			    	 x------------x
167*cdf0e10cSrcweir 			    	 |	   |	  |
168*cdf0e10cSrcweir 			 		 |	   |	  |
169*cdf0e10cSrcweir 			   ------|-----O------|------>X
170*cdf0e10cSrcweir 			   -1  	 |	   |	  |		+1
171*cdf0e10cSrcweir 			    	 |	   |	  |
172*cdf0e10cSrcweir 			    	 x------------x
173*cdf0e10cSrcweir 			        1      |       0
174*cdf0e10cSrcweir 			    	 	   |
175*cdf0e10cSrcweir 			    		   |-1
176*cdf0e10cSrcweir 
177*cdf0e10cSrcweir 			######################################
178*cdf0e10cSrcweir 			######################################
179*cdf0e10cSrcweir 			######################################
180*cdf0e10cSrcweir 		*/
181*cdf0e10cSrcweir 
182*cdf0e10cSrcweir 		const ::basegfx::B2DPoint& p0(aTransform * ::basegfx::B2DPoint(maSize.getX(),maSize.getY()));
183*cdf0e10cSrcweir 		const ::basegfx::B2DPoint& p1(aTransform * ::basegfx::B2DPoint(0.0,maSize.getY()));
184*cdf0e10cSrcweir 		const ::basegfx::B2DPoint& p2(aTransform * ::basegfx::B2DPoint(0.0,0.0));
185*cdf0e10cSrcweir 		const ::basegfx::B2DPoint& p3(aTransform * ::basegfx::B2DPoint(maSize.getX(),0.0));
186*cdf0e10cSrcweir 
187*cdf0e10cSrcweir 		canvas::Vertex vertex;
188*cdf0e10cSrcweir 		vertex.r = 1.0f;
189*cdf0e10cSrcweir 		vertex.g = 1.0f;
190*cdf0e10cSrcweir 		vertex.b = 1.0f;
191*cdf0e10cSrcweir 		vertex.a = static_cast<float>(fAlpha);
192*cdf0e10cSrcweir 		vertex.z = 0.0f;
193*cdf0e10cSrcweir 
194*cdf0e10cSrcweir         {
195*cdf0e10cSrcweir             pRenderModule->beginPrimitive( canvas::IRenderModule::PRIMITIVE_TYPE_QUAD );
196*cdf0e10cSrcweir 
197*cdf0e10cSrcweir             // issue an endPrimitive() when leaving the scope
198*cdf0e10cSrcweir             const ::comphelper::ScopeGuard aScopeGuard(
199*cdf0e10cSrcweir                 boost::bind( &::canvas::IRenderModule::endPrimitive,
200*cdf0e10cSrcweir                              ::boost::ref(pRenderModule) ) );
201*cdf0e10cSrcweir 
202*cdf0e10cSrcweir             vertex.u=static_cast<float>(u2); vertex.v=static_cast<float>(v2);
203*cdf0e10cSrcweir             vertex.x=static_cast<float>(p0.getX()); vertex.y=static_cast<float>(p0.getY());
204*cdf0e10cSrcweir             pRenderModule->pushVertex(vertex);
205*cdf0e10cSrcweir 
206*cdf0e10cSrcweir             vertex.u=static_cast<float>(u1); vertex.v=static_cast<float>(v2);
207*cdf0e10cSrcweir             vertex.x=static_cast<float>(p1.getX()); vertex.y=static_cast<float>(p1.getY());
208*cdf0e10cSrcweir             pRenderModule->pushVertex(vertex);
209*cdf0e10cSrcweir 
210*cdf0e10cSrcweir             vertex.u=static_cast<float>(u1); vertex.v=static_cast<float>(v1);
211*cdf0e10cSrcweir             vertex.x=static_cast<float>(p2.getX()); vertex.y=static_cast<float>(p2.getY());
212*cdf0e10cSrcweir             pRenderModule->pushVertex(vertex);
213*cdf0e10cSrcweir 
214*cdf0e10cSrcweir             vertex.u=static_cast<float>(u2); vertex.v=static_cast<float>(v1);
215*cdf0e10cSrcweir             vertex.x=static_cast<float>(p3.getX()); vertex.y=static_cast<float>(p3.getY());
216*cdf0e10cSrcweir             pRenderModule->pushVertex(vertex);
217*cdf0e10cSrcweir         }
218*cdf0e10cSrcweir 
219*cdf0e10cSrcweir 		return !(pRenderModule->isError());
220*cdf0e10cSrcweir 	}
221*cdf0e10cSrcweir 
222*cdf0e10cSrcweir 	//////////////////////////////////////////////////////////////////////////////////
223*cdf0e10cSrcweir 	// Surface::drawRectangularArea
224*cdf0e10cSrcweir 	//////////////////////////////////////////////////////////////////////////////////
225*cdf0e10cSrcweir 
226*cdf0e10cSrcweir 	bool Surface::drawRectangularArea(
227*cdf0e10cSrcweir 						double                         fAlpha,
228*cdf0e10cSrcweir                         const ::basegfx::B2DPoint&     rPos,
229*cdf0e10cSrcweir 						const ::basegfx::B2DRectangle& rArea,
230*cdf0e10cSrcweir 						const ::basegfx::B2DHomMatrix& rTransform )
231*cdf0e10cSrcweir 	{
232*cdf0e10cSrcweir         if( rArea.isEmpty() )
233*cdf0e10cSrcweir             return true; // immediate exit for empty area
234*cdf0e10cSrcweir 
235*cdf0e10cSrcweir 		IRenderModuleSharedPtr pRenderModule(mpPageManager->getRenderModule());
236*cdf0e10cSrcweir 
237*cdf0e10cSrcweir 		RenderModuleGuard aGuard( pRenderModule );
238*cdf0e10cSrcweir 
239*cdf0e10cSrcweir 		prepareRendering();
240*cdf0e10cSrcweir 
241*cdf0e10cSrcweir 		// these positions are relative to the texture
242*cdf0e10cSrcweir 		::basegfx::B2IPoint aPos1(
243*cdf0e10cSrcweir 			::basegfx::fround(rArea.getMinimum().getX()),
244*cdf0e10cSrcweir 			::basegfx::fround(rArea.getMinimum().getY()));
245*cdf0e10cSrcweir 		::basegfx::B2IPoint aPos2(
246*cdf0e10cSrcweir 			::basegfx::fround(rArea.getMaximum().getX()),
247*cdf0e10cSrcweir 			::basegfx::fround(rArea.getMaximum().getY()) );
248*cdf0e10cSrcweir 
249*cdf0e10cSrcweir 		// clip the positions to the area this surface covers
250*cdf0e10cSrcweir 		aPos1.setX(::std::max(aPos1.getX(),maSourceOffset.getX()));
251*cdf0e10cSrcweir 		aPos1.setY(::std::max(aPos1.getY(),maSourceOffset.getY()));
252*cdf0e10cSrcweir 		aPos2.setX(::std::min(aPos2.getX(),maSourceOffset.getX()+maSize.getX()));
253*cdf0e10cSrcweir 		aPos2.setY(::std::min(aPos2.getY(),maSourceOffset.getY()+maSize.getY()));
254*cdf0e10cSrcweir 
255*cdf0e10cSrcweir 		// if the resulting area is empty, return immediately
256*cdf0e10cSrcweir 		::basegfx::B2IVector aSize(aPos2 - aPos1);
257*cdf0e10cSrcweir 		if(aSize.getX() <= 0 || aSize.getY() <= 0)
258*cdf0e10cSrcweir 			return true;
259*cdf0e10cSrcweir 
260*cdf0e10cSrcweir         ::basegfx::B2IPoint aDestOffset;
261*cdf0e10cSrcweir         if( mpFragment )
262*cdf0e10cSrcweir             aDestOffset = mpFragment->getPos();
263*cdf0e10cSrcweir 
264*cdf0e10cSrcweir 		// convert size to normalized device coordinates
265*cdf0e10cSrcweir 		const ::basegfx::B2DRectangle& rUV(
266*cdf0e10cSrcweir             getUVCoords(aPos1 - maSourceOffset + aDestOffset,
267*cdf0e10cSrcweir                         aSize) );
268*cdf0e10cSrcweir 		const double u1(rUV.getMinX());
269*cdf0e10cSrcweir 		const double v1(rUV.getMinY());
270*cdf0e10cSrcweir 		const double u2(rUV.getMaxX());
271*cdf0e10cSrcweir 		const double v2(rUV.getMaxY());
272*cdf0e10cSrcweir 
273*cdf0e10cSrcweir 		// concatenate transforms
274*cdf0e10cSrcweir         // 1) offset of surface subarea
275*cdf0e10cSrcweir         // 2) surface transform
276*cdf0e10cSrcweir 		// 3) translation to output position [rPos]
277*cdf0e10cSrcweir         basegfx::B2DHomMatrix aTransform(basegfx::tools::createTranslateB2DHomMatrix(aPos1.getX(), aPos1.getY()));
278*cdf0e10cSrcweir         aTransform = aTransform * rTransform;
279*cdf0e10cSrcweir 		aTransform.translate(::basegfx::fround(rPos.getX()),
280*cdf0e10cSrcweir                              ::basegfx::fround(rPos.getY()));
281*cdf0e10cSrcweir 
282*cdf0e10cSrcweir 
283*cdf0e10cSrcweir 		/*
284*cdf0e10cSrcweir 			######################################
285*cdf0e10cSrcweir 			######################################
286*cdf0e10cSrcweir 			######################################
287*cdf0e10cSrcweir 
288*cdf0e10cSrcweir 			    		   Y
289*cdf0e10cSrcweir 			    		   ^+1
290*cdf0e10cSrcweir 			    		   |
291*cdf0e10cSrcweir 			       2	   |	   3
292*cdf0e10cSrcweir 			    	 x------------x
293*cdf0e10cSrcweir 			    	 |	   |	  |
294*cdf0e10cSrcweir 			 		 |	   |	  |
295*cdf0e10cSrcweir 			   ------|-----O------|------>X
296*cdf0e10cSrcweir 			   -1  	 |	   |	  |		+1
297*cdf0e10cSrcweir 			    	 |	   |	  |
298*cdf0e10cSrcweir 			    	 x------------x
299*cdf0e10cSrcweir 			        1      |       0
300*cdf0e10cSrcweir 			    	 	   |
301*cdf0e10cSrcweir 			    		   |-1
302*cdf0e10cSrcweir 
303*cdf0e10cSrcweir 			######################################
304*cdf0e10cSrcweir 			######################################
305*cdf0e10cSrcweir 			######################################
306*cdf0e10cSrcweir 		*/
307*cdf0e10cSrcweir 
308*cdf0e10cSrcweir 		const ::basegfx::B2DPoint& p0(aTransform * ::basegfx::B2DPoint(aSize.getX(),aSize.getY()));
309*cdf0e10cSrcweir 		const ::basegfx::B2DPoint& p1(aTransform * ::basegfx::B2DPoint(0.0,			aSize.getY()));
310*cdf0e10cSrcweir 		const ::basegfx::B2DPoint& p2(aTransform * ::basegfx::B2DPoint(0.0,			0.0));
311*cdf0e10cSrcweir 		const ::basegfx::B2DPoint& p3(aTransform * ::basegfx::B2DPoint(aSize.getX(),0.0));
312*cdf0e10cSrcweir 
313*cdf0e10cSrcweir 		canvas::Vertex vertex;
314*cdf0e10cSrcweir 		vertex.r = 1.0f;
315*cdf0e10cSrcweir 		vertex.g = 1.0f;
316*cdf0e10cSrcweir 		vertex.b = 1.0f;
317*cdf0e10cSrcweir 		vertex.a = static_cast<float>(fAlpha);
318*cdf0e10cSrcweir 		vertex.z = 0.0f;
319*cdf0e10cSrcweir 
320*cdf0e10cSrcweir         {
321*cdf0e10cSrcweir             pRenderModule->beginPrimitive( canvas::IRenderModule::PRIMITIVE_TYPE_QUAD );
322*cdf0e10cSrcweir 
323*cdf0e10cSrcweir             // issue an endPrimitive() when leaving the scope
324*cdf0e10cSrcweir             const ::comphelper::ScopeGuard aScopeGuard(
325*cdf0e10cSrcweir                 boost::bind( &::canvas::IRenderModule::endPrimitive,
326*cdf0e10cSrcweir                              ::boost::ref(pRenderModule) ) );
327*cdf0e10cSrcweir 
328*cdf0e10cSrcweir             vertex.u=static_cast<float>(u2); vertex.v=static_cast<float>(v2);
329*cdf0e10cSrcweir             vertex.x=static_cast<float>(p0.getX()); vertex.y=static_cast<float>(p0.getY());
330*cdf0e10cSrcweir             pRenderModule->pushVertex(vertex);
331*cdf0e10cSrcweir 
332*cdf0e10cSrcweir             vertex.u=static_cast<float>(u1); vertex.v=static_cast<float>(v2);
333*cdf0e10cSrcweir             vertex.x=static_cast<float>(p1.getX()); vertex.y=static_cast<float>(p1.getY());
334*cdf0e10cSrcweir             pRenderModule->pushVertex(vertex);
335*cdf0e10cSrcweir 
336*cdf0e10cSrcweir             vertex.u=static_cast<float>(u1); vertex.v=static_cast<float>(v1);
337*cdf0e10cSrcweir             vertex.x=static_cast<float>(p2.getX()); vertex.y=static_cast<float>(p2.getY());
338*cdf0e10cSrcweir             pRenderModule->pushVertex(vertex);
339*cdf0e10cSrcweir 
340*cdf0e10cSrcweir             vertex.u=static_cast<float>(u2); vertex.v=static_cast<float>(v1);
341*cdf0e10cSrcweir             vertex.x=static_cast<float>(p3.getX()); vertex.y=static_cast<float>(p3.getY());
342*cdf0e10cSrcweir             pRenderModule->pushVertex(vertex);
343*cdf0e10cSrcweir         }
344*cdf0e10cSrcweir 
345*cdf0e10cSrcweir 		return !(pRenderModule->isError());
346*cdf0e10cSrcweir 	}
347*cdf0e10cSrcweir 
348*cdf0e10cSrcweir 	//////////////////////////////////////////////////////////////////////////////////
349*cdf0e10cSrcweir 	// Surface::drawWithClip
350*cdf0e10cSrcweir 	//////////////////////////////////////////////////////////////////////////////////
351*cdf0e10cSrcweir 
352*cdf0e10cSrcweir 	bool Surface::drawWithClip( double                          fAlpha,
353*cdf0e10cSrcweir 								const ::basegfx::B2DPoint&      rPos,
354*cdf0e10cSrcweir 								const ::basegfx::B2DPolygon&    rClipPoly,
355*cdf0e10cSrcweir 								const ::basegfx::B2DHomMatrix&  rTransform )
356*cdf0e10cSrcweir 	{
357*cdf0e10cSrcweir 		IRenderModuleSharedPtr pRenderModule(mpPageManager->getRenderModule());
358*cdf0e10cSrcweir 
359*cdf0e10cSrcweir 		RenderModuleGuard aGuard( pRenderModule );
360*cdf0e10cSrcweir 
361*cdf0e10cSrcweir 		prepareRendering();
362*cdf0e10cSrcweir 
363*cdf0e10cSrcweir 		// untransformed surface rectangle, relative to the whole
364*cdf0e10cSrcweir 		// image (note: this surface might actually only be a tile of
365*cdf0e10cSrcweir 		// the whole image, with non-zero maSourceOffset)
366*cdf0e10cSrcweir 		const double x1(maSourceOffset.getX());
367*cdf0e10cSrcweir 		const double y1(maSourceOffset.getY());
368*cdf0e10cSrcweir 		const double w(maSize.getX());
369*cdf0e10cSrcweir 		const double h(maSize.getY());
370*cdf0e10cSrcweir 		const double x2(x1+w);
371*cdf0e10cSrcweir 		const double y2(y1+h);
372*cdf0e10cSrcweir 		const ::basegfx::B2DRectangle aSurfaceClipRect(x1,y1,x2,y2);
373*cdf0e10cSrcweir 
374*cdf0e10cSrcweir 		// concatenate transforms
375*cdf0e10cSrcweir 		// we use 'fround' here to avoid rounding errors. the vertices will
376*cdf0e10cSrcweir 		// be transformed by the overall transform and uv coordinates will
377*cdf0e10cSrcweir 		// be calculated from the result, and this is why we need to use
378*cdf0e10cSrcweir 		// integer coordinates here...
379*cdf0e10cSrcweir         basegfx::B2DHomMatrix aTransform;
380*cdf0e10cSrcweir         aTransform = aTransform * rTransform;
381*cdf0e10cSrcweir 		aTransform.translate(::basegfx::fround(rPos.getX()),
382*cdf0e10cSrcweir                              ::basegfx::fround(rPos.getY()));
383*cdf0e10cSrcweir 
384*cdf0e10cSrcweir 		/*
385*cdf0e10cSrcweir 			######################################
386*cdf0e10cSrcweir 			######################################
387*cdf0e10cSrcweir 			######################################
388*cdf0e10cSrcweir 
389*cdf0e10cSrcweir 			    		   Y
390*cdf0e10cSrcweir 			    		   ^+1
391*cdf0e10cSrcweir 			    		   |
392*cdf0e10cSrcweir 			       2	   |	   3
393*cdf0e10cSrcweir 			    	 x------------x
394*cdf0e10cSrcweir 			    	 |	   |	  |
395*cdf0e10cSrcweir 			 		 |	   |	  |
396*cdf0e10cSrcweir 			   ------|-----O------|------>X
397*cdf0e10cSrcweir 			   -1  	 |	   |	  |		+1
398*cdf0e10cSrcweir 			    	 |	   |	  |
399*cdf0e10cSrcweir 			    	 x------------x
400*cdf0e10cSrcweir 			        1      |       0
401*cdf0e10cSrcweir 			    	 	   |
402*cdf0e10cSrcweir 			    		   |-1
403*cdf0e10cSrcweir 
404*cdf0e10cSrcweir 			######################################
405*cdf0e10cSrcweir 			######################################
406*cdf0e10cSrcweir 			######################################
407*cdf0e10cSrcweir 		*/
408*cdf0e10cSrcweir 
409*cdf0e10cSrcweir 		// uv coordinates that map the surface rectangle
410*cdf0e10cSrcweir 		// to the destination rectangle.
411*cdf0e10cSrcweir 		const ::basegfx::B2DRectangle& rUV( getUVCoords() );
412*cdf0e10cSrcweir 
413*cdf0e10cSrcweir 		basegfx::B2DPolygon rTriangleList(basegfx::tools::clipTriangleListOnRange(rClipPoly,
414*cdf0e10cSrcweir                                                                                   aSurfaceClipRect));
415*cdf0e10cSrcweir 
416*cdf0e10cSrcweir 		// Push vertices to backend renderer
417*cdf0e10cSrcweir 		if(const sal_uInt32 nVertexCount = rTriangleList.count())
418*cdf0e10cSrcweir 		{
419*cdf0e10cSrcweir 			canvas::Vertex vertex;
420*cdf0e10cSrcweir 			vertex.r = 1.0f;
421*cdf0e10cSrcweir 			vertex.g = 1.0f;
422*cdf0e10cSrcweir 			vertex.b = 1.0f;
423*cdf0e10cSrcweir 			vertex.a = static_cast<float>(fAlpha);
424*cdf0e10cSrcweir 			vertex.z = 0.0f;
425*cdf0e10cSrcweir 
426*cdf0e10cSrcweir #if defined(TRIANGLE_LOG) && defined(DBG_UTIL)
427*cdf0e10cSrcweir 			OSL_TRACE( "Surface::draw(): numvertices %d numtriangles %d\n",
428*cdf0e10cSrcweir 						nVertexCount,
429*cdf0e10cSrcweir 						nVertexCount/3 );
430*cdf0e10cSrcweir #endif
431*cdf0e10cSrcweir 
432*cdf0e10cSrcweir 			pRenderModule->beginPrimitive( canvas::IRenderModule::PRIMITIVE_TYPE_TRIANGLE );
433*cdf0e10cSrcweir 
434*cdf0e10cSrcweir 			// issue an endPrimitive() when leaving the scope
435*cdf0e10cSrcweir 			const ::comphelper::ScopeGuard aScopeGuard(
436*cdf0e10cSrcweir 				boost::bind( &::canvas::IRenderModule::endPrimitive,
437*cdf0e10cSrcweir 								::boost::ref(pRenderModule) ) );
438*cdf0e10cSrcweir 
439*cdf0e10cSrcweir 			for(sal_uInt32 nIndex=0; nIndex<nVertexCount; ++nIndex)
440*cdf0e10cSrcweir 			{
441*cdf0e10cSrcweir 				const basegfx::B2DPoint &aPoint = rTriangleList.getB2DPoint(nIndex);
442*cdf0e10cSrcweir 				basegfx::B2DPoint aTransformedPoint(aTransform * aPoint);
443*cdf0e10cSrcweir 				const double tu(((aPoint.getX()-aSurfaceClipRect.getMinX())*rUV.getWidth()/w)+rUV.getMinX());
444*cdf0e10cSrcweir                 const double tv(((aPoint.getY()-aSurfaceClipRect.getMinY())*rUV.getHeight()/h)+rUV.getMinY());
445*cdf0e10cSrcweir 				vertex.u=static_cast<float>(tu);
446*cdf0e10cSrcweir 				vertex.v=static_cast<float>(tv);
447*cdf0e10cSrcweir 				vertex.x=static_cast<float>(aTransformedPoint.getX());
448*cdf0e10cSrcweir 				vertex.y=static_cast<float>(aTransformedPoint.getY());
449*cdf0e10cSrcweir 				pRenderModule->pushVertex(vertex);
450*cdf0e10cSrcweir 			}
451*cdf0e10cSrcweir 		}
452*cdf0e10cSrcweir 
453*cdf0e10cSrcweir 		return !(pRenderModule->isError());
454*cdf0e10cSrcweir 	}
455*cdf0e10cSrcweir 
456*cdf0e10cSrcweir 	//////////////////////////////////////////////////////////////////////////////////
457*cdf0e10cSrcweir 	// Surface::prepareRendering
458*cdf0e10cSrcweir 	//////////////////////////////////////////////////////////////////////////////////
459*cdf0e10cSrcweir 
460*cdf0e10cSrcweir 	void Surface::prepareRendering()
461*cdf0e10cSrcweir 	{
462*cdf0e10cSrcweir 		mpPageManager->validatePages();
463*cdf0e10cSrcweir 
464*cdf0e10cSrcweir 		// clients requested to draw from this surface, therefore one
465*cdf0e10cSrcweir 		// of the above implemented concrete rendering operations
466*cdf0e10cSrcweir 		// was triggered. we therefore need to ask the pagemanager
467*cdf0e10cSrcweir 		// to allocate some space for the fragment we're dedicated to.
468*cdf0e10cSrcweir 		if(!(mpFragment))
469*cdf0e10cSrcweir 		{
470*cdf0e10cSrcweir 			mpFragment = mpPageManager->allocateSpace(maSize);
471*cdf0e10cSrcweir             if( mpFragment )
472*cdf0e10cSrcweir             {
473*cdf0e10cSrcweir 			    mpFragment->setColorBuffer(mpColorBuffer);
474*cdf0e10cSrcweir     			mpFragment->setSourceOffset(maSourceOffset);
475*cdf0e10cSrcweir             }
476*cdf0e10cSrcweir 		}
477*cdf0e10cSrcweir 
478*cdf0e10cSrcweir         if( mpFragment )
479*cdf0e10cSrcweir         {
480*cdf0e10cSrcweir 		    // now we need to 'select' the fragment, which will in turn
481*cdf0e10cSrcweir 		    // pull informations from the image on demand.
482*cdf0e10cSrcweir 		    // in case this fragment is still not located on any of the
483*cdf0e10cSrcweir 		    // available pages ['naked'], we force the page manager to
484*cdf0e10cSrcweir 		    // do it now, no way to defer this any longer...
485*cdf0e10cSrcweir 		    if(!(mpFragment->select(mbIsDirty)))
486*cdf0e10cSrcweir 			    mpPageManager->nakedFragment(mpFragment);
487*cdf0e10cSrcweir 
488*cdf0e10cSrcweir         }
489*cdf0e10cSrcweir 	    mbIsDirty=false;
490*cdf0e10cSrcweir 	}
491*cdf0e10cSrcweir 
492*cdf0e10cSrcweir 	//////////////////////////////////////////////////////////////////////////////////
493*cdf0e10cSrcweir 	// End of file
494*cdf0e10cSrcweir 	//////////////////////////////////////////////////////////////////////////////////
495*cdf0e10cSrcweir }
496*cdf0e10cSrcweir 
497