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.

1079 lines
34KB

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