Audio plugin host https://kx.studio/carla
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.

995 lines
28KB

  1. /*
  2. * DISTRHO Plugin Framework (DPF)
  3. * Copyright (C) 2012-2018 Filipe Coelho <falktx@falktx.com>
  4. *
  5. * Permission to use, copy, modify, and/or distribute this software for any purpose with
  6. * or without fee is hereby granted, provided that the above copyright notice and this
  7. * permission notice appear in all copies.
  8. *
  9. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD
  10. * TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN
  11. * NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
  12. * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
  13. * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  14. * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. #include "../NanoVG.hpp"
  17. #include "WidgetPrivateData.hpp"
  18. #ifndef DGL_NO_SHARED_RESOURCES
  19. # include "Resources.hpp"
  20. #endif
  21. // -----------------------------------------------------------------------
  22. #if defined(DISTRHO_OS_WINDOWS)
  23. # include <windows.h>
  24. # define DGL_EXT(PROC, func) static PROC func;
  25. DGL_EXT(PFNGLACTIVETEXTUREPROC, glActiveTexture)
  26. DGL_EXT(PFNGLATTACHSHADERPROC, glAttachShader)
  27. DGL_EXT(PFNGLBINDATTRIBLOCATIONPROC, glBindAttribLocation)
  28. DGL_EXT(PFNGLBINDBUFFERPROC, glBindBuffer)
  29. DGL_EXT(PFNGLBUFFERDATAPROC, glBufferData)
  30. DGL_EXT(PFNGLCOMPILESHADERPROC, glCompileShader)
  31. DGL_EXT(PFNGLCREATEPROGRAMPROC, glCreateProgram)
  32. DGL_EXT(PFNGLCREATESHADERPROC, glCreateShader)
  33. DGL_EXT(PFNGLDELETEBUFFERSPROC, glDeleteBuffers)
  34. DGL_EXT(PFNGLDELETEPROGRAMPROC, glDeleteProgram)
  35. DGL_EXT(PFNGLDELETESHADERPROC, glDeleteShader)
  36. DGL_EXT(PFNGLDISABLEVERTEXATTRIBARRAYPROC, glDisableVertexAttribArray)
  37. DGL_EXT(PFNGLENABLEVERTEXATTRIBARRAYPROC, glEnableVertexAttribArray)
  38. DGL_EXT(PFNGLGENBUFFERSPROC, glGenBuffers)
  39. DGL_EXT(PFNGLGETPROGRAMIVPROC, glGetProgramiv)
  40. DGL_EXT(PFNGLGETPROGRAMINFOLOGPROC, glGetProgramInfoLog)
  41. DGL_EXT(PFNGLGETSHADERIVPROC, glGetShaderiv)
  42. DGL_EXT(PFNGLGETSHADERINFOLOGPROC, glGetShaderInfoLog)
  43. DGL_EXT(PFNGLGETUNIFORMLOCATIONPROC, glGetUniformLocation)
  44. DGL_EXT(PFNGLLINKPROGRAMPROC, glLinkProgram)
  45. DGL_EXT(PFNGLSHADERSOURCEPROC, glShaderSource)
  46. DGL_EXT(PFNGLSTENCILOPSEPARATEPROC, glStencilOpSeparate)
  47. DGL_EXT(PFNGLUNIFORM1IPROC, glUniform1i)
  48. DGL_EXT(PFNGLUNIFORM2FVPROC, glUniform2fv)
  49. DGL_EXT(PFNGLUNIFORM4FVPROC, glUniform4fv)
  50. DGL_EXT(PFNGLUSEPROGRAMPROC, glUseProgram)
  51. DGL_EXT(PFNGLVERTEXATTRIBPOINTERPROC, glVertexAttribPointer)
  52. # undef DGL_EXT
  53. #endif
  54. // -----------------------------------------------------------------------
  55. // Include NanoVG OpenGL implementation
  56. //#define STB_IMAGE_STATIC
  57. #define NANOVG_GL2_IMPLEMENTATION
  58. #include "nanovg/nanovg_gl.h"
  59. #if defined(NANOVG_GL2)
  60. # define nvgCreateGL nvgCreateGL2
  61. # define nvgDeleteGL nvgDeleteGL2
  62. #elif defined(NANOVG_GL3)
  63. # define nvgCreateGL nvgCreateGL3
  64. # define nvgDeleteGL nvgDeleteGL3
  65. #elif defined(NANOVG_GLES2)
  66. # define nvgCreateGL nvgCreateGLES2
  67. # define nvgDeleteGL nvgDeleteGLES2
  68. #elif defined(NANOVG_GLES3)
  69. # define nvgCreateGL nvgCreateGLES3
  70. # define nvgDeleteGL nvgDeleteGLES3
  71. #endif
  72. static NVGcontext* nvgCreateGL_helper(int flags)
  73. {
  74. #if defined(DISTRHO_OS_WINDOWS)
  75. static bool needsInit = true;
  76. if (needsInit)
  77. {
  78. needsInit = false;
  79. # define DGL_EXT(PROC, func) \
  80. func = (PROC) wglGetProcAddress ( #func ); \
  81. DISTRHO_SAFE_ASSERT_RETURN(func != nullptr, nullptr);
  82. DGL_EXT(PFNGLACTIVETEXTUREPROC, glActiveTexture)
  83. DGL_EXT(PFNGLATTACHSHADERPROC, glAttachShader)
  84. DGL_EXT(PFNGLBINDATTRIBLOCATIONPROC, glBindAttribLocation)
  85. DGL_EXT(PFNGLBINDBUFFERPROC, glBindBuffer)
  86. DGL_EXT(PFNGLBUFFERDATAPROC, glBufferData)
  87. DGL_EXT(PFNGLCOMPILESHADERPROC, glCompileShader)
  88. DGL_EXT(PFNGLCREATEPROGRAMPROC, glCreateProgram)
  89. DGL_EXT(PFNGLCREATESHADERPROC, glCreateShader)
  90. DGL_EXT(PFNGLDELETEBUFFERSPROC, glDeleteBuffers)
  91. DGL_EXT(PFNGLDELETEPROGRAMPROC, glDeleteProgram)
  92. DGL_EXT(PFNGLDELETESHADERPROC, glDeleteShader)
  93. DGL_EXT(PFNGLDISABLEVERTEXATTRIBARRAYPROC, glDisableVertexAttribArray)
  94. DGL_EXT(PFNGLENABLEVERTEXATTRIBARRAYPROC, glEnableVertexAttribArray)
  95. DGL_EXT(PFNGLGENBUFFERSPROC, glGenBuffers)
  96. DGL_EXT(PFNGLGETPROGRAMIVPROC, glGetProgramiv)
  97. DGL_EXT(PFNGLGETPROGRAMINFOLOGPROC, glGetProgramInfoLog)
  98. DGL_EXT(PFNGLGETSHADERIVPROC, glGetShaderiv)
  99. DGL_EXT(PFNGLGETSHADERINFOLOGPROC, glGetShaderInfoLog)
  100. DGL_EXT(PFNGLGETUNIFORMLOCATIONPROC, glGetUniformLocation)
  101. DGL_EXT(PFNGLLINKPROGRAMPROC, glLinkProgram)
  102. DGL_EXT(PFNGLSHADERSOURCEPROC, glShaderSource)
  103. DGL_EXT(PFNGLSTENCILOPSEPARATEPROC, glStencilOpSeparate)
  104. DGL_EXT(PFNGLUNIFORM1IPROC, glUniform1i)
  105. DGL_EXT(PFNGLUNIFORM2FVPROC, glUniform2fv)
  106. DGL_EXT(PFNGLUNIFORM4FVPROC, glUniform4fv)
  107. DGL_EXT(PFNGLUSEPROGRAMPROC, glUseProgram)
  108. DGL_EXT(PFNGLVERTEXATTRIBPOINTERPROC, glVertexAttribPointer)
  109. # undef DGL_EXT
  110. }
  111. #endif
  112. return nvgCreateGL(flags);
  113. }
  114. // -----------------------------------------------------------------------
  115. START_NAMESPACE_DGL
  116. // -----------------------------------------------------------------------
  117. // NanoImage
  118. NanoImage::NanoImage()
  119. : fHandle(),
  120. fSize() {}
  121. NanoImage::NanoImage(const Handle& handle)
  122. : fHandle(handle),
  123. fSize()
  124. {
  125. DISTRHO_SAFE_ASSERT_RETURN(fHandle.context != nullptr && fHandle.imageId != 0,);
  126. _updateSize();
  127. }
  128. NanoImage::~NanoImage()
  129. {
  130. if (fHandle.context != nullptr && fHandle.imageId != 0)
  131. nvgDeleteImage(fHandle.context, fHandle.imageId);
  132. }
  133. NanoImage& NanoImage::operator=(const Handle& handle)
  134. {
  135. if (fHandle.context != nullptr && fHandle.imageId != 0)
  136. nvgDeleteImage(fHandle.context, fHandle.imageId);
  137. fHandle.context = handle.context;
  138. fHandle.imageId = handle.imageId;
  139. return *this;
  140. }
  141. bool NanoImage::isValid() const noexcept
  142. {
  143. return (fHandle.context != nullptr && fHandle.imageId != 0);
  144. }
  145. Size<uint> NanoImage::getSize() const noexcept
  146. {
  147. return fSize;
  148. }
  149. GLuint NanoImage::getTextureHandle() const
  150. {
  151. DISTRHO_SAFE_ASSERT_RETURN(fHandle.context != nullptr && fHandle.imageId != 0, 0);
  152. return nvglImageHandle(fHandle.context, fHandle.imageId);
  153. }
  154. void NanoImage::_updateSize()
  155. {
  156. int w=0, h=0;
  157. nvgImageSize(fHandle.context, fHandle.imageId, &w, &h);
  158. if (w < 0) w = 0;
  159. if (h < 0) h = 0;
  160. fSize.setSize(static_cast<uint>(w), static_cast<uint>(h));
  161. }
  162. // -----------------------------------------------------------------------
  163. // Paint
  164. NanoVG::Paint::Paint() noexcept
  165. : radius(0.0f), feather(0.0f), innerColor(), outerColor(), imageId(0)
  166. {
  167. std::memset(xform, 0, sizeof(float)*6);
  168. std::memset(extent, 0, sizeof(float)*2);
  169. }
  170. NanoVG::Paint::Paint(const NVGpaint& p) noexcept
  171. : radius(p.radius), feather(p.feather), innerColor(p.innerColor), outerColor(p.outerColor), imageId(p.image)
  172. {
  173. std::memcpy(xform, p.xform, sizeof(float)*6);
  174. std::memcpy(extent, p.extent, sizeof(float)*2);
  175. }
  176. NanoVG::Paint::operator NVGpaint() const noexcept
  177. {
  178. NVGpaint p;
  179. p.radius = radius;
  180. p.feather = feather;
  181. p.innerColor = innerColor;
  182. p.outerColor = outerColor;
  183. p.image = imageId;
  184. std::memcpy(p.xform, xform, sizeof(float)*6);
  185. std::memcpy(p.extent, extent, sizeof(float)*2);
  186. return p;
  187. }
  188. // -----------------------------------------------------------------------
  189. // NanoVG
  190. NanoVG::NanoVG(int flags)
  191. : fContext(nvgCreateGL_helper(flags)),
  192. fInFrame(false),
  193. fIsSubWidget(false) {}
  194. NanoVG::NanoVG(NanoWidget* groupWidget)
  195. : fContext(groupWidget->fContext),
  196. fInFrame(false),
  197. fIsSubWidget(true) {}
  198. NanoVG::~NanoVG()
  199. {
  200. DISTRHO_SAFE_ASSERT(! fInFrame);
  201. if (fContext != nullptr && ! fIsSubWidget)
  202. nvgDeleteGL(fContext);
  203. }
  204. // -----------------------------------------------------------------------
  205. void NanoVG::beginFrame(const uint width, const uint height, const float scaleFactor)
  206. {
  207. if (fContext == nullptr) return;
  208. DISTRHO_SAFE_ASSERT_RETURN(scaleFactor > 0.0f,);
  209. DISTRHO_SAFE_ASSERT_RETURN(! fInFrame,);
  210. fInFrame = true;
  211. nvgBeginFrame(fContext, static_cast<int>(width), static_cast<int>(height), scaleFactor);
  212. }
  213. void NanoVG::beginFrame(Widget* const widget)
  214. {
  215. DISTRHO_SAFE_ASSERT_RETURN(widget != nullptr,);
  216. DISTRHO_SAFE_ASSERT_RETURN(! fInFrame,);
  217. fInFrame = true;
  218. if (fContext == nullptr)
  219. return;
  220. Window& window(widget->getParentWindow());
  221. nvgBeginFrame(fContext, static_cast<int>(window.getWidth()), static_cast<int>(window.getHeight()), 1.0f);
  222. }
  223. void NanoVG::cancelFrame()
  224. {
  225. DISTRHO_SAFE_ASSERT_RETURN(fInFrame,);
  226. if (fContext != nullptr)
  227. nvgCancelFrame(fContext);
  228. fInFrame = false;
  229. }
  230. void NanoVG::endFrame()
  231. {
  232. DISTRHO_SAFE_ASSERT_RETURN(fInFrame,);
  233. // Save current blend state
  234. GLboolean blendEnabled;
  235. GLint blendSrc, blendDst;
  236. glGetBooleanv(GL_BLEND, &blendEnabled);
  237. glGetIntegerv(GL_BLEND_SRC_ALPHA, &blendSrc);
  238. glGetIntegerv(GL_BLEND_DST_ALPHA, &blendDst);
  239. if (fContext != nullptr)
  240. nvgEndFrame(fContext);
  241. // Restore blend state
  242. if (blendEnabled)
  243. glEnable(GL_BLEND);
  244. else
  245. glDisable(GL_BLEND);
  246. glBlendFunc(blendSrc, blendDst);
  247. fInFrame = false;
  248. }
  249. // -----------------------------------------------------------------------
  250. // State Handling
  251. void NanoVG::save()
  252. {
  253. if (fContext != nullptr)
  254. nvgSave(fContext);
  255. }
  256. void NanoVG::restore()
  257. {
  258. if (fContext != nullptr)
  259. nvgRestore(fContext);
  260. }
  261. void NanoVG::reset()
  262. {
  263. if (fContext != nullptr)
  264. nvgReset(fContext);
  265. }
  266. // -----------------------------------------------------------------------
  267. // Render styles
  268. void NanoVG::strokeColor(const Color& color)
  269. {
  270. if (fContext != nullptr)
  271. nvgStrokeColor(fContext, color);
  272. }
  273. void NanoVG::strokeColor(const int red, const int green, const int blue, const int alpha)
  274. {
  275. if (fContext != nullptr)
  276. {
  277. DISTRHO_SAFE_ASSERT_RETURN(red >= 0 && red <= 255,);
  278. DISTRHO_SAFE_ASSERT_RETURN(green >= 0 && green <= 255,);
  279. DISTRHO_SAFE_ASSERT_RETURN(blue >= 0 && blue <= 255,);
  280. DISTRHO_SAFE_ASSERT_RETURN(alpha >= 0 && alpha <= 255,);
  281. nvgStrokeColor(fContext, nvgRGBA(static_cast<uchar>(red),
  282. static_cast<uchar>(green),
  283. static_cast<uchar>(blue),
  284. static_cast<uchar>(alpha)));
  285. }
  286. }
  287. void NanoVG::strokeColor(const float red, const float green, const float blue, const float alpha)
  288. {
  289. if (fContext != nullptr)
  290. nvgStrokeColor(fContext, nvgRGBAf(red, green, blue, alpha));
  291. }
  292. void NanoVG::strokePaint(const Paint& paint)
  293. {
  294. if (fContext != nullptr)
  295. nvgStrokePaint(fContext, paint);
  296. }
  297. void NanoVG::fillColor(const Color& color)
  298. {
  299. if (fContext != nullptr)
  300. nvgFillColor(fContext, color);
  301. }
  302. void NanoVG::fillColor(const int red, const int green, const int blue, const int alpha)
  303. {
  304. if (fContext != nullptr)
  305. {
  306. DISTRHO_SAFE_ASSERT_RETURN(red >= 0 && red <= 255,);
  307. DISTRHO_SAFE_ASSERT_RETURN(green >= 0 && green <= 255,);
  308. DISTRHO_SAFE_ASSERT_RETURN(blue >= 0 && blue <= 255,);
  309. DISTRHO_SAFE_ASSERT_RETURN(alpha >= 0 && alpha <= 255,);
  310. nvgFillColor(fContext, nvgRGBA(static_cast<uchar>(red),
  311. static_cast<uchar>(green),
  312. static_cast<uchar>(blue),
  313. static_cast<uchar>(alpha)));
  314. }
  315. }
  316. void NanoVG::fillColor(const float red, const float green, const float blue, const float alpha)
  317. {
  318. if (fContext != nullptr)
  319. nvgFillColor(fContext, nvgRGBAf(red, green, blue, alpha));
  320. }
  321. void NanoVG::fillPaint(const Paint& paint)
  322. {
  323. if (fContext != nullptr)
  324. nvgFillPaint(fContext, paint);
  325. }
  326. void NanoVG::miterLimit(float limit)
  327. {
  328. if (fContext == nullptr) return;
  329. DISTRHO_SAFE_ASSERT_RETURN(limit > 0.0f,);
  330. nvgMiterLimit(fContext, limit);
  331. }
  332. void NanoVG::strokeWidth(float size)
  333. {
  334. if (fContext == nullptr) return;
  335. DISTRHO_SAFE_ASSERT_RETURN(size > 0.0f,);
  336. nvgStrokeWidth(fContext, size);
  337. }
  338. void NanoVG::lineCap(NanoVG::LineCap cap)
  339. {
  340. if (fContext != nullptr)
  341. nvgLineCap(fContext, cap);
  342. }
  343. void NanoVG::lineJoin(NanoVG::LineCap join)
  344. {
  345. if (fContext != nullptr)
  346. nvgLineJoin(fContext, join);
  347. }
  348. void NanoVG::globalAlpha(float alpha)
  349. {
  350. if (fContext != nullptr)
  351. nvgGlobalAlpha(fContext, alpha);
  352. }
  353. // -----------------------------------------------------------------------
  354. // Transforms
  355. void NanoVG::resetTransform()
  356. {
  357. if (fContext != nullptr)
  358. nvgResetTransform(fContext);
  359. }
  360. void NanoVG::transform(float a, float b, float c, float d, float e, float f)
  361. {
  362. if (fContext != nullptr)
  363. nvgTransform(fContext, a, b, c, d, e, f);
  364. }
  365. void NanoVG::translate(float x, float y)
  366. {
  367. if (fContext != nullptr)
  368. nvgTranslate(fContext, x, y);
  369. }
  370. void NanoVG::rotate(float angle)
  371. {
  372. if (fContext == nullptr) return;
  373. DISTRHO_SAFE_ASSERT_RETURN(angle > 0.0f,);
  374. nvgRotate(fContext, angle);
  375. }
  376. void NanoVG::skewX(float angle)
  377. {
  378. if (fContext == nullptr) return;
  379. DISTRHO_SAFE_ASSERT_RETURN(angle > 0.0f,);
  380. nvgSkewX(fContext, angle);
  381. }
  382. void NanoVG::skewY(float angle)
  383. {
  384. if (fContext == nullptr) return;
  385. DISTRHO_SAFE_ASSERT_RETURN(angle > 0.0f,);
  386. nvgSkewY(fContext, angle);
  387. }
  388. void NanoVG::scale(float x, float y)
  389. {
  390. if (fContext == nullptr) return;
  391. DISTRHO_SAFE_ASSERT_RETURN(x > 0.0f,);
  392. DISTRHO_SAFE_ASSERT_RETURN(y > 0.0f,);
  393. nvgScale(fContext, x, y);
  394. }
  395. void NanoVG::currentTransform(float xform[6])
  396. {
  397. if (fContext != nullptr)
  398. nvgCurrentTransform(fContext, xform);
  399. }
  400. void NanoVG::transformIdentity(float dst[6])
  401. {
  402. nvgTransformIdentity(dst);
  403. }
  404. void NanoVG::transformTranslate(float dst[6], float tx, float ty)
  405. {
  406. nvgTransformTranslate(dst, tx, ty);
  407. }
  408. void NanoVG::transformScale(float dst[6], float sx, float sy)
  409. {
  410. nvgTransformScale(dst, sx, sy);
  411. }
  412. void NanoVG::transformRotate(float dst[6], float a)
  413. {
  414. nvgTransformRotate(dst, a);
  415. }
  416. void NanoVG::transformSkewX(float dst[6], float a)
  417. {
  418. nvgTransformSkewX(dst, a);
  419. }
  420. void NanoVG::transformSkewY(float dst[6], float a)
  421. {
  422. nvgTransformSkewY(dst, a);
  423. }
  424. void NanoVG::transformMultiply(float dst[6], const float src[6])
  425. {
  426. nvgTransformMultiply(dst, src);
  427. }
  428. void NanoVG::transformPremultiply(float dst[6], const float src[6])
  429. {
  430. nvgTransformPremultiply(dst, src);
  431. }
  432. int NanoVG::transformInverse(float dst[6], const float src[6])
  433. {
  434. return nvgTransformInverse(dst, src);
  435. }
  436. void NanoVG::transformPoint(float& dstx, float& dsty, const float xform[6], float srcx, float srcy)
  437. {
  438. nvgTransformPoint(&dstx, &dsty, xform, srcx, srcy);
  439. }
  440. float NanoVG::degToRad(float deg)
  441. {
  442. return nvgDegToRad(deg);
  443. }
  444. float NanoVG::radToDeg(float rad)
  445. {
  446. return nvgRadToDeg(rad);
  447. }
  448. // -----------------------------------------------------------------------
  449. // Images
  450. NanoImage::Handle NanoVG::createImageFromFile(const char* filename, ImageFlags imageFlags)
  451. {
  452. return createImageFromFile(filename, static_cast<int>(imageFlags));
  453. }
  454. NanoImage::Handle NanoVG::createImageFromFile(const char* filename, int imageFlags)
  455. {
  456. if (fContext == nullptr) return NanoImage::Handle();
  457. DISTRHO_SAFE_ASSERT_RETURN(filename != nullptr && filename[0] != '\0', NanoImage::Handle());
  458. return NanoImage::Handle(fContext, nvgCreateImage(fContext, filename, imageFlags));
  459. }
  460. NanoImage::Handle NanoVG::createImageFromMemory(uchar* data, uint dataSize, ImageFlags imageFlags)
  461. {
  462. return createImageFromMemory(data, dataSize, static_cast<int>(imageFlags));
  463. }
  464. NanoImage::Handle NanoVG::createImageFromMemory(uchar* data, uint dataSize, int imageFlags)
  465. {
  466. if (fContext == nullptr) return NanoImage::Handle();
  467. DISTRHO_SAFE_ASSERT_RETURN(data != nullptr, NanoImage::Handle());
  468. DISTRHO_SAFE_ASSERT_RETURN(dataSize > 0, NanoImage::Handle());
  469. return NanoImage::Handle(fContext, nvgCreateImageMem(fContext, imageFlags, data,static_cast<int>(dataSize)));
  470. }
  471. NanoImage::Handle NanoVG::createImageFromRGBA(uint w, uint h, const uchar* data, ImageFlags imageFlags)
  472. {
  473. return createImageFromRGBA(w, h, data, static_cast<int>(imageFlags));
  474. }
  475. NanoImage::Handle NanoVG::createImageFromRGBA(uint w, uint h, const uchar* data, int imageFlags)
  476. {
  477. if (fContext == nullptr) return NanoImage::Handle();
  478. DISTRHO_SAFE_ASSERT_RETURN(data != nullptr, NanoImage::Handle());
  479. return NanoImage::Handle(fContext, nvgCreateImageRGBA(fContext,
  480. static_cast<int>(w),
  481. static_cast<int>(h), imageFlags, data));
  482. }
  483. NanoImage::Handle NanoVG::createImageFromTextureHandle(GLuint textureId, uint w, uint h, ImageFlags imageFlags, bool deleteTexture)
  484. {
  485. return createImageFromTextureHandle(textureId, w, h, static_cast<int>(imageFlags), deleteTexture);
  486. }
  487. NanoImage::Handle NanoVG::createImageFromTextureHandle(GLuint textureId, uint w, uint h, int imageFlags, bool deleteTexture)
  488. {
  489. if (fContext == nullptr) return NanoImage::Handle();
  490. DISTRHO_SAFE_ASSERT_RETURN(textureId != 0, NanoImage::Handle());
  491. if (! deleteTexture)
  492. imageFlags |= NVG_IMAGE_NODELETE;
  493. return NanoImage::Handle(fContext, nvglCreateImageFromHandle(fContext,
  494. textureId,
  495. static_cast<int>(w),
  496. static_cast<int>(h), imageFlags));
  497. }
  498. // -----------------------------------------------------------------------
  499. // Paints
  500. NanoVG::Paint NanoVG::linearGradient(float sx, float sy, float ex, float ey, const Color& icol, const Color& ocol)
  501. {
  502. if (fContext == nullptr) return Paint();
  503. return nvgLinearGradient(fContext, sx, sy, ex, ey, icol, ocol);
  504. }
  505. NanoVG::Paint NanoVG::boxGradient(float x, float y, float w, float h, float r, float f, const Color& icol, const Color& ocol)
  506. {
  507. if (fContext == nullptr) return Paint();
  508. return nvgBoxGradient(fContext, x, y, w, h, r, f, icol, ocol);
  509. }
  510. NanoVG::Paint NanoVG::radialGradient(float cx, float cy, float inr, float outr, const Color& icol, const Color& ocol)
  511. {
  512. if (fContext == nullptr) return Paint();
  513. return nvgRadialGradient(fContext, cx, cy, inr, outr, icol, ocol);
  514. }
  515. NanoVG::Paint NanoVG::imagePattern(float ox, float oy, float ex, float ey, float angle, const NanoImage& image, float alpha)
  516. {
  517. if (fContext == nullptr) return Paint();
  518. const int imageId(image.fHandle.imageId);
  519. DISTRHO_SAFE_ASSERT_RETURN(imageId != 0, Paint());
  520. return nvgImagePattern(fContext, ox, oy, ex, ey, angle, imageId, alpha);
  521. }
  522. // -----------------------------------------------------------------------
  523. // Scissoring
  524. void NanoVG::scissor(float x, float y, float w, float h)
  525. {
  526. if (fContext != nullptr)
  527. nvgScissor(fContext, x, y, w, h);
  528. }
  529. void NanoVG::intersectScissor(float x, float y, float w, float h)
  530. {
  531. if (fContext != nullptr)
  532. nvgIntersectScissor(fContext, x, y, w, h);
  533. }
  534. void NanoVG::resetScissor()
  535. {
  536. if (fContext != nullptr)
  537. nvgResetScissor(fContext);
  538. }
  539. // -----------------------------------------------------------------------
  540. // Paths
  541. void NanoVG::beginPath()
  542. {
  543. if (fContext != nullptr)
  544. nvgBeginPath(fContext);
  545. }
  546. void NanoVG::moveTo(float x, float y)
  547. {
  548. if (fContext != nullptr)
  549. nvgMoveTo(fContext, x, y);
  550. }
  551. void NanoVG::lineTo(float x, float y)
  552. {
  553. if (fContext != nullptr)
  554. nvgLineTo(fContext, x, y);
  555. }
  556. void NanoVG::bezierTo(float c1x, float c1y, float c2x, float c2y, float x, float y)
  557. {
  558. if (fContext != nullptr)
  559. nvgBezierTo(fContext, c1x, c1y, c2x, c2y, x, y);
  560. }
  561. void NanoVG::quadTo(float cx, float cy, float x, float y)
  562. {
  563. if (fContext != nullptr)
  564. nvgQuadTo(fContext, cx, cy, x, y);
  565. }
  566. void NanoVG::arcTo(float x1, float y1, float x2, float y2, float radius)
  567. {
  568. if (fContext != nullptr)
  569. nvgArcTo(fContext, x1, y1, x2, y2, radius);
  570. }
  571. void NanoVG::closePath()
  572. {
  573. if (fContext != nullptr)
  574. nvgClosePath(fContext);
  575. }
  576. void NanoVG::pathWinding(NanoVG::Winding dir)
  577. {
  578. if (fContext != nullptr)
  579. nvgPathWinding(fContext, dir);
  580. }
  581. void NanoVG::arc(float cx, float cy, float r, float a0, float a1, NanoVG::Winding dir)
  582. {
  583. if (fContext != nullptr)
  584. nvgArc(fContext, cx, cy, r, a0, a1, dir);
  585. }
  586. void NanoVG::rect(float x, float y, float w, float h)
  587. {
  588. if (fContext != nullptr)
  589. nvgRect(fContext, x, y, w, h);
  590. }
  591. void NanoVG::roundedRect(float x, float y, float w, float h, float r)
  592. {
  593. if (fContext != nullptr)
  594. nvgRoundedRect(fContext, x, y, w, h, r);
  595. }
  596. void NanoVG::ellipse(float cx, float cy, float rx, float ry)
  597. {
  598. if (fContext != nullptr)
  599. nvgEllipse(fContext, cx, cy, rx, ry);
  600. }
  601. void NanoVG::circle(float cx, float cy, float r)
  602. {
  603. if (fContext != nullptr)
  604. nvgCircle(fContext, cx, cy, r);
  605. }
  606. void NanoVG::fill()
  607. {
  608. if (fContext != nullptr)
  609. nvgFill(fContext);
  610. }
  611. void NanoVG::stroke()
  612. {
  613. if (fContext != nullptr)
  614. nvgStroke(fContext);
  615. }
  616. // -----------------------------------------------------------------------
  617. // Text
  618. NanoVG::FontId NanoVG::createFontFromFile(const char* name, const char* filename)
  619. {
  620. if (fContext == nullptr) return -1;
  621. DISTRHO_SAFE_ASSERT_RETURN(name != nullptr && name[0] != '\0', -1);
  622. DISTRHO_SAFE_ASSERT_RETURN(filename != nullptr && filename[0] != '\0', -1);
  623. return nvgCreateFont(fContext, name, filename);
  624. }
  625. NanoVG::FontId NanoVG::createFontFromMemory(const char* name, const uchar* data, uint dataSize, bool freeData)
  626. {
  627. if (fContext == nullptr) return -1;
  628. DISTRHO_SAFE_ASSERT_RETURN(name != nullptr && name[0] != '\0', -1);
  629. DISTRHO_SAFE_ASSERT_RETURN(data != nullptr, -1);
  630. return nvgCreateFontMem(fContext, name, const_cast<uchar*>(data), static_cast<int>(dataSize), freeData);
  631. }
  632. NanoVG::FontId NanoVG::findFont(const char* name)
  633. {
  634. if (fContext == nullptr) return -1;
  635. DISTRHO_SAFE_ASSERT_RETURN(name != nullptr && name[0] != '\0', -1);
  636. return nvgFindFont(fContext, name);
  637. }
  638. void NanoVG::fontSize(float size)
  639. {
  640. if (fContext == nullptr) return;
  641. DISTRHO_SAFE_ASSERT_RETURN(size > 0.0f,);
  642. nvgFontSize(fContext, size);
  643. }
  644. void NanoVG::fontBlur(float blur)
  645. {
  646. if (fContext == nullptr) return;
  647. DISTRHO_SAFE_ASSERT_RETURN(blur >= 0.0f,);
  648. nvgFontBlur(fContext, blur);
  649. }
  650. void NanoVG::textLetterSpacing(float spacing)
  651. {
  652. if (fContext == nullptr) return;
  653. DISTRHO_SAFE_ASSERT_RETURN(spacing >= 0.0f,);
  654. nvgTextLetterSpacing(fContext, spacing);
  655. }
  656. void NanoVG::textLineHeight(float lineHeight)
  657. {
  658. if (fContext == nullptr) return;
  659. DISTRHO_SAFE_ASSERT_RETURN(lineHeight > 0.0f,);
  660. nvgTextLineHeight(fContext, lineHeight);
  661. }
  662. void NanoVG::textAlign(NanoVG::Align align)
  663. {
  664. if (fContext != nullptr)
  665. nvgTextAlign(fContext, align);
  666. }
  667. void NanoVG::textAlign(int align)
  668. {
  669. if (fContext != nullptr)
  670. nvgTextAlign(fContext, align);
  671. }
  672. void NanoVG::fontFaceId(FontId font)
  673. {
  674. if (fContext == nullptr) return;
  675. DISTRHO_SAFE_ASSERT_RETURN(font >= 0,);
  676. nvgFontFaceId(fContext, font);
  677. }
  678. void NanoVG::fontFace(const char* font)
  679. {
  680. if (fContext == nullptr) return;
  681. DISTRHO_SAFE_ASSERT_RETURN(font != nullptr && font[0] != '\0',);
  682. nvgFontFace(fContext, font);
  683. }
  684. float NanoVG::text(float x, float y, const char* string, const char* end)
  685. {
  686. if (fContext == nullptr) return 0.0f;
  687. DISTRHO_SAFE_ASSERT_RETURN(string != nullptr && string[0] != '\0', 0.0f);
  688. return nvgText(fContext, x, y, string, end);
  689. }
  690. void NanoVG::textBox(float x, float y, float breakRowWidth, const char* string, const char* end)
  691. {
  692. if (fContext == nullptr) return;
  693. DISTRHO_SAFE_ASSERT_RETURN(string != nullptr && string[0] != '\0',);
  694. nvgTextBox(fContext, x, y, breakRowWidth, string, end);
  695. }
  696. float NanoVG::textBounds(float x, float y, const char* string, const char* end, Rectangle<float>& bounds)
  697. {
  698. if (fContext == nullptr) return 0.0f;
  699. DISTRHO_SAFE_ASSERT_RETURN(string != nullptr && string[0] != '\0', 0.0f);
  700. float b[4];
  701. const float ret = nvgTextBounds(fContext, x, y, string, end, b);
  702. bounds = Rectangle<float>(b[0], b[1], b[2], b[3]);
  703. return ret;
  704. }
  705. void NanoVG::textBoxBounds(float x, float y, float breakRowWidth, const char* string, const char* end, float bounds[4])
  706. {
  707. if (fContext == nullptr) return;
  708. DISTRHO_SAFE_ASSERT_RETURN(string != nullptr && string[0] != '\0',);
  709. nvgTextBoxBounds(fContext, x, y, breakRowWidth, string, end, bounds);
  710. }
  711. int NanoVG::textGlyphPositions(float x, float y, const char* string, const char* end, NanoVG::GlyphPosition& positions, int maxPositions)
  712. {
  713. if (fContext == nullptr) return 0;
  714. DISTRHO_SAFE_ASSERT_RETURN(string != nullptr && string[0] != '\0', 0);
  715. return nvgTextGlyphPositions(fContext, x, y, string, end, (NVGglyphPosition*)&positions, maxPositions);
  716. }
  717. void NanoVG::textMetrics(float* ascender, float* descender, float* lineh)
  718. {
  719. if (fContext != nullptr)
  720. nvgTextMetrics(fContext, ascender, descender, lineh);
  721. }
  722. int NanoVG::textBreakLines(const char* string, const char* end, float breakRowWidth, NanoVG::TextRow& rows, int maxRows)
  723. {
  724. if (fContext != nullptr)
  725. return nvgTextBreakLines(fContext, string, end, breakRowWidth, (NVGtextRow*)&rows, maxRows);
  726. return 0;
  727. }
  728. #ifndef DGL_NO_SHARED_RESOURCES
  729. void NanoVG::loadSharedResources()
  730. {
  731. if (nvgFindFont(fContext, NANOVG_DEJAVU_SANS_TTF) >= 0)
  732. return;
  733. using namespace dpf_resources;
  734. nvgCreateFontMem(fContext, NANOVG_DEJAVU_SANS_TTF, (const uchar*)dejavusans_ttf, dejavusans_ttf_size, 0);
  735. }
  736. #endif
  737. // -----------------------------------------------------------------------
  738. struct NanoWidget::PrivateData {
  739. NanoWidget* const self;
  740. std::vector<NanoWidget*> subWidgets;
  741. PrivateData(NanoWidget* const s)
  742. : self(s),
  743. subWidgets() {}
  744. ~PrivateData()
  745. {
  746. subWidgets.clear();
  747. }
  748. };
  749. NanoWidget::NanoWidget(Window& parent, int flags)
  750. : Widget(parent),
  751. NanoVG(flags),
  752. nData(new PrivateData(this))
  753. {
  754. pData->needsScaling = true;
  755. }
  756. NanoWidget::NanoWidget(Widget* groupWidget, int flags)
  757. : Widget(groupWidget, true),
  758. NanoVG(flags),
  759. nData(new PrivateData(this))
  760. {
  761. pData->needsScaling = true;
  762. }
  763. NanoWidget::NanoWidget(NanoWidget* groupWidget)
  764. : Widget(groupWidget, false),
  765. NanoVG(groupWidget),
  766. nData(new PrivateData(this))
  767. {
  768. pData->needsScaling = true;
  769. pData->skipDisplay = true;
  770. groupWidget->nData->subWidgets.push_back(this);
  771. }
  772. NanoWidget::~NanoWidget()
  773. {
  774. delete nData;
  775. }
  776. void NanoWidget::onDisplay()
  777. {
  778. NanoVG::beginFrame(getWidth(), getHeight());
  779. onNanoDisplay();
  780. for (std::vector<NanoWidget*>::iterator it = nData->subWidgets.begin(); it != nData->subWidgets.end(); ++it)
  781. {
  782. NanoWidget* const widget(*it);
  783. widget->onNanoDisplay();
  784. }
  785. NanoVG::endFrame();
  786. }
  787. // -----------------------------------------------------------------------
  788. END_NAMESPACE_DGL
  789. #undef final
  790. #if defined(__GNUC__) && (__GNUC__ >= 6)
  791. # pragma GCC diagnostic push
  792. # pragma GCC diagnostic ignored "-Wmisleading-indentation"
  793. # pragma GCC diagnostic ignored "-Wshift-negative-value"
  794. #endif
  795. extern "C" {
  796. #include "nanovg/nanovg.c"
  797. }
  798. #if defined(__GNUC__) && (__GNUC__ >= 6)
  799. # pragma GCC diagnostic pop
  800. #endif
  801. // -----------------------------------------------------------------------