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.

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