15900e8ecSAndrew Rist /************************************************************** 2cdf0e10cSrcweir * 35900e8ecSAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one 45900e8ecSAndrew Rist * or more contributor license agreements. See the NOTICE file 55900e8ecSAndrew Rist * distributed with this work for additional information 65900e8ecSAndrew Rist * regarding copyright ownership. The ASF licenses this file 75900e8ecSAndrew Rist * to you under the Apache License, Version 2.0 (the 85900e8ecSAndrew Rist * "License"); you may not use this file except in compliance 95900e8ecSAndrew Rist * with the License. You may obtain a copy of the License at 10cdf0e10cSrcweir * 115900e8ecSAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0 12cdf0e10cSrcweir * 135900e8ecSAndrew Rist * Unless required by applicable law or agreed to in writing, 145900e8ecSAndrew Rist * software distributed under the License is distributed on an 155900e8ecSAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 165900e8ecSAndrew Rist * KIND, either express or implied. See the License for the 175900e8ecSAndrew Rist * specific language governing permissions and limitations 185900e8ecSAndrew Rist * under the License. 19cdf0e10cSrcweir * 205900e8ecSAndrew Rist *************************************************************/ 215900e8ecSAndrew Rist 225900e8ecSAndrew Rist 23cdf0e10cSrcweir 24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove 25cdf0e10cSrcweir #include "precompiled_svtools.hxx" 26cdf0e10cSrcweir 27cdf0e10cSrcweir #include <vos/timer.hxx> 28cdf0e10cSrcweir #include <tools/debug.hxx> 29cdf0e10cSrcweir #include <vcl/outdev.hxx> 30cdf0e10cSrcweir #include <tools/poly.hxx> 31cdf0e10cSrcweir #include "grfcache.hxx" 32*ddde725dSArmin Le Grand #include <rtl/crc.h> 33cdf0e10cSrcweir #include <memory> 34cdf0e10cSrcweir 35cdf0e10cSrcweir // ----------- 36cdf0e10cSrcweir // - Defines - 37cdf0e10cSrcweir // ----------- 38cdf0e10cSrcweir 39cdf0e10cSrcweir #define RELEASE_TIMEOUT 10000 40cdf0e10cSrcweir #define MAX_BMP_EXTENT 4096 41cdf0e10cSrcweir 42cdf0e10cSrcweir // ----------- 43cdf0e10cSrcweir // - statics - 44cdf0e10cSrcweir // ----------- 45cdf0e10cSrcweir 46cdf0e10cSrcweir static const char aHexData[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' }; 47cdf0e10cSrcweir 48cdf0e10cSrcweir // ------------- 49cdf0e10cSrcweir // - GraphicID - 50cdf0e10cSrcweir // ------------- 51cdf0e10cSrcweir 52cdf0e10cSrcweir class GraphicID 53cdf0e10cSrcweir { 54cdf0e10cSrcweir private: 55cdf0e10cSrcweir 56cdf0e10cSrcweir sal_uInt32 mnID1; 57cdf0e10cSrcweir sal_uInt32 mnID2; 58cdf0e10cSrcweir sal_uInt32 mnID3; 59cdf0e10cSrcweir sal_uInt32 mnID4; 60cdf0e10cSrcweir 61cdf0e10cSrcweir GraphicID(); 62cdf0e10cSrcweir 63cdf0e10cSrcweir public: 64cdf0e10cSrcweir 65cdf0e10cSrcweir 66cdf0e10cSrcweir GraphicID( const GraphicObject& rObj ); 67cdf0e10cSrcweir ~GraphicID() {} 68cdf0e10cSrcweir 69cdf0e10cSrcweir sal_Bool operator==( const GraphicID& rID ) const 70cdf0e10cSrcweir { 71cdf0e10cSrcweir return( rID.mnID1 == mnID1 && rID.mnID2 == mnID2 && 72cdf0e10cSrcweir rID.mnID3 == mnID3 && rID.mnID4 == mnID4 ); 73cdf0e10cSrcweir } 74cdf0e10cSrcweir 75cdf0e10cSrcweir ByteString GetIDString() const; 76cdf0e10cSrcweir sal_Bool IsEmpty() const { return( 0 == mnID4 ); } 77cdf0e10cSrcweir }; 78cdf0e10cSrcweir 79cdf0e10cSrcweir // ----------------------------------------------------------------------------- 80cdf0e10cSrcweir 81cdf0e10cSrcweir GraphicID::GraphicID( const GraphicObject& rObj ) 82cdf0e10cSrcweir { 83cdf0e10cSrcweir const Graphic& rGraphic = rObj.GetGraphic(); 84cdf0e10cSrcweir 85cdf0e10cSrcweir mnID1 = ( (sal_uLong) rGraphic.GetType() ) << 28; 86cdf0e10cSrcweir 87cdf0e10cSrcweir switch( rGraphic.GetType() ) 88cdf0e10cSrcweir { 89cdf0e10cSrcweir case( GRAPHIC_BITMAP ): 90cdf0e10cSrcweir { 91*ddde725dSArmin Le Grand if(rGraphic.getSvgData().get()) 92*ddde725dSArmin Le Grand { 93*ddde725dSArmin Le Grand const SvgDataPtr& rSvgDataPtr = rGraphic.getSvgData(); 94*ddde725dSArmin Le Grand const basegfx::B2DRange& rRange = rSvgDataPtr->getRange(); 95*ddde725dSArmin Le Grand 96*ddde725dSArmin Le Grand mnID1 |= rSvgDataPtr->getSvgDataArrayLength(); 97*ddde725dSArmin Le Grand mnID2 = basegfx::fround(rRange.getWidth()); 98*ddde725dSArmin Le Grand mnID3 = basegfx::fround(rRange.getHeight()); 99*ddde725dSArmin Le Grand mnID4 = rtl_crc32(0, rSvgDataPtr->getSvgDataArray().get(), rSvgDataPtr->getSvgDataArrayLength()); 100*ddde725dSArmin Le Grand } 101*ddde725dSArmin Le Grand else if( rGraphic.IsAnimated() ) 102cdf0e10cSrcweir { 103cdf0e10cSrcweir const Animation aAnimation( rGraphic.GetAnimation() ); 104cdf0e10cSrcweir 105cdf0e10cSrcweir mnID1 |= ( aAnimation.Count() & 0x0fffffff ); 106cdf0e10cSrcweir mnID2 = aAnimation.GetDisplaySizePixel().Width(); 107cdf0e10cSrcweir mnID3 = aAnimation.GetDisplaySizePixel().Height(); 108cdf0e10cSrcweir mnID4 = rGraphic.GetChecksum(); 109cdf0e10cSrcweir } 110cdf0e10cSrcweir else 111cdf0e10cSrcweir { 112cdf0e10cSrcweir const BitmapEx aBmpEx( rGraphic.GetBitmapEx() ); 113cdf0e10cSrcweir 114cdf0e10cSrcweir mnID1 |= ( ( ( (sal_uLong) aBmpEx.GetTransparentType() << 8 ) | ( aBmpEx.IsAlpha() ? 1 : 0 ) ) & 0x0fffffff ); 115cdf0e10cSrcweir mnID2 = aBmpEx.GetSizePixel().Width(); 116cdf0e10cSrcweir mnID3 = aBmpEx.GetSizePixel().Height(); 117cdf0e10cSrcweir mnID4 = rGraphic.GetChecksum(); 118cdf0e10cSrcweir } 119cdf0e10cSrcweir } 120cdf0e10cSrcweir break; 121cdf0e10cSrcweir 122cdf0e10cSrcweir case( GRAPHIC_GDIMETAFILE ): 123cdf0e10cSrcweir { 124cdf0e10cSrcweir const GDIMetaFile aMtf( rGraphic.GetGDIMetaFile() ); 125cdf0e10cSrcweir 126cdf0e10cSrcweir mnID1 |= ( aMtf.GetActionCount() & 0x0fffffff ); 127cdf0e10cSrcweir mnID2 = aMtf.GetPrefSize().Width(); 128cdf0e10cSrcweir mnID3 = aMtf.GetPrefSize().Height(); 129cdf0e10cSrcweir mnID4 = rGraphic.GetChecksum(); 130cdf0e10cSrcweir } 131cdf0e10cSrcweir break; 132cdf0e10cSrcweir 133cdf0e10cSrcweir default: 134cdf0e10cSrcweir mnID2 = mnID3 = mnID4 = 0; 135cdf0e10cSrcweir break; 136cdf0e10cSrcweir } 137cdf0e10cSrcweir } 138cdf0e10cSrcweir 139cdf0e10cSrcweir // ----------------------------------------------------------------------------- 140cdf0e10cSrcweir 141cdf0e10cSrcweir ByteString GraphicID::GetIDString() const 142cdf0e10cSrcweir { 143cdf0e10cSrcweir ByteString aHexStr; 144cdf0e10cSrcweir sal_Char* pStr = aHexStr.AllocBuffer( 32 ); 145cdf0e10cSrcweir sal_Int32 nShift; 146cdf0e10cSrcweir 147cdf0e10cSrcweir for( nShift = 28; nShift >= 0; nShift -= 4 ) 148cdf0e10cSrcweir *pStr++ = aHexData[ ( mnID1 >> (sal_uInt32) nShift ) & 0xf ]; 149cdf0e10cSrcweir 150cdf0e10cSrcweir for( nShift = 28; nShift >= 0; nShift -= 4 ) 151cdf0e10cSrcweir *pStr++ = aHexData[ ( mnID2 >> (sal_uInt32) nShift ) & 0xf ]; 152cdf0e10cSrcweir 153cdf0e10cSrcweir for( nShift = 28; nShift >= 0; nShift -= 4 ) 154cdf0e10cSrcweir *pStr++ = aHexData[ ( mnID3 >> (sal_uInt32) nShift ) & 0xf ]; 155cdf0e10cSrcweir 156cdf0e10cSrcweir for( nShift = 28; nShift >= 0; nShift -= 4 ) 157cdf0e10cSrcweir *pStr++ = aHexData[ ( mnID4 >> (sal_uInt32) nShift ) & 0xf ]; 158cdf0e10cSrcweir 159cdf0e10cSrcweir return aHexStr; 160cdf0e10cSrcweir } 161cdf0e10cSrcweir 162cdf0e10cSrcweir // --------------------- 163cdf0e10cSrcweir // - GraphicCacheEntry - 164cdf0e10cSrcweir // --------------------- 165cdf0e10cSrcweir 166cdf0e10cSrcweir class GraphicCacheEntry 167cdf0e10cSrcweir { 168cdf0e10cSrcweir private: 169cdf0e10cSrcweir 170cdf0e10cSrcweir List maGraphicObjectList; 171cdf0e10cSrcweir GraphicID maID; 172cdf0e10cSrcweir GfxLink maGfxLink; 173cdf0e10cSrcweir BitmapEx* mpBmpEx; 174cdf0e10cSrcweir GDIMetaFile* mpMtf; 175cdf0e10cSrcweir Animation* mpAnimation; 176cdf0e10cSrcweir sal_Bool mbSwappedAll; 177cdf0e10cSrcweir 178*ddde725dSArmin Le Grand // SvgData support 179*ddde725dSArmin Le Grand SvgDataPtr maSvgData; 180*ddde725dSArmin Le Grand 181cdf0e10cSrcweir sal_Bool ImplInit( const GraphicObject& rObj ); 182cdf0e10cSrcweir sal_Bool ImplMatches( const GraphicObject& rObj ) const { return( GraphicID( rObj ) == maID ); } 183cdf0e10cSrcweir void ImplFillSubstitute( Graphic& rSubstitute ); 184cdf0e10cSrcweir 185cdf0e10cSrcweir public: 186cdf0e10cSrcweir 187cdf0e10cSrcweir GraphicCacheEntry( const GraphicObject& rObj ); 188cdf0e10cSrcweir ~GraphicCacheEntry(); 189cdf0e10cSrcweir 190cdf0e10cSrcweir const GraphicID& GetID() const { return maID; } 191cdf0e10cSrcweir 192cdf0e10cSrcweir void AddGraphicObjectReference( const GraphicObject& rObj, Graphic& rSubstitute ); 193cdf0e10cSrcweir sal_Bool ReleaseGraphicObjectReference( const GraphicObject& rObj ); 194cdf0e10cSrcweir sal_uLong GetGraphicObjectReferenceCount() { return maGraphicObjectList.Count(); } 195cdf0e10cSrcweir sal_Bool HasGraphicObjectReference( const GraphicObject& rObj ); 196cdf0e10cSrcweir 197cdf0e10cSrcweir void TryToSwapIn(); 198cdf0e10cSrcweir void GraphicObjectWasSwappedOut( const GraphicObject& rObj ); 199cdf0e10cSrcweir sal_Bool FillSwappedGraphicObject( const GraphicObject& rObj, Graphic& rSubstitute ); 200cdf0e10cSrcweir void GraphicObjectWasSwappedIn( const GraphicObject& rObj ); 201cdf0e10cSrcweir }; 202cdf0e10cSrcweir 203cdf0e10cSrcweir // ----------------------------------------------------------------------------- 204cdf0e10cSrcweir 205cdf0e10cSrcweir GraphicCacheEntry::GraphicCacheEntry( const GraphicObject& rObj ) : 206cdf0e10cSrcweir maID ( rObj ), 207cdf0e10cSrcweir mpBmpEx ( NULL ), 208cdf0e10cSrcweir mpMtf ( NULL ), 209cdf0e10cSrcweir mpAnimation ( NULL ), 210*ddde725dSArmin Le Grand mbSwappedAll ( true ) 211cdf0e10cSrcweir { 212*ddde725dSArmin Le Grand mbSwappedAll = !ImplInit(rObj); 213cdf0e10cSrcweir maGraphicObjectList.Insert( (void*) &rObj, LIST_APPEND ); 214cdf0e10cSrcweir } 215cdf0e10cSrcweir 216cdf0e10cSrcweir // ----------------------------------------------------------------------------- 217cdf0e10cSrcweir 218cdf0e10cSrcweir GraphicCacheEntry::~GraphicCacheEntry() 219cdf0e10cSrcweir { 220cdf0e10cSrcweir DBG_ASSERT( !maGraphicObjectList.Count(), "GraphicCacheEntry::~GraphicCacheEntry(): Not all GraphicObjects are removed from this entry" ); 221cdf0e10cSrcweir 222cdf0e10cSrcweir delete mpBmpEx; 223cdf0e10cSrcweir delete mpMtf; 224cdf0e10cSrcweir delete mpAnimation; 225cdf0e10cSrcweir } 226cdf0e10cSrcweir 227cdf0e10cSrcweir // ----------------------------------------------------------------------------- 228cdf0e10cSrcweir 229cdf0e10cSrcweir sal_Bool GraphicCacheEntry::ImplInit( const GraphicObject& rObj ) 230cdf0e10cSrcweir { 231cdf0e10cSrcweir sal_Bool bRet; 232cdf0e10cSrcweir 233cdf0e10cSrcweir if( !rObj.IsSwappedOut() ) 234cdf0e10cSrcweir { 235cdf0e10cSrcweir const Graphic& rGraphic = rObj.GetGraphic(); 236cdf0e10cSrcweir 237cdf0e10cSrcweir if( mpBmpEx ) 238cdf0e10cSrcweir delete mpBmpEx, mpBmpEx = NULL; 239cdf0e10cSrcweir 240cdf0e10cSrcweir if( mpMtf ) 241cdf0e10cSrcweir delete mpMtf, mpMtf = NULL; 242cdf0e10cSrcweir 243cdf0e10cSrcweir if( mpAnimation ) 244cdf0e10cSrcweir delete mpAnimation, mpAnimation = NULL; 245cdf0e10cSrcweir 246cdf0e10cSrcweir switch( rGraphic.GetType() ) 247cdf0e10cSrcweir { 248cdf0e10cSrcweir case( GRAPHIC_BITMAP ): 249cdf0e10cSrcweir { 250*ddde725dSArmin Le Grand if(rGraphic.getSvgData().get()) 251*ddde725dSArmin Le Grand { 252*ddde725dSArmin Le Grand maSvgData = rGraphic.getSvgData(); 253*ddde725dSArmin Le Grand } 254*ddde725dSArmin Le Grand else if( rGraphic.IsAnimated() ) 255*ddde725dSArmin Le Grand { 256cdf0e10cSrcweir mpAnimation = new Animation( rGraphic.GetAnimation() ); 257*ddde725dSArmin Le Grand } 258cdf0e10cSrcweir else 259*ddde725dSArmin Le Grand { 260cdf0e10cSrcweir mpBmpEx = new BitmapEx( rGraphic.GetBitmapEx() ); 261cdf0e10cSrcweir } 262*ddde725dSArmin Le Grand } 263cdf0e10cSrcweir break; 264cdf0e10cSrcweir 265cdf0e10cSrcweir case( GRAPHIC_GDIMETAFILE ): 266cdf0e10cSrcweir { 267cdf0e10cSrcweir mpMtf = new GDIMetaFile( rGraphic.GetGDIMetaFile() ); 268cdf0e10cSrcweir } 269cdf0e10cSrcweir break; 270cdf0e10cSrcweir 271cdf0e10cSrcweir default: 272cdf0e10cSrcweir DBG_ASSERT( GetID().IsEmpty(), "GraphicCacheEntry::ImplInit: Could not initialize graphic! (=>KA)" ); 273cdf0e10cSrcweir break; 274cdf0e10cSrcweir } 275cdf0e10cSrcweir 276cdf0e10cSrcweir if( rGraphic.IsLink() ) 277cdf0e10cSrcweir maGfxLink = ( (Graphic&) rGraphic ).GetLink(); 278cdf0e10cSrcweir else 279cdf0e10cSrcweir maGfxLink = GfxLink(); 280cdf0e10cSrcweir 281cdf0e10cSrcweir bRet = sal_True; 282cdf0e10cSrcweir } 283cdf0e10cSrcweir else 284cdf0e10cSrcweir bRet = sal_False; 285cdf0e10cSrcweir 286cdf0e10cSrcweir return bRet; 287cdf0e10cSrcweir } 288cdf0e10cSrcweir 289cdf0e10cSrcweir // ----------------------------------------------------------------------------- 290cdf0e10cSrcweir 291cdf0e10cSrcweir void GraphicCacheEntry::ImplFillSubstitute( Graphic& rSubstitute ) 292cdf0e10cSrcweir { 293cdf0e10cSrcweir // create substitute for graphic; 294cdf0e10cSrcweir const Size aPrefSize( rSubstitute.GetPrefSize() ); 295cdf0e10cSrcweir const MapMode aPrefMapMode( rSubstitute.GetPrefMapMode() ); 296cdf0e10cSrcweir const Link aAnimationNotifyHdl( rSubstitute.GetAnimationNotifyHdl() ); 297cdf0e10cSrcweir const String aDocFileName( rSubstitute.GetDocFileName() ); 298cdf0e10cSrcweir const sal_uLong nDocFilePos = rSubstitute.GetDocFilePos(); 299cdf0e10cSrcweir const GraphicType eOldType = rSubstitute.GetType(); 300cdf0e10cSrcweir const sal_Bool bDefaultType = ( rSubstitute.GetType() == GRAPHIC_DEFAULT ); 301cdf0e10cSrcweir 302cdf0e10cSrcweir if( rSubstitute.IsLink() && ( GFX_LINK_TYPE_NONE == maGfxLink.GetType() ) ) 303cdf0e10cSrcweir maGfxLink = rSubstitute.GetLink(); 304cdf0e10cSrcweir 305*ddde725dSArmin Le Grand if(maSvgData.get()) 306*ddde725dSArmin Le Grand { 307*ddde725dSArmin Le Grand rSubstitute = maSvgData; 308*ddde725dSArmin Le Grand } 309*ddde725dSArmin Le Grand else if( mpBmpEx ) 310*ddde725dSArmin Le Grand { 311cdf0e10cSrcweir rSubstitute = *mpBmpEx; 312*ddde725dSArmin Le Grand } 313cdf0e10cSrcweir else if( mpAnimation ) 314*ddde725dSArmin Le Grand { 315cdf0e10cSrcweir rSubstitute = *mpAnimation; 316*ddde725dSArmin Le Grand } 317cdf0e10cSrcweir else if( mpMtf ) 318*ddde725dSArmin Le Grand { 319cdf0e10cSrcweir rSubstitute = *mpMtf; 320*ddde725dSArmin Le Grand } 321cdf0e10cSrcweir else 322*ddde725dSArmin Le Grand { 323cdf0e10cSrcweir rSubstitute.Clear(); 324*ddde725dSArmin Le Grand } 325cdf0e10cSrcweir 326cdf0e10cSrcweir if( eOldType != GRAPHIC_NONE ) 327cdf0e10cSrcweir { 328cdf0e10cSrcweir rSubstitute.SetPrefSize( aPrefSize ); 329cdf0e10cSrcweir rSubstitute.SetPrefMapMode( aPrefMapMode ); 330cdf0e10cSrcweir rSubstitute.SetAnimationNotifyHdl( aAnimationNotifyHdl ); 331cdf0e10cSrcweir rSubstitute.SetDocFileName( aDocFileName, nDocFilePos ); 332cdf0e10cSrcweir } 333cdf0e10cSrcweir 334cdf0e10cSrcweir if( GFX_LINK_TYPE_NONE != maGfxLink.GetType() ) 335*ddde725dSArmin Le Grand { 336cdf0e10cSrcweir rSubstitute.SetLink( maGfxLink ); 337*ddde725dSArmin Le Grand } 338cdf0e10cSrcweir 339cdf0e10cSrcweir if( bDefaultType ) 340*ddde725dSArmin Le Grand { 341cdf0e10cSrcweir rSubstitute.SetDefaultType(); 342cdf0e10cSrcweir } 343*ddde725dSArmin Le Grand } 344cdf0e10cSrcweir 345cdf0e10cSrcweir // ----------------------------------------------------------------------------- 346cdf0e10cSrcweir 347cdf0e10cSrcweir void GraphicCacheEntry::AddGraphicObjectReference( const GraphicObject& rObj, Graphic& rSubstitute ) 348cdf0e10cSrcweir { 349cdf0e10cSrcweir if( mbSwappedAll ) 350cdf0e10cSrcweir mbSwappedAll = !ImplInit( rObj ); 351cdf0e10cSrcweir 352cdf0e10cSrcweir ImplFillSubstitute( rSubstitute ); 353cdf0e10cSrcweir maGraphicObjectList.Insert( (void*) &rObj, LIST_APPEND ); 354cdf0e10cSrcweir } 355cdf0e10cSrcweir 356cdf0e10cSrcweir // ----------------------------------------------------------------------------- 357cdf0e10cSrcweir 358cdf0e10cSrcweir sal_Bool GraphicCacheEntry::ReleaseGraphicObjectReference( const GraphicObject& rObj ) 359cdf0e10cSrcweir { 360cdf0e10cSrcweir sal_Bool bRet = sal_False; 361cdf0e10cSrcweir 362cdf0e10cSrcweir for( void* pObj = maGraphicObjectList.First(); !bRet && pObj; pObj = maGraphicObjectList.Next() ) 363cdf0e10cSrcweir { 364cdf0e10cSrcweir if( &rObj == (GraphicObject*) pObj ) 365cdf0e10cSrcweir { 366cdf0e10cSrcweir maGraphicObjectList.Remove( pObj ); 367cdf0e10cSrcweir bRet = sal_True; 368cdf0e10cSrcweir } 369cdf0e10cSrcweir } 370cdf0e10cSrcweir 371cdf0e10cSrcweir return bRet; 372cdf0e10cSrcweir } 373cdf0e10cSrcweir 374cdf0e10cSrcweir // ----------------------------------------------------------------------------- 375cdf0e10cSrcweir 376cdf0e10cSrcweir sal_Bool GraphicCacheEntry::HasGraphicObjectReference( const GraphicObject& rObj ) 377cdf0e10cSrcweir { 378cdf0e10cSrcweir sal_Bool bRet = sal_False; 379cdf0e10cSrcweir 380cdf0e10cSrcweir for( void* pObj = maGraphicObjectList.First(); !bRet && pObj; pObj = maGraphicObjectList.Next() ) 381cdf0e10cSrcweir if( &rObj == (GraphicObject*) pObj ) 382cdf0e10cSrcweir bRet = sal_True; 383cdf0e10cSrcweir 384cdf0e10cSrcweir return bRet; 385cdf0e10cSrcweir } 386cdf0e10cSrcweir 387cdf0e10cSrcweir // ----------------------------------------------------------------------------- 388cdf0e10cSrcweir 389cdf0e10cSrcweir void GraphicCacheEntry::TryToSwapIn() 390cdf0e10cSrcweir { 391cdf0e10cSrcweir if( mbSwappedAll && maGraphicObjectList.Count() ) 392cdf0e10cSrcweir ( (GraphicObject*) maGraphicObjectList.First() )->FireSwapInRequest(); 393cdf0e10cSrcweir } 394cdf0e10cSrcweir 395cdf0e10cSrcweir // ----------------------------------------------------------------------------- 396cdf0e10cSrcweir 397cdf0e10cSrcweir void GraphicCacheEntry::GraphicObjectWasSwappedOut( const GraphicObject& /*rObj*/ ) 398cdf0e10cSrcweir { 399cdf0e10cSrcweir mbSwappedAll = sal_True; 400cdf0e10cSrcweir 401cdf0e10cSrcweir for( void* pObj = maGraphicObjectList.First(); mbSwappedAll && pObj; pObj = maGraphicObjectList.Next() ) 402cdf0e10cSrcweir if( !( (GraphicObject*) pObj )->IsSwappedOut() ) 403cdf0e10cSrcweir mbSwappedAll = sal_False; 404cdf0e10cSrcweir 405cdf0e10cSrcweir if( mbSwappedAll ) 406cdf0e10cSrcweir { 407cdf0e10cSrcweir delete mpBmpEx, mpBmpEx = NULL; 408cdf0e10cSrcweir delete mpMtf, mpMtf = NULL; 409cdf0e10cSrcweir delete mpAnimation, mpAnimation = NULL; 410cdf0e10cSrcweir } 411cdf0e10cSrcweir } 412cdf0e10cSrcweir 413cdf0e10cSrcweir // ----------------------------------------------------------------------------- 414cdf0e10cSrcweir 415cdf0e10cSrcweir sal_Bool GraphicCacheEntry::FillSwappedGraphicObject( const GraphicObject& rObj, Graphic& rSubstitute ) 416cdf0e10cSrcweir { 417cdf0e10cSrcweir sal_Bool bRet; 418cdf0e10cSrcweir 419cdf0e10cSrcweir if( !mbSwappedAll && rObj.IsSwappedOut() ) 420cdf0e10cSrcweir { 421cdf0e10cSrcweir ImplFillSubstitute( rSubstitute ); 422cdf0e10cSrcweir bRet = sal_True; 423cdf0e10cSrcweir } 424cdf0e10cSrcweir else 425cdf0e10cSrcweir bRet = sal_False; 426cdf0e10cSrcweir 427cdf0e10cSrcweir return bRet; 428cdf0e10cSrcweir } 429cdf0e10cSrcweir 430cdf0e10cSrcweir // ----------------------------------------------------------------------------- 431cdf0e10cSrcweir 432cdf0e10cSrcweir void GraphicCacheEntry::GraphicObjectWasSwappedIn( const GraphicObject& rObj ) 433cdf0e10cSrcweir { 434cdf0e10cSrcweir if( mbSwappedAll ) 435cdf0e10cSrcweir mbSwappedAll = !ImplInit( rObj ); 436cdf0e10cSrcweir } 437cdf0e10cSrcweir 438cdf0e10cSrcweir // ---------------------------- 439cdf0e10cSrcweir // - GraphicDisplayCacheEntry - 440cdf0e10cSrcweir // ---------------------------- 441cdf0e10cSrcweir 442cdf0e10cSrcweir class GraphicDisplayCacheEntry 443cdf0e10cSrcweir { 444cdf0e10cSrcweir private: 445cdf0e10cSrcweir 446cdf0e10cSrcweir ::vos::TTimeValue maReleaseTime; 447cdf0e10cSrcweir const GraphicCacheEntry* mpRefCacheEntry; 448cdf0e10cSrcweir GDIMetaFile* mpMtf; 449cdf0e10cSrcweir BitmapEx* mpBmpEx; 450cdf0e10cSrcweir GraphicAttr maAttr; 451cdf0e10cSrcweir Size maOutSizePix; 452cdf0e10cSrcweir sal_uLong mnCacheSize; 453cdf0e10cSrcweir sal_uLong mnOutDevDrawMode; 454cdf0e10cSrcweir sal_uInt16 mnOutDevBitCount; 455cdf0e10cSrcweir 456cdf0e10cSrcweir public: 457cdf0e10cSrcweir 458cdf0e10cSrcweir static sal_uLong GetNeededSize( OutputDevice* pOut, const Point& rPt, const Size& rSz, 459cdf0e10cSrcweir const GraphicObject& rObj, const GraphicAttr& rAttr ); 460cdf0e10cSrcweir 461cdf0e10cSrcweir public: 462cdf0e10cSrcweir 463cdf0e10cSrcweir GraphicDisplayCacheEntry( const GraphicCacheEntry* pRefCacheEntry, 464cdf0e10cSrcweir OutputDevice* pOut, const Point& rPt, const Size& rSz, 465cdf0e10cSrcweir const GraphicObject& rObj, const GraphicAttr& rAttr, 466cdf0e10cSrcweir const BitmapEx& rBmpEx ) : 467cdf0e10cSrcweir mpRefCacheEntry( pRefCacheEntry ), 468cdf0e10cSrcweir mpMtf( NULL ), mpBmpEx( new BitmapEx( rBmpEx ) ), 469cdf0e10cSrcweir maAttr( rAttr ), maOutSizePix( pOut->LogicToPixel( rSz ) ), 470cdf0e10cSrcweir mnCacheSize( GetNeededSize( pOut, rPt, rSz, rObj, rAttr ) ), 471cdf0e10cSrcweir mnOutDevDrawMode( pOut->GetDrawMode() ), 472cdf0e10cSrcweir mnOutDevBitCount( pOut->GetBitCount() ) 473cdf0e10cSrcweir { 474cdf0e10cSrcweir } 475cdf0e10cSrcweir 476cdf0e10cSrcweir GraphicDisplayCacheEntry( const GraphicCacheEntry* pRefCacheEntry, 477cdf0e10cSrcweir OutputDevice* pOut, const Point& rPt, const Size& rSz, 478cdf0e10cSrcweir const GraphicObject& rObj, const GraphicAttr& rAttr, 479cdf0e10cSrcweir const GDIMetaFile& rMtf ) : 480cdf0e10cSrcweir mpRefCacheEntry( pRefCacheEntry ), 481cdf0e10cSrcweir mpMtf( new GDIMetaFile( rMtf ) ), mpBmpEx( NULL ), 482cdf0e10cSrcweir maAttr( rAttr ), maOutSizePix( pOut->LogicToPixel( rSz ) ), 483cdf0e10cSrcweir mnCacheSize( GetNeededSize( pOut, rPt, rSz, rObj, rAttr ) ), 484cdf0e10cSrcweir mnOutDevDrawMode( pOut->GetDrawMode() ), 485cdf0e10cSrcweir mnOutDevBitCount( pOut->GetBitCount() ) 486cdf0e10cSrcweir { 487cdf0e10cSrcweir } 488cdf0e10cSrcweir 489cdf0e10cSrcweir 490cdf0e10cSrcweir ~GraphicDisplayCacheEntry(); 491cdf0e10cSrcweir 492cdf0e10cSrcweir const GraphicAttr& GetAttr() const { return maAttr; } 493cdf0e10cSrcweir const Size& GetOutputSizePixel() const { return maOutSizePix; } 494cdf0e10cSrcweir sal_uLong GetCacheSize() const { return mnCacheSize; } 495cdf0e10cSrcweir const GraphicCacheEntry* GetReferencedCacheEntry() const { return mpRefCacheEntry; } 496cdf0e10cSrcweir sal_uLong GetOutDevDrawMode() const { return mnOutDevDrawMode; } 497cdf0e10cSrcweir sal_uInt16 GetOutDevBitCount() const { return mnOutDevBitCount; } 498cdf0e10cSrcweir 499cdf0e10cSrcweir void SetReleaseTime( const ::vos::TTimeValue& rReleaseTime ) { maReleaseTime = rReleaseTime; } 500cdf0e10cSrcweir const ::vos::TTimeValue& GetReleaseTime() const { return maReleaseTime; } 501cdf0e10cSrcweir 502cdf0e10cSrcweir sal_Bool Matches( OutputDevice* pOut, const Point& /*rPtPixel*/, const Size& rSzPixel, 503cdf0e10cSrcweir const GraphicCacheEntry* pCacheEntry, const GraphicAttr& rAttr ) const 504cdf0e10cSrcweir { 505cdf0e10cSrcweir // #i46805# Additional match 506cdf0e10cSrcweir // criteria: outdev draw mode and 507cdf0e10cSrcweir // bit count. One cannot reuse 508cdf0e10cSrcweir // this cache object, if it's 509cdf0e10cSrcweir // e.g. generated for 510cdf0e10cSrcweir // DRAWMODE_GRAYBITMAP. 511cdf0e10cSrcweir return( ( pCacheEntry == mpRefCacheEntry ) && 512cdf0e10cSrcweir ( maAttr == rAttr ) && 513cdf0e10cSrcweir ( ( maOutSizePix == rSzPixel ) || ( !maOutSizePix.Width() && !maOutSizePix.Height() ) ) && 514cdf0e10cSrcweir ( pOut->GetBitCount() == mnOutDevBitCount ) && 515cdf0e10cSrcweir ( pOut->GetDrawMode() == mnOutDevDrawMode ) ); 516cdf0e10cSrcweir } 517cdf0e10cSrcweir 518cdf0e10cSrcweir void Draw( OutputDevice* pOut, const Point& rPt, const Size& rSz ) const; 519cdf0e10cSrcweir }; 520cdf0e10cSrcweir 521cdf0e10cSrcweir // ----------------------------------------------------------------------------- 522cdf0e10cSrcweir 523cdf0e10cSrcweir sal_uLong GraphicDisplayCacheEntry::GetNeededSize( OutputDevice* pOut, const Point& /*rPt*/, const Size& rSz, 524cdf0e10cSrcweir const GraphicObject& rObj, const GraphicAttr& rAttr ) 525cdf0e10cSrcweir { 526cdf0e10cSrcweir const Graphic& rGraphic = rObj.GetGraphic(); 527cdf0e10cSrcweir const GraphicType eType = rGraphic.GetType(); 528cdf0e10cSrcweir sal_uLong nNeededSize; 529cdf0e10cSrcweir 530cdf0e10cSrcweir if( GRAPHIC_BITMAP == eType ) 531cdf0e10cSrcweir { 532cdf0e10cSrcweir const Size aOutSizePix( pOut->LogicToPixel( rSz ) ); 533cdf0e10cSrcweir const long nBitCount = pOut->GetBitCount(); 534cdf0e10cSrcweir 535cdf0e10cSrcweir if( ( aOutSizePix.Width() > MAX_BMP_EXTENT ) || 536cdf0e10cSrcweir ( aOutSizePix.Height() > MAX_BMP_EXTENT ) ) 537cdf0e10cSrcweir { 538cdf0e10cSrcweir nNeededSize = ULONG_MAX; 539cdf0e10cSrcweir } 540cdf0e10cSrcweir else if( nBitCount ) 541cdf0e10cSrcweir { 542cdf0e10cSrcweir nNeededSize = aOutSizePix.Width() * aOutSizePix.Height() * nBitCount / 8; 543cdf0e10cSrcweir 544cdf0e10cSrcweir if( rObj.IsTransparent() || ( rAttr.GetRotation() % 3600 ) ) 545cdf0e10cSrcweir nNeededSize += nNeededSize / nBitCount; 546cdf0e10cSrcweir } 547cdf0e10cSrcweir else 548cdf0e10cSrcweir { 549cdf0e10cSrcweir DBG_ERROR( "GraphicDisplayCacheEntry::GetNeededSize(): pOut->GetBitCount() == 0" ); 550cdf0e10cSrcweir nNeededSize = 256000; 551cdf0e10cSrcweir } 552cdf0e10cSrcweir } 553cdf0e10cSrcweir else if( GRAPHIC_GDIMETAFILE == eType ) 554cdf0e10cSrcweir nNeededSize = rGraphic.GetSizeBytes(); 555cdf0e10cSrcweir else 556cdf0e10cSrcweir nNeededSize = 0; 557cdf0e10cSrcweir 558cdf0e10cSrcweir return nNeededSize; 559cdf0e10cSrcweir } 560cdf0e10cSrcweir 561cdf0e10cSrcweir // ----------------------------------------------------------------------------- 562cdf0e10cSrcweir 563cdf0e10cSrcweir GraphicDisplayCacheEntry::~GraphicDisplayCacheEntry() 564cdf0e10cSrcweir { 565cdf0e10cSrcweir if( mpMtf ) 566cdf0e10cSrcweir delete mpMtf; 567cdf0e10cSrcweir 568cdf0e10cSrcweir if( mpBmpEx ) 569cdf0e10cSrcweir delete mpBmpEx; 570cdf0e10cSrcweir } 571cdf0e10cSrcweir 572cdf0e10cSrcweir // ----------------------------------------------------------------------------- 573cdf0e10cSrcweir 574cdf0e10cSrcweir void GraphicDisplayCacheEntry::Draw( OutputDevice* pOut, const Point& rPt, const Size& rSz ) const 575cdf0e10cSrcweir { 576cdf0e10cSrcweir if( mpMtf ) 577cdf0e10cSrcweir GraphicManager::ImplDraw( pOut, rPt, rSz, *mpMtf, maAttr ); 578cdf0e10cSrcweir else if( mpBmpEx ) 579cdf0e10cSrcweir { 580cdf0e10cSrcweir if( maAttr.IsRotated() ) 581cdf0e10cSrcweir { 582cdf0e10cSrcweir Polygon aPoly( Rectangle( rPt, rSz ) ); 583cdf0e10cSrcweir 584cdf0e10cSrcweir aPoly.Rotate( rPt, maAttr.GetRotation() % 3600 ); 585cdf0e10cSrcweir const Rectangle aRotBoundRect( aPoly.GetBoundRect() ); 586cdf0e10cSrcweir pOut->DrawBitmapEx( aRotBoundRect.TopLeft(), aRotBoundRect.GetSize(), *mpBmpEx ); 587cdf0e10cSrcweir } 588cdf0e10cSrcweir else 589cdf0e10cSrcweir pOut->DrawBitmapEx( rPt, rSz, *mpBmpEx ); 590cdf0e10cSrcweir } 591cdf0e10cSrcweir } 592cdf0e10cSrcweir 593cdf0e10cSrcweir // ----------------------- 594cdf0e10cSrcweir // - GraphicCache - 595cdf0e10cSrcweir // ----------------------- 596cdf0e10cSrcweir 597cdf0e10cSrcweir GraphicCache::GraphicCache( GraphicManager& rMgr, sal_uLong nDisplayCacheSize, sal_uLong nMaxObjDisplayCacheSize ) : 598cdf0e10cSrcweir mrMgr ( rMgr ), 599cdf0e10cSrcweir mnReleaseTimeoutSeconds ( 0UL ), 600cdf0e10cSrcweir mnMaxDisplaySize ( nDisplayCacheSize ), 601cdf0e10cSrcweir mnMaxObjDisplaySize ( nMaxObjDisplayCacheSize ), 602cdf0e10cSrcweir mnUsedDisplaySize ( 0UL ) 603cdf0e10cSrcweir { 604cdf0e10cSrcweir maReleaseTimer.SetTimeoutHdl( LINK( this, GraphicCache, ReleaseTimeoutHdl ) ); 605cdf0e10cSrcweir maReleaseTimer.SetTimeout( RELEASE_TIMEOUT ); 606cdf0e10cSrcweir maReleaseTimer.Start(); 607cdf0e10cSrcweir } 608cdf0e10cSrcweir 609cdf0e10cSrcweir // ----------------------------------------------------------------------------- 610cdf0e10cSrcweir 611cdf0e10cSrcweir GraphicCache::~GraphicCache() 612cdf0e10cSrcweir { 613cdf0e10cSrcweir DBG_ASSERT( !maGraphicCache.Count(), "GraphicCache::~GraphicCache(): there are some GraphicObjects in cache" ); 614cdf0e10cSrcweir DBG_ASSERT( !maDisplayCache.Count(), "GraphicCache::~GraphicCache(): there are some GraphicObjects in display cache" ); 615cdf0e10cSrcweir } 616cdf0e10cSrcweir 617cdf0e10cSrcweir // ----------------------------------------------------------------------------- 618cdf0e10cSrcweir 619cdf0e10cSrcweir void GraphicCache::AddGraphicObject( const GraphicObject& rObj, Graphic& rSubstitute, 620cdf0e10cSrcweir const ByteString* pID, const GraphicObject* pCopyObj ) 621cdf0e10cSrcweir { 622cdf0e10cSrcweir sal_Bool bInserted = sal_False; 623cdf0e10cSrcweir 624cdf0e10cSrcweir if( !rObj.IsSwappedOut() && 625cdf0e10cSrcweir ( pID || ( pCopyObj && ( pCopyObj->GetType() != GRAPHIC_NONE ) ) || ( rObj.GetType() != GRAPHIC_NONE ) ) ) 626cdf0e10cSrcweir { 627cdf0e10cSrcweir if( pCopyObj ) 628cdf0e10cSrcweir { 629cdf0e10cSrcweir GraphicCacheEntry* pEntry = static_cast< GraphicCacheEntry* >( maGraphicCache.First() ); 630cdf0e10cSrcweir 631cdf0e10cSrcweir while( !bInserted && pEntry ) 632cdf0e10cSrcweir { 633cdf0e10cSrcweir if( pEntry->HasGraphicObjectReference( *pCopyObj ) ) 634cdf0e10cSrcweir { 635cdf0e10cSrcweir pEntry->AddGraphicObjectReference( rObj, rSubstitute ); 636cdf0e10cSrcweir bInserted = sal_True; 637cdf0e10cSrcweir } 638cdf0e10cSrcweir else 639cdf0e10cSrcweir { 640cdf0e10cSrcweir pEntry = static_cast< GraphicCacheEntry* >( maGraphicCache.Next() ); 641cdf0e10cSrcweir } 642cdf0e10cSrcweir } 643cdf0e10cSrcweir } 644cdf0e10cSrcweir 645cdf0e10cSrcweir if( !bInserted ) 646cdf0e10cSrcweir { 647cdf0e10cSrcweir GraphicCacheEntry* pEntry = static_cast< GraphicCacheEntry* >( maGraphicCache.First() ); 648cdf0e10cSrcweir ::std::auto_ptr< GraphicID > apID; 649cdf0e10cSrcweir 650cdf0e10cSrcweir if( !pID ) 651cdf0e10cSrcweir { 652cdf0e10cSrcweir apID.reset( new GraphicID( rObj ) ); 653cdf0e10cSrcweir } 654cdf0e10cSrcweir 655cdf0e10cSrcweir while( !bInserted && pEntry ) 656cdf0e10cSrcweir { 657cdf0e10cSrcweir const GraphicID& rEntryID = pEntry->GetID(); 658cdf0e10cSrcweir 659cdf0e10cSrcweir if( pID ) 660cdf0e10cSrcweir { 661cdf0e10cSrcweir if( rEntryID.GetIDString() == *pID ) 662cdf0e10cSrcweir { 663cdf0e10cSrcweir pEntry->TryToSwapIn(); 664cdf0e10cSrcweir 665cdf0e10cSrcweir // since pEntry->TryToSwapIn can modify our current list, we have to 666cdf0e10cSrcweir // iterate from beginning to add a reference to the appropriate 667cdf0e10cSrcweir // CacheEntry object; after this, quickly jump out of the outer iteration 668cdf0e10cSrcweir for( pEntry = static_cast< GraphicCacheEntry* >( maGraphicCache.First() ); 669cdf0e10cSrcweir !bInserted && pEntry; 670cdf0e10cSrcweir pEntry = static_cast< GraphicCacheEntry* >( maGraphicCache.Next() ) ) 671cdf0e10cSrcweir { 672cdf0e10cSrcweir const GraphicID& rID = pEntry->GetID(); 673cdf0e10cSrcweir 674cdf0e10cSrcweir if( rID.GetIDString() == *pID ) 675cdf0e10cSrcweir { 676cdf0e10cSrcweir pEntry->AddGraphicObjectReference( rObj, rSubstitute ); 677cdf0e10cSrcweir bInserted = sal_True; 678cdf0e10cSrcweir } 679cdf0e10cSrcweir } 680cdf0e10cSrcweir 681cdf0e10cSrcweir if( !bInserted ) 682cdf0e10cSrcweir { 683cdf0e10cSrcweir maGraphicCache.Insert( new GraphicCacheEntry( rObj ), LIST_APPEND ); 684cdf0e10cSrcweir bInserted = sal_True; 685cdf0e10cSrcweir } 686cdf0e10cSrcweir } 687cdf0e10cSrcweir } 688cdf0e10cSrcweir else 689cdf0e10cSrcweir { 690cdf0e10cSrcweir if( rEntryID == *apID ) 691cdf0e10cSrcweir { 692cdf0e10cSrcweir pEntry->AddGraphicObjectReference( rObj, rSubstitute ); 693cdf0e10cSrcweir bInserted = sal_True; 694cdf0e10cSrcweir } 695cdf0e10cSrcweir } 696cdf0e10cSrcweir 697cdf0e10cSrcweir if( !bInserted ) 698cdf0e10cSrcweir pEntry = static_cast< GraphicCacheEntry* >( maGraphicCache.Next() ); 699cdf0e10cSrcweir } 700cdf0e10cSrcweir } 701cdf0e10cSrcweir } 702cdf0e10cSrcweir 703cdf0e10cSrcweir if( !bInserted ) 704cdf0e10cSrcweir maGraphicCache.Insert( new GraphicCacheEntry( rObj ), LIST_APPEND ); 705cdf0e10cSrcweir } 706cdf0e10cSrcweir 707cdf0e10cSrcweir // ----------------------------------------------------------------------------- 708cdf0e10cSrcweir 709cdf0e10cSrcweir void GraphicCache::ReleaseGraphicObject( const GraphicObject& rObj ) 710cdf0e10cSrcweir { 711cdf0e10cSrcweir // Release cached object 712cdf0e10cSrcweir GraphicCacheEntry* pEntry = (GraphicCacheEntry*) maGraphicCache.First(); 713cdf0e10cSrcweir sal_Bool bRemoved = sal_False; 714cdf0e10cSrcweir 715cdf0e10cSrcweir while( !bRemoved && pEntry ) 716cdf0e10cSrcweir { 717cdf0e10cSrcweir bRemoved = pEntry->ReleaseGraphicObjectReference( rObj ); 718cdf0e10cSrcweir 719cdf0e10cSrcweir if( bRemoved ) 720cdf0e10cSrcweir { 721cdf0e10cSrcweir if( 0 == pEntry->GetGraphicObjectReferenceCount() ) 722cdf0e10cSrcweir { 723cdf0e10cSrcweir // if graphic cache entry has no more references, 724cdf0e10cSrcweir // the corresponding display cache object can be removed 725cdf0e10cSrcweir GraphicDisplayCacheEntry* pDisplayEntry = (GraphicDisplayCacheEntry*) maDisplayCache.First(); 726cdf0e10cSrcweir 727cdf0e10cSrcweir while( pDisplayEntry ) 728cdf0e10cSrcweir { 729cdf0e10cSrcweir if( pDisplayEntry->GetReferencedCacheEntry() == pEntry ) 730cdf0e10cSrcweir { 731cdf0e10cSrcweir mnUsedDisplaySize -= pDisplayEntry->GetCacheSize(); 732cdf0e10cSrcweir maDisplayCache.Remove( pDisplayEntry ); 733cdf0e10cSrcweir delete pDisplayEntry; 734cdf0e10cSrcweir pDisplayEntry = (GraphicDisplayCacheEntry*) maDisplayCache.GetCurObject(); 735cdf0e10cSrcweir } 736cdf0e10cSrcweir else 737cdf0e10cSrcweir pDisplayEntry = (GraphicDisplayCacheEntry*) maDisplayCache.Next(); 738cdf0e10cSrcweir } 739cdf0e10cSrcweir 740cdf0e10cSrcweir // delete graphic cache entry 741cdf0e10cSrcweir maGraphicCache.Remove( (void*) pEntry ); 742cdf0e10cSrcweir delete pEntry; 743cdf0e10cSrcweir } 744cdf0e10cSrcweir } 745cdf0e10cSrcweir else 746cdf0e10cSrcweir pEntry = (GraphicCacheEntry*) maGraphicCache.Next(); 747cdf0e10cSrcweir } 748cdf0e10cSrcweir 749cdf0e10cSrcweir DBG_ASSERT( bRemoved, "GraphicCache::ReleaseGraphicObject(...): GraphicObject not found in cache" ); 750cdf0e10cSrcweir } 751cdf0e10cSrcweir 752cdf0e10cSrcweir // ----------------------------------------------------------------------------- 753cdf0e10cSrcweir 754cdf0e10cSrcweir void GraphicCache::GraphicObjectWasSwappedOut( const GraphicObject& rObj ) 755cdf0e10cSrcweir { 756cdf0e10cSrcweir // notify cache that rObj is swapped out (and can thus be pruned 757cdf0e10cSrcweir // from the cache) 758cdf0e10cSrcweir GraphicCacheEntry* pEntry = ImplGetCacheEntry( rObj ); 759cdf0e10cSrcweir 760cdf0e10cSrcweir if( pEntry ) 761cdf0e10cSrcweir pEntry->GraphicObjectWasSwappedOut( rObj ); 762cdf0e10cSrcweir } 763cdf0e10cSrcweir 764cdf0e10cSrcweir // ----------------------------------------------------------------------------- 765cdf0e10cSrcweir 766cdf0e10cSrcweir sal_Bool GraphicCache::FillSwappedGraphicObject( const GraphicObject& rObj, Graphic& rSubstitute ) 767cdf0e10cSrcweir { 768cdf0e10cSrcweir GraphicCacheEntry* pEntry = ImplGetCacheEntry( rObj ); 769cdf0e10cSrcweir 770cdf0e10cSrcweir if( !pEntry ) 771cdf0e10cSrcweir return sal_False; 772cdf0e10cSrcweir 773cdf0e10cSrcweir return pEntry->FillSwappedGraphicObject( rObj, rSubstitute ); 774cdf0e10cSrcweir } 775cdf0e10cSrcweir 776cdf0e10cSrcweir // ----------------------------------------------------------------------------- 777cdf0e10cSrcweir 778cdf0e10cSrcweir void GraphicCache::GraphicObjectWasSwappedIn( const GraphicObject& rObj ) 779cdf0e10cSrcweir { 780cdf0e10cSrcweir GraphicCacheEntry* pEntry = ImplGetCacheEntry( rObj ); 781cdf0e10cSrcweir 782cdf0e10cSrcweir if( pEntry ) 783cdf0e10cSrcweir { 784cdf0e10cSrcweir if( pEntry->GetID().IsEmpty() ) 785cdf0e10cSrcweir { 786cdf0e10cSrcweir ReleaseGraphicObject( rObj ); 787cdf0e10cSrcweir AddGraphicObject( rObj, (Graphic&) rObj.GetGraphic(), NULL, NULL ); 788cdf0e10cSrcweir } 789cdf0e10cSrcweir else 790cdf0e10cSrcweir pEntry->GraphicObjectWasSwappedIn( rObj ); 791cdf0e10cSrcweir } 792cdf0e10cSrcweir } 793cdf0e10cSrcweir 794cdf0e10cSrcweir // ----------------------------------------------------------------------------- 795cdf0e10cSrcweir 796cdf0e10cSrcweir void GraphicCache::SetMaxDisplayCacheSize( sal_uLong nNewCacheSize ) 797cdf0e10cSrcweir { 798cdf0e10cSrcweir mnMaxDisplaySize = nNewCacheSize; 799cdf0e10cSrcweir 800cdf0e10cSrcweir if( GetMaxDisplayCacheSize() < GetUsedDisplayCacheSize() ) 801cdf0e10cSrcweir ImplFreeDisplayCacheSpace( GetUsedDisplayCacheSize() - GetMaxDisplayCacheSize() ); 802cdf0e10cSrcweir } 803cdf0e10cSrcweir 804cdf0e10cSrcweir // ----------------------------------------------------------------------------- 805cdf0e10cSrcweir 806cdf0e10cSrcweir void GraphicCache::SetMaxObjDisplayCacheSize( sal_uLong nNewMaxObjSize, sal_Bool bDestroyGreaterCached ) 807cdf0e10cSrcweir { 808cdf0e10cSrcweir const sal_Bool bDestroy = ( bDestroyGreaterCached && ( nNewMaxObjSize < mnMaxObjDisplaySize ) ); 809cdf0e10cSrcweir 810cdf0e10cSrcweir mnMaxObjDisplaySize = Min( nNewMaxObjSize, mnMaxDisplaySize ); 811cdf0e10cSrcweir 812cdf0e10cSrcweir if( bDestroy ) 813cdf0e10cSrcweir { 814cdf0e10cSrcweir GraphicDisplayCacheEntry* pCacheObj = (GraphicDisplayCacheEntry*) maDisplayCache.First(); 815cdf0e10cSrcweir 816cdf0e10cSrcweir while( pCacheObj ) 817cdf0e10cSrcweir { 818cdf0e10cSrcweir if( pCacheObj->GetCacheSize() > mnMaxObjDisplaySize ) 819cdf0e10cSrcweir { 820cdf0e10cSrcweir mnUsedDisplaySize -= pCacheObj->GetCacheSize(); 821cdf0e10cSrcweir maDisplayCache.Remove( pCacheObj ); 822cdf0e10cSrcweir delete pCacheObj; 823cdf0e10cSrcweir pCacheObj = (GraphicDisplayCacheEntry*) maDisplayCache.GetCurObject(); 824cdf0e10cSrcweir } 825cdf0e10cSrcweir else 826cdf0e10cSrcweir pCacheObj = (GraphicDisplayCacheEntry*) maDisplayCache.Next(); 827cdf0e10cSrcweir } 828cdf0e10cSrcweir } 829cdf0e10cSrcweir } 830cdf0e10cSrcweir 831cdf0e10cSrcweir // ----------------------------------------------------------------------------- 832cdf0e10cSrcweir 833cdf0e10cSrcweir void GraphicCache::SetCacheTimeout( sal_uLong nTimeoutSeconds ) 834cdf0e10cSrcweir { 835cdf0e10cSrcweir if( mnReleaseTimeoutSeconds != nTimeoutSeconds ) 836cdf0e10cSrcweir { 837cdf0e10cSrcweir GraphicDisplayCacheEntry* pDisplayEntry = (GraphicDisplayCacheEntry*) maDisplayCache.First(); 838cdf0e10cSrcweir ::vos::TTimeValue aReleaseTime; 839cdf0e10cSrcweir 840cdf0e10cSrcweir if( ( mnReleaseTimeoutSeconds = nTimeoutSeconds ) != 0 ) 841cdf0e10cSrcweir { 842cdf0e10cSrcweir osl_getSystemTime( &aReleaseTime ); 843cdf0e10cSrcweir aReleaseTime.addTime( ::vos::TTimeValue( nTimeoutSeconds, 0 ) ); 844cdf0e10cSrcweir } 845cdf0e10cSrcweir 846cdf0e10cSrcweir while( pDisplayEntry ) 847cdf0e10cSrcweir { 848cdf0e10cSrcweir pDisplayEntry->SetReleaseTime( aReleaseTime ); 849cdf0e10cSrcweir pDisplayEntry = (GraphicDisplayCacheEntry*) maDisplayCache.Next(); 850cdf0e10cSrcweir } 851cdf0e10cSrcweir } 852cdf0e10cSrcweir } 853cdf0e10cSrcweir 854cdf0e10cSrcweir // ----------------------------------------------------------------------------- 855cdf0e10cSrcweir 856cdf0e10cSrcweir void GraphicCache::ClearDisplayCache() 857cdf0e10cSrcweir { 858cdf0e10cSrcweir for( void* pObj = maDisplayCache.First(); pObj; pObj = maDisplayCache.Next() ) 859cdf0e10cSrcweir delete (GraphicDisplayCacheEntry*) pObj; 860cdf0e10cSrcweir 861cdf0e10cSrcweir maDisplayCache.Clear(); 862cdf0e10cSrcweir mnUsedDisplaySize = 0UL; 863cdf0e10cSrcweir } 864cdf0e10cSrcweir 865cdf0e10cSrcweir // ----------------------------------------------------------------------------- 866cdf0e10cSrcweir 867cdf0e10cSrcweir sal_Bool GraphicCache::IsDisplayCacheable( OutputDevice* pOut, const Point& rPt, const Size& rSz, 868cdf0e10cSrcweir const GraphicObject& rObj, const GraphicAttr& rAttr ) const 869cdf0e10cSrcweir { 870cdf0e10cSrcweir return( GraphicDisplayCacheEntry::GetNeededSize( pOut, rPt, rSz, rObj, rAttr ) <= 871cdf0e10cSrcweir GetMaxObjDisplayCacheSize() ); 872cdf0e10cSrcweir } 873cdf0e10cSrcweir 874cdf0e10cSrcweir // ----------------------------------------------------------------------------- 875cdf0e10cSrcweir 876cdf0e10cSrcweir sal_Bool GraphicCache::IsInDisplayCache( OutputDevice* pOut, const Point& rPt, const Size& rSz, 877cdf0e10cSrcweir const GraphicObject& rObj, const GraphicAttr& rAttr ) const 878cdf0e10cSrcweir { 879cdf0e10cSrcweir const Point aPtPixel( pOut->LogicToPixel( rPt ) ); 880cdf0e10cSrcweir const Size aSzPixel( pOut->LogicToPixel( rSz ) ); 881cdf0e10cSrcweir const GraphicCacheEntry* pCacheEntry = ( (GraphicCache*) this )->ImplGetCacheEntry( rObj ); 882cdf0e10cSrcweir //GraphicDisplayCacheEntry* pDisplayEntry = (GraphicDisplayCacheEntry*) ( (GraphicCache*) this )->maDisplayCache.First(); // -Wall removed .... 883cdf0e10cSrcweir sal_Bool bFound = sal_False; 884cdf0e10cSrcweir 885cdf0e10cSrcweir if( pCacheEntry ) 886cdf0e10cSrcweir { 887cdf0e10cSrcweir for( long i = 0, nCount = maDisplayCache.Count(); !bFound && ( i < nCount ); i++ ) 888cdf0e10cSrcweir if( ( (GraphicDisplayCacheEntry*) maDisplayCache.GetObject( i ) )->Matches( pOut, aPtPixel, aSzPixel, pCacheEntry, rAttr ) ) 889cdf0e10cSrcweir bFound = sal_True; 890cdf0e10cSrcweir } 891cdf0e10cSrcweir 892cdf0e10cSrcweir return bFound; 893cdf0e10cSrcweir } 894cdf0e10cSrcweir 895cdf0e10cSrcweir // ----------------------------------------------------------------------------- 896cdf0e10cSrcweir 897cdf0e10cSrcweir ByteString GraphicCache::GetUniqueID( const GraphicObject& rObj ) const 898cdf0e10cSrcweir { 899cdf0e10cSrcweir ByteString aRet; 900cdf0e10cSrcweir GraphicCacheEntry* pEntry = ( (GraphicCache*) this )->ImplGetCacheEntry( rObj ); 901cdf0e10cSrcweir 902cdf0e10cSrcweir // ensure that the entry is correctly initialized (it has to be read at least once) 903cdf0e10cSrcweir if( pEntry && pEntry->GetID().IsEmpty() ) 904cdf0e10cSrcweir pEntry->TryToSwapIn(); 905cdf0e10cSrcweir 906cdf0e10cSrcweir // do another call to ImplGetCacheEntry in case of modified entry list 907cdf0e10cSrcweir pEntry = ( (GraphicCache*) this )->ImplGetCacheEntry( rObj ); 908cdf0e10cSrcweir 909cdf0e10cSrcweir if( pEntry ) 910cdf0e10cSrcweir aRet = pEntry->GetID().GetIDString(); 911cdf0e10cSrcweir 912cdf0e10cSrcweir return aRet; 913cdf0e10cSrcweir } 914cdf0e10cSrcweir 915cdf0e10cSrcweir // ----------------------------------------------------------------------------- 916cdf0e10cSrcweir 917cdf0e10cSrcweir sal_Bool GraphicCache::CreateDisplayCacheObj( OutputDevice* pOut, const Point& rPt, const Size& rSz, 918cdf0e10cSrcweir const GraphicObject& rObj, const GraphicAttr& rAttr, 919cdf0e10cSrcweir const BitmapEx& rBmpEx ) 920cdf0e10cSrcweir { 921cdf0e10cSrcweir const sal_uLong nNeededSize = GraphicDisplayCacheEntry::GetNeededSize( pOut, rPt, rSz, rObj, rAttr ); 922cdf0e10cSrcweir sal_Bool bRet = sal_False; 923cdf0e10cSrcweir 924cdf0e10cSrcweir if( nNeededSize <= GetMaxObjDisplayCacheSize() ) 925cdf0e10cSrcweir { 926cdf0e10cSrcweir if( nNeededSize > GetFreeDisplayCacheSize() ) 927cdf0e10cSrcweir ImplFreeDisplayCacheSpace( nNeededSize - GetFreeDisplayCacheSize() ); 928cdf0e10cSrcweir 929cdf0e10cSrcweir GraphicDisplayCacheEntry* pNewEntry = new GraphicDisplayCacheEntry( ImplGetCacheEntry( rObj ), 930cdf0e10cSrcweir pOut, rPt, rSz, rObj, rAttr, rBmpEx ); 931cdf0e10cSrcweir 932cdf0e10cSrcweir if( GetCacheTimeout() ) 933cdf0e10cSrcweir { 934cdf0e10cSrcweir ::vos::TTimeValue aReleaseTime; 935cdf0e10cSrcweir 936cdf0e10cSrcweir osl_getSystemTime( &aReleaseTime ); 937cdf0e10cSrcweir aReleaseTime.addTime( ::vos::TTimeValue( GetCacheTimeout(), 0 ) ); 938cdf0e10cSrcweir pNewEntry->SetReleaseTime( aReleaseTime ); 939cdf0e10cSrcweir } 940cdf0e10cSrcweir 941cdf0e10cSrcweir maDisplayCache.Insert( pNewEntry, LIST_APPEND ); 942cdf0e10cSrcweir mnUsedDisplaySize += pNewEntry->GetCacheSize(); 943cdf0e10cSrcweir bRet = sal_True; 944cdf0e10cSrcweir } 945cdf0e10cSrcweir 946cdf0e10cSrcweir return bRet; 947cdf0e10cSrcweir } 948cdf0e10cSrcweir 949cdf0e10cSrcweir // ----------------------------------------------------------------------------- 950cdf0e10cSrcweir 951cdf0e10cSrcweir sal_Bool GraphicCache::CreateDisplayCacheObj( OutputDevice* pOut, const Point& rPt, const Size& rSz, 952cdf0e10cSrcweir const GraphicObject& rObj, const GraphicAttr& rAttr, 953cdf0e10cSrcweir const GDIMetaFile& rMtf ) 954cdf0e10cSrcweir { 955cdf0e10cSrcweir const sal_uLong nNeededSize = GraphicDisplayCacheEntry::GetNeededSize( pOut, rPt, rSz, rObj, rAttr ); 956cdf0e10cSrcweir sal_Bool bRet = sal_False; 957cdf0e10cSrcweir 958cdf0e10cSrcweir if( nNeededSize <= GetMaxObjDisplayCacheSize() ) 959cdf0e10cSrcweir { 960cdf0e10cSrcweir if( nNeededSize > GetFreeDisplayCacheSize() ) 961cdf0e10cSrcweir ImplFreeDisplayCacheSpace( nNeededSize - GetFreeDisplayCacheSize() ); 962cdf0e10cSrcweir 963cdf0e10cSrcweir GraphicDisplayCacheEntry* pNewEntry = new GraphicDisplayCacheEntry( ImplGetCacheEntry( rObj ), 964cdf0e10cSrcweir pOut, rPt, rSz, rObj, rAttr, rMtf ); 965cdf0e10cSrcweir 966cdf0e10cSrcweir if( GetCacheTimeout() ) 967cdf0e10cSrcweir { 968cdf0e10cSrcweir ::vos::TTimeValue aReleaseTime; 969cdf0e10cSrcweir 970cdf0e10cSrcweir osl_getSystemTime( &aReleaseTime ); 971cdf0e10cSrcweir aReleaseTime.addTime( ::vos::TTimeValue( GetCacheTimeout(), 0 ) ); 972cdf0e10cSrcweir pNewEntry->SetReleaseTime( aReleaseTime ); 973cdf0e10cSrcweir } 974cdf0e10cSrcweir 975cdf0e10cSrcweir maDisplayCache.Insert( pNewEntry, LIST_APPEND ); 976cdf0e10cSrcweir mnUsedDisplaySize += pNewEntry->GetCacheSize(); 977cdf0e10cSrcweir bRet = sal_True; 978cdf0e10cSrcweir } 979cdf0e10cSrcweir 980cdf0e10cSrcweir return bRet; 981cdf0e10cSrcweir } 982cdf0e10cSrcweir 983cdf0e10cSrcweir // ----------------------------------------------------------------------------- 984cdf0e10cSrcweir 985cdf0e10cSrcweir sal_Bool GraphicCache::DrawDisplayCacheObj( OutputDevice* pOut, const Point& rPt, const Size& rSz, 986cdf0e10cSrcweir const GraphicObject& rObj, const GraphicAttr& rAttr ) 987cdf0e10cSrcweir { 988cdf0e10cSrcweir const Point aPtPixel( pOut->LogicToPixel( rPt ) ); 989cdf0e10cSrcweir const Size aSzPixel( pOut->LogicToPixel( rSz ) ); 990cdf0e10cSrcweir const GraphicCacheEntry* pCacheEntry = ImplGetCacheEntry( rObj ); 991cdf0e10cSrcweir GraphicDisplayCacheEntry* pDisplayCacheEntry = (GraphicDisplayCacheEntry*) maDisplayCache.First(); 992cdf0e10cSrcweir sal_Bool bRet = sal_False; 993cdf0e10cSrcweir 994cdf0e10cSrcweir while( !bRet && pDisplayCacheEntry ) 995cdf0e10cSrcweir { 996cdf0e10cSrcweir if( pDisplayCacheEntry->Matches( pOut, aPtPixel, aSzPixel, pCacheEntry, rAttr ) ) 997cdf0e10cSrcweir { 998cdf0e10cSrcweir ::vos::TTimeValue aReleaseTime; 999cdf0e10cSrcweir 1000cdf0e10cSrcweir // put found object at last used position 1001cdf0e10cSrcweir maDisplayCache.Insert( maDisplayCache.Remove( pDisplayCacheEntry ), LIST_APPEND ); 1002cdf0e10cSrcweir 1003cdf0e10cSrcweir if( GetCacheTimeout() ) 1004cdf0e10cSrcweir { 1005cdf0e10cSrcweir osl_getSystemTime( &aReleaseTime ); 1006cdf0e10cSrcweir aReleaseTime.addTime( ::vos::TTimeValue( GetCacheTimeout(), 0 ) ); 1007cdf0e10cSrcweir } 1008cdf0e10cSrcweir 1009cdf0e10cSrcweir pDisplayCacheEntry->SetReleaseTime( aReleaseTime ); 1010cdf0e10cSrcweir bRet = sal_True; 1011cdf0e10cSrcweir } 1012cdf0e10cSrcweir else 1013cdf0e10cSrcweir pDisplayCacheEntry = (GraphicDisplayCacheEntry*) maDisplayCache.Next(); 1014cdf0e10cSrcweir } 1015cdf0e10cSrcweir 1016cdf0e10cSrcweir if( bRet ) 1017cdf0e10cSrcweir pDisplayCacheEntry->Draw( pOut, rPt, rSz ); 1018cdf0e10cSrcweir 1019cdf0e10cSrcweir return bRet; 1020cdf0e10cSrcweir } 1021cdf0e10cSrcweir 1022cdf0e10cSrcweir // ----------------------------------------------------------------------------- 1023cdf0e10cSrcweir 1024cdf0e10cSrcweir sal_Bool GraphicCache::ImplFreeDisplayCacheSpace( sal_uLong nSizeToFree ) 1025cdf0e10cSrcweir { 1026cdf0e10cSrcweir sal_uLong nFreedSize = 0UL; 1027cdf0e10cSrcweir 1028cdf0e10cSrcweir if( nSizeToFree ) 1029cdf0e10cSrcweir { 1030cdf0e10cSrcweir void* pObj = maDisplayCache.First(); 1031cdf0e10cSrcweir 1032cdf0e10cSrcweir if( nSizeToFree > mnUsedDisplaySize ) 1033cdf0e10cSrcweir nSizeToFree = mnUsedDisplaySize; 1034cdf0e10cSrcweir 1035cdf0e10cSrcweir while( pObj ) 1036cdf0e10cSrcweir { 1037cdf0e10cSrcweir GraphicDisplayCacheEntry* pCacheObj = (GraphicDisplayCacheEntry*) pObj; 1038cdf0e10cSrcweir 1039cdf0e10cSrcweir nFreedSize += pCacheObj->GetCacheSize(); 1040cdf0e10cSrcweir mnUsedDisplaySize -= pCacheObj->GetCacheSize(); 1041cdf0e10cSrcweir maDisplayCache.Remove( pObj ); 1042cdf0e10cSrcweir delete pCacheObj; 1043cdf0e10cSrcweir 1044cdf0e10cSrcweir if( nFreedSize >= nSizeToFree ) 1045cdf0e10cSrcweir break; 1046cdf0e10cSrcweir else 1047cdf0e10cSrcweir pObj = maDisplayCache.GetCurObject(); 1048cdf0e10cSrcweir } 1049cdf0e10cSrcweir } 1050cdf0e10cSrcweir 1051cdf0e10cSrcweir return( nFreedSize >= nSizeToFree ); 1052cdf0e10cSrcweir } 1053cdf0e10cSrcweir 1054cdf0e10cSrcweir // ----------------------------------------------------------------------------- 1055cdf0e10cSrcweir 1056cdf0e10cSrcweir GraphicCacheEntry* GraphicCache::ImplGetCacheEntry( const GraphicObject& rObj ) 1057cdf0e10cSrcweir { 1058cdf0e10cSrcweir GraphicCacheEntry* pRet = NULL; 1059cdf0e10cSrcweir 1060cdf0e10cSrcweir for( void* pObj = maGraphicCache.First(); !pRet && pObj; pObj = maGraphicCache.Next() ) 1061cdf0e10cSrcweir if( ( (GraphicCacheEntry*) pObj )->HasGraphicObjectReference( rObj ) ) 1062cdf0e10cSrcweir pRet = (GraphicCacheEntry*) pObj; 1063cdf0e10cSrcweir 1064cdf0e10cSrcweir return pRet; 1065cdf0e10cSrcweir } 1066cdf0e10cSrcweir 1067cdf0e10cSrcweir // ----------------------------------------------------------------------------- 1068cdf0e10cSrcweir 1069cdf0e10cSrcweir IMPL_LINK( GraphicCache, ReleaseTimeoutHdl, Timer*, pTimer ) 1070cdf0e10cSrcweir { 1071cdf0e10cSrcweir pTimer->Stop(); 1072cdf0e10cSrcweir 1073cdf0e10cSrcweir ::vos::TTimeValue aCurTime; 1074cdf0e10cSrcweir GraphicDisplayCacheEntry* pDisplayEntry = (GraphicDisplayCacheEntry*) maDisplayCache.First(); 1075cdf0e10cSrcweir 1076cdf0e10cSrcweir osl_getSystemTime( &aCurTime ); 1077cdf0e10cSrcweir 1078cdf0e10cSrcweir while( pDisplayEntry ) 1079cdf0e10cSrcweir { 1080cdf0e10cSrcweir const ::vos::TTimeValue& rReleaseTime = pDisplayEntry->GetReleaseTime(); 1081cdf0e10cSrcweir 1082cdf0e10cSrcweir if( !rReleaseTime.isEmpty() && ( rReleaseTime < aCurTime ) ) 1083cdf0e10cSrcweir { 1084cdf0e10cSrcweir mnUsedDisplaySize -= pDisplayEntry->GetCacheSize(); 1085cdf0e10cSrcweir maDisplayCache.Remove( pDisplayEntry ); 1086cdf0e10cSrcweir delete pDisplayEntry; 1087cdf0e10cSrcweir pDisplayEntry = (GraphicDisplayCacheEntry*) maDisplayCache.GetCurObject(); 1088cdf0e10cSrcweir } 1089cdf0e10cSrcweir else 1090cdf0e10cSrcweir pDisplayEntry = (GraphicDisplayCacheEntry*) maDisplayCache.Next(); 1091cdf0e10cSrcweir } 1092cdf0e10cSrcweir 1093cdf0e10cSrcweir pTimer->Start(); 1094cdf0e10cSrcweir 1095cdf0e10cSrcweir return 0; 1096cdf0e10cSrcweir } 1097