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

#ifndef SkThreadedBMPDevice_DEFINED
#define SkThreadedBMPDevice_DEFINED

#include "SkBitmapDevice.h"
#include "SkDraw.h"
#include "SkRectPriv.h"
#include "SkTaskGroup2D.h"
#include <new>

class SkThreadedBMPDevice : public SkBitmapDevice {
public:
    // When threads = 0, we make fThreadCnt = tiles. Otherwise fThreadCnt = threads.
    // When executor = nullptr, we manages the thread pool. Otherwise, the caller manages it.
    SkThreadedBMPDevice(const SkBitmap& bitmap, int tiles, int threads = 0,
                        SkExecutor* executor = nullptr);

    ~SkThreadedBMPDevice() override { fQueue.finish(); }

protected:
    void drawPaint(const SkPaint& paint) override;
    void drawPoints(SkCanvas::PointMode mode, size_t count,
                            const SkPoint[], const SkPaint& paint) override;
    void drawRect(const SkRect& r, const SkPaint& paint) override;
    void drawRRect(const SkRRect& rr, const SkPaint& paint) override;

    void drawPath(const SkPath&, const SkPaint&, const SkMatrix* prePathMatrix,
                  bool pathIsMutable) override;
    void drawSprite(const SkBitmap&, int x, int y, const SkPaint&) override;
    void drawPosText(const void* text, size_t len, const SkScalar pos[],
                     int scalarsPerPos, const SkPoint& offset, const SkPaint& paint) override;
    void drawVertices(const SkVertices*, const SkMatrix* bones, int boneCount, SkBlendMode,
                      const SkPaint&) override;

    void drawBitmap(const SkBitmap&, const SkMatrix&, const SkRect* dstOrNull,
                    const SkPaint&) override;
    void drawBitmapRect(const SkBitmap& bitmap, const SkRect* src, const SkRect& dst,
                        const SkPaint& paint, SkCanvas::SrcRectConstraint constraint) override;

    sk_sp<SkSpecialImage> snapSpecial() override;

    void flush() override;

private:
    // We store DrawState inside DrawElement because inifFn and drawFn both want to use it
    struct DrawState {
        SkPixmap fDst;
        SkMatrix fMatrix;
        SkRasterClip fRC;

        DrawState() {}
        explicit DrawState(SkThreadedBMPDevice* dev);

        SkDraw getDraw() const;
    };

    class TileDraw : public SkDraw {
        public: TileDraw(const DrawState& ds, const SkIRect& tileBounds);
        private: SkRasterClip fTileRC;
    };

    class DrawElement {
    public:
        using InitFn = std::function<void(SkArenaAlloc* threadAlloc, DrawElement* element)>;
        using DrawFn = std::function<void(SkArenaAlloc* threadAlloc, const DrawState& ds,
                                          const SkIRect& tileBounds)>;

        DrawElement() {}
        DrawElement(SkThreadedBMPDevice* device, DrawFn&& drawFn, const SkIRect& drawBounds)
                : fInitialized(true)
                , fDrawFn(std::move(drawFn))
                , fDS(device)
                , fDrawBounds(drawBounds) {}
        DrawElement(SkThreadedBMPDevice* device, InitFn&& initFn, const SkIRect& drawBounds)
                : fInitialized(false)
                , fNeedInit(true)
                , fInitFn(std::move(initFn))
                , fDS(device)
                , fDrawBounds(drawBounds) {}

        SK_ALWAYS_INLINE bool tryInitOnce(SkArenaAlloc* alloc) {
            bool t = true;
            // If there are multiple threads reaching this point simutaneously,
            // compare_exchange_strong ensures that only one thread can enter the if condition and
            // do the initialization.
            if (!fInitialized && fNeedInit && fNeedInit.compare_exchange_strong(t, false)) {
#ifdef SK_DEBUG
                fDrawFn = 0; // Invalidate fDrawFn
#endif
                fInitFn(alloc, this);
                fInitialized = true;
                SkASSERT(fDrawFn != 0); // Ensure that fInitFn does populate fDrawFn
                return true;
            }
            return false;
        }

        SK_ALWAYS_INLINE bool tryDraw(const SkIRect& tileBounds, SkArenaAlloc* alloc) {
            if (!SkIRect::Intersects(tileBounds, fDrawBounds)) {
                return true;
            }
            if (fInitialized) {
                fDrawFn(alloc, fDS, tileBounds);
                return true;
            }
            return false;
        }

        SkDraw getDraw() const { return fDS.getDraw(); }
        void setDrawFn(DrawFn&& fn) { fDrawFn = std::move(fn); }

    private:
        std::atomic<bool>   fInitialized;
        std::atomic<bool>   fNeedInit;
        InitFn              fInitFn;
        DrawFn              fDrawFn;
        DrawState           fDS;
        SkIRect             fDrawBounds;
    };

    class DrawQueue : public SkWorkKernel2D {
    public:
        static constexpr int MAX_QUEUE_SIZE = 100000;

        DrawQueue(SkThreadedBMPDevice* device) : fDevice(device) {}
        void reset();

        // For ~SkThreadedBMPDevice() to shutdown tasks, we use this instead of reset because reset
        // will start new tasks.
        void finish() { fTasks->finish(); }

        // Push a draw command into the queue. If Fn is DrawFn, we're pushing an element without
        // the need of initialization. If Fn is InitFn, we're pushing an element with init-once
        // and the InitFn will generate the DrawFn during initialization.
        template<bool useCTM = true, typename Fn>
        SK_ALWAYS_INLINE void push(const SkRect& rawDrawBounds, Fn&& fn) {
            if (fSize == MAX_QUEUE_SIZE) {
                this->reset();
            }
            SkASSERT(fSize < MAX_QUEUE_SIZE);
            SkIRect drawBounds = fDevice->transformDrawBounds<useCTM>(rawDrawBounds);
            fElements[fSize].~DrawElement(); // release previous resources to prevent memory leak
            new (&fElements[fSize++]) DrawElement(fDevice, std::move(fn), drawBounds);
            fTasks->addColumn();
        }

        // SkWorkKernel2D
        bool initColumn(int column, int thread) override;
        bool work2D(int row, int column, int thread) override;

    private:
        SkThreadedBMPDevice*                fDevice;
        std::unique_ptr<SkTaskGroup2D>      fTasks;
        SkTArray<SkSTArenaAlloc<8 << 10>>   fThreadAllocs; // 8k stack size
        DrawElement                         fElements[MAX_QUEUE_SIZE];
        int                                 fSize;
    };

    template<bool useCTM = true>
    SkIRect transformDrawBounds(const SkRect& drawBounds) const {
        if (drawBounds == SkRectPriv::MakeLargest()) {
            return SkRectPriv::MakeILarge();
        }
        SkRect transformedBounds;
        if (useCTM) {
            this->ctm().mapRect(&transformedBounds, drawBounds);
        } else {
            transformedBounds = drawBounds;
        }
        return transformedBounds.roundOut();
    }



    template<typename T>
    T* cloneArray(const T* array, int count) {
       T* clone = fAlloc.makeArrayDefault<T>(count);
       memcpy(clone, array, sizeof(T) * count);
       return clone;
    }

    SkBitmap snapBitmap(const SkBitmap& bitmap);

    const int fTileCnt;
    const int fThreadCnt;
    SkTArray<SkIRect> fTileBounds;

    /**
     * This can either be
     * 1. fInternalExecutor.get() which means that we're managing the thread pool's life cycle.
     * 2. provided by our caller which means that our caller is managing the threads' life cycle.
     * In the 2nd case, fInternalExecutor == nullptr.
     */
    SkExecutor* fExecutor = nullptr;
    std::unique_ptr<SkExecutor> fInternalExecutor;

    SkSTArenaAlloc<8 << 10> fAlloc; // so we can allocate memory that lives until flush

    DrawQueue fQueue;

    friend struct SkInitOnceData;   // to access DrawElement and DrawState
    friend class SkDraw;            // to access DrawState

    typedef SkBitmapDevice INHERITED;
};

// Passed to SkDraw::drawXXX to enable threaded draw with init-once. The goal is to reuse as much
// code as possible from SkDraw. (See SkDraw::drawPath and SkDraw::drawDevPath for an example.)
struct SkInitOnceData {
    SkArenaAlloc* fAlloc;
    SkThreadedBMPDevice::DrawElement* fElement;

    void setEmptyDrawFn() {
        fElement->setDrawFn([](SkArenaAlloc* threadAlloc, const SkThreadedBMPDevice::DrawState& ds,
                               const SkIRect& tileBounds){});
    }
};

#endif // SkThreadedBMPDevice_DEFINED