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 .
21 #include <basegfx/range/b2drange.hxx>
22 #include <basegfx/range/b1drange.hxx>
23 #include <basegfx/range/b2dpolyrange.hxx>
24 #include <basegfx/matrix/b2dhommatrix.hxx>
25 #include <basegfx/polygon/b2dpolypolygon.hxx>
26 #include <basegfx/polygon/b2dpolypolygontools.hxx>
27 #include <basegfx/polygon/b2dpolypolygoncutter.hxx>
28 #include <osl/diagnose.h>
32 using namespace ::com::sun::star
;
38 Layer::Layer( Dummy
) :
43 mbBackgroundLayer(true),
53 mbBackgroundLayer(false),
58 ViewLayerSharedPtr
Layer::addView( const ViewSharedPtr
& rNewView
)
60 OSL_ASSERT( rNewView
);
62 ViewEntryVector::iterator aIter
;
63 const ViewEntryVector::iterator
aEnd( maViewEntries
.end() );
64 if( (aIter
=std::find_if( maViewEntries
.begin(),
66 [&rNewView
]( const ViewEntry
& rViewEntry
)
67 { return rViewEntry
.getView() == rNewView
; } ) ) != aEnd
)
69 // already added - just return existing layer
70 return aIter
->mpViewLayer
;
74 // not yet added - create new view layer
75 ViewLayerSharedPtr pNewLayer
;
76 if( mbBackgroundLayer
)
79 pNewLayer
= rNewView
->createViewLayer(maBounds
);
82 maViewEntries
.emplace_back( rNewView
,
85 return maViewEntries
.back().mpViewLayer
;
88 ViewLayerSharedPtr
Layer::removeView( const ViewSharedPtr
& rView
)
92 ViewEntryVector::iterator aIter
;
93 const ViewEntryVector::iterator
aEnd( maViewEntries
.end() );
94 if( (aIter
=std::find_if( maViewEntries
.begin(),
96 [&rView
]( const ViewEntry
& rViewEntry
)
97 { return rViewEntry
.getView() == rView
; } ) ) == aEnd
)
99 // View was not added/is already removed
100 return ViewLayerSharedPtr();
103 OSL_ENSURE( std::count_if( maViewEntries
.begin(),
105 [&rView
]( const ViewEntry
& rViewEntry
)
106 { return rViewEntry
.getView() == rView
; } ) == 1,
107 "Layer::removeView(): view added multiple times" );
109 ViewLayerSharedPtr
pRet( aIter
->mpViewLayer
);
110 maViewEntries
.erase(aIter
);
115 void Layer::setShapeViews( ShapeSharedPtr
const& rShape
) const
117 rShape
->clearAllViewLayers();
119 for( const auto& rViewEntry
: maViewEntries
)
120 rShape
->addViewLayer( rViewEntry
.getViewLayer(), false );
123 void Layer::setPriority( const ::basegfx::B1DRange
& rPrioRange
)
125 if( !mbBackgroundLayer
)
127 for( const auto& rViewEntry
: maViewEntries
)
128 rViewEntry
.getViewLayer()->setPriority( rPrioRange
);
132 void Layer::addUpdateRange( ::basegfx::B2DRange
const& rUpdateRange
)
134 // TODO(Q1): move this to B2DMultiRange
135 if( !rUpdateRange
.isEmpty() )
136 maUpdateAreas
.appendElement( rUpdateRange
,
137 basegfx::B2VectorOrientation::Positive
);
140 void Layer::updateBounds( ShapeSharedPtr
const& rShape
)
142 if( !mbBackgroundLayer
)
147 maNewBounds
.expand( rShape
->getUpdateArea() );
150 mbBoundsDirty
= true;
153 bool Layer::commitBounds()
155 mbBoundsDirty
= false;
157 if( mbBackgroundLayer
)
160 if( maNewBounds
== maBounds
)
163 maBounds
= maNewBounds
;
164 if( std::count_if( maViewEntries
.begin(),
166 [this]( const ViewEntry
& rViewEntry
)
167 { return rViewEntry
.getViewLayer()->resize( this->maBounds
); }
173 // layer content invalid, update areas have wrong
174 // coordinates/not sensible anymore.
180 void Layer::clearUpdateRanges()
182 maUpdateAreas
.clear();
185 void Layer::clearContent()
187 // clear content on all view layers
188 for( const auto& rViewEntry
: maViewEntries
)
189 rViewEntry
.getViewLayer()->clearAll();
191 // layer content cleared, update areas are not sensible
199 LayerEndUpdate( const LayerEndUpdate
& ) = delete;
200 LayerEndUpdate
& operator=( const LayerEndUpdate
& ) = delete;
201 explicit LayerEndUpdate( LayerSharedPtr
const& rLayer
) :
205 ~LayerEndUpdate() { if(mpLayer
) mpLayer
->endUpdate(); }
208 LayerSharedPtr mpLayer
;
211 Layer::EndUpdater
Layer::beginUpdate()
213 if( maUpdateAreas
.count() )
215 // perform proper layer update. That means, setup proper
216 // clipping, and render each shape that intersects with
217 // the calculated update area
218 ::basegfx::B2DPolyPolygon
aClip( maUpdateAreas
.solveCrossovers() );
219 aClip
= ::basegfx::utils::stripNeutralPolygons(aClip
);
220 aClip
= ::basegfx::utils::stripDispensablePolygons(aClip
);
222 // actually, if there happen to be shapes with zero
223 // update area in the maUpdateAreas vector, the
224 // resulting clip polygon will be empty.
227 for( const auto& rViewEntry
: maViewEntries
)
229 const ViewLayerSharedPtr
& pViewLayer
= rViewEntry
.getViewLayer();
231 // set clip to all view layers and
232 pViewLayer
->setClip( aClip
);
234 // clear update area on all view layers
242 return std::make_shared
<LayerEndUpdate
>(shared_from_this());
245 void Layer::endUpdate()
251 basegfx::B2DPolyPolygon aEmptyClip
;
252 for( const auto& rViewEntry
: maViewEntries
)
253 rViewEntry
.getViewLayer()->setClip( aEmptyClip
);
259 bool Layer::isInsideUpdateArea( ShapeSharedPtr
const& rShape
) const
261 return maUpdateAreas
.overlaps( rShape
->getUpdateArea() );
264 LayerSharedPtr
Layer::createBackgroundLayer()
266 return LayerSharedPtr(new Layer( BackgroundLayer
));
269 LayerSharedPtr
Layer::createLayer( )
271 return LayerSharedPtr( new Layer
);
277 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */