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