Branch libreoffice-5-0-4
[LibreOffice.git] / canvas / source / cairo / cairo_devicehelper.cxx
blob7fd8a466333451e839d91f54d48524584a619c10
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 <canvas/debug.hxx>
21 #include <canvas/verbosetrace.hxx>
22 #include <canvas/canvastools.hxx>
24 #include <osl/mutex.hxx>
25 #include <cppuhelper/compbase1.hxx>
27 #include <com/sun/star/lang/NoSupportException.hpp>
29 #include <toolkit/helper/vclunohelper.hxx>
30 #include <basegfx/tools/canvastools.hxx>
31 #include <basegfx/tools/unopolypolygon.hxx>
33 #include <vcl/canvastools.hxx>
34 #include <vcl/dibtools.hxx>
36 #include <tools/stream.hxx>
38 #include "cairo_spritecanvas.hxx"
39 #include "cairo_canvasbitmap.hxx"
40 #include "cairo_devicehelper.hxx"
42 using namespace ::cairo;
43 using namespace ::com::sun::star;
45 namespace cairocanvas
47 DeviceHelper::DeviceHelper() :
48 mpSurfaceProvider( NULL ),
49 mpRefDevice( NULL ),
50 mpSurface()
54 void DeviceHelper::implInit( SurfaceProvider& rSurfaceProvider,
55 OutputDevice& rRefDevice )
57 mpSurfaceProvider = &rSurfaceProvider;
58 mpRefDevice = &rRefDevice;
60 // no own surface, this is handled by derived classes
63 void DeviceHelper::init( SurfaceProvider& rSurfaceProvider,
64 OutputDevice& rRefDevice )
66 implInit(rSurfaceProvider, rRefDevice);
68 OutputDevice* pOutDev = getOutputDevice();
69 mpSurface = pOutDev->CreateSurface(pOutDev->GetOutOffXPixel(),
70 pOutDev->GetOutOffYPixel(),
71 pOutDev->GetOutputWidthPixel(),
72 pOutDev->GetOutputHeightPixel());
75 void DeviceHelper::disposing()
77 // release all references
78 mpSurface.reset();
79 mpRefDevice = NULL;
80 mpSurfaceProvider = NULL;
83 void DeviceHelper::setSize( const ::basegfx::B2ISize& rSize )
85 SAL_INFO(
86 "canvas.cairo",
87 "device size " << rSize.getX() << " x " << rSize.getY());
89 if( !mpRefDevice )
90 return; // disposed
92 OutputDevice* pOutDev = getOutputDevice();
94 // X11 only
95 bool bReuseSurface = mpSurface &&
96 mpSurface->Resize(rSize.getX() + pOutDev->GetOutOffXPixel(),
97 rSize.getY() + pOutDev->GetOutOffYPixel());
99 if (!bReuseSurface)
101 mpSurface = pOutDev->CreateSurface(
102 pOutDev->GetOutOffXPixel(),
103 pOutDev->GetOutOffYPixel(),
104 rSize.getX(), rSize.getY() );
108 geometry::RealSize2D DeviceHelper::getPhysicalResolution()
110 // Map a one-by-one millimeter box to pixel
111 const MapMode aOldMapMode( mpRefDevice->GetMapMode() );
112 mpRefDevice->SetMapMode( MapMode(MAP_MM) );
113 const Size aPixelSize( mpRefDevice->LogicToPixel(Size(1,1)) );
114 mpRefDevice->SetMapMode( aOldMapMode );
116 return vcl::unotools::size2DFromSize( aPixelSize );
119 geometry::RealSize2D DeviceHelper::getPhysicalSize()
121 if( !mpRefDevice )
122 return ::canvas::tools::createInfiniteSize2D(); // we're disposed
124 // Map the pixel dimensions of the output window to millimeter
125 const MapMode aOldMapMode( mpRefDevice->GetMapMode() );
126 mpRefDevice->SetMapMode( MapMode(MAP_MM) );
127 const Size aLogSize( mpRefDevice->PixelToLogic(mpRefDevice->GetOutputSizePixel()) );
128 mpRefDevice->SetMapMode( aOldMapMode );
130 return vcl::unotools::size2DFromSize( aLogSize );
133 uno::Reference< rendering::XLinePolyPolygon2D > DeviceHelper::createCompatibleLinePolyPolygon(
134 const uno::Reference< rendering::XGraphicDevice >& ,
135 const uno::Sequence< uno::Sequence< geometry::RealPoint2D > >& points )
137 // disposed?
138 if( !mpSurfaceProvider )
139 return uno::Reference< rendering::XLinePolyPolygon2D >(); // we're disposed
141 return uno::Reference< rendering::XLinePolyPolygon2D >(
142 new ::basegfx::unotools::UnoPolyPolygon(
143 ::basegfx::unotools::polyPolygonFromPoint2DSequenceSequence( points ) ) );
146 uno::Reference< rendering::XBezierPolyPolygon2D > DeviceHelper::createCompatibleBezierPolyPolygon(
147 const uno::Reference< rendering::XGraphicDevice >& ,
148 const uno::Sequence< uno::Sequence< geometry::RealBezierSegment2D > >& points )
150 // disposed?
151 if( !mpSurfaceProvider )
152 return uno::Reference< rendering::XBezierPolyPolygon2D >(); // we're disposed
154 return uno::Reference< rendering::XBezierPolyPolygon2D >(
155 new ::basegfx::unotools::UnoPolyPolygon(
156 ::basegfx::unotools::polyPolygonFromBezier2DSequenceSequence( points ) ) );
159 uno::Reference< rendering::XBitmap > DeviceHelper::createCompatibleBitmap(
160 const uno::Reference< rendering::XGraphicDevice >& rDevice,
161 const geometry::IntegerSize2D& size )
163 // disposed?
164 if( !mpSurfaceProvider )
165 return uno::Reference< rendering::XBitmap >(); // we're disposed
167 return uno::Reference< rendering::XBitmap >(
168 new CanvasBitmap(
169 ::basegfx::unotools::b2ISizeFromIntegerSize2D( size ),
170 SurfaceProviderRef(mpSurfaceProvider),
171 rDevice.get(),
172 false ));
175 uno::Reference< rendering::XVolatileBitmap > DeviceHelper::createVolatileBitmap(
176 const uno::Reference< rendering::XGraphicDevice >& ,
177 const geometry::IntegerSize2D& /*size*/ )
179 return uno::Reference< rendering::XVolatileBitmap >();
182 uno::Reference< rendering::XBitmap > DeviceHelper::createCompatibleAlphaBitmap(
183 const uno::Reference< rendering::XGraphicDevice >& rDevice,
184 const geometry::IntegerSize2D& size )
186 // disposed?
187 if( !mpSurfaceProvider )
188 return uno::Reference< rendering::XBitmap >(); // we're disposed
190 return uno::Reference< rendering::XBitmap >(
191 new CanvasBitmap(
192 ::basegfx::unotools::b2ISizeFromIntegerSize2D( size ),
193 SurfaceProviderRef(mpSurfaceProvider),
194 rDevice.get(),
195 true ));
198 uno::Reference< rendering::XVolatileBitmap > DeviceHelper::createVolatileAlphaBitmap(
199 const uno::Reference< rendering::XGraphicDevice >& ,
200 const geometry::IntegerSize2D& /*size*/ )
202 return uno::Reference< rendering::XVolatileBitmap >();
205 uno::Any DeviceHelper::isAccelerated() const
207 return ::com::sun::star::uno::makeAny(false);
210 uno::Any DeviceHelper::getDeviceHandle() const
212 return uno::makeAny( reinterpret_cast< sal_Int64 >(mpRefDevice.get()) );
215 uno::Any DeviceHelper::getSurfaceHandle() const
217 return uno::Any();
220 namespace
222 struct DeviceColorSpace: public rtl::StaticWithInit<uno::Reference<rendering::XColorSpace>,
223 DeviceColorSpace>
225 uno::Reference<rendering::XColorSpace> operator()()
227 return vcl::unotools::createStandardColorSpace();
232 uno::Reference<rendering::XColorSpace> DeviceHelper::getColorSpace() const
234 // always the same
235 return DeviceColorSpace::get();
238 void DeviceHelper::dumpScreenContent() const
240 static sal_Int32 nFilePostfixCount(0);
242 if( mpRefDevice )
244 OUString aFilename("dbg_frontbuffer");
245 aFilename += OUString::number(nFilePostfixCount);
246 aFilename += ".bmp";
248 SvFileStream aStream( aFilename, STREAM_STD_READWRITE );
250 const ::Point aEmptyPoint;
251 bool bOldMap( mpRefDevice->IsMapModeEnabled() );
252 mpRefDevice->EnableMapMode( false );
253 const ::Bitmap aTempBitmap(mpRefDevice->GetBitmap(aEmptyPoint, mpRefDevice->GetOutputSizePixel()));
254 WriteDIB(aTempBitmap, aStream, false, true);
255 mpRefDevice->EnableMapMode( bOldMap );
257 ++nFilePostfixCount;
261 SurfaceSharedPtr DeviceHelper::createSurface( const ::basegfx::B2ISize& rSize, int aContent )
263 if( mpSurface )
264 return mpSurface->getSimilar( aContent, rSize.getX(), rSize.getY() );
266 return SurfaceSharedPtr();
269 SurfaceSharedPtr DeviceHelper::createSurface( BitmapSystemData& rData, const Size& rSize )
271 if (mpRefDevice)
272 return mpRefDevice->CreateBitmapSurface(rData, rSize);
274 return SurfaceSharedPtr();
278 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */