GrDrawAtlasOp.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. /*
  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. #include "src/gpu/ops/GrDrawAtlasOp.h"
  8. #include "include/core/SkRSXform.h"
  9. #include "include/private/GrRecordingContext.h"
  10. #include "include/utils/SkRandom.h"
  11. #include "src/core/SkRectPriv.h"
  12. #include "src/gpu/GrCaps.h"
  13. #include "src/gpu/GrDefaultGeoProcFactory.h"
  14. #include "src/gpu/GrDrawOpTest.h"
  15. #include "src/gpu/GrOpFlushState.h"
  16. #include "src/gpu/GrRecordingContextPriv.h"
  17. #include "src/gpu/SkGr.h"
  18. #include "src/gpu/ops/GrSimpleMeshDrawOpHelper.h"
  19. namespace {
  20. class DrawAtlasOp final : public GrMeshDrawOp {
  21. private:
  22. using Helper = GrSimpleMeshDrawOpHelper;
  23. public:
  24. DEFINE_OP_CLASS_ID
  25. DrawAtlasOp(const Helper::MakeArgs&, const SkPMColor4f& color,
  26. const SkMatrix& viewMatrix, GrAAType, int spriteCount, const SkRSXform* xforms,
  27. const SkRect* rects, const SkColor* colors);
  28. const char* name() const override { return "DrawAtlasOp"; }
  29. void visitProxies(const VisitProxyFunc& func) const override {
  30. fHelper.visitProxies(func);
  31. }
  32. #ifdef SK_DEBUG
  33. SkString dumpInfo() const override;
  34. #endif
  35. FixedFunctionFlags fixedFunctionFlags() const override;
  36. GrProcessorSet::Analysis finalize(const GrCaps&, const GrAppliedClip*,
  37. bool hasMixedSampledCoverage, GrClampType) override;
  38. private:
  39. void onPrepareDraws(Target*) override;
  40. void onExecute(GrOpFlushState*, const SkRect& chainBounds) override;
  41. const SkPMColor4f& color() const { return fColor; }
  42. const SkMatrix& viewMatrix() const { return fViewMatrix; }
  43. bool hasColors() const { return fHasColors; }
  44. int quadCount() const { return fQuadCount; }
  45. CombineResult onCombineIfPossible(GrOp* t, const GrCaps&) override;
  46. struct Geometry {
  47. SkPMColor4f fColor;
  48. SkTArray<uint8_t, true> fVerts;
  49. };
  50. SkSTArray<1, Geometry, true> fGeoData;
  51. Helper fHelper;
  52. SkMatrix fViewMatrix;
  53. SkPMColor4f fColor;
  54. int fQuadCount;
  55. bool fHasColors;
  56. typedef GrMeshDrawOp INHERITED;
  57. };
  58. static sk_sp<GrGeometryProcessor> make_gp(const GrShaderCaps* shaderCaps,
  59. bool hasColors,
  60. const SkPMColor4f& color,
  61. const SkMatrix& viewMatrix) {
  62. using namespace GrDefaultGeoProcFactory;
  63. Color gpColor(color);
  64. if (hasColors) {
  65. gpColor.fType = Color::kPremulGrColorAttribute_Type;
  66. }
  67. return GrDefaultGeoProcFactory::Make(shaderCaps, gpColor, Coverage::kSolid_Type,
  68. LocalCoords::kHasExplicit_Type, viewMatrix);
  69. }
  70. DrawAtlasOp::DrawAtlasOp(const Helper::MakeArgs& helperArgs, const SkPMColor4f& color,
  71. const SkMatrix& viewMatrix, GrAAType aaType, int spriteCount,
  72. const SkRSXform* xforms, const SkRect* rects, const SkColor* colors)
  73. : INHERITED(ClassID()), fHelper(helperArgs, aaType), fColor(color) {
  74. SkASSERT(xforms);
  75. SkASSERT(rects);
  76. fViewMatrix = viewMatrix;
  77. Geometry& installedGeo = fGeoData.push_back();
  78. installedGeo.fColor = color;
  79. // Figure out stride and offsets
  80. // Order within the vertex is: position [color] texCoord
  81. size_t texOffset = sizeof(SkPoint);
  82. size_t vertexStride = 2 * sizeof(SkPoint);
  83. fHasColors = SkToBool(colors);
  84. if (colors) {
  85. texOffset += sizeof(GrColor);
  86. vertexStride += sizeof(GrColor);
  87. }
  88. // Compute buffer size and alloc buffer
  89. fQuadCount = spriteCount;
  90. int allocSize = static_cast<int>(4 * vertexStride * spriteCount);
  91. installedGeo.fVerts.reset(allocSize);
  92. uint8_t* currVertex = installedGeo.fVerts.begin();
  93. SkRect bounds = SkRectPriv::MakeLargestInverted();
  94. // TODO4F: Preserve float colors
  95. int paintAlpha = GrColorUnpackA(installedGeo.fColor.toBytes_RGBA());
  96. for (int spriteIndex = 0; spriteIndex < spriteCount; ++spriteIndex) {
  97. // Transform rect
  98. SkPoint strip[4];
  99. const SkRect& currRect = rects[spriteIndex];
  100. xforms[spriteIndex].toTriStrip(currRect.width(), currRect.height(), strip);
  101. // Copy colors if necessary
  102. if (colors) {
  103. // convert to GrColor
  104. SkColor color = colors[spriteIndex];
  105. if (paintAlpha != 255) {
  106. color = SkColorSetA(color, SkMulDiv255Round(SkColorGetA(color), paintAlpha));
  107. }
  108. GrColor grColor = SkColorToPremulGrColor(color);
  109. *(reinterpret_cast<GrColor*>(currVertex + sizeof(SkPoint))) = grColor;
  110. *(reinterpret_cast<GrColor*>(currVertex + vertexStride + sizeof(SkPoint))) = grColor;
  111. *(reinterpret_cast<GrColor*>(currVertex + 2 * vertexStride + sizeof(SkPoint))) =
  112. grColor;
  113. *(reinterpret_cast<GrColor*>(currVertex + 3 * vertexStride + sizeof(SkPoint))) =
  114. grColor;
  115. }
  116. // Copy position and uv to verts
  117. *(reinterpret_cast<SkPoint*>(currVertex)) = strip[0];
  118. *(reinterpret_cast<SkPoint*>(currVertex + texOffset)) =
  119. SkPoint::Make(currRect.fLeft, currRect.fTop);
  120. SkRectPriv::GrowToInclude(&bounds, strip[0]);
  121. currVertex += vertexStride;
  122. *(reinterpret_cast<SkPoint*>(currVertex)) = strip[1];
  123. *(reinterpret_cast<SkPoint*>(currVertex + texOffset)) =
  124. SkPoint::Make(currRect.fLeft, currRect.fBottom);
  125. SkRectPriv::GrowToInclude(&bounds, strip[1]);
  126. currVertex += vertexStride;
  127. *(reinterpret_cast<SkPoint*>(currVertex)) = strip[2];
  128. *(reinterpret_cast<SkPoint*>(currVertex + texOffset)) =
  129. SkPoint::Make(currRect.fRight, currRect.fTop);
  130. SkRectPriv::GrowToInclude(&bounds, strip[2]);
  131. currVertex += vertexStride;
  132. *(reinterpret_cast<SkPoint*>(currVertex)) = strip[3];
  133. *(reinterpret_cast<SkPoint*>(currVertex + texOffset)) =
  134. SkPoint::Make(currRect.fRight, currRect.fBottom);
  135. SkRectPriv::GrowToInclude(&bounds, strip[3]);
  136. currVertex += vertexStride;
  137. }
  138. this->setTransformedBounds(bounds, viewMatrix, HasAABloat::kNo, IsZeroArea::kNo);
  139. }
  140. #ifdef SK_DEBUG
  141. SkString DrawAtlasOp::dumpInfo() const {
  142. SkString string;
  143. for (const auto& geo : fGeoData) {
  144. string.appendf("Color: 0x%08x, Quads: %d\n", geo.fColor.toBytes_RGBA(),
  145. geo.fVerts.count() / 4);
  146. }
  147. string += fHelper.dumpInfo();
  148. string += INHERITED::dumpInfo();
  149. return string;
  150. }
  151. #endif
  152. void DrawAtlasOp::onPrepareDraws(Target* target) {
  153. // Setup geometry processor
  154. sk_sp<GrGeometryProcessor> gp(make_gp(target->caps().shaderCaps(),
  155. this->hasColors(),
  156. this->color(),
  157. this->viewMatrix()));
  158. int instanceCount = fGeoData.count();
  159. size_t vertexStride = gp->vertexStride();
  160. int numQuads = this->quadCount();
  161. QuadHelper helper(target, vertexStride, numQuads);
  162. void* verts = helper.vertices();
  163. if (!verts) {
  164. SkDebugf("Could not allocate vertices\n");
  165. return;
  166. }
  167. uint8_t* vertPtr = reinterpret_cast<uint8_t*>(verts);
  168. for (int i = 0; i < instanceCount; i++) {
  169. const Geometry& args = fGeoData[i];
  170. size_t allocSize = args.fVerts.count();
  171. memcpy(vertPtr, args.fVerts.begin(), allocSize);
  172. vertPtr += allocSize;
  173. }
  174. helper.recordDraw(target, std::move(gp));
  175. }
  176. void DrawAtlasOp::onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) {
  177. fHelper.executeDrawsAndUploads(this, flushState, chainBounds);
  178. }
  179. GrOp::CombineResult DrawAtlasOp::onCombineIfPossible(GrOp* t, const GrCaps& caps) {
  180. DrawAtlasOp* that = t->cast<DrawAtlasOp>();
  181. if (!fHelper.isCompatible(that->fHelper, caps, this->bounds(), that->bounds())) {
  182. return CombineResult::kCannotCombine;
  183. }
  184. // We currently use a uniform viewmatrix for this op.
  185. if (!this->viewMatrix().cheapEqualTo(that->viewMatrix())) {
  186. return CombineResult::kCannotCombine;
  187. }
  188. if (this->hasColors() != that->hasColors()) {
  189. return CombineResult::kCannotCombine;
  190. }
  191. if (!this->hasColors() && this->color() != that->color()) {
  192. return CombineResult::kCannotCombine;
  193. }
  194. fGeoData.push_back_n(that->fGeoData.count(), that->fGeoData.begin());
  195. fQuadCount += that->quadCount();
  196. return CombineResult::kMerged;
  197. }
  198. GrDrawOp::FixedFunctionFlags DrawAtlasOp::fixedFunctionFlags() const {
  199. return fHelper.fixedFunctionFlags();
  200. }
  201. GrProcessorSet::Analysis DrawAtlasOp::finalize(
  202. const GrCaps& caps, const GrAppliedClip* clip, bool hasMixedSampledCoverage,
  203. GrClampType clampType) {
  204. GrProcessorAnalysisColor gpColor;
  205. if (this->hasColors()) {
  206. gpColor.setToUnknown();
  207. } else {
  208. gpColor.setToConstant(fColor);
  209. }
  210. auto result = fHelper.finalizeProcessors(caps, clip, hasMixedSampledCoverage, clampType,
  211. GrProcessorAnalysisCoverage::kNone, &gpColor);
  212. if (gpColor.isConstant(&fColor)) {
  213. fHasColors = false;
  214. }
  215. return result;
  216. }
  217. } // anonymous namespace
  218. std::unique_ptr<GrDrawOp> GrDrawAtlasOp::Make(GrRecordingContext* context,
  219. GrPaint&& paint,
  220. const SkMatrix& viewMatrix,
  221. GrAAType aaType,
  222. int spriteCount,
  223. const SkRSXform* xforms,
  224. const SkRect* rects,
  225. const SkColor* colors) {
  226. return GrSimpleMeshDrawOpHelper::FactoryHelper<DrawAtlasOp>(context, std::move(paint),
  227. viewMatrix, aaType,
  228. spriteCount, xforms,
  229. rects, colors);
  230. }
  231. #if GR_TEST_UTILS
  232. static SkRSXform random_xform(SkRandom* random) {
  233. static const SkScalar kMinExtent = -100.f;
  234. static const SkScalar kMaxExtent = 100.f;
  235. static const SkScalar kMinScale = 0.1f;
  236. static const SkScalar kMaxScale = 100.f;
  237. static const SkScalar kMinRotate = -SK_ScalarPI;
  238. static const SkScalar kMaxRotate = SK_ScalarPI;
  239. SkRSXform xform = SkRSXform::MakeFromRadians(random->nextRangeScalar(kMinScale, kMaxScale),
  240. random->nextRangeScalar(kMinRotate, kMaxRotate),
  241. random->nextRangeScalar(kMinExtent, kMaxExtent),
  242. random->nextRangeScalar(kMinExtent, kMaxExtent),
  243. random->nextRangeScalar(kMinExtent, kMaxExtent),
  244. random->nextRangeScalar(kMinExtent, kMaxExtent));
  245. return xform;
  246. }
  247. static SkRect random_texRect(SkRandom* random) {
  248. static const SkScalar kMinCoord = 0.0f;
  249. static const SkScalar kMaxCoord = 1024.f;
  250. SkRect texRect = SkRect::MakeLTRB(random->nextRangeScalar(kMinCoord, kMaxCoord),
  251. random->nextRangeScalar(kMinCoord, kMaxCoord),
  252. random->nextRangeScalar(kMinCoord, kMaxCoord),
  253. random->nextRangeScalar(kMinCoord, kMaxCoord));
  254. texRect.sort();
  255. return texRect;
  256. }
  257. static void randomize_params(uint32_t count, SkRandom* random, SkTArray<SkRSXform>* xforms,
  258. SkTArray<SkRect>* texRects, SkTArray<GrColor>* colors,
  259. bool hasColors) {
  260. for (uint32_t v = 0; v < count; v++) {
  261. xforms->push_back(random_xform(random));
  262. texRects->push_back(random_texRect(random));
  263. if (hasColors) {
  264. colors->push_back(GrRandomColor(random));
  265. }
  266. }
  267. }
  268. GR_DRAW_OP_TEST_DEFINE(DrawAtlasOp) {
  269. uint32_t spriteCount = random->nextRangeU(1, 100);
  270. SkTArray<SkRSXform> xforms(spriteCount);
  271. SkTArray<SkRect> texRects(spriteCount);
  272. SkTArray<GrColor> colors;
  273. bool hasColors = random->nextBool();
  274. randomize_params(spriteCount, random, &xforms, &texRects, &colors, hasColors);
  275. SkMatrix viewMatrix = GrTest::TestMatrix(random);
  276. GrAAType aaType = GrAAType::kNone;
  277. if (numSamples > 1 && random->nextBool()) {
  278. aaType = GrAAType::kMSAA;
  279. }
  280. return GrDrawAtlasOp::Make(context, std::move(paint), viewMatrix, aaType, spriteCount,
  281. xforms.begin(), texRects.begin(),
  282. hasColors ? colors.begin() : nullptr);
  283. }
  284. #endif