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 | 9a725dd | 2017-09-20 09:53:22 -0400 | [diff] [blame] | 75 | RequiresDstTexture finalize(const GrCaps& caps, const GrAppliedClip*, |
| 76 | GrPixelConfigIsClamped dstIsClamped) override { |
Brian Salomon | 9a03642 | 2017-07-13 17:04:43 -0400 | [diff] [blame] | 77 | // Set the color to unknown because the subclass may change the color later. |
| 78 | GrProcessorAnalysisColor gpColor; |
| 79 | gpColor.setToUnknown(); |
| 80 | // We ignore the clip so pass this rather than the GrAppliedClip param. |
| 81 | static GrAppliedClip kNoClip; |
Brian Osman | 9a725dd | 2017-09-20 09:53:22 -0400 | [diff] [blame] | 82 | return fHelper.xpRequiresDstTexture(caps, &kNoClip, dstIsClamped, |
| 83 | GrProcessorAnalysisCoverage::kNone, &gpColor); |
Brian Salomon | 9a03642 | 2017-07-13 17:04:43 -0400 | [diff] [blame] | 84 | } |
| 85 | |
Robert Phillips | eb35f4d | 2017-03-21 07:56:47 -0400 | [diff] [blame] | 86 | protected: |
Robert Phillips | eb35f4d | 2017-03-21 07:56:47 -0400 | [diff] [blame] | 87 | GrColor fColor; |
| 88 | bool fHasLocalRect; |
| 89 | GrQuad fLocalQuad; |
| 90 | SkRect fRect; |
| 91 | |
| 92 | private: |
Robert Phillips | eb35f4d | 2017-03-21 07:56:47 -0400 | [diff] [blame] | 93 | bool onCombineIfPossible(GrOp*, const GrCaps&) override { return false; } |
| 94 | |
Brian Salomon | 91326c3 | 2017-08-09 16:02:19 -0400 | [diff] [blame] | 95 | void onPrepareDraws(Target* target) override { |
Robert Phillips | eb35f4d | 2017-03-21 07:56:47 -0400 | [diff] [blame] | 96 | using namespace GrDefaultGeoProcFactory; |
| 97 | |
| 98 | // The vertex attrib order is always pos, color, local coords. |
| 99 | static const int kColorOffset = sizeof(SkPoint); |
| 100 | static const int kLocalOffset = sizeof(SkPoint) + sizeof(GrColor); |
| 101 | |
| 102 | sk_sp<GrGeometryProcessor> gp = |
| 103 | GrDefaultGeoProcFactory::Make(Color::kPremulGrColorAttribute_Type, |
| 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 | |
| 113 | size_t vertexStride = gp->getVertexStride(); |
| 114 | |
| 115 | SkASSERT(fHasLocalRect |
| 116 | ? vertexStride == sizeof(GrDefaultGeoProcFactory::PositionColorLocalCoordAttr) |
| 117 | : vertexStride == sizeof(GrDefaultGeoProcFactory::PositionColorAttr)); |
| 118 | |
| 119 | const GrBuffer* indexBuffer; |
| 120 | int firstIndex; |
| 121 | uint16_t* indices = target->makeIndexSpace(6, &indexBuffer, &firstIndex); |
| 122 | if (!indices) { |
| 123 | SkDebugf("Indices could not be allocated for GrAtlasedOp.\n"); |
| 124 | return; |
| 125 | } |
| 126 | |
| 127 | const GrBuffer* vertexBuffer; |
| 128 | int firstVertex; |
| 129 | void* vertices = target->makeVertexSpace(vertexStride, 4, &vertexBuffer, &firstVertex); |
| 130 | if (!vertices) { |
| 131 | SkDebugf("Vertices could not be allocated for GrAtlasedOp.\n"); |
| 132 | return; |
| 133 | } |
| 134 | |
| 135 | // Setup indices |
| 136 | indices[0] = 0; |
| 137 | indices[1] = 1; |
| 138 | indices[2] = 2; |
Brian Salomon | 57caa66 | 2017-10-18 12:21:05 +0000 | [diff] [blame] | 139 | indices[3] = 2; |
| 140 | indices[4] = 1; |
Robert Phillips | eb35f4d | 2017-03-21 07:56:47 -0400 | [diff] [blame] | 141 | indices[5] = 3; |
| 142 | |
| 143 | // Setup positions |
| 144 | SkPoint* position = (SkPoint*) vertices; |
Brian Salomon | ec42e15 | 2018-05-18 12:52:22 -0400 | [diff] [blame] | 145 | SkPointPriv::SetRectTriStrip(position, fRect, vertexStride); |
Robert Phillips | eb35f4d | 2017-03-21 07:56:47 -0400 | [diff] [blame] | 146 | |
| 147 | // Setup vertex colors |
| 148 | GrColor* color = (GrColor*)((intptr_t)vertices + kColorOffset); |
| 149 | for (int i = 0; i < 4; ++i) { |
| 150 | *color = fColor; |
| 151 | color = (GrColor*)((intptr_t)color + vertexStride); |
| 152 | } |
| 153 | |
| 154 | // Setup local coords |
| 155 | if (fHasLocalRect) { |
| 156 | SkPoint* coords = (SkPoint*)((intptr_t) vertices + kLocalOffset); |
| 157 | for (int i = 0; i < 4; i++) { |
| 158 | *coords = fLocalQuad.point(i); |
| 159 | coords = (SkPoint*)((intptr_t) coords + vertexStride); |
| 160 | } |
| 161 | } |
| 162 | |
Chris Dalton | 3809bab | 2017-06-13 10:55:06 -0600 | [diff] [blame] | 163 | GrMesh mesh(GrPrimitiveType::kTriangles); |
Brian Salomon | 802cb31 | 2018-06-08 18:05:20 -0400 | [diff] [blame] | 164 | mesh.setIndexed(indexBuffer, 6, firstIndex, 0, 3, GrPrimitiveRestart::kNo); |
Chris Dalton | 114a3c0 | 2017-05-26 15:17:19 -0600 | [diff] [blame] | 165 | mesh.setVertexData(vertexBuffer, firstVertex); |
Robert Phillips | eb35f4d | 2017-03-21 07:56:47 -0400 | [diff] [blame] | 166 | |
Brian Salomon | 9a03642 | 2017-07-13 17:04:43 -0400 | [diff] [blame] | 167 | target->draw(gp.get(), fHelper.makePipeline(target), 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 | |
| 303 | fAtlasProxy = proxyProvider->createFullyLazyProxy( |
| 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, |
| 322 | kRGBA_8888_GrPixelConfig); |
| 323 | return fAtlasProxy; |
Robert Phillips | eb35f4d | 2017-03-21 07:56:47 -0400 | [diff] [blame] | 324 | } |
| 325 | |
Robert Phillips | eb35f4d | 2017-03-21 07:56:47 -0400 | [diff] [blame] | 326 | /* |
Robert Phillips | 7d79e7b | 2018-02-14 11:09:57 -0500 | [diff] [blame] | 327 | * 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] | 328 | */ |
Chris Dalton | fe199b7 | 2017-05-05 11:26:15 -0400 | [diff] [blame] | 329 | void preFlush(GrOnFlushResourceProvider* resourceProvider, |
Robert Phillips | eb35f4d | 2017-03-21 07:56:47 -0400 | [diff] [blame] | 330 | const uint32_t* opListIDs, int numOpListIDs, |
| 331 | SkTArray<sk_sp<GrRenderTargetContext>>* results) override { |
| 332 | SkASSERT(!results->count()); |
| 333 | |
| 334 | // Until MDB is landed we will most-likely only have one opList. |
| 335 | SkTDArray<LinkedListHeader*> lists; |
| 336 | for (int i = 0; i < numOpListIDs; ++i) { |
| 337 | if (LinkedListHeader* list = this->getList(opListIDs[i])) { |
| 338 | lists.push(list); |
| 339 | } |
| 340 | } |
| 341 | |
| 342 | if (!lists.count()) { |
| 343 | return; // nothing to atlas |
| 344 | } |
| 345 | |
Robert Phillips | 7d79e7b | 2018-02-14 11:09:57 -0500 | [diff] [blame] | 346 | if (!resourceProvider->instatiateProxy(fAtlasProxy.get())) { |
| 347 | return; |
| 348 | } |
Robert Phillips | eb35f4d | 2017-03-21 07:56:47 -0400 | [diff] [blame] | 349 | |
Robert Phillips | eb35f4d | 2017-03-21 07:56:47 -0400 | [diff] [blame] | 350 | // At this point all the GrAtlasedOp's should have lined up to read from 'atlasDest' and |
| 351 | // there should either be two writes to clear it or no writes. |
Robert Phillips | 7d79e7b | 2018-02-14 11:09:57 -0500 | [diff] [blame] | 352 | SkASSERT(9 == fAtlasProxy->getPendingReadCnt_TestOnly()); |
| 353 | SkASSERT(2 == fAtlasProxy->getPendingWriteCnt_TestOnly() || |
| 354 | 0 == fAtlasProxy->getPendingWriteCnt_TestOnly()); |
Robert Phillips | eb35f4d | 2017-03-21 07:56:47 -0400 | [diff] [blame] | 355 | sk_sp<GrRenderTargetContext> rtc = resourceProvider->makeRenderTargetContext( |
Robert Phillips | 7d79e7b | 2018-02-14 11:09:57 -0500 | [diff] [blame] | 356 | fAtlasProxy, |
Robert Phillips | eb35f4d | 2017-03-21 07:56:47 -0400 | [diff] [blame] | 357 | nullptr, nullptr); |
Robert Phillips | eb35f4d | 2017-03-21 07:56:47 -0400 | [diff] [blame] | 358 | |
Chris Dalton | 344e903 | 2017-12-11 15:42:09 -0700 | [diff] [blame] | 359 | // clear the atlas |
Robert Phillips | 7d79e7b | 2018-02-14 11:09:57 -0500 | [diff] [blame] | 360 | rtc->clear(nullptr, 0x0, GrRenderTargetContext::CanClearFullscreen::kYes); |
Robert Phillips | eb35f4d | 2017-03-21 07:56:47 -0400 | [diff] [blame] | 361 | |
| 362 | int blocksInAtlas = 0; |
| 363 | for (int i = 0; i < lists.count(); ++i) { |
| 364 | for (AtlasedRectOp* op = lists[i]->fHead; op; op = op->next()) { |
| 365 | SkIRect r = SkIRect::MakeXYWH(blocksInAtlas*kAtlasTileSize, 0, |
| 366 | kAtlasTileSize, kAtlasTileSize); |
| 367 | |
| 368 | // For now, we avoid the resource buffer issues and just use clears |
| 369 | #if 1 |
Chris Dalton | 344e903 | 2017-12-11 15:42:09 -0700 | [diff] [blame] | 370 | rtc->clear(&r, op->color(), GrRenderTargetContext::CanClearFullscreen::kNo); |
Robert Phillips | eb35f4d | 2017-03-21 07:56:47 -0400 | [diff] [blame] | 371 | #else |
Robert Phillips | eb35f4d | 2017-03-21 07:56:47 -0400 | [diff] [blame] | 372 | GrPaint paint; |
Brian Salomon | 9a03642 | 2017-07-13 17:04:43 -0400 | [diff] [blame] | 373 | paint.setColor4f(GrColor4f::FromGrColor(op->color())); |
| 374 | std::unique_ptr<GrDrawOp> drawOp(NonAARectOp::Make(std::move(paint), |
| 375 | SkRect::Make(r))); |
| 376 | rtc->priv().testingOnly_addDrawOp(std::move(drawOp)); |
Robert Phillips | eb35f4d | 2017-03-21 07:56:47 -0400 | [diff] [blame] | 377 | #endif |
| 378 | blocksInAtlas++; |
| 379 | |
| 380 | // Set the atlased Op's color to white (so we know we're not using it for |
| 381 | // the final draw). |
| 382 | op->setColor(0xFFFFFFFF); |
| 383 | |
| 384 | // Set the atlased Op's localRect to point to where it landed in the atlas |
| 385 | op->setLocalRect(SkRect::Make(r)); |
Robert Phillips | eb35f4d | 2017-03-21 07:56:47 -0400 | [diff] [blame] | 386 | } |
| 387 | |
| 388 | // We've updated all these ops and we certainly don't want to process them again |
| 389 | this->clearOpsFor(lists[i]); |
| 390 | } |
| 391 | |
Robert Phillips | eb35f4d | 2017-03-21 07:56:47 -0400 | [diff] [blame] | 392 | results->push_back(std::move(rtc)); |
| 393 | } |
| 394 | |
| 395 | private: |
| 396 | typedef struct { |
| 397 | uint32_t fID; |
| 398 | AtlasedRectOp* fHead; |
| 399 | } LinkedListHeader; |
| 400 | |
| 401 | LinkedListHeader* getList(uint32_t opListID) { |
| 402 | for (int i = 0; i < fOps.count(); ++i) { |
| 403 | if (opListID == fOps[i].fID) { |
| 404 | return &(fOps[i]); |
| 405 | } |
| 406 | } |
| 407 | return nullptr; |
| 408 | } |
| 409 | |
| 410 | void clearOpsFor(LinkedListHeader* header) { |
| 411 | // The AtlasedRectOps have yet to execute (and this class doesn't own them) so just |
| 412 | // forget about them in the laziest way possible. |
| 413 | header->fHead = nullptr; |
| 414 | header->fID = 0; // invalid opList ID |
| 415 | } |
| 416 | |
| 417 | // Each opList containing AtlasedRectOps gets its own internal singly-linked list |
| 418 | SkTDArray<LinkedListHeader> fOps; |
| 419 | |
Robert Phillips | 7d79e7b | 2018-02-14 11:09:57 -0500 | [diff] [blame] | 420 | // The fully lazy proxy for the atlas |
| 421 | sk_sp<GrTextureProxy> fAtlasProxy; |
Robert Phillips | eb35f4d | 2017-03-21 07:56:47 -0400 | [diff] [blame] | 422 | |
| 423 | // Set to true when the testing harness expects this object to be no longer used |
| 424 | bool fDone; |
| 425 | }; |
| 426 | |
| 427 | // This creates an off-screen rendertarget whose ops which eventually pull from the atlas. |
| 428 | 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] | 429 | sk_sp<GrTextureProxy> atlasProxy) { |
Robert Phillips | 0c4b7b1 | 2018-03-06 08:20:37 -0500 | [diff] [blame] | 430 | sk_sp<GrRenderTargetContext> rtc(context->contextPriv().makeDeferredRenderTargetContext( |
Robert Phillips | dd3b3f4 | 2017-04-24 10:57:28 -0400 | [diff] [blame] | 431 | SkBackingFit::kApprox, |
Robert Phillips | eb35f4d | 2017-03-21 07:56:47 -0400 | [diff] [blame] | 432 | 3*kDrawnTileSize, |
| 433 | kDrawnTileSize, |
| 434 | kRGBA_8888_GrPixelConfig, |
| 435 | nullptr)); |
| 436 | |
Chris Dalton | 344e903 | 2017-12-11 15:42:09 -0700 | [diff] [blame] | 437 | rtc->clear(nullptr, GrColorPackRGBA(255, 0, 0, 255), |
| 438 | GrRenderTargetContext::CanClearFullscreen::kYes); |
Robert Phillips | eb35f4d | 2017-03-21 07:56:47 -0400 | [diff] [blame] | 439 | |
| 440 | for (int i = 0; i < 3; ++i) { |
| 441 | SkRect r = SkRect::MakeXYWH(i*kDrawnTileSize, 0, kDrawnTileSize, kDrawnTileSize); |
| 442 | |
Robert Phillips | 7d79e7b | 2018-02-14 11:09:57 -0500 | [diff] [blame] | 443 | auto fp = GrSimpleTextureEffect::Make(atlasProxy, SkMatrix::I()); |
Robert Phillips | eb35f4d | 2017-03-21 07:56:47 -0400 | [diff] [blame] | 444 | GrPaint paint; |
| 445 | paint.addColorFragmentProcessor(std::move(fp)); |
| 446 | paint.setPorterDuffXPFactory(SkBlendMode::kSrc); |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 447 | std::unique_ptr<AtlasedRectOp> op(AtlasedRectOp::Make(context, |
| 448 | std::move(paint), r, start + i)); |
Robert Phillips | eb35f4d | 2017-03-21 07:56:47 -0400 | [diff] [blame] | 449 | |
| 450 | AtlasedRectOp* sparePtr = op.get(); |
| 451 | |
Brian Salomon | 9a03642 | 2017-07-13 17:04:43 -0400 | [diff] [blame] | 452 | uint32_t opListID = rtc->priv().testingOnly_addDrawOp(std::move(op)); |
Chris Dalton | f104fec | 2018-05-22 16:17:48 -0600 | [diff] [blame] | 453 | SkASSERT(SK_InvalidUniqueID != opListID); |
Robert Phillips | eb35f4d | 2017-03-21 07:56:47 -0400 | [diff] [blame] | 454 | |
| 455 | object->addOp(opListID, sparePtr); |
| 456 | } |
| 457 | |
| 458 | return rtc->asTextureProxyRef(); |
| 459 | } |
| 460 | |
| 461 | // Enable this if you want to debug the final draws w/o having the atlasCallback create the |
| 462 | // atlas |
| 463 | #if 0 |
Chris Dalton | 1265894 | 2017-10-05 19:45:25 -0600 | [diff] [blame] | 464 | #include "SkImageEncoder.h" |
Robert Phillips | eb35f4d | 2017-03-21 07:56:47 -0400 | [diff] [blame] | 465 | #include "SkGrPriv.h" |
Chris Dalton | 1265894 | 2017-10-05 19:45:25 -0600 | [diff] [blame] | 466 | #include "sk_tool_utils.h" |
| 467 | |
| 468 | static void save_bm(const SkBitmap& bm, const char name[]) { |
| 469 | bool result = sk_tool_utils::EncodeImageToFile(name, bm, SkEncodedImageFormat::kPNG, 100); |
| 470 | SkASSERT(result); |
| 471 | } |
Robert Phillips | eb35f4d | 2017-03-21 07:56:47 -0400 | [diff] [blame] | 472 | |
| 473 | sk_sp<GrTextureProxy> pre_create_atlas(GrContext* context) { |
| 474 | SkBitmap bm; |
| 475 | bm.allocN32Pixels(18, 2, true); |
| 476 | bm.erase(SK_ColorRED, SkIRect::MakeXYWH(0, 0, 2, 2)); |
| 477 | bm.erase(SK_ColorGREEN, SkIRect::MakeXYWH(2, 0, 2, 2)); |
| 478 | bm.erase(SK_ColorBLUE, SkIRect::MakeXYWH(4, 0, 2, 2)); |
| 479 | bm.erase(SK_ColorCYAN, SkIRect::MakeXYWH(6, 0, 2, 2)); |
| 480 | bm.erase(SK_ColorMAGENTA, SkIRect::MakeXYWH(8, 0, 2, 2)); |
| 481 | bm.erase(SK_ColorYELLOW, SkIRect::MakeXYWH(10, 0, 2, 2)); |
| 482 | bm.erase(SK_ColorBLACK, SkIRect::MakeXYWH(12, 0, 2, 2)); |
| 483 | bm.erase(SK_ColorGRAY, SkIRect::MakeXYWH(14, 0, 2, 2)); |
| 484 | bm.erase(SK_ColorWHITE, SkIRect::MakeXYWH(16, 0, 2, 2)); |
| 485 | |
| 486 | #if 1 |
| 487 | save_bm(bm, "atlas-fake.png"); |
| 488 | #endif |
| 489 | |
Brian Osman | 2b23c4b | 2018-06-01 12:25:08 -0400 | [diff] [blame] | 490 | GrSurfaceDesc desc = GrImageInfoToSurfaceDesc(bm.info()); |
Robert Phillips | eb35f4d | 2017-03-21 07:56:47 -0400 | [diff] [blame] | 491 | desc.fFlags |= kRenderTarget_GrSurfaceFlag; |
| 492 | |
| 493 | sk_sp<GrSurfaceProxy> tmp = GrSurfaceProxy::MakeDeferred(*context->caps(), |
| 494 | context->textureProvider(), |
| 495 | desc, SkBudgeted::kYes, |
| 496 | bm.getPixels(), bm.rowBytes()); |
| 497 | |
| 498 | return sk_ref_sp(tmp->asTextureProxy()); |
| 499 | } |
Robert Phillips | eb35f4d | 2017-03-21 07:56:47 -0400 | [diff] [blame] | 500 | #endif |
| 501 | |
Robert Phillips | 7d79e7b | 2018-02-14 11:09:57 -0500 | [diff] [blame] | 502 | |
Robert Phillips | eb35f4d | 2017-03-21 07:56:47 -0400 | [diff] [blame] | 503 | static void test_color(skiatest::Reporter* reporter, const SkBitmap& bm, int x, SkColor expected) { |
| 504 | SkColor readback = bm.getColor(x, kDrawnTileSize/2); |
| 505 | REPORTER_ASSERT(reporter, expected == readback); |
| 506 | if (expected != readback) { |
| 507 | SkDebugf("Color mismatch: %x %x\n", expected, readback); |
| 508 | } |
| 509 | } |
| 510 | |
| 511 | /* |
| 512 | * For the atlasing test we make a DAG that looks like: |
| 513 | * |
| 514 | * RT1 with ops: 0,1,2 RT2 with ops: 3,4,5 RT3 with ops: 6,7,8 |
| 515 | * \ / |
| 516 | * \ / |
| 517 | * RT4 |
| 518 | * We then flush RT4 and expect only ops 0-5 to be atlased together. |
| 519 | * Each op is just a solid colored rect so both the atlas and the final image should appear as: |
| 520 | * R G B C M Y |
| 521 | * with the atlas having width = 6*kAtlasTileSize and height = kAtlasTileSize. |
| 522 | * |
Robert Phillips | 7d79e7b | 2018-02-14 11:09:57 -0500 | [diff] [blame] | 523 | * Note: until partial flushes in MDB lands, the atlas will actually have width= 9*kAtlasTileSize |
| 524 | * and look like: |
Robert Phillips | eb35f4d | 2017-03-21 07:56:47 -0400 | [diff] [blame] | 525 | * R G B C M Y K Grey White |
| 526 | */ |
Chris Dalton | fe199b7 | 2017-05-05 11:26:15 -0400 | [diff] [blame] | 527 | DEF_GPUTEST_FOR_GL_RENDERING_CONTEXTS(OnFlushCallbackTest, reporter, ctxInfo) { |
Robert Phillips | eb35f4d | 2017-03-21 07:56:47 -0400 | [diff] [blame] | 528 | static const int kNumProxies = 3; |
| 529 | |
| 530 | GrContext* context = ctxInfo.grContext(); |
Robert Phillips | 7d79e7b | 2018-02-14 11:09:57 -0500 | [diff] [blame] | 531 | auto proxyProvider = context->contextPriv().proxyProvider(); |
Robert Phillips | eb35f4d | 2017-03-21 07:56:47 -0400 | [diff] [blame] | 532 | |
Chris Dalton | fe199b7 | 2017-05-05 11:26:15 -0400 | [diff] [blame] | 533 | AtlasObject object; |
Robert Phillips | eb35f4d | 2017-03-21 07:56:47 -0400 | [diff] [blame] | 534 | |
Chris Dalton | fe199b7 | 2017-05-05 11:26:15 -0400 | [diff] [blame] | 535 | context->contextPriv().addOnFlushCallbackObject(&object); |
Robert Phillips | eb35f4d | 2017-03-21 07:56:47 -0400 | [diff] [blame] | 536 | |
| 537 | sk_sp<GrTextureProxy> proxies[kNumProxies]; |
| 538 | for (int i = 0; i < kNumProxies; ++i) { |
Robert Phillips | 7d79e7b | 2018-02-14 11:09:57 -0500 | [diff] [blame] | 539 | proxies[i] = make_upstream_image(context, &object, i*3, |
| 540 | object.getAtlasProxy(proxyProvider)); |
Robert Phillips | eb35f4d | 2017-03-21 07:56:47 -0400 | [diff] [blame] | 541 | } |
| 542 | |
| 543 | static const int kFinalWidth = 6*kDrawnTileSize; |
| 544 | static const int kFinalHeight = kDrawnTileSize; |
| 545 | |
Robert Phillips | 0c4b7b1 | 2018-03-06 08:20:37 -0500 | [diff] [blame] | 546 | sk_sp<GrRenderTargetContext> rtc(context->contextPriv().makeDeferredRenderTargetContext( |
Robert Phillips | dd3b3f4 | 2017-04-24 10:57:28 -0400 | [diff] [blame] | 547 | SkBackingFit::kApprox, |
Robert Phillips | eb35f4d | 2017-03-21 07:56:47 -0400 | [diff] [blame] | 548 | kFinalWidth, |
| 549 | kFinalHeight, |
| 550 | kRGBA_8888_GrPixelConfig, |
| 551 | nullptr)); |
| 552 | |
Chris Dalton | 344e903 | 2017-12-11 15:42:09 -0700 | [diff] [blame] | 553 | rtc->clear(nullptr, 0xFFFFFFFF, GrRenderTargetContext::CanClearFullscreen::kYes); |
Robert Phillips | eb35f4d | 2017-03-21 07:56:47 -0400 | [diff] [blame] | 554 | |
| 555 | // Note that this doesn't include the third texture proxy |
| 556 | for (int i = 0; i < kNumProxies-1; ++i) { |
| 557 | SkRect r = SkRect::MakeXYWH(i*3*kDrawnTileSize, 0, 3*kDrawnTileSize, kDrawnTileSize); |
| 558 | |
| 559 | SkMatrix t = SkMatrix::MakeTrans(-i*3*kDrawnTileSize, 0); |
| 560 | |
| 561 | GrPaint paint; |
Brian Osman | 2240be9 | 2017-10-18 13:15:13 -0400 | [diff] [blame] | 562 | auto fp = GrSimpleTextureEffect::Make(std::move(proxies[i]), t); |
Robert Phillips | eb35f4d | 2017-03-21 07:56:47 -0400 | [diff] [blame] | 563 | paint.setPorterDuffXPFactory(SkBlendMode::kSrc); |
| 564 | paint.addColorFragmentProcessor(std::move(fp)); |
| 565 | |
| 566 | rtc->drawRect(GrNoClip(), std::move(paint), GrAA::kNo, SkMatrix::I(), r); |
| 567 | } |
| 568 | |
Greg Daniel | a5cb781 | 2017-06-16 09:45:32 -0400 | [diff] [blame] | 569 | rtc->prepareForExternalIO(0, nullptr); |
Robert Phillips | eb35f4d | 2017-03-21 07:56:47 -0400 | [diff] [blame] | 570 | |
| 571 | SkBitmap readBack; |
| 572 | readBack.allocN32Pixels(kFinalWidth, kFinalHeight); |
| 573 | |
| 574 | SkDEBUGCODE(bool result =) rtc->readPixels(readBack.info(), readBack.getPixels(), |
| 575 | readBack.rowBytes(), 0, 0); |
| 576 | SkASSERT(result); |
| 577 | |
Chris Dalton | fe199b7 | 2017-05-05 11:26:15 -0400 | [diff] [blame] | 578 | context->contextPriv().testingOnly_flushAndRemoveOnFlushCallbackObject(&object); |
| 579 | |
| 580 | object.markAsDone(); |
Robert Phillips | eb35f4d | 2017-03-21 07:56:47 -0400 | [diff] [blame] | 581 | |
Robert Phillips | eb35f4d | 2017-03-21 07:56:47 -0400 | [diff] [blame] | 582 | int x = kDrawnTileSize/2; |
| 583 | test_color(reporter, readBack, x, SK_ColorRED); |
| 584 | x += kDrawnTileSize; |
| 585 | test_color(reporter, readBack, x, SK_ColorGREEN); |
| 586 | x += kDrawnTileSize; |
| 587 | test_color(reporter, readBack, x, SK_ColorBLUE); |
| 588 | x += kDrawnTileSize; |
| 589 | test_color(reporter, readBack, x, SK_ColorCYAN); |
| 590 | x += kDrawnTileSize; |
| 591 | test_color(reporter, readBack, x, SK_ColorMAGENTA); |
| 592 | x += kDrawnTileSize; |
| 593 | test_color(reporter, readBack, x, SK_ColorYELLOW); |
| 594 | } |