Skip to content

Commit

Permalink
Adding support for ClearRect with clipping geometry (microsoft#2053)
Browse files Browse the repository at this point in the history
* Adding support for ClearRect with clipping geometry
  • Loading branch information
msft-Jeyaram authored Feb 24, 2017
1 parent c4a2e35 commit e7edfdf
Show file tree
Hide file tree
Showing 22 changed files with 301 additions and 13 deletions.
59 changes: 46 additions & 13 deletions Frameworks/CoreGraphics/CGContext.mm
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,8 @@ inline HRESULT GetTarget(ID2D1Image** pTarget) {
woc::unique_cf<CGColorSpaceRef> _fillColorSpace;
woc::unique_cf<CGColorSpaceRef> _strokeColorSpace;

ComPtr<ID2D1SolidColorBrush> transparentBrush{ nullptr };

// Keeps track of the depth of a 'stack' of PushBeginDraw/PopEndDraw calls
// Since nothing needs to actually be put on a stack, just increment a counter insteads
std::atomic_uint32_t _beginEndDrawDepth = { 0 };
Expand Down Expand Up @@ -491,6 +493,7 @@ inline HRESULT PopEndDraw() {

HRESULT PushLayer(CGRect* rect = nullptr);
HRESULT PopLayer();
HRESULT ClearRect(CGRect rect);

template <typename Lambda> // Lambda takes the form HRESULT (*)(CGContextRef, ID2D1DeviceContext*)
HRESULT Draw(_CGCoordinateMode coordinateMode, CGAffineTransform* additionalTransform, Lambda&& drawLambda);
Expand Down Expand Up @@ -2297,22 +2300,46 @@ void CGContextShowGlyphsWithAdvances(CGContextRef context, const CGGlyph* glyphs
#pragma endregion

#pragma region Drawing Operations - Basic Shapes

HRESULT __CGContext::ClearRect(CGRect rect) {
PushBeginDraw();
auto endDraw = wil::ScopeExit([this]() { PopEndDraw(); });

ComPtr<ID2D1Factory> factory = Factory();
ComPtr<ID2D1RectangleGeometry> rectangle;
RETURN_IF_FAILED(factory->CreateRectangleGeometry(__CGRectToD2D_F(rect), &rectangle));

// Transformed geometry
ComPtr<ID2D1TransformedGeometry> transformedRectangle;
RETURN_IF_FAILED(factory->CreateTransformedGeometry(rectangle.Get(),
__CGAffineTransformToD2D_F(CGContextGetUserSpaceToDeviceSpaceTransform(this)),
&transformedRectangle));

RETURN_IF_FAILED(PushGState());

if (CurrentGState().shouldAntialias != _kCGTrinaryDefault || !allowsAntialiasing) {
deviceContext->SetAntialiasMode(GetAntialiasMode());
}

RETURN_IF_FAILED(CurrentGState().IntersectClippingGeometry(transformedRectangle.Get(), kCGPathEOFill));

if (!transparentBrush) {
RETURN_IF_FAILED(deviceContext->CreateSolidColorBrush(D2D1::ColorF(D2D1::ColorF::Black, 0.0f), &transparentBrush));
}

deviceContext->SetPrimitiveBlend(D2D1_PRIMITIVE_BLEND_COPY);
deviceContext->FillGeometry(CurrentGState().clippingGeometry.Get(), transparentBrush.Get());
deviceContext->SetPrimitiveBlend(D2D1_PRIMITIVE_BLEND_SOURCE_OVER);
RETURN_IF_FAILED(PopGState());
return S_OK;
}

/**
@Status Caveat
@Notes only supports the scenario where clipping is not set.
Also only works on untransformed coordinates.
The cleared region will be cleared in device space.
@Status Interoperable
*/
void CGContextClearRect(CGContextRef context, CGRect rect) {
NOISY_RETURN_IF_NULL(context);
ComPtr<ID2D1DeviceContext> deviceContext = context->DeviceContext();
if (!context->CurrentGState().clippingGeometry) {
_CGContextPushBeginDraw(context);
deviceContext->PushAxisAlignedClip(__CGRectToD2D_F(rect), context->GetAntialiasMode());
deviceContext->Clear(nullptr); // transparent black clear
deviceContext->PopAxisAlignedClip();
_CGContextPopEndDraw(context);
}
FAIL_FAST_IF_FAILED(context->ClearRect(rect));
}

HRESULT __CGContext::_CreateShadowEffect(ID2D1Image* inputImage, ID2D1Effect** outShadowEffect) {
Expand Down Expand Up @@ -3031,7 +3058,13 @@ CGContextRef CGBitmapContextCreateWithData(void* data,
size_t estimatedBytesPerRow = (imputedBitsPerPixel >> 3) * width;

if (data && estimatedBytesPerRow > bytesPerRow) {
TraceError(TAG, L"Invalid data stride: a %ux%u %ubpp context requires at least a %u-byte stride (requested: %u bytes/row).", width, height, imputedBitsPerPixel, estimatedBytesPerRow, bytesPerRow);
TraceError(TAG,
L"Invalid data stride: a %ux%u %ubpp context requires at least a %u-byte stride (requested: %u bytes/row).",
width,
height,
imputedBitsPerPixel,
estimatedBytesPerRow,
bytesPerRow);
return nullptr;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,7 @@
<ClangCompile Include="$(StarboardBasePath)\tests\unittests\CoreGraphics.Drawing\CGContextDrawing_PatternTests.cpp" />
<ClangCompile Include="$(StarboardBasePath)\tests\unittests\CoreGraphics.drawing\CGContextDrawing_FillModeTests.cpp" />
<ClangCompile Include="$(StarboardBasePath)\tests\unittests\CoreGraphics.drawing\CGContextDrawing_AntiAliasTests.cpp" />
<ClangCompile Include="$(StarboardBasePath)\tests\unittests\CoreGraphics.Drawing\CGContextDrawing_ClearRectTests.cpp" />
<ClangCompile Include="$(StarboardBasePath)\tests\unittests\CoreGraphics.Drawing\CGPathDrawingTests.cpp" />
<ClangCompile Include="$(StarboardBasePath)\tests\unittests\CoreGraphics.Drawing\DrawingTest.cpp" />
<ClangCompile Include="$(StarboardBasePath)\tests\unittests\CoreGraphics.drawing\ImageComparison.cpp" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
//******************************************************************************
//
// Copyright (c) Microsoft. All rights reserved.
//
// This code is licensed under the MIT License (MIT).
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
//******************************************************************************

#include "DrawingTest.h"

#pragma region ClearRect

class CGClearRect : public WhiteBackgroundTest<>, public ::testing::WithParamInterface<::testing::tuple<CGRect, CGAffineTransform>> {
CFStringRef CreateOutputFilename() {
CGRect rect = ::testing::get<0>(GetParam());
CGAffineTransform transformation = ::testing::get<1>(GetParam());

return CFStringCreateWithFormat(nullptr,
nullptr,
CFSTR("TestImage.CGContext.ClearRect.(%0.0f.%0.0f)%0.0fx%0.0f.[%0.1f,%0."
"1f,%0.1f,%0.1f,%0.1f,%0.1f].png"),
rect.origin.x,
rect.origin.y,
rect.size.width,
rect.size.height,
transformation.a,
transformation.b,
transformation.c,
transformation.d,
transformation.tx,
transformation.ty);
}
};

DRAW_TEST_P(CGClearRect, Transformed) {
CGRect rect = ::testing::get<0>(GetParam());
CGAffineTransform transformation = ::testing::get<1>(GetParam());

CGContextRef context = GetDrawingContext();

CGContextAddRect(context, CGRectMake(100, 50, 50, 50));
CGContextConcatCTM(context, transformation);
CGContextClip(context);

CGContextSetRGBFillColor(context, 0.48, 0.73, 0, 1);
CGContextClearRect(context, rect);
}

DRAW_TEST_P(CGClearRect, Transformed2) {
CGRect rect = ::testing::get<0>(GetParam());
CGAffineTransform transformation = ::testing::get<1>(GetParam());

CGContextRef context = GetDrawingContext();
CGRect bounds = GetDrawingBounds();

CGContextAddRect(context, CGRectMake(0, 0, 250, 250));
CGContextConcatCTM(context, transformation);
CGContextClip(context);

CGContextSetRGBFillColor(context, 0.48, 0.73, 0, 1);
CGContextClearRect(context, rect);
}

static CGRect rects[] = { CGRectMake(0, 0, 100, 100), CGRectMake(0, 0, 50, 250), CGRectMake(100, 100, 125, 250) };
static CGAffineTransform transformation[] = { CGAffineTransformMakeRotation(0.4),
CGAffineTransformMakeTranslation(3, 6),
CGAffineTransformMakeScale(0.25, 0.75),
CGAffineTransformMake(1.f, 0.f, 0.3f, 1.f, 0.f, 0.f),
CGAffineTransformIdentity };

INSTANTIATE_TEST_CASE_P(CGContextTests, CGClearRect, ::testing::Combine(::testing::ValuesIn(rects), ::testing::ValuesIn(transformation)));

DRAW_TEST_F(CGContext, ClearRect, WhiteBackgroundTest<>) {
CGContextRef context = GetDrawingContext();
CGRect bounds = GetDrawingBounds();

CGContextSetRGBFillColor(context, 1.0, 0.0, 0.0, 1);
CGContextFillRect(context, bounds);

CGRect borderRect = CGRectInset(bounds, 30, 50);
CGContextClearRect(context, borderRect);
}

// Failure to fill in an Arc geometry - #2059
// Note add a test for CGContextFill of an Arc that spans from 0-2*PI
DISABLED_DRAW_TEST_F(CGContext, ClearRectArc, WhiteBackgroundTest<>) {
CGContextRef context = GetDrawingContext();
CGRect bounds = GetDrawingBounds();

CGRect cirleRect = CGRectMake(0, 0, 100, 100);
CGContextAddArc(context, 50, 50, 50, 0.0, 2 * M_PI, 0);
CGContextClip(context);
CGContextClearRect(context, cirleRect);
}

DRAW_TEST_F(CGContext, ClearRectTransparencyLayer, WhiteBackgroundTest<>) {
CGContextRef context = GetDrawingContext();
CGRect bounds = GetDrawingBounds();

CGContextSetRGBFillColor(context, 1, 0, 0, 1);
CGContextFillRect(context, bounds);

CGContextBeginTransparencyLayer(context, nullptr);
CGContextSetRGBFillColor(context, 0, 0, 1, 1);
CGContextFillRect(context, bounds);

CGContextClearRect(context, CGRectMake(0, 0, 100, 100));

CGContextEndTransparencyLayer(context);
}

class CGClearRectArc : public WhiteBackgroundTest<>, public ::testing::WithParamInterface<::testing::tuple<CGRect, CGPoint>> {
CFStringRef CreateOutputFilename() {
CGRect rect = ::testing::get<0>(GetParam());
CGPoint sweep = ::testing::get<1>(GetParam());

return CFStringCreateWithFormat(nullptr,
nullptr,
CFSTR("TestImage.CGContext.CGClearRectArc.(%0.0f.%0.0f)%0.0fx%0.0f.sweep(%0.0f--%0.0f).png"),
rect.origin.x,
rect.origin.y,
rect.size.width,
rect.size.height,
sweep.x,
sweep.y);
}
};

DRAW_TEST_P(CGClearRectArc, ClearArc) {
CGContextRef context = GetDrawingContext();
CGRect rect = ::testing::get<0>(GetParam());
CGPoint sweep = ::testing::get<1>(GetParam());

CGContextAddArc(context, 50, 50, 50, sweep.x, sweep.y, 0);
CGContextClip(context);
CGContextClearRect(context, rect);
}

static CGPoint sweep[] = { CGPointMake(0, M_PI), CGPointMake(M_PI, 0), CGPointMake(0.3 * M_PI, 0.6 * M_PI), CGPointMake(0, 1.9 * M_PI) };
// TODO: enable when #2062 is fixed.
INSTANTIATE_TEST_CASE_P(DISABLED_CGContextTests,
CGClearRectArc,
::testing::Combine(::testing::ValuesIn(rects), ::testing::ValuesIn(sweep)));

DRAW_TEST_F(CGContext, CustomPathClearRect, WhiteBackgroundTest<>) {
CGContextRef context = GetDrawingContext();
CGRect bounds = GetDrawingBounds();

CGMutablePathRef thepath = CGPathCreateMutable();
CGPathMoveToPoint(thepath, NULL, 30, 100);
CGPathAddCurveToPoint(thepath, NULL, 47.0f, 67.0f, 50.0f, 55.0f, 45.0f, 50.0f);
CGPathAddCurveToPoint(thepath, NULL, 42.0f, 47.0f, 37.0f, 46.0f, 30.0f, 55.0f);

CGPathAddCurveToPoint(thepath, NULL, 23.0f, 46.0f, 18.0f, 47.0f, 15.0f, 50.0f);
CGPathAddCurveToPoint(thepath, NULL, 10.0f, 55.0f, 13.0f, 67.0f, 30.0f, 100.0f);

CGPathCloseSubpath(thepath);
CGContextAddPath(context, thepath);

CGContextClip(context);
CGContextClearRect(context, CGRectMake(0, 0, 50, 70));
CGPathRelease(thepath);
}
#pragma endregion ClearRect
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.
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.
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.
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.
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.
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.
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.
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.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -114,3 +114,34 @@ DISABLED_DRAW_TEST_F(CGContextFill, ConcentricRectsEvenOdd, WhiteBackgroundTest<

CGPathRelease(path);
}

class CGContextArcFill : public WhiteBackgroundTest<>, public ::testing::WithParamInterface<::testing::tuple<CGPoint, int>> {
CFStringRef CreateOutputFilename() {
CGPoint sweep = ::testing::get<0>(GetParam());
int direction = ::testing::get<1>(GetParam());
return CFStringCreateWithFormat(nullptr,
nullptr,
CFSTR("TestImage.CGContextFill.Arc.sweep(%0.0f--%0.0f).direction.%d.png"),
sweep.x,
sweep.y,
direction);
}
};

DRAW_TEST_P(CGContextArcFill, FillArc) {
CGContextRef context = GetDrawingContext();
CGPoint sweep = ::testing::get<0>(GetParam());
int direction = ::testing::get<1>(GetParam());
CGContextAddArc(context, 50, 50, 50, sweep.x, sweep.y, direction);
CGContextFillPath(context);
}

static CGPoint sweep[] = { CGPointMake(0, M_PI),
CGPointMake(2 * M_PI, 0),
CGPointMake(0.3 * M_PI, 0.6 * M_PI),
CGPointMake(0, 1.9 * M_PI) };
static int directions[] = { 0, 1 };
// TODO: enable when #2062 is fixed.
INSTANTIATE_TEST_CASE_P(DISABLED_CGContextFill,
CGContextArcFill,
::testing::Combine(::testing::ValuesIn(sweep), ::testing::ValuesIn(directions)));

0 comments on commit e7edfdf

Please sign in to comment.