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

Side by Side Diff: Source/platform/graphics/Gradient.cpp

Issue 201503002: Simplify Gradient by holding a Color instead of four floats. (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: Created 6 years, 9 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
« no previous file with comments | « Source/platform/graphics/Gradient.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) 2006, 2007, 2008, 2010 Apple Inc. All rights reserved. 2 * Copyright (C) 2006, 2007, 2008, 2010 Apple Inc. All rights reserved.
3 * Copyright (C) 2007 Alp Toker <alp@atoker.com> 3 * Copyright (C) 2007 Alp Toker <alp@atoker.com>
4 * Copyright (C) 2013 Google Inc. All rights reserved. 4 * Copyright (C) 2013 Google Inc. All rights reserved.
5 * 5 *
6 * Redistribution and use in source and binary forms, with or without 6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions 7 * modification, are permitted provided that the following conditions
8 * are met: 8 * are met:
9 * 1. Redistributions of source code must retain the above copyright 9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer. 10 * notice, this list of conditions and the following disclaimer.
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
63 , m_spreadMethod(SpreadMethodPad) 63 , m_spreadMethod(SpreadMethodPad)
64 { 64 {
65 } 65 }
66 66
67 Gradient::~Gradient() 67 Gradient::~Gradient()
68 { 68 {
69 } 69 }
70 70
71 void Gradient::addColorStop(float value, const Color& color) 71 void Gradient::addColorStop(float value, const Color& color)
72 { 72 {
73 float r; 73 m_stops.append(ColorStop(value, color));
74 float g;
75 float b;
76 float a;
77 color.getRGBA(r, g, b, a);
78 m_stops.append(ColorStop(value, r, g, b, a));
79 74
80 m_stopsSorted = false; 75 m_stopsSorted = false;
81 m_gradient.clear(); 76 m_gradient.clear();
82 } 77 }
83 78
84 void Gradient::addColorStop(const Gradient::ColorStop& stop) 79 void Gradient::addColorStop(const Gradient::ColorStop& stop)
85 { 80 {
86 m_stops.append(stop); 81 m_stops.append(stop);
87 82
88 m_stopsSorted = false; 83 m_stopsSorted = false;
(...skipping 14 matching lines...) Expand all
103 98
104 if (!m_stops.size()) 99 if (!m_stops.size())
105 return; 100 return;
106 101
107 std::stable_sort(m_stops.begin(), m_stops.end(), compareStops); 102 std::stable_sort(m_stops.begin(), m_stops.end(), compareStops);
108 } 103 }
109 104
110 bool Gradient::hasAlpha() const 105 bool Gradient::hasAlpha() const
111 { 106 {
112 for (size_t i = 0; i < m_stops.size(); i++) { 107 for (size_t i = 0; i < m_stops.size(); i++) {
113 if (m_stops[i].alpha < 1) 108 if (m_stops[i].color.hasAlpha())
114 return true; 109 return true;
115 } 110 }
116 111
117 return false; 112 return false;
118 } 113 }
119 114
120 void Gradient::setSpreadMethod(GradientSpreadMethod spreadMethod) 115 void Gradient::setSpreadMethod(GradientSpreadMethod spreadMethod)
121 { 116 {
122 // FIXME: Should it become necessary, allow calls to this method after m_gra dient has been set. 117 // FIXME: Should it become necessary, allow calls to this method after m_gra dient has been set.
123 ASSERT(!m_gradient); 118 ASSERT(!m_gradient);
(...skipping 16 matching lines...) Expand all
140 void Gradient::setGradientSpaceTransform(const AffineTransform& gradientSpaceTra nsformation) 135 void Gradient::setGradientSpaceTransform(const AffineTransform& gradientSpaceTra nsformation)
141 { 136 {
142 if (m_gradientSpaceTransformation == gradientSpaceTransformation) 137 if (m_gradientSpaceTransformation == gradientSpaceTransformation)
143 return; 138 return;
144 139
145 m_gradientSpaceTransformation = gradientSpaceTransformation; 140 m_gradientSpaceTransformation = gradientSpaceTransformation;
146 if (m_gradient) 141 if (m_gradient)
147 m_gradient->setLocalMatrix(affineTransformToSkMatrix(m_gradientSpaceTran sformation)); 142 m_gradient->setLocalMatrix(affineTransformToSkMatrix(m_gradientSpaceTran sformation));
148 } 143 }
149 144
150 static inline U8CPU F2B(float x)
151 {
152 return static_cast<int>(x * 255);
153 }
154
155 static SkColor makeSkColor(float a, float r, float g, float b)
156 {
157 return SkColorSetARGB(F2B(a), F2B(r), F2B(g), F2B(b));
158 }
159
160 // Determine the total number of stops needed, including pseudo-stops at the 145 // Determine the total number of stops needed, including pseudo-stops at the
161 // ends as necessary. 146 // ends as necessary.
162 static size_t totalStopsNeeded(const Gradient::ColorStop* stopData, size_t count ) 147 static size_t totalStopsNeeded(const Gradient::ColorStop* stopData, size_t count )
163 { 148 {
164 // N.B.: The tests in this function should kept in sync with the ones in 149 // N.B.: The tests in this function should kept in sync with the ones in
165 // fillStops(), or badness happens. 150 // fillStops(), or badness happens.
166 const Gradient::ColorStop* stop = stopData; 151 const Gradient::ColorStop* stop = stopData;
167 size_t countUsed = count; 152 size_t countUsed = count;
168 if (count < 1 || stop->stop > 0.0) 153 if (count < 1 || stop->stop > 0.0)
169 countUsed++; 154 countUsed++;
170 stop += count - 1; 155 stop += count - 1;
171 if (count < 1 || stop->stop < 1.0) 156 if (count < 1 || stop->stop < 1.0)
172 countUsed++; 157 countUsed++;
173 return countUsed; 158 return countUsed;
174 } 159 }
175 160
176 // Collect sorted stop position and color information into the pos and colors 161 // Collect sorted stop position and color information into the pos and colors
177 // buffers, ensuring stops at both 0.0 and 1.0. The buffers must be large 162 // buffers, ensuring stops at both 0.0 and 1.0. The buffers must be large
178 // enough to hold information for all stops, including the new endpoints if 163 // enough to hold information for all stops, including the new endpoints if
179 // stops at 0.0 and 1.0 aren't already included. 164 // stops at 0.0 and 1.0 aren't already included.
180 static void fillStops(const Gradient::ColorStop* stopData, 165 static void fillStops(const Gradient::ColorStop* stopData,
181 size_t count, SkScalar* pos, SkColor* colors) 166 size_t count, SkScalar* pos, SkColor* colors)
182 { 167 {
183 const Gradient::ColorStop* stop = stopData; 168 const Gradient::ColorStop* stop = stopData;
184 size_t start = 0; 169 size_t start = 0;
185 if (count < 1) { 170 if (count < 1) {
186 // A gradient with no stops must be transparent black. 171 // A gradient with no stops must be transparent black.
187 pos[0] = WebCoreFloatToSkScalar(0.0); 172 pos[0] = WebCoreFloatToSkScalar(0.0);
188 colors[0] = makeSkColor(0.0, 0.0, 0.0, 0.0); 173 colors[0] = SK_ColorTRANSPARENT;
189 start = 1; 174 start = 1;
190 } else if (stop->stop > 0.0) { 175 } else if (stop->stop > 0.0) {
191 // Copy the first stop to 0.0. The first stop position may have a slight 176 // Copy the first stop to 0.0. The first stop position may have a slight
192 // rounding error, but we don't care in this float comparison, since 177 // rounding error, but we don't care in this float comparison, since
193 // 0.0 comes through cleanly and people aren't likely to want a gradient 178 // 0.0 comes through cleanly and people aren't likely to want a gradient
194 // with a stop at (0 + epsilon). 179 // with a stop at (0 + epsilon).
195 pos[0] = WebCoreFloatToSkScalar(0.0); 180 pos[0] = WebCoreFloatToSkScalar(0.0);
196 colors[0] = makeSkColor(stop->alpha, stop->red, stop->green, stop->blue) ; 181 colors[0] = stop->color.rgb();
f(malita) 2014/03/17 18:50:17 We should use SkColorSetARGB() to avoid any packin
197 start = 1; 182 start = 1;
198 } 183 }
199 184
200 for (size_t i = start; i < start + count; i++) { 185 for (size_t i = start; i < start + count; i++) {
201 pos[i] = WebCoreFloatToSkScalar(stop->stop); 186 pos[i] = WebCoreFloatToSkScalar(stop->stop);
202 colors[i] = makeSkColor(stop->alpha, stop->red, stop->green, stop->blue) ; 187 colors[i] = stop->color.rgb();
f(malita) 2014/03/17 18:50:17 Ditto.
203 ++stop; 188 ++stop;
204 } 189 }
205 190
206 // Copy the last stop to 1.0 if needed. See comment above about this float 191 // Copy the last stop to 1.0 if needed. See comment above about this float
207 // comparison. 192 // comparison.
208 if (count < 1 || (--stop)->stop < 1.0) { 193 if (count < 1 || (--stop)->stop < 1.0) {
209 pos[start + count] = WebCoreFloatToSkScalar(1.0); 194 pos[start + count] = WebCoreFloatToSkScalar(1.0);
210 colors[start + count] = colors[start + count - 1]; 195 colors[start + count] = colors[start + count - 1];
211 } 196 }
212 } 197 }
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after
273 if (!m_gradient) { 258 if (!m_gradient) {
274 // use last color, since our "geometry" was degenerate (e.g. radius==0) 259 // use last color, since our "geometry" was degenerate (e.g. radius==0)
275 m_gradient = adoptRef(new SkColorShader(colors[countUsed - 1])); 260 m_gradient = adoptRef(new SkColorShader(colors[countUsed - 1]));
276 } else { 261 } else {
277 m_gradient->setLocalMatrix(affineTransformToSkMatrix(m_gradientSpaceTran sformation)); 262 m_gradient->setLocalMatrix(affineTransformToSkMatrix(m_gradientSpaceTran sformation));
278 } 263 }
279 return m_gradient.get(); 264 return m_gradient.get();
280 } 265 }
281 266
282 } //namespace 267 } //namespace
OLDNEW
« no previous file with comments | « Source/platform/graphics/Gradient.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698