android: Update app-specific/MIME type icons
[LibreOffice.git] / vcl / headless / BitmapHelper.cxx
blob3eb29aa76ad381e47fba475ccf34fe0929f0b3ab
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::unique_ptr<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 // the alpha values need to be inverted for Cairo
88 // so big stupid copy and invert here
89 const int nImageSize = pMaskBuf->mnHeight * pMaskBuf->mnScanlineSize;
90 pAlphaBits.reset(new unsigned char[nImageSize]);
91 memcpy(pAlphaBits.get(), pMaskBuf->mpBits, nImageSize);
93 // TODO: make upper layers use standard alpha
94 sal_uInt32* pLDst = reinterpret_cast<sal_uInt32*>(pAlphaBits.get());
95 for (int i = nImageSize / sizeof(sal_uInt32); --i >= 0; ++pLDst)
96 *pLDst = ~*pLDst;
97 assert(reinterpret_cast<unsigned char*>(pLDst) == pAlphaBits.get() + nImageSize);
99 implSetSurface(cairo_image_surface_create_for_data(pAlphaBits.get(), CAIRO_FORMAT_A8,
100 pMaskBuf->mnWidth, pMaskBuf->mnHeight,
101 pMaskBuf->mnScanlineSize));
104 namespace
106 // check for env var that decides for using downscale pattern
107 const char* pDisableDownScale(getenv("SAL_DISABLE_CAIRO_DOWNSCALE"));
108 bool bDisableDownScale(nullptr != pDisableDownScale);
110 sal_Int64 estimateUsageInBytesForSurfaceHelper(const SurfaceHelper* pHelper)
112 sal_Int64 nRetval(0);
114 if (nullptr != pHelper)
116 cairo_surface_t* pSurface(pHelper->getSurface());
118 if (pSurface)
120 const tools::Long nStride(cairo_image_surface_get_stride(pSurface));
121 const tools::Long nHeight(cairo_image_surface_get_height(pSurface));
123 nRetval = nStride * nHeight;
125 // if we do downscale, size will grow by 1/4 + 1/16 + 1/32 + ...,
126 // rough estimation just multiplies by 1.25, should be good enough
127 // for estimation of buffer survival time
128 if (!bDisableDownScale)
130 nRetval = (nRetval * 5) / 4;
135 return nRetval;
138 } // end anonymous namespace
140 SystemDependentData_BitmapHelper::SystemDependentData_BitmapHelper(
141 std::shared_ptr<BitmapHelper> xBitmapHelper)
142 : basegfx::SystemDependentData(Application::GetSystemDependentDataManager())
143 , maBitmapHelper(std::move(xBitmapHelper))
147 sal_Int64 SystemDependentData_BitmapHelper::estimateUsageInBytes() const
149 return estimateUsageInBytesForSurfaceHelper(maBitmapHelper.get());
152 SystemDependentData_MaskHelper::SystemDependentData_MaskHelper(
153 std::shared_ptr<MaskHelper> xMaskHelper)
154 : basegfx::SystemDependentData(Application::GetSystemDependentDataManager())
155 , maMaskHelper(std::move(xMaskHelper))
159 sal_Int64 SystemDependentData_MaskHelper::estimateUsageInBytes() const
161 return estimateUsageInBytesForSurfaceHelper(maMaskHelper.get());
164 namespace
166 // MM02 decide to use buffers or not
167 const char* pDisableMM02Goodies(getenv("SAL_DISABLE_MM02_GOODIES"));
168 bool bUseBuffer(nullptr == pDisableMM02Goodies);
169 const tools::Long nMinimalSquareSizeToBuffer(64 * 64);
172 void tryToUseSourceBuffer(const SalBitmap& rSourceBitmap, std::shared_ptr<BitmapHelper>& rSurface)
174 // MM02 try to access buffered BitmapHelper
175 std::shared_ptr<SystemDependentData_BitmapHelper> pSystemDependentData_BitmapHelper;
176 const bool bBufferSource(bUseBuffer
177 && rSourceBitmap.GetSize().Width() * rSourceBitmap.GetSize().Height()
178 > nMinimalSquareSizeToBuffer);
180 if (bBufferSource)
182 pSystemDependentData_BitmapHelper
183 = rSourceBitmap.getSystemDependentData<SystemDependentData_BitmapHelper>();
185 if (pSystemDependentData_BitmapHelper)
187 // reuse buffered data
188 rSurface = pSystemDependentData_BitmapHelper->getBitmapHelper();
192 if (rSurface)
193 return;
195 // create data on-demand
196 rSurface = std::make_shared<BitmapHelper>(rSourceBitmap);
198 if (bBufferSource)
200 // add to buffering mechanism to potentially reuse next time
201 rSourceBitmap.addOrReplaceSystemDependentData<SystemDependentData_BitmapHelper>(rSurface);
205 void tryToUseMaskBuffer(const SalBitmap& rMaskBitmap, std::shared_ptr<MaskHelper>& rMask)
207 // MM02 try to access buffered MaskHelper
208 std::shared_ptr<SystemDependentData_MaskHelper> pSystemDependentData_MaskHelper;
209 const bool bBufferMask(bUseBuffer
210 && rMaskBitmap.GetSize().Width() * rMaskBitmap.GetSize().Height()
211 > nMinimalSquareSizeToBuffer);
213 if (bBufferMask)
215 pSystemDependentData_MaskHelper
216 = rMaskBitmap.getSystemDependentData<SystemDependentData_MaskHelper>();
218 if (pSystemDependentData_MaskHelper)
220 // reuse buffered data
221 rMask = pSystemDependentData_MaskHelper->getMaskHelper();
225 if (rMask)
226 return;
228 // create data on-demand
229 rMask = std::make_shared<MaskHelper>(rMaskBitmap);
231 if (bBufferMask)
233 // add to buffering mechanism to potentially reuse next time
234 rMaskBitmap.addOrReplaceSystemDependentData<SystemDependentData_MaskHelper>(rMask);
238 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */