KiCad PCB EDA Suite
Loading...
Searching...
No Matches
view.cpp
Go to the documentation of this file.
1/*
2 * This program source code file is part of KiCad, a free EDA CAD application.
3 *
4 * Copyright (C) 2013-2017 CERN
5 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
6 *
7 * @author Tomasz Wlostowski <[email protected]>
8 * @author Maciej Suminski <[email protected]>
9 *
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22 */
23
24
25#include <layer_ids.h>
26#include <trace_helpers.h>
27#include <wx/log.h>
28
29#include <view/view.h>
30#include <view/view_group.h>
31#include <view/view_item.h>
32#include <view/view_rtree.h>
33#include <view/view_overlay.h>
34
35#include <gal/definitions.h>
37#include <gal/painter.h>
39#include <algorithm>
40#include <array>
41#include <chrono>
42#include <cinttypes>
43#include <cmath>
44#include <limits>
45#include <tuple>
46#include <unordered_map>
47#include <unordered_set>
48
49#include <core/profile.h>
50
51
52#ifdef KICAD_GAL_PROFILE
53#include <wx/log.h>
54#include <trace_helpers.h>
55#endif
56
57namespace KIGFX {
58
59class VIEW;
60
62{
63public:
65 m_view( nullptr ),
68 m_pendingIndex( -1 ),
69 m_drawPriority( 0 ),
70 m_cachedIndex( -1 ),
71 m_groups( nullptr ),
72 m_groupsSize( 0 ) {}
73
75 {
77 }
78
79 int GetFlags() const
80 {
81 return m_flags;
82 }
83
84private:
85 friend class VIEW;
86
93 int getGroup( int aLayer ) const
94 {
95 for( int i = 0; i < m_groupsSize; ++i )
96 {
97 if( m_groups[i].first == aLayer )
98 return m_groups[i].second;
99 }
100
101 return -1;
102 }
103
110 void setGroup( int aLayer, int aGroup )
111 {
112 // Look if there is already an entry for the layer
113 for( int i = 0; i < m_groupsSize; ++i )
114 {
115 if( m_groups[i].first == aLayer )
116 {
117 m_groups[i].second = aGroup;
118 return;
119 }
120 }
121
122 // If there was no entry for the given layer - create one
123 std::pair<int, int>* newGroups = new std::pair<int, int>[m_groupsSize + 1];
124
125 if( m_groupsSize > 0 )
126 {
127 std::copy( m_groups, m_groups + m_groupsSize, newGroups );
128 delete[] m_groups;
129 }
130
131 m_groups = newGroups;
132 newGroups[m_groupsSize++] = { aLayer, aGroup };
133 }
134
135
140 {
141 delete[] m_groups;
142 m_groups = nullptr;
143 m_groupsSize = 0;
144 }
145
151 inline bool storesGroups() const
152 {
153 return m_groupsSize > 0;
154 }
155
163 void reorderGroups( const std::unordered_map<int, int>& aReorderMap )
164 {
165 for( int i = 0; i < m_groupsSize; ++i )
166 {
167 auto it = aReorderMap.find( m_groups[i].first );
168
169 if( it != aReorderMap.end() )
170 m_groups[i].first = it->second;
171 }
172 }
173
179 void saveLayers( const std::vector<int>& aLayers )
180 {
181 m_layers.clear();
182
183 for( int layer : aLayers )
184 {
185 wxCHECK2_MSG( layer >= 0 && layer < VIEW::VIEW_MAX_LAYERS, continue,
186 wxString::Format( wxT( "Invalid layer number: %d" ), layer ) );
187 m_layers.push_back( layer );
188 }
189 }
190
194 int requiredUpdate() const
195 {
196 return m_requiredUpdate;
197 }
198
203 {
205 }
206
210 bool isRenderable() const
211 {
212 return m_flags == VISIBLE;
213 }
214
218 int m_pendingIndex; // Place in the view's pending list, or -1
221
222 std::pair<int, int>* m_groups;
225
226 std::vector<int> m_layers;
227
229};
230
231
239
240
241static RETAINED_GROUP_ORDER retainedOrderKey( VIEW_ITEM* aItem, int aPriority, const BOX2I& aBBox,
242 bool aUseDrawPriority, bool aReverseDrawOrder,
243 double aLodScale )
244{
246 key.priority = aUseDrawPriority ? ( aReverseDrawOrder ? -int64_t( aPriority ) : int64_t( aPriority ) ) : 0;
247
248 key.bbox = { aBBox.GetLeft(), aBBox.GetTop(), aBBox.GetRight(), aBBox.GetBottom() };
249 key.className = aItem->GetClass();
250 key.lodScale = std::isnan( aLodScale ) ? std::numeric_limits<double>::infinity() : aLodScale;
251
252 return key;
253}
254
255
256template <typename T>
257static void sortRetainedOrder( VIEW* aView, std::vector<T>& aEntries )
258{
259 std::sort( aEntries.begin(), aEntries.end(), []( const T& a, const T& b )
260 {
261 return a.order < b.order;
262 } );
263
264 for( size_t first = 0; first < aEntries.size(); )
265 {
266 size_t last = first + 1;
267
268 while( last < aEntries.size() && aEntries[first].order.SamePrefix( aEntries[last].order ) )
269 ++last;
270
271 if( last - first > 1 )
272 {
273 for( size_t i = first; i < last; ++i )
274 {
275 T& entry = aEntries[i];
276 entry.order.contentHash = aView->GetGAL()->GroupContentFingerprint( entry.group );
277 entry.order.contentTie = true;
278 }
279
280 std::sort( aEntries.begin() + first, aEntries.begin() + last,
281 []( const T& a, const T& b ) { return a.order < b.order; } );
282 }
283
284 first = last;
285 }
286}
287
288
290{
291 if( aItem->m_viewPrivData )
292 {
293 if( aItem->m_viewPrivData->m_view )
294 aItem->m_viewPrivData->m_view->unlinkItem( aItem );
295
296 delete aItem->m_viewPrivData;
297 aItem->m_viewPrivData = nullptr;
298 }
299}
300
301
303 m_enableOrderModifier( true ),
304 m_scale( 4.0 ),
305 m_minScale( 0.2 ), m_maxScale( 50000.0 ),
306 m_mirrorX( false ), m_mirrorY( false ),
309 m_painter( nullptr ),
310 m_gal( nullptr ),
311 m_useDrawPriority( false ),
313 m_reverseDrawOrder( false ),
315 m_cameraDirtyOnly( false )
316{
317 // Set m_boundary to define the max area size. The default area size
318 // is defined here as the max value of a int.
319 // this is a default value acceptable for Pcbnew and Gerbview, but too large for Eeschema.
320 // So in eeschema a call to SetBoundary() with a smaller value will be needed.
321 typedef std::numeric_limits<int> coord_limits;
322 double pos = coord_limits::lowest() / 2 + coord_limits::epsilon();
323 double size = coord_limits::max() - coord_limits::epsilon();
324 m_boundary.SetOrigin( pos, pos );
325 m_boundary.SetSize( size, size );
326
327 m_allItems.reset( new std::vector<VIEW_ITEM*> );
328 m_allItems->reserve( 32768 );
329
330 // Redraw everything at the beginning
331 MarkDirty();
332
333 // View uses layers to display EDA_ITEMs (item may be displayed on several layers, for example
334 // pad may be shown on pad, pad hole and solder paste layers). There are usual copper layers
335 // (eg. F.Cu, B.Cu, internal and so on) and layers for displaying objects such as texts,
336 // silkscreen, pads, vias, etc.
337 for( int ii = 0; ii < VIEW_MAX_LAYERS; ++ii )
338 {
339 auto [it, _] = m_layers.emplace( ii, VIEW_LAYER() );
340 VIEW_LAYER& l = it->second;
341
342 l.items = std::make_shared<VIEW_RTREE>();
343 l.id = ii;
344 l.renderingOrder = ii;
345 l.visible = true;
346 l.displayOnly = false;
347 l.diffLayer = false;
348 l.hasNegatives = false;
350 }
351
353
354 m_preview.reset( new KIGFX::VIEW_GROUP() );
355 Add( m_preview.get() );
356}
357
358
360{
361 Remove( m_preview.get() );
362}
363
364
365void VIEW::Add( VIEW_ITEM* aItem, int aDrawPriority )
366{
367 if( aDrawPriority < 0 )
368 aDrawPriority = m_nextDrawPriority++;
369
370 if( !aItem->m_viewPrivData )
371 aItem->m_viewPrivData = new VIEW_ITEM_DATA;
372
373 // One view pointer and one index per item, so re-registering strands the first entry to
374 // dangle at free time
375 if( VIEW* previous = aItem->m_viewPrivData->m_view )
376 previous->unlinkItem( aItem );
377
378 aItem->m_viewPrivData->m_view = this;
379 aItem->m_viewPrivData->m_pendingIndex = -1;
380 aItem->m_viewPrivData->m_drawPriority = aDrawPriority;
381 const BOX2I bbox = aItem->ViewBBox();
382 aItem->m_viewPrivData->m_bbox = bbox;
383 aItem->m_viewPrivData->m_cachedIndex = m_allItems->size();
384
385 std::vector<int> layers = aItem->ViewGetLayers();
386
387 std::erase_if( layers, []( int layer )
388 {
389 return layer < 0 || layer >= VIEW_MAX_LAYERS;
390 } );
391
392 if( layers.empty() )
393 return;
394
395 aItem->viewPrivData()->saveLayers( layers );
396
397 m_allItems->push_back( aItem );
398
399 for( int layer : layers )
400 {
401 VIEW_LAYER& l = m_layers[layer];
402 l.items->Insert( aItem, bbox );
404 }
405
406 // The camera-reuse path would replay a scene without this item.
408
409 SetVisible( aItem, true );
410 Update( aItem, KIGFX::INITIAL_ADD );
411}
412
413
414void VIEW::AddBatch( const std::vector<VIEW_ITEM*>& aItems )
415{
416 // Phase 1: Register all items and collect per-layer data
417 std::unordered_map<int, std::vector<std::pair<VIEW_ITEM*, BOX2I>>> layerBulk;
418 std::unordered_set<VIEW_ITEM*> seen;
419 std::vector<VIEW_ITEM*> registered;
420
421 seen.reserve( aItems.size() );
422 registered.reserve( aItems.size() );
423
424 for( VIEW_ITEM* item : aItems )
425 {
426 // A repeat within the batch cannot be detached below because its layer entries are not
427 // inserted until phase 2, so it would reach BulkLoad twice
428 if( !item || !seen.insert( item ).second )
429 continue;
430
431 int drawPriority = m_nextDrawPriority++;
432
433 if( !item->m_viewPrivData )
434 item->m_viewPrivData = new VIEW_ITEM_DATA;
435
436 // Same single-registration invariant as Add()
437 if( VIEW* previous = item->m_viewPrivData->m_view )
438 previous->unlinkItem( item );
439
440 item->m_viewPrivData->m_view = this;
441 item->m_viewPrivData->m_pendingIndex = -1;
442 item->m_viewPrivData->m_drawPriority = drawPriority;
443 const BOX2I bbox = item->ViewBBox();
444 item->m_viewPrivData->m_bbox = bbox;
445 item->m_viewPrivData->m_cachedIndex = m_allItems->size();
446
447 std::vector<int> layers = item->ViewGetLayers();
448
449 std::erase_if( layers, []( int layer )
450 {
451 return layer < 0 || layer >= VIEW_MAX_LAYERS;
452 } );
453
454 if( layers.empty() )
455 continue;
456
457 item->viewPrivData()->saveLayers( layers );
458 m_allItems->push_back( item );
459 registered.push_back( item );
460
461 for( int layer : layers )
462 layerBulk[layer].emplace_back( item, bbox );
463 }
464
465 // Phase 2: Bulk load each layer's R-tree
466 for( auto& [layerId, items] : layerBulk )
467 {
468 VIEW_LAYER& l = m_layers[layerId];
469 l.items->BulkLoad( items );
471 }
472
473 if( !aItems.empty() )
475
476 // Phase 3: Set visibility and queue initial invalidation.
477 // INITIAL_ADD is required even though VIEW_ITEM_DATA defaults VISIBLE=true (making
478 // SetVisible a no-op). Without it, items reused after VIEW::Clear() retain stale GAL
479 // cache group IDs that point to freed memory.
480 for( VIEW_ITEM* item : registered )
481 {
482 SetVisible( item, true );
483 Update( item, KIGFX::INITIAL_ADD );
484 }
485}
486
487
489{
490 unlinkItem( aItem );
491}
492
493
495{
496 static int s_gcCounter = 0;
497
498 if( aItem && aItem->m_viewPrivData )
499 {
500 if( aItem->m_viewPrivData->m_view != nullptr && aItem->m_viewPrivData->m_view != this )
501 {
502 return;
503 }
504
505 std::vector<VIEW_ITEM*>::iterator item = m_allItems->end();
506 int cachedIndex = aItem->m_viewPrivData->m_cachedIndex;
507
508 if( cachedIndex >= 0
509 && cachedIndex < static_cast<ssize_t>( m_allItems->size() )
510 && ( *m_allItems )[cachedIndex] == aItem )
511 {
512 item = m_allItems->begin() + cachedIndex;
513 }
514 else
515 {
516 item = std::find( m_allItems->begin(), m_allItems->end(), aItem );
517 }
518
519 if( aItem->m_viewPrivData->m_pendingIndex >= 0 )
520 {
522 aItem->m_viewPrivData->m_pendingIndex = -1;
523 }
524
525 if( item != m_allItems->end() )
526 {
527 *item = nullptr;
529
530 s_gcCounter++;
531
532 if( s_gcCounter > 4096 )
533 {
534 // Perform defragmentation
535 std::erase_if( *m_allItems,
536 []( VIEW_ITEM* it )
537 {
538 return it == nullptr;
539 } );
540
541 // Update cached indices
542 for( size_t idx = 0; idx < m_allItems->size(); idx++ )
543 ( *m_allItems )[idx]->m_viewPrivData->m_cachedIndex = idx;
544
545 s_gcCounter = 0;
546 }
547 }
548
549 const BOX2I* bbox = &aItem->m_viewPrivData->m_bbox;
550
551 for( int layer : aItem->m_viewPrivData->m_layers )
552 {
553 VIEW_LAYER& l = m_layers[layer];
554 l.items->Remove( aItem, bbox );
556
557 // Clear the GAL cache
558 int prevGroup = aItem->m_viewPrivData->getGroup( layer );
559
560 if( prevGroup >= 0 )
561 m_gal->DeleteGroup( prevGroup );
562 }
563
565 aItem->m_viewPrivData->m_view = nullptr;
566
567 // The camera-reuse path would still draw the removed item.
569 }
570}
571
572
573void VIEW::SetRequired( int aLayerId, int aRequiredId, bool aRequired )
574{
575 wxCHECK( (unsigned) aLayerId < m_layers.size(), /*void*/ );
576 wxCHECK( (unsigned) aRequiredId < m_layers.size(), /*void*/ );
577
578 std::set<int>& required = m_layers[aLayerId].requiredLayers;
579 const bool changed = aRequired ? required.insert( aRequiredId ).second : required.erase( aRequiredId ) > 0;
580
581 // redrawRect() skips layers whose required layers are hidden, so the cached output is stale.
582 if( changed )
583 layerStateChanged( aLayerId );
584}
585
586
587void VIEW::layerStateChanged( int aLayer )
588{
589 if( !m_gal || !m_gal->HasRetainedScene() )
590 {
591 if( IsCached( aLayer ) )
592 {
595 }
596
597 return;
598 }
599
600 m_sceneLayersStale = true;
601 m_lodRefreshLayers.push_back( aLayer );
602
603 // Items of the kept non-cached targets can read the layer's visibility as well
608}
609
610
611void VIEW::noteLodRead( int aLayer ) const
612{
613 if( aLayer < 0 || aLayer >= VIEW_MAX_LAYERS )
614 return;
615
616 std::vector<bool>& readers = m_lodReads[aLayer];
617
618 if( readers.empty() )
619 readers.resize( VIEW_MAX_LAYERS );
620
621 readers[m_lodProbeLayer] = true;
622}
623
624
626{
627 if( !m_gal || !m_gal->HasRetainedScene() )
628 return;
629
630 m_gal->SetSceneScale( m_scale );
631 m_gal->SetSceneDrawOrdered( m_useDrawPriority );
632
634 {
635 for( const auto& [id, l] : m_layers )
636 {
637 const bool drawn = l.target == TARGET_CACHED && l.visible && areRequiredLayersEnabled( id );
638
639 m_gal->SetSceneLayerState( id, l.renderingOrder, drawn, l.hasNegatives );
640
641 COLOR4D toward;
642 double keep = 1.0;
643
644 if( l.target == TARGET_CACHED && m_painter && m_painter->GetSettings()->GetLayerDim( id, toward, keep ) )
645 m_gal->SetSceneLayerDim( id, toward, keep );
646 else
647 m_gal->SetSceneLayerDim( id, COLOR4D::BLACK, 1.0 );
648 }
649
650 m_sceneLayersStale = false;
651 }
652
653 if( m_lodRefreshLayers.empty() )
654 return;
655
656 // A rebuild asks every item anyway
657 if( m_gal->HasStaticRebuildPending() || m_lodReads.empty() )
658 {
659 m_lodRefreshLayers.clear();
660 return;
661 }
662
663 if( !m_gal->IsVisible() || !m_gal->IsInitialized() )
664 return;
665
667
668 if( !ctx.IsUpdating() )
669 return;
670
672
673 std::vector<bool> readers( VIEW_MAX_LAYERS );
674
675 for( int read : m_lodRefreshLayers )
676 {
677 auto it = m_lodReads.find( read );
678
679 if( it == m_lodReads.end() )
680 continue;
681
682 for( int reader = 0; reader < VIEW_MAX_LAYERS; ++reader )
683 {
684 if( it->second[reader] )
685 readers[reader] = true;
686 }
687 }
688
689 m_lodRefreshLayers.clear();
690
691 BOX2I everywhere;
692 everywhere.SetMaximum();
693
694 for( auto& [id, l] : m_layers )
695 {
696 if( !readers[id] || l.target != TARGET_CACHED )
697 continue;
698
699 const int layer = id;
700
701 auto place = [&]( VIEW_ITEM* aItem ) -> bool
702 {
703 placeCachedGroup( aItem, layer );
704 return true;
705 };
706
707 l.items->Query( everywhere, place );
708 }
709}
710
711
713{
714 if( !m_gal )
715 return;
716
717 m_gal->MarkStaticDirty();
718
719 // Whatever rebuilds the cached scene, such as showing a layer, can also change what the kept
720 // non-cached targets draw
721 if( m_gal->HasRetainedScene() )
722 {
725 m_sceneLayersStale = true;
726 }
727}
728
729
731{
732 // Slots are handed out in arrival order, which cannot follow a draw priority
733 if( !m_gal || !m_gal->HasRetainedScene() || m_useDrawPriority )
734 {
736 return;
737 }
738
739 m_cameraDirtyOnly = false;
740}
741
742
743int VIEW::Query( const BOX2I& aRect, std::vector<LAYER_ITEM_PAIR>& aResult ) const
744{
745 if( m_orderedLayers.empty() )
746 return 0;
747
748 int layer = UNDEFINED_LAYER;
749 auto visitor =
750 [&]( VIEW_ITEM* item ) -> bool
751 {
752 aResult.push_back( VIEW::LAYER_ITEM_PAIR( item, layer ) );
753 return true;
754 };
755
756 std::vector<VIEW_LAYER*>::const_reverse_iterator i;
757
758 // execute queries in reverse direction, so that items that are on the top of
759 // the rendering stack are returned first.
760 for( i = m_orderedLayers.rbegin(); i != m_orderedLayers.rend(); ++i )
761 {
762 // ignore layers that do not contain actual items (i.e. the selection box, menus, floats)
763 if( ( *i )->displayOnly || !( *i )->visible )
764 continue;
765
766 layer = ( *i )->id;
767 ( *i )->items->Query( aRect, visitor );
768 }
769
770 return aResult.size();
771}
772
773
774void VIEW::Query( const BOX2I& aRect, const std::function<bool( VIEW_ITEM* )>& aFunc ) const
775{
776 if( m_orderedLayers.empty() )
777 return;
778
779 for( const auto& i : m_orderedLayers )
780 {
781 // ignore layers that do not contain actual items (i.e. the selection box, menus, floats)
782 if( i->displayOnly || !i->visible )
783 continue;
784
785 i->items->Query( aRect, aFunc );
786 }
787}
788
789
790VECTOR2D VIEW::ToWorld( const VECTOR2D& aCoord, bool aAbsolute ) const
791{
792 const MATRIX3x3D& matrix = m_gal->GetScreenWorldMatrix();
793
794 if( aAbsolute )
795 return VECTOR2D( matrix * aCoord );
796 else
797 return VECTOR2D( matrix.GetScale().x * aCoord.x, matrix.GetScale().y * aCoord.y );
798}
799
800
801double VIEW::ToWorld( double aSize ) const
802{
803 const MATRIX3x3D& matrix = m_gal->GetScreenWorldMatrix();
804
805 return fabs( matrix.GetScale().x * aSize );
806}
807
808
809VECTOR2D VIEW::ToScreen( const VECTOR2D& aCoord, bool aAbsolute ) const
810{
811 const MATRIX3x3D& matrix = m_gal->GetWorldScreenMatrix();
812
813 if( aAbsolute )
814 return VECTOR2D( matrix * aCoord );
815 else
816 return VECTOR2D( matrix.GetScale().x * aCoord.x, matrix.GetScale().y * aCoord.y );
817}
818
819
820double VIEW::ToScreen( double aSize ) const
821{
822 const MATRIX3x3D& matrix = m_gal->GetWorldScreenMatrix();
823
824 return matrix.GetScale().x * aSize;
825}
826
827
828void VIEW::CopySettings( const VIEW* aOtherView )
829{
830 wxASSERT_MSG( false, wxT( "This is not implemented" ) );
831}
832
833
834void VIEW::SetGAL( GAL* aGal )
835{
836 bool recacheGroups = ( m_gal != nullptr ); // recache groups only if GAL is reassigned
837 m_gal = aGal;
838
839 // A new retained GAL knows no layer state and would show every layer
840 m_sceneLayersStale = true;
841
842 // clear group numbers, so everything is going to be recached
843 if( recacheGroups )
844 {
847 }
848
849 // every target has to be refreshed
850 MarkDirty();
851
852 // SetScale() would derive the centre from a GAL still at its default zoom, shifting the view a
853 // little on every backend switch.
855 m_gal->SetZoomFactor( m_scale );
857}
858
859
861{
862 BOX2D rect;
863 VECTOR2D screenSize = m_gal->GetScreenPixelSize();
864
865 rect.SetOrigin( ToWorld( VECTOR2D( 0, 0 ) ) );
866 rect.SetEnd( ToWorld( screenSize ) );
867
868 return rect.Normalize();
869}
870
871
872void VIEW::SetViewport( const BOX2D& aViewport )
873{
874 VECTOR2D ssize = ToWorld( m_gal->GetScreenPixelSize(), false );
875
876 wxCHECK( fabs(ssize.x) > 0 && fabs(ssize.y) > 0, /*void*/ );
877
878 VECTOR2D centre = aViewport.Centre();
879 VECTOR2D vsize = aViewport.GetSize();
880 double zoom = 1.0 / std::max( fabs( vsize.x / ssize.x ), fabs( vsize.y / ssize.y ) );
881
882 SetCenter( centre );
883 SetScale( GetScale() * zoom );
884}
885
886
887void VIEW::SetMirror( bool aMirrorX, bool aMirrorY )
888{
889 wxASSERT_MSG( !aMirrorY, _( "Mirroring for Y axis is not supported yet" ) );
890
891 m_mirrorX = aMirrorX;
892 m_mirrorY = aMirrorY;
893 m_gal->SetFlip( aMirrorX, aMirrorY );
894
895 // Redraw everything
896 MarkDirty();
897}
898
899
900void VIEW::SetScale( double aScale, VECTOR2D aAnchor )
901{
902 if( aAnchor == VECTOR2D( 0, 0 ) )
903 aAnchor = m_center;
904
905 VECTOR2D a = ToScreen( aAnchor );
906 const double oldScale = m_scale;
907
908 if( aScale < m_minScale )
910 else if( aScale > m_maxScale )
912 else
913 m_scale = aScale;
914
915 m_gal->SetZoomFactor( m_scale );
916 m_gal->ComputeWorldScreenMatrix();
917
918 VECTOR2D delta = ToWorld( a ) - aAnchor;
919
921
922 // Redraw everything after the viewport has changed
924
925 if( m_scale != oldScale )
927}
928
929
930bool VIEW::IsShownAtScale( double aLod ) const
931{
932 noteKeptLod( aLod );
933
934 return aLod < m_scale;
935}
936
937
938void VIEW::noteKeptLod( double aLod ) const
939{
940 // A kept target has to be emitted again once the scale crosses one of these
941 if( !m_keptScaleUse || aLod == VIEW_ITEM::LOD_SHOW || aLod == VIEW_ITEM::LOD_HIDE )
942 return;
943
944 if( aLod < m_scale )
945 m_keptScaleUse->lodBelow = std::max( m_keptScaleUse->lodBelow, aLod );
946 else
947 m_keptScaleUse->lodAbove = std::min( m_keptScaleUse->lodAbove, aLod );
948}
949
950
952{
953 if( !m_gal || !m_gal->HasRetainedScene() )
954 return;
955
956 for( int target : { TARGET_NONCACHED, TARGET_OVERLAY } )
957 {
958 const SCALE_USE& use = m_scaleUse[target];
959
960 // A pan reuses the target, and so does a zoom that changes nothing it holds
962 {
963 MarkTargetDirty( target );
964 m_cameraDirtyOnly = false;
965 }
966 }
967}
968
969
970void VIEW::SetCenter( const VECTOR2D& aCenter )
971{
972 m_center = aCenter;
973
974 if( !m_boundary.Contains( aCenter ) )
975 {
976 if( m_center.x < m_boundary.GetLeft() )
977 m_center.x = m_boundary.GetLeft();
978 else if( aCenter.x > m_boundary.GetRight() )
979 m_center.x = m_boundary.GetRight();
980
981 if( m_center.y < m_boundary.GetTop() )
982 m_center.y = m_boundary.GetTop();
983 else if( m_center.y > m_boundary.GetBottom() )
984 m_center.y = m_boundary.GetBottom();
985 }
986
987 m_gal->SetLookAtPoint( m_center );
988 m_gal->ComputeWorldScreenMatrix();
989
990 // Redraw everything after the viewport has changed
992}
993
994
995void VIEW::SetCenter( const VECTOR2D& aCenter, const std::vector<BOX2D>& obscuringScreenRects )
996{
997 if( obscuringScreenRects.empty() )
998 return SetCenter( aCenter );
999
1000 BOX2D screenRect( { 0, 0 }, m_gal->GetScreenPixelSize() );
1001 SHAPE_POLY_SET unobscuredPoly( screenRect );
1002 VECTOR2D unobscuredCenter = screenRect.Centre();
1003
1004 for( const BOX2D& obscuringScreenRect : obscuringScreenRects )
1005 {
1006 SHAPE_POLY_SET obscuringPoly( obscuringScreenRect );
1007 unobscuredPoly.BooleanSubtract( obscuringPoly );
1008 }
1009
1010 /*
1011 * Perform a step-wise deflate to find the center of the largest unobscured area
1012 */
1013
1014 BOX2I bbox = unobscuredPoly.BBox();
1015 int step = std::min( bbox.GetWidth(), bbox.GetHeight() ) / 10;
1016
1017 if( step < 20 )
1018 step = 20;
1019
1020 while( !unobscuredPoly.IsEmpty() )
1021 {
1022 unobscuredCenter = unobscuredPoly.BBox().Centre();
1024 }
1025
1026 SetCenter( aCenter - ToWorld( unobscuredCenter - screenRect.Centre(), false ) );
1027}
1028
1029
1030void VIEW::SetLayerOrder( int aLayer, int aRenderingOrder, bool aAutoSort )
1031{
1032 m_layers[aLayer].renderingOrder = aRenderingOrder;
1033
1034 if( aAutoSort )
1036}
1037
1038
1039int VIEW::GetLayerOrder( int aLayer ) const
1040{
1041 return m_layers.at( aLayer ).renderingOrder;
1042}
1043
1044
1045void VIEW::SortLayers( std::vector<int>& aLayers ) const
1046{
1047 std::sort( aLayers.begin(), aLayers.end(),
1048 [this]( int a, int b )
1049 {
1050 return GetLayerOrder( a ) > GetLayerOrder( b );
1051 } );
1052}
1053
1054
1055void VIEW::ReorderLayerData( std::unordered_map<int, int> aReorderMap )
1056{
1057 // GerbView's remapping is not a permutation once an image is deleted, so permute in place.
1058 // Rebuilding the map can drop an id, and m_layers[] then default-constructs a null R-tree
1059 std::map<int, VIEW_LAYER> moved;
1060 std::unordered_set<int> destinations;
1061
1062 for( const auto& [from, to] : aReorderMap )
1063 {
1064 if( from == to )
1065 continue;
1066
1067 auto source = m_layers.find( from );
1068
1069 if( source == m_layers.end() || !m_layers.count( to ) )
1070 continue;
1071
1072 moved.emplace( from, source->second );
1073 destinations.insert( to );
1074 }
1075
1076 // Every mover was copied before any of them was overwritten, so these can run in any order
1077 for( const auto& [from, layer] : moved )
1078 {
1079 auto destination = m_layers.find( aReorderMap.at( from ) );
1080
1081 destination->second = layer;
1082 destination->second.id = destination->first;
1083 }
1084
1085 // A source nothing moves onto keeps its settings but must own an empty tree rather than the
1086 // one it just handed to its destination
1087 for( const auto& [from, _] : moved )
1088 {
1089 if( !destinations.count( from ) )
1090 m_layers.find( from )->second.items = std::make_shared<VIEW_RTREE>();
1091 }
1092
1093 // Visibility and targets moved between ids
1094 m_layerCachesStale = true;
1095
1097
1098 // A recolor leaves each retained group on the scene layer of its old id
1099 if( m_gal && m_gal->HasRetainedScene() )
1101
1102 for( VIEW_ITEM* item : *m_allItems )
1103 {
1104 if( !item )
1105 continue;
1106
1107 VIEW_ITEM_DATA* viewData = item->viewPrivData();
1108
1109 if( !viewData )
1110 continue;
1111
1112 std::vector<int> layers = item->ViewGetLayers();
1113 viewData->saveLayers( layers );
1114
1115 viewData->reorderGroups( aReorderMap );
1116
1117 viewData->m_requiredUpdate |= COLOR;
1120 }
1121
1122 UpdateItems();
1123}
1124
1125
1126void VIEW::UpdateLayerColor( int aLayer )
1127{
1128 // There is no point in updating non-cached layers
1129 if( !IsCached( aLayer ) )
1130 return;
1131
1132 // The layer's fade can follow the same settings as its colours
1133 m_sceneLayersStale = true;
1134
1135 BOX2I r;
1136
1137 r.SetMaximum();
1138
1139 if( m_gal->IsVisible() )
1140 {
1142
1143 if( ctx.IsUpdating() )
1144 {
1145 std::vector<std::pair<VIEW_ITEM*, int>> groups;
1146
1147 auto collect = [&]( VIEW_ITEM* aItem )
1148 {
1149 if( int group = aItem->viewPrivData()->getGroup( aLayer ); group >= 0 )
1150 groups.emplace_back( aItem, group );
1151
1152 return true;
1153 };
1154
1155 m_layers[aLayer].items->Query( r, collect );
1156
1157 // Items without a uniform color are drawn again, as in recolorAllLayers()
1158 for( const auto& [item, group] : groups )
1159 recolorGroup( item, aLayer, group );
1160
1161 MarkTargetDirty( m_layers[aLayer].target );
1162
1163 // A retained scene's ChangeGroupColor() does not re-upload the static buffer
1166 }
1167 }
1168}
1169
1170
1172{
1173 recolorAllLayers( false );
1174}
1175
1176
1178{
1179 recolorAllLayers( m_gal->HasRetainedScene() );
1180}
1181
1182
1183void VIEW::UpdateLayersColor( bool aVisibility, const std::function<bool( int )>& aLayers )
1184{
1185 recolorAllLayers( aVisibility && m_gal->HasRetainedScene(), aLayers );
1186}
1187
1188
1189void VIEW::recolorAllLayers( bool aReplace, const std::function<bool( int )>& aLayers )
1190{
1191 // A layer's fade can follow the same settings as its colours
1192 m_sceneLayersStale = true;
1193
1194 if( aReplace )
1195 {
1197 m_gal->SetSceneScale( m_scale );
1199 }
1200
1201 if( m_gal->IsVisible() )
1202 {
1204
1205 if( ctx.IsUpdating() )
1206 {
1207 for( VIEW_ITEM* item : *m_allItems )
1208 {
1209 if( !item )
1210 continue;
1211
1212 VIEW_ITEM_DATA* viewData = item->viewPrivData();
1213
1214 if( !viewData )
1215 continue;
1216
1217 for( int layer : viewData->m_layers )
1218 {
1219 if( aLayers && !aLayers( layer ) )
1220 continue;
1221
1222 int group = viewData->getGroup( layer );
1223
1224 if( group >= 0 )
1225 recolorGroup( item, layer, group );
1226
1227 if( group >= 0 && aReplace )
1228 placeCachedGroup( item, layer );
1229 }
1230 }
1231 }
1232 }
1233
1234 MarkDirty();
1235}
1236
1237
1239{
1240 if( m_topLayers.size() == 0 )
1241 return 0;
1242
1243 return *m_topLayers.begin();
1244}
1245
1246
1247void VIEW::SetTopLayer( int aLayer, bool aEnabled )
1248{
1249 if( aEnabled )
1250 {
1251 if( m_topLayers.count( aLayer ) == 1 )
1252 return;
1253
1254 m_topLayers.insert( aLayer );
1255
1256 // Move the layer closer to front
1258 m_layers[aLayer].renderingOrder += TOP_LAYER_MODIFIER;
1259 }
1260 else
1261 {
1262 if( m_topLayers.count( aLayer ) == 0 )
1263 return;
1264
1265 m_topLayers.erase( aLayer );
1266
1267 // Restore the previous rendering order
1269 m_layers[aLayer].renderingOrder -= TOP_LAYER_MODIFIER;
1270 }
1271}
1272
1273
1274void VIEW::EnableTopLayer( bool aEnable )
1275{
1276 if( aEnable == m_enableOrderModifier )
1277 return;
1278
1279 m_enableOrderModifier = aEnable;
1280
1281 std::set<unsigned int>::iterator it;
1282
1283 if( aEnable )
1284 {
1285 for( it = m_topLayers.begin(); it != m_topLayers.end(); ++it )
1286 m_layers[*it].renderingOrder += TOP_LAYER_MODIFIER;
1287 }
1288 else
1289 {
1290 for( it = m_topLayers.begin(); it != m_topLayers.end(); ++it )
1291 m_layers[*it].renderingOrder -= TOP_LAYER_MODIFIER;
1292 }
1293
1296}
1297
1298
1300{
1301 std::set<unsigned int>::iterator it;
1302
1304 {
1305 // Restore the previous rendering order for layers that were marked as top
1306 for( it = m_topLayers.begin(); it != m_topLayers.end(); ++it )
1307 m_layers[*it].renderingOrder -= TOP_LAYER_MODIFIER;
1308 }
1309
1310 m_topLayers.clear();
1311}
1312
1313
1315{
1317
1318 // A retained scene keeps depth per layer, not per group
1319 if( m_gal->IsVisible() && !m_gal->HasRetainedScene() )
1320 {
1322
1323 if( ctx.IsUpdating() )
1324 {
1325 for( VIEW_ITEM* item : *m_allItems )
1326 {
1327 if( !item )
1328 continue;
1329
1330 VIEW_ITEM_DATA* viewData = item->viewPrivData();
1331
1332 if( !viewData )
1333 continue;
1334
1335 for( int layer : viewData->m_layers )
1336 {
1337 int group = viewData->getGroup( layer );
1338
1339 if( group >= 0 )
1340 m_gal->ChangeGroupDepth( group, m_layers[layer].renderingOrder );
1341 }
1342 }
1343 }
1344 }
1345
1346 MarkDirty();
1347}
1348
1349
1351{
1352 DRAW_ITEM_VISITOR( VIEW* aView, int aLayer, bool aUseDrawPriority, bool aReverseDrawOrder ) :
1353 view( aView ),
1354 layer( aLayer ),
1355 useDrawPriority( aUseDrawPriority ),
1356 reverseDrawOrder( aReverseDrawOrder ),
1357 drawForcedTransparent( false ),
1358 foundForcedTransparent( false ),
1359 placing( false )
1360 {
1361 }
1362
1363 bool operator()( VIEW_ITEM* aItem )
1364 {
1365 wxCHECK( aItem->viewPrivData(), false );
1366
1367 if( aItem->m_forcedTransparency > 0 && !drawForcedTransparent )
1368 {
1370
1371 if( placing )
1372 drawItems.push_back( aItem );
1373
1374 return true;
1375 }
1376
1377 // A retained scene decides what is drawn itself, so it is told about every item
1379 {
1380 drawItems.push_back( aItem );
1381
1382 return true;
1383 }
1384
1385 const double itemLOD = aItem->ViewGetLOD( layer, view );
1386
1387 view->noteKeptLod( itemLOD );
1388
1389 // Conditions that have to be fulfilled for an item to be drawn
1390 bool drawCondition = aItem->viewPrivData()->isRenderable() && view->IsShownAtScale( itemLOD );
1391
1392 if( !drawCondition )
1393 return true;
1394
1395 if( useDrawPriority )
1396 drawItems.push_back( aItem );
1397 else
1398 view->draw( aItem, layer );
1399
1400 return true;
1401 }
1402
1404 {
1405 if( placing )
1406 {
1407 std::vector<VIEW::LAYER_ITEM_PAIR> items;
1408 items.reserve( drawItems.size() );
1409
1410 for( VIEW_ITEM* item : drawItems )
1411 items.emplace_back( item, layer );
1412
1413 view->placeRetainedInOrder( items, false );
1414 return;
1415 }
1416
1417 if( reverseDrawOrder )
1418 {
1419 std::sort( drawItems.begin(), drawItems.end(),
1420 []( VIEW_ITEM* a, VIEW_ITEM* b ) -> bool
1421 {
1422 return b->viewPrivData()->m_drawPriority
1423 < a->viewPrivData()->m_drawPriority;
1424 } );
1425 }
1426 else
1427 {
1428 std::sort( drawItems.begin(), drawItems.end(),
1429 []( VIEW_ITEM* a, VIEW_ITEM* b ) -> bool
1430 {
1431 return a->viewPrivData()->m_drawPriority
1432 < b->viewPrivData()->m_drawPriority;
1433 } );
1434 }
1435
1436 for( VIEW_ITEM* item : drawItems )
1437 view->draw( item, layer );
1438 }
1439
1443 std::vector<VIEW_ITEM*> drawItems;
1447};
1448
1449
1450void VIEW::placeRetainedInOrder( const std::vector<LAYER_ITEM_PAIR>& aItems, bool aReplacing )
1451{
1452 std::vector<RETAINED_ORDER_ITEM> ordered;
1453 ordered.reserve( aItems.size() );
1454
1455 for( const auto& [item, layer] : aItems )
1456 {
1457 VIEW_ITEM_DATA* data = item->viewPrivData();
1458
1459 if( !data || !IsCached( layer ) )
1460 continue;
1461
1462 if( data->getGroup( layer ) < 0 )
1463 updateItemGeometry( item, layer, false );
1464
1465 ordered.push_back( { item, layer, data->getGroup( layer ),
1467 m_reverseDrawOrder, cachedGroupLOD( item, layer ) ) } );
1468 }
1469
1470 sortRetainedOrder( this, ordered );
1471
1472 for( const RETAINED_ORDER_ITEM& entry : ordered )
1473 {
1474 if( aReplacing )
1475 releaseReplacedGroup( entry.item, entry.layer );
1476
1477 placeCachedGroup( entry.item, entry.layer, &entry.order );
1478 }
1479
1480 if( aReplacing )
1482}
1483
1484
1485void VIEW::placeCachedGroup( VIEW_ITEM* aItem, int aLayer,
1486 const RETAINED_GROUP_ORDER* aOrder )
1487{
1488 VIEW_ITEM_DATA* viewData = aItem->viewPrivData();
1489
1490 if( !viewData )
1491 return;
1492
1494 {
1495 m_deferredRetainedPlacements.emplace_back( aItem, aLayer );
1496 return;
1497 }
1498
1499 if( viewData->getGroup( aLayer ) < 0 )
1500 updateItemGeometry( aItem, aLayer );
1501
1502 int group = viewData->getGroup( aLayer );
1503
1504 if( group < 0 )
1505 return;
1506
1507 // cachedGroupLOD() records which other layers the item reads, so showing or hiding one of them
1508 // places it again. The layer's own visibility is layer state.
1509 const double lod = aOrder ? aOrder->lodScale : cachedGroupLOD( aItem, aLayer );
1510 RETAINED_GROUP_ORDER order = aOrder ? *aOrder
1511 : retainedOrderKey( aItem, viewData->m_drawPriority, viewData->m_bbox,
1513 m_reverseDrawOrder, lod );
1514
1515 if( !aOrder )
1516 {
1517 order.contentHash = m_gal->GroupContentFingerprint( group );
1518 order.contentTie = true;
1519 }
1520
1521 m_gal->SetGroupPlacement( group, aLayer, lod,
1522 viewData->isRenderable() && aItem->m_forcedTransparency <= 0, order );
1523}
1524
1525
1526double VIEW::cachedGroupLOD( VIEW_ITEM* aItem, int aLayer )
1527{
1528 m_lodProbeLayer = aLayer;
1529 const double lod = aItem->ViewGetLOD( aLayer, this );
1530 m_lodProbeLayer = -1;
1531 return lod;
1532}
1533
1534
1535void VIEW::redrawRect( const BOX2I& aRect )
1536{
1538
1539 const bool retained = m_gal && m_gal->HasRetainedScene();
1540
1542
1543 if( retained )
1544 {
1545 for( int target : { TARGET_NONCACHED, TARGET_OVERLAY } )
1546 {
1547 if( IsTargetDirty( target ) || m_gal->RefillsNonCachedTargets() )
1548 m_scaleUse[target] = SCALE_USE();
1549 }
1550 }
1551
1552 for( VIEW_LAYER* l : m_orderedLayers )
1553 {
1554 if( l->items->IsEmpty() )
1555 continue;
1556
1557 const bool placing = retained && l->target == TARGET_CACHED;
1558
1559 // A backend that empties its non-cached targets on each drawn frame needs a clean
1560 // target emitted along with the dirty one, or it goes missing
1561 const bool refilled = l->target != TARGET_CACHED && m_gal && m_gal->RefillsNonCachedTargets();
1562
1563 // A retained scene keeps its cached layers until the backend asks for all of them again. A
1564 // pending rebuild empties the static pool even when TARGET_CACHED is clean.
1565 const bool targetDirty = placing ? m_gal->HasStaticRebuildPending()
1566 : ( IsTargetDirty( l->target ) || refilled
1567 || ( l->target == TARGET_CACHED && m_gal
1568 && m_gal->HasStaticRebuildPending() ) );
1569
1570 // A rebuild places the groups of hidden layers too, as the scene leaves those layers out
1571 if( targetDirty && ( placing || ( l->visible && areRequiredLayersEnabled( l->id ) ) ) )
1572 {
1573 DRAW_ITEM_VISITOR drawFunc( this, l->id, m_useDrawPriority, m_reverseDrawOrder );
1574
1575 m_gal->SetTarget( l->target );
1576 m_gal->SetLayerDepth( l->renderingOrder );
1577
1578 // Differential layer also work for the negatives, since both special layer types
1579 // will composite on separate layers (at least in Cairo)
1580 if( l->diffLayer )
1581 m_gal->StartDiffLayer();
1582 else if( l->hasNegatives )
1583 m_gal->StartNegativesLayer();
1584
1585 if( placing )
1586 {
1587 drawFunc.placing = true;
1588 m_gal->BeginSceneLayer( l->id );
1589 }
1590
1591 const bool kept = retained && l->target != TARGET_CACHED;
1592
1593 if( kept )
1594 {
1595 m_keptScaleUse = &m_scaleUse[l->target];
1596 m_scaleRead = false;
1597 m_gal->TakeWorldScaleRead();
1598
1599 double below, above;
1600 m_gal->TakeWorldScaleLimits( below, above );
1601 }
1602
1603 l->items->Query( aRect, drawFunc );
1604
1605 if( m_useDrawPriority || placing )
1606 drawFunc.deferredDraw();
1607
1608 m_keptScaleUse = nullptr;
1609
1610 if( kept && ( m_gal->TakeWorldScaleRead() || m_scaleRead ) )
1611 m_scaleUse[l->target].read = true;
1612
1613 // Limits on the world scale become limits on the view's, which is proportional
1614 if( kept && m_gal->GetWorldScaleForScreenSpace() > 0.0 )
1615 {
1616 const double toView = m_scale / m_gal->GetWorldScaleForScreenSpace();
1617 double below, above;
1618
1619 m_gal->TakeWorldScaleLimits( below, above );
1620
1621 SCALE_USE& use = m_scaleUse[l->target];
1622 use.lodBelow = std::max( use.lodBelow, below * toView );
1623
1624 if( above < std::numeric_limits<double>::max() )
1625 use.lodAbove = std::min( use.lodAbove, above * toView );
1626 }
1627
1628 if( l->diffLayer )
1629 m_gal->EndDiffLayer();
1630 else if( l->hasNegatives )
1631 m_gal->EndNegativesLayer();
1632
1633 if( drawFunc.foundForcedTransparent )
1634 {
1635 drawFunc.drawForcedTransparent = true;
1636
1637 m_gal->SetTarget( TARGET_NONCACHED );
1638 m_gal->EnableDepthTest( true );
1639 m_gal->SetLayerDepth( l->renderingOrder );
1640
1641 l->items->Query( aRect, drawFunc );
1642 }
1643 }
1644 }
1645}
1646
1647
1648void VIEW::draw( VIEW_ITEM* aItem, int aLayer, bool aImmediate )
1649{
1650 VIEW_ITEM_DATA* viewData = aItem->viewPrivData();
1651
1652 if( !viewData )
1653 return;
1654
1655 if( m_layerCachedFlagCache[ aLayer ] && !aImmediate )
1656 {
1657 int group = viewData->getGroup( aLayer );
1658
1659 if( group >= 0 )
1660 {
1661 m_gal->DrawGroup( group );
1662 }
1663 else
1664 {
1665 wxLogTrace( traceRhi, wxS( "ViewCache: miss layer=%d item=%p drawingMode=%d" ),
1666 aLayer, static_cast<const void*>( aItem ), m_gal->IsDrawingMode() ? 1 : 0 );
1667
1668 // Only a retained scene builds a missed group mid-frame. Other backends wait for the next
1669 // update, since opening a group in Cairo ends its drawing surface.
1670 if( !m_gal->HasRetainedScene() )
1671 {
1672 Update( aItem );
1673 return;
1674 }
1675
1676 if( m_gal->IsDrawingMode() )
1677 {
1678 updateItemGeometry( aItem, aLayer, false );
1679 group = viewData->getGroup( aLayer );
1680
1681 if( group >= 0 )
1682 {
1683 m_gal->DrawGroup( group );
1684 return;
1685 }
1686 }
1687
1688 // Outside drawing mode there is no open group to draw into, so rebuild on the next pass
1689 wxLogTrace( traceRhi, wxS( "ViewCache: defer-build layer=%d item=%p" ),
1690 aLayer, static_cast<const void*>( aItem ) );
1691
1692 Update( aItem, REPAINT );
1693 }
1694 }
1695 else
1696 {
1697 // Immediate mode
1698 if( !m_painter->Draw( aItem, aLayer ) )
1699 aItem->ViewDraw( aLayer, this );
1700 }
1701}
1702
1703
1704void VIEW::draw( VIEW_ITEM* aItem, bool aImmediate )
1705{
1706 std::vector<int> layers = aItem->ViewGetLayers();
1707
1708 // Sorting is needed for drawing order dependent GALs (like Cairo)
1709 if( !m_gal || !m_gal->IsOpenGlEngine())
1710 SortLayers( layers );
1711
1712 for( int layer : layers )
1713 {
1714 auto it = m_layers.find( layer );
1715
1716 if( it == m_layers.end() )
1717 continue;
1718
1719 if( m_gal )
1720 m_gal->SetLayerDepth( it->second.renderingOrder );
1721
1722 draw( aItem, layer, aImmediate );
1723 }
1724}
1725
1726
1727void VIEW::draw( VIEW_GROUP* aGroup, bool aImmediate )
1728{
1729 for( unsigned int i = 0; i < aGroup->GetSize(); i++ )
1730 draw( aGroup->GetItem(i), aImmediate );
1731}
1732
1733
1735{
1736 RECACHE_ITEM_VISITOR( VIEW* aView, GAL* aGal, int aLayer ) :
1737 view( aView ),
1738 gal( aGal ),
1739 layer( aLayer )
1740 {
1741 }
1742
1743 bool operator()( VIEW_ITEM* aItem )
1744 {
1745 VIEW_ITEM_DATA* viewData = aItem->viewPrivData();
1746
1747 if( !viewData )
1748 return false;
1749
1750 // A retained scene keeps the cached group until the item is drawn again, so its replacement
1751 // takes the same place in the draw order, see updateItemGeometry()
1752 if( !gal->HasRetainedScene() )
1753 {
1754 int group = viewData->getGroup( layer );
1755
1756 if( group >= 0 )
1757 gal->DeleteGroup( group );
1758
1759 viewData->setGroup( layer, -1 );
1760 }
1761
1762 view->Update( aItem, KIGFX::REPAINT );
1763
1764 return true;
1765 }
1766
1770};
1771
1772
1774{
1775 BOX2I r;
1776 r.SetMaximum();
1777
1778 // Invalidate viewPrivData for all items before clearing. This ensures that items
1779 // which persist outside the view (like selection groups) won't have stale references
1780 // to this view, which could cause issues if they're later removed and re-added.
1782
1783 for( VIEW_ITEM* item : *m_allItems )
1784 {
1785 if( item && item->m_viewPrivData )
1786 item->m_viewPrivData->m_view = nullptr;
1787 }
1788
1789 m_allItems->clear();
1790
1791 for( auto& [_, layer] : m_layers )
1792 layer.items->RemoveAll();
1793
1795
1796 m_gal->ClearCache();
1797}
1798
1799
1801{
1802 // A kept target that only the camera moved still holds valid content
1803 if( m_gal && m_gal->HasRetainedScene() && !m_gal->RefillsNonCachedTargets() )
1804 {
1805 for( int target : { TARGET_NONCACHED, TARGET_OVERLAY } )
1806 {
1807 if( !m_targetEdited[target] )
1808 m_dirtyTargets[target] = false;
1809 }
1810 }
1811
1812 // Without a retained scene the cached and non-cached targets share surfaces, so they clear
1813 // together
1814 if( m_gal && !m_gal->HasRetainedScene() )
1815 {
1817 {
1818 m_gal->ClearTarget( TARGET_NONCACHED );
1819 m_gal->ClearTarget( TARGET_CACHED );
1820
1821 MarkDirty();
1822 }
1823 }
1824 else
1825 {
1827 m_gal->ClearTarget( TARGET_CACHED );
1828
1830 m_gal->ClearTarget( TARGET_NONCACHED );
1831 }
1832
1834 {
1835 m_gal->ClearTarget( TARGET_OVERLAY );
1836 }
1837}
1838
1839
1841{
1842#ifdef KICAD_GAL_PROFILE
1843 PROF_TIMER totalRealTime("view-redraw-total");
1844 latencyProbeZoomToRender.Checkpoint("view-redraw-start");
1845#endif /* KICAD_GAL_PROFILE */
1846
1847 BOX2I recti;
1848
1849 // A retained scene holds every item, not only the visible ones, so a full redraw visits them all
1850 if( m_gal && m_gal->HasRetainedScene() && m_gal->IsInitialized() )
1851 {
1852 recti.SetMaximum();
1853 }
1854 else
1855 {
1856 VECTOR2D screenSize = m_gal->GetScreenPixelSize();
1857 BOX2D rect( ToWorld( VECTOR2D( 0, 0 ) ),
1858 ToWorld( screenSize ) - ToWorld( VECTOR2D( 0, 0 ) ) );
1859
1860 rect.Normalize();
1861 recti = BOX2ISafe( rect );
1862 }
1863
1864 redrawRect( recti );
1865
1866 // All targets were redrawn, so nothing is dirty
1867 MarkClean();
1868
1869#ifdef KICAD_GAL_PROFILE
1870 totalRealTime.Stop();
1871 wxLogTrace( traceGalProfile, wxS( "VIEW::Redraw(): %.1f ms" ), totalRealTime.msecs() );
1872 latencyProbeZoomToRender.AddTimer( totalRealTime );
1873 latencyProbeZoomToRender.Checkpoint("view-redraw-end");
1874
1875#endif /* KICAD_GAL_PROFILE */
1876}
1877
1878
1880{
1881 return m_gal->GetScreenPixelSize();
1882}
1883
1884
1886{
1888 view( aView )
1889 {
1890 }
1891
1892 bool operator()( VIEW_ITEM* aItem )
1893 {
1894 aItem->viewPrivData()->deleteGroups();
1895
1896 return true;
1897 }
1898
1900};
1901
1902
1904{
1905 BOX2I r;
1906
1907 r.SetMaximum();
1908 CLEAR_LAYER_CACHE_VISITOR visitor( this );
1909
1910 for( auto& [_, layer] : m_layers )
1911 layer.items->Query( r, visitor );
1912}
1913
1914
1915void VIEW::invalidateItem( VIEW_ITEM* aItem, int aUpdateFlags )
1916{
1917 // updateLayers updates geometry too, so we do not have to update both of them at the
1918 // same time
1919 if( aUpdateFlags & LAYERS )
1920 updateLayers( aItem );
1921 else if( aUpdateFlags & GEOMETRY )
1922 updateBbox( aItem );
1923
1924 // Now that we have initialized, set flags to ALL for the code below
1925 if( aUpdateFlags & INITIAL_ADD )
1926 aUpdateFlags = ALL;
1927
1928 std::vector<int> layers = aItem->ViewGetLayers();
1929 bool cachedMutation = false;
1930 const bool retained = m_gal && m_gal->HasRetainedScene();
1931
1932 // Iterate through layers used by the item and recache it immediately
1933 for( int layer : layers )
1934 {
1935 if( IsCached( layer ) )
1936 {
1937 cachedMutation = true;
1938
1939 if( aUpdateFlags & ( GEOMETRY | LAYERS | REPAINT ) )
1940 updateItemGeometry( aItem, layer );
1941 else if( aUpdateFlags & COLOR )
1942 updateItemColor( aItem, layer );
1943
1944 // Visibility is a placement, the group itself is unchanged
1945 if( retained && ( aUpdateFlags & APPEARANCE ) )
1946 placeCachedGroup( aItem, layer );
1947 }
1948
1949 // Mark those layers as dirty, so the VIEW will be refreshed
1950 MarkTargetDirty( m_layers[layer].target );
1951 }
1952
1953 if( retained && !m_useDrawPriority && aItem->m_forcedTransparency <= 0 )
1954 {
1955 // The groups above are already patched into the scene, but a non-cached layer still needs
1956 // the frame off the camera-reuse path
1958 }
1959 else if( aUpdateFlags & ( GEOMETRY | LAYERS | REPAINT ) )
1960 {
1962 }
1963 else if( cachedMutation && ( aUpdateFlags & ( APPEARANCE | COLOR ) ) )
1964 {
1965 // A retained scene patches a color change into the group. Visibility changes cannot be
1966 // patched that way and rebuild.
1967 const bool colorOnlyPatchable = ( aUpdateFlags & COLOR ) && !( aUpdateFlags & APPEARANCE )
1968 && m_gal && m_gal->HasRetainedScene();
1969
1970 if( !colorOnlyPatchable )
1972 }
1973
1974 aItem->viewPrivData()->clearUpdateFlags();
1975}
1976
1977
1979{
1980 int n = 0;
1981
1982 m_orderedLayers.resize( m_layers.size() );
1983
1984 for( auto& [layer_id, layer] : m_layers )
1985 m_orderedLayers[n++] = &layer;
1986
1988
1989 // A retained scene takes the new order as layer state
1990 m_sceneLayersStale = true;
1991
1992 MarkDirty();
1993}
1994
1995
1996void VIEW::recolorGroup( VIEW_ITEM* aItem, int aLayer, int aGroup )
1997{
1998 if( m_painter->HasUniformColor( aItem, aLayer ) )
1999 {
2000 m_gal->ChangeGroupColor( aGroup, m_painter->GetSettings()->GetColor( aItem, aLayer ) );
2001 markDimExemption( aItem, aGroup );
2002 }
2003 else
2004 {
2005 updateItemGeometry( aItem, aLayer );
2006 }
2007}
2008
2009
2010void VIEW::markDimExemption( const VIEW_ITEM* aItem, int aGroup )
2011{
2012 if( m_gal->HasRetainedScene() )
2013 m_gal->ChangeGroupUndimmed( aGroup, m_painter->GetSettings()->IsDimExempt( aItem ) );
2014}
2015
2016
2017void VIEW::updateItemColor( VIEW_ITEM* aItem, int aLayer )
2018{
2019 VIEW_ITEM_DATA* viewData = aItem->viewPrivData();
2020 wxCHECK( IsCached( aLayer ), /*void*/ ); // This will check if the layer exists
2021
2022 if( !viewData )
2023 return;
2024
2025 int group = viewData->getGroup( aLayer );
2026
2027 if( group >= 0 )
2028 recolorGroup( aItem, aLayer, group );
2029}
2030
2031
2032void VIEW::updateItemGeometry( VIEW_ITEM* aItem, int aLayer, bool aPlace )
2033{
2034 VIEW_ITEM_DATA* viewData = aItem->viewPrivData();
2035
2036 if( !viewData )
2037 return;
2038
2039 auto it = m_layers.find( aLayer );
2040
2041 if( it == m_layers.end() )
2042 return;
2043
2044 VIEW_LAYER& l = it->second;
2045
2046 // Save the extra map lookup in IsCached by open coding here
2047 if( l.target != TARGET_CACHED )
2048 return;
2049
2050 int group = viewData->getGroup( aLayer );
2051
2052 m_gal->SetTarget( l.target );
2053 m_gal->SetLayerDepth( l.renderingOrder );
2054
2055 // A batch draws all of its items again before it places any. Deleting each old group here
2056 // would hand the freed slots back in reverse, so the old group waits for its replacement.
2057 const bool keepForPlacement = group >= 0 && aPlace && m_deferRetainedPlacement && m_gal->HasRetainedScene()
2058 && m_replacedRetainedGroups.emplace( std::make_pair( aItem, aLayer ), group ).second;
2059
2060 if( group >= 0 && !keepForPlacement )
2061 m_gal->DeleteGroup( group );
2062
2063 group = m_gal->BeginGroup();
2064 viewData->setGroup( aLayer, group );
2065
2066 wxLogTrace( traceGalGroups, wxS( "updateItemGeometry() item=%p layer=%d newGroup=%d" ),
2067 static_cast<void*>( aItem ), aLayer, group );
2068
2069 if( !m_painter->Draw( aItem, aLayer ) )
2070 aItem->ViewDraw( aLayer, this );
2071
2072 m_gal->EndGroup();
2073 markDimExemption( aItem, group );
2074
2075 if( aPlace && m_gal->HasRetainedScene() )
2076 placeCachedGroup( aItem, aLayer );
2077}
2078
2079
2080void VIEW::releaseReplacedGroup( VIEW_ITEM* aItem, int aLayer )
2081{
2082 auto it = m_replacedRetainedGroups.find( std::make_pair( aItem, aLayer ) );
2083
2084 if( it == m_replacedRetainedGroups.end() )
2085 return;
2086
2087 m_gal->DeleteGroup( it->second );
2088 m_replacedRetainedGroups.erase( it );
2089}
2090
2091
2093{
2094 for( const auto& [key, group] : m_replacedRetainedGroups )
2095 m_gal->DeleteGroup( group );
2096
2098}
2099
2100
2102{
2103 std::vector<int> layers = aItem->ViewGetLayers();
2104
2105 wxASSERT( aItem->m_viewPrivData ); //must have a viewPrivData
2106
2107 const BOX2I new_bbox = aItem->ViewBBox();
2108
2109 // The R-tree removal below keys on the bbox the item was inserted with, so it must be
2110 // copied before the m_bbox overwrite that follows rather than aliased to it. Otherwise
2111 // a moved item is removed with the wrong box and the R-tree falls back to a full-tree
2112 // search instead of the targeted removal.
2113 const BOX2I old_bbox = aItem->m_viewPrivData->m_bbox;
2114
2115 if( new_bbox == old_bbox )
2116 return;
2117
2118 aItem->m_viewPrivData->m_bbox = new_bbox;
2119
2120 for( int layer : layers )
2121 {
2122 auto it = m_layers.find( layer );
2123
2124 if( it == m_layers.end() )
2125 continue;
2126
2127 VIEW_LAYER& l = it->second;
2128 l.items->Remove( aItem, &old_bbox );
2129 l.items->Insert( aItem, new_bbox );
2131 }
2132
2134}
2135
2136
2138{
2139 VIEW_ITEM_DATA* viewData = aItem->viewPrivData();
2140
2141 if( !viewData )
2142 return;
2143
2144 const BOX2I new_bbox = aItem->ViewBBox();
2145 std::vector<int> newLayers = aItem->ViewGetLayers();
2146
2147 // If neither the layers nor the bbox have changed, skip the expensive R-tree operations
2148 if( newLayers == viewData->m_layers && new_bbox == viewData->m_bbox )
2149 return;
2150
2151 // Remove the item from previous layer set
2152 const BOX2I& old_bbox = aItem->m_viewPrivData->m_bbox;
2153
2154 for( int layer : aItem->m_viewPrivData->m_layers )
2155 {
2156 auto it = m_layers.find( layer );
2157
2158 if( it == m_layers.end() )
2159 continue;
2160
2161 VIEW_LAYER& l = it->second;
2162 l.items->Remove( aItem, &old_bbox );
2164
2165 if( IsCached( l.id ) )
2166 {
2167 // Redraw the item from scratch
2168 int prevGroup = viewData->getGroup( layer );
2169
2170 if( prevGroup >= 0 )
2171 {
2172 m_gal->DeleteGroup( prevGroup );
2173 viewData->setGroup( l.id, -1 );
2174 }
2175 }
2176 }
2177
2178 aItem->m_viewPrivData->m_bbox = new_bbox;
2179 viewData->saveLayers( newLayers );
2180
2181 for( int layer : newLayers )
2182 {
2183 auto it = m_layers.find( layer );
2184
2185 if( it == m_layers.end() )
2186 continue;
2187
2188 VIEW_LAYER& l = it->second;
2189 l.items->Insert( aItem, new_bbox );
2191 }
2192
2194}
2195
2196
2197bool VIEW::areRequiredLayersEnabled( int aLayerId ) const
2198{
2199 auto it = m_layers.find( aLayerId );
2200
2201 if( it == m_layers.end() )
2202 return false;
2203
2204 for( int layer : it->second.requiredLayers )
2205 {
2206 // That is enough if just one layer is not enabled
2207
2208 auto it2 = m_layers.find( layer );
2209
2210 if( it2 == m_layers.end() || !it2->second.visible )
2211 return false;
2212
2213 if( !areRequiredLayersEnabled( layer ) )
2214 return false;
2215 }
2216
2217 return true;
2218}
2219
2220
2222{
2223 BOX2I r;
2224
2225 r.SetMaximum();
2226
2227 for( const auto& [_, l] : m_layers )
2228 {
2229 if( IsCached( l.id ) )
2230 {
2231 RECACHE_ITEM_VISITOR visitor( this, m_gal, l.id );
2232 l.items->Query( r, visitor );
2233 }
2234 }
2235}
2236
2237
2239{
2240 if( !m_gal->IsVisible() || !m_gal->IsInitialized() )
2241 {
2242 wxLogTrace( traceGalGroups,
2243 wxS( "UpdateItems() SKIP: visible=%d initialized=%d" ),
2244 m_gal->IsVisible(), m_gal->IsInitialized() );
2245 return;
2246 }
2247
2248#ifdef KICAD_GAL_PROFILE
2249 latencyProbeZoomToRender.Checkpoint("view-update-items");
2250#endif
2251
2253 return;
2254
2255 // Groups cached below are placed against the scale they will be drawn at
2256 if( m_gal->HasRetainedScene() )
2257 m_gal->SetSceneScale( m_scale );
2258
2259 unsigned int cntGeomUpdate = 0;
2260 bool anyUpdated = false;
2261
2262 // Other backends scan every item, since the shape of their R-trees depends on that order
2263 const bool retained = m_gal->HasRetainedScene();
2264 const bool listed = retained && !m_pendingListIncomplete;
2265
2266 const std::vector<VIEW_ITEM*>& flagged = listed ? m_pendingItems : *m_allItems;
2267
2268 for( VIEW_ITEM* item : flagged )
2269 {
2270 if( !item )
2271 continue;
2272
2273 auto vpd = item->viewPrivData();
2274
2275 if( !vpd )
2276 continue;
2277
2278 if( vpd->m_requiredUpdate != NONE )
2279 {
2280 anyUpdated = true;
2281
2282 if( vpd->m_requiredUpdate & ( GEOMETRY | LAYERS ) )
2283 cntGeomUpdate++;
2284 }
2285 }
2286
2287 unsigned int cntTotal = m_allItems->size();
2288
2289 double ratio = (double) cntGeomUpdate / (double) cntTotal;
2290
2291 // R*-tree individual inserts use forced reinsertion on node overflow, making them
2292 // significantly more expensive than simple R-tree inserts. At ~5% changed items
2293 // the cost of individual Remove+Insert operations exceeds a full bulk rebuild.
2294 if( ratio > 0.05 )
2295 {
2296 auto allItems = *m_allItems;
2297
2298 // Clear all R-trees
2299 for( auto& [_, layer] : m_layers )
2300 layer.items->RemoveAll();
2301
2302 // Collect items per layer for bulk loading
2303 std::unordered_map<int, std::vector<std::pair<VIEW_ITEM*, BOX2I>>> layerBulk;
2304
2305 for( VIEW_ITEM* item : allItems )
2306 {
2307 if( !item )
2308 continue;
2309
2310 const BOX2I bbox = item->ViewBBox();
2311 item->m_viewPrivData->m_bbox = bbox;
2312
2313 std::vector<int> layers = item->ViewGetLayers();
2314 item->viewPrivData()->saveLayers( layers );
2315
2316 for( int layer : layers )
2317 {
2318 auto it = m_layers.find( layer );
2319
2320 wxCHECK2_MSG( it != m_layers.end(), continue, wxS( "Invalid layer" ) );
2321 layerBulk[layer].emplace_back( item, bbox );
2322 }
2323
2324 // The bulk rebuild handled R-tree reinsertion, so clear LAYERS|GEOMETRY
2325 // to avoid redundant R-tree work in invalidateItem(). Replace with REPAINT
2326 // so items still reach updateItemGeometry() for GAL cache rebuilds.
2327 if( item->viewPrivData()->m_requiredUpdate & ( LAYERS | GEOMETRY ) )
2328 {
2329 item->viewPrivData()->m_requiredUpdate &= ~( LAYERS | GEOMETRY );
2330 item->viewPrivData()->m_requiredUpdate |= REPAINT;
2331 }
2332 }
2333
2334 // Bulk load each layer's R-tree
2335 for( auto& [layerId, items] : layerBulk )
2336 {
2337 auto it = m_layers.find( layerId );
2338
2339 if( it != m_layers.end() )
2340 {
2341 it->second.items->BulkLoad( items );
2342 MarkTargetDirty( it->second.target );
2343 }
2344 }
2345
2347 }
2348
2349 // Skipping the loop below leaves m_requiredUpdate and m_hasPendingItemUpdates set so the
2350 // work is redone once the canvas has a valid context again
2351 bool updatesProcessed = true;
2352
2353 if( anyUpdated )
2354 {
2355 unsigned int cntInvalidated = 0;
2357
2358 if( ctx.IsUpdating() )
2359 {
2360 auto invalidate = [&]( VIEW_ITEM* item )
2361 {
2362 if( item && item->viewPrivData() && item->viewPrivData()->m_requiredUpdate != NONE )
2363 {
2364 cntInvalidated++;
2365 invalidateItem( item, item->viewPrivData()->m_requiredUpdate );
2366 item->viewPrivData()->m_requiredUpdate = NONE;
2367 }
2368 };
2369
2370 if( retained )
2371 {
2372 if( m_gal->HasStaticRebuildPending() )
2373 {
2374 // The full redraw places every cached group, including those hidden at this scale
2376
2377 for( size_t next = 0; next < flagged.size(); ++next )
2378 invalidate( flagged[next] );
2379
2383 }
2384 else
2385 {
2386 size_t next = 0;
2387
2388 // An invalidation may queue another item, so sort each newly queued batch.
2389 while( next < flagged.size() )
2390 {
2391 std::vector<RETAINED_ORDER_ITEM> batch;
2392
2393 while( next < flagged.size() )
2394 {
2395 VIEW_ITEM* item = flagged[next++];
2396
2397 if( !item || !item->viewPrivData() || item->viewPrivData()->m_requiredUpdate == NONE )
2398 continue;
2399
2400 const VIEW_ITEM_DATA* data = item->viewPrivData();
2401 const int front = data->m_layers.empty() ? -1 : data->m_layers.front();
2402
2403 batch.push_back(
2404 { item, front, front < 0 ? -1 : data->getGroup( front ),
2407 data->m_layers.empty()
2408 ? 0.0
2409 : cachedGroupLOD( item, data->m_layers.front() ) ) } );
2410 }
2411
2412 std::sort( batch.begin(), batch.end(),
2413 []( const RETAINED_ORDER_ITEM& a, const RETAINED_ORDER_ITEM& b )
2414 {
2415 return a.order < b.order;
2416 } );
2417
2419
2420 for( const RETAINED_ORDER_ITEM& entry : batch )
2421 invalidate( entry.item );
2422
2424
2425 placeRetainedInOrder( std::exchange( m_deferredRetainedPlacements, {} ), true );
2426 }
2427 }
2428 }
2429 else
2430 {
2431 // Invalidating an item can flag another, which grows the list being walked.
2432 for( size_t ii = 0; ii < flagged.size(); ++ii )
2433 invalidate( flagged[ii] );
2434 }
2435 }
2436 else
2437 {
2438 updatesProcessed = false;
2439 }
2440
2441 wxLogTrace( traceGalGroups, wxS( "UpdateItems() invalidated %u items" ), cntInvalidated );
2442 }
2443
2444 wxLogTrace( traceGalGroups,
2445 wxS( "UpdateItems() total=%u geomUpdate=%u anyUpdated=%d bulkRebuild=%d" ),
2446 cntTotal, cntGeomUpdate, anyUpdated, ratio > 0.05 );
2447
2448#ifdef KICAD_GAL_PROFILE
2449 wxLogTrace( traceGalProfile, wxS( "View update: total items %u, geom %u anyUpdated %u" ),
2450 cntTotal, cntGeomUpdate, (unsigned) anyUpdated );
2451#endif
2452
2453 if( updatesProcessed )
2454 {
2458 }
2459}
2460
2461
2463{
2464 for( VIEW_ITEM* item : m_pendingItems )
2465 {
2466 if( item && item->viewPrivData() )
2467 item->viewPrivData()->m_pendingIndex = -1;
2468 }
2469
2470 m_pendingItems.clear();
2471}
2472
2473
2474void VIEW::UpdateAllItems( int aUpdateFlags )
2475{
2476 if( aUpdateFlags == NONE )
2477 return;
2478
2479 for( VIEW_ITEM* item : *m_allItems )
2480 {
2481 if( item && item->viewPrivData() )
2482 {
2483 item->viewPrivData()->m_requiredUpdate |= aUpdateFlags;
2486 }
2487 }
2488}
2489
2490
2492 std::function<bool( VIEW_ITEM* )> aCondition )
2493{
2494 if( aUpdateFlags == NONE )
2495 return;
2496
2497 for( VIEW_ITEM* item : *m_allItems )
2498 {
2499 if( !item )
2500 continue;
2501
2502 if( aCondition( item ) )
2503 {
2504 if( item->viewPrivData() )
2505 {
2506 item->viewPrivData()->m_requiredUpdate |= aUpdateFlags;
2509 }
2510 }
2511 }
2512}
2513
2514
2515void VIEW::UpdateAllItemsConditionally( std::function<int( VIEW_ITEM* )> aItemFlagsProvider )
2516{
2517 for( VIEW_ITEM* item : *m_allItems )
2518 {
2519 if( !item )
2520 continue;
2521
2522 if( item->viewPrivData() )
2523 {
2524 int flags = aItemFlagsProvider( item );
2525 item->viewPrivData()->m_requiredUpdate |= flags;
2526
2527 if( flags != NONE )
2528 {
2531 }
2532 }
2533 }
2534}
2535
2536
2537
2538std::unique_ptr<VIEW> VIEW::DataReference() const
2539{
2540 std::unique_ptr<VIEW> ret = std::make_unique<VIEW>();
2541 ret->m_allItems = m_allItems;
2542 ret->m_layers = m_layers;
2543 ret->m_hasPendingItemUpdates = m_hasPendingItemUpdates;
2544 ret->m_pendingListIncomplete = true;
2545 ret->m_cameraDirtyOnly = m_cameraDirtyOnly;
2546 ret->SortOrderedLayers();
2547 return ret;
2548}
2549
2550
2551void VIEW::SetVisible( VIEW_ITEM* aItem, bool aIsVisible )
2552{
2553 if( !aItem )
2554 return;
2555
2556 VIEW_ITEM_DATA* viewData = aItem->viewPrivData();
2557
2558 if( !viewData )
2559 return;
2560
2561 bool cur_visible = viewData->m_flags & VISIBLE;
2562
2563 if( cur_visible != aIsVisible )
2564 {
2565 if( aIsVisible )
2566 viewData->m_flags |= VISIBLE;
2567 else
2568 viewData->m_flags &= ~VISIBLE;
2569
2570 Update( aItem, APPEARANCE | COLOR );
2571 }
2572}
2573
2574
2575void VIEW::Hide( VIEW_ITEM* aItem, bool aHide, bool aHideOverlay )
2576{
2577 if( !aItem )
2578 return;
2579
2580 VIEW_ITEM_DATA* viewData = aItem->viewPrivData();
2581
2582 if( !viewData )
2583 return;
2584
2585 if( !( viewData->m_flags & VISIBLE ) )
2586 return;
2587
2588 if( aHideOverlay )
2589 viewData->m_flags |= OVERLAY_HIDDEN;
2590
2591 if( aHide )
2592 viewData->m_flags |= HIDDEN;
2593 else
2594 viewData->m_flags &= ~( HIDDEN | OVERLAY_HIDDEN );
2595
2596 Update( aItem, APPEARANCE );
2597}
2598
2599
2600bool VIEW::IsVisible( const VIEW_ITEM* aItem ) const
2601{
2602 const VIEW_ITEM_DATA* viewData = aItem->viewPrivData();
2603
2604 return viewData && ( viewData->m_flags & VISIBLE );
2605}
2606
2607
2608bool VIEW::IsHiddenOnOverlay( const VIEW_ITEM* aItem ) const
2609{
2610 const VIEW_ITEM_DATA* viewData = aItem->viewPrivData();
2611
2612 return viewData && ( viewData->m_flags & OVERLAY_HIDDEN );
2613}
2614
2615
2616bool VIEW::HasItem( const VIEW_ITEM* aItem ) const
2617{
2618 const VIEW_ITEM_DATA* viewData = aItem->viewPrivData();
2619
2620 return viewData && viewData->m_view == this;
2621}
2622
2623
2624void VIEW::Update( const VIEW_ITEM* aItem ) const
2625{
2626 Update( aItem, ALL );
2627}
2628
2629
2630void VIEW::Update( const VIEW_ITEM* aItem, int aUpdateFlags ) const
2631{
2632 VIEW_ITEM_DATA* viewData = aItem->viewPrivData();
2633
2634 if( !viewData )
2635 return;
2636
2637 assert( aUpdateFlags != NONE );
2638
2639 viewData->m_requiredUpdate |= aUpdateFlags;
2641
2642 // An item records a single list position, so only this view's own items are listed
2643 if( viewData->m_view != this )
2644 {
2646 }
2647 else if( viewData->m_pendingIndex < 0 )
2648 {
2649 viewData->m_pendingIndex = static_cast<int>( m_pendingItems.size() );
2650 m_pendingItems.push_back( const_cast<VIEW_ITEM*>( aItem ) );
2651 }
2652}
2653
2654
2655void VIEW::RepaintLayers( const BOX2I& aRect, const std::function<bool( int )>& aLayerFilter,
2656 const std::function<bool( VIEW_ITEM*, int )>& aItemFilter )
2657{
2658 if( !m_gal->IsVisible() || !m_gal->IsInitialized() )
2659 return;
2660
2661 std::vector<LAYER_ITEM_PAIR> stale;
2662
2663 for( VIEW_LAYER* l : m_orderedLayers )
2664 {
2665 if( l->target != TARGET_CACHED || !l->visible || !aLayerFilter( l->id ) )
2666 continue;
2667
2668 auto collect = [&]( VIEW_ITEM* aItem ) -> bool
2669 {
2670 if( aItemFilter( aItem, l->id ) )
2671 stale.emplace_back( aItem, l->id );
2672
2673 return true;
2674 };
2675
2676 l->items->Query( aRect, collect );
2677 }
2678
2679 if( stale.empty() )
2680 return;
2681
2683
2684 if( !ctx.IsUpdating() )
2685 return;
2686
2687 if( m_gal->HasRetainedScene() )
2688 m_gal->SetSceneScale( m_scale );
2689
2690 for( const LAYER_ITEM_PAIR& pair : stale )
2691 updateItemGeometry( pair.first, pair.second );
2692
2693 if( m_gal->HasRetainedScene() && !m_useDrawPriority )
2694 {
2695 // The camera path uploads the already placed groups, so a zoom and the net names it redraws
2696 // still go out as one camera frame
2697 const bool cameraOnly = m_cameraDirtyOnly || !IsDirty();
2698
2700 m_cameraDirtyOnly = cameraOnly;
2701 return;
2702 }
2703
2706}
2707
2708
2709std::shared_ptr<VIEW_OVERLAY> VIEW::MakeOverlay()
2710{
2711 std::shared_ptr<VIEW_OVERLAY> overlay = std::make_shared<VIEW_OVERLAY>();
2712
2713 Add( overlay.get() );
2714 return overlay;
2715}
2716
2717
2719{
2720 if( !m_preview )
2721 return;
2722
2723 m_preview->Clear();
2724
2725 for( VIEW_ITEM* item : m_ownedItems )
2726 delete item;
2727
2728 m_ownedItems.clear();
2729 Update( m_preview.get() );
2730}
2731
2732
2734{
2735 m_preview.reset( new KIGFX::VIEW_GROUP() );
2736 Add( m_preview.get() );
2737}
2738
2739
2740void VIEW::AddToPreview( VIEW_ITEM* aItem, bool aTakeOwnership )
2741{
2742 Hide( aItem, false );
2743 m_preview->Add( aItem );
2744
2745 if( aTakeOwnership )
2746 m_ownedItems.push_back( aItem );
2747
2748 SetVisible( m_preview.get(), true );
2749 Hide( m_preview.get(), false );
2750 Update( m_preview.get() );
2751}
2752
2753
2754void VIEW::ShowPreview( bool aShow )
2755{
2756 SetVisible( m_preview.get(), aShow );
2757}
2758
2759
2761{
2762 // Tools ask for this once per item they look at, far more often than a layer changes
2763 if( !m_layerCachesStale )
2764 return;
2765
2766 for( const auto& layer : m_layers )
2767 {
2768 m_layerVisibilityCache[ layer.first ] = layer.second.visible;
2769 m_layerCachedFlagCache[ layer.first ] = (layer.second.target == TARGET_CACHED);
2770 }
2771
2772 m_layerCachesStale = false;
2773}
2774
2775} // namespace KIGFX
constexpr int ARC_LOW_DEF
Definition base_units.h:143
constexpr BOX2I BOX2ISafe(const BOX2D &aInput)
Definition box2.h:924
BOX2< VECTOR2I > BOX2I
Definition box2.h:917
BOX2< VECTOR2D > BOX2D
Definition box2.h:918
constexpr void SetMaximum()
Definition box2.h:77
constexpr size_type GetWidth() const
Definition box2.h:217
constexpr Vec Centre() const
Definition box2.h:100
constexpr size_type GetHeight() const
Definition box2.h:218
constexpr coord_type GetLeft() const
Definition box2.h:231
constexpr coord_type GetRight() const
Definition box2.h:220
constexpr const SizeVec & GetSize() const
Definition box2.h:209
constexpr coord_type GetTop() const
Definition box2.h:232
constexpr coord_type GetBottom() const
Definition box2.h:225
A color representation with 4 components: red, green, blue, alpha.
Definition color4d.h:101
static const COLOR4D BLACK
Definition color4d.h:404
Abstract interface for drawing on a 2D-surface.
virtual std::array< uint64_t, 2 > GroupContentFingerprint(int aGroupNumber) const
Extend VIEW_ITEM by possibility of grouping items into a single object.
Definition view_group.h:39
virtual unsigned int GetSize() const
Return the number of stored items.
virtual VIEW_ITEM * GetItem(unsigned int aIdx) const
bool storesGroups() const
Return information if the item uses at least one group id (ie.
Definition view.cpp:151
std::vector< int > m_layers
Definition view.cpp:226
int requiredUpdate() const
Return current update flag for an item.
Definition view.cpp:194
int m_requiredUpdate
Flag required for updating.
Definition view.cpp:217
int m_flags
Visibility flags.
Definition view.cpp:216
int m_cachedIndex
Cached index in m_allItems.
Definition view.cpp:220
void deleteGroups()
Remove all of the stored group ids.
Definition view.cpp:139
bool isRenderable() const
Return if the item should be drawn or not.
Definition view.cpp:210
int m_drawPriority
Order to draw this item in a layer, lowest first.
Definition view.cpp:219
friend class VIEW
Definition view.cpp:85
VIEW * m_view
Current dynamic view the item is assigned to.
Definition view.cpp:215
std::pair< int, int > * m_groups
layer_number:group_id pairs for each layer the item occupies.
Definition view.cpp:222
int getGroup(int aLayer) const
Return number of the group id for the given layer, or -1 in case it was not cached before.
Definition view.cpp:93
void reorderGroups(const std::unordered_map< int, int > &aReorderMap)
Reorder the stored groups (to facilitate reordering of layers).
Definition view.cpp:163
BOX2I m_bbox
Stores layer numbers used by the item.
Definition view.cpp:228
void setGroup(int aLayer, int aGroup)
Set a group id for the item and the layer combination.
Definition view.cpp:110
void saveLayers(const std::vector< int > &aLayers)
Save layers used by the item.
Definition view.cpp:179
int GetFlags() const
Definition view.cpp:79
void clearUpdateFlags()
Mark an item as already updated, so it is not going to be redrawn.
Definition view.cpp:202
An abstract base class for deriving all objects that can be added to a VIEW.
Definition view_item.h:82
virtual const BOX2I ViewBBox() const =0
Return the bounding box of the item covering all its layers.
double m_forcedTransparency
Additional transparency for diff'ing items.
Definition view_item.h:206
VIEW_ITEM_DATA * viewPrivData() const
Definition view_item.h:157
virtual void ViewDraw(int aLayer, VIEW *aView) const
Draw the parts of the object belonging to layer aLayer.
Definition view_item.h:123
virtual std::vector< int > ViewGetLayers() const =0
Return the all the layers within the VIEW the object is painted on.
virtual wxString GetClass() const =0
Return the class name.
static constexpr double LOD_HIDE
Return this constant from ViewGetLOD() to hide the item unconditionally.
Definition view_item.h:176
static constexpr double LOD_SHOW
Return this constant from ViewGetLOD() to show the item unconditionally.
Definition view_item.h:181
virtual double ViewGetLOD(int aLayer, const VIEW *aView) const
Return the level of detail (LOD) of the item.
Definition view_item.h:151
VIEW_ITEM_DATA * m_viewPrivData
Definition view_item.h:205
Hold a (potentially large) number of VIEW_ITEMs and renders them on a graphics device provided by the...
Definition view.h:68
double GetScale() const
Definition view.h:298
void SetMirror(bool aMirrorX, bool aMirrorY)
Control the mirroring of the VIEW.
Definition view.cpp:887
bool m_layerCachesStale
A layer's visibility or target changed since SyncLayerVisibilityCache() last copied them.
Definition view.h:1103
void ShowPreview(bool aShow=true)
Definition view.cpp:2754
void markSceneEdited()
Note that items of the cached scene were added, removed or re-cached.
Definition view.cpp:730
std::vector< int > m_lodRefreshLayers
Layers shown or hidden since the last sync, whose readers have to be placed again.
Definition view.h:1184
bool m_cameraDirtyOnly
The current dirty state is due only to camera changes.
Definition view.h:1205
double m_maxScale
Definition view.h:1093
BOX2D GetViewport() const
Return the current viewport visible area rectangle.
Definition view.cpp:860
void releaseReplacedGroup(VIEW_ITEM *aItem, int aLayer)
Delete the group a deferred placement of an item on a layer replaces, if it is still kept.
Definition view.cpp:2080
void CopySettings(const VIEW *aOtherView)
Copy layers and visibility settings from another view.
Definition view.cpp:828
virtual void SetScale(double aScale, VECTOR2D aAnchor={ 0, 0 })
Set the scaling factor, zooming around a given anchor point.
Definition view.cpp:900
static constexpr int TOP_LAYER_MODIFIER
Rendering order modifier for layers that are marked as top layers.
Definition view.h:902
void draw(VIEW_ITEM *aItem, int aLayer, bool aImmediate=false)
Draw an item, but on a specified layers.
Definition view.cpp:1648
SCALE_USE * m_keptScaleUse
The SCALE_USE of the kept target being drawn, updated by each level of detail drawn.
Definition view.h:1172
bool m_reverseDrawOrder
Flag to reverse the draw order when using draw priority.
Definition view.h:1127
std::unordered_map< int, std::vector< bool > > m_lodReads
For each layer whose visibility an item asked about, the cached layers those items were placed on.
Definition view.h:1195
bool m_targetEdited[TARGETS_NUMBER]
The target's content changed, not just the camera.
Definition view.h:1118
void UpdateAllLayersOrder()
Do everything that is needed to apply the rendering order of layers.
Definition view.cpp:1314
int m_lodProbeLayer
Cached layer being placed while an item is asked for its level of detail, else -1.
Definition view.h:1189
void placeCachedGroup(VIEW_ITEM *aItem, int aLayer, const RETAINED_GROUP_ORDER *aOrder=nullptr)
Tell a retained scene whether the cached group of aItem on aLayer belongs in it, caching the group fi...
Definition view.cpp:1485
void updateItemColor(VIEW_ITEM *aItem, int aLayer)
Update colors that are used for an item to be drawn.
Definition view.cpp:2017
void SetViewport(const BOX2D &aViewport)
Set the visible area of the VIEW.
Definition view.cpp:872
void SetRequired(int aLayerId, int aRequiredId, bool aRequired=true)
Mark the aRequiredId layer as required for the aLayerId layer.
Definition view.cpp:573
VECTOR2D ToScreen(const VECTOR2D &aCoord, bool aAbsolute=true) const
Convert a world space point/vector to a point/vector in screen space coordinates.
Definition view.cpp:809
void recolorGroup(VIEW_ITEM *aItem, int aLayer, int aGroup)
Definition view.cpp:1996
void markDimExemption(const VIEW_ITEM *aItem, int aGroup)
Keep a group of a retained scene out of its layer's fade if the painter exempts its item.
Definition view.cpp:2010
double cachedGroupLOD(VIEW_ITEM *aItem, int aLayer)
Definition view.cpp:1526
static bool compareRenderingOrder(VIEW_LAYER *aI, VIEW_LAYER *aJ)
Determine rendering order of layers. Used in display order sorting function.
Definition view.h:1056
int GetLayerOrder(int aLayer) const
Return rendering order of a particular layer.
Definition view.cpp:1039
virtual void Add(VIEW_ITEM *aItem, int aDrawPriority=-1)
Add a VIEW_ITEM to the view.
Definition view.cpp:365
virtual void Remove(VIEW_ITEM *aItem)
Remove a VIEW_ITEM from the view.
Definition view.cpp:488
void ClearTargets()
Clear targets that are marked as dirty.
Definition view.cpp:1800
virtual void EnableTopLayer(bool aEnable)
Enable or disable display of the top layer.
Definition view.cpp:1274
void updateItemGeometry(VIEW_ITEM *aItem, int aLayer, bool aPlace=true)
Update all information needed to draw an item.
Definition view.cpp:2032
bool m_mirrorX
Definition view.h:1095
std::vector< std::pair< VIEW_ITEM *, int > > m_deferredRetainedPlacements
Definition view.h:1138
void UpdateAllLayersColor()
Apply the new coloring scheme to all layers.
Definition view.cpp:1171
void MarkStaticDirty()
Flag the GAL's static geometry pool as dirty so the next full redraw rebuilds it.
Definition view.cpp:712
bool m_deferRetainedPlacement
Definition view.h:1137
bool m_mirrorY
Definition view.h:1096
std::shared_ptr< std::vector< VIEW_ITEM * > > m_allItems
Flat list of all items.
Definition view.h:1082
std::shared_ptr< VIEW_OVERLAY > MakeOverlay()
Definition view.cpp:2709
void MarkGeometryDirty()
Signal a change to cached geometry so the next paint leaves the camera-reuse path and rebuilds the st...
Definition view.h:741
void rescaleKeptTargets()
Mark the kept targets whose content the new scale changes, so they are emitted again.
Definition view.cpp:951
void SetGAL(GAL *aGal)
Assign a rendering device for the VIEW.
Definition view.cpp:834
int Query(const BOX2I &aRect, std::vector< LAYER_ITEM_PAIR > &aResult) const
Find all visible items that touch or are within the rectangle aRect.
Definition view.cpp:743
std::vector< VIEW_ITEM * > m_ownedItems
Definition view.h:1070
virtual void Update(const VIEW_ITEM *aItem, int aUpdateFlags) const
For dynamic VIEWs, inform the associated VIEW that the graphical representation of this item has chan...
Definition view.cpp:2630
void invalidateItem(VIEW_ITEM *aItem, int aUpdateFlags)
Manage dirty flags & redraw queuing when updating an item.
Definition view.cpp:1915
const VECTOR2I & GetScreenPixelSize() const
Return the size of the our rendering area in pixels.
Definition view.cpp:1879
bool HasItem(const VIEW_ITEM *aItem) const
Indicates whether or not the given item has been added to the view.
Definition view.cpp:2616
virtual int GetTopLayer() const
Definition view.cpp:1238
PAINTER * m_painter
PAINTER contains information how do draw items.
Definition view.h:1106
virtual void Redraw()
Immediately redraws the whole view.
Definition view.cpp:1840
void Clear()
Remove all items from the view.
Definition view.cpp:1773
std::set< unsigned int > m_topLayers
The set of layers that are displayed on the top.
Definition view.h:1085
bool m_enableOrderModifier
Whether to use rendering order modifier or not.
Definition view.h:1073
SCALE_USE m_scaleUse[TARGETS_NUMBER]
Definition view.h:1166
GAL * GetGAL() const
Return the GAL this view is using to draw graphical primitives.
Definition view.h:224
VECTOR2D ToWorld(const VECTOR2D &aCoord, bool aAbsolute=true) const
Converts a screen space point/vector to a point/vector in world space coordinates.
Definition view.cpp:790
void UpdateAllLayersColorAndVisibility()
Apply the new coloring scheme to all layers after a render settings change that can also hide items,...
Definition view.cpp:1177
bool IsHiddenOnOverlay(const VIEW_ITEM *aItem) const
Definition view.cpp:2608
void ClearTopLayers()
Remove all layers from the on-the-top set (they are no longer displayed over the rest of layers).
Definition view.cpp:1299
void InitPreview()
Definition view.cpp:2733
bool m_dirtyTargets[TARGETS_NUMBER]
Flag to mark targets as dirty so they have to be redrawn on the next refresh event.
Definition view.h:1112
void SetLayerOrder(int aLayer, int aRenderingOrder, bool aAutoSort=true)
Set rendering order of a particular layer.
Definition view.cpp:1030
void ClearPreview()
Definition view.cpp:2718
void MarkClean()
Force redraw of view on the next rendering.
Definition view.h:804
bool IsShownAtScale(double aLod) const
Return true if something with level of detail aLod is drawn at the current scale.
Definition view.cpp:930
static constexpr int VIEW_MAX_LAYERS
Maximum number of layers that may be shown.
Definition view.h:899
double m_minScale
Definition view.h:1092
double m_scale
Definition view.h:1090
void layerStateChanged(int aLayer)
React to a change of a layer's visibility, order, required layers or negatives.
Definition view.cpp:587
void SyncRetainedScene()
Bring a retained scene up to date with the view scale and layer state, and place again the items whos...
Definition view.cpp:625
void updateLayers(VIEW_ITEM *aItem)
Update set of layers that an item occupies.
Definition view.cpp:2137
void RecacheAllItems()
Rebuild GAL display lists.
Definition view.cpp:2221
bool areRequiredLayersEnabled(int aLayerId) const
Check if every layer required by the aLayerId layer is enabled.
Definition view.cpp:2197
bool IsTargetDirty(int aTarget) const
Return true if any of layers belonging to the target or the target itself should be redrawn.
Definition view.h:718
bool m_pendingListIncomplete
Items were flagged without being listed, so UpdateItems() has to scan them all.
Definition view.h:1149
int m_nextDrawPriority
The next sequential drawing priority.
Definition view.h:1124
bool m_scaleRead
GetScale() was called, see SCALE_USE::read.
Definition view.h:1167
bool m_useDrawPriority
Flag to respect draw priority when drawing items.
Definition view.h:1121
void UpdateItems()
Iterate through the list of items that asked for updating and updates them.
Definition view.cpp:2238
void MarkViewportDirty()
Force a redraw for a camera change alone, which lets the paint reuse the cached scene.
Definition view.h:793
void SyncLayerVisibilityCache()
Definition view.cpp:2760
bool IsCached(int aLayer) const
Return true if the layer is cached.
Definition view.h:766
void AddBatch(const std::vector< VIEW_ITEM * > &aItems)
Add a batch of items to the view, using bulk-loaded R-trees for initial population.
Definition view.cpp:414
std::pair< VIEW_ITEM *, int > LAYER_ITEM_PAIR
Definition view.h:72
void SortLayers(std::vector< int > &aLayers) const
Change the order of given layer ids, so after sorting the order corresponds to layers rendering order...
Definition view.cpp:1045
BASE_SET m_layerVisibilityCache
Definition view.h:1097
void UpdateAllItems(int aUpdateFlags)
Update all items in the view according to the given flags.
Definition view.cpp:2474
bool m_hasPendingItemUpdates
True when at least one item has deferred update flags that still need processing.
Definition view.h:1130
std::unique_ptr< KIGFX::VIEW_GROUP > m_preview
Definition view.h:1069
static void OnDestroy(VIEW_ITEM *aItem)
Nasty hack, invoked by the destructor of VIEW_ITEM to auto-remove the item from the owning VIEW if th...
Definition view.cpp:289
void clearPendingItems()
Empty m_pendingItems and forget each listed item's place in it.
Definition view.cpp:2462
bool m_sceneLayersStale
A layer's order, visibility, required layers or negatives changed since the last sync.
Definition view.h:1154
std::vector< VIEW_ITEM * > m_pendingItems
Items flagged through Update() since the last UpdateItems(), so a frame that changed a few items need...
Definition view.h:1136
virtual ~VIEW()
Definition view.cpp:359
GAL * m_gal
Interface to PAINTER that is used to draw items.
Definition view.h:1109
bool IsDirty() const
Return true if any of the VIEW layers needs to be refreshened.
Definition view.h:701
void placeRetainedInOrder(const std::vector< LAYER_ITEM_PAIR > &aItems, bool aReplacing)
Cache the missing groups of these items, then place them all in retained draw order.
Definition view.cpp:1450
std::map< int, VIEW_LAYER > m_layers
The set of possible displayed layers and its properties.
Definition view.h:1076
void RepaintLayers(const BOX2I &aRect, const std::function< bool(int)> &aLayerFilter, const std::function< bool(VIEW_ITEM *, int)> &aItemFilter)
Re-cache only some layers of the items in aRect.
Definition view.cpp:2655
std::unique_ptr< VIEW > DataReference() const
Return a new VIEW object that shares the same set of VIEW_ITEMs and LAYERs.
Definition view.cpp:2538
void Hide(VIEW_ITEM *aItem, bool aHide=true, bool aHideOverlay=false)
Temporarily hide the item in the view (e.g.
Definition view.cpp:2575
virtual void SetTopLayer(int aLayer, bool aEnabled=true)
Set given layer to be displayed on the top or sets back the default order of layers.
Definition view.cpp:1247
void AddToPreview(VIEW_ITEM *aItem, bool aTakeOwnership=true)
Definition view.cpp:2740
void UpdateLayerColor(int aLayer)
Apply the new coloring scheme held by RENDER_SETTINGS in case that it has changed.
Definition view.cpp:1126
void MarkDirty()
Force redraw of view on the next rendering.
Definition view.h:779
friend class VIEW_ITEM
Definition view.h:70
BOX2D m_boundary
Definition view.h:1091
void noteLodRead(int aLayer) const
Record that the item being placed asked whether aLayer is visible.
Definition view.cpp:611
void recolorAllLayers(bool aReplace, const std::function< bool(int)> &aLayers={})
Recolor every cached group on the layers aLayers accepts, or all when empty, and place it again in a ...
Definition view.cpp:1189
void noteKeptLod(double aLod) const
Definition view.cpp:938
std::map< std::pair< VIEW_ITEM *, int >, int > m_replacedRetainedGroups
Groups replaced by items redrawn during a deferred placement.
Definition view.h:1144
void SetCenter(const VECTOR2D &aCenter)
Set the center point of the VIEW (i.e.
Definition view.cpp:970
std::vector< VIEW_LAYER * > m_orderedLayers
Sorted list of pointers to members of m_layers.
Definition view.h:1079
void clearGroupCache()
Clear cached GAL group numbers (ONLY numbers stored in VIEW_ITEMs, not group objects used by GAL).
Definition view.cpp:1903
VECTOR2D m_center
Center point of the VIEW (the point at which we are looking at).
Definition view.h:1088
void SortOrderedLayers()
Sorts m_orderedLayers after layer rendering order has changed.
Definition view.cpp:1978
void unlinkItem(VIEW_ITEM *aItem)
Detach a single VIEW_ITEM from this view.
Definition view.cpp:494
void MarkTargetDirty(int aTarget)
Set or clear target 'dirty' flag.
Definition view.h:729
void UpdateLayersColor(bool aVisibility, const std::function< bool(int)> &aLayers)
Recolor only the layers aLayers accepts, along with the fade the canvas may apply to them.
Definition view.cpp:1183
void updateBbox(VIEW_ITEM *aItem)
Update bounding box of an item.
Definition view.cpp:2101
bool IsVisible(const VIEW_ITEM *aItem) const
Return information if the item is visible (or not).
Definition view.cpp:2600
void releaseReplacedGroups()
Delete every group still kept for a deferred placement that did not happen.
Definition view.cpp:2092
BASE_SET m_layerCachedFlagCache
Definition view.h:1098
void UpdateAllItemsConditionally(int aUpdateFlags, std::function< bool(VIEW_ITEM *)> aCondition)
Update items in the view according to the given flags and condition.
Definition view.cpp:2491
void SetVisible(VIEW_ITEM *aItem, bool aIsVisible=true)
Set the item visibility.
Definition view.cpp:2551
void ReorderLayerData(std::unordered_map< int, int > aReorderMap)
Remap the data between layer ids without invalidating that data.
Definition view.cpp:1055
void redrawRect(const BOX2I &aRect)
Redraw contents within rectangle aRect.
Definition view.cpp:1535
VECTOR2< T > GetScale() const
Get the scale components of the matrix.
Definition matrix3x3.h:291
A small class to help profiling.
Definition profile.h:46
void Stop()
Save the time when this function was called, and set the counter stane to stop.
Definition profile.h:86
double msecs(bool aSinceLast=false)
Definition profile.h:148
Represent a set of closed polygons.
bool IsEmpty() const
Return true if the set is empty (no polygons at all)
void Deflate(int aAmount, CORNER_STRATEGY aCornerStrategy, int aMaxError)
void BooleanSubtract(const SHAPE_POLY_SET &b)
Perform boolean polyset difference.
const BOX2I BBox(int aClearance=0) const override
Compute a bounding box of the shape, with a margin of aClearance a collision.
@ ALLOW_ACUTE_CORNERS
just inflate the polygon. Acute angles create spikes
static bool registered
#define _(s)
const wxChar *const traceGalGroups
Flag to enable debug output of GAL cached group creation and drawing.
const wxChar *const traceRhi
Flag to enable debug output of the RHI rendering backend.
const wxChar *const traceGalProfile
Flag to enable debug output of GAL performance profiling.
std::string source
@ UNDEFINED_LAYER
Definition layer_ids.h:57
MATRIX3x3< double > MATRIX3x3D
Definition matrix3x3.h:469
The Cairo implementation of the graphics abstraction layer.
Definition eda_group.h:30
static RETAINED_GROUP_ORDER retainedOrderKey(VIEW_ITEM *aItem, int aPriority, const BOX2I &aBBox, bool aUseDrawPriority, bool aReverseDrawOrder, double aLodScale)
Definition view.cpp:241
static void sortRetainedOrder(VIEW *aView, std::vector< T > &aEntries)
Definition view.cpp:257
@ COLOR
Color has changed.
Definition view_item.h:50
@ INITIAL_ADD
Item is being added to the view.
Definition view_item.h:53
@ NONE
No updates are required.
Definition view_item.h:48
@ REPAINT
Item needs to be redrawn.
Definition view_item.h:54
@ APPEARANCE
Visibility flag has changed.
Definition view_item.h:49
@ GEOMETRY
Position or shape has changed.
Definition view_item.h:51
@ LAYERS
Layers have changed.
Definition view_item.h:52
@ ALL
All except INITIAL_ADD.
Definition view_item.h:55
@ TARGET_NONCACHED
Auxiliary rendering target (noncached)
Definition definitions.h:34
@ TARGET_CACHED
Main rendering target (cached)
Definition definitions.h:33
@ TARGET_OVERLAY
Items that may change while the view stays the same (noncached)
Definition definitions.h:35
@ HIDDEN
Item is temporarily hidden (usually in favor of a being drawn from an overlay, such as a SELECTION).
Definition view_item.h:66
@ OVERLAY_HIDDEN
Item is temporarily hidden from being drawn on an overlay.
Definition view_item.h:67
@ VISIBLE
Item is visible (in general)
Definition view_item.h:62
std::shared_ptr< PNS_LOG_VIEWER_OVERLAY > overlay
CITER next(CITER it)
Definition ptree.cpp:120
std::array< uint64_t, 2 > contentHash
RETAINED_GROUP_ORDER order
Definition view.cpp:237
bool operator()(VIEW_ITEM *aItem)
Definition view.cpp:1892
bool operator()(VIEW_ITEM *aItem)
Definition view.cpp:1363
DRAW_ITEM_VISITOR(VIEW *aView, int aLayer, bool aUseDrawPriority, bool aReverseDrawOrder)
Definition view.cpp:1352
int layers[VIEW_MAX_LAYERS]
Definition view.cpp:1441
std::vector< VIEW_ITEM * > drawItems
Definition view.cpp:1443
bool operator()(VIEW_ITEM *aItem)
Definition view.cpp:1743
RECACHE_ITEM_VISITOR(VIEW *aView, GAL *aGal, int aLayer)
Definition view.cpp:1736
How the content of a kept target depends on the scale it was emitted at.
Definition view.h:1160
bool read
something was sized from the scale
Definition view.h:1161
double lodAbove
nearest threshold above it
Definition view.h:1163
double lodBelow
nearest threshold under that scale
Definition view.h:1162
bool diffLayer
Layer should be drawn differentially over lower layers.
Definition view.h:911
int renderingOrder
Rendering order of this layer.
Definition view.h:916
bool hasNegatives
Layer should be drawn separately to not delete lower layers.
Definition view.h:914
bool visible
Is the layer to be rendered?
Definition view.h:907
bool displayOnly
Is the layer display only?
Definition view.h:908
std::shared_ptr< VIEW_RTREE > items
R-tree indexing all items on this layer.
Definition view.h:915
RENDER_TARGET target
Where the layer should be rendered.
Definition view.h:918
int id
Layer ID.
Definition view.h:917
bool moved
int delta
wxLogTrace helper definitions.
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:708
VECTOR2< double > VECTOR2D
Definition vector2d.h:707
static PIECE rect(double aLeft, double aTop, double aRight, double aBottom, bool aMixed=false)