1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
22 #include <canvas/debug.hxx>
23 #include <canvas/verbosetrace.hxx>
24 #include <canvas/canvastools.hxx>
25 #include <tools/diagnose_ex.h>
27 #include <osl/mutex.hxx>
29 #include <com/sun/star/awt/XWindow.hpp>
30 #include <com/sun/star/awt/XSystemDependentWindowPeer.hpp>
31 #include <com/sun/star/registry/XRegistryKey.hpp>
32 #include <com/sun/star/lang/XSingleServiceFactory.hpp>
33 #include <com/sun/star/uno/XComponentContext.hpp>
34 #include <com/sun/star/lang/NoSupportException.hpp>
36 #include <toolkit/helper/vclunohelper.hxx>
37 #include <cppuhelper/factory.hxx>
38 #include <cppuhelper/implementationentry.hxx>
39 #include <comphelper/servicedecl.hxx>
40 #include <vcl/opengl/OpenGLWrapper.hxx>
42 #include <basegfx/matrix/b2dhommatrix.hxx>
43 #include <basegfx/point/b2dpoint.hxx>
44 #include <basegfx/tools/canvastools.hxx>
45 #include <basegfx/numeric/ftools.hxx>
47 #include "dx_graphicsprovider.hxx"
48 #include "dx_winstuff.hxx"
49 #include "dx_canvas.hxx"
51 #include <vcl/sysdata.hxx>
53 #define CANVAS_TECH "GDI+"
54 #define CANVAS_SERVICE_NAME "com.sun.star.rendering.Canvas." CANVAS_TECH
55 #define CANVAS_IMPLEMENTATION_NAME "com.sun.star.comp.rendering.Canvas." CANVAS_TECH
56 #define BITMAPCANVAS_SERVICE_NAME "com.sun.star.rendering.BitmapCanvas." CANVAS_TECH
57 #define BITMAPCANVAS_IMPLEMENTATION_NAME "com.sun.star.comp.rendering.BitmapCanvas." CANVAS_TECH
60 using namespace ::com::sun::star
;
64 /// Actual canonical implementation of the GraphicsProvider interface
65 class GraphicsProviderImpl
: public GraphicsProvider
67 GraphicsSharedPtr mpGraphics
;
69 explicit GraphicsProviderImpl( Gdiplus::Graphics
* pGraphics
) : mpGraphics( pGraphics
) {}
70 virtual GraphicsSharedPtr
getGraphics() { return mpGraphics
; }
73 Canvas::Canvas( const uno::Sequence
< uno::Any
>& aArguments
,
74 const uno::Reference
< uno::XComponentContext
>& rxContext
) :
75 maArguments(aArguments
),
76 mxComponentContext( rxContext
)
80 void Canvas::initialize()
82 // #i64742# Only perform initialization when not in probe mode
83 if( maArguments
.getLength() == 0 )
86 // tdf#93870 - force VCL canvas in OpenGL mode for now.
87 assert( !OpenGLWrapper::isVCLOpenGLEnabled() );
89 VERBOSE_TRACE( "Canvas::initialize called" );
91 // At index 1, we expect a HWND handle here, containing a
92 // pointer to a valid window, on which to output
93 // At index 2, we expect the current window bound rect
94 ENSURE_ARG_OR_THROW( maArguments
.getLength() >= 6 &&
95 maArguments
[5].getValueTypeClass() == uno::TypeClass_SEQUENCE
,
96 "Canvas::initialize: wrong number of arguments, or wrong types" );
98 uno::Sequence
<sal_Int8
> aSeq
;
99 maArguments
[5] >>= aSeq
;
101 const SystemGraphicsData
* pSysData
=reinterpret_cast<const SystemGraphicsData
*>(aSeq
.getConstArray());
102 if( !pSysData
|| !pSysData
->hDC
)
103 throw lang::NoSupportException("Passed SystemGraphicsData or HDC invalid!");
106 maArguments
[0] >>= nPtr
;
107 OutputDevice
* pOutDev
= reinterpret_cast<OutputDevice
*>(nPtr
);
108 ENSURE_ARG_OR_THROW( pOutDev
!= NULL
,"Canvas::initialize: invalid OutDev pointer" );
111 maDeviceHelper
.init( pSysData
->hDC
, pOutDev
, *this );
112 maCanvasHelper
.setDevice( *this );
113 maCanvasHelper
.setTarget(
114 GraphicsProviderSharedPtr(
115 new GraphicsProviderImpl(
116 Gdiplus::Graphics::FromHDC(pSysData
->hDC
))));
118 maArguments
.realloc(0);
121 void Canvas::disposeThis()
123 ::osl::MutexGuard
aGuard( m_aMutex
);
125 mxComponentContext
.clear();
128 CanvasBaseT::disposeThis();
131 OUString SAL_CALL
Canvas::getServiceName( ) throw (uno::RuntimeException
)
133 return OUString( CANVAS_SERVICE_NAME
);
136 BitmapCanvas::BitmapCanvas( const uno::Sequence
< uno::Any
>& aArguments
,
137 const uno::Reference
< uno::XComponentContext
>& rxContext
) :
138 maArguments(aArguments
),
139 mxComponentContext( rxContext
),
144 void BitmapCanvas::initialize()
146 // #i64742# Only perform initialization when not in probe mode
147 if( maArguments
.getLength() == 0 )
150 VERBOSE_TRACE( "BitmapCanvas::initialize called" );
152 // At index 1, we expect a HWND handle here, containing a
153 // pointer to a valid window, on which to output
154 // At index 2, we expect the current window bound rect
155 ENSURE_ARG_OR_THROW( maArguments
.getLength() >= 6 &&
156 maArguments
[5].getValueTypeClass() == uno::TypeClass_SEQUENCE
,
157 "Canvas::initialize: wrong number of arguments, or wrong types" );
159 uno::Sequence
<sal_Int8
> aSeq
;
160 maArguments
[5] >>= aSeq
;
162 const SystemGraphicsData
* pSysData
=reinterpret_cast<const SystemGraphicsData
*>(aSeq
.getConstArray());
163 if( !pSysData
|| !pSysData
->hDC
)
164 throw lang::NoSupportException( "Passed SystemGraphicsData or HDC invalid!");
167 maArguments
[0] >>= nPtr
;
168 OutputDevice
* pOutDev
= reinterpret_cast<OutputDevice
*>(nPtr
);
169 ENSURE_ARG_OR_THROW( pOutDev
!= NULL
,"Canvas::initialize: invalid OutDev pointer" );
172 maDeviceHelper
.init( pSysData
->hDC
, pOutDev
, *this );
173 maCanvasHelper
.setDevice( *this );
175 // check whether we can actually provide a BitmapCanvas
176 // here. for this, check whether the HDC has a bitmap
179 hBmp
=(HBITMAP
)GetCurrentObject(pSysData
->hDC
, OBJ_BITMAP
);
180 if( !hBmp
|| GetObjectType(pSysData
->hDC
) != OBJ_MEMDC
)
182 throw lang::NoSupportException( "Passed HDC is no mem DC/has no bitmap selected!");
185 mpTarget
.reset( new DXBitmap(
187 Gdiplus::Bitmap::FromHBITMAP(
191 maCanvasHelper
.setTarget( mpTarget
);
193 maArguments
.realloc(0);
196 void BitmapCanvas::disposeThis()
198 ::osl::MutexGuard
aGuard( m_aMutex
);
201 mxComponentContext
.clear();
204 BitmapCanvasBaseT::disposeThis();
207 OUString SAL_CALL
BitmapCanvas::getServiceName( ) throw (uno::RuntimeException
)
209 return OUString( BITMAPCANVAS_SERVICE_NAME
);
212 IBitmapSharedPtr
BitmapCanvas::getBitmap() const
217 static uno::Reference
<uno::XInterface
> initCanvas( Canvas
* pCanvas
)
219 uno::Reference
<uno::XInterface
> xRet(static_cast<cppu::OWeakObject
*>(pCanvas
));
220 pCanvas
->initialize();
224 namespace sdecl
= comphelper::service_decl
;
225 sdecl::class_
<Canvas
, sdecl::with_args
<true> > serviceImpl1(&initCanvas
);
226 const sdecl::ServiceDecl
dxCanvasDecl(
228 CANVAS_IMPLEMENTATION_NAME
,
229 CANVAS_SERVICE_NAME
);
231 static uno::Reference
<uno::XInterface
> initBitmapCanvas( BitmapCanvas
* pCanvas
)
233 uno::Reference
<uno::XInterface
> xRet(static_cast<cppu::OWeakObject
*>(pCanvas
));
234 pCanvas
->initialize();
238 namespace sdecl
= comphelper::service_decl
;
239 sdecl::class_
<BitmapCanvas
, sdecl::with_args
<true> > serviceImpl2(&initBitmapCanvas
);
240 const sdecl::ServiceDecl
dxBitmapCanvasDecl(
242 BITMAPCANVAS_IMPLEMENTATION_NAME
,
243 BITMAPCANVAS_SERVICE_NAME
);
246 // The C shared lib entry points
247 COMPHELPER_SERVICEDECL_EXPORTS2(gdipluscanvas
,
248 dxcanvas::dxCanvasDecl
,
249 dxcanvas::dxBitmapCanvasDecl
)
251 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */