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