aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/gpu/GrRecordReplaceDraw.h
blob: 538661a7b4afad42fd7e14a9dd5d8e3a6c3067f0 (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
/*
 * Copyright 2014 Google Inc.
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#ifndef GrRecordReplaceDraw_DEFINED
#define GrRecordReplaceDraw_DEFINED

#include "SkChecksum.h"
#include "SkDrawPictureCallback.h"
#include "SkImage.h"
#include "SkRect.h"
#include "SkTDynamicHash.h"

class SkBBoxHierarchy;
class SkBitmap;
class SkCanvas;
class SkImage;
class SkMatrix;
class SkPaint;
class SkPicture;
class SkRecord;

// GrReplacements collects op ranges that can be replaced with
// a single drawBitmap call (using a precomputed bitmap).
class GrReplacements {
public:
    // All the operations between fStart and fStop (inclusive) will be replaced with
    // a single drawBitmap call using fPos, fImage and fPaint.
    class ReplacementInfo {
    public:
        struct Key {
            Key(uint32_t pictureID, const SkMatrix& initialMat,
                const int* key, int keySize, bool copyKey = false)
            : fKeySize(keySize)
            , fFreeKey(copyKey) {
                fIDMatrix.fPictureID = pictureID;
                fIDMatrix.fInitialMat = initialMat;
                fIDMatrix.fInitialMat.getType(); // force initialization of type so hashes match

                if (copyKey) {
                    int* tempKey = SkNEW_ARRAY(int, keySize);
                    memcpy(tempKey, key, keySize * sizeof(int));
                    fKey = tempKey;
                } else {
                    fKey = key;
                }

                // The pictureID/matrix portion needs to be tightly packed.
                GR_STATIC_ASSERT(sizeof(IDMatrix) == sizeof(uint32_t)+                // pictureID
                                              9 * sizeof(SkScalar)+sizeof(uint32_t)); // matrix
            }

            ~Key() {
                if (fFreeKey) {
                    SkDELETE_ARRAY(fKey);
                }
            }
            bool operator==(const Key& other) const {
                if (fKeySize != other.fKeySize) {
                    return false;
                }
                return fIDMatrix.fPictureID == other.fIDMatrix.fPictureID &&
                       fIDMatrix.fInitialMat.cheapEqualTo(other.fIDMatrix.fInitialMat) &&
                       !memcmp(fKey, other.fKey, fKeySize * sizeof(int));
            }

            uint32_t hash() const {
                uint32_t hash = SkChecksum::Murmur3(reinterpret_cast<const uint32_t*>(fKey),
                                                    fKeySize * sizeof(int));
                return SkChecksum::Murmur3(reinterpret_cast<const uint32_t*>(&fIDMatrix),
                                           sizeof(IDMatrix), hash);
            }

        private:
            struct IDMatrix {
                uint32_t fPictureID;
                SkMatrix fInitialMat;
            }              fIDMatrix;

            const int*     fKey;
            const int      fKeySize;
            const bool     fFreeKey;
        };

        static const Key& GetKey(const ReplacementInfo& layer) { return layer.fKey; }
        static uint32_t Hash(const Key& key) { return key.hash(); }

        ReplacementInfo(uint32_t pictureID, const SkMatrix& initialMat,
                        const int* key, int keySize)
            : fKey(pictureID, initialMat, key, keySize, true)
            , fImage(NULL)
            , fPaint(NULL) {
        }
        ~ReplacementInfo() { fImage->unref(); SkDELETE(fPaint); }

        const Key       fKey;
        unsigned        fStop;
        SkIPoint        fPos;
        SkImage*        fImage;  // Owns a ref
        const SkPaint*  fPaint;  // Owned by this object

        SkIRect         fSrcRect;
    };

    ~GrReplacements() { this->freeAll(); }

    // Add a new replacement range.
    ReplacementInfo* newReplacement(uint32_t pictureID, const SkMatrix& initialMat,
                                    const int* key, int keySize);

    const ReplacementInfo* lookup(uint32_t pictureID, const SkMatrix& initalMat,
                                  const int* key, int keySize) const;

private:
    SkTDynamicHash<ReplacementInfo, ReplacementInfo::Key> fReplacementHash;

    void freeAll();
};

// Draw an SkPicture into an SkCanvas replacing saveLayer/restore blocks with
// drawBitmap calls.  A convenience wrapper around SkRecords::Draw.
// It returns the number of saveLayer/restore blocks replaced with drawBitmap calls.
int GrRecordReplaceDraw(const SkPicture*,
                        SkCanvas*,
                        const GrReplacements*,
                        const SkMatrix& initialMatrix,
                        SkDrawPictureCallback*);

#endif // GrRecordReplaceDraw_DEFINED