Skip to content

Commit

Permalink
Merge pull request #1097 from iPlug2/graphics/gl-context
Browse files Browse the repository at this point in the history
Activate and Deactivate GL context
  • Loading branch information
olilarkin committed May 20, 2024
2 parents 34b7101 + b189eb3 commit 6c639b9
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 3 deletions.
4 changes: 4 additions & 0 deletions IGraphics/Drawing/IGraphicsNanoVG.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -480,6 +480,8 @@ void IGraphicsNanoVG::OnViewDestroyed()

void IGraphicsNanoVG::DrawResize()
{
ActivateGLContext();

if (mMainFrameBuffer != nullptr)
nvgDeleteFramebuffer(mMainFrameBuffer);

Expand All @@ -490,6 +492,8 @@ void IGraphicsNanoVG::DrawResize()
if (mMainFrameBuffer == nullptr)
DBGMSG("Could not init FBO.\n");
}

DeactivateGLContext();
}

void IGraphicsNanoVG::BeginFrame()
Expand Down
6 changes: 3 additions & 3 deletions IGraphics/IGraphics.h
Original file line number Diff line number Diff line change
Expand Up @@ -971,10 +971,10 @@ class IGraphics
virtual const char* GetAppGroupID() const { return ""; }

protected:
/* Implemented on Windows to store previously active GLContext and HDC for restoring, calls GetDC */
virtual void ActivateGLContext() {};
/* Activate the context for the view (GL only) */
virtual void ActivateGLContext() {};

/* Implemented on Windows to restore previous GL context calls ReleaseDC */
/* Deactivate the context for the view (GL only) */
virtual void DeactivateGLContext() {};

/** Creates a platform native text entry field.
Expand Down
4 changes: 4 additions & 0 deletions IGraphics/Platforms/IGraphicsMac.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ class IGraphicsMac final : public IGRAPHICS_DRAW_CLASS

IPopupMenu* CreatePlatformPopupMenu(IPopupMenu& menu, const IRECT bounds, bool& isAsync) override;
void CreatePlatformTextEntry(int paramIdx, const IText& text, const IRECT& bounds, int length, const char* str) override;

void ActivateGLContext() override;
void DeactivateGLContext() override;

private:
void PointToScreen(float& x, float& y) const;
void ScreenToPoint(float& x, float& y) const;
Expand Down
15 changes: 15 additions & 0 deletions IGraphics/Platforms/IGraphicsMac.mm
Original file line number Diff line number Diff line change
Expand Up @@ -725,6 +725,21 @@ static int GetSystemVersion()
return EUIAppearance::Light;
}

void IGraphicsMac::ActivateGLContext()
{
#ifdef IGRAPHICS_GL
IGRAPHICS_VIEW* pView = (IGRAPHICS_VIEW*) mView;
[[pView openGLContext] makeCurrentContext];
#endif
}

void IGraphicsMac::DeactivateGLContext()
{
#ifdef IGRAPHICS_GL
[NSOpenGLContext clearCurrentContext];
#endif
}

#if defined IGRAPHICS_NANOVG
#include "IGraphicsNanoVG.cpp"
#elif defined IGRAPHICS_SKIA
Expand Down
13 changes: 13 additions & 0 deletions IGraphics/Platforms/IGraphicsMac_view.mm
Original file line number Diff line number Diff line change
Expand Up @@ -580,6 +580,12 @@ - (void) viewDidMoveToWindow
name:NSViewFrameDidChangeNotification
object:self];
#endif
#ifdef IGRAPHICS_GL
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(frameDidChange:)
name:NSViewGlobalFrameDidChangeNotification
object:self];
#endif

// [[NSNotificationCenter defaultCenter] addObserver:self
// selector:@selector(windowResized:) name:NSWindowDidEndLiveResizeNotification
Expand Down Expand Up @@ -670,6 +676,7 @@ - (void) render
#endif
#ifdef IGRAPHICS_GL
[[self openGLContext] flushBuffer];
[NSOpenGLContext clearCurrentContext];
#endif
}
}
Expand Down Expand Up @@ -1299,6 +1306,12 @@ - (void) frameDidChange:(NSNotification*) pNotification
self.frame.size.height * scale)];
}
#endif
#ifdef IGRAPHICS_GL
- (void) frameDidChange:(NSNotification*) pNotification
{
[[self openGLContext] makeCurrentContext];
}
#endif

//- (void) windowResized: (NSNotification*) notification;
//{
Expand Down

0 comments on commit 6c639b9

Please sign in to comment.