Avoid potential negative array index access to cached text.
[LibreOffice.git] / vcl / headless / BitmapHelper.cxx
blob2cdf502fc9f2dbbfa9fe87093e6fcf242b3412ca
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 <svdata.hxx>
22 #include <utility>
24 BitmapHelper::BitmapHelper(const SalBitmap& rSourceBitmap, const bool bForceARGB32)
25 #ifdef HAVE_CAIRO_FORMAT_RGB24_888
26 : m_bForceARGB32(bForceARGB32)
27 #endif
29 const SvpSalBitmap& rSrcBmp = static_cast<const SvpSalBitmap&>(rSourceBitmap);
30 #ifdef HAVE_CAIRO_FORMAT_RGB24_888
31 if ((rSrcBmp.GetBitCount() != 32 && rSrcBmp.GetBitCount() != 24) || bForceARGB32)
32 #else
33 (void)bForceARGB32;
34 if (rSrcBmp.GetBitCount() != 32)
35 #endif
37 //big stupid copy here
38 const BitmapBuffer* pSrc = rSrcBmp.GetBuffer();
39 const SalTwoRect aTwoRect
40 = { 0, 0, pSrc->mnWidth, pSrc->mnHeight, 0, 0, pSrc->mnWidth, pSrc->mnHeight };
41 std::optional<BitmapBuffer> pTmp
42 = (pSrc->mnFormat == SVP_24BIT_FORMAT
43 ? FastConvert24BitRgbTo32BitCairo(pSrc)
44 : StretchAndConvert(*pSrc, aTwoRect, SVP_CAIRO_FORMAT));
45 aTmpBmp.Create(std::move(pTmp));
47 assert(aTmpBmp.GetBitCount() == 32);
48 implSetSurface(CairoCommon::createCairoSurface(aTmpBmp.GetBuffer()));
50 else
52 implSetSurface(CairoCommon::createCairoSurface(rSrcBmp.GetBuffer()));
56 void BitmapHelper::mark_dirty() { cairo_surface_mark_dirty(implGetSurface()); }
58 unsigned char* BitmapHelper::getBits(sal_Int32& rStride)
60 cairo_surface_flush(implGetSurface());
62 unsigned char* mask_data = cairo_image_surface_get_data(implGetSurface());
64 const cairo_format_t nFormat = cairo_image_surface_get_format(implGetSurface());
65 #ifdef HAVE_CAIRO_FORMAT_RGB24_888
66 if (!m_bForceARGB32)
67 assert(nFormat == CAIRO_FORMAT_RGB24_888 && "Expected RGB24_888 image");
68 else
69 #endif
71 assert(nFormat == CAIRO_FORMAT_ARGB32
72 && "need to implement CAIRO_FORMAT_A1 after all here");
75 rStride
76 = cairo_format_stride_for_width(nFormat, cairo_image_surface_get_width(implGetSurface()));
78 return mask_data;
81 MaskHelper::MaskHelper(const SalBitmap& rAlphaBitmap)
83 const SvpSalBitmap& rMask = static_cast<const SvpSalBitmap&>(rAlphaBitmap);
84 const BitmapBuffer* pMaskBuf = rMask.GetBuffer();
85 assert(rAlphaBitmap.GetBitCount() == 8 && "we only support 8-bit masks now");
87 implSetSurface(cairo_image_surface_create_for_data(pMaskBuf->mpBits, CAIRO_FORMAT_A8,
88 pMaskBuf->mnWidth, pMaskBuf->mnHeight,
89 pMaskBuf->mnScanlineSize));
92 namespace
94 // check for env var that decides for using downscale pattern
95 const char* pDisableDownScale(getenv("SAL_DISABLE_CAIRO_DOWNSCALE"));
96 bool bDisableDownScale(nullptr != pDisableDownScale);
98 sal_Int64 estimateUsageInBytesForSurfaceHelper(const SurfaceHelper* pHelper)
100 sal_Int64 nRetval(0);
102 if (nullptr != pHelper)
104 cairo_surface_t* pSurface(pHelper->getSurface());
106 if (pSurface)
108 const tools::Long nStride(cairo_image_surface_get_stride(pSurface));
109 const tools::Long nHeight(cairo_image_surface_get_height(pSurface));
111 nRetval = nStride * nHeight;
113 // if we do downscale, size will grow by 1/4 + 1/16 + 1/32 + ...,
114 // rough estimation just multiplies by 1.25, should be good enough
115 // for estimation of buffer survival time
116 if (!bDisableDownScale)
118 nRetval = (nRetval * 5) / 4;
123 return nRetval;
126 } // end anonymous namespace
128 SystemDependentData_BitmapHelper::SystemDependentData_BitmapHelper(
129 std::shared_ptr<BitmapHelper> xBitmapHelper)
130 : basegfx::SystemDependentData(Application::GetSystemDependentDataManager())
131 , maBitmapHelper(std::move(xBitmapHelper))
135 sal_Int64 SystemDependentData_BitmapHelper::estimateUsageInBytes() const
137 return estimateUsageInBytesForSurfaceHelper(maBitmapHelper.get());
140 SystemDependentData_MaskHelper::SystemDependentData_MaskHelper(
141 std::shared_ptr<MaskHelper> xMaskHelper)
142 : basegfx::SystemDependentData(Application::GetSystemDependentDataManager())
143 , maMaskHelper(std::move(xMaskHelper))
147 sal_Int64 SystemDependentData_MaskHelper::estimateUsageInBytes() const
149 return estimateUsageInBytesForSurfaceHelper(maMaskHelper.get());
152 namespace
154 // MM02 decide to use buffers or not
155 const char* pDisableMM02Goodies(getenv("SAL_DISABLE_MM02_GOODIES"));
156 bool bUseBuffer(nullptr == pDisableMM02Goodies);
157 const tools::Long nMinimalSquareSizeToBuffer(64 * 64);
160 void tryToUseSourceBuffer(const SalBitmap& rSourceBitmap, std::shared_ptr<BitmapHelper>& rSurface)
162 // MM02 try to access buffered BitmapHelper
163 std::shared_ptr<SystemDependentData_BitmapHelper> pSystemDependentData_BitmapHelper;
164 const bool bBufferSource(bUseBuffer
165 && rSourceBitmap.GetSize().Width() * rSourceBitmap.GetSize().Height()
166 > nMinimalSquareSizeToBuffer);
168 if (bBufferSource)
170 pSystemDependentData_BitmapHelper
171 = rSourceBitmap.getSystemDependentData<SystemDependentData_BitmapHelper>();
173 if (pSystemDependentData_BitmapHelper)
175 // reuse buffered data
176 rSurface = pSystemDependentData_BitmapHelper->getBitmapHelper();
180 if (rSurface)
181 return;
183 // create data on-demand
184 rSurface = std::make_shared<BitmapHelper>(rSourceBitmap);
186 if (bBufferSource)
188 // add to buffering mechanism to potentially reuse next time
189 rSourceBitmap.addOrReplaceSystemDependentData<SystemDependentData_BitmapHelper>(rSurface);
193 void tryToUseMaskBuffer(const SalBitmap& rMaskBitmap, std::shared_ptr<MaskHelper>& rMask)
195 // MM02 try to access buffered MaskHelper
196 std::shared_ptr<SystemDependentData_MaskHelper> pSystemDependentData_MaskHelper;
197 const bool bBufferMask(bUseBuffer
198 && rMaskBitmap.GetSize().Width() * rMaskBitmap.GetSize().Height()
199 > nMinimalSquareSizeToBuffer);
201 if (bBufferMask)
203 pSystemDependentData_MaskHelper
204 = rMaskBitmap.getSystemDependentData<SystemDependentData_MaskHelper>();
206 if (pSystemDependentData_MaskHelper)
208 // reuse buffered data
209 rMask = pSystemDependentData_MaskHelper->getMaskHelper();
213 if (rMask)
214 return;
216 // create data on-demand
217 rMask = std::make_shared<MaskHelper>(rMaskBitmap);
219 if (bBufferMask)
221 // add to buffering mechanism to potentially reuse next time
222 rMaskBitmap.addOrReplaceSystemDependentData<SystemDependentData_MaskHelper>(rMask);
226 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */