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