1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 .
22 #include <headless/svpbmp.hxx>
23 #include <headless/svpinst.hxx>
24 #include <headless/svpvd.hxx>
25 #include <headless/svpgdi.hxx>
27 #include <basegfx/vector/b2ivector.hxx>
28 #include <comphelper/lok.hxx>
32 using namespace basegfx
;
34 SvpSalVirtualDevice::SvpSalVirtualDevice(cairo_surface_t
* pRefSurface
, cairo_surface_t
* pPreExistingTarget
)
35 : m_pRefSurface(pRefSurface
)
36 , m_pSurface(pPreExistingTarget
)
37 , m_bOwnsSurface(!pPreExistingTarget
)
39 cairo_surface_reference(m_pRefSurface
);
42 SvpSalVirtualDevice::~SvpSalVirtualDevice()
45 cairo_surface_destroy(m_pSurface
);
46 cairo_surface_destroy(m_pRefSurface
);
49 SvpSalGraphics
* SvpSalVirtualDevice::AddGraphics(SvpSalGraphics
* pGraphics
)
51 pGraphics
->setSurface(m_pSurface
, m_aFrameSize
);
52 m_aGraphics
.push_back(pGraphics
);
56 SalGraphics
* SvpSalVirtualDevice::AcquireGraphics()
58 return AddGraphics(new SvpSalGraphics());
61 void SvpSalVirtualDevice::ReleaseGraphics( SalGraphics
* pGraphics
)
63 m_aGraphics
.erase(std::remove(m_aGraphics
.begin(), m_aGraphics
.end(), dynamic_cast<SvpSalGraphics
*>(pGraphics
)), m_aGraphics
.end());
67 bool SvpSalVirtualDevice::SetSize( tools::Long nNewDX
, tools::Long nNewDY
)
69 return SetSizeUsingBuffer(nNewDX
, nNewDY
, nullptr);
72 bool SvpSalVirtualDevice::CreateSurface(tools::Long nNewDX
, tools::Long nNewDY
, sal_uInt8
*const pBuffer
)
76 cairo_surface_destroy(m_pSurface
);
81 // The buffer should only be set by VirtualDevice::SetOutputSizePixelScaleOffsetAndLOKBuffer()
82 // when used to draw a tile for LOK. It cannot be used for something else, because otherwise
83 // this would need a way to detect whether this is a tiled paint that needs LOK handling
84 // or whether it's that something else that just might happen to be called with LOK active.
85 assert(comphelper::LibreOfficeKit::isActive());
86 // Force scaling of the painting
87 double fScale
= comphelper::LibreOfficeKit::getDPIScale();
89 m_pSurface
= cairo_image_surface_create_for_data(pBuffer
, CAIRO_FORMAT_ARGB32
,
90 nNewDX
, nNewDY
, cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32
, nNewDX
));
91 dl_cairo_surface_set_device_scale(m_pSurface
, fScale
, fScale
);
93 else if(nNewDX
<= 32 && nNewDY
<= 32)
95 double fXScale
, fYScale
;
96 dl_cairo_surface_get_device_scale(m_pRefSurface
, &fXScale
, &fYScale
);
100 // Force image-based surface if small. Small VirtualDevice instances are often used for small
101 // temporary bitmaps that will eventually have GetBitmap() called on them, which would cause
102 // X Server roundtrip with Xlib-based surface, which may be way more costly than doing the drawing
103 // in software (which should be fairly cheap for small surfaces anyway).
104 m_pSurface
= cairo_surface_create_similar_image(m_pRefSurface
, CAIRO_FORMAT_ARGB32
, nNewDX
, nNewDY
);
105 dl_cairo_surface_set_device_scale(m_pSurface
, fXScale
, fYScale
);
109 m_pSurface
= cairo_surface_create_similar(m_pRefSurface
, CAIRO_CONTENT_COLOR_ALPHA
, nNewDX
, nNewDY
);
110 // Device scale is inherited in this case.
113 SAL_WARN_IF(cairo_surface_status(m_pSurface
) != CAIRO_STATUS_SUCCESS
, "vcl", "surface of size " << nNewDX
<< " by " << nNewDY
<< " creation failed with status of: " << cairo_status_to_string(cairo_surface_status(m_pSurface
)));
114 return cairo_surface_status(m_pSurface
) == CAIRO_STATUS_SUCCESS
;
117 bool SvpSalVirtualDevice::SetSizeUsingBuffer( tools::Long nNewDX
, tools::Long nNewDY
,
118 sal_uInt8
*const pBuffer
)
120 bool bSuccess
= true;
127 if (!m_pSurface
|| m_aFrameSize
.getX() != nNewDX
||
128 m_aFrameSize
.getY() != nNewDY
)
130 m_aFrameSize
= basegfx::B2IVector(nNewDX
, nNewDY
);
133 bSuccess
= CreateSurface(nNewDX
, nNewDY
, pBuffer
);
137 // update device in existing graphics
138 for (auto const& graphic
: m_aGraphics
)
139 graphic
->setSurface(m_pSurface
, m_aFrameSize
);
145 tools::Long
SvpSalVirtualDevice::GetWidth() const
147 return m_pSurface
? m_aFrameSize
.getX() : 0;
150 tools::Long
SvpSalVirtualDevice::GetHeight() const
152 return m_pSurface
? m_aFrameSize
.getY() : 0;
157 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */