KiCad PCB EDA Suite
Loading...
Searching...
No Matches
draw_panel_gal.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#include <eda_draw_frame.h>
24#include <kiface_base.h>
25#include <macros.h>
26#include <scoped_set_reset.h>
28#include <trace_helpers.h>
29
31#include <view/view.h>
33#include <gal/painter.h>
34#include <base_screen.h>
35#include <gal/cursors.h>
39#include <gal/cairo/cairo_gal.h>
40#include <math/vector2wx.h>
41
42
44#include <tool/tool_manager.h>
45
46#include <widgets/wx_infobar.h>
47
48#include <kiplatform/ui.h>
49
50#include <core/profile.h>
51
52#include <wx/display.h>
53
54#include <pgm_base.h>
55#include <confirm.h>
56
57
63static const wxChar traceDrawPanel[] = wxT( "KICAD_DRAW_PANEL" );
64
65
66EDA_DRAW_PANEL_GAL::EDA_DRAW_PANEL_GAL( wxWindow* aParentWindow, wxWindowID aWindowId,
67 const wxPoint& aPosition, const wxSize& aSize,
68 KIGFX::GAL_DISPLAY_OPTIONS& aOptions, GAL_TYPE aGalType ) :
69 wxScrolledCanvas( aParentWindow, aWindowId, aPosition, aSize ),
70 m_MouseCapturedLost( false ),
71 m_parent( aParentWindow ),
72 m_edaFrame( nullptr ),
75 m_drawing( false ),
76 m_drawingEnabled( false ),
77 m_needIdleRefresh( false ),
78 m_gal( nullptr ),
79 m_view( nullptr ),
80 m_painter( nullptr ),
81 m_viewControls( nullptr ),
83 m_options( aOptions ),
84 m_eventDispatcher( nullptr ),
85 m_lostFocus( false ),
86 m_glRecoveryAttempted( false ),
87 m_stealsFocus( true ),
88 m_statusPopup( nullptr )
89{
90#ifdef _WIN32
91 // need to fix broken cairo rendering on Windows with wx 3.3
92 SetDoubleBuffered( false );
93#endif
94 m_PaintEventCounter = std::make_unique<PROF_COUNTER>( "Draw panel paint events" );
95
96 if( Pgm().GetCommonSettings()->m_Appearance.show_scrollbars )
97 ShowScrollbars( wxSHOW_SB_ALWAYS, wxSHOW_SB_ALWAYS );
98 else
99 ShowScrollbars( wxSHOW_SB_NEVER, wxSHOW_SB_NEVER );
100
101 SetLayoutDirection( wxLayout_LeftToRight );
102
103 m_edaFrame = dynamic_cast<EDA_DRAW_FRAME*>( m_parent );
104
105 // If we're in a dialog, we have to go looking for our parent frame
106 if( !m_edaFrame )
107 {
108 wxWindow* ancestor = aParentWindow->GetParent();
109
110 while( ancestor && !dynamic_cast<EDA_DRAW_FRAME*>( ancestor ) )
111 ancestor = ancestor->GetParent();
112
113 if( ancestor )
114 m_edaFrame = dynamic_cast<EDA_DRAW_FRAME*>( ancestor );
115 }
116
117 SwitchBackend( aGalType );
118 SetBackgroundStyle( wxBG_STYLE_CUSTOM );
119
120 EnableScrolling( false, false ); // otherwise Zoom Auto disables GAL canvas
121 KIPLATFORM::UI::SetOverlayScrolling( this, false ); // Prevent excessive repaint on GTK
122 KIPLATFORM::UI::ImmControl( this, false ); // Ensure our panel can't suck in IME events
123
124 Connect( wxEVT_SIZE, wxSizeEventHandler( EDA_DRAW_PANEL_GAL::onSize ), nullptr, this );
125 Connect( wxEVT_ENTER_WINDOW, wxMouseEventHandler( EDA_DRAW_PANEL_GAL::onEnter ), nullptr,
126 this );
127 Connect( wxEVT_KILL_FOCUS, wxFocusEventHandler( EDA_DRAW_PANEL_GAL::onLostFocus ), nullptr,
128 this );
129
130 const wxEventType events[] = {
131 // Binding both EVT_CHAR and EVT_CHAR_HOOK ensures that all key events,
132 // especially special key like arrow keys, are handled by the GAL event dispatcher,
133 // and not sent to GUI without filtering, because they have a default action (scroll)
134 // that must not be called.
135 wxEVT_LEFT_UP,
136 wxEVT_LEFT_DOWN,
137 wxEVT_LEFT_DCLICK,
138 wxEVT_RIGHT_UP,
139 wxEVT_RIGHT_DOWN,
140 wxEVT_RIGHT_DCLICK,
141 wxEVT_MIDDLE_UP,
142 wxEVT_MIDDLE_DOWN,
143 wxEVT_MIDDLE_DCLICK,
144 wxEVT_AUX1_UP,
145 wxEVT_AUX1_DOWN,
146 wxEVT_AUX1_DCLICK,
147 wxEVT_AUX2_UP,
148 wxEVT_AUX2_DOWN,
149 wxEVT_AUX2_DCLICK,
150 wxEVT_MOTION,
151 wxEVT_MOUSEWHEEL,
152 wxEVT_CHAR,
153 wxEVT_CHAR_HOOK,
154 wxEVT_MAGNIFY,
156 };
157
158 for( wxEventType eventType : events )
159 Connect( eventType, wxEventHandler( EDA_DRAW_PANEL_GAL::OnEvent ), nullptr,
161
162 // Set up timer to detect when drawing starts
163 m_refreshTimer.SetOwner( this );
164 Connect( m_refreshTimer.GetId(), wxEVT_TIMER,
165 wxTimerEventHandler( EDA_DRAW_PANEL_GAL::onRefreshTimer ), nullptr, this );
166
167 Connect( wxEVT_SHOW, wxShowEventHandler( EDA_DRAW_PANEL_GAL::onShowEvent ), nullptr, this );
168}
169
170
172{
173 // Ensure EDA_DRAW_PANEL_GAL::onShowEvent is not fired during Dtor process
174 Disconnect( wxEVT_SHOW, wxShowEventHandler( EDA_DRAW_PANEL_GAL::onShowEvent ) );
175 StopDrawing();
176
177 wxASSERT( !m_drawing );
178
179 delete m_viewControls;
180 delete m_view;
181 delete m_gal;
182 m_gal = nullptr; // Ensure OnShow is not called
183}
184
185
187{
189 wxScrolledCanvas::SetFocus();
190 m_lostFocus = false;
191}
192
193
194void EDA_DRAW_PANEL_GAL::onPaint( wxPaintEvent& WXUNUSED( aEvent ) )
195{
196 DoRePaint( false );
197}
198
199
200bool EDA_DRAW_PANEL_GAL::recoverFromGalError( const std::exception& aError )
201{
202 // A predicted GPU out-of-memory cannot be cured by reinitializing OpenGL; it would just
203 // hit the same VRAM ceiling. Skip straight to the software fallback.
204 const bool gpuOutOfMemory = dynamic_cast<const KIGFX::GPU_OOM_ERROR*>( &aError ) != nullptr;
205
206 try
207 {
208 // Sleep/wake and GPU resets can invalidate the entire GL context.
209 // Try a full reinit of the current backend before falling back.
210 if( !gpuOutOfMemory && !m_glRecoveryAttempted )
211 {
213 GAL_TYPE prevBackend = m_backend;
215
216 if( SwitchBackend( prevBackend ) )
217 {
218 StartDrawing();
219 return true;
220 }
221 }
222
224 {
225 m_glRecoveryAttempted = false;
227
228 DisplayInfoMessage( m_parent, _( "Could not use OpenGL, falling back to software rendering" ),
229 wxString( aError.what() ) );
230
231 StartDrawing();
232 return true;
233 }
234
235 DisplayErrorMessage( m_parent, _( "Graphics error" ), wxString( aError.what() ) );
236 }
237 catch( std::exception& recoveryErr )
238 {
239 DisplayErrorMessage( m_parent, _( "Graphics error during recovery" ), wxString( recoveryErr.what() ) );
240 }
241 catch( ... )
242 {
243 DisplayErrorMessage( m_parent, _( "Graphics error during recovery" ),
244 _( "Unknown exception during backend switch" ) );
245 }
246
247 return false;
248}
249
250
251bool EDA_DRAW_PANEL_GAL::DoRePaint( bool aAllowSkip )
252{
253 if( !m_refreshMutex.try_lock() )
254 return false;
255
256 std::lock_guard<std::mutex> lock( m_refreshMutex, std::adopt_lock );
257
258 if( !m_drawingEnabled )
259 return false;
260
261 if( !m_gal->IsInitialized() || !m_gal->IsVisible() || m_gal->IsContextLocked() )
262 return false;
263
264 if( m_drawing )
265 return false;
266
267 m_lastRepaintStart = wxGetLocalTimeMillis();
268
269 // Repaint the canvas, and fix scrollbar cursors
270 // Usually called by a OnPaint event, but because it does not use a wxPaintDC,
271 // it can be called outside a wxPaintEvent.
272
273 // Update current zoom settings if the canvas is managed by a EDA frame
274 // (i.e. not by a preview panel in a dialog)
275 if( !IsDialogPreview() && GetParentEDAFrame() && GetParentEDAFrame()->GetScreen() )
277
278 if( Pgm().GetCommonSettings()->m_Appearance.show_scrollbars )
279 m_viewControls->UpdateScrollbars();
280
281#ifdef KICAD_GAL_PROFILE
282 latencyProbeZoomToRender.Checkpoint("do-repaint-start");
283#endif
284
285 SCOPED_SET_RESET<bool> drawing( m_drawing, true );
286
287 ( *m_PaintEventCounter )++;
288
289 wxASSERT( m_painter );
290
291 KIGFX::RENDER_SETTINGS* settings =
292 static_cast<KIGFX::RENDER_SETTINGS*>( m_painter->GetSettings() );
293
294 PROF_TIMER cntUpd("view-upd-items", false);
295 PROF_TIMER cntTotal("view-total", false);
296 PROF_TIMER cntCtx("view-context-create", false);
297 PROF_TIMER cntCtxDestroy("view-context-destroy", false);
298 PROF_TIMER cntRedraw("view-redraw-rects", false);
299
300 bool isDirty = false;
301
302 cntTotal.Start();
303
304 try
305 {
306 VECTOR2D cursorPos = m_viewControls->GetCursorPosition();
307 bool viewDirty = m_view->IsDirty();
308 bool cursorMoved = ( cursorPos != m_lastCursorPosition );
309 bool hasPendingItemUpdates = m_view->HasPendingItemUpdates();
310
311 // Skip all update work when nothing has changed since the previous frame.
312 // Never skip when responding to a native paint event or explicit ForceRefresh
313 // because the window content may have been invalidated by the OS.
314 if( aAllowSkip && !viewDirty && !cursorMoved && !hasPendingItemUpdates )
315 {
316 m_lastRepaintEnd = wxGetLocalTimeMillis();
317 return true;
318 }
319
320 if( hasPendingItemUpdates )
321 {
322 cntUpd.Start();
323
324 try
325 {
326 m_view->UpdateItems();
327 }
328 catch( std::out_of_range& err )
329 {
330 // Don't do anything here but don't fail
331 // This can happen when we don't catch `at()` calls
332 wxLogTrace( traceDrawPanel, wxS( "Out of Range error: %s" ), err.what() );
333 }
334 catch( std::runtime_error& err )
335 {
336 // Handle GL errors (e.g. glMapBuffer failure) that surface during UpdateItems().
337 // These can occur on macOS under memory pressure when embedding large 3D models.
338 // Log and continue so the outer handler can decide whether to switch backends.
339 wxLogTrace( traceDrawPanel, wxS( "Runtime error during UpdateItems: %s" ),
340 err.what() );
341 throw;
342 }
343
344 cntUpd.Stop();
345 viewDirty = m_view->IsDirty();
346 }
347
348 // After processing item updates, skip the GL cycle when neither the
349 // view targets nor the cursor position have changed.
350 if( aAllowSkip && !viewDirty && !cursorMoved )
351 {
352 m_lastRepaintEnd = wxGetLocalTimeMillis();
353 return true;
354 }
355
356 m_lastCursorPosition = cursorPos;
357
358 // GAL_DRAWING_CONTEXT can throw in the dtor, so we need to scope
359 // the full lifetime inside the try block
360 {
361 cntCtx.Start();
363 cntCtx.Stop();
364
365 if( m_view->IsTargetDirty( KIGFX::TARGET_OVERLAY )
366 && !m_gal->HasTarget( KIGFX::TARGET_OVERLAY ) )
367 {
368 m_view->MarkDirty();
369 }
370
371 m_gal->SetClearColor( settings->GetBackgroundColor() );
372 m_gal->SetGridColor( settings->GetGridColor() );
373 m_gal->SetCursorColor( settings->GetCursorColor() );
374
375 // OpenGL double-buffering leaves the back buffer undefined after
376 // SwapBuffers, so a full clear is always required before compositing.
377 // Cairo only needs to clear when NONCACHED content changed.
379 m_gal->ClearScreen();
380
381 if( m_view->IsDirty() )
382 {
383 if( m_backend != GAL_TYPE_OPENGL // Already called in opengl
384 && m_view->IsTargetDirty( KIGFX::TARGET_NONCACHED ) )
385 {
386 m_gal->ClearScreen();
387 }
388
389 m_view->ClearTargets();
390
391 // Grid has to be redrawn only when the NONCACHED target is redrawn
392 if( m_view->IsTargetDirty( KIGFX::TARGET_NONCACHED ) )
393 m_gal->DrawGrid();
394
395 cntRedraw.Start();
396 m_view->Redraw();
397 cntRedraw.Stop();
398 isDirty = true;
399 }
400
401 m_gal->DrawCursor( cursorPos );
402
403 #ifdef KICAD_GAL_PROFILE
404 latencyProbeZoomToRender.Checkpoint("do-repaint-pre-ctx-destroy");
405 #endif
406
407
408 cntCtxDestroy.Start();
409 }
410
411 // ctx goes out of scope here so destructor would be called
412 cntCtxDestroy.Stop();
413
414#ifdef KICAD_GAL_PROFILE
415 latencyProbeZoomToRender.Checkpoint("do-repaint-ctx-done");
416#endif
417
418 // OpenGL frame completed successfully, allow future recovery attempts
419 m_glRecoveryAttempted = false;
420 }
421 catch( std::exception& err )
422 {
423 wxLogTrace( traceDrawPanel, wxS( "DoRePaint exception: %s" ), err.what() );
424
425 if( recoverFromGalError( err ) )
426 return true;
427
428 StopDrawing();
429 }
430 catch( ... )
431 {
432 DisplayErrorMessage( m_parent, _( "Graphics error" ), _( "Unknown exception" ) );
433 StopDrawing();
434 }
435
436 if( isDirty )
437 {
438#ifdef KICAD_GAL_PROFILE
439 wxLogTrace( traceGalProfile, "View timing: %s %s %s %s %s",
440 cntTotal.to_string(),
441 cntUpd.to_string(),
442 cntRedraw.to_string(),
443 cntCtx.to_string(),
444 cntCtxDestroy.to_string()
445 );
446#endif
447 }
448
449 m_lastRepaintEnd = wxGetLocalTimeMillis();
450
451#ifdef KICAD_GAL_PROFILE
452 wxLogTrace( traceGalProfile, "%s", latencyProbeZoomToRender.to_string() );
453 latencyProbeRepaintToMotion.Reset();
454 latencyProbeRepaintToMotion.Checkpoint("repaint-done");
455#endif
456
457 return true;
458}
459
460
461void EDA_DRAW_PANEL_GAL::onSize( wxSizeEvent& aEvent )
462{
463 // If we get a second wx update call before the first finishes, don't crash
464 if( m_gal->IsContextLocked() )
465 return;
466
468 wxSize clientSize = GetClientSize();
469 WX_INFOBAR* infobar = GetParentEDAFrame() ? GetParentEDAFrame()->GetInfoBar() : nullptr;
470
471 if( ToVECTOR2I( clientSize ) == m_gal->GetScreenPixelSize() )
472 return;
473
474 // Note: ( +1, +1 ) prevents an ugly black line on right and bottom on Mac
475 clientSize.x = std::max( 10, clientSize.x + 1 );
476 clientSize.y = std::max( 10, clientSize.y + 1 );
477
478 VECTOR2D bottom( 0, 0 );
479
480 if( m_view )
481 bottom = m_view->ToWorld( m_gal->GetScreenPixelSize(), true );
482
483 m_gal->ResizeScreen( clientSize.GetX(), clientSize.GetY() );
484
485 if( m_view )
486 {
487 if( infobar && infobar->IsLocked() )
488 {
489 VECTOR2D halfScreen( std::ceil( 0.5 * clientSize.x ), std::ceil( 0.5 * clientSize.y ) );
490 m_view->SetCenter( bottom - m_view->ToWorld( halfScreen, false ) );
491 }
492
493 m_view->MarkTargetDirty( KIGFX::TARGET_CACHED );
494 m_view->MarkTargetDirty( KIGFX::TARGET_NONCACHED );
495 }
496}
497
498
503
504
505void EDA_DRAW_PANEL_GAL::Refresh( bool aEraseBackground, const wxRect* aRect )
506{
507 wxLongLong now = wxGetLocalTimeMillis();
508 wxLongLong delta = now - m_lastRepaintEnd;
509 bool galInitialized = m_gal && m_gal->IsInitialized();
510
511 // wxGetLocalTimeMillis is wall clock, so an NTP correction or manual
512 // clock change can make delta negative. Treat that as "long enough".
513 if( delta < 0 )
514 delta = 0;
515
516 // When vsync is available the driver throttles SwapBuffers, so we only need
517 // a small guard to avoid queueing work faster than the GPU can consume it.
518 // Without vsync, cap the render rate at the monitor refresh rate so the
519 // GPU is not saturated producing frames that will never be shown.
520 int minPeriodMs = 3;
521
522 if( galInitialized && m_gal->GetSwapInterval() == 0 )
523 {
524 // wxDisplay reports 0 on headless, some virtualized, and a few driver
525 // combinations. Clamp to a plausible monitor range before trusting it
526 // and fall back to 60 Hz otherwise.
527 int refreshHz = 60;
528 int reported = wxDisplay( this ).GetCurrentMode().refresh;
529
530 if( reported >= 24 && reported <= 1000 )
531 refreshHz = reported;
532
533 minPeriodMs = 1000 / refreshHz;
534 }
535
536 if( delta >= minPeriodMs )
537 {
538 if( !DoRePaint() )
540 }
541 else if( !m_refreshTimer.IsRunning() )
542 {
543 m_refreshTimer.StartOnce( static_cast<int>( ( minPeriodMs - delta ).GetValue() ) );
544 }
545}
546
547
549{
550 if( !m_drawingEnabled )
551 {
552 if( m_gal && m_gal->IsInitialized() )
553 {
554 Connect( wxEVT_PAINT, wxPaintEventHandler( EDA_DRAW_PANEL_GAL::onPaint ), nullptr,
555 this );
556
557 Connect( wxEVT_IDLE, wxIdleEventHandler( EDA_DRAW_PANEL_GAL::onIdle ), nullptr, this );
558
559 m_drawingEnabled = true;
560 }
561 else
562 {
563 // Try again soon
564 m_refreshTimer.StartOnce( 100 );
565 return;
566 }
567 }
568
569 DoRePaint( false );
570}
571
572
573bool EDA_DRAW_PANEL_GAL::GetScreenshot( wxImage& aDstImage )
574{
575 if( m_backend != GAL_TYPE_OPENGL || !m_gal )
576 return false;
577
578 DoRePaint( false );
579
580 return static_cast<KIGFX::OPENGL_GAL*>( m_gal )->GetScreenshot( aDstImage );
581}
582
583
585{
586 m_eventDispatcher = aEventDispatcher;
587}
588
589
591{
592 // Start querying GAL if it is ready
593 m_refreshTimer.StartOnce( 100 );
594}
595
596
598{
599 m_refreshTimer.Stop();
600 m_drawingEnabled = false;
601
602 Disconnect( wxEVT_PAINT, wxPaintEventHandler( EDA_DRAW_PANEL_GAL::onPaint ), nullptr, this );
603
604 Disconnect( wxEVT_IDLE, wxIdleEventHandler( EDA_DRAW_PANEL_GAL::onIdle ), nullptr, this );
605}
606
607
609{
610 // Set display settings for high contrast mode
611 KIGFX::RENDER_SETTINGS* rSettings = m_view->GetPainter()->GetSettings();
612
613 SetTopLayer( aLayer );
614
615 rSettings->ClearHighContrastLayers();
616 rSettings->SetLayerIsHighContrast( aLayer );
617
618 m_view->UpdateAllLayersColor();
619}
620
621
623{
624 m_view->ClearTopLayers();
625 m_view->SetTopLayer( aLayer );
626 m_view->UpdateAllLayersOrder();
627}
628
629
631{
632 // Do not do anything if the currently used GAL is correct
633 if( aGalType == m_backend && m_gal != nullptr )
634 return true;
635
636 VECTOR2D grid_size = m_gal ? m_gal->GetGridSize() : VECTOR2D();
637 bool grid_visibility = m_gal ? m_gal->GetGridVisibility() : true;
638 bool result = true; // assume everything will be fine
639
640 // Prevent refreshing canvas during backend switch
641 StopDrawing();
642
643 KIGFX::GAL* new_gal = nullptr;
644
645 try
646 {
647 switch( aGalType )
648 {
649 case GAL_TYPE_OPENGL:
650 {
651 wxString errormsg = KIGFX::OPENGL_GAL::CheckFeatures( m_options );
652
653 if( errormsg.empty() )
654 {
655 new_gal = new KIGFX::OPENGL_GAL( GetVcSettings(), m_options, this, this, this );
656 }
657 else
658 {
659 if( GAL_FALLBACK != aGalType )
660 {
661 aGalType = GAL_FALLBACK;
663 m_parent,
664 _( "Could not use OpenGL, falling back to software rendering" ),
665 errormsg );
666 new_gal = new KIGFX::CAIRO_GAL( m_options, this, this, this );
667 }
668 else
669 {
670 // We're well and truly banjaxed if we get here without a fallback.
671 DisplayInfoMessage( m_parent, _( "Could not use OpenGL" ), errormsg );
672 }
673 }
674
675 break;
676 }
677
678 case GAL_TYPE_CAIRO:
679 new_gal = new KIGFX::CAIRO_GAL( m_options, this, this, this );
680 break;
681
682 default:
683 wxASSERT( false );
685 // warn about unhandled GAL canvas type, but continue with the fallback option
686
687 case GAL_TYPE_NONE:
688 // KIGFX::GAL is a stub - it actually does cannot display anything,
689 // but prevents code relying on GAL canvas existence from crashing
690 new_gal = new KIGFX::GAL( m_options );
691 break;
692 }
693 }
694 catch( std::runtime_error& err )
695 {
696 // Create a dummy GAL
697 new_gal = new KIGFX::GAL( m_options );
698 aGalType = GAL_TYPE_NONE;
699 DisplayErrorMessage( m_parent, _( "Error switching GAL backend" ), wxString( err.what() ) );
700 result = false;
701 }
702
703 // trigger update of the gal options in case they differ from the defaults
704 m_options.NotifyChanged();
705
706 delete m_gal;
707 m_gal = new_gal;
708
709 wxSize clientSize = GetClientSize();
710 clientSize.x = std::max( 10, clientSize.x );
711 clientSize.y = std::max( 10, clientSize.y );
712 m_gal->ResizeScreen( clientSize.GetX(), clientSize.GetY() );
713
714 if( grid_size.x > 0 && grid_size.y > 0 )
715 m_gal->SetGridSize( grid_size );
716
717 m_gal->SetGridVisibility( grid_visibility );
718
719 // Make sure the cursor is set on the new canvas
721
722 if( m_painter )
723 m_painter->SetGAL( m_gal );
724
725 if( m_view )
726 {
727 m_view->SetGAL( m_gal );
728 // Note: OpenGL requires reverse draw order when draw priority is enabled
729 m_view->ReverseDrawOrder( aGalType == GAL_TYPE_OPENGL );
730 }
731
732 m_backend = aGalType;
733
734 return result;
735}
736
737
738void EDA_DRAW_PANEL_GAL::OnEvent( wxEvent& aEvent )
739{
740 bool shouldSetFocus = m_lostFocus && m_stealsFocus
741 && !KIUI::IsInputControlFocused() // Don't steal from input controls
742 && !KIUI::IsModalDialogFocused() // Don't steal from dialogs
743 && KIPLATFORM::UI::IsWindowActive( m_edaFrame ); // Don't steal from other windows
744
745 if( shouldSetFocus )
746 SetFocus();
747
748 if( !m_eventDispatcher )
749 aEvent.Skip();
750 else
751 m_eventDispatcher->DispatchWxEvent( aEvent );
752
753 Refresh();
754}
755
756
757void EDA_DRAW_PANEL_GAL::onEnter( wxMouseEvent& aEvent )
758{
759 bool shouldSetFocus = m_stealsFocus
760 && !KIUI::IsInputControlFocused() // Don't steal from input controls
761 && !KIUI::IsModalDialogFocused() // Don't steal from dialogs
762 && KIPLATFORM::UI::IsWindowActive( m_edaFrame ); // Don't steal from other windows
763
764 // Getting focus is necessary in order to receive key events properly
765 if( shouldSetFocus )
766 SetFocus();
767
768 aEvent.Skip();
769}
770
771
772void EDA_DRAW_PANEL_GAL::onLostFocus( wxFocusEvent& aEvent )
773{
774 m_lostFocus = true;
775
776 m_viewControls->CancelDrag();
777
778 // Reset the tool dispatcher's button state when focus is lost. This prevents
779 // the dispatcher from thinking the button is still pressed when focus returns,
780 // which can cause selection and drag operations to stop working.
782 m_eventDispatcher->ResetState();
783
784 aEvent.Skip();
785}
786
787
788void EDA_DRAW_PANEL_GAL::onIdle( wxIdleEvent& aEvent )
789{
791 {
792 m_needIdleRefresh = false;
793 Refresh();
794 }
795
796 aEvent.Skip();
797}
798
799
800void EDA_DRAW_PANEL_GAL::onRefreshTimer( wxTimerEvent& aEvent )
801{
802 ForceRefresh();
803}
804
805
806void EDA_DRAW_PANEL_GAL::onShowEvent( wxShowEvent& aEvent )
807{
808 if( m_gal && m_gal->IsInitialized() && m_gal->IsVisible() )
809 {
810 OnShow();
811 }
812}
813
814
816{
817 if( !m_gal )
818 return;
819
820 DPI_SCALING_COMMON dpi( nullptr, m_parent );
821
822 bool hidpi = false;
823
824 // Cursor scaling factor cannot be set for a wxCursor on GTK and OSX (at least before wx 3.3),
825 // resulting in 4x rendered size on 2x window scale.
826 // MSW renders the bitmap as-is, without scaling, so this works here.
827#ifdef __WXMSW__
828 hidpi = dpi.GetContentScaleFactor() >= 2.0;
829#endif
830
831 m_gal->SetNativeCursorStyle( aCursor, hidpi );
832}
833
834
835std::shared_ptr<KIGFX::VIEW_OVERLAY> EDA_DRAW_PANEL_GAL::DebugOverlay()
836{
837 if( !m_debugOverlay )
838 {
839 m_debugOverlay.reset( new KIGFX::VIEW_OVERLAY() );
840 m_view->Add( m_debugOverlay.get() );
841 }
842
843 return m_debugOverlay;
844}
845
846
848{
849 if( m_debugOverlay )
850 {
851 m_view->Remove( m_debugOverlay.get() );
852 m_debugOverlay = nullptr;
853 }
854}
855
856
858{
860
861 KIGFX::VC_SETTINGS vcSettings;
862 vcSettings.m_warpCursor = cfg->m_Input.center_on_zoom;
864 vcSettings.m_autoPanSettingEnabled = cfg->m_Input.auto_pan;
866 vcSettings.m_horizontalPan = cfg->m_Input.horizontal_pan;
868 vcSettings.m_zoomSpeed = cfg->m_Input.zoom_speed;
869 vcSettings.m_zoomSpeedAuto = cfg->m_Input.zoom_speed_auto;
874 vcSettings.m_dragLeft = cfg->m_Input.drag_left;
875 vcSettings.m_dragMiddle = cfg->m_Input.drag_middle;
876 vcSettings.m_dragRight = cfg->m_Input.drag_right;
879
880 return vcSettings;
881}
BASE_SCREEN class implementation.
VECTOR2D m_ScrollCenter
Current scroll center point in logical units.
Definition base_screen.h:96
Class to handle configuration and automatic determination of the DPI scale to use for canvases.
double GetContentScaleFactor() const override
Get the content scale factor, which may be different from the scale factor on some platforms.
WX_INFOBAR * GetInfoBar()
The base class for create windows for drawing purpose.
virtual BASE_SCREEN * GetScreen() const
Return a pointer to a BASE_SCREEN or one of its derivatives.
std::unique_ptr< PROF_COUNTER > m_PaintEventCounter
bool m_glRecoveryAttempted
Set after an OpenGL recovery attempt to prevent infinite retry loops.
EDA_DRAW_FRAME * m_edaFrame
Parent EDA_DRAW_FRAME (if available)
void onLostFocus(wxFocusEvent &aEvent)
static constexpr GAL_TYPE GAL_FALLBACK
std::unique_ptr< KIGFX::PAINTER > m_painter
Contains information about how to draw items using GAL.
void onSize(wxSizeEvent &aEvent)
bool m_needIdleRefresh
True when canvas needs to be refreshed from idle handler.
virtual void SetHighContrastLayer(int aLayer)
Take care of display settings for the given layer to be displayed in high contrast mode.
bool m_stealsFocus
Flag to indicate whether the panel should take focus at certain times (when moused over,...
KIGFX::GAL_DISPLAY_OPTIONS & m_options
void StopDrawing()
Prevent the GAL canvas from further drawing until it is recreated or StartDrawing() is called.
static KIGFX::VC_SETTINGS GetVcSettings()
Gets a populated View Controls settings object dervived from our program settings.
virtual void SetTopLayer(int aLayer)
Move the selected layer to the top, so it is displayed above all others.
void ClearDebugOverlay()
Clear the contents of the debug overlay and removes it from the VIEW.
virtual KIGFX::VIEW * GetView() const
Return a pointer to the #VIEW instance used in the panel.
void ForceRefresh()
Force a redraw.
bool recoverFromGalError(const std::exception &aErr)
bool m_drawing
True if GAL is currently redrawing the view.
KIGFX::GAL * m_gal
Interface for drawing objects on a 2D-surface.
EDA_DRAW_PANEL_GAL(wxWindow *aParentWindow, wxWindowID aWindowId, const wxPoint &aPosition, const wxSize &aSize, KIGFX::GAL_DISPLAY_OPTIONS &aOptions, GAL_TYPE aGalType=GAL_TYPE_OPENGL)
Create a drawing panel that is contained inside aParentWindow.
void onRefreshTimer(wxTimerEvent &aEvent)
void SetCurrentCursor(KICURSOR aCursor)
Set the current cursor shape for this panel.
virtual void onPaint(wxPaintEvent &WXUNUSED(aEvent))
bool m_lostFocus
Flag to indicate that focus should be regained on the next mouse event.
virtual void OnShow()
Called when the window is shown for the first time.
std::shared_ptr< KIGFX::VIEW_OVERLAY > m_debugOverlay
Optional overlay for drawing transient debug objects.
virtual void Refresh(bool aEraseBackground=true, const wxRect *aRect=nullptr) override
wxTimer m_refreshTimer
Timer to prevent too-frequent refreshing.
TOOL_DISPATCHER * m_eventDispatcher
Processes and forwards events to tools.
wxWindow * m_parent
Pointer to the parent window.
wxLongLong m_lastRepaintStart
Timestamp of the last repaint start.
@ GAL_TYPE_OPENGL
OpenGL implementation.
@ GAL_TYPE_CAIRO
Cairo implementation.
@ GAL_TYPE_NONE
GAL not used (the legacy wxDC engine is used)
KIGFX::VIEW * m_view
Stores view settings (scale, center, etc.) and items to be drawn.
bool m_MouseCapturedLost
used on wxMSW: true after a wxEVT_MOUSE_CAPTURE_LOST was received false after the mouse is recaptured...
KIGFX::WX_VIEW_CONTROLS * m_viewControls
Control for VIEW (moving, zooming, etc.)
void SetFocus() override
void onIdle(wxIdleEvent &aEvent)
void SetEventDispatcher(TOOL_DISPATCHER *aEventDispatcher)
Set a dispatcher that processes events and forwards them to tools.
void onEnter(wxMouseEvent &aEvent)
void RequestRefresh()
Make sure a refresh gets done on the next idle event if it hasn't already.
std::mutex m_refreshMutex
Blocks multiple calls to the draw.
virtual bool SwitchBackend(GAL_TYPE aGalType)
Switch method of rendering graphics.
void StartDrawing()
Begin drawing if it was stopped previously.
wxLongLong m_lastRepaintEnd
Timestamp of the last repaint end.
bool DoRePaint(bool aAllowSkip=true)
Repaint the canvas, and fix scrollbar cursors.
void OnEvent(wxEvent &aEvent)
Used to forward events to the canvas from popups, etc.
bool m_drawingEnabled
Flag that determines if VIEW may use GAL for redrawing the screen.
std::shared_ptr< KIGFX::VIEW_OVERLAY > DebugOverlay()
Create an overlay for rendering debug graphics.
void onShowEvent(wxShowEvent &aEvent)
static constexpr bool GAL_FALLBACK_AVAILABLE
GAL_TYPE m_backend
Currently used GAL.
VECTOR2D m_lastCursorPosition
Last cursor position sent to GAL for drawing.
bool GetScreenshot(wxImage &aDstImage)
Capture the current canvas contents into aDstImage.
EDA_DRAW_FRAME * GetParentEDAFrame() const
Returns parent EDA_DRAW_FRAME, if available or NULL otherwise.
Abstract interface for drawing on a 2D-surface.
Raised when a GPU buffer allocation is predicted to exceed the available video memory.
OpenGL implementation of the Graphics Abstraction Layer.
Definition opengl_gal.h:70
static wxString CheckFeatures(GAL_DISPLAY_OPTIONS &aOptions)
Checks OpenGL features.
Container for all the knowledge about how graphical objects are drawn on any output surface/device.
void ClearHighContrastLayers()
Clear the list of active layers.
virtual const COLOR4D & GetGridColor()=0
Return current grid color settings.
virtual const COLOR4D & GetBackgroundColor() const =0
Return current background color settings.
void SetLayerIsHighContrast(int aLayerId, bool aEnabled=true)
Set the specified layer as high-contrast.
virtual const COLOR4D & GetCursorColor()=0
Return current cursor color settings.
const VECTOR2D & GetCenter() const
Return the center point of this VIEW (in world space coordinates).
Definition view.h:351
static const wxEventType EVT_REFRESH_MOUSE
Event that forces mouse move event in the dispatcher (eg.
virtual COMMON_SETTINGS * GetCommonSettings() const
Definition pgm_base.cpp:528
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
void Start()
Start or restart the counter.
Definition profile.h:74
std::string to_string()
Definition profile.h:153
RAII class that sets an value at construction and resets it to the original value at destruction.
A modified version of the wxInfoBar class that allows us to:
Definition wx_infobar.h:77
bool IsLocked()
Returns true if the infobar is being updated.
Definition wx_infobar.h:207
void DisplayInfoMessage(wxWindow *aParent, const wxString &aMessage, const wxString &aExtraInfo)
Display an informational message box with aMessage.
Definition confirm.cpp:245
void DisplayErrorMessage(wxWindow *aParent, const wxString &aText, const wxString &aExtraInfo)
Display an error message with aMessage.
Definition confirm.cpp:217
This file is part of the common library.
KICURSOR
Definition cursors.h:40
@ ARROW
Definition cursors.h:42
#define _(s)
static const wxChar traceDrawPanel[]
Flag to enable drawing panel debugging output.
const wxChar *const traceGalProfile
Flag to enable debug output of GAL performance profiling.
This file contains miscellaneous commonly used macros and functions.
#define KI_FALLTHROUGH
The KI_FALLTHROUGH macro is to be used when switch statement cases should purposely fallthrough from ...
Definition macros.h:79
@ 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
void SetOverlayScrolling(const wxWindow *aWindow, bool overlay)
Used to set overlay/non-overlay scrolling mode in a window.
Definition wxgtk/ui.cpp:301
void ImmControl(wxWindow *aWindow, bool aEnable)
Configures the IME mode of a given control handle.
Definition wxgtk/ui.cpp:418
void ImeNotifyCancelComposition(wxWindow *aWindow)
Asks the IME to cancel.
Definition wxgtk/ui.cpp:423
bool IsWindowActive(wxWindow *aWindow)
Check to see if the given window is the currently active window (e.g.
Definition wxgtk/ui.cpp:148
KICOMMON_API bool IsInputControlFocused(wxWindow *aFocus=nullptr)
Check if a input control has focus.
KICOMMON_API bool IsModalDialogFocused()
PGM_BASE & Pgm()
The global program "get" accessor.
see class PGM_BASE
MOUSE_DRAG_ACTION drag_right
MOUSE_DRAG_ACTION drag_middle
MOUSE_DRAG_ACTION drag_left
Structure to keep VIEW_CONTROLS settings for easy store/restore operations.
MOUSE_DRAG_ACTION m_dragLeft
bool m_horizontalPan
Enable horizontal panning with the horizontal scroll/trackpad input.
bool m_scrollReversePanH
Whether to invert the scroll wheel movement for horizontal pan.
bool m_autoPanSettingEnabled
Flag for turning on autopanning.
bool m_focusFollowSchPcb
Flag for automatic focus switching between Schematic and PCB editors.
float m_autoPanAcceleration
How fast does panning accelerate when approaching the window boundary.
MOUSE_DRAG_ACTION m_dragMiddle
int m_zoomSpeed
Zoom speed for the non-accelerating zoom controller.
int m_scrollModifierZoom
What modifier key to enable zoom with the (vertical) scroll wheel.
int m_scrollModifierPanH
What modifier key to enable horizontal pan with the (vertical) scroll wheel.
bool m_warpCursor
If the cursor is allowed to be warped.
MOUSE_DRAG_ACTION m_dragRight
bool m_scrollReverseZoom
Whether to invert the scroll wheel movement for zoom.
int m_motionPanModifier
What modifier key pans the view when the mouse moves with it held.
bool m_zoomAcceleration
Enable the accelerating zoom controller.
bool m_zoomSpeedAuto
When true, ignore zoom_speed and pick a platform-specific default.
int m_scrollModifierPanV
What modifier key to enable vertical with the (vertical) scroll wheel.
wxString result
Test unit parsing edge cases and error handling.
int delta
wxLogTrace helper definitions.
VECTOR2< double > VECTOR2D
Definition vector2d.h:682
VECTOR2I ToVECTOR2I(const wxSize &aSize)
Definition vector2wx.h:26
WX_VIEW_CONTROLS class definition.