LibreOffice Module canvas (master) 1
dx_canvas.cxx
Go to the documentation of this file.
1// /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
20#include <sal/config.h>
21
22#include <memory>
23#include <utility>
24
25#include <sal/log.hxx>
26
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>
38#include <osl/mutex.hxx>
40#include <vcl/sysdata.hxx>
42#include <vcl/window.hxx>
43
45
46#include "dx_canvas.hxx"
48#include "dx_winstuff.hxx"
49
50using namespace ::com::sun::star;
51
52namespace dxcanvas
53{
54 namespace {
55
57 class GraphicsProviderImpl : public GraphicsProvider
58 {
60 public:
61 explicit GraphicsProviderImpl( GraphicsSharedPtr && pGraphics ) : mpGraphics( std::move(pGraphics) ) {}
62 virtual GraphicsSharedPtr getGraphics() override { return mpGraphics; }
63 };
64
65 }
66
67 Canvas::Canvas( const uno::Sequence< uno::Any >& aArguments,
68 const uno::Reference< uno::XComponentContext >& rxContext ) :
69 maArguments(aArguments),
70 mxComponentContext( rxContext )
71 {
72 }
73
74 void Canvas::initialize()
75 {
76 // #i64742# Only perform initialization when not in probe mode
77 if( maArguments.getLength() == 0 )
78 return;
79
81
82 SAL_INFO("canvas.directx", "Canvas::initialize called" );
83
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" );
90
91 uno::Sequence<sal_Int8> aSeq;
92 maArguments[4] >>= aSeq;
93
94 const SystemGraphicsData* pSysData=reinterpret_cast<const SystemGraphicsData*>(aSeq.getConstArray());
95 if( !pSysData || !pSysData->hDC )
96 throw lang::NoSupportException("Passed SystemGraphicsData or HDC invalid!");
97
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" );
102
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))));
109
110 maArguments.realloc(0);
111 }
112
113 void Canvas::disposeThis()
114 {
115 ::osl::MutexGuard aGuard( m_aMutex );
116
117 mxComponentContext.clear();
118
119 // forward to parent
120 CanvasBaseT::disposeThis();
121 }
122
123 OUString SAL_CALL Canvas::getServiceName( )
124 {
125 return "com.sun.star.rendering.Canvas.GDI+";
126 }
127
128 // XServiceInfo
129 css::uno::Sequence<OUString> Canvas::getSupportedServiceNames( )
130 {
131 return { "com.sun.star.rendering.Canvas.GDI+" };
132 }
133 OUString Canvas::getImplementationName( )
134 {
135 return "com.sun.star.comp.rendering.Canvas.GDI+";
136 }
137 sal_Bool Canvas::supportsService( const OUString& sServiceName )
138 {
140 }
141
142 BitmapCanvas::BitmapCanvas( const uno::Sequence< uno::Any >& aArguments,
143 const uno::Reference< uno::XComponentContext >& rxContext ) :
144 maArguments(aArguments),
145 mxComponentContext( rxContext ),
146 mpTarget()
147 {
148 }
149
151 {
152 // #i64742# Only perform initialization when not in probe mode
153 if( maArguments.getLength() == 0 )
154 return;
155
156 SAL_INFO("canvas.directx", "BitmapCanvas::initialize called" );
157
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" );
164
165 uno::Sequence<sal_Int8> aSeq;
166 maArguments[4] >>= aSeq;
167
168 const SystemGraphicsData* pSysData=reinterpret_cast<const SystemGraphicsData*>(aSeq.getConstArray());
169 if( !pSysData || !pSysData->hDC )
170 throw lang::NoSupportException( "Passed SystemGraphicsData or HDC invalid!");
171
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" );
176
177 // setup helper
178 maDeviceHelper.init( pSysData->hDC, pOutDev, *this );
179 maCanvasHelper.setDevice( *this );
180
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 )
187 {
188 throw lang::NoSupportException( "Passed HDC is no mem DC/has no bitmap selected!");
189 }
190
191 mpTarget = std::make_shared<DXBitmap>(
193 Gdiplus::Bitmap::FromHBITMAP(
194 hBmp, nullptr) ),
195 false );
196
197 maCanvasHelper.setTarget( mpTarget );
198
199 maArguments.realloc(0);
200 }
201
203 {
204 ::osl::MutexGuard aGuard( m_aMutex );
205
206 mpTarget.reset();
207 mxComponentContext.clear();
208
209 // forward to parent
210 BitmapCanvasBaseT::disposeThis();
211 }
212
213 OUString SAL_CALL BitmapCanvas::getServiceName( )
214 {
215 return "com.sun.star.rendering.BitmapCanvas.GDI+";
216 }
217
218 // XServiceInfo
219 css::uno::Sequence<OUString> BitmapCanvas::getSupportedServiceNames( )
220 {
221 return { "com.sun.star.rendering.BitmapCanvas.GDI+" };
222 }
224 {
225 return "com.sun.star.comp.rendering.BitmapCanvas.GDI+";
226 }
227 sal_Bool BitmapCanvas::supportsService( const OUString& sServiceName )
228 {
230 }
231
233 {
234 return mpTarget;
235 }
236
237 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
239 css::uno::XComponentContext* context, css::uno::Sequence<css::uno::Any> const& args)
240 {
241 rtl::Reference<Canvas> xCanvas(new Canvas(args, context));
242 xCanvas->initialize();
243 return cppu::acquire(xCanvas.get());
244 }
245
246 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
248 css::uno::XComponentContext* context, css::uno::Sequence<css::uno::Any> const& args)
249 {
250 rtl::Reference<BitmapCanvas> xCanvas(new BitmapCanvas(args, context));
251 xCanvas->initialize();
252 return cppu::acquire(xCanvas.get());
253 }
254}
255
256/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
css::uno::Reference< css::uno::XComponentContext > mxComponentContext
constexpr OUStringLiteral sServiceName
Product of this component's factory.
Definition: dx_canvas.hxx:138
virtual css::uno::Sequence< OUString > SAL_CALL getSupportedServiceNames() override
Definition: dx_canvas.cxx:219
virtual OUString SAL_CALL getServiceName() override
Definition: dx_canvas.cxx:213
virtual void disposeThis() override
Dispose all internal references.
Definition: dx_canvas.cxx:202
css::uno::Sequence< css::uno::Any > maArguments
Definition: dx_canvas.hxx:167
virtual OUString SAL_CALL getImplementationName() override
Definition: dx_canvas.cxx:223
css::uno::Reference< css::uno::XComponentContext > mxComponentContext
Definition: dx_canvas.hxx:168
IBitmapSharedPtr mpTarget
Definition: dx_canvas.hxx:169
virtual sal_Bool SAL_CALL supportsService(const OUString &) override
Definition: dx_canvas.cxx:227
virtual IBitmapSharedPtr getBitmap() const override
Definition: dx_canvas.cxx:232
Product of this component's factory.
Definition: dx_canvas.hxx:77
Canvas(const css::uno::Sequence< css::uno::Any > &aArguments, const css::uno::Reference< css::uno::XComponentContext > &rxContext)
#define ENSURE_ARG_OR_THROW(c, m)
GraphicsSharedPtr mpGraphics
Definition: dx_canvas.cxx:59
std::mutex m_aMutex
Sequence< PropertyValue > aArguments
Sequence< sal_Int8 > aSeq
#define SAL_INFO(area, stream)
VCL_DLLPUBLIC bool isVCLSkiaEnabled()
bool CPPUHELPER_DLLPUBLIC supportsService(css::lang::XServiceInfo *implementation, rtl::OUString const &name)
std::shared_ptr< Gdiplus::Graphics > GraphicsSharedPtr
Definition: dx_winstuff.hxx:58
std::shared_ptr< IBitmap > IBitmapSharedPtr
Definition: dx_ibitmap.hxx:58
SAL_DLLPUBLIC_EXPORT css::uno::XInterface * canvas_gdiplus_BitmapCanvas_get_implementation(css::uno::XComponentContext *context, css::uno::Sequence< css::uno::Any > const &args)
Definition: dx_canvas.cxx:247
SAL_DLLPUBLIC_EXPORT css::uno::XInterface * canvas_gdiplus_Canvas_get_implementation(css::uno::XComponentContext *context, css::uno::Sequence< css::uno::Any > const &args)
Definition: dx_canvas.cxx:238
std::shared_ptr< Gdiplus::Bitmap > BitmapSharedPtr
Definition: dx_winstuff.hxx:60
args
T * mpTarget
unsigned char sal_Bool