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.

1303 lines
39KB

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