Skip to content

Commit

Permalink
Use memcpy in SVGPathByteStreamSource::readType
Browse files Browse the repository at this point in the history
https://bugs.webkit.org/show_bug.cgi?id=250676
rdar://104552389

Reviewed by Ryosuke Niwa.

Use memcpy in SVGPathByteStreamSource::readType and simplify the
SVGPathByteStreamBuilder::writeType() logic.

* Source/WebCore/svg/SVGPathByteStream.h:
(WebCore::SVGPathByteStream::append):
(): Deleted.
* Source/WebCore/svg/SVGPathByteStreamBuilder.cpp:
(WebCore::SVGPathByteStreamBuilder::moveTo):
(WebCore::SVGPathByteStreamBuilder::lineTo):
(WebCore::SVGPathByteStreamBuilder::lineToHorizontal):
(WebCore::SVGPathByteStreamBuilder::lineToVertical):
(WebCore::SVGPathByteStreamBuilder::curveToCubic):
(WebCore::SVGPathByteStreamBuilder::curveToCubicSmooth):
(WebCore::SVGPathByteStreamBuilder::curveToQuadratic):
(WebCore::SVGPathByteStreamBuilder::curveToQuadraticSmooth):
(WebCore::SVGPathByteStreamBuilder::arcTo):
(WebCore::SVGPathByteStreamBuilder::closePath):
* Source/WebCore/svg/SVGPathByteStreamBuilder.h:
* Source/WebCore/svg/SVGPathByteStreamSource.h:

Canonical link: https://commits.webkit.org/267523@main
  • Loading branch information
cdumez committed Aug 31, 2023
1 parent 589a9da commit e2aa0c2
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 62 deletions.
11 changes: 0 additions & 11 deletions Source/WebCore/svg/SVGPathByteStream.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,6 @@

namespace WebCore {

// Type definitions for the byte stream data
typedef union {
bool value;
unsigned char bytes[sizeof(bool)];
} BoolByte;

typedef union {
float value;
unsigned char bytes[sizeof(float)];
} FloatByte;

class SVGPathByteStream {
WTF_MAKE_FAST_ALLOCATED;
public:
Expand Down
32 changes: 16 additions & 16 deletions Source/WebCore/svg/SVGPathByteStreamBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ SVGPathByteStreamBuilder::SVGPathByteStreamBuilder(SVGPathByteStream& byteStream

void SVGPathByteStreamBuilder::moveTo(const FloatPoint& targetPoint, bool, PathCoordinateMode mode)
{
writeSegmentType(mode == RelativeCoordinates ? SVGPathSegType::MoveToRel : SVGPathSegType::MoveToAbs);
writeType(mode == RelativeCoordinates ? SVGPathSegType::MoveToRel : SVGPathSegType::MoveToAbs);
writeFloatPoint(targetPoint);
}

Expand All @@ -46,58 +46,58 @@ void SVGPathByteStreamBuilder::lineTo(const FloatPoint& targetPoint, PathCoordin

void SVGPathByteStreamBuilder::lineToHorizontal(float x, PathCoordinateMode mode)
{
writeSegmentType(mode == RelativeCoordinates ? SVGPathSegType::LineToHorizontalRel : SVGPathSegType::LineToHorizontalAbs);
writeFloat(x);
writeType(mode == RelativeCoordinates ? SVGPathSegType::LineToHorizontalRel : SVGPathSegType::LineToHorizontalAbs);
writeType(x);
}

void SVGPathByteStreamBuilder::lineToVertical(float y, PathCoordinateMode mode)
{
writeSegmentType(mode == RelativeCoordinates ? SVGPathSegType::LineToVerticalRel : SVGPathSegType::LineToVerticalAbs);
writeFloat(y);
writeType(mode == RelativeCoordinates ? SVGPathSegType::LineToVerticalRel : SVGPathSegType::LineToVerticalAbs);
writeType(y);
}

void SVGPathByteStreamBuilder::curveToCubic(const FloatPoint& point1, const FloatPoint& point2, const FloatPoint& targetPoint, PathCoordinateMode mode)
{
writeSegmentType(mode == RelativeCoordinates ? SVGPathSegType::CurveToCubicRel : SVGPathSegType::CurveToCubicAbs);
writeType(mode == RelativeCoordinates ? SVGPathSegType::CurveToCubicRel : SVGPathSegType::CurveToCubicAbs);
writeFloatPoint(point1);
writeFloatPoint(point2);
writeFloatPoint(targetPoint);
}

void SVGPathByteStreamBuilder::curveToCubicSmooth(const FloatPoint& point2, const FloatPoint& targetPoint, PathCoordinateMode mode)
{
writeSegmentType(mode == RelativeCoordinates ? SVGPathSegType::CurveToCubicSmoothRel : SVGPathSegType::CurveToCubicSmoothAbs);
writeType(mode == RelativeCoordinates ? SVGPathSegType::CurveToCubicSmoothRel : SVGPathSegType::CurveToCubicSmoothAbs);
writeFloatPoint(point2);
writeFloatPoint(targetPoint);
}

void SVGPathByteStreamBuilder::curveToQuadratic(const FloatPoint& point1, const FloatPoint& targetPoint, PathCoordinateMode mode)
{
writeSegmentType(mode == RelativeCoordinates ? SVGPathSegType::CurveToQuadraticRel : SVGPathSegType::CurveToQuadraticAbs);
writeType(mode == RelativeCoordinates ? SVGPathSegType::CurveToQuadraticRel : SVGPathSegType::CurveToQuadraticAbs);
writeFloatPoint(point1);
writeFloatPoint(targetPoint);
}

void SVGPathByteStreamBuilder::curveToQuadraticSmooth(const FloatPoint& targetPoint, PathCoordinateMode mode)
{
writeSegmentType(mode == RelativeCoordinates ? SVGPathSegType::CurveToQuadraticSmoothRel : SVGPathSegType::CurveToQuadraticSmoothAbs);
writeType(mode == RelativeCoordinates ? SVGPathSegType::CurveToQuadraticSmoothRel : SVGPathSegType::CurveToQuadraticSmoothAbs);
writeFloatPoint(targetPoint);
}

void SVGPathByteStreamBuilder::arcTo(float r1, float r2, float angle, bool largeArcFlag, bool sweepFlag, const FloatPoint& targetPoint, PathCoordinateMode mode)
{
writeSegmentType(mode == RelativeCoordinates ? SVGPathSegType::ArcRel : SVGPathSegType::ArcAbs);
writeFloat(r1);
writeFloat(r2);
writeFloat(angle);
writeFlag(largeArcFlag);
writeFlag(sweepFlag);
writeType(mode == RelativeCoordinates ? SVGPathSegType::ArcRel : SVGPathSegType::ArcAbs);
writeType(r1);
writeType(r2);
writeType(angle);
writeType(largeArcFlag);
writeType(sweepFlag);
writeFloatPoint(targetPoint);
}

void SVGPathByteStreamBuilder::closePath()
{
writeSegmentType(SVGPathSegType::ClosePath);
writeType(SVGPathSegType::ClosePath);
}

} // namespace WebCore
28 changes: 10 additions & 18 deletions Source/WebCore/svg/SVGPathByteStreamBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,30 +51,22 @@ class SVGPathByteStreamBuilder final : public SVGPathConsumer {
void curveToQuadraticSmooth(const FloatPoint&, PathCoordinateMode) final;
void arcTo(float, float, float, bool largeArcFlag, bool sweepFlag, const FloatPoint&, PathCoordinateMode) final;

template<typename ByteType>
void writeType(const ByteType& type)
template<typename DataType>
void writeType(const DataType& data)
{
m_byteStream.append(std::span { type.bytes, sizeof(ByteType) });
}
typedef union {
DataType value;
uint8_t bytes[sizeof(DataType)];
} ByteType;

void writeFlag(bool value)
{
BoolByte data;
data.value = value;
writeType(data);
}

void writeFloat(float value)
{
FloatByte data;
data.value = value;
writeType(data);
ByteType type = { data };
m_byteStream.append(std::span { type.bytes, sizeof(ByteType) });
}

void writeFloatPoint(const FloatPoint& point)
{
writeFloat(point.x());
writeFloat(point.y());
writeType(point.x());
writeType(point.y());
}

void writeSegmentType(SVGPathSegType type)
Expand Down
29 changes: 12 additions & 17 deletions Source/WebCore/svg/SVGPathByteStreamSource.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,43 +48,38 @@ class SVGPathByteStreamSource final : public SVGPathSource {
#if COMPILER(MSVC)
#pragma warning(disable: 4701)
#endif
template<typename DataType, typename ByteType>
template<typename DataType>
DataType readType()
{
ByteType data;
size_t typeSize = sizeof(ByteType);
DataType data;
size_t dataSize = sizeof(DataType);

for (size_t i = 0; i < typeSize; ++i) {
ASSERT_WITH_SECURITY_IMPLICATION(m_streamCurrent < m_streamEnd);
data.bytes[i] = *m_streamCurrent;
++m_streamCurrent;
}

return data.value;
ASSERT_WITH_SECURITY_IMPLICATION(m_streamCurrent + dataSize <= m_streamEnd);
memcpy(&data, m_streamCurrent, dataSize);
m_streamCurrent += dataSize;
return data;
}

bool readFlag()
{
return readType<bool, BoolByte>();
return readType<bool>();
}

float readFloat()
{
return readType<float, FloatByte>();
return readType<float>();
}

SVGPathSegType readSVGSegmentType()
{
static_assert(std::is_same_v<std::underlying_type_t<SVGPathSegType>, uint8_t>);
uint8_t byte = *m_streamCurrent;
++m_streamCurrent;
return static_cast<SVGPathSegType>(byte);
return static_cast<SVGPathSegType>(readType<uint8_t>());
}

FloatPoint readFloatPoint()
{
float x = readType<float, FloatByte>();
float y = readType<float, FloatByte>();
float x = readType<float>();
float y = readType<float>();
return FloatPoint(x, y);
}

Expand Down

0 comments on commit e2aa0c2

Please sign in to comment.