Bump version to 6.4-15
[LibreOffice.git] / slideshow / source / engine / slide / layer.cxx
blobfce4c234e78a157edf05b558a420681e4907cd09
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 <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>
30 #include "layer.hxx"
32 using namespace ::com::sun::star;
34 namespace slideshow
36 namespace internal
38 Layer::Layer( Dummy ) :
39 maViewEntries(),
40 maBounds(),
41 maNewBounds(),
42 mbBoundsDirty(false),
43 mbBackgroundLayer(true),
44 mbClipSet(false)
48 Layer::Layer() :
49 maViewEntries(),
50 maBounds(),
51 maNewBounds(),
52 mbBoundsDirty(false),
53 mbBackgroundLayer(false),
54 mbClipSet(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(),
65 aEnd,
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 )
77 pNewLayer = rNewView;
78 else
79 pNewLayer = rNewView->createViewLayer(maBounds);
81 // add to local list
82 maViewEntries.emplace_back( rNewView,
83 pNewLayer );
85 return maViewEntries.back().mpViewLayer;
88 ViewLayerSharedPtr Layer::removeView( const ViewSharedPtr& rView )
90 OSL_ASSERT( rView );
92 ViewEntryVector::iterator aIter;
93 const ViewEntryVector::iterator aEnd( maViewEntries.end() );
94 if( (aIter=std::find_if( maViewEntries.begin(),
95 aEnd,
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(),
104 aEnd,
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);
112 return pRet;
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 )
144 if( !mbBoundsDirty )
145 maNewBounds.reset();
147 maNewBounds.expand( rShape->getUpdateArea() );
150 mbBoundsDirty = true;
153 bool Layer::commitBounds()
155 mbBoundsDirty = false;
157 if( mbBackgroundLayer )
158 return false;
160 if( maNewBounds == maBounds )
161 return false;
163 maBounds = maNewBounds;
164 if( std::count_if( maViewEntries.begin(),
165 maViewEntries.end(),
166 [this]( const ViewEntry& rViewEntry )
167 { return rViewEntry.getViewLayer()->resize( this->maBounds ); }
168 ) == 0 )
170 return false;
173 // layer content invalid, update areas have wrong
174 // coordinates/not sensible anymore.
175 clearUpdateRanges();
177 return true;
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
192 // anymore.
193 clearUpdateRanges();
196 class LayerEndUpdate
198 public:
199 LayerEndUpdate( const LayerEndUpdate& ) = delete;
200 LayerEndUpdate& operator=( const LayerEndUpdate& ) = delete;
201 explicit LayerEndUpdate( LayerSharedPtr const& rLayer ) :
202 mpLayer( rLayer )
205 ~LayerEndUpdate() { if(mpLayer) mpLayer->endUpdate(); }
207 private:
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.
225 if( aClip.count() )
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
235 pViewLayer->clear();
238 mbClipSet = true;
242 return std::make_shared<LayerEndUpdate>(shared_from_this());
245 void Layer::endUpdate()
247 if( mbClipSet )
249 mbClipSet = false;
251 basegfx::B2DPolyPolygon aEmptyClip;
252 for( const auto& rViewEntry : maViewEntries )
253 rViewEntry.getViewLayer()->setClip( aEmptyClip );
256 clearUpdateRanges();
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: */