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.

1268 lines
38KB

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