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.

840 lines
29KB

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