xref: /aosp_15_r20/external/skia/tests/TextureOpTest.cpp (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
1 /*
2  * Copyright 2020 Google LLC
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/SkAlphaType.h"
9 #include "include/core/SkBlendMode.h"
10 #include "include/core/SkMatrix.h"
11 #include "include/core/SkRect.h"
12 #include "include/core/SkRefCnt.h"
13 #include "include/core/SkSamplingOptions.h"
14 #include "include/core/SkSize.h"
15 #include "include/core/SkTypes.h"
16 #include "include/gpu/GpuTypes.h"
17 #include "include/gpu/ganesh/GrBackendSurface.h"
18 #include "include/gpu/ganesh/GrDirectContext.h"
19 #include "include/gpu/ganesh/GrRecordingContext.h"
20 #include "include/gpu/ganesh/GrTypes.h"
21 #include "include/private/gpu/ganesh/GrTypesPriv.h"
22 #include "src/base/SkArenaAlloc.h"
23 #include "src/gpu/SkBackingFit.h"
24 #include "src/gpu/Swizzle.h"
25 #include "src/gpu/ganesh/GrAppliedClip.h"
26 #include "src/gpu/ganesh/GrCaps.h"
27 #include "src/gpu/ganesh/GrColorSpaceXform.h"
28 #include "src/gpu/ganesh/GrDirectContextPriv.h"
29 #include "src/gpu/ganesh/GrProcessorSet.h"
30 #include "src/gpu/ganesh/GrProxyProvider.h"
31 #include "src/gpu/ganesh/GrRecordingContextPriv.h"
32 #include "src/gpu/ganesh/GrSamplerState.h"
33 #include "src/gpu/ganesh/GrSurfaceProxy.h"
34 #include "src/gpu/ganesh/GrSurfaceProxyView.h"
35 #include "src/gpu/ganesh/geometry/GrQuad.h"
36 #include "src/gpu/ganesh/ops/GrOp.h"
37 #include "src/gpu/ganesh/ops/OpsTask.h"
38 #include "src/gpu/ganesh/ops/TextureOp.h"
39 #include "tests/CtsEnforcement.h"
40 #include "tests/Test.h"
41 
42 #include <utility>
43 
44 struct GrContextOptions;
45 
46 using namespace skgpu::ganesh;
47 
48 class OpsTaskTestingAccess {
49 public:
50     typedef skgpu::ganesh::OpsTask::OpChain OpChain;
51 };
52 
check_chain(OpsTaskTestingAccess::OpChain * chain,SkRect firstRect,SkRect lastRect,int expectedNumOps)53 static void check_chain(OpsTaskTestingAccess::OpChain* chain, SkRect firstRect, SkRect lastRect,
54                         int expectedNumOps) {
55     int actualNumOps = 0;
56     for (const auto& op : GrOp::ChainRange<>(chain->head())) {
57         ++actualNumOps;
58 
59         if (actualNumOps == 1) {
60             SkAssertResult(op.bounds() == firstRect.makeOutset(0.5f, 0.5f));
61         } else if (actualNumOps == expectedNumOps) {
62             SkAssertResult(op.bounds() == lastRect.makeOutset(0.5f, 0.5f));
63         }
64     }
65 
66     SkAssertResult(actualNumOps == expectedNumOps);
67 }
68 
create_proxy(GrRecordingContext * rContext)69 static sk_sp<GrSurfaceProxy> create_proxy(GrRecordingContext* rContext) {
70     const GrCaps* caps = rContext->priv().caps();
71 
72     static constexpr SkISize kDimensions = {16, 16};
73 
74     const GrBackendFormat format = caps->getDefaultBackendFormat(GrColorType::kRGBA_8888,
75                                                                  GrRenderable::kYes);
76     return rContext->priv().proxyProvider()->createProxy(format,
77                                                          kDimensions,
78                                                          GrRenderable::kYes,
79                                                          1,
80                                                          skgpu::Mipmapped::kNo,
81                                                          SkBackingFit::kExact,
82                                                          skgpu::Budgeted::kNo,
83                                                          GrProtected::kNo,
84                                                          /*label=*/"TextureOpTest",
85                                                          GrInternalSurfaceFlags::kNone);
86 }
87 
create_op(GrDirectContext * dContext,SkRect rect,const GrSurfaceProxyView & proxyView,bool isAA)88 static GrOp::Owner create_op(GrDirectContext* dContext, SkRect rect,
89                              const GrSurfaceProxyView& proxyView, bool isAA) {
90     DrawQuad quad;
91 
92     quad.fDevice = GrQuad::MakeFromRect(rect.makeOutset(0.5f, 0.5f),  SkMatrix::I());
93     quad.fLocal = GrQuad(rect);
94     quad.fEdgeFlags = isAA ? GrQuadAAFlags::kAll : GrQuadAAFlags::kNone;
95 
96     return TextureOp::Make(dContext,
97                            proxyView,
98                            kPremul_SkAlphaType,
99                            nullptr,
100                            GrSamplerState::Filter::kNearest,
101                            GrSamplerState::MipmapMode::kNone,
102                            {1.f, 1.f, 1.f, 1.f},
103                            TextureOp::Saturate::kYes,
104                            SkBlendMode::kSrcOver,
105                            isAA ? GrAAType::kCoverage
106                                 : GrAAType::kNone,
107                            &quad,
108                            nullptr);
109 }
110 
111 // This unit test exercises the crbug.com/1112259 case.
DEF_GANESH_TEST_FOR_RENDERING_CONTEXTS(TextureOpTest,reporter,ctxInfo,CtsEnforcement::kNever)112 DEF_GANESH_TEST_FOR_RENDERING_CONTEXTS(TextureOpTest, reporter, ctxInfo, CtsEnforcement::kNever) {
113     GrDirectContext* dContext = ctxInfo.directContext();
114     const GrCaps* caps = dContext->priv().caps();
115     SkArenaAlloc arena{nullptr, 0, 1024};
116     auto auditTrail = dContext->priv().auditTrail();
117 
118     if (!caps->dynamicStateArrayGeometryProcessorTextureSupport()) {
119         // This test requires chaining
120         return;
121     }
122 
123     GrSurfaceProxyView proxyViewA(create_proxy(dContext),
124                                   kTopLeft_GrSurfaceOrigin,
125                                   skgpu::Swizzle::RGBA());
126     GrSurfaceProxyView proxyViewB(create_proxy(dContext),
127                                   kTopLeft_GrSurfaceOrigin,
128                                   skgpu::Swizzle::RGBA());
129     GrSurfaceProxyView proxyViewC(create_proxy(dContext),
130                                   kTopLeft_GrSurfaceOrigin,
131                                   skgpu::Swizzle::RGBA());
132 
133     static const SkRect kOpARect{  0,  0, 16, 16 };
134     static const SkRect kOpBRect{ 32,  0, 48, 16 };
135     static const SkRect kOpCRect{  0, 32, 16, 48 };
136     static const SkRect kOpDRect{ 32, 32, 48, 48 };
137 
138     // opA & opB can chain together but can't merge bc they have different proxies
139     GrOp::Owner opA = create_op(dContext, kOpARect, proxyViewA, false);
140     GrOp::Owner opB = create_op(dContext, kOpBRect, proxyViewB, false);
141 
142     GrAppliedClip noClip = GrAppliedClip::Disabled();
143     OpsTaskTestingAccess::OpChain chain1(std::move(opA), GrProcessorSet::EmptySetAnalysis(),
144                                          &noClip, nullptr);
145     chain1.appendOp(std::move(opB), GrProcessorSet::EmptySetAnalysis(), nullptr, &noClip, *caps,
146                     &arena, dContext->priv().auditTrail());
147     check_chain(&chain1, kOpARect, kOpBRect, 2);
148 
149     // opC & opD can also chain together but can't merge (bc, again, they have different
150     // proxies). Note, however, that opA and opD do share a proxy so can be merged if opA's
151     // anti-aliasing is upgraded to coverage.
152     GrOp::Owner opC = create_op(dContext, kOpCRect, proxyViewC, true);
153     GrOp::Owner opD = create_op(dContext, kOpDRect, proxyViewA, true);
154 
155     OpsTaskTestingAccess::OpChain chain2(std::move(opC), GrProcessorSet::EmptySetAnalysis(),
156                                          &noClip, nullptr);
157     chain2.appendOp(std::move(opD), GrProcessorSet::EmptySetAnalysis(), nullptr, &noClip, *caps,
158                     &arena, auditTrail);
159     check_chain(&chain2, kOpCRect, kOpDRect, 2);
160 
161     // opA and opD, now in separate chains, can merge when the two chains are combined while
162     // opB and opC can still only chain.
163     chain1.prependChain(&chain2, *caps, &arena, auditTrail);
164 
165     // We should end up with the chain
166     //   opC - opD/opA - opB
167     check_chain(&chain1, kOpCRect, kOpBRect, 3);
168 
169     chain1.deleteOps();
170 }
171