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

#include "SkThreadedBMPDevice.h"

#include "SkPath.h"
#include "SkTaskGroup.h"
#include "SkVertices.h"

#include <mutex>
#include <vector>

constexpr int MAX_CACHE_LINE = 64;

// Some basic logics and data structures that are shared across the current experimental schedulers.
class TiledDrawSchedulerBase : public TiledDrawScheduler {
public:
    TiledDrawSchedulerBase(int tiles, WorkFunc work)
            : fTileCnt(tiles), fIsFinishing(false), fDrawCnt(0), fWork(std::move(work)) {}

    void signal() override {
        fDrawCnt++;
    }
    void finish() override {
        fIsFinishing.store(true, std::memory_order_relaxed);
    }

protected:
    const int                   fTileCnt;
    std::atomic<bool>           fIsFinishing;
    std::atomic<int>            fDrawCnt;
    WorkFunc                    fWork;
};

class TiledDrawSchedulerBySpinning : public TiledDrawSchedulerBase {
public:
    TiledDrawSchedulerBySpinning(int tiles, WorkFunc work)
            : TiledDrawSchedulerBase(tiles, std::move(work)), fScheduleData(tiles) {}

    void signal() final { this->TiledDrawSchedulerBase::signal(); }
    void finish() final { this->TiledDrawSchedulerBase::finish(); }

    bool next(int& tileIndex) final {
        int& drawIndex = fScheduleData[tileIndex].fDrawIndex;
        SkASSERT(drawIndex <= fDrawCnt);
        while (true) {
            bool isFinishing = fIsFinishing.load(std::memory_order_relaxed);
            if (isFinishing && drawIndex >= fDrawCnt) {
                return false;
            } else if (drawIndex < fDrawCnt) {
                fWork(tileIndex, drawIndex++);
                return true;
            }
        }
    }

private:
    // alignas(MAX_CACHE_LINE) to avoid false sharing by cache lines
    struct alignas(MAX_CACHE_LINE) TileScheduleData {
        TileScheduleData() : fDrawIndex(0) {}

        int fDrawIndex; // next draw index for this tile
    };

    std::vector<TileScheduleData>  fScheduleData;
};

class TiledDrawSchedulerFlexible : public TiledDrawSchedulerBase {
public:
    TiledDrawSchedulerFlexible(int tiles, WorkFunc work)
            : TiledDrawSchedulerBase(tiles, std::move(work)), fScheduleData(tiles) {}

    void signal() final { this->TiledDrawSchedulerBase::signal(); }
    void finish() final { this->TiledDrawSchedulerBase::finish(); }

    bool next(int& tileIndex) final {
        int failCnt = 0;
        while (true) {
            TileScheduleData& scheduleData = fScheduleData[tileIndex];
            bool locked = scheduleData.fMutex.try_lock();
            bool processed = false;

            if (locked) {
                if (scheduleData.fDrawIndex < fDrawCnt) {
                    fWork(tileIndex, scheduleData.fDrawIndex++);
                    processed = true;
                } else {
                    failCnt += fIsFinishing.load(std::memory_order_relaxed);
                }
                scheduleData.fMutex.unlock();
            }

            if (processed) {
                return true;
            } else {
                if (failCnt >= fTileCnt) {
                    return false;
                }
                tileIndex = (tileIndex + 1) % fTileCnt;
            }
        }
    }

private:
    // alignas(MAX_CACHE_LINE) to avoid false sharing by cache lines
    struct alignas(MAX_CACHE_LINE) TileScheduleData {
        TileScheduleData() : fDrawIndex(0) {}

        int         fDrawIndex; // next draw index for this tile
        std::mutex  fMutex;     // the mutex for the thread to acquire
    };

    std::vector<TileScheduleData>  fScheduleData;
};

class TiledDrawSchedulerBySemaphores : public TiledDrawSchedulerBase {
public:
    TiledDrawSchedulerBySemaphores(int tiles, WorkFunc work)
            : TiledDrawSchedulerBase(tiles, std::move(work)), fScheduleData(tiles) {}


    void signal() final {
        this->TiledDrawSchedulerBase::signal();
        signalRoot();
    }

    void finish() final {
        this->TiledDrawSchedulerBase::finish();
        signalRoot();
    }

    bool next(int& tileIndex) final {
        SkASSERT(tileIndex >= 0 && tileIndex < fTileCnt);
        TileScheduleData& scheduleData = fScheduleData[tileIndex];
        while (true) {
            scheduleData.fSemaphore.wait();
            int leftChild = (tileIndex + 1) * 2 - 1;
            int rightChild = leftChild + 1;
            if (leftChild < fTileCnt) {
                fScheduleData[leftChild].fSemaphore.signal();
            }
            if (rightChild < fTileCnt) {
                fScheduleData[rightChild].fSemaphore.signal();
            }

            bool isFinishing = fIsFinishing.load(std::memory_order_relaxed);
            if (isFinishing && scheduleData.fDrawIndex >= fDrawCnt) {
                return false;
            } else {
                SkASSERT(scheduleData.fDrawIndex < fDrawCnt);
                fWork(tileIndex, scheduleData.fDrawIndex++);
                return true;
            }
        }
    }

private:
    // alignas(MAX_CACHE_LINE) to avoid false sharing by cache lines
    struct alignas(MAX_CACHE_LINE) TileScheduleData {
        TileScheduleData() : fDrawIndex(0) {}

        int         fDrawIndex;
        SkSemaphore fSemaphore;
    };

    void signalRoot() {
        SkASSERT(fTileCnt > 0);
        fScheduleData[0].fSemaphore.signal();
    }

    std::vector<TileScheduleData> fScheduleData;
};

void SkThreadedBMPDevice::startThreads() {
    SkASSERT(fThreadFutures.count() == 0);
    SkASSERT(fQueueSize == 0);

    TiledDrawScheduler::WorkFunc work = [this](int tileIndex, int drawIndex){
        auto& element = fQueue[drawIndex];
        if (SkIRect::Intersects(fTileBounds[tileIndex], element.fDrawBounds)) {
            element.fDrawFn(fTileBounds[tileIndex]);
        }
    };

    // using Scheduler = TiledDrawSchedulerBySemaphores;
    // using Scheduler = TiledDrawSchedulerBySpinning;
    using Scheduler = TiledDrawSchedulerFlexible;
    fScheduler.reset(new Scheduler(fTileCnt, work));
    for(int i = 0; i < fThreadCnt; ++i) {
        fThreadFutures.push_back(std::async(std::launch::async, [this, i]() {
            int tileIndex = i;
            while (fScheduler->next(tileIndex)) {}
        }));
    }
}

void SkThreadedBMPDevice::finishThreads() {
    fScheduler->finish();
    for(auto& future : fThreadFutures) {
        future.wait();
    }
    fThreadFutures.reset();
    fQueueSize = 0;
    fScheduler.reset(nullptr);
}

SkThreadedBMPDevice::SkThreadedBMPDevice(const SkBitmap& bitmap, int tiles, int threads)
        : INHERITED(bitmap)
        , fTileCnt(tiles)
        , fThreadCnt(threads <= 0 ? tiles : threads)
{
    // Tiling using stripes for now; we'll explore better tiling in the future.
    int h = (bitmap.height() + fTileCnt - 1) / SkTMax(fTileCnt, 1);
    int w = bitmap.width();
    int top = 0;
    for(int tid = 0; tid < fTileCnt; ++tid, top += h) {
        fTileBounds.push_back(SkIRect::MakeLTRB(0, top, w, top + h));
    }
    fQueueSize = 0;
    startThreads();
}

void SkThreadedBMPDevice::flush() {
    finishThreads();
    startThreads();
}

// Having this captured in lambda seems to be faster than saving this in DrawElement
struct SkThreadedBMPDevice::DrawState {
    SkPixmap fDst;
    SkMatrix fMatrix;
    SkRasterClip fRC;

    explicit DrawState(SkThreadedBMPDevice* dev) {
        // we need fDst to be set, and if we're actually drawing, to dirty the genID
        if (!dev->accessPixels(&fDst)) {
            // NoDrawDevice uses us (why?) so we have to catch this case w/ no pixels
            fDst.reset(dev->imageInfo(), nullptr, 0);
        }
        fMatrix = dev->ctm();
        fRC = dev->fRCStack.rc();
    }

    SkDraw getThreadDraw(SkRasterClip& threadRC, const SkIRect& threadBounds) const {
        SkDraw draw;
        draw.fDst = fDst;
        draw.fMatrix = &fMatrix;
        threadRC = fRC;
        threadRC.op(threadBounds, SkRegion::kIntersect_Op);
        draw.fRC = &threadRC;
        return draw;
    }
};

SkIRect SkThreadedBMPDevice::transformDrawBounds(const SkRect& drawBounds) const {
    if (drawBounds.isLargest()) {
        return SkIRect::MakeLargest();
    }
    SkRect transformedBounds;
    this->ctm().mapRect(&transformedBounds, drawBounds);
    return transformedBounds.roundOut();
}

// The do {...} while (false) is to enforce trailing semicolon as suggested by mtklein@
#define THREADED_DRAW(drawBounds, actualDrawCall)                                                  \
    do {                                                                                           \
        if (fQueueSize == MAX_QUEUE_SIZE) {                                                        \
            this->flush();                                                                         \
        }                                                                                          \
        DrawState ds(this);                                                                        \
        SkASSERT(fQueueSize < MAX_QUEUE_SIZE);                                                     \
        fQueue[fQueueSize++] = {                                                                   \
            this->transformDrawBounds(drawBounds),                                                 \
            [=](const SkIRect& tileBounds) {                                                       \
                SkRasterClip tileRC;                                                               \
                SkDraw draw = ds.getThreadDraw(tileRC, tileBounds);                                \
                draw.actualDrawCall;                                                               \
            },                                                                                     \
        };                                                                                         \
        fScheduler->signal();                                                                      \
    } while (false)

static inline SkRect get_fast_bounds(const SkRect& r, const SkPaint& p) {
    SkRect result;
    if (p.canComputeFastBounds()) {
        result = p.computeFastBounds(r, &result);
    } else {
        result = SkRect::MakeLargest();
    }
    return result;
}

void SkThreadedBMPDevice::drawPaint(const SkPaint& paint) {
    THREADED_DRAW(SkRect::MakeLargest(), drawPaint(paint));
}

void SkThreadedBMPDevice::drawPoints(SkCanvas::PointMode mode, size_t count,
        const SkPoint pts[], const SkPaint& paint) {
    // TODO tighter drawBounds
    SkRect drawBounds = SkRect::MakeLargest();
    THREADED_DRAW(drawBounds, drawPoints(mode, count, pts, paint, nullptr));
}

void SkThreadedBMPDevice::drawRect(const SkRect& r, const SkPaint& paint) {
    SkRect drawBounds = get_fast_bounds(r, paint);
    THREADED_DRAW(drawBounds, drawRect(r, paint));
}

void SkThreadedBMPDevice::drawRRect(const SkRRect& rrect, const SkPaint& paint) {
#ifdef SK_IGNORE_BLURRED_RRECT_OPT
    SkPath  path;

    path.addRRect(rrect);
    // call the VIRTUAL version, so any subclasses who do handle drawPath aren't
    // required to override drawRRect.
    this->drawPath(path, paint, nullptr, false);
#else
    SkRect drawBounds = get_fast_bounds(rrect.getBounds(), paint);
    THREADED_DRAW(drawBounds, drawRRect(rrect, paint));
#endif
}

void SkThreadedBMPDevice::drawPath(const SkPath& path, const SkPaint& paint,
        const SkMatrix* prePathMatrix, bool pathIsMutable) {
    SkRect drawBounds = path.isInverseFillType() ? SkRect::MakeLargest()
                                                 : get_fast_bounds(path.getBounds(), paint);
    // For thread safety, make path imutable
    THREADED_DRAW(drawBounds, drawPath(path, paint, prePathMatrix, false));
}

void SkThreadedBMPDevice::drawBitmap(const SkBitmap& bitmap, SkScalar x, SkScalar y,
        const SkPaint& paint) {
    SkMatrix matrix = SkMatrix::MakeTrans(x, y);
    LogDrawScaleFactor(SkMatrix::Concat(this->ctm(), matrix), paint.getFilterQuality());
    SkRect drawBounds = SkRect::MakeWH(bitmap.width(), bitmap.height());
    matrix.mapRect(&drawBounds);
    THREADED_DRAW(drawBounds, drawBitmap(bitmap, matrix, nullptr, paint));
}

void SkThreadedBMPDevice::drawSprite(const SkBitmap& bitmap, int x, int y, const SkPaint& paint) {
    SkRect drawBounds = SkRect::MakeXYWH(x, y, bitmap.width(), bitmap.height());
    THREADED_DRAW(drawBounds, drawSprite(bitmap, x, y, paint));
}

void SkThreadedBMPDevice::drawText(const void* text, size_t len, SkScalar x, SkScalar y,
        const SkPaint& paint) {
    SkRect drawBounds = SkRect::MakeLargest(); // TODO tighter drawBounds
    THREADED_DRAW(drawBounds, drawText((const char*)text, len, x, y, paint, &this->surfaceProps()));
}

void SkThreadedBMPDevice::drawPosText(const void* text, size_t len, const SkScalar xpos[],
        int scalarsPerPos, const SkPoint& offset, const SkPaint& paint) {
    SkRect drawBounds = SkRect::MakeLargest(); // TODO tighter drawBounds
    THREADED_DRAW(drawBounds, drawPosText((const char*)text, len, xpos, scalarsPerPos, offset,
                                          paint, &surfaceProps()));
}

void SkThreadedBMPDevice::drawVertices(const SkVertices* vertices, SkBlendMode bmode,
        const SkPaint& paint) {
    SkRect drawBounds = SkRect::MakeLargest(); // TODO tighter drawBounds
    THREADED_DRAW(drawBounds, drawVertices(vertices->mode(), vertices->vertexCount(),
                                           vertices->positions(), vertices->texCoords(),
                                           vertices->colors(), bmode, vertices->indices(),
                                           vertices->indexCount(), paint));
}

void SkThreadedBMPDevice::drawDevice(SkBaseDevice* device, int x, int y, const SkPaint& paint) {
    SkASSERT(!paint.getImageFilter());
    SkRect drawBounds = SkRect::MakeXYWH(x, y, device->width(), device->height());
    THREADED_DRAW(drawBounds,
                  drawSprite(static_cast<SkBitmapDevice*>(device)->fBitmap, x, y, paint));
}