xref: /trunk/main/canvas/source/cairo/cairo_spritecanvas.cxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
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 <canvas/debug.hxx>
32*cdf0e10cSrcweir #include <canvas/verbosetrace.hxx>
33*cdf0e10cSrcweir #include <canvas/canvastools.hxx>
34*cdf0e10cSrcweir #include <tools/diagnose_ex.h>
35*cdf0e10cSrcweir 
36*cdf0e10cSrcweir #include <osl/mutex.hxx>
37*cdf0e10cSrcweir 
38*cdf0e10cSrcweir #include <com/sun/star/registry/XRegistryKey.hpp>
39*cdf0e10cSrcweir #include <com/sun/star/lang/XSingleServiceFactory.hpp>
40*cdf0e10cSrcweir #include <com/sun/star/uno/XComponentContext.hpp>
41*cdf0e10cSrcweir #include <com/sun/star/lang/NoSupportException.hpp>
42*cdf0e10cSrcweir 
43*cdf0e10cSrcweir #include <toolkit/helper/vclunohelper.hxx>
44*cdf0e10cSrcweir 
45*cdf0e10cSrcweir #include <basegfx/matrix/b2dhommatrix.hxx>
46*cdf0e10cSrcweir #include <basegfx/point/b2dpoint.hxx>
47*cdf0e10cSrcweir #include <basegfx/tools/canvastools.hxx>
48*cdf0e10cSrcweir #include <basegfx/numeric/ftools.hxx>
49*cdf0e10cSrcweir 
50*cdf0e10cSrcweir #include "cairo_spritecanvas.hxx"
51*cdf0e10cSrcweir 
52*cdf0e10cSrcweir using namespace ::cairo;
53*cdf0e10cSrcweir using namespace ::com::sun::star;
54*cdf0e10cSrcweir 
55*cdf0e10cSrcweir namespace cairocanvas
56*cdf0e10cSrcweir {
57*cdf0e10cSrcweir     SpriteCanvas::SpriteCanvas( const uno::Sequence< uno::Any >&                aArguments,
58*cdf0e10cSrcweir                                 const uno::Reference< uno::XComponentContext >& rxContext ) :
59*cdf0e10cSrcweir         maArguments(aArguments),
60*cdf0e10cSrcweir         mxComponentContext( rxContext )
61*cdf0e10cSrcweir     {
62*cdf0e10cSrcweir     }
63*cdf0e10cSrcweir 
64*cdf0e10cSrcweir     void SpriteCanvas::initialize()
65*cdf0e10cSrcweir     {
66*cdf0e10cSrcweir         VERBOSE_TRACE("CairoSpriteCanvas created %p\n", this);
67*cdf0e10cSrcweir 
68*cdf0e10cSrcweir         // #i64742# Only call initialize when not in probe mode
69*cdf0e10cSrcweir         if( maArguments.getLength() == 0 )
70*cdf0e10cSrcweir             return;
71*cdf0e10cSrcweir 
72*cdf0e10cSrcweir         /* maArguments:
73*cdf0e10cSrcweir            0: ptr to creating instance (Window or VirtualDevice)
74*cdf0e10cSrcweir            1: SystemEnvData as a streamed Any (or empty for VirtualDevice)
75*cdf0e10cSrcweir            2: current bounds of creating instance
76*cdf0e10cSrcweir            3: bool, denoting always on top state for Window (always false for VirtualDevice)
77*cdf0e10cSrcweir            4: XWindow for creating Window (or empty for VirtualDevice)
78*cdf0e10cSrcweir            5: SystemGraphicsData as a streamed Any
79*cdf0e10cSrcweir          */
80*cdf0e10cSrcweir         ENSURE_ARG_OR_THROW( maArguments.getLength() >= 4 &&
81*cdf0e10cSrcweir                              maArguments[0].getValueTypeClass() == uno::TypeClass_HYPER &&
82*cdf0e10cSrcweir                              maArguments[4].getValueTypeClass() == uno::TypeClass_INTERFACE,
83*cdf0e10cSrcweir                              "CairoSpriteCanvas::initialize: wrong number of arguments, or wrong types" );
84*cdf0e10cSrcweir 
85*cdf0e10cSrcweir         awt::Rectangle aRect;
86*cdf0e10cSrcweir         maArguments[2] >>= aRect;
87*cdf0e10cSrcweir 
88*cdf0e10cSrcweir         sal_Bool bIsFullscreen( sal_False );
89*cdf0e10cSrcweir         maArguments[3] >>= bIsFullscreen;
90*cdf0e10cSrcweir 
91*cdf0e10cSrcweir         uno::Reference< awt::XWindow > xParentWindow;
92*cdf0e10cSrcweir         maArguments[4] >>= xParentWindow;
93*cdf0e10cSrcweir 
94*cdf0e10cSrcweir         Window* pParentWindow = VCLUnoHelper::GetWindow(xParentWindow);
95*cdf0e10cSrcweir         if( !pParentWindow )
96*cdf0e10cSrcweir             throw lang::NoSupportException(
97*cdf0e10cSrcweir                 ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM(
98*cdf0e10cSrcweir                                      "Parent window not VCL window, or canvas out-of-process!")),
99*cdf0e10cSrcweir                 NULL);
100*cdf0e10cSrcweir 
101*cdf0e10cSrcweir         bool bHasXRender = IsCairoWorking(pParentWindow);
102*cdf0e10cSrcweir         ENSURE_ARG_OR_THROW( bHasXRender == true,
103*cdf0e10cSrcweir                              "CairoSpriteCanvas::SpriteCanvas: No RENDER extension" );
104*cdf0e10cSrcweir 
105*cdf0e10cSrcweir         Size aPixelSize( pParentWindow->GetOutputSizePixel() );
106*cdf0e10cSrcweir         const ::basegfx::B2ISize aSize( aPixelSize.Width(),
107*cdf0e10cSrcweir                                         aPixelSize.Height() );
108*cdf0e10cSrcweir 
109*cdf0e10cSrcweir         ENSURE_ARG_OR_THROW( pParentWindow != NULL,
110*cdf0e10cSrcweir                              "CairoSpriteCanvas::initialize: invalid Window pointer" );
111*cdf0e10cSrcweir 
112*cdf0e10cSrcweir         // setup helper
113*cdf0e10cSrcweir         maDeviceHelper.init( *pParentWindow,
114*cdf0e10cSrcweir                              *this,
115*cdf0e10cSrcweir                              aSize,
116*cdf0e10cSrcweir                              bIsFullscreen );
117*cdf0e10cSrcweir 
118*cdf0e10cSrcweir         setWindow(uno::Reference<awt::XWindow2>(xParentWindow, uno::UNO_QUERY_THROW));
119*cdf0e10cSrcweir 
120*cdf0e10cSrcweir         maCanvasHelper.init( maRedrawManager,
121*cdf0e10cSrcweir                              *this,
122*cdf0e10cSrcweir                              aSize );
123*cdf0e10cSrcweir 
124*cdf0e10cSrcweir         maArguments.realloc(0);
125*cdf0e10cSrcweir     }
126*cdf0e10cSrcweir 
127*cdf0e10cSrcweir     void SAL_CALL SpriteCanvas::disposing()
128*cdf0e10cSrcweir     {
129*cdf0e10cSrcweir         ::osl::MutexGuard aGuard( m_aMutex );
130*cdf0e10cSrcweir 
131*cdf0e10cSrcweir         mxComponentContext.clear();
132*cdf0e10cSrcweir 
133*cdf0e10cSrcweir         // forward to parent
134*cdf0e10cSrcweir         SpriteCanvasBaseT::disposing();
135*cdf0e10cSrcweir     }
136*cdf0e10cSrcweir 
137*cdf0e10cSrcweir     ::sal_Bool SAL_CALL SpriteCanvas::showBuffer( ::sal_Bool bUpdateAll ) throw (uno::RuntimeException)
138*cdf0e10cSrcweir     {
139*cdf0e10cSrcweir         return updateScreen( bUpdateAll );
140*cdf0e10cSrcweir     }
141*cdf0e10cSrcweir 
142*cdf0e10cSrcweir     ::sal_Bool SAL_CALL SpriteCanvas::switchBuffer( ::sal_Bool bUpdateAll ) throw (uno::RuntimeException)
143*cdf0e10cSrcweir     {
144*cdf0e10cSrcweir         return updateScreen( bUpdateAll );
145*cdf0e10cSrcweir     }
146*cdf0e10cSrcweir 
147*cdf0e10cSrcweir     sal_Bool SAL_CALL SpriteCanvas::updateScreen( sal_Bool bUpdateAll ) throw (uno::RuntimeException)
148*cdf0e10cSrcweir     {
149*cdf0e10cSrcweir         ::osl::MutexGuard aGuard( m_aMutex );
150*cdf0e10cSrcweir 
151*cdf0e10cSrcweir         // avoid repaints on hidden window (hidden: not mapped to
152*cdf0e10cSrcweir         // screen). Return failure, since the screen really has _not_
153*cdf0e10cSrcweir         // been updated (caller should try again later)
154*cdf0e10cSrcweir         return !mbIsVisible ? false : maCanvasHelper.updateScreen(
155*cdf0e10cSrcweir             ::basegfx::unotools::b2IRectangleFromAwtRectangle(maBounds),
156*cdf0e10cSrcweir             bUpdateAll,
157*cdf0e10cSrcweir             mbSurfaceDirty);
158*cdf0e10cSrcweir     }
159*cdf0e10cSrcweir 
160*cdf0e10cSrcweir     ::rtl::OUString SAL_CALL SpriteCanvas::getServiceName(  ) throw (uno::RuntimeException)
161*cdf0e10cSrcweir     {
162*cdf0e10cSrcweir         return ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( SPRITECANVAS_SERVICE_NAME ) );
163*cdf0e10cSrcweir     }
164*cdf0e10cSrcweir 
165*cdf0e10cSrcweir     SurfaceSharedPtr SpriteCanvas::getSurface()
166*cdf0e10cSrcweir     {
167*cdf0e10cSrcweir         return maDeviceHelper.getBufferSurface();
168*cdf0e10cSrcweir     }
169*cdf0e10cSrcweir 
170*cdf0e10cSrcweir     SurfaceSharedPtr SpriteCanvas::createSurface( const ::basegfx::B2ISize& rSize, Content aContent )
171*cdf0e10cSrcweir     {
172*cdf0e10cSrcweir         return maDeviceHelper.createSurface( rSize, aContent );
173*cdf0e10cSrcweir     }
174*cdf0e10cSrcweir 
175*cdf0e10cSrcweir     SurfaceSharedPtr SpriteCanvas::createSurface( ::Bitmap& rBitmap )
176*cdf0e10cSrcweir     {
177*cdf0e10cSrcweir         BitmapSystemData aData;
178*cdf0e10cSrcweir         if( rBitmap.GetSystemData( aData ) ) {
179*cdf0e10cSrcweir             const Size& rSize = rBitmap.GetSizePixel();
180*cdf0e10cSrcweir 
181*cdf0e10cSrcweir             return maDeviceHelper.createSurface( aData, rSize );
182*cdf0e10cSrcweir         }
183*cdf0e10cSrcweir 
184*cdf0e10cSrcweir         return SurfaceSharedPtr();
185*cdf0e10cSrcweir     }
186*cdf0e10cSrcweir 
187*cdf0e10cSrcweir     SurfaceSharedPtr SpriteCanvas::changeSurface( bool, bool )
188*cdf0e10cSrcweir     {
189*cdf0e10cSrcweir         // non-modifiable surface here
190*cdf0e10cSrcweir         return SurfaceSharedPtr();
191*cdf0e10cSrcweir     }
192*cdf0e10cSrcweir 
193*cdf0e10cSrcweir     OutputDevice* SpriteCanvas::getOutputDevice()
194*cdf0e10cSrcweir     {
195*cdf0e10cSrcweir         return maDeviceHelper.getOutputDevice();
196*cdf0e10cSrcweir     }
197*cdf0e10cSrcweir 
198*cdf0e10cSrcweir     SurfaceSharedPtr SpriteCanvas::getBufferSurface()
199*cdf0e10cSrcweir     {
200*cdf0e10cSrcweir         return maDeviceHelper.getBufferSurface();
201*cdf0e10cSrcweir     }
202*cdf0e10cSrcweir 
203*cdf0e10cSrcweir     SurfaceSharedPtr SpriteCanvas::getWindowSurface()
204*cdf0e10cSrcweir     {
205*cdf0e10cSrcweir         return maDeviceHelper.getWindowSurface();
206*cdf0e10cSrcweir     }
207*cdf0e10cSrcweir 
208*cdf0e10cSrcweir     const ::basegfx::B2ISize& SpriteCanvas::getSizePixel()
209*cdf0e10cSrcweir     {
210*cdf0e10cSrcweir         return maDeviceHelper.getSizePixel();
211*cdf0e10cSrcweir     }
212*cdf0e10cSrcweir 
213*cdf0e10cSrcweir     void SpriteCanvas::setSizePixel( const ::basegfx::B2ISize& rSize )
214*cdf0e10cSrcweir     {
215*cdf0e10cSrcweir         maCanvasHelper.setSize( rSize );
216*cdf0e10cSrcweir         // re-set background surface, in case it needed recreation
217*cdf0e10cSrcweir         maCanvasHelper.setSurface( maDeviceHelper.getBufferSurface(),
218*cdf0e10cSrcweir                                    false );
219*cdf0e10cSrcweir     }
220*cdf0e10cSrcweir 
221*cdf0e10cSrcweir     void SpriteCanvas::flush()
222*cdf0e10cSrcweir     {
223*cdf0e10cSrcweir         maDeviceHelper.flush();
224*cdf0e10cSrcweir     }
225*cdf0e10cSrcweir 
226*cdf0e10cSrcweir     bool SpriteCanvas::repaint( const SurfaceSharedPtr&       pSurface,
227*cdf0e10cSrcweir                                 const rendering::ViewState&   viewState,
228*cdf0e10cSrcweir                                 const rendering::RenderState& renderState )
229*cdf0e10cSrcweir     {
230*cdf0e10cSrcweir         return maCanvasHelper.repaint( pSurface, viewState, renderState );
231*cdf0e10cSrcweir     }
232*cdf0e10cSrcweir }
233