1*25ea7f45SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*25ea7f45SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*25ea7f45SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*25ea7f45SAndrew Rist  * distributed with this work for additional information
6*25ea7f45SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*25ea7f45SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*25ea7f45SAndrew Rist  * "License"); you may not use this file except in compliance
9*25ea7f45SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*25ea7f45SAndrew Rist  *
11*25ea7f45SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*25ea7f45SAndrew Rist  *
13*25ea7f45SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*25ea7f45SAndrew Rist  * software distributed under the License is distributed on an
15*25ea7f45SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*25ea7f45SAndrew Rist  * KIND, either express or implied.  See the License for the
17*25ea7f45SAndrew Rist  * specific language governing permissions and limitations
18*25ea7f45SAndrew Rist  * under the License.
19*25ea7f45SAndrew Rist  *
20*25ea7f45SAndrew Rist  *************************************************************/
21*25ea7f45SAndrew Rist 
22*25ea7f45SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_canvas.hxx"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #include <canvas/debug.hxx>
28cdf0e10cSrcweir #include <canvas/verbosetrace.hxx>
29cdf0e10cSrcweir #include <canvas/canvastools.hxx>
30cdf0e10cSrcweir #include <tools/diagnose_ex.h>
31cdf0e10cSrcweir 
32cdf0e10cSrcweir #include <osl/mutex.hxx>
33cdf0e10cSrcweir 
34cdf0e10cSrcweir #include <com/sun/star/registry/XRegistryKey.hpp>
35cdf0e10cSrcweir #include <com/sun/star/lang/XSingleServiceFactory.hpp>
36cdf0e10cSrcweir #include <com/sun/star/uno/XComponentContext.hpp>
37cdf0e10cSrcweir #include <com/sun/star/lang/NoSupportException.hpp>
38cdf0e10cSrcweir 
39cdf0e10cSrcweir #include <toolkit/helper/vclunohelper.hxx>
40cdf0e10cSrcweir 
41cdf0e10cSrcweir #include <basegfx/matrix/b2dhommatrix.hxx>
42cdf0e10cSrcweir #include <basegfx/point/b2dpoint.hxx>
43cdf0e10cSrcweir #include <basegfx/tools/canvastools.hxx>
44cdf0e10cSrcweir #include <basegfx/numeric/ftools.hxx>
45cdf0e10cSrcweir 
46cdf0e10cSrcweir #include "cairo_spritecanvas.hxx"
47cdf0e10cSrcweir 
48cdf0e10cSrcweir using namespace ::cairo;
49cdf0e10cSrcweir using namespace ::com::sun::star;
50cdf0e10cSrcweir 
51cdf0e10cSrcweir namespace cairocanvas
52cdf0e10cSrcweir {
SpriteCanvas(const uno::Sequence<uno::Any> & aArguments,const uno::Reference<uno::XComponentContext> & rxContext)53cdf0e10cSrcweir     SpriteCanvas::SpriteCanvas( const uno::Sequence< uno::Any >&                aArguments,
54cdf0e10cSrcweir                                 const uno::Reference< uno::XComponentContext >& rxContext ) :
55cdf0e10cSrcweir         maArguments(aArguments),
56cdf0e10cSrcweir         mxComponentContext( rxContext )
57cdf0e10cSrcweir     {
58cdf0e10cSrcweir     }
59cdf0e10cSrcweir 
initialize()60cdf0e10cSrcweir     void SpriteCanvas::initialize()
61cdf0e10cSrcweir     {
62cdf0e10cSrcweir 		VERBOSE_TRACE("CairoSpriteCanvas created %p\n", this);
63cdf0e10cSrcweir 
64cdf0e10cSrcweir         // #i64742# Only call initialize when not in probe mode
65cdf0e10cSrcweir         if( maArguments.getLength() == 0 )
66cdf0e10cSrcweir             return;
67cdf0e10cSrcweir 
68cdf0e10cSrcweir         /* maArguments:
69cdf0e10cSrcweir            0: ptr to creating instance (Window or VirtualDevice)
70cdf0e10cSrcweir            1: SystemEnvData as a streamed Any (or empty for VirtualDevice)
71cdf0e10cSrcweir            2: current bounds of creating instance
72cdf0e10cSrcweir            3: bool, denoting always on top state for Window (always false for VirtualDevice)
73cdf0e10cSrcweir            4: XWindow for creating Window (or empty for VirtualDevice)
74cdf0e10cSrcweir            5: SystemGraphicsData as a streamed Any
75cdf0e10cSrcweir          */
76cdf0e10cSrcweir         ENSURE_ARG_OR_THROW( maArguments.getLength() >= 4 &&
77cdf0e10cSrcweir                              maArguments[0].getValueTypeClass() == uno::TypeClass_HYPER &&
78cdf0e10cSrcweir                              maArguments[4].getValueTypeClass() == uno::TypeClass_INTERFACE,
79cdf0e10cSrcweir                              "CairoSpriteCanvas::initialize: wrong number of arguments, or wrong types" );
80cdf0e10cSrcweir 
81cdf0e10cSrcweir         awt::Rectangle aRect;
82cdf0e10cSrcweir         maArguments[2] >>= aRect;
83cdf0e10cSrcweir 
84cdf0e10cSrcweir         sal_Bool bIsFullscreen( sal_False );
85cdf0e10cSrcweir         maArguments[3] >>= bIsFullscreen;
86cdf0e10cSrcweir 
87cdf0e10cSrcweir         uno::Reference< awt::XWindow > xParentWindow;
88cdf0e10cSrcweir         maArguments[4] >>= xParentWindow;
89cdf0e10cSrcweir 
90cdf0e10cSrcweir         Window* pParentWindow = VCLUnoHelper::GetWindow(xParentWindow);
91cdf0e10cSrcweir         if( !pParentWindow )
92cdf0e10cSrcweir             throw lang::NoSupportException(
93cdf0e10cSrcweir                 ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM(
94cdf0e10cSrcweir                                      "Parent window not VCL window, or canvas out-of-process!")),
95cdf0e10cSrcweir                 NULL);
96cdf0e10cSrcweir 
97cdf0e10cSrcweir         bool bHasXRender = IsCairoWorking(pParentWindow);
98cdf0e10cSrcweir         ENSURE_ARG_OR_THROW( bHasXRender == true,
99cdf0e10cSrcweir                              "CairoSpriteCanvas::SpriteCanvas: No RENDER extension" );
100cdf0e10cSrcweir 
101cdf0e10cSrcweir         Size aPixelSize( pParentWindow->GetOutputSizePixel() );
102cdf0e10cSrcweir         const ::basegfx::B2ISize aSize( aPixelSize.Width(),
103cdf0e10cSrcweir                                         aPixelSize.Height() );
104cdf0e10cSrcweir 
105cdf0e10cSrcweir         ENSURE_ARG_OR_THROW( pParentWindow != NULL,
106cdf0e10cSrcweir                              "CairoSpriteCanvas::initialize: invalid Window pointer" );
107cdf0e10cSrcweir 
108cdf0e10cSrcweir         // setup helper
109cdf0e10cSrcweir         maDeviceHelper.init( *pParentWindow,
110cdf0e10cSrcweir                              *this,
111cdf0e10cSrcweir                              aSize,
112cdf0e10cSrcweir                              bIsFullscreen );
113cdf0e10cSrcweir 
114cdf0e10cSrcweir         setWindow(uno::Reference<awt::XWindow2>(xParentWindow, uno::UNO_QUERY_THROW));
115cdf0e10cSrcweir 
116cdf0e10cSrcweir         maCanvasHelper.init( maRedrawManager,
117cdf0e10cSrcweir                              *this,
118cdf0e10cSrcweir                              aSize );
119cdf0e10cSrcweir 
120cdf0e10cSrcweir         maArguments.realloc(0);
121cdf0e10cSrcweir     }
122cdf0e10cSrcweir 
disposing()123cdf0e10cSrcweir     void SAL_CALL SpriteCanvas::disposing()
124cdf0e10cSrcweir     {
125cdf0e10cSrcweir         ::osl::MutexGuard aGuard( m_aMutex );
126cdf0e10cSrcweir 
127cdf0e10cSrcweir         mxComponentContext.clear();
128cdf0e10cSrcweir 
129cdf0e10cSrcweir         // forward to parent
130cdf0e10cSrcweir         SpriteCanvasBaseT::disposing();
131cdf0e10cSrcweir     }
132cdf0e10cSrcweir 
showBuffer(::sal_Bool bUpdateAll)133cdf0e10cSrcweir     ::sal_Bool SAL_CALL SpriteCanvas::showBuffer( ::sal_Bool bUpdateAll ) throw (uno::RuntimeException)
134cdf0e10cSrcweir     {
135cdf0e10cSrcweir         return updateScreen( bUpdateAll );
136cdf0e10cSrcweir     }
137cdf0e10cSrcweir 
switchBuffer(::sal_Bool bUpdateAll)138cdf0e10cSrcweir     ::sal_Bool SAL_CALL SpriteCanvas::switchBuffer( ::sal_Bool bUpdateAll ) throw (uno::RuntimeException)
139cdf0e10cSrcweir     {
140cdf0e10cSrcweir         return updateScreen( bUpdateAll );
141cdf0e10cSrcweir     }
142cdf0e10cSrcweir 
updateScreen(sal_Bool bUpdateAll)143cdf0e10cSrcweir     sal_Bool SAL_CALL SpriteCanvas::updateScreen( sal_Bool bUpdateAll ) throw (uno::RuntimeException)
144cdf0e10cSrcweir     {
145cdf0e10cSrcweir         ::osl::MutexGuard aGuard( m_aMutex );
146cdf0e10cSrcweir 
147cdf0e10cSrcweir         // avoid repaints on hidden window (hidden: not mapped to
148cdf0e10cSrcweir         // screen). Return failure, since the screen really has _not_
149cdf0e10cSrcweir         // been updated (caller should try again later)
150cdf0e10cSrcweir         return !mbIsVisible ? false : maCanvasHelper.updateScreen(
151cdf0e10cSrcweir             ::basegfx::unotools::b2IRectangleFromAwtRectangle(maBounds),
152cdf0e10cSrcweir             bUpdateAll,
153cdf0e10cSrcweir             mbSurfaceDirty);
154cdf0e10cSrcweir     }
155cdf0e10cSrcweir 
getServiceName()156cdf0e10cSrcweir     ::rtl::OUString SAL_CALL SpriteCanvas::getServiceName(  ) throw (uno::RuntimeException)
157cdf0e10cSrcweir     {
158cdf0e10cSrcweir         return ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( SPRITECANVAS_SERVICE_NAME ) );
159cdf0e10cSrcweir     }
160cdf0e10cSrcweir 
getSurface()161cdf0e10cSrcweir     SurfaceSharedPtr SpriteCanvas::getSurface()
162cdf0e10cSrcweir     {
163cdf0e10cSrcweir         return maDeviceHelper.getBufferSurface();
164cdf0e10cSrcweir     }
165cdf0e10cSrcweir 
createSurface(const::basegfx::B2ISize & rSize,Content aContent)166cdf0e10cSrcweir     SurfaceSharedPtr SpriteCanvas::createSurface( const ::basegfx::B2ISize& rSize, Content aContent )
167cdf0e10cSrcweir     {
168cdf0e10cSrcweir         return maDeviceHelper.createSurface( rSize, aContent );
169cdf0e10cSrcweir     }
170cdf0e10cSrcweir 
createSurface(::Bitmap & rBitmap)171cdf0e10cSrcweir     SurfaceSharedPtr SpriteCanvas::createSurface( ::Bitmap& rBitmap )
172cdf0e10cSrcweir     {
173cdf0e10cSrcweir         BitmapSystemData aData;
174cdf0e10cSrcweir         if( rBitmap.GetSystemData( aData ) ) {
175cdf0e10cSrcweir             const Size& rSize = rBitmap.GetSizePixel();
176cdf0e10cSrcweir 
177cdf0e10cSrcweir             return maDeviceHelper.createSurface( aData, rSize );
178cdf0e10cSrcweir         }
179cdf0e10cSrcweir 
180cdf0e10cSrcweir         return SurfaceSharedPtr();
181cdf0e10cSrcweir     }
182cdf0e10cSrcweir 
changeSurface(bool,bool)183cdf0e10cSrcweir     SurfaceSharedPtr SpriteCanvas::changeSurface( bool, bool )
184cdf0e10cSrcweir     {
185cdf0e10cSrcweir         // non-modifiable surface here
186cdf0e10cSrcweir         return SurfaceSharedPtr();
187cdf0e10cSrcweir     }
188cdf0e10cSrcweir 
getOutputDevice()189cdf0e10cSrcweir     OutputDevice* SpriteCanvas::getOutputDevice()
190cdf0e10cSrcweir     {
191cdf0e10cSrcweir         return maDeviceHelper.getOutputDevice();
192cdf0e10cSrcweir     }
193cdf0e10cSrcweir 
getBufferSurface()194cdf0e10cSrcweir     SurfaceSharedPtr SpriteCanvas::getBufferSurface()
195cdf0e10cSrcweir     {
196cdf0e10cSrcweir         return maDeviceHelper.getBufferSurface();
197cdf0e10cSrcweir     }
198cdf0e10cSrcweir 
getWindowSurface()199cdf0e10cSrcweir     SurfaceSharedPtr SpriteCanvas::getWindowSurface()
200cdf0e10cSrcweir     {
201cdf0e10cSrcweir         return maDeviceHelper.getWindowSurface();
202cdf0e10cSrcweir     }
203cdf0e10cSrcweir 
getSizePixel()204cdf0e10cSrcweir     const ::basegfx::B2ISize& SpriteCanvas::getSizePixel()
205cdf0e10cSrcweir     {
206cdf0e10cSrcweir         return maDeviceHelper.getSizePixel();
207cdf0e10cSrcweir     }
208cdf0e10cSrcweir 
setSizePixel(const::basegfx::B2ISize & rSize)209cdf0e10cSrcweir     void SpriteCanvas::setSizePixel( const ::basegfx::B2ISize& rSize )
210cdf0e10cSrcweir     {
211cdf0e10cSrcweir 		maCanvasHelper.setSize( rSize );
212cdf0e10cSrcweir         // re-set background surface, in case it needed recreation
213cdf0e10cSrcweir 		maCanvasHelper.setSurface( maDeviceHelper.getBufferSurface(),
214cdf0e10cSrcweir                                    false );
215cdf0e10cSrcweir     }
216cdf0e10cSrcweir 
flush()217cdf0e10cSrcweir     void SpriteCanvas::flush()
218cdf0e10cSrcweir     {
219cdf0e10cSrcweir         maDeviceHelper.flush();
220cdf0e10cSrcweir     }
221cdf0e10cSrcweir 
repaint(const SurfaceSharedPtr & pSurface,const rendering::ViewState & viewState,const rendering::RenderState & renderState)222cdf0e10cSrcweir     bool SpriteCanvas::repaint( const SurfaceSharedPtr&       pSurface,
223cdf0e10cSrcweir                                 const rendering::ViewState&   viewState,
224cdf0e10cSrcweir                                 const rendering::RenderState& renderState )
225cdf0e10cSrcweir     {
226cdf0e10cSrcweir         return maCanvasHelper.repaint( pSurface, viewState, renderState );
227cdf0e10cSrcweir     }
228cdf0e10cSrcweir }
229