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.

813 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& pixelFormat, void* contextToShareWith)
  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, pixelFormat, contextToShareWith);
  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 scale)
  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() * scale).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& context)
  201. {
  202. Graphics g (&context);
  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. }
  382. void detach()
  383. {
  384. Component& comp = *getComponent();
  385. if (CachedImage* const oldCachedImage = CachedImage::get (comp))
  386. oldCachedImage->stop(); // (must stop this before detaching it from the component)
  387. comp.setCachedComponentImage (nullptr);
  388. context.nativeContext = nullptr;
  389. }
  390. };
  391. //==============================================================================
  392. OpenGLContext::OpenGLContext()
  393. : nativeContext (nullptr), renderer (nullptr), contextToShareWith (nullptr),
  394. renderComponents (true)
  395. {
  396. }
  397. OpenGLContext::~OpenGLContext()
  398. {
  399. detach();
  400. }
  401. void OpenGLContext::setRenderer (OpenGLRenderer* rendererToUse) noexcept
  402. {
  403. // This method must not be called when the context has already been attached!
  404. // Call it before attaching your context, or use detach() first, before calling this!
  405. jassert (nativeContext == nullptr);
  406. renderer = rendererToUse;
  407. }
  408. void OpenGLContext::setComponentPaintingEnabled (bool shouldPaintComponent) noexcept
  409. {
  410. // This method must not be called when the context has already been attached!
  411. // Call it before attaching your context, or use detach() first, before calling this!
  412. jassert (nativeContext == nullptr);
  413. renderComponents = shouldPaintComponent;
  414. }
  415. void OpenGLContext::setPixelFormat (const OpenGLPixelFormat& preferredPixelFormat) noexcept
  416. {
  417. // This method must not be called when the context has already been attached!
  418. // Call it before attaching your context, or use detach() first, before calling this!
  419. jassert (nativeContext == nullptr);
  420. pixelFormat = preferredPixelFormat;
  421. }
  422. void OpenGLContext::setNativeSharedContext (void* nativeContextToShareWith) noexcept
  423. {
  424. // This method must not be called when the context has already been attached!
  425. // Call it before attaching your context, or use detach() first, before calling this!
  426. jassert (nativeContext == nullptr);
  427. contextToShareWith = nativeContextToShareWith;
  428. }
  429. void OpenGLContext::attachTo (Component& component)
  430. {
  431. component.repaint();
  432. if (getTargetComponent() != &component)
  433. {
  434. detach();
  435. attachment = new Attachment (*this, component);
  436. }
  437. }
  438. void OpenGLContext::detach()
  439. {
  440. attachment = nullptr;
  441. nativeContext = nullptr;
  442. }
  443. bool OpenGLContext::isAttached() const noexcept
  444. {
  445. return nativeContext != nullptr;
  446. }
  447. Component* OpenGLContext::getTargetComponent() const noexcept
  448. {
  449. return attachment != nullptr ? attachment->getComponent() : nullptr;
  450. }
  451. OpenGLContext* OpenGLContext::getCurrentContext()
  452. {
  453. #if JUCE_ANDROID
  454. NativeContext* const nc = NativeContext::getActiveContext();
  455. if (nc == nullptr)
  456. return nullptr;
  457. CachedImage* currentContext = CachedImage::get (nc->component);
  458. #else
  459. CachedImage* currentContext = dynamic_cast <CachedImage*> (Thread::getCurrentThread());
  460. #endif
  461. return currentContext != nullptr ? &currentContext->context : nullptr;
  462. }
  463. bool OpenGLContext::makeActive() const noexcept { return nativeContext != nullptr && nativeContext->makeActive(); }
  464. bool OpenGLContext::isActive() const noexcept { return nativeContext != nullptr && nativeContext->isActive(); }
  465. void OpenGLContext::deactivateCurrentContext() { NativeContext::deactivateCurrentContext(); }
  466. void OpenGLContext::triggerRepaint()
  467. {
  468. if (CachedImage* const cachedImage = getCachedImage())
  469. cachedImage->triggerRepaint();
  470. }
  471. void OpenGLContext::swapBuffers()
  472. {
  473. if (nativeContext != nullptr)
  474. nativeContext->swapBuffers();
  475. }
  476. unsigned int OpenGLContext::getFrameBufferID() const noexcept
  477. {
  478. return nativeContext != nullptr ? nativeContext->getFrameBufferID() : 0;
  479. }
  480. bool OpenGLContext::setSwapInterval (int numFramesPerSwap)
  481. {
  482. return nativeContext != nullptr && nativeContext->setSwapInterval (numFramesPerSwap);
  483. }
  484. int OpenGLContext::getSwapInterval() const
  485. {
  486. return nativeContext != nullptr ? nativeContext->getSwapInterval() : 0;
  487. }
  488. void* OpenGLContext::getRawContext() const noexcept
  489. {
  490. return nativeContext != nullptr ? nativeContext->getRawContext() : nullptr;
  491. }
  492. OpenGLContext::CachedImage* OpenGLContext::getCachedImage() const noexcept
  493. {
  494. Component* const comp = getTargetComponent();
  495. return comp != nullptr ? CachedImage::get (*comp) : nullptr;
  496. }
  497. bool OpenGLContext::areShadersAvailable() const
  498. {
  499. CachedImage* const c = getCachedImage();
  500. return c != nullptr && c->shadersAvailable;
  501. }
  502. ReferenceCountedObject* OpenGLContext::getAssociatedObject (const char* name) const
  503. {
  504. jassert (name != nullptr);
  505. CachedImage* const c = getCachedImage();
  506. // This method must only be called from an openGL rendering callback.
  507. jassert (c != nullptr && nativeContext != nullptr);
  508. jassert (getCurrentContext() != nullptr);
  509. const int index = c->associatedObjectNames.indexOf (name);
  510. return index >= 0 ? c->associatedObjects.getUnchecked (index) : nullptr;
  511. }
  512. void OpenGLContext::setAssociatedObject (const char* name, ReferenceCountedObject* newObject)
  513. {
  514. jassert (name != nullptr);
  515. CachedImage* const c = getCachedImage();
  516. // This method must only be called from an openGL rendering callback.
  517. jassert (c != nullptr && nativeContext != nullptr);
  518. jassert (getCurrentContext() != nullptr);
  519. const int index = c->associatedObjectNames.indexOf (name);
  520. if (index >= 0)
  521. {
  522. c->associatedObjects.set (index, newObject);
  523. }
  524. else
  525. {
  526. c->associatedObjectNames.add (name);
  527. c->associatedObjects.add (newObject);
  528. }
  529. }
  530. void OpenGLContext::copyTexture (const Rectangle<int>& targetClipArea,
  531. const Rectangle<int>& anchorPosAndTextureSize,
  532. const int contextWidth, const int contextHeight)
  533. {
  534. if (contextWidth <= 0 || contextHeight <= 0)
  535. return;
  536. JUCE_CHECK_OPENGL_ERROR
  537. glBlendFunc (GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
  538. glEnable (GL_BLEND);
  539. #if JUCE_USE_OPENGL_SHADERS
  540. if (areShadersAvailable())
  541. {
  542. struct OverlayShaderProgram : public ReferenceCountedObject
  543. {
  544. OverlayShaderProgram (OpenGLContext& context)
  545. : program (context), builder (program), params (program)
  546. {}
  547. static const OverlayShaderProgram& select (OpenGLContext& context)
  548. {
  549. static const char programValueID[] = "juceGLComponentOverlayShader";
  550. OverlayShaderProgram* program = static_cast <OverlayShaderProgram*> (context.getAssociatedObject (programValueID));
  551. if (program == nullptr)
  552. {
  553. program = new OverlayShaderProgram (context);
  554. context.setAssociatedObject (programValueID, program);
  555. }
  556. program->program.use();
  557. return *program;
  558. }
  559. struct ProgramBuilder
  560. {
  561. ProgramBuilder (OpenGLShaderProgram& program)
  562. {
  563. program.addShader ("attribute " JUCE_HIGHP " vec2 position;"
  564. "uniform " JUCE_HIGHP " vec2 screenSize;"
  565. "varying " JUCE_HIGHP " vec2 pixelPos;"
  566. "void main()"
  567. "{"
  568. "pixelPos = position;"
  569. JUCE_HIGHP " vec2 scaled = position / (0.5 * screenSize.xy);"
  570. "gl_Position = vec4 (scaled.x - 1.0, 1.0 - scaled.y, 0, 1.0);"
  571. "}",
  572. GL_VERTEX_SHADER);
  573. program.addShader ("uniform sampler2D imageTexture;"
  574. "uniform " JUCE_HIGHP " float textureBounds[4];"
  575. "varying " JUCE_HIGHP " vec2 pixelPos;"
  576. "void main()"
  577. "{"
  578. JUCE_HIGHP " vec2 texturePos = (pixelPos - vec2 (textureBounds[0], textureBounds[1]))"
  579. "/ vec2 (textureBounds[2], textureBounds[3]);"
  580. "gl_FragColor = texture2D (imageTexture, vec2 (texturePos.x, 1.0 - texturePos.y));"
  581. "}",
  582. GL_FRAGMENT_SHADER);
  583. program.link();
  584. }
  585. };
  586. struct Params
  587. {
  588. Params (OpenGLShaderProgram& program)
  589. : positionAttribute (program, "position"),
  590. screenSize (program, "screenSize"),
  591. imageTexture (program, "imageTexture"),
  592. textureBounds (program, "textureBounds")
  593. {}
  594. void set (const float targetWidth, const float targetHeight, const Rectangle<float>& bounds) 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. }
  601. OpenGLShaderProgram::Attribute positionAttribute;
  602. OpenGLShaderProgram::Uniform screenSize, imageTexture, textureBounds;
  603. };
  604. OpenGLShaderProgram program;
  605. ProgramBuilder builder;
  606. Params params;
  607. };
  608. const GLshort left = (GLshort) targetClipArea.getX();
  609. const GLshort top = (GLshort) targetClipArea.getY();
  610. const GLshort right = (GLshort) targetClipArea.getRight();
  611. const GLshort bottom = (GLshort) targetClipArea.getBottom();
  612. const GLshort vertices[] = { left, bottom, right, bottom, left, top, right, top };
  613. const OverlayShaderProgram& program = OverlayShaderProgram::select (*this);
  614. program.params.set ((float) contextWidth, (float) contextHeight, anchorPosAndTextureSize.toFloat());
  615. extensions.glVertexAttribPointer (program.params.positionAttribute.attributeID, 2, GL_SHORT, GL_FALSE, 4, vertices);
  616. extensions.glEnableVertexAttribArray (program.params.positionAttribute.attributeID);
  617. JUCE_CHECK_OPENGL_ERROR
  618. glDrawArrays (GL_TRIANGLE_STRIP, 0, 4);
  619. extensions.glUseProgram (0);
  620. extensions.glDisableVertexAttribArray (program.params.positionAttribute.attributeID);
  621. }
  622. #if JUCE_USE_OPENGL_FIXED_FUNCTION
  623. else
  624. #endif
  625. #endif
  626. #if JUCE_USE_OPENGL_FIXED_FUNCTION
  627. {
  628. glEnable (GL_SCISSOR_TEST);
  629. glScissor (targetClipArea.getX(), contextHeight - targetClipArea.getBottom(),
  630. targetClipArea.getWidth(), targetClipArea.getHeight());
  631. JUCE_CHECK_OPENGL_ERROR
  632. glColor4f (1.0f, 1.0f, 1.0f, 1.0f);
  633. glDisableClientState (GL_COLOR_ARRAY);
  634. glDisableClientState (GL_NORMAL_ARRAY);
  635. glEnableClientState (GL_VERTEX_ARRAY);
  636. glEnableClientState (GL_TEXTURE_COORD_ARRAY);
  637. OpenGLHelpers::prepareFor2D (contextWidth, contextHeight);
  638. JUCE_CHECK_OPENGL_ERROR
  639. const GLfloat textureCoords[] = { 0, 0, 1.0f, 0, 0, 1.0f, 1.0f, 1.0f };
  640. glTexCoordPointer (2, GL_FLOAT, 0, textureCoords);
  641. const GLshort left = (GLshort) anchorPosAndTextureSize.getX();
  642. const GLshort right = (GLshort) anchorPosAndTextureSize.getRight();
  643. const GLshort top = (GLshort) (contextHeight - anchorPosAndTextureSize.getY());
  644. const GLshort bottom = (GLshort) (contextHeight - anchorPosAndTextureSize.getBottom());
  645. const GLshort vertices[] = { left, bottom, right, bottom, left, top, right, top };
  646. glVertexPointer (2, GL_SHORT, 0, vertices);
  647. glDrawArrays (GL_TRIANGLE_STRIP, 0, 4);
  648. glDisable (GL_SCISSOR_TEST);
  649. }
  650. #endif
  651. JUCE_CHECK_OPENGL_ERROR
  652. }