LibreOffice Module vcl (master) 1
virdev.cxx
Go to the documentation of this file.
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 <sal/config.h>
21
22#include <comphelper/lok.hxx>
23#include <sal/log.hxx>
24#include <tools/debug.hxx>
25
27#include <vcl/virdev.hxx>
28
29#include <ImplOutDevData.hxx>
32#include <impfontcache.hxx>
33#include <salinst.hxx>
34#include <salgdi.hxx>
35#include <salvd.hxx>
36#include <svdata.hxx>
37
38using namespace ::com::sun::star::uno;
39
41{
42 const vcl::ExtOutDevData* pOutDevData(GetExtOutDevData());
43 const vcl::PDFExtOutDevData* pPDFData(dynamic_cast<const vcl::PDFExtOutDevData*>(pOutDevData));
44 return pPDFData == nullptr;
45}
46
48{
50
51 if ( mpGraphics )
52 return true;
53
54 mbInitLineColor = true;
55 mbInitFillColor = true;
56 mbInitFont = true;
57 mbInitTextColor = true;
58 mbInitClipRegion = true;
59
60 ImplSVData* pSVData = ImplGetSVData();
61
62 if ( mpVirDev )
63 {
64 mpGraphics = mpVirDev->AcquireGraphics();
65 // if needed retry after releasing least recently used virtual device graphics
66 while ( !mpGraphics )
67 {
68 if ( !pSVData->maGDIData.mpLastVirGraphics )
69 break;
71 mpGraphics = mpVirDev->AcquireGraphics();
72 }
73 // update global LRU list of virtual device graphics
74 if ( mpGraphics )
75 {
77 pSVData->maGDIData.mpFirstVirGraphics = const_cast<VirtualDevice*>(this);
78 if ( mpNextGraphics )
79 mpNextGraphics->mpPrevGraphics = const_cast<VirtualDevice*>(this);
80 if ( !pSVData->maGDIData.mpLastVirGraphics )
81 pSVData->maGDIData.mpLastVirGraphics = const_cast<VirtualDevice*>(this);
82 }
83 }
84
85 if ( mpGraphics )
86 {
89 }
90
91 return mpGraphics != nullptr;
92}
93
94void VirtualDevice::ReleaseGraphics( bool bRelease )
95{
97
98 if ( !mpGraphics )
99 return;
100
101 // release the fonts of the physically released graphics device
102 if ( bRelease )
104
105 ImplSVData* pSVData = ImplGetSVData();
106
107 VirtualDevice* pVirDev = this;
108
109 if ( bRelease )
110 pVirDev->mpVirDev->ReleaseGraphics( mpGraphics );
111 // remove from global LRU list of virtual device graphics
112 if ( mpPrevGraphics )
114 else
116 if ( mpNextGraphics )
118 else
120
121 mpGraphics = nullptr;
122 mpPrevGraphics = nullptr;
123 mpNextGraphics = nullptr;
124}
125
127 tools::Long nDX, tools::Long nDY, const SystemGraphicsData *pData )
128{
129 SAL_INFO( "vcl.virdev", "ImplInitVirDev(" << nDX << "," << nDY << ")" );
130
132 mbForceZeroExtleadBug = false;
133 mnBitCount = 0;
134 mbScreenComp = false;
135
136
137 bool bErase = nDX > 0 && nDY > 0;
138
139 if ( nDX < 1 )
140 nDX = 1;
141
142 if ( nDY < 1 )
143 nDY = 1;
144
145 ImplSVData* pSVData = ImplGetSVData();
146
147 if ( !pOutDev )
148 pOutDev = ImplGetDefaultWindow()->GetOutDev();
149 if( !pOutDev )
150 return;
151
152 SalGraphics* pGraphics;
153 if ( !pOutDev->mpGraphics )
154 (void)pOutDev->AcquireGraphics();
155 pGraphics = pOutDev->mpGraphics;
156 if ( pGraphics )
157 mpVirDev = pSVData->mpDefInst->CreateVirtualDevice(*pGraphics, nDX, nDY, meFormatAndAlpha, pData);
158 else
159 mpVirDev = nullptr;
160 if ( !mpVirDev )
161 {
162 // do not abort but throw an exception, may be the current thread terminates anyway (plugin-scenario)
163 throw css::uno::RuntimeException(
164 "Could not create system bitmap!",
165 css::uno::Reference< css::uno::XInterface >() );
166 }
167
168 mnBitCount = pOutDev->GetBitCount();
169 mnOutWidth = nDX;
170 mnOutHeight = nDY;
171
172 mbScreenComp = pOutDev->IsScreenComp();
173
174 mbDevOutput = true;
177 mnDPIX = pOutDev->mnDPIX;
178 mnDPIY = pOutDev->mnDPIY;
180 maFont = pOutDev->maFont;
181
182 if( maTextColor != pOutDev->maTextColor )
183 {
184 maTextColor = pOutDev->maTextColor;
185 mbInitTextColor = true;
186 }
187
188 // virtual devices have white background by default
190
191 // #i59283# don't erase user-provided surface
192 if( !pData && bErase)
193 Erase();
194
195 // register VirDev in the list
196 mpNext = pSVData->maGDIData.mpFirstVirDev;
197 mpPrev = nullptr;
198 if ( mpNext )
199 mpNext->mpPrev = this;
200 pSVData->maGDIData.mpFirstVirDev = this;
201}
202
204 OutDevType eOutDevType)
205 : OutputDevice(eOutDevType)
206 , meFormatAndAlpha(eFormatAndAlpha)
207{
208 SAL_INFO( "vcl.virdev", "VirtualDevice::VirtualDevice( " << static_cast<int>(eFormatAndAlpha)
209 << ", " << static_cast<int>(eOutDevType) << " )" );
210
211 ImplInitVirDev(pCompDev ? pCompDev : Application::GetDefaultDevice(), 0, 0);
212}
213
215 DeviceFormat eFormat)
217 , meFormatAndAlpha(eFormat)
218{
219 SAL_INFO( "vcl.virdev", "VirtualDevice::VirtualDevice( " << static_cast<int>(eFormat) << " )" );
220
221 ImplInitVirDev(Application::GetDefaultDevice(), rSize.Width(), rSize.Height(), &rData);
222}
223
225{
226 SAL_INFO( "vcl.virdev", "VirtualDevice::~VirtualDevice()" );
227 disposeOnce();
228}
229
231{
232 SAL_INFO( "vcl.virdev", "VirtualDevice::dispose()" );
233
234 ImplSVData* pSVData = ImplGetSVData();
235
237
238 mpVirDev.reset();
239
240 // remove this VirtualDevice from the double-linked global list
241 if( mpPrev )
243 else
244 pSVData->maGDIData.mpFirstVirDev = mpNext;
245
246 if( mpNext )
248
250}
251
252bool VirtualDevice::InnerImplSetOutputSizePixel( const Size& rNewSize, bool bErase,
253 sal_uInt8 *const pBuffer)
254{
255 SAL_INFO( "vcl.virdev",
256 "VirtualDevice::InnerImplSetOutputSizePixel( " << rNewSize.Width() << ", "
257 << rNewSize.Height() << ", " << int(bErase) << " )" );
258
259 if ( !mpVirDev )
260 return false;
261 else if ( rNewSize == GetOutputSizePixel() )
262 {
263 if ( bErase )
264 Erase();
265 SAL_INFO( "vcl.virdev", "Trying to re-use a VirtualDevice but this time using a pre-allocated buffer");
266 return true;
267 }
268
269 bool bRet;
270 tools::Long nNewWidth = rNewSize.Width(), nNewHeight = rNewSize.Height();
271
272 if ( nNewWidth < 1 )
273 nNewWidth = 1;
274
275 if ( nNewHeight < 1 )
276 nNewHeight = 1;
277
278 if ( bErase )
279 {
280 if ( pBuffer )
281 bRet = mpVirDev->SetSizeUsingBuffer( nNewWidth, nNewHeight, pBuffer );
282 else
283 bRet = mpVirDev->SetSize( nNewWidth, nNewHeight );
284
285 if ( bRet )
286 {
287 mnOutWidth = rNewSize.Width();
288 mnOutHeight = rNewSize.Height();
289 Erase();
290 }
291 }
292 else
293 {
294 std::unique_ptr<SalVirtualDevice> pNewVirDev;
295 ImplSVData* pSVData = ImplGetSVData();
296
297 // we need a graphics
298 if ( !mpGraphics && !AcquireGraphics() )
299 return false;
300
301 assert(mpGraphics);
302
303 pNewVirDev = pSVData->mpDefInst->CreateVirtualDevice(*mpGraphics, nNewWidth, nNewHeight, meFormatAndAlpha);
304 if ( pNewVirDev )
305 {
306 SalGraphics* pGraphics = pNewVirDev->AcquireGraphics();
307 if ( pGraphics )
308 {
309 tools::Long nWidth;
310 tools::Long nHeight;
311 if ( mnOutWidth < nNewWidth )
312 nWidth = mnOutWidth;
313 else
314 nWidth = nNewWidth;
315 if ( mnOutHeight < nNewHeight )
316 nHeight = mnOutHeight;
317 else
318 nHeight = nNewHeight;
319 SalTwoRect aPosAry(0, 0, nWidth, nHeight, 0, 0, nWidth, nHeight);
320 pGraphics->CopyBits( aPosAry, *mpGraphics, *this, *this );
321 pNewVirDev->ReleaseGraphics( pGraphics );
323 mpVirDev = std::move(pNewVirDev);
324 mnOutWidth = rNewSize.Width();
325 mnOutHeight = rNewSize.Height();
326 bRet = true;
327 }
328 else
329 {
330 bRet = false;
331 }
332 }
333 else
334 bRet = false;
335 }
336
337 return bRet;
338}
339
340// #i32109#: Fill opaque areas correctly (without relying on
341// fill/linecolor state)
343{
344 // Set line and fill color to black (->opaque),
345 // fill rect with that (linecolor, too, because of
346 // those pesky missing pixel problems)
350 DrawRect( rRect );
351 Pop();
352}
353
354bool VirtualDevice::ImplSetOutputSizePixel( const Size& rNewSize, bool bErase,
355 sal_uInt8 *const pBuffer)
356{
357 if( InnerImplSetOutputSizePixel(rNewSize, bErase, pBuffer) )
358 {
360 {
361 // #110958# Setup alpha bitmap
362 if(mpAlphaVDev && mpAlphaVDev->GetOutputSizePixel() != rNewSize)
363 {
365 }
366
367 if( !mpAlphaVDev )
368 {
370 mpAlphaVDev->InnerImplSetOutputSizePixel(rNewSize, bErase, nullptr);
371 }
372
373 // TODO: copy full outdev state to new one, here. Also needed in outdev2.cxx:DrawOutDev
376
379
381
383 }
384
385 return true;
386 }
387
388 return false;
389}
390
391void VirtualDevice::EnableRTL( bool bEnable )
392{
393 // virdevs default to not mirroring, they will only be set to mirroring
394 // under rare circumstances in the UI, eg the valueset control
395 // because each virdev has its own SalGraphics we can safely switch the SalGraphics here
396 // ...hopefully
397 if( AcquireGraphics() )
399
401}
402
403bool VirtualDevice::SetOutputSizePixel( const Size& rNewSize, bool bErase )
404{
405 return ImplSetOutputSizePixel(rNewSize, bErase, nullptr);
406}
407
409 const Size& rNewSize, const Fraction& rScale, const Point& rNewOffset,
410 sal_uInt8 *const pBuffer)
411{
412 // If this is ever needed for something else than LOK, changes will
413 // be needed in SvpSalVirtualDevice::CreateSurface() .
415 assert(pBuffer);
417 mm.SetOrigin( rNewOffset );
418 mm.SetScaleX( rScale );
419 mm.SetScaleY( rScale );
420 SetMapMode( mm );
421 return ImplSetOutputSizePixel(rNewSize, true, pBuffer);
422}
423
425{
426 sal_Int32 nDPIX = 600, nDPIY = 600;
427 switch( i_eRefDevMode )
428 {
429 case RefDevMode::NONE:
430 default:
431 SAL_WARN( "vcl.virdev", "VDev::SetRefDev illegal argument!" );
432 break;
434 nDPIX = nDPIY = 600;
435 break;
436 case RefDevMode::MSO1:
437 nDPIX = nDPIY = 6*1440;
438 break;
439 case RefDevMode::PDF1:
440 nDPIX = nDPIY = 720;
441 break;
442 }
443 ImplSetReferenceDevice( i_eRefDevMode, nDPIX, nDPIY );
444}
445
446void VirtualDevice::SetReferenceDevice( sal_Int32 i_nDPIX, sal_Int32 i_nDPIY )
447{
448 ImplSetReferenceDevice( RefDevMode::Custom, i_nDPIX, i_nDPIY );
449}
450
452{
453 return true;
454}
455
456void VirtualDevice::ImplSetReferenceDevice( RefDevMode i_eRefDevMode, sal_Int32 i_nDPIX, sal_Int32 i_nDPIY )
457{
458 mnDPIX = i_nDPIX;
459 mnDPIY = i_nDPIY;
461
462 EnableOutput( false ); // prevent output on reference device
463 mbScreenComp = false;
464
465 // invalidate currently selected fonts
466 mbInitFont = true;
467 mbNewFont = true;
468
469 // avoid adjusting font lists when already in refdev mode
470 RefDevMode nOldRefDevMode = meRefDevMode;
471 meRefDevMode = i_eRefDevMode;
472 if( nOldRefDevMode != RefDevMode::NONE )
473 return;
474
475 // the reference device should have only scalable fonts
476 // => clean up the original font lists before getting new ones
477 mpFontInstance.clear();
478 mpFontFaceCollection.reset();
479
480 // preserve global font lists
481 ImplSVData* pSVData = ImplGetSVData();
482 mxFontCollection.reset();
483 mxFontCache.reset();
484
485 // get font list with scalable fonts only
486 (void)AcquireGraphics();
487 mxFontCollection = pSVData->maGDIData.mxScreenFontList->Clone();
488
489 // prepare to use new font lists
490 mxFontCache = std::make_shared<ImplFontCache>();
491}
492
494{
495 return mnBitCount;
496}
497
499{
500 return true;
501}
502
504{
506}
507
509{
510#ifdef UNX
511 // backwards compatible line metrics after fixing #i60945#
513 return 0;
514#endif
515
516 return mpFontInstance->mxFontMetric->GetExternalLeading();
517}
518
519/* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */
static OutputDevice * GetDefaultDevice()
Get the default "device" (in this case the default window).
Definition: svapp.cxx:1060
Some things multiple-inherit from VclAbstractDialog and OutputDevice, so we need to use virtual inher...
Definition: outdev.hxx:170
sal_Int32 mnDPIY
Definition: outdev.hxx:213
virtual void ReleaseGraphics(bool bRelease=true)=0
Release the graphics device, and remove it from the graphics device list.
void SetAntialiasing(AntialiasingFlags nMode)
Definition: outdev.cxx:349
void EnableOutput(bool bEnable=true)
Definition: outdev.cxx:341
bool mbDevOutput
Definition: outdev.hxx:244
std::shared_ptr< ImplFontCache > mxFontCache
Definition: outdev.hxx:262
virtual void dispose() override
Definition: outdev.cxx:144
virtual bool AcquireGraphics() const =0
Acquire a graphics device that the output device uses to draw on.
Size GetOutputSizePixel() const
Definition: outdev.hxx:314
std::shared_ptr< vcl::font::PhysicalFontCollection > mxFontCollection
Definition: outdev.hxx:261
void DrawRect(const tools::Rectangle &rRect)
Definition: rect.cxx:50
vcl::ExtOutDevData * GetExtOutDevData() const
Definition: outdev.hxx:416
void SetLineColor()
Definition: line.cxx:37
Color maTextColor
Definition: outdev.hxx:229
void SetMapMode()
Definition: map.cxx:610
virtual void ImplReleaseFonts()
bool mbNewFont
Definition: outdev.hxx:254
virtual sal_uInt16 GetBitCount() const
Definition: outdev.cxx:372
virtual bool IsScreenComp() const
Definition: outdev.hxx:310
bool mbInitLineColor
Definition: outdev.hxx:248
tools::Long mnOutWidth
Definition: outdev.hxx:210
SalGraphics * mpGraphics
Graphics context to draw on.
Definition: outdev.hxx:182
bool mbInitFont
Definition: outdev.hxx:250
VclPtr< OutputDevice > mpNextGraphics
Next output device in list.
Definition: outdev.hxx:184
bool mbInitTextColor
Definition: outdev.hxx:251
vcl::Font maFont
Definition: outdev.hxx:228
rtl::Reference< LogicalFontInstance > mpFontInstance
Definition: outdev.hxx:186
bool mbInitClipRegion
Definition: outdev.hxx:252
RasterOp meRasterOp
Definition: outdev.hxx:232
void SetFillColor()
Definition: fill.cxx:29
AntialiasingFlags mnAntialiasing
Definition: outdev.hxx:237
const Color & GetLineColor() const
Definition: outdev.hxx:510
VclPtr< VirtualDevice > mpAlphaVDev
Definition: outdev.hxx:196
const MapMode & GetMapMode() const
Definition: outdev.hxx:1557
sal_Int32 mnDPIScalePercentage
For HiDPI displays, we want to draw elements for a percentage larger.
Definition: outdev.hxx:214
void Erase()
Definition: wallpaper.cxx:96
void Push(vcl::PushFlags nFlags=vcl::PushFlags::ALL)
Definition: stack.cxx:32
void SetBackground()
Definition: background.cxx:27
void Pop()
Definition: stack.cxx:91
std::unique_ptr< vcl::font::PhysicalFontFaceCollection > mpFontFaceCollection
Definition: outdev.hxx:187
AntialiasingFlags GetAntialiasing() const
Definition: outdev.hxx:484
virtual void EnableRTL(bool bEnable=true)
Definition: outdev.cxx:647
friend class VirtualDevice
Definition: outdev.hxx:172
sal_Int32 mnDPIX
Definition: outdev.hxx:212
bool mbInitFillColor
Definition: outdev.hxx:249
VclPtr< OutputDevice > mpPrevGraphics
Previous output device in list.
Definition: outdev.hxx:183
tools::Long mnOutHeight
Definition: outdev.hxx:211
const Color & GetFillColor() const
Definition: outdev.hxx:515
void SetLayout(SalLayoutFlags aLayout)
Definition: salgdi.hxx:171
virtual void SetXORMode(bool bSet, bool bInvertOnly)=0
void setAntiAlias(bool bNew)
Definition: salgdi.hxx:83
void CopyBits(const SalTwoRect &rPosAry, const OutputDevice &rOutDev)
virtual std::unique_ptr< SalVirtualDevice > CreateVirtualDevice(SalGraphics &rGraphics, tools::Long &rDX, tools::Long &rDY, DeviceFormat eFormat, const SystemGraphicsData *pData=nullptr)=0
constexpr tools::Long Height() const
constexpr tools::Long Width() const
void disposeAndClear()
Definition: vclptr.hxx:200
static VclPtr< reference_type > Create(Arg &&... arg)
A construction helper for VclPtr.
Definition: vclptr.hxx:127
void Compat_ZeroExtleadBug()
Definition: virdev.cxx:503
bool SetOutputSizePixel(const Size &rNewSize, bool bErase=true)
Definition: virdev.cxx:403
VclPtr< VirtualDevice > mpPrev
Definition: virdev.hxx:49
bool mbScreenComp
Definition: virdev.hxx:52
bool IsVirtual() const override
Definition: virdev.cxx:451
virtual void EnableRTL(bool bEnable=true) override
Definition: virdev.cxx:391
void SetReferenceDevice(RefDevMode)
Definition: virdev.cxx:424
bool SetOutputSizePixelScaleOffsetAndLOKBuffer(const Size &rNewSize, const Fraction &rScale, const Point &rNewOffset, sal_uInt8 *pBuffer)
Definition: virdev.cxx:408
bool CanEnableNativeWidget() const override
Determine if native widgets can be enabled.
Definition: virdev.cxx:40
SAL_DLLPRIVATE void ImplInitVirDev(const OutputDevice *pOutDev, tools::Long nDX, tools::Long nDY, const SystemGraphicsData *pData=nullptr)
Definition: virdev.cxx:126
sal_uInt16 mnBitCount
Definition: virdev.hxx:51
virtual tools::Long GetFontExtLeading() const override
Definition: virdev.cxx:508
SAL_DLLPRIVATE void ImplFillOpaqueRectangle(const tools::Rectangle &rRect)
Used for alpha VDev, to set areas to opaque.
Definition: virdev.cxx:342
RefDevMode meRefDevMode
Definition: virdev.hxx:54
std::unique_ptr< SalVirtualDevice > mpVirDev
Definition: virdev.hxx:48
virtual ~VirtualDevice() override
Definition: virdev.cxx:224
bool mbForceZeroExtleadBug
Definition: virdev.hxx:55
SAL_DLLPRIVATE void ImplSetReferenceDevice(RefDevMode, sal_Int32 i_nDPIX, sal_Int32 i_nDPIY)
Definition: virdev.cxx:456
const DeviceFormat meFormatAndAlpha
Definition: virdev.hxx:53
virtual bool UsePolyPolygonForComplexGradient() override
Definition: virdev.cxx:498
VclPtr< VirtualDevice > mpNext
Definition: virdev.hxx:50
virtual sal_uInt16 GetBitCount() const override
Definition: virdev.cxx:493
virtual bool AcquireGraphics() const override
Acquire a graphics device that the output device uses to draw on.
Definition: virdev.cxx:47
SAL_DLLPRIVATE bool InnerImplSetOutputSizePixel(const Size &rNewSize, bool bErase, sal_uInt8 *pBuffer)
Definition: virdev.cxx:252
SAL_DLLPRIVATE bool ImplSetOutputSizePixel(const Size &rNewSize, bool bErase, sal_uInt8 *pBuffer)
Definition: virdev.cxx:354
virtual void ReleaseGraphics(bool bRelease=true) override
Release the graphics device, and remove it from the graphics device list.
Definition: virdev.cxx:94
virtual void dispose() override
Definition: virdev.cxx:230
::OutputDevice const * GetOutDev() const
Definition: window.cxx:567
constexpr ::Color COL_WHITE(0xFF, 0xFF, 0xFF)
constexpr ::Color COL_BLACK(0x00, 0x00, 0x00)
constexpr ::Color COL_TRANSPARENT(ColorTransparency, 0xFF, 0xFF, 0xFF, 0xFF)
#define DBG_TESTSOLARMUTEX()
#define SAL_WARN(area, stream)
#define SAL_INFO(area, stream)
std::unique_ptr< sal_Int32[]> pData
long Long
OutDevType
Definition: outdev.hxx:145
@ OUTDEV_VIRDEV
Definition: outdev.hxx:145
DeviceFormat
Definition: salgtype.hxx:28
SalInstance * mpDefInst
Definition: svdata.hxx:391
ImplSVGDIData maGDIData
Definition: svdata.hxx:400
VclPtr< OutputDevice > mpLastVirGraphics
Definition: svdata.hxx:220
std::shared_ptr< vcl::font::PhysicalFontCollection > mxScreenFontList
Definition: svdata.hxx:227
std::shared_ptr< ImplFontCache > mxScreenFontCache
Definition: svdata.hxx:228
VclPtr< VirtualDevice > mpFirstVirDev
Definition: svdata.hxx:223
VclPtr< OutputDevice > mpFirstVirGraphics
Definition: svdata.hxx:219
ImplSVData * ImplGetSVData()
Definition: svdata.cxx:77
vcl::Window * ImplGetDefaultWindow()
Returns either the application window, or the default GL context window.
Definition: svdata.cxx:212
unsigned char sal_uInt8