Branch libreoffice-5-0-4
[LibreOffice.git] / canvas / source / vcl / bitmapbackbuffer.cxx
blob4fd79bc2df33103a3237a66ae8faa65406989003
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 .
21 #include "bitmapbackbuffer.hxx"
23 #include <osl/mutex.hxx>
25 #include <vcl/svapp.hxx>
26 #include <vcl/bitmapex.hxx>
27 #include <vcl/bmpacc.hxx>
30 namespace vclcanvas
32 BitmapBackBuffer::BitmapBackBuffer( const BitmapEx& rBitmap,
33 const OutputDevice& rRefDevice ) :
34 maBitmap( rBitmap ),
35 mpVDev( NULL ),
36 mrRefDevice( rRefDevice ),
37 mbBitmapContentIsCurrent( false ),
38 mbVDevContentIsCurrent( false )
42 BitmapBackBuffer::~BitmapBackBuffer()
44 // make sure solar mutex is held on deletion (other methods
45 // are supposed to be called with already locked solar mutex)
46 SolarMutexGuard aGuard;
48 mpVDev.disposeAndClear();
51 OutputDevice& BitmapBackBuffer::getOutDev()
53 createVDev();
54 updateVDev();
55 return *mpVDev;
58 const OutputDevice& BitmapBackBuffer::getOutDev() const
60 createVDev();
61 updateVDev();
62 return *mpVDev;
65 void BitmapBackBuffer::clear()
67 // force current content to bitmap, make all transparent white
68 getBitmapReference().Erase(COL_TRANSPARENT);
71 BitmapEx& BitmapBackBuffer::getBitmapReference()
73 OSL_ENSURE( !mbBitmapContentIsCurrent || !mbVDevContentIsCurrent,
74 "BitmapBackBuffer::getBitmapReference(): Both bitmap and VDev are valid?!" );
76 if( mbVDevContentIsCurrent && mpVDev )
78 // VDev content is more current than bitmap - copy contents before!
79 mpVDev->EnableMapMode( false );
80 mpVDev->SetAntialiasing( AntialiasingFlags::EnableB2dDraw );
81 const Point aEmptyPoint;
82 *maBitmap = mpVDev->GetBitmapEx( aEmptyPoint,
83 mpVDev->GetOutputSizePixel() );
86 // client queries bitmap, and will possibly alter content -
87 // next time, VDev needs to be updated
88 mbBitmapContentIsCurrent = true;
89 mbVDevContentIsCurrent = false;
91 return *maBitmap;
94 Size BitmapBackBuffer::getBitmapSizePixel() const
96 Size aSize = maBitmap->GetSizePixel();
98 if( mbVDevContentIsCurrent && mpVDev )
100 mpVDev->EnableMapMode( false );
101 mpVDev->SetAntialiasing( AntialiasingFlags::EnableB2dDraw );
102 aSize = mpVDev->GetOutputSizePixel();
105 return aSize;
108 void BitmapBackBuffer::createVDev() const
110 if( !mpVDev )
112 // VDev not yet created, do it now. Create an alpha-VDev,
113 // if bitmap has transparency.
114 mpVDev = maBitmap->IsTransparent() ?
115 VclPtr<VirtualDevice>::Create( mrRefDevice, 0, 0 ) :
116 VclPtr<VirtualDevice>::Create( mrRefDevice );
118 OSL_ENSURE( mpVDev,
119 "BitmapBackBuffer::createVDev(): Unable to create VirtualDevice" );
121 mpVDev->SetOutputSizePixel( maBitmap->GetSizePixel() );
123 // #i95645#
124 #if defined( MACOSX )
125 // use AA on VCLCanvas for Mac
126 mpVDev->SetAntialiasing( AntialiasingFlags::EnableB2dDraw | mpVDev->GetAntialiasing() );
127 #else
128 // switch off AA for WIN32 and UNIX, the VCLCanvas does not look good with it and
129 // is not required to do AA. It would need to be adapted to use it correctly
130 // (especially gradient painting). This will need extra work.
131 mpVDev->SetAntialiasing(mpVDev->GetAntialiasing() & ~AntialiasingFlags::EnableB2dDraw);
132 #endif
136 void BitmapBackBuffer::updateVDev() const
138 OSL_ENSURE( !mbBitmapContentIsCurrent || !mbVDevContentIsCurrent,
139 "BitmapBackBuffer::updateVDev(): Both bitmap and VDev are valid?!" );
141 if( mpVDev && mbBitmapContentIsCurrent )
143 // fill with bitmap content
144 mpVDev->EnableMapMode( false );
145 mpVDev->SetAntialiasing( AntialiasingFlags::EnableB2dDraw );
146 const Point aEmptyPoint;
147 mpVDev->DrawBitmapEx( aEmptyPoint, *maBitmap );
150 // canvas queried the VDev, and will possibly paint into
151 // it. Next time, bitmap must be updated
152 mbBitmapContentIsCurrent = false;
153 mbVDevContentIsCurrent = true;
157 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */