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