1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
|
/*
* Copyright (c) 2008, Google Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following disclaimer
* in the documentation and/or other materials provided with the
* distribution.
* * Neither the name of Google Inc. nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "config.h"
#include "PlatformContextSkia.h"
#include "AffineTransform.h"
#include "CanvasLayerChromium.h"
#include "GraphicsContext.h"
#include "ImageBuffer.h"
#include "NativeImageSkia.h"
#include "PlatformContextSkia.h"
#include "SkiaUtils.h"
#include "TilingData.h"
#include "skia/ext/image_operations.h"
#include "skia/ext/platform_canvas.h"
#include "SkBitmap.h"
#include "SkColorPriv.h"
#include "SkShader.h"
#include "SkDashPathEffect.h"
#if USE(GLES2_RENDERING)
#include "GraphicsContext3D.h"
#include "GLES2Canvas.h"
#include "GLES2Texture.h"
#endif
#include <wtf/MathExtras.h>
#include <wtf/OwnArrayPtr.h>
#include <wtf/Vector.h>
namespace WebCore {
extern bool isPathSkiaSafe(const SkMatrix& transform, const SkPath& path);
// State -----------------------------------------------------------------------
// Encapsulates the additional painting state information we store for each
// pushed graphics state.
struct PlatformContextSkia::State {
State();
State(const State&);
~State();
// Common shader state.
float m_alpha;
SkXfermode::Mode m_xferMode;
bool m_useAntialiasing;
SkDrawLooper* m_looper;
// Fill.
SkColor m_fillColor;
SkShader* m_fillShader;
// Stroke.
StrokeStyle m_strokeStyle;
SkColor m_strokeColor;
SkShader* m_strokeShader;
float m_strokeThickness;
int m_dashRatio; // Ratio of the length of a dash to its width.
float m_miterLimit;
SkPaint::Cap m_lineCap;
SkPaint::Join m_lineJoin;
SkDashPathEffect* m_dash;
// Text. (See cTextFill & friends in GraphicsContext.h.)
int m_textDrawingMode;
// Helper function for applying the state's alpha value to the given input
// color to produce a new output color.
SkColor applyAlpha(SkColor) const;
#if OS(LINUX) || OS(WINDOWS)
// If non-empty, the current State is clipped to this image.
SkBitmap m_imageBufferClip;
// If m_imageBufferClip is non-empty, this is the region the image is clipped to.
FloatRect m_clip;
#endif
// This is a list of clipping paths which are currently active, in the
// order in which they were pushed.
WTF::Vector<SkPath> m_antiAliasClipPaths;
InterpolationQuality m_interpolationQuality;
PlatformContextSkia::State cloneInheritedProperties();
private:
// Not supported.
void operator=(const State&);
};
// Note: Keep theses default values in sync with GraphicsContextState.
PlatformContextSkia::State::State()
: m_alpha(1)
, m_xferMode(SkXfermode::kSrcOver_Mode)
, m_useAntialiasing(true)
, m_looper(0)
, m_fillColor(0xFF000000)
, m_fillShader(0)
, m_strokeStyle(SolidStroke)
, m_strokeColor(Color::black)
, m_strokeShader(0)
, m_strokeThickness(0)
, m_dashRatio(3)
, m_miterLimit(4)
, m_lineCap(SkPaint::kDefault_Cap)
, m_lineJoin(SkPaint::kDefault_Join)
, m_dash(0)
, m_textDrawingMode(cTextFill)
, m_interpolationQuality(InterpolationHigh)
{
}
PlatformContextSkia::State::State(const State& other)
: m_alpha(other.m_alpha)
, m_xferMode(other.m_xferMode)
, m_useAntialiasing(other.m_useAntialiasing)
, m_looper(other.m_looper)
, m_fillColor(other.m_fillColor)
, m_fillShader(other.m_fillShader)
, m_strokeStyle(other.m_strokeStyle)
, m_strokeColor(other.m_strokeColor)
, m_strokeShader(other.m_strokeShader)
, m_strokeThickness(other.m_strokeThickness)
, m_dashRatio(other.m_dashRatio)
, m_miterLimit(other.m_miterLimit)
, m_lineCap(other.m_lineCap)
, m_lineJoin(other.m_lineJoin)
, m_dash(other.m_dash)
, m_textDrawingMode(other.m_textDrawingMode)
#if OS(LINUX) || OS(WINDOWS)
, m_imageBufferClip(other.m_imageBufferClip)
, m_clip(other.m_clip)
#endif
, m_antiAliasClipPaths(other.m_antiAliasClipPaths)
, m_interpolationQuality(other.m_interpolationQuality)
{
// Up the ref count of these. saveRef does nothing if 'this' is NULL.
m_looper->safeRef();
m_dash->safeRef();
m_fillShader->safeRef();
m_strokeShader->safeRef();
}
PlatformContextSkia::State::~State()
{
m_looper->safeUnref();
m_dash->safeUnref();
m_fillShader->safeUnref();
m_strokeShader->safeUnref();
}
// Returns a new State with all of this object's inherited properties copied.
PlatformContextSkia::State PlatformContextSkia::State::cloneInheritedProperties()
{
PlatformContextSkia::State state(*this);
// Everything is inherited except for the clip paths.
state.m_antiAliasClipPaths.clear();
return state;
}
SkColor PlatformContextSkia::State::applyAlpha(SkColor c) const
{
int s = roundf(m_alpha * 256);
if (s >= 256)
return c;
if (s < 0)
return 0;
int a = SkAlphaMul(SkColorGetA(c), s);
return (c & 0x00FFFFFF) | (a << 24);
}
// PlatformContextSkia ---------------------------------------------------------
// Danger: canvas can be NULL.
PlatformContextSkia::PlatformContextSkia(skia::PlatformCanvas* canvas)
: m_canvas(canvas)
#if OS(WINDOWS)
, m_drawingToImageBuffer(false)
#endif
#if USE(GLES2_RENDERING)
, m_useGPU(false)
, m_gpuCanvas(0)
, m_backingStoreState(None)
#endif
{
m_stateStack.append(State());
m_state = &m_stateStack.last();
}
PlatformContextSkia::~PlatformContextSkia()
{
#if USE(GLES2_RENDERING) && USE(ACCELERATED_COMPOSITING)
if (m_gpuCanvas) {
CanvasLayerChromium* layer = static_cast<CanvasLayerChromium*>(m_gpuCanvas->context()->platformLayer());
layer->setPrepareTextureCallback(0);
}
#endif
}
void PlatformContextSkia::setCanvas(skia::PlatformCanvas* canvas)
{
m_canvas = canvas;
}
#if OS(WINDOWS)
void PlatformContextSkia::setDrawingToImageBuffer(bool value)
{
m_drawingToImageBuffer = value;
}
bool PlatformContextSkia::isDrawingToImageBuffer() const
{
return m_drawingToImageBuffer;
}
#endif
void PlatformContextSkia::save()
{
ASSERT(!hasImageResamplingHint());
m_stateStack.append(m_state->cloneInheritedProperties());
m_state = &m_stateStack.last();
#if OS(LINUX) || OS(WINDOWS)
// The clip image only needs to be applied once. Reset the image so that we
// don't attempt to clip multiple times.
m_state->m_imageBufferClip.reset();
#endif
// Save our native canvas.
canvas()->save();
}
#if OS(LINUX) || OS(WINDOWS)
void PlatformContextSkia::beginLayerClippedToImage(const FloatRect& rect,
const ImageBuffer* imageBuffer)
{
// Skia doesn't support clipping to an image, so we create a layer. The next
// time restore is invoked the layer and |imageBuffer| are combined to
// create the resulting image.
m_state->m_clip = rect;
SkRect bounds = { SkFloatToScalar(rect.x()), SkFloatToScalar(rect.y()),
SkFloatToScalar(rect.right()), SkFloatToScalar(rect.bottom()) };
canvas()->clipRect(bounds);
canvas()->saveLayerAlpha(&bounds, 255,
static_cast<SkCanvas::SaveFlags>(SkCanvas::kHasAlphaLayer_SaveFlag | SkCanvas::kFullColorLayer_SaveFlag));
// Copy off the image as |imageBuffer| may be deleted before restore is invoked.
const SkBitmap* bitmap = imageBuffer->context()->platformContext()->bitmap();
if (!bitmap->pixelRef()) {
// The bitmap owns it's pixels. This happens when we've allocated the
// pixels in some way and assigned them directly to the bitmap (as
// happens when we allocate a DIB). In this case the assignment operator
// does not copy the pixels, rather the copied bitmap ends up
// referencing the same pixels. As the pixels may not live as long as we
// need it to, we copy the image.
bitmap->copyTo(&m_state->m_imageBufferClip, SkBitmap::kARGB_8888_Config);
} else {
// If there is a pixel ref, we can safely use the assignment operator.
m_state->m_imageBufferClip = *bitmap;
}
}
#endif
void PlatformContextSkia::clipPathAntiAliased(const SkPath& clipPath)
{
// If we are currently tracking any anti-alias clip paths, then we already
// have a layer in place and don't need to add another.
bool haveLayerOutstanding = m_state->m_antiAliasClipPaths.size();
// See comments in applyAntiAliasedClipPaths about how this works.
m_state->m_antiAliasClipPaths.append(clipPath);
if (!haveLayerOutstanding) {
SkRect bounds = clipPath.getBounds();
canvas()->saveLayerAlpha(&bounds, 255, static_cast<SkCanvas::SaveFlags>(SkCanvas::kHasAlphaLayer_SaveFlag | SkCanvas::kFullColorLayer_SaveFlag | SkCanvas::kClipToLayer_SaveFlag));
}
}
void PlatformContextSkia::restore()
{
#if OS(LINUX) || OS(WINDOWS)
if (!m_state->m_imageBufferClip.empty()) {
applyClipFromImage(m_state->m_clip, m_state->m_imageBufferClip);
canvas()->restore();
}
#endif
if (!m_state->m_antiAliasClipPaths.isEmpty())
applyAntiAliasedClipPaths(m_state->m_antiAliasClipPaths);
m_stateStack.removeLast();
m_state = &m_stateStack.last();
// Restore our native canvas.
canvas()->restore();
}
void PlatformContextSkia::drawRect(SkRect rect)
{
SkPaint paint;
int fillcolorNotTransparent = m_state->m_fillColor & 0xFF000000;
if (fillcolorNotTransparent) {
setupPaintForFilling(&paint);
canvas()->drawRect(rect, paint);
}
if (m_state->m_strokeStyle != NoStroke
&& (m_state->m_strokeColor & 0xFF000000)) {
// We do a fill of four rects to simulate the stroke of a border.
SkColor oldFillColor = m_state->m_fillColor;
// setFillColor() will set the shader to NULL, so save a ref to it now.
SkShader* oldFillShader = m_state->m_fillShader;
oldFillShader->safeRef();
setFillColor(m_state->m_strokeColor);
paint.reset();
setupPaintForFilling(&paint);
SkRect topBorder = { rect.fLeft, rect.fTop, rect.fRight, rect.fTop + 1 };
canvas()->drawRect(topBorder, paint);
SkRect bottomBorder = { rect.fLeft, rect.fBottom - 1, rect.fRight, rect.fBottom };
canvas()->drawRect(bottomBorder, paint);
SkRect leftBorder = { rect.fLeft, rect.fTop + 1, rect.fLeft + 1, rect.fBottom - 1 };
canvas()->drawRect(leftBorder, paint);
SkRect rightBorder = { rect.fRight - 1, rect.fTop + 1, rect.fRight, rect.fBottom - 1 };
canvas()->drawRect(rightBorder, paint);
setFillColor(oldFillColor);
setFillShader(oldFillShader);
oldFillShader->safeUnref();
}
}
void PlatformContextSkia::setupPaintCommon(SkPaint* paint) const
{
#if defined(SK_DEBUG)
{
SkPaint defaultPaint;
SkASSERT(*paint == defaultPaint);
}
#endif
paint->setAntiAlias(m_state->m_useAntialiasing);
paint->setXfermodeMode(m_state->m_xferMode);
paint->setLooper(m_state->m_looper);
}
void PlatformContextSkia::setupPaintForFilling(SkPaint* paint) const
{
setupPaintCommon(paint);
paint->setColor(m_state->applyAlpha(m_state->m_fillColor));
paint->setShader(m_state->m_fillShader);
}
float PlatformContextSkia::setupPaintForStroking(SkPaint* paint, SkRect* rect, int length) const
{
setupPaintCommon(paint);
float width = m_state->m_strokeThickness;
paint->setColor(m_state->applyAlpha(m_state->m_strokeColor));
paint->setShader(m_state->m_strokeShader);
paint->setStyle(SkPaint::kStroke_Style);
paint->setStrokeWidth(SkFloatToScalar(width));
paint->setStrokeCap(m_state->m_lineCap);
paint->setStrokeJoin(m_state->m_lineJoin);
paint->setStrokeMiter(SkFloatToScalar(m_state->m_miterLimit));
if (m_state->m_dash)
paint->setPathEffect(m_state->m_dash);
else {
switch (m_state->m_strokeStyle) {
case NoStroke:
case SolidStroke:
break;
case DashedStroke:
width = m_state->m_dashRatio * width;
// Fall through.
case DottedStroke:
// Truncate the width, since we don't want fuzzy dots or dashes.
int dashLength = static_cast<int>(width);
// Subtract off the endcaps, since they're rendered separately.
int distance = length - 2 * static_cast<int>(m_state->m_strokeThickness);
int phase = 1;
if (dashLength > 1) {
// Determine how many dashes or dots we should have.
int numDashes = distance / dashLength;
int remainder = distance % dashLength;
// Adjust the phase to center the dashes within the line.
if (numDashes % 2 == 0) {
// Even: shift right half a dash, minus half the remainder
phase = (dashLength - remainder) / 2;
} else {
// Odd: shift right a full dash, minus half the remainder
phase = dashLength - remainder / 2;
}
}
SkScalar dashLengthSk = SkIntToScalar(dashLength);
SkScalar intervals[2] = { dashLengthSk, dashLengthSk };
paint->setPathEffect(new SkDashPathEffect(intervals, 2, SkIntToScalar(phase)))->unref();
}
}
return width;
}
void PlatformContextSkia::setDrawLooper(SkDrawLooper* dl)
{
SkRefCnt_SafeAssign(m_state->m_looper, dl);
}
void PlatformContextSkia::setMiterLimit(float ml)
{
m_state->m_miterLimit = ml;
}
void PlatformContextSkia::setAlpha(float alpha)
{
m_state->m_alpha = alpha;
}
void PlatformContextSkia::setLineCap(SkPaint::Cap lc)
{
m_state->m_lineCap = lc;
}
void PlatformContextSkia::setLineJoin(SkPaint::Join lj)
{
m_state->m_lineJoin = lj;
}
void PlatformContextSkia::setXfermodeMode(SkXfermode::Mode pdm)
{
m_state->m_xferMode = pdm;
}
void PlatformContextSkia::setFillColor(SkColor color)
{
m_state->m_fillColor = color;
setFillShader(NULL);
}
SkDrawLooper* PlatformContextSkia::getDrawLooper() const
{
return m_state->m_looper;
}
StrokeStyle PlatformContextSkia::getStrokeStyle() const
{
return m_state->m_strokeStyle;
}
void PlatformContextSkia::setStrokeStyle(StrokeStyle strokeStyle)
{
m_state->m_strokeStyle = strokeStyle;
}
void PlatformContextSkia::setStrokeColor(SkColor strokeColor)
{
m_state->m_strokeColor = strokeColor;
setStrokeShader(NULL);
}
float PlatformContextSkia::getStrokeThickness() const
{
return m_state->m_strokeThickness;
}
void PlatformContextSkia::setStrokeThickness(float thickness)
{
m_state->m_strokeThickness = thickness;
}
void PlatformContextSkia::setStrokeShader(SkShader* strokeShader)
{
if (strokeShader != m_state->m_strokeShader) {
m_state->m_strokeShader->safeUnref();
m_state->m_strokeShader = strokeShader;
m_state->m_strokeShader->safeRef();
}
}
int PlatformContextSkia::getTextDrawingMode() const
{
return m_state->m_textDrawingMode;
}
float PlatformContextSkia::getAlpha() const
{
return m_state->m_alpha;
}
void PlatformContextSkia::setTextDrawingMode(int mode)
{
// cTextClip is never used, so we assert that it isn't set:
// https://bugs.webkit.org/show_bug.cgi?id=21898
ASSERT(!(mode & cTextClip));
m_state->m_textDrawingMode = mode;
}
void PlatformContextSkia::setUseAntialiasing(bool enable)
{
m_state->m_useAntialiasing = enable;
}
SkColor PlatformContextSkia::effectiveFillColor() const
{
return m_state->applyAlpha(m_state->m_fillColor);
}
SkColor PlatformContextSkia::effectiveStrokeColor() const
{
return m_state->applyAlpha(m_state->m_strokeColor);
}
void PlatformContextSkia::beginPath()
{
m_path.reset();
}
void PlatformContextSkia::addPath(const SkPath& path)
{
m_path.addPath(path, m_canvas->getTotalMatrix());
}
SkPath PlatformContextSkia::currentPathInLocalCoordinates() const
{
SkPath localPath = m_path;
const SkMatrix& matrix = m_canvas->getTotalMatrix();
SkMatrix inverseMatrix;
if (!matrix.invert(&inverseMatrix))
return SkPath();
localPath.transform(inverseMatrix);
return localPath;
}
void PlatformContextSkia::setFillRule(SkPath::FillType fr)
{
m_path.setFillType(fr);
}
void PlatformContextSkia::setFillShader(SkShader* fillShader)
{
if (fillShader != m_state->m_fillShader) {
m_state->m_fillShader->safeUnref();
m_state->m_fillShader = fillShader;
m_state->m_fillShader->safeRef();
}
}
InterpolationQuality PlatformContextSkia::interpolationQuality() const
{
return m_state->m_interpolationQuality;
}
void PlatformContextSkia::setInterpolationQuality(InterpolationQuality interpolationQuality)
{
m_state->m_interpolationQuality = interpolationQuality;
}
void PlatformContextSkia::setDashPathEffect(SkDashPathEffect* dash)
{
if (dash != m_state->m_dash) {
m_state->m_dash->safeUnref();
m_state->m_dash = dash;
}
}
void PlatformContextSkia::paintSkPaint(const SkRect& rect,
const SkPaint& paint)
{
m_canvas->drawRect(rect, paint);
}
const SkBitmap* PlatformContextSkia::bitmap() const
{
return &m_canvas->getDevice()->accessBitmap(false);
}
bool PlatformContextSkia::isPrinting()
{
return m_canvas->getTopPlatformDevice().IsVectorial();
}
void PlatformContextSkia::getImageResamplingHint(IntSize* srcSize, FloatSize* dstSize) const
{
*srcSize = m_imageResamplingHintSrcSize;
*dstSize = m_imageResamplingHintDstSize;
}
void PlatformContextSkia::setImageResamplingHint(const IntSize& srcSize, const FloatSize& dstSize)
{
m_imageResamplingHintSrcSize = srcSize;
m_imageResamplingHintDstSize = dstSize;
}
void PlatformContextSkia::clearImageResamplingHint()
{
m_imageResamplingHintSrcSize = IntSize();
m_imageResamplingHintDstSize = FloatSize();
}
bool PlatformContextSkia::hasImageResamplingHint() const
{
return !m_imageResamplingHintSrcSize.isEmpty() && !m_imageResamplingHintDstSize.isEmpty();
}
#if OS(LINUX) || OS(WINDOWS)
void PlatformContextSkia::applyClipFromImage(const FloatRect& rect, const SkBitmap& imageBuffer)
{
// NOTE: this assumes the image mask contains opaque black for the portions that are to be shown, as such we
// only look at the alpha when compositing. I'm not 100% sure this is what WebKit expects for image clipping.
SkPaint paint;
paint.setXfermodeMode(SkXfermode::kDstIn_Mode);
m_canvas->drawBitmap(imageBuffer, SkFloatToScalar(rect.x()), SkFloatToScalar(rect.y()), &paint);
}
#endif
void PlatformContextSkia::applyAntiAliasedClipPaths(WTF::Vector<SkPath>& paths)
{
// Anti-aliased clipping:
//
// Skia's clipping is 1-bit only. Consider what would happen if it were 8-bit:
// We have a square canvas, filled with white and we declare a circular
// clipping path. Then we fill twice with a black rectangle. The fractional
// pixels would first get the correct color (white * alpha + black * (1 -
// alpha)), but the second fill would apply the alpha to the already
// modified color and the result would be too dark.
//
// This, anti-aliased clipping needs to be performed after the drawing has
// been done. In order to do this, we create a new layer of the canvas in
// clipPathAntiAliased and store the clipping path. All drawing is done to
// the layer's bitmap while it's in effect. When WebKit calls restore() to
// undo the clipping, this function is called.
//
// Here, we walk the list of clipping paths backwards and, for each, we
// clear outside of the clipping path. We only need a single extra layer
// for any number of clipping paths.
//
// When we call restore on the SkCanvas, the layer's bitmap is composed
// into the layer below and we end up with correct, anti-aliased clipping.
SkPaint paint;
paint.setXfermodeMode(SkXfermode::kClear_Mode);
paint.setAntiAlias(true);
paint.setStyle(SkPaint::kFill_Style);
for (size_t i = paths.size() - 1; i < paths.size(); --i) {
paths[i].setFillType(SkPath::kInverseWinding_FillType);
m_canvas->drawPath(paths[i], paint);
}
m_canvas->restore();
}
#if USE(GLES2_RENDERING)
#if USE(ACCELERATED_COMPOSITING)
class PrepareTextureCallbackImpl : public CanvasLayerChromium::PrepareTextureCallback {
public:
static PassOwnPtr<PrepareTextureCallbackImpl> create(PlatformContextSkia* pcs)
{
return new PrepareTextureCallbackImpl(pcs);
}
virtual void willPrepareTexture()
{
m_pcs->prepareForHardwareDraw();
}
private:
explicit PrepareTextureCallbackImpl(PlatformContextSkia* pcs) : m_pcs(pcs) {}
PlatformContextSkia* m_pcs;
};
#endif
void PlatformContextSkia::setGraphicsContext3D(GraphicsContext3D* context, const WebCore::IntSize& size)
{
m_useGPU = true;
m_gpuCanvas = new GLES2Canvas(context, size);
m_uploadTexture.clear();
#if USE(ACCELERATED_COMPOSITING)
CanvasLayerChromium* layer = static_cast<CanvasLayerChromium*>(context->platformLayer());
layer->setPrepareTextureCallback(PrepareTextureCallbackImpl::create(this));
#endif
}
void PlatformContextSkia::prepareForSoftwareDraw() const
{
if (!m_useGPU)
return;
if (m_backingStoreState == Hardware) {
// Depending on the blend mode we need to do one of a few things:
// * For associative blend modes, we can draw into an initially empty
// canvas and then composite the results on top of the hardware drawn
// results before the next hardware draw or swapBuffers().
// * For non-associative blend modes we have to do a readback and then
// software draw. When we re-upload in this mode we have to blow
// away whatever is in the hardware backing store (do a copy instead
// of a compositing operation).
if (m_state->m_xferMode == SkXfermode::kSrcOver_Mode) {
// Last drawn on hardware; clear out the canvas.
m_canvas->save();
SkRect bounds = {0, 0, m_canvas->getDevice()->width(), m_canvas->getDevice()->height()};
m_canvas->clipRect(bounds, SkRegion::kReplace_Op);
m_canvas->drawARGB(0, 0, 0, 0, SkXfermode::kClear_Mode);
m_canvas->restore();
// Start compositing into the empty canvas.
m_backingStoreState = Mixed;
} else {
readbackHardwareToSoftware();
// When we switch back to hardware copy the results, don't composite.
m_backingStoreState = Software;
}
} else if (m_backingStoreState == Mixed) {
if (m_state->m_xferMode != SkXfermode::kSrcOver_Mode) {
// Have to composite our currently software drawn data...
uploadSoftwareToHardware(CompositeSourceOver);
// then do a readback so we can hardware draw stuff.
readbackHardwareToSoftware();
m_backingStoreState = Software;
}
}
}
void PlatformContextSkia::prepareForHardwareDraw() const
{
if (!m_useGPU)
return;
if (m_backingStoreState == Software) {
// Last drawn in software; upload everything we've drawn.
uploadSoftwareToHardware(CompositeCopy);
} else if (m_backingStoreState == Mixed) {
// Stuff in software/hardware, composite the software stuff on top of
// the hardware stuff.
uploadSoftwareToHardware(CompositeSourceOver);
}
m_backingStoreState = Hardware;
}
void PlatformContextSkia::syncSoftwareCanvas() const
{
if (!m_useGPU)
return;
if (m_backingStoreState == Hardware)
readbackHardwareToSoftware();
else if (m_backingStoreState == Mixed) {
// Have to composite our currently software drawn data..
uploadSoftwareToHardware(CompositeSourceOver);
// then do a readback.
readbackHardwareToSoftware();
m_backingStoreState = Software;
}
m_backingStoreState = Software;
}
void PlatformContextSkia::uploadSoftwareToHardware(CompositeOperator op) const
{
const SkBitmap& bitmap = m_canvas->getDevice()->accessBitmap(false);
SkAutoLockPixels lock(bitmap);
GraphicsContext3D* context = m_gpuCanvas->context();
if (!m_uploadTexture || m_uploadTexture->tiles().totalSizeX() < bitmap.width() || m_uploadTexture->tiles().totalSizeY() < bitmap.height())
m_uploadTexture = GLES2Texture::create(context, GLES2Texture::BGRA8, bitmap.width(), bitmap.height());
m_uploadTexture->load(bitmap.getPixels());
IntRect rect(0, 0, bitmap.width(), bitmap.height());
AffineTransform identity;
gpuCanvas()->drawTexturedRect(m_uploadTexture.get(), rect, rect, identity, 1.0, DeviceColorSpace, op);
}
void PlatformContextSkia::readbackHardwareToSoftware() const
{
const SkBitmap& bitmap = m_canvas->getDevice()->accessBitmap(true);
SkAutoLockPixels lock(bitmap);
int width = bitmap.width(), height = bitmap.height();
OwnArrayPtr<uint32_t> buf(new uint32_t[width]);
GraphicsContext3D* context = m_gpuCanvas->context();
// Flips the image vertically.
for (int y = 0; y < height; ++y) {
uint32_t* pixels = bitmap.getAddr32(0, y);
if (context->supportsBGRA())
context->readPixels(0, height - 1 - y, width, 1, GraphicsContext3D::BGRA_EXT, GraphicsContext3D::UNSIGNED_BYTE, pixels);
else {
context->readPixels(0, height - 1 - y, width, 1, GraphicsContext3D::RGBA, GraphicsContext3D::UNSIGNED_BYTE, pixels);
for (int i = 0; i < width; ++i) {
uint32_t pixel = pixels[i];
// Swizzles from RGBA -> BGRA.
pixels[i] = pixel & 0xFF00FF00 | ((pixel & 0x00FF0000) >> 16) | ((pixel & 0x000000FF) << 16);
}
}
}
}
#endif
} // namespace WebCore
|