cid#1607171 Data race condition
[LibreOffice.git] / canvas / source / cairo / cairo_devicehelper.cxx
blob3d3f7ef8301cd83f618cc0fa195fdcfc63524cf0
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <sal/config.h>
21 #include <sal/log.hxx>
23 #include <basegfx/utils/canvastools.hxx>
24 #include <basegfx/utils/unopolypolygon.hxx>
25 #include <tools/stream.hxx>
26 #include <vcl/bitmapex.hxx>
27 #include <vcl/canvastools.hxx>
28 #include <vcl/dibtools.hxx>
30 #include <canvas/canvastools.hxx>
32 #include "cairo_canvasbitmap.hxx"
33 #include "cairo_devicehelper.hxx"
35 using namespace ::cairo;
36 using namespace ::com::sun::star;
38 namespace cairocanvas
40 DeviceHelper::DeviceHelper() :
41 mpSurfaceProvider( nullptr ),
42 mpRefDevice( nullptr )
46 void DeviceHelper::implInit( SurfaceProvider& rSurfaceProvider,
47 OutputDevice& rRefDevice )
49 mpSurfaceProvider = &rSurfaceProvider;
50 mpRefDevice = &rRefDevice;
52 // no own surface, this is handled by derived classes
55 void DeviceHelper::init( SurfaceProvider& rSurfaceProvider,
56 OutputDevice& rRefDevice )
58 implInit(rSurfaceProvider, rRefDevice);
60 OutputDevice* pOutDev = getOutputDevice();
61 mpSurface = pOutDev->CreateSurface(pOutDev->GetOutOffXPixel(),
62 pOutDev->GetOutOffYPixel(),
63 pOutDev->GetOutputWidthPixel(),
64 pOutDev->GetOutputHeightPixel());
67 void DeviceHelper::disposing()
69 // release all references
70 mpSurface.reset();
71 mpRefDevice = nullptr;
72 mpSurfaceProvider = nullptr;
75 void DeviceHelper::setSize( const ::basegfx::B2ISize& rSize )
77 SAL_INFO(
78 "canvas.cairo",
79 "device size " << rSize.getWidth() << " x " << rSize.getHeight());
81 if( !mpRefDevice )
82 return; // disposed
84 OutputDevice* pOutDev = getOutputDevice();
86 // X11 only
87 bool bReuseSurface = mpSurface &&
88 mpSurface->Resize(rSize.getWidth() + pOutDev->GetOutOffXPixel(),
89 rSize.getHeight() + pOutDev->GetOutOffYPixel());
91 if (!bReuseSurface)
93 mpSurface = pOutDev->CreateSurface(
94 pOutDev->GetOutOffXPixel(),
95 pOutDev->GetOutOffYPixel(),
96 rSize.getWidth(), rSize.getHeight() );
100 geometry::RealSize2D DeviceHelper::getPhysicalResolution()
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 );
108 return vcl::unotools::size2DFromSize( aPixelSize );
111 geometry::RealSize2D DeviceHelper::getPhysicalSize()
113 if( !mpRefDevice )
114 return ::canvas::tools::createInfiniteSize2D(); // we're disposed
116 // Map the pixel dimensions of the output window to millimeter
117 const MapMode aOldMapMode( mpRefDevice->GetMapMode() );
118 mpRefDevice->SetMapMode( MapMode(MapUnit::MapMM) );
119 const Size aLogSize( mpRefDevice->PixelToLogic(mpRefDevice->GetOutputSizePixel()) );
120 mpRefDevice->SetMapMode( aOldMapMode );
122 return vcl::unotools::size2DFromSize( aLogSize );
125 uno::Reference< rendering::XLinePolyPolygon2D > DeviceHelper::createCompatibleLinePolyPolygon(
126 const uno::Reference< rendering::XGraphicDevice >& ,
127 const uno::Sequence< uno::Sequence< geometry::RealPoint2D > >& points )
129 // disposed?
130 if( !mpSurfaceProvider )
131 return uno::Reference< rendering::XLinePolyPolygon2D >(); // we're disposed
133 return uno::Reference< rendering::XLinePolyPolygon2D >(
134 new ::basegfx::unotools::UnoPolyPolygon(
135 ::basegfx::unotools::polyPolygonFromPoint2DSequenceSequence( points ) ) );
138 uno::Reference< rendering::XBezierPolyPolygon2D > DeviceHelper::createCompatibleBezierPolyPolygon(
139 const uno::Reference< rendering::XGraphicDevice >& ,
140 const uno::Sequence< uno::Sequence< geometry::RealBezierSegment2D > >& points )
142 // disposed?
143 if( !mpSurfaceProvider )
144 return uno::Reference< rendering::XBezierPolyPolygon2D >(); // we're disposed
146 return uno::Reference< rendering::XBezierPolyPolygon2D >(
147 new ::basegfx::unotools::UnoPolyPolygon(
148 ::basegfx::unotools::polyPolygonFromBezier2DSequenceSequence( points ) ) );
151 uno::Reference< rendering::XBitmap > DeviceHelper::createCompatibleBitmap(
152 const uno::Reference< rendering::XGraphicDevice >& rDevice,
153 const geometry::IntegerSize2D& size )
155 // disposed?
156 if( !mpSurfaceProvider )
157 return uno::Reference< rendering::XBitmap >(); // we're disposed
159 return uno::Reference< rendering::XBitmap >(
160 new CanvasBitmap(
161 ::basegfx::unotools::b2ISizeFromIntegerSize2D( size ),
162 SurfaceProviderRef(mpSurfaceProvider),
163 rDevice.get(),
164 false ));
167 uno::Reference< rendering::XVolatileBitmap > DeviceHelper::createVolatileBitmap(
168 const uno::Reference< rendering::XGraphicDevice >& ,
169 const geometry::IntegerSize2D& /*size*/ )
171 return uno::Reference< rendering::XVolatileBitmap >();
174 uno::Reference< rendering::XBitmap > DeviceHelper::createCompatibleAlphaBitmap(
175 const uno::Reference< rendering::XGraphicDevice >& rDevice,
176 const geometry::IntegerSize2D& size )
178 // disposed?
179 if( !mpSurfaceProvider )
180 return uno::Reference< rendering::XBitmap >(); // we're disposed
182 return uno::Reference< rendering::XBitmap >(
183 new CanvasBitmap(
184 ::basegfx::unotools::b2ISizeFromIntegerSize2D( size ),
185 SurfaceProviderRef(mpSurfaceProvider),
186 rDevice.get(),
187 true ));
190 uno::Reference< rendering::XVolatileBitmap > DeviceHelper::createVolatileAlphaBitmap(
191 const uno::Reference< rendering::XGraphicDevice >& ,
192 const geometry::IntegerSize2D& /*size*/ )
194 return uno::Reference< rendering::XVolatileBitmap >();
197 uno::Any DeviceHelper::isAccelerated() const
199 return css::uno::Any(false);
202 uno::Any DeviceHelper::getDeviceHandle() const
204 return uno::Any( reinterpret_cast< sal_Int64 >(mpRefDevice.get()) );
207 uno::Any DeviceHelper::getSurfaceHandle() const
209 return uno::Any();
212 uno::Reference<rendering::XColorSpace> const & DeviceHelper::getColorSpace() const
214 static uno::Reference<rendering::XColorSpace> SPACE = vcl::unotools::createStandardColorSpace();
215 // always the same
216 return SPACE;
219 void DeviceHelper::dumpScreenContent() const
221 static sal_Int32 nFilePostfixCount(0);
223 if( !mpRefDevice )
224 return;
226 OUString aFilename = "dbg_frontbuffer" + OUString::number(nFilePostfixCount) + ".bmp";
228 SvFileStream aStream( aFilename, StreamMode::STD_READWRITE );
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 );
237 ++nFilePostfixCount;
240 SurfaceSharedPtr DeviceHelper::createSurface( const ::basegfx::B2ISize& rSize, int aContent )
242 if( mpSurface )
243 return mpSurface->getSimilar( aContent, rSize.getWidth(), rSize.getHeight() );
245 return SurfaceSharedPtr();
248 SurfaceSharedPtr DeviceHelper::createSurface( BitmapSystemData const & rData, const Size& rSize )
250 if (mpRefDevice)
251 return mpRefDevice->CreateBitmapSurface(rData, rSize);
253 return SurfaceSharedPtr();
257 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */