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.

1243 lines
37KB

  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. bool invalidateAll() override
  118. {
  119. validArea.clear();
  120. triggerRepaint();
  121. return false;
  122. }
  123. bool invalidate (const Rectangle<int>& area) override
  124. {
  125. validArea.subtract (area * scale);
  126. triggerRepaint();
  127. return false;
  128. }
  129. void releaseResources() override
  130. {
  131. stop();
  132. }
  133. void triggerRepaint()
  134. {
  135. needsUpdate = 1;
  136. repaintEvent.signal();
  137. }
  138. //==============================================================================
  139. bool ensureFrameBufferSize()
  140. {
  141. auto fbW = cachedImageFrameBuffer.getWidth();
  142. auto fbH = cachedImageFrameBuffer.getHeight();
  143. if (fbW != viewportArea.getWidth() || fbH != viewportArea.getHeight() || ! cachedImageFrameBuffer.isValid())
  144. {
  145. if (! cachedImageFrameBuffer.initialise (context, viewportArea.getWidth(), viewportArea.getHeight()))
  146. return false;
  147. validArea.clear();
  148. JUCE_CHECK_OPENGL_ERROR
  149. }
  150. return true;
  151. }
  152. void clearRegionInFrameBuffer (const RectangleList<int>& list)
  153. {
  154. glClearColor (0, 0, 0, 0);
  155. glEnable (GL_SCISSOR_TEST);
  156. auto previousFrameBufferTarget = OpenGLFrameBuffer::getCurrentFrameBufferTarget();
  157. cachedImageFrameBuffer.makeCurrentRenderingTarget();
  158. auto imageH = cachedImageFrameBuffer.getHeight();
  159. for (auto& r : list)
  160. {
  161. glScissor (r.getX(), imageH - r.getBottom(), r.getWidth(), r.getHeight());
  162. glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
  163. }
  164. glDisable (GL_SCISSOR_TEST);
  165. context.extensions.glBindFramebuffer (GL_FRAMEBUFFER, previousFrameBufferTarget);
  166. JUCE_CHECK_OPENGL_ERROR
  167. }
  168. bool renderFrame()
  169. {
  170. MessageManager::Lock::ScopedTryLockType mmLock (messageManagerLock, false);
  171. const bool isUpdating = needsUpdate.compareAndSetBool (0, 1);
  172. if (context.renderComponents && isUpdating)
  173. {
  174. // This avoids hogging the message thread when doing intensive rendering.
  175. if (lastMMLockReleaseTime + 1 >= Time::getMillisecondCounter())
  176. Thread::sleep (2);
  177. while (! shouldExit())
  178. {
  179. doWorkWhileWaitingForLock (false);
  180. if (mmLock.retryLock ())
  181. break;
  182. }
  183. if (shouldExit())
  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. messageManagerLock.exit();
  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 (auto* peer = component.getPeer())
  220. {
  221. lastScreenBounds = component.getTopLevelComponent()->getScreenBounds();
  222. auto newScale = Desktop::getInstance().getDisplays()
  223. .getDisplayContaining (lastScreenBounds.getCentre()).scale;
  224. auto 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. auto 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. // Allow the message thread to finish setting-up the context before using it..
  342. MessageManager::Lock::ScopedTryLockType mmLock (messageManagerLock, false);
  343. do
  344. {
  345. if (shouldExit())
  346. return ThreadPoolJob::jobHasFinished;
  347. } while (! mmLock.retryLock ());
  348. }
  349. initialiseOnThread();
  350. hasInitialised = true;
  351. while (! shouldExit())
  352. {
  353. #if JUCE_IOS
  354. if (backgroundProcessCheck.isBackgroundProcess())
  355. {
  356. repaintEvent.wait (300);
  357. continue;
  358. }
  359. #endif
  360. if (shouldExit())
  361. break;
  362. if (! renderFrame())
  363. repaintEvent.wait (5); // failed to render, so avoid a tight fail-loop.
  364. else if (! context.continuousRepaint && ! shouldExit())
  365. repaintEvent.wait (-1);
  366. }
  367. hasInitialised = false;
  368. context.makeActive();
  369. shutdownOnThread();
  370. OpenGLContext::deactivateCurrentContext();
  371. return ThreadPoolJob::jobHasFinished;
  372. }
  373. void initialiseOnThread()
  374. {
  375. // On android, this can get called twice, so drop any previous state..
  376. associatedObjectNames.clear();
  377. associatedObjects.clear();
  378. cachedImageFrameBuffer.release();
  379. context.makeActive();
  380. nativeContext->initialiseOnRenderThread (context);
  381. #if JUCE_ANDROID
  382. // On android the context may be created in initialiseOnRenderThread
  383. // and we therefore need to call makeActive again
  384. context.makeActive();
  385. #endif
  386. context.extensions.initialise();
  387. #if JUCE_OPENGL3
  388. if (OpenGLShaderProgram::getLanguageVersion() > 1.2)
  389. {
  390. context.extensions.glGenVertexArrays (1, &vertexArrayObject);
  391. bindVertexArray();
  392. }
  393. #endif
  394. glViewport (0, 0, component.getWidth(), component.getHeight());
  395. nativeContext->setSwapInterval (1);
  396. #if ! JUCE_OPENGL_ES
  397. JUCE_CHECK_OPENGL_ERROR
  398. shadersAvailable = OpenGLShaderProgram::getLanguageVersion() > 0;
  399. clearGLError();
  400. #endif
  401. if (context.renderer != nullptr)
  402. context.renderer->newOpenGLContextCreated();
  403. }
  404. void shutdownOnThread()
  405. {
  406. if (context.renderer != nullptr)
  407. context.renderer->openGLContextClosing();
  408. #if JUCE_OPENGL3
  409. if (vertexArrayObject != 0)
  410. context.extensions.glDeleteVertexArrays (1, &vertexArrayObject);
  411. #endif
  412. associatedObjectNames.clear();
  413. associatedObjects.clear();
  414. cachedImageFrameBuffer.release();
  415. nativeContext->shutdownOnRenderThread();
  416. }
  417. //==============================================================================
  418. struct BlockingWorker : public OpenGLContext::AsyncWorker
  419. {
  420. BlockingWorker (OpenGLContext::AsyncWorker::Ptr && workerToUse)
  421. : originalWorker (static_cast<OpenGLContext::AsyncWorker::Ptr&&> (workerToUse))
  422. {}
  423. void operator() (OpenGLContext& calleeContext)
  424. {
  425. if (originalWorker != nullptr)
  426. (*originalWorker) (calleeContext);
  427. finishedSignal.signal();
  428. }
  429. void block() noexcept { finishedSignal.wait(); }
  430. OpenGLContext::AsyncWorker::Ptr originalWorker;
  431. WaitableEvent finishedSignal;
  432. };
  433. bool doWorkWhileWaitingForLock (bool contextIsAlreadyActive)
  434. {
  435. bool contextActivated = false;
  436. for (OpenGLContext::AsyncWorker::Ptr work = workQueue.removeAndReturn (0);
  437. work != nullptr && (! shouldExit()); work = workQueue.removeAndReturn (0))
  438. {
  439. if ((! contextActivated) && (! contextIsAlreadyActive))
  440. {
  441. if (! context.makeActive())
  442. break;
  443. contextActivated = true;
  444. }
  445. NativeContext::Locker locker (*nativeContext);
  446. (*work) (context);
  447. clearGLError();
  448. }
  449. if (contextActivated)
  450. OpenGLContext::deactivateCurrentContext();
  451. return shouldExit();
  452. }
  453. void execute (OpenGLContext::AsyncWorker::Ptr workerToUse, bool shouldBlock, bool calledFromDestructor = false)
  454. {
  455. if (calledFromDestructor || destroying.get() == 0)
  456. {
  457. BlockingWorker* blocker = (shouldBlock ? new BlockingWorker (static_cast<OpenGLContext::AsyncWorker::Ptr&&> (workerToUse)) : nullptr);
  458. OpenGLContext::AsyncWorker::Ptr worker = (blocker != nullptr ? blocker : static_cast<OpenGLContext::AsyncWorker::Ptr&&> (workerToUse));
  459. workQueue.add (worker);
  460. messageManagerLock.abort();
  461. context.triggerRepaint();
  462. if (blocker != nullptr)
  463. blocker->block();
  464. }
  465. else
  466. {
  467. jassertfalse; // you called execute AFTER you detached your openglcontext
  468. }
  469. }
  470. //==============================================================================
  471. static CachedImage* get (Component& c) noexcept
  472. {
  473. return dynamic_cast<CachedImage*> (c.getCachedComponentImage());
  474. }
  475. //==============================================================================
  476. friend class NativeContext;
  477. ScopedPointer<NativeContext> nativeContext;
  478. OpenGLContext& context;
  479. Component& component;
  480. OpenGLFrameBuffer cachedImageFrameBuffer;
  481. RectangleList<int> validArea;
  482. Rectangle<int> viewportArea, lastScreenBounds;
  483. double scale = 1.0;
  484. #if JUCE_OPENGL3
  485. GLuint vertexArrayObject = 0;
  486. #endif
  487. StringArray associatedObjectNames;
  488. ReferenceCountedArray<ReferenceCountedObject> associatedObjects;
  489. WaitableEvent canPaintNowFlag, finishedPaintingFlag, repaintEvent;
  490. #if JUCE_OPENGL_ES
  491. bool shadersAvailable = true;
  492. #else
  493. bool shadersAvailable = false;
  494. #endif
  495. bool hasInitialised = false;
  496. Atomic<int> needsUpdate { 1 }, destroying;
  497. uint32 lastMMLockReleaseTime = 0;
  498. ScopedPointer<ThreadPool> renderThread;
  499. ReferenceCountedArray<OpenGLContext::AsyncWorker, CriticalSection> workQueue;
  500. MessageManager::Lock messageManagerLock;
  501. #if JUCE_IOS
  502. iOSBackgroundProcessCheck backgroundProcessCheck;
  503. #endif
  504. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (CachedImage)
  505. };
  506. //==============================================================================
  507. class OpenGLContext::Attachment : public ComponentMovementWatcher,
  508. private Timer
  509. {
  510. public:
  511. Attachment (OpenGLContext& c, Component& comp)
  512. : ComponentMovementWatcher (&comp), context (c)
  513. {
  514. if (canBeAttached (comp))
  515. attach();
  516. }
  517. ~Attachment()
  518. {
  519. detach();
  520. }
  521. void detach()
  522. {
  523. auto& comp = *getComponent();
  524. stop();
  525. comp.setCachedComponentImage (nullptr);
  526. context.nativeContext = nullptr;
  527. }
  528. void componentMovedOrResized (bool /*wasMoved*/, bool /*wasResized*/) override
  529. {
  530. auto& comp = *getComponent();
  531. if (isAttached (comp) != canBeAttached (comp))
  532. componentVisibilityChanged();
  533. if (comp.getWidth() > 0 && comp.getHeight() > 0
  534. && context.nativeContext != nullptr)
  535. {
  536. if (auto* c = CachedImage::get (comp))
  537. c->handleResize();
  538. if (auto* peer = comp.getTopLevelComponent()->getPeer())
  539. context.nativeContext->updateWindowPosition (peer->getAreaCoveredBy (comp));
  540. }
  541. }
  542. void componentPeerChanged() override
  543. {
  544. detach();
  545. componentVisibilityChanged();
  546. }
  547. void componentVisibilityChanged() override
  548. {
  549. auto& comp = *getComponent();
  550. if (canBeAttached (comp))
  551. {
  552. if (isAttached (comp))
  553. comp.repaint(); // (needed when windows are un-minimised)
  554. else
  555. attach();
  556. }
  557. else
  558. {
  559. detach();
  560. }
  561. }
  562. #if JUCE_DEBUG || JUCE_LOG_ASSERTIONS
  563. void componentBeingDeleted (Component& c) override
  564. {
  565. /* You must call detach() or delete your OpenGLContext to remove it
  566. from a component BEFORE deleting the component that it is using!
  567. */
  568. jassertfalse;
  569. ComponentMovementWatcher::componentBeingDeleted (c);
  570. }
  571. #endif
  572. void update()
  573. {
  574. auto& comp = *getComponent();
  575. if (canBeAttached (comp))
  576. start();
  577. else
  578. stop();
  579. }
  580. private:
  581. OpenGLContext& context;
  582. bool canBeAttached (const Component& comp) noexcept
  583. {
  584. return (! context.overrideCanAttach) && comp.getWidth() > 0 && comp.getHeight() > 0 && isShowingOrMinimised (comp);
  585. }
  586. static bool isShowingOrMinimised (const Component& c)
  587. {
  588. if (! c.isVisible())
  589. return false;
  590. if (auto* p = c.getParentComponent())
  591. return isShowingOrMinimised (*p);
  592. return c.getPeer() != nullptr;
  593. }
  594. static bool isAttached (const Component& comp) noexcept
  595. {
  596. return comp.getCachedComponentImage() != nullptr;
  597. }
  598. void attach()
  599. {
  600. auto& comp = *getComponent();
  601. auto* newCachedImage = new CachedImage (context, comp,
  602. context.openGLPixelFormat,
  603. context.contextToShareWith);
  604. comp.setCachedComponentImage (newCachedImage);
  605. start();
  606. }
  607. void stop()
  608. {
  609. stopTimer();
  610. auto& comp = *getComponent();
  611. #if JUCE_MAC
  612. [[(NSView*) comp.getWindowHandle() window] disableScreenUpdatesUntilFlush];
  613. #endif
  614. if (auto* oldCachedImage = CachedImage::get (comp))
  615. oldCachedImage->stop(); // (must stop this before detaching it from the component)
  616. }
  617. void start()
  618. {
  619. auto& comp = *getComponent();
  620. if (auto* cachedImage = CachedImage::get (comp))
  621. {
  622. cachedImage->start(); // (must wait until this is attached before starting its thread)
  623. cachedImage->updateViewportSize (true);
  624. startTimer (400);
  625. }
  626. }
  627. void timerCallback() override
  628. {
  629. if (auto* cachedImage = CachedImage::get (*getComponent()))
  630. cachedImage->checkViewportBounds();
  631. }
  632. };
  633. //==============================================================================
  634. OpenGLContext::OpenGLContext()
  635. {
  636. }
  637. OpenGLContext::~OpenGLContext()
  638. {
  639. detach();
  640. }
  641. void OpenGLContext::setRenderer (OpenGLRenderer* rendererToUse) noexcept
  642. {
  643. // This method must not be called when the context has already been attached!
  644. // Call it before attaching your context, or use detach() first, before calling this!
  645. jassert (nativeContext == nullptr);
  646. renderer = rendererToUse;
  647. }
  648. void OpenGLContext::setComponentPaintingEnabled (bool shouldPaintComponent) noexcept
  649. {
  650. // This method must not be called when the context has already been attached!
  651. // Call it before attaching your context, or use detach() first, before calling this!
  652. jassert (nativeContext == nullptr);
  653. renderComponents = shouldPaintComponent;
  654. }
  655. void OpenGLContext::setContinuousRepainting (bool shouldContinuouslyRepaint) noexcept
  656. {
  657. continuousRepaint = shouldContinuouslyRepaint;
  658. triggerRepaint();
  659. }
  660. void OpenGLContext::setPixelFormat (const OpenGLPixelFormat& preferredPixelFormat) noexcept
  661. {
  662. // This method must not be called when the context has already been attached!
  663. // Call it before attaching your context, or use detach() first, before calling this!
  664. jassert (nativeContext == nullptr);
  665. openGLPixelFormat = preferredPixelFormat;
  666. }
  667. void OpenGLContext::setTextureMagnificationFilter (OpenGLContext::TextureMagnificationFilter magFilterMode) noexcept
  668. {
  669. texMagFilter = magFilterMode;
  670. }
  671. void OpenGLContext::setNativeSharedContext (void* nativeContextToShareWith) noexcept
  672. {
  673. // This method must not be called when the context has already been attached!
  674. // Call it before attaching your context, or use detach() first, before calling this!
  675. jassert (nativeContext == nullptr);
  676. contextToShareWith = nativeContextToShareWith;
  677. }
  678. void OpenGLContext::setMultisamplingEnabled (bool b) noexcept
  679. {
  680. // This method must not be called when the context has already been attached!
  681. // Call it before attaching your context, or use detach() first, before calling this!
  682. jassert (nativeContext == nullptr);
  683. useMultisampling = b;
  684. }
  685. void OpenGLContext::setOpenGLVersionRequired (OpenGLVersion v) noexcept
  686. {
  687. versionRequired = v;
  688. }
  689. void OpenGLContext::attachTo (Component& component)
  690. {
  691. component.repaint();
  692. if (getTargetComponent() != &component)
  693. {
  694. detach();
  695. attachment = new Attachment (*this, component);
  696. }
  697. }
  698. void OpenGLContext::detach()
  699. {
  700. if (auto* a = attachment.get())
  701. {
  702. a->detach(); // must detach before nulling our pointer
  703. attachment.reset();
  704. }
  705. nativeContext = nullptr;
  706. }
  707. bool OpenGLContext::isAttached() const noexcept
  708. {
  709. return nativeContext != nullptr;
  710. }
  711. Component* OpenGLContext::getTargetComponent() const noexcept
  712. {
  713. return attachment != nullptr ? attachment->getComponent() : nullptr;
  714. }
  715. OpenGLContext* OpenGLContext::getContextAttachedTo (Component& c) noexcept
  716. {
  717. if (auto* ci = CachedImage::get (c))
  718. return &(ci->context);
  719. return nullptr;
  720. }
  721. static ThreadLocalValue<OpenGLContext*> currentThreadActiveContext;
  722. OpenGLContext* OpenGLContext::getCurrentContext()
  723. {
  724. return currentThreadActiveContext.get();
  725. }
  726. bool OpenGLContext::makeActive() const noexcept
  727. {
  728. auto& current = currentThreadActiveContext.get();
  729. if (nativeContext != nullptr && nativeContext->makeActive())
  730. {
  731. current = const_cast<OpenGLContext*> (this);
  732. return true;
  733. }
  734. current = nullptr;
  735. return false;
  736. }
  737. bool OpenGLContext::isActive() const noexcept
  738. {
  739. return nativeContext != nullptr && nativeContext->isActive();
  740. }
  741. void OpenGLContext::deactivateCurrentContext()
  742. {
  743. NativeContext::deactivateCurrentContext();
  744. currentThreadActiveContext.get() = nullptr;
  745. }
  746. void OpenGLContext::triggerRepaint()
  747. {
  748. if (auto* cachedImage = getCachedImage())
  749. cachedImage->triggerRepaint();
  750. }
  751. void OpenGLContext::swapBuffers()
  752. {
  753. if (nativeContext != nullptr)
  754. nativeContext->swapBuffers();
  755. }
  756. unsigned int OpenGLContext::getFrameBufferID() const noexcept
  757. {
  758. return nativeContext != nullptr ? nativeContext->getFrameBufferID() : 0;
  759. }
  760. bool OpenGLContext::setSwapInterval (int numFramesPerSwap)
  761. {
  762. return nativeContext != nullptr && nativeContext->setSwapInterval (numFramesPerSwap);
  763. }
  764. int OpenGLContext::getSwapInterval() const
  765. {
  766. return nativeContext != nullptr ? nativeContext->getSwapInterval() : 0;
  767. }
  768. void* OpenGLContext::getRawContext() const noexcept
  769. {
  770. return nativeContext != nullptr ? nativeContext->getRawContext() : nullptr;
  771. }
  772. OpenGLContext::CachedImage* OpenGLContext::getCachedImage() const noexcept
  773. {
  774. if (auto* comp = getTargetComponent())
  775. return CachedImage::get (*comp);
  776. return nullptr;
  777. }
  778. bool OpenGLContext::areShadersAvailable() const
  779. {
  780. auto* c = getCachedImage();
  781. return c != nullptr && c->shadersAvailable;
  782. }
  783. ReferenceCountedObject* OpenGLContext::getAssociatedObject (const char* name) const
  784. {
  785. jassert (name != nullptr);
  786. auto* c = getCachedImage();
  787. // This method must only be called from an openGL rendering callback.
  788. jassert (c != nullptr && nativeContext != nullptr);
  789. jassert (getCurrentContext() != nullptr);
  790. const int index = c->associatedObjectNames.indexOf (name);
  791. return index >= 0 ? c->associatedObjects.getUnchecked (index) : nullptr;
  792. }
  793. void OpenGLContext::setAssociatedObject (const char* name, ReferenceCountedObject* newObject)
  794. {
  795. jassert (name != nullptr);
  796. if (auto* c = getCachedImage())
  797. {
  798. // This method must only be called from an openGL rendering callback.
  799. jassert (nativeContext != nullptr);
  800. jassert (getCurrentContext() != nullptr);
  801. const int index = c->associatedObjectNames.indexOf (name);
  802. if (index >= 0)
  803. {
  804. if (newObject != nullptr)
  805. {
  806. c->associatedObjects.set (index, newObject);
  807. }
  808. else
  809. {
  810. c->associatedObjectNames.remove (index);
  811. c->associatedObjects.remove (index);
  812. }
  813. }
  814. else if (newObject != nullptr)
  815. {
  816. c->associatedObjectNames.add (name);
  817. c->associatedObjects.add (newObject);
  818. }
  819. }
  820. }
  821. void OpenGLContext::setImageCacheSize (size_t newSize) noexcept { imageCacheMaxSize = newSize; }
  822. size_t OpenGLContext::getImageCacheSize() const noexcept { return imageCacheMaxSize; }
  823. void OpenGLContext::execute (OpenGLContext::AsyncWorker::Ptr workerToUse, bool shouldBlock)
  824. {
  825. if (auto* c = getCachedImage())
  826. c->execute (static_cast<OpenGLContext::AsyncWorker::Ptr&&> (workerToUse), shouldBlock);
  827. else
  828. jassertfalse; // You must have attached the context to a component
  829. }
  830. void OpenGLContext::overrideCanBeAttached (bool newCanAttach)
  831. {
  832. if (overrideCanAttach != newCanAttach)
  833. {
  834. overrideCanAttach = newCanAttach;
  835. if (auto* a = attachment.get())
  836. a->update();
  837. }
  838. }
  839. //==============================================================================
  840. struct DepthTestDisabler
  841. {
  842. DepthTestDisabler() noexcept
  843. {
  844. glGetBooleanv (GL_DEPTH_TEST, &wasEnabled);
  845. if (wasEnabled)
  846. glDisable (GL_DEPTH_TEST);
  847. }
  848. ~DepthTestDisabler() noexcept
  849. {
  850. if (wasEnabled)
  851. glEnable (GL_DEPTH_TEST);
  852. }
  853. GLboolean wasEnabled;
  854. };
  855. //==============================================================================
  856. void OpenGLContext::copyTexture (const Rectangle<int>& targetClipArea,
  857. const Rectangle<int>& anchorPosAndTextureSize,
  858. const int contextWidth, const int contextHeight,
  859. bool flippedVertically)
  860. {
  861. if (contextWidth <= 0 || contextHeight <= 0)
  862. return;
  863. JUCE_CHECK_OPENGL_ERROR
  864. glBlendFunc (GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
  865. glEnable (GL_BLEND);
  866. DepthTestDisabler depthDisabler;
  867. if (areShadersAvailable())
  868. {
  869. struct OverlayShaderProgram : public ReferenceCountedObject
  870. {
  871. OverlayShaderProgram (OpenGLContext& context)
  872. : program (context), builder (program), params (program)
  873. {}
  874. static const OverlayShaderProgram& select (OpenGLContext& context)
  875. {
  876. static const char programValueID[] = "juceGLComponentOverlayShader";
  877. OverlayShaderProgram* program = static_cast<OverlayShaderProgram*> (context.getAssociatedObject (programValueID));
  878. if (program == nullptr)
  879. {
  880. program = new OverlayShaderProgram (context);
  881. context.setAssociatedObject (programValueID, program);
  882. }
  883. program->program.use();
  884. return *program;
  885. }
  886. struct ProgramBuilder
  887. {
  888. ProgramBuilder (OpenGLShaderProgram& prog)
  889. {
  890. prog.addVertexShader (OpenGLHelpers::translateVertexShaderToV3 (
  891. "attribute " JUCE_HIGHP " vec2 position;"
  892. "uniform " JUCE_HIGHP " vec2 screenSize;"
  893. "uniform " JUCE_HIGHP " float textureBounds[4];"
  894. "uniform " JUCE_HIGHP " vec2 vOffsetAndScale;"
  895. "varying " JUCE_HIGHP " vec2 texturePos;"
  896. "void main()"
  897. "{"
  898. JUCE_HIGHP " vec2 scaled = position / (0.5 * screenSize.xy);"
  899. "gl_Position = vec4 (scaled.x - 1.0, 1.0 - scaled.y, 0, 1.0);"
  900. "texturePos = (position - vec2 (textureBounds[0], textureBounds[1])) / vec2 (textureBounds[2], textureBounds[3]);"
  901. "texturePos = vec2 (texturePos.x, vOffsetAndScale.x + vOffsetAndScale.y * texturePos.y);"
  902. "}"));
  903. prog.addFragmentShader (OpenGLHelpers::translateFragmentShaderToV3 (
  904. "uniform sampler2D imageTexture;"
  905. "varying " JUCE_HIGHP " vec2 texturePos;"
  906. "void main()"
  907. "{"
  908. "gl_FragColor = texture2D (imageTexture, texturePos);"
  909. "}"));
  910. prog.link();
  911. }
  912. };
  913. struct Params
  914. {
  915. Params (OpenGLShaderProgram& prog)
  916. : positionAttribute (prog, "position"),
  917. screenSize (prog, "screenSize"),
  918. imageTexture (prog, "imageTexture"),
  919. textureBounds (prog, "textureBounds"),
  920. vOffsetAndScale (prog, "vOffsetAndScale")
  921. {}
  922. void set (const float targetWidth, const float targetHeight, const Rectangle<float>& bounds, bool flipVertically) const
  923. {
  924. const GLfloat m[] = { bounds.getX(), bounds.getY(), bounds.getWidth(), bounds.getHeight() };
  925. textureBounds.set (m, 4);
  926. imageTexture.set (0);
  927. screenSize.set (targetWidth, targetHeight);
  928. vOffsetAndScale.set (flipVertically ? 0.0f : 1.0f,
  929. flipVertically ? 1.0f : -1.0f);
  930. }
  931. OpenGLShaderProgram::Attribute positionAttribute;
  932. OpenGLShaderProgram::Uniform screenSize, imageTexture, textureBounds, vOffsetAndScale;
  933. };
  934. OpenGLShaderProgram program;
  935. ProgramBuilder builder;
  936. Params params;
  937. };
  938. auto left = (GLshort) targetClipArea.getX();
  939. auto top = (GLshort) targetClipArea.getY();
  940. auto right = (GLshort) targetClipArea.getRight();
  941. auto bottom = (GLshort) targetClipArea.getBottom();
  942. const GLshort vertices[] = { left, bottom, right, bottom, left, top, right, top };
  943. auto& program = OverlayShaderProgram::select (*this);
  944. program.params.set ((float) contextWidth, (float) contextHeight, anchorPosAndTextureSize.toFloat(), flippedVertically);
  945. GLuint vertexBuffer = 0;
  946. extensions.glGenBuffers (1, &vertexBuffer);
  947. extensions.glBindBuffer (GL_ARRAY_BUFFER, vertexBuffer);
  948. extensions.glBufferData (GL_ARRAY_BUFFER, sizeof (vertices), vertices, GL_STATIC_DRAW);
  949. auto index = (GLuint) program.params.positionAttribute.attributeID;
  950. extensions.glVertexAttribPointer (index, 2, GL_SHORT, GL_FALSE, 4, 0);
  951. extensions.glEnableVertexAttribArray (index);
  952. JUCE_CHECK_OPENGL_ERROR
  953. glDrawArrays (GL_TRIANGLE_STRIP, 0, 4);
  954. extensions.glBindBuffer (GL_ARRAY_BUFFER, 0);
  955. extensions.glUseProgram (0);
  956. extensions.glDisableVertexAttribArray (index);
  957. extensions.glDeleteBuffers (1, &vertexBuffer);
  958. }
  959. else
  960. {
  961. jassert (attachment == nullptr); // Running on an old graphics card!
  962. }
  963. JUCE_CHECK_OPENGL_ERROR
  964. }
  965. #if JUCE_ANDROID
  966. EGLDisplay OpenGLContext::NativeContext::display = EGL_NO_DISPLAY;
  967. EGLDisplay OpenGLContext::NativeContext::config;
  968. void OpenGLContext::NativeContext::surfaceCreated (jobject holder)
  969. {
  970. ignoreUnused (holder);
  971. if (auto* cachedImage = CachedImage::get (component))
  972. {
  973. if (auto* pool = cachedImage->renderThread.get())
  974. {
  975. if (! pool->contains (cachedImage))
  976. {
  977. cachedImage->resume();
  978. cachedImage->context.triggerRepaint();
  979. }
  980. }
  981. }
  982. }
  983. void OpenGLContext::NativeContext::surfaceDestroyed (jobject holder)
  984. {
  985. ignoreUnused (holder);
  986. // unlike the name suggests this will be called just before the
  987. // surface is destroyed. We need to pause the render thread.
  988. if (auto* cachedImage = CachedImage::get (component))
  989. {
  990. cachedImage->pause();
  991. if (auto* threadPool = cachedImage->renderThread.get())
  992. threadPool->waitForJobToFinish (cachedImage, -1);
  993. }
  994. }
  995. #endif
  996. } // namespace juce