tdf#130857 qt weld: Implement QtInstanceWidget::get_text_height
[LibreOffice.git] / vcl / headless / BitmapHelper.cxx
blobcfa9f1556eeb4ae61b2be346efe3b2c6567eaa6a
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 <headless/BitmapHelper.hxx>
21 #include <vcl/cairo.hxx>
22 #include <vcl/svapp.hxx>
23 #include <utility>
25 BitmapHelper::BitmapHelper(const SalBitmap& rSourceBitmap, const bool bForceARGB32)
26 #ifdef HAVE_CAIRO_FORMAT_RGB24_888
27 : m_bForceARGB32(bForceARGB32)
28 #endif
30 const SvpSalBitmap& rSrcBmp = static_cast<const SvpSalBitmap&>(rSourceBitmap);
31 #ifdef HAVE_CAIRO_FORMAT_RGB24_888
32 if ((rSrcBmp.GetBitCount() != 32 && rSrcBmp.GetBitCount() != 24) || bForceARGB32)
33 #else
34 (void)bForceARGB32;
35 if (rSrcBmp.GetBitCount() != 32)
36 #endif
38 //big stupid copy here
39 const BitmapBuffer* pSrc = rSrcBmp.GetBuffer();
40 const SalTwoRect aTwoRect
41 = { 0, 0, pSrc->mnWidth, pSrc->mnHeight, 0, 0, pSrc->mnWidth, pSrc->mnHeight };
42 std::optional<BitmapBuffer> pTmp
43 = (pSrc->meFormat == SVP_24BIT_FORMAT
44 && pSrc->meDirection == ScanlineDirection::TopDown)
45 ? FastConvert24BitRgbTo32BitCairo(pSrc)
46 : StretchAndConvert(*pSrc, aTwoRect, SVP_CAIRO_FORMAT);
47 aTmpBmp.Create(std::move(pTmp));
49 assert(aTmpBmp.GetBitCount() == 32);
50 implSetSurface(CairoCommon::createCairoSurface(aTmpBmp.GetBuffer()));
52 else
54 implSetSurface(CairoCommon::createCairoSurface(rSrcBmp.GetBuffer()));
58 void BitmapHelper::mark_dirty() { cairo_surface_mark_dirty(implGetSurface()); }
60 unsigned char* BitmapHelper::getBits(sal_Int32& rStride)
62 cairo_surface_flush(implGetSurface());
64 unsigned char* mask_data = cairo_image_surface_get_data(implGetSurface());
66 const cairo_format_t nFormat = cairo_image_surface_get_format(implGetSurface());
67 #ifdef HAVE_CAIRO_FORMAT_RGB24_888
68 if (!m_bForceARGB32)
69 assert(nFormat == CAIRO_FORMAT_RGB24_888 && "Expected RGB24_888 image");
70 else
71 #endif
73 assert(nFormat == CAIRO_FORMAT_ARGB32
74 && "need to implement CAIRO_FORMAT_A1 after all here");
77 rStride
78 = cairo_format_stride_for_width(nFormat, cairo_image_surface_get_width(implGetSurface()));
80 return mask_data;
83 MaskHelper::MaskHelper(const SalBitmap& rAlphaBitmap)
85 const SvpSalBitmap& rMask = static_cast<const SvpSalBitmap&>(rAlphaBitmap);
86 const BitmapBuffer* pMaskBuf = rMask.GetBuffer();
87 assert(rAlphaBitmap.GetBitCount() == 8 && "we only support 8-bit masks now");
89 implSetSurface(cairo_image_surface_create_for_data(pMaskBuf->mpBits, CAIRO_FORMAT_A8,
90 pMaskBuf->mnWidth, pMaskBuf->mnHeight,
91 pMaskBuf->mnScanlineSize));
94 namespace
96 // check for env var that decides for using downscale pattern
97 const char* pDisableDownScale(getenv("SAL_DISABLE_CAIRO_DOWNSCALE"));
98 bool bDisableDownScale(nullptr != pDisableDownScale);
100 sal_Int64 estimateUsageInBytesForSurfaceHelper(const SurfaceHelper* pHelper)
102 sal_Int64 nRetval(0);
104 if (nullptr != pHelper)
106 cairo_surface_t* pSurface(pHelper->getSurface());
108 if (pSurface)
110 const tools::Long nStride(cairo_image_surface_get_stride(pSurface));
111 const tools::Long nHeight(cairo_image_surface_get_height(pSurface));
113 nRetval = nStride * nHeight;
115 // if we do downscale, size will grow by 1/4 + 1/16 + 1/32 + ...,
116 // rough estimation just multiplies by 1.25, should be good enough
117 // for estimation of buffer survival time
118 if (!bDisableDownScale)
120 nRetval = (nRetval * 5) / 4;
125 return nRetval;
128 } // end anonymous namespace
130 SystemDependentData_BitmapHelper::SystemDependentData_BitmapHelper(
131 std::shared_ptr<BitmapHelper> xBitmapHelper)
132 : basegfx::SystemDependentData(Application::GetSystemDependentDataManager(),
133 basegfx::SDD_Type::SDDType_BitmapHelper)
134 , maBitmapHelper(std::move(xBitmapHelper))
138 sal_Int64 SystemDependentData_BitmapHelper::estimateUsageInBytes() const
140 return estimateUsageInBytesForSurfaceHelper(maBitmapHelper.get());
143 SystemDependentData_MaskHelper::SystemDependentData_MaskHelper(
144 std::shared_ptr<MaskHelper> xMaskHelper)
145 : basegfx::SystemDependentData(Application::GetSystemDependentDataManager(),
146 basegfx::SDD_Type::SDDType_MaskHelper)
147 , maMaskHelper(std::move(xMaskHelper))
151 sal_Int64 SystemDependentData_MaskHelper::estimateUsageInBytes() const
153 return estimateUsageInBytesForSurfaceHelper(maMaskHelper.get());
156 namespace
158 // MM02 decide to use buffers or not
159 const char* pDisableMM02Goodies(getenv("SAL_DISABLE_MM02_GOODIES"));
160 bool bUseBuffer(nullptr == pDisableMM02Goodies);
161 const tools::Long nMinimalSquareSizeToBuffer(64 * 64);
164 void tryToUseSourceBuffer(const SalBitmap& rSourceBitmap, std::shared_ptr<BitmapHelper>& rSurface)
166 // MM02 try to access buffered BitmapHelper
167 std::shared_ptr<SystemDependentData_BitmapHelper> pSystemDependentData_BitmapHelper;
168 const bool bBufferSource(bUseBuffer
169 && rSourceBitmap.GetSize().Width() * rSourceBitmap.GetSize().Height()
170 > nMinimalSquareSizeToBuffer);
172 if (bBufferSource)
174 pSystemDependentData_BitmapHelper
175 = rSourceBitmap.getSystemDependentData<SystemDependentData_BitmapHelper>(
176 basegfx::SDD_Type::SDDType_BitmapHelper);
178 if (pSystemDependentData_BitmapHelper)
180 // reuse buffered data
181 rSurface = pSystemDependentData_BitmapHelper->getBitmapHelper();
185 if (rSurface)
186 return;
188 // create data on-demand
189 rSurface = std::make_shared<BitmapHelper>(rSourceBitmap);
191 if (bBufferSource)
193 // add to buffering mechanism to potentially reuse next time
194 rSourceBitmap.addOrReplaceSystemDependentData<SystemDependentData_BitmapHelper>(rSurface);
198 void tryToUseMaskBuffer(const SalBitmap& rMaskBitmap, std::shared_ptr<MaskHelper>& rMask)
200 // MM02 try to access buffered MaskHelper
201 std::shared_ptr<SystemDependentData_MaskHelper> pSystemDependentData_MaskHelper;
202 const bool bBufferMask(bUseBuffer
203 && rMaskBitmap.GetSize().Width() * rMaskBitmap.GetSize().Height()
204 > nMinimalSquareSizeToBuffer);
206 if (bBufferMask)
208 pSystemDependentData_MaskHelper
209 = rMaskBitmap.getSystemDependentData<SystemDependentData_MaskHelper>(
210 basegfx::SDD_Type::SDDType_MaskHelper);
212 if (pSystemDependentData_MaskHelper)
214 // reuse buffered data
215 rMask = pSystemDependentData_MaskHelper->getMaskHelper();
219 if (rMask)
220 return;
222 // create data on-demand
223 rMask = std::make_shared<MaskHelper>(rMaskBitmap);
225 if (bBufferMask)
227 // add to buffering mechanism to potentially reuse next time
228 rMaskBitmap.addOrReplaceSystemDependentData<SystemDependentData_MaskHelper>(rMask);
232 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */