Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implementation of ClearRect with masking support #2133

Merged
merged 4 commits into from
Mar 2, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 39 additions & 12 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(); });

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

prefer return PopEndDraw() at the end; otherwise, we lose the HRESULT from EndDraw.


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
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,169 @@
//******************************************************************************
//
// 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<PixelByPixelImageComparator<PixelComparisonModeMask<>>>,
public ::testing::WithParamInterface<::testing::tuple<CGRect, CGAffineTransform, CGRect>> {
CFStringRef CreateOutputFilename() {
CGRect rect = ::testing::get<0>(GetParam());
CGAffineTransform transformation = ::testing::get<1>(GetParam());
CGRect addRect = ::testing::get<2>(GetParam());

return CFStringCreateWithFormat(
nullptr,
nullptr,
CFSTR("TestImage.CGContext.ClearRect.(%0.0f.%0.0f)%0.0fx%0.0f.clear.(%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,
addRect.origin.x,
addRect.origin.y,
addRect.size.width,
addRect.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());
CGRect addRect = ::testing::get<2>(GetParam());

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

CGContextAddRect(context, addRect);
CGContextConcatCTM(context, transformation);
CGContextClip(context);

CGContextClearRect(context, rect);
}

static CGRect addRects[] = { CGRectMake(10, 20, 300, 400), CGRectMake(0, 0, 250, 250) };
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), ::testing::ValuesIn(addRects)));

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);

CGContextConcatCTM(context, CGAffineTransformMakeRotation(0.2));
CGContextClip(context);
CGContextClearRect(context, CGRectMake(0, 0, 50, 98));
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.
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