1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #ifndef INCLUDED_SVTOOLS_GRFMGR_HXX
21 #define INCLUDED_SVTOOLS_GRFMGR_HXX
23 #include <vcl/graph.hxx>
24 #include <svtools/svtdllapi.h>
25 #include <o3tl/typed_flags_set.hxx>
27 enum class GraphicManagerDrawFlags
31 USE_DRAWMODE_SETTINGS
= 0x04,
34 STANDARD
= (CACHED
|SMOOTHSCALE
),
38 template<> struct typed_flags
<GraphicManagerDrawFlags
> : is_typed_flags
<GraphicManagerDrawFlags
, 0x1f> {};
43 #define GRFMGR_AUTOSWAPSTREAM_LINK nullptr
44 #define GRFMGR_AUTOSWAPSTREAM_LOADED reinterpret_cast<SvStream*>(sal_IntPtr(-3))
45 #define GRFMGR_AUTOSWAPSTREAM_TEMP reinterpret_cast<SvStream*>(sal_IntPtr(-2))
46 #define GRFMGR_AUTOSWAPSTREAM_NONE reinterpret_cast<SvStream*>(sal_IntPtr(-1))
49 enum class GraphicAdjustmentFlags
61 template<> struct typed_flags
<GraphicAdjustmentFlags
> : is_typed_flags
<GraphicAdjustmentFlags
, 0x1f> {};
66 GRAPHICDRAWMODE_STANDARD
= 0,
67 GRAPHICDRAWMODE_GREYS
= 1,
68 GRAPHICDRAWMODE_MONO
= 2,
69 GRAPHICDRAWMODE_WATERMARK
= 3
74 class BitmapWriteAccess
;
77 struct GrfSimpleCacheObj
;
80 class SVT_DLLPUBLIC GraphicAttr
85 BmpMirrorFlags mnMirrFlags
;
90 sal_uInt16 mnRotate10
;
97 sal_uInt8 mcTransparency
;
98 GraphicDrawMode meDrawMode
;
105 bool operator==( const GraphicAttr
& rAttr
) const;
106 bool operator!=( const GraphicAttr
& rAttr
) const { return !( *this == rAttr
); }
108 void SetDrawMode( GraphicDrawMode eDrawMode
) { meDrawMode
= eDrawMode
; }
109 GraphicDrawMode
GetDrawMode() const { return meDrawMode
; }
111 void SetMirrorFlags( BmpMirrorFlags nMirrFlags
) { mnMirrFlags
= nMirrFlags
; }
112 BmpMirrorFlags
GetMirrorFlags() const { return mnMirrFlags
; }
114 void SetCrop( long nLeft_100TH_MM
, long nTop_100TH_MM
, long nRight_100TH_MM
, long nBottom_100TH_MM
)
116 mnLeftCrop
= nLeft_100TH_MM
; mnTopCrop
= nTop_100TH_MM
;
117 mnRightCrop
= nRight_100TH_MM
; mnBottomCrop
= nBottom_100TH_MM
;
119 long GetLeftCrop() const { return mnLeftCrop
; }
120 long GetTopCrop() const { return mnTopCrop
; }
121 long GetRightCrop() const { return mnRightCrop
; }
122 long GetBottomCrop() const { return mnBottomCrop
; }
124 void SetRotation( sal_uInt16 nRotate10
) { mnRotate10
= nRotate10
; }
125 sal_uInt16
GetRotation() const { return mnRotate10
; }
127 void SetLuminance( short nLuminancePercent
) { mnLumPercent
= nLuminancePercent
; }
128 short GetLuminance() const { return mnLumPercent
; }
130 void SetContrast( short nContrastPercent
) { mnContPercent
= nContrastPercent
; }
131 short GetContrast() const { return mnContPercent
; }
133 void SetChannelR( short nChannelRPercent
) { mnRPercent
= nChannelRPercent
; }
134 short GetChannelR() const { return mnRPercent
; }
136 void SetChannelG( short nChannelGPercent
) { mnGPercent
= nChannelGPercent
; }
137 short GetChannelG() const { return mnGPercent
; }
139 void SetChannelB( short nChannelBPercent
) { mnBPercent
= nChannelBPercent
; }
140 short GetChannelB() const { return mnBPercent
; }
142 void SetGamma( double fGamma
) { mfGamma
= fGamma
; }
143 double GetGamma() const { return mfGamma
; }
145 void SetInvert( bool bInvert
) { mbInvert
= bInvert
; }
146 bool IsInvert() const { return mbInvert
; }
148 void SetTransparency( sal_uInt8 cTransparency
) { mcTransparency
= cTransparency
; }
149 sal_uInt8
GetTransparency() const { return mcTransparency
; }
151 bool IsSpecialDrawMode() const { return( meDrawMode
!= GRAPHICDRAWMODE_STANDARD
); }
152 bool IsMirrored() const { return mnMirrFlags
!= BmpMirrorFlags::NONE
; }
153 bool IsCropped() const
155 return( mnLeftCrop
!= 0 || mnTopCrop
!= 0 ||
156 mnRightCrop
!= 0 || mnBottomCrop
!= 0 );
158 bool IsRotated() const { return( ( mnRotate10
% 3600 ) != 0 ); }
159 bool IsTransparent() const { return( mcTransparency
> 0 ); }
160 bool IsAdjusted() const
162 return( mnLumPercent
!= 0 || mnContPercent
!= 0 || mnRPercent
!= 0 ||
163 mnGPercent
!= 0 || mnBPercent
!= 0 || mfGamma
!= 1.0 || mbInvert
);
166 friend SvStream
& WriteGraphicAttr( SvStream
& rOStm
, const GraphicAttr
& rAttr
);
167 friend SvStream
& ReadGraphicAttr( SvStream
& rIStm
, GraphicAttr
& rAttr
);
170 class SVT_DLLPUBLIC GraphicObject
: public SvDataCopyStream
172 friend class GraphicManager
;
173 friend class SdrGrafObj
;
177 static GraphicManager
* mpGlobalMgr
;
182 MapMode maPrefMapMode
;
183 sal_uLong mnSizeBytes
;
185 GraphicManager
* mpMgr
;
187 Link
<>* mpSwapStreamHdl
;
189 Timer
* mpSwapOutTimer
;
190 GrfSimpleCacheObj
* mpSimpleCache
;
191 sal_uLong mnAnimationLoopCount
;
193 // a unique increasing ID to be able to say which data change is older
194 sal_uLong mnDataChangeTimeStamp
;
196 bool mbAutoSwapped
: 1;
197 bool mbTransparent
: 1;
200 bool mbIsInSwapIn
: 1;
201 bool mbIsInSwapOut
: 1;
204 void SVT_DLLPRIVATE
ImplConstruct();
205 void SVT_DLLPRIVATE
ImplAssignGraphicData();
206 void SVT_DLLPRIVATE
ImplSetGraphicManager(
207 const GraphicManager
* pMgr
,
208 const OString
* pID
= NULL
,
209 const GraphicObject
* pCopyObj
= NULL
211 void SVT_DLLPRIVATE
ImplAutoSwapIn();
212 bool SVT_DLLPRIVATE
ImplIsAutoSwapped() const { return mbAutoSwapped
; }
213 bool SVT_DLLPRIVATE
ImplGetCropParams(
217 const GraphicAttr
* pAttr
,
218 tools::PolyPolygon
& rClipPolyPoly
,
219 bool& bRectClipRegion
222 /** Render a given number of tiles in an optimized way
224 This method recursively subdivides the tile rendering problem
225 in smaller parts, i.e. rendering output size x with few tiles
226 of size y, which in turn are generated from the original
227 bitmap in a recursive fashion. The subdivision size can be
228 controlled by the exponent argument, which specifies the
229 minimal number of smaller tiles used in one recursion
230 step. The resulting tile size is given as the integer number
231 of repetitions of the original bitmap along x and y. As the
232 exponent need not necessarily divide these numbers without
233 remainder, the repetition counts are effectively converted to
234 base-exponent numbers, where each place denotes the number of
235 times the corresponding tile size is rendered.
238 Virtual device to render everything into
241 Number of repetitions per subdivision step, _must_ be greater than 1
244 Number of original tiles to generate in x direction
247 Number of original tiles to generate in y direction
249 @param rTileSizePixel
250 Size in pixel of the original tile bitmap to render it in
253 Graphic attributes to be used for rendering
256 Graphic flags to be used for rendering
259 Current output point for this recursion level (should start with (0,0))
261 @return true, if everything was successfully rendered.
263 bool SVT_DLLPRIVATE
ImplRenderTempTile(
264 VirtualDevice
& rVDev
,
268 const Size
& rTileSizePixel
,
269 const GraphicAttr
* pAttr
,
270 GraphicManagerDrawFlags nFlags
273 /// internally called by ImplRenderTempTile()
274 bool SVT_DLLPRIVATE
ImplRenderTileRecursive(
275 VirtualDevice
& rVDev
,
280 int nRemainderTilesX
,
281 int nRemainderTilesY
,
282 const Size
& rTileSizePixel
,
283 const GraphicAttr
* pAttr
,
284 GraphicManagerDrawFlags nFlags
,
285 ImplTileInfo
& rTileInfo
288 bool SVT_DLLPRIVATE
ImplDrawTiled(
290 const Rectangle
& rArea
,
291 const Size
& rSizePixel
,
293 const GraphicAttr
* pAttr
,
294 GraphicManagerDrawFlags nFlags
,
298 bool SVT_DLLPRIVATE
ImplDrawTiled(
303 const Size
& rTileSize
,
304 const GraphicAttr
* pAttr
,
305 GraphicManagerDrawFlags nFlags
308 void SVT_DLLPRIVATE
ImplTransformBitmap(
310 const GraphicAttr
& rAttr
,
311 const Size
& rCropLeftTop
,
312 const Size
& rCropRightBottom
,
313 const Rectangle
& rCropRect
,
314 const Size
& rDstSize
,
318 DECL_LINK_TYPED( ImplAutoSwapOutHdl
, Timer
*, void );
320 // restart SwapOut timer; this is like touching in a cache to reset to the full timeout value
321 void SVT_DLLPRIVATE
restartSwapOutTimer() const;
323 // Handle evtl. needed AfterDataChanges, needs to be called when new
324 // graphic data is swapped in/added to the GraphicManager
325 void SVT_DLLPRIVATE
ImplAfterDataChange();
328 void GraphicManagerDestroyed();
329 SvStream
* GetSwapStream() const;
332 virtual void Load( SvStream
& ) SAL_OVERRIDE
;
333 virtual void Save( SvStream
& ) SAL_OVERRIDE
;
334 virtual void Assign( const SvDataCopyStream
& ) SAL_OVERRIDE
;
340 GraphicObject( const GraphicManager
* pMgr
= NULL
);
341 GraphicObject( const Graphic
& rGraphic
, const GraphicManager
* pMgr
= NULL
);
342 GraphicObject( const GraphicObject
& rCacheObj
, const GraphicManager
* pMgr
= NULL
);
343 explicit GraphicObject( const OString
& rUniqueID
, const GraphicManager
* pMgr
= NULL
);
344 virtual ~GraphicObject();
346 GraphicObject
& operator=( const GraphicObject
& rCacheObj
);
347 bool operator==( const GraphicObject
& rCacheObj
) const;
348 bool operator!=( const GraphicObject
& rCacheObj
) const { return !( *this == rCacheObj
); }
350 bool HasSwapStreamHdl() const { return( mpSwapStreamHdl
!= NULL
&& mpSwapStreamHdl
->IsSet() ); }
351 void SetSwapStreamHdl();
352 void SetSwapStreamHdl(const Link
<>& rHdl
);
353 sal_uLong
GetSwapOutTimeout() const { return( mpSwapOutTimer
? mpSwapOutTimer
->GetTimeout() : 0 ); }
355 void FireSwapInRequest();
356 void FireSwapOutRequest();
358 GraphicManager
& GetGraphicManager() const { return *mpMgr
; }
364 const GraphicAttr
* pAttr
= NULL
,
365 GraphicManagerDrawFlags nFlags
= GraphicManagerDrawFlags::STANDARD
368 const Graphic
& GetGraphic() const;
369 void SetGraphic( const Graphic
& rGraphic
, const GraphicObject
* pCopyObj
= 0);
370 void SetGraphic( const Graphic
& rGraphic
, const OUString
& rLink
);
372 /** Get graphic transformed according to given attributes
374 This method returns a Graphic transformed, cropped and scaled
375 to the given parameters, ready to be rendered to printer or
376 display. The returned graphic has the same visual appearance
377 as if it had been drawn via GraphicObject::Draw() to a
378 specific output device.
381 Desired output size in logical coordinates. The mapmode to
382 interpret these logical coordinates in is given by the second
386 Mapmode the output should be interpreted in. This is used to
387 interpret rDestSize, to set the appropriate PrefMapMode on the
388 returned Graphic, and to deal correctly with metafile graphics.
391 Graphic attributes used to transform the graphic. This
392 includes cropping, rotation, mirroring, and various color
393 adjustment parameters.
395 @return the readily transformed Graphic
397 Graphic
GetTransformedGraphic(
398 const Size
& rDestSize
,
399 const MapMode
& rDestMap
,
400 const GraphicAttr
& rAttr
402 Graphic
GetTransformedGraphic( const GraphicAttr
* pAttr
= NULL
) const; // TODO: Change to Impl
404 void SetAttr( const GraphicAttr
& rAttr
);
405 const GraphicAttr
& GetAttr() const { return maAttr
; }
407 bool HasLink() const { return !maLink
.isEmpty(); }
409 void SetLink( const OUString
& rLink
);
410 OUString
GetLink() const { return maLink
; }
412 bool HasUserData() const { return !maUserData
.isEmpty(); }
414 void SetUserData( const OUString
& rUserData
);
415 OUString
GetUserData() const { return maUserData
; }
417 OString
GetUniqueID() const;
419 GraphicType
GetType() const { return meType
; }
420 const Size
& GetPrefSize() const { return maPrefSize
; }
421 const MapMode
& GetPrefMapMode() const { return maPrefMapMode
; }
422 sal_uLong
GetSizeBytes() const { return mnSizeBytes
; }
423 bool IsTransparent() const { return mbTransparent
; }
424 bool IsAlpha() const { return mbAlpha
; }
425 bool IsAnimated() const { return mbAnimated
; }
426 bool IsEPS() const { return mbEPS
; }
428 Link
<> GetAnimationNotifyHdl() const { return GetGraphic().GetAnimationNotifyHdl(); }
431 bool SwapOut( SvStream
* pOStm
);
434 bool IsInSwapIn() const { return mbIsInSwapIn
; }
435 bool IsInSwapOut() const { return mbIsInSwapOut
; }
436 bool IsSwappedOut() const { return( mbAutoSwapped
|| maGraphic
.IsSwapOut() ); }
442 const GraphicAttr
* pAttr
= NULL
,
443 GraphicManagerDrawFlags nFlags
= GraphicManagerDrawFlags::STANDARD
446 /** Draw the graphic repeatedly into the given output rectangle
449 OutputDevice where the rendering should take place
452 The output area that is filled with tiled instances of this graphic
455 The actual size of a single tile
458 Offset from the left, top position of rArea, where to start
459 the tiling. The upper left corner of the graphic tilings will
460 virtually start at this position. Concretely, only that many
461 tiles are drawn to completely fill the given output area.
467 Optional rendering flags
469 @param nTileCacheSize1D
470 Optional dimension of the generated cache tiles. The pOut sees
471 a number of tile draws, which have approximately
472 nTileCacheSize1D times nTileCacheSize1D bitmap sizes if the
473 tile bitmap is smaller. Otherwise, the tile is drawn as
474 is. This is useful if e.g. you want only a few, very large
475 bitmap drawings appear on the outdev.
477 @return sal_True, if drawing completed successfully
481 const Rectangle
& rArea
,
484 const GraphicAttr
* pAttr
= NULL
,
485 GraphicManagerDrawFlags nFlags
= GraphicManagerDrawFlags::STANDARD
,
486 int nTileCacheSize1D
=128
493 long nExtraData
= 0L,
494 const GraphicAttr
* pAttr
= NULL
,
495 GraphicManagerDrawFlags nFlags
= GraphicManagerDrawFlags::STANDARD
,
496 OutputDevice
* pFirstFrameOutDev
= NULL
499 void StopAnimation( OutputDevice
* pOut
= NULL
, long nExtraData
= 0L );
501 friend SvStream
& WriteGraphicObject( SvStream
& rOStm
, const GraphicObject
& rGraphicObj
);
502 friend SvStream
& ReadGraphicObject( SvStream
& rIStm
, GraphicObject
& rGraphicObj
);
504 static GraphicObject
CreateGraphicObjectFromURL( const OUString
&rURL
);
505 // will inspect an object ( e.g. a control ) for any 'ImageURL'
506 // properties and return these in a vector. Note: this implementation
507 // will cater for XNameContainer objects and deepinspect any containees
509 static void InspectForGraphicObjectImageURL( const ::com::sun::star::uno::Reference
< ::com::sun::star::uno::XInterface
>& rxIf
, std::vector
< OUString
>& rvEmbedImgUrls
);
511 // create CropScaling information
512 // fWidth, fHeight: object size
513 // f*Crop: crop values relative to original bitmap size
514 basegfx::B2DVector
calculateCropScaling(
520 double fBottomCrop
) const;
523 sal_uLong
GetDataChangeTimeStamp() const { return mnDataChangeTimeStamp
; }
526 typedef ::std::vector
< GraphicObject
* > GraphicObjectList_impl
;
528 class SVT_DLLPUBLIC GraphicManager
530 friend class GraphicObject
;
531 friend class GraphicDisplayCacheEntry
;
535 GraphicObjectList_impl maObjList
;
536 sal_uLong mnUsedSize
; // currently used memory footprint of all swapped in graphics
537 GraphicCache
* mpCache
;
539 GraphicManager( const GraphicManager
& ) {}
540 GraphicManager
& operator=( const GraphicManager
& ) { return *this; }
542 bool SVT_DLLPRIVATE
ImplDraw(
547 const GraphicAttr
& rAttr
,
548 const GraphicManagerDrawFlags nFlags
,
552 static bool SVT_DLLPRIVATE
ImplCreateOutput(
556 const BitmapEx
& rBmpEx
,
557 const GraphicAttr
& rAttr
,
558 const GraphicManagerDrawFlags nFlags
,
559 BitmapEx
* pBmpEx
= NULL
561 static bool SVT_DLLPRIVATE
ImplCreateOutput(
565 const GDIMetaFile
& rMtf
,
566 const GraphicAttr
& rAttr
,
567 const GraphicManagerDrawFlags nFlags
,
568 GDIMetaFile
& rOutMtf
,
572 static void SVT_DLLPRIVATE
ImplAdjust(
574 const GraphicAttr
& rAttr
,
575 GraphicAdjustmentFlags nAdjustmentFlags
577 static void SVT_DLLPRIVATE
ImplAdjust(
579 const GraphicAttr
& rAttr
,
580 GraphicAdjustmentFlags nAdjustmentFlags
582 static void SVT_DLLPRIVATE
ImplAdjust(
583 Animation
& rAnimation
,
584 const GraphicAttr
& rAttr
,
585 GraphicAdjustmentFlags nAdjustmentFlags
588 static void SVT_DLLPRIVATE
ImplDraw(
592 const GDIMetaFile
& rMtf
,
593 const GraphicAttr
& rAttr
596 // Only used by GraphicObject's Ctor's and Dtor's
597 void SVT_DLLPRIVATE
ImplRegisterObj(
598 const GraphicObject
& rObj
,
599 Graphic
& rSubstitute
,
600 const OString
* pID
= NULL
,
601 const GraphicObject
* pCopyObj
= NULL
603 void SVT_DLLPRIVATE
ImplUnregisterObj( const GraphicObject
& rObj
);
604 inline bool SVT_DLLPRIVATE
ImplHasObjects() const { return !maObjList
.empty(); }
606 // Only used in swap case by GraphicObject
607 void SVT_DLLPRIVATE
ImplGraphicObjectWasSwappedOut( const GraphicObject
& rObj
);
608 void SVT_DLLPRIVATE
ImplGraphicObjectWasSwappedIn( const GraphicObject
& rObj
);
610 OString SVT_DLLPRIVATE
ImplGetUniqueID( const GraphicObject
& rObj
) const;
612 // This method allows to check memory footprint for all currently swapped in GraphicObjects on this GraphicManager
613 // which are based on Bitmaps. This is needed on 32Bit systems and only does something on those systems. The problem
614 // to solve is that normally the SwapOut is timer-driven, but even with short timer settings there are situations
615 // where this does not trigger - or in other words: A maximum limitation for GraphicManagers was not in place before.
616 // For 32Bit systems this leads to situations where graphics will be missing. This method will actively swap out
617 // the longest swapped in graphics until a maximum memory boundary (derived from user settings in tools/options/memory)
618 // is no longer exceeded
619 void SVT_DLLPRIVATE
ImplCheckSizeOfSwappedInGraphics(const GraphicObject
* pGraphicToIgnore
);
622 GraphicManager( sal_uLong nCacheSize
= 10000000UL, sal_uLong nMaxObjCacheSize
= 2400000UL );
625 void SetMaxCacheSize( sal_uLong nNewCacheSize
);
626 sal_uLong
GetMaxCacheSize() const;
628 void SetMaxObjCacheSize(
629 sal_uLong nNewMaxObjSize
,
630 bool bDestroyGreaterCached
= false
633 void SetCacheTimeout( sal_uLong nTimeoutSeconds
);
639 const GraphicObject
& rObj
,
640 const GraphicAttr
& rAttr
648 const GraphicAttr
& rAttr
,
649 const GraphicManagerDrawFlags nFlags
,
654 #endif // INCLUDED_SVTOOLS_GRFMGR_HXX
656 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */