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.

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