/src/libreoffice/vcl/headless/BitmapHelper.cxx
Line | Count | Source (jump to first uncovered line) |
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 | | #include <headless/BitmapHelper.hxx> |
21 | | #include <vcl/cairo.hxx> |
22 | | #include <vcl/CairoFormats.hxx> |
23 | | #include <vcl/svapp.hxx> |
24 | | #include <utility> |
25 | | |
26 | | BitmapHelper::BitmapHelper(const SalBitmap& rSourceBitmap, const bool bForceARGB32) |
27 | | #ifdef HAVE_CAIRO_FORMAT_RGB24_888 |
28 | 13.0k | : m_bForceARGB32(bForceARGB32) |
29 | | #endif |
30 | 13.0k | { |
31 | 13.0k | const SvpSalBitmap& rSrcBmp = static_cast<const SvpSalBitmap&>(rSourceBitmap); |
32 | 13.0k | #ifdef HAVE_CAIRO_FORMAT_RGB24_888 |
33 | 13.0k | if ((rSrcBmp.GetBitCount() != 32 && rSrcBmp.GetBitCount() != 24) || bForceARGB32) |
34 | | #else |
35 | | (void)bForceARGB32; |
36 | | if (rSrcBmp.GetBitCount() != 32) |
37 | | #endif |
38 | 10.9k | { |
39 | | //big stupid copy here |
40 | 10.9k | const BitmapBuffer* pSrc = rSrcBmp.GetBuffer(); |
41 | 10.9k | const SalTwoRect aTwoRect |
42 | 10.9k | = { 0, 0, pSrc->mnWidth, pSrc->mnHeight, 0, 0, pSrc->mnWidth, pSrc->mnHeight }; |
43 | 10.9k | std::optional<BitmapBuffer> pTmp |
44 | 10.9k | = (pSrc->meFormat == SVP_24BIT_FORMAT |
45 | 10.9k | && pSrc->meDirection == ScanlineDirection::TopDown) |
46 | 10.9k | ? FastConvert24BitRgbTo32BitCairo(pSrc) |
47 | 10.9k | : StretchAndConvert(*pSrc, aTwoRect, SVP_CAIRO_FORMAT); |
48 | 10.9k | aTmpBmp.Create(std::move(pTmp)); |
49 | | |
50 | 10.9k | assert(aTmpBmp.GetBitCount() == 32); |
51 | 10.9k | implSetSurface(CairoCommon::createCairoSurface(aTmpBmp.GetBuffer())); |
52 | 10.9k | } |
53 | 2.14k | else |
54 | 2.14k | { |
55 | 2.14k | implSetSurface(CairoCommon::createCairoSurface(rSrcBmp.GetBuffer())); |
56 | 2.14k | } |
57 | 13.0k | } |
58 | | |
59 | 2.29k | void BitmapHelper::mark_dirty() { cairo_surface_mark_dirty(implGetSurface()); } |
60 | | |
61 | | unsigned char* BitmapHelper::getBits(sal_Int32& rStride) |
62 | 2.29k | { |
63 | 2.29k | cairo_surface_flush(implGetSurface()); |
64 | | |
65 | 2.29k | unsigned char* mask_data = cairo_image_surface_get_data(implGetSurface()); |
66 | | |
67 | 2.29k | const cairo_format_t nFormat = cairo_image_surface_get_format(implGetSurface()); |
68 | 2.29k | #ifdef HAVE_CAIRO_FORMAT_RGB24_888 |
69 | 2.29k | if (!m_bForceARGB32) |
70 | 0 | assert(nFormat == CAIRO_FORMAT_RGB24_888 && "Expected RGB24_888 image"); |
71 | 2.29k | else |
72 | 2.29k | #endif |
73 | 2.29k | { |
74 | 2.29k | assert(nFormat == CAIRO_FORMAT_ARGB32 |
75 | 2.29k | && "need to implement CAIRO_FORMAT_A1 after all here"); |
76 | 2.29k | } |
77 | | |
78 | 2.29k | rStride |
79 | 2.29k | = cairo_format_stride_for_width(nFormat, cairo_image_surface_get_width(implGetSurface())); |
80 | | |
81 | 2.29k | return mask_data; |
82 | 2.29k | } |
83 | | |
84 | | MaskHelper::MaskHelper(const SalBitmap& rAlphaBitmap) |
85 | 4 | { |
86 | 4 | const SvpSalBitmap& rMask = static_cast<const SvpSalBitmap&>(rAlphaBitmap); |
87 | 4 | const BitmapBuffer* pMaskBuf = rMask.GetBuffer(); |
88 | 4 | assert(rAlphaBitmap.GetBitCount() == 8 && "we only support 8-bit masks now"); |
89 | | |
90 | 4 | implSetSurface(cairo_image_surface_create_for_data(pMaskBuf->mpBits, CAIRO_FORMAT_A8, |
91 | 4 | pMaskBuf->mnWidth, pMaskBuf->mnHeight, |
92 | 4 | pMaskBuf->mnScanlineSize)); |
93 | 4 | } |
94 | | |
95 | | namespace |
96 | | { |
97 | | // check for env var that decides for using downscale pattern |
98 | | const char* pDisableDownScale(getenv("SAL_DISABLE_CAIRO_DOWNSCALE")); |
99 | | bool bDisableDownScale(nullptr != pDisableDownScale); |
100 | | |
101 | | sal_Int64 estimateUsageInBytesForSurfaceHelper(const SurfaceHelper* pHelper) |
102 | 0 | { |
103 | 0 | sal_Int64 nRetval(0); |
104 | |
|
105 | 0 | if (nullptr != pHelper) |
106 | 0 | { |
107 | 0 | cairo_surface_t* pSurface(pHelper->getSurface()); |
108 | |
|
109 | 0 | if (pSurface) |
110 | 0 | { |
111 | 0 | const tools::Long nStride(cairo_image_surface_get_stride(pSurface)); |
112 | 0 | const tools::Long nHeight(cairo_image_surface_get_height(pSurface)); |
113 | |
|
114 | 0 | nRetval = nStride * nHeight; |
115 | | |
116 | | // if we do downscale, size will grow by 1/4 + 1/16 + 1/32 + ..., |
117 | | // rough estimation just multiplies by 1.25, should be good enough |
118 | | // for estimation of buffer survival time |
119 | 0 | if (!bDisableDownScale) |
120 | 0 | { |
121 | 0 | nRetval = (nRetval * 5) / 4; |
122 | 0 | } |
123 | 0 | } |
124 | 0 | } |
125 | |
|
126 | 0 | return nRetval; |
127 | 0 | } |
128 | | |
129 | | } // end anonymous namespace |
130 | | |
131 | | SystemDependentData_BitmapHelper::SystemDependentData_BitmapHelper( |
132 | | std::shared_ptr<BitmapHelper> xBitmapHelper) |
133 | 674 | : basegfx::SystemDependentData(Application::GetSystemDependentDataManager(), |
134 | 674 | basegfx::SDD_Type::SDDType_BitmapHelper) |
135 | 674 | , maBitmapHelper(std::move(xBitmapHelper)) |
136 | 674 | { |
137 | 674 | } |
138 | | |
139 | | sal_Int64 SystemDependentData_BitmapHelper::estimateUsageInBytes() const |
140 | 0 | { |
141 | 0 | return estimateUsageInBytesForSurfaceHelper(maBitmapHelper.get()); |
142 | 0 | } |
143 | | |
144 | | SystemDependentData_MaskHelper::SystemDependentData_MaskHelper( |
145 | | std::shared_ptr<MaskHelper> xMaskHelper) |
146 | 0 | : basegfx::SystemDependentData(Application::GetSystemDependentDataManager(), |
147 | 0 | basegfx::SDD_Type::SDDType_MaskHelper) |
148 | 0 | , maMaskHelper(std::move(xMaskHelper)) |
149 | 0 | { |
150 | 0 | } |
151 | | |
152 | | sal_Int64 SystemDependentData_MaskHelper::estimateUsageInBytes() const |
153 | 0 | { |
154 | 0 | return estimateUsageInBytesForSurfaceHelper(maMaskHelper.get()); |
155 | 0 | } |
156 | | |
157 | | namespace |
158 | | { |
159 | | // MM02 decide to use buffers or not |
160 | | const char* pDisableMM02Goodies(getenv("SAL_DISABLE_MM02_GOODIES")); |
161 | | bool bUseBuffer(nullptr == pDisableMM02Goodies); |
162 | | const tools::Long nMinimalSquareSizeToBuffer(64 * 64); |
163 | | } |
164 | | |
165 | | void tryToUseSourceBuffer(const SalBitmap& rSourceBitmap, std::shared_ptr<BitmapHelper>& rSurface) |
166 | 10.8k | { |
167 | | // MM02 try to access buffered BitmapHelper |
168 | 10.8k | std::shared_ptr<SystemDependentData_BitmapHelper> pSystemDependentData_BitmapHelper; |
169 | 10.8k | const bool bBufferSource(bUseBuffer |
170 | 10.8k | && rSourceBitmap.GetSize().Width() * rSourceBitmap.GetSize().Height() |
171 | 10.8k | > nMinimalSquareSizeToBuffer); |
172 | | |
173 | 10.8k | if (bBufferSource) |
174 | 674 | { |
175 | 674 | pSystemDependentData_BitmapHelper |
176 | 674 | = rSourceBitmap.getSystemDependentData<SystemDependentData_BitmapHelper>( |
177 | 674 | basegfx::SDD_Type::SDDType_BitmapHelper); |
178 | | |
179 | 674 | if (pSystemDependentData_BitmapHelper) |
180 | 0 | { |
181 | | // reuse buffered data |
182 | 0 | rSurface = pSystemDependentData_BitmapHelper->getBitmapHelper(); |
183 | 0 | } |
184 | 674 | } |
185 | | |
186 | 10.8k | if (rSurface) |
187 | 0 | return; |
188 | | |
189 | | // create data on-demand |
190 | 10.8k | rSurface = std::make_shared<BitmapHelper>(rSourceBitmap); |
191 | | |
192 | 10.8k | if (bBufferSource) |
193 | 674 | { |
194 | | // add to buffering mechanism to potentially reuse next time |
195 | 674 | rSourceBitmap.addOrReplaceSystemDependentData<SystemDependentData_BitmapHelper>(rSurface); |
196 | 674 | } |
197 | 10.8k | } |
198 | | |
199 | | void tryToUseMaskBuffer(const SalBitmap& rMaskBitmap, std::shared_ptr<MaskHelper>& rMask) |
200 | 4 | { |
201 | | // MM02 try to access buffered MaskHelper |
202 | 4 | std::shared_ptr<SystemDependentData_MaskHelper> pSystemDependentData_MaskHelper; |
203 | 4 | const bool bBufferMask(bUseBuffer |
204 | 4 | && rMaskBitmap.GetSize().Width() * rMaskBitmap.GetSize().Height() |
205 | 4 | > nMinimalSquareSizeToBuffer); |
206 | | |
207 | 4 | if (bBufferMask) |
208 | 0 | { |
209 | 0 | pSystemDependentData_MaskHelper |
210 | 0 | = rMaskBitmap.getSystemDependentData<SystemDependentData_MaskHelper>( |
211 | 0 | basegfx::SDD_Type::SDDType_MaskHelper); |
212 | |
|
213 | 0 | if (pSystemDependentData_MaskHelper) |
214 | 0 | { |
215 | | // reuse buffered data |
216 | 0 | rMask = pSystemDependentData_MaskHelper->getMaskHelper(); |
217 | 0 | } |
218 | 0 | } |
219 | | |
220 | 4 | if (rMask) |
221 | 0 | return; |
222 | | |
223 | | // create data on-demand |
224 | 4 | rMask = std::make_shared<MaskHelper>(rMaskBitmap); |
225 | | |
226 | 4 | if (bBufferMask) |
227 | 0 | { |
228 | | // add to buffering mechanism to potentially reuse next time |
229 | 0 | rMaskBitmap.addOrReplaceSystemDependentData<SystemDependentData_MaskHelper>(rMask); |
230 | 0 | } |
231 | 4 | } |
232 | | |
233 | | /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |