/src/libreoffice/vcl/headless/BitmapHelper.cxx
Line | Count | Source |
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 | 11.3k | : m_bForceARGB32(bForceARGB32) |
29 | | #endif |
30 | 11.3k | { |
31 | 11.3k | const SvpSalBitmap& rSrcBmp = static_cast<const SvpSalBitmap&>(rSourceBitmap); |
32 | 11.3k | #ifdef HAVE_CAIRO_FORMAT_RGB24_888 |
33 | 11.3k | if ((rSrcBmp.GetBitCount() != 32 && rSrcBmp.GetBitCount() != 24) || bForceARGB32) |
34 | | #else |
35 | | (void)bForceARGB32; |
36 | | if (rSrcBmp.GetBitCount() != 32) |
37 | | #endif |
38 | 9.58k | { |
39 | | //big stupid copy here |
40 | 9.58k | const BitmapBuffer* pSrc = rSrcBmp.GetBuffer(); |
41 | 9.58k | const SalTwoRect aTwoRect |
42 | 9.58k | = { 0, 0, pSrc->mnWidth, pSrc->mnHeight, 0, 0, pSrc->mnWidth, pSrc->mnHeight }; |
43 | 9.58k | std::optional<BitmapBuffer> pTmp |
44 | 9.58k | = (pSrc->meFormat == SVP_24BIT_FORMAT |
45 | 853 | && pSrc->meDirection == ScanlineDirection::TopDown) |
46 | 9.58k | ? FastConvert24BitRgbTo32BitCairo(pSrc) |
47 | 9.58k | : StretchAndConvert(*pSrc, aTwoRect, SVP_CAIRO_FORMAT); |
48 | 9.58k | aTmpBmp.Create(std::move(pTmp)); |
49 | | |
50 | 9.58k | assert(aTmpBmp.GetBitCount() == 32); |
51 | 9.58k | implSetSurface(CairoCommon::createCairoSurface(aTmpBmp.GetBuffer())); |
52 | 9.58k | } |
53 | 1.71k | else |
54 | 1.71k | { |
55 | 1.71k | implSetSurface(CairoCommon::createCairoSurface(rSrcBmp.GetBuffer())); |
56 | 1.71k | } |
57 | 11.3k | } |
58 | | |
59 | 1.50k | void BitmapHelper::mark_dirty() { cairo_surface_mark_dirty(implGetSurface()); } |
60 | | |
61 | | unsigned char* BitmapHelper::getBits(sal_Int32& rStride) |
62 | 1.50k | { |
63 | 1.50k | cairo_surface_flush(implGetSurface()); |
64 | | |
65 | 1.50k | unsigned char* mask_data = cairo_image_surface_get_data(implGetSurface()); |
66 | | |
67 | 1.50k | const cairo_format_t nFormat = cairo_image_surface_get_format(implGetSurface()); |
68 | 1.50k | #ifdef HAVE_CAIRO_FORMAT_RGB24_888 |
69 | 1.50k | if (!m_bForceARGB32) |
70 | 1.50k | assert(nFormat == CAIRO_FORMAT_RGB24_888 && "Expected RGB24_888 image"); |
71 | 1.50k | else |
72 | 1.50k | #endif |
73 | 1.50k | { |
74 | 1.50k | assert(nFormat == CAIRO_FORMAT_ARGB32 |
75 | 1.50k | && "need to implement CAIRO_FORMAT_A1 after all here"); |
76 | 1.50k | } |
77 | | |
78 | 1.50k | rStride |
79 | 1.50k | = cairo_format_stride_for_width(nFormat, cairo_image_surface_get_width(implGetSurface())); |
80 | | |
81 | 1.50k | return mask_data; |
82 | 1.50k | } |
83 | | |
84 | | namespace |
85 | | { |
86 | | // check for env var that decides for using downscale pattern |
87 | | const char* pDisableDownScale(getenv("SAL_DISABLE_CAIRO_DOWNSCALE")); |
88 | | bool bDisableDownScale(nullptr != pDisableDownScale); |
89 | | |
90 | | sal_Int64 estimateUsageInBytesForSurfaceHelper(const SurfaceHelper* pHelper) |
91 | 0 | { |
92 | 0 | sal_Int64 nRetval(0); |
93 | |
|
94 | 0 | if (nullptr != pHelper) |
95 | 0 | { |
96 | 0 | cairo_surface_t* pSurface(pHelper->getSurface()); |
97 | |
|
98 | 0 | if (pSurface) |
99 | 0 | { |
100 | 0 | const tools::Long nStride(cairo_image_surface_get_stride(pSurface)); |
101 | 0 | const tools::Long nHeight(cairo_image_surface_get_height(pSurface)); |
102 | |
|
103 | 0 | nRetval = nStride * nHeight; |
104 | | |
105 | | // if we do downscale, size will grow by 1/4 + 1/16 + 1/32 + ..., |
106 | | // rough estimation just multiplies by 1.25, should be good enough |
107 | | // for estimation of buffer survival time |
108 | 0 | if (!bDisableDownScale) |
109 | 0 | { |
110 | 0 | nRetval = (nRetval * 5) / 4; |
111 | 0 | } |
112 | 0 | } |
113 | 0 | } |
114 | |
|
115 | 0 | return nRetval; |
116 | 0 | } |
117 | | |
118 | | } // end anonymous namespace |
119 | | |
120 | | SystemDependentData_BitmapHelper::SystemDependentData_BitmapHelper( |
121 | | std::shared_ptr<BitmapHelper> xBitmapHelper) |
122 | 1.34k | : basegfx::SystemDependentData(Application::GetSystemDependentDataManager(), |
123 | 1.34k | basegfx::SDD_Type::SDDType_BitmapHelper) |
124 | 1.34k | , maBitmapHelper(std::move(xBitmapHelper)) |
125 | 1.34k | { |
126 | 1.34k | } |
127 | | |
128 | | sal_Int64 SystemDependentData_BitmapHelper::estimateUsageInBytes() const |
129 | 0 | { |
130 | 0 | return estimateUsageInBytesForSurfaceHelper(maBitmapHelper.get()); |
131 | 0 | } |
132 | | |
133 | | namespace |
134 | | { |
135 | | // MM02 decide to use buffers or not |
136 | | const char* pDisableMM02Goodies(getenv("SAL_DISABLE_MM02_GOODIES")); |
137 | | bool bUseBuffer(nullptr == pDisableMM02Goodies); |
138 | | const tools::Long nMinimalSquareSizeToBuffer(64 * 64); |
139 | | } |
140 | | |
141 | | void tryToUseSourceBuffer(const SalBitmap& rSourceBitmap, std::shared_ptr<BitmapHelper>& rSurface) |
142 | 9.80k | { |
143 | | // MM02 try to access buffered BitmapHelper |
144 | 9.80k | std::shared_ptr<SystemDependentData_BitmapHelper> pSystemDependentData_BitmapHelper; |
145 | 9.80k | const bool bBufferSource(bUseBuffer |
146 | 9.80k | && rSourceBitmap.GetSize().Width() * rSourceBitmap.GetSize().Height() |
147 | 9.80k | > nMinimalSquareSizeToBuffer); |
148 | | |
149 | 9.80k | if (bBufferSource) |
150 | 1.34k | { |
151 | 1.34k | pSystemDependentData_BitmapHelper |
152 | 1.34k | = rSourceBitmap.getSystemDependentData<SystemDependentData_BitmapHelper>( |
153 | 1.34k | basegfx::SDD_Type::SDDType_BitmapHelper); |
154 | | |
155 | 1.34k | if (pSystemDependentData_BitmapHelper) |
156 | 0 | { |
157 | | // reuse buffered data |
158 | 0 | rSurface = pSystemDependentData_BitmapHelper->getBitmapHelper(); |
159 | 0 | } |
160 | 1.34k | } |
161 | | |
162 | 9.80k | if (rSurface) |
163 | 0 | return; |
164 | | |
165 | | // create data on-demand |
166 | 9.80k | rSurface = std::make_shared<BitmapHelper>(rSourceBitmap); |
167 | | |
168 | 9.80k | if (bBufferSource) |
169 | 1.34k | { |
170 | | // add to buffering mechanism to potentially reuse next time |
171 | 1.34k | rSourceBitmap.addOrReplaceSystemDependentData<SystemDependentData_BitmapHelper>(rSurface); |
172 | 1.34k | } |
173 | 9.80k | } |
174 | | |
175 | | /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |