Skip to content

Commit

Permalink
Review fillRectWithBitmap draw context method
Browse files Browse the repository at this point in the history
Remove Quartz implementation as it does not work and add implementation for Direct2D
  • Loading branch information
scheffle committed Aug 6, 2023
1 parent 91b0e9b commit 8847ccf
Show file tree
Hide file tree
Showing 8 changed files with 112 additions and 4 deletions.
5 changes: 5 additions & 0 deletions vstgui/lib/cdrawcontext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,11 @@ void CDrawContext::fillRectWithBitmap (CBitmap* bitmap, const CRect& srcRect, co
{
if (srcRect.isEmpty () || dstRect.isEmpty ())
return;
if (srcRect.getWidth () == dstRect.getWidth () && srcRect.getHeight () == dstRect.getHeight ())
{
drawBitmap (bitmap, dstRect, srcRect.getTopLeft (), alpha);
return;
}

if (impl->device)
{
Expand Down
4 changes: 3 additions & 1 deletion vstgui/lib/platform/mac/coregraphicsdevicecontext.mm
Original file line number Diff line number Diff line change
Expand Up @@ -849,7 +849,7 @@ void drawCGImageRef (CGContextRef context, CGImageRef image, CGLayerRef layer,
//------------------------------------------------------------------------
const IPlatformGraphicsDeviceContextBitmapExt* CoreGraphicsDeviceContext::asBitmapExt () const
{
return this;
return nullptr;
}

//------------------------------------------------------------------------
Expand All @@ -867,6 +867,7 @@ void drawCGImageRef (CGContextRef context, CGImageRef image, CGLayerRef layer,
CRect dstRect, double alpha,
BitmapInterpolationQuality quality) const
{
#if 0
auto cgBitmap = dynamic_cast<CGBitmap*> (&bitmap);
if (!cgBitmap)
return false;
Expand All @@ -889,6 +890,7 @@ void drawCGImageRef (CGContextRef context, CGImageRef image, CGLayerRef layer,

CGContextDrawTiledImage (context, r, cgImage);
});
#endif
return false;
}

Expand Down
79 changes: 78 additions & 1 deletion vstgui/lib/platform/win32/direct2d/d2dgraphicscontext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -843,7 +843,84 @@ void D2DGraphicsDeviceContext::setTransformMatrix (const TransformMatrix& tm) co
//------------------------------------------------------------------------
const IPlatformGraphicsDeviceContextBitmapExt* D2DGraphicsDeviceContext::asBitmapExt () const
{
return nullptr;
return this;
}

//------------------------------------------------------------------------
bool D2DGraphicsDeviceContext::drawBitmapNinePartTiled (IPlatformBitmap& bitmap, CRect dest,
const CNinePartTiledDescription& desc,
double alpha,
BitmapInterpolationQuality quality) const
{
return false;
}

//------------------------------------------------------------------------
bool D2DGraphicsDeviceContext::fillRectWithBitmap (IPlatformBitmap& bitmap, CRect srcRect,
CRect dstRect, double alpha,
BitmapInterpolationQuality quality) const
{
D2DBitmap* d2dBitmap = dynamic_cast<D2DBitmap*> (&bitmap);
if (!d2dBitmap || !d2dBitmap->getSource ())
return false;
auto d2d1Bitmap =
D2DBitmapCache::getBitmap (d2dBitmap, impl->deviceContext.get (), impl->device.get ());
if (!d2d1Bitmap)
return false;

auto originalClip = impl->state.clip;
auto cr = dstRect;
impl->state.tm.transform (cr);
cr.bound (originalClip);

double bitmapScaleFactor = d2dBitmap->getScaleFactor ();
CGraphicsTransform bitmapTransform;
bitmapTransform.scale (1. / bitmapScaleFactor, 1. / bitmapScaleFactor);
auto invBitmapTransform = bitmapTransform.inverse ();
invBitmapTransform.transform (dstRect);
invBitmapTransform.transform (srcRect);

D2D1_IMAGE_BRUSH_PROPERTIES imageBrushProp = {};
imageBrushProp.sourceRectangle = convert (srcRect);
imageBrushProp.extendModeX = imageBrushProp.extendModeY = D2D1_EXTEND_MODE_WRAP;
switch (quality)
{
case BitmapInterpolationQuality::kLow:
imageBrushProp.interpolationMode = D2D1_INTERPOLATION_MODE_NEAREST_NEIGHBOR;
break;

case BitmapInterpolationQuality::kMedium:
case BitmapInterpolationQuality::kHigh:
default:
imageBrushProp.interpolationMode = D2D1_INTERPOLATION_MODE_LINEAR;
break;
}
CGraphicsTransform brushTransform;
brushTransform.translate (dstRect.getTopLeft ());

D2D1_BRUSH_PROPERTIES brushProp = {};
brushProp.opacity = 1.f;
brushProp.transform = convert (brushTransform);

COM::Ptr<ID2D1ImageBrush> brush;
auto hr = impl->deviceContext->CreateImageBrush (d2d1Bitmap, imageBrushProp, brushProp,
brush.adoptPtr ());
if (FAILED (hr))
return false;

impl->state.clip = cr;

auto originalTransformMatrix = impl->state.tm;
TransformMatrix tm = originalTransformMatrix * bitmapTransform;
setTransformMatrix (tm);

impl->doInContext ([&] (auto deviceContext) {
deviceContext->FillRectangle (convert (dstRect), brush.get ());
});

setTransformMatrix (originalTransformMatrix);
impl->state.clip = originalClip;
return true;
}

//------------------------------------------------------------------------
Expand Down
9 changes: 8 additions & 1 deletion vstgui/lib/platform/win32/direct2d/d2dgraphicscontext.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ namespace VSTGUI {
class D2DGraphicsDevice;

//------------------------------------------------------------------------
class D2DGraphicsDeviceContext : public IPlatformGraphicsDeviceContext
class D2DGraphicsDeviceContext : public IPlatformGraphicsDeviceContext,
public IPlatformGraphicsDeviceContextBitmapExt
{
public:
D2DGraphicsDeviceContext (const D2DGraphicsDevice& device, ID2D1DeviceContext* deviceContext,
Expand Down Expand Up @@ -65,6 +66,12 @@ class D2DGraphicsDeviceContext : public IPlatformGraphicsDeviceContext
// extension
const IPlatformGraphicsDeviceContextBitmapExt* asBitmapExt () const override;

bool drawBitmapNinePartTiled (IPlatformBitmap& bitmap, CRect dest,
const CNinePartTiledDescription& desc, double alpha,
BitmapInterpolationQuality quality) const override;
bool fillRectWithBitmap (IPlatformBitmap& bitmap, CRect srcRect, CRect dstRect, double alpha,
BitmapInterpolationQuality quality) const override;

// private
void drawTextLayout (IDWriteTextLayout* textLayout, CPoint pos, CColor color, bool antialias);

Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
16 changes: 16 additions & 0 deletions vstgui/standalone/examples/standalone/resource/resources.uidesc

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion vstgui/standalone/examples/standalone/resource/test.uidesc
Original file line number Diff line number Diff line change
Expand Up @@ -710,6 +710,7 @@
"autosize": "left right top bottom ",
"back-color": "~ WhiteCColor",
"background-offset": "0, 0",
"bitmap": "box",
"class": "CMultiLineTextLabel",
"default-value": "0.5",
"font": "EffectsEighty",
Expand All @@ -734,7 +735,7 @@
"style-round-rect": "false",
"style-shadow-text": "false",
"text-alignment": "left",
"text-inset": "3, 3",
"text-inset": "13, 13",
"text-rotation": "0",
"text-shadow-offset": "1, 1",
"title": "This is a multine line text label! That wraps the lines if they are too long.",
Expand Down

0 comments on commit 8847ccf

Please sign in to comment.