Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(453)

Side by Side Diff: Source/core/svg/SVGPathByteStreamBuilder.cpp

Issue 149913004: Coalesce writes in SVGPathByteStreamBuilder (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: Created 6 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « Source/core/svg/SVGPathByteStreamBuilder.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright (C) Research In Motion Limited 2010. All rights reserved. 2 * Copyright (C) Research In Motion Limited 2010. All rights reserved.
3 * 3 *
4 * This library is free software; you can redistribute it and/or 4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public 5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either 6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version. 7 * version 2 of the License, or (at your option) any later version.
8 * 8 *
9 * This library is distributed in the hope that it will be useful, 9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details. 12 * Library General Public License for more details.
13 * 13 *
14 * You should have received a copy of the GNU Library General Public License 14 * You should have received a copy of the GNU Library General Public License
15 * along with this library; see the file COPYING.LIB. If not, write to 15 * along with this library; see the file COPYING.LIB. If not, write to
16 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 16 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA. 17 * Boston, MA 02110-1301, USA.
18 */ 18 */
19 19
20 #include "config.h" 20 #include "config.h"
21 21
22 #include "core/svg/SVGPathByteStreamBuilder.h" 22 #include "core/svg/SVGPathByteStreamBuilder.h"
23 23
24 #include "core/svg/SVGPathSeg.h" 24 #include "core/svg/SVGPathSeg.h"
25 #include "wtf/OwnPtr.h" 25 #include "wtf/OwnPtr.h"
26 26
27 namespace WebCore { 27 namespace WebCore {
28 28
29 // Helper class that coalesces writes to a SVGPathByteStream to a local buffer.
30 class CoalescingBuffer {
31 public:
32 CoalescingBuffer(SVGPathByteStream* byteStream)
33 : m_currentOffset(0)
34 , m_byteStream(byteStream)
35 {
36 ASSERT(byteStream);
37 }
38 ~CoalescingBuffer()
39 {
40 for (size_t i = 0; i < m_currentOffset; ++i)
41 m_byteStream->append(m_bytes[i]);
Chris Evans 2014/02/05 18:54:48 Drive-by: I was looking at this recently and notic
42 }
43
44 template<typename DataType>
45 void writeType(DataType value)
46 {
47 ByteType<DataType> data;
48 data.value = value;
49 size_t typeSize = sizeof(ByteType<DataType>);
50 ASSERT(m_currentOffset + typeSize <= sizeof(m_bytes));
51 memcpy(m_bytes + m_currentOffset, data.bytes, typeSize);
52 m_currentOffset += typeSize;
53 }
54
55 void writeFlag(bool value) { writeType<bool>(value); }
56 void writeFloat(float value) { writeType<float>(value); }
57 void writeFloatPoint(const FloatPoint& point)
58 {
59 writeType<float>(point.x());
60 writeType<float>(point.y());
61 }
62 void writeSegmentType(unsigned short value) { writeType<unsigned short>(valu e); }
63
64 private:
65 // Adjust size to fit the largest command (in serialized/byte-stream format) .
66 // Currently a cubic segment.
67 size_t m_currentOffset;
68 unsigned char m_bytes[sizeof(unsigned short) + sizeof(FloatPoint) * 3];
69 SVGPathByteStream* m_byteStream;
70 };
71
29 SVGPathByteStreamBuilder::SVGPathByteStreamBuilder() 72 SVGPathByteStreamBuilder::SVGPathByteStreamBuilder()
30 : m_byteStream(0) 73 : m_byteStream(0)
31 { 74 {
32 } 75 }
33 76
34 void SVGPathByteStreamBuilder::moveTo(const FloatPoint& targetPoint, bool, PathC oordinateMode mode) 77 void SVGPathByteStreamBuilder::moveTo(const FloatPoint& targetPoint, bool, PathC oordinateMode mode)
35 { 78 {
36 ASSERT(m_byteStream); 79 CoalescingBuffer buffer(m_byteStream);
37 writeSegmentType(mode == RelativeCoordinates ? PathSegMoveToRel : PathSegMo veToAbs); 80 buffer.writeSegmentType(mode == RelativeCoordinates ? PathSegMoveToRel : Pa thSegMoveToAbs);
38 writeFloatPoint(targetPoint); 81 buffer.writeFloatPoint(targetPoint);
39 } 82 }
40 83
41 void SVGPathByteStreamBuilder::lineTo(const FloatPoint& targetPoint, PathCoordin ateMode mode) 84 void SVGPathByteStreamBuilder::lineTo(const FloatPoint& targetPoint, PathCoordin ateMode mode)
42 { 85 {
43 ASSERT(m_byteStream); 86 CoalescingBuffer buffer(m_byteStream);
44 writeSegmentType(mode == RelativeCoordinates ? PathSegLineToRel : PathSegLin eToAbs); 87 buffer.writeSegmentType(mode == RelativeCoordinates ? PathSegLineToRel : Pat hSegLineToAbs);
45 writeFloatPoint(targetPoint); 88 buffer.writeFloatPoint(targetPoint);
46 } 89 }
47 90
48 void SVGPathByteStreamBuilder::lineToHorizontal(float x, PathCoordinateMode mode ) 91 void SVGPathByteStreamBuilder::lineToHorizontal(float x, PathCoordinateMode mode )
49 { 92 {
50 ASSERT(m_byteStream); 93 CoalescingBuffer buffer(m_byteStream);
51 writeSegmentType(mode == RelativeCoordinates ? PathSegLineToHorizontalRel : PathSegLineToHorizontalAbs); 94 buffer.writeSegmentType(mode == RelativeCoordinates ? PathSegLineToHorizonta lRel : PathSegLineToHorizontalAbs);
52 writeFloat(x); 95 buffer.writeFloat(x);
53 } 96 }
54 97
55 void SVGPathByteStreamBuilder::lineToVertical(float y, PathCoordinateMode mode) 98 void SVGPathByteStreamBuilder::lineToVertical(float y, PathCoordinateMode mode)
56 { 99 {
57 ASSERT(m_byteStream); 100 CoalescingBuffer buffer(m_byteStream);
58 writeSegmentType(mode == RelativeCoordinates ? PathSegLineToVerticalRel : Pa thSegLineToVerticalAbs); 101 buffer.writeSegmentType(mode == RelativeCoordinates ? PathSegLineToVerticalR el : PathSegLineToVerticalAbs);
59 writeFloat(y); 102 buffer.writeFloat(y);
60 } 103 }
61 104
62 void SVGPathByteStreamBuilder::curveToCubic(const FloatPoint& point1, const Floa tPoint& point2, const FloatPoint& targetPoint, PathCoordinateMode mode) 105 void SVGPathByteStreamBuilder::curveToCubic(const FloatPoint& point1, const Floa tPoint& point2, const FloatPoint& targetPoint, PathCoordinateMode mode)
63 { 106 {
64 ASSERT(m_byteStream); 107 CoalescingBuffer buffer(m_byteStream);
65 writeSegmentType(mode == RelativeCoordinates ? PathSegCurveToCubicRel : Path SegCurveToCubicAbs); 108 buffer.writeSegmentType(mode == RelativeCoordinates ? PathSegCurveToCubicRel : PathSegCurveToCubicAbs);
66 writeFloatPoint(point1); 109 buffer.writeFloatPoint(point1);
67 writeFloatPoint(point2); 110 buffer.writeFloatPoint(point2);
68 writeFloatPoint(targetPoint); 111 buffer.writeFloatPoint(targetPoint);
69 } 112 }
70 113
71 void SVGPathByteStreamBuilder::curveToCubicSmooth(const FloatPoint& point2, cons t FloatPoint& targetPoint, PathCoordinateMode mode) 114 void SVGPathByteStreamBuilder::curveToCubicSmooth(const FloatPoint& point2, cons t FloatPoint& targetPoint, PathCoordinateMode mode)
72 { 115 {
73 ASSERT(m_byteStream); 116 CoalescingBuffer buffer(m_byteStream);
74 writeSegmentType(mode == RelativeCoordinates ? PathSegCurveToCubicSmoothRel : PathSegCurveToCubicSmoothAbs); 117 buffer.writeSegmentType(mode == RelativeCoordinates ? PathSegCurveToCubicSmo othRel : PathSegCurveToCubicSmoothAbs);
75 writeFloatPoint(point2); 118 buffer.writeFloatPoint(point2);
76 writeFloatPoint(targetPoint); 119 buffer.writeFloatPoint(targetPoint);
77 } 120 }
78 121
79 void SVGPathByteStreamBuilder::curveToQuadratic(const FloatPoint& point1, const FloatPoint& targetPoint, PathCoordinateMode mode) 122 void SVGPathByteStreamBuilder::curveToQuadratic(const FloatPoint& point1, const FloatPoint& targetPoint, PathCoordinateMode mode)
80 { 123 {
81 ASSERT(m_byteStream); 124 CoalescingBuffer buffer(m_byteStream);
82 writeSegmentType(mode == RelativeCoordinates ? PathSegCurveToQuadraticRel : PathSegCurveToQuadraticAbs); 125 buffer.writeSegmentType(mode == RelativeCoordinates ? PathSegCurveToQuadrati cRel : PathSegCurveToQuadraticAbs);
83 writeFloatPoint(point1); 126 buffer.writeFloatPoint(point1);
84 writeFloatPoint(targetPoint); 127 buffer.writeFloatPoint(targetPoint);
85 } 128 }
86 129
87 void SVGPathByteStreamBuilder::curveToQuadraticSmooth(const FloatPoint& targetPo int, PathCoordinateMode mode) 130 void SVGPathByteStreamBuilder::curveToQuadraticSmooth(const FloatPoint& targetPo int, PathCoordinateMode mode)
88 { 131 {
89 ASSERT(m_byteStream); 132 CoalescingBuffer buffer(m_byteStream);
90 writeSegmentType(mode == RelativeCoordinates ? PathSegCurveToQuadraticSmooth Rel : PathSegCurveToQuadraticSmoothAbs); 133 buffer.writeSegmentType(mode == RelativeCoordinates ? PathSegCurveToQuadrati cSmoothRel : PathSegCurveToQuadraticSmoothAbs);
91 writeFloatPoint(targetPoint); 134 buffer.writeFloatPoint(targetPoint);
92 } 135 }
93 136
94 void SVGPathByteStreamBuilder::arcTo(float r1, float r2, float angle, bool large ArcFlag, bool sweepFlag, const FloatPoint& targetPoint, PathCoordinateMode mode) 137 void SVGPathByteStreamBuilder::arcTo(float r1, float r2, float angle, bool large ArcFlag, bool sweepFlag, const FloatPoint& targetPoint, PathCoordinateMode mode)
95 { 138 {
96 ASSERT(m_byteStream); 139 CoalescingBuffer buffer(m_byteStream);
97 writeSegmentType(mode == RelativeCoordinates ? PathSegArcRel : PathSegArcAbs ); 140 buffer.writeSegmentType(mode == RelativeCoordinates ? PathSegArcRel : PathSe gArcAbs);
98 writeFloat(r1); 141 buffer.writeFloat(r1);
99 writeFloat(r2); 142 buffer.writeFloat(r2);
100 writeFloat(angle); 143 buffer.writeFloat(angle);
101 writeFlag(largeArcFlag); 144 buffer.writeFlag(largeArcFlag);
102 writeFlag(sweepFlag); 145 buffer.writeFlag(sweepFlag);
103 writeFloatPoint(targetPoint); 146 buffer.writeFloatPoint(targetPoint);
104 } 147 }
105 148
106 void SVGPathByteStreamBuilder::closePath() 149 void SVGPathByteStreamBuilder::closePath()
107 { 150 {
108 ASSERT(m_byteStream); 151 CoalescingBuffer buffer(m_byteStream);
109 writeSegmentType(PathSegClosePath); 152 buffer.writeSegmentType(PathSegClosePath);
110 } 153 }
111 154
112 } // namespace WebCore 155 } // namespace WebCore
OLDNEW
« no previous file with comments | « Source/core/svg/SVGPathByteStreamBuilder.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698