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