-
Notifications
You must be signed in to change notification settings - Fork 808
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
Add support for colored and non-colored Patterns #1952
Changes from 7 commits
2a06c45
5cbe655
adcef78
58dbe68
95821be
0d94658
1d2120b
20dad65
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1769,10 +1769,11 @@ static CGImageRef __CGContextCreateRenderableImage(CGImageRef image) { | |
|
||
#pragma region Drawing Parameters - Stroke / Fill Patterns | ||
|
||
template <typename ContextStageLambda> // Takes the form HRESULT(*)(CGContextRef) | ||
static HRESULT _CreatePatternBrush( | ||
CGContextRef context, CGPatternRef pattern, const CGFloat* components, ID2D1BitmapBrush1** brush, ContextStageLambda&& contextStage) { | ||
// TODO #1592: change to support grayscale (masks) after dustins change. | ||
template <typename ContextStageLambda> // Takes the form HRESULT(*)(CGContextRef,bool) | ||
static HRESULT _CreatePatternBrush(CGContextRef context, | ||
CGPatternRef pattern, | ||
ID2D1BitmapBrush1** brush, | ||
ContextStageLambda&& contextStage) { | ||
woc::unique_cf<CGColorSpaceRef> colorspace{ CGColorSpaceCreateDeviceRGB() }; | ||
|
||
// We need to generate the pattern as an image (then tile it) | ||
|
@@ -1781,20 +1782,23 @@ static HRESULT _CreatePatternBrush( | |
|
||
size_t bitsPerComponent = 8; | ||
size_t bytesPerRow = 4 * tileSize.size.width; | ||
CGBitmapInfo bitmapInfo = kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrder32Big; | ||
CGBitmapInfo bitmapInfo = kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrderDefault; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How does this work? We should not be using alphafirst formats since that is not natively supported by d2d. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @rajsesh-msft alpha first + default = There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. However, @msft-Jeyaram: If you want to avoid unnecessary bitmap copies, please use AlphaLast|Big: That is There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry: premultiplied alpha last | big There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @DHowett-MSFT updating 😄 |
||
|
||
woc::unique_cf<CGContextRef> patternContext{ CGBitmapContextCreate( | ||
nullptr, tileSize.size.width, tileSize.size.height, bitsPerComponent, bytesPerRow, colorspace.get(), bitmapInfo) }; | ||
RETURN_HR_IF_NULL(E_UNEXPECTED, patternContext); | ||
|
||
// Determine if this pattern is a colored pattern (the coloring is specified in the pattern callback) or if it is stencil pattern (the | ||
// color is set outside and not within the pattern callback) | ||
bool isColored = _CGPatternIsColored(pattern); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: can you just do this in the lambda? will simplify the code. #ByDesign There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it'll be duplicated in both stoke and fill. In reply to: 100935286 [](ancestors = 100935286) |
||
|
||
// Stage the drawing context | ||
RETURN_IF_FAILED(std::forward<ContextStageLambda>(contextStage)(patternContext.get())); | ||
RETURN_IF_FAILED(std::forward<ContextStageLambda>(contextStage)(patternContext.get(), isColored)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. On more thought, the lambda is really only saving us from having to check if it's stroke vs fill. For this case I think it'd be better to pass in a flag (enum or just bool since this'll be the only difference) and inline the lambda stuff here. #ByDesign There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes but this helps make this more general. In reply to: 101356532 [](ancestors = 101356532) |
||
|
||
// Now we ask the user to draw | ||
_CGPatternIssueCallBack(patternContext.get(), pattern); | ||
|
||
// Get the image out of it | ||
|
||
woc::unique_cf<CGImageRef> bitmapTiledimage{ CGBitmapContextCreateImage(patternContext.get()) }; | ||
woc::unique_cf<CGImageRef> tileImage{ __CGContextCreateRenderableImage(bitmapTiledimage.get()) }; | ||
RETURN_HR_IF_NULL(E_UNEXPECTED, tileImage); | ||
|
@@ -1848,9 +1852,18 @@ void CGContextSetFillPattern(CGContextRef context, CGPatternRef pattern, const C | |
} | ||
|
||
ComPtr<ID2D1BitmapBrush1> bitmapBrush; | ||
FAIL_FAST_IF_FAILED(_CreatePatternBrush(context, pattern, components, &bitmapBrush, [&](CGContextRef drawingContext) { | ||
FAIL_FAST_IF_FAILED(_CreatePatternBrush(context, pattern, &bitmapBrush, [&](CGContextRef drawingContext, bool isColored) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is it possible that we'll have other pattern variables to address in the future? Even if not, I think it would make more sense for the function to take (CGContextRef, CGPatternRef) rather than a bool flag, especially since the lambdas are almost identical already. #WontFix There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. no and we don't need to keep on passing it. In reply to: 101328139 [](ancestors = 101328139) |
||
CGContextSetFillColorSpace(drawingContext, context->FillColorSpace()); | ||
CGContextSetFillColor(drawingContext, components); | ||
if (isColored) { | ||
// It's a colored pattern, the color is specified by the pattern callback. | ||
// The 'components' are alpha value. | ||
CGContextSetAlpha(drawingContext, components[0]); | ||
} else { | ||
// It is a stencil pattern, we should stage the context's color | ||
// The 'components' are the color for the stencil. | ||
CGContextSetFillColor(drawingContext, components); | ||
} | ||
|
||
return S_OK; | ||
})); | ||
// set the fill brush | ||
|
@@ -1879,9 +1892,17 @@ void CGContextSetStrokePattern(CGContextRef context, CGPatternRef pattern, const | |
} | ||
|
||
ComPtr<ID2D1BitmapBrush1> bitmapBrush; | ||
FAIL_FAST_IF_FAILED(_CreatePatternBrush(context, pattern, components, &bitmapBrush, [&](CGContextRef drawingContext) { | ||
FAIL_FAST_IF_FAILED(_CreatePatternBrush(context, pattern, &bitmapBrush, [&](CGContextRef drawingContext, bool isColored) { | ||
CGContextSetStrokeColorSpace(drawingContext, context->StrokeColorSpace()); | ||
CGContextSetStrokeColor(drawingContext, components); | ||
if (isColored) { | ||
// It's a colored pattern, the color is specified by the pattern callback. | ||
// The 'components' are alpha value. | ||
CGContextSetAlpha(drawingContext, components[0]); | ||
} else { | ||
// It is a stencil pattern, we should stage the context's color | ||
// The 'components' are the color for the stencil. | ||
CGContextSetStrokeColor(drawingContext, components); | ||
} | ||
return S_OK; | ||
})); | ||
// set the stroke brush | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: make the out param the last one #WontFix
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it's much nicer to have the lambda last in my opinion.
In reply to: 101327104 [](ancestors = 101327104)