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.

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