Simplify a bit
[LibreOffice.git] / canvas / source / directx / dx_canvas.cxx
blobc19ea1ea901739634577679c09a1416c25a5612c
1 // /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 .
20 #include <sal/config.h>
22 #include <memory>
23 #include <utility>
25 #include <sal/log.hxx>
27 #include <basegfx/matrix/b2dhommatrix.hxx>
28 #include <basegfx/numeric/ftools.hxx>
29 #include <basegfx/point/b2dpoint.hxx>
30 #include <basegfx/utils/canvastools.hxx>
31 #include <com/sun/star/awt/XSystemDependentWindowPeer.hpp>
32 #include <com/sun/star/awt/XWindow.hpp>
33 #include <com/sun/star/lang/NoSupportException.hpp>
34 #include <com/sun/star/lang/XSingleServiceFactory.hpp>
35 #include <com/sun/star/registry/XRegistryKey.hpp>
36 #include <com/sun/star/uno/XComponentContext.hpp>
37 #include <cppuhelper/supportsservice.hxx>
38 #include <osl/mutex.hxx>
39 #include <comphelper/diagnose_ex.hxx>
40 #include <vcl/sysdata.hxx>
41 #include <vcl/skia/SkiaHelper.hxx>
42 #include <vcl/window.hxx>
44 #include <canvas/canvastools.hxx>
46 #include "dx_canvas.hxx"
47 #include "dx_graphicsprovider.hxx"
48 #include "dx_winstuff.hxx"
50 using namespace ::com::sun::star;
52 namespace dxcanvas
54 namespace {
56 /// Actual canonical implementation of the GraphicsProvider interface
57 class GraphicsProviderImpl : public GraphicsProvider
59 GraphicsSharedPtr mpGraphics;
60 public:
61 explicit GraphicsProviderImpl( GraphicsSharedPtr && pGraphics ) : mpGraphics( std::move(pGraphics) ) {}
62 virtual GraphicsSharedPtr getGraphics() override { return mpGraphics; }
67 Canvas::Canvas( const uno::Sequence< uno::Any >& aArguments,
68 const uno::Reference< uno::XComponentContext >& rxContext ) :
69 maArguments(aArguments),
70 mxComponentContext( rxContext )
74 void Canvas::initialize()
76 // #i64742# Only perform initialization when not in probe mode
77 if( maArguments.getLength() == 0 )
78 return;
80 assert( !SkiaHelper::isVCLSkiaEnabled() );
82 SAL_INFO("canvas.directx", "Canvas::initialize called" );
84 // At index 1, we expect a HWND handle here, containing a
85 // pointer to a valid window, on which to output
86 // At index 2, we expect the current window bound rect
87 ENSURE_ARG_OR_THROW( maArguments.getLength() >= 5 &&
88 maArguments[4].getValueTypeClass() == uno::TypeClass_SEQUENCE,
89 "Canvas::initialize: wrong number of arguments, or wrong types" );
91 uno::Sequence<sal_Int8> aSeq;
92 maArguments[4] >>= aSeq;
94 const SystemGraphicsData* pSysData=reinterpret_cast<const SystemGraphicsData*>(aSeq.getConstArray());
95 if( !pSysData || !pSysData->hDC )
96 throw lang::NoSupportException("Passed SystemGraphicsData or HDC invalid!");
98 sal_Int64 nPtr = 0;
99 maArguments[0] >>= nPtr;
100 OutputDevice* pOutDev = reinterpret_cast<OutputDevice*>(nPtr);
101 ENSURE_ARG_OR_THROW( pOutDev != nullptr,"Canvas::initialize: invalid OutDev pointer" );
103 // setup helper
104 maDeviceHelper.init( pSysData->hDC, pOutDev, *this );
105 maCanvasHelper.setDevice( *this );
106 maCanvasHelper.setTarget(
107 std::make_shared<GraphicsProviderImpl>(
108 GraphicsSharedPtr(Gdiplus::Graphics::FromHDC(pSysData->hDC))));
110 maArguments.realloc(0);
113 void Canvas::disposeThis()
115 ::osl::MutexGuard aGuard( m_aMutex );
117 mxComponentContext.clear();
119 // forward to parent
120 CanvasBaseT::disposeThis();
123 OUString SAL_CALL Canvas::getServiceName( )
125 return "com.sun.star.rendering.Canvas.GDI+";
128 // XServiceInfo
129 css::uno::Sequence<OUString> Canvas::getSupportedServiceNames( )
131 return { "com.sun.star.rendering.Canvas.GDI+" };
133 OUString Canvas::getImplementationName( )
135 return "com.sun.star.comp.rendering.Canvas.GDI+";
137 sal_Bool Canvas::supportsService( const OUString& sServiceName )
139 return cppu::supportsService(this, sServiceName);
142 BitmapCanvas::BitmapCanvas( const uno::Sequence< uno::Any >& aArguments,
143 const uno::Reference< uno::XComponentContext >& rxContext ) :
144 maArguments(aArguments),
145 mxComponentContext( rxContext ),
146 mpTarget()
150 void BitmapCanvas::initialize()
152 // #i64742# Only perform initialization when not in probe mode
153 if( maArguments.getLength() == 0 )
154 return;
156 SAL_INFO("canvas.directx", "BitmapCanvas::initialize called" );
158 // At index 1, we expect a HWND handle here, containing a
159 // pointer to a valid window, on which to output
160 // At index 2, we expect the current window bound rect
161 ENSURE_ARG_OR_THROW( maArguments.getLength() >= 5 &&
162 maArguments[4].getValueTypeClass() == uno::TypeClass_SEQUENCE,
163 "Canvas::initialize: wrong number of arguments, or wrong types" );
165 uno::Sequence<sal_Int8> aSeq;
166 maArguments[4] >>= aSeq;
168 const SystemGraphicsData* pSysData=reinterpret_cast<const SystemGraphicsData*>(aSeq.getConstArray());
169 if( !pSysData || !pSysData->hDC )
170 throw lang::NoSupportException( "Passed SystemGraphicsData or HDC invalid!");
172 sal_Int64 nPtr = 0;
173 maArguments[0] >>= nPtr;
174 OutputDevice* pOutDev = reinterpret_cast<OutputDevice*>(nPtr);
175 ENSURE_ARG_OR_THROW( pOutDev != nullptr,"Canvas::initialize: invalid OutDev pointer" );
177 // setup helper
178 maDeviceHelper.init( pSysData->hDC, pOutDev, *this );
179 maCanvasHelper.setDevice( *this );
181 // check whether we can actually provide a BitmapCanvas
182 // here. for this, check whether the HDC has a bitmap
183 // selected.
184 HBITMAP hBmp;
185 hBmp=static_cast<HBITMAP>(GetCurrentObject(pSysData->hDC, OBJ_BITMAP));
186 if( !hBmp || GetObjectType(pSysData->hDC) != OBJ_MEMDC )
188 throw lang::NoSupportException( "Passed HDC is no mem DC/has no bitmap selected!");
191 mpTarget = std::make_shared<DXBitmap>(
192 BitmapSharedPtr(
193 Gdiplus::Bitmap::FromHBITMAP(
194 hBmp, nullptr) ),
195 false );
197 maCanvasHelper.setTarget( mpTarget );
199 maArguments.realloc(0);
202 void BitmapCanvas::disposeThis()
204 ::osl::MutexGuard aGuard( m_aMutex );
206 mpTarget.reset();
207 mxComponentContext.clear();
209 // forward to parent
210 BitmapCanvasBaseT::disposeThis();
213 OUString SAL_CALL BitmapCanvas::getServiceName( )
215 return "com.sun.star.rendering.BitmapCanvas.GDI+";
218 // XServiceInfo
219 css::uno::Sequence<OUString> BitmapCanvas::getSupportedServiceNames( )
221 return { "com.sun.star.rendering.BitmapCanvas.GDI+" };
223 OUString BitmapCanvas::getImplementationName( )
225 return "com.sun.star.comp.rendering.BitmapCanvas.GDI+";
227 sal_Bool BitmapCanvas::supportsService( const OUString& sServiceName )
229 return cppu::supportsService(this, sServiceName);
232 IBitmapSharedPtr BitmapCanvas::getBitmap() const
234 return mpTarget;
237 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
238 canvas_gdiplus_Canvas_get_implementation(
239 css::uno::XComponentContext* context, css::uno::Sequence<css::uno::Any> const& args)
241 rtl::Reference<Canvas> xCanvas(new Canvas(args, context));
242 xCanvas->initialize();
243 return cppu::acquire(xCanvas.get());
246 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
247 canvas_gdiplus_BitmapCanvas_get_implementation(
248 css::uno::XComponentContext* context, css::uno::Sequence<css::uno::Any> const& args)
250 rtl::Reference<BitmapCanvas> xCanvas(new BitmapCanvas(args, context));
251 xCanvas->initialize();
252 return cppu::acquire(xCanvas.get());
256 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */