blob: 107b0be397a45be321d02ab71a3caa2e9e505d18 [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"
Mike Reed274218e2018-01-08 15:05:02 -050014#include "SkRectPriv.h"
jvanverth14b88032015-08-07 12:18:54 -070015
Ruiqi Maob609e6d2018-07-17 10:19:38 -040016static sk_sp<GrGeometryProcessor> make_gp(const GrShaderCaps* shaderCaps,
17 bool hasColors,
Brian Salomon8c852be2017-01-04 10:44:42 -050018 GrColor color,
19 const SkMatrix& viewMatrix) {
jvanverth14b88032015-08-07 12:18:54 -070020 using namespace GrDefaultGeoProcFactory;
jvanverth14b88032015-08-07 12:18:54 -070021 Color gpColor(color);
22 if (hasColors) {
Brian Salomon3de0aee2017-01-29 09:34:17 -050023 gpColor.fType = Color::kPremulGrColorAttribute_Type;
jvanverth14b88032015-08-07 12:18:54 -070024 }
halcanary9d524f22016-03-29 09:03:52 -070025
Ruiqi Maob609e6d2018-07-17 10:19:38 -040026 return GrDefaultGeoProcFactory::Make(shaderCaps, gpColor, Coverage::kSolid_Type,
Brian Salomon8c852be2017-01-04 10:44:42 -050027 LocalCoords::kHasExplicit_Type, viewMatrix);
jvanverth14b88032015-08-07 12:18:54 -070028}
29
Brian Salomon0088f942017-07-12 11:51:27 -040030GrDrawAtlasOp::GrDrawAtlasOp(const Helper::MakeArgs& helperArgs, GrColor color,
31 const SkMatrix& viewMatrix, GrAAType aaType, int spriteCount,
Brian Salomonfc527d22016-12-14 21:07:01 -050032 const SkRSXform* xforms, const SkRect* rects, const SkColor* colors)
Brian Salomonf99a1732017-07-14 09:24:30 -040033 : INHERITED(ClassID()), fHelper(helperArgs, aaType), fColor(color) {
jvanverth5a4d2352015-08-12 08:15:31 -070034 SkASSERT(xforms);
35 SkASSERT(rects);
halcanary9d524f22016-03-29 09:03:52 -070036
jvanverth14b88032015-08-07 12:18:54 -070037 fViewMatrix = viewMatrix;
bsalomon0432dd62016-06-30 07:19:27 -070038 Geometry& installedGeo = fGeoData.push_back();
39 installedGeo.fColor = color;
halcanary9d524f22016-03-29 09:03:52 -070040
jvanverth5a4d2352015-08-12 08:15:31 -070041 // Figure out stride and offsets
42 // Order within the vertex is: position [color] texCoord
43 size_t texOffset = sizeof(SkPoint);
Brian Salomonfc527d22016-12-14 21:07:01 -050044 size_t vertexStride = 2 * sizeof(SkPoint);
jvanverth5a4d2352015-08-12 08:15:31 -070045 fHasColors = SkToBool(colors);
jvanverth14b88032015-08-07 12:18:54 -070046 if (colors) {
jvanverth5a4d2352015-08-12 08:15:31 -070047 texOffset += sizeof(GrColor);
48 vertexStride += sizeof(GrColor);
jvanverth14b88032015-08-07 12:18:54 -070049 }
halcanary9d524f22016-03-29 09:03:52 -070050
jvanverth5a4d2352015-08-12 08:15:31 -070051 // Compute buffer size and alloc buffer
52 fQuadCount = spriteCount;
Brian Salomonfc527d22016-12-14 21:07:01 -050053 int allocSize = static_cast<int>(4 * vertexStride * spriteCount);
jvanverth5a4d2352015-08-12 08:15:31 -070054 installedGeo.fVerts.reset(allocSize);
55 uint8_t* currVertex = installedGeo.fVerts.begin();
halcanary9d524f22016-03-29 09:03:52 -070056
Mike Reed274218e2018-01-08 15:05:02 -050057 SkRect bounds = SkRectPriv::MakeLargestInverted();
jvanverth5a4d2352015-08-12 08:15:31 -070058 int paintAlpha = GrColorUnpackA(installedGeo.fColor);
59 for (int spriteIndex = 0; spriteIndex < spriteCount; ++spriteIndex) {
60 // Transform rect
Brian Salomon57caa662017-10-18 12:21:05 +000061 SkPoint strip[4];
jvanverth5a4d2352015-08-12 08:15:31 -070062 const SkRect& currRect = rects[spriteIndex];
Brian Salomon57caa662017-10-18 12:21:05 +000063 xforms[spriteIndex].toTriStrip(currRect.width(), currRect.height(), strip);
halcanary9d524f22016-03-29 09:03:52 -070064
jvanverth5a4d2352015-08-12 08:15:31 -070065 // Copy colors if necessary
66 if (colors) {
67 // convert to GrColor
68 SkColor color = colors[spriteIndex];
69 if (paintAlpha != 255) {
70 color = SkColorSetA(color, SkMulDiv255Round(SkColorGetA(color), paintAlpha));
71 }
bsalomonf1b7a1d2015-09-28 06:26:28 -070072 GrColor grColor = SkColorToPremulGrColor(color);
halcanary9d524f22016-03-29 09:03:52 -070073
Brian Salomonfc527d22016-12-14 21:07:01 -050074 *(reinterpret_cast<GrColor*>(currVertex + sizeof(SkPoint))) = grColor;
75 *(reinterpret_cast<GrColor*>(currVertex + vertexStride + sizeof(SkPoint))) = grColor;
76 *(reinterpret_cast<GrColor*>(currVertex + 2 * vertexStride + sizeof(SkPoint))) =
77 grColor;
78 *(reinterpret_cast<GrColor*>(currVertex + 3 * vertexStride + sizeof(SkPoint))) =
79 grColor;
jvanverth5a4d2352015-08-12 08:15:31 -070080 }
halcanary9d524f22016-03-29 09:03:52 -070081
jvanverth5a4d2352015-08-12 08:15:31 -070082 // Copy position and uv to verts
Brian Salomon57caa662017-10-18 12:21:05 +000083 *(reinterpret_cast<SkPoint*>(currVertex)) = strip[0];
Brian Salomonfc527d22016-12-14 21:07:01 -050084 *(reinterpret_cast<SkPoint*>(currVertex + texOffset)) =
85 SkPoint::Make(currRect.fLeft, currRect.fTop);
Mike Reed185ffe92018-01-08 17:09:54 -050086 SkRectPriv::GrowToInclude(&bounds, strip[0]);
jvanverth5a4d2352015-08-12 08:15:31 -070087 currVertex += vertexStride;
88
Brian Salomon57caa662017-10-18 12:21:05 +000089 *(reinterpret_cast<SkPoint*>(currVertex)) = strip[1];
Brian Salomon62563de2017-10-17 19:14:05 +000090 *(reinterpret_cast<SkPoint*>(currVertex + texOffset)) =
91 SkPoint::Make(currRect.fLeft, currRect.fBottom);
Mike Reed185ffe92018-01-08 17:09:54 -050092 SkRectPriv::GrowToInclude(&bounds, strip[1]);
Brian Salomon57caa662017-10-18 12:21:05 +000093 currVertex += vertexStride;
94
95 *(reinterpret_cast<SkPoint*>(currVertex)) = strip[2];
96 *(reinterpret_cast<SkPoint*>(currVertex + texOffset)) =
97 SkPoint::Make(currRect.fRight, currRect.fTop);
Mike Reed185ffe92018-01-08 17:09:54 -050098 SkRectPriv::GrowToInclude(&bounds, strip[2]);
Brian Salomon57caa662017-10-18 12:21:05 +000099 currVertex += vertexStride;
100
101 *(reinterpret_cast<SkPoint*>(currVertex)) = strip[3];
102 *(reinterpret_cast<SkPoint*>(currVertex + texOffset)) =
103 SkPoint::Make(currRect.fRight, currRect.fBottom);
Mike Reed185ffe92018-01-08 17:09:54 -0500104 SkRectPriv::GrowToInclude(&bounds, strip[3]);
jvanverth5a4d2352015-08-12 08:15:31 -0700105 currVertex += vertexStride;
106 }
halcanary9d524f22016-03-29 09:03:52 -0700107
bsalomon88cf17d2016-07-08 06:40:56 -0700108 this->setTransformedBounds(bounds, viewMatrix, HasAABloat::kNo, IsZeroArea::kNo);
jvanverth14b88032015-08-07 12:18:54 -0700109}
halcanary9d524f22016-03-29 09:03:52 -0700110
Brian Salomon0088f942017-07-12 11:51:27 -0400111SkString GrDrawAtlasOp::dumpInfo() const {
112 SkString string;
113 for (const auto& geo : fGeoData) {
114 string.appendf("Color: 0x%08x, Quads: %d\n", geo.fColor, geo.fVerts.count() / 4);
115 }
116 string += fHelper.dumpInfo();
117 string += INHERITED::dumpInfo();
118 return string;
119}
120
Brian Salomon91326c32017-08-09 16:02:19 -0400121void GrDrawAtlasOp::onPrepareDraws(Target* target) {
Brian Salomon0088f942017-07-12 11:51:27 -0400122 // Setup geometry processor
Ruiqi Maob609e6d2018-07-17 10:19:38 -0400123 sk_sp<GrGeometryProcessor> gp(make_gp(target->caps().shaderCaps(),
124 this->hasColors(),
125 this->color(),
126 this->viewMatrix()));
Brian Salomon0088f942017-07-12 11:51:27 -0400127
128 int instanceCount = fGeoData.count();
Brian Salomon92be2f72018-06-19 14:33:47 -0400129 size_t vertexStride =
130 sizeof(SkPoint) + sizeof(SkPoint) + (this->hasColors() ? sizeof(GrColor) : 0);
131 SkASSERT(vertexStride == gp->debugOnly_vertexStride());
Brian Salomon0088f942017-07-12 11:51:27 -0400132
Brian Salomon0088f942017-07-12 11:51:27 -0400133 int numQuads = this->quadCount();
Brian Salomon7eae3e02018-08-07 14:02:38 +0000134 QuadHelper helper(target, vertexStride, numQuads);
135 void* verts = helper.vertices();
Brian Salomon0088f942017-07-12 11:51:27 -0400136 if (!verts) {
137 SkDebugf("Could not allocate vertices\n");
138 return;
139 }
140
141 uint8_t* vertPtr = reinterpret_cast<uint8_t*>(verts);
142 for (int i = 0; i < instanceCount; i++) {
143 const Geometry& args = fGeoData[i];
144
145 size_t allocSize = args.fVerts.count();
146 memcpy(vertPtr, args.fVerts.begin(), allocSize);
147 vertPtr += allocSize;
148 }
Brian Salomon49348902018-06-26 09:12:38 -0400149 auto pipe = fHelper.makePipeline(target);
Brian Salomon7eae3e02018-08-07 14:02:38 +0000150 helper.recordDraw(target, std::move(gp), pipe.fPipeline, pipe.fFixedDynamicState);
Brian Salomon0088f942017-07-12 11:51:27 -0400151}
152
Brian Salomon7eae3e02018-08-07 14:02:38 +0000153GrOp::CombineResult GrDrawAtlasOp::onCombineIfPossible(GrOp* t, const GrCaps& caps) {
Brian Salomonfc527d22016-12-14 21:07:01 -0500154 GrDrawAtlasOp* that = t->cast<GrDrawAtlasOp>();
bsalomonabd30f52015-08-13 13:34:48 -0700155
Brian Salomon0088f942017-07-12 11:51:27 -0400156 if (!fHelper.isCompatible(that->fHelper, caps, this->bounds(), that->bounds())) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000157 return CombineResult::kCannotCombine;
jvanverth14b88032015-08-07 12:18:54 -0700158 }
bsalomonabd30f52015-08-13 13:34:48 -0700159
Brian Salomonfc527d22016-12-14 21:07:01 -0500160 // We currently use a uniform viewmatrix for this op.
jvanverth14b88032015-08-07 12:18:54 -0700161 if (!this->viewMatrix().cheapEqualTo(that->viewMatrix())) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000162 return CombineResult::kCannotCombine;
jvanverth14b88032015-08-07 12:18:54 -0700163 }
halcanary9d524f22016-03-29 09:03:52 -0700164
jvanverth14b88032015-08-07 12:18:54 -0700165 if (this->hasColors() != that->hasColors()) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000166 return CombineResult::kCannotCombine;
jvanverth14b88032015-08-07 12:18:54 -0700167 }
halcanary9d524f22016-03-29 09:03:52 -0700168
jvanverth14b88032015-08-07 12:18:54 -0700169 if (!this->hasColors() && this->color() != that->color()) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000170 return CombineResult::kCannotCombine;
jvanverth14b88032015-08-07 12:18:54 -0700171 }
halcanary9d524f22016-03-29 09:03:52 -0700172
bsalomon0432dd62016-06-30 07:19:27 -0700173 fGeoData.push_back_n(that->fGeoData.count(), that->fGeoData.begin());
jvanverth5a4d2352015-08-12 08:15:31 -0700174 fQuadCount += that->quadCount();
halcanary9d524f22016-03-29 09:03:52 -0700175
bsalomon88cf17d2016-07-08 06:40:56 -0700176 this->joinBounds(*that);
Brian Salomon7eae3e02018-08-07 14:02:38 +0000177 return CombineResult::kMerged;
jvanverth14b88032015-08-07 12:18:54 -0700178}
179
Brian Salomon0088f942017-07-12 11:51:27 -0400180GrDrawOp::FixedFunctionFlags GrDrawAtlasOp::fixedFunctionFlags() const {
181 return fHelper.fixedFunctionFlags();
182}
183
184GrDrawOp::RequiresDstTexture GrDrawAtlasOp::finalize(const GrCaps& caps,
Brian Osman532b3f92018-07-11 10:02:07 -0400185 const GrAppliedClip* clip) {
Brian Salomon0088f942017-07-12 11:51:27 -0400186 GrProcessorAnalysisColor gpColor;
187 if (this->hasColors()) {
188 gpColor.setToUnknown();
189 } else {
190 gpColor.setToConstant(fColor);
191 }
Brian Osman532b3f92018-07-11 10:02:07 -0400192 auto result = fHelper.xpRequiresDstTexture(caps, clip, GrProcessorAnalysisCoverage::kNone,
193 &gpColor);
Brian Salomon0088f942017-07-12 11:51:27 -0400194 if (gpColor.isConstant(&fColor)) {
195 fHasColors = false;
196 }
197 return result;
198}
199
Hal Canary6f6961e2017-01-31 13:50:44 -0500200#if GR_TEST_UTILS
jvanverth14b88032015-08-07 12:18:54 -0700201
jvanverth5a4d2352015-08-12 08:15:31 -0700202static SkRSXform random_xform(SkRandom* random) {
203 static const SkScalar kMinExtent = -100.f;
204 static const SkScalar kMaxExtent = 100.f;
205 static const SkScalar kMinScale = 0.1f;
206 static const SkScalar kMaxScale = 100.f;
207 static const SkScalar kMinRotate = -SK_ScalarPI;
208 static const SkScalar kMaxRotate = SK_ScalarPI;
209
210 SkRSXform xform = SkRSXform::MakeFromRadians(random->nextRangeScalar(kMinScale, kMaxScale),
211 random->nextRangeScalar(kMinRotate, kMaxRotate),
212 random->nextRangeScalar(kMinExtent, kMaxExtent),
213 random->nextRangeScalar(kMinExtent, kMaxExtent),
214 random->nextRangeScalar(kMinExtent, kMaxExtent),
215 random->nextRangeScalar(kMinExtent, kMaxExtent));
216 return xform;
jvanverth14b88032015-08-07 12:18:54 -0700217}
218
jvanverth5a4d2352015-08-12 08:15:31 -0700219static SkRect random_texRect(SkRandom* random) {
220 static const SkScalar kMinCoord = 0.0f;
221 static const SkScalar kMaxCoord = 1024.f;
halcanary9d524f22016-03-29 09:03:52 -0700222
jvanverth5a4d2352015-08-12 08:15:31 -0700223 SkRect texRect = SkRect::MakeLTRB(random->nextRangeScalar(kMinCoord, kMaxCoord),
224 random->nextRangeScalar(kMinCoord, kMaxCoord),
225 random->nextRangeScalar(kMinCoord, kMaxCoord),
226 random->nextRangeScalar(kMinCoord, kMaxCoord));
227 texRect.sort();
228 return texRect;
229}
230
Brian Salomonfc527d22016-12-14 21:07:01 -0500231static void randomize_params(uint32_t count, SkRandom* random, SkTArray<SkRSXform>* xforms,
232 SkTArray<SkRect>* texRects, SkTArray<GrColor>* colors,
233 bool hasColors) {
jvanverth14b88032015-08-07 12:18:54 -0700234 for (uint32_t v = 0; v < count; v++) {
jvanverth5a4d2352015-08-12 08:15:31 -0700235 xforms->push_back(random_xform(random));
236 texRects->push_back(random_texRect(random));
jvanverth14b88032015-08-07 12:18:54 -0700237 if (hasColors) {
238 colors->push_back(GrRandomColor(random));
239 }
240 }
241}
242
Brian Salomon0088f942017-07-12 11:51:27 -0400243GR_DRAW_OP_TEST_DEFINE(GrDrawAtlasOp) {
jvanverth14b88032015-08-07 12:18:54 -0700244 uint32_t spriteCount = random->nextRangeU(1, 100);
halcanary9d524f22016-03-29 09:03:52 -0700245
jvanverth5a4d2352015-08-12 08:15:31 -0700246 SkTArray<SkRSXform> xforms(spriteCount);
247 SkTArray<SkRect> texRects(spriteCount);
jvanverth1acf2502015-08-13 11:53:39 -0700248 SkTArray<GrColor> colors;
halcanary9d524f22016-03-29 09:03:52 -0700249
jvanverth14b88032015-08-07 12:18:54 -0700250 bool hasColors = random->nextBool();
halcanary9d524f22016-03-29 09:03:52 -0700251
Brian Salomonfc527d22016-12-14 21:07:01 -0500252 randomize_params(spriteCount, random, &xforms, &texRects, &colors, hasColors);
halcanary9d524f22016-03-29 09:03:52 -0700253
jvanverth14b88032015-08-07 12:18:54 -0700254 SkMatrix viewMatrix = GrTest::TestMatrix(random);
Brian Salomon0088f942017-07-12 11:51:27 -0400255 GrAAType aaType = GrAAType::kNone;
256 if (GrFSAAType::kUnifiedMSAA == fsaaType && random->nextBool()) {
257 aaType = GrAAType::kMSAA;
258 }
halcanary9d524f22016-03-29 09:03:52 -0700259
Robert Phillips7c525e62018-06-12 10:11:12 -0400260 return GrDrawAtlasOp::Make(context, std::move(paint), viewMatrix, aaType, spriteCount,
261 xforms.begin(), texRects.begin(),
262 hasColors ? colors.begin() : nullptr);
jvanverth14b88032015-08-07 12:18:54 -0700263}
264
265#endif