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 #ifndef IOS
21 
22 #include <headless/svpbmp.hxx>
23 #include <headless/svpinst.hxx>
24 #include <headless/svpvd.hxx>
25 #include <headless/svpgdi.hxx>
26 
27 #include <basegfx/vector/b2ivector.hxx>
28 #include <comphelper/lok.hxx>
29 
30 #include <cairo.h>
31 
32 using namespace basegfx;
33 
SvpSalVirtualDevice(cairo_surface_t * pRefSurface,cairo_surface_t * pPreExistingTarget)34 SvpSalVirtualDevice::SvpSalVirtualDevice(cairo_surface_t* pRefSurface, cairo_surface_t* pPreExistingTarget)
35     : m_pRefSurface(pRefSurface)
36     , m_pSurface(pPreExistingTarget)
37     , m_bOwnsSurface(!pPreExistingTarget)
38 {
39     cairo_surface_reference(m_pRefSurface);
40 }
41 
~SvpSalVirtualDevice()42 SvpSalVirtualDevice::~SvpSalVirtualDevice()
43 {
44     if (m_bOwnsSurface)
45         cairo_surface_destroy(m_pSurface);
46     cairo_surface_destroy(m_pRefSurface);
47 }
48 
AddGraphics(SvpSalGraphics * pGraphics)49 SvpSalGraphics* SvpSalVirtualDevice::AddGraphics(SvpSalGraphics* pGraphics)
50 {
51     pGraphics->setSurface(m_pSurface, m_aFrameSize);
52     m_aGraphics.push_back(pGraphics);
53     return pGraphics;
54 }
55 
AcquireGraphics()56 SalGraphics* SvpSalVirtualDevice::AcquireGraphics()
57 {
58     return AddGraphics(new SvpSalGraphics());
59 }
60 
ReleaseGraphics(SalGraphics * pGraphics)61 void SvpSalVirtualDevice::ReleaseGraphics( SalGraphics* pGraphics )
62 {
63     m_aGraphics.erase(std::remove(m_aGraphics.begin(), m_aGraphics.end(), dynamic_cast<SvpSalGraphics*>(pGraphics)), m_aGraphics.end());
64     delete pGraphics;
65 }
66 
SetSize(tools::Long nNewDX,tools::Long nNewDY)67 bool SvpSalVirtualDevice::SetSize( tools::Long nNewDX, tools::Long nNewDY )
68 {
69     return SetSizeUsingBuffer(nNewDX, nNewDY, nullptr);
70 }
71 
CreateSurface(tools::Long nNewDX,tools::Long nNewDY,sal_uInt8 * const pBuffer)72 void SvpSalVirtualDevice::CreateSurface(tools::Long nNewDX, tools::Long nNewDY, sal_uInt8 *const pBuffer)
73 {
74     if (m_pSurface)
75     {
76         cairo_surface_destroy(m_pSurface);
77     }
78 
79     if (pBuffer)
80     {
81         double fXScale, fYScale;
82         if (comphelper::LibreOfficeKit::isActive())
83         {
84             // Force scaling of the painting
85             fXScale = fYScale = comphelper::LibreOfficeKit::getDPIScale();
86         }
87         else
88         {
89             dl_cairo_surface_get_device_scale(m_pRefSurface, &fXScale, &fYScale);
90             nNewDX *= fXScale;
91             nNewDY *= fYScale;
92         }
93 
94         m_pSurface = cairo_image_surface_create_for_data(pBuffer, CAIRO_FORMAT_ARGB32,
95                             nNewDX, nNewDY, cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, nNewDX));
96 
97         dl_cairo_surface_set_device_scale(m_pSurface, fXScale, fYScale);
98     }
99     else
100     {
101         m_pSurface = cairo_surface_create_similar(m_pRefSurface, CAIRO_CONTENT_COLOR_ALPHA, nNewDX, nNewDY);
102     }
103 }
104 
SetSizeUsingBuffer(tools::Long nNewDX,tools::Long nNewDY,sal_uInt8 * const pBuffer)105 bool SvpSalVirtualDevice::SetSizeUsingBuffer( tools::Long nNewDX, tools::Long nNewDY,
106         sal_uInt8 *const pBuffer)
107 {
108     if (nNewDX == 0)
109         nNewDX = 1;
110     if (nNewDY == 0)
111         nNewDY = 1;
112 
113     if (!m_pSurface || m_aFrameSize.getX() != nNewDX ||
114                        m_aFrameSize.getY() != nNewDY)
115     {
116         m_aFrameSize = basegfx::B2IVector(nNewDX, nNewDY);
117 
118         if (m_bOwnsSurface)
119             CreateSurface(nNewDX, nNewDY, pBuffer);
120 
121         assert(m_pSurface);
122 
123         // update device in existing graphics
124         for (auto const& graphic : m_aGraphics)
125             graphic->setSurface(m_pSurface, m_aFrameSize);
126     }
127     return true;
128 }
129 
GetWidth() const130 tools::Long SvpSalVirtualDevice::GetWidth() const
131 {
132     return m_pSurface ? m_aFrameSize.getX() : 0;
133 }
134 
GetHeight() const135 tools::Long SvpSalVirtualDevice::GetHeight() const
136 {
137     return m_pSurface ? m_aFrameSize.getY() : 0;
138 }
139 
140 #endif
141 
142 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
143