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.

juce_win32_Direct2DGraphicsContext.cpp 26KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780
  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2022 - Raw Material Software Limited
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. By using JUCE, you agree to the terms of both the JUCE 7 End-User License
  8. Agreement and JUCE Privacy Policy.
  9. End User License Agreement: www.juce.com/juce-7-licence
  10. Privacy Policy: www.juce.com/juce-privacy-policy
  11. Or: You may also use this code under the terms of the GPL v3 (see
  12. www.gnu.org/licenses).
  13. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  14. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  15. DISCLAIMED.
  16. ==============================================================================
  17. */
  18. namespace juce
  19. {
  20. template <typename Type>
  21. D2D1_RECT_F rectangleToRectF (const Rectangle<Type>& r)
  22. {
  23. return { (float) r.getX(), (float) r.getY(), (float) r.getRight(), (float) r.getBottom() };
  24. }
  25. static D2D1_COLOR_F colourToD2D (Colour c)
  26. {
  27. return { c.getFloatRed(), c.getFloatGreen(), c.getFloatBlue(), c.getFloatAlpha() };
  28. }
  29. static void pathToGeometrySink (const Path& path, ID2D1GeometrySink* sink, const AffineTransform& transform)
  30. {
  31. Path::Iterator it (path);
  32. while (it.next())
  33. {
  34. switch (it.elementType)
  35. {
  36. case Path::Iterator::cubicTo:
  37. {
  38. transform.transformPoint (it.x1, it.y1);
  39. transform.transformPoint (it.x2, it.y2);
  40. transform.transformPoint (it.x3, it.y3);
  41. sink->AddBezier ({ { it.x1, it.y1 }, { it.x2, it.y2 }, { it.x3, it.y3 } });
  42. break;
  43. }
  44. case Path::Iterator::lineTo:
  45. {
  46. transform.transformPoint (it.x1, it.y1);
  47. sink->AddLine ({ it.x1, it.y1 });
  48. break;
  49. }
  50. case Path::Iterator::quadraticTo:
  51. {
  52. transform.transformPoint (it.x1, it.y1);
  53. transform.transformPoint (it.x2, it.y2);
  54. sink->AddQuadraticBezier ({ { it.x1, it.y1 }, { it.x2, it.y2 } });
  55. break;
  56. }
  57. case Path::Iterator::closePath:
  58. {
  59. sink->EndFigure (D2D1_FIGURE_END_CLOSED);
  60. break;
  61. }
  62. case Path::Iterator::startNewSubPath:
  63. {
  64. transform.transformPoint (it.x1, it.y1);
  65. sink->BeginFigure ({ it.x1, it.y1 }, D2D1_FIGURE_BEGIN_FILLED);
  66. break;
  67. }
  68. }
  69. }
  70. }
  71. static D2D1::Matrix3x2F transformToMatrix (const AffineTransform& transform)
  72. {
  73. return { transform.mat00, transform.mat10, transform.mat01, transform.mat11, transform.mat02, transform.mat12 };
  74. }
  75. static D2D1_POINT_2F pointTransformed (int x, int y, const AffineTransform& transform)
  76. {
  77. transform.transformPoint (x, y);
  78. return { (FLOAT) x, (FLOAT) y };
  79. }
  80. static void rectToGeometrySink (const Rectangle<int>& rect, ID2D1GeometrySink* sink, const AffineTransform& transform)
  81. {
  82. sink->BeginFigure (pointTransformed (rect.getX(), rect.getY(), transform), D2D1_FIGURE_BEGIN_FILLED);
  83. sink->AddLine (pointTransformed (rect.getRight(), rect.getY(), transform));
  84. sink->AddLine (pointTransformed (rect.getRight(), rect.getBottom(), transform));
  85. sink->AddLine (pointTransformed (rect.getX(), rect.getBottom(), transform));
  86. sink->EndFigure (D2D1_FIGURE_END_CLOSED);
  87. }
  88. //==============================================================================
  89. struct Direct2DLowLevelGraphicsContext::Pimpl
  90. {
  91. ID2D1PathGeometry* rectListToPathGeometry (const RectangleList<int>& clipRegion)
  92. {
  93. ID2D1PathGeometry* p = nullptr;
  94. factories->d2dFactory->CreatePathGeometry (&p);
  95. ComSmartPtr<ID2D1GeometrySink> sink;
  96. auto hr = p->Open (sink.resetAndGetPointerAddress()); // xxx handle error
  97. sink->SetFillMode (D2D1_FILL_MODE_WINDING);
  98. for (int i = clipRegion.getNumRectangles(); --i >= 0;)
  99. rectToGeometrySink (clipRegion.getRectangle(i), sink, AffineTransform());
  100. hr = sink->Close();
  101. return p;
  102. }
  103. ID2D1PathGeometry* pathToPathGeometry (const Path& path, const AffineTransform& transform)
  104. {
  105. ID2D1PathGeometry* p = nullptr;
  106. factories->d2dFactory->CreatePathGeometry (&p);
  107. ComSmartPtr<ID2D1GeometrySink> sink;
  108. auto hr = p->Open (sink.resetAndGetPointerAddress());
  109. sink->SetFillMode (D2D1_FILL_MODE_WINDING); // xxx need to check Path::isUsingNonZeroWinding()
  110. pathToGeometrySink (path, sink, transform);
  111. hr = sink->Close();
  112. return p;
  113. }
  114. SharedResourcePointer<Direct2DFactories> factories;
  115. ComSmartPtr<ID2D1HwndRenderTarget> renderingTarget;
  116. ComSmartPtr<ID2D1SolidColorBrush> colourBrush;
  117. };
  118. //==============================================================================
  119. struct Direct2DLowLevelGraphicsContext::SavedState
  120. {
  121. public:
  122. SavedState (Direct2DLowLevelGraphicsContext& owner_)
  123. : owner (owner_)
  124. {
  125. if (owner.currentState != nullptr)
  126. {
  127. // xxx seems like a very slow way to create one of these, and this is a performance
  128. // bottleneck.. Can the same internal objects be shared by multiple state objects, maybe using copy-on-write?
  129. setFill (owner.currentState->fillType);
  130. currentBrush = owner.currentState->currentBrush;
  131. clipRect = owner.currentState->clipRect;
  132. transform = owner.currentState->transform;
  133. font = owner.currentState->font;
  134. currentFontFace = owner.currentState->currentFontFace;
  135. }
  136. else
  137. {
  138. const auto size = owner.pimpl->renderingTarget->GetPixelSize();
  139. clipRect.setSize (size.width, size.height);
  140. setFill (FillType (Colours::black));
  141. }
  142. }
  143. ~SavedState()
  144. {
  145. clearClip();
  146. clearFont();
  147. clearFill();
  148. clearPathClip();
  149. clearImageClip();
  150. complexClipLayer = nullptr;
  151. bitmapMaskLayer = nullptr;
  152. }
  153. void clearClip()
  154. {
  155. popClips();
  156. shouldClipRect = false;
  157. }
  158. void clipToRectangle (const Rectangle<int>& r)
  159. {
  160. clearClip();
  161. clipRect = r.toFloat().transformedBy (transform).getSmallestIntegerContainer();
  162. shouldClipRect = true;
  163. pushClips();
  164. }
  165. void clearPathClip()
  166. {
  167. popClips();
  168. if (shouldClipComplex)
  169. {
  170. complexClipGeometry = nullptr;
  171. shouldClipComplex = false;
  172. }
  173. }
  174. void Direct2DLowLevelGraphicsContext::SavedState::clipToPath (ID2D1Geometry* geometry)
  175. {
  176. clearPathClip();
  177. if (complexClipLayer == nullptr)
  178. owner.pimpl->renderingTarget->CreateLayer (complexClipLayer.resetAndGetPointerAddress());
  179. complexClipGeometry = geometry;
  180. shouldClipComplex = true;
  181. pushClips();
  182. }
  183. void clearRectListClip()
  184. {
  185. popClips();
  186. if (shouldClipRectList)
  187. {
  188. rectListGeometry = nullptr;
  189. shouldClipRectList = false;
  190. }
  191. }
  192. void clipToRectList (ID2D1Geometry* geometry)
  193. {
  194. clearRectListClip();
  195. if (rectListLayer == nullptr)
  196. owner.pimpl->renderingTarget->CreateLayer (rectListLayer.resetAndGetPointerAddress());
  197. rectListGeometry = geometry;
  198. shouldClipRectList = true;
  199. pushClips();
  200. }
  201. void clearImageClip()
  202. {
  203. popClips();
  204. if (shouldClipBitmap)
  205. {
  206. maskBitmap = nullptr;
  207. bitmapMaskBrush = nullptr;
  208. shouldClipBitmap = false;
  209. }
  210. }
  211. void clipToImage (const Image& clipImage, const AffineTransform& clipTransform)
  212. {
  213. clearImageClip();
  214. if (bitmapMaskLayer == nullptr)
  215. owner.pimpl->renderingTarget->CreateLayer (bitmapMaskLayer.resetAndGetPointerAddress());
  216. D2D1_BRUSH_PROPERTIES brushProps = { 1, transformToMatrix (clipTransform) };
  217. auto bmProps = D2D1::BitmapBrushProperties (D2D1_EXTEND_MODE_WRAP, D2D1_EXTEND_MODE_WRAP);
  218. D2D1_SIZE_U size = { (UINT32) clipImage.getWidth(), (UINT32) clipImage.getHeight() };
  219. auto bp = D2D1::BitmapProperties();
  220. maskImage = clipImage.convertedToFormat (Image::ARGB);
  221. Image::BitmapData bd (maskImage, Image::BitmapData::readOnly); // xxx should be maskImage?
  222. bp.pixelFormat = owner.pimpl->renderingTarget->GetPixelFormat();
  223. bp.pixelFormat.alphaMode = D2D1_ALPHA_MODE_PREMULTIPLIED;
  224. auto hr = owner.pimpl->renderingTarget->CreateBitmap (size, bd.data, bd.lineStride, bp, maskBitmap.resetAndGetPointerAddress());
  225. hr = owner.pimpl->renderingTarget->CreateBitmapBrush (maskBitmap, bmProps, brushProps, bitmapMaskBrush.resetAndGetPointerAddress());
  226. imageMaskLayerParams = D2D1::LayerParameters();
  227. imageMaskLayerParams.opacityBrush = bitmapMaskBrush;
  228. shouldClipBitmap = true;
  229. pushClips();
  230. }
  231. void popClips()
  232. {
  233. if (clipsBitmap)
  234. {
  235. owner.pimpl->renderingTarget->PopLayer();
  236. clipsBitmap = false;
  237. }
  238. if (clipsComplex)
  239. {
  240. owner.pimpl->renderingTarget->PopLayer();
  241. clipsComplex = false;
  242. }
  243. if (clipsRectList)
  244. {
  245. owner.pimpl->renderingTarget->PopLayer();
  246. clipsRectList = false;
  247. }
  248. if (clipsRect)
  249. {
  250. owner.pimpl->renderingTarget->PopAxisAlignedClip();
  251. clipsRect = false;
  252. }
  253. }
  254. void pushClips()
  255. {
  256. if (shouldClipRect && !clipsRect)
  257. {
  258. owner.pimpl->renderingTarget->PushAxisAlignedClip (rectangleToRectF (clipRect), D2D1_ANTIALIAS_MODE_PER_PRIMITIVE);
  259. clipsRect = true;
  260. }
  261. if (shouldClipRectList && !clipsRectList)
  262. {
  263. auto layerParams = D2D1::LayerParameters();
  264. rectListGeometry->GetBounds (D2D1::IdentityMatrix(), &layerParams.contentBounds);
  265. layerParams.geometricMask = rectListGeometry;
  266. owner.pimpl->renderingTarget->PushLayer (layerParams, rectListLayer);
  267. clipsRectList = true;
  268. }
  269. if (shouldClipComplex && !clipsComplex)
  270. {
  271. auto layerParams = D2D1::LayerParameters();
  272. complexClipGeometry->GetBounds (D2D1::IdentityMatrix(), &layerParams.contentBounds);
  273. layerParams.geometricMask = complexClipGeometry;
  274. owner.pimpl->renderingTarget->PushLayer (layerParams, complexClipLayer);
  275. clipsComplex = true;
  276. }
  277. if (shouldClipBitmap && !clipsBitmap)
  278. {
  279. owner.pimpl->renderingTarget->PushLayer (imageMaskLayerParams, bitmapMaskLayer);
  280. clipsBitmap = true;
  281. }
  282. }
  283. void setFill (const FillType& newFillType)
  284. {
  285. if (fillType != newFillType)
  286. {
  287. fillType = newFillType;
  288. clearFill();
  289. }
  290. }
  291. void clearFont()
  292. {
  293. currentFontFace = localFontFace = nullptr;
  294. }
  295. void setFont (const Font& newFont)
  296. {
  297. if (font != newFont)
  298. {
  299. font = newFont;
  300. clearFont();
  301. }
  302. }
  303. void createFont()
  304. {
  305. if (currentFontFace == nullptr)
  306. {
  307. auto typefacePtr = font.getTypefacePtr();
  308. auto* typeface = dynamic_cast<WindowsDirectWriteTypeface*> (typefacePtr.get());
  309. currentFontFace = typeface->getIDWriteFontFace();
  310. fontHeightToEmSizeFactor = typeface->getUnitsToHeightScaleFactor();
  311. }
  312. }
  313. void setOpacity (float newOpacity)
  314. {
  315. fillType.setOpacity (newOpacity);
  316. if (currentBrush != nullptr)
  317. currentBrush->SetOpacity (newOpacity);
  318. }
  319. void clearFill()
  320. {
  321. gradientStops = nullptr;
  322. linearGradient = nullptr;
  323. radialGradient = nullptr;
  324. bitmap = nullptr;
  325. bitmapBrush = nullptr;
  326. currentBrush = nullptr;
  327. }
  328. void createBrush()
  329. {
  330. if (currentBrush == nullptr)
  331. {
  332. if (fillType.isColour())
  333. {
  334. auto colour = colourToD2D (fillType.colour);
  335. owner.pimpl->colourBrush->SetColor (colour);
  336. currentBrush = owner.pimpl->colourBrush;
  337. }
  338. else if (fillType.isTiledImage())
  339. {
  340. D2D1_BRUSH_PROPERTIES brushProps = { fillType.getOpacity(), transformToMatrix (fillType.transform) };
  341. auto bmProps = D2D1::BitmapBrushProperties (D2D1_EXTEND_MODE_WRAP, D2D1_EXTEND_MODE_WRAP);
  342. image = fillType.image;
  343. D2D1_SIZE_U size = { (UINT32) image.getWidth(), (UINT32) image.getHeight() };
  344. auto bp = D2D1::BitmapProperties();
  345. this->image = image.convertedToFormat (Image::ARGB);
  346. Image::BitmapData bd (this->image, Image::BitmapData::readOnly);
  347. bp.pixelFormat = owner.pimpl->renderingTarget->GetPixelFormat();
  348. bp.pixelFormat.alphaMode = D2D1_ALPHA_MODE_PREMULTIPLIED;
  349. auto hr = owner.pimpl->renderingTarget->CreateBitmap (size, bd.data, bd.lineStride, bp, bitmap.resetAndGetPointerAddress());
  350. hr = owner.pimpl->renderingTarget->CreateBitmapBrush (bitmap, bmProps, brushProps, bitmapBrush.resetAndGetPointerAddress());
  351. currentBrush = bitmapBrush;
  352. }
  353. else if (fillType.isGradient())
  354. {
  355. gradientStops = nullptr;
  356. D2D1_BRUSH_PROPERTIES brushProps = { fillType.getOpacity(), transformToMatrix (fillType.transform.followedBy (transform)) };
  357. const int numColors = fillType.gradient->getNumColours();
  358. HeapBlock<D2D1_GRADIENT_STOP> stops (numColors);
  359. for (int i = fillType.gradient->getNumColours(); --i >= 0;)
  360. {
  361. stops[i].color = colourToD2D (fillType.gradient->getColour (i));
  362. stops[i].position = (FLOAT) fillType.gradient->getColourPosition (i);
  363. }
  364. owner.pimpl->renderingTarget->CreateGradientStopCollection (stops.getData(), numColors, gradientStops.resetAndGetPointerAddress());
  365. if (fillType.gradient->isRadial)
  366. {
  367. radialGradient = nullptr;
  368. const auto p1 = fillType.gradient->point1;
  369. const auto p2 = fillType.gradient->point2;
  370. const auto r = p1.getDistanceFrom(p2);
  371. const auto props = D2D1::RadialGradientBrushProperties ({ p1.x, p1.y }, {}, r, r);
  372. owner.pimpl->renderingTarget->CreateRadialGradientBrush (props, brushProps, gradientStops, radialGradient.resetAndGetPointerAddress());
  373. currentBrush = radialGradient;
  374. }
  375. else
  376. {
  377. linearGradient = 0;
  378. const auto p1 = fillType.gradient->point1;
  379. const auto p2 = fillType.gradient->point2;
  380. const auto props = D2D1::LinearGradientBrushProperties ({ p1.x, p1.y }, { p2.x, p2.y });
  381. owner.pimpl->renderingTarget->CreateLinearGradientBrush (props, brushProps, gradientStops, linearGradient.resetAndGetPointerAddress());
  382. currentBrush = linearGradient;
  383. }
  384. }
  385. }
  386. }
  387. Direct2DLowLevelGraphicsContext& owner;
  388. AffineTransform transform;
  389. Font font;
  390. float fontHeightToEmSizeFactor = 1.0f;
  391. IDWriteFontFace* currentFontFace = nullptr;
  392. ComSmartPtr<IDWriteFontFace> localFontFace;
  393. Rectangle<int> clipRect;
  394. bool clipsRect = false, shouldClipRect = false;
  395. Image image;
  396. ComSmartPtr<ID2D1Bitmap> bitmap; // xxx needs a better name - what is this for??
  397. bool clipsBitmap = false, shouldClipBitmap = false;
  398. ComSmartPtr<ID2D1Geometry> complexClipGeometry;
  399. D2D1_LAYER_PARAMETERS complexClipLayerParams;
  400. ComSmartPtr<ID2D1Layer> complexClipLayer;
  401. bool clipsComplex = false, shouldClipComplex = false;
  402. ComSmartPtr<ID2D1Geometry> rectListGeometry;
  403. D2D1_LAYER_PARAMETERS rectListLayerParams;
  404. ComSmartPtr<ID2D1Layer> rectListLayer;
  405. bool clipsRectList = false, shouldClipRectList = false;
  406. Image maskImage;
  407. D2D1_LAYER_PARAMETERS imageMaskLayerParams;
  408. ComSmartPtr<ID2D1Layer> bitmapMaskLayer;
  409. ComSmartPtr<ID2D1Bitmap> maskBitmap;
  410. ComSmartPtr<ID2D1BitmapBrush> bitmapMaskBrush;
  411. ID2D1Brush* currentBrush = nullptr;
  412. ComSmartPtr<ID2D1BitmapBrush> bitmapBrush;
  413. ComSmartPtr<ID2D1LinearGradientBrush> linearGradient;
  414. ComSmartPtr<ID2D1RadialGradientBrush> radialGradient;
  415. ComSmartPtr<ID2D1GradientStopCollection> gradientStops;
  416. FillType fillType;
  417. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (SavedState)
  418. };
  419. //==============================================================================
  420. Direct2DLowLevelGraphicsContext::Direct2DLowLevelGraphicsContext (HWND hwnd_)
  421. : hwnd (hwnd_),
  422. currentState (nullptr),
  423. pimpl (new Pimpl())
  424. {
  425. RECT windowRect;
  426. GetClientRect (hwnd, &windowRect);
  427. D2D1_SIZE_U size = { (UINT32) (windowRect.right - windowRect.left), (UINT32) (windowRect.bottom - windowRect.top) };
  428. bounds.setSize (size.width, size.height);
  429. if (pimpl->factories->d2dFactory != nullptr)
  430. {
  431. auto hr = pimpl->factories->d2dFactory->CreateHwndRenderTarget ({}, { hwnd, size }, pimpl->renderingTarget.resetAndGetPointerAddress());
  432. jassert (SUCCEEDED (hr)); ignoreUnused (hr);
  433. hr = pimpl->renderingTarget->CreateSolidColorBrush (D2D1::ColorF::ColorF (0.0f, 0.0f, 0.0f, 1.0f), pimpl->colourBrush.resetAndGetPointerAddress());
  434. }
  435. }
  436. Direct2DLowLevelGraphicsContext::~Direct2DLowLevelGraphicsContext()
  437. {
  438. states.clear();
  439. }
  440. void Direct2DLowLevelGraphicsContext::resized()
  441. {
  442. RECT windowRect;
  443. GetClientRect (hwnd, &windowRect);
  444. D2D1_SIZE_U size = { (UINT32) (windowRect.right - windowRect.left), (UINT32) (windowRect.bottom - windowRect.top) };
  445. pimpl->renderingTarget->Resize (size);
  446. bounds.setSize (size.width, size.height);
  447. }
  448. void Direct2DLowLevelGraphicsContext::clear()
  449. {
  450. pimpl->renderingTarget->Clear (D2D1::ColorF (D2D1::ColorF::White, 0.0f)); // xxx why white and not black?
  451. }
  452. void Direct2DLowLevelGraphicsContext::start()
  453. {
  454. pimpl->renderingTarget->BeginDraw();
  455. saveState();
  456. }
  457. void Direct2DLowLevelGraphicsContext::end()
  458. {
  459. states.clear();
  460. currentState = nullptr;
  461. pimpl->renderingTarget->EndDraw();
  462. pimpl->renderingTarget->CheckWindowState();
  463. }
  464. void Direct2DLowLevelGraphicsContext::setOrigin (Point<int> o)
  465. {
  466. addTransform (AffineTransform::translation ((float) o.x, (float) o.y));
  467. }
  468. void Direct2DLowLevelGraphicsContext::addTransform (const AffineTransform& transform)
  469. {
  470. currentState->transform = transform.followedBy (currentState->transform);
  471. }
  472. float Direct2DLowLevelGraphicsContext::getPhysicalPixelScaleFactor()
  473. {
  474. return std::sqrt (std::abs (currentState->transform.getDeterminant()));
  475. }
  476. bool Direct2DLowLevelGraphicsContext::clipToRectangle (const Rectangle<int>& r)
  477. {
  478. currentState->clipToRectangle (r);
  479. return ! isClipEmpty();
  480. }
  481. bool Direct2DLowLevelGraphicsContext::clipToRectangleList (const RectangleList<int>& clipRegion)
  482. {
  483. currentState->clipToRectList (pimpl->rectListToPathGeometry (clipRegion));
  484. return ! isClipEmpty();
  485. }
  486. void Direct2DLowLevelGraphicsContext::excludeClipRectangle (const Rectangle<int>&)
  487. {
  488. //xxx
  489. }
  490. void Direct2DLowLevelGraphicsContext::clipToPath (const Path& path, const AffineTransform& transform)
  491. {
  492. currentState->clipToPath (pimpl->pathToPathGeometry (path, transform));
  493. }
  494. void Direct2DLowLevelGraphicsContext::clipToImageAlpha (const Image& sourceImage, const AffineTransform& transform)
  495. {
  496. currentState->clipToImage (sourceImage, transform);
  497. }
  498. bool Direct2DLowLevelGraphicsContext::clipRegionIntersects (const Rectangle<int>& r)
  499. {
  500. return currentState->clipRect.intersects (r.toFloat().transformedBy (currentState->transform).getSmallestIntegerContainer());
  501. }
  502. Rectangle<int> Direct2DLowLevelGraphicsContext::getClipBounds() const
  503. {
  504. // xxx could this take into account complex clip regions?
  505. return currentState->clipRect.toFloat().transformedBy (currentState->transform.inverted()).getSmallestIntegerContainer();
  506. }
  507. bool Direct2DLowLevelGraphicsContext::isClipEmpty() const
  508. {
  509. return currentState->clipRect.isEmpty();
  510. }
  511. void Direct2DLowLevelGraphicsContext::saveState()
  512. {
  513. states.add (new SavedState (*this));
  514. currentState = states.getLast();
  515. }
  516. void Direct2DLowLevelGraphicsContext::restoreState()
  517. {
  518. jassert (states.size() > 1); //you should never pop the last state!
  519. states.removeLast (1);
  520. currentState = states.getLast();
  521. }
  522. void Direct2DLowLevelGraphicsContext::beginTransparencyLayer (float /*opacity*/)
  523. {
  524. jassertfalse; //xxx todo
  525. }
  526. void Direct2DLowLevelGraphicsContext::endTransparencyLayer()
  527. {
  528. jassertfalse; //xxx todo
  529. }
  530. void Direct2DLowLevelGraphicsContext::setFill (const FillType& fillType)
  531. {
  532. currentState->setFill (fillType);
  533. }
  534. void Direct2DLowLevelGraphicsContext::setOpacity (float newOpacity)
  535. {
  536. currentState->setOpacity (newOpacity);
  537. }
  538. void Direct2DLowLevelGraphicsContext::setInterpolationQuality (Graphics::ResamplingQuality /*quality*/)
  539. {
  540. }
  541. void Direct2DLowLevelGraphicsContext::fillRect (const Rectangle<int>& r, bool /*replaceExistingContents*/)
  542. {
  543. fillRect (r.toFloat());
  544. }
  545. void Direct2DLowLevelGraphicsContext::fillRect (const Rectangle<float>& r)
  546. {
  547. pimpl->renderingTarget->SetTransform (transformToMatrix (currentState->transform));
  548. currentState->createBrush();
  549. pimpl->renderingTarget->FillRectangle (rectangleToRectF (r), currentState->currentBrush);
  550. pimpl->renderingTarget->SetTransform (D2D1::IdentityMatrix());
  551. }
  552. void Direct2DLowLevelGraphicsContext::fillRectList (const RectangleList<float>& list)
  553. {
  554. for (auto& r : list)
  555. fillRect (r);
  556. }
  557. void Direct2DLowLevelGraphicsContext::fillPath (const Path& p, const AffineTransform& transform)
  558. {
  559. currentState->createBrush();
  560. ComSmartPtr<ID2D1Geometry> geometry (pimpl->pathToPathGeometry (p, transform.followedBy (currentState->transform)));
  561. if (pimpl->renderingTarget != nullptr)
  562. pimpl->renderingTarget->FillGeometry (geometry, currentState->currentBrush);
  563. }
  564. void Direct2DLowLevelGraphicsContext::drawImage (const Image& image, const AffineTransform& transform)
  565. {
  566. pimpl->renderingTarget->SetTransform (transformToMatrix (transform.followedBy (currentState->transform)));
  567. D2D1_SIZE_U size = { (UINT32) image.getWidth(), (UINT32) image.getHeight() };
  568. auto bp = D2D1::BitmapProperties();
  569. Image img (image.convertedToFormat (Image::ARGB));
  570. Image::BitmapData bd (img, Image::BitmapData::readOnly);
  571. bp.pixelFormat = pimpl->renderingTarget->GetPixelFormat();
  572. bp.pixelFormat.alphaMode = D2D1_ALPHA_MODE_PREMULTIPLIED;
  573. {
  574. ComSmartPtr<ID2D1Bitmap> tempBitmap;
  575. pimpl->renderingTarget->CreateBitmap (size, bd.data, bd.lineStride, bp, tempBitmap.resetAndGetPointerAddress());
  576. if (tempBitmap != nullptr)
  577. pimpl->renderingTarget->DrawBitmap (tempBitmap);
  578. }
  579. pimpl->renderingTarget->SetTransform (D2D1::IdentityMatrix());
  580. }
  581. void Direct2DLowLevelGraphicsContext::drawLine (const Line<float>& line)
  582. {
  583. // xxx doesn't seem to be correctly aligned, may need nudging by 0.5 to match the software renderer's behaviour
  584. pimpl->renderingTarget->SetTransform (transformToMatrix (currentState->transform));
  585. currentState->createBrush();
  586. pimpl->renderingTarget->DrawLine (D2D1::Point2F (line.getStartX(), line.getStartY()),
  587. D2D1::Point2F (line.getEndX(), line.getEndY()),
  588. currentState->currentBrush);
  589. pimpl->renderingTarget->SetTransform (D2D1::IdentityMatrix());
  590. }
  591. void Direct2DLowLevelGraphicsContext::setFont (const Font& newFont)
  592. {
  593. currentState->setFont (newFont);
  594. }
  595. const Font& Direct2DLowLevelGraphicsContext::getFont()
  596. {
  597. return currentState->font;
  598. }
  599. void Direct2DLowLevelGraphicsContext::drawGlyph (int glyphNumber, const AffineTransform& transform)
  600. {
  601. currentState->createBrush();
  602. currentState->createFont();
  603. auto hScale = currentState->font.getHorizontalScale();
  604. pimpl->renderingTarget->SetTransform (transformToMatrix (AffineTransform::scale (hScale, 1.0f)
  605. .followedBy (transform)
  606. .followedBy (currentState->transform)));
  607. const auto glyphIndices = (UINT16) glyphNumber;
  608. const auto glyphAdvances = 0.0f;
  609. DWRITE_GLYPH_OFFSET offset = { 0.0f, 0.0f };
  610. DWRITE_GLYPH_RUN glyphRun;
  611. glyphRun.fontFace = currentState->currentFontFace;
  612. glyphRun.fontEmSize = (FLOAT) (currentState->font.getHeight() * currentState->fontHeightToEmSizeFactor);
  613. glyphRun.glyphCount = 1;
  614. glyphRun.glyphIndices = &glyphIndices;
  615. glyphRun.glyphAdvances = &glyphAdvances;
  616. glyphRun.glyphOffsets = &offset;
  617. glyphRun.isSideways = FALSE;
  618. glyphRun.bidiLevel = 0;
  619. pimpl->renderingTarget->DrawGlyphRun ({}, &glyphRun, currentState->currentBrush);
  620. pimpl->renderingTarget->SetTransform (D2D1::IdentityMatrix());
  621. }
  622. bool Direct2DLowLevelGraphicsContext::drawTextLayout (const AttributedString& text, const Rectangle<float>& area)
  623. {
  624. pimpl->renderingTarget->SetTransform (transformToMatrix (currentState->transform));
  625. DirectWriteTypeLayout::drawToD2DContext (text, area,
  626. *(pimpl->renderingTarget),
  627. *(pimpl->factories->directWriteFactory),
  628. *(pimpl->factories->systemFonts));
  629. pimpl->renderingTarget->SetTransform (D2D1::IdentityMatrix());
  630. return true;
  631. }
  632. } // namespace juce