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.

1205 lines
38KB

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