Robert Phillips | e837e61 | 2019-11-15 11:02:50 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2019 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 | */ |
Michael Ludwig | fe13ca3 | 2019-11-21 10:26:41 -0500 | [diff] [blame] | 7 | #include "src/core/SkBlendModePriv.h" |
Weston Tracey | 074414f | 2020-05-29 12:55:06 +0000 | [diff] [blame] | 8 | #include "src/gpu/GrClip.h" |
Robert Phillips | e837e61 | 2019-11-15 11:02:50 -0500 | [diff] [blame] | 9 | #include "src/gpu/GrContextPriv.h" |
Robert Phillips | e19babf | 2020-04-06 13:57:30 -0400 | [diff] [blame] | 10 | #include "src/gpu/GrProxyProvider.h" |
Robert Phillips | e837e61 | 2019-11-15 11:02:50 -0500 | [diff] [blame] | 11 | #include "src/gpu/GrRenderTargetContext.h" |
| 12 | #include "src/gpu/ops/GrFillRectOp.h" |
| 13 | #include "src/gpu/ops/GrTextureOp.h" |
| 14 | #include "tests/Test.h" |
| 15 | |
| 16 | static std::unique_ptr<GrRenderTargetContext> new_RTC(GrContext* context) { |
Greg Daniel | e20fcad | 2020-01-08 11:52:34 -0500 | [diff] [blame] | 17 | return GrRenderTargetContext::Make( |
| 18 | context, GrColorType::kRGBA_8888, nullptr, SkBackingFit::kExact, {128, 128}); |
Robert Phillips | e837e61 | 2019-11-15 11:02:50 -0500 | [diff] [blame] | 19 | } |
| 20 | |
Robert Phillips | e837e61 | 2019-11-15 11:02:50 -0500 | [diff] [blame] | 21 | sk_sp<GrSurfaceProxy> create_proxy(GrContext* context) { |
Brian Salomon | a56a746 | 2020-02-07 14:17:25 -0500 | [diff] [blame] | 22 | static constexpr SkISize kDimensions = {128, 128}; |
Robert Phillips | e837e61 | 2019-11-15 11:02:50 -0500 | [diff] [blame] | 23 | |
| 24 | const GrBackendFormat format = context->priv().caps()->getDefaultBackendFormat( |
| 25 | GrColorType::kRGBA_8888, |
| 26 | GrRenderable::kYes); |
Robert Phillips | e837e61 | 2019-11-15 11:02:50 -0500 | [diff] [blame] | 27 | return context->priv().proxyProvider()->createProxy( |
Brian Salomon | df1bd6d | 2020-03-26 20:37:01 -0400 | [diff] [blame] | 28 | format, kDimensions, GrRenderable::kYes, 1, GrMipMapped::kNo, SkBackingFit::kExact, |
| 29 | SkBudgeted::kNo, GrProtected::kNo, GrInternalSurfaceFlags::kNone); |
Robert Phillips | e837e61 | 2019-11-15 11:02:50 -0500 | [diff] [blame] | 30 | } |
Robert Phillips | e837e61 | 2019-11-15 11:02:50 -0500 | [diff] [blame] | 31 | |
| 32 | typedef GrQuadAAFlags (*PerQuadAAFunc)(int i); |
| 33 | |
| 34 | typedef void (*BulkRectTest)(skiatest::Reporter* reporter, GrContext* context, |
Michael Ludwig | fe13ca3 | 2019-11-21 10:26:41 -0500 | [diff] [blame] | 35 | PerQuadAAFunc perQuadAA, GrAAType overallAA, SkBlendMode blendMode, |
Robert Phillips | e837e61 | 2019-11-15 11:02:50 -0500 | [diff] [blame] | 36 | int requestedTotNumQuads, int expectedNumOps); |
| 37 | |
| 38 | //------------------------------------------------------------------------------------------------- |
| 39 | static void bulk_fill_rect_create_test(skiatest::Reporter* reporter, GrContext* context, |
| 40 | PerQuadAAFunc perQuadAA, GrAAType overallAA, |
Michael Ludwig | fe13ca3 | 2019-11-21 10:26:41 -0500 | [diff] [blame] | 41 | SkBlendMode blendMode, |
Robert Phillips | e837e61 | 2019-11-15 11:02:50 -0500 | [diff] [blame] | 42 | int requestedTotNumQuads, int expectedNumOps) { |
| 43 | |
| 44 | std::unique_ptr<GrRenderTargetContext> rtc = new_RTC(context); |
| 45 | |
| 46 | auto quads = new GrRenderTargetContext::QuadSetEntry[requestedTotNumQuads]; |
| 47 | |
| 48 | for (int i = 0; i < requestedTotNumQuads; ++i) { |
| 49 | quads[i].fRect = SkRect::MakeWH(100.5f, 100.5f); // prevent the int non-AA optimization |
| 50 | quads[i].fColor = SK_PMColor4fWHITE; |
| 51 | quads[i].fLocalMatrix = SkMatrix::I(); |
| 52 | quads[i].fAAFlags = perQuadAA(i); |
| 53 | } |
| 54 | |
| 55 | GrPaint paint; |
Michael Ludwig | fe13ca3 | 2019-11-21 10:26:41 -0500 | [diff] [blame] | 56 | paint.setXPFactory(SkBlendMode_AsXPFactory(blendMode)); |
Weston Tracey | 074414f | 2020-05-29 12:55:06 +0000 | [diff] [blame] | 57 | GrFillRectOp::AddFillRectOps(rtc.get(), GrNoClip(), context, std::move(paint), overallAA, |
Robert Phillips | e837e61 | 2019-11-15 11:02:50 -0500 | [diff] [blame] | 58 | SkMatrix::I(), quads, requestedTotNumQuads); |
| 59 | |
| 60 | GrOpsTask* opsTask = rtc->testingOnly_PeekLastOpsTask(); |
| 61 | int actualNumOps = opsTask->numOpChains(); |
| 62 | |
| 63 | int actualTotNumQuads = 0; |
| 64 | |
| 65 | for (int i = 0; i < actualNumOps; ++i) { |
| 66 | const GrOp* tmp = opsTask->getChain(i); |
| 67 | REPORTER_ASSERT(reporter, tmp->classID() == GrFillRectOp::ClassID()); |
| 68 | REPORTER_ASSERT(reporter, tmp->isChainTail()); |
| 69 | actualTotNumQuads += ((GrDrawOp*) tmp)->numQuads(); |
| 70 | } |
| 71 | |
| 72 | REPORTER_ASSERT(reporter, expectedNumOps == actualNumOps); |
| 73 | REPORTER_ASSERT(reporter, requestedTotNumQuads == actualTotNumQuads); |
| 74 | |
Greg Daniel | 0a2464f | 2020-05-14 15:45:44 -0400 | [diff] [blame] | 75 | context->flushAndSubmit(); |
Robert Phillips | e837e61 | 2019-11-15 11:02:50 -0500 | [diff] [blame] | 76 | |
| 77 | delete[] quads; |
| 78 | } |
| 79 | |
Robert Phillips | e837e61 | 2019-11-15 11:02:50 -0500 | [diff] [blame] | 80 | //------------------------------------------------------------------------------------------------- |
| 81 | static void bulk_texture_rect_create_test(skiatest::Reporter* reporter, GrContext* context, |
| 82 | PerQuadAAFunc perQuadAA, GrAAType overallAA, |
Michael Ludwig | fe13ca3 | 2019-11-21 10:26:41 -0500 | [diff] [blame] | 83 | SkBlendMode blendMode, |
Robert Phillips | e837e61 | 2019-11-15 11:02:50 -0500 | [diff] [blame] | 84 | int requestedTotNumQuads, int expectedNumOps) { |
| 85 | |
| 86 | std::unique_ptr<GrRenderTargetContext> rtc = new_RTC(context); |
| 87 | |
Michael Ludwig | fe13ca3 | 2019-11-21 10:26:41 -0500 | [diff] [blame] | 88 | sk_sp<GrSurfaceProxy> proxyA = create_proxy(context); |
| 89 | sk_sp<GrSurfaceProxy> proxyB = create_proxy(context); |
| 90 | GrSurfaceProxyView proxyViewA(std::move(proxyA), kTopLeft_GrSurfaceOrigin, GrSwizzle::RGBA()); |
| 91 | GrSurfaceProxyView proxyViewB(std::move(proxyB), kTopLeft_GrSurfaceOrigin, GrSwizzle::RGBA()); |
Robert Phillips | e837e61 | 2019-11-15 11:02:50 -0500 | [diff] [blame] | 92 | |
| 93 | auto set = new GrRenderTargetContext::TextureSetEntry[requestedTotNumQuads]; |
| 94 | |
| 95 | for (int i = 0; i < requestedTotNumQuads; ++i) { |
Michael Ludwig | fe13ca3 | 2019-11-21 10:26:41 -0500 | [diff] [blame] | 96 | // Alternate between two proxies to prevent op merging if the batch API was forced to submit |
| 97 | // one op at a time (to work, this does require that all fDstRects overlap). |
| 98 | set[i].fProxyView = i % 2 == 0 ? proxyViewA : proxyViewB; |
Brian Salomon | fc11844 | 2019-11-22 19:09:27 -0500 | [diff] [blame] | 99 | set[i].fSrcAlphaType = kPremul_SkAlphaType; |
Robert Phillips | e837e61 | 2019-11-15 11:02:50 -0500 | [diff] [blame] | 100 | set[i].fSrcRect = SkRect::MakeWH(100.0f, 100.0f); |
| 101 | set[i].fDstRect = SkRect::MakeWH(100.5f, 100.5f); // prevent the int non-AA optimization |
| 102 | set[i].fDstClipQuad = nullptr; |
| 103 | set[i].fPreViewMatrix = nullptr; |
| 104 | set[i].fAlpha = 1.0f; |
| 105 | set[i].fAAFlags = perQuadAA(i); |
| 106 | } |
| 107 | |
Weston Tracey | 074414f | 2020-05-29 12:55:06 +0000 | [diff] [blame] | 108 | GrTextureOp::AddTextureSetOps(rtc.get(), GrNoClip(), context, set, requestedTotNumQuads, |
Michael Ludwig | 379e496 | 2019-12-06 13:21:26 -0500 | [diff] [blame] | 109 | requestedTotNumQuads, // We alternate so proxyCnt == cnt |
| 110 | GrSamplerState::Filter::kNearest, |
| 111 | GrTextureOp::Saturate::kYes, |
| 112 | blendMode, |
| 113 | overallAA, |
| 114 | SkCanvas::kStrict_SrcRectConstraint, |
| 115 | SkMatrix::I(), nullptr); |
Robert Phillips | e837e61 | 2019-11-15 11:02:50 -0500 | [diff] [blame] | 116 | |
| 117 | GrOpsTask* opsTask = rtc->testingOnly_PeekLastOpsTask(); |
| 118 | int actualNumOps = opsTask->numOpChains(); |
| 119 | |
| 120 | int actualTotNumQuads = 0; |
| 121 | |
Michael Ludwig | fe13ca3 | 2019-11-21 10:26:41 -0500 | [diff] [blame] | 122 | if (blendMode != SkBlendMode::kSrcOver || |
| 123 | !context->priv().caps()->dynamicStateArrayGeometryProcessorTextureSupport()) { |
| 124 | // In either of these two cases, GrTextureOp creates one op per quad instead. Since |
| 125 | // each entry alternates proxies but overlaps geometrically, this will prevent the ops |
| 126 | // from being merged back into fewer ops. |
| 127 | expectedNumOps = requestedTotNumQuads; |
| 128 | } |
| 129 | uint32_t expectedOpID = blendMode == SkBlendMode::kSrcOver ? GrTextureOp::ClassID() |
| 130 | : GrFillRectOp::ClassID(); |
Robert Phillips | e837e61 | 2019-11-15 11:02:50 -0500 | [diff] [blame] | 131 | for (int i = 0; i < actualNumOps; ++i) { |
| 132 | const GrOp* tmp = opsTask->getChain(i); |
Michael Ludwig | fe13ca3 | 2019-11-21 10:26:41 -0500 | [diff] [blame] | 133 | REPORTER_ASSERT(reporter, tmp->classID() == expectedOpID); |
Robert Phillips | e837e61 | 2019-11-15 11:02:50 -0500 | [diff] [blame] | 134 | REPORTER_ASSERT(reporter, tmp->isChainTail()); |
| 135 | actualTotNumQuads += ((GrDrawOp*) tmp)->numQuads(); |
| 136 | } |
| 137 | |
| 138 | REPORTER_ASSERT(reporter, expectedNumOps == actualNumOps); |
| 139 | REPORTER_ASSERT(reporter, requestedTotNumQuads == actualTotNumQuads); |
| 140 | |
Greg Daniel | 0a2464f | 2020-05-14 15:45:44 -0400 | [diff] [blame] | 141 | context->flushAndSubmit(); |
Robert Phillips | e837e61 | 2019-11-15 11:02:50 -0500 | [diff] [blame] | 142 | |
| 143 | delete[] set; |
| 144 | } |
Robert Phillips | e837e61 | 2019-11-15 11:02:50 -0500 | [diff] [blame] | 145 | |
| 146 | //------------------------------------------------------------------------------------------------- |
| 147 | static void run_test(GrContext* context, skiatest::Reporter* reporter, BulkRectTest test) { |
Robert Phillips | e837e61 | 2019-11-15 11:02:50 -0500 | [diff] [blame] | 148 | // This is the simple case where there is no AA at all. We expect 2 non-AA clumps of quads. |
| 149 | { |
| 150 | auto noAA = [](int i) -> GrQuadAAFlags { |
| 151 | return GrQuadAAFlags::kNone; |
| 152 | }; |
| 153 | |
| 154 | static const int kNumExpectedOps = 2; |
| 155 | |
Michael Ludwig | fe13ca3 | 2019-11-21 10:26:41 -0500 | [diff] [blame] | 156 | test(reporter, context, noAA, GrAAType::kNone, SkBlendMode::kSrcOver, |
Robert Phillips | e837e61 | 2019-11-15 11:02:50 -0500 | [diff] [blame] | 157 | 2*GrResourceProvider::MaxNumNonAAQuads(), kNumExpectedOps); |
| 158 | } |
| 159 | |
| 160 | // This is the same as the above case except the overall AA is kCoverage. However, since |
| 161 | // the per-quad AA is still none, all the quads should be downgraded to non-AA. |
| 162 | { |
| 163 | auto noAA = [](int i) -> GrQuadAAFlags { |
| 164 | return GrQuadAAFlags::kNone; |
| 165 | }; |
| 166 | |
| 167 | static const int kNumExpectedOps = 2; |
| 168 | |
Michael Ludwig | fe13ca3 | 2019-11-21 10:26:41 -0500 | [diff] [blame] | 169 | test(reporter, context, noAA, GrAAType::kCoverage, SkBlendMode::kSrcOver, |
Robert Phillips | e837e61 | 2019-11-15 11:02:50 -0500 | [diff] [blame] | 170 | 2*GrResourceProvider::MaxNumNonAAQuads(), kNumExpectedOps); |
| 171 | } |
| 172 | |
| 173 | // This case has an overall AA of kCoverage but the per-quad AA alternates. |
| 174 | // We should end up with several aa-sized clumps |
| 175 | { |
| 176 | auto alternateAA = [](int i) -> GrQuadAAFlags { |
| 177 | return (i % 2) ? GrQuadAAFlags::kAll : GrQuadAAFlags::kNone; |
| 178 | }; |
| 179 | |
| 180 | int numExpectedOps = 2*GrResourceProvider::MaxNumNonAAQuads() / |
| 181 | GrResourceProvider::MaxNumAAQuads(); |
| 182 | |
Michael Ludwig | fe13ca3 | 2019-11-21 10:26:41 -0500 | [diff] [blame] | 183 | test(reporter, context, alternateAA, GrAAType::kCoverage, SkBlendMode::kSrcOver, |
Robert Phillips | e837e61 | 2019-11-15 11:02:50 -0500 | [diff] [blame] | 184 | 2*GrResourceProvider::MaxNumNonAAQuads(), numExpectedOps); |
| 185 | } |
| 186 | |
| 187 | // In this case we have a run of MaxNumAAQuads non-AA quads and then AA quads. This |
| 188 | // exercises the case where we have a clump of quads that can't be upgraded to AA bc of |
| 189 | // its size. We expect one clump of non-AA quads followed by one clump of AA quads. |
| 190 | { |
| 191 | auto runOfNonAA = [](int i) -> GrQuadAAFlags { |
| 192 | return (i < GrResourceProvider::MaxNumAAQuads()) ? GrQuadAAFlags::kNone |
| 193 | : GrQuadAAFlags::kAll; |
| 194 | }; |
| 195 | |
| 196 | static const int kNumExpectedOps = 2; |
| 197 | |
Michael Ludwig | fe13ca3 | 2019-11-21 10:26:41 -0500 | [diff] [blame] | 198 | test(reporter, context, runOfNonAA, GrAAType::kCoverage, SkBlendMode::kSrcOver, |
| 199 | 2*GrResourceProvider::MaxNumAAQuads(), kNumExpectedOps); |
| 200 | } |
| 201 | |
| 202 | // In this case we use a blend mode other than src-over, which hits the GrFillRectOp fallback |
| 203 | // code path for GrTextureOp. We pass in the expected results if batching was successful, to |
| 204 | // that bulk_fill_rect_create_test batches on all modes; bulk_texture_rect_create_test is |
| 205 | // responsible for revising its expectations. |
| 206 | { |
| 207 | auto fixedAA = [](int i) -> GrQuadAAFlags { |
| 208 | return GrQuadAAFlags::kAll; |
| 209 | }; |
| 210 | |
| 211 | static const int kNumExpectedOps = 2; |
| 212 | |
| 213 | test(reporter, context, fixedAA, GrAAType::kCoverage, SkBlendMode::kSrcATop, |
Robert Phillips | e837e61 | 2019-11-15 11:02:50 -0500 | [diff] [blame] | 214 | 2*GrResourceProvider::MaxNumAAQuads(), kNumExpectedOps); |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(BulkFillRectTest, reporter, ctxInfo) { |
| 219 | run_test(ctxInfo.grContext(), reporter, bulk_fill_rect_create_test); |
| 220 | } |
| 221 | |
Robert Phillips | e837e61 | 2019-11-15 11:02:50 -0500 | [diff] [blame] | 222 | DEF_GPUTEST_FOR_RENDERING_CONTEXTS(BulkTextureRectTest, reporter, ctxInfo) { |
| 223 | run_test(ctxInfo.grContext(), reporter, bulk_texture_rect_create_test); |
| 224 | } |