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.

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