summaryrefslogtreecommitdiffstats
path: root/Source/WebCore/platform/graphics/android/context/PlatformGraphicsContextRecording.cpp
blob: 10bf36383deafa3d46fa505225369e61da3c363d (plain)
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
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
/*
 * Copyright 2012, The Android Open Source Project
 *
 * 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.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``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.
 */

#define LOG_TAG "PlatformGraphicsContextRecording"
#define LOG_NDEBUG 1

#include "config.h"
#include "PlatformGraphicsContextRecording.h"

#include "AndroidLog.h"
#include "FloatRect.h"
#include "FloatQuad.h"
#include "Font.h"
#include "GraphicsContext.h"
#include "GraphicsOperation.h"
#include "LinearAllocator.h"
#include "PlatformGraphicsContextSkia.h"
#include "RTree.h"
#include "SkDevice.h"

#include "wtf/NonCopyingSort.h"
#include "wtf/HashSet.h"
#include "wtf/StringHasher.h"

#define NEW_OP(X) new (heap()) GraphicsOperation::X

#define USE_CLIPPING_PAINTER true

// Operations smaller than this area aren't considered opaque, and thus don't
// clip operations below. Chosen empirically.
#define MIN_TRACKED_OPAQUE_AREA 750

// Cap on ClippingPainter's recursive depth. Chosen empirically.
#define MAX_CLIPPING_RECURSION_COUNT 400

namespace WebCore {

static FloatRect approximateTextBounds(size_t numGlyphs,
    const SkPoint pos[], const SkPaint& paint)
{
    if (!numGlyphs || !pos) {
        return FloatRect();
    }

    // get glyph position bounds
    SkScalar minX = pos[0].x();
    SkScalar maxX = minX;
    SkScalar minY = pos[0].y();
    SkScalar maxY = minY;
    for (size_t i = 1; i < numGlyphs; ++i) {
        SkScalar x = pos[i].x();
        SkScalar y = pos[i].y();
        minX = std::min(minX, x);
        maxX = std::max(maxX, x);
        minY = std::min(minY, y);
        maxY = std::max(maxY, y);
    }

    // build final rect
    SkPaint::FontMetrics metrics;
    SkScalar bufY = paint.getFontMetrics(&metrics);
    SkScalar bufX = bufY * 2;
    SkScalar adjY = metrics.fAscent / 2;
    minY += adjY;
    maxY += adjY;
    SkRect rect;
    rect.set(minX - bufX, minY - bufY, maxX + bufX, maxY + bufY);
    return rect;
}

class StateHash {
public:
    static unsigned hash(PlatformGraphicsContext::State* const& state)
    {
        return StringHasher::hashMemory(state, sizeof(PlatformGraphicsContext::State));
    }

    static bool equal(PlatformGraphicsContext::State* const& a,
                      PlatformGraphicsContext::State* const& b)
    {
        return a && b && !memcmp(a, b, sizeof(PlatformGraphicsContext::State));
    }

    static const bool safeToCompareToEmptyOrDeleted = false;
};

class SkPaintHash {
public:
    static unsigned hash(const SkPaint* const& paint)
    {
        return StringHasher::hashMemory(paint, sizeof(SkPaint));
    }

    static bool equal(const SkPaint* const& a,
                      const SkPaint* const& b)
    {
        return a && b && (*a == *b);
    }

    static const bool safeToCompareToEmptyOrDeleted = false;
};

typedef HashSet<PlatformGraphicsContext::State*, StateHash> StateHashSet;
typedef HashSet<const SkPaint*, SkPaintHash> SkPaintHashSet;

class CanvasState {
public:
    CanvasState(CanvasState* parent)
        : m_parent(parent)
        , m_isTransparencyLayer(false)
    {}

    CanvasState(CanvasState* parent, float opacity)
        : m_parent(parent)
        , m_isTransparencyLayer(true)
        , m_opacity(opacity)
    {}

    ~CanvasState() {
        ALOGV("Delete %p", this);
        for (size_t i = 0; i < m_operations.size(); i++)
            m_operations[i]->~RecordingData();
        m_operations.clear();
    }

    bool isParentOf(CanvasState* other) {
        while (other->m_parent) {
            if (other->m_parent == this)
                return true;
            other = other->m_parent;
        }
        return false;
    }

    void playback(PlatformGraphicsContext* context, size_t fromId, size_t toId) const {
        ALOGV("playback %p from %d->%d", this, fromId, toId);
        for (size_t i = 0; i < m_operations.size(); i++) {
            RecordingData *data = m_operations[i];
            if (data->m_orderBy < fromId)
                continue;
            if (data->m_orderBy > toId)
                break;
            ALOGV("Applying operation[%d] %p->%s()", i, data->m_operation,
                  data->m_operation->name());
            data->m_operation->apply(context);
        }
    }

    CanvasState* parent() { return m_parent; }

    void enterState(PlatformGraphicsContext* context) {
        ALOGV("enterState %p", this);
        if (m_isTransparencyLayer)
            context->beginTransparencyLayer(m_opacity);
        else
            context->save();
    }

    void exitState(PlatformGraphicsContext* context) {
        ALOGV("exitState %p", this);
        if (m_isTransparencyLayer)
            context->endTransparencyLayer();
        else
            context->restore();
    }

    void adoptAndAppend(RecordingData* data) {
        m_operations.append(data);
    }

    bool isTransparencyLayer() {
        return m_isTransparencyLayer;
    }

    void* operator new(size_t size, LinearAllocator* la) {
        return la->alloc(size);
    }

private:
    CanvasState *m_parent;
    bool m_isTransparencyLayer;
    float m_opacity;
    Vector<RecordingData*> m_operations;
};

class RecordingImpl {
private:
    // Careful, ordering matters here. Ordering is first constructed == last destroyed,
    // so we have to make sure our Heap is the first thing listed so that it is
    // the last thing destroyed.
    LinearAllocator m_heap;
public:
    RecordingImpl()
        : m_tree(&m_heap)
        , m_nodeCount(0)
    {
    }

    ~RecordingImpl() {
        clearStates();
        clearCanvasStates();
        clearSkPaints();
    }

    PlatformGraphicsContext::State* getState(PlatformGraphicsContext::State* inState) {
        StateHashSet::iterator it = m_states.find(inState);
        if (it != m_states.end())
            return (*it);
        void* buf = heap()->alloc(sizeof(PlatformGraphicsContext::State));
        PlatformGraphicsContext::State* state = new (buf) PlatformGraphicsContext::State(*inState);
        m_states.add(state);
        return state;
    }

    const SkPaint* getSkPaint(const SkPaint& inPaint) {
        SkPaintHashSet::iterator it = m_paints.find(&inPaint);
        if (it != m_paints.end())
            return (*it);
        void* buf = heap()->alloc(sizeof(SkPaint));
        SkPaint* paint = new (buf) SkPaint(inPaint);
        m_paints.add(paint);
        return paint;
    }

    void addCanvasState(CanvasState* state) {
        m_canvasStates.append(state);
    }

    void removeCanvasState(const CanvasState* state) {
        if (m_canvasStates.last() == state)
            m_canvasStates.removeLast();
        else {
            size_t indx = m_canvasStates.find(state);
            m_canvasStates.remove(indx);
        }
    }

    void applyState(PlatformGraphicsContext* context,
                    CanvasState* fromState, size_t fromId,
                    CanvasState* toState, size_t toId) {
        ALOGV("applyState(%p->%p, %d-%d)", fromState, toState, fromId, toId);
        if (fromState != toState && fromState) {
            if (fromState->isParentOf(toState)) {
                // Going down the tree, playback any parent operations then save
                // before playing back our current operations
                applyState(context, fromState, fromId, toState->parent(), toId);
                toState->enterState(context);
            } else if (toState->isParentOf(fromState)) {
                // Going up the tree, pop some states
                while (fromState != toState) {
                    fromState->exitState(context);
                    fromState = fromState->parent();
                }
            } else {
                // Siblings in the tree
                fromState->exitState(context);
                applyState(context, fromState->parent(), fromId, toState, toId);
                return;
            }
        } else if (!fromState) {
            if (toState->parent())
                applyState(context, fromState, fromId, toState->parent(), toId);
            toState->enterState(context);
        }
        toState->playback(context, fromId, toId);
    }

    LinearAllocator* heap() { return &m_heap; }

    RTree::RTree m_tree;
    int m_nodeCount;

    void dumpMemoryStats() {
        static const char* PREFIX = "  ";
        ALOGD("Heap:");
        m_heap.dumpMemoryStats(PREFIX);
    }

private:

    void clearStates() {
        StateHashSet::iterator end = m_states.end();
        for (StateHashSet::iterator it = m_states.begin(); it != end; ++it)
            (*it)->~State();
        m_states.clear();
    }

    void clearSkPaints() {
        SkPaintHashSet::iterator end = m_paints.end();
        for (SkPaintHashSet::iterator it = m_paints.begin(); it != end; ++it)
            (*it)->~SkPaint();
        m_paints.clear();
    }

    void clearCanvasStates() {
        for (size_t i = 0; i < m_canvasStates.size(); i++)
            m_canvasStates[i]->~CanvasState();
        m_canvasStates.clear();
    }

    StateHashSet m_states;
    SkPaintHashSet m_paints;
    Vector<CanvasState*> m_canvasStates;
};

Recording::~Recording()
{
    delete m_recording;
}

static bool CompareRecordingDataOrder(const RecordingData* a, const RecordingData* b)
{
    return a->m_orderBy < b->m_orderBy;
}

static IntRect enclosedIntRect(const FloatRect& rect)
{
    float left = ceilf(rect.x());
    float top = ceilf(rect.y());
    float width = floorf(rect.maxX()) - left;
    float height = floorf(rect.maxY()) - top;

    return IntRect(clampToInteger(left), clampToInteger(top),
                   clampToInteger(width), clampToInteger(height));
}

#if USE_CLIPPING_PAINTER
class ClippingPainter {
public:
    ClippingPainter(RecordingImpl* recording,
                    PlatformGraphicsContextSkia& context,
                    const SkMatrix& initialMatrix,
                    Vector<RecordingData*> &nodes)
        : m_recording(recording)
        , m_context(context)
        , m_initialMatrix(initialMatrix)
        , m_nodes(nodes)
        , m_lastOperationId(0)
        , m_currState(0)
    {}

    void draw(const SkIRect& bounds) {
        drawWithClipRecursive(static_cast<int>(m_nodes.size()) - 1, bounds, 0);

        while (m_currState) {
            m_currState->exitState(&m_context);
            m_currState = m_currState->parent();
        }
    }

private:
    void drawOperation(RecordingData* node, const SkRegion* uncovered)
    {
        GraphicsOperation::Operation* op = node->m_operation;
        m_recording->applyState(&m_context, m_currState,
                                m_lastOperationId, op->m_canvasState, node->m_orderBy);
        m_currState = op->m_canvasState;
        m_lastOperationId = node->m_orderBy;

        // if other opaque operations will cover the current one, clip that area out
        // (and restore the clip immediately after drawing)
        if (uncovered) {
            m_context.save();
            m_context.canvas()->clipRegion(*uncovered, SkRegion::kIntersect_Op);
        }
        op->apply(&(m_context));
        if (uncovered)
            m_context.restore();
    }

    void drawWithClipRecursive(int index, const SkIRect& bounds, const SkRegion* uncovered)
    {
        if (index < 0)
            return;
        RecordingData* recordingData = m_nodes[index];
        GraphicsOperation::Operation* op = recordingData->m_operation;
        if (index != 0) {
            const IntRect* opaqueRect = op->opaqueRect();
            if (!opaqueRect || opaqueRect->isEmpty()) {
                drawWithClipRecursive(index - 1, bounds, uncovered);
            } else {
                SkRegion newUncovered;
                if (uncovered)
                    newUncovered = *uncovered;
                else
                    newUncovered = SkRegion(bounds);

                SkRect mappedRect = *opaqueRect;
                m_initialMatrix.mapRect(&mappedRect);
                newUncovered.op(enclosedIntRect(mappedRect), SkRegion::kDifference_Op);
                if (!newUncovered.isEmpty())
                    drawWithClipRecursive(index - 1, bounds, &newUncovered);
            }
        }

        if (!uncovered || !uncovered->isEmpty())
            drawOperation(recordingData, uncovered);
    }

    RecordingImpl* m_recording;
    PlatformGraphicsContextSkia& m_context;
    const SkMatrix& m_initialMatrix;
    const Vector<RecordingData*>& m_nodes;
    size_t m_lastOperationId;
    CanvasState* m_currState;
};
#endif // USE_CLIPPING_PAINTER

void Recording::draw(SkCanvas* canvas)
{
    if (!m_recording) {
        ALOGW("No recording!");
        return;
    }
    SkRect clip;
    if (!canvas->getClipBounds(&clip)) {
        ALOGW("Empty clip!");
        return;
    }
    Vector<RecordingData*> nodes;

    WebCore::IntRect iclip = enclosingIntRect(clip);
    m_recording->m_tree.search(iclip, nodes);

    size_t count = nodes.size();
    ALOGV("Drawing %d nodes out of %d", count, m_recording->m_nodeCount);
    if (count) {
        int saveCount = canvas->getSaveCount();
        nonCopyingSort(nodes.begin(), nodes.end(), CompareRecordingDataOrder);
        PlatformGraphicsContextSkia context(canvas);
#if USE_CLIPPING_PAINTER
        if (canvas->getDevice() && canvas->getDevice()->config() != SkBitmap::kNo_Config
            && count < MAX_CLIPPING_RECURSION_COUNT) {
            ClippingPainter painter(recording(), context, canvas->getTotalMatrix(), nodes);
            painter.draw(canvas->getTotalClip().getBounds());
        } else
#endif
        {
            CanvasState* currState = 0;
            size_t lastOperationId = 0;
            for (size_t i = 0; i < count; i++) {
                GraphicsOperation::Operation* op = nodes[i]->m_operation;
                m_recording->applyState(&context, currState, lastOperationId,
                                        op->m_canvasState, nodes[i]->m_orderBy);
                currState = op->m_canvasState;
                lastOperationId = nodes[i]->m_orderBy;
                ALOGV("apply: %p->%s()", op, op->name());
                op->apply(&context);
            }
            while (currState) {
                currState->exitState(&context);
                currState = currState->parent();
            }
        }
        if (saveCount != canvas->getSaveCount()) {
            ALOGW("Save/restore mismatch! %d vs. %d", saveCount, canvas->getSaveCount());
        }
    }
}

void Recording::setRecording(RecordingImpl* impl)
{
    if (m_recording == impl)
        return;
    if (m_recording)
        delete m_recording;
    m_recording = impl;
}

//**************************************
// PlatformGraphicsContextRecording
//**************************************

PlatformGraphicsContextRecording::PlatformGraphicsContextRecording(Recording* recording)
    : PlatformGraphicsContext()
    , mPicture(0)
    , mRecording(recording)
    , mOperationState(0)
    , m_maxZoomScale(1)
    , m_isEmpty(true)
    , m_canvasProxy(this)
{
    ALOGV("RECORDING: begin");
    if (mRecording)
        mRecording->setRecording(new RecordingImpl());
    mMatrixStack.append(SkMatrix::I());
    mCurrentMatrix = &(mMatrixStack.last());
    pushStateOperation(new (heap()) CanvasState(0));
}

PlatformGraphicsContextRecording::~PlatformGraphicsContextRecording()
{
    ALOGV("RECORDING: end");
    IF_ALOGV()
        mRecording->recording()->dumpMemoryStats();
}

bool PlatformGraphicsContextRecording::isPaintingDisabled()
{
    return !mRecording;
}

SkCanvas* PlatformGraphicsContextRecording::recordingCanvas()
{
    m_maxZoomScale = 1e6f;
    return &m_canvasProxy;
}

//**************************************
// State management
//**************************************

void PlatformGraphicsContextRecording::beginTransparencyLayer(float opacity)
{
    CanvasState* parent = mRecordingStateStack.last().mCanvasState;
    pushStateOperation(new (heap()) CanvasState(parent, opacity));
    mRecordingStateStack.last().disableOpaqueTracking();
}

void PlatformGraphicsContextRecording::endTransparencyLayer()
{
    popStateOperation();
}

void PlatformGraphicsContextRecording::save()
{
    PlatformGraphicsContext::save();
    CanvasState* parent = mRecordingStateStack.last().mCanvasState;
    pushStateOperation(new (heap()) CanvasState(parent));
    pushMatrix();
}

void PlatformGraphicsContextRecording::restore()
{
    PlatformGraphicsContext::restore();
    popMatrix();
    popStateOperation();
}

//**************************************
// State setters
//**************************************

void PlatformGraphicsContextRecording::setAlpha(float alpha)
{
    PlatformGraphicsContext::setAlpha(alpha);
    mOperationState = 0;
}

void PlatformGraphicsContextRecording::setCompositeOperation(CompositeOperator op)
{
    PlatformGraphicsContext::setCompositeOperation(op);
    mOperationState = 0;
}

bool PlatformGraphicsContextRecording::setFillColor(const Color& c)
{
    if (PlatformGraphicsContext::setFillColor(c)) {
        mOperationState = 0;
        return true;
    }
    return false;
}

bool PlatformGraphicsContextRecording::setFillShader(SkShader* fillShader)
{
    if (PlatformGraphicsContext::setFillShader(fillShader)) {
        mOperationState = 0;
        return true;
    }
    return false;
}

void PlatformGraphicsContextRecording::setLineCap(LineCap cap)
{
    PlatformGraphicsContext::setLineCap(cap);
    mOperationState = 0;
}

void PlatformGraphicsContextRecording::setLineDash(const DashArray& dashes, float dashOffset)
{
    PlatformGraphicsContext::setLineDash(dashes, dashOffset);
    mOperationState = 0;
}

void PlatformGraphicsContextRecording::setLineJoin(LineJoin join)
{
    PlatformGraphicsContext::setLineJoin(join);
    mOperationState = 0;
}

void PlatformGraphicsContextRecording::setMiterLimit(float limit)
{
    PlatformGraphicsContext::setMiterLimit(limit);
    mOperationState = 0;
}

void PlatformGraphicsContextRecording::setShadow(int radius, int dx, int dy, SkColor c)
{
    PlatformGraphicsContext::setShadow(radius, dx, dy, c);
    mOperationState = 0;
}

void PlatformGraphicsContextRecording::setShouldAntialias(bool useAA)
{
    m_state->useAA = useAA;
    PlatformGraphicsContext::setShouldAntialias(useAA);
    mOperationState = 0;
}

bool PlatformGraphicsContextRecording::setStrokeColor(const Color& c)
{
    if (PlatformGraphicsContext::setStrokeColor(c)) {
        mOperationState = 0;
        return true;
    }
    return false;
}

bool PlatformGraphicsContextRecording::setStrokeShader(SkShader* strokeShader)
{
    if (PlatformGraphicsContext::setStrokeShader(strokeShader)) {
        mOperationState = 0;
        return true;
    }
    return false;
}

void PlatformGraphicsContextRecording::setStrokeStyle(StrokeStyle style)
{
    PlatformGraphicsContext::setStrokeStyle(style);
    mOperationState = 0;
}

void PlatformGraphicsContextRecording::setStrokeThickness(float f)
{
    PlatformGraphicsContext::setStrokeThickness(f);
    mOperationState = 0;
}

//**************************************
// Matrix operations
//**************************************

void PlatformGraphicsContextRecording::concatCTM(const AffineTransform& affine)
{
    mCurrentMatrix->preConcat(affine);
    appendStateOperation(NEW_OP(ConcatCTM)(affine));
}

void PlatformGraphicsContextRecording::rotate(float angleInRadians)
{
    float value = angleInRadians * (180.0f / 3.14159265f);
    mCurrentMatrix->preRotate(SkFloatToScalar(value));
    appendStateOperation(NEW_OP(Rotate)(angleInRadians));
}

void PlatformGraphicsContextRecording::scale(const FloatSize& size)
{
    mCurrentMatrix->preScale(SkFloatToScalar(size.width()), SkFloatToScalar(size.height()));
    appendStateOperation(NEW_OP(Scale)(size));
}

void PlatformGraphicsContextRecording::translate(float x, float y)
{
    mCurrentMatrix->preTranslate(SkFloatToScalar(x), SkFloatToScalar(y));
    appendStateOperation(NEW_OP(Translate)(x, y));
}

const SkMatrix& PlatformGraphicsContextRecording::getTotalMatrix()
{
    return *mCurrentMatrix;
}

//**************************************
// Clipping
//**************************************

void PlatformGraphicsContextRecording::addInnerRoundedRectClip(const IntRect& rect,
                                                      int thickness)
{
    mRecordingStateStack.last().disableOpaqueTracking();
    appendStateOperation(NEW_OP(InnerRoundedRectClip)(rect, thickness));
}

void PlatformGraphicsContextRecording::canvasClip(const Path& path)
{
    mRecordingStateStack.last().disableOpaqueTracking();
    clip(path);
}

bool PlatformGraphicsContextRecording::clip(const FloatRect& rect)
{
    clipState(rect);
    appendStateOperation(NEW_OP(Clip)(rect));
    return true;
}

bool PlatformGraphicsContextRecording::clip(const Path& path)
{
    mRecordingStateStack.last().disableOpaqueTracking();
    clipState(path.boundingRect());
    appendStateOperation(NEW_OP(ClipPath)(path));
    return true;
}

bool PlatformGraphicsContextRecording::clipConvexPolygon(size_t numPoints,
                                                const FloatPoint*, bool antialias)
{
    // TODO
    return true;
}

bool PlatformGraphicsContextRecording::clipOut(const IntRect& r)
{
    mRecordingStateStack.last().disableOpaqueTracking();
    appendStateOperation(NEW_OP(ClipOut)(r));
    return true;
}

bool PlatformGraphicsContextRecording::clipOut(const Path& path)
{
    mRecordingStateStack.last().disableOpaqueTracking();
    appendStateOperation(NEW_OP(ClipPath)(path, true));
    return true;
}

bool PlatformGraphicsContextRecording::clipPath(const Path& pathToClip, WindRule clipRule)
{
    mRecordingStateStack.last().disableOpaqueTracking();
    clipState(pathToClip.boundingRect());
    GraphicsOperation::ClipPath* operation = NEW_OP(ClipPath)(pathToClip);
    operation->setWindRule(clipRule);
    appendStateOperation(operation);
    return true;
}

void PlatformGraphicsContextRecording::clearRect(const FloatRect& rect)
{
    appendDrawingOperation(NEW_OP(ClearRect)(rect), rect);
}

//**************************************
// Drawing
//**************************************

void PlatformGraphicsContextRecording::drawBitmapPattern(
        const SkBitmap& bitmap, const SkMatrix& matrix,
        CompositeOperator compositeOp, const FloatRect& destRect)
{
    appendDrawingOperation(
            NEW_OP(DrawBitmapPattern)(bitmap, matrix, compositeOp, destRect),
            destRect);
}

void PlatformGraphicsContextRecording::drawBitmapRect(const SkBitmap& bitmap,
                                   const SkIRect* srcPtr, const SkRect& dst,
                                   CompositeOperator op)
{
    float widthScale = dst.width() == 0 ? 1 : bitmap.width() / dst.width();
    float heightScale = dst.height() == 0 ? 1 : bitmap.height() / dst.height();
    m_maxZoomScale = std::max(m_maxZoomScale, std::max(widthScale, heightScale));
    // null src implies full bitmap as source rect
    SkIRect src = srcPtr ? *srcPtr : SkIRect::MakeWH(bitmap.width(), bitmap.height());
    appendDrawingOperation(NEW_OP(DrawBitmapRect)(bitmap, src, dst, op), dst);
}

void PlatformGraphicsContextRecording::drawConvexPolygon(size_t numPoints,
                                                const FloatPoint* points,
                                                bool shouldAntialias)
{
    if (numPoints < 1) return;
    if (numPoints != 4) {
        // TODO: Build a path and call draw on that (webkit currently never calls this)
        ALOGW("drawConvexPolygon with numPoints != 4 is not supported!");
        return;
    }
    FloatRect bounds;
    bounds.fitToPoints(points[0], points[1], points[2], points[3]);
    appendDrawingOperation(NEW_OP(DrawConvexPolygonQuad)(points, shouldAntialias), bounds);
}

void PlatformGraphicsContextRecording::drawEllipse(const IntRect& rect)
{
    appendDrawingOperation(NEW_OP(DrawEllipse)(rect), rect);
}

void PlatformGraphicsContextRecording::drawFocusRing(const Vector<IntRect>& rects,
                                            int width, int offset,
                                            const Color& color)
{
    if (!rects.size())
        return;
    IntRect bounds = rects[0];
    for (size_t i = 1; i < rects.size(); i++)
        bounds.unite(rects[i]);
    appendDrawingOperation(NEW_OP(DrawFocusRing)(rects, width, offset, color), bounds);
}

void PlatformGraphicsContextRecording::drawHighlightForText(
        const Font& font, const TextRun& run, const FloatPoint& point, int h,
        const Color& backgroundColor, ColorSpace colorSpace, int from,
        int to, bool isActive)
{
    IntRect rect = (IntRect)font.selectionRectForText(run, point, h, from, to);
    if (isActive)
        fillRect(rect, backgroundColor);
    else {
        int x = rect.x(), y = rect.y(), w = rect.width(), h = rect.height();
        const int t = 3, t2 = t * 2;

        fillRect(IntRect(x, y, w, t), backgroundColor);
        fillRect(IntRect(x, y+h-t, w, t), backgroundColor);
        fillRect(IntRect(x, y+t, t, h-t2), backgroundColor);
        fillRect(IntRect(x+w-t, y+t, t, h-t2), backgroundColor);
    }
}

void PlatformGraphicsContextRecording::drawLine(const IntPoint& point1,
                             const IntPoint& point2)
{
    FloatRect bounds = FloatQuad(point1, point1, point2, point2).boundingBox();
    float width = m_state->strokeThickness;
    if (!width) width = 1;
    bounds.inflate(width);
    appendDrawingOperation(NEW_OP(DrawLine)(point1, point2), bounds);
}

void PlatformGraphicsContextRecording::drawLineForText(const FloatPoint& pt, float width)
{
    FloatRect bounds(pt.x(), pt.y(), width, m_state->strokeThickness);
    appendDrawingOperation(NEW_OP(DrawLineForText)(pt, width), bounds);
}

void PlatformGraphicsContextRecording::drawLineForTextChecking(const FloatPoint& pt,
        float width, GraphicsContext::TextCheckingLineStyle lineStyle)
{
    FloatRect bounds(pt.x(), pt.y(), width, m_state->strokeThickness);
    appendDrawingOperation(NEW_OP(DrawLineForTextChecking)(pt, width, lineStyle), bounds);
}

void PlatformGraphicsContextRecording::drawRect(const IntRect& rect)
{
    appendDrawingOperation(NEW_OP(DrawRect)(rect), rect);
}

void PlatformGraphicsContextRecording::fillPath(const Path& pathToFill, WindRule fillRule)
{
    appendDrawingOperation(NEW_OP(FillPath)(pathToFill, fillRule), pathToFill.boundingRect());
}

void PlatformGraphicsContextRecording::fillRect(const FloatRect& rect)
{
    appendDrawingOperation(NEW_OP(FillRect)(rect), rect);
}

void PlatformGraphicsContextRecording::fillRect(const FloatRect& rect,
                                       const Color& color)
{
    GraphicsOperation::FillRect* operation = NEW_OP(FillRect)(rect);
    operation->setColor(color);
    appendDrawingOperation(operation, rect);
}

void PlatformGraphicsContextRecording::fillRoundedRect(
        const IntRect& rect, const IntSize& topLeft, const IntSize& topRight,
        const IntSize& bottomLeft, const IntSize& bottomRight,
        const Color& color)
{
    appendDrawingOperation(NEW_OP(FillRoundedRect)(rect, topLeft,
                 topRight, bottomLeft, bottomRight, color), rect);
}

void PlatformGraphicsContextRecording::strokeArc(const IntRect& r, int startAngle,
                              int angleSpan)
{
    appendDrawingOperation(NEW_OP(StrokeArc)(r, startAngle, angleSpan), r);
}

void PlatformGraphicsContextRecording::strokePath(const Path& pathToStroke)
{
    appendDrawingOperation(NEW_OP(StrokePath)(pathToStroke), pathToStroke.boundingRect());
}

void PlatformGraphicsContextRecording::strokeRect(const FloatRect& rect, float lineWidth)
{
    FloatRect bounds = rect;
    bounds.inflate(lineWidth);
    appendDrawingOperation(NEW_OP(StrokeRect)(rect, lineWidth), bounds);
}

void PlatformGraphicsContextRecording::drawPosText(const void* inText, size_t byteLength,
                                                   const SkPoint inPos[], const SkPaint& inPaint)
{
    if (inPaint.getTextEncoding() != SkPaint::kGlyphID_TextEncoding) {
        ALOGE("Unsupported text encoding! %d", inPaint.getTextEncoding());
        return;
    }
    FloatRect bounds = approximateTextBounds(byteLength / sizeof(uint16_t), inPos, inPaint);
    bounds.move(m_textOffset); // compensate font rendering-side translates

    const SkPaint* paint = mRecording->recording()->getSkPaint(inPaint);
    size_t posSize = sizeof(SkPoint) * paint->countText(inText, byteLength);
    void* text = heap()->alloc(byteLength);
    SkPoint* pos = (SkPoint*) heap()->alloc(posSize);
    memcpy(text, inText, byteLength);
    memcpy(pos, inPos, posSize);
    appendDrawingOperation(NEW_OP(DrawPosText)(text, byteLength, pos, paint), bounds);
}

void PlatformGraphicsContextRecording::drawMediaButton(const IntRect& rect, RenderSkinMediaButton::MediaButton buttonType,
                                                       bool translucent, bool drawBackground,
                                                       const IntRect& thumb)
{
    appendDrawingOperation(NEW_OP(DrawMediaButton)(rect, buttonType,
            translucent, drawBackground, thumb), rect);
}

void PlatformGraphicsContextRecording::clipState(const FloatRect& clip)
{
    if (mRecordingStateStack.size()) {
        SkRect mapBounds;
        mCurrentMatrix->mapRect(&mapBounds, clip);
        mRecordingStateStack.last().clip(mapBounds);
    }
}

void PlatformGraphicsContextRecording::pushStateOperation(CanvasState* canvasState)
{
    ALOGV("RECORDING: pushStateOperation: %p(isLayer=%d)", canvasState, canvasState->isTransparencyLayer());

    RecordingState* parent = mRecordingStateStack.isEmpty() ? 0 : &(mRecordingStateStack.last());
    mRecordingStateStack.append(RecordingState(canvasState, parent));
    mRecording->recording()->addCanvasState(canvasState);
}

void PlatformGraphicsContextRecording::popStateOperation()
{
    RecordingState state = mRecordingStateStack.last();
    mRecordingStateStack.removeLast();
    mOperationState = 0;
    if (!state.mHasDrawing) {
        ALOGV("RECORDING: popStateOperation is deleting %p(isLayer=%d)",
                state.mCanvasState, state.mCanvasState->isTransparencyLayer());
        mRecording->recording()->removeCanvasState(state.mCanvasState);
        state.mCanvasState->~CanvasState();
        heap()->rewindIfLastAlloc(state.mCanvasState, sizeof(CanvasState));
    } else {
        ALOGV("RECORDING: popStateOperation: %p(isLayer=%d)",
                state.mCanvasState, state.mCanvasState->isTransparencyLayer());
        // Make sure we propagate drawing upwards so we don't delete our parent
        mRecordingStateStack.last().mHasDrawing = true;
    }
}

void PlatformGraphicsContextRecording::pushMatrix()
{
    mMatrixStack.append(mMatrixStack.last());
    mCurrentMatrix = &(mMatrixStack.last());
}

void PlatformGraphicsContextRecording::popMatrix()
{
    mMatrixStack.removeLast();
    mCurrentMatrix = &(mMatrixStack.last());
}

IntRect PlatformGraphicsContextRecording::calculateFinalBounds(FloatRect bounds)
{
    if (bounds.isEmpty() && mRecordingStateStack.last().mHasClip) {
        ALOGV("Empty bounds, but has clip so using that");
        return enclosingIntRect(mRecordingStateStack.last().mBounds);
    }
    if (m_gc->hasShadow()) {
        const ShadowRec& shadow = m_state->shadow;
        if (shadow.blur > 0)
            bounds.inflate(ceilf(shadow.blur));
        bounds.setWidth(bounds.width() + abs(shadow.dx));
        bounds.setHeight(bounds.height() + abs(shadow.dy));
        if (shadow.dx < 0)
            bounds.move(shadow.dx, 0);
        if (shadow.dy < 0)
            bounds.move(0, shadow.dy);
        // Add a bit extra to deal with rounding and blurring
        bounds.inflate(4);
    }
    if (m_state->strokeStyle != NoStroke)
        bounds.inflate(std::min(1.0f, m_state->strokeThickness));
    SkRect translated;
    mCurrentMatrix->mapRect(&translated, bounds);
    FloatRect ftrect = translated;
    if (mRecordingStateStack.last().mHasClip
            && !translated.intersect(mRecordingStateStack.last().mBounds)) {
        ALOGV("Operation bounds=" FLOAT_RECT_FORMAT " clipped out by clip=" FLOAT_RECT_FORMAT,
                FLOAT_RECT_ARGS(ftrect), FLOAT_RECT_ARGS(mRecordingStateStack.last().mBounds));
        return IntRect();
    }
    return enclosingIntRect(translated);
}

IntRect PlatformGraphicsContextRecording::calculateCoveredBounds(FloatRect bounds)
{
    if (mRecordingStateStack.last().mOpaqueTrackingDisabled
        || m_state->alpha != 1.0f
        || (m_state->fillShader != 0 && !m_state->fillShader->isOpaque())
        || (m_state->mode != SkXfermode::kSrc_Mode && m_state->mode != SkXfermode::kSrcOver_Mode)
        || !mCurrentMatrix->rectStaysRect()) {
        return IntRect();
    }

    SkRect translated;
    mCurrentMatrix->mapRect(&translated, bounds);
    FloatRect ftrect = translated;
    if (mRecordingStateStack.last().mHasClip
            && !translated.intersect(mRecordingStateStack.last().mBounds)) {
        ALOGV("Operation opaque area=" FLOAT_RECT_FORMAT " clipped out by clip=" FLOAT_RECT_FORMAT,
                FLOAT_RECT_ARGS(ftrect), FLOAT_RECT_ARGS(mRecordingStateStack.last().mBounds));
        return IntRect();
    }
    return enclosedIntRect(translated);
}

void PlatformGraphicsContextRecording::appendDrawingOperation(
        GraphicsOperation::Operation* operation, const FloatRect& untranslatedBounds)
{
    m_isEmpty = false;
    RecordingState& state = mRecordingStateStack.last();
    state.mHasDrawing = true;
    if (!mOperationState)
        mOperationState = mRecording->recording()->getState(m_state);
    operation->m_state = mOperationState;
    operation->m_canvasState = state.mCanvasState;

    WebCore::IntRect ibounds = calculateFinalBounds(untranslatedBounds);
    if (ibounds.isEmpty()) {
        ALOGV("RECORDING: Operation %s() was clipped out", operation->name());
        operation->~Operation();
        return;
    }
#if USE_CLIPPING_PAINTER
    if (operation->isOpaque()
        && !untranslatedBounds.isEmpty()
        && (untranslatedBounds.width() * untranslatedBounds.height() > MIN_TRACKED_OPAQUE_AREA)) {
        // if the operation maps to an opaque rect, record the area it will cover
        operation->setOpaqueRect(calculateCoveredBounds(untranslatedBounds));
    }
#endif
    ALOGV("RECORDING: appendOperation %p->%s() bounds " INT_RECT_FORMAT, operation, operation->name(),
            INT_RECT_ARGS(ibounds));
    RecordingData* data = new (heap()) RecordingData(operation, mRecording->recording()->m_nodeCount++);
    mRecording->recording()->m_tree.insert(ibounds, data);
}

void PlatformGraphicsContextRecording::appendStateOperation(GraphicsOperation::Operation* operation)
{
    ALOGV("RECORDING: appendOperation %p->%s()", operation, operation->name());
    RecordingData* data = new (heap()) RecordingData(operation, mRecording->recording()->m_nodeCount++);
    mRecordingStateStack.last().mCanvasState->adoptAndAppend(data);
}

LinearAllocator* PlatformGraphicsContextRecording::heap()
{
    return mRecording->recording()->heap();
}

}   // WebCore