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