b9085c3379831e9f9d83577d6e0611ba3119d8c2
[WebKit-https.git] / Source / WebCore / platform / graphics / GraphicsContext.cpp
1 /*
2  * Copyright (C) 2003, 2004, 2005, 2006, 2009, 2013 Apple Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23  * THE POSSIBILITY OF SUCH DAMAGE.
24  */
25
26 #include "config.h"
27 #include "GraphicsContext.h"
28
29 #include "BidiResolver.h"
30 #include "BitmapImage.h"
31 #include "DisplayListRecorder.h"
32 #include "FloatRoundedRect.h"
33 #include "Gradient.h"
34 #include "ImageBuffer.h"
35 #include "IntRect.h"
36 #include "RoundedRect.h"
37 #include "TextRun.h"
38 #include "TextStream.h"
39
40 namespace WebCore {
41
42 class TextRunIterator {
43 public:
44     TextRunIterator()
45         : m_textRun(0)
46         , m_offset(0)
47     {
48     }
49
50     TextRunIterator(const TextRun* textRun, unsigned offset)
51         : m_textRun(textRun)
52         , m_offset(offset)
53     {
54     }
55
56     TextRunIterator(const TextRunIterator& other)
57         : m_textRun(other.m_textRun)
58         , m_offset(other.m_offset)
59     {
60     }
61
62     unsigned offset() const { return m_offset; }
63     void increment() { m_offset++; }
64     bool atEnd() const { return !m_textRun || m_offset >= m_textRun->length(); }
65     UChar current() const { return (*m_textRun)[m_offset]; }
66     UCharDirection direction() const { return atEnd() ? U_OTHER_NEUTRAL : u_charDirection(current()); }
67
68     bool operator==(const TextRunIterator& other)
69     {
70         return m_offset == other.m_offset && m_textRun == other.m_textRun;
71     }
72
73     bool operator!=(const TextRunIterator& other) { return !operator==(other); }
74
75 private:
76     const TextRun* m_textRun;
77     unsigned m_offset;
78 };
79
80 #define CHECK_FOR_CHANGED_PROPERTY(flag, property) \
81     if ((m_changeFlags & GraphicsContextState::flag) && (m_state.property != state.property)) \
82         changeFlags |= GraphicsContextState::flag;
83
84 GraphicsContextState::StateChangeFlags GraphicsContextStateChange::changesFromState(const GraphicsContextState& state) const
85 {
86     GraphicsContextState::StateChangeFlags changeFlags = GraphicsContextState::NoChange;
87
88     CHECK_FOR_CHANGED_PROPERTY(StrokeGradientChange, strokeGradient);
89     CHECK_FOR_CHANGED_PROPERTY(StrokePatternChange, strokePattern);
90     CHECK_FOR_CHANGED_PROPERTY(FillGradientChange, fillGradient);
91     CHECK_FOR_CHANGED_PROPERTY(FillPatternChange, fillPattern);
92
93     if ((m_changeFlags & GraphicsContextState::ShadowChange)
94         && (m_state.shadowOffset != state.shadowOffset
95             || m_state.shadowBlur != state.shadowBlur
96             || m_state.shadowColor != state.shadowColor))
97         changeFlags |= GraphicsContextState::ShadowChange;
98
99     CHECK_FOR_CHANGED_PROPERTY(StrokeThicknessChange, strokeThickness);
100     CHECK_FOR_CHANGED_PROPERTY(TextDrawingModeChange, textDrawingMode);
101     CHECK_FOR_CHANGED_PROPERTY(StrokeColorChange, strokeColor);
102     CHECK_FOR_CHANGED_PROPERTY(FillColorChange, fillColor);
103     CHECK_FOR_CHANGED_PROPERTY(StrokeStyleChange, strokeStyle);
104     CHECK_FOR_CHANGED_PROPERTY(FillRuleChange, fillRule);
105     CHECK_FOR_CHANGED_PROPERTY(AlphaChange, alpha);
106
107     if ((m_changeFlags & (GraphicsContextState::CompositeOperationChange | GraphicsContextState::BlendModeChange))
108         && (m_state.compositeOperator != state.compositeOperator || m_state.blendMode != state.blendMode))
109         changeFlags |= (GraphicsContextState::CompositeOperationChange | GraphicsContextState::BlendModeChange);
110
111     CHECK_FOR_CHANGED_PROPERTY(ShouldAntialiasChange, shouldAntialias);
112     CHECK_FOR_CHANGED_PROPERTY(ShouldSmoothFontsChange, shouldSmoothFonts);
113     CHECK_FOR_CHANGED_PROPERTY(ShouldSubpixelQuantizeFontsChange, shouldSubpixelQuantizeFonts);
114     CHECK_FOR_CHANGED_PROPERTY(ShadowsIgnoreTransformsChange, shadowsIgnoreTransforms);
115     CHECK_FOR_CHANGED_PROPERTY(DrawLuminanceMaskChange, drawLuminanceMask);
116     CHECK_FOR_CHANGED_PROPERTY(ImageInterpolationQualityChange, imageInterpolationQuality);
117
118     return changeFlags;
119 }
120
121 void GraphicsContextStateChange::accumulate(const GraphicsContextState& state, GraphicsContextState::StateChangeFlags flags)
122 {
123     // FIXME: This code should move to GraphicsContextState.
124     if (flags & GraphicsContextState::StrokeGradientChange)
125         m_state.strokeGradient = state.strokeGradient;
126
127     if (flags & GraphicsContextState::StrokePatternChange)
128         m_state.strokePattern = state.strokePattern;
129
130     if (flags & GraphicsContextState::FillGradientChange)
131         m_state.fillGradient = state.fillGradient;
132
133     if (flags & GraphicsContextState::FillPatternChange)
134         m_state.fillPattern = state.fillPattern;
135
136     if (flags & GraphicsContextState::ShadowChange) {
137         // FIXME: Deal with state.shadowsUseLegacyRadius.
138         m_state.shadowOffset = state.shadowOffset;
139         m_state.shadowBlur = state.shadowBlur;
140         m_state.shadowColor = state.shadowColor;
141     }
142
143     if (flags & GraphicsContextState::StrokeThicknessChange)
144         m_state.strokeThickness = state.strokeThickness;
145
146     if (flags & GraphicsContextState::TextDrawingModeChange)
147         m_state.textDrawingMode = state.textDrawingMode;
148
149     if (flags & GraphicsContextState::StrokeColorChange)
150         m_state.strokeColor = state.strokeColor;
151
152     if (flags & GraphicsContextState::FillColorChange)
153         m_state.fillColor = state.fillColor;
154
155     if (flags & GraphicsContextState::StrokeStyleChange)
156         m_state.strokeStyle = state.strokeStyle;
157
158     if (flags & GraphicsContextState::FillRuleChange)
159         m_state.fillRule = state.fillRule;
160
161     if (flags & GraphicsContextState::AlphaChange)
162         m_state.alpha = state.alpha;
163
164     if (flags & (GraphicsContextState::CompositeOperationChange | GraphicsContextState::BlendModeChange)) {
165         m_state.compositeOperator = state.compositeOperator;
166         m_state.blendMode = state.blendMode;
167     }
168
169     if (flags & GraphicsContextState::ShouldAntialiasChange)
170         m_state.shouldAntialias = state.shouldAntialias;
171
172     if (flags & GraphicsContextState::ShouldSmoothFontsChange)
173         m_state.shouldSmoothFonts = state.shouldSmoothFonts;
174
175     if (flags & GraphicsContextState::ShouldSubpixelQuantizeFontsChange)
176         m_state.shouldSubpixelQuantizeFonts = state.shouldSubpixelQuantizeFonts;
177
178     if (flags & GraphicsContextState::ShadowsIgnoreTransformsChange)
179         m_state.shadowsIgnoreTransforms = state.shadowsIgnoreTransforms;
180
181     if (flags & GraphicsContextState::DrawLuminanceMaskChange)
182         m_state.drawLuminanceMask = state.drawLuminanceMask;
183
184     if (flags & GraphicsContextState::ImageInterpolationQualityChange)
185         m_state.imageInterpolationQuality = state.imageInterpolationQuality;
186     
187     m_changeFlags |= flags;
188 }
189
190 void GraphicsContextStateChange::apply(GraphicsContext& context) const
191 {
192     if (m_changeFlags & GraphicsContextState::StrokeGradientChange)
193         context.setStrokeGradient(*m_state.strokeGradient);
194
195     if (m_changeFlags & GraphicsContextState::StrokePatternChange)
196         context.setStrokePattern(*m_state.strokePattern);
197
198     if (m_changeFlags & GraphicsContextState::FillGradientChange)
199         context.setFillGradient(*m_state.fillGradient);
200
201     if (m_changeFlags & GraphicsContextState::FillPatternChange)
202         context.setFillPattern(*m_state.fillPattern);
203
204     if (m_changeFlags & GraphicsContextState::ShadowChange) {
205 #if USE(CG)
206         if (m_state.shadowsUseLegacyRadius)
207             context.setLegacyShadow(m_state.shadowOffset, m_state.shadowBlur, m_state.shadowColor);
208         else
209 #endif
210             context.setShadow(m_state.shadowOffset, m_state.shadowBlur, m_state.shadowColor);
211     }
212
213     if (m_changeFlags & GraphicsContextState::StrokeThicknessChange)
214         context.setStrokeThickness(m_state.strokeThickness);
215
216     if (m_changeFlags & GraphicsContextState::TextDrawingModeChange)
217         context.setTextDrawingMode(m_state.textDrawingMode);
218
219     if (m_changeFlags & GraphicsContextState::StrokeColorChange)
220         context.setStrokeColor(m_state.strokeColor);
221
222     if (m_changeFlags & GraphicsContextState::FillColorChange)
223         context.setFillColor(m_state.fillColor);
224
225     if (m_changeFlags & GraphicsContextState::StrokeStyleChange)
226         context.setStrokeStyle(m_state.strokeStyle);
227
228     if (m_changeFlags & GraphicsContextState::FillRuleChange)
229         context.setFillRule(m_state.fillRule);
230
231     if (m_changeFlags & GraphicsContextState::AlphaChange)
232         context.setAlpha(m_state.alpha);
233
234     if (m_changeFlags & (GraphicsContextState::CompositeOperationChange | GraphicsContextState::BlendModeChange))
235         context.setCompositeOperation(m_state.compositeOperator, m_state.blendMode);
236
237     if (m_changeFlags & GraphicsContextState::ShouldAntialiasChange)
238         context.setShouldAntialias(m_state.shouldAntialias);
239
240     if (m_changeFlags & GraphicsContextState::ShouldSmoothFontsChange)
241         context.setShouldSmoothFonts(m_state.shouldSmoothFonts);
242
243     if (m_changeFlags & GraphicsContextState::ShouldSubpixelQuantizeFontsChange)
244         context.setShouldSubpixelQuantizeFonts(m_state.shouldSubpixelQuantizeFonts);
245
246     if (m_changeFlags & GraphicsContextState::ShadowsIgnoreTransformsChange)
247         context.setShadowsIgnoreTransforms(m_state.shadowsIgnoreTransforms);
248
249     if (m_changeFlags & GraphicsContextState::DrawLuminanceMaskChange)
250         context.setDrawLuminanceMask(m_state.drawLuminanceMask);
251
252     if (m_changeFlags & GraphicsContextState::ImageInterpolationQualityChange)
253         context.setImageInterpolationQuality(m_state.imageInterpolationQuality);
254 }
255
256 void GraphicsContextStateChange::dump(TextStream& ts) const
257 {
258     ts.dumpProperty("change-flags", m_changeFlags);
259
260     if (m_changeFlags & GraphicsContextState::StrokeGradientChange)
261         ts.dumpProperty("stroke-gradient", m_state.strokeGradient.get());
262
263     if (m_changeFlags & GraphicsContextState::StrokePatternChange)
264         ts.dumpProperty("stroke-pattern", m_state.strokePattern.get());
265
266     if (m_changeFlags & GraphicsContextState::FillGradientChange)
267         ts.dumpProperty("fill-gradient", m_state.fillGradient.get());
268
269     if (m_changeFlags & GraphicsContextState::FillPatternChange)
270         ts.dumpProperty("fill-pattern", m_state.fillPattern.get());
271
272     if (m_changeFlags & GraphicsContextState::ShadowChange) {
273         ts.dumpProperty("shadow-blur", m_state.shadowBlur);
274         ts.dumpProperty("shadow-offset", m_state.shadowOffset);
275 #if USE(CG)
276         ts.dumpProperty("shadows-use-legacy-radius", m_state.shadowsUseLegacyRadius);
277 #endif
278     }
279
280     if (m_changeFlags & GraphicsContextState::StrokeThicknessChange)
281         ts.dumpProperty("stroke-thickness", m_state.strokeThickness);
282
283     if (m_changeFlags & GraphicsContextState::TextDrawingModeChange)
284         ts.dumpProperty("text-drawing-mode", m_state.textDrawingMode);
285
286     if (m_changeFlags & GraphicsContextState::StrokeColorChange)
287         ts.dumpProperty("stroke-color", m_state.strokeColor);
288
289     if (m_changeFlags & GraphicsContextState::FillColorChange)
290         ts.dumpProperty("fill-color", m_state.fillColor);
291
292     if (m_changeFlags & GraphicsContextState::StrokeStyleChange)
293         ts.dumpProperty("stroke-style", m_state.strokeStyle);
294
295     if (m_changeFlags & GraphicsContextState::FillRuleChange)
296         ts.dumpProperty("fill-rule", m_state.fillRule);
297
298     if (m_changeFlags & GraphicsContextState::AlphaChange)
299         ts.dumpProperty("alpha", m_state.alpha);
300
301     if (m_changeFlags & GraphicsContextState::CompositeOperationChange)
302         ts.dumpProperty("composite-operator", m_state.compositeOperator);
303
304     if (m_changeFlags & GraphicsContextState::BlendModeChange)
305         ts.dumpProperty("blend-mode", m_state.blendMode);
306
307     if (m_changeFlags & GraphicsContextState::ShouldAntialiasChange)
308         ts.dumpProperty("should-antialias", m_state.shouldAntialias);
309
310     if (m_changeFlags & GraphicsContextState::ShouldSmoothFontsChange)
311         ts.dumpProperty("should-smooth-fonts", m_state.shouldSmoothFonts);
312
313     if (m_changeFlags & GraphicsContextState::ShouldSubpixelQuantizeFontsChange)
314         ts.dumpProperty("should-subpixel-quantize-fonts", m_state.shouldSubpixelQuantizeFonts);
315
316     if (m_changeFlags & GraphicsContextState::ShadowsIgnoreTransformsChange)
317         ts.dumpProperty("shadows-ignore-transforms", m_state.shadowsIgnoreTransforms);
318
319     if (m_changeFlags & GraphicsContextState::DrawLuminanceMaskChange)
320         ts.dumpProperty("draw-luminance-mask", m_state.drawLuminanceMask);
321 }
322
323 TextStream& operator<<(TextStream& ts, const GraphicsContextStateChange& stateChange)
324 {
325     stateChange.dump(ts);
326     return ts;
327 }
328
329 GraphicsContext::GraphicsContext(NonPaintingReasons nonPaintingReasons)
330     : m_nonPaintingReasons(nonPaintingReasons)
331 {
332 }
333
334 GraphicsContext::GraphicsContext(PlatformGraphicsContext* platformGraphicsContext)
335 {
336     platformInit(platformGraphicsContext);
337 }
338
339 GraphicsContext::~GraphicsContext()
340 {
341     ASSERT(m_stack.isEmpty());
342     ASSERT(!m_transparencyCount);
343     platformDestroy();
344 }
345
346 void GraphicsContext::save()
347 {
348     if (paintingDisabled())
349         return;
350
351     m_stack.append(m_state);
352
353     if (isRecording()) {
354         m_displayListRecorder->save();
355         return;
356     }
357
358     savePlatformState();
359 }
360
361 void GraphicsContext::restore()
362 {
363     if (paintingDisabled())
364         return;
365
366     if (m_stack.isEmpty()) {
367         LOG_ERROR("ERROR void GraphicsContext::restore() stack is empty");
368         return;
369     }
370     m_state = m_stack.last();
371     m_stack.removeLast();
372
373     // Make sure we deallocate the state stack buffer when it goes empty.
374     // Canvas elements will immediately save() again, but that goes into inline capacity.
375     if (m_stack.isEmpty())
376         m_stack.clear();
377
378     if (isRecording()) {
379         m_displayListRecorder->restore();
380         return;
381     }
382
383     restorePlatformState();
384 }
385
386 void GraphicsContext::drawRaisedEllipse(const FloatRect& rect, const Color& ellipseColor, const Color& shadowColor)
387 {
388     if (paintingDisabled())
389         return;
390
391     save();
392
393     setStrokeColor(shadowColor);
394     setFillColor(shadowColor);
395
396     drawEllipse(FloatRect(rect.x(), rect.y() + 1, rect.width(), rect.height()));
397
398     setStrokeColor(ellipseColor);
399     setFillColor(ellipseColor);
400
401     drawEllipse(rect);  
402
403     restore();
404 }
405
406 void GraphicsContext::setStrokeThickness(float thickness)
407 {
408     m_state.strokeThickness = thickness;
409     if (isRecording()) {
410         m_displayListRecorder->updateState(m_state, GraphicsContextState::StrokeThicknessChange);
411         return;
412     }
413
414     setPlatformStrokeThickness(thickness);
415 }
416
417 void GraphicsContext::setStrokeStyle(StrokeStyle style)
418 {
419     m_state.strokeStyle = style;
420     if (isRecording()) {
421         m_displayListRecorder->updateState(m_state, GraphicsContextState::StrokeStyleChange);
422         return;
423     }
424     setPlatformStrokeStyle(style);
425 }
426
427 void GraphicsContext::setStrokeColor(const Color& color)
428 {
429     m_state.strokeColor = color;
430     m_state.strokeGradient = nullptr;
431     m_state.strokePattern = nullptr;
432     if (isRecording()) {
433         m_displayListRecorder->updateState(m_state, GraphicsContextState::StrokeColorChange);
434         return;
435     }
436     setPlatformStrokeColor(color);
437 }
438
439 void GraphicsContext::setShadow(const FloatSize& offset, float blur, const Color& color)
440 {
441     m_state.shadowOffset = offset;
442     m_state.shadowBlur = blur;
443     m_state.shadowColor = color;
444 #if USE(CG)
445     m_state.shadowsUseLegacyRadius = false;
446 #endif
447     if (isRecording()) {
448         m_displayListRecorder->updateState(m_state, GraphicsContextState::ShadowChange);
449         return;
450     }
451     setPlatformShadow(offset, blur, color);
452 }
453
454 void GraphicsContext::setLegacyShadow(const FloatSize& offset, float blur, const Color& color)
455 {
456     m_state.shadowOffset = offset;
457     m_state.shadowBlur = blur;
458     m_state.shadowColor = color;
459 #if USE(CG)
460     m_state.shadowsUseLegacyRadius = true;
461 #endif
462     if (isRecording()) {
463         m_displayListRecorder->updateState(m_state, GraphicsContextState::ShadowChange);
464         return;
465     }
466     setPlatformShadow(offset, blur, color);
467 }
468
469 void GraphicsContext::clearShadow()
470 {
471     m_state.shadowOffset = FloatSize();
472     m_state.shadowBlur = 0;
473     m_state.shadowColor = Color();
474 #if USE(CG)
475     m_state.shadowsUseLegacyRadius = false;
476 #endif
477
478     if (isRecording()) {
479         m_displayListRecorder->clearShadow();
480         return;
481     }
482     clearPlatformShadow();
483 }
484
485 bool GraphicsContext::getShadow(FloatSize& offset, float& blur, Color& color) const
486 {
487     offset = m_state.shadowOffset;
488     blur = m_state.shadowBlur;
489     color = m_state.shadowColor;
490
491     return hasShadow();
492 }
493
494 #if USE(CAIRO)
495 bool GraphicsContext::mustUseShadowBlur() const
496 {
497     // We can't avoid ShadowBlur if the shadow has blur.
498     if (hasBlurredShadow())
499         return true;
500     // We can avoid ShadowBlur and optimize, since we're not drawing on a
501     // canvas and box shadows are affected by the transformation matrix.
502     if (!m_state.shadowsIgnoreTransforms)
503         return false;
504     // We can avoid ShadowBlur, since there are no transformations to apply to the canvas.
505     if (getCTM().isIdentity())
506         return false;
507     // Otherwise, no chance avoiding ShadowBlur.
508     return true;
509 }
510 #endif
511
512 void GraphicsContext::setFillColor(const Color& color)
513 {
514     m_state.fillColor = color;
515     m_state.fillGradient = nullptr;
516     m_state.fillPattern = nullptr;
517
518     if (isRecording()) {
519         m_displayListRecorder->updateState(m_state, GraphicsContextState::FillColorChange);
520         return;
521     }
522
523     setPlatformFillColor(color);
524 }
525
526 void GraphicsContext::setShadowsIgnoreTransforms(bool shadowsIgnoreTransforms)
527 {
528     m_state.shadowsIgnoreTransforms = shadowsIgnoreTransforms;
529     if (isRecording())
530         m_displayListRecorder->updateState(m_state, GraphicsContextState::ShadowsIgnoreTransformsChange);
531 }
532
533 void GraphicsContext::setShouldAntialias(bool shouldAntialias)
534 {
535     m_state.shouldAntialias = shouldAntialias;
536
537     if (isRecording()) {
538         m_displayListRecorder->updateState(m_state, GraphicsContextState::ShouldAntialiasChange);
539         return;
540     }
541
542     setPlatformShouldAntialias(shouldAntialias);
543 }
544
545 void GraphicsContext::setShouldSmoothFonts(bool shouldSmoothFonts)
546 {
547     m_state.shouldSmoothFonts = shouldSmoothFonts;
548     
549     if (isRecording()) {
550         m_displayListRecorder->updateState(m_state, GraphicsContextState::ShouldSmoothFontsChange);
551         return;
552     }
553     
554     setPlatformShouldSmoothFonts(shouldSmoothFonts);
555 }
556
557 void GraphicsContext::setShouldSubpixelQuantizeFonts(bool shouldSubpixelQuantizeFonts)
558 {
559     m_state.shouldSubpixelQuantizeFonts = shouldSubpixelQuantizeFonts;
560     if (isRecording())
561         m_displayListRecorder->updateState(m_state, GraphicsContextState::ShouldSubpixelQuantizeFontsChange);
562 }
563
564 void GraphicsContext::setImageInterpolationQuality(InterpolationQuality imageInterpolationQuality)
565 {
566     m_state.imageInterpolationQuality = imageInterpolationQuality;
567
568     if (paintingDisabled())
569         return;
570
571     if (isRecording()) {
572         m_displayListRecorder->updateState(m_state, GraphicsContextState::ImageInterpolationQualityChange);
573         return;
574     }
575
576     setPlatformImageInterpolationQuality(imageInterpolationQuality);
577 }
578
579 void GraphicsContext::setStrokePattern(Ref<Pattern>&& pattern)
580 {
581     m_state.strokeGradient = nullptr;
582     m_state.strokePattern = WTFMove(pattern);
583     if (isRecording())
584         m_displayListRecorder->updateState(m_state, GraphicsContextState::StrokePatternChange);
585 }
586
587 void GraphicsContext::setFillPattern(Ref<Pattern>&& pattern)
588 {
589     m_state.fillGradient = nullptr;
590     m_state.fillPattern = WTFMove(pattern);
591     if (isRecording())
592         m_displayListRecorder->updateState(m_state, GraphicsContextState::FillPatternChange);
593 }
594
595 void GraphicsContext::setStrokeGradient(Ref<Gradient>&& gradient)
596 {
597     m_state.strokeGradient = WTFMove(gradient);
598     m_state.strokePattern = nullptr;
599     if (isRecording())
600         m_displayListRecorder->updateState(m_state, GraphicsContextState::StrokeGradientChange);
601 }
602
603 void GraphicsContext::setFillRule(WindRule fillRule)
604 {
605     m_state.fillRule = fillRule;
606     if (isRecording())
607         m_displayListRecorder->updateState(m_state, GraphicsContextState::FillRuleChange);
608 }
609
610 void GraphicsContext::setFillGradient(Ref<Gradient>&& gradient)
611 {
612     m_state.fillGradient = WTFMove(gradient);
613     m_state.fillPattern = nullptr;
614     if (isRecording())
615         m_displayListRecorder->updateState(m_state, GraphicsContextState::FillGradientChange); // FIXME: also fill pattern?
616 }
617
618 void GraphicsContext::beginTransparencyLayer(float opacity)
619 {
620     if (isRecording()) {
621         m_displayListRecorder->beginTransparencyLayer(opacity);
622         return;
623     }
624     beginPlatformTransparencyLayer(opacity);
625     ++m_transparencyCount;
626 }
627
628 void GraphicsContext::endTransparencyLayer()
629 {
630     if (isRecording()) {
631         m_displayListRecorder->endTransparencyLayer();
632         return;
633     }
634     endPlatformTransparencyLayer();
635     ASSERT(m_transparencyCount > 0);
636     --m_transparencyCount;
637 }
638
639 float GraphicsContext::drawText(const FontCascade& font, const TextRun& run, const FloatPoint& point, int from, int to)
640 {
641     if (paintingDisabled())
642         return 0;
643
644     // Display list recording for text content is done at glyphs level. See GraphicsContext::drawGlyphs.
645     return font.drawText(*this, run, point, from, to);
646 }
647
648 void GraphicsContext::drawGlyphs(const FontCascade& fontCascade, const Font& font, const GlyphBuffer& buffer, int from, int numGlyphs, const FloatPoint& point)
649 {
650     if (paintingDisabled())
651         return;
652
653     if (isRecording()) {
654         m_displayListRecorder->drawGlyphs(font, buffer, from, numGlyphs, point, fontCascade.fontDescription().fontSmoothing());
655         return;
656     }
657
658     fontCascade.drawGlyphs(*this, font, buffer, from, numGlyphs, point, fontCascade.fontDescription().fontSmoothing());
659 }
660
661 void GraphicsContext::drawEmphasisMarks(const FontCascade& font, const TextRun& run, const AtomicString& mark, const FloatPoint& point, int from, int to)
662 {
663     if (paintingDisabled())
664         return;
665
666     font.drawEmphasisMarks(*this, run, mark, point, from, to);
667 }
668
669 void GraphicsContext::drawBidiText(const FontCascade& font, const TextRun& run, const FloatPoint& point, FontCascade::CustomFontNotReadyAction customFontNotReadyAction)
670 {
671     if (paintingDisabled())
672         return;
673
674     BidiResolver<TextRunIterator, BidiCharacterRun> bidiResolver;
675     bidiResolver.setStatus(BidiStatus(run.direction(), run.directionalOverride()));
676     bidiResolver.setPositionIgnoringNestedIsolates(TextRunIterator(&run, 0));
677
678     // FIXME: This ownership should be reversed. We should pass BidiRunList
679     // to BidiResolver in createBidiRunsForLine.
680     BidiRunList<BidiCharacterRun>& bidiRuns = bidiResolver.runs();
681     bidiResolver.createBidiRunsForLine(TextRunIterator(&run, run.length()));
682
683     if (!bidiRuns.runCount())
684         return;
685
686     FloatPoint currPoint = point;
687     BidiCharacterRun* bidiRun = bidiRuns.firstRun();
688     while (bidiRun) {
689         TextRun subrun = run.subRun(bidiRun->start(), bidiRun->stop() - bidiRun->start());
690         bool isRTL = bidiRun->level() % 2;
691         subrun.setDirection(isRTL ? RTL : LTR);
692         subrun.setDirectionalOverride(bidiRun->dirOverride(false));
693
694         float width = font.drawText(*this, subrun, currPoint, 0, -1, customFontNotReadyAction);
695         currPoint.move(width, 0);
696
697         bidiRun = bidiRun->next();
698     }
699
700     bidiRuns.clear();
701 }
702
703 void GraphicsContext::drawImage(Image& image, const FloatPoint& destination, const ImagePaintingOptions& imagePaintingOptions)
704 {
705     drawImage(image, FloatRect(destination, image.size()), FloatRect(FloatPoint(), image.size()), imagePaintingOptions);
706 }
707
708 void GraphicsContext::drawImage(Image& image, const FloatRect& destination, const ImagePaintingOptions& imagePaintingOptions)
709 {
710 #if PLATFORM(IOS)
711     FloatRect srcRect(FloatPoint(), image.originalSize());
712 #else
713     FloatRect srcRect(FloatPoint(), image.size());
714 #endif
715         
716     drawImage(image, destination, srcRect, imagePaintingOptions);
717 }
718
719 void GraphicsContext::drawImage(Image& image, const FloatRect& destination, const FloatRect& source, const ImagePaintingOptions& imagePaintingOptions)
720 {
721     if (paintingDisabled())
722         return;
723
724     if (isRecording()) {
725         m_displayListRecorder->drawImage(image, destination, source, imagePaintingOptions);
726         return;
727     }
728
729     InterpolationQualityMaintainer interpolationQualityForThisScope(*this, imagePaintingOptions.m_interpolationQuality);
730     image.draw(*this, destination, source, imagePaintingOptions.m_compositeOperator, imagePaintingOptions.m_blendMode, imagePaintingOptions.m_orientationDescription);
731 }
732
733 void GraphicsContext::drawTiledImage(Image& image, const FloatRect& destination, const FloatPoint& source, const FloatSize& tileSize, const FloatSize& spacing, const ImagePaintingOptions& imagePaintingOptions)
734 {
735     if (paintingDisabled())
736         return;
737
738     if (isRecording()) {
739         m_displayListRecorder->drawTiledImage(image, destination, source, tileSize, spacing, imagePaintingOptions);
740         return;
741     }
742
743     InterpolationQualityMaintainer interpolationQualityForThisScope(*this, imagePaintingOptions.m_interpolationQuality);
744     image.drawTiled(*this, destination, source, tileSize, spacing, imagePaintingOptions.m_compositeOperator, imagePaintingOptions.m_blendMode);
745 }
746
747 void GraphicsContext::drawTiledImage(Image& image, const FloatRect& destination, const FloatRect& source, const FloatSize& tileScaleFactor,
748     Image::TileRule hRule, Image::TileRule vRule, const ImagePaintingOptions& imagePaintingOptions)
749 {
750     if (paintingDisabled())
751         return;
752
753     if (isRecording()) {
754         m_displayListRecorder->drawTiledImage(image, destination, source, tileScaleFactor, hRule, vRule, imagePaintingOptions);
755         return;
756     }
757
758     if (hRule == Image::StretchTile && vRule == Image::StretchTile) {
759         // Just do a scale.
760         drawImage(image, destination, source, imagePaintingOptions);
761         return;
762     }
763
764     InterpolationQualityMaintainer interpolationQualityForThisScope(*this, imagePaintingOptions.m_interpolationQuality);
765     image.drawTiled(*this, destination, source, tileScaleFactor, hRule, vRule, imagePaintingOptions.m_compositeOperator);
766 }
767
768 void GraphicsContext::drawImageBuffer(ImageBuffer& image, const FloatPoint& destination, const ImagePaintingOptions& imagePaintingOptions)
769 {
770     drawImageBuffer(image, FloatRect(destination, image.logicalSize()), FloatRect(FloatPoint(), image.logicalSize()), imagePaintingOptions);
771 }
772
773 void GraphicsContext::drawImageBuffer(ImageBuffer& image, const FloatRect& destination, const ImagePaintingOptions& imagePaintingOptions)
774 {
775     drawImageBuffer(image, destination, FloatRect(FloatPoint(), FloatSize(image.logicalSize())), imagePaintingOptions);
776 }
777
778 void GraphicsContext::drawImageBuffer(ImageBuffer& image, const FloatRect& destination, const FloatRect& source, const ImagePaintingOptions& imagePaintingOptions)
779 {
780     if (paintingDisabled())
781         return;
782
783     InterpolationQualityMaintainer interpolationQualityForThisScope(*this, imagePaintingOptions.m_interpolationQuality);
784     image.draw(*this, destination, source, imagePaintingOptions.m_compositeOperator, imagePaintingOptions.m_blendMode);
785 }
786
787 void GraphicsContext::drawConsumingImageBuffer(std::unique_ptr<ImageBuffer> image, const FloatPoint& destination, const ImagePaintingOptions& imagePaintingOptions)
788 {
789     if (!image)
790         return;
791     IntSize imageLogicalSize = image->logicalSize();
792     drawConsumingImageBuffer(WTFMove(image), FloatRect(destination, imageLogicalSize), FloatRect(FloatPoint(), imageLogicalSize), imagePaintingOptions);
793 }
794
795 void GraphicsContext::drawConsumingImageBuffer(std::unique_ptr<ImageBuffer> image, const FloatRect& destination, const ImagePaintingOptions& imagePaintingOptions)
796 {
797     if (!image)
798         return;
799     IntSize imageLogicalSize = image->logicalSize();
800     drawConsumingImageBuffer(WTFMove(image), destination, FloatRect(FloatPoint(), FloatSize(imageLogicalSize)), imagePaintingOptions);
801 }
802
803 void GraphicsContext::drawConsumingImageBuffer(std::unique_ptr<ImageBuffer> image, const FloatRect& destination, const FloatRect& source, const ImagePaintingOptions& imagePaintingOptions)
804 {
805     if (paintingDisabled() || !image)
806         return;
807     
808     InterpolationQualityMaintainer interpolationQualityForThisScope(*this, imagePaintingOptions.m_interpolationQuality);
809     ImageBuffer::drawConsuming(WTFMove(image), *this, destination, source, imagePaintingOptions.m_compositeOperator, imagePaintingOptions.m_blendMode);
810 }
811
812 void GraphicsContext::clipRoundedRect(const FloatRoundedRect& rect)
813 {
814     if (paintingDisabled())
815         return;
816
817     Path path;
818     path.addRoundedRect(rect);
819     clipPath(path);
820 }
821
822 void GraphicsContext::clipOutRoundedRect(const FloatRoundedRect& rect)
823 {
824     if (paintingDisabled())
825         return;
826
827     if (!rect.isRounded()) {
828         clipOut(rect.rect());
829         return;
830     }
831
832     Path path;
833     path.addRoundedRect(rect);
834     clipOut(path);
835 }
836
837 #if !USE(CG) && !USE(CAIRO)
838 IntRect GraphicsContext::clipBounds() const
839 {
840     ASSERT_NOT_REACHED();
841     return IntRect();
842 }
843 #endif
844
845 void GraphicsContext::setTextDrawingMode(TextDrawingModeFlags mode)
846 {
847     m_state.textDrawingMode = mode;
848     if (paintingDisabled())
849         return;
850
851     if (isRecording()) {
852         m_displayListRecorder->updateState(m_state, GraphicsContextState::TextDrawingModeChange);
853         return;
854     }
855     setPlatformTextDrawingMode(mode);
856 }
857
858 void GraphicsContext::fillRect(const FloatRect& rect, Gradient& gradient)
859 {
860     if (paintingDisabled())
861         return;
862
863     if (isRecording()) {
864         m_displayListRecorder->fillRect(rect, gradient);
865         return;
866     }
867
868     gradient.fill(this, rect);
869 }
870
871 void GraphicsContext::fillRect(const FloatRect& rect, const Color& color, CompositeOperator op, BlendMode blendMode)
872 {
873     if (paintingDisabled())
874         return;
875
876     if (isRecording()) {
877         m_displayListRecorder->fillRect(rect, color, op, blendMode);
878         return;
879     }
880
881     CompositeOperator previousOperator = compositeOperation();
882     setCompositeOperation(op, blendMode);
883     fillRect(rect, color);
884     setCompositeOperation(previousOperator);
885 }
886
887 void GraphicsContext::fillRoundedRect(const FloatRoundedRect& rect, const Color& color, BlendMode blendMode)
888 {
889     if (paintingDisabled())
890         return;
891
892     if (isRecording()) {
893         m_displayListRecorder->fillRoundedRect(rect, color, blendMode);
894         return;
895     }
896
897     if (rect.isRounded()) {
898         setCompositeOperation(compositeOperation(), blendMode);
899         platformFillRoundedRect(rect, color);
900         setCompositeOperation(compositeOperation());
901     } else
902         fillRect(rect.rect(), color, compositeOperation(), blendMode);
903 }
904
905 #if !USE(CG) && !USE(CAIRO)
906 void GraphicsContext::fillRectWithRoundedHole(const IntRect& rect, const FloatRoundedRect& roundedHoleRect, const Color& color)
907 {
908     if (paintingDisabled())
909         return;
910
911     Path path;
912     path.addRect(rect);
913
914     if (!roundedHoleRect.radii().isZero())
915         path.addRoundedRect(roundedHoleRect);
916     else
917         path.addRect(roundedHoleRect.rect());
918
919     WindRule oldFillRule = fillRule();
920     Color oldFillColor = fillColor();
921     
922     setFillRule(RULE_EVENODD);
923     setFillColor(color);
924
925     fillPath(path);
926     
927     setFillRule(oldFillRule);
928     setFillColor(oldFillColor);
929 }
930 #endif
931
932 void GraphicsContext::setAlpha(float alpha)
933 {
934     m_state.alpha = alpha;
935     if (isRecording()) {
936         m_displayListRecorder->updateState(m_state, GraphicsContextState::AlphaChange);
937         return;
938     }
939     setPlatformAlpha(alpha);
940 }
941
942 void GraphicsContext::setCompositeOperation(CompositeOperator compositeOperation, BlendMode blendMode)
943 {
944     m_state.compositeOperator = compositeOperation;
945     m_state.blendMode = blendMode;
946     if (isRecording()) {
947         m_displayListRecorder->updateState(m_state, GraphicsContextState::CompositeOperationChange);
948         return;
949     }
950     setPlatformCompositeOperation(compositeOperation, blendMode);
951 }
952
953 void GraphicsContext::setDrawLuminanceMask(bool drawLuminanceMask)
954 {
955     m_state.drawLuminanceMask = drawLuminanceMask;
956     if (isRecording())
957         m_displayListRecorder->updateState(m_state, GraphicsContextState::DrawLuminanceMaskChange);
958 }
959
960 #if !USE(CG)
961 // Implement this if you want to go push the drawing mode into your native context immediately.
962 void GraphicsContext::setPlatformTextDrawingMode(TextDrawingModeFlags)
963 {
964 }
965 #endif
966
967 #if !USE(CAIRO)
968 void GraphicsContext::setPlatformStrokeStyle(StrokeStyle)
969 {
970 }
971 #endif
972
973 #if !USE(CG)
974 void GraphicsContext::setPlatformShouldSmoothFonts(bool)
975 {
976 }
977 #endif
978
979 #if !USE(CG) && !USE(CAIRO)
980 bool GraphicsContext::isAcceleratedContext() const
981 {
982     return false;
983 }
984 #endif
985
986 void GraphicsContext::adjustLineToPixelBoundaries(FloatPoint& p1, FloatPoint& p2, float strokeWidth, StrokeStyle penStyle)
987 {
988     // For odd widths, we add in 0.5 to the appropriate x/y so that the float arithmetic
989     // works out.  For example, with a border width of 3, WebKit will pass us (y1+y2)/2, e.g.,
990     // (50+53)/2 = 103/2 = 51 when we want 51.5.  It is always true that an even width gave
991     // us a perfect position, but an odd width gave us a position that is off by exactly 0.5.
992     if (penStyle == DottedStroke || penStyle == DashedStroke) {
993         if (p1.x() == p2.x()) {
994             p1.setY(p1.y() + strokeWidth);
995             p2.setY(p2.y() - strokeWidth);
996         } else {
997             p1.setX(p1.x() + strokeWidth);
998             p2.setX(p2.x() - strokeWidth);
999         }
1000     }
1001
1002     if (static_cast<int>(strokeWidth) % 2) { //odd
1003         if (p1.x() == p2.x()) {
1004             // We're a vertical line.  Adjust our x.
1005             p1.setX(p1.x() + 0.5f);
1006             p2.setX(p2.x() + 0.5f);
1007         } else {
1008             // We're a horizontal line. Adjust our y.
1009             p1.setY(p1.y() + 0.5f);
1010             p2.setY(p2.y() + 0.5f);
1011         }
1012     }
1013 }
1014
1015 static bool scalesMatch(AffineTransform a, AffineTransform b)
1016 {
1017     return a.xScale() == b.xScale() && a.yScale() == b.yScale();
1018 }
1019
1020 std::unique_ptr<ImageBuffer> GraphicsContext::createCompatibleBuffer(const FloatSize& size, bool hasAlpha) const
1021 {
1022     // Make the buffer larger if the context's transform is scaling it so we need a higher
1023     // resolution than one pixel per unit. Also set up a corresponding scale factor on the
1024     // graphics context.
1025
1026     AffineTransform transform = getCTM(DefinitelyIncludeDeviceScale);
1027     FloatSize scaledSize(static_cast<int>(ceil(size.width() * transform.xScale())), static_cast<int>(ceil(size.height() * transform.yScale())));
1028
1029     std::unique_ptr<ImageBuffer> buffer = ImageBuffer::createCompatibleBuffer(scaledSize, 1, ColorSpaceSRGB, *this, hasAlpha);
1030     if (!buffer)
1031         return nullptr;
1032
1033     buffer->context().scale(FloatSize(scaledSize.width() / size.width(), scaledSize.height() / size.height()));
1034
1035     return buffer;
1036 }
1037
1038 bool GraphicsContext::isCompatibleWithBuffer(ImageBuffer& buffer) const
1039 {
1040     GraphicsContext& bufferContext = buffer.context();
1041
1042     return scalesMatch(getCTM(), bufferContext.getCTM()) && isAcceleratedContext() == bufferContext.isAcceleratedContext();
1043 }
1044
1045 #if !USE(CG)
1046 void GraphicsContext::platformApplyDeviceScaleFactor(float)
1047 {
1048 }
1049 #endif
1050
1051 void GraphicsContext::applyDeviceScaleFactor(float deviceScaleFactor)
1052 {
1053     scale(FloatSize(deviceScaleFactor, deviceScaleFactor));
1054
1055     if (isRecording()) {
1056         m_displayListRecorder->applyDeviceScaleFactor(deviceScaleFactor);
1057         return;
1058     }
1059
1060     platformApplyDeviceScaleFactor(deviceScaleFactor);
1061 }
1062
1063 void GraphicsContext::fillEllipse(const FloatRect& ellipse)
1064 {
1065     platformFillEllipse(ellipse);
1066 }
1067
1068 void GraphicsContext::strokeEllipse(const FloatRect& ellipse)
1069 {
1070     platformStrokeEllipse(ellipse);
1071 }
1072
1073 void GraphicsContext::fillEllipseAsPath(const FloatRect& ellipse)
1074 {
1075     Path path;
1076     path.addEllipse(ellipse);
1077     fillPath(path);
1078 }
1079
1080 void GraphicsContext::strokeEllipseAsPath(const FloatRect& ellipse)
1081 {
1082     Path path;
1083     path.addEllipse(ellipse);
1084     strokePath(path);
1085 }
1086
1087 #if !USE(CG)
1088 void GraphicsContext::platformFillEllipse(const FloatRect& ellipse)
1089 {
1090     if (paintingDisabled())
1091         return;
1092
1093     fillEllipseAsPath(ellipse);
1094 }
1095
1096 void GraphicsContext::platformStrokeEllipse(const FloatRect& ellipse)
1097 {
1098     if (paintingDisabled())
1099         return;
1100
1101     strokeEllipseAsPath(ellipse);
1102 }
1103 #endif
1104
1105 FloatRect GraphicsContext::computeUnderlineBoundsForText(const FloatPoint& point, float width, bool printing)
1106 {
1107     Color dummyColor;
1108     return computeLineBoundsAndAntialiasingModeForText(point, width, printing, dummyColor);
1109 }
1110
1111 FloatRect GraphicsContext::computeLineBoundsAndAntialiasingModeForText(const FloatPoint& point, float width, bool printing, Color& color)
1112 {
1113     FloatPoint origin = point;
1114     float thickness = std::max(strokeThickness(), 0.5f);
1115     if (printing)
1116         return FloatRect(origin, FloatSize(width, thickness));
1117
1118     AffineTransform transform = getCTM(GraphicsContext::DefinitelyIncludeDeviceScale);
1119     // Just compute scale in x dimension, assuming x and y scales are equal.
1120     float scale = transform.b() ? sqrtf(transform.a() * transform.a() + transform.b() * transform.b()) : transform.a();
1121     if (scale < 1.0) {
1122         // This code always draws a line that is at least one-pixel line high,
1123         // which tends to visually overwhelm text at small scales. To counter this
1124         // effect, an alpha is applied to the underline color when text is at small scales.
1125         static const float minimumUnderlineAlpha = 0.4f;
1126         float shade = scale > minimumUnderlineAlpha ? scale : minimumUnderlineAlpha;
1127         int alpha = color.alpha() * shade;
1128         color = Color(color.red(), color.green(), color.blue(), alpha);
1129     }
1130
1131     FloatPoint devicePoint = transform.mapPoint(point);
1132     // Visual overflow might occur here due to integral roundf/ceilf. visualOverflowForDecorations adjusts the overflow value for underline decoration.
1133     FloatPoint deviceOrigin = FloatPoint(roundf(devicePoint.x()), ceilf(devicePoint.y()));
1134     if (auto inverse = transform.inverse())
1135         origin = inverse.value().mapPoint(deviceOrigin);
1136     return FloatRect(origin, FloatSize(width, thickness));
1137 }
1138
1139 void GraphicsContext::applyState(const GraphicsContextState& state)
1140 {
1141     setPlatformShadow(state.shadowOffset, state.shadowBlur, state.shadowColor);
1142     setPlatformStrokeThickness(state.strokeThickness);
1143     setPlatformTextDrawingMode(state.textDrawingMode);
1144     setPlatformStrokeColor(state.strokeColor);
1145     setPlatformFillColor(state.fillColor);
1146     setPlatformStrokeStyle(state.strokeStyle);
1147     setPlatformAlpha(state.alpha);
1148     setPlatformCompositeOperation(state.compositeOperator, state.blendMode);
1149     setPlatformShouldAntialias(state.shouldAntialias);
1150     setPlatformShouldSmoothFonts(state.shouldSmoothFonts);
1151 }
1152
1153 }