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