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/svpvd.hxx>
23 #include <headless/svpgdi.hxx>
25 #include <basegfx/vector/b2ivector.hxx>
26 #include <comphelper/lok.hxx>
30 using namespace basegfx
;
32 SvpSalVirtualDevice::SvpSalVirtualDevice(cairo_surface_t
* pRefSurface
, cairo_surface_t
* pPreExistingTarget
)
33 : m_pRefSurface(pRefSurface
)
34 , m_pSurface(pPreExistingTarget
)
35 , m_bOwnsSurface(!pPreExistingTarget
)
37 cairo_surface_reference(m_pRefSurface
);
40 SvpSalVirtualDevice::~SvpSalVirtualDevice()
43 cairo_surface_destroy(m_pSurface
);
44 cairo_surface_destroy(m_pRefSurface
);
47 SvpSalGraphics
* SvpSalVirtualDevice::AddGraphics(SvpSalGraphics
* pGraphics
)
49 pGraphics
->setSurface(m_pSurface
, m_aFrameSize
);
50 m_aGraphics
.push_back(pGraphics
);
54 SalGraphics
* SvpSalVirtualDevice::AcquireGraphics()
56 return AddGraphics(new SvpSalGraphics());
59 void SvpSalVirtualDevice::ReleaseGraphics( SalGraphics
* pGraphics
)
61 std::erase(m_aGraphics
, dynamic_cast<SvpSalGraphics
*>(pGraphics
));
65 bool SvpSalVirtualDevice::SetSize( tools::Long nNewDX
, tools::Long nNewDY
)
67 return SetSizeUsingBuffer(nNewDX
, nNewDY
, nullptr);
70 bool SvpSalVirtualDevice::CreateSurface(tools::Long nNewDX
, tools::Long nNewDY
, sal_uInt8
*const pBuffer
)
74 cairo_surface_destroy(m_pSurface
);
79 // The buffer should only be set by VirtualDevice::SetOutputSizePixelScaleOffsetAndLOKBuffer()
80 // when used to draw a tile for LOK. It cannot be used for something else, because otherwise
81 // this would need a way to detect whether this is a tiled paint that needs LOK handling
82 // or whether it's that something else that just might happen to be called with LOK active.
83 assert(comphelper::LibreOfficeKit::isActive());
84 // Force scaling of the painting
85 double fScale
= comphelper::LibreOfficeKit::getDPIScale();
87 m_pSurface
= cairo_image_surface_create_for_data(pBuffer
, CAIRO_FORMAT_ARGB32
,
88 nNewDX
, nNewDY
, cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32
, nNewDX
));
89 dl_cairo_surface_set_device_scale(m_pSurface
, fScale
, fScale
);
91 else if(nNewDX
<= 32 && nNewDY
<= 32)
93 double fXScale
, fYScale
;
94 dl_cairo_surface_get_device_scale(m_pRefSurface
, &fXScale
, &fYScale
);
98 // Force image-based surface if small. Small VirtualDevice instances are often used for small
99 // temporary bitmaps that will eventually have GetBitmap() called on them, which would cause
100 // X Server roundtrip with Xlib-based surface, which may be way more costly than doing the drawing
101 // in software (which should be fairly cheap for small surfaces anyway).
102 m_pSurface
= cairo_surface_create_similar_image(m_pRefSurface
, CAIRO_FORMAT_ARGB32
, nNewDX
, nNewDY
);
103 dl_cairo_surface_set_device_scale(m_pSurface
, fXScale
, fYScale
);
107 m_pSurface
= cairo_surface_create_similar(m_pRefSurface
, CAIRO_CONTENT_COLOR_ALPHA
, nNewDX
, nNewDY
);
108 // Device scale is inherited in this case.
111 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
)));
112 return cairo_surface_status(m_pSurface
) == CAIRO_STATUS_SUCCESS
;
115 bool SvpSalVirtualDevice::SetSizeUsingBuffer( tools::Long nNewDX
, tools::Long nNewDY
,
116 sal_uInt8
*const pBuffer
)
118 bool bSuccess
= true;
125 if (!m_pSurface
|| m_aFrameSize
.getX() != nNewDX
||
126 m_aFrameSize
.getY() != nNewDY
)
128 m_aFrameSize
= basegfx::B2IVector(nNewDX
, nNewDY
);
131 bSuccess
= CreateSurface(nNewDX
, nNewDY
, pBuffer
);
135 // update device in existing graphics
136 for (auto const& graphic
: m_aGraphics
)
137 graphic
->setSurface(m_pSurface
, m_aFrameSize
);
143 tools::Long
SvpSalVirtualDevice::GetWidth() const
145 return m_pSurface
? m_aFrameSize
.getX() : 0;
148 tools::Long
SvpSalVirtualDevice::GetHeight() const
150 return m_pSurface
? m_aFrameSize
.getY() : 0;
155 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */