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.

1335 lines
40KB

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