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.

783 lines
25KB

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