<commit_before>/*
 *   Copyright (c) 2008-2019 SLIBIO <https://github.com/SLIBIO>
 *
 *   Permission is hereby granted, free of charge, to any person obtaining a copy
 *   of this software and associated documentation files (the "Software"), to deal
 *   in the Software without restriction, including without limitation the rights
 *   to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 *   copies of the Software, and to permit persons to whom the Software is
 *   furnished to do so, subject to the following conditions:
 *
 *   The above copyright notice and this permission notice shall be included in
 *   all copies or substantial portions of the Software.
 *
 *   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 *   IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 *   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 *   AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 *   LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 *   OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 *   THE SOFTWARE.
 */

#include "slib/core/definition.h"

#if defined(SLIB_UI_IS_MACOS)

#include "slib/ui/render_view.h"

#include "slib/render/opengl.h"
#include "slib/core/thread.h"

#include "view_macos.h"

@interface SLIBGLViewHandle : SLIBViewHandle
{
	@public sl_bool m_flagRenderingContinuously;
	@public sl_bool m_flagRequestRender;
	@public sl_bool m_flagUpdate;
	@public sl_bool m_flagViewVisible;
	@public CGFloat m_viewportWidth;
	@public CGFloat m_viewportHeight;

	@public slib::Mutex m_lockRender;
	
	slib::AtomicRef<slib::Thread> m_thread;
}

-(void)queryViewStatus;

@end

namespace slib
{
	
	namespace priv
	{
		namespace render_view
		{
			
			class RenderViewInstance : public macOS_ViewInstance, public IRenderViewInstance
			{
				SLIB_DECLARE_OBJECT
				
			public:
				SLIBGLViewHandle* getHandle()
				{
					return (SLIBGLViewHandle*)m_handle;
				}
				
				void setRedrawMode(RenderView* view, RedrawMode mode) override
				{
					SLIBGLViewHandle* handle = getHandle();
					if (handle != nil) {
						handle->m_flagRenderingContinuously = mode == RedrawMode::Continuously;
					}
				}
				
				void requestRender(RenderView* view) override
				{
					SLIBGLViewHandle* handle = getHandle();
					if (handle != nil) {
						handle->m_flagRequestRender = sl_true;
					}
				}
				
			};
			
			SLIB_DEFINE_OBJECT(RenderViewInstance, ViewInstance)
			
		}
	}
	
	using namespace priv::render_view;

	Ref<ViewInstance> RenderView::createNativeWidget(ViewInstance* parent)
	{
		Ref<RenderViewInstance> ret = macOS_ViewInstance::create<RenderViewInstance, SLIBGLViewHandle>(this, parent);
		if (ret.isNotNull()) {
			ret->setRedrawMode(this, getRedrawMode());
			return ret;
		}
		return sl_null;
	}
	
	Ptr<IRenderViewInstance> RenderView::getRenderViewInstance()
	{
		return CastRef<RenderViewInstance>(getViewInstance());
	}

	namespace priv
	{
		namespace gl_view
		{
			static void RenderThreadProc(__weak SLIBGLViewHandle* _handle)
			{
				Ref<RenderEngine> engine = GL::createEngine();
				if (engine.isNull()) {
					return;
				}
				
				sl_bool flagFirstFrame = sl_true;
				
				TimeCounter timer;
				NSOpenGLContext* context = nil;
				
				NSOpenGLPixelFormatAttribute attrs[] = {
					NSOpenGLPFADoubleBuffer,
					NSOpenGLPFAColorSize, 24,
					NSOpenGLPFAAlphaSize, 8,
					NSOpenGLPFADepthSize, 24,
					NSOpenGLPFAStencilSize, 8,
					0
				};
				NSOpenGLPixelFormat* pf = [[NSOpenGLPixelFormat alloc] initWithAttributes:attrs];
				if (pf == nil) {
					return;
				}
				
				int frameNumber = 0;
				
				while (1) {
					
					sl_bool flagWorking = sl_false;
					
					Ref<Thread> thread = Thread::getCurrent();
					if (thread.isNull() || thread->isNotStopping()) {
						
						SLIBGLViewHandle* handle = _handle;
						if (handle == nil) {
							return;
						}
						Ref<macOS_ViewInstance> instance = handle->m_viewInstance;
						if (instance.isNull()) {
							return;
						}
						
						if (frameNumber % 10 == 0) {
							dispatch_async(dispatch_get_main_queue(), ^{
								[handle queryViewStatus];
							});
						}
						
						do {
							MutexLocker lock(&(handle->m_lockRender));
							if (!(handle->m_flagViewVisible)) {
								if (context != nil) {
									[context clearDrawable];
									context = nil;
									handle->m_flagUpdate = sl_true;
								}
								break;
							}
							if (context == nil) {
								context = [[NSOpenGLContext alloc] initWithFormat:pf shareContext:nil];
								if (context == nil) {
									return;
								}
								[context setView: handle];
							}
							if (handle->m_flagUpdate) {
								[context update];
								handle->m_flagUpdate = sl_false;
							}
							sl_bool flagUpdate = sl_false;
							if (handle->m_flagRenderingContinuously) {
								flagUpdate = sl_true;
							} else {
								if (handle->m_flagRequestRender) {
									flagUpdate = sl_true;
								}
							}<commit_msg>[macOS] ui/RenderView: fix crash: RenderView crashed from macOS Catalina<commit_after>/*
 *   Copyright (c) 2008-2019 SLIBIO <https://github.com/SLIBIO>
 *
 *   Permission is hereby granted, free of charge, to any person obtaining a copy
 *   of this software and associated documentation files (the "Software"), to deal
 *   in the Software without restriction, including without limitation the rights
 *   to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 *   copies of the Software, and to permit persons to whom the Software is
 *   furnished to do so, subject to the following conditions:
 *
 *   The above copyright notice and this permission notice shall be included in
 *   all copies or substantial portions of the Software.
 *
 *   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 *   IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 *   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 *   AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 *   LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 *   OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 *   THE SOFTWARE.
 */

#include "slib/core/definition.h"

#if defined(SLIB_UI_IS_MACOS)

#include "slib/ui/render_view.h"

#include "slib/render/opengl.h"
#include "slib/core/thread.h"

#include "view_macos.h"

@interface SLIBGLViewHandle : SLIBViewHandle
{
	@public sl_bool m_flagRenderingContinuously;
	@public sl_bool m_flagRequestRender;
	@public sl_bool m_flagUpdate;
	@public sl_bool m_flagViewVisible;
	@public CGFloat m_viewportWidth;
	@public CGFloat m_viewportHeight;

	@public slib::Mutex m_lockRender;
	
	slib::AtomicRef<slib::Thread> m_thread;
}

-(void)queryViewStatus;

@end

namespace slib
{
	
	namespace priv
	{
		namespace render_view
		{
			
			class RenderViewInstance : public macOS_ViewInstance, public IRenderViewInstance
			{
				SLIB_DECLARE_OBJECT
				
			public:
				SLIBGLViewHandle* getHandle()
				{
					return (SLIBGLViewHandle*)m_handle;
				}
				
				void setRedrawMode(RenderView* view, RedrawMode mode) override
				{
					SLIBGLViewHandle* handle = getHandle();
					if (handle != nil) {
						handle->m_flagRenderingContinuously = mode == RedrawMode::Continuously;
					}
				}
				
				void requestRender(RenderView* view) override
				{
					SLIBGLViewHandle* handle = getHandle();
					if (handle != nil) {
						handle->m_flagRequestRender = sl_true;
					}
				}
				
			};
			
			SLIB_DEFINE_OBJECT(RenderViewInstance, ViewInstance)
			
		}
	}
	
	using namespace priv::render_view;

	Ref<ViewInstance> RenderView::createNativeWidget(ViewInstance* parent)
	{
		Ref<RenderViewInstance> ret = macOS_ViewInstance::create<RenderViewInstance, SLIBGLViewHandle>(this, parent);
		if (ret.isNotNull()) {
			ret->setRedrawMode(this, getRedrawMode());
			return ret;
		}
		return sl_null;
	}
	
	Ptr<IRenderViewInstance> RenderView::getRenderViewInstance()
	{
		return CastRef<RenderViewInstance>(getViewInstance());
	}

	namespace priv
	{
		namespace gl_view
		{
			static void RenderThreadProc(__weak SLIBGLViewHandle* _handle)
			{
				Ref<RenderEngine> engine = GL::createEngine();
				if (engine.isNull()) {
					return;
				}
				
				sl_bool flagFirstFrame = sl_true;
				
				TimeCounter timer;
				NSOpenGLContext* context = nil;
				
				NSOpenGLPixelFormatAttribute attrs[] = {
					NSOpenGLPFADoubleBuffer,
					NSOpenGLPFAColorSize, 24,
					NSOpenGLPFAAlphaSize, 8,
					NSOpenGLPFADepthSize, 24,
					NSOpenGLPFAStencilSize, 8,
					0
				};
				NSOpenGLPixelFormat* pf = [[NSOpenGLPixelFormat alloc] initWithAttributes:attrs];
				if (pf == nil) {
					return;
				}
				
				int frameNumber = 0;
				
				while (1) {
					
					sl_bool flagWorking = sl_false;
					
					Ref<Thread> thread = Thread::getCurrent();
					if (thread.isNull() || thread->isNotStopping()) {
						
						SLIBGLViewHandle* handle = _handle;
						if (handle == nil) {
							return;
						}
						Ref<macOS_ViewInstance> instance = handle->m_viewInstance;
						if (instance.isNull()) {
							return;
						}
						
						if (frameNumber % 10 == 0) {
							dispatch_async(dispatch_get_main_queue(), ^{
								[handle queryViewStatus];
							});
						}
						
						do {
							MutexLocker lock(&(handle->m_lockRender));
							if (!(handle->m_flagViewVisible)) {
								if (context != nil) {
									[context clearDrawable];
									context = nil;
									handle->m_flagUpdate = sl_true;
								}
								break;
							}
							if (context == nil) {
								context = [[NSOpenGLContext alloc] initWithFormat:pf shareContext:nil];
								if (context == nil) {
									return;
								}
								dispatch_async(dispatch_get_main_queue(), ^{
									[context setView: handle];
								});
							}
							if (handle->m_flagUpdate) {
								[context update];
								handle->m_flagUpdate = sl_false;
							}
							sl_bool flagUpdate = sl_false;
							if (handle->m_flagRenderingContinuously) {
								flagUpdate = sl_true;
							} else {
								if (handle->m_flagRequestRender) {
									flagUpdate = sl_true;
								}
							}
{'fraction_non_alphanumeric': 0.0938137321549966, 'fraction_numerical': 0.0038846265902690103, 'mean_word_length': 2.942572741194487, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 47, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4018983', 'n_tokens_mistral': 4277, 'n_tokens_neox': 3136, 'n_words': 817}