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.

1358 lines
41KB

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