1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_canvas.hxx"
26 
27 #include <canvas/debug.hxx>
28 #include <canvas/verbosetrace.hxx>
29 #include <tools/diagnose_ex.h>
30 
31 #include <rtl/logfile.hxx>
32 #include <rtl/math.hxx>
33 
34 #include <canvas/canvastools.hxx>
35 
36 #include <basegfx/matrix/b2dhommatrix.hxx>
37 #include <basegfx/point/b2dpoint.hxx>
38 
39 #include "cairo_canvascustomsprite.hxx"
40 #include "cairo_spritecanvas.hxx"
41 
42 
43 using namespace ::cairo;
44 using namespace ::com::sun::star;
45 
46 namespace cairocanvas
47 {
CanvasCustomSprite(const::com::sun::star::geometry::RealSize2D & rSpriteSize,const SpriteCanvasRef & rRefDevice)48     CanvasCustomSprite::CanvasCustomSprite( const ::com::sun::star::geometry::RealSize2D& 	rSpriteSize,
49                                             const SpriteCanvasRef&                          rRefDevice ) :
50         mpSpriteCanvas( rRefDevice ),
51 		maSize( ::canvas::tools::roundUp( rSpriteSize.Width ),
52 				::canvas::tools::roundUp( rSpriteSize.Height ) )
53     {
54         ENSURE_OR_THROW( rRefDevice.get(),
55                           "CanvasCustomSprite::CanvasCustomSprite(): Invalid sprite canvas" );
56 
57 		OSL_TRACE("sprite size: %d, %d",
58 				  ::canvas::tools::roundUp( rSpriteSize.Width ),
59 				  ::canvas::tools::roundUp( rSpriteSize.Height ));
60 
61 		mpBufferSurface = mpSpriteCanvas->createSurface( maSize );
62 
63         maCanvasHelper.init( maSize,
64                              *rRefDevice,
65                              rRefDevice.get() );
66 		maCanvasHelper.setSurface( mpBufferSurface, true );
67 
68         maSpriteHelper.init( rSpriteSize,
69                              rRefDevice );
70 		maSpriteHelper.setSurface( mpBufferSurface );
71 
72         // clear sprite to 100% transparent
73         maCanvasHelper.clear();
74     }
75 
disposing()76     void SAL_CALL CanvasCustomSprite::disposing()
77     {
78         ::osl::MutexGuard aGuard( m_aMutex );
79 
80         mpSpriteCanvas.clear();
81 		mpBufferSurface.reset();
82 
83         // forward to parent
84         CanvasCustomSpriteBaseT::disposing();
85     }
86 
redraw(const CairoSharedPtr & pCairo,bool bBufferedUpdate) const87     void CanvasCustomSprite::redraw( const CairoSharedPtr& pCairo,
88                                      bool                  bBufferedUpdate ) const
89     {
90         ::osl::MutexGuard aGuard( m_aMutex );
91 
92         redraw( pCairo, maSpriteHelper.getPosPixel(), bBufferedUpdate );
93     }
94 
redraw(const CairoSharedPtr & pCairo,const::basegfx::B2DPoint & rOrigOutputPos,bool bBufferedUpdate) const95     void CanvasCustomSprite::redraw( const CairoSharedPtr&      pCairo,
96                                      const ::basegfx::B2DPoint& rOrigOutputPos,
97                                      bool                       bBufferedUpdate ) const
98     {
99         ::osl::MutexGuard aGuard( m_aMutex );
100 
101         maSpriteHelper.redraw( pCairo,
102                                rOrigOutputPos,
103                                mbSurfaceDirty,
104                                bBufferedUpdate );
105 
106         mbSurfaceDirty = false;
107     }
108 
repaint(const SurfaceSharedPtr & pSurface,const rendering::ViewState & viewState,const rendering::RenderState & renderState)109     bool CanvasCustomSprite::repaint( const SurfaceSharedPtr&       pSurface,
110                                       const rendering::ViewState&	viewState,
111                                       const rendering::RenderState&	renderState )
112     {
113         return maCanvasHelper.repaint( pSurface, viewState, renderState );
114     }
115 
getSurface()116     SurfaceSharedPtr CanvasCustomSprite::getSurface()
117     {
118         return mpBufferSurface;
119     }
120 
createSurface(const::basegfx::B2ISize & rSize,Content aContent)121     SurfaceSharedPtr CanvasCustomSprite::createSurface( const ::basegfx::B2ISize& rSize, Content aContent )
122     {
123         return mpSpriteCanvas->createSurface(rSize,aContent);
124     }
125 
createSurface(::Bitmap & rBitmap)126     SurfaceSharedPtr CanvasCustomSprite::createSurface( ::Bitmap& rBitmap )
127     {
128         return mpSpriteCanvas->createSurface(rBitmap);
129     }
130 
changeSurface(bool bHasAlpha,bool bCopyContent)131 	SurfaceSharedPtr CanvasCustomSprite::changeSurface( bool bHasAlpha, bool bCopyContent )
132 	{
133 		if( !bHasAlpha && !bCopyContent )
134 		{
135 			OSL_TRACE("replacing sprite background surface");
136 
137 			mpBufferSurface = mpSpriteCanvas->createSurface( maSize, CAIRO_CONTENT_COLOR );
138 			maSpriteHelper.setSurface( mpBufferSurface );
139 
140 			return mpBufferSurface;
141 		}
142 
143 		return SurfaceSharedPtr();
144 	}
145 
getOutputDevice()146     OutputDevice* CanvasCustomSprite::getOutputDevice()
147     {
148         return mpSpriteCanvas->getOutputDevice();
149     }
150 
151 #define IMPLEMENTATION_NAME "CairoCanvas.CanvasCustomSprite"
152 #define SERVICE_NAME "com.sun.star.rendering.CanvasCustomSprite"
153 
getImplementationName()154     ::rtl::OUString SAL_CALL CanvasCustomSprite::getImplementationName() throw( uno::RuntimeException )
155     {
156         return ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( IMPLEMENTATION_NAME ) );
157     }
158 
supportsService(const::rtl::OUString & ServiceName)159     sal_Bool SAL_CALL CanvasCustomSprite::supportsService( const ::rtl::OUString& ServiceName ) throw( uno::RuntimeException )
160     {
161         return ServiceName.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM ( SERVICE_NAME ) );
162     }
163 
getSupportedServiceNames()164     uno::Sequence< ::rtl::OUString > SAL_CALL CanvasCustomSprite::getSupportedServiceNames()  throw( uno::RuntimeException )
165     {
166         uno::Sequence< ::rtl::OUString > aRet(1);
167         aRet[0] = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM ( SERVICE_NAME ) );
168 
169         return aRet;
170     }
171 }
172