blob: 6659c6a633ec0c64987d48d1efe25026ab4150a6 [file] [log] [blame]
jvanverth14b88032015-08-07 12:18:54 -07001/*
2 * Copyright 2015 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
Brian Salomonfc527d22016-12-14 21:07:01 -05008#include "GrDrawAtlasOp.h"
Brian Salomon5ec9def2016-12-20 15:34:05 -05009#include "GrDrawOpTest.h"
Brian Salomon742e31d2016-12-07 17:06:19 -050010#include "GrOpFlushState.h"
jvanverth5a4d2352015-08-12 08:15:31 -070011#include "SkGr.h"
jvanverth5a4d2352015-08-12 08:15:31 -070012#include "SkRSXform.h"
Brian Salomon742e31d2016-12-07 17:06:19 -050013#include "SkRandom.h"
jvanverth14b88032015-08-07 12:18:54 -070014
Brian Salomon8c852be2017-01-04 10:44:42 -050015static sk_sp<GrGeometryProcessor> make_gp(bool hasColors,
16 GrColor color,
17 const SkMatrix& viewMatrix) {
jvanverth14b88032015-08-07 12:18:54 -070018 using namespace GrDefaultGeoProcFactory;
jvanverth14b88032015-08-07 12:18:54 -070019 Color gpColor(color);
20 if (hasColors) {
Brian Salomon3de0aee2017-01-29 09:34:17 -050021 gpColor.fType = Color::kPremulGrColorAttribute_Type;
jvanverth14b88032015-08-07 12:18:54 -070022 }
halcanary9d524f22016-03-29 09:03:52 -070023
Brian Salomon8c852be2017-01-04 10:44:42 -050024 return GrDefaultGeoProcFactory::Make(gpColor, Coverage::kSolid_Type,
25 LocalCoords::kHasExplicit_Type, viewMatrix);
jvanverth14b88032015-08-07 12:18:54 -070026}
27
Brian Salomon0088f942017-07-12 11:51:27 -040028GrDrawAtlasOp::GrDrawAtlasOp(const Helper::MakeArgs& helperArgs, GrColor color,
29 const SkMatrix& viewMatrix, GrAAType aaType, int spriteCount,
Brian Salomonfc527d22016-12-14 21:07:01 -050030 const SkRSXform* xforms, const SkRect* rects, const SkColor* colors)
Brian Salomonf99a1732017-07-14 09:24:30 -040031 : INHERITED(ClassID()), fHelper(helperArgs, aaType), fColor(color) {
jvanverth5a4d2352015-08-12 08:15:31 -070032 SkASSERT(xforms);
33 SkASSERT(rects);
halcanary9d524f22016-03-29 09:03:52 -070034
jvanverth14b88032015-08-07 12:18:54 -070035 fViewMatrix = viewMatrix;
bsalomon0432dd62016-06-30 07:19:27 -070036 Geometry& installedGeo = fGeoData.push_back();
37 installedGeo.fColor = color;
halcanary9d524f22016-03-29 09:03:52 -070038
jvanverth5a4d2352015-08-12 08:15:31 -070039 // Figure out stride and offsets
40 // Order within the vertex is: position [color] texCoord
41 size_t texOffset = sizeof(SkPoint);
Brian Salomonfc527d22016-12-14 21:07:01 -050042 size_t vertexStride = 2 * sizeof(SkPoint);
jvanverth5a4d2352015-08-12 08:15:31 -070043 fHasColors = SkToBool(colors);
jvanverth14b88032015-08-07 12:18:54 -070044 if (colors) {
jvanverth5a4d2352015-08-12 08:15:31 -070045 texOffset += sizeof(GrColor);
46 vertexStride += sizeof(GrColor);
jvanverth14b88032015-08-07 12:18:54 -070047 }
halcanary9d524f22016-03-29 09:03:52 -070048
jvanverth5a4d2352015-08-12 08:15:31 -070049 // Compute buffer size and alloc buffer
50 fQuadCount = spriteCount;
Brian Salomonfc527d22016-12-14 21:07:01 -050051 int allocSize = static_cast<int>(4 * vertexStride * spriteCount);
jvanverth5a4d2352015-08-12 08:15:31 -070052 installedGeo.fVerts.reset(allocSize);
53 uint8_t* currVertex = installedGeo.fVerts.begin();
halcanary9d524f22016-03-29 09:03:52 -070054
jvanverth5a4d2352015-08-12 08:15:31 -070055 SkRect bounds;
56 bounds.setLargestInverted();
57 int paintAlpha = GrColorUnpackA(installedGeo.fColor);
58 for (int spriteIndex = 0; spriteIndex < spriteCount; ++spriteIndex) {
59 // Transform rect
60 SkPoint quad[4];
61 const SkRect& currRect = rects[spriteIndex];
62 xforms[spriteIndex].toQuad(currRect.width(), currRect.height(), quad);
halcanary9d524f22016-03-29 09:03:52 -070063
jvanverth5a4d2352015-08-12 08:15:31 -070064 // Copy colors if necessary
65 if (colors) {
66 // convert to GrColor
67 SkColor color = colors[spriteIndex];
68 if (paintAlpha != 255) {
69 color = SkColorSetA(color, SkMulDiv255Round(SkColorGetA(color), paintAlpha));
70 }
bsalomonf1b7a1d2015-09-28 06:26:28 -070071 GrColor grColor = SkColorToPremulGrColor(color);
halcanary9d524f22016-03-29 09:03:52 -070072
Brian Salomonfc527d22016-12-14 21:07:01 -050073 *(reinterpret_cast<GrColor*>(currVertex + sizeof(SkPoint))) = grColor;
74 *(reinterpret_cast<GrColor*>(currVertex + vertexStride + sizeof(SkPoint))) = grColor;
75 *(reinterpret_cast<GrColor*>(currVertex + 2 * vertexStride + sizeof(SkPoint))) =
76 grColor;
77 *(reinterpret_cast<GrColor*>(currVertex + 3 * vertexStride + sizeof(SkPoint))) =
78 grColor;
jvanverth5a4d2352015-08-12 08:15:31 -070079 }
halcanary9d524f22016-03-29 09:03:52 -070080
jvanverth5a4d2352015-08-12 08:15:31 -070081 // Copy position and uv to verts
82 *(reinterpret_cast<SkPoint*>(currVertex)) = quad[0];
Brian Salomonfc527d22016-12-14 21:07:01 -050083 *(reinterpret_cast<SkPoint*>(currVertex + texOffset)) =
84 SkPoint::Make(currRect.fLeft, currRect.fTop);
jvanverth5a4d2352015-08-12 08:15:31 -070085 bounds.growToInclude(quad[0].fX, quad[0].fY);
86 currVertex += vertexStride;
87
88 *(reinterpret_cast<SkPoint*>(currVertex)) = quad[1];
Brian Salomonfc527d22016-12-14 21:07:01 -050089 *(reinterpret_cast<SkPoint*>(currVertex + texOffset)) =
90 SkPoint::Make(currRect.fRight, currRect.fTop);
jvanverth5a4d2352015-08-12 08:15:31 -070091 bounds.growToInclude(quad[1].fX, quad[1].fY);
92 currVertex += vertexStride;
halcanary9d524f22016-03-29 09:03:52 -070093
jvanverth5a4d2352015-08-12 08:15:31 -070094 *(reinterpret_cast<SkPoint*>(currVertex)) = quad[2];
Brian Salomonfc527d22016-12-14 21:07:01 -050095 *(reinterpret_cast<SkPoint*>(currVertex + texOffset)) =
96 SkPoint::Make(currRect.fRight, currRect.fBottom);
jvanverth5a4d2352015-08-12 08:15:31 -070097 bounds.growToInclude(quad[2].fX, quad[2].fY);
98 currVertex += vertexStride;
halcanary9d524f22016-03-29 09:03:52 -070099
jvanverth5a4d2352015-08-12 08:15:31 -0700100 *(reinterpret_cast<SkPoint*>(currVertex)) = quad[3];
Brian Salomonfc527d22016-12-14 21:07:01 -0500101 *(reinterpret_cast<SkPoint*>(currVertex + texOffset)) =
102 SkPoint::Make(currRect.fLeft, currRect.fBottom);
jvanverth5a4d2352015-08-12 08:15:31 -0700103 bounds.growToInclude(quad[3].fX, quad[3].fY);
104 currVertex += vertexStride;
105 }
halcanary9d524f22016-03-29 09:03:52 -0700106
bsalomon88cf17d2016-07-08 06:40:56 -0700107 this->setTransformedBounds(bounds, viewMatrix, HasAABloat::kNo, IsZeroArea::kNo);
jvanverth14b88032015-08-07 12:18:54 -0700108}
halcanary9d524f22016-03-29 09:03:52 -0700109
Brian Salomon0088f942017-07-12 11:51:27 -0400110SkString GrDrawAtlasOp::dumpInfo() const {
111 SkString string;
112 for (const auto& geo : fGeoData) {
113 string.appendf("Color: 0x%08x, Quads: %d\n", geo.fColor, geo.fVerts.count() / 4);
114 }
115 string += fHelper.dumpInfo();
116 string += INHERITED::dumpInfo();
117 return string;
118}
119
Brian Salomon91326c32017-08-09 16:02:19 -0400120void GrDrawAtlasOp::onPrepareDraws(Target* target) {
Brian Salomon0088f942017-07-12 11:51:27 -0400121 // Setup geometry processor
122 sk_sp<GrGeometryProcessor> gp(make_gp(this->hasColors(), this->color(), this->viewMatrix()));
123
124 int instanceCount = fGeoData.count();
125 size_t vertexStride = gp->getVertexStride();
126 SkASSERT(vertexStride ==
127 sizeof(SkPoint) + sizeof(SkPoint) + (this->hasColors() ? sizeof(GrColor) : 0));
128
129 QuadHelper helper;
130 int numQuads = this->quadCount();
131 void* verts = helper.init(target, vertexStride, numQuads);
132 if (!verts) {
133 SkDebugf("Could not allocate vertices\n");
134 return;
135 }
136
137 uint8_t* vertPtr = reinterpret_cast<uint8_t*>(verts);
138 for (int i = 0; i < instanceCount; i++) {
139 const Geometry& args = fGeoData[i];
140
141 size_t allocSize = args.fVerts.count();
142 memcpy(vertPtr, args.fVerts.begin(), allocSize);
143 vertPtr += allocSize;
144 }
145 helper.recordDraw(target, gp.get(), fHelper.makePipeline(target));
146}
147
Brian Salomonfc527d22016-12-14 21:07:01 -0500148bool GrDrawAtlasOp::onCombineIfPossible(GrOp* t, const GrCaps& caps) {
149 GrDrawAtlasOp* that = t->cast<GrDrawAtlasOp>();
bsalomonabd30f52015-08-13 13:34:48 -0700150
Brian Salomon0088f942017-07-12 11:51:27 -0400151 if (!fHelper.isCompatible(that->fHelper, caps, this->bounds(), that->bounds())) {
jvanverth14b88032015-08-07 12:18:54 -0700152 return false;
153 }
bsalomonabd30f52015-08-13 13:34:48 -0700154
Brian Salomonfc527d22016-12-14 21:07:01 -0500155 // We currently use a uniform viewmatrix for this op.
jvanverth14b88032015-08-07 12:18:54 -0700156 if (!this->viewMatrix().cheapEqualTo(that->viewMatrix())) {
157 return false;
158 }
halcanary9d524f22016-03-29 09:03:52 -0700159
jvanverth14b88032015-08-07 12:18:54 -0700160 if (this->hasColors() != that->hasColors()) {
161 return false;
162 }
halcanary9d524f22016-03-29 09:03:52 -0700163
jvanverth14b88032015-08-07 12:18:54 -0700164 if (!this->hasColors() && this->color() != that->color()) {
165 return false;
166 }
halcanary9d524f22016-03-29 09:03:52 -0700167
bsalomon0432dd62016-06-30 07:19:27 -0700168 fGeoData.push_back_n(that->fGeoData.count(), that->fGeoData.begin());
jvanverth5a4d2352015-08-12 08:15:31 -0700169 fQuadCount += that->quadCount();
halcanary9d524f22016-03-29 09:03:52 -0700170
bsalomon88cf17d2016-07-08 06:40:56 -0700171 this->joinBounds(*that);
jvanverth14b88032015-08-07 12:18:54 -0700172 return true;
173}
174
Brian Salomon0088f942017-07-12 11:51:27 -0400175GrDrawOp::FixedFunctionFlags GrDrawAtlasOp::fixedFunctionFlags() const {
176 return fHelper.fixedFunctionFlags();
177}
178
179GrDrawOp::RequiresDstTexture GrDrawAtlasOp::finalize(const GrCaps& caps,
180 const GrAppliedClip* clip) {
181 GrProcessorAnalysisColor gpColor;
182 if (this->hasColors()) {
183 gpColor.setToUnknown();
184 } else {
185 gpColor.setToConstant(fColor);
186 }
187 auto result =
188 fHelper.xpRequiresDstTexture(caps, clip, GrProcessorAnalysisCoverage::kNone, &gpColor);
189 if (gpColor.isConstant(&fColor)) {
190 fHasColors = false;
191 }
192 return result;
193}
194
Hal Canary6f6961e2017-01-31 13:50:44 -0500195#if GR_TEST_UTILS
jvanverth14b88032015-08-07 12:18:54 -0700196
jvanverth5a4d2352015-08-12 08:15:31 -0700197static SkRSXform random_xform(SkRandom* random) {
198 static const SkScalar kMinExtent = -100.f;
199 static const SkScalar kMaxExtent = 100.f;
200 static const SkScalar kMinScale = 0.1f;
201 static const SkScalar kMaxScale = 100.f;
202 static const SkScalar kMinRotate = -SK_ScalarPI;
203 static const SkScalar kMaxRotate = SK_ScalarPI;
204
205 SkRSXform xform = SkRSXform::MakeFromRadians(random->nextRangeScalar(kMinScale, kMaxScale),
206 random->nextRangeScalar(kMinRotate, kMaxRotate),
207 random->nextRangeScalar(kMinExtent, kMaxExtent),
208 random->nextRangeScalar(kMinExtent, kMaxExtent),
209 random->nextRangeScalar(kMinExtent, kMaxExtent),
210 random->nextRangeScalar(kMinExtent, kMaxExtent));
211 return xform;
jvanverth14b88032015-08-07 12:18:54 -0700212}
213
jvanverth5a4d2352015-08-12 08:15:31 -0700214static SkRect random_texRect(SkRandom* random) {
215 static const SkScalar kMinCoord = 0.0f;
216 static const SkScalar kMaxCoord = 1024.f;
halcanary9d524f22016-03-29 09:03:52 -0700217
jvanverth5a4d2352015-08-12 08:15:31 -0700218 SkRect texRect = SkRect::MakeLTRB(random->nextRangeScalar(kMinCoord, kMaxCoord),
219 random->nextRangeScalar(kMinCoord, kMaxCoord),
220 random->nextRangeScalar(kMinCoord, kMaxCoord),
221 random->nextRangeScalar(kMinCoord, kMaxCoord));
222 texRect.sort();
223 return texRect;
224}
225
Brian Salomonfc527d22016-12-14 21:07:01 -0500226static void randomize_params(uint32_t count, SkRandom* random, SkTArray<SkRSXform>* xforms,
227 SkTArray<SkRect>* texRects, SkTArray<GrColor>* colors,
228 bool hasColors) {
jvanverth14b88032015-08-07 12:18:54 -0700229 for (uint32_t v = 0; v < count; v++) {
jvanverth5a4d2352015-08-12 08:15:31 -0700230 xforms->push_back(random_xform(random));
231 texRects->push_back(random_texRect(random));
jvanverth14b88032015-08-07 12:18:54 -0700232 if (hasColors) {
233 colors->push_back(GrRandomColor(random));
234 }
235 }
236}
237
Brian Salomon0088f942017-07-12 11:51:27 -0400238GR_DRAW_OP_TEST_DEFINE(GrDrawAtlasOp) {
jvanverth14b88032015-08-07 12:18:54 -0700239 uint32_t spriteCount = random->nextRangeU(1, 100);
halcanary9d524f22016-03-29 09:03:52 -0700240
jvanverth5a4d2352015-08-12 08:15:31 -0700241 SkTArray<SkRSXform> xforms(spriteCount);
242 SkTArray<SkRect> texRects(spriteCount);
jvanverth1acf2502015-08-13 11:53:39 -0700243 SkTArray<GrColor> colors;
halcanary9d524f22016-03-29 09:03:52 -0700244
jvanverth14b88032015-08-07 12:18:54 -0700245 bool hasColors = random->nextBool();
halcanary9d524f22016-03-29 09:03:52 -0700246
Brian Salomonfc527d22016-12-14 21:07:01 -0500247 randomize_params(spriteCount, random, &xforms, &texRects, &colors, hasColors);
halcanary9d524f22016-03-29 09:03:52 -0700248
jvanverth14b88032015-08-07 12:18:54 -0700249 SkMatrix viewMatrix = GrTest::TestMatrix(random);
Brian Salomon0088f942017-07-12 11:51:27 -0400250 GrAAType aaType = GrAAType::kNone;
251 if (GrFSAAType::kUnifiedMSAA == fsaaType && random->nextBool()) {
252 aaType = GrAAType::kMSAA;
253 }
halcanary9d524f22016-03-29 09:03:52 -0700254
Brian Salomon0088f942017-07-12 11:51:27 -0400255 return GrDrawAtlasOp::Make(std::move(paint), viewMatrix, aaType, spriteCount, xforms.begin(),
256 texRects.begin(), hasColors ? colors.begin() : nullptr);
jvanverth14b88032015-08-07 12:18:54 -0700257}
258
259#endif