1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
#include <test/bootstrapfixture.hxx>
#include <com/sun/star/beans/PropertyValue.hpp>
#include <vcl/BitmapReadAccess.hxx>
#include <vcl/graph.hxx>
#include <vcl/graphicfilter.hxx>
#include <tools/stream.hxx>
#include <impgraph.hxx>
#include <graphic/GraphicFormatDetector.hxx>
#include <graphic/MemoryManaged.hxx>
using namespace css;
namespace
{
BitmapEx createBitmap(Size aSize, bool bAlpha = false)
{
Bitmap aBitmap(aSize, vcl::PixelFormat::N24_BPP);
aBitmap.Erase(COL_LIGHTRED);
aBitmap.SetPrefSize(Size(aSize.Width() * 2, aSize.Height() * 3));
aBitmap.SetPrefMapMode(MapMode(MapUnit::Map100thMM));
if (bAlpha)
{
sal_uInt8 uAlphaValue = 0x80;
AlphaMask aAlphaMask(aSize, &uAlphaValue);
return BitmapEx(aBitmap, aAlphaMask);
}
else
{
return BitmapEx(aBitmap);
}
}
void createBitmapAndExportForType(SvStream& rStream, std::u16string_view sType,
Size aSize = Size(120, 100), bool bAlpha = false)
{
BitmapEx aBitmapEx = createBitmap(aSize, bAlpha);
uno::Sequence<beans::PropertyValue> aFilterData;
GraphicFilter& rGraphicFilter = GraphicFilter::GetGraphicFilter();
sal_uInt16 nFilterFormat = rGraphicFilter.GetExportFormatNumberForShortName(sType);
rGraphicFilter.ExportGraphic(aBitmapEx, u"none", rStream, nFilterFormat, &aFilterData);
rStream.Seek(STREAM_SEEK_TO_BEGIN);
}
Graphic makeUnloadedGraphic(std::u16string_view sType, Size aSize = Size(120, 100),
bool bAlpha = false)
{
SvMemoryStream aStream;
GraphicFilter& rGraphicFilter = GraphicFilter::GetGraphicFilter();
createBitmapAndExportForType(aStream, sType, aSize, bAlpha);
return rGraphicFilter.ImportUnloadedGraphic(aStream);
}
constexpr OUString DATA_DIRECTORY = u"/vcl/qa/cppunit/data/"_ustr;
Graphic loadGraphic(std::u16string_view rFilename)
{
test::Directories aDirectories;
OUString aFilename = aDirectories.getURLFromSrc(DATA_DIRECTORY) + rFilename;
SvFileStream aFileStream(aFilename, StreamMode::READ);
GraphicFilter& rGraphicFilter = GraphicFilter::GetGraphicFilter();
Graphic aGraphic;
CPPUNIT_ASSERT_EQUAL(ERRCODE_NONE, rGraphicFilter.ImportGraphic(aGraphic, u"", aFileStream,
GRFILTER_FORMAT_DONTKNOW));
return aGraphic;
}
Graphic loadUnloadedGraphic(std::u16string_view rFilename)
{
test::Directories aDirectories;
OUString aFilename = aDirectories.getURLFromSrc(DATA_DIRECTORY) + rFilename;
SvFileStream aFileStream(aFilename, StreamMode::READ);
GraphicFilter& rGraphicFilter = GraphicFilter::GetGraphicFilter();
return rGraphicFilter.ImportUnloadedGraphic(aFileStream);
}
} // end anonymous
class GraphicMemoryTest : public test::BootstrapFixture
{
};
CPPUNIT_TEST_FIXTURE(GraphicMemoryTest, testMemoryManager_Empty)
{
auto& rManager = vcl::graphic::MemoryManager::get();
CPPUNIT_ASSERT_EQUAL(size_t(0), rManager.getManagedObjects().size());
Graphic aGraphic1;
CPPUNIT_ASSERT_EQUAL(size_t(0), rManager.getManagedObjects().size());
Graphic aGraphic2;
CPPUNIT_ASSERT_EQUAL(size_t(0), rManager.getManagedObjects().size());
Graphic aGraphic3;
CPPUNIT_ASSERT_EQUAL(size_t(0), rManager.getManagedObjects().size());
aGraphic1 = aGraphic2;
CPPUNIT_ASSERT_EQUAL(size_t(0), rManager.getManagedObjects().size());
aGraphic2 = aGraphic3;
CPPUNIT_ASSERT_EQUAL(size_t(0), rManager.getManagedObjects().size());
}
CPPUNIT_TEST_FIXTURE(GraphicMemoryTest, testMemoryManager_Unloaded)
{
auto& rManager = vcl::graphic::MemoryManager::get();
CPPUNIT_ASSERT_EQUAL(size_t(0), rManager.getManagedObjects().size());
Graphic aGraphic_100 = makeUnloadedGraphic(u"jpg", Size(100, 100));
Graphic aGraphic_200 = makeUnloadedGraphic(u"png", Size(200, 100));
Graphic aGraphic_300 = makeUnloadedGraphic(u"jpg", Size(300, 100));
CPPUNIT_ASSERT_EQUAL(size_t(0), rManager.getManagedObjects().size());
aGraphic_100.makeAvailable();
CPPUNIT_ASSERT_EQUAL(size_t(1), rManager.getManagedObjects().size());
CPPUNIT_ASSERT_EQUAL(sal_Int64(30000), rManager.getTotalSize());
CPPUNIT_ASSERT_EQUAL(sal_uLong(30000), aGraphic_100.GetSizeBytes());
aGraphic_200.makeAvailable();
CPPUNIT_ASSERT_EQUAL(size_t(2), rManager.getManagedObjects().size());
CPPUNIT_ASSERT_EQUAL(sal_Int64(90000), rManager.getTotalSize());
CPPUNIT_ASSERT_EQUAL(sal_uLong(30000), aGraphic_100.GetSizeBytes());
CPPUNIT_ASSERT_EQUAL(sal_uLong(60000), aGraphic_200.GetSizeBytes());
aGraphic_300.makeAvailable();
CPPUNIT_ASSERT_EQUAL(size_t(3), rManager.getManagedObjects().size());
CPPUNIT_ASSERT_EQUAL(sal_Int64(180000), rManager.getTotalSize());
CPPUNIT_ASSERT_EQUAL(sal_uLong(30000), aGraphic_100.GetSizeBytes());
CPPUNIT_ASSERT_EQUAL(sal_uLong(60000), aGraphic_200.GetSizeBytes());
CPPUNIT_ASSERT_EQUAL(sal_uLong(90000), aGraphic_300.GetSizeBytes());
}
CPPUNIT_TEST_FIXTURE(GraphicMemoryTest, testMemoryManager_Destruction)
{
auto& rManager = vcl::graphic::MemoryManager::get();
CPPUNIT_ASSERT_EQUAL(size_t(0), rManager.getManagedObjects().size());
Graphic aGraphic_100 = makeUnloadedGraphic(u"jpg", Size(100, 100));
Graphic aGraphic_200 = makeUnloadedGraphic(u"png", Size(200, 100));
Graphic aGraphic_300 = makeUnloadedGraphic(u"jpg", Size(300, 100));
CPPUNIT_ASSERT_EQUAL(size_t(0), rManager.getManagedObjects().size());
aGraphic_100.makeAvailable();
aGraphic_200.makeAvailable();
aGraphic_300.makeAvailable();
CPPUNIT_ASSERT_EQUAL(size_t(3), rManager.getManagedObjects().size());
CPPUNIT_ASSERT_EQUAL(sal_Int64(180000), rManager.getTotalSize());
aGraphic_200 = Graphic();
CPPUNIT_ASSERT_EQUAL(size_t(2), rManager.getManagedObjects().size());
CPPUNIT_ASSERT_EQUAL(sal_Int64(120000), rManager.getTotalSize());
aGraphic_300 = Graphic();
CPPUNIT_ASSERT_EQUAL(size_t(1), rManager.getManagedObjects().size());
CPPUNIT_ASSERT_EQUAL(sal_Int64(30000), rManager.getTotalSize());
}
CPPUNIT_TEST_FIXTURE(GraphicMemoryTest, testMemoryManager_Copy)
{
auto& rManager = vcl::graphic::MemoryManager::get();
CPPUNIT_ASSERT_EQUAL(size_t(0), rManager.getManagedObjects().size());
Graphic aGraphic_100 = makeUnloadedGraphic(u"jpg", Size(100, 100));
Graphic aGraphic_200 = makeUnloadedGraphic(u"png", Size(200, 100));
Graphic aGraphic_300 = makeUnloadedGraphic(u"jpg", Size(300, 100));
Graphic aSVG = loadUnloadedGraphic(u"SimpleExample.svg");
CPPUNIT_ASSERT_EQUAL(size_t(0), rManager.getManagedObjects().size());
aGraphic_100.makeAvailable();
aGraphic_200.makeAvailable();
aGraphic_300.makeAvailable();
}
CPPUNIT_TEST_FIXTURE(GraphicMemoryTest, testMemoryManager)
{
auto& rManager = vcl::graphic::MemoryManager::get();
CPPUNIT_ASSERT_EQUAL(size_t(0), rManager.getManagedObjects().size());
Graphic aGraphic;
CPPUNIT_ASSERT_EQUAL(size_t(0), rManager.getManagedObjects().size());
Graphic aGraphicPng = loadGraphic(u"TypeDetectionExample.png");
{
CPPUNIT_ASSERT_EQUAL(GraphicType::Bitmap, aGraphicPng.GetType());
CPPUNIT_ASSERT_EQUAL(sal_uLong(300), aGraphicPng.GetSizeBytes());
CPPUNIT_ASSERT_EQUAL(true, aGraphicPng.isAvailable());
}
CPPUNIT_ASSERT_EQUAL(size_t(1), rManager.getManagedObjects().size());
CPPUNIT_ASSERT_EQUAL(sal_Int64(300), rManager.getTotalSize());
CPPUNIT_ASSERT_EQUAL(false, aGraphicPng.ImplGetImpGraphic()->isSwappedOut());
CPPUNIT_ASSERT_EQUAL(true, aGraphicPng.ImplGetImpGraphic()->swapOut());
CPPUNIT_ASSERT_EQUAL(true, aGraphicPng.ImplGetImpGraphic()->isSwappedOut());
CPPUNIT_ASSERT_EQUAL(false, aGraphicPng.isAvailable());
CPPUNIT_ASSERT_EQUAL(size_t(1), rManager.getManagedObjects().size());
CPPUNIT_ASSERT_EQUAL(sal_Int64(0), rManager.getTotalSize());
Graphic aGraphicJpg = makeUnloadedGraphic(u"jpg");
{
CPPUNIT_ASSERT_EQUAL(tools::Long(120), aGraphicJpg.GetSizePixel().Width());
CPPUNIT_ASSERT_EQUAL(tools::Long(100), aGraphicJpg.GetSizePixel().Height());
CPPUNIT_ASSERT_EQUAL(false, aGraphicJpg.isAvailable());
}
CPPUNIT_ASSERT_EQUAL(size_t(1), rManager.getManagedObjects().size());
CPPUNIT_ASSERT_EQUAL(sal_Int64(0), rManager.getTotalSize());
aGraphicJpg.makeAvailable();
CPPUNIT_ASSERT_EQUAL(size_t(2), rManager.getManagedObjects().size());
CPPUNIT_ASSERT_EQUAL(sal_Int64(36000), rManager.getTotalSize());
CPPUNIT_ASSERT_EQUAL(true, aGraphicPng.makeAvailable());
CPPUNIT_ASSERT_EQUAL(false, aGraphicPng.ImplGetImpGraphic()->isSwappedOut());
CPPUNIT_ASSERT_EQUAL(size_t(2), rManager.getManagedObjects().size());
CPPUNIT_ASSERT_EQUAL(sal_Int64(36300), rManager.getTotalSize());
aGraphicJpg = aGraphic;
CPPUNIT_ASSERT_EQUAL(size_t(1), rManager.getManagedObjects().size());
CPPUNIT_ASSERT_EQUAL(sal_Int64(300), rManager.getTotalSize());
}
namespace
{
class TestManaged : public vcl::graphic::MemoryManaged
{
public:
std::chrono::high_resolution_clock::time_point maLastUsed
= std::chrono::high_resolution_clock::now();
TestManaged(bool bRegister, sal_Int64 nSize = 0)
: MemoryManaged(bRegister)
{
updateCurrentSizeInBytes(nSize);
}
void setCurrentSize(sal_Int64 nSize) { updateCurrentSizeInBytes(nSize); }
void callRegister() { registerIntoManager(); }
void callUnregister() { unregisterFromManager(); }
bool canReduceMemory() const override { return false; }
bool reduceMemory() override { return false; }
std::chrono::high_resolution_clock::time_point getLastUsed() const override
{
return maLastUsed;
}
void dumpState(rtl::OStringBuffer& /*rState*/) override {}
};
}
CPPUNIT_TEST_FIXTURE(GraphicMemoryTest, testMemoryManagerX)
{
auto& rManager = vcl::graphic::MemoryManager::get();
CPPUNIT_ASSERT_EQUAL(size_t(0), rManager.getManagedObjects().size());
TestManaged aTestManaged(false, 1000);
CPPUNIT_ASSERT_EQUAL(size_t(0), rManager.getManagedObjects().size());
TestManaged aTestManaged2(true, 100);
CPPUNIT_ASSERT_EQUAL(size_t(1), rManager.getManagedObjects().size());
CPPUNIT_ASSERT_EQUAL(sal_Int64(100), rManager.getTotalSize());
aTestManaged2.setCurrentSize(400);
CPPUNIT_ASSERT_EQUAL(size_t(1), rManager.getManagedObjects().size());
CPPUNIT_ASSERT_EQUAL(sal_Int64(400), rManager.getTotalSize());
aTestManaged.setCurrentSize(600);
CPPUNIT_ASSERT_EQUAL(size_t(1), rManager.getManagedObjects().size());
CPPUNIT_ASSERT_EQUAL(sal_Int64(400), rManager.getTotalSize());
aTestManaged.callRegister();
CPPUNIT_ASSERT_EQUAL(size_t(2), rManager.getManagedObjects().size());
CPPUNIT_ASSERT_EQUAL(sal_Int64(1000), rManager.getTotalSize());
aTestManaged.callUnregister();
CPPUNIT_ASSERT_EQUAL(size_t(1), rManager.getManagedObjects().size());
CPPUNIT_ASSERT_EQUAL(sal_Int64(400), rManager.getTotalSize());
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|