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.

852 lines
30KB

  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 (int x, int y)
  68. {
  69. addTransform (AffineTransform::translation ((float) x, (float) y));
  70. }
  71. void addTransform (const AffineTransform& transform)
  72. {
  73. currentState->transform = transform.followedBy (currentState->transform);
  74. }
  75. float getScaleFactor()
  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. renderingTarget->SetTransform (transformToMatrix (currentState->transform));
  147. currentState->createBrush();
  148. renderingTarget->FillRectangle (rectangleToRectF (r), currentState->currentBrush);
  149. renderingTarget->SetTransform (D2D1::IdentityMatrix());
  150. }
  151. void fillPath (const Path& p, const AffineTransform& transform)
  152. {
  153. currentState->createBrush();
  154. ComSmartPtr <ID2D1Geometry> geometry (pathToPathGeometry (p, transform.followedBy (currentState->transform)));
  155. if (renderingTarget != nullptr)
  156. renderingTarget->FillGeometry (geometry, currentState->currentBrush);
  157. }
  158. void drawImage (const Image& image, const AffineTransform& transform)
  159. {
  160. renderingTarget->SetTransform (transformToMatrix (transform.followedBy (currentState->transform)));
  161. D2D1_SIZE_U size;
  162. size.width = image.getWidth();
  163. size.height = image.getHeight();
  164. D2D1_BITMAP_PROPERTIES bp = D2D1::BitmapProperties();
  165. Image img (image.convertedToFormat (Image::ARGB));
  166. Image::BitmapData bd (img, Image::BitmapData::readOnly);
  167. bp.pixelFormat = renderingTarget->GetPixelFormat();
  168. bp.pixelFormat.alphaMode = D2D1_ALPHA_MODE_PREMULTIPLIED;
  169. {
  170. ComSmartPtr <ID2D1Bitmap> tempBitmap;
  171. renderingTarget->CreateBitmap (size, bd.data, bd.lineStride, bp, tempBitmap.resetAndGetPointerAddress());
  172. if (tempBitmap != nullptr)
  173. renderingTarget->DrawBitmap (tempBitmap);
  174. }
  175. renderingTarget->SetTransform (D2D1::IdentityMatrix());
  176. }
  177. void drawLine (const Line <float>& line)
  178. {
  179. // xxx doesn't seem to be correctly aligned, may need nudging by 0.5 to match the software renderer's behaviour
  180. renderingTarget->SetTransform (transformToMatrix (currentState->transform));
  181. currentState->createBrush();
  182. renderingTarget->DrawLine (D2D1::Point2F (line.getStartX(), line.getStartY()),
  183. D2D1::Point2F (line.getEndX(), line.getEndY()),
  184. currentState->currentBrush);
  185. renderingTarget->SetTransform (D2D1::IdentityMatrix());
  186. }
  187. void drawVerticalLine (int x, float top, float bottom)
  188. {
  189. // xxx doesn't seem to be correctly aligned, may need nudging by 0.5 to match the software renderer's behaviour
  190. renderingTarget->SetTransform (transformToMatrix (currentState->transform));
  191. currentState->createBrush();
  192. renderingTarget->DrawLine (D2D1::Point2F ((FLOAT) x, top),
  193. D2D1::Point2F ((FLOAT) x, bottom),
  194. currentState->currentBrush);
  195. renderingTarget->SetTransform (D2D1::IdentityMatrix());
  196. }
  197. void drawHorizontalLine (int y, float left, float right)
  198. {
  199. // xxx doesn't seem to be correctly aligned, may need nudging by 0.5 to match the software renderer's behaviour
  200. renderingTarget->SetTransform (transformToMatrix (currentState->transform));
  201. currentState->createBrush();
  202. renderingTarget->DrawLine (D2D1::Point2F (left, (FLOAT) y),
  203. D2D1::Point2F (right, (FLOAT) y),
  204. currentState->currentBrush);
  205. renderingTarget->SetTransform (D2D1::IdentityMatrix());
  206. }
  207. void setFont (const Font& newFont)
  208. {
  209. currentState->setFont (newFont);
  210. }
  211. const Font& getFont()
  212. {
  213. return currentState->font;
  214. }
  215. void drawGlyph (int glyphNumber, const AffineTransform& transform)
  216. {
  217. currentState->createBrush();
  218. currentState->createFont();
  219. float hScale = currentState->font.getHorizontalScale();
  220. renderingTarget->SetTransform (transformToMatrix (AffineTransform::scale (hScale, 1.0f)
  221. .followedBy (transform)
  222. .followedBy (currentState->transform)));
  223. const UINT16 glyphIndices = (UINT16) glyphNumber;
  224. const FLOAT glyphAdvances = 0;
  225. DWRITE_GLYPH_OFFSET offset;
  226. offset.advanceOffset = 0;
  227. offset.ascenderOffset = 0;
  228. DWRITE_GLYPH_RUN glyphRun;
  229. glyphRun.fontFace = currentState->currentFontFace;
  230. glyphRun.fontEmSize = (FLOAT) (currentState->font.getHeight() * currentState->fontHeightToEmSizeFactor);
  231. glyphRun.glyphCount = 1;
  232. glyphRun.glyphIndices = &glyphIndices;
  233. glyphRun.glyphAdvances = &glyphAdvances;
  234. glyphRun.glyphOffsets = &offset;
  235. glyphRun.isSideways = FALSE;
  236. glyphRun.bidiLevel = 0;
  237. renderingTarget->DrawGlyphRun (D2D1::Point2F (0, 0), &glyphRun, currentState->currentBrush);
  238. renderingTarget->SetTransform (D2D1::IdentityMatrix());
  239. }
  240. bool drawTextLayout (const AttributedString& text, const Rectangle<float>& area)
  241. {
  242. renderingTarget->SetTransform (transformToMatrix (currentState->transform));
  243. const Direct2DFactories& factories = Direct2DFactories::getInstance();
  244. DirectWriteTypeLayout::drawToD2DContext (text, area, renderingTarget, factories.directWriteFactory,
  245. factories.d2dFactory, factories.systemFonts);
  246. renderingTarget->SetTransform (D2D1::IdentityMatrix());
  247. return true;
  248. }
  249. //==============================================================================
  250. class SavedState
  251. {
  252. public:
  253. SavedState (Direct2DLowLevelGraphicsContext& owner_)
  254. : owner (owner_), currentBrush (0),
  255. fontHeightToEmSizeFactor (1.0f), currentFontFace (0),
  256. clipsRect (false), shouldClipRect (false),
  257. clipsRectList (false), shouldClipRectList (false),
  258. clipsComplex (false), shouldClipComplex (false),
  259. clipsBitmap (false), shouldClipBitmap (false)
  260. {
  261. if (owner.currentState != nullptr)
  262. {
  263. // xxx seems like a very slow way to create one of these, and this is a performance
  264. // bottleneck.. Can the same internal objects be shared by multiple state objects, maybe using copy-on-write?
  265. setFill (owner.currentState->fillType);
  266. currentBrush = owner.currentState->currentBrush;
  267. clipRect = owner.currentState->clipRect;
  268. transform = owner.currentState->transform;
  269. font = owner.currentState->font;
  270. currentFontFace = owner.currentState->currentFontFace;
  271. }
  272. else
  273. {
  274. const D2D1_SIZE_U size (owner.renderingTarget->GetPixelSize());
  275. clipRect.setSize (size.width, size.height);
  276. setFill (FillType (Colours::black));
  277. }
  278. }
  279. ~SavedState()
  280. {
  281. clearClip();
  282. clearFont();
  283. clearFill();
  284. clearPathClip();
  285. clearImageClip();
  286. complexClipLayer = 0;
  287. bitmapMaskLayer = 0;
  288. }
  289. void clearClip()
  290. {
  291. popClips();
  292. shouldClipRect = false;
  293. }
  294. void clipToRectangle (const Rectangle<int>& r)
  295. {
  296. clearClip();
  297. clipRect = r.toFloat().transformed (transform).getSmallestIntegerContainer();
  298. shouldClipRect = true;
  299. pushClips();
  300. }
  301. void clearPathClip()
  302. {
  303. popClips();
  304. if (shouldClipComplex)
  305. {
  306. complexClipGeometry = 0;
  307. shouldClipComplex = false;
  308. }
  309. }
  310. void clipToPath (ID2D1Geometry* geometry)
  311. {
  312. clearPathClip();
  313. if (complexClipLayer == 0)
  314. owner.renderingTarget->CreateLayer (complexClipLayer.resetAndGetPointerAddress());
  315. complexClipGeometry = geometry;
  316. shouldClipComplex = true;
  317. pushClips();
  318. }
  319. void clearRectListClip()
  320. {
  321. popClips();
  322. if (shouldClipRectList)
  323. {
  324. rectListGeometry = 0;
  325. shouldClipRectList = false;
  326. }
  327. }
  328. void clipToRectList (ID2D1Geometry* geometry)
  329. {
  330. clearRectListClip();
  331. if (rectListLayer == 0)
  332. owner.renderingTarget->CreateLayer (rectListLayer.resetAndGetPointerAddress());
  333. rectListGeometry = geometry;
  334. shouldClipRectList = true;
  335. pushClips();
  336. }
  337. void clearImageClip()
  338. {
  339. popClips();
  340. if (shouldClipBitmap)
  341. {
  342. maskBitmap = 0;
  343. bitmapMaskBrush = 0;
  344. shouldClipBitmap = false;
  345. }
  346. }
  347. void clipToImage (const Image& image, const AffineTransform& transform)
  348. {
  349. clearImageClip();
  350. if (bitmapMaskLayer == 0)
  351. owner.renderingTarget->CreateLayer (bitmapMaskLayer.resetAndGetPointerAddress());
  352. D2D1_BRUSH_PROPERTIES brushProps;
  353. brushProps.opacity = 1;
  354. brushProps.transform = transformToMatrix (transform);
  355. D2D1_BITMAP_BRUSH_PROPERTIES bmProps = D2D1::BitmapBrushProperties (D2D1_EXTEND_MODE_WRAP, D2D1_EXTEND_MODE_WRAP);
  356. D2D1_SIZE_U size;
  357. size.width = image.getWidth();
  358. size.height = image.getHeight();
  359. D2D1_BITMAP_PROPERTIES bp = D2D1::BitmapProperties();
  360. maskImage = image.convertedToFormat (Image::ARGB);
  361. Image::BitmapData bd (this->image, Image::BitmapData::readOnly); // xxx should be maskImage?
  362. bp.pixelFormat = owner.renderingTarget->GetPixelFormat();
  363. bp.pixelFormat.alphaMode = D2D1_ALPHA_MODE_PREMULTIPLIED;
  364. HRESULT hr = owner.renderingTarget->CreateBitmap (size, bd.data, bd.lineStride, bp, maskBitmap.resetAndGetPointerAddress());
  365. hr = owner.renderingTarget->CreateBitmapBrush (maskBitmap, bmProps, brushProps, bitmapMaskBrush.resetAndGetPointerAddress());
  366. imageMaskLayerParams = D2D1::LayerParameters();
  367. imageMaskLayerParams.opacityBrush = bitmapMaskBrush;
  368. shouldClipBitmap = true;
  369. pushClips();
  370. }
  371. void popClips()
  372. {
  373. if (clipsBitmap)
  374. {
  375. owner.renderingTarget->PopLayer();
  376. clipsBitmap = false;
  377. }
  378. if (clipsComplex)
  379. {
  380. owner.renderingTarget->PopLayer();
  381. clipsComplex = false;
  382. }
  383. if (clipsRectList)
  384. {
  385. owner.renderingTarget->PopLayer();
  386. clipsRectList = false;
  387. }
  388. if (clipsRect)
  389. {
  390. owner.renderingTarget->PopAxisAlignedClip();
  391. clipsRect = false;
  392. }
  393. }
  394. void pushClips()
  395. {
  396. if (shouldClipRect && ! clipsRect)
  397. {
  398. owner.renderingTarget->PushAxisAlignedClip (rectangleToRectF (clipRect), D2D1_ANTIALIAS_MODE_PER_PRIMITIVE);
  399. clipsRect = true;
  400. }
  401. if (shouldClipRectList && ! clipsRectList)
  402. {
  403. D2D1_LAYER_PARAMETERS layerParams = D2D1::LayerParameters();
  404. rectListGeometry->GetBounds (D2D1::IdentityMatrix(), &layerParams.contentBounds);
  405. layerParams.geometricMask = rectListGeometry;
  406. owner.renderingTarget->PushLayer (layerParams, rectListLayer);
  407. clipsRectList = true;
  408. }
  409. if (shouldClipComplex && ! clipsComplex)
  410. {
  411. D2D1_LAYER_PARAMETERS layerParams = D2D1::LayerParameters();
  412. complexClipGeometry->GetBounds (D2D1::IdentityMatrix(), &layerParams.contentBounds);
  413. layerParams.geometricMask = complexClipGeometry;
  414. owner.renderingTarget->PushLayer (layerParams, complexClipLayer);
  415. clipsComplex = true;
  416. }
  417. if (shouldClipBitmap && ! clipsBitmap)
  418. {
  419. owner.renderingTarget->PushLayer (imageMaskLayerParams, bitmapMaskLayer);
  420. clipsBitmap = true;
  421. }
  422. }
  423. void setFill (const FillType& newFillType)
  424. {
  425. if (fillType != newFillType)
  426. {
  427. fillType = newFillType;
  428. clearFill();
  429. }
  430. }
  431. void clearFont()
  432. {
  433. currentFontFace = localFontFace = 0;
  434. }
  435. void setFont (const Font& newFont)
  436. {
  437. if (font != newFont)
  438. {
  439. font = newFont;
  440. clearFont();
  441. }
  442. }
  443. void createFont()
  444. {
  445. if (currentFontFace == nullptr)
  446. {
  447. WindowsDirectWriteTypeface* typeface = dynamic_cast<WindowsDirectWriteTypeface*> (font.getTypeface());
  448. currentFontFace = typeface->getIDWriteFontFace();
  449. fontHeightToEmSizeFactor = typeface->unitsToHeightScaleFactor();
  450. }
  451. }
  452. void setOpacity (float newOpacity)
  453. {
  454. fillType.setOpacity (newOpacity);
  455. if (currentBrush != nullptr)
  456. currentBrush->SetOpacity (newOpacity);
  457. }
  458. void clearFill()
  459. {
  460. gradientStops = 0;
  461. linearGradient = 0;
  462. radialGradient = 0;
  463. bitmap = 0;
  464. bitmapBrush = 0;
  465. currentBrush = 0;
  466. }
  467. void createBrush()
  468. {
  469. if (currentBrush == 0)
  470. {
  471. if (fillType.isColour())
  472. {
  473. D2D1_COLOR_F colour = colourToD2D (fillType.colour);
  474. owner.colourBrush->SetColor (colour);
  475. currentBrush = owner.colourBrush;
  476. }
  477. else if (fillType.isTiledImage())
  478. {
  479. D2D1_BRUSH_PROPERTIES brushProps;
  480. brushProps.opacity = fillType.getOpacity();
  481. brushProps.transform = transformToMatrix (fillType.transform);
  482. D2D1_BITMAP_BRUSH_PROPERTIES bmProps = D2D1::BitmapBrushProperties (D2D1_EXTEND_MODE_WRAP,D2D1_EXTEND_MODE_WRAP);
  483. image = fillType.image;
  484. D2D1_SIZE_U size;
  485. size.width = image.getWidth();
  486. size.height = image.getHeight();
  487. D2D1_BITMAP_PROPERTIES bp = D2D1::BitmapProperties();
  488. this->image = image.convertedToFormat (Image::ARGB);
  489. Image::BitmapData bd (this->image, Image::BitmapData::readOnly);
  490. bp.pixelFormat = owner.renderingTarget->GetPixelFormat();
  491. bp.pixelFormat.alphaMode = D2D1_ALPHA_MODE_PREMULTIPLIED;
  492. HRESULT hr = owner.renderingTarget->CreateBitmap (size, bd.data, bd.lineStride, bp, bitmap.resetAndGetPointerAddress());
  493. hr = owner.renderingTarget->CreateBitmapBrush (bitmap, bmProps, brushProps, bitmapBrush.resetAndGetPointerAddress());
  494. currentBrush = bitmapBrush;
  495. }
  496. else if (fillType.isGradient())
  497. {
  498. gradientStops = 0;
  499. D2D1_BRUSH_PROPERTIES brushProps;
  500. brushProps.opacity = fillType.getOpacity();
  501. brushProps.transform = transformToMatrix (fillType.transform.followedBy (transform));
  502. const int numColors = fillType.gradient->getNumColours();
  503. HeapBlock<D2D1_GRADIENT_STOP> stops (numColors);
  504. for (int i = fillType.gradient->getNumColours(); --i >= 0;)
  505. {
  506. stops[i].color = colourToD2D (fillType.gradient->getColour(i));
  507. stops[i].position = (FLOAT) fillType.gradient->getColourPosition(i);
  508. }
  509. owner.renderingTarget->CreateGradientStopCollection (stops.getData(), numColors, gradientStops.resetAndGetPointerAddress());
  510. if (fillType.gradient->isRadial)
  511. {
  512. radialGradient = 0;
  513. const Point<float> p1 = fillType.gradient->point1;
  514. const Point<float> p2 = fillType.gradient->point2;
  515. float r = p1.getDistanceFrom (p2);
  516. D2D1_RADIAL_GRADIENT_BRUSH_PROPERTIES props =
  517. D2D1::RadialGradientBrushProperties (D2D1::Point2F (p1.x, p1.y),
  518. D2D1::Point2F (0, 0),
  519. r, r);
  520. owner.renderingTarget->CreateRadialGradientBrush (props, brushProps, gradientStops, radialGradient.resetAndGetPointerAddress());
  521. currentBrush = radialGradient;
  522. }
  523. else
  524. {
  525. linearGradient = 0;
  526. const Point<float> p1 = fillType.gradient->point1;
  527. const Point<float> p2 = fillType.gradient->point2;
  528. D2D1_LINEAR_GRADIENT_BRUSH_PROPERTIES props =
  529. D2D1::LinearGradientBrushProperties (D2D1::Point2F (p1.x, p1.y),
  530. D2D1::Point2F (p2.x, p2.y));
  531. owner.renderingTarget->CreateLinearGradientBrush (props, brushProps, gradientStops, linearGradient.resetAndGetPointerAddress());
  532. currentBrush = linearGradient;
  533. }
  534. }
  535. }
  536. }
  537. //==============================================================================
  538. //xxx most of these members should probably be private...
  539. Direct2DLowLevelGraphicsContext& owner;
  540. AffineTransform transform;
  541. Font font;
  542. float fontHeightToEmSizeFactor;
  543. IDWriteFontFace* currentFontFace;
  544. ComSmartPtr <IDWriteFontFace> localFontFace;
  545. FillType fillType;
  546. Image image;
  547. ComSmartPtr <ID2D1Bitmap> bitmap; // xxx needs a better name - what is this for??
  548. Rectangle<int> clipRect;
  549. bool clipsRect, shouldClipRect;
  550. ComSmartPtr <ID2D1Geometry> complexClipGeometry;
  551. D2D1_LAYER_PARAMETERS complexClipLayerParams;
  552. ComSmartPtr <ID2D1Layer> complexClipLayer;
  553. bool clipsComplex, shouldClipComplex;
  554. ComSmartPtr <ID2D1Geometry> rectListGeometry;
  555. D2D1_LAYER_PARAMETERS rectListLayerParams;
  556. ComSmartPtr <ID2D1Layer> rectListLayer;
  557. bool clipsRectList, shouldClipRectList;
  558. Image maskImage;
  559. D2D1_LAYER_PARAMETERS imageMaskLayerParams;
  560. ComSmartPtr <ID2D1Layer> bitmapMaskLayer;
  561. ComSmartPtr <ID2D1Bitmap> maskBitmap;
  562. ComSmartPtr <ID2D1BitmapBrush> bitmapMaskBrush;
  563. bool clipsBitmap, shouldClipBitmap;
  564. ID2D1Brush* currentBrush;
  565. ComSmartPtr <ID2D1BitmapBrush> bitmapBrush;
  566. ComSmartPtr <ID2D1LinearGradientBrush> linearGradient;
  567. ComSmartPtr <ID2D1RadialGradientBrush> radialGradient;
  568. ComSmartPtr <ID2D1GradientStopCollection> gradientStops;
  569. private:
  570. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (SavedState)
  571. };
  572. //==============================================================================
  573. private:
  574. HWND hwnd;
  575. ComSmartPtr <ID2D1HwndRenderTarget> renderingTarget;
  576. ComSmartPtr <ID2D1SolidColorBrush> colourBrush;
  577. Rectangle<int> bounds;
  578. SavedState* currentState;
  579. OwnedArray<SavedState> states;
  580. //==============================================================================
  581. static D2D1_RECT_F rectangleToRectF (const Rectangle<int>& r)
  582. {
  583. return D2D1::RectF ((float) r.getX(), (float) r.getY(), (float) r.getRight(), (float) r.getBottom());
  584. }
  585. static D2D1_COLOR_F colourToD2D (Colour c)
  586. {
  587. return D2D1::ColorF::ColorF (c.getFloatRed(), c.getFloatGreen(), c.getFloatBlue(), c.getFloatAlpha());
  588. }
  589. static D2D1_POINT_2F pointTransformed (int x, int y, const AffineTransform& transform)
  590. {
  591. transform.transformPoint (x, y);
  592. return D2D1::Point2F ((FLOAT) x, (FLOAT) y);
  593. }
  594. static void rectToGeometrySink (const Rectangle<int>& rect, ID2D1GeometrySink* sink)
  595. {
  596. sink->BeginFigure (pointTransformed (rect.getX(), rect.getY()), D2D1_FIGURE_BEGIN_FILLED);
  597. sink->AddLine (pointTransformed (rect.getRight(), rect.getY()));
  598. sink->AddLine (pointTransformed (rect.getRight(), rect.getBottom()));
  599. sink->AddLine (pointTransformed (rect.getX(), rect.getBottom()));
  600. sink->EndFigure (D2D1_FIGURE_END_CLOSED);
  601. }
  602. static ID2D1PathGeometry* rectListToPathGeometry (const RectangleList<int>& clipRegion)
  603. {
  604. ID2D1PathGeometry* p = nullptr;
  605. Direct2DFactories::getInstance().d2dFactory->CreatePathGeometry (&p);
  606. ComSmartPtr <ID2D1GeometrySink> sink;
  607. HRESULT hr = p->Open (sink.resetAndGetPointerAddress()); // xxx handle error
  608. sink->SetFillMode (D2D1_FILL_MODE_WINDING);
  609. for (int i = clipRegion.getNumRectangles(); --i >= 0;)
  610. rectToGeometrySink (clipRegion.getRectangle(i), sink);
  611. hr = sink->Close();
  612. return p;
  613. }
  614. static void pathToGeometrySink (const Path& path, ID2D1GeometrySink* sink, const AffineTransform& transform)
  615. {
  616. Path::Iterator it (path);
  617. while (it.next())
  618. {
  619. switch (it.elementType)
  620. {
  621. case Path::Iterator::cubicTo:
  622. {
  623. D2D1_BEZIER_SEGMENT seg;
  624. transform.transformPoint (it.x1, it.y1);
  625. seg.point1 = D2D1::Point2F (it.x1, it.y1);
  626. transform.transformPoint (it.x2, it.y2);
  627. seg.point2 = D2D1::Point2F (it.x2, it.y2);
  628. transform.transformPoint(it.x3, it.y3);
  629. seg.point3 = D2D1::Point2F (it.x3, it.y3);
  630. sink->AddBezier (seg);
  631. break;
  632. }
  633. case Path::Iterator::lineTo:
  634. {
  635. transform.transformPoint (it.x1, it.y1);
  636. sink->AddLine (D2D1::Point2F (it.x1, it.y1));
  637. break;
  638. }
  639. case Path::Iterator::quadraticTo:
  640. {
  641. D2D1_QUADRATIC_BEZIER_SEGMENT seg;
  642. transform.transformPoint (it.x1, it.y1);
  643. seg.point1 = D2D1::Point2F (it.x1, it.y1);
  644. transform.transformPoint (it.x2, it.y2);
  645. seg.point2 = D2D1::Point2F (it.x2, it.y2);
  646. sink->AddQuadraticBezier (seg);
  647. break;
  648. }
  649. case Path::Iterator::closePath:
  650. {
  651. sink->EndFigure (D2D1_FIGURE_END_CLOSED);
  652. break;
  653. }
  654. case Path::Iterator::startNewSubPath:
  655. {
  656. transform.transformPoint (it.x1, it.y1);
  657. sink->BeginFigure (D2D1::Point2F (it.x1, it.y1), D2D1_FIGURE_BEGIN_FILLED);
  658. break;
  659. }
  660. }
  661. }
  662. }
  663. static ID2D1PathGeometry* pathToPathGeometry (const Path& path, const AffineTransform& transform)
  664. {
  665. ID2D1PathGeometry* p = nullptr;
  666. Direct2DFactories::getInstance().d2dFactory->CreatePathGeometry (&p);
  667. ComSmartPtr <ID2D1GeometrySink> sink;
  668. HRESULT hr = p->Open (sink.resetAndGetPointerAddress());
  669. sink->SetFillMode (D2D1_FILL_MODE_WINDING); // xxx need to check Path::isUsingNonZeroWinding()
  670. pathToGeometrySink (path, sink, transform);
  671. hr = sink->Close();
  672. return p;
  673. }
  674. static D2D1::Matrix3x2F transformToMatrix (const AffineTransform& transform)
  675. {
  676. D2D1::Matrix3x2F matrix;
  677. matrix._11 = transform.mat00;
  678. matrix._12 = transform.mat10;
  679. matrix._21 = transform.mat01;
  680. matrix._22 = transform.mat11;
  681. matrix._31 = transform.mat02;
  682. matrix._32 = transform.mat12;
  683. return matrix;
  684. }
  685. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (Direct2DLowLevelGraphicsContext)
  686. };