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.

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