The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1444 lines
44KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2020 - Raw Material Software Limited
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. By using JUCE, you agree to the terms of both the JUCE 6 End-User License
  8. Agreement and JUCE Privacy Policy (both effective as of the 16th June 2020).
  9. End User License Agreement: www.juce.com/juce-6-licence
  10. Privacy Policy: www.juce.com/juce-privacy-policy
  11. Or: You may also use this code under the terms of the GPL v3 (see
  12. www.gnu.org/licenses).
  13. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  14. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  15. DISCLAIMED.
  16. ==============================================================================
  17. */
  18. namespace juce
  19. {
  20. #if JUCE_IOS
  21. struct AppInactivityCallback // NB: this is a duplicate of an internal declaration in juce_core
  22. {
  23. virtual ~AppInactivityCallback() {}
  24. virtual void appBecomingInactive() = 0;
  25. };
  26. extern Array<AppInactivityCallback*> appBecomingInactiveCallbacks;
  27. // On iOS, all GL calls will crash when the app is running in the background, so
  28. // this prevents them from happening (which some messy locking behaviour)
  29. struct iOSBackgroundProcessCheck : public AppInactivityCallback
  30. {
  31. iOSBackgroundProcessCheck() { isBackgroundProcess(); appBecomingInactiveCallbacks.add (this); }
  32. ~iOSBackgroundProcessCheck() override { appBecomingInactiveCallbacks.removeAllInstancesOf (this); }
  33. bool isBackgroundProcess()
  34. {
  35. const bool b = Process::isForegroundProcess();
  36. isForeground.set (b ? 1 : 0);
  37. return ! b;
  38. }
  39. void appBecomingInactive() override
  40. {
  41. int counter = 2000;
  42. while (--counter > 0 && isForeground.get() != 0)
  43. Thread::sleep (1);
  44. }
  45. private:
  46. Atomic<int> isForeground;
  47. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (iOSBackgroundProcessCheck)
  48. };
  49. #endif
  50. #if JUCE_WINDOWS && JUCE_WIN_PER_MONITOR_DPI_AWARE
  51. extern JUCE_API double getScaleFactorForWindow (HWND);
  52. #endif
  53. static bool contextHasTextureNpotFeature()
  54. {
  55. if (getOpenGLVersion() >= Version (2))
  56. return true;
  57. // If the version is < 2, we can't use the newer extension-checking API
  58. // so we have to use glGetString
  59. const auto* extensionsBegin = glGetString (GL_EXTENSIONS);
  60. if (extensionsBegin == nullptr)
  61. return false;
  62. const auto* extensionsEnd = findNullTerminator (extensionsBegin);
  63. const std::string extensionsString (extensionsBegin, extensionsEnd);
  64. const auto stringTokens = StringArray::fromTokens (extensionsString.c_str(), false);
  65. return stringTokens.contains ("GL_ARB_texture_non_power_of_two");
  66. }
  67. //==============================================================================
  68. class OpenGLContext::CachedImage : public CachedComponentImage,
  69. private ThreadPoolJob
  70. {
  71. public:
  72. CachedImage (OpenGLContext& c, Component& comp,
  73. const OpenGLPixelFormat& pixFormat, void* contextToShare)
  74. : ThreadPoolJob ("OpenGL Rendering"),
  75. context (c),
  76. component (comp)
  77. {
  78. nativeContext.reset (new NativeContext (component, pixFormat, contextToShare,
  79. c.useMultisampling, c.versionRequired));
  80. if (nativeContext->createdOk())
  81. context.nativeContext = nativeContext.get();
  82. else
  83. nativeContext.reset();
  84. }
  85. ~CachedImage() override
  86. {
  87. stop();
  88. }
  89. //==============================================================================
  90. void start()
  91. {
  92. if (nativeContext != nullptr)
  93. {
  94. #if JUCE_MAC
  95. cvDisplayLinkWrapper = std::make_unique<CVDisplayLinkWrapper> (*this);
  96. cvDisplayLinkWrapper->updateActiveDisplay();
  97. #endif
  98. renderThread = std::make_unique<ThreadPool> (1);
  99. resume();
  100. }
  101. }
  102. void stop()
  103. {
  104. if (renderThread != nullptr)
  105. {
  106. // make sure everything has finished executing
  107. destroying = true;
  108. if (workQueue.size() > 0)
  109. {
  110. if (! renderThread->contains (this))
  111. resume();
  112. while (workQueue.size() != 0)
  113. Thread::sleep (20);
  114. }
  115. pause();
  116. renderThread.reset();
  117. }
  118. #if JUCE_MAC
  119. cvDisplayLinkWrapper = nullptr;
  120. #endif
  121. hasInitialised = false;
  122. }
  123. //==============================================================================
  124. void pause()
  125. {
  126. signalJobShouldExit();
  127. messageManagerLock.abort();
  128. if (renderThread != nullptr)
  129. {
  130. repaintEvent.signal();
  131. renderThread->removeJob (this, true, -1);
  132. }
  133. }
  134. void resume()
  135. {
  136. if (renderThread != nullptr)
  137. renderThread->addJob (this, false);
  138. }
  139. //==============================================================================
  140. void paint (Graphics&) override
  141. {
  142. updateViewportSize (false);
  143. }
  144. bool invalidateAll() override
  145. {
  146. validArea.clear();
  147. triggerRepaint();
  148. return false;
  149. }
  150. bool invalidate (const Rectangle<int>& area) override
  151. {
  152. validArea.subtract (area.toFloat().transformedBy (transform).getSmallestIntegerContainer());
  153. triggerRepaint();
  154. return false;
  155. }
  156. void releaseResources() override
  157. {
  158. stop();
  159. }
  160. void triggerRepaint()
  161. {
  162. needsUpdate = 1;
  163. repaintEvent.signal();
  164. }
  165. //==============================================================================
  166. bool ensureFrameBufferSize()
  167. {
  168. auto fbW = cachedImageFrameBuffer.getWidth();
  169. auto fbH = cachedImageFrameBuffer.getHeight();
  170. if (fbW != viewportArea.getWidth() || fbH != viewportArea.getHeight() || ! cachedImageFrameBuffer.isValid())
  171. {
  172. if (! cachedImageFrameBuffer.initialise (context, viewportArea.getWidth(), viewportArea.getHeight()))
  173. return false;
  174. validArea.clear();
  175. JUCE_CHECK_OPENGL_ERROR
  176. }
  177. return true;
  178. }
  179. void clearRegionInFrameBuffer (const RectangleList<int>& list)
  180. {
  181. glClearColor (0, 0, 0, 0);
  182. glEnable (GL_SCISSOR_TEST);
  183. auto previousFrameBufferTarget = OpenGLFrameBuffer::getCurrentFrameBufferTarget();
  184. cachedImageFrameBuffer.makeCurrentRenderingTarget();
  185. auto imageH = cachedImageFrameBuffer.getHeight();
  186. for (auto& r : list)
  187. {
  188. glScissor (r.getX(), imageH - r.getBottom(), r.getWidth(), r.getHeight());
  189. glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
  190. }
  191. glDisable (GL_SCISSOR_TEST);
  192. context.extensions.glBindFramebuffer (GL_FRAMEBUFFER, previousFrameBufferTarget);
  193. JUCE_CHECK_OPENGL_ERROR
  194. }
  195. bool renderFrame()
  196. {
  197. MessageManager::Lock::ScopedTryLockType mmLock (messageManagerLock, false);
  198. auto isUpdatingTestValue = true;
  199. auto isUpdating = needsUpdate.compare_exchange_strong (isUpdatingTestValue, false);
  200. if (context.renderComponents && isUpdating)
  201. {
  202. // This avoids hogging the message thread when doing intensive rendering.
  203. if (lastMMLockReleaseTime + 1 >= Time::getMillisecondCounter())
  204. Thread::sleep (2);
  205. while (! shouldExit())
  206. {
  207. doWorkWhileWaitingForLock (false);
  208. if (mmLock.retryLock())
  209. break;
  210. }
  211. if (shouldExit())
  212. return false;
  213. }
  214. if (! context.makeActive())
  215. return false;
  216. NativeContext::Locker locker (*nativeContext);
  217. JUCE_CHECK_OPENGL_ERROR
  218. doWorkWhileWaitingForLock (true);
  219. if (context.renderer != nullptr)
  220. {
  221. glViewport (0, 0, viewportArea.getWidth(), viewportArea.getHeight());
  222. context.currentRenderScale = scale;
  223. context.renderer->renderOpenGL();
  224. clearGLError();
  225. bindVertexArray();
  226. }
  227. if (context.renderComponents)
  228. {
  229. if (isUpdating)
  230. {
  231. paintComponent();
  232. if (! hasInitialised)
  233. return false;
  234. messageManagerLock.exit();
  235. lastMMLockReleaseTime = Time::getMillisecondCounter();
  236. }
  237. glViewport (0, 0, viewportArea.getWidth(), viewportArea.getHeight());
  238. drawComponentBuffer();
  239. }
  240. context.swapBuffers();
  241. OpenGLContext::deactivateCurrentContext();
  242. return true;
  243. }
  244. void updateViewportSize (bool canTriggerUpdate)
  245. {
  246. JUCE_ASSERT_MESSAGE_THREAD
  247. if (auto* peer = component.getPeer())
  248. {
  249. #if JUCE_MAC
  250. const auto displayScale = [this]
  251. {
  252. if (auto* wrapper = cvDisplayLinkWrapper.get())
  253. if (wrapper->updateActiveDisplay())
  254. nativeContext->setNominalVideoRefreshPeriodS (wrapper->getNominalVideoRefreshPeriodS());
  255. if (auto* view = getCurrentView())
  256. {
  257. if ([view respondsToSelector: @selector (backingScaleFactor)])
  258. return [(id) view backingScaleFactor];
  259. if (auto* window = [view window])
  260. return [window backingScaleFactor];
  261. }
  262. return scale;
  263. }();
  264. #else
  265. const auto displayScale = Desktop::getInstance().getDisplays().getDisplayForRect (component.getTopLevelComponent()->getScreenBounds())->scale;
  266. #endif
  267. auto localBounds = component.getLocalBounds();
  268. auto newArea = peer->getComponent().getLocalArea (&component, localBounds).withZeroOrigin() * displayScale;
  269. #if JUCE_WINDOWS && JUCE_WIN_PER_MONITOR_DPI_AWARE
  270. auto newScale = getScaleFactorForWindow (nativeContext->getNativeHandle());
  271. auto desktopScale = Desktop::getInstance().getGlobalScaleFactor();
  272. if (! approximatelyEqual (1.0f, desktopScale))
  273. newScale *= desktopScale;
  274. #else
  275. auto newScale = displayScale;
  276. #endif
  277. if (scale != newScale || viewportArea != newArea)
  278. {
  279. scale = newScale;
  280. viewportArea = newArea;
  281. transform = AffineTransform::scale ((float) newArea.getWidth() / (float) localBounds.getWidth(),
  282. (float) newArea.getHeight() / (float) localBounds.getHeight());
  283. nativeContext->updateWindowPosition (peer->getAreaCoveredBy (component));
  284. if (canTriggerUpdate)
  285. invalidateAll();
  286. }
  287. }
  288. }
  289. void bindVertexArray() noexcept
  290. {
  291. if (openGLVersion.major >= 3)
  292. if (vertexArrayObject != 0)
  293. context.extensions.glBindVertexArray (vertexArrayObject);
  294. }
  295. void checkViewportBounds()
  296. {
  297. auto screenBounds = component.getTopLevelComponent()->getScreenBounds();
  298. if (lastScreenBounds != screenBounds)
  299. {
  300. updateViewportSize (true);
  301. lastScreenBounds = screenBounds;
  302. }
  303. }
  304. void paintComponent()
  305. {
  306. // you mustn't set your own cached image object when attaching a GL context!
  307. jassert (get (component) == this);
  308. if (! ensureFrameBufferSize())
  309. return;
  310. RectangleList<int> invalid (viewportArea);
  311. invalid.subtract (validArea);
  312. validArea = viewportArea;
  313. if (! invalid.isEmpty())
  314. {
  315. clearRegionInFrameBuffer (invalid);
  316. {
  317. std::unique_ptr<LowLevelGraphicsContext> g (createOpenGLGraphicsContext (context, cachedImageFrameBuffer));
  318. g->clipToRectangleList (invalid);
  319. g->addTransform (transform);
  320. paintOwner (*g);
  321. JUCE_CHECK_OPENGL_ERROR
  322. }
  323. if (! context.isActive())
  324. context.makeActive();
  325. }
  326. JUCE_CHECK_OPENGL_ERROR
  327. }
  328. void drawComponentBuffer()
  329. {
  330. #if ! JUCE_ANDROID
  331. glEnable (GL_TEXTURE_2D);
  332. clearGLError();
  333. #endif
  334. #if JUCE_WINDOWS
  335. // some stupidly old drivers are missing this function, so try to at least avoid a crash here,
  336. // but if you hit this assertion you may want to have your own version check before using the
  337. // component rendering stuff on such old drivers.
  338. jassert (context.extensions.glActiveTexture != nullptr);
  339. if (context.extensions.glActiveTexture != nullptr)
  340. #endif
  341. context.extensions.glActiveTexture (GL_TEXTURE0);
  342. glBindTexture (GL_TEXTURE_2D, cachedImageFrameBuffer.getTextureID());
  343. bindVertexArray();
  344. const Rectangle<int> cacheBounds (cachedImageFrameBuffer.getWidth(), cachedImageFrameBuffer.getHeight());
  345. context.copyTexture (cacheBounds, cacheBounds, cacheBounds.getWidth(), cacheBounds.getHeight(), false);
  346. glBindTexture (GL_TEXTURE_2D, 0);
  347. JUCE_CHECK_OPENGL_ERROR
  348. }
  349. void paintOwner (LowLevelGraphicsContext& llgc)
  350. {
  351. Graphics g (llgc);
  352. #if JUCE_ENABLE_REPAINT_DEBUGGING
  353. #ifdef JUCE_IS_REPAINT_DEBUGGING_ACTIVE
  354. if (JUCE_IS_REPAINT_DEBUGGING_ACTIVE)
  355. #endif
  356. {
  357. g.saveState();
  358. }
  359. #endif
  360. JUCE_TRY
  361. {
  362. component.paintEntireComponent (g, false);
  363. }
  364. JUCE_CATCH_EXCEPTION
  365. #if JUCE_ENABLE_REPAINT_DEBUGGING
  366. #ifdef JUCE_IS_REPAINT_DEBUGGING_ACTIVE
  367. if (JUCE_IS_REPAINT_DEBUGGING_ACTIVE)
  368. #endif
  369. {
  370. // enabling this code will fill all areas that get repainted with a colour overlay, to show
  371. // clearly when things are being repainted.
  372. g.restoreState();
  373. static Random rng;
  374. g.fillAll (Colour ((uint8) rng.nextInt (255),
  375. (uint8) rng.nextInt (255),
  376. (uint8) rng.nextInt (255),
  377. (uint8) 0x50));
  378. }
  379. #endif
  380. }
  381. void handleResize()
  382. {
  383. updateViewportSize (true);
  384. #if JUCE_MAC
  385. if (hasInitialised)
  386. {
  387. [nativeContext->view update];
  388. renderFrame();
  389. }
  390. #endif
  391. }
  392. //==============================================================================
  393. JobStatus runJob() override
  394. {
  395. {
  396. // Allow the message thread to finish setting-up the context before using it.
  397. MessageManager::Lock::ScopedTryLockType mmLock (messageManagerLock, false);
  398. do
  399. {
  400. if (shouldExit())
  401. return ThreadPoolJob::jobHasFinished;
  402. } while (! mmLock.retryLock());
  403. }
  404. if (! initialiseOnThread())
  405. {
  406. hasInitialised = false;
  407. return ThreadPoolJob::jobHasFinished;
  408. }
  409. hasInitialised = true;
  410. while (! shouldExit())
  411. {
  412. #if JUCE_IOS
  413. if (backgroundProcessCheck.isBackgroundProcess())
  414. {
  415. repaintEvent.wait (300);
  416. repaintEvent.reset();
  417. continue;
  418. }
  419. #endif
  420. if (shouldExit())
  421. break;
  422. #if JUCE_MAC
  423. if (context.continuousRepaint)
  424. {
  425. repaintEvent.wait (-1);
  426. renderFrame();
  427. }
  428. else
  429. #endif
  430. if (! renderFrame())
  431. repaintEvent.wait (5); // failed to render, so avoid a tight fail-loop.
  432. else if (! context.continuousRepaint && ! shouldExit())
  433. repaintEvent.wait (-1);
  434. repaintEvent.reset();
  435. }
  436. hasInitialised = false;
  437. context.makeActive();
  438. shutdownOnThread();
  439. OpenGLContext::deactivateCurrentContext();
  440. return ThreadPoolJob::jobHasFinished;
  441. }
  442. bool initialiseOnThread()
  443. {
  444. // On android, this can get called twice, so drop any previous state.
  445. associatedObjectNames.clear();
  446. associatedObjects.clear();
  447. cachedImageFrameBuffer.release();
  448. context.makeActive();
  449. if (! nativeContext->initialiseOnRenderThread (context))
  450. return false;
  451. #if JUCE_ANDROID
  452. // On android the context may be created in initialiseOnRenderThread
  453. // and we therefore need to call makeActive again
  454. context.makeActive();
  455. #endif
  456. gl::loadFunctions();
  457. openGLVersion = getOpenGLVersion();
  458. if (openGLVersion.major >= 3)
  459. {
  460. context.extensions.glGenVertexArrays (1, &vertexArrayObject);
  461. bindVertexArray();
  462. }
  463. glViewport (0, 0, viewportArea.getWidth(), viewportArea.getHeight());
  464. nativeContext->setSwapInterval (1);
  465. #if ! JUCE_OPENGL_ES
  466. JUCE_CHECK_OPENGL_ERROR
  467. shadersAvailable = OpenGLShaderProgram::getLanguageVersion() > 0;
  468. clearGLError();
  469. #endif
  470. textureNpotSupported = contextHasTextureNpotFeature();
  471. if (context.renderer != nullptr)
  472. context.renderer->newOpenGLContextCreated();
  473. #if JUCE_MAC
  474. jassert (cvDisplayLinkWrapper != nullptr);
  475. nativeContext->setNominalVideoRefreshPeriodS (cvDisplayLinkWrapper->getNominalVideoRefreshPeriodS());
  476. #endif
  477. return true;
  478. }
  479. void shutdownOnThread()
  480. {
  481. if (context.renderer != nullptr)
  482. context.renderer->openGLContextClosing();
  483. if (vertexArrayObject != 0)
  484. context.extensions.glDeleteVertexArrays (1, &vertexArrayObject);
  485. associatedObjectNames.clear();
  486. associatedObjects.clear();
  487. cachedImageFrameBuffer.release();
  488. nativeContext->shutdownOnRenderThread();
  489. }
  490. //==============================================================================
  491. struct BlockingWorker : public OpenGLContext::AsyncWorker
  492. {
  493. BlockingWorker (OpenGLContext::AsyncWorker::Ptr && workerToUse)
  494. : originalWorker (std::move (workerToUse))
  495. {}
  496. void operator() (OpenGLContext& calleeContext)
  497. {
  498. if (originalWorker != nullptr)
  499. (*originalWorker) (calleeContext);
  500. finishedSignal.signal();
  501. }
  502. void block() noexcept { finishedSignal.wait(); }
  503. OpenGLContext::AsyncWorker::Ptr originalWorker;
  504. WaitableEvent finishedSignal;
  505. };
  506. bool doWorkWhileWaitingForLock (bool contextIsAlreadyActive)
  507. {
  508. bool contextActivated = false;
  509. for (OpenGLContext::AsyncWorker::Ptr work = workQueue.removeAndReturn (0);
  510. work != nullptr && (! shouldExit()); work = workQueue.removeAndReturn (0))
  511. {
  512. if ((! contextActivated) && (! contextIsAlreadyActive))
  513. {
  514. if (! context.makeActive())
  515. break;
  516. contextActivated = true;
  517. }
  518. NativeContext::Locker locker (*nativeContext);
  519. (*work) (context);
  520. clearGLError();
  521. }
  522. if (contextActivated)
  523. OpenGLContext::deactivateCurrentContext();
  524. return shouldExit();
  525. }
  526. void execute (OpenGLContext::AsyncWorker::Ptr workerToUse, bool shouldBlock, bool calledFromDestructor = false)
  527. {
  528. if (calledFromDestructor || ! destroying)
  529. {
  530. if (shouldBlock)
  531. {
  532. auto blocker = new BlockingWorker (std::move (workerToUse));
  533. OpenGLContext::AsyncWorker::Ptr worker (*blocker);
  534. workQueue.add (worker);
  535. messageManagerLock.abort();
  536. context.triggerRepaint();
  537. blocker->block();
  538. }
  539. else
  540. {
  541. workQueue.add (std::move (workerToUse));
  542. messageManagerLock.abort();
  543. context.triggerRepaint();
  544. }
  545. }
  546. else
  547. {
  548. jassertfalse; // you called execute AFTER you detached your OpenGLContext
  549. }
  550. }
  551. //==============================================================================
  552. static CachedImage* get (Component& c) noexcept
  553. {
  554. return dynamic_cast<CachedImage*> (c.getCachedComponentImage());
  555. }
  556. //==============================================================================
  557. friend class NativeContext;
  558. std::unique_ptr<NativeContext> nativeContext;
  559. OpenGLContext& context;
  560. Component& component;
  561. Version openGLVersion;
  562. OpenGLFrameBuffer cachedImageFrameBuffer;
  563. RectangleList<int> validArea;
  564. Rectangle<int> viewportArea, lastScreenBounds;
  565. double scale = 1.0;
  566. AffineTransform transform;
  567. GLuint vertexArrayObject = 0;
  568. StringArray associatedObjectNames;
  569. ReferenceCountedArray<ReferenceCountedObject> associatedObjects;
  570. WaitableEvent canPaintNowFlag, finishedPaintingFlag, repaintEvent { true };
  571. #if JUCE_OPENGL_ES
  572. bool shadersAvailable = true;
  573. #else
  574. bool shadersAvailable = false;
  575. #endif
  576. bool textureNpotSupported = false;
  577. std::atomic<bool> hasInitialised { false }, needsUpdate { true }, destroying { false };
  578. uint32 lastMMLockReleaseTime = 0;
  579. #if JUCE_MAC
  580. NSView* getCurrentView() const
  581. {
  582. if (auto* peer = component.getPeer())
  583. return static_cast<NSView*> (peer->getNativeHandle());
  584. return nullptr;
  585. }
  586. NSScreen* getCurrentScreen() const
  587. {
  588. JUCE_ASSERT_MESSAGE_THREAD;
  589. if (auto* view = getCurrentView())
  590. if (auto* window = [view window])
  591. return [window screen];
  592. return nullptr;
  593. }
  594. struct CVDisplayLinkWrapper
  595. {
  596. explicit CVDisplayLinkWrapper (CachedImage& cachedImageIn)
  597. : cachedImage (cachedImageIn),
  598. continuousRepaint (cachedImageIn.context.continuousRepaint.load())
  599. {
  600. CVDisplayLinkCreateWithActiveCGDisplays (&displayLink);
  601. CVDisplayLinkSetOutputCallback (displayLink, &displayLinkCallback, this);
  602. CVDisplayLinkStart (displayLink);
  603. }
  604. double getNominalVideoRefreshPeriodS() const
  605. {
  606. const auto nominalVideoRefreshPeriod = CVDisplayLinkGetNominalOutputVideoRefreshPeriod (displayLink);
  607. if ((nominalVideoRefreshPeriod.flags & kCVTimeIsIndefinite) == 0)
  608. return (double) nominalVideoRefreshPeriod.timeValue / (double) nominalVideoRefreshPeriod.timeScale;
  609. return 0.0;
  610. }
  611. /* Returns true if updated, or false otherwise. */
  612. bool updateActiveDisplay()
  613. {
  614. auto* oldScreen = std::exchange (currentScreen, cachedImage.getCurrentScreen());
  615. if (oldScreen == currentScreen)
  616. return false;
  617. for (NSScreen* screen in [NSScreen screens])
  618. if (screen == currentScreen)
  619. if (NSNumber* number = [[screen deviceDescription] objectForKey: @"NSScreenNumber"])
  620. CVDisplayLinkSetCurrentCGDisplay (displayLink, [number unsignedIntValue]);
  621. return true;
  622. }
  623. ~CVDisplayLinkWrapper()
  624. {
  625. CVDisplayLinkStop (displayLink);
  626. CVDisplayLinkRelease (displayLink);
  627. }
  628. static CVReturn displayLinkCallback (CVDisplayLinkRef, const CVTimeStamp*, const CVTimeStamp*,
  629. CVOptionFlags, CVOptionFlags*, void* displayLinkContext)
  630. {
  631. auto* self = reinterpret_cast<CVDisplayLinkWrapper*> (displayLinkContext);
  632. if (self->continuousRepaint)
  633. self->cachedImage.repaintEvent.signal();
  634. return kCVReturnSuccess;
  635. }
  636. CachedImage& cachedImage;
  637. const bool continuousRepaint;
  638. CVDisplayLinkRef displayLink;
  639. NSScreen* currentScreen = nullptr;
  640. };
  641. std::unique_ptr<CVDisplayLinkWrapper> cvDisplayLinkWrapper;
  642. #endif
  643. std::unique_ptr<ThreadPool> renderThread;
  644. ReferenceCountedArray<OpenGLContext::AsyncWorker, CriticalSection> workQueue;
  645. MessageManager::Lock messageManagerLock;
  646. #if JUCE_IOS
  647. iOSBackgroundProcessCheck backgroundProcessCheck;
  648. #endif
  649. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (CachedImage)
  650. };
  651. //==============================================================================
  652. class OpenGLContext::Attachment : public ComponentMovementWatcher,
  653. private Timer
  654. {
  655. public:
  656. Attachment (OpenGLContext& c, Component& comp)
  657. : ComponentMovementWatcher (&comp), context (c)
  658. {
  659. if (canBeAttached (comp))
  660. attach();
  661. }
  662. ~Attachment() override
  663. {
  664. detach();
  665. }
  666. void detach()
  667. {
  668. auto& comp = *getComponent();
  669. stop();
  670. comp.setCachedComponentImage (nullptr);
  671. context.nativeContext = nullptr;
  672. }
  673. void componentMovedOrResized (bool /*wasMoved*/, bool /*wasResized*/) override
  674. {
  675. auto& comp = *getComponent();
  676. if (isAttached (comp) != canBeAttached (comp))
  677. componentVisibilityChanged();
  678. if (comp.getWidth() > 0 && comp.getHeight() > 0
  679. && context.nativeContext != nullptr)
  680. {
  681. if (auto* c = CachedImage::get (comp))
  682. c->handleResize();
  683. if (auto* peer = comp.getTopLevelComponent()->getPeer())
  684. context.nativeContext->updateWindowPosition (peer->getAreaCoveredBy (comp));
  685. }
  686. }
  687. using ComponentMovementWatcher::componentMovedOrResized;
  688. void componentPeerChanged() override
  689. {
  690. detach();
  691. componentVisibilityChanged();
  692. }
  693. void componentVisibilityChanged() override
  694. {
  695. auto& comp = *getComponent();
  696. if (canBeAttached (comp))
  697. {
  698. if (isAttached (comp))
  699. comp.repaint(); // (needed when windows are un-minimised)
  700. else
  701. attach();
  702. }
  703. else
  704. {
  705. detach();
  706. }
  707. }
  708. using ComponentMovementWatcher::componentVisibilityChanged;
  709. #if JUCE_DEBUG || JUCE_LOG_ASSERTIONS
  710. void componentBeingDeleted (Component& c) override
  711. {
  712. /* You must call detach() or delete your OpenGLContext to remove it
  713. from a component BEFORE deleting the component that it is using!
  714. */
  715. jassertfalse;
  716. ComponentMovementWatcher::componentBeingDeleted (c);
  717. }
  718. #endif
  719. void update()
  720. {
  721. auto& comp = *getComponent();
  722. if (canBeAttached (comp))
  723. start();
  724. else
  725. stop();
  726. }
  727. private:
  728. OpenGLContext& context;
  729. bool canBeAttached (const Component& comp) noexcept
  730. {
  731. return (! context.overrideCanAttach) && comp.getWidth() > 0 && comp.getHeight() > 0 && isShowingOrMinimised (comp);
  732. }
  733. static bool isShowingOrMinimised (const Component& c)
  734. {
  735. if (! c.isVisible())
  736. return false;
  737. if (auto* p = c.getParentComponent())
  738. return isShowingOrMinimised (*p);
  739. return c.getPeer() != nullptr;
  740. }
  741. static bool isAttached (const Component& comp) noexcept
  742. {
  743. return comp.getCachedComponentImage() != nullptr;
  744. }
  745. void attach()
  746. {
  747. auto& comp = *getComponent();
  748. auto* newCachedImage = new CachedImage (context, comp,
  749. context.openGLPixelFormat,
  750. context.contextToShareWith);
  751. comp.setCachedComponentImage (newCachedImage);
  752. start();
  753. }
  754. void stop()
  755. {
  756. stopTimer();
  757. auto& comp = *getComponent();
  758. #if JUCE_MAC
  759. [[(NSView*) comp.getWindowHandle() window] disableScreenUpdatesUntilFlush];
  760. #endif
  761. if (auto* oldCachedImage = CachedImage::get (comp))
  762. oldCachedImage->stop(); // (must stop this before detaching it from the component)
  763. }
  764. void start()
  765. {
  766. auto& comp = *getComponent();
  767. if (auto* cachedImage = CachedImage::get (comp))
  768. {
  769. cachedImage->start(); // (must wait until this is attached before starting its thread)
  770. cachedImage->updateViewportSize (true);
  771. startTimer (400);
  772. }
  773. }
  774. void timerCallback() override
  775. {
  776. if (auto* cachedImage = CachedImage::get (*getComponent()))
  777. cachedImage->checkViewportBounds();
  778. }
  779. };
  780. //==============================================================================
  781. OpenGLContext::OpenGLContext()
  782. {
  783. }
  784. OpenGLContext::~OpenGLContext()
  785. {
  786. detach();
  787. }
  788. void OpenGLContext::setRenderer (OpenGLRenderer* rendererToUse) noexcept
  789. {
  790. // This method must not be called when the context has already been attached!
  791. // Call it before attaching your context, or use detach() first, before calling this!
  792. jassert (nativeContext == nullptr);
  793. renderer = rendererToUse;
  794. }
  795. void OpenGLContext::setComponentPaintingEnabled (bool shouldPaintComponent) noexcept
  796. {
  797. // This method must not be called when the context has already been attached!
  798. // Call it before attaching your context, or use detach() first, before calling this!
  799. jassert (nativeContext == nullptr);
  800. renderComponents = shouldPaintComponent;
  801. }
  802. void OpenGLContext::setContinuousRepainting (bool shouldContinuouslyRepaint) noexcept
  803. {
  804. continuousRepaint = shouldContinuouslyRepaint;
  805. #if JUCE_MAC
  806. if (auto* component = getTargetComponent())
  807. {
  808. detach();
  809. attachment.reset (new Attachment (*this, *component));
  810. }
  811. #endif
  812. triggerRepaint();
  813. }
  814. void OpenGLContext::setPixelFormat (const OpenGLPixelFormat& preferredPixelFormat) noexcept
  815. {
  816. // This method must not be called when the context has already been attached!
  817. // Call it before attaching your context, or use detach() first, before calling this!
  818. jassert (nativeContext == nullptr);
  819. openGLPixelFormat = preferredPixelFormat;
  820. }
  821. void OpenGLContext::setTextureMagnificationFilter (OpenGLContext::TextureMagnificationFilter magFilterMode) noexcept
  822. {
  823. texMagFilter = magFilterMode;
  824. }
  825. void OpenGLContext::setNativeSharedContext (void* nativeContextToShareWith) noexcept
  826. {
  827. // This method must not be called when the context has already been attached!
  828. // Call it before attaching your context, or use detach() first, before calling this!
  829. jassert (nativeContext == nullptr);
  830. contextToShareWith = nativeContextToShareWith;
  831. }
  832. void OpenGLContext::setMultisamplingEnabled (bool b) noexcept
  833. {
  834. // This method must not be called when the context has already been attached!
  835. // Call it before attaching your context, or use detach() first, before calling this!
  836. jassert (nativeContext == nullptr);
  837. useMultisampling = b;
  838. }
  839. void OpenGLContext::setOpenGLVersionRequired (OpenGLVersion v) noexcept
  840. {
  841. versionRequired = v;
  842. }
  843. void OpenGLContext::attachTo (Component& component)
  844. {
  845. component.repaint();
  846. if (getTargetComponent() != &component)
  847. {
  848. detach();
  849. attachment.reset (new Attachment (*this, component));
  850. }
  851. }
  852. void OpenGLContext::detach()
  853. {
  854. if (auto* a = attachment.get())
  855. {
  856. a->detach(); // must detach before nulling our pointer
  857. attachment.reset();
  858. }
  859. nativeContext = nullptr;
  860. }
  861. bool OpenGLContext::isAttached() const noexcept
  862. {
  863. return nativeContext != nullptr;
  864. }
  865. Component* OpenGLContext::getTargetComponent() const noexcept
  866. {
  867. return attachment != nullptr ? attachment->getComponent() : nullptr;
  868. }
  869. OpenGLContext* OpenGLContext::getContextAttachedTo (Component& c) noexcept
  870. {
  871. if (auto* ci = CachedImage::get (c))
  872. return &(ci->context);
  873. return nullptr;
  874. }
  875. static ThreadLocalValue<OpenGLContext*> currentThreadActiveContext;
  876. OpenGLContext* OpenGLContext::getCurrentContext()
  877. {
  878. return currentThreadActiveContext.get();
  879. }
  880. bool OpenGLContext::makeActive() const noexcept
  881. {
  882. auto& current = currentThreadActiveContext.get();
  883. if (nativeContext != nullptr && nativeContext->makeActive())
  884. {
  885. current = const_cast<OpenGLContext*> (this);
  886. return true;
  887. }
  888. current = nullptr;
  889. return false;
  890. }
  891. bool OpenGLContext::isActive() const noexcept
  892. {
  893. return nativeContext != nullptr && nativeContext->isActive();
  894. }
  895. void OpenGLContext::deactivateCurrentContext()
  896. {
  897. NativeContext::deactivateCurrentContext();
  898. currentThreadActiveContext.get() = nullptr;
  899. }
  900. void OpenGLContext::triggerRepaint()
  901. {
  902. if (auto* cachedImage = getCachedImage())
  903. cachedImage->triggerRepaint();
  904. }
  905. void OpenGLContext::swapBuffers()
  906. {
  907. if (nativeContext != nullptr)
  908. nativeContext->swapBuffers();
  909. }
  910. unsigned int OpenGLContext::getFrameBufferID() const noexcept
  911. {
  912. return nativeContext != nullptr ? nativeContext->getFrameBufferID() : 0;
  913. }
  914. bool OpenGLContext::setSwapInterval (int numFramesPerSwap)
  915. {
  916. return nativeContext != nullptr && nativeContext->setSwapInterval (numFramesPerSwap);
  917. }
  918. int OpenGLContext::getSwapInterval() const
  919. {
  920. return nativeContext != nullptr ? nativeContext->getSwapInterval() : 0;
  921. }
  922. void* OpenGLContext::getRawContext() const noexcept
  923. {
  924. return nativeContext != nullptr ? nativeContext->getRawContext() : nullptr;
  925. }
  926. OpenGLContext::CachedImage* OpenGLContext::getCachedImage() const noexcept
  927. {
  928. if (auto* comp = getTargetComponent())
  929. return CachedImage::get (*comp);
  930. return nullptr;
  931. }
  932. bool OpenGLContext::areShadersAvailable() const
  933. {
  934. auto* c = getCachedImage();
  935. return c != nullptr && c->shadersAvailable;
  936. }
  937. bool OpenGLContext::isTextureNpotSupported() const
  938. {
  939. auto* c = getCachedImage();
  940. return c != nullptr && c->textureNpotSupported;
  941. }
  942. ReferenceCountedObject* OpenGLContext::getAssociatedObject (const char* name) const
  943. {
  944. jassert (name != nullptr);
  945. auto* c = getCachedImage();
  946. // This method must only be called from an openGL rendering callback.
  947. jassert (c != nullptr && nativeContext != nullptr);
  948. jassert (getCurrentContext() != nullptr);
  949. auto index = c->associatedObjectNames.indexOf (name);
  950. return index >= 0 ? c->associatedObjects.getUnchecked (index).get() : nullptr;
  951. }
  952. void OpenGLContext::setAssociatedObject (const char* name, ReferenceCountedObject* newObject)
  953. {
  954. jassert (name != nullptr);
  955. if (auto* c = getCachedImage())
  956. {
  957. // This method must only be called from an openGL rendering callback.
  958. jassert (nativeContext != nullptr);
  959. jassert (getCurrentContext() != nullptr);
  960. const int index = c->associatedObjectNames.indexOf (name);
  961. if (index >= 0)
  962. {
  963. if (newObject != nullptr)
  964. {
  965. c->associatedObjects.set (index, newObject);
  966. }
  967. else
  968. {
  969. c->associatedObjectNames.remove (index);
  970. c->associatedObjects.remove (index);
  971. }
  972. }
  973. else if (newObject != nullptr)
  974. {
  975. c->associatedObjectNames.add (name);
  976. c->associatedObjects.add (newObject);
  977. }
  978. }
  979. }
  980. void OpenGLContext::setImageCacheSize (size_t newSize) noexcept { imageCacheMaxSize = newSize; }
  981. size_t OpenGLContext::getImageCacheSize() const noexcept { return imageCacheMaxSize; }
  982. void OpenGLContext::execute (OpenGLContext::AsyncWorker::Ptr workerToUse, bool shouldBlock)
  983. {
  984. if (auto* c = getCachedImage())
  985. c->execute (std::move (workerToUse), shouldBlock);
  986. else
  987. jassertfalse; // You must have attached the context to a component
  988. }
  989. //==============================================================================
  990. struct DepthTestDisabler
  991. {
  992. DepthTestDisabler() noexcept
  993. {
  994. glGetBooleanv (GL_DEPTH_TEST, &wasEnabled);
  995. if (wasEnabled)
  996. glDisable (GL_DEPTH_TEST);
  997. }
  998. ~DepthTestDisabler() noexcept
  999. {
  1000. if (wasEnabled)
  1001. glEnable (GL_DEPTH_TEST);
  1002. }
  1003. GLboolean wasEnabled;
  1004. };
  1005. //==============================================================================
  1006. void OpenGLContext::copyTexture (const Rectangle<int>& targetClipArea,
  1007. const Rectangle<int>& anchorPosAndTextureSize,
  1008. const int contextWidth, const int contextHeight,
  1009. bool flippedVertically)
  1010. {
  1011. if (contextWidth <= 0 || contextHeight <= 0)
  1012. return;
  1013. JUCE_CHECK_OPENGL_ERROR
  1014. glBlendFunc (GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
  1015. glEnable (GL_BLEND);
  1016. DepthTestDisabler depthDisabler;
  1017. if (areShadersAvailable())
  1018. {
  1019. struct OverlayShaderProgram : public ReferenceCountedObject
  1020. {
  1021. OverlayShaderProgram (OpenGLContext& context)
  1022. : program (context), builder (program), params (program)
  1023. {}
  1024. static const OverlayShaderProgram& select (OpenGLContext& context)
  1025. {
  1026. static const char programValueID[] = "juceGLComponentOverlayShader";
  1027. OverlayShaderProgram* program = static_cast<OverlayShaderProgram*> (context.getAssociatedObject (programValueID));
  1028. if (program == nullptr)
  1029. {
  1030. program = new OverlayShaderProgram (context);
  1031. context.setAssociatedObject (programValueID, program);
  1032. }
  1033. program->program.use();
  1034. return *program;
  1035. }
  1036. struct ProgramBuilder
  1037. {
  1038. ProgramBuilder (OpenGLShaderProgram& prog)
  1039. {
  1040. prog.addVertexShader (OpenGLHelpers::translateVertexShaderToV3 (
  1041. "attribute " JUCE_HIGHP " vec2 position;"
  1042. "uniform " JUCE_HIGHP " vec2 screenSize;"
  1043. "uniform " JUCE_HIGHP " float textureBounds[4];"
  1044. "uniform " JUCE_HIGHP " vec2 vOffsetAndScale;"
  1045. "varying " JUCE_HIGHP " vec2 texturePos;"
  1046. "void main()"
  1047. "{"
  1048. JUCE_HIGHP " vec2 scaled = position / (0.5 * screenSize.xy);"
  1049. "gl_Position = vec4 (scaled.x - 1.0, 1.0 - scaled.y, 0, 1.0);"
  1050. "texturePos = (position - vec2 (textureBounds[0], textureBounds[1])) / vec2 (textureBounds[2], textureBounds[3]);"
  1051. "texturePos = vec2 (texturePos.x, vOffsetAndScale.x + vOffsetAndScale.y * texturePos.y);"
  1052. "}"));
  1053. prog.addFragmentShader (OpenGLHelpers::translateFragmentShaderToV3 (
  1054. "uniform sampler2D imageTexture;"
  1055. "varying " JUCE_HIGHP " vec2 texturePos;"
  1056. "void main()"
  1057. "{"
  1058. "gl_FragColor = texture2D (imageTexture, texturePos);"
  1059. "}"));
  1060. prog.link();
  1061. }
  1062. };
  1063. struct Params
  1064. {
  1065. Params (OpenGLShaderProgram& prog)
  1066. : positionAttribute (prog, "position"),
  1067. screenSize (prog, "screenSize"),
  1068. imageTexture (prog, "imageTexture"),
  1069. textureBounds (prog, "textureBounds"),
  1070. vOffsetAndScale (prog, "vOffsetAndScale")
  1071. {}
  1072. void set (const float targetWidth, const float targetHeight, const Rectangle<float>& bounds, bool flipVertically) const
  1073. {
  1074. const GLfloat m[] = { bounds.getX(), bounds.getY(), bounds.getWidth(), bounds.getHeight() };
  1075. textureBounds.set (m, 4);
  1076. imageTexture.set (0);
  1077. screenSize.set (targetWidth, targetHeight);
  1078. vOffsetAndScale.set (flipVertically ? 0.0f : 1.0f,
  1079. flipVertically ? 1.0f : -1.0f);
  1080. }
  1081. OpenGLShaderProgram::Attribute positionAttribute;
  1082. OpenGLShaderProgram::Uniform screenSize, imageTexture, textureBounds, vOffsetAndScale;
  1083. };
  1084. OpenGLShaderProgram program;
  1085. ProgramBuilder builder;
  1086. Params params;
  1087. };
  1088. auto left = (GLshort) targetClipArea.getX();
  1089. auto top = (GLshort) targetClipArea.getY();
  1090. auto right = (GLshort) targetClipArea.getRight();
  1091. auto bottom = (GLshort) targetClipArea.getBottom();
  1092. const GLshort vertices[] = { left, bottom, right, bottom, left, top, right, top };
  1093. auto& program = OverlayShaderProgram::select (*this);
  1094. program.params.set ((float) contextWidth, (float) contextHeight, anchorPosAndTextureSize.toFloat(), flippedVertically);
  1095. GLuint vertexBuffer = 0;
  1096. extensions.glGenBuffers (1, &vertexBuffer);
  1097. extensions.glBindBuffer (GL_ARRAY_BUFFER, vertexBuffer);
  1098. extensions.glBufferData (GL_ARRAY_BUFFER, sizeof (vertices), vertices, GL_STATIC_DRAW);
  1099. auto index = (GLuint) program.params.positionAttribute.attributeID;
  1100. extensions.glVertexAttribPointer (index, 2, GL_SHORT, GL_FALSE, 4, nullptr);
  1101. extensions.glEnableVertexAttribArray (index);
  1102. JUCE_CHECK_OPENGL_ERROR
  1103. if (extensions.glCheckFramebufferStatus (GL_FRAMEBUFFER) == GL_FRAMEBUFFER_COMPLETE)
  1104. {
  1105. glDrawArrays (GL_TRIANGLE_STRIP, 0, 4);
  1106. extensions.glBindBuffer (GL_ARRAY_BUFFER, 0);
  1107. extensions.glUseProgram (0);
  1108. extensions.glDisableVertexAttribArray (index);
  1109. extensions.glDeleteBuffers (1, &vertexBuffer);
  1110. }
  1111. else
  1112. {
  1113. clearGLError();
  1114. }
  1115. }
  1116. else
  1117. {
  1118. jassert (attachment == nullptr); // Running on an old graphics card!
  1119. }
  1120. JUCE_CHECK_OPENGL_ERROR
  1121. }
  1122. #if JUCE_ANDROID
  1123. EGLDisplay OpenGLContext::NativeContext::display = EGL_NO_DISPLAY;
  1124. EGLDisplay OpenGLContext::NativeContext::config;
  1125. void OpenGLContext::NativeContext::surfaceCreated (LocalRef<jobject> holder)
  1126. {
  1127. ignoreUnused (holder);
  1128. if (auto* cachedImage = CachedImage::get (component))
  1129. {
  1130. if (auto* pool = cachedImage->renderThread.get())
  1131. {
  1132. if (! pool->contains (cachedImage))
  1133. {
  1134. cachedImage->resume();
  1135. cachedImage->context.triggerRepaint();
  1136. }
  1137. }
  1138. }
  1139. }
  1140. void OpenGLContext::NativeContext::surfaceDestroyed (LocalRef<jobject> holder)
  1141. {
  1142. ignoreUnused (holder);
  1143. // unlike the name suggests this will be called just before the
  1144. // surface is destroyed. We need to pause the render thread.
  1145. if (auto* cachedImage = CachedImage::get (component))
  1146. {
  1147. cachedImage->pause();
  1148. if (auto* threadPool = cachedImage->renderThread.get())
  1149. threadPool->waitForJobToFinish (cachedImage, -1);
  1150. }
  1151. }
  1152. #endif
  1153. } // namespace juce