Coverage Report

Created: 2021-08-22 09:07

/src/skia/src/gpu/ops/GrSmallPathAtlasMgr.cpp
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 2020 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 "src/gpu/ops/GrSmallPathAtlasMgr.h"
9
10
#include "src/gpu/geometry/GrStyledShape.h"
11
#include "src/gpu/ops/GrSmallPathShapeData.h"
12
13
#ifdef DF_PATH_TRACKING
14
static int g_NumCachedShapes = 0;
15
static int g_NumFreedShapes = 0;
16
#endif
17
18
42
GrSmallPathAtlasMgr::GrSmallPathAtlasMgr() {}
19
20
42
GrSmallPathAtlasMgr::~GrSmallPathAtlasMgr() {
21
42
    this->reset();
22
42
}
23
24
42
void GrSmallPathAtlasMgr::reset() {
25
42
    ShapeDataList::Iter iter;
26
42
    iter.init(fShapeList, ShapeDataList::Iter::kHead_IterStart);
27
42
    GrSmallPathShapeData* shapeData;
28
279
    while ((shapeData = iter.get())) {
29
237
        iter.next();
30
237
        delete shapeData;
31
237
    }
32
33
42
    fShapeList.reset();
34
42
    fShapeCache.reset();
35
36
#ifdef DF_PATH_TRACKING
37
    SkDebugf("Cached shapes: %d, freed shapes: %d\n", g_NumCachedShapes, g_NumFreedShapes);
38
#endif
39
40
42
    fAtlas = nullptr;
41
42
}
42
43
3.08k
bool GrSmallPathAtlasMgr::initAtlas(GrProxyProvider* proxyProvider, const GrCaps* caps) {
44
3.08k
    if (fAtlas) {
45
3.03k
        return true;
46
3.03k
    }
47
48
42
    static constexpr size_t kMaxAtlasTextureBytes = 2048 * 2048;
49
42
    static constexpr size_t kPlotWidth = 512;
50
42
    static constexpr size_t kPlotHeight = 256;
51
52
42
    const GrBackendFormat format = caps->getDefaultBackendFormat(GrColorType::kAlpha_8,
53
42
                                                                 GrRenderable::kNo);
54
55
42
    GrDrawOpAtlasConfig atlasConfig(caps->maxTextureSize(), kMaxAtlasTextureBytes);
56
42
    SkISize size = atlasConfig.atlasDimensions(kA8_GrMaskFormat);
57
42
    fAtlas = GrDrawOpAtlas::Make(proxyProvider, format,
58
42
                                 GrColorType::kAlpha_8, size.width(), size.height(),
59
42
                                 kPlotWidth, kPlotHeight, this,
60
42
                                 GrDrawOpAtlas::AllowMultitexturing::kYes, this);
61
62
42
    return SkToBool(fAtlas);
63
42
}
64
65
0
void GrSmallPathAtlasMgr::deleteCacheEntry(GrSmallPathShapeData* shapeData) {
66
0
    fShapeCache.remove(shapeData->fKey);
67
0
    fShapeList.remove(shapeData);
68
0
    delete shapeData;
69
0
}
70
71
2.43k
GrSmallPathShapeData* GrSmallPathAtlasMgr::findOrCreate(const GrSmallPathShapeDataKey& key) {
72
2.43k
    auto shapeData = fShapeCache.find(key);
73
2.43k
    if (!shapeData) {
74
        // TODO: move the key into the ctor
75
285
        shapeData = new GrSmallPathShapeData(key);
76
285
        fShapeCache.add(shapeData);
77
285
        fShapeList.addToTail(shapeData);
78
#ifdef DF_PATH_TRACKING
79
        ++g_NumCachedShapes;
80
#endif
81
2.15k
    } else if (!fAtlas->hasID(shapeData->fAtlasLocator.plotLocator())) {
82
0
        shapeData->fAtlasLocator.invalidatePlotLocator();
83
0
    }
84
85
2.43k
    return shapeData;
86
2.43k
}
87
88
GrSmallPathShapeData* GrSmallPathAtlasMgr::findOrCreate(const GrStyledShape& shape,
89
2.40k
                                                        int desiredDimension) {
90
2.40k
    GrSmallPathShapeDataKey key(shape, desiredDimension);
91
92
    // TODO: move the key into 'findOrCreate'
93
2.40k
    return this->findOrCreate(key);
94
2.40k
}
95
96
GrSmallPathShapeData* GrSmallPathAtlasMgr::findOrCreate(const GrStyledShape& shape,
97
33
                                                        const SkMatrix& ctm) {
98
33
    GrSmallPathShapeDataKey key(shape, ctm);
99
100
    // TODO: move the key into 'findOrCreate'
101
33
    return this->findOrCreate(key);
102
33
}
103
104
GrDrawOpAtlas::ErrorCode GrSmallPathAtlasMgr::addToAtlas(GrResourceProvider* resourceProvider,
105
                                                         GrDeferredUploadTarget* target,
106
                                                         int width, int height, const void* image,
107
285
                                                         GrDrawOpAtlas::AtlasLocator* locator) {
108
285
    return fAtlas->addToAtlas(resourceProvider, target, width, height, image, locator);
109
285
}
110
111
void GrSmallPathAtlasMgr::setUseToken(GrSmallPathShapeData* shapeData,
112
2.43k
                                      GrDeferredUploadToken token) {
113
2.43k
    fAtlas->setLastUseToken(shapeData->fAtlasLocator, token);
114
2.43k
}
115
116
// Callback to clear out internal path cache when eviction occurs
117
7
void GrSmallPathAtlasMgr::evict(GrDrawOpAtlas::PlotLocator plotLocator) {
118
    // remove any paths that use this plot
119
7
    ShapeDataList::Iter iter;
120
7
    iter.init(fShapeList, ShapeDataList::Iter::kHead_IterStart);
121
7
    GrSmallPathShapeData* shapeData;
122
55
    while ((shapeData = iter.get())) {
123
48
        iter.next();
124
48
        if (plotLocator == shapeData->fAtlasLocator.plotLocator()) {
125
48
            fShapeCache.remove(shapeData->fKey);
126
48
            fShapeList.remove(shapeData);
127
48
            delete shapeData;
128
#ifdef DF_PATH_TRACKING
129
            ++g_NumFreedShapes;
130
#endif
131
48
        }
132
48
    }
133
7
}