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.

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