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.

1247 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 = new NativeContext (component, pixFormat, contextToShare,
  62. c.useMultisampling, c.versionRequired);
  63. if (nativeContext->createdOk())
  64. context.nativeContext = nativeContext;
  65. else
  66. nativeContext = nullptr;
  67. }
  68. ~CachedImage()
  69. {
  70. stop();
  71. }
  72. //==============================================================================
  73. void start()
  74. {
  75. if (nativeContext != nullptr)
  76. {
  77. renderThread = 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. execute (new DoNothingWorker(), true, true);
  92. }
  93. pause();
  94. renderThread = nullptr;
  95. }
  96. hasInitialised = false;
  97. }
  98. //==============================================================================
  99. void pause()
  100. {
  101. if (renderThread != nullptr)
  102. {
  103. repaintEvent.signal();
  104. renderThread->removeJob (this, true, -1);
  105. }
  106. }
  107. void resume()
  108. {
  109. if (renderThread != nullptr)
  110. renderThread->addJob (this, false);
  111. }
  112. //==============================================================================
  113. void paint (Graphics&) override {}
  114. bool invalidateAll() override
  115. {
  116. validArea.clear();
  117. triggerRepaint();
  118. return false;
  119. }
  120. bool invalidate (const Rectangle<int>& area) override
  121. {
  122. validArea.subtract (area * scale);
  123. triggerRepaint();
  124. return false;
  125. }
  126. void releaseResources() override
  127. {
  128. stop();
  129. }
  130. void triggerRepaint()
  131. {
  132. needsUpdate = 1;
  133. repaintEvent.signal();
  134. }
  135. //==============================================================================
  136. bool ensureFrameBufferSize()
  137. {
  138. auto fbW = cachedImageFrameBuffer.getWidth();
  139. auto fbH = cachedImageFrameBuffer.getHeight();
  140. if (fbW != viewportArea.getWidth() || fbH != viewportArea.getHeight() || ! cachedImageFrameBuffer.isValid())
  141. {
  142. if (! cachedImageFrameBuffer.initialise (context, viewportArea.getWidth(), viewportArea.getHeight()))
  143. return false;
  144. validArea.clear();
  145. JUCE_CHECK_OPENGL_ERROR
  146. }
  147. return true;
  148. }
  149. void clearRegionInFrameBuffer (const RectangleList<int>& list)
  150. {
  151. glClearColor (0, 0, 0, 0);
  152. glEnable (GL_SCISSOR_TEST);
  153. auto previousFrameBufferTarget = OpenGLFrameBuffer::getCurrentFrameBufferTarget();
  154. cachedImageFrameBuffer.makeCurrentRenderingTarget();
  155. auto imageH = cachedImageFrameBuffer.getHeight();
  156. for (auto& r : list)
  157. {
  158. glScissor (r.getX(), imageH - r.getBottom(), r.getWidth(), r.getHeight());
  159. glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
  160. }
  161. glDisable (GL_SCISSOR_TEST);
  162. context.extensions.glBindFramebuffer (GL_FRAMEBUFFER, previousFrameBufferTarget);
  163. JUCE_CHECK_OPENGL_ERROR
  164. }
  165. bool renderFrame()
  166. {
  167. ScopedPointer<MessageManagerLock> mmLock;
  168. const bool isUpdating = needsUpdate.compareAndSetBool (0, 1);
  169. if (context.renderComponents && isUpdating)
  170. {
  171. MessageLockWorker worker (*this);
  172. // This avoids hogging the message thread when doing intensive rendering.
  173. if (lastMMLockReleaseTime + 1 >= Time::getMillisecondCounter())
  174. Thread::sleep (2);
  175. mmLock = new MessageManagerLock (worker); // need to acquire this before locking the context.
  176. if (! mmLock->lockWasGained())
  177. return false;
  178. updateViewportSize (false);
  179. }
  180. if (! context.makeActive())
  181. return false;
  182. NativeContext::Locker locker (*nativeContext);
  183. JUCE_CHECK_OPENGL_ERROR
  184. doWorkWhileWaitingForLock (true);
  185. if (context.renderer != nullptr)
  186. {
  187. glViewport (0, 0, viewportArea.getWidth(), viewportArea.getHeight());
  188. context.currentRenderScale = scale;
  189. context.renderer->renderOpenGL();
  190. clearGLError();
  191. bindVertexArray();
  192. }
  193. if (context.renderComponents)
  194. {
  195. if (isUpdating)
  196. {
  197. paintComponent();
  198. if (! hasInitialised)
  199. return false;
  200. mmLock = nullptr;
  201. lastMMLockReleaseTime = Time::getMillisecondCounter();
  202. }
  203. glViewport (0, 0, viewportArea.getWidth(), viewportArea.getHeight());
  204. drawComponentBuffer();
  205. }
  206. context.swapBuffers();
  207. OpenGLContext::deactivateCurrentContext();
  208. return true;
  209. }
  210. void updateViewportSize (bool canTriggerUpdate)
  211. {
  212. if (auto* peer = component.getPeer())
  213. {
  214. lastScreenBounds = component.getTopLevelComponent()->getScreenBounds();
  215. auto newScale = Desktop::getInstance().getDisplays()
  216. .getDisplayContaining (lastScreenBounds.getCentre()).scale;
  217. auto newArea = peer->getComponent().getLocalArea (&component, component.getLocalBounds())
  218. .withZeroOrigin()
  219. * newScale;
  220. if (scale != newScale || viewportArea != newArea)
  221. {
  222. scale = newScale;
  223. viewportArea = newArea;
  224. if (canTriggerUpdate)
  225. invalidateAll();
  226. }
  227. }
  228. }
  229. void bindVertexArray() noexcept
  230. {
  231. #if JUCE_OPENGL3
  232. if (vertexArrayObject != 0)
  233. context.extensions.glBindVertexArray (vertexArrayObject);
  234. #endif
  235. }
  236. void checkViewportBounds()
  237. {
  238. auto screenBounds = component.getTopLevelComponent()->getScreenBounds();
  239. if (lastScreenBounds != screenBounds)
  240. updateViewportSize (true);
  241. }
  242. void paintComponent()
  243. {
  244. // you mustn't set your own cached image object when attaching a GL context!
  245. jassert (get (component) == this);
  246. if (! ensureFrameBufferSize())
  247. return;
  248. RectangleList<int> invalid (viewportArea);
  249. invalid.subtract (validArea);
  250. validArea = viewportArea;
  251. if (! invalid.isEmpty())
  252. {
  253. clearRegionInFrameBuffer (invalid);
  254. {
  255. ScopedPointer<LowLevelGraphicsContext> g (createOpenGLGraphicsContext (context, cachedImageFrameBuffer));
  256. g->clipToRectangleList (invalid);
  257. g->addTransform (AffineTransform::scale ((float) scale));
  258. paintOwner (*g);
  259. JUCE_CHECK_OPENGL_ERROR
  260. }
  261. if (! context.isActive())
  262. context.makeActive();
  263. }
  264. JUCE_CHECK_OPENGL_ERROR
  265. }
  266. void drawComponentBuffer()
  267. {
  268. #if ! JUCE_ANDROID
  269. glEnable (GL_TEXTURE_2D);
  270. clearGLError();
  271. #endif
  272. #if JUCE_WINDOWS
  273. // some stupidly old drivers are missing this function, so try to at least avoid a crash here,
  274. // but if you hit this assertion you may want to have your own version check before using the
  275. // component rendering stuff on such old drivers.
  276. jassert (context.extensions.glActiveTexture != nullptr);
  277. if (context.extensions.glActiveTexture != nullptr)
  278. #endif
  279. context.extensions.glActiveTexture (GL_TEXTURE0);
  280. glBindTexture (GL_TEXTURE_2D, cachedImageFrameBuffer.getTextureID());
  281. bindVertexArray();
  282. const Rectangle<int> cacheBounds (cachedImageFrameBuffer.getWidth(), cachedImageFrameBuffer.getHeight());
  283. context.copyTexture (cacheBounds, cacheBounds, cacheBounds.getWidth(), cacheBounds.getHeight(), false);
  284. glBindTexture (GL_TEXTURE_2D, 0);
  285. JUCE_CHECK_OPENGL_ERROR
  286. }
  287. void paintOwner (LowLevelGraphicsContext& llgc)
  288. {
  289. Graphics g (llgc);
  290. #if JUCE_ENABLE_REPAINT_DEBUGGING
  291. #ifdef JUCE_IS_REPAINT_DEBUGGING_ACTIVE
  292. if (JUCE_IS_REPAINT_DEBUGGING_ACTIVE)
  293. #endif
  294. {
  295. g.saveState();
  296. }
  297. #endif
  298. JUCE_TRY
  299. {
  300. component.paintEntireComponent (g, false);
  301. }
  302. JUCE_CATCH_EXCEPTION
  303. #if JUCE_ENABLE_REPAINT_DEBUGGING
  304. #ifdef JUCE_IS_REPAINT_DEBUGGING_ACTIVE
  305. if (JUCE_IS_REPAINT_DEBUGGING_ACTIVE)
  306. #endif
  307. {
  308. // enabling this code will fill all areas that get repainted with a colour overlay, to show
  309. // clearly when things are being repainted.
  310. g.restoreState();
  311. static Random rng;
  312. g.fillAll (Colour ((uint8) rng.nextInt (255),
  313. (uint8) rng.nextInt (255),
  314. (uint8) rng.nextInt (255),
  315. (uint8) 0x50));
  316. }
  317. #endif
  318. }
  319. void handleResize()
  320. {
  321. updateViewportSize (true);
  322. #if JUCE_MAC
  323. if (hasInitialised)
  324. {
  325. [nativeContext->view update];
  326. renderFrame();
  327. }
  328. #endif
  329. }
  330. //==============================================================================
  331. JobStatus runJob() override
  332. {
  333. {
  334. MessageLockWorker worker (*this);
  335. // Allow the message thread to finish setting-up the context before using it..
  336. MessageManagerLock mml (worker);
  337. if (! mml.lockWasGained())
  338. return ThreadPoolJob::jobHasFinished;
  339. }
  340. initialiseOnThread();
  341. hasInitialised = true;
  342. while (! shouldExit())
  343. {
  344. #if JUCE_IOS
  345. if (backgroundProcessCheck.isBackgroundProcess())
  346. {
  347. repaintEvent.wait (300);
  348. continue;
  349. }
  350. #endif
  351. if (shouldExit())
  352. break;
  353. if (! renderFrame())
  354. repaintEvent.wait (5); // failed to render, so avoid a tight fail-loop.
  355. else if (! context.continuousRepaint && ! shouldExit())
  356. repaintEvent.wait (-1);
  357. }
  358. hasInitialised = false;
  359. context.makeActive();
  360. shutdownOnThread();
  361. OpenGLContext::deactivateCurrentContext();
  362. return ThreadPoolJob::jobHasFinished;
  363. }
  364. void initialiseOnThread()
  365. {
  366. // On android, this can get called twice, so drop any previous state..
  367. associatedObjectNames.clear();
  368. associatedObjects.clear();
  369. cachedImageFrameBuffer.release();
  370. context.makeActive();
  371. nativeContext->initialiseOnRenderThread (context);
  372. #if JUCE_ANDROID
  373. // On android the context may be created in initialiseOnRenderThread
  374. // and we therefore need to call makeActive again
  375. context.makeActive();
  376. #endif
  377. context.extensions.initialise();
  378. #if JUCE_OPENGL3
  379. if (OpenGLShaderProgram::getLanguageVersion() > 1.2)
  380. {
  381. context.extensions.glGenVertexArrays (1, &vertexArrayObject);
  382. bindVertexArray();
  383. }
  384. #endif
  385. glViewport (0, 0, component.getWidth(), component.getHeight());
  386. nativeContext->setSwapInterval (1);
  387. #if ! JUCE_OPENGL_ES
  388. JUCE_CHECK_OPENGL_ERROR
  389. shadersAvailable = OpenGLShaderProgram::getLanguageVersion() > 0;
  390. clearGLError();
  391. #endif
  392. if (context.renderer != nullptr)
  393. context.renderer->newOpenGLContextCreated();
  394. }
  395. void shutdownOnThread()
  396. {
  397. if (context.renderer != nullptr)
  398. context.renderer->openGLContextClosing();
  399. #if JUCE_OPENGL3
  400. if (vertexArrayObject != 0)
  401. context.extensions.glDeleteVertexArrays (1, &vertexArrayObject);
  402. #endif
  403. associatedObjectNames.clear();
  404. associatedObjects.clear();
  405. cachedImageFrameBuffer.release();
  406. nativeContext->shutdownOnRenderThread();
  407. }
  408. //==============================================================================
  409. struct MessageLockWorker : public MessageManagerLock::BailOutChecker
  410. {
  411. MessageLockWorker (CachedImage& cachedImageRequestingLock)
  412. : owner (cachedImageRequestingLock)
  413. {
  414. }
  415. bool shouldAbortAcquiringLock() override { return owner.doWorkWhileWaitingForLock (false); }
  416. CachedImage& owner;
  417. JUCE_DECLARE_NON_COPYABLE (MessageLockWorker)
  418. };
  419. //==============================================================================
  420. struct BlockingWorker : public OpenGLContext::AsyncWorker
  421. {
  422. BlockingWorker (OpenGLContext::AsyncWorker::Ptr && workerToUse)
  423. : originalWorker (static_cast<OpenGLContext::AsyncWorker::Ptr&&> (workerToUse))
  424. {}
  425. void operator() (OpenGLContext& calleeContext)
  426. {
  427. if (originalWorker != nullptr)
  428. (*originalWorker) (calleeContext);
  429. finishedSignal.signal();
  430. }
  431. void block() noexcept { finishedSignal.wait(); }
  432. OpenGLContext::AsyncWorker::Ptr originalWorker;
  433. WaitableEvent finishedSignal;
  434. };
  435. bool doWorkWhileWaitingForLock (bool contextIsAlreadyActive)
  436. {
  437. bool contextActivated = false;
  438. for (OpenGLContext::AsyncWorker::Ptr work = workQueue.removeAndReturn (0);
  439. work != nullptr && (! shouldExit()); work = workQueue.removeAndReturn (0))
  440. {
  441. if ((! contextActivated) && (! contextIsAlreadyActive))
  442. {
  443. if (! context.makeActive())
  444. break;
  445. contextActivated = true;
  446. }
  447. NativeContext::Locker locker (*nativeContext);
  448. (*work) (context);
  449. clearGLError();
  450. }
  451. if (contextActivated)
  452. OpenGLContext::deactivateCurrentContext();
  453. return shouldExit();
  454. }
  455. void execute (OpenGLContext::AsyncWorker::Ptr workerToUse, bool shouldBlock, bool calledFromDestructor = false)
  456. {
  457. if (calledFromDestructor || destroying.get() == 0)
  458. {
  459. BlockingWorker* blocker = (shouldBlock ? new BlockingWorker (static_cast<OpenGLContext::AsyncWorker::Ptr&&> (workerToUse)) : nullptr);
  460. OpenGLContext::AsyncWorker::Ptr worker = (blocker != nullptr ? blocker : static_cast<OpenGLContext::AsyncWorker::Ptr&&> (workerToUse));
  461. workQueue.add (worker);
  462. context.triggerRepaint();
  463. if (blocker != nullptr)
  464. blocker->block();
  465. }
  466. else
  467. {
  468. jassertfalse; // you called execute AFTER you detached your openglcontext
  469. }
  470. }
  471. //==============================================================================
  472. static CachedImage* get (Component& c) noexcept
  473. {
  474. return dynamic_cast<CachedImage*> (c.getCachedComponentImage());
  475. }
  476. //==============================================================================
  477. // used to push no work on to the gl thread to easily block
  478. struct DoNothingWorker : public OpenGLContext::AsyncWorker
  479. {
  480. DoNothingWorker() {}
  481. void operator() (OpenGLContext&) override {}
  482. };
  483. //==============================================================================
  484. friend class NativeContext;
  485. ScopedPointer<NativeContext> nativeContext;
  486. OpenGLContext& context;
  487. Component& component;
  488. OpenGLFrameBuffer cachedImageFrameBuffer;
  489. RectangleList<int> validArea;
  490. Rectangle<int> viewportArea, lastScreenBounds;
  491. double scale = 1.0;
  492. #if JUCE_OPENGL3
  493. GLuint vertexArrayObject = 0;
  494. #endif
  495. StringArray associatedObjectNames;
  496. ReferenceCountedArray<ReferenceCountedObject> associatedObjects;
  497. WaitableEvent canPaintNowFlag, finishedPaintingFlag, repaintEvent;
  498. #if JUCE_OPENGL_ES
  499. bool shadersAvailable = true;
  500. #else
  501. bool shadersAvailable = false;
  502. #endif
  503. bool hasInitialised = false;
  504. Atomic<int> needsUpdate { 1 }, destroying;
  505. uint32 lastMMLockReleaseTime = 0;
  506. ScopedPointer<ThreadPool> renderThread;
  507. ReferenceCountedArray<OpenGLContext::AsyncWorker, CriticalSection> workQueue;
  508. #if JUCE_IOS
  509. iOSBackgroundProcessCheck backgroundProcessCheck;
  510. #endif
  511. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (CachedImage)
  512. };
  513. //==============================================================================
  514. class OpenGLContext::Attachment : public ComponentMovementWatcher,
  515. private Timer
  516. {
  517. public:
  518. Attachment (OpenGLContext& c, Component& comp)
  519. : ComponentMovementWatcher (&comp), context (c)
  520. {
  521. if (canBeAttached (comp))
  522. attach();
  523. }
  524. ~Attachment()
  525. {
  526. detach();
  527. }
  528. void detach()
  529. {
  530. auto& comp = *getComponent();
  531. stop();
  532. comp.setCachedComponentImage (nullptr);
  533. context.nativeContext = nullptr;
  534. }
  535. void componentMovedOrResized (bool /*wasMoved*/, bool /*wasResized*/) override
  536. {
  537. auto& comp = *getComponent();
  538. if (isAttached (comp) != canBeAttached (comp))
  539. componentVisibilityChanged();
  540. if (comp.getWidth() > 0 && comp.getHeight() > 0
  541. && context.nativeContext != nullptr)
  542. {
  543. if (auto* c = CachedImage::get (comp))
  544. c->handleResize();
  545. if (auto* peer = comp.getTopLevelComponent()->getPeer())
  546. context.nativeContext->updateWindowPosition (peer->getAreaCoveredBy (comp));
  547. }
  548. }
  549. void componentPeerChanged() override
  550. {
  551. detach();
  552. componentVisibilityChanged();
  553. }
  554. void componentVisibilityChanged() override
  555. {
  556. auto& comp = *getComponent();
  557. if (canBeAttached (comp))
  558. {
  559. if (isAttached (comp))
  560. comp.repaint(); // (needed when windows are un-minimised)
  561. else
  562. attach();
  563. }
  564. else
  565. {
  566. detach();
  567. }
  568. }
  569. #if JUCE_DEBUG || JUCE_LOG_ASSERTIONS
  570. void componentBeingDeleted (Component& c) override
  571. {
  572. /* You must call detach() or delete your OpenGLContext to remove it
  573. from a component BEFORE deleting the component that it is using!
  574. */
  575. jassertfalse;
  576. ComponentMovementWatcher::componentBeingDeleted (c);
  577. }
  578. #endif
  579. void update()
  580. {
  581. auto& comp = *getComponent();
  582. if (canBeAttached (comp))
  583. start();
  584. else
  585. stop();
  586. }
  587. private:
  588. OpenGLContext& context;
  589. bool canBeAttached (const Component& comp) noexcept
  590. {
  591. return (! context.overrideCanAttach) && comp.getWidth() > 0 && comp.getHeight() > 0 && isShowingOrMinimised (comp);
  592. }
  593. static bool isShowingOrMinimised (const Component& c)
  594. {
  595. if (! c.isVisible())
  596. return false;
  597. if (auto* p = c.getParentComponent())
  598. return isShowingOrMinimised (*p);
  599. return c.getPeer() != nullptr;
  600. }
  601. static bool isAttached (const Component& comp) noexcept
  602. {
  603. return comp.getCachedComponentImage() != nullptr;
  604. }
  605. void attach()
  606. {
  607. auto& comp = *getComponent();
  608. auto* newCachedImage = new CachedImage (context, comp,
  609. context.openGLPixelFormat,
  610. context.contextToShareWith);
  611. comp.setCachedComponentImage (newCachedImage);
  612. start();
  613. }
  614. void stop()
  615. {
  616. stopTimer();
  617. auto& comp = *getComponent();
  618. #if JUCE_MAC
  619. [[(NSView*) comp.getWindowHandle() window] disableScreenUpdatesUntilFlush];
  620. #endif
  621. if (auto* oldCachedImage = CachedImage::get (comp))
  622. oldCachedImage->stop(); // (must stop this before detaching it from the component)
  623. }
  624. void start()
  625. {
  626. auto& comp = *getComponent();
  627. if (auto* cachedImage = CachedImage::get (comp))
  628. {
  629. cachedImage->start(); // (must wait until this is attached before starting its thread)
  630. cachedImage->updateViewportSize (true);
  631. startTimer (400);
  632. }
  633. }
  634. void timerCallback() override
  635. {
  636. if (auto* cachedImage = CachedImage::get (*getComponent()))
  637. cachedImage->checkViewportBounds();
  638. }
  639. };
  640. //==============================================================================
  641. OpenGLContext::OpenGLContext()
  642. {
  643. }
  644. OpenGLContext::~OpenGLContext()
  645. {
  646. detach();
  647. }
  648. void OpenGLContext::setRenderer (OpenGLRenderer* rendererToUse) noexcept
  649. {
  650. // This method must not be called when the context has already been attached!
  651. // Call it before attaching your context, or use detach() first, before calling this!
  652. jassert (nativeContext == nullptr);
  653. renderer = rendererToUse;
  654. }
  655. void OpenGLContext::setComponentPaintingEnabled (bool shouldPaintComponent) noexcept
  656. {
  657. // This method must not be called when the context has already been attached!
  658. // Call it before attaching your context, or use detach() first, before calling this!
  659. jassert (nativeContext == nullptr);
  660. renderComponents = shouldPaintComponent;
  661. }
  662. void OpenGLContext::setContinuousRepainting (bool shouldContinuouslyRepaint) noexcept
  663. {
  664. continuousRepaint = shouldContinuouslyRepaint;
  665. triggerRepaint();
  666. }
  667. void OpenGLContext::setPixelFormat (const OpenGLPixelFormat& preferredPixelFormat) noexcept
  668. {
  669. // This method must not be called when the context has already been attached!
  670. // Call it before attaching your context, or use detach() first, before calling this!
  671. jassert (nativeContext == nullptr);
  672. openGLPixelFormat = preferredPixelFormat;
  673. }
  674. void OpenGLContext::setNativeSharedContext (void* nativeContextToShareWith) noexcept
  675. {
  676. // This method must not be called when the context has already been attached!
  677. // Call it before attaching your context, or use detach() first, before calling this!
  678. jassert (nativeContext == nullptr);
  679. contextToShareWith = nativeContextToShareWith;
  680. }
  681. void OpenGLContext::setMultisamplingEnabled (bool b) noexcept
  682. {
  683. // This method must not be called when the context has already been attached!
  684. // Call it before attaching your context, or use detach() first, before calling this!
  685. jassert (nativeContext == nullptr);
  686. useMultisampling = b;
  687. }
  688. void OpenGLContext::setOpenGLVersionRequired (OpenGLVersion v) noexcept
  689. {
  690. versionRequired = v;
  691. }
  692. void OpenGLContext::attachTo (Component& component)
  693. {
  694. component.repaint();
  695. if (getTargetComponent() != &component)
  696. {
  697. detach();
  698. attachment = new Attachment (*this, component);
  699. }
  700. }
  701. void OpenGLContext::detach()
  702. {
  703. if (auto* a = attachment.get())
  704. {
  705. a->detach(); // must detach before nulling our pointer
  706. attachment = nullptr;
  707. }
  708. nativeContext = nullptr;
  709. }
  710. bool OpenGLContext::isAttached() const noexcept
  711. {
  712. return nativeContext != nullptr;
  713. }
  714. Component* OpenGLContext::getTargetComponent() const noexcept
  715. {
  716. return attachment != nullptr ? attachment->getComponent() : nullptr;
  717. }
  718. OpenGLContext* OpenGLContext::getContextAttachedTo (Component& c) noexcept
  719. {
  720. if (auto* ci = CachedImage::get (c))
  721. return &(ci->context);
  722. return nullptr;
  723. }
  724. static ThreadLocalValue<OpenGLContext*> currentThreadActiveContext;
  725. OpenGLContext* OpenGLContext::getCurrentContext()
  726. {
  727. return currentThreadActiveContext.get();
  728. }
  729. bool OpenGLContext::makeActive() const noexcept
  730. {
  731. auto& current = currentThreadActiveContext.get();
  732. if (nativeContext != nullptr && nativeContext->makeActive())
  733. {
  734. current = const_cast<OpenGLContext*> (this);
  735. return true;
  736. }
  737. current = nullptr;
  738. return false;
  739. }
  740. bool OpenGLContext::isActive() const noexcept
  741. {
  742. return nativeContext != nullptr && nativeContext->isActive();
  743. }
  744. void OpenGLContext::deactivateCurrentContext()
  745. {
  746. NativeContext::deactivateCurrentContext();
  747. currentThreadActiveContext.get() = nullptr;
  748. }
  749. void OpenGLContext::triggerRepaint()
  750. {
  751. if (auto* cachedImage = getCachedImage())
  752. cachedImage->triggerRepaint();
  753. }
  754. void OpenGLContext::swapBuffers()
  755. {
  756. if (nativeContext != nullptr)
  757. nativeContext->swapBuffers();
  758. }
  759. unsigned int OpenGLContext::getFrameBufferID() const noexcept
  760. {
  761. return nativeContext != nullptr ? nativeContext->getFrameBufferID() : 0;
  762. }
  763. bool OpenGLContext::setSwapInterval (int numFramesPerSwap)
  764. {
  765. return nativeContext != nullptr && nativeContext->setSwapInterval (numFramesPerSwap);
  766. }
  767. int OpenGLContext::getSwapInterval() const
  768. {
  769. return nativeContext != nullptr ? nativeContext->getSwapInterval() : 0;
  770. }
  771. void* OpenGLContext::getRawContext() const noexcept
  772. {
  773. return nativeContext != nullptr ? nativeContext->getRawContext() : nullptr;
  774. }
  775. OpenGLContext::CachedImage* OpenGLContext::getCachedImage() const noexcept
  776. {
  777. if (auto* comp = getTargetComponent())
  778. return CachedImage::get (*comp);
  779. return nullptr;
  780. }
  781. bool OpenGLContext::areShadersAvailable() const
  782. {
  783. auto* c = getCachedImage();
  784. return c != nullptr && c->shadersAvailable;
  785. }
  786. ReferenceCountedObject* OpenGLContext::getAssociatedObject (const char* name) const
  787. {
  788. jassert (name != nullptr);
  789. auto* c = getCachedImage();
  790. // This method must only be called from an openGL rendering callback.
  791. jassert (c != nullptr && nativeContext != nullptr);
  792. jassert (getCurrentContext() != nullptr);
  793. const int index = c->associatedObjectNames.indexOf (name);
  794. return index >= 0 ? c->associatedObjects.getUnchecked (index) : nullptr;
  795. }
  796. void OpenGLContext::setAssociatedObject (const char* name, ReferenceCountedObject* newObject)
  797. {
  798. jassert (name != nullptr);
  799. if (auto* c = getCachedImage())
  800. {
  801. // This method must only be called from an openGL rendering callback.
  802. jassert (nativeContext != nullptr);
  803. jassert (getCurrentContext() != nullptr);
  804. const int index = c->associatedObjectNames.indexOf (name);
  805. if (index >= 0)
  806. {
  807. if (newObject != nullptr)
  808. {
  809. c->associatedObjects.set (index, newObject);
  810. }
  811. else
  812. {
  813. c->associatedObjectNames.remove (index);
  814. c->associatedObjects.remove (index);
  815. }
  816. }
  817. else if (newObject != nullptr)
  818. {
  819. c->associatedObjectNames.add (name);
  820. c->associatedObjects.add (newObject);
  821. }
  822. }
  823. }
  824. void OpenGLContext::setImageCacheSize (size_t newSize) noexcept { imageCacheMaxSize = newSize; }
  825. size_t OpenGLContext::getImageCacheSize() const noexcept { return imageCacheMaxSize; }
  826. void OpenGLContext::execute (OpenGLContext::AsyncWorker::Ptr workerToUse, bool shouldBlock)
  827. {
  828. if (auto* c = getCachedImage())
  829. c->execute (static_cast<OpenGLContext::AsyncWorker::Ptr&&> (workerToUse), shouldBlock);
  830. else
  831. jassertfalse; // You must have attached the context to a component
  832. }
  833. void OpenGLContext::overrideCanBeAttached (bool newCanAttach)
  834. {
  835. if (overrideCanAttach != newCanAttach)
  836. {
  837. overrideCanAttach = newCanAttach;
  838. if (auto* a = attachment.get())
  839. a->update();
  840. }
  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