Skip to content

Commit 9b64889

Browse files
[Cairo] Create Cairo patterns from Gradient objects on-the-fly
https://bugs.webkit.org/show_bug.cgi?id=177947 Reviewed by Carlos Garcia Campos. Stop caching cairo_pattern_t objects for a specific global alpha value in the Gradient class. Instead, create these as required, for whatever alpha value. This drops some efficiency benefits in exchange for better Cairo operation isolation, while also matching the same approach that is used in the Pattern implementation for Cairo. Introduce the createPlatformGradient() method for Cairo, resuing the implementation of platformGradient() that's now removed. The Cairo-specific setPlatformGradientSpaceTransform() method is also removed since there's no cached cairo_pattern_t object that we can update. The Cairo-specific m_platformGradientAlpha member float is also deleted. Gradient::fill() and prepareCairoContextSource() are the only two places that need to create a cairo_pattern_t object off of Gradient, so they are updated accordingly. No new tests -- no changes in behavior. * platform/graphics/Gradient.cpp: (WebCore::Gradient::setGradientSpaceTransform): (WebCore::Gradient::setPlatformGradientSpaceTransform): Deleted. * platform/graphics/Gradient.h: * platform/graphics/cairo/GradientCairo.cpp: (WebCore::Gradient::platformDestroy): (WebCore::Gradient::createPlatformGradient): (WebCore::Gradient::fill): (WebCore::Gradient::platformGradient): Deleted. (WebCore::Gradient::setPlatformGradientSpaceTransform): Deleted. * platform/graphics/cairo/PlatformContextCairo.cpp: (WebCore::prepareCairoContextSource): git-svn-id: http://svn.webkit.org/repository/webkit/trunk@222975 268f45cc-cd09-0410-ab3c-d52691b4dbfc
1 parent 947d23c commit 9b64889

File tree

5 files changed

+61
-55
lines changed

5 files changed

+61
-55
lines changed

Source/WebCore/ChangeLog

+39
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,42 @@
1+
2017-10-06 Zan Dobersek <[email protected]>
2+
3+
[Cairo] Create Cairo patterns from Gradient objects on-the-fly
4+
https://bugs.webkit.org/show_bug.cgi?id=177947
5+
6+
Reviewed by Carlos Garcia Campos.
7+
8+
Stop caching cairo_pattern_t objects for a specific global alpha value
9+
in the Gradient class. Instead, create these as required, for whatever
10+
alpha value. This drops some efficiency benefits in exchange for better
11+
Cairo operation isolation, while also matching the same approach that
12+
is used in the Pattern implementation for Cairo.
13+
14+
Introduce the createPlatformGradient() method for Cairo, resuing the
15+
implementation of platformGradient() that's now removed. The
16+
Cairo-specific setPlatformGradientSpaceTransform() method is also
17+
removed since there's no cached cairo_pattern_t object that we can
18+
update. The Cairo-specific m_platformGradientAlpha member float is also
19+
deleted.
20+
21+
Gradient::fill() and prepareCairoContextSource() are the only two places
22+
that need to create a cairo_pattern_t object off of Gradient, so they
23+
are updated accordingly.
24+
25+
No new tests -- no changes in behavior.
26+
27+
* platform/graphics/Gradient.cpp:
28+
(WebCore::Gradient::setGradientSpaceTransform):
29+
(WebCore::Gradient::setPlatformGradientSpaceTransform): Deleted.
30+
* platform/graphics/Gradient.h:
31+
* platform/graphics/cairo/GradientCairo.cpp:
32+
(WebCore::Gradient::platformDestroy):
33+
(WebCore::Gradient::createPlatformGradient):
34+
(WebCore::Gradient::fill):
35+
(WebCore::Gradient::platformGradient): Deleted.
36+
(WebCore::Gradient::setPlatformGradientSpaceTransform): Deleted.
37+
* platform/graphics/cairo/PlatformContextCairo.cpp:
38+
(WebCore::prepareCairoContextSource):
39+
140
2017-10-06 Ms2ger <[email protected]>
241

342
Create bindings for WebGL2's versions of compressedTexImage2D.

Source/WebCore/platform/graphics/Gradient.cpp

-7
Original file line numberDiff line numberDiff line change
@@ -163,17 +163,10 @@ void Gradient::setGradientSpaceTransform(const AffineTransform& gradientSpaceTra
163163
return;
164164

165165
m_gradientSpaceTransformation = gradientSpaceTransformation;
166-
setPlatformGradientSpaceTransform(gradientSpaceTransformation);
167166

168167
invalidateHash();
169168
}
170169

171-
#if !USE(CAIRO)
172-
void Gradient::setPlatformGradientSpaceTransform(const AffineTransform&)
173-
{
174-
}
175-
#endif
176-
177170
unsigned Gradient::hash() const
178171
{
179172
if (m_cachedHash)

Source/WebCore/platform/graphics/Gradient.h

+1-8
Original file line numberDiff line numberDiff line change
@@ -159,8 +159,6 @@ namespace WebCore {
159159
void fill(GraphicsContext*, const FloatRect&);
160160
void adjustParametersForTiledDrawing(FloatSize&, FloatRect&, const FloatSize& spacing);
161161

162-
void setPlatformGradientSpaceTransform(const AffineTransform& gradientSpaceTransformation);
163-
164162
unsigned hash() const;
165163
void invalidateHash() { m_cachedHash = 0; }
166164

@@ -170,7 +168,7 @@ namespace WebCore {
170168
#elif USE(DIRECT2D)
171169
PlatformGradient createPlatformGradientIfNecessary(ID2D1RenderTarget*);
172170
#elif USE(CAIRO)
173-
PlatformGradient platformGradient(float globalAlpha);
171+
PlatformGradient createPlatformGradient(float globalAlpha);
174172
#endif
175173

176174
private:
@@ -201,11 +199,6 @@ namespace WebCore {
201199
mutable unsigned m_cachedHash;
202200

203201
PlatformGradient m_gradient;
204-
205-
#if USE(CAIRO)
206-
float m_platformGradientAlpha;
207-
#endif
208-
209202
};
210203

211204
} //namespace

Source/WebCore/platform/graphics/cairo/GradientCairo.cpp

+16-36
Original file line numberDiff line numberDiff line change
@@ -37,77 +37,57 @@ namespace WebCore {
3737

3838
void Gradient::platformDestroy()
3939
{
40-
if (m_gradient) {
41-
cairo_pattern_destroy(m_gradient);
42-
m_gradient = 0;
43-
}
44-
}
45-
46-
cairo_pattern_t* Gradient::platformGradient()
47-
{
48-
return platformGradient(1);
4940
}
5041

51-
cairo_pattern_t* Gradient::platformGradient(float globalAlpha)
42+
cairo_pattern_t* Gradient::createPlatformGradient(float globalAlpha)
5243
{
53-
if (m_gradient && m_platformGradientAlpha == globalAlpha)
54-
return m_gradient;
55-
56-
platformDestroy();
57-
m_platformGradientAlpha = globalAlpha;
58-
44+
cairo_pattern_t* gradient;
5945
if (m_radial)
60-
m_gradient = cairo_pattern_create_radial(m_p0.x(), m_p0.y(), m_r0, m_p1.x(), m_p1.y(), m_r1);
46+
gradient = cairo_pattern_create_radial(m_p0.x(), m_p0.y(), m_r0, m_p1.x(), m_p1.y(), m_r1);
6147
else
62-
m_gradient = cairo_pattern_create_linear(m_p0.x(), m_p0.y(), m_p1.x(), m_p1.y());
48+
gradient = cairo_pattern_create_linear(m_p0.x(), m_p0.y(), m_p1.x(), m_p1.y());
6349

6450
for (const auto& stop : m_stops) {
6551
if (stop.color.isExtended()) {
66-
cairo_pattern_add_color_stop_rgba(m_gradient, stop.offset, stop.color.asExtended().red(), stop.color.asExtended().green(), stop.color.asExtended().blue(),
52+
cairo_pattern_add_color_stop_rgba(gradient, stop.offset, stop.color.asExtended().red(), stop.color.asExtended().green(), stop.color.asExtended().blue(),
6753
stop.color.asExtended().alpha() * globalAlpha);
6854
} else {
6955
float r, g, b, a;
7056
stop.color.getRGBA(r, g, b, a);
71-
cairo_pattern_add_color_stop_rgba(m_gradient, stop.offset, r, g, b, a * globalAlpha);
57+
cairo_pattern_add_color_stop_rgba(gradient, stop.offset, r, g, b, a * globalAlpha);
7258
}
7359
}
7460

7561
switch (m_spreadMethod) {
7662
case SpreadMethodPad:
77-
cairo_pattern_set_extend(m_gradient, CAIRO_EXTEND_PAD);
63+
cairo_pattern_set_extend(gradient, CAIRO_EXTEND_PAD);
7864
break;
7965
case SpreadMethodReflect:
80-
cairo_pattern_set_extend(m_gradient, CAIRO_EXTEND_REFLECT);
66+
cairo_pattern_set_extend(gradient, CAIRO_EXTEND_REFLECT);
8167
break;
8268
case SpreadMethodRepeat:
83-
cairo_pattern_set_extend(m_gradient, CAIRO_EXTEND_REPEAT);
69+
cairo_pattern_set_extend(gradient, CAIRO_EXTEND_REPEAT);
8470
break;
8571
}
8672

8773
cairo_matrix_t matrix = toCairoMatrix(m_gradientSpaceTransformation);
8874
cairo_matrix_invert(&matrix);
89-
cairo_pattern_set_matrix(m_gradient, &matrix);
90-
91-
return m_gradient;
92-
}
75+
cairo_pattern_set_matrix(gradient, &matrix);
9376

94-
void Gradient::setPlatformGradientSpaceTransform(const AffineTransform& gradientSpaceTransformation)
95-
{
96-
if (m_gradient) {
97-
cairo_matrix_t matrix = toCairoMatrix(gradientSpaceTransformation);
98-
cairo_matrix_invert(&matrix);
99-
cairo_pattern_set_matrix(m_gradient, &matrix);
100-
}
77+
return gradient;
10178
}
10279

10380
void Gradient::fill(GraphicsContext* context, const FloatRect& rect)
10481
{
105-
cairo_t* cr = context->platformContext()->cr();
82+
RefPtr<cairo_pattern_t> gradient = adoptRef(createPlatformGradient(1.0));
10683

10784
context->save();
108-
cairo_set_source(cr, platformGradient());
85+
86+
cairo_t* cr = context->platformContext()->cr();
87+
cairo_set_source(cr, gradient.get());
10988
cairo_rectangle(cr, rect.x(), rect.y(), rect.width(), rect.height());
11089
cairo_fill(cr);
90+
11191
context->restore();
11292
}
11393

Source/WebCore/platform/graphics/cairo/PlatformContextCairo.cpp

+5-4
Original file line numberDiff line numberDiff line change
@@ -269,12 +269,13 @@ static inline void reduceSourceByAlpha(cairo_t* cr, float alpha)
269269
static void prepareCairoContextSource(cairo_t* cr, Pattern* pattern, Gradient* gradient, const Color& color, float globalAlpha)
270270
{
271271
if (pattern) {
272-
RefPtr<cairo_pattern_t> cairoPattern(adoptRef(pattern->createPlatformPattern(AffineTransform())));
272+
RefPtr<cairo_pattern_t> cairoPattern = adoptRef(pattern->createPlatformPattern(AffineTransform()));
273273
cairo_set_source(cr, cairoPattern.get());
274274
reduceSourceByAlpha(cr, globalAlpha);
275-
} else if (gradient)
276-
cairo_set_source(cr, gradient->platformGradient(globalAlpha));
277-
else { // Solid color source.
275+
} else if (gradient) {
276+
RefPtr<cairo_pattern_t> cairoPattern = adoptRef(gradient->createPlatformGradient(globalAlpha));
277+
cairo_set_source(cr, cairoPattern.get());
278+
} else { // Solid color source.
278279
if (globalAlpha < 1)
279280
setSourceRGBAFromColor(cr, colorWithOverrideAlpha(color.rgb(), color.alpha() / 255.f * globalAlpha));
280281
else

0 commit comments

Comments
 (0)