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.

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