Skip to content

Commit

Permalink
Fixes several drawing issues
Browse files Browse the repository at this point in the history
  • Loading branch information
crsib committed Apr 20, 2022
1 parent b9180f9 commit 3bff6d5
Show file tree
Hide file tree
Showing 20 changed files with 385 additions and 127 deletions.
344 changes: 272 additions & 72 deletions libraries/lib-graphics-wx/graphics/WXGraphicsContextPainter.cpp

Large diffs are not rendered by default.

14 changes: 13 additions & 1 deletion libraries/lib-graphics-wx/graphics/WXGraphicsContextPainter.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@

#include "graphics/Painter.h"

class wxWindow;
class wxDC;
class wxGraphicsRenderer;
class wxGraphicsContext;
class wxGraphicsPath;
class wxPoint2DDouble;
Expand All @@ -25,7 +28,13 @@ GRAPHICS_WX_API RendererID WXGraphicsContextPainterRendererID();
class GRAPHICS_WX_API WXGraphicsContextPainter final : public Painter
{
public:
explicit WXGraphicsContextPainter(wxGraphicsContext* graphicsContext, const wxFont& defaultFont);
WXGraphicsContextPainter(
wxGraphicsRenderer* renderer, wxWindow* window, const wxFont& defaultFont);
WXGraphicsContextPainter(
wxGraphicsRenderer* renderer, wxDC* dc, const wxFont& defaultFont);
WXGraphicsContextPainter(
wxGraphicsRenderer* renderer, const wxFont& defaultFont);

~WXGraphicsContextPainter();

WXGraphicsContextPainter(const WXGraphicsContextPainter&) = delete;
Expand Down Expand Up @@ -57,6 +66,9 @@ class GRAPHICS_WX_API WXGraphicsContextPainter final : public Painter
void Flush() override;

private:
void BeginPaint() override;
void EndPaint() override;

void DoClear(const Rect& rect, Color color) override;

void UpdateBrush(const Brush& brush) override;
Expand Down
27 changes: 5 additions & 22 deletions libraries/lib-graphics-wx/graphics/WXPainterFactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,44 +23,27 @@ namespace
{
wxGraphicsRenderer* GetRenderer()
{
//return wxGraphicsRenderer::GetDefaultRenderer();
return wxGraphicsRenderer::GetDirect2DRenderer();
}
}

std::unique_ptr<Painter> CreatePainter(wxWindow* wnd)
{
return std::make_unique<WXGraphicsContextPainter>(
GetRenderer()->CreateContext(wnd), wnd->GetFont());
GetRenderer(), wnd, wnd->GetFont());
}

std::unique_ptr<Painter> CreatePainter(wxWindowDC& dc)
std::unique_ptr<Painter> CreatePainterFromDC(wxDC& dc)
{
return std::make_unique<WXGraphicsContextPainter>(
GetRenderer()->CreateContext(dc), dc.GetFont());
}

std::unique_ptr<Painter> CreatePainter(wxMemoryDC& dc)
{
return std::make_unique<WXGraphicsContextPainter>(
GetRenderer()->CreateContext(dc), dc.GetFont());
}

std::unique_ptr<Painter> CreatePainter(wxPrinterDC& dc)
{
return std::make_unique<WXGraphicsContextPainter>(
GetRenderer()->CreateContext(dc), dc.GetFont());
}

std::unique_ptr<Painter> CreatePainter(wxDC& dc)
{
return std::make_unique<WXGraphicsContextPainter>(
GetRenderer()->CreateContextFromUnknownDC(dc), dc.GetFont());
GetRenderer(), &dc, dc.GetFont());
}

Painter& GetMeasuringPainter()
{
static auto context = std::make_unique<WXGraphicsContextPainter>(
GetRenderer()->CreateMeasuringContext(), *wxNORMAL_FONT);
GetRenderer(), *wxNORMAL_FONT);

return *context;
}
5 changes: 1 addition & 4 deletions libraries/lib-graphics-wx/graphics/WXPainterFactory.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,6 @@ class wxPrinterDC;
class wxDC;

GRAPHICS_WX_API std::unique_ptr<Painter> CreatePainter(wxWindow* wnd);
GRAPHICS_WX_API std::unique_ptr<Painter> CreatePainter(wxWindowDC& dc);
GRAPHICS_WX_API std::unique_ptr<Painter> CreatePainter(wxMemoryDC& dc);
GRAPHICS_WX_API std::unique_ptr<Painter> CreatePainter(wxPrinterDC& dc);
GRAPHICS_WX_API std::unique_ptr<Painter> CreatePainter(wxDC& dc);
GRAPHICS_WX_API std::unique_ptr<Painter> CreatePainterFromDC(wxDC& dc);

GRAPHICS_WX_API Painter& GetMeasuringPainter();
11 changes: 8 additions & 3 deletions libraries/lib-graphics-wx/graphics/WXPainterUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ PenStyle GetPenStyle(wxPenStyle style) noexcept
switch (style)
{
case wxPENSTYLE_INVALID:
case wxPENSTYLE_TRANSPARENT:
return PenStyle::None;
case wxPENSTYLE_SOLID:
return PenStyle::Solid;
Expand All @@ -45,7 +46,7 @@ wxPenStyle GetPenStyle(PenStyle style) noexcept
switch (style)
{
case PenStyle::None:
return wxPENSTYLE_INVALID;
return wxPENSTYLE_TRANSPARENT;
case PenStyle::Solid:
return wxPENSTYLE_SOLID;
case PenStyle::Dot:
Expand Down Expand Up @@ -75,14 +76,18 @@ GRAPHICS_WX_API Brush BrushFromWXBrush(const wxBrush& brush) noexcept

wxPen wxPenFromPen(const Pen& pen) noexcept
{
return wxPen(wxColorFromColor(pen.GetColor()), pen.GetWidth(), GetPenStyle(pen.GetStyle()));
return pen.GetStyle() != PenStyle::None ?
wxPen(
wxColorFromColor(pen.GetColor()), pen.GetWidth(),
GetPenStyle(pen.GetStyle())) :
wxNullPen;
}

wxBrush wxBrushFromBrush(const Brush& brush) noexcept
{
return brush.GetStyle() == BrushStyle::Solid ?
wxBrush(wxColorFromColor(brush.GetColor())) :
wxBrush();
wxNullBrush;
}

GRAPHICS_WX_API std::shared_ptr<PainterFont>
Expand Down
6 changes: 3 additions & 3 deletions libraries/lib-graphics-wx/waveform/WaveBitmapCache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -379,17 +379,17 @@ bool WaveBitmapCache::InitializeElement(
FrameStatistics::SectionID::WaveBitmapCache);

auto imageData = mCachedImage->GetData();
constexpr auto stride = CacheElementWidth * 3;

const auto columnsCount = mLookupHelper->AvailableColumns;

const auto defaultColor = Triplet(mPaintParamters.BlankColor);

const auto height = static_cast<uint32_t>(mPaintParamters.Height);

auto rowData = imageData;

for (uint32_t row = 0; row < height; ++row)
{
auto rowData = imageData + row * stride;
auto colorFunction = mLookupHelper->ColorFunctions.data();

for (size_t pixel = 0; pixel < columnsCount; ++pixel)
Expand All @@ -408,7 +408,7 @@ bool WaveBitmapCache::InitializeElement(
element.IsComplete = mLookupHelper->IsComplete;

element.Bitmap = mPainter->CreateImage(
PainterImageFormat::RGB888, CacheElementWidth, height, imageData);
PainterImageFormat::RGB888, columnsCount, height, imageData);

return true;
}
Expand Down
16 changes: 16 additions & 0 deletions libraries/lib-graphics/graphics/Painter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -499,6 +499,11 @@ void Painter::Clear(const Rect& rect, Color color)
DoClear(rect, color);
}

PaintEventHolder Painter::Paint()
{
return PaintEventHolder(*this);
}

PainterOffscreenHolder Painter::PaintOn(PainterImage& image)
{
return PainterOffscreenHolder(image, *this);
Expand Down Expand Up @@ -854,3 +859,14 @@ Rect Painter::GetImageRect(const PainterImage& image) const
return { Point {}, Size { static_cast<float>(image.GetWidth()),
static_cast<float>(image.GetHeight()) } };
}

PaintEventHolder::~PaintEventHolder()
{
mPainter.EndPaint();
}

PaintEventHolder::PaintEventHolder(Painter& painter)
: mPainter(painter)
{
mPainter.BeginPaint();
}
19 changes: 18 additions & 1 deletion libraries/lib-graphics/graphics/Painter.h
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,19 @@ class GRAPHICS_API PainterOffscreenHolder final
friend class Painter;
};

class GRAPHICS_API PaintEventHolder final
{
public:
~PaintEventHolder();

private:
PaintEventHolder(Painter& painter);

Painter& mPainter;

friend class Painter;
};

class GRAPHICS_API Painter /* not final */
{
public:
Expand Down Expand Up @@ -366,9 +379,12 @@ class GRAPHICS_API Painter /* not final */

virtual void Flush() = 0;

PaintEventHolder Paint();
PainterOffscreenHolder PaintOn(PainterImage& image);

protected:
virtual void BeginPaint() = 0;
virtual void EndPaint() = 0;
virtual void DoClear(const Rect& rect, Color color) = 0;

virtual void UpdateBrush(const Brush& brush) = 0;
Expand Down Expand Up @@ -414,7 +430,7 @@ class GRAPHICS_API Painter /* not final */

struct PainterState final
{
Pen Pen;
Pen Pen { Pen::NoPen };
Brush Brush;
std::shared_ptr<PainterFont> Font;
bool Antialiasing { true };
Expand All @@ -429,4 +445,5 @@ class GRAPHICS_API Painter /* not final */
friend class PainterClipStateMutator;
friend class PainterPath;
friend class PainterOffscreenHolder;
friend class PaintEventHolder;
};
6 changes: 3 additions & 3 deletions libraries/lib-theme/AColor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -559,7 +559,7 @@ void AColor::Arrow(Painter& painter, wxCoord x, wxCoord y, int width, bool down)
{
pt[0].x = x;
pt[0].y = y + half;
pt[1].x = y + half;
pt[1].x = x + half;
pt[1].y = y;
pt[2].x = x + width;
pt[2].y = y + half;
Expand Down Expand Up @@ -716,7 +716,7 @@ void AColor::Light(PainterStateMutator& mutator, bool selected, bool highlight)
if (!inited)
Init();

int index = (int)selected;
int index = (int)selected;
const auto brush = BrushFromWXBrush(highlight ? AColor::uglyBrush : lightBrush[index]);
mutator.SetBrush(brush);
const auto pen = PenFromWXPen(highlight ? AColor::uglyPen : lightPen[index]);
Expand Down Expand Up @@ -748,7 +748,7 @@ void AColor::Dark(PainterStateMutator& mutator, bool selected, bool highlight)
if (!inited)
Init();

int index = (int)selected;
int index = (int)selected;
auto& brush = highlight ? AColor::uglyBrush : darkBrush[index];
mutator.SetBrush(BrushFromWXBrush(brush));
auto& pen = highlight ? AColor::uglyPen : darkPen[index];
Expand Down
6 changes: 4 additions & 2 deletions src/AdornedRulerPanel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1555,6 +1555,8 @@ void AdornedRulerPanel::OnAudioStartStop(AudioIOEvent evt)

void AdornedRulerPanel::OnPaint(wxPaintEvent & WXUNUSED(evt))
{
auto paintEvent = mPainter.Paint();

const auto &viewInfo = ViewInfo::Get( *GetProject() );
const auto &playRegion = viewInfo.playRegion;
const auto playRegionBounds = std::pair{
Expand Down Expand Up @@ -2159,8 +2161,8 @@ void AdornedRulerPanel::DrawBothOverlays()
wxASSERT( false );
}
else
pCellularPanel->DrawOverlays( false, mPainter );
DrawOverlays( false, mPainter );
pCellularPanel->Refresh();
Refresh();
}

void AdornedRulerPanel::UpdateButtonStates()
Expand Down
2 changes: 2 additions & 0 deletions src/FreqWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -884,6 +884,8 @@ void FrequencyPlotDialog::OnAxisChoice(wxCommandEvent & WXUNUSED(event))

void FrequencyPlotDialog::PlotPaint(wxPaintEvent & event)
{
auto paintEvent = mPainter->Paint();

if (!mBitmap)
DrawPlot();

Expand Down
2 changes: 1 addition & 1 deletion src/Printing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ bool AudacityPrintout::OnPrintPage(int WXUNUSED(page))
int screenTotalHeight =
TrackView::GetTotalHeight( *mTracks ) + rulerScreenHeight;

auto painter = CreatePainter(*dc);
auto painter = CreatePainterFromDC(*dc);

double scale = height / (double)screenTotalHeight;

Expand Down
9 changes: 7 additions & 2 deletions src/TrackPanel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -432,8 +432,11 @@ void TrackPanel::OnTimer(wxTimerEvent& )
// Notify listeners for timer ticks
window.GetPlaybackScroller().OnTimer();

DrawOverlays(false, *mPainter);
mRuler->DrawOverlays(false, *mPainter);
//DrawOverlays(false, *mPainter);
//mRuler->DrawOverlays(false, *mPainter);

Refresh();
mRuler->Refresh();

if(IsAudioActive() && gAudioIO->GetNumCaptureChannels()) {

Expand Down Expand Up @@ -479,6 +482,8 @@ void TrackPanel::OnPaint(wxPaintEvent & /* event */)
auto sw =
FrameStatistics::CreateStopwatch(FrameStatistics::SectionID::TrackPanel);

auto paint = mPainter->Paint();

DrawTracks();

auto clipStateMutator = mPainter->GetClipStateMutator();
Expand Down
2 changes: 2 additions & 0 deletions src/effects/Compressor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -638,6 +638,8 @@ EffectCompressorPanel::EffectCompressorPanel(

void EffectCompressorPanel::OnPaint(wxPaintEvent & WXUNUSED(evt))
{
auto paintEvent = mPainter->Paint();

int width, height;
GetSize(&width, &height);

Expand Down
2 changes: 2 additions & 0 deletions src/effects/Equalization.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3025,6 +3025,8 @@ void EqualizationPanel::OnPaint(wxPaintEvent & WXUNUSED(event))
int width, height;
GetSize(&width, &height);

auto paintEvent = mPainter->Paint();

Brush bkgndBrush(ColorFromWXColor(wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE)));

auto stateMutator = mPainter->GetStateMutator();
Expand Down
2 changes: 2 additions & 0 deletions src/effects/ScienFilter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -761,6 +761,8 @@ void EffectScienFilterPanel::OnPaint(wxPaintEvent & WXUNUSED(evt))
int width, height;
GetSize(&width, &height);

auto paintEvent = mPainter->Paint();

auto stateMutator = mPainter->GetStateMutator();

Brush bkgndBrush(ColorFromWXColor(wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE)));
Expand Down
7 changes: 3 additions & 4 deletions src/tracks/playabletrack/wavetrack/ui/WaveformView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -350,15 +350,14 @@ void DrawMinMaxRMS(
zoomInfo, t0 + trimLeft, t1 + trimLeft);

auto left = rect.GetLeft() + leftOffset;
auto height = rect.GetHeight();

for (auto it = range.begin(); it != range.end(); ++it)
{
const auto elementLeftOffset = it.GetLeftOffset();
const auto width = GraphicsDataCacheBase::CacheElementWidth;
const auto width = it->AvailableColumns - elementLeftOffset;

painter.DrawImage(
*it->Bitmap, left - elementLeftOffset, rect.GetTop(), width,
rect.GetHeight());
painter.DrawImage(*it->Bitmap, left, rect.GetTop(), width, height, elementLeftOffset, 0);

left += width;
}
Expand Down

0 comments on commit 3bff6d5

Please sign in to comment.