xref: /AOO41X/main/canvas/source/directx/dx_canvas.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 <ctype.h> // don't ask. msdev breaks otherwise...
32*cdf0e10cSrcweir #include <canvas/debug.hxx>
33*cdf0e10cSrcweir #include <canvas/verbosetrace.hxx>
34*cdf0e10cSrcweir #include <canvas/canvastools.hxx>
35*cdf0e10cSrcweir #include <tools/diagnose_ex.h>
36*cdf0e10cSrcweir 
37*cdf0e10cSrcweir #include <osl/mutex.hxx>
38*cdf0e10cSrcweir 
39*cdf0e10cSrcweir #include <com/sun/star/awt/XWindow.hpp>
40*cdf0e10cSrcweir #include <com/sun/star/awt/XSystemDependentWindowPeer.hpp>
41*cdf0e10cSrcweir #include <com/sun/star/registry/XRegistryKey.hpp>
42*cdf0e10cSrcweir #include <com/sun/star/lang/XSingleServiceFactory.hpp>
43*cdf0e10cSrcweir #include <com/sun/star/uno/XComponentContext.hpp>
44*cdf0e10cSrcweir #include <com/sun/star/lang/NoSupportException.hpp>
45*cdf0e10cSrcweir 
46*cdf0e10cSrcweir #include <toolkit/helper/vclunohelper.hxx>
47*cdf0e10cSrcweir #include <cppuhelper/factory.hxx>
48*cdf0e10cSrcweir #include <cppuhelper/implementationentry.hxx>
49*cdf0e10cSrcweir #include <comphelper/servicedecl.hxx>
50*cdf0e10cSrcweir 
51*cdf0e10cSrcweir #include <basegfx/matrix/b2dhommatrix.hxx>
52*cdf0e10cSrcweir #include <basegfx/point/b2dpoint.hxx>
53*cdf0e10cSrcweir #include <basegfx/tools/canvastools.hxx>
54*cdf0e10cSrcweir #include <basegfx/numeric/ftools.hxx>
55*cdf0e10cSrcweir 
56*cdf0e10cSrcweir #include "dx_graphicsprovider.hxx"
57*cdf0e10cSrcweir #include "dx_winstuff.hxx"
58*cdf0e10cSrcweir #include "dx_canvas.hxx"
59*cdf0e10cSrcweir 
60*cdf0e10cSrcweir #include <vcl/sysdata.hxx>
61*cdf0e10cSrcweir 
62*cdf0e10cSrcweir #define CANVAS_TECH "GDI+"
63*cdf0e10cSrcweir #define CANVAS_SERVICE_NAME        		 "com.sun.star.rendering.Canvas."      		 CANVAS_TECH
64*cdf0e10cSrcweir #define CANVAS_IMPLEMENTATION_NAME 		 "com.sun.star.comp.rendering.Canvas." 		 CANVAS_TECH
65*cdf0e10cSrcweir #define BITMAPCANVAS_SERVICE_NAME        "com.sun.star.rendering.BitmapCanvas."      CANVAS_TECH
66*cdf0e10cSrcweir #define BITMAPCANVAS_IMPLEMENTATION_NAME "com.sun.star.comp.rendering.BitmapCanvas." CANVAS_TECH
67*cdf0e10cSrcweir 
68*cdf0e10cSrcweir 
69*cdf0e10cSrcweir using namespace ::com::sun::star;
70*cdf0e10cSrcweir 
71*cdf0e10cSrcweir namespace dxcanvas
72*cdf0e10cSrcweir {
73*cdf0e10cSrcweir     /// Actual canonical implementation of the GraphicsProvider interface
74*cdf0e10cSrcweir     class GraphicsProviderImpl : public GraphicsProvider
75*cdf0e10cSrcweir     {
76*cdf0e10cSrcweir         GraphicsSharedPtr mpGraphics;
77*cdf0e10cSrcweir     public:
78*cdf0e10cSrcweir         explicit GraphicsProviderImpl( Gdiplus::Graphics* pGraphics ) : mpGraphics( pGraphics ) {}
79*cdf0e10cSrcweir         virtual GraphicsSharedPtr getGraphics() { return mpGraphics; }
80*cdf0e10cSrcweir     };
81*cdf0e10cSrcweir 
82*cdf0e10cSrcweir     Canvas::Canvas( const uno::Sequence< uno::Any >&                aArguments,
83*cdf0e10cSrcweir                     const uno::Reference< uno::XComponentContext >& rxContext ) :
84*cdf0e10cSrcweir         maArguments(aArguments),
85*cdf0e10cSrcweir         mxComponentContext( rxContext )
86*cdf0e10cSrcweir     {
87*cdf0e10cSrcweir     }
88*cdf0e10cSrcweir 
89*cdf0e10cSrcweir     void Canvas::initialize()
90*cdf0e10cSrcweir     {
91*cdf0e10cSrcweir         // #i64742# Only perform initialization when not in probe mode
92*cdf0e10cSrcweir         if( maArguments.getLength() == 0 )
93*cdf0e10cSrcweir             return;
94*cdf0e10cSrcweir 
95*cdf0e10cSrcweir         VERBOSE_TRACE( "Canvas::initialize called" );
96*cdf0e10cSrcweir 
97*cdf0e10cSrcweir         // At index 1, we expect a HWND handle here, containing a
98*cdf0e10cSrcweir         // pointer to a valid window, on which to output
99*cdf0e10cSrcweir         // At index 2, we expect the current window bound rect
100*cdf0e10cSrcweir         ENSURE_ARG_OR_THROW( maArguments.getLength() >= 6 &&
101*cdf0e10cSrcweir                              maArguments[5].getValueTypeClass() == uno::TypeClass_SEQUENCE,
102*cdf0e10cSrcweir                              "SpriteCanvas::initialize: wrong number of arguments, or wrong types" );
103*cdf0e10cSrcweir 
104*cdf0e10cSrcweir         uno::Sequence<sal_Int8> aSeq;
105*cdf0e10cSrcweir         maArguments[5] >>= aSeq;
106*cdf0e10cSrcweir 
107*cdf0e10cSrcweir         const SystemGraphicsData* pSysData=reinterpret_cast<const SystemGraphicsData*>(aSeq.getConstArray());
108*cdf0e10cSrcweir         if( !pSysData || !pSysData->hDC )
109*cdf0e10cSrcweir             throw lang::NoSupportException(
110*cdf0e10cSrcweir                 ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM(
111*cdf0e10cSrcweir                                      "Passed SystemGraphicsData or HDC invalid!")),
112*cdf0e10cSrcweir                 NULL);
113*cdf0e10cSrcweir 
114*cdf0e10cSrcweir         // setup helper
115*cdf0e10cSrcweir         maDeviceHelper.init( pSysData->hDC,
116*cdf0e10cSrcweir                              *this );
117*cdf0e10cSrcweir         maCanvasHelper.setDevice( *this );
118*cdf0e10cSrcweir         maCanvasHelper.setTarget(
119*cdf0e10cSrcweir             GraphicsProviderSharedPtr(
120*cdf0e10cSrcweir                 new GraphicsProviderImpl(
121*cdf0e10cSrcweir                     Gdiplus::Graphics::FromHDC(pSysData->hDC))));
122*cdf0e10cSrcweir 
123*cdf0e10cSrcweir         maArguments.realloc(0);
124*cdf0e10cSrcweir     }
125*cdf0e10cSrcweir 
126*cdf0e10cSrcweir     void SAL_CALL Canvas::disposing()
127*cdf0e10cSrcweir     {
128*cdf0e10cSrcweir         ::osl::MutexGuard aGuard( m_aMutex );
129*cdf0e10cSrcweir 
130*cdf0e10cSrcweir         mxComponentContext.clear();
131*cdf0e10cSrcweir 
132*cdf0e10cSrcweir         // forward to parent
133*cdf0e10cSrcweir         CanvasBaseT::disposing();
134*cdf0e10cSrcweir     }
135*cdf0e10cSrcweir 
136*cdf0e10cSrcweir     ::rtl::OUString SAL_CALL Canvas::getServiceName(  ) throw (uno::RuntimeException)
137*cdf0e10cSrcweir     {
138*cdf0e10cSrcweir         return ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( CANVAS_SERVICE_NAME ) );
139*cdf0e10cSrcweir     }
140*cdf0e10cSrcweir 
141*cdf0e10cSrcweir     BitmapCanvas::BitmapCanvas( const uno::Sequence< uno::Any >&                aArguments,
142*cdf0e10cSrcweir                                 const uno::Reference< uno::XComponentContext >& rxContext ) :
143*cdf0e10cSrcweir         maArguments(aArguments),
144*cdf0e10cSrcweir         mxComponentContext( rxContext ),
145*cdf0e10cSrcweir         mpTarget()
146*cdf0e10cSrcweir     {
147*cdf0e10cSrcweir     }
148*cdf0e10cSrcweir 
149*cdf0e10cSrcweir     void BitmapCanvas::initialize()
150*cdf0e10cSrcweir     {
151*cdf0e10cSrcweir         // #i64742# Only perform initialization when not in probe mode
152*cdf0e10cSrcweir         if( maArguments.getLength() == 0 )
153*cdf0e10cSrcweir             return;
154*cdf0e10cSrcweir 
155*cdf0e10cSrcweir         VERBOSE_TRACE( "BitmapCanvas::initialize called" );
156*cdf0e10cSrcweir 
157*cdf0e10cSrcweir         // At index 1, we expect a HWND handle here, containing a
158*cdf0e10cSrcweir         // pointer to a valid window, on which to output
159*cdf0e10cSrcweir         // At index 2, we expect the current window bound rect
160*cdf0e10cSrcweir         ENSURE_ARG_OR_THROW( maArguments.getLength() >= 6 &&
161*cdf0e10cSrcweir                              maArguments[5].getValueTypeClass() == uno::TypeClass_SEQUENCE,
162*cdf0e10cSrcweir                              "SpriteCanvas::initialize: wrong number of arguments, or wrong types" );
163*cdf0e10cSrcweir 
164*cdf0e10cSrcweir         uno::Sequence<sal_Int8> aSeq;
165*cdf0e10cSrcweir         maArguments[5] >>= aSeq;
166*cdf0e10cSrcweir 
167*cdf0e10cSrcweir         const SystemGraphicsData* pSysData=reinterpret_cast<const SystemGraphicsData*>(aSeq.getConstArray());
168*cdf0e10cSrcweir         if( !pSysData || !pSysData->hDC )
169*cdf0e10cSrcweir             throw lang::NoSupportException(
170*cdf0e10cSrcweir                 ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM(
171*cdf0e10cSrcweir                                      "Passed SystemGraphicsData or HDC invalid!")),
172*cdf0e10cSrcweir                 NULL);
173*cdf0e10cSrcweir 
174*cdf0e10cSrcweir         // setup helper
175*cdf0e10cSrcweir         maDeviceHelper.init( pSysData->hDC,
176*cdf0e10cSrcweir                              *this );
177*cdf0e10cSrcweir         maCanvasHelper.setDevice( *this );
178*cdf0e10cSrcweir 
179*cdf0e10cSrcweir         // check whether we can actually provide a BitmapCanvas
180*cdf0e10cSrcweir         // here. for this, check whether the HDC has a bitmap
181*cdf0e10cSrcweir         // selected.
182*cdf0e10cSrcweir         HBITMAP hBmp;
183*cdf0e10cSrcweir         hBmp=(HBITMAP)GetCurrentObject(pSysData->hDC, OBJ_BITMAP);
184*cdf0e10cSrcweir         if( !hBmp || GetObjectType(pSysData->hDC) != OBJ_MEMDC )
185*cdf0e10cSrcweir         {
186*cdf0e10cSrcweir             throw lang::NoSupportException(
187*cdf0e10cSrcweir                 ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM(
188*cdf0e10cSrcweir                                      "Passed HDC is no mem DC/has no bitmap selected!")),
189*cdf0e10cSrcweir                 NULL);
190*cdf0e10cSrcweir         }
191*cdf0e10cSrcweir 
192*cdf0e10cSrcweir         mpTarget.reset( new DXBitmap(
193*cdf0e10cSrcweir                             BitmapSharedPtr(
194*cdf0e10cSrcweir                                 Gdiplus::Bitmap::FromHBITMAP(
195*cdf0e10cSrcweir                                     hBmp, 0) ),
196*cdf0e10cSrcweir                             false ));
197*cdf0e10cSrcweir 
198*cdf0e10cSrcweir         maCanvasHelper.setTarget( mpTarget );
199*cdf0e10cSrcweir 
200*cdf0e10cSrcweir         maArguments.realloc(0);
201*cdf0e10cSrcweir     }
202*cdf0e10cSrcweir 
203*cdf0e10cSrcweir     void SAL_CALL BitmapCanvas::disposing()
204*cdf0e10cSrcweir     {
205*cdf0e10cSrcweir         ::osl::MutexGuard aGuard( m_aMutex );
206*cdf0e10cSrcweir 
207*cdf0e10cSrcweir         mpTarget.reset();
208*cdf0e10cSrcweir         mxComponentContext.clear();
209*cdf0e10cSrcweir 
210*cdf0e10cSrcweir         // forward to parent
211*cdf0e10cSrcweir         BitmapCanvasBaseT::disposing();
212*cdf0e10cSrcweir     }
213*cdf0e10cSrcweir 
214*cdf0e10cSrcweir     ::rtl::OUString SAL_CALL BitmapCanvas::getServiceName(  ) throw (uno::RuntimeException)
215*cdf0e10cSrcweir     {
216*cdf0e10cSrcweir         return ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( BITMAPCANVAS_SERVICE_NAME ) );
217*cdf0e10cSrcweir     }
218*cdf0e10cSrcweir 
219*cdf0e10cSrcweir     IBitmapSharedPtr BitmapCanvas::getBitmap() const
220*cdf0e10cSrcweir     {
221*cdf0e10cSrcweir         return mpTarget;
222*cdf0e10cSrcweir     }
223*cdf0e10cSrcweir 
224*cdf0e10cSrcweir     static uno::Reference<uno::XInterface> initCanvas( Canvas* pCanvas )
225*cdf0e10cSrcweir     {
226*cdf0e10cSrcweir         uno::Reference<uno::XInterface> xRet(static_cast<cppu::OWeakObject*>(pCanvas));
227*cdf0e10cSrcweir         pCanvas->initialize();
228*cdf0e10cSrcweir         return xRet;
229*cdf0e10cSrcweir     }
230*cdf0e10cSrcweir 
231*cdf0e10cSrcweir     namespace sdecl = comphelper::service_decl;
232*cdf0e10cSrcweir     sdecl::class_<Canvas, sdecl::with_args<true> > serviceImpl1(&initCanvas);
233*cdf0e10cSrcweir     const sdecl::ServiceDecl dxCanvasDecl(
234*cdf0e10cSrcweir         serviceImpl1,
235*cdf0e10cSrcweir         CANVAS_IMPLEMENTATION_NAME,
236*cdf0e10cSrcweir         CANVAS_SERVICE_NAME );
237*cdf0e10cSrcweir 
238*cdf0e10cSrcweir     static uno::Reference<uno::XInterface> initBitmapCanvas( BitmapCanvas* pCanvas )
239*cdf0e10cSrcweir     {
240*cdf0e10cSrcweir         uno::Reference<uno::XInterface> xRet(static_cast<cppu::OWeakObject*>(pCanvas));
241*cdf0e10cSrcweir         pCanvas->initialize();
242*cdf0e10cSrcweir         return xRet;
243*cdf0e10cSrcweir     }
244*cdf0e10cSrcweir 
245*cdf0e10cSrcweir     namespace sdecl = comphelper::service_decl;
246*cdf0e10cSrcweir     sdecl::class_<BitmapCanvas, sdecl::with_args<true> > serviceImpl2(&initBitmapCanvas);
247*cdf0e10cSrcweir     const sdecl::ServiceDecl dxBitmapCanvasDecl(
248*cdf0e10cSrcweir         serviceImpl2,
249*cdf0e10cSrcweir         BITMAPCANVAS_IMPLEMENTATION_NAME,
250*cdf0e10cSrcweir         BITMAPCANVAS_SERVICE_NAME );
251*cdf0e10cSrcweir }
252*cdf0e10cSrcweir 
253*cdf0e10cSrcweir // The C shared lib entry points
254*cdf0e10cSrcweir COMPHELPER_SERVICEDECL_EXPORTS2(dxcanvas::dxCanvasDecl,
255*cdf0e10cSrcweir                                 dxcanvas::dxBitmapCanvasDecl);
256