LibreOffice Module canvas (master)  1
cairo_devicehelper.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 #include <sal/log.hxx>
22 
25 #include <tools/stream.hxx>
26 #include <vcl/bitmapex.hxx>
27 #include <vcl/canvastools.hxx>
28 #include <vcl/dibtools.hxx>
29 
30 #include <canvas/canvastools.hxx>
31 
32 #include "cairo_canvasbitmap.hxx"
33 #include "cairo_devicehelper.hxx"
34 
35 using namespace ::cairo;
36 using namespace ::com::sun::star;
37 
38 namespace cairocanvas
39 {
41  mpSurfaceProvider( nullptr ),
42  mpRefDevice( nullptr )
43  {
44  }
45 
46  void DeviceHelper::implInit( SurfaceProvider& rSurfaceProvider,
47  OutputDevice& rRefDevice )
48  {
49  mpSurfaceProvider = &rSurfaceProvider;
50  mpRefDevice = &rRefDevice;
51 
52  // no own surface, this is handled by derived classes
53  }
54 
55  void DeviceHelper::init( SurfaceProvider& rSurfaceProvider,
56  OutputDevice& rRefDevice )
57  {
58  implInit(rSurfaceProvider, rRefDevice);
59 
60  OutputDevice* pOutDev = getOutputDevice();
61  mpSurface = pOutDev->CreateSurface(pOutDev->GetOutOffXPixel(),
62  pOutDev->GetOutOffYPixel(),
63  pOutDev->GetOutputWidthPixel(),
64  pOutDev->GetOutputHeightPixel());
65  }
66 
68  {
69  // release all references
70  mpSurface.reset();
71  mpRefDevice = nullptr;
72  mpSurfaceProvider = nullptr;
73  }
74 
75  void DeviceHelper::setSize( const ::basegfx::B2ISize& rSize )
76  {
77  SAL_INFO(
78  "canvas.cairo",
79  "device size " << rSize.getX() << " x " << rSize.getY());
80 
81  if( !mpRefDevice )
82  return; // disposed
83 
84  OutputDevice* pOutDev = getOutputDevice();
85 
86  // X11 only
87  bool bReuseSurface = mpSurface &&
88  mpSurface->Resize(rSize.getX() + pOutDev->GetOutOffXPixel(),
89  rSize.getY() + pOutDev->GetOutOffYPixel());
90 
91  if (!bReuseSurface)
92  {
93  mpSurface = pOutDev->CreateSurface(
94  pOutDev->GetOutOffXPixel(),
95  pOutDev->GetOutOffYPixel(),
96  rSize.getX(), rSize.getY() );
97  }
98  }
99 
100  geometry::RealSize2D DeviceHelper::getPhysicalResolution()
101  {
102  // Map a one-by-one millimeter box to pixel
103  const MapMode aOldMapMode( mpRefDevice->GetMapMode() );
104  mpRefDevice->SetMapMode( MapMode(MapUnit::MapMM) );
105  const Size aPixelSize( mpRefDevice->LogicToPixel(Size(1,1)) );
106  mpRefDevice->SetMapMode( aOldMapMode );
107 
108  return vcl::unotools::size2DFromSize( aPixelSize );
109  }
110 
111  geometry::RealSize2D DeviceHelper::getPhysicalSize()
112  {
113  if( !mpRefDevice )
115 
116  // Map the pixel dimensions of the output window to millimeter
117  const MapMode aOldMapMode( mpRefDevice->GetMapMode() );
118  mpRefDevice->SetMapMode( MapMode(MapUnit::MapMM) );
120  mpRefDevice->SetMapMode( aOldMapMode );
121 
122  return vcl::unotools::size2DFromSize( aLogSize );
123  }
124 
125  uno::Reference< rendering::XLinePolyPolygon2D > DeviceHelper::createCompatibleLinePolyPolygon(
126  const uno::Reference< rendering::XGraphicDevice >& ,
127  const uno::Sequence< uno::Sequence< geometry::RealPoint2D > >& points )
128  {
129  // disposed?
130  if( !mpSurfaceProvider )
131  return uno::Reference< rendering::XLinePolyPolygon2D >(); // we're disposed
132 
133  return uno::Reference< rendering::XLinePolyPolygon2D >(
134  new ::basegfx::unotools::UnoPolyPolygon(
136  }
137 
138  uno::Reference< rendering::XBezierPolyPolygon2D > DeviceHelper::createCompatibleBezierPolyPolygon(
139  const uno::Reference< rendering::XGraphicDevice >& ,
140  const uno::Sequence< uno::Sequence< geometry::RealBezierSegment2D > >& points )
141  {
142  // disposed?
143  if( !mpSurfaceProvider )
144  return uno::Reference< rendering::XBezierPolyPolygon2D >(); // we're disposed
145 
146  return uno::Reference< rendering::XBezierPolyPolygon2D >(
147  new ::basegfx::unotools::UnoPolyPolygon(
149  }
150 
151  uno::Reference< rendering::XBitmap > DeviceHelper::createCompatibleBitmap(
152  const uno::Reference< rendering::XGraphicDevice >& rDevice,
153  const geometry::IntegerSize2D& size )
154  {
155  // disposed?
156  if( !mpSurfaceProvider )
157  return uno::Reference< rendering::XBitmap >(); // we're disposed
158 
159  return uno::Reference< rendering::XBitmap >(
160  new CanvasBitmap(
163  rDevice.get(),
164  false ));
165  }
166 
167  uno::Reference< rendering::XVolatileBitmap > DeviceHelper::createVolatileBitmap(
168  const uno::Reference< rendering::XGraphicDevice >& ,
169  const geometry::IntegerSize2D& /*size*/ )
170  {
171  return uno::Reference< rendering::XVolatileBitmap >();
172  }
173 
174  uno::Reference< rendering::XBitmap > DeviceHelper::createCompatibleAlphaBitmap(
175  const uno::Reference< rendering::XGraphicDevice >& rDevice,
176  const geometry::IntegerSize2D& size )
177  {
178  // disposed?
179  if( !mpSurfaceProvider )
180  return uno::Reference< rendering::XBitmap >(); // we're disposed
181 
182  return uno::Reference< rendering::XBitmap >(
183  new CanvasBitmap(
186  rDevice.get(),
187  true ));
188  }
189 
190  uno::Reference< rendering::XVolatileBitmap > DeviceHelper::createVolatileAlphaBitmap(
191  const uno::Reference< rendering::XGraphicDevice >& ,
192  const geometry::IntegerSize2D& /*size*/ )
193  {
194  return uno::Reference< rendering::XVolatileBitmap >();
195  }
196 
198  {
199  return css::uno::Any(false);
200  }
201 
203  {
204  return uno::Any( reinterpret_cast< sal_Int64 >(mpRefDevice.get()) );
205  }
206 
208  {
209  return uno::Any();
210  }
211 
212  uno::Reference<rendering::XColorSpace> const & DeviceHelper::getColorSpace() const
213  {
214  static uno::Reference<rendering::XColorSpace> SPACE = vcl::unotools::createStandardColorSpace();
215  // always the same
216  return SPACE;
217  }
218 
220  {
221  static sal_Int32 nFilePostfixCount(0);
222 
223  if( !mpRefDevice )
224  return;
225 
226  OUString aFilename = "dbg_frontbuffer" + OUString::number(nFilePostfixCount) + ".bmp";
227 
228  SvFileStream aStream( aFilename, StreamMode::STD_READWRITE );
229 
230  const ::Point aEmptyPoint;
231  bool bOldMap( mpRefDevice->IsMapModeEnabled() );
232  mpRefDevice->EnableMapMode( false );
233  const ::BitmapEx aTempBitmap(mpRefDevice->GetBitmapEx(aEmptyPoint, mpRefDevice->GetOutputSizePixel()));
234  WriteDIB(aTempBitmap, aStream, false);
235  mpRefDevice->EnableMapMode( bOldMap );
236 
237  ++nFilePostfixCount;
238  }
239 
240  SurfaceSharedPtr DeviceHelper::createSurface( const ::basegfx::B2ISize& rSize, int aContent )
241  {
242  if( mpSurface )
243  return mpSurface->getSimilar( aContent, rSize.getX(), rSize.getY() );
244 
245  return SurfaceSharedPtr();
246  }
247 
249  {
250  if (mpRefDevice)
251  return mpRefDevice->CreateBitmapSurface(rData, rSize);
252 
253  return SurfaceSharedPtr();
254  }
255 }
256 
257 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
Target interface for XCachedPrimitive implementations.
bool IsMapModeEnabled() const
css::uno::Reference< css::rendering::XVolatileBitmap > createVolatileAlphaBitmap(const css::uno::Reference< css::rendering::XGraphicDevice > &rDevice, const css::geometry::IntegerSize2D &size)
tools::Long GetOutOffXPixel() const
cairo::SurfaceSharedPtr CreateSurface(const cairo::CairoSurfaceSharedPtr &rSurface) const
css::uno::Any getSurfaceHandle() const
const MapMode & GetMapMode() const
css::uno::Reference< css::rendering::XLinePolyPolygon2D > createCompatibleLinePolyPolygon(const css::uno::Reference< css::rendering::XGraphicDevice > &rDevice, const css::uno::Sequence< css::uno::Sequence< css::geometry::RealPoint2D > > &points)
cairo::SurfaceSharedPtr CreateBitmapSurface(const BitmapSystemData &rData, const Size &rSize) const
void disposing()
Dispose all internal references.
void EnableMapMode(bool bEnable=true)
void SetMapMode()
#define SPACE
::basegfx::B2DPolyPolygon polyPolygonFromPoint2DSequenceSequence(const uno::Sequence< uno::Sequence< geometry::RealPoint2D > > &points)
css::uno::Reference< css::rendering::XVolatileBitmap > createVolatileBitmap(const css::uno::Reference< css::rendering::XGraphicDevice > &rDevice, const css::geometry::IntegerSize2D &size)
::basegfx::B2DPolyPolygon polyPolygonFromBezier2DSequenceSequence(const uno::Sequence< uno::Sequence< geometry::RealBezierSegment2D > > &curves)
void init(SurfaceProvider &rSurfaceProvider, OutputDevice &rRefDevice)
init helper
std::shared_ptr< Surface > SurfaceSharedPtr
css::geometry::RealSize2D getPhysicalResolution()
void setSize(const ::basegfx::B2ISize &rSize)
css::uno::Reference< css::rendering::XBitmap > createCompatibleAlphaBitmap(const css::uno::Reference< css::rendering::XGraphicDevice > &rDevice, const css::geometry::IntegerSize2D &size)
tools::Long GetOutputHeightPixel() const
geometry::RealSize2D size2DFromSize(const Size &rSize)
css::uno::Any isAccelerated() const
Size GetOutputSizePixel() const
css::uno::Any getDeviceHandle() const
VclPtr< OutputDevice > mpRefDevice
tools::Long GetOutOffYPixel() const
SAL_WARN_UNUSED_RESULT Point PixelToLogic(const Point &rDevicePt) const
SAL_WARN_UNUSED_RESULT Point LogicToPixel(const Point &rLogicPt) const
::cairo::SurfaceSharedPtr createSurface(const ::basegfx::B2ISize &rSize, int aContent)
void implInit(SurfaceProvider &rSurfaceProvider, OutputDevice &rRefDevice)
init helper
::cairo::SurfaceSharedPtr mpSurface
SurfaceProvider * mpSurfaceProvider
Surface provider.
uno::Reference< rendering::XColorSpace > createStandardColorSpace()
css::geometry::RealSize2D getPhysicalSize()
::basegfx::B2IVector b2ISizeFromIntegerSize2D(const geometry::IntegerSize2D &rSize)
geometry::RealSize2D createInfiniteSize2D()
Create a RealSize2D with both coordinate values set to +infinity.
Definition: canvastools.cxx:67
#define SAL_INFO(area, stream)
css::uno::Reference< css::rendering::XBitmap > createCompatibleBitmap(const css::uno::Reference< css::rendering::XGraphicDevice > &rDevice, const css::geometry::IntegerSize2D &size)
::rtl::Reference< SurfaceProvider > SurfaceProviderRef
css::uno::Reference< css::rendering::XBezierPolyPolygon2D > createCompatibleBezierPolyPolygon(const css::uno::Reference< css::rendering::XGraphicDevice > &rDevice, const css::uno::Sequence< css::uno::Sequence< css::geometry::RealBezierSegment2D > > &points)
css::uno::Reference< css::rendering::XColorSpace > const & getColorSpace() const
BitmapEx GetBitmapEx(const Point &rSrcPt, const Size &rSize) const
OutputDevice * get() const
void dumpScreenContent() const
called when DumpScreenContent property is enabled on XGraphicDevice, and writes out bitmaps of curren...
OutputDevice * getOutputDevice() const
bool WriteDIB(const Bitmap &rSource, SvStream &rOStm, bool bCompressed, bool bFileHeader)
tools::Long GetOutputWidthPixel() const