1 /*
2 * Copyright 2013 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 "include/core/SkString.h"
9 #include "include/gpu/GrBackendSurface.h"
10 #include "include/gpu/GrContextOptions.h"
11 #include "include/gpu/GrTexture.h"
12 #include "include/private/GrRecordingContext.h"
13 #include "include/private/SkTo.h"
14 #include "src/core/SkMathPriv.h"
15 #include "src/gpu/GrClip.h"
16 #include "src/gpu/GrContextPriv.h"
17 #include "src/gpu/GrDrawOpAtlas.h"
18 #include "src/gpu/GrDrawingManager.h"
19 #include "src/gpu/GrGpu.h"
20 #include "src/gpu/GrGpuResourceCacheAccess.h"
21 #include "src/gpu/GrMemoryPool.h"
22 #include "src/gpu/GrRecordingContextPriv.h"
23 #include "src/gpu/GrRenderTargetContext.h"
24 #include "src/gpu/GrRenderTargetContextPriv.h"
25 #include "src/gpu/GrRenderTargetProxy.h"
26 #include "src/gpu/GrResourceCache.h"
27 #include "src/gpu/GrSemaphore.h"
28 #include "src/gpu/GrSurfaceContextPriv.h"
29 #include "src/gpu/SkGr.h"
30 #include "src/gpu/ccpr/GrCCPathCache.h"
31 #include "src/gpu/ccpr/GrCoverageCountingPathRenderer.h"
32 #include "src/gpu/ops/GrMeshDrawOp.h"
33 #include "src/gpu/text/GrStrikeCache.h"
34 #include "src/gpu/text/GrTextBlobCache.h"
35 #include "src/image/SkImage_Gpu.h"
36
37 #include <algorithm>
38
39 ///////////////////////////////////////////////////////////////////////////////
40
changeTimestamp(uint32_t newTimestamp)41 void GrResourceCache::changeTimestamp(uint32_t newTimestamp) { fTimestamp = newTimestamp; }
42
43 #ifdef SK_DEBUG
countUniqueKeysWithTag(const char * tag) const44 int GrResourceCache::countUniqueKeysWithTag(const char* tag) const {
45 int count = 0;
46 UniqueHash::ConstIter iter(&fUniqueHash);
47 while (!iter.done()) {
48 if (0 == strcmp(tag, (*iter).getUniqueKey().tag())) {
49 ++count;
50 }
51 ++iter;
52 }
53 return count;
54 }
55 #endif
56
57 ///////////////////////////////////////////////////////////////////////////////
58
59 #define ASSERT_SINGLE_OWNER \
60 SkDEBUGCODE(GrSingleOwner::AutoEnforce debug_SingleOwner(fRenderTargetContext->singleOwner());)
61
62
testingOnly_getOpsTaskID()63 uint32_t GrRenderTargetContextPriv::testingOnly_getOpsTaskID() {
64 return fRenderTargetContext->getOpsTask()->uniqueID();
65 }
66
testingOnly_addDrawOp(std::unique_ptr<GrDrawOp> op)67 void GrRenderTargetContextPriv::testingOnly_addDrawOp(std::unique_ptr<GrDrawOp> op) {
68 this->testingOnly_addDrawOp(GrNoClip(), std::move(op));
69 }
70
testingOnly_addDrawOp(const GrClip & clip,std::unique_ptr<GrDrawOp> op,const std::function<GrRenderTargetContext::WillAddOpFn> & willAddFn)71 void GrRenderTargetContextPriv::testingOnly_addDrawOp(
72 const GrClip& clip,
73 std::unique_ptr<GrDrawOp> op,
74 const std::function<GrRenderTargetContext::WillAddOpFn>& willAddFn) {
75 ASSERT_SINGLE_OWNER
76 if (fRenderTargetContext->fContext->priv().abandoned()) {
77 fRenderTargetContext->fContext->priv().opMemoryPool()->release(std::move(op));
78 return;
79 }
80 SkDEBUGCODE(fRenderTargetContext->validate());
81 GR_AUDIT_TRAIL_AUTO_FRAME(fRenderTargetContext->auditTrail(),
82 "GrRenderTargetContext::testingOnly_addDrawOp");
83 fRenderTargetContext->addDrawOp(clip, std::move(op), willAddFn);
84 }
85
86 #undef ASSERT_SINGLE_OWNER
87
88 //////////////////////////////////////////////////////////////////////////////
89
testingOnly_drawPathDirectly(const DrawPathArgs & args)90 void GrCoverageCountingPathRenderer::testingOnly_drawPathDirectly(const DrawPathArgs& args) {
91 // Call onDrawPath() directly: We want to test paths that might fail onCanDrawPath() simply for
92 // performance reasons, and GrPathRenderer::drawPath() assert that this call returns true.
93 // The test is responsible to not draw any paths that CCPR is not actually capable of.
94 this->onDrawPath(args);
95 }
96
97 const GrCCPerFlushResources*
testingOnly_getCurrentFlushResources()98 GrCoverageCountingPathRenderer::testingOnly_getCurrentFlushResources() {
99 SkASSERT(fFlushing);
100 if (fFlushingPaths.empty()) {
101 return nullptr;
102 }
103 // All pending paths should share the same resources.
104 const GrCCPerFlushResources* resources = fFlushingPaths.front()->fFlushResources.get();
105 #ifdef SK_DEBUG
106 for (const auto& flushingPaths : fFlushingPaths) {
107 SkASSERT(flushingPaths->fFlushResources.get() == resources);
108 }
109 #endif
110 return resources;
111 }
112
testingOnly_getPathCache() const113 const GrCCPathCache* GrCoverageCountingPathRenderer::testingOnly_getPathCache() const {
114 return fPathCache.get();
115 }
116
testingOnly_frontCopyAtlasTexture() const117 const GrTexture* GrCCPerFlushResources::testingOnly_frontCopyAtlasTexture() const {
118 if (fCopyAtlasStack.empty()) {
119 return nullptr;
120 }
121 const GrTextureProxy* proxy = fCopyAtlasStack.front().textureProxy();
122 return (proxy) ? proxy->peekTexture() : nullptr;
123 }
124
testingOnly_frontRenderedAtlasTexture() const125 const GrTexture* GrCCPerFlushResources::testingOnly_frontRenderedAtlasTexture() const {
126 if (fRenderedAtlasStack.empty()) {
127 return nullptr;
128 }
129 const GrTextureProxy* proxy = fRenderedAtlasStack.front().textureProxy();
130 return (proxy) ? proxy->peekTexture() : nullptr;
131 }
132
133 const SkTHashTable<GrCCPathCache::HashNode, const GrCCPathCache::Key&>&
testingOnly_getHashTable() const134 GrCCPathCache::testingOnly_getHashTable() const {
135 return fHashTable;
136 }
137
testingOnly_getLRU() const138 const SkTInternalLList<GrCCPathCacheEntry>& GrCCPathCache::testingOnly_getLRU() const {
139 return fLRU;
140 }
141
testingOnly_peekOnFlushRefCnt() const142 int GrCCPathCacheEntry::testingOnly_peekOnFlushRefCnt() const { return fOnFlushRefCnt; }
143
testingOnly_peekOnFlushRefCnt() const144 int GrCCCachedAtlas::testingOnly_peekOnFlushRefCnt() const { return fOnFlushRefCnt; }
145
146 //////////////////////////////////////////////////////////////////////////////
147
148 #define DRAW_OP_TEST_EXTERN(Op) \
149 extern std::unique_ptr<GrDrawOp> Op##__Test(GrPaint&&, SkRandom*, \
150 GrRecordingContext*, int numSamples)
151 #define DRAW_OP_TEST_ENTRY(Op) Op##__Test
152
153 DRAW_OP_TEST_EXTERN(AAConvexPathOp);
154 DRAW_OP_TEST_EXTERN(AAFlatteningConvexPathOp);
155 DRAW_OP_TEST_EXTERN(AAHairlineOp);
156 DRAW_OP_TEST_EXTERN(AAStrokeRectOp);
157 DRAW_OP_TEST_EXTERN(CircleOp);
158 DRAW_OP_TEST_EXTERN(DashOp);
159 DRAW_OP_TEST_EXTERN(DefaultPathOp);
160 DRAW_OP_TEST_EXTERN(DIEllipseOp);
161 DRAW_OP_TEST_EXTERN(EllipseOp);
162 DRAW_OP_TEST_EXTERN(FillRectOp);
163 DRAW_OP_TEST_EXTERN(GrAtlasTextOp);
164 DRAW_OP_TEST_EXTERN(DrawAtlasOp);
165 DRAW_OP_TEST_EXTERN(DrawVerticesOp);
166 DRAW_OP_TEST_EXTERN(NonAALatticeOp);
167 DRAW_OP_TEST_EXTERN(NonAAStrokeRectOp);
168 DRAW_OP_TEST_EXTERN(ShadowRRectOp);
169 DRAW_OP_TEST_EXTERN(SmallPathOp);
170 DRAW_OP_TEST_EXTERN(RegionOp);
171 DRAW_OP_TEST_EXTERN(RRectOp);
172 DRAW_OP_TEST_EXTERN(TesselatingPathOp);
173 DRAW_OP_TEST_EXTERN(TextureOp);
174
GrDrawRandomOp(SkRandom * random,GrRenderTargetContext * renderTargetContext,GrPaint && paint)175 void GrDrawRandomOp(SkRandom* random, GrRenderTargetContext* renderTargetContext, GrPaint&& paint) {
176 auto context = renderTargetContext->surfPriv().getContext();
177 using MakeDrawOpFn = std::unique_ptr<GrDrawOp>(GrPaint&&, SkRandom*,
178 GrRecordingContext*, int numSamples);
179 static constexpr MakeDrawOpFn* gFactories[] = {
180 DRAW_OP_TEST_ENTRY(AAConvexPathOp),
181 DRAW_OP_TEST_ENTRY(AAFlatteningConvexPathOp),
182 DRAW_OP_TEST_ENTRY(AAHairlineOp),
183 DRAW_OP_TEST_ENTRY(AAStrokeRectOp),
184 DRAW_OP_TEST_ENTRY(CircleOp),
185 DRAW_OP_TEST_ENTRY(DashOp),
186 DRAW_OP_TEST_ENTRY(DefaultPathOp),
187 DRAW_OP_TEST_ENTRY(DIEllipseOp),
188 DRAW_OP_TEST_ENTRY(EllipseOp),
189 DRAW_OP_TEST_ENTRY(FillRectOp),
190 DRAW_OP_TEST_ENTRY(GrAtlasTextOp),
191 DRAW_OP_TEST_ENTRY(DrawAtlasOp),
192 DRAW_OP_TEST_ENTRY(DrawVerticesOp),
193 DRAW_OP_TEST_ENTRY(NonAALatticeOp),
194 DRAW_OP_TEST_ENTRY(NonAAStrokeRectOp),
195 DRAW_OP_TEST_ENTRY(ShadowRRectOp),
196 DRAW_OP_TEST_ENTRY(SmallPathOp),
197 DRAW_OP_TEST_ENTRY(RegionOp),
198 DRAW_OP_TEST_ENTRY(RRectOp),
199 DRAW_OP_TEST_ENTRY(TesselatingPathOp),
200 DRAW_OP_TEST_ENTRY(TextureOp),
201 };
202
203 static constexpr size_t kTotal = SK_ARRAY_COUNT(gFactories);
204 uint32_t index = random->nextULessThan(static_cast<uint32_t>(kTotal));
205 auto op = gFactories[index](
206 std::move(paint), random, context, renderTargetContext->numSamples());
207 SkASSERT(op);
208 renderTargetContext->priv().testingOnly_addDrawOp(std::move(op));
209 }
210