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>
39 #include <tools/diagnose_ex.h>
40 #include <vcl/sysdata.hxx>
42 #include <vcl/skia/SkiaHelper.hxx>
43 
44 #include <canvas/canvastools.hxx>
45 
46 #include "dx_canvas.hxx"
47 #include "dx_graphicsprovider.hxx"
48 #include "dx_winstuff.hxx"
49 
50 using namespace ::com::sun::star;
51 
52 namespace 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 
80  // tdf#93870 - force VCL canvas in OpenGL mode for now.
83 
84  SAL_INFO("canvas.directx", "Canvas::initialize called" );
85 
86  // At index 1, we expect a HWND handle here, containing a
87  // pointer to a valid window, on which to output
88  // At index 2, we expect the current window bound rect
89  ENSURE_ARG_OR_THROW( maArguments.getLength() >= 5 &&
90  maArguments[4].getValueTypeClass() == uno::TypeClass_SEQUENCE,
91  "Canvas::initialize: wrong number of arguments, or wrong types" );
92 
93  uno::Sequence<sal_Int8> aSeq;
94  maArguments[4] >>= aSeq;
95 
96  const SystemGraphicsData* pSysData=reinterpret_cast<const SystemGraphicsData*>(aSeq.getConstArray());
97  if( !pSysData || !pSysData->hDC )
98  throw lang::NoSupportException("Passed SystemGraphicsData or HDC invalid!");
99 
100  sal_Int64 nPtr = 0;
101  maArguments[0] >>= nPtr;
102  OutputDevice* pOutDev = reinterpret_cast<OutputDevice*>(nPtr);
103  ENSURE_ARG_OR_THROW( pOutDev != nullptr,"Canvas::initialize: invalid OutDev pointer" );
104 
105  // setup helper
106  maDeviceHelper.init( pSysData->hDC, pOutDev, *this );
107  maCanvasHelper.setDevice( *this );
108  maCanvasHelper.setTarget(
109  std::make_shared<GraphicsProviderImpl>(
110  GraphicsSharedPtr(Gdiplus::Graphics::FromHDC(pSysData->hDC))));
111 
112  maArguments.realloc(0);
113  }
114 
115  void Canvas::disposeThis()
116  {
117  ::osl::MutexGuard aGuard( m_aMutex );
118 
119  mxComponentContext.clear();
120 
121  // forward to parent
122  CanvasBaseT::disposeThis();
123  }
124 
125  OUString SAL_CALL Canvas::getServiceName( )
126  {
127  return "com.sun.star.rendering.Canvas.GDI+";
128  }
129 
130  // XServiceInfo
131  css::uno::Sequence<OUString> Canvas::getSupportedServiceNames( )
132  {
133  return { "com.sun.star.rendering.Canvas.GDI+" };
134  }
135  OUString Canvas::getImplementationName( )
136  {
137  return "com.sun.star.comp.rendering.Canvas.GDI+";
138  }
139  sal_Bool Canvas::supportsService( const OUString& sServiceName )
140  {
141  return cppu::supportsService(this, sServiceName);
142  }
143 
144  BitmapCanvas::BitmapCanvas( const uno::Sequence< uno::Any >& aArguments,
145  const uno::Reference< uno::XComponentContext >& rxContext ) :
146  maArguments(aArguments),
147  mxComponentContext( rxContext ),
148  mpTarget()
149  {
150  }
151 
153  {
154  // #i64742# Only perform initialization when not in probe mode
155  if( maArguments.getLength() == 0 )
156  return;
157 
158  SAL_INFO("canvas.directx", "BitmapCanvas::initialize called" );
159 
160  // At index 1, we expect a HWND handle here, containing a
161  // pointer to a valid window, on which to output
162  // At index 2, we expect the current window bound rect
163  ENSURE_ARG_OR_THROW( maArguments.getLength() >= 5 &&
164  maArguments[4].getValueTypeClass() == uno::TypeClass_SEQUENCE,
165  "Canvas::initialize: wrong number of arguments, or wrong types" );
166 
167  uno::Sequence<sal_Int8> aSeq;
168  maArguments[4] >>= aSeq;
169 
170  const SystemGraphicsData* pSysData=reinterpret_cast<const SystemGraphicsData*>(aSeq.getConstArray());
171  if( !pSysData || !pSysData->hDC )
172  throw lang::NoSupportException( "Passed SystemGraphicsData or HDC invalid!");
173 
174  sal_Int64 nPtr = 0;
175  maArguments[0] >>= nPtr;
176  OutputDevice* pOutDev = reinterpret_cast<OutputDevice*>(nPtr);
177  ENSURE_ARG_OR_THROW( pOutDev != nullptr,"Canvas::initialize: invalid OutDev pointer" );
178 
179  // setup helper
180  maDeviceHelper.init( pSysData->hDC, pOutDev, *this );
181  maCanvasHelper.setDevice( *this );
182 
183  // check whether we can actually provide a BitmapCanvas
184  // here. for this, check whether the HDC has a bitmap
185  // selected.
186  HBITMAP hBmp;
187  hBmp=static_cast<HBITMAP>(GetCurrentObject(pSysData->hDC, OBJ_BITMAP));
188  if( !hBmp || GetObjectType(pSysData->hDC) != OBJ_MEMDC )
189  {
190  throw lang::NoSupportException( "Passed HDC is no mem DC/has no bitmap selected!");
191  }
192 
193  mpTarget = std::make_shared<DXBitmap>(
195  Gdiplus::Bitmap::FromHBITMAP(
196  hBmp, nullptr) ),
197  false );
198 
199  maCanvasHelper.setTarget( mpTarget );
200 
201  maArguments.realloc(0);
202  }
203 
205  {
206  ::osl::MutexGuard aGuard( m_aMutex );
207 
208  mpTarget.reset();
209  mxComponentContext.clear();
210 
211  // forward to parent
212  BitmapCanvasBaseT::disposeThis();
213  }
214 
215  OUString SAL_CALL BitmapCanvas::getServiceName( )
216  {
217  return "com.sun.star.rendering.BitmapCanvas.GDI+";
218  }
219 
220  // XServiceInfo
221  css::uno::Sequence<OUString> BitmapCanvas::getSupportedServiceNames( )
222  {
223  return { "com.sun.star.rendering.BitmapCanvas.GDI+" };
224  }
226  {
227  return "com.sun.star.comp.rendering.BitmapCanvas.GDI+";
228  }
229  sal_Bool BitmapCanvas::supportsService( const OUString& sServiceName )
230  {
231  return cppu::supportsService(this, sServiceName);
232  }
233 
235  {
236  return mpTarget;
237  }
238 
239  extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
241  css::uno::XComponentContext* context, css::uno::Sequence<css::uno::Any> const& args)
242  {
243  rtl::Reference<Canvas> xCanvas(new Canvas(args, context));
244  xCanvas->initialize();
245  xCanvas->acquire();
246  return static_cast<cppu::OWeakObject*>(xCanvas.get());
247  }
248 
249  extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
251  css::uno::XComponentContext* context, css::uno::Sequence<css::uno::Any> const& args)
252  {
253  rtl::Reference<BitmapCanvas> xCanvas(new BitmapCanvas(args, context));
254  xCanvas->initialize();
255  xCanvas->acquire();
256  return static_cast<cppu::OWeakObject*>(xCanvas.get());
257  }
258 }
259 
260 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
Canvas(const css::uno::Sequence< css::uno::Any > &aArguments, const css::uno::Reference< css::uno::XComponentContext > &rxContext)
osl::Mutex m_aMutex
std::shared_ptr< IBitmap > IBitmapSharedPtr
Definition: dx_ibitmap.hxx:58
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:240
virtual void disposeThis() override
Dispose all internal references.
Definition: dx_canvas.cxx:204
const BorderLinePrimitive2D *pCandidateB assert(pCandidateA)
bool CPPUHELPER_DLLPUBLIC supportsService(css::lang::XServiceInfo *implementation, rtl::OUString const &name)
Product of this component's factory.
Definition: dx_canvas.hxx:137
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:250
T * mpTarget
static bool isVCLOpenGLEnabled()
Product of this component's factory.
Definition: dx_canvas.hxx:76
GraphicsSharedPtr mpGraphics
Definition: dx_canvas.cxx:59
css::uno::Reference< css::uno::XComponentContext > mxComponentContext
unsigned char sal_Bool
IBitmapSharedPtr mpTarget
Definition: dx_canvas.hxx:169
css::uno::Reference< css::uno::XComponentContext > mxComponentContext
Definition: dx_canvas.hxx:168
#define ENSURE_ARG_OR_THROW(c, m)
std::shared_ptr< Gdiplus::Bitmap > BitmapSharedPtr
Definition: dx_winstuff.hxx:62
virtual css::uno::Sequence< OUString > SAL_CALL getSupportedServiceNames() override
Definition: dx_canvas.cxx:221
VCL_DLLPUBLIC bool isVCLSkiaEnabled()
virtual sal_Bool SAL_CALL supportsService(const OUString &) override
Definition: dx_canvas.cxx:229
css::uno::Sequence< css::uno::Any > maArguments
Definition: dx_canvas.hxx:167
virtual IBitmapSharedPtr getBitmap() const override
Definition: dx_canvas.cxx:234
#define SAL_INFO(area, stream)
virtual OUString SAL_CALL getServiceName() override
Definition: dx_canvas.cxx:215
Sequence< sal_Int8 > aSeq
virtual OUString SAL_CALL getImplementationName() override
Definition: dx_canvas.cxx:225
std::shared_ptr< Gdiplus::Graphics > GraphicsSharedPtr
Definition: dx_winstuff.hxx:60