Robert Phillips | eb35f4d | 2017-03-21 07:56:47 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017 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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "tests/Test.h" |
Robert Phillips | eb35f4d | 2017-03-21 07:56:47 -0400 | [diff] [blame] | 9 | |
Michael Ludwig | fd4f4df | 2019-05-29 09:51:09 -0400 | [diff] [blame] | 10 | #include "include/core/SkBitmap.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 11 | #include "include/gpu/GrBackendSemaphore.h" |
Michael Ludwig | fd4f4df | 2019-05-29 09:51:09 -0400 | [diff] [blame] | 12 | #include "src/core/SkPointPriv.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 13 | #include "src/gpu/GrContextPriv.h" |
| 14 | #include "src/gpu/GrDefaultGeoProcFactory.h" |
Brian Salomon | f2ebdd9 | 2019-09-30 12:15:30 -0400 | [diff] [blame] | 15 | #include "src/gpu/GrImageInfo.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 16 | #include "src/gpu/GrOnFlushResourceProvider.h" |
Robert Phillips | d2f1873 | 2020-03-04 16:12:08 -0500 | [diff] [blame] | 17 | #include "src/gpu/GrProgramInfo.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 18 | #include "src/gpu/GrProxyProvider.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 19 | #include "src/gpu/GrRenderTargetContextPriv.h" |
| 20 | #include "src/gpu/GrResourceProvider.h" |
Greg Daniel | 456f9b5 | 2020-03-05 19:14:18 +0000 | [diff] [blame] | 21 | #include "src/gpu/GrTexture.h" |
Brian Salomon | b8f098d | 2020-01-07 11:15:44 -0500 | [diff] [blame] | 22 | #include "src/gpu/effects/GrTextureEffect.h" |
Michael Ludwig | fd4f4df | 2019-05-29 09:51:09 -0400 | [diff] [blame] | 23 | #include "src/gpu/geometry/GrQuad.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 24 | #include "src/gpu/ops/GrSimpleMeshDrawOpHelper.h" |
Brian Salomon | 557e812 | 2019-10-24 10:37:08 -0400 | [diff] [blame] | 25 | #include "tests/TestUtils.h" |
Robert Phillips | eb35f4d | 2017-03-21 07:56:47 -0400 | [diff] [blame] | 26 | |
Brian Salomon | 9a03642 | 2017-07-13 17:04:43 -0400 | [diff] [blame] | 27 | namespace { |
Robert Phillips | eb35f4d | 2017-03-21 07:56:47 -0400 | [diff] [blame] | 28 | // This is a simplified mesh drawing op that can be used in the atlas generation test. |
| 29 | // Please see AtlasedRectOp below. |
Brian Salomon | 9a03642 | 2017-07-13 17:04:43 -0400 | [diff] [blame] | 30 | class NonAARectOp : public GrMeshDrawOp { |
| 31 | protected: |
| 32 | using Helper = GrSimpleMeshDrawOpHelper; |
| 33 | |
Robert Phillips | eb35f4d | 2017-03-21 07:56:47 -0400 | [diff] [blame] | 34 | public: |
| 35 | DEFINE_OP_CLASS_ID |
Robert Phillips | eb35f4d | 2017-03-21 07:56:47 -0400 | [diff] [blame] | 36 | |
| 37 | // This creates an instance of a simple non-AA solid color rect-drawing Op |
Robert Phillips | b97da53 | 2019-02-12 15:24:12 -0500 | [diff] [blame] | 38 | static std::unique_ptr<GrDrawOp> Make(GrRecordingContext* context, |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 39 | GrPaint&& paint, |
| 40 | const SkRect& r) { |
| 41 | return Helper::FactoryHelper<NonAARectOp>(context, std::move(paint), r, nullptr, ClassID()); |
Robert Phillips | eb35f4d | 2017-03-21 07:56:47 -0400 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | // This creates an instance of a simple non-AA textured rect-drawing Op |
Robert Phillips | b97da53 | 2019-02-12 15:24:12 -0500 | [diff] [blame] | 45 | static std::unique_ptr<GrDrawOp> Make(GrRecordingContext* context, |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 46 | GrPaint&& paint, |
| 47 | const SkRect& r, |
| 48 | const SkRect& local) { |
| 49 | return Helper::FactoryHelper<NonAARectOp>(context, std::move(paint), r, &local, ClassID()); |
Robert Phillips | eb35f4d | 2017-03-21 07:56:47 -0400 | [diff] [blame] | 50 | } |
| 51 | |
Brian Osman | cf86085 | 2018-10-31 14:04:39 -0400 | [diff] [blame] | 52 | const SkPMColor4f& color() const { return fColor; } |
Robert Phillips | eb35f4d | 2017-03-21 07:56:47 -0400 | [diff] [blame] | 53 | |
Brian Osman | cf86085 | 2018-10-31 14:04:39 -0400 | [diff] [blame] | 54 | NonAARectOp(const Helper::MakeArgs& helperArgs, const SkPMColor4f& color, const SkRect& r, |
Brian Salomon | 9a03642 | 2017-07-13 17:04:43 -0400 | [diff] [blame] | 55 | const SkRect* localRect, int32_t classID) |
| 56 | : INHERITED(classID) |
| 57 | , fColor(color) |
| 58 | , fHasLocalRect(SkToBool(localRect)) |
| 59 | , fRect(r) |
| 60 | , fHelper(helperArgs, GrAAType::kNone) { |
| 61 | if (fHasLocalRect) { |
Brian Salomon | a33b67c | 2018-05-17 10:42:14 -0400 | [diff] [blame] | 62 | fLocalQuad = GrQuad(*localRect); |
Brian Salomon | 9a03642 | 2017-07-13 17:04:43 -0400 | [diff] [blame] | 63 | } |
| 64 | // Choose some conservative values for aa bloat and zero area. |
Greg Daniel | 5faf474 | 2019-10-01 15:14:44 -0400 | [diff] [blame] | 65 | this->setBounds(r, HasAABloat::kYes, IsHairline::kYes); |
Brian Salomon | 9a03642 | 2017-07-13 17:04:43 -0400 | [diff] [blame] | 66 | } |
| 67 | |
Robert Phillips | b493eeb | 2017-09-13 13:10:52 -0400 | [diff] [blame] | 68 | const char* name() const override { return "NonAARectOp"; } |
| 69 | |
Chris Dalton | 1706cbf | 2019-05-21 19:35:29 -0600 | [diff] [blame] | 70 | void visitProxies(const VisitProxyFunc& func) const override { |
Robert Phillips | d2f1873 | 2020-03-04 16:12:08 -0500 | [diff] [blame] | 71 | if (fProgramInfo) { |
Chris Dalton | be45742 | 2020-03-16 18:05:03 -0600 | [diff] [blame] | 72 | fProgramInfo->visitFPProxies(func); |
Robert Phillips | d2f1873 | 2020-03-04 16:12:08 -0500 | [diff] [blame] | 73 | } else { |
| 74 | fHelper.visitProxies(func); |
| 75 | } |
Robert Phillips | b493eeb | 2017-09-13 13:10:52 -0400 | [diff] [blame] | 76 | } |
| 77 | |
Brian Salomon | 9a03642 | 2017-07-13 17:04:43 -0400 | [diff] [blame] | 78 | FixedFunctionFlags fixedFunctionFlags() const override { return FixedFunctionFlags::kNone; } |
| 79 | |
Chris Dalton | 6ce447a | 2019-06-23 18:07:38 -0600 | [diff] [blame] | 80 | GrProcessorSet::Analysis finalize( |
| 81 | const GrCaps& caps, const GrAppliedClip*, bool hasMixedSampledCoverage, |
| 82 | GrClampType clampType) override { |
Brian Salomon | 9a03642 | 2017-07-13 17:04:43 -0400 | [diff] [blame] | 83 | // Set the color to unknown because the subclass may change the color later. |
| 84 | GrProcessorAnalysisColor gpColor; |
| 85 | gpColor.setToUnknown(); |
| 86 | // We ignore the clip so pass this rather than the GrAppliedClip param. |
Michael Ludwig | d1d997e | 2020-06-04 15:52:44 -0400 | [diff] [blame] | 87 | static GrAppliedClip kNoClip = GrAppliedClip::Disabled(); |
Chris Dalton | 6ce447a | 2019-06-23 18:07:38 -0600 | [diff] [blame] | 88 | return fHelper.finalizeProcessors(caps, &kNoClip, hasMixedSampledCoverage, clampType, |
| 89 | GrProcessorAnalysisCoverage::kNone, &gpColor); |
Brian Salomon | 9a03642 | 2017-07-13 17:04:43 -0400 | [diff] [blame] | 90 | } |
| 91 | |
Robert Phillips | eb35f4d | 2017-03-21 07:56:47 -0400 | [diff] [blame] | 92 | protected: |
Brian Osman | cf86085 | 2018-10-31 14:04:39 -0400 | [diff] [blame] | 93 | SkPMColor4f fColor; |
| 94 | bool fHasLocalRect; |
| 95 | GrQuad fLocalQuad; |
| 96 | SkRect fRect; |
Robert Phillips | eb35f4d | 2017-03-21 07:56:47 -0400 | [diff] [blame] | 97 | |
| 98 | private: |
Robert Phillips | 2669a7b | 2020-03-12 12:07:19 -0400 | [diff] [blame] | 99 | GrProgramInfo* programInfo() override { return fProgramInfo; } |
| 100 | |
Robert Phillips | 4133dc4 | 2020-03-11 15:55:55 -0400 | [diff] [blame] | 101 | void onCreateProgramInfo(const GrCaps* caps, |
| 102 | SkArenaAlloc* arena, |
Brian Salomon | 8afde5f | 2020-04-01 16:22:00 -0400 | [diff] [blame] | 103 | const GrSurfaceProxyView* writeView, |
Robert Phillips | 4133dc4 | 2020-03-11 15:55:55 -0400 | [diff] [blame] | 104 | GrAppliedClip&& appliedClip, |
| 105 | const GrXferProcessor::DstProxyView& dstProxyView) override { |
Robert Phillips | eb35f4d | 2017-03-21 07:56:47 -0400 | [diff] [blame] | 106 | using namespace GrDefaultGeoProcFactory; |
| 107 | |
Robert Phillips | 7cd0bfe | 2019-11-20 16:08:10 -0500 | [diff] [blame] | 108 | GrGeometryProcessor* gp = GrDefaultGeoProcFactory::Make( |
Robert Phillips | d2f1873 | 2020-03-04 16:12:08 -0500 | [diff] [blame] | 109 | arena, |
Ruiqi Mao | b609e6d | 2018-07-17 10:19:38 -0400 | [diff] [blame] | 110 | Color::kPremulGrColorAttribute_Type, |
Robert Phillips | eb35f4d | 2017-03-21 07:56:47 -0400 | [diff] [blame] | 111 | Coverage::kSolid_Type, |
| 112 | fHasLocalRect ? LocalCoords::kHasExplicit_Type |
| 113 | : LocalCoords::kUnused_Type, |
| 114 | SkMatrix::I()); |
| 115 | if (!gp) { |
Robert Phillips | d2f1873 | 2020-03-04 16:12:08 -0500 | [diff] [blame] | 116 | SkDebugf("Couldn't create GrGeometryProcessor\n"); |
Robert Phillips | 4133dc4 | 2020-03-11 15:55:55 -0400 | [diff] [blame] | 117 | return; |
Robert Phillips | eb35f4d | 2017-03-21 07:56:47 -0400 | [diff] [blame] | 118 | } |
| 119 | |
Brian Salomon | 8afde5f | 2020-04-01 16:22:00 -0400 | [diff] [blame] | 120 | fProgramInfo = fHelper.createProgramInfo(caps, arena, writeView, std::move(appliedClip), |
Robert Phillips | 4133dc4 | 2020-03-11 15:55:55 -0400 | [diff] [blame] | 121 | dstProxyView, gp, GrPrimitiveType::kTriangles); |
Robert Phillips | d2f1873 | 2020-03-04 16:12:08 -0500 | [diff] [blame] | 122 | } |
| 123 | |
Robert Phillips | d2f1873 | 2020-03-04 16:12:08 -0500 | [diff] [blame] | 124 | void onPrepareDraws(Target* target) override { |
| 125 | |
| 126 | // The vertex attrib order is always pos, color, local coords. |
| 127 | static const int kColorOffset = sizeof(SkPoint); |
| 128 | static const int kLocalOffset = sizeof(SkPoint) + sizeof(GrColor); |
| 129 | |
| 130 | if (!fProgramInfo) { |
Robert Phillips | 4133dc4 | 2020-03-11 15:55:55 -0400 | [diff] [blame] | 131 | this->createProgramInfo(target); |
Robert Phillips | d2f1873 | 2020-03-04 16:12:08 -0500 | [diff] [blame] | 132 | if (!fProgramInfo) { |
| 133 | return; |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | size_t vertexStride = fProgramInfo->primProc().vertexStride(); |
Robert Phillips | eb35f4d | 2017-03-21 07:56:47 -0400 | [diff] [blame] | 138 | |
Brian Salomon | 12d2264 | 2019-01-29 14:38:50 -0500 | [diff] [blame] | 139 | sk_sp<const GrBuffer> indexBuffer; |
Robert Phillips | eb35f4d | 2017-03-21 07:56:47 -0400 | [diff] [blame] | 140 | int firstIndex; |
| 141 | uint16_t* indices = target->makeIndexSpace(6, &indexBuffer, &firstIndex); |
| 142 | if (!indices) { |
| 143 | SkDebugf("Indices could not be allocated for GrAtlasedOp.\n"); |
| 144 | return; |
| 145 | } |
| 146 | |
Brian Salomon | 12d2264 | 2019-01-29 14:38:50 -0500 | [diff] [blame] | 147 | sk_sp<const GrBuffer> vertexBuffer; |
Robert Phillips | eb35f4d | 2017-03-21 07:56:47 -0400 | [diff] [blame] | 148 | int firstVertex; |
| 149 | void* vertices = target->makeVertexSpace(vertexStride, 4, &vertexBuffer, &firstVertex); |
| 150 | if (!vertices) { |
| 151 | SkDebugf("Vertices could not be allocated for GrAtlasedOp.\n"); |
| 152 | return; |
| 153 | } |
| 154 | |
| 155 | // Setup indices |
| 156 | indices[0] = 0; |
| 157 | indices[1] = 1; |
| 158 | indices[2] = 2; |
Brian Salomon | 57caa66 | 2017-10-18 12:21:05 +0000 | [diff] [blame] | 159 | indices[3] = 2; |
| 160 | indices[4] = 1; |
Robert Phillips | eb35f4d | 2017-03-21 07:56:47 -0400 | [diff] [blame] | 161 | indices[5] = 3; |
| 162 | |
| 163 | // Setup positions |
| 164 | SkPoint* position = (SkPoint*) vertices; |
Brian Salomon | ec42e15 | 2018-05-18 12:52:22 -0400 | [diff] [blame] | 165 | SkPointPriv::SetRectTriStrip(position, fRect, vertexStride); |
Robert Phillips | eb35f4d | 2017-03-21 07:56:47 -0400 | [diff] [blame] | 166 | |
| 167 | // Setup vertex colors |
| 168 | GrColor* color = (GrColor*)((intptr_t)vertices + kColorOffset); |
| 169 | for (int i = 0; i < 4; ++i) { |
Brian Osman | cf86085 | 2018-10-31 14:04:39 -0400 | [diff] [blame] | 170 | *color = fColor.toBytes_RGBA(); |
Robert Phillips | eb35f4d | 2017-03-21 07:56:47 -0400 | [diff] [blame] | 171 | color = (GrColor*)((intptr_t)color + vertexStride); |
| 172 | } |
| 173 | |
| 174 | // Setup local coords |
| 175 | if (fHasLocalRect) { |
| 176 | SkPoint* coords = (SkPoint*)((intptr_t) vertices + kLocalOffset); |
| 177 | for (int i = 0; i < 4; i++) { |
| 178 | *coords = fLocalQuad.point(i); |
| 179 | coords = (SkPoint*)((intptr_t) coords + vertexStride); |
| 180 | } |
| 181 | } |
| 182 | |
Robert Phillips | d2f1873 | 2020-03-04 16:12:08 -0500 | [diff] [blame] | 183 | fMesh = target->allocMesh(); |
Chris Dalton | 37c7bdd | 2020-03-13 09:21:12 -0600 | [diff] [blame] | 184 | fMesh->setIndexed(indexBuffer, 6, firstIndex, 0, 3, GrPrimitiveRestart::kNo, vertexBuffer, |
| 185 | firstVertex); |
Chris Dalton | 07cdcfc9 | 2019-02-26 11:13:22 -0700 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | void onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) override { |
Robert Phillips | d2f1873 | 2020-03-04 16:12:08 -0500 | [diff] [blame] | 189 | if (!fProgramInfo || !fMesh) { |
| 190 | return; |
| 191 | } |
Robert Phillips | 3968fcb | 2019-12-05 16:40:31 -0500 | [diff] [blame] | 192 | |
Chris Dalton | 765ed36 | 2020-03-16 17:34:44 -0600 | [diff] [blame] | 193 | flushState->bindPipelineAndScissorClip(*fProgramInfo, chainBounds); |
| 194 | flushState->bindTextures(fProgramInfo->primProc(), nullptr, fProgramInfo->pipeline()); |
| 195 | flushState->drawMesh(*fMesh); |
Robert Phillips | eb35f4d | 2017-03-21 07:56:47 -0400 | [diff] [blame] | 196 | } |
| 197 | |
Robert Phillips | d2f1873 | 2020-03-04 16:12:08 -0500 | [diff] [blame] | 198 | Helper fHelper; |
Chris Dalton | eb694b7 | 2020-03-16 09:25:50 -0600 | [diff] [blame] | 199 | GrSimpleMesh* fMesh = nullptr; |
Robert Phillips | d2f1873 | 2020-03-04 16:12:08 -0500 | [diff] [blame] | 200 | GrProgramInfo* fProgramInfo = nullptr; |
Brian Salomon | 9a03642 | 2017-07-13 17:04:43 -0400 | [diff] [blame] | 201 | |
| 202 | typedef GrMeshDrawOp INHERITED; |
Robert Phillips | eb35f4d | 2017-03-21 07:56:47 -0400 | [diff] [blame] | 203 | }; |
| 204 | |
Brian Salomon | 9a03642 | 2017-07-13 17:04:43 -0400 | [diff] [blame] | 205 | } // anonymous namespace |
| 206 | |
Brian Salomon | 9a03642 | 2017-07-13 17:04:43 -0400 | [diff] [blame] | 207 | static constexpr SkRect kEmptyRect = SkRect::MakeEmpty(); |
| 208 | |
| 209 | namespace { |
| 210 | |
Robert Phillips | eb35f4d | 2017-03-21 07:56:47 -0400 | [diff] [blame] | 211 | /* |
| 212 | * Atlased ops just draw themselves as textured rects with the texture pixels being |
| 213 | * pulled out of the atlas. Their color is based on their ID. |
| 214 | */ |
| 215 | class AtlasedRectOp final : public NonAARectOp { |
| 216 | public: |
| 217 | DEFINE_OP_CLASS_ID |
| 218 | |
| 219 | ~AtlasedRectOp() override { |
| 220 | fID = -1; |
| 221 | } |
| 222 | |
| 223 | const char* name() const override { return "AtlasedRectOp"; } |
| 224 | |
| 225 | int id() const { return fID; } |
| 226 | |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 227 | static std::unique_ptr<AtlasedRectOp> Make(GrContext* context, |
| 228 | GrPaint&& paint, |
| 229 | const SkRect& r, |
| 230 | int id) { |
| 231 | GrDrawOp* op = Helper::FactoryHelper<AtlasedRectOp>(context, std::move(paint), |
| 232 | r, id).release(); |
Brian Salomon | 9a03642 | 2017-07-13 17:04:43 -0400 | [diff] [blame] | 233 | return std::unique_ptr<AtlasedRectOp>(static_cast<AtlasedRectOp*>(op)); |
| 234 | } |
| 235 | |
| 236 | // We set the initial color of the NonAARectOp based on the ID. |
| 237 | // Note that we force creation of a NonAARectOp that has local coords in anticipation of |
| 238 | // pulling from the atlas. |
Brian Osman | cf86085 | 2018-10-31 14:04:39 -0400 | [diff] [blame] | 239 | AtlasedRectOp(const Helper::MakeArgs& helperArgs, const SkPMColor4f& color, const SkRect& r, |
Brian Osman | 936fe7d | 2018-10-30 15:30:35 -0400 | [diff] [blame] | 240 | int id) |
Brian Osman | cf86085 | 2018-10-31 14:04:39 -0400 | [diff] [blame] | 241 | : INHERITED(helperArgs, SkPMColor4f::FromBytes_RGBA(kColors[id]), r, &kEmptyRect, |
| 242 | ClassID()) |
Brian Salomon | 9a03642 | 2017-07-13 17:04:43 -0400 | [diff] [blame] | 243 | , fID(id) |
| 244 | , fNext(nullptr) { |
| 245 | SkASSERT(fID < kMaxIDs); |
Robert Phillips | eb35f4d | 2017-03-21 07:56:47 -0400 | [diff] [blame] | 246 | } |
| 247 | |
Brian Osman | cf86085 | 2018-10-31 14:04:39 -0400 | [diff] [blame] | 248 | void setColor(const SkPMColor4f& color) { fColor = color; } |
Robert Phillips | eb35f4d | 2017-03-21 07:56:47 -0400 | [diff] [blame] | 249 | void setLocalRect(const SkRect& localRect) { |
| 250 | SkASSERT(fHasLocalRect); // This should've been created to anticipate this |
Brian Salomon | a33b67c | 2018-05-17 10:42:14 -0400 | [diff] [blame] | 251 | fLocalQuad = GrQuad(localRect); |
Robert Phillips | eb35f4d | 2017-03-21 07:56:47 -0400 | [diff] [blame] | 252 | } |
| 253 | |
| 254 | AtlasedRectOp* next() const { return fNext; } |
| 255 | void setNext(AtlasedRectOp* next) { |
| 256 | fNext = next; |
| 257 | } |
| 258 | |
| 259 | private: |
Robert Phillips | eb35f4d | 2017-03-21 07:56:47 -0400 | [diff] [blame] | 260 | |
| 261 | static const int kMaxIDs = 9; |
Brian Osman | 1be2b7c | 2018-10-29 16:07:15 -0400 | [diff] [blame] | 262 | static const GrColor kColors[kMaxIDs]; |
Robert Phillips | eb35f4d | 2017-03-21 07:56:47 -0400 | [diff] [blame] | 263 | |
| 264 | int fID; |
Greg Daniel | f41b2bd | 2019-08-22 16:19:24 -0400 | [diff] [blame] | 265 | // The Atlased ops have an internal singly-linked list of ops that land in the same opsTask |
Robert Phillips | eb35f4d | 2017-03-21 07:56:47 -0400 | [diff] [blame] | 266 | AtlasedRectOp* fNext; |
| 267 | |
| 268 | typedef NonAARectOp INHERITED; |
| 269 | }; |
| 270 | |
Brian Salomon | 9a03642 | 2017-07-13 17:04:43 -0400 | [diff] [blame] | 271 | } // anonymous namespace |
| 272 | |
Robert Phillips | eb35f4d | 2017-03-21 07:56:47 -0400 | [diff] [blame] | 273 | const GrColor AtlasedRectOp::kColors[kMaxIDs] = { |
| 274 | GrColorPackRGBA(255, 0, 0, 255), |
| 275 | GrColorPackRGBA(0, 255, 0, 255), |
| 276 | GrColorPackRGBA(0, 0, 255, 255), |
| 277 | GrColorPackRGBA(0, 255, 255, 255), |
| 278 | GrColorPackRGBA(255, 0, 255, 255), |
| 279 | GrColorPackRGBA(255, 255, 0, 255), |
| 280 | GrColorPackRGBA(0, 0, 0, 255), |
| 281 | GrColorPackRGBA(128, 128, 128, 255), |
| 282 | GrColorPackRGBA(255, 255, 255, 255) |
| 283 | }; |
| 284 | |
| 285 | static const int kDrawnTileSize = 16; |
| 286 | |
| 287 | /* |
| 288 | * Rather than performing any rect packing, this atlaser just lays out constant-sized |
| 289 | * tiles in an Nx1 row |
| 290 | */ |
| 291 | static const int kAtlasTileSize = 2; |
| 292 | |
| 293 | /* |
| 294 | * This class aggregates the op information required for atlasing |
| 295 | */ |
Chris Dalton | fe199b7 | 2017-05-05 11:26:15 -0400 | [diff] [blame] | 296 | class AtlasObject final : public GrOnFlushCallbackObject { |
Robert Phillips | eb35f4d | 2017-03-21 07:56:47 -0400 | [diff] [blame] | 297 | public: |
Brian Salomon | 557e812 | 2019-10-24 10:37:08 -0400 | [diff] [blame] | 298 | AtlasObject(skiatest::Reporter* reporter) : fDone(false), fReporter(reporter) {} |
Robert Phillips | eb35f4d | 2017-03-21 07:56:47 -0400 | [diff] [blame] | 299 | |
| 300 | ~AtlasObject() override { |
| 301 | SkASSERT(fDone); |
| 302 | } |
| 303 | |
| 304 | void markAsDone() { |
| 305 | fDone = true; |
| 306 | } |
| 307 | |
Greg Daniel | f41b2bd | 2019-08-22 16:19:24 -0400 | [diff] [blame] | 308 | // Insert the new op in an internal singly-linked list for 'opsTaskID' |
| 309 | void addOp(uint32_t opsTaskID, AtlasedRectOp* op) { |
Robert Phillips | eb35f4d | 2017-03-21 07:56:47 -0400 | [diff] [blame] | 310 | LinkedListHeader* header = nullptr; |
| 311 | for (int i = 0; i < fOps.count(); ++i) { |
Greg Daniel | f41b2bd | 2019-08-22 16:19:24 -0400 | [diff] [blame] | 312 | if (opsTaskID == fOps[i].fID) { |
Robert Phillips | eb35f4d | 2017-03-21 07:56:47 -0400 | [diff] [blame] | 313 | header = &(fOps[i]); |
| 314 | } |
| 315 | } |
| 316 | |
| 317 | if (!header) { |
Greg Daniel | f41b2bd | 2019-08-22 16:19:24 -0400 | [diff] [blame] | 318 | fOps.push_back({opsTaskID, nullptr}); |
Robert Phillips | eb35f4d | 2017-03-21 07:56:47 -0400 | [diff] [blame] | 319 | header = &(fOps[fOps.count()-1]); |
| 320 | } |
| 321 | |
| 322 | op->setNext(header->fHead); |
| 323 | header->fHead = op; |
| 324 | } |
| 325 | |
Robert Phillips | 7d79e7b | 2018-02-14 11:09:57 -0500 | [diff] [blame] | 326 | int numOps() const { return fOps.count(); } |
| 327 | |
| 328 | // Get the fully lazy proxy that is backing the atlas. Its actual width isn't |
| 329 | // known until flush time. |
Greg Daniel | d11ae2e | 2020-02-10 16:36:07 -0500 | [diff] [blame] | 330 | GrSurfaceProxyView getAtlasView(GrProxyProvider* proxyProvider, const GrCaps* caps) { |
| 331 | if (fAtlasView) { |
| 332 | return fAtlasView; |
Robert Phillips | 7d79e7b | 2018-02-14 11:09:57 -0500 | [diff] [blame] | 333 | } |
| 334 | |
Robert Phillips | 0a15cc6 | 2019-07-30 12:49:10 -0400 | [diff] [blame] | 335 | const GrBackendFormat format = caps->getDefaultBackendFormat(GrColorType::kRGBA_8888, |
| 336 | GrRenderable::kYes); |
Greg Daniel | d11ae2e | 2020-02-10 16:36:07 -0500 | [diff] [blame] | 337 | auto proxy = GrProxyProvider::MakeFullyLazyProxy( |
Brian Salomon | 63410e9 | 2020-03-23 18:32:50 -0400 | [diff] [blame] | 338 | [](GrResourceProvider* resourceProvider, |
| 339 | const GrSurfaceProxy::LazySurfaceDesc& desc) |
Brian Salomon | beb7f52 | 2019-08-30 16:19:42 -0400 | [diff] [blame] | 340 | -> GrSurfaceProxy::LazyCallbackResult { |
Brian Salomon | 63410e9 | 2020-03-23 18:32:50 -0400 | [diff] [blame] | 341 | SkASSERT(desc.fDimensions.width() < 0 && desc.fDimensions.height() < 0); |
Brian Salomon | a56a746 | 2020-02-07 14:17:25 -0500 | [diff] [blame] | 342 | SkISize dims; |
Robert Phillips | 7d79e7b | 2018-02-14 11:09:57 -0500 | [diff] [blame] | 343 | // TODO: until partial flushes in MDB lands we're stuck having |
| 344 | // all 9 atlas draws occur |
Brian Salomon | a56a746 | 2020-02-07 14:17:25 -0500 | [diff] [blame] | 345 | dims.fWidth = 9 /*this->numOps()*/ * kAtlasTileSize; |
| 346 | dims.fHeight = kAtlasTileSize; |
Robert Phillips | 7d79e7b | 2018-02-14 11:09:57 -0500 | [diff] [blame] | 347 | |
Brian Salomon | 63410e9 | 2020-03-23 18:32:50 -0400 | [diff] [blame] | 348 | return resourceProvider->createTexture(dims, desc.fFormat, desc.fRenderable, |
| 349 | desc.fSampleCnt, desc.fMipMapped, |
| 350 | desc.fBudgeted, desc.fProtected); |
Robert Phillips | 7d79e7b | 2018-02-14 11:09:57 -0500 | [diff] [blame] | 351 | }, |
Greg Daniel | 4065d45 | 2018-11-16 15:43:41 -0500 | [diff] [blame] | 352 | format, |
Brian Salomon | 27b4d8d | 2019-07-22 14:23:45 -0400 | [diff] [blame] | 353 | GrRenderable::kYes, |
| 354 | 1, |
Brian Salomon | e8a766b | 2019-07-19 14:24:36 -0400 | [diff] [blame] | 355 | GrProtected::kNo, |
Brian Salomon | beb7f52 | 2019-08-30 16:19:42 -0400 | [diff] [blame] | 356 | *proxyProvider->caps(), |
| 357 | GrSurfaceProxy::UseAllocator::kNo); |
Robert Phillips | 5f78adf | 2019-04-22 12:41:39 -0400 | [diff] [blame] | 358 | |
Brian Salomon | df1bd6d | 2020-03-26 20:37:01 -0400 | [diff] [blame] | 359 | GrSwizzle readSwizzle = caps->getReadSwizzle(format, GrColorType::kRGBA_8888); |
Greg Daniel | d11ae2e | 2020-02-10 16:36:07 -0500 | [diff] [blame] | 360 | fAtlasView = {std::move(proxy), kBottomLeft_GrSurfaceOrigin, readSwizzle}; |
| 361 | return fAtlasView; |
Robert Phillips | eb35f4d | 2017-03-21 07:56:47 -0400 | [diff] [blame] | 362 | } |
| 363 | |
Robert Phillips | eb35f4d | 2017-03-21 07:56:47 -0400 | [diff] [blame] | 364 | /* |
Robert Phillips | 7d79e7b | 2018-02-14 11:09:57 -0500 | [diff] [blame] | 365 | * This callback creates the atlas and updates the AtlasedRectOps to read from it |
Robert Phillips | eb35f4d | 2017-03-21 07:56:47 -0400 | [diff] [blame] | 366 | */ |
Chris Dalton | fe199b7 | 2017-05-05 11:26:15 -0400 | [diff] [blame] | 367 | void preFlush(GrOnFlushResourceProvider* resourceProvider, |
Greg Daniel | f41b2bd | 2019-08-22 16:19:24 -0400 | [diff] [blame] | 368 | const uint32_t* opsTaskIDs, |
Chris Dalton | c4b4735 | 2019-08-23 10:10:36 -0600 | [diff] [blame] | 369 | int numOpsTaskIDs) override { |
Greg Daniel | f41b2bd | 2019-08-22 16:19:24 -0400 | [diff] [blame] | 370 | // Until MDB is landed we will most-likely only have one opsTask. |
Robert Phillips | eb35f4d | 2017-03-21 07:56:47 -0400 | [diff] [blame] | 371 | SkTDArray<LinkedListHeader*> lists; |
Greg Daniel | f41b2bd | 2019-08-22 16:19:24 -0400 | [diff] [blame] | 372 | for (int i = 0; i < numOpsTaskIDs; ++i) { |
| 373 | if (LinkedListHeader* list = this->getList(opsTaskIDs[i])) { |
Mike Reed | 5edcd31 | 2018-08-08 11:23:41 -0400 | [diff] [blame] | 374 | lists.push_back(list); |
Robert Phillips | eb35f4d | 2017-03-21 07:56:47 -0400 | [diff] [blame] | 375 | } |
| 376 | } |
| 377 | |
| 378 | if (!lists.count()) { |
| 379 | return; // nothing to atlas |
| 380 | } |
| 381 | |
Greg Daniel | d11ae2e | 2020-02-10 16:36:07 -0500 | [diff] [blame] | 382 | if (!resourceProvider->instatiateProxy(fAtlasView.proxy())) { |
Robert Phillips | 7d79e7b | 2018-02-14 11:09:57 -0500 | [diff] [blame] | 383 | return; |
| 384 | } |
Robert Phillips | eb35f4d | 2017-03-21 07:56:47 -0400 | [diff] [blame] | 385 | |
Greg Daniel | d11ae2e | 2020-02-10 16:36:07 -0500 | [diff] [blame] | 386 | // At this point 'fAtlasView' proxy should be instantiated and have: |
| 387 | // 1 ref from the 'fAtlasView' proxy sk_sp |
Robert Phillips | 3d4cac5 | 2019-06-11 08:08:08 -0400 | [diff] [blame] | 388 | // 9 refs from the 9 AtlasedRectOps |
Robert Phillips | e5f7328 | 2019-06-18 17:15:04 -0400 | [diff] [blame] | 389 | // The backing GrSurface should have only 1 though bc there is only one proxy |
Greg Daniel | d11ae2e | 2020-02-10 16:36:07 -0500 | [diff] [blame] | 390 | CheckSingleThreadedProxyRefs(fReporter, fAtlasView.proxy(), 10, 1); |
| 391 | auto rtc = resourceProvider->makeRenderTargetContext( |
| 392 | fAtlasView.refProxy(), fAtlasView.origin(), GrColorType::kRGBA_8888, nullptr, |
| 393 | nullptr); |
Robert Phillips | eb35f4d | 2017-03-21 07:56:47 -0400 | [diff] [blame] | 394 | |
Chris Dalton | 344e903 | 2017-12-11 15:42:09 -0700 | [diff] [blame] | 395 | // clear the atlas |
Michael Ludwig | 81d4172 | 2020-05-26 16:57:38 -0400 | [diff] [blame] | 396 | rtc->clear(SK_PMColor4fTRANSPARENT); |
Robert Phillips | eb35f4d | 2017-03-21 07:56:47 -0400 | [diff] [blame] | 397 | |
| 398 | int blocksInAtlas = 0; |
| 399 | for (int i = 0; i < lists.count(); ++i) { |
| 400 | for (AtlasedRectOp* op = lists[i]->fHead; op; op = op->next()) { |
| 401 | SkIRect r = SkIRect::MakeXYWH(blocksInAtlas*kAtlasTileSize, 0, |
| 402 | kAtlasTileSize, kAtlasTileSize); |
| 403 | |
| 404 | // For now, we avoid the resource buffer issues and just use clears |
| 405 | #if 1 |
Michael Ludwig | 81d4172 | 2020-05-26 16:57:38 -0400 | [diff] [blame] | 406 | rtc->clear(r, op->color()); |
Robert Phillips | eb35f4d | 2017-03-21 07:56:47 -0400 | [diff] [blame] | 407 | #else |
Robert Phillips | eb35f4d | 2017-03-21 07:56:47 -0400 | [diff] [blame] | 408 | GrPaint paint; |
Brian Osman | 9a9baae | 2018-11-05 15:06:26 -0500 | [diff] [blame] | 409 | paint.setColor4f(op->color()); |
Brian Salomon | 9a03642 | 2017-07-13 17:04:43 -0400 | [diff] [blame] | 410 | std::unique_ptr<GrDrawOp> drawOp(NonAARectOp::Make(std::move(paint), |
| 411 | SkRect::Make(r))); |
| 412 | rtc->priv().testingOnly_addDrawOp(std::move(drawOp)); |
Robert Phillips | eb35f4d | 2017-03-21 07:56:47 -0400 | [diff] [blame] | 413 | #endif |
| 414 | blocksInAtlas++; |
| 415 | |
| 416 | // Set the atlased Op's color to white (so we know we're not using it for |
| 417 | // the final draw). |
Brian Osman | cf86085 | 2018-10-31 14:04:39 -0400 | [diff] [blame] | 418 | op->setColor(SK_PMColor4fWHITE); |
Robert Phillips | eb35f4d | 2017-03-21 07:56:47 -0400 | [diff] [blame] | 419 | |
| 420 | // Set the atlased Op's localRect to point to where it landed in the atlas |
| 421 | op->setLocalRect(SkRect::Make(r)); |
Robert Phillips | eb35f4d | 2017-03-21 07:56:47 -0400 | [diff] [blame] | 422 | } |
| 423 | |
| 424 | // We've updated all these ops and we certainly don't want to process them again |
| 425 | this->clearOpsFor(lists[i]); |
| 426 | } |
Robert Phillips | eb35f4d | 2017-03-21 07:56:47 -0400 | [diff] [blame] | 427 | } |
| 428 | |
| 429 | private: |
| 430 | typedef struct { |
| 431 | uint32_t fID; |
| 432 | AtlasedRectOp* fHead; |
| 433 | } LinkedListHeader; |
| 434 | |
Greg Daniel | f41b2bd | 2019-08-22 16:19:24 -0400 | [diff] [blame] | 435 | LinkedListHeader* getList(uint32_t opsTaskID) { |
Robert Phillips | eb35f4d | 2017-03-21 07:56:47 -0400 | [diff] [blame] | 436 | for (int i = 0; i < fOps.count(); ++i) { |
Greg Daniel | f41b2bd | 2019-08-22 16:19:24 -0400 | [diff] [blame] | 437 | if (opsTaskID == fOps[i].fID) { |
Robert Phillips | eb35f4d | 2017-03-21 07:56:47 -0400 | [diff] [blame] | 438 | return &(fOps[i]); |
| 439 | } |
| 440 | } |
| 441 | return nullptr; |
| 442 | } |
| 443 | |
| 444 | void clearOpsFor(LinkedListHeader* header) { |
| 445 | // The AtlasedRectOps have yet to execute (and this class doesn't own them) so just |
| 446 | // forget about them in the laziest way possible. |
| 447 | header->fHead = nullptr; |
Greg Daniel | f41b2bd | 2019-08-22 16:19:24 -0400 | [diff] [blame] | 448 | header->fID = 0; // invalid opsTask ID |
Robert Phillips | eb35f4d | 2017-03-21 07:56:47 -0400 | [diff] [blame] | 449 | } |
| 450 | |
Greg Daniel | f41b2bd | 2019-08-22 16:19:24 -0400 | [diff] [blame] | 451 | // Each opsTask containing AtlasedRectOps gets its own internal singly-linked list |
Greg Daniel | d11ae2e | 2020-02-10 16:36:07 -0500 | [diff] [blame] | 452 | SkTDArray<LinkedListHeader> fOps; |
Robert Phillips | eb35f4d | 2017-03-21 07:56:47 -0400 | [diff] [blame] | 453 | |
Robert Phillips | 7d79e7b | 2018-02-14 11:09:57 -0500 | [diff] [blame] | 454 | // The fully lazy proxy for the atlas |
Greg Daniel | d11ae2e | 2020-02-10 16:36:07 -0500 | [diff] [blame] | 455 | GrSurfaceProxyView fAtlasView; |
Robert Phillips | eb35f4d | 2017-03-21 07:56:47 -0400 | [diff] [blame] | 456 | |
| 457 | // Set to true when the testing harness expects this object to be no longer used |
Greg Daniel | d11ae2e | 2020-02-10 16:36:07 -0500 | [diff] [blame] | 458 | bool fDone; |
Brian Salomon | 557e812 | 2019-10-24 10:37:08 -0400 | [diff] [blame] | 459 | |
Greg Daniel | d11ae2e | 2020-02-10 16:36:07 -0500 | [diff] [blame] | 460 | skiatest::Reporter* fReporter; |
Robert Phillips | eb35f4d | 2017-03-21 07:56:47 -0400 | [diff] [blame] | 461 | }; |
| 462 | |
| 463 | // This creates an off-screen rendertarget whose ops which eventually pull from the atlas. |
Greg Daniel | d11ae2e | 2020-02-10 16:36:07 -0500 | [diff] [blame] | 464 | static GrSurfaceProxyView make_upstream_image(GrContext* context, AtlasObject* object, int start, |
| 465 | GrSurfaceProxyView atlasView, |
| 466 | SkAlphaType atlasAlphaType) { |
Greg Daniel | e20fcad | 2020-01-08 11:52:34 -0500 | [diff] [blame] | 467 | auto rtc = GrRenderTargetContext::Make( |
| 468 | context, GrColorType::kRGBA_8888, nullptr, SkBackingFit::kApprox, |
| 469 | {3 * kDrawnTileSize, kDrawnTileSize}); |
Robert Phillips | eb35f4d | 2017-03-21 07:56:47 -0400 | [diff] [blame] | 470 | |
Michael Ludwig | 81d4172 | 2020-05-26 16:57:38 -0400 | [diff] [blame] | 471 | rtc->clear({ 1, 0, 0, 1 }); |
Robert Phillips | eb35f4d | 2017-03-21 07:56:47 -0400 | [diff] [blame] | 472 | |
| 473 | for (int i = 0; i < 3; ++i) { |
| 474 | SkRect r = SkRect::MakeXYWH(i*kDrawnTileSize, 0, kDrawnTileSize, kDrawnTileSize); |
| 475 | |
Greg Daniel | d2ccbb5 | 2020-02-05 10:45:39 -0500 | [diff] [blame] | 476 | auto fp = GrTextureEffect::Make(atlasView, atlasAlphaType); |
Robert Phillips | eb35f4d | 2017-03-21 07:56:47 -0400 | [diff] [blame] | 477 | GrPaint paint; |
| 478 | paint.addColorFragmentProcessor(std::move(fp)); |
| 479 | paint.setPorterDuffXPFactory(SkBlendMode::kSrc); |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 480 | std::unique_ptr<AtlasedRectOp> op(AtlasedRectOp::Make(context, |
| 481 | std::move(paint), r, start + i)); |
Robert Phillips | eb35f4d | 2017-03-21 07:56:47 -0400 | [diff] [blame] | 482 | |
| 483 | AtlasedRectOp* sparePtr = op.get(); |
| 484 | |
Greg Daniel | f41b2bd | 2019-08-22 16:19:24 -0400 | [diff] [blame] | 485 | uint32_t opsTaskID; |
Michael Ludwig | 7c12e28 | 2020-05-29 09:54:07 -0400 | [diff] [blame] | 486 | rtc->priv().testingOnly_addDrawOp(nullptr, std::move(op), |
Greg Daniel | f41b2bd | 2019-08-22 16:19:24 -0400 | [diff] [blame] | 487 | [&opsTaskID](GrOp* op, uint32_t id) { opsTaskID = id; }); |
| 488 | SkASSERT(SK_InvalidUniqueID != opsTaskID); |
Robert Phillips | eb35f4d | 2017-03-21 07:56:47 -0400 | [diff] [blame] | 489 | |
Greg Daniel | f41b2bd | 2019-08-22 16:19:24 -0400 | [diff] [blame] | 490 | object->addOp(opsTaskID, sparePtr); |
Robert Phillips | eb35f4d | 2017-03-21 07:56:47 -0400 | [diff] [blame] | 491 | } |
| 492 | |
Greg Daniel | d11ae2e | 2020-02-10 16:36:07 -0500 | [diff] [blame] | 493 | return rtc->readSurfaceView(); |
Robert Phillips | eb35f4d | 2017-03-21 07:56:47 -0400 | [diff] [blame] | 494 | } |
| 495 | |
| 496 | // Enable this if you want to debug the final draws w/o having the atlasCallback create the |
| 497 | // atlas |
| 498 | #if 0 |
| 499 | #include "SkGrPriv.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 500 | #include "include/core/SkImageEncoder.h" |
| 501 | #include "tools/ToolUtils.h" |
Chris Dalton | 1265894 | 2017-10-05 19:45:25 -0600 | [diff] [blame] | 502 | |
| 503 | static void save_bm(const SkBitmap& bm, const char name[]) { |
Mike Klein | ea3f014 | 2019-03-20 11:12:10 -0500 | [diff] [blame] | 504 | bool result = ToolUtils::EncodeImageToFile(name, bm, SkEncodedImageFormat::kPNG, 100); |
Chris Dalton | 1265894 | 2017-10-05 19:45:25 -0600 | [diff] [blame] | 505 | SkASSERT(result); |
| 506 | } |
Robert Phillips | eb35f4d | 2017-03-21 07:56:47 -0400 | [diff] [blame] | 507 | |
| 508 | sk_sp<GrTextureProxy> pre_create_atlas(GrContext* context) { |
| 509 | SkBitmap bm; |
| 510 | bm.allocN32Pixels(18, 2, true); |
| 511 | bm.erase(SK_ColorRED, SkIRect::MakeXYWH(0, 0, 2, 2)); |
| 512 | bm.erase(SK_ColorGREEN, SkIRect::MakeXYWH(2, 0, 2, 2)); |
| 513 | bm.erase(SK_ColorBLUE, SkIRect::MakeXYWH(4, 0, 2, 2)); |
| 514 | bm.erase(SK_ColorCYAN, SkIRect::MakeXYWH(6, 0, 2, 2)); |
| 515 | bm.erase(SK_ColorMAGENTA, SkIRect::MakeXYWH(8, 0, 2, 2)); |
| 516 | bm.erase(SK_ColorYELLOW, SkIRect::MakeXYWH(10, 0, 2, 2)); |
| 517 | bm.erase(SK_ColorBLACK, SkIRect::MakeXYWH(12, 0, 2, 2)); |
| 518 | bm.erase(SK_ColorGRAY, SkIRect::MakeXYWH(14, 0, 2, 2)); |
| 519 | bm.erase(SK_ColorWHITE, SkIRect::MakeXYWH(16, 0, 2, 2)); |
| 520 | |
| 521 | #if 1 |
| 522 | save_bm(bm, "atlas-fake.png"); |
| 523 | #endif |
| 524 | |
Robert Phillips | eb35f4d | 2017-03-21 07:56:47 -0400 | [diff] [blame] | 525 | desc.fFlags |= kRenderTarget_GrSurfaceFlag; |
| 526 | |
| 527 | sk_sp<GrSurfaceProxy> tmp = GrSurfaceProxy::MakeDeferred(*context->caps(), |
| 528 | context->textureProvider(), |
Brian Salomon | a56a746 | 2020-02-07 14:17:25 -0500 | [diff] [blame] | 529 | dm.dimensions(), SkBudgeted::kYes, |
Robert Phillips | eb35f4d | 2017-03-21 07:56:47 -0400 | [diff] [blame] | 530 | bm.getPixels(), bm.rowBytes()); |
| 531 | |
| 532 | return sk_ref_sp(tmp->asTextureProxy()); |
| 533 | } |
Robert Phillips | eb35f4d | 2017-03-21 07:56:47 -0400 | [diff] [blame] | 534 | #endif |
| 535 | |
Robert Phillips | 7d79e7b | 2018-02-14 11:09:57 -0500 | [diff] [blame] | 536 | |
Robert Phillips | eb35f4d | 2017-03-21 07:56:47 -0400 | [diff] [blame] | 537 | static void test_color(skiatest::Reporter* reporter, const SkBitmap& bm, int x, SkColor expected) { |
| 538 | SkColor readback = bm.getColor(x, kDrawnTileSize/2); |
| 539 | REPORTER_ASSERT(reporter, expected == readback); |
| 540 | if (expected != readback) { |
| 541 | SkDebugf("Color mismatch: %x %x\n", expected, readback); |
| 542 | } |
| 543 | } |
| 544 | |
| 545 | /* |
| 546 | * For the atlasing test we make a DAG that looks like: |
| 547 | * |
| 548 | * RT1 with ops: 0,1,2 RT2 with ops: 3,4,5 RT3 with ops: 6,7,8 |
| 549 | * \ / |
| 550 | * \ / |
| 551 | * RT4 |
| 552 | * We then flush RT4 and expect only ops 0-5 to be atlased together. |
| 553 | * Each op is just a solid colored rect so both the atlas and the final image should appear as: |
| 554 | * R G B C M Y |
| 555 | * with the atlas having width = 6*kAtlasTileSize and height = kAtlasTileSize. |
| 556 | * |
Robert Phillips | 7d79e7b | 2018-02-14 11:09:57 -0500 | [diff] [blame] | 557 | * Note: until partial flushes in MDB lands, the atlas will actually have width= 9*kAtlasTileSize |
| 558 | * and look like: |
Robert Phillips | eb35f4d | 2017-03-21 07:56:47 -0400 | [diff] [blame] | 559 | * R G B C M Y K Grey White |
| 560 | */ |
Chris Dalton | fe199b7 | 2017-05-05 11:26:15 -0400 | [diff] [blame] | 561 | DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(OnFlushCallbackTest, reporter, ctxInfo) { |
Greg Daniel | d11ae2e | 2020-02-10 16:36:07 -0500 | [diff] [blame] | 562 | static const int kNumViews = 3; |
Robert Phillips | eb35f4d | 2017-03-21 07:56:47 -0400 | [diff] [blame] | 563 | |
| 564 | GrContext* context = ctxInfo.grContext(); |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 565 | auto proxyProvider = context->priv().proxyProvider(); |
Robert Phillips | eb35f4d | 2017-03-21 07:56:47 -0400 | [diff] [blame] | 566 | |
Brian Salomon | 557e812 | 2019-10-24 10:37:08 -0400 | [diff] [blame] | 567 | AtlasObject object(reporter); |
Robert Phillips | eb35f4d | 2017-03-21 07:56:47 -0400 | [diff] [blame] | 568 | |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 569 | context->priv().addOnFlushCallbackObject(&object); |
Robert Phillips | eb35f4d | 2017-03-21 07:56:47 -0400 | [diff] [blame] | 570 | |
Greg Daniel | d11ae2e | 2020-02-10 16:36:07 -0500 | [diff] [blame] | 571 | GrSurfaceProxyView views[kNumViews]; |
| 572 | for (int i = 0; i < kNumViews; ++i) { |
| 573 | views[i] = make_upstream_image(context, &object, i * 3, |
| 574 | object.getAtlasView(proxyProvider, context->priv().caps()), |
| 575 | kPremul_SkAlphaType); |
Robert Phillips | eb35f4d | 2017-03-21 07:56:47 -0400 | [diff] [blame] | 576 | } |
| 577 | |
| 578 | static const int kFinalWidth = 6*kDrawnTileSize; |
| 579 | static const int kFinalHeight = kDrawnTileSize; |
| 580 | |
Greg Daniel | e20fcad | 2020-01-08 11:52:34 -0500 | [diff] [blame] | 581 | auto rtc = GrRenderTargetContext::Make( |
| 582 | context, GrColorType::kRGBA_8888, nullptr, SkBackingFit::kApprox, |
| 583 | {kFinalWidth, kFinalHeight}); |
Robert Phillips | eb35f4d | 2017-03-21 07:56:47 -0400 | [diff] [blame] | 584 | |
Michael Ludwig | 81d4172 | 2020-05-26 16:57:38 -0400 | [diff] [blame] | 585 | rtc->clear(SK_PMColor4fWHITE); |
Robert Phillips | eb35f4d | 2017-03-21 07:56:47 -0400 | [diff] [blame] | 586 | |
| 587 | // Note that this doesn't include the third texture proxy |
Greg Daniel | d11ae2e | 2020-02-10 16:36:07 -0500 | [diff] [blame] | 588 | for (int i = 0; i < kNumViews - 1; ++i) { |
Robert Phillips | eb35f4d | 2017-03-21 07:56:47 -0400 | [diff] [blame] | 589 | SkRect r = SkRect::MakeXYWH(i*3*kDrawnTileSize, 0, 3*kDrawnTileSize, kDrawnTileSize); |
| 590 | |
Mike Reed | 1f60733 | 2020-05-21 12:11:27 -0400 | [diff] [blame] | 591 | SkMatrix t = SkMatrix::Translate(-i*3*kDrawnTileSize, 0); |
Robert Phillips | eb35f4d | 2017-03-21 07:56:47 -0400 | [diff] [blame] | 592 | |
| 593 | GrPaint paint; |
Greg Daniel | d11ae2e | 2020-02-10 16:36:07 -0500 | [diff] [blame] | 594 | auto fp = GrTextureEffect::Make(std::move(views[i]), kPremul_SkAlphaType, t); |
Robert Phillips | eb35f4d | 2017-03-21 07:56:47 -0400 | [diff] [blame] | 595 | paint.setPorterDuffXPFactory(SkBlendMode::kSrc); |
| 596 | paint.addColorFragmentProcessor(std::move(fp)); |
| 597 | |
Michael Ludwig | 7c12e28 | 2020-05-29 09:54:07 -0400 | [diff] [blame] | 598 | rtc->drawRect(nullptr, std::move(paint), GrAA::kNo, SkMatrix::I(), r); |
Robert Phillips | eb35f4d | 2017-03-21 07:56:47 -0400 | [diff] [blame] | 599 | } |
| 600 | |
Greg Daniel | 9efe386 | 2020-06-11 11:51:06 -0400 | [diff] [blame] | 601 | rtc->flush(SkSurface::BackendSurfaceAccess::kNoAccess, GrFlushInfo(), nullptr); |
Robert Phillips | eb35f4d | 2017-03-21 07:56:47 -0400 | [diff] [blame] | 602 | |
| 603 | SkBitmap readBack; |
| 604 | readBack.allocN32Pixels(kFinalWidth, kFinalHeight); |
| 605 | |
| 606 | SkDEBUGCODE(bool result =) rtc->readPixels(readBack.info(), readBack.getPixels(), |
Brian Salomon | 1d43530 | 2019-07-01 13:05:28 -0400 | [diff] [blame] | 607 | readBack.rowBytes(), {0, 0}); |
Robert Phillips | eb35f4d | 2017-03-21 07:56:47 -0400 | [diff] [blame] | 608 | SkASSERT(result); |
| 609 | |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 610 | context->priv().testingOnly_flushAndRemoveOnFlushCallbackObject(&object); |
Chris Dalton | fe199b7 | 2017-05-05 11:26:15 -0400 | [diff] [blame] | 611 | |
| 612 | object.markAsDone(); |
Robert Phillips | eb35f4d | 2017-03-21 07:56:47 -0400 | [diff] [blame] | 613 | |
Robert Phillips | eb35f4d | 2017-03-21 07:56:47 -0400 | [diff] [blame] | 614 | int x = kDrawnTileSize/2; |
| 615 | test_color(reporter, readBack, x, SK_ColorRED); |
| 616 | x += kDrawnTileSize; |
| 617 | test_color(reporter, readBack, x, SK_ColorGREEN); |
| 618 | x += kDrawnTileSize; |
| 619 | test_color(reporter, readBack, x, SK_ColorBLUE); |
| 620 | x += kDrawnTileSize; |
| 621 | test_color(reporter, readBack, x, SK_ColorCYAN); |
| 622 | x += kDrawnTileSize; |
| 623 | test_color(reporter, readBack, x, SK_ColorMAGENTA); |
| 624 | x += kDrawnTileSize; |
| 625 | test_color(reporter, readBack, x, SK_ColorYELLOW); |
| 626 | } |