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.

816 lines
27KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-11 by Raw Material Software Ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the GNU General
  7. Public License (Version 2), as published by the Free Software Foundation.
  8. A copy of the license is included in the JUCE distribution, or can be found
  9. online at www.gnu.org/licenses.
  10. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  13. ------------------------------------------------------------------------------
  14. To release a closed-source product which uses JUCE, commercial licenses are
  15. available: visit www.rawmaterialsoftware.com/juce for more information.
  16. ==============================================================================
  17. */
  18. class OpenGLContext::CachedImage : public CachedComponentImage,
  19. public Thread
  20. {
  21. public:
  22. CachedImage (OpenGLContext& c, Component& comp,
  23. const OpenGLPixelFormat& pixFormat, void* contextToShare)
  24. : Thread ("OpenGL Rendering"),
  25. context (c), component (comp),
  26. scale (1.0),
  27. #if JUCE_OPENGL_ES
  28. shadersAvailable (true),
  29. #else
  30. shadersAvailable (false),
  31. #endif
  32. needsUpdate (1)
  33. {
  34. nativeContext = new NativeContext (component, pixFormat, contextToShare);
  35. if (nativeContext->createdOk())
  36. context.nativeContext = nativeContext;
  37. else
  38. nativeContext = nullptr;
  39. }
  40. ~CachedImage()
  41. {
  42. stop();
  43. }
  44. void start()
  45. {
  46. #if ! JUCE_ANDROID
  47. if (nativeContext != nullptr)
  48. startThread (6);
  49. #endif
  50. }
  51. void stop()
  52. {
  53. #if ! JUCE_ANDROID
  54. stopThread (10000);
  55. #endif
  56. }
  57. //==============================================================================
  58. void paint (Graphics&)
  59. {
  60. if (ComponentPeer* const peer = component.getPeer())
  61. peer->addMaskedRegion (peer->getComponent().getLocalArea (&component, component.getLocalBounds()));
  62. }
  63. void invalidateAll()
  64. {
  65. validArea.clear();
  66. triggerRepaint();
  67. }
  68. void invalidate (const Rectangle<int>& area)
  69. {
  70. validArea.subtract (area);
  71. triggerRepaint();
  72. }
  73. void releaseResources() {}
  74. void triggerRepaint()
  75. {
  76. needsUpdate = 1;
  77. #if JUCE_ANDROID
  78. if (nativeContext != nullptr)
  79. nativeContext->triggerRepaint();
  80. #else
  81. notify();
  82. #endif
  83. }
  84. //==============================================================================
  85. bool ensureFrameBufferSize()
  86. {
  87. const int fbW = cachedImageFrameBuffer.getWidth();
  88. const int fbH = cachedImageFrameBuffer.getHeight();
  89. if (fbW != viewportArea.getWidth() || fbH != viewportArea.getHeight() || ! cachedImageFrameBuffer.isValid())
  90. {
  91. if (! cachedImageFrameBuffer.initialise (context, viewportArea.getWidth(), viewportArea.getHeight()))
  92. return false;
  93. validArea.clear();
  94. JUCE_CHECK_OPENGL_ERROR
  95. }
  96. return true;
  97. }
  98. void clearRegionInFrameBuffer (const RectangleList& list, const float scaleFactor)
  99. {
  100. glClearColor (0, 0, 0, 0);
  101. glEnable (GL_SCISSOR_TEST);
  102. const GLuint previousFrameBufferTarget = OpenGLFrameBuffer::getCurrentFrameBufferTarget();
  103. cachedImageFrameBuffer.makeCurrentRenderingTarget();
  104. const int imageH = cachedImageFrameBuffer.getHeight();
  105. for (const Rectangle<int>* i = list.begin(), * const e = list.end(); i != e; ++i)
  106. {
  107. const Rectangle<int> r ((i->toFloat() * scaleFactor).getSmallestIntegerContainer());
  108. glScissor (r.getX(), imageH - r.getBottom(), r.getWidth(), r.getHeight());
  109. glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
  110. }
  111. glDisable (GL_SCISSOR_TEST);
  112. context.extensions.glBindFramebuffer (GL_FRAMEBUFFER, previousFrameBufferTarget);
  113. JUCE_CHECK_OPENGL_ERROR
  114. }
  115. bool renderFrame()
  116. {
  117. ScopedPointer<MessageManagerLock> mmLock;
  118. const bool isUpdating = needsUpdate.compareAndSetBool (0, 1);
  119. if (context.renderComponents && isUpdating)
  120. {
  121. mmLock = new MessageManagerLock (this); // need to acquire this before locking the context.
  122. if (! mmLock->lockWasGained())
  123. return false;
  124. }
  125. if (! context.makeActive())
  126. return false;
  127. NativeContext::Locker locker (*nativeContext);
  128. JUCE_CHECK_OPENGL_ERROR
  129. if (context.renderer != nullptr)
  130. {
  131. glViewport (0, 0, viewportArea.getWidth(), viewportArea.getHeight());
  132. context.renderer->renderOpenGL();
  133. clearGLError();
  134. }
  135. if (context.renderComponents)
  136. {
  137. if (isUpdating)
  138. {
  139. paintComponent();
  140. mmLock = nullptr;
  141. }
  142. glViewport (0, 0, viewportArea.getWidth(), viewportArea.getHeight());
  143. drawComponentBuffer();
  144. }
  145. context.swapBuffers();
  146. return true;
  147. }
  148. void updateViewportSize (bool canTriggerUpdate)
  149. {
  150. const double newScale = Desktop::getInstance().getDisplays()
  151. .getDisplayContaining (component.getScreenBounds().getCentre()).scale;
  152. Rectangle<int> newArea (roundToInt (component.getWidth() * newScale),
  153. roundToInt (component.getHeight() * newScale));
  154. if (scale != newScale || viewportArea != newArea)
  155. {
  156. scale = newScale;
  157. viewportArea = newArea;
  158. if (canTriggerUpdate)
  159. invalidateAll();
  160. }
  161. }
  162. void paintComponent()
  163. {
  164. // you mustn't set your own cached image object when attaching a GL context!
  165. jassert (get (component) == this);
  166. updateViewportSize (false);
  167. if (! ensureFrameBufferSize())
  168. return;
  169. RectangleList invalid (viewportArea);
  170. invalid.subtract (validArea);
  171. validArea = viewportArea;
  172. if (! invalid.isEmpty())
  173. {
  174. clearRegionInFrameBuffer (invalid, (float) scale);
  175. {
  176. ScopedPointer<LowLevelGraphicsContext> g (createOpenGLGraphicsContext (context, cachedImageFrameBuffer));
  177. g->addTransform (AffineTransform::scale ((float) scale));
  178. g->clipToRectangleList (invalid);
  179. paintOwner (*g);
  180. JUCE_CHECK_OPENGL_ERROR
  181. }
  182. if (! context.isActive())
  183. context.makeActive();
  184. }
  185. JUCE_CHECK_OPENGL_ERROR
  186. }
  187. void drawComponentBuffer()
  188. {
  189. #if ! JUCE_ANDROID
  190. glEnable (GL_TEXTURE_2D);
  191. clearGLError();
  192. #endif
  193. context.extensions.glActiveTexture (GL_TEXTURE0);
  194. glBindTexture (GL_TEXTURE_2D, cachedImageFrameBuffer.getTextureID());
  195. const Rectangle<int> cacheBounds (cachedImageFrameBuffer.getWidth(), cachedImageFrameBuffer.getHeight());
  196. context.copyTexture (cacheBounds, cacheBounds, cacheBounds.getWidth(), cacheBounds.getHeight(), false);
  197. glBindTexture (GL_TEXTURE_2D, 0);
  198. JUCE_CHECK_OPENGL_ERROR
  199. }
  200. void paintOwner (LowLevelGraphicsContext& llgc)
  201. {
  202. Graphics g (&llgc);
  203. #if JUCE_ENABLE_REPAINT_DEBUGGING
  204. g.saveState();
  205. #endif
  206. JUCE_TRY
  207. {
  208. component.paintEntireComponent (g, false);
  209. }
  210. JUCE_CATCH_EXCEPTION
  211. #if JUCE_ENABLE_REPAINT_DEBUGGING
  212. // enabling this code will fill all areas that get repainted with a colour overlay, to show
  213. // clearly when things are being repainted.
  214. g.restoreState();
  215. static Random rng;
  216. g.fillAll (Colour ((uint8) rng.nextInt (255),
  217. (uint8) rng.nextInt (255),
  218. (uint8) rng.nextInt (255),
  219. (uint8) 0x50));
  220. #endif
  221. }
  222. //==============================================================================
  223. void run()
  224. {
  225. {
  226. // Allow the message thread to finish setting-up the context before using it..
  227. MessageManagerLock mml (this);
  228. if (! mml.lockWasGained())
  229. return;
  230. }
  231. nativeContext->makeActive();
  232. nativeContext->setSwapInterval (1);
  233. initialiseOnThread();
  234. #if JUCE_USE_OPENGL_SHADERS && ! JUCE_OPENGL_ES
  235. shadersAvailable = OpenGLShaderProgram::getLanguageVersion() > 0;
  236. #endif
  237. while (! threadShouldExit())
  238. {
  239. if (! renderFrame())
  240. wait (5); // failed to render, so avoid a tight fail-loop.
  241. }
  242. shutdownOnThread();
  243. }
  244. void initialiseOnThread()
  245. {
  246. associatedObjectNames.clear();
  247. associatedObjects.clear();
  248. cachedImageFrameBuffer.release();
  249. nativeContext->initialiseOnRenderThread();
  250. glViewport (0, 0, component.getWidth(), component.getHeight());
  251. context.extensions.initialise();
  252. if (context.renderer != nullptr)
  253. context.renderer->newOpenGLContextCreated();
  254. }
  255. void shutdownOnThread()
  256. {
  257. if (context.renderer != nullptr)
  258. context.renderer->openGLContextClosing();
  259. cachedImageFrameBuffer.release();
  260. nativeContext->shutdownOnRenderThread();
  261. associatedObjectNames.clear();
  262. associatedObjects.clear();
  263. }
  264. //==============================================================================
  265. static CachedImage* get (Component& c) noexcept
  266. {
  267. return dynamic_cast<CachedImage*> (c.getCachedComponentImage());
  268. }
  269. //==============================================================================
  270. ScopedPointer<NativeContext> nativeContext;
  271. OpenGLContext& context;
  272. Component& component;
  273. OpenGLFrameBuffer cachedImageFrameBuffer;
  274. RectangleList validArea;
  275. Rectangle<int> viewportArea;
  276. double scale;
  277. StringArray associatedObjectNames;
  278. ReferenceCountedArray<ReferenceCountedObject> associatedObjects;
  279. WaitableEvent canPaintNowFlag, finishedPaintingFlag;
  280. bool shadersAvailable;
  281. Atomic<int> needsUpdate;
  282. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (CachedImage)
  283. };
  284. //==============================================================================
  285. #if JUCE_ANDROID
  286. void OpenGLContext::NativeContext::contextCreatedCallback()
  287. {
  288. isInsideGLCallback = true;
  289. if (CachedImage* const c = CachedImage::get (component))
  290. c->initialiseOnThread();
  291. else
  292. jassertfalse;
  293. isInsideGLCallback = false;
  294. }
  295. void OpenGLContext::NativeContext::renderCallback()
  296. {
  297. isInsideGLCallback = true;
  298. if (CachedImage* const c = CachedImage::get (component))
  299. c->renderFrame();
  300. isInsideGLCallback = false;
  301. }
  302. #endif
  303. //==============================================================================
  304. class OpenGLContext::Attachment : public ComponentMovementWatcher
  305. {
  306. public:
  307. Attachment (OpenGLContext& c, Component& comp)
  308. : ComponentMovementWatcher (&comp), context (c)
  309. {
  310. if (canBeAttached (comp))
  311. attach();
  312. }
  313. ~Attachment()
  314. {
  315. detach();
  316. }
  317. void componentMovedOrResized (bool /*wasMoved*/, bool /*wasResized*/)
  318. {
  319. Component& comp = *getComponent();
  320. if (isAttached (comp) != canBeAttached (comp))
  321. componentVisibilityChanged();
  322. if (comp.getWidth() > 0 && comp.getHeight() > 0
  323. && context.nativeContext != nullptr)
  324. {
  325. if (CachedImage* const c = CachedImage::get (comp))
  326. c->updateViewportSize (true);
  327. context.nativeContext->updateWindowPosition (comp.getTopLevelComponent()
  328. ->getLocalArea (&comp, comp.getLocalBounds()));
  329. }
  330. }
  331. void componentPeerChanged()
  332. {
  333. detach();
  334. componentVisibilityChanged();
  335. }
  336. void componentVisibilityChanged()
  337. {
  338. Component& comp = *getComponent();
  339. if (canBeAttached (comp))
  340. {
  341. if (! isAttached (comp))
  342. attach();
  343. }
  344. else
  345. {
  346. detach();
  347. }
  348. }
  349. #if JUCE_DEBUG || JUCE_LOG_ASSERTIONS
  350. void componentBeingDeleted (Component& component)
  351. {
  352. /* You must call detach() or delete your OpenGLContext to remove it
  353. from a component BEFORE deleting the component that it is using!
  354. */
  355. jassertfalse;
  356. ComponentMovementWatcher::componentBeingDeleted (component);
  357. }
  358. #endif
  359. private:
  360. OpenGLContext& context;
  361. static bool canBeAttached (const Component& comp) noexcept
  362. {
  363. return comp.getWidth() > 0 && comp.getHeight() > 0 && comp.isShowing();
  364. }
  365. static bool isAttached (const Component& comp) noexcept
  366. {
  367. return comp.getCachedComponentImage() != nullptr;
  368. }
  369. void attach()
  370. {
  371. Component& comp = *getComponent();
  372. CachedImage* const newCachedImage = new CachedImage (context, comp,
  373. context.pixelFormat,
  374. context.contextToShareWith);
  375. comp.setCachedComponentImage (newCachedImage);
  376. newCachedImage->start(); // (must wait until this is attached before starting its thread)
  377. newCachedImage->updateViewportSize (true);
  378. }
  379. void detach()
  380. {
  381. Component& comp = *getComponent();
  382. if (CachedImage* const oldCachedImage = CachedImage::get (comp))
  383. oldCachedImage->stop(); // (must stop this before detaching it from the component)
  384. comp.setCachedComponentImage (nullptr);
  385. context.nativeContext = nullptr;
  386. }
  387. };
  388. //==============================================================================
  389. OpenGLContext::OpenGLContext()
  390. : nativeContext (nullptr), renderer (nullptr), contextToShareWith (nullptr),
  391. renderComponents (true)
  392. {
  393. }
  394. OpenGLContext::~OpenGLContext()
  395. {
  396. detach();
  397. }
  398. void OpenGLContext::setRenderer (OpenGLRenderer* rendererToUse) noexcept
  399. {
  400. // This method must not be called when the context has already been attached!
  401. // Call it before attaching your context, or use detach() first, before calling this!
  402. jassert (nativeContext == nullptr);
  403. renderer = rendererToUse;
  404. }
  405. void OpenGLContext::setComponentPaintingEnabled (bool shouldPaintComponent) noexcept
  406. {
  407. // This method must not be called when the context has already been attached!
  408. // Call it before attaching your context, or use detach() first, before calling this!
  409. jassert (nativeContext == nullptr);
  410. renderComponents = shouldPaintComponent;
  411. }
  412. void OpenGLContext::setPixelFormat (const OpenGLPixelFormat& preferredPixelFormat) noexcept
  413. {
  414. // This method must not be called when the context has already been attached!
  415. // Call it before attaching your context, or use detach() first, before calling this!
  416. jassert (nativeContext == nullptr);
  417. pixelFormat = preferredPixelFormat;
  418. }
  419. void OpenGLContext::setNativeSharedContext (void* nativeContextToShareWith) noexcept
  420. {
  421. // This method must not be called when the context has already been attached!
  422. // Call it before attaching your context, or use detach() first, before calling this!
  423. jassert (nativeContext == nullptr);
  424. contextToShareWith = nativeContextToShareWith;
  425. }
  426. void OpenGLContext::attachTo (Component& component)
  427. {
  428. component.repaint();
  429. if (getTargetComponent() != &component)
  430. {
  431. detach();
  432. attachment = new Attachment (*this, component);
  433. }
  434. }
  435. void OpenGLContext::detach()
  436. {
  437. attachment = nullptr;
  438. nativeContext = nullptr;
  439. }
  440. bool OpenGLContext::isAttached() const noexcept
  441. {
  442. return nativeContext != nullptr;
  443. }
  444. Component* OpenGLContext::getTargetComponent() const noexcept
  445. {
  446. return attachment != nullptr ? attachment->getComponent() : nullptr;
  447. }
  448. OpenGLContext* OpenGLContext::getCurrentContext()
  449. {
  450. #if JUCE_ANDROID
  451. if (NativeContext* const nc = NativeContext::getActiveContext())
  452. if (CachedImage* currentContext = CachedImage::get (nc->component))
  453. #else
  454. if (CachedImage* currentContext = dynamic_cast <CachedImage*> (Thread::getCurrentThread()))
  455. #endif
  456. return &currentContext->context;
  457. return nullptr;
  458. }
  459. bool OpenGLContext::makeActive() const noexcept { return nativeContext != nullptr && nativeContext->makeActive(); }
  460. bool OpenGLContext::isActive() const noexcept { return nativeContext != nullptr && nativeContext->isActive(); }
  461. void OpenGLContext::deactivateCurrentContext() { NativeContext::deactivateCurrentContext(); }
  462. void OpenGLContext::triggerRepaint()
  463. {
  464. if (CachedImage* const cachedImage = getCachedImage())
  465. cachedImage->triggerRepaint();
  466. }
  467. void OpenGLContext::swapBuffers()
  468. {
  469. if (nativeContext != nullptr)
  470. nativeContext->swapBuffers();
  471. }
  472. unsigned int OpenGLContext::getFrameBufferID() const noexcept
  473. {
  474. return nativeContext != nullptr ? nativeContext->getFrameBufferID() : 0;
  475. }
  476. bool OpenGLContext::setSwapInterval (int numFramesPerSwap)
  477. {
  478. return nativeContext != nullptr && nativeContext->setSwapInterval (numFramesPerSwap);
  479. }
  480. int OpenGLContext::getSwapInterval() const
  481. {
  482. return nativeContext != nullptr ? nativeContext->getSwapInterval() : 0;
  483. }
  484. void* OpenGLContext::getRawContext() const noexcept
  485. {
  486. return nativeContext != nullptr ? nativeContext->getRawContext() : nullptr;
  487. }
  488. OpenGLContext::CachedImage* OpenGLContext::getCachedImage() const noexcept
  489. {
  490. if (Component* const comp = getTargetComponent())
  491. return CachedImage::get (*comp);
  492. return nullptr;
  493. }
  494. bool OpenGLContext::areShadersAvailable() const
  495. {
  496. CachedImage* const c = getCachedImage();
  497. return c != nullptr && c->shadersAvailable;
  498. }
  499. ReferenceCountedObject* OpenGLContext::getAssociatedObject (const char* name) const
  500. {
  501. jassert (name != nullptr);
  502. CachedImage* const c = getCachedImage();
  503. // This method must only be called from an openGL rendering callback.
  504. jassert (c != nullptr && nativeContext != nullptr);
  505. jassert (getCurrentContext() != nullptr);
  506. const int index = c->associatedObjectNames.indexOf (name);
  507. return index >= 0 ? c->associatedObjects.getUnchecked (index) : nullptr;
  508. }
  509. void OpenGLContext::setAssociatedObject (const char* name, ReferenceCountedObject* newObject)
  510. {
  511. jassert (name != nullptr);
  512. CachedImage* const c = getCachedImage();
  513. // This method must only be called from an openGL rendering callback.
  514. jassert (c != nullptr && nativeContext != nullptr);
  515. jassert (getCurrentContext() != nullptr);
  516. const int index = c->associatedObjectNames.indexOf (name);
  517. if (index >= 0)
  518. {
  519. c->associatedObjects.set (index, newObject);
  520. }
  521. else
  522. {
  523. c->associatedObjectNames.add (name);
  524. c->associatedObjects.add (newObject);
  525. }
  526. }
  527. void OpenGLContext::copyTexture (const Rectangle<int>& targetClipArea,
  528. const Rectangle<int>& anchorPosAndTextureSize,
  529. const int contextWidth, const int contextHeight,
  530. bool flippedVertically)
  531. {
  532. if (contextWidth <= 0 || contextHeight <= 0)
  533. return;
  534. JUCE_CHECK_OPENGL_ERROR
  535. glBlendFunc (GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
  536. glEnable (GL_BLEND);
  537. #if JUCE_USE_OPENGL_SHADERS
  538. if (areShadersAvailable())
  539. {
  540. struct OverlayShaderProgram : public ReferenceCountedObject
  541. {
  542. OverlayShaderProgram (OpenGLContext& context)
  543. : program (context), builder (program), params (program)
  544. {}
  545. static const OverlayShaderProgram& select (OpenGLContext& context)
  546. {
  547. static const char programValueID[] = "juceGLComponentOverlayShader";
  548. OverlayShaderProgram* program = static_cast <OverlayShaderProgram*> (context.getAssociatedObject (programValueID));
  549. if (program == nullptr)
  550. {
  551. program = new OverlayShaderProgram (context);
  552. context.setAssociatedObject (programValueID, program);
  553. }
  554. program->program.use();
  555. return *program;
  556. }
  557. struct ProgramBuilder
  558. {
  559. ProgramBuilder (OpenGLShaderProgram& prog)
  560. {
  561. prog.addShader ("attribute " JUCE_HIGHP " vec2 position;"
  562. "uniform " JUCE_HIGHP " vec2 screenSize;"
  563. "varying " JUCE_HIGHP " vec2 pixelPos;"
  564. "void main()"
  565. "{"
  566. "pixelPos = position;"
  567. JUCE_HIGHP " vec2 scaled = position / (0.5 * screenSize.xy);"
  568. "gl_Position = vec4 (scaled.x - 1.0, 1.0 - scaled.y, 0, 1.0);"
  569. "}",
  570. GL_VERTEX_SHADER);
  571. prog.addShader ("uniform sampler2D imageTexture;"
  572. "uniform " JUCE_HIGHP " float textureBounds[4];"
  573. "uniform " JUCE_HIGHP " vec2 vOffsetAndScale;"
  574. "varying " JUCE_HIGHP " vec2 pixelPos;"
  575. "void main()"
  576. "{"
  577. JUCE_HIGHP " vec2 texturePos = (pixelPos - vec2 (textureBounds[0], textureBounds[1]))"
  578. "/ vec2 (textureBounds[2], textureBounds[3]);"
  579. "gl_FragColor = texture2D (imageTexture, vec2 (texturePos.x, vOffsetAndScale.x + vOffsetAndScale.y * texturePos.y));"
  580. "}",
  581. GL_FRAGMENT_SHADER);
  582. prog.link();
  583. }
  584. };
  585. struct Params
  586. {
  587. Params (OpenGLShaderProgram& prog)
  588. : positionAttribute (prog, "position"),
  589. screenSize (prog, "screenSize"),
  590. imageTexture (prog, "imageTexture"),
  591. textureBounds (prog, "textureBounds"),
  592. vOffsetAndScale (prog, "vOffsetAndScale")
  593. {}
  594. void set (const float targetWidth, const float targetHeight, const Rectangle<float>& bounds, bool flippedVertically) const
  595. {
  596. const GLfloat m[] = { bounds.getX(), bounds.getY(), bounds.getWidth(), bounds.getHeight() };
  597. textureBounds.set (m, 4);
  598. imageTexture.set (0);
  599. screenSize.set (targetWidth, targetHeight);
  600. vOffsetAndScale.set (flippedVertically ? 0.0f : 1.0f,
  601. flippedVertically ? 1.0f : -1.0f);
  602. }
  603. OpenGLShaderProgram::Attribute positionAttribute;
  604. OpenGLShaderProgram::Uniform screenSize, imageTexture, textureBounds, vOffsetAndScale;
  605. };
  606. OpenGLShaderProgram program;
  607. ProgramBuilder builder;
  608. Params params;
  609. };
  610. const GLshort left = (GLshort) targetClipArea.getX();
  611. const GLshort top = (GLshort) targetClipArea.getY();
  612. const GLshort right = (GLshort) targetClipArea.getRight();
  613. const GLshort bottom = (GLshort) targetClipArea.getBottom();
  614. const GLshort vertices[] = { left, bottom, right, bottom, left, top, right, top };
  615. const OverlayShaderProgram& program = OverlayShaderProgram::select (*this);
  616. program.params.set ((float) contextWidth, (float) contextHeight, anchorPosAndTextureSize.toFloat(), flippedVertically);
  617. extensions.glVertexAttribPointer (program.params.positionAttribute.attributeID, 2, GL_SHORT, GL_FALSE, 4, vertices);
  618. extensions.glEnableVertexAttribArray (program.params.positionAttribute.attributeID);
  619. JUCE_CHECK_OPENGL_ERROR
  620. glDrawArrays (GL_TRIANGLE_STRIP, 0, 4);
  621. extensions.glUseProgram (0);
  622. extensions.glDisableVertexAttribArray (program.params.positionAttribute.attributeID);
  623. }
  624. #if JUCE_USE_OPENGL_FIXED_FUNCTION
  625. else
  626. #endif
  627. #endif
  628. #if JUCE_USE_OPENGL_FIXED_FUNCTION
  629. {
  630. glEnable (GL_SCISSOR_TEST);
  631. glScissor (targetClipArea.getX(), contextHeight - targetClipArea.getBottom(),
  632. targetClipArea.getWidth(), targetClipArea.getHeight());
  633. JUCE_CHECK_OPENGL_ERROR
  634. glColor4f (1.0f, 1.0f, 1.0f, 1.0f);
  635. glDisableClientState (GL_COLOR_ARRAY);
  636. glDisableClientState (GL_NORMAL_ARRAY);
  637. glEnableClientState (GL_VERTEX_ARRAY);
  638. glEnableClientState (GL_TEXTURE_COORD_ARRAY);
  639. OpenGLHelpers::prepareFor2D (contextWidth, contextHeight);
  640. JUCE_CHECK_OPENGL_ERROR
  641. const GLfloat textureCoords[] = { 0, 0, 1.0f, 0, 0, 1.0f, 1.0f, 1.0f };
  642. glTexCoordPointer (2, GL_FLOAT, 0, textureCoords);
  643. const GLshort left = (GLshort) anchorPosAndTextureSize.getX();
  644. const GLshort right = (GLshort) anchorPosAndTextureSize.getRight();
  645. const GLshort top = (GLshort) (contextHeight - anchorPosAndTextureSize.getY());
  646. const GLshort bottom = (GLshort) (contextHeight - anchorPosAndTextureSize.getBottom());
  647. const GLshort vertices[] = { left, bottom, right, bottom, left, top, right, top };
  648. glVertexPointer (2, GL_SHORT, 0, vertices);
  649. glDrawArrays (GL_TRIANGLE_STRIP, 0, 4);
  650. glDisable (GL_SCISSOR_TEST);
  651. }
  652. #endif
  653. JUCE_CHECK_OPENGL_ERROR
  654. }