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