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.

1421 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. renderThread = std::make_unique<ThreadPool> (1);
  95. renderThread->setThreadPriorities (9);
  96. resume();
  97. }
  98. }
  99. void stop()
  100. {
  101. if (renderThread != nullptr)
  102. {
  103. // make sure everything has finished executing
  104. destroying = true;
  105. if (workQueue.size() > 0)
  106. {
  107. if (! renderThread->contains (this))
  108. resume();
  109. while (workQueue.size() != 0)
  110. Thread::sleep (20);
  111. }
  112. pause();
  113. renderThread.reset();
  114. }
  115. hasInitialised = false;
  116. }
  117. //==============================================================================
  118. void pause()
  119. {
  120. signalJobShouldExit();
  121. messageManagerLock.abort();
  122. if (renderThread != nullptr)
  123. {
  124. repaintEvent.signal();
  125. renderThread->removeJob (this, true, -1);
  126. }
  127. }
  128. void resume()
  129. {
  130. #if JUCE_MAC
  131. initialDisplayTopLeft = Desktop::getInstance().getDisplays()
  132. .getDisplayForRect (component.getTopLevelComponent()->getScreenBounds())
  133. ->totalArea
  134. .getTopLeft();
  135. #endif
  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. auto localBounds = component.getLocalBounds();
  250. const auto currentDisplay = Desktop::getInstance().getDisplays().getDisplayForRect (component.getTopLevelComponent()->getScreenBounds());
  251. if (currentDisplay != lastDisplay)
  252. {
  253. #if JUCE_MAC
  254. if (cvDisplayLinkWrapper != nullptr)
  255. {
  256. cvDisplayLinkWrapper->updateActiveDisplay (currentDisplay->totalArea.getTopLeft());
  257. nativeContext->setNominalVideoRefreshPeriodS (cvDisplayLinkWrapper->getNominalVideoRefreshPeriodS());
  258. }
  259. #endif
  260. lastDisplay = currentDisplay;
  261. }
  262. const auto displayScale = currentDisplay->scale;
  263. auto newArea = peer->getComponent().getLocalArea (&component, localBounds).withZeroOrigin() * displayScale;
  264. #if JUCE_WINDOWS && JUCE_WIN_PER_MONITOR_DPI_AWARE
  265. auto newScale = getScaleFactorForWindow (nativeContext->getNativeHandle());
  266. auto desktopScale = Desktop::getInstance().getGlobalScaleFactor();
  267. if (! approximatelyEqual (1.0f, desktopScale))
  268. newScale *= desktopScale;
  269. #else
  270. auto newScale = displayScale;
  271. #endif
  272. if (scale != newScale || viewportArea != newArea)
  273. {
  274. scale = newScale;
  275. viewportArea = newArea;
  276. transform = AffineTransform::scale ((float) newArea.getWidth() / (float) localBounds.getWidth(),
  277. (float) newArea.getHeight() / (float) localBounds.getHeight());
  278. nativeContext->updateWindowPosition (peer->getAreaCoveredBy (component));
  279. if (canTriggerUpdate)
  280. invalidateAll();
  281. }
  282. }
  283. }
  284. void bindVertexArray() noexcept
  285. {
  286. if (openGLVersion.major >= 3)
  287. if (vertexArrayObject != 0)
  288. context.extensions.glBindVertexArray (vertexArrayObject);
  289. }
  290. void checkViewportBounds()
  291. {
  292. auto screenBounds = component.getTopLevelComponent()->getScreenBounds();
  293. if (lastScreenBounds != screenBounds)
  294. {
  295. updateViewportSize (true);
  296. lastScreenBounds = screenBounds;
  297. }
  298. }
  299. void paintComponent()
  300. {
  301. // you mustn't set your own cached image object when attaching a GL context!
  302. jassert (get (component) == this);
  303. if (! ensureFrameBufferSize())
  304. return;
  305. RectangleList<int> invalid (viewportArea);
  306. invalid.subtract (validArea);
  307. validArea = viewportArea;
  308. if (! invalid.isEmpty())
  309. {
  310. clearRegionInFrameBuffer (invalid);
  311. {
  312. std::unique_ptr<LowLevelGraphicsContext> g (createOpenGLGraphicsContext (context, cachedImageFrameBuffer));
  313. g->clipToRectangleList (invalid);
  314. g->addTransform (transform);
  315. paintOwner (*g);
  316. JUCE_CHECK_OPENGL_ERROR
  317. }
  318. if (! context.isActive())
  319. context.makeActive();
  320. }
  321. JUCE_CHECK_OPENGL_ERROR
  322. }
  323. void drawComponentBuffer()
  324. {
  325. #if ! JUCE_ANDROID
  326. glEnable (GL_TEXTURE_2D);
  327. clearGLError();
  328. #endif
  329. #if JUCE_WINDOWS
  330. // some stupidly old drivers are missing this function, so try to at least avoid a crash here,
  331. // but if you hit this assertion you may want to have your own version check before using the
  332. // component rendering stuff on such old drivers.
  333. jassert (context.extensions.glActiveTexture != nullptr);
  334. if (context.extensions.glActiveTexture != nullptr)
  335. #endif
  336. context.extensions.glActiveTexture (GL_TEXTURE0);
  337. glBindTexture (GL_TEXTURE_2D, cachedImageFrameBuffer.getTextureID());
  338. bindVertexArray();
  339. const Rectangle<int> cacheBounds (cachedImageFrameBuffer.getWidth(), cachedImageFrameBuffer.getHeight());
  340. context.copyTexture (cacheBounds, cacheBounds, cacheBounds.getWidth(), cacheBounds.getHeight(), false);
  341. glBindTexture (GL_TEXTURE_2D, 0);
  342. JUCE_CHECK_OPENGL_ERROR
  343. }
  344. void paintOwner (LowLevelGraphicsContext& llgc)
  345. {
  346. Graphics g (llgc);
  347. #if JUCE_ENABLE_REPAINT_DEBUGGING
  348. #ifdef JUCE_IS_REPAINT_DEBUGGING_ACTIVE
  349. if (JUCE_IS_REPAINT_DEBUGGING_ACTIVE)
  350. #endif
  351. {
  352. g.saveState();
  353. }
  354. #endif
  355. JUCE_TRY
  356. {
  357. component.paintEntireComponent (g, false);
  358. }
  359. JUCE_CATCH_EXCEPTION
  360. #if JUCE_ENABLE_REPAINT_DEBUGGING
  361. #ifdef JUCE_IS_REPAINT_DEBUGGING_ACTIVE
  362. if (JUCE_IS_REPAINT_DEBUGGING_ACTIVE)
  363. #endif
  364. {
  365. // enabling this code will fill all areas that get repainted with a colour overlay, to show
  366. // clearly when things are being repainted.
  367. g.restoreState();
  368. static Random rng;
  369. g.fillAll (Colour ((uint8) rng.nextInt (255),
  370. (uint8) rng.nextInt (255),
  371. (uint8) rng.nextInt (255),
  372. (uint8) 0x50));
  373. }
  374. #endif
  375. }
  376. void handleResize()
  377. {
  378. updateViewportSize (true);
  379. #if JUCE_MAC
  380. if (hasInitialised)
  381. {
  382. [nativeContext->view update];
  383. renderFrame();
  384. }
  385. #endif
  386. }
  387. //==============================================================================
  388. JobStatus runJob() override
  389. {
  390. {
  391. // Allow the message thread to finish setting-up the context before using it.
  392. MessageManager::Lock::ScopedTryLockType mmLock (messageManagerLock, false);
  393. do
  394. {
  395. if (shouldExit())
  396. return ThreadPoolJob::jobHasFinished;
  397. } while (! mmLock.retryLock());
  398. }
  399. if (! initialiseOnThread())
  400. {
  401. hasInitialised = false;
  402. return ThreadPoolJob::jobHasFinished;
  403. }
  404. hasInitialised = true;
  405. while (! shouldExit())
  406. {
  407. #if JUCE_IOS
  408. if (backgroundProcessCheck.isBackgroundProcess())
  409. {
  410. repaintEvent.wait (300);
  411. repaintEvent.reset();
  412. continue;
  413. }
  414. #endif
  415. if (shouldExit())
  416. break;
  417. #if JUCE_MAC
  418. if (context.continuousRepaint)
  419. {
  420. repaintEvent.wait (-1);
  421. renderFrame();
  422. }
  423. else
  424. #endif
  425. if (! renderFrame())
  426. repaintEvent.wait (5); // failed to render, so avoid a tight fail-loop.
  427. else if (! context.continuousRepaint && ! shouldExit())
  428. repaintEvent.wait (-1);
  429. repaintEvent.reset();
  430. }
  431. hasInitialised = false;
  432. context.makeActive();
  433. shutdownOnThread();
  434. OpenGLContext::deactivateCurrentContext();
  435. return ThreadPoolJob::jobHasFinished;
  436. }
  437. bool initialiseOnThread()
  438. {
  439. // On android, this can get called twice, so drop any previous state.
  440. associatedObjectNames.clear();
  441. associatedObjects.clear();
  442. cachedImageFrameBuffer.release();
  443. context.makeActive();
  444. if (! nativeContext->initialiseOnRenderThread (context))
  445. return false;
  446. #if JUCE_ANDROID
  447. // On android the context may be created in initialiseOnRenderThread
  448. // and we therefore need to call makeActive again
  449. context.makeActive();
  450. #endif
  451. gl::loadFunctions();
  452. openGLVersion = getOpenGLVersion();
  453. if (openGLVersion.major >= 3)
  454. {
  455. context.extensions.glGenVertexArrays (1, &vertexArrayObject);
  456. bindVertexArray();
  457. }
  458. glViewport (0, 0, viewportArea.getWidth(), viewportArea.getHeight());
  459. nativeContext->setSwapInterval (1);
  460. #if ! JUCE_OPENGL_ES
  461. JUCE_CHECK_OPENGL_ERROR
  462. shadersAvailable = OpenGLShaderProgram::getLanguageVersion() > 0;
  463. clearGLError();
  464. #endif
  465. textureNpotSupported = contextHasTextureNpotFeature();
  466. if (context.renderer != nullptr)
  467. context.renderer->newOpenGLContextCreated();
  468. #if JUCE_MAC
  469. cvDisplayLinkWrapper = std::make_unique<CVDisplayLinkWrapper> (*this);
  470. cvDisplayLinkWrapper->updateActiveDisplay (initialDisplayTopLeft);
  471. nativeContext->setNominalVideoRefreshPeriodS (cvDisplayLinkWrapper->getNominalVideoRefreshPeriodS());
  472. #endif
  473. return true;
  474. }
  475. void shutdownOnThread()
  476. {
  477. #if JUCE_MAC
  478. cvDisplayLinkWrapper = nullptr;
  479. #endif
  480. if (context.renderer != nullptr)
  481. context.renderer->openGLContextClosing();
  482. if (vertexArrayObject != 0)
  483. context.extensions.glDeleteVertexArrays (1, &vertexArrayObject);
  484. associatedObjectNames.clear();
  485. associatedObjects.clear();
  486. cachedImageFrameBuffer.release();
  487. nativeContext->shutdownOnRenderThread();
  488. }
  489. //==============================================================================
  490. struct BlockingWorker : public OpenGLContext::AsyncWorker
  491. {
  492. BlockingWorker (OpenGLContext::AsyncWorker::Ptr && workerToUse)
  493. : originalWorker (std::move (workerToUse))
  494. {}
  495. void operator() (OpenGLContext& calleeContext)
  496. {
  497. if (originalWorker != nullptr)
  498. (*originalWorker) (calleeContext);
  499. finishedSignal.signal();
  500. }
  501. void block() noexcept { finishedSignal.wait(); }
  502. OpenGLContext::AsyncWorker::Ptr originalWorker;
  503. WaitableEvent finishedSignal;
  504. };
  505. bool doWorkWhileWaitingForLock (bool contextIsAlreadyActive)
  506. {
  507. bool contextActivated = false;
  508. for (OpenGLContext::AsyncWorker::Ptr work = workQueue.removeAndReturn (0);
  509. work != nullptr && (! shouldExit()); work = workQueue.removeAndReturn (0))
  510. {
  511. if ((! contextActivated) && (! contextIsAlreadyActive))
  512. {
  513. if (! context.makeActive())
  514. break;
  515. contextActivated = true;
  516. }
  517. NativeContext::Locker locker (*nativeContext);
  518. (*work) (context);
  519. clearGLError();
  520. }
  521. if (contextActivated)
  522. OpenGLContext::deactivateCurrentContext();
  523. return shouldExit();
  524. }
  525. void execute (OpenGLContext::AsyncWorker::Ptr workerToUse, bool shouldBlock, bool calledFromDestructor = false)
  526. {
  527. if (calledFromDestructor || ! destroying)
  528. {
  529. if (shouldBlock)
  530. {
  531. auto blocker = new BlockingWorker (std::move (workerToUse));
  532. OpenGLContext::AsyncWorker::Ptr worker (*blocker);
  533. workQueue.add (worker);
  534. messageManagerLock.abort();
  535. context.triggerRepaint();
  536. blocker->block();
  537. }
  538. else
  539. {
  540. workQueue.add (std::move (workerToUse));
  541. messageManagerLock.abort();
  542. context.triggerRepaint();
  543. }
  544. }
  545. else
  546. {
  547. jassertfalse; // you called execute AFTER you detached your OpenGLContext
  548. }
  549. }
  550. //==============================================================================
  551. static CachedImage* get (Component& c) noexcept
  552. {
  553. return dynamic_cast<CachedImage*> (c.getCachedComponentImage());
  554. }
  555. //==============================================================================
  556. friend class NativeContext;
  557. std::unique_ptr<NativeContext> nativeContext;
  558. OpenGLContext& context;
  559. Component& component;
  560. Version openGLVersion;
  561. OpenGLFrameBuffer cachedImageFrameBuffer;
  562. RectangleList<int> validArea;
  563. Rectangle<int> viewportArea, lastScreenBounds;
  564. const Displays::Display* lastDisplay = nullptr;
  565. double scale = 1.0;
  566. AffineTransform transform;
  567. GLuint vertexArrayObject = 0;
  568. #if JUCE_MAC
  569. Point<int> initialDisplayTopLeft;
  570. #endif
  571. StringArray associatedObjectNames;
  572. ReferenceCountedArray<ReferenceCountedObject> associatedObjects;
  573. WaitableEvent canPaintNowFlag, finishedPaintingFlag, repaintEvent { true };
  574. #if JUCE_OPENGL_ES
  575. bool shadersAvailable = true;
  576. #else
  577. bool shadersAvailable = false;
  578. #endif
  579. bool textureNpotSupported = false;
  580. std::atomic<bool> hasInitialised { false }, needsUpdate { true }, destroying { false };
  581. uint32 lastMMLockReleaseTime = 0;
  582. #if JUCE_MAC
  583. struct CVDisplayLinkWrapper
  584. {
  585. explicit CVDisplayLinkWrapper (CachedImage& cachedImageIn)
  586. : cachedImage (cachedImageIn),
  587. continuousRepaint (cachedImageIn.context.continuousRepaint.load())
  588. {
  589. CVDisplayLinkCreateWithActiveCGDisplays (&displayLink);
  590. CVDisplayLinkSetOutputCallback (displayLink, &displayLinkCallback, this);
  591. CVDisplayLinkStart (displayLink);
  592. }
  593. double getNominalVideoRefreshPeriodS() const
  594. {
  595. const auto nominalVideoRefreshPeriod = CVDisplayLinkGetNominalOutputVideoRefreshPeriod (displayLink);
  596. if ((nominalVideoRefreshPeriod.flags & kCVTimeIsIndefinite) == 0)
  597. return (double) nominalVideoRefreshPeriod.timeValue / (double) nominalVideoRefreshPeriod.timeScale;
  598. return 0.0;
  599. }
  600. void updateActiveDisplay (Point<int> topLeftOfDisplay)
  601. {
  602. CGDirectDisplayID displayID;
  603. uint32_t numDisplays = 0;
  604. constexpr uint32_t maxNumDisplays = 1;
  605. CGGetDisplaysWithPoint (convertToCGPoint (topLeftOfDisplay), maxNumDisplays, &displayID, &numDisplays);
  606. if (numDisplays == 1)
  607. CVDisplayLinkSetCurrentCGDisplay (displayLink, displayID);
  608. }
  609. ~CVDisplayLinkWrapper()
  610. {
  611. CVDisplayLinkStop (displayLink);
  612. CVDisplayLinkRelease (displayLink);
  613. }
  614. static CVReturn displayLinkCallback (CVDisplayLinkRef, const CVTimeStamp*, const CVTimeStamp*,
  615. CVOptionFlags, CVOptionFlags*, void* displayLinkContext)
  616. {
  617. auto* self = reinterpret_cast<CVDisplayLinkWrapper*> (displayLinkContext);
  618. if (self->continuousRepaint)
  619. self->cachedImage.repaintEvent.signal();
  620. return kCVReturnSuccess;
  621. }
  622. CachedImage& cachedImage;
  623. const bool continuousRepaint;
  624. CVDisplayLinkRef displayLink;
  625. };
  626. std::unique_ptr<CVDisplayLinkWrapper> cvDisplayLinkWrapper;
  627. #endif
  628. std::unique_ptr<ThreadPool> renderThread;
  629. ReferenceCountedArray<OpenGLContext::AsyncWorker, CriticalSection> workQueue;
  630. MessageManager::Lock messageManagerLock;
  631. #if JUCE_IOS
  632. iOSBackgroundProcessCheck backgroundProcessCheck;
  633. #endif
  634. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (CachedImage)
  635. };
  636. //==============================================================================
  637. class OpenGLContext::Attachment : public ComponentMovementWatcher,
  638. private Timer
  639. {
  640. public:
  641. Attachment (OpenGLContext& c, Component& comp)
  642. : ComponentMovementWatcher (&comp), context (c)
  643. {
  644. if (canBeAttached (comp))
  645. attach();
  646. }
  647. ~Attachment() override
  648. {
  649. detach();
  650. }
  651. void detach()
  652. {
  653. auto& comp = *getComponent();
  654. stop();
  655. comp.setCachedComponentImage (nullptr);
  656. context.nativeContext = nullptr;
  657. }
  658. void componentMovedOrResized (bool /*wasMoved*/, bool /*wasResized*/) override
  659. {
  660. auto& comp = *getComponent();
  661. if (isAttached (comp) != canBeAttached (comp))
  662. componentVisibilityChanged();
  663. if (comp.getWidth() > 0 && comp.getHeight() > 0
  664. && context.nativeContext != nullptr)
  665. {
  666. if (auto* c = CachedImage::get (comp))
  667. c->handleResize();
  668. if (auto* peer = comp.getTopLevelComponent()->getPeer())
  669. context.nativeContext->updateWindowPosition (peer->getAreaCoveredBy (comp));
  670. }
  671. }
  672. using ComponentMovementWatcher::componentMovedOrResized;
  673. void componentPeerChanged() override
  674. {
  675. detach();
  676. componentVisibilityChanged();
  677. }
  678. void componentVisibilityChanged() override
  679. {
  680. auto& comp = *getComponent();
  681. if (canBeAttached (comp))
  682. {
  683. if (isAttached (comp))
  684. comp.repaint(); // (needed when windows are un-minimised)
  685. else
  686. attach();
  687. }
  688. else
  689. {
  690. detach();
  691. }
  692. }
  693. using ComponentMovementWatcher::componentVisibilityChanged;
  694. #if JUCE_DEBUG || JUCE_LOG_ASSERTIONS
  695. void componentBeingDeleted (Component& c) override
  696. {
  697. /* You must call detach() or delete your OpenGLContext to remove it
  698. from a component BEFORE deleting the component that it is using!
  699. */
  700. jassertfalse;
  701. ComponentMovementWatcher::componentBeingDeleted (c);
  702. }
  703. #endif
  704. void update()
  705. {
  706. auto& comp = *getComponent();
  707. if (canBeAttached (comp))
  708. start();
  709. else
  710. stop();
  711. }
  712. private:
  713. OpenGLContext& context;
  714. bool canBeAttached (const Component& comp) noexcept
  715. {
  716. return (! context.overrideCanAttach) && comp.getWidth() > 0 && comp.getHeight() > 0 && isShowingOrMinimised (comp);
  717. }
  718. static bool isShowingOrMinimised (const Component& c)
  719. {
  720. if (! c.isVisible())
  721. return false;
  722. if (auto* p = c.getParentComponent())
  723. return isShowingOrMinimised (*p);
  724. return c.getPeer() != nullptr;
  725. }
  726. static bool isAttached (const Component& comp) noexcept
  727. {
  728. return comp.getCachedComponentImage() != nullptr;
  729. }
  730. void attach()
  731. {
  732. auto& comp = *getComponent();
  733. auto* newCachedImage = new CachedImage (context, comp,
  734. context.openGLPixelFormat,
  735. context.contextToShareWith);
  736. comp.setCachedComponentImage (newCachedImage);
  737. start();
  738. }
  739. void stop()
  740. {
  741. stopTimer();
  742. auto& comp = *getComponent();
  743. #if JUCE_MAC
  744. [[(NSView*) comp.getWindowHandle() window] disableScreenUpdatesUntilFlush];
  745. #endif
  746. if (auto* oldCachedImage = CachedImage::get (comp))
  747. oldCachedImage->stop(); // (must stop this before detaching it from the component)
  748. }
  749. void start()
  750. {
  751. auto& comp = *getComponent();
  752. if (auto* cachedImage = CachedImage::get (comp))
  753. {
  754. cachedImage->start(); // (must wait until this is attached before starting its thread)
  755. cachedImage->updateViewportSize (true);
  756. startTimer (400);
  757. }
  758. }
  759. void timerCallback() override
  760. {
  761. if (auto* cachedImage = CachedImage::get (*getComponent()))
  762. cachedImage->checkViewportBounds();
  763. }
  764. };
  765. //==============================================================================
  766. OpenGLContext::OpenGLContext()
  767. {
  768. }
  769. OpenGLContext::~OpenGLContext()
  770. {
  771. detach();
  772. }
  773. void OpenGLContext::setRenderer (OpenGLRenderer* rendererToUse) noexcept
  774. {
  775. // This method must not be called when the context has already been attached!
  776. // Call it before attaching your context, or use detach() first, before calling this!
  777. jassert (nativeContext == nullptr);
  778. renderer = rendererToUse;
  779. }
  780. void OpenGLContext::setComponentPaintingEnabled (bool shouldPaintComponent) noexcept
  781. {
  782. // This method must not be called when the context has already been attached!
  783. // Call it before attaching your context, or use detach() first, before calling this!
  784. jassert (nativeContext == nullptr);
  785. renderComponents = shouldPaintComponent;
  786. }
  787. void OpenGLContext::setContinuousRepainting (bool shouldContinuouslyRepaint) noexcept
  788. {
  789. continuousRepaint = shouldContinuouslyRepaint;
  790. #if JUCE_MAC
  791. if (auto* component = getTargetComponent())
  792. {
  793. detach();
  794. attachment.reset (new Attachment (*this, *component));
  795. }
  796. #endif
  797. triggerRepaint();
  798. }
  799. void OpenGLContext::setPixelFormat (const OpenGLPixelFormat& preferredPixelFormat) noexcept
  800. {
  801. // This method must not be called when the context has already been attached!
  802. // Call it before attaching your context, or use detach() first, before calling this!
  803. jassert (nativeContext == nullptr);
  804. openGLPixelFormat = preferredPixelFormat;
  805. }
  806. void OpenGLContext::setTextureMagnificationFilter (OpenGLContext::TextureMagnificationFilter magFilterMode) noexcept
  807. {
  808. texMagFilter = magFilterMode;
  809. }
  810. void OpenGLContext::setNativeSharedContext (void* nativeContextToShareWith) noexcept
  811. {
  812. // This method must not be called when the context has already been attached!
  813. // Call it before attaching your context, or use detach() first, before calling this!
  814. jassert (nativeContext == nullptr);
  815. contextToShareWith = nativeContextToShareWith;
  816. }
  817. void OpenGLContext::setMultisamplingEnabled (bool b) noexcept
  818. {
  819. // This method must not be called when the context has already been attached!
  820. // Call it before attaching your context, or use detach() first, before calling this!
  821. jassert (nativeContext == nullptr);
  822. useMultisampling = b;
  823. }
  824. void OpenGLContext::setOpenGLVersionRequired (OpenGLVersion v) noexcept
  825. {
  826. versionRequired = v;
  827. }
  828. void OpenGLContext::attachTo (Component& component)
  829. {
  830. component.repaint();
  831. if (getTargetComponent() != &component)
  832. {
  833. detach();
  834. attachment.reset (new Attachment (*this, component));
  835. }
  836. }
  837. void OpenGLContext::detach()
  838. {
  839. if (auto* a = attachment.get())
  840. {
  841. a->detach(); // must detach before nulling our pointer
  842. attachment.reset();
  843. }
  844. nativeContext = nullptr;
  845. }
  846. bool OpenGLContext::isAttached() const noexcept
  847. {
  848. return nativeContext != nullptr;
  849. }
  850. Component* OpenGLContext::getTargetComponent() const noexcept
  851. {
  852. return attachment != nullptr ? attachment->getComponent() : nullptr;
  853. }
  854. OpenGLContext* OpenGLContext::getContextAttachedTo (Component& c) noexcept
  855. {
  856. if (auto* ci = CachedImage::get (c))
  857. return &(ci->context);
  858. return nullptr;
  859. }
  860. static ThreadLocalValue<OpenGLContext*> currentThreadActiveContext;
  861. OpenGLContext* OpenGLContext::getCurrentContext()
  862. {
  863. return currentThreadActiveContext.get();
  864. }
  865. bool OpenGLContext::makeActive() const noexcept
  866. {
  867. auto& current = currentThreadActiveContext.get();
  868. if (nativeContext != nullptr && nativeContext->makeActive())
  869. {
  870. current = const_cast<OpenGLContext*> (this);
  871. return true;
  872. }
  873. current = nullptr;
  874. return false;
  875. }
  876. bool OpenGLContext::isActive() const noexcept
  877. {
  878. return nativeContext != nullptr && nativeContext->isActive();
  879. }
  880. void OpenGLContext::deactivateCurrentContext()
  881. {
  882. NativeContext::deactivateCurrentContext();
  883. currentThreadActiveContext.get() = nullptr;
  884. }
  885. void OpenGLContext::triggerRepaint()
  886. {
  887. if (auto* cachedImage = getCachedImage())
  888. cachedImage->triggerRepaint();
  889. }
  890. void OpenGLContext::swapBuffers()
  891. {
  892. if (nativeContext != nullptr)
  893. nativeContext->swapBuffers();
  894. }
  895. unsigned int OpenGLContext::getFrameBufferID() const noexcept
  896. {
  897. return nativeContext != nullptr ? nativeContext->getFrameBufferID() : 0;
  898. }
  899. bool OpenGLContext::setSwapInterval (int numFramesPerSwap)
  900. {
  901. return nativeContext != nullptr && nativeContext->setSwapInterval (numFramesPerSwap);
  902. }
  903. int OpenGLContext::getSwapInterval() const
  904. {
  905. return nativeContext != nullptr ? nativeContext->getSwapInterval() : 0;
  906. }
  907. void* OpenGLContext::getRawContext() const noexcept
  908. {
  909. return nativeContext != nullptr ? nativeContext->getRawContext() : nullptr;
  910. }
  911. OpenGLContext::CachedImage* OpenGLContext::getCachedImage() const noexcept
  912. {
  913. if (auto* comp = getTargetComponent())
  914. return CachedImage::get (*comp);
  915. return nullptr;
  916. }
  917. bool OpenGLContext::areShadersAvailable() const
  918. {
  919. auto* c = getCachedImage();
  920. return c != nullptr && c->shadersAvailable;
  921. }
  922. bool OpenGLContext::isTextureNpotSupported() const
  923. {
  924. auto* c = getCachedImage();
  925. return c != nullptr && c->textureNpotSupported;
  926. }
  927. ReferenceCountedObject* OpenGLContext::getAssociatedObject (const char* name) const
  928. {
  929. jassert (name != nullptr);
  930. auto* c = getCachedImage();
  931. // This method must only be called from an openGL rendering callback.
  932. jassert (c != nullptr && nativeContext != nullptr);
  933. jassert (getCurrentContext() != nullptr);
  934. auto index = c->associatedObjectNames.indexOf (name);
  935. return index >= 0 ? c->associatedObjects.getUnchecked (index).get() : nullptr;
  936. }
  937. void OpenGLContext::setAssociatedObject (const char* name, ReferenceCountedObject* newObject)
  938. {
  939. jassert (name != nullptr);
  940. if (auto* c = getCachedImage())
  941. {
  942. // This method must only be called from an openGL rendering callback.
  943. jassert (nativeContext != nullptr);
  944. jassert (getCurrentContext() != nullptr);
  945. const int index = c->associatedObjectNames.indexOf (name);
  946. if (index >= 0)
  947. {
  948. if (newObject != nullptr)
  949. {
  950. c->associatedObjects.set (index, newObject);
  951. }
  952. else
  953. {
  954. c->associatedObjectNames.remove (index);
  955. c->associatedObjects.remove (index);
  956. }
  957. }
  958. else if (newObject != nullptr)
  959. {
  960. c->associatedObjectNames.add (name);
  961. c->associatedObjects.add (newObject);
  962. }
  963. }
  964. }
  965. void OpenGLContext::setImageCacheSize (size_t newSize) noexcept { imageCacheMaxSize = newSize; }
  966. size_t OpenGLContext::getImageCacheSize() const noexcept { return imageCacheMaxSize; }
  967. void OpenGLContext::execute (OpenGLContext::AsyncWorker::Ptr workerToUse, bool shouldBlock)
  968. {
  969. if (auto* c = getCachedImage())
  970. c->execute (std::move (workerToUse), shouldBlock);
  971. else
  972. jassertfalse; // You must have attached the context to a component
  973. }
  974. //==============================================================================
  975. struct DepthTestDisabler
  976. {
  977. DepthTestDisabler() noexcept
  978. {
  979. glGetBooleanv (GL_DEPTH_TEST, &wasEnabled);
  980. if (wasEnabled)
  981. glDisable (GL_DEPTH_TEST);
  982. }
  983. ~DepthTestDisabler() noexcept
  984. {
  985. if (wasEnabled)
  986. glEnable (GL_DEPTH_TEST);
  987. }
  988. GLboolean wasEnabled;
  989. };
  990. //==============================================================================
  991. void OpenGLContext::copyTexture (const Rectangle<int>& targetClipArea,
  992. const Rectangle<int>& anchorPosAndTextureSize,
  993. const int contextWidth, const int contextHeight,
  994. bool flippedVertically)
  995. {
  996. if (contextWidth <= 0 || contextHeight <= 0)
  997. return;
  998. JUCE_CHECK_OPENGL_ERROR
  999. glBlendFunc (GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
  1000. glEnable (GL_BLEND);
  1001. DepthTestDisabler depthDisabler;
  1002. if (areShadersAvailable())
  1003. {
  1004. struct OverlayShaderProgram : public ReferenceCountedObject
  1005. {
  1006. OverlayShaderProgram (OpenGLContext& context)
  1007. : program (context), builder (program), params (program)
  1008. {}
  1009. static const OverlayShaderProgram& select (OpenGLContext& context)
  1010. {
  1011. static const char programValueID[] = "juceGLComponentOverlayShader";
  1012. OverlayShaderProgram* program = static_cast<OverlayShaderProgram*> (context.getAssociatedObject (programValueID));
  1013. if (program == nullptr)
  1014. {
  1015. program = new OverlayShaderProgram (context);
  1016. context.setAssociatedObject (programValueID, program);
  1017. }
  1018. program->program.use();
  1019. return *program;
  1020. }
  1021. struct ProgramBuilder
  1022. {
  1023. ProgramBuilder (OpenGLShaderProgram& prog)
  1024. {
  1025. prog.addVertexShader (OpenGLHelpers::translateVertexShaderToV3 (
  1026. "attribute " JUCE_HIGHP " vec2 position;"
  1027. "uniform " JUCE_HIGHP " vec2 screenSize;"
  1028. "uniform " JUCE_HIGHP " float textureBounds[4];"
  1029. "uniform " JUCE_HIGHP " vec2 vOffsetAndScale;"
  1030. "varying " JUCE_HIGHP " vec2 texturePos;"
  1031. "void main()"
  1032. "{"
  1033. JUCE_HIGHP " vec2 scaled = position / (0.5 * screenSize.xy);"
  1034. "gl_Position = vec4 (scaled.x - 1.0, 1.0 - scaled.y, 0, 1.0);"
  1035. "texturePos = (position - vec2 (textureBounds[0], textureBounds[1])) / vec2 (textureBounds[2], textureBounds[3]);"
  1036. "texturePos = vec2 (texturePos.x, vOffsetAndScale.x + vOffsetAndScale.y * texturePos.y);"
  1037. "}"));
  1038. prog.addFragmentShader (OpenGLHelpers::translateFragmentShaderToV3 (
  1039. "uniform sampler2D imageTexture;"
  1040. "varying " JUCE_HIGHP " vec2 texturePos;"
  1041. "void main()"
  1042. "{"
  1043. "gl_FragColor = texture2D (imageTexture, texturePos);"
  1044. "}"));
  1045. prog.link();
  1046. }
  1047. };
  1048. struct Params
  1049. {
  1050. Params (OpenGLShaderProgram& prog)
  1051. : positionAttribute (prog, "position"),
  1052. screenSize (prog, "screenSize"),
  1053. imageTexture (prog, "imageTexture"),
  1054. textureBounds (prog, "textureBounds"),
  1055. vOffsetAndScale (prog, "vOffsetAndScale")
  1056. {}
  1057. void set (const float targetWidth, const float targetHeight, const Rectangle<float>& bounds, bool flipVertically) const
  1058. {
  1059. const GLfloat m[] = { bounds.getX(), bounds.getY(), bounds.getWidth(), bounds.getHeight() };
  1060. textureBounds.set (m, 4);
  1061. imageTexture.set (0);
  1062. screenSize.set (targetWidth, targetHeight);
  1063. vOffsetAndScale.set (flipVertically ? 0.0f : 1.0f,
  1064. flipVertically ? 1.0f : -1.0f);
  1065. }
  1066. OpenGLShaderProgram::Attribute positionAttribute;
  1067. OpenGLShaderProgram::Uniform screenSize, imageTexture, textureBounds, vOffsetAndScale;
  1068. };
  1069. OpenGLShaderProgram program;
  1070. ProgramBuilder builder;
  1071. Params params;
  1072. };
  1073. auto left = (GLshort) targetClipArea.getX();
  1074. auto top = (GLshort) targetClipArea.getY();
  1075. auto right = (GLshort) targetClipArea.getRight();
  1076. auto bottom = (GLshort) targetClipArea.getBottom();
  1077. const GLshort vertices[] = { left, bottom, right, bottom, left, top, right, top };
  1078. auto& program = OverlayShaderProgram::select (*this);
  1079. program.params.set ((float) contextWidth, (float) contextHeight, anchorPosAndTextureSize.toFloat(), flippedVertically);
  1080. GLuint vertexBuffer = 0;
  1081. extensions.glGenBuffers (1, &vertexBuffer);
  1082. extensions.glBindBuffer (GL_ARRAY_BUFFER, vertexBuffer);
  1083. extensions.glBufferData (GL_ARRAY_BUFFER, sizeof (vertices), vertices, GL_STATIC_DRAW);
  1084. auto index = (GLuint) program.params.positionAttribute.attributeID;
  1085. extensions.glVertexAttribPointer (index, 2, GL_SHORT, GL_FALSE, 4, nullptr);
  1086. extensions.glEnableVertexAttribArray (index);
  1087. JUCE_CHECK_OPENGL_ERROR
  1088. if (extensions.glCheckFramebufferStatus (GL_FRAMEBUFFER) == GL_FRAMEBUFFER_COMPLETE)
  1089. {
  1090. glDrawArrays (GL_TRIANGLE_STRIP, 0, 4);
  1091. extensions.glBindBuffer (GL_ARRAY_BUFFER, 0);
  1092. extensions.glUseProgram (0);
  1093. extensions.glDisableVertexAttribArray (index);
  1094. extensions.glDeleteBuffers (1, &vertexBuffer);
  1095. }
  1096. else
  1097. {
  1098. clearGLError();
  1099. }
  1100. }
  1101. else
  1102. {
  1103. jassert (attachment == nullptr); // Running on an old graphics card!
  1104. }
  1105. JUCE_CHECK_OPENGL_ERROR
  1106. }
  1107. #if JUCE_ANDROID
  1108. EGLDisplay OpenGLContext::NativeContext::display = EGL_NO_DISPLAY;
  1109. EGLDisplay OpenGLContext::NativeContext::config;
  1110. void OpenGLContext::NativeContext::surfaceCreated (LocalRef<jobject> holder)
  1111. {
  1112. ignoreUnused (holder);
  1113. if (auto* cachedImage = CachedImage::get (component))
  1114. {
  1115. if (auto* pool = cachedImage->renderThread.get())
  1116. {
  1117. if (! pool->contains (cachedImage))
  1118. {
  1119. cachedImage->resume();
  1120. cachedImage->context.triggerRepaint();
  1121. }
  1122. }
  1123. }
  1124. }
  1125. void OpenGLContext::NativeContext::surfaceDestroyed (LocalRef<jobject> holder)
  1126. {
  1127. ignoreUnused (holder);
  1128. // unlike the name suggests this will be called just before the
  1129. // surface is destroyed. We need to pause the render thread.
  1130. if (auto* cachedImage = CachedImage::get (component))
  1131. {
  1132. cachedImage->pause();
  1133. if (auto* threadPool = cachedImage->renderThread.get())
  1134. threadPool->waitForJobToFinish (cachedImage, -1);
  1135. }
  1136. }
  1137. #endif
  1138. } // namespace juce