1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_canvas.hxx"
26 
27 #include "bitmapbackbuffer.hxx"
28 
29 #include <osl/mutex.hxx>
30 #include <vos/mutex.hxx>
31 
32 #include <vcl/svapp.hxx>
33 #include <vcl/bitmapex.hxx>
34 #include <vcl/bmpacc.hxx>
35 
36 
37 namespace vclcanvas
38 {
BitmapBackBuffer(const BitmapEx & rBitmap,const OutputDevice & rRefDevice)39     BitmapBackBuffer::BitmapBackBuffer( const BitmapEx& 		rBitmap,
40                                         const OutputDevice& 	rRefDevice ) :
41         maBitmap( rBitmap ),
42         mpVDev( NULL ),
43         mrRefDevice( rRefDevice ),
44         mbBitmapContentIsCurrent( false ),
45         mbVDevContentIsCurrent( false )
46     {
47     }
48 
~BitmapBackBuffer()49     BitmapBackBuffer::~BitmapBackBuffer()
50     {
51         // make sure solar mutex is held on deletion (other methods
52         // are supposed to be called with already locked solar mutex)
53         ::vos::OGuard aGuard( Application::GetSolarMutex() );
54 
55         if( mpVDev )
56             delete mpVDev;
57     }
58 
getOutDev()59     OutputDevice& BitmapBackBuffer::getOutDev()
60     {
61         createVDev();
62         updateVDev();
63         return *mpVDev;
64     }
65 
getOutDev() const66     const OutputDevice& BitmapBackBuffer::getOutDev() const
67     {
68         createVDev();
69         updateVDev();
70         return *mpVDev;
71     }
72 
clear()73     void BitmapBackBuffer::clear()
74     {
75         // force current content to bitmap, make all transparent white
76         getBitmapReference().Erase(COL_TRANSPARENT);
77     }
78 
getBitmapReference()79     BitmapEx& BitmapBackBuffer::getBitmapReference()
80     {
81         OSL_ENSURE( !mbBitmapContentIsCurrent || !mbVDevContentIsCurrent,
82                     "BitmapBackBuffer::getBitmapReference(): Both bitmap and VDev are valid?!" );
83 
84         if( mbVDevContentIsCurrent && mpVDev )
85         {
86             // VDev content is more current than bitmap - copy contents before!
87             mpVDev->EnableMapMode( sal_False );
88             mpVDev->SetAntialiasing( ANTIALIASING_ENABLE_B2DDRAW );
89             const Point aEmptyPoint;
90             *maBitmap = mpVDev->GetBitmapEx( aEmptyPoint,
91                                              mpVDev->GetOutputSizePixel() );
92         }
93 
94         // client queries bitmap, and will possibly alter content -
95         // next time, VDev needs to be updated
96         mbBitmapContentIsCurrent = true;
97         mbVDevContentIsCurrent 	 = false;
98 
99         return *maBitmap;
100     }
101 
getBitmapSizePixel() const102     Size BitmapBackBuffer::getBitmapSizePixel() const
103     {
104         Size aSize = maBitmap->GetSizePixel();
105 
106         if( mbVDevContentIsCurrent && mpVDev )
107         {
108             mpVDev->EnableMapMode( sal_False );
109             mpVDev->SetAntialiasing( ANTIALIASING_ENABLE_B2DDRAW );
110             aSize = mpVDev->GetOutputSizePixel();
111         }
112 
113         return aSize;
114     }
115 
createVDev() const116     void BitmapBackBuffer::createVDev() const
117     {
118         if( !mpVDev )
119         {
120             // VDev not yet created, do it now. Create an alpha-VDev,
121             // if bitmap has transparency.
122             mpVDev = maBitmap->IsTransparent() ?
123                 new VirtualDevice( mrRefDevice, 0, 0 ) :
124                 new VirtualDevice( mrRefDevice );
125 
126             OSL_ENSURE( mpVDev,
127                         "BitmapBackBuffer::createVDev(): Unable to create VirtualDevice" );
128 
129             mpVDev->SetOutputSizePixel( maBitmap->GetSizePixel() );
130 
131             // #i95645#
132 #if defined( QUARTZ )
133             // use AA on VCLCanvas for Mac
134 			mpVDev->SetAntialiasing( ANTIALIASING_ENABLE_B2DDRAW | mpVDev->GetAntialiasing() );
135 #else
136             // switch off AA for WIN32 and UNIX, the VCLCanvas does not look good with it and
137             // is not required to do AA. It would need to be adapted to use it correctly
138             // (especially gradient painting). This will need extra work.
139 			mpVDev->SetAntialiasing(mpVDev->GetAntialiasing() & ~ANTIALIASING_ENABLE_B2DDRAW);
140 #endif
141         }
142     }
143 
updateVDev() const144     void BitmapBackBuffer::updateVDev() const
145     {
146         OSL_ENSURE( !mbBitmapContentIsCurrent || !mbVDevContentIsCurrent,
147                     "BitmapBackBuffer::updateVDev(): Both bitmap and VDev are valid?!" );
148 
149         if( mpVDev && mbBitmapContentIsCurrent )
150         {
151             // fill with bitmap content
152             mpVDev->EnableMapMode( sal_False );
153             mpVDev->SetAntialiasing( ANTIALIASING_ENABLE_B2DDRAW );
154             const Point aEmptyPoint;
155             mpVDev->DrawBitmapEx( aEmptyPoint, *maBitmap );
156         }
157 
158         // canvas queried the VDev, and will possibly paint into
159         // it. Next time, bitmap must be updated
160         mbBitmapContentIsCurrent = false;
161         mbVDevContentIsCurrent 	 = true;
162     }
163 }
164 
165