Skip to content

Commit

Permalink
Update CGContext, CGPath and sorta CGImage for the CFCppBase changes. (
Browse files Browse the repository at this point in the history
  • Loading branch information
DHowett authored Nov 10, 2016
1 parent 19715b6 commit 0da72bd
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 54 deletions.
49 changes: 22 additions & 27 deletions Frameworks/CoreGraphics/CGContext.mm
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ inline void ClearStrokeStyle() {
}
};

struct __CGContextImpl {
struct __CGContext : CoreFoundation::CppBase<__CGContext> {
ComPtr<ID2D1RenderTarget> renderTarget{ nullptr };

// Calculated at creation time, this transform flips CG's drawing commands,
Expand All @@ -142,47 +142,45 @@ inline void ClearStrokeStyle() {
bool allowsFontSubpixelPositioning = false;
bool allowsFontSubpixelQuantization = false;

__CGContextImpl() {
__CGContext() {
// Set up a default/baseline state
stateStack.emplace();
}
};

struct __CGContext : CoreFoundation::CppBase<__CGContext, __CGContextImpl> {
inline ComPtr<ID2D1RenderTarget>& RenderTarget() {
return _impl.renderTarget;
return renderTarget;
}

inline std::stack<__CGContextDrawingState>& GStateStack() {
return _impl.stateStack;
return stateStack;
}

inline __CGContextDrawingState& CurrentGState() {
return GStateStack().top();
}

inline bool HasPath() {
return _impl.currentPath != nullptr;
return currentPath != nullptr;
}

inline CGMutablePathRef Path() {
if (!_impl.currentPath) {
_impl.currentPath.reset(CGPathCreateMutable());
if (!currentPath) {
currentPath.reset(CGPathCreateMutable());
}
return _impl.currentPath.get();
return currentPath.get();
}

inline void SetPath(CGMutablePathRef path) {
_impl.currentPath.reset(CGPathRetain(path));
currentPath.reset(CGPathRetain(path));
}

inline void ClearPath() {
_impl.currentPath.reset();
currentPath.reset();
}

inline ComPtr<ID2D1Factory> Factory() {
ComPtr<ID2D1Factory> factory;
_impl.renderTarget->GetFactory(&factory);
renderTarget->GetFactory(&factory);
return factory;
}
};
Expand All @@ -208,7 +206,7 @@ CFTypeID CGContextGetTypeID() {

#pragma region Global State - Lifetime
static void __CGContextInitWithRenderTarget(CGContextRef context, ID2D1RenderTarget* renderTarget) {
context->_impl.renderTarget = renderTarget;
context->renderTarget = renderTarget;
// Reference platform defaults:
// * Fill : fully transparent black
// * Stroke: fully opaque black
Expand All @@ -219,7 +217,7 @@ static void __CGContextInitWithRenderTarget(CGContextRef context, ID2D1RenderTar
// CG is a lower-left origin system (LLO), but D2D is upper left (ULO).
// We have to translate the render area back onscreen and flip it up to ULO.
D2D1_SIZE_F targetSize = renderTarget->GetSize();
context->Impl().deviceTransform = CGAffineTransformMake(1.f, 0.f, 0.f, -1.f, 0.f, targetSize.height);
context->deviceTransform = CGAffineTransformMake(1.f, 0.f, 0.f, -1.f, 0.f, targetSize.height);
}

CGContextRef _CGContextCreateWithD2DRenderTarget(ID2D1RenderTarget* renderTarget) {
Expand Down Expand Up @@ -415,7 +413,7 @@ CGPoint CGContextConvertPointToDeviceSpace(CGContextRef context, CGPoint point)
*/
CGAffineTransform CGContextGetUserSpaceToDeviceSpaceTransform(CGContextRef context) {
NOISY_RETURN_IF_NULL(context, StubReturn());
return CGAffineTransformConcat(context->CurrentGState().transform, context->Impl().deviceTransform);
return CGAffineTransformConcat(context->CurrentGState().transform, context->deviceTransform);
}
#pragma endregion

Expand Down Expand Up @@ -1158,7 +1156,7 @@ void CGContextSetCMYKStrokeColor(CGContextRef context, CGFloat cyan, CGFloat mag
// This is that private interface.
void _CGContextSetShadowProjectionTransform(CGContextRef context, CGAffineTransform transform) {
NOISY_RETURN_IF_NULL(context);
context->Impl().shadowProjectionTransform = transform;
context->shadowProjectionTransform = transform;
}

/**
Expand All @@ -1170,7 +1168,7 @@ void CGContextSetShadow(CGContextRef context, CGSize offset, CGFloat blur) {
auto& state = context->CurrentGState();
// The default shadow colour on the reference platform is black at 33% alpha.
state.shadowColor = { 0.f, 0.f, 0.f, 1.f / 3.f };
state.shadowOffset = CGSizeApplyAffineTransform(offset, context->Impl().shadowProjectionTransform);
state.shadowOffset = CGSizeApplyAffineTransform(offset, context->shadowProjectionTransform);
state.shadowBlur = blur;
}

Expand All @@ -1190,7 +1188,7 @@ void CGContextSetShadowWithColor(CGContextRef context, CGSize offset, CGFloat bl
// This is in line with the reference platform's shadowing specification.
state.shadowColor = { 0.f, 0.f, 0.f, 0.f };
}
state.shadowOffset = CGSizeApplyAffineTransform(offset, context->Impl().shadowProjectionTransform);
state.shadowOffset = CGSizeApplyAffineTransform(offset, context->shadowProjectionTransform);
state.shadowBlur = blur;
}
#pragma endregion
Expand Down Expand Up @@ -1405,7 +1403,7 @@ static HRESULT __CGContextCreateShadowEffect(CGContextRef context,
RETURN_IF_FAILED(deviceContext->CreateEffect(CLSID_D2D12DAffineTransform, &affineTransformEffect));
affineTransformEffect->SetInputEffect(0, shadowEffect.Get());

CGSize deviceTransformedShadowOffset = CGSizeApplyAffineTransform(state.shadowOffset, context->Impl().deviceTransform);
CGSize deviceTransformedShadowOffset = CGSizeApplyAffineTransform(state.shadowOffset, context->deviceTransform);
RETURN_IF_FAILED(affineTransformEffect->SetValue(D2D1_2DAFFINETRANSFORM_PROP_TRANSFORM_MATRIX,
D2D1::Matrix3x2F::Translation(deviceTransformedShadowOffset.width,
deviceTransformedShadowOffset.height)));
Expand Down Expand Up @@ -1820,14 +1818,11 @@ void CGContextDrawGlyphRun(CGContextRef context, const DWRITE_GLYPH_RUN* glyphRu
#pragma endregion

#pragma region CGBitmapContext
struct __CGBitmapContextImpl {
woc::unique_cf<CGImageRef> image;
};
struct __CGBitmapContext : CoreFoundation::CppBase<__CGBitmapContext, __CGContext> {
woc::unique_cf<CGImageRef> _image;

struct __CGBitmapContext : CoreFoundation::CppBase<__CGBitmapContext, __CGBitmapContextImpl, __CGContext> {
inline void SetImage(CGImageRef image) {
_impl.image.reset(image);
CGImageRetain(image);
_image.reset(CGImageRetain(image));
}
};

Expand Down Expand Up @@ -1909,7 +1904,7 @@ CGImageRef CGBitmapContextGetImage(CGContextRef context) {
TraceError(TAG, L"Image requested from non-bitmap CGContext.");
return nullptr;
}
return ((__CGBitmapContext*)context)->Impl().image.get();
return ((__CGBitmapContext*)context)->_image.get();
}

CGContextRef _CGBitmapContextCreateWithRenderTarget(ID2D1RenderTarget* renderTarget, CGImageRef img) {
Expand Down
50 changes: 24 additions & 26 deletions Frameworks/CoreGraphics/CGPath.mm
Original file line number Diff line number Diff line change
Expand Up @@ -46,41 +46,39 @@ inline CGPoint __CreateCGPointWithTransform(CGFloat x, CGFloat y, const CGAffine
using namespace std;
using namespace Microsoft::WRL;

struct __CGPathImpl {
struct __CGPath : CoreFoundation::CppBase<__CGPath> {
ComPtr<ID2D1PathGeometry> pathGeometry;
ComPtr<ID2D1GeometrySink> geometrySink;

bool figureClosed;
CGPoint currentPoint{ 0, 0 };
CGPoint startingPoint{ 0, 0 };

__CGPathImpl() : figureClosed(true) {
__CGPath() : figureClosed(true) {
}
};

struct __CGPath : CoreFoundation::CppBase<__CGPath, __CGPathImpl> {
ComPtr<ID2D1PathGeometry> GetPathGeometry() {
return _impl.pathGeometry;
return pathGeometry;
}

ComPtr<ID2D1GeometrySink> GetGeometrySink() {
return _impl.geometrySink;
return geometrySink;
}

CGPoint GetCurrentPoint() {
return _impl.currentPoint;
return currentPoint;
}

CGPoint GetStartingPoint() {
return _impl.startingPoint;
return startingPoint;
}

void SetCurrentPoint(CGPoint newPoint) {
_impl.currentPoint = newPoint;
currentPoint = newPoint;
}

void SetStartingPoint(CGPoint newPoint) {
_impl.startingPoint = newPoint;
startingPoint = newPoint;
}

// A private helper function for re-opening a path geometry. CGPath does not
Expand All @@ -90,7 +88,7 @@ void SetStartingPoint(CGPoint newPoint) {
// we must open the path again. This cannot be done normally, so we must
// create a new path with the old path information to edit.
HRESULT PreparePathForEditing() {
if (!_impl.geometrySink) {
if (!geometrySink) {
// Re-open this geometry.
ComPtr<ID2D1Factory> factory;
RETURN_IF_FAILED(_CGGetD2DFactory(&factory));
Expand All @@ -104,46 +102,46 @@ HRESULT PreparePathForEditing() {
// reason so this will force it to do otherwise.
RETURN_IF_FAILED(factory->CreatePathGeometry(&newPath));
RETURN_IF_FAILED(newPath->Open(&newSink));
RETURN_IF_FAILED(_impl.pathGeometry->Stream(newSink.Get()));
RETURN_IF_FAILED(pathGeometry->Stream(newSink.Get()));

_impl.pathGeometry = newPath;
_impl.geometrySink = newSink;
pathGeometry = newPath;
geometrySink = newSink;

// Without a new figure being created, it's by default closed
_impl.figureClosed = true;
figureClosed = true;
}
return S_OK;
}

HRESULT ClosePath() {
if (_impl.geometrySink) {
if (geometrySink) {
EndFigure(D2D1_FIGURE_END_OPEN);
RETURN_IF_FAILED(_impl.geometrySink->Close());
_impl.geometrySink = nullptr;
RETURN_IF_FAILED(geometrySink->Close());
geometrySink = nullptr;
}
return S_OK;
}

void BeginFigure() {
if (_impl.figureClosed) {
_impl.geometrySink->BeginFigure(_CGPointToD2D_F(_impl.currentPoint), D2D1_FIGURE_BEGIN_FILLED);
_impl.figureClosed = false;
if (figureClosed) {
geometrySink->BeginFigure(_CGPointToD2D_F(currentPoint), D2D1_FIGURE_BEGIN_FILLED);
figureClosed = false;
}
}

void EndFigure(D2D1_FIGURE_END figureStatus) {
if (!_impl.figureClosed) {
_impl.geometrySink->EndFigure(figureStatus);
_impl.figureClosed = true;
if (!figureClosed) {
geometrySink->EndFigure(figureStatus);
figureClosed = true;
}
}

HRESULT InitializeGeometries() {
ComPtr<ID2D1Factory> factory;
RETURN_IF_FAILED(_CGGetD2DFactory(&factory));

RETURN_IF_FAILED(factory->CreatePathGeometry(&_impl.pathGeometry));
RETURN_IF_FAILED(_impl.pathGeometry->Open(&_impl.geometrySink));
RETURN_IF_FAILED(factory->CreatePathGeometry(&pathGeometry));
RETURN_IF_FAILED(pathGeometry->Open(&geometrySink));

return S_OK;
}
Expand Down
5 changes: 4 additions & 1 deletion Frameworks/include/CGImageInternal.h
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,10 @@ struct __CGImageImpl {
}
};

struct __CGImage : CoreFoundation::CppBase<__CGImage, __CGImageImpl> {
struct __CGImage : CoreFoundation::CppBase<__CGImage> {
// TODO(JJ) REMOVE THIS; Merge Impl into __CGImage.
__CGImageImpl _impl;

inline Microsoft::WRL::ComPtr<IWICBitmap>& ImageSource() {
return _impl.bitmapImageSource;
}
Expand Down

0 comments on commit 0da72bd

Please sign in to comment.