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 #ifdef WNT
51 # include <tools/prewin.h>
52 # include <windows.h>
53 # include <tools/postwin.h>
54 #endif
55 
56 #include <vcl/sysdata.hxx>
57 
58 #include "cairo_canvas.hxx"
59 
60 using namespace ::cairo;
61 using namespace ::com::sun::star;
62 
63 namespace cairocanvas
64 {
65     Canvas::Canvas( const uno::Sequence< uno::Any >&                aArguments,
66                     const uno::Reference< uno::XComponentContext >& rxContext ) :
67         maArguments(aArguments),
68         mxComponentContext( rxContext )
69     {
70     }
71 
72     void Canvas::initialize()
73     {
74         // #i64742# Only perform initialization when not in probe mode
75         if( maArguments.getLength() == 0 )
76             return;
77 
78         /* maArguments:
79            0: ptr to creating instance (Window or VirtualDevice)
80            1: SystemEnvData as a streamed Any (or empty for VirtualDevice)
81            2: current bounds of creating instance
82            3: bool, denoting always on top state for Window (always false for VirtualDevice)
83            4: XWindow for creating Window (or empty for VirtualDevice)
84            5: SystemGraphicsData as a streamed Any
85          */
86 		VERBOSE_TRACE("Canvas created %p\n", this);
87 
88         ENSURE_ARG_OR_THROW( maArguments.getLength() >= 6 &&
89                              maArguments[0].getValueTypeClass() == uno::TypeClass_HYPER &&
90                              maArguments[5].getValueTypeClass() == uno::TypeClass_SEQUENCE,
91                              "Canvas::initialize: wrong number of arguments, or wrong types" );
92 
93         // We expect a single Any here, containing a pointer to a valid
94         // VCL output device, on which to output (mostly needed for text)
95         sal_Int64 nPtr = 0;
96         maArguments[0] >>= nPtr;
97         OutputDevice* pOutDev = reinterpret_cast<OutputDevice*>(nPtr);
98 
99         ENSURE_ARG_OR_THROW( pOutDev != NULL,
100                              "Canvas::initialize: invalid OutDev pointer" );
101 
102         awt::Rectangle aBounds;
103         maArguments[2] >>= aBounds;
104 
105         uno::Sequence<sal_Int8> aSeq;
106         maArguments[5] >>= aSeq;
107 
108         const SystemGraphicsData* pSysData=reinterpret_cast<const SystemGraphicsData*>(aSeq.getConstArray());
109         if( !pSysData || !pSysData->nSize )
110             throw lang::NoSupportException(
111                 ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM(
112                                      "Passed SystemGraphicsData invalid!")),
113                 NULL);
114 
115         bool bHasXRender = IsCairoWorking(pOutDev);
116         ENSURE_ARG_OR_THROW( bHasXRender == true,
117                              "SpriteCanvas::SpriteCanvas: No RENDER extension" );
118 
119         // setup helper
120         maDeviceHelper.init( *this,
121                              *pOutDev );
122 
123         maCanvasHelper.init( basegfx::B2ISize(aBounds.Width, aBounds.Height),
124                              *this, this );
125 
126         // forward surface to render on to canvashelper
127         maCanvasHelper.setSurface(
128             maDeviceHelper.getSurface(),
129             false );
130 
131         maArguments.realloc(0);
132     }
133 
134     Canvas::~Canvas()
135     {
136         OSL_TRACE( "CairoCanvas destroyed" );
137     }
138 
139     void SAL_CALL Canvas::disposing()
140     {
141         ::osl::MutexGuard aGuard( m_aMutex );
142 
143         mxComponentContext.clear();
144 
145         // forward to parent
146         CanvasBaseT::disposing();
147     }
148 
149     ::rtl::OUString SAL_CALL Canvas::getServiceName(  ) throw (uno::RuntimeException)
150     {
151         return ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( CANVAS_SERVICE_NAME ) );
152     }
153 
154     bool Canvas::repaint( const SurfaceSharedPtr&       pSurface,
155                           const rendering::ViewState&   viewState,
156                           const rendering::RenderState&	renderState )
157     {
158         return maCanvasHelper.repaint( pSurface, viewState, renderState );
159     }
160 
161     SurfaceSharedPtr Canvas::getSurface()
162     {
163         return maDeviceHelper.getSurface();
164     }
165 
166     SurfaceSharedPtr Canvas::createSurface( const ::basegfx::B2ISize& rSize, Content aContent )
167     {
168         return maDeviceHelper.createSurface( rSize, aContent );
169     }
170 
171     SurfaceSharedPtr Canvas::createSurface( ::Bitmap& rBitmap )
172     {
173         SurfaceSharedPtr pSurface;
174 
175         BitmapSystemData aData;
176         if( rBitmap.GetSystemData( aData ) ) {
177             const Size& rSize = rBitmap.GetSizePixel();
178 
179             pSurface = maDeviceHelper.createSurface( aData, rSize );
180         }
181 
182         return pSurface;
183     }
184 
185     SurfaceSharedPtr Canvas::changeSurface( bool, bool )
186     {
187         // non-modifiable surface here
188         return SurfaceSharedPtr();
189     }
190 
191     OutputDevice* Canvas::getOutputDevice()
192     {
193         return maDeviceHelper.getOutputDevice();
194     }
195 }
196