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