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, meFormat, 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 DeviceFormat eAlphaFormat, OutDevType eOutDevType)
205 : OutputDevice(eOutDevType)
206 , meFormat(eFormat)
207 , meAlphaFormat(eAlphaFormat)
208{
209 SAL_INFO( "vcl.virdev", "VirtualDevice::VirtualDevice( " << static_cast<int>(eFormat)
210 << ", " << static_cast<int>(eAlphaFormat)
211 << ", " << static_cast<int>(eOutDevType) << " )" );
212
213 ImplInitVirDev(pCompDev ? pCompDev : Application::GetDefaultDevice(), 0, 0);
214}
215
217 DeviceFormat eFormat)
219 , meFormat(eFormat)
220 , meAlphaFormat(DeviceFormat::NONE)
221{
222 SAL_INFO( "vcl.virdev", "VirtualDevice::VirtualDevice( " << static_cast<int>(eFormat) << " )" );
223
224 ImplInitVirDev(Application::GetDefaultDevice(), rSize.Width(), rSize.Height(), &rData);
225}
226
228{
229 SAL_INFO( "vcl.virdev", "VirtualDevice::~VirtualDevice()" );
230 disposeOnce();
231}
232
234{
235 SAL_INFO( "vcl.virdev", "VirtualDevice::dispose()" );
236
237 ImplSVData* pSVData = ImplGetSVData();
238
240
241 mpVirDev.reset();
242
243 // remove this VirtualDevice from the double-linked global list
244 if( mpPrev )
246 else
247 pSVData->maGDIData.mpFirstVirDev = mpNext;
248
249 if( mpNext )
251
253}
254
255bool VirtualDevice::InnerImplSetOutputSizePixel( const Size& rNewSize, bool bErase,
256 sal_uInt8 *const pBuffer)
257{
258 SAL_INFO( "vcl.virdev",
259 "VirtualDevice::InnerImplSetOutputSizePixel( " << rNewSize.Width() << ", "
260 << rNewSize.Height() << ", " << int(bErase) << " )" );
261
262 if ( !mpVirDev )
263 return false;
264 else if ( rNewSize == GetOutputSizePixel() )
265 {
266 if ( bErase )
267 Erase();
268 SAL_INFO( "vcl.virdev", "Trying to re-use a VirtualDevice but this time using a pre-allocated buffer");
269 return true;
270 }
271
272 bool bRet;
273 tools::Long nNewWidth = rNewSize.Width(), nNewHeight = rNewSize.Height();
274
275 if ( nNewWidth < 1 )
276 nNewWidth = 1;
277
278 if ( nNewHeight < 1 )
279 nNewHeight = 1;
280
281 if ( bErase )
282 {
283 if ( pBuffer )
284 bRet = mpVirDev->SetSizeUsingBuffer( nNewWidth, nNewHeight, pBuffer );
285 else
286 bRet = mpVirDev->SetSize( nNewWidth, nNewHeight );
287
288 if ( bRet )
289 {
290 mnOutWidth = rNewSize.Width();
291 mnOutHeight = rNewSize.Height();
292 Erase();
293 }
294 }
295 else
296 {
297 std::unique_ptr<SalVirtualDevice> pNewVirDev;
298 ImplSVData* pSVData = ImplGetSVData();
299
300 // we need a graphics
301 if ( !mpGraphics && !AcquireGraphics() )
302 return false;
303
304 assert(mpGraphics);
305
306 pNewVirDev = pSVData->mpDefInst->CreateVirtualDevice(*mpGraphics, nNewWidth, nNewHeight, meFormat);
307 if ( pNewVirDev )
308 {
309 SalGraphics* pGraphics = pNewVirDev->AcquireGraphics();
310 if ( pGraphics )
311 {
312 tools::Long nWidth;
313 tools::Long nHeight;
314 if ( mnOutWidth < nNewWidth )
315 nWidth = mnOutWidth;
316 else
317 nWidth = nNewWidth;
318 if ( mnOutHeight < nNewHeight )
319 nHeight = mnOutHeight;
320 else
321 nHeight = nNewHeight;
322 SalTwoRect aPosAry(0, 0, nWidth, nHeight, 0, 0, nWidth, nHeight);
323 pGraphics->CopyBits( aPosAry, *mpGraphics, *this, *this );
324 pNewVirDev->ReleaseGraphics( pGraphics );
326 mpVirDev = std::move(pNewVirDev);
327 mnOutWidth = rNewSize.Width();
328 mnOutHeight = rNewSize.Height();
329 bRet = true;
330 }
331 else
332 {
333 bRet = false;
334 }
335 }
336 else
337 bRet = false;
338 }
339
340 return bRet;
341}
342
343// #i32109#: Fill opaque areas correctly (without relying on
344// fill/linecolor state)
346{
347 // Set line and fill color to black (->opaque),
348 // fill rect with that (linecolor, too, because of
349 // those pesky missing pixel problems)
353 DrawRect( rRect );
354 Pop();
355}
356
357bool VirtualDevice::ImplSetOutputSizePixel( const Size& rNewSize, bool bErase,
358 sal_uInt8 *const pBuffer)
359{
360 if( InnerImplSetOutputSizePixel(rNewSize, bErase, pBuffer) )
361 {
363 {
364 // #110958# Setup alpha bitmap
365 if(mpAlphaVDev && mpAlphaVDev->GetOutputSizePixel() != rNewSize)
366 {
368 }
369
370 if( !mpAlphaVDev )
371 {
373 mpAlphaVDev->InnerImplSetOutputSizePixel(rNewSize, bErase, nullptr);
374 }
375
376 // TODO: copy full outdev state to new one, here. Also needed in outdev2.cxx:DrawOutDev
379
382
384
386 }
387
388 return true;
389 }
390
391 return false;
392}
393
394void VirtualDevice::EnableRTL( bool bEnable )
395{
396 // virdevs default to not mirroring, they will only be set to mirroring
397 // under rare circumstances in the UI, eg the valueset control
398 // because each virdev has its own SalGraphics we can safely switch the SalGraphics here
399 // ...hopefully
400 if( AcquireGraphics() )
402
404}
405
406bool VirtualDevice::SetOutputSizePixel( const Size& rNewSize, bool bErase )
407{
408 return ImplSetOutputSizePixel(rNewSize, bErase, nullptr);
409}
410
412 const Size& rNewSize, const Fraction& rScale, const Point& rNewOffset,
413 sal_uInt8 *const pBuffer)
414{
415 // If this is ever needed for something else than LOK, changes will
416 // be needed in SvpSalVirtualDevice::CreateSurface() .
418 assert(pBuffer);
420 mm.SetOrigin( rNewOffset );
421 mm.SetScaleX( rScale );
422 mm.SetScaleY( rScale );
423 SetMapMode( mm );
424 return ImplSetOutputSizePixel(rNewSize, true, pBuffer);
425}
426
428{
429 sal_Int32 nDPIX = 600, nDPIY = 600;
430 switch( i_eRefDevMode )
431 {
432 case RefDevMode::NONE:
433 default:
434 SAL_WARN( "vcl.virdev", "VDev::SetRefDev illegal argument!" );
435 break;
437 nDPIX = nDPIY = 600;
438 break;
439 case RefDevMode::MSO1:
440 nDPIX = nDPIY = 6*1440;
441 break;
442 case RefDevMode::PDF1:
443 nDPIX = nDPIY = 720;
444 break;
445 }
446 ImplSetReferenceDevice( i_eRefDevMode, nDPIX, nDPIY );
447}
448
449void VirtualDevice::SetReferenceDevice( sal_Int32 i_nDPIX, sal_Int32 i_nDPIY )
450{
451 ImplSetReferenceDevice( RefDevMode::Custom, i_nDPIX, i_nDPIY );
452}
453
455{
456 return true;
457}
458
459void VirtualDevice::ImplSetReferenceDevice( RefDevMode i_eRefDevMode, sal_Int32 i_nDPIX, sal_Int32 i_nDPIY )
460{
461 mnDPIX = i_nDPIX;
462 mnDPIY = i_nDPIY;
464
465 EnableOutput( false ); // prevent output on reference device
466 mbScreenComp = false;
467
468 // invalidate currently selected fonts
469 mbInitFont = true;
470 mbNewFont = true;
471
472 // avoid adjusting font lists when already in refdev mode
473 RefDevMode nOldRefDevMode = meRefDevMode;
474 meRefDevMode = i_eRefDevMode;
475 if( nOldRefDevMode != RefDevMode::NONE )
476 return;
477
478 // the reference device should have only scalable fonts
479 // => clean up the original font lists before getting new ones
480 mpFontInstance.clear();
481 mpFontFaceCollection.reset();
482
483 // preserve global font lists
484 ImplSVData* pSVData = ImplGetSVData();
485 mxFontCollection.reset();
486 mxFontCache.reset();
487
488 // get font list with scalable fonts only
489 (void)AcquireGraphics();
490 mxFontCollection = pSVData->maGDIData.mxScreenFontList->Clone();
491
492 // prepare to use new font lists
493 mxFontCache = std::make_shared<ImplFontCache>();
494}
495
497{
498 return mnBitCount;
499}
500
502{
503 return true;
504}
505
507{
509}
510
512{
513#ifdef UNX
514 // backwards compatible line metrics after fixing #i60945#
516 return 0;
517#endif
518
519 return mpFontInstance->mxFontMetric->GetExternalLeading();
520}
521
522/* 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:1166
Some things multiple-inherit from VclAbstractDialog and OutputDevice, so we need to use virtual inher...
Definition: outdev.hxx:171
sal_Int32 mnDPIY
Definition: outdev.hxx:214
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:350
void EnableOutput(bool bEnable=true)
Definition: outdev.cxx:342
bool mbDevOutput
Definition: outdev.hxx:245
std::shared_ptr< ImplFontCache > mxFontCache
Definition: outdev.hxx:263
virtual void dispose() override
Definition: outdev.cxx:145
virtual bool AcquireGraphics() const =0
Acquire a graphics device that the output device uses to draw on.
Size GetOutputSizePixel() const
Definition: outdev.hxx:315
std::shared_ptr< vcl::font::PhysicalFontCollection > mxFontCollection
Definition: outdev.hxx:262
void DrawRect(const tools::Rectangle &rRect)
Definition: rect.cxx:51
vcl::ExtOutDevData * GetExtOutDevData() const
Definition: outdev.hxx:417
void SetLineColor()
Definition: line.cxx:37
Color maTextColor
Definition: outdev.hxx:230
void SetMapMode()
Definition: map.cxx:654
virtual void ImplReleaseFonts()
bool mbNewFont
Definition: outdev.hxx:255
virtual sal_uInt16 GetBitCount() const
Definition: outdev.cxx:373
virtual bool IsScreenComp() const
Definition: outdev.hxx:311
bool mbInitLineColor
Definition: outdev.hxx:249
tools::Long mnOutWidth
Definition: outdev.hxx:211
SalGraphics * mpGraphics
Graphics context to draw on.
Definition: outdev.hxx:183
bool mbInitFont
Definition: outdev.hxx:251
VclPtr< OutputDevice > mpNextGraphics
Next output device in list.
Definition: outdev.hxx:185
bool mbInitTextColor
Definition: outdev.hxx:252
vcl::Font maFont
Definition: outdev.hxx:229
rtl::Reference< LogicalFontInstance > mpFontInstance
Definition: outdev.hxx:187
bool mbInitClipRegion
Definition: outdev.hxx:253
RasterOp meRasterOp
Definition: outdev.hxx:233
void SetFillColor()
Definition: fill.cxx:29
AntialiasingFlags mnAntialiasing
Definition: outdev.hxx:238
const Color & GetLineColor() const
Definition: outdev.hxx:511
VclPtr< VirtualDevice > mpAlphaVDev
Definition: outdev.hxx:197
const MapMode & GetMapMode() const
Definition: outdev.hxx:1558
sal_Int32 mnDPIScalePercentage
For HiDPI displays, we want to draw elements for a percentage larger.
Definition: outdev.hxx:215
void Erase()
Definition: wallpaper.cxx:96
void Push(vcl::PushFlags nFlags=vcl::PushFlags::ALL)
Definition: stack.cxx:33
void SetBackground()
Definition: background.cxx:27
void Pop()
Definition: stack.cxx:92
std::unique_ptr< vcl::font::PhysicalFontFaceCollection > mpFontFaceCollection
Definition: outdev.hxx:188
AntialiasingFlags GetAntialiasing() const
Definition: outdev.hxx:485
virtual void EnableRTL(bool bEnable=true)
Definition: outdev.cxx:648
friend class VirtualDevice
Definition: outdev.hxx:173
sal_Int32 mnDPIX
Definition: outdev.hxx:213
bool mbInitFillColor
Definition: outdev.hxx:250
VclPtr< OutputDevice > mpPrevGraphics
Previous output device in list.
Definition: outdev.hxx:184
tools::Long mnOutHeight
Definition: outdev.hxx:212
const Color & GetFillColor() const
Definition: outdev.hxx:516
void SetLayout(SalLayoutFlags aLayout)
Definition: salgdi.hxx:180
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:506
bool SetOutputSizePixel(const Size &rNewSize, bool bErase=true)
Definition: virdev.cxx:406
VclPtr< VirtualDevice > mpPrev
Definition: virdev.hxx:49
const DeviceFormat meFormat
Definition: virdev.hxx:53
const DeviceFormat meAlphaFormat
Definition: virdev.hxx:54
bool mbScreenComp
Definition: virdev.hxx:52
bool IsVirtual() const override
Definition: virdev.cxx:454
virtual void EnableRTL(bool bEnable=true) override
Definition: virdev.cxx:394
void SetReferenceDevice(RefDevMode)
Definition: virdev.cxx:427
bool SetOutputSizePixelScaleOffsetAndLOKBuffer(const Size &rNewSize, const Fraction &rScale, const Point &rNewOffset, sal_uInt8 *pBuffer)
Definition: virdev.cxx:411
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:511
SAL_DLLPRIVATE void ImplFillOpaqueRectangle(const tools::Rectangle &rRect)
Used for alpha VDev, to set areas to opaque.
Definition: virdev.cxx:345
RefDevMode meRefDevMode
Definition: virdev.hxx:55
std::unique_ptr< SalVirtualDevice > mpVirDev
Definition: virdev.hxx:48
virtual ~VirtualDevice() override
Definition: virdev.cxx:227
bool mbForceZeroExtleadBug
Definition: virdev.hxx:56
SAL_DLLPRIVATE void ImplSetReferenceDevice(RefDevMode, sal_Int32 i_nDPIX, sal_Int32 i_nDPIY)
Definition: virdev.cxx:459
virtual bool UsePolyPolygonForComplexGradient() override
Definition: virdev.cxx:501
VclPtr< VirtualDevice > mpNext
Definition: virdev.hxx:50
virtual sal_uInt16 GetBitCount() const override
Definition: virdev.cxx:496
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:255
SAL_DLLPRIVATE bool ImplSetOutputSizePixel(const Size &rNewSize, bool bErase, sal_uInt8 *pBuffer)
Definition: virdev.cxx:357
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:233
::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
NONE
long Long
OutDevType
Definition: outdev.hxx:144
@ OUTDEV_VIRDEV
Definition: outdev.hxx:144
DeviceFormat
Definition: salgtype.hxx:28
SalInstance * mpDefInst
Definition: svdata.hxx:392
ImplSVGDIData maGDIData
Definition: svdata.hxx:401
VclPtr< OutputDevice > mpLastVirGraphics
Definition: svdata.hxx:222
std::shared_ptr< vcl::font::PhysicalFontCollection > mxScreenFontList
Definition: svdata.hxx:229
std::shared_ptr< ImplFontCache > mxScreenFontCache
Definition: svdata.hxx:230
VclPtr< VirtualDevice > mpFirstVirDev
Definition: svdata.hxx:225
VclPtr< OutputDevice > mpFirstVirGraphics
Definition: svdata.hxx:221
ImplSVData * ImplGetSVData()
Definition: svdata.cxx:76
vcl::Window * ImplGetDefaultWindow()
Returns either the application window, or the default GL context window.
Definition: svdata.cxx:211
unsigned char sal_uInt8