SkDraw_vertices.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  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. #include "include/core/SkString.h"
  8. #include "include/private/SkNx.h"
  9. #include "src/core/SkArenaAlloc.h"
  10. #include "src/core/SkAutoBlitterChoose.h"
  11. #include "src/core/SkConvertPixels.h"
  12. #include "src/core/SkCoreBlitters.h"
  13. #include "src/core/SkDraw.h"
  14. #include "src/core/SkRasterClip.h"
  15. #include "src/core/SkRasterPipeline.h"
  16. #include "src/core/SkScan.h"
  17. #include "src/core/SkVertState.h"
  18. #include "src/shaders/SkComposeShader.h"
  19. #include "src/shaders/SkShaderBase.h"
  20. struct Matrix43 {
  21. float fMat[12]; // column major
  22. Sk4f map(float x, float y) const {
  23. return Sk4f::Load(&fMat[0]) * x + Sk4f::Load(&fMat[4]) * y + Sk4f::Load(&fMat[8]);
  24. }
  25. void setConcat(const Matrix43& a, const SkMatrix& b) {
  26. fMat[ 0] = a.dot(0, b.getScaleX(), b.getSkewY());
  27. fMat[ 1] = a.dot(1, b.getScaleX(), b.getSkewY());
  28. fMat[ 2] = a.dot(2, b.getScaleX(), b.getSkewY());
  29. fMat[ 3] = a.dot(3, b.getScaleX(), b.getSkewY());
  30. fMat[ 4] = a.dot(0, b.getSkewX(), b.getScaleY());
  31. fMat[ 5] = a.dot(1, b.getSkewX(), b.getScaleY());
  32. fMat[ 6] = a.dot(2, b.getSkewX(), b.getScaleY());
  33. fMat[ 7] = a.dot(3, b.getSkewX(), b.getScaleY());
  34. fMat[ 8] = a.dot(0, b.getTranslateX(), b.getTranslateY()) + a.fMat[ 8];
  35. fMat[ 9] = a.dot(1, b.getTranslateX(), b.getTranslateY()) + a.fMat[ 9];
  36. fMat[10] = a.dot(2, b.getTranslateX(), b.getTranslateY()) + a.fMat[10];
  37. fMat[11] = a.dot(3, b.getTranslateX(), b.getTranslateY()) + a.fMat[11];
  38. }
  39. private:
  40. float dot(int index, float x, float y) const {
  41. return fMat[index + 0] * x + fMat[index + 4] * y;
  42. }
  43. };
  44. static SkScan::HairRCProc ChooseHairProc(bool doAntiAlias) {
  45. return doAntiAlias ? SkScan::AntiHairLine : SkScan::HairLine;
  46. }
  47. static bool SK_WARN_UNUSED_RESULT
  48. texture_to_matrix(const VertState& state, const SkPoint verts[], const SkPoint texs[],
  49. SkMatrix* matrix) {
  50. SkPoint src[3], dst[3];
  51. src[0] = texs[state.f0];
  52. src[1] = texs[state.f1];
  53. src[2] = texs[state.f2];
  54. dst[0] = verts[state.f0];
  55. dst[1] = verts[state.f1];
  56. dst[2] = verts[state.f2];
  57. return matrix->setPolyToPoly(src, dst, 3);
  58. }
  59. class SkTriColorShader : public SkShaderBase {
  60. public:
  61. SkTriColorShader(bool isOpaque) : fIsOpaque(isOpaque) {}
  62. Matrix43* getMatrix43() { return &fM43; }
  63. bool isOpaque() const override { return fIsOpaque; }
  64. protected:
  65. #ifdef SK_ENABLE_LEGACY_SHADERCONTEXT
  66. Context* onMakeContext(const ContextRec& rec, SkArenaAlloc* alloc) const override {
  67. return nullptr;
  68. }
  69. #endif
  70. bool onAppendStages(const SkStageRec& rec) const override {
  71. rec.fPipeline->append(SkRasterPipeline::seed_shader);
  72. rec.fPipeline->append(SkRasterPipeline::matrix_4x3, &fM43);
  73. return true;
  74. }
  75. private:
  76. // For serialization. This will never be called.
  77. Factory getFactory() const override { return nullptr; }
  78. const char* getTypeName() const override { return nullptr; }
  79. Matrix43 fM43;
  80. const bool fIsOpaque;
  81. typedef SkShaderBase INHERITED;
  82. };
  83. static bool SK_WARN_UNUSED_RESULT
  84. update_tricolor_matrix(const SkMatrix& ctmInv, const SkPoint pts[], const SkPMColor4f colors[],
  85. int index0, int index1, int index2, Matrix43* result) {
  86. SkMatrix m, im;
  87. m.reset();
  88. m.set(0, pts[index1].fX - pts[index0].fX);
  89. m.set(1, pts[index2].fX - pts[index0].fX);
  90. m.set(2, pts[index0].fX);
  91. m.set(3, pts[index1].fY - pts[index0].fY);
  92. m.set(4, pts[index2].fY - pts[index0].fY);
  93. m.set(5, pts[index0].fY);
  94. if (!m.invert(&im)) {
  95. return false;
  96. }
  97. SkMatrix dstToUnit;
  98. dstToUnit.setConcat(im, ctmInv);
  99. Sk4f c0 = Sk4f::Load(colors[index0].vec()),
  100. c1 = Sk4f::Load(colors[index1].vec()),
  101. c2 = Sk4f::Load(colors[index2].vec());
  102. Matrix43 colorm;
  103. (c1 - c0).store(&colorm.fMat[0]);
  104. (c2 - c0).store(&colorm.fMat[4]);
  105. c0.store(&colorm.fMat[8]);
  106. result->setConcat(colorm, dstToUnit);
  107. return true;
  108. }
  109. // Convert the SkColors into float colors. The conversion depends on some conditions:
  110. // - If the pixmap has a dst colorspace, we have to be "color-correct".
  111. // Do we map into dst-colorspace before or after we interpolate?
  112. // - We have to decide when to apply per-color alpha (before or after we interpolate)
  113. //
  114. // For now, we will take a simple approach, but recognize this is just a start:
  115. // - convert colors into dst colorspace before interpolation (matches gradients)
  116. // - apply per-color alpha before interpolation (matches old version of vertices)
  117. //
  118. static SkPMColor4f* convert_colors(const SkColor src[], int count, SkColorSpace* deviceCS,
  119. SkArenaAlloc* alloc) {
  120. SkPMColor4f* dst = alloc->makeArray<SkPMColor4f>(count);
  121. SkImageInfo srcInfo = SkImageInfo::Make(count, 1, kBGRA_8888_SkColorType,
  122. kUnpremul_SkAlphaType, SkColorSpace::MakeSRGB());
  123. SkImageInfo dstInfo = SkImageInfo::Make(count, 1, kRGBA_F32_SkColorType,
  124. kPremul_SkAlphaType, sk_ref_sp(deviceCS));
  125. SkConvertPixels(dstInfo, dst, 0, srcInfo, src, 0);
  126. return dst;
  127. }
  128. static bool compute_is_opaque(const SkColor colors[], int count) {
  129. uint32_t c = ~0;
  130. for (int i = 0; i < count; ++i) {
  131. c &= colors[i];
  132. }
  133. return SkColorGetA(c) == 0xFF;
  134. }
  135. void SkDraw::drawVertices(SkVertices::VertexMode vmode, int vertexCount,
  136. const SkPoint vertices[], const SkPoint textures[],
  137. const SkColor colors[], const SkVertices::BoneIndices boneIndices[],
  138. const SkVertices::BoneWeights boneWeights[], SkBlendMode bmode,
  139. const uint16_t indices[], int indexCount,
  140. const SkPaint& paint, const SkVertices::Bone bones[],
  141. int boneCount) const {
  142. SkASSERT(0 == vertexCount || vertices);
  143. // abort early if there is nothing to draw
  144. if (vertexCount < 3 || (indices && indexCount < 3) || fRC->isEmpty()) {
  145. return;
  146. }
  147. SkMatrix ctmInv;
  148. if (!fMatrix->invert(&ctmInv)) {
  149. return;
  150. }
  151. // make textures and shader mutually consistent
  152. SkShader* shader = paint.getShader();
  153. if (!(shader && textures)) {
  154. shader = nullptr;
  155. textures = nullptr;
  156. }
  157. // We can simplify things for certain blendmodes. This is for speed, and SkComposeShader
  158. // itself insists we don't pass kSrc or kDst to it.
  159. //
  160. if (colors && textures) {
  161. switch (bmode) {
  162. case SkBlendMode::kSrc:
  163. colors = nullptr;
  164. break;
  165. case SkBlendMode::kDst:
  166. textures = nullptr;
  167. break;
  168. default: break;
  169. }
  170. }
  171. // we don't use the shader if there are no textures
  172. if (!textures) {
  173. shader = nullptr;
  174. }
  175. constexpr size_t kDefVertexCount = 16;
  176. constexpr size_t kOuterSize = sizeof(SkTriColorShader) +
  177. sizeof(SkShader_Blend) +
  178. (2 * sizeof(SkPoint) + sizeof(SkColor4f)) * kDefVertexCount;
  179. SkSTArenaAlloc<kOuterSize> outerAlloc;
  180. // deform vertices using the skeleton if it is passed in
  181. if (bones && boneCount) {
  182. // allocate space for the deformed vertices
  183. SkPoint* deformed = outerAlloc.makeArray<SkPoint>(vertexCount);
  184. // deform the vertices
  185. if (boneIndices && boneWeights) {
  186. for (int i = 0; i < vertexCount; i ++) {
  187. const SkVertices::BoneIndices& indices = boneIndices[i];
  188. const SkVertices::BoneWeights& weights = boneWeights[i];
  189. // apply the world transform
  190. SkPoint worldPoint = bones[0].mapPoint(vertices[i]);
  191. // apply bone deformations
  192. deformed[i] = SkPoint::Make(0.0f, 0.0f);
  193. for (uint32_t j = 0; j < 4; j ++) {
  194. // get the attachment data
  195. uint32_t index = indices[j];
  196. float weight = weights[j];
  197. // skip the bone if there is no weight
  198. if (weight == 0.0f) {
  199. continue;
  200. }
  201. SkASSERT(index != 0);
  202. // deformed += M * v * w
  203. deformed[i] += bones[index].mapPoint(worldPoint) * weight;
  204. }
  205. }
  206. } else {
  207. // no bones, so only apply world transform
  208. SkMatrix worldTransform = SkMatrix::I();
  209. worldTransform.setAffine(bones[0].values);
  210. worldTransform.mapPoints(deformed, vertices, vertexCount);
  211. }
  212. // change the vertices to point to deformed
  213. vertices = deformed;
  214. }
  215. SkPoint* devVerts = outerAlloc.makeArray<SkPoint>(vertexCount);
  216. fMatrix->mapPoints(devVerts, vertices, vertexCount);
  217. {
  218. SkRect bounds;
  219. // this also sets bounds to empty if we see a non-finite value
  220. bounds.set(devVerts, vertexCount);
  221. if (bounds.isEmpty()) {
  222. return;
  223. }
  224. }
  225. VertState state(vertexCount, indices, indexCount);
  226. VertState::Proc vertProc = state.chooseProc(vmode);
  227. if (colors || textures) {
  228. SkPMColor4f* dstColors = nullptr;
  229. Matrix43* matrix43 = nullptr;
  230. if (colors) {
  231. dstColors = convert_colors(colors, vertexCount, fDst.colorSpace(), &outerAlloc);
  232. SkTriColorShader* triShader = outerAlloc.make<SkTriColorShader>(
  233. compute_is_opaque(colors,
  234. vertexCount));
  235. matrix43 = triShader->getMatrix43();
  236. if (shader) {
  237. shader = outerAlloc.make<SkShader_Blend>(bmode,
  238. sk_ref_sp(triShader), sk_ref_sp(shader),
  239. nullptr);
  240. } else {
  241. shader = triShader;
  242. }
  243. }
  244. SkPaint p(paint);
  245. p.setShader(sk_ref_sp(shader));
  246. if (!textures) { // only tricolor shader
  247. SkASSERT(matrix43);
  248. auto blitter = SkCreateRasterPipelineBlitter(fDst, p, *fMatrix, &outerAlloc);
  249. while (vertProc(&state)) {
  250. if (!update_tricolor_matrix(ctmInv, vertices, dstColors,
  251. state.f0, state.f1, state.f2,
  252. matrix43)) {
  253. continue;
  254. }
  255. SkPoint tmp[] = {
  256. devVerts[state.f0], devVerts[state.f1], devVerts[state.f2]
  257. };
  258. SkScan::FillTriangle(tmp, *fRC, blitter);
  259. }
  260. } else {
  261. while (vertProc(&state)) {
  262. SkSTArenaAlloc<2048> innerAlloc;
  263. const SkMatrix* ctm = fMatrix;
  264. SkMatrix tmpCtm;
  265. if (textures) {
  266. SkMatrix localM;
  267. if (!texture_to_matrix(state, vertices, textures, &localM)) {
  268. continue;
  269. }
  270. tmpCtm = SkMatrix::Concat(*fMatrix, localM);
  271. ctm = &tmpCtm;
  272. }
  273. if (matrix43 && !update_tricolor_matrix(ctmInv, vertices, dstColors,
  274. state.f0, state.f1, state.f2,
  275. matrix43)) {
  276. continue;
  277. }
  278. SkPoint tmp[] = {
  279. devVerts[state.f0], devVerts[state.f1], devVerts[state.f2]
  280. };
  281. auto blitter = SkCreateRasterPipelineBlitter(fDst, p, *ctm, &innerAlloc);
  282. SkScan::FillTriangle(tmp, *fRC, blitter);
  283. }
  284. }
  285. } else {
  286. // no colors[] and no texture, stroke hairlines with paint's color.
  287. SkPaint p;
  288. p.setStyle(SkPaint::kStroke_Style);
  289. SkAutoBlitterChoose blitter(*this, nullptr, p);
  290. // Abort early if we failed to create a shader context.
  291. if (blitter->isNullBlitter()) {
  292. return;
  293. }
  294. SkScan::HairRCProc hairProc = ChooseHairProc(paint.isAntiAlias());
  295. const SkRasterClip& clip = *fRC;
  296. while (vertProc(&state)) {
  297. SkPoint array[] = {
  298. devVerts[state.f0], devVerts[state.f1], devVerts[state.f2], devVerts[state.f0]
  299. };
  300. hairProc(array, 4, clip, blitter.get());
  301. }
  302. }
  303. }