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.

814 lines
26KB

  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 (true)
  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 = true;
  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. if (context.renderComponents && needsUpdate)
  119. {
  120. mmLock = new MessageManagerLock (this); // need to acquire this before locking the context.
  121. if (! mmLock->lockWasGained())
  122. return false;
  123. }
  124. if (! context.makeActive())
  125. return false;
  126. NativeContext::Locker locker (*nativeContext);
  127. JUCE_CHECK_OPENGL_ERROR
  128. glViewport (0, 0, viewportArea.getWidth(), viewportArea.getHeight());
  129. if (context.renderer != nullptr)
  130. {
  131. context.renderer->renderOpenGL();
  132. clearGLError();
  133. }
  134. if (context.renderComponents)
  135. {
  136. if (needsUpdate)
  137. {
  138. needsUpdate = false;
  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());
  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. initialiseOnThread();
  233. #if JUCE_USE_OPENGL_SHADERS && ! JUCE_OPENGL_ES
  234. shadersAvailable = OpenGLShaderProgram::getLanguageVersion() > 0;
  235. #endif
  236. while (! threadShouldExit())
  237. {
  238. const uint32 frameRenderStartTime = Time::getMillisecondCounter();
  239. if (renderFrame())
  240. waitForNextFrame (frameRenderStartTime);
  241. }
  242. shutdownOnThread();
  243. }
  244. void initialiseOnThread()
  245. {
  246. associatedObjectNames.clear();
  247. associatedObjects.clear();
  248. nativeContext->initialiseOnRenderThread();
  249. glViewport (0, 0, component.getWidth(), component.getHeight());
  250. context.extensions.initialise();
  251. if (context.renderer != nullptr)
  252. context.renderer->newOpenGLContextCreated();
  253. }
  254. void shutdownOnThread()
  255. {
  256. if (context.renderer != nullptr)
  257. context.renderer->openGLContextClosing();
  258. nativeContext->shutdownOnRenderThread();
  259. associatedObjectNames.clear();
  260. associatedObjects.clear();
  261. }
  262. void waitForNextFrame (const uint32 frameRenderStartTime)
  263. {
  264. const int defaultFPS = 60;
  265. const int elapsed = (int) (Time::getMillisecondCounter() - frameRenderStartTime);
  266. wait (jmax (1, (1000 / defaultFPS - 1) - elapsed));
  267. }
  268. //==============================================================================
  269. static CachedImage* get (Component& c) noexcept
  270. {
  271. return dynamic_cast<CachedImage*> (c.getCachedComponentImage());
  272. }
  273. //==============================================================================
  274. ScopedPointer<NativeContext> nativeContext;
  275. OpenGLContext& context;
  276. Component& component;
  277. OpenGLFrameBuffer cachedImageFrameBuffer;
  278. RectangleList validArea;
  279. Rectangle<int> viewportArea;
  280. double scale;
  281. StringArray associatedObjectNames;
  282. ReferenceCountedArray<ReferenceCountedObject> associatedObjects;
  283. WaitableEvent canPaintNowFlag, finishedPaintingFlag;
  284. bool volatile shadersAvailable;
  285. bool volatile needsUpdate;
  286. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (CachedImage)
  287. };
  288. //==============================================================================
  289. #if JUCE_ANDROID
  290. void OpenGLContext::NativeContext::contextCreatedCallback()
  291. {
  292. isInsideGLCallback = true;
  293. if (CachedImage* const c = CachedImage::get (component))
  294. c->initialiseOnThread();
  295. else
  296. jassertfalse;
  297. isInsideGLCallback = false;
  298. }
  299. void OpenGLContext::NativeContext::renderCallback()
  300. {
  301. isInsideGLCallback = true;
  302. if (CachedImage* const c = CachedImage::get (component))
  303. c->renderFrame();
  304. isInsideGLCallback = false;
  305. }
  306. #endif
  307. //==============================================================================
  308. class OpenGLContext::Attachment : public ComponentMovementWatcher
  309. {
  310. public:
  311. Attachment (OpenGLContext& c, Component& comp)
  312. : ComponentMovementWatcher (&comp), context (c)
  313. {
  314. if (canBeAttached (comp))
  315. attach();
  316. }
  317. ~Attachment()
  318. {
  319. detach();
  320. }
  321. void componentMovedOrResized (bool /*wasMoved*/, bool /*wasResized*/)
  322. {
  323. Component& comp = *getComponent();
  324. if (isAttached (comp) != canBeAttached (comp))
  325. componentVisibilityChanged();
  326. if (comp.getWidth() > 0 && comp.getHeight() > 0
  327. && context.nativeContext != nullptr)
  328. {
  329. if (CachedImage* const c = CachedImage::get (comp))
  330. c->updateViewportSize (true);
  331. context.nativeContext->updateWindowPosition (comp.getTopLevelComponent()
  332. ->getLocalArea (&comp, comp.getLocalBounds()));
  333. }
  334. }
  335. void componentPeerChanged()
  336. {
  337. detach();
  338. componentVisibilityChanged();
  339. }
  340. void componentVisibilityChanged()
  341. {
  342. Component& comp = *getComponent();
  343. if (canBeAttached (comp))
  344. {
  345. if (! isAttached (comp))
  346. attach();
  347. }
  348. else
  349. {
  350. detach();
  351. }
  352. }
  353. #if JUCE_DEBUG || JUCE_LOG_ASSERTIONS
  354. void componentBeingDeleted (Component& component)
  355. {
  356. /* You must call detach() or delete your OpenGLContext to remove it
  357. from a component BEFORE deleting the component that it is using!
  358. */
  359. jassertfalse;
  360. ComponentMovementWatcher::componentBeingDeleted (component);
  361. }
  362. #endif
  363. private:
  364. OpenGLContext& context;
  365. static bool canBeAttached (const Component& comp) noexcept
  366. {
  367. return comp.getWidth() > 0 && comp.getHeight() > 0 && comp.isShowing();
  368. }
  369. static bool isAttached (const Component& comp) noexcept
  370. {
  371. return comp.getCachedComponentImage() != nullptr;
  372. }
  373. void attach()
  374. {
  375. Component& comp = *getComponent();
  376. CachedImage* const newCachedImage = new CachedImage (context, comp,
  377. context.pixelFormat,
  378. context.contextToShareWith);
  379. comp.setCachedComponentImage (newCachedImage);
  380. newCachedImage->start(); // (must wait until this is attached before starting its thread)
  381. newCachedImage->updateViewportSize (true);
  382. }
  383. void detach()
  384. {
  385. Component& comp = *getComponent();
  386. if (CachedImage* const oldCachedImage = CachedImage::get (comp))
  387. oldCachedImage->stop(); // (must stop this before detaching it from the component)
  388. comp.setCachedComponentImage (nullptr);
  389. context.nativeContext = nullptr;
  390. }
  391. };
  392. //==============================================================================
  393. OpenGLContext::OpenGLContext()
  394. : nativeContext (nullptr), renderer (nullptr), contextToShareWith (nullptr),
  395. renderComponents (true)
  396. {
  397. }
  398. OpenGLContext::~OpenGLContext()
  399. {
  400. detach();
  401. }
  402. void OpenGLContext::setRenderer (OpenGLRenderer* rendererToUse) noexcept
  403. {
  404. // This method must not be called when the context has already been attached!
  405. // Call it before attaching your context, or use detach() first, before calling this!
  406. jassert (nativeContext == nullptr);
  407. renderer = rendererToUse;
  408. }
  409. void OpenGLContext::setComponentPaintingEnabled (bool shouldPaintComponent) noexcept
  410. {
  411. // This method must not be called when the context has already been attached!
  412. // Call it before attaching your context, or use detach() first, before calling this!
  413. jassert (nativeContext == nullptr);
  414. renderComponents = shouldPaintComponent;
  415. }
  416. void OpenGLContext::setPixelFormat (const OpenGLPixelFormat& preferredPixelFormat) noexcept
  417. {
  418. // This method must not be called when the context has already been attached!
  419. // Call it before attaching your context, or use detach() first, before calling this!
  420. jassert (nativeContext == nullptr);
  421. pixelFormat = preferredPixelFormat;
  422. }
  423. void OpenGLContext::setNativeSharedContext (void* nativeContextToShareWith) noexcept
  424. {
  425. // This method must not be called when the context has already been attached!
  426. // Call it before attaching your context, or use detach() first, before calling this!
  427. jassert (nativeContext == nullptr);
  428. contextToShareWith = nativeContextToShareWith;
  429. }
  430. void OpenGLContext::attachTo (Component& component)
  431. {
  432. component.repaint();
  433. if (getTargetComponent() != &component)
  434. {
  435. detach();
  436. attachment = new Attachment (*this, component);
  437. }
  438. }
  439. void OpenGLContext::detach()
  440. {
  441. attachment = nullptr;
  442. nativeContext = nullptr;
  443. }
  444. bool OpenGLContext::isAttached() const noexcept
  445. {
  446. return nativeContext != nullptr;
  447. }
  448. Component* OpenGLContext::getTargetComponent() const noexcept
  449. {
  450. return attachment != nullptr ? attachment->getComponent() : nullptr;
  451. }
  452. OpenGLContext* OpenGLContext::getCurrentContext()
  453. {
  454. #if JUCE_ANDROID
  455. NativeContext* const nc = NativeContext::getActiveContext();
  456. if (nc == nullptr)
  457. return nullptr;
  458. CachedImage* currentContext = CachedImage::get (nc->component);
  459. #else
  460. CachedImage* currentContext = dynamic_cast <CachedImage*> (Thread::getCurrentThread());
  461. #endif
  462. return currentContext != nullptr ? &currentContext->context : nullptr;
  463. }
  464. bool OpenGLContext::makeActive() const noexcept { return nativeContext != nullptr && nativeContext->makeActive(); }
  465. bool OpenGLContext::isActive() const noexcept { return nativeContext != nullptr && nativeContext->isActive(); }
  466. void OpenGLContext::deactivateCurrentContext() { NativeContext::deactivateCurrentContext(); }
  467. void OpenGLContext::triggerRepaint()
  468. {
  469. if (CachedImage* const cachedImage = getCachedImage())
  470. cachedImage->triggerRepaint();
  471. }
  472. void OpenGLContext::swapBuffers()
  473. {
  474. if (nativeContext != nullptr)
  475. nativeContext->swapBuffers();
  476. }
  477. unsigned int OpenGLContext::getFrameBufferID() const noexcept
  478. {
  479. return nativeContext != nullptr ? nativeContext->getFrameBufferID() : 0;
  480. }
  481. bool OpenGLContext::setSwapInterval (int numFramesPerSwap)
  482. {
  483. return nativeContext != nullptr && nativeContext->setSwapInterval (numFramesPerSwap);
  484. }
  485. int OpenGLContext::getSwapInterval() const
  486. {
  487. return nativeContext != nullptr ? nativeContext->getSwapInterval() : 0;
  488. }
  489. void* OpenGLContext::getRawContext() const noexcept
  490. {
  491. return nativeContext != nullptr ? nativeContext->getRawContext() : nullptr;
  492. }
  493. OpenGLContext::CachedImage* OpenGLContext::getCachedImage() const noexcept
  494. {
  495. Component* const comp = getTargetComponent();
  496. return comp != nullptr ? CachedImage::get (*comp) : nullptr;
  497. }
  498. bool OpenGLContext::areShadersAvailable() const
  499. {
  500. CachedImage* const c = getCachedImage();
  501. return c != nullptr && c->shadersAvailable;
  502. }
  503. ReferenceCountedObject* OpenGLContext::getAssociatedObject (const char* name) const
  504. {
  505. jassert (name != nullptr);
  506. CachedImage* const c = getCachedImage();
  507. // This method must only be called from an openGL rendering callback.
  508. jassert (c != nullptr && nativeContext != nullptr);
  509. jassert (getCurrentContext() != nullptr);
  510. const int index = c->associatedObjectNames.indexOf (name);
  511. return index >= 0 ? c->associatedObjects.getUnchecked (index) : nullptr;
  512. }
  513. void OpenGLContext::setAssociatedObject (const char* name, ReferenceCountedObject* newObject)
  514. {
  515. jassert (name != nullptr);
  516. CachedImage* const c = getCachedImage();
  517. // This method must only be called from an openGL rendering callback.
  518. jassert (c != nullptr && nativeContext != nullptr);
  519. jassert (getCurrentContext() != nullptr);
  520. const int index = c->associatedObjectNames.indexOf (name);
  521. if (index >= 0)
  522. {
  523. c->associatedObjects.set (index, newObject);
  524. }
  525. else
  526. {
  527. c->associatedObjectNames.add (name);
  528. c->associatedObjects.add (newObject);
  529. }
  530. }
  531. void OpenGLContext::copyTexture (const Rectangle<int>& targetClipArea,
  532. const Rectangle<int>& anchorPosAndTextureSize,
  533. const int contextWidth, const int contextHeight)
  534. {
  535. if (contextWidth <= 0 || contextHeight <= 0)
  536. return;
  537. JUCE_CHECK_OPENGL_ERROR
  538. glBlendFunc (GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
  539. glEnable (GL_BLEND);
  540. #if JUCE_USE_OPENGL_SHADERS
  541. if (areShadersAvailable())
  542. {
  543. struct OverlayShaderProgram : public ReferenceCountedObject
  544. {
  545. OverlayShaderProgram (OpenGLContext& context)
  546. : program (context), builder (program), params (program)
  547. {}
  548. static const OverlayShaderProgram& select (OpenGLContext& context)
  549. {
  550. static const char programValueID[] = "juceGLComponentOverlayShader";
  551. OverlayShaderProgram* program = static_cast <OverlayShaderProgram*> (context.getAssociatedObject (programValueID));
  552. if (program == nullptr)
  553. {
  554. program = new OverlayShaderProgram (context);
  555. context.setAssociatedObject (programValueID, program);
  556. }
  557. program->program.use();
  558. return *program;
  559. }
  560. struct ProgramBuilder
  561. {
  562. ProgramBuilder (OpenGLShaderProgram& prog)
  563. {
  564. prog.addShader ("attribute " JUCE_HIGHP " vec2 position;"
  565. "uniform " JUCE_HIGHP " vec2 screenSize;"
  566. "varying " JUCE_HIGHP " vec2 pixelPos;"
  567. "void main()"
  568. "{"
  569. "pixelPos = position;"
  570. JUCE_HIGHP " vec2 scaled = position / (0.5 * screenSize.xy);"
  571. "gl_Position = vec4 (scaled.x - 1.0, 1.0 - scaled.y, 0, 1.0);"
  572. "}",
  573. GL_VERTEX_SHADER);
  574. prog.addShader ("uniform sampler2D imageTexture;"
  575. "uniform " JUCE_HIGHP " float textureBounds[4];"
  576. "varying " JUCE_HIGHP " vec2 pixelPos;"
  577. "void main()"
  578. "{"
  579. JUCE_HIGHP " vec2 texturePos = (pixelPos - vec2 (textureBounds[0], textureBounds[1]))"
  580. "/ vec2 (textureBounds[2], textureBounds[3]);"
  581. "gl_FragColor = texture2D (imageTexture, vec2 (texturePos.x, 1.0 - texturePos.y));"
  582. "}",
  583. GL_FRAGMENT_SHADER);
  584. prog.link();
  585. }
  586. };
  587. struct Params
  588. {
  589. Params (OpenGLShaderProgram& prog)
  590. : positionAttribute (prog, "position"),
  591. screenSize (prog, "screenSize"),
  592. imageTexture (prog, "imageTexture"),
  593. textureBounds (prog, "textureBounds")
  594. {}
  595. void set (const float targetWidth, const float targetHeight, const Rectangle<float>& bounds) const
  596. {
  597. const GLfloat m[] = { bounds.getX(), bounds.getY(), bounds.getWidth(), bounds.getHeight() };
  598. textureBounds.set (m, 4);
  599. imageTexture.set (0);
  600. screenSize.set (targetWidth, targetHeight);
  601. }
  602. OpenGLShaderProgram::Attribute positionAttribute;
  603. OpenGLShaderProgram::Uniform screenSize, imageTexture, textureBounds;
  604. };
  605. OpenGLShaderProgram program;
  606. ProgramBuilder builder;
  607. Params params;
  608. };
  609. const GLshort left = (GLshort) targetClipArea.getX();
  610. const GLshort top = (GLshort) targetClipArea.getY();
  611. const GLshort right = (GLshort) targetClipArea.getRight();
  612. const GLshort bottom = (GLshort) targetClipArea.getBottom();
  613. const GLshort vertices[] = { left, bottom, right, bottom, left, top, right, top };
  614. const OverlayShaderProgram& program = OverlayShaderProgram::select (*this);
  615. program.params.set ((float) contextWidth, (float) contextHeight, anchorPosAndTextureSize.toFloat());
  616. extensions.glVertexAttribPointer (program.params.positionAttribute.attributeID, 2, GL_SHORT, GL_FALSE, 4, vertices);
  617. extensions.glEnableVertexAttribArray (program.params.positionAttribute.attributeID);
  618. JUCE_CHECK_OPENGL_ERROR
  619. glDrawArrays (GL_TRIANGLE_STRIP, 0, 4);
  620. extensions.glUseProgram (0);
  621. extensions.glDisableVertexAttribArray (program.params.positionAttribute.attributeID);
  622. }
  623. #if JUCE_USE_OPENGL_FIXED_FUNCTION
  624. else
  625. #endif
  626. #endif
  627. #if JUCE_USE_OPENGL_FIXED_FUNCTION
  628. {
  629. glEnable (GL_SCISSOR_TEST);
  630. glScissor (targetClipArea.getX(), contextHeight - targetClipArea.getBottom(),
  631. targetClipArea.getWidth(), targetClipArea.getHeight());
  632. JUCE_CHECK_OPENGL_ERROR
  633. glColor4f (1.0f, 1.0f, 1.0f, 1.0f);
  634. glDisableClientState (GL_COLOR_ARRAY);
  635. glDisableClientState (GL_NORMAL_ARRAY);
  636. glEnableClientState (GL_VERTEX_ARRAY);
  637. glEnableClientState (GL_TEXTURE_COORD_ARRAY);
  638. OpenGLHelpers::prepareFor2D (contextWidth, contextHeight);
  639. JUCE_CHECK_OPENGL_ERROR
  640. const GLfloat textureCoords[] = { 0, 0, 1.0f, 0, 0, 1.0f, 1.0f, 1.0f };
  641. glTexCoordPointer (2, GL_FLOAT, 0, textureCoords);
  642. const GLshort left = (GLshort) anchorPosAndTextureSize.getX();
  643. const GLshort right = (GLshort) anchorPosAndTextureSize.getRight();
  644. const GLshort top = (GLshort) (contextHeight - anchorPosAndTextureSize.getY());
  645. const GLshort bottom = (GLshort) (contextHeight - anchorPosAndTextureSize.getBottom());
  646. const GLshort vertices[] = { left, bottom, right, bottom, left, top, right, top };
  647. glVertexPointer (2, GL_SHORT, 0, vertices);
  648. glDrawArrays (GL_TRIANGLE_STRIP, 0, 4);
  649. glDisable (GL_SCISSOR_TEST);
  650. }
  651. #endif
  652. JUCE_CHECK_OPENGL_ERROR
  653. }