1/*
2 * Copyright 2014 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#include "GrPathRange.h"
9#include "SkPath.h"
10
11enum {
12    kPathsPerGroup = 16 // Paths get tracked in groups of 16 for lazy loading.
13};
14
15GrPathRange::GrPathRange(GrGpu* gpu,
16                         PathGenerator* pathGenerator,
17                         const SkStrokeRec& stroke)
18    : INHERITED(gpu, kIsWrapped),
19      fPathGenerator(SkRef(pathGenerator)),
20      fNumPaths(fPathGenerator->getNumPaths()),
21      fStroke(stroke) {
22    const int numGroups = (fNumPaths + kPathsPerGroup - 1) / kPathsPerGroup;
23    fGeneratedPaths.reset((numGroups + 7) / 8); // 1 bit per path group.
24    memset(&fGeneratedPaths.front(), 0, fGeneratedPaths.count());
25}
26
27GrPathRange::GrPathRange(GrGpu* gpu,
28                         int numPaths,
29                         const SkStrokeRec& stroke)
30    : INHERITED(gpu, kIsWrapped),
31      fNumPaths(numPaths),
32      fStroke(stroke) {
33}
34
35void GrPathRange::willDrawPaths(const uint32_t indices[], int count) const {
36    if (NULL == fPathGenerator.get()) {
37        return;
38    }
39
40    bool didLoadPaths = false;
41
42    for (int i = 0; i < count; ++i) {
43        SkASSERT(indices[i] < static_cast<uint32_t>(fNumPaths));
44
45        const int groupIndex = indices[i] / kPathsPerGroup;
46        const int groupByte = groupIndex / 8;
47        const uint8_t groupBit = 1 << (groupIndex % 8);
48
49        const bool hasPath = SkToBool(fGeneratedPaths[groupByte] & groupBit);
50        if (!hasPath) {
51            // We track which paths are loaded in groups of kPathsPerGroup. To
52            // mark a path as loaded we need to load the entire group.
53            const int groupFirstPath = groupIndex * kPathsPerGroup;
54            const int groupLastPath = SkTMin(groupFirstPath + kPathsPerGroup, fNumPaths) - 1;
55
56            SkPath path;
57            for (int pathIdx = groupFirstPath; pathIdx <= groupLastPath; ++pathIdx) {
58                fPathGenerator->generatePath(pathIdx, &path);
59                this->onInitPath(pathIdx, path);
60            }
61
62            fGeneratedPaths[groupByte] |= groupBit;
63            didLoadPaths = true;
64        }
65    }
66
67    if (didLoadPaths) {
68        this->didChangeGpuMemorySize();
69    }
70}
71