VerticesTest.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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/SkCanvas.h"
  8. #include "include/core/SkSurface.h"
  9. #include "include/core/SkVertices.h"
  10. #include "tests/Test.h"
  11. #include "tools/ToolUtils.h"
  12. static bool equal(const SkVertices* v0, const SkVertices* v1) {
  13. if (v0->mode() != v1->mode()) {
  14. return false;
  15. }
  16. if (v0->vertexCount() != v1->vertexCount()) {
  17. return false;
  18. }
  19. if (v0->indexCount() != v1->indexCount()) {
  20. return false;
  21. }
  22. if (!!v0->texCoords() != !!v1->texCoords()) {
  23. return false;
  24. }
  25. if (!!v0->colors() != !!v1->colors()) {
  26. return false;
  27. }
  28. for (int i = 0; i < v0->vertexCount(); ++i) {
  29. if (v0->positions()[i] != v1->positions()[i]) {
  30. return false;
  31. }
  32. if (v0->texCoords()) {
  33. if (v0->texCoords()[i] != v1->texCoords()[i]) {
  34. return false;
  35. }
  36. }
  37. if (v0->colors()) {
  38. if (v0->colors()[i] != v1->colors()[i]) {
  39. return false;
  40. }
  41. }
  42. }
  43. for (int i = 0; i < v0->indexCount(); ++i) {
  44. if (v0->indices()[i] != v1->indices()[i]) {
  45. return false;
  46. }
  47. }
  48. return true;
  49. }
  50. DEF_TEST(Vertices, reporter) {
  51. int vCount = 5;
  52. int iCount = 9; // odd value exercises padding logic in encode()
  53. const uint32_t texFlags[] = { 0, SkVertices::kHasTexCoords_BuilderFlag };
  54. const uint32_t colFlags[] = { 0, SkVertices::kHasColors_BuilderFlag };
  55. for (auto texF : texFlags) {
  56. for (auto colF : colFlags) {
  57. uint32_t flags = texF | colF;
  58. SkVertices::Builder builder(SkVertices::kTriangles_VertexMode, vCount, iCount, flags);
  59. for (int i = 0; i < vCount; ++i) {
  60. float x = (float)i;
  61. builder.positions()[i].set(x, 1);
  62. if (builder.texCoords()) {
  63. builder.texCoords()[i].set(x, 2);
  64. }
  65. if (builder.colors()) {
  66. builder.colors()[i] = SkColorSetARGB(0xFF, i, 0x80, 0);
  67. }
  68. }
  69. for (int i = 0; i < builder.indexCount(); ++i) {
  70. builder.indices()[i] = i % vCount;
  71. }
  72. sk_sp<SkVertices> v0 = builder.detach();
  73. sk_sp<SkData> data = v0->encode();
  74. sk_sp<SkVertices> v1 = SkVertices::Decode(data->data(), data->size());
  75. REPORTER_ASSERT(reporter, v0->uniqueID() != 0);
  76. REPORTER_ASSERT(reporter, v1->uniqueID() != 0);
  77. REPORTER_ASSERT(reporter, v0->uniqueID() != v1->uniqueID());
  78. REPORTER_ASSERT(reporter, equal(v0.get(), v1.get()));
  79. }
  80. }
  81. {
  82. // This has the maximum number of vertices to be rewritten as indexed triangles without
  83. // overflowing a 16bit index.
  84. SkVertices::Builder builder(SkVertices::kTriangleFan_VertexMode, UINT16_MAX + 1, 0,
  85. SkVertices::kHasColors_BuilderFlag);
  86. REPORTER_ASSERT(reporter, builder.isValid());
  87. }
  88. {
  89. // This has too many to be rewritten.
  90. SkVertices::Builder builder(SkVertices::kTriangleFan_VertexMode, UINT16_MAX + 2, 0,
  91. SkVertices::kHasColors_BuilderFlag);
  92. REPORTER_ASSERT(reporter, !builder.isValid());
  93. }
  94. {
  95. // Only two vertices - can't be rewritten.
  96. SkVertices::Builder builder(SkVertices::kTriangleFan_VertexMode, 2, 0,
  97. SkVertices::kHasColors_BuilderFlag);
  98. REPORTER_ASSERT(reporter, !builder.isValid());
  99. }
  100. {
  101. // Minimum number of indices to be rewritten.
  102. SkVertices::Builder builder(SkVertices::kTriangleFan_VertexMode, 10, 3,
  103. SkVertices::kHasColors_BuilderFlag);
  104. REPORTER_ASSERT(reporter, builder.isValid());
  105. }
  106. {
  107. // Too few indices to be rewritten.
  108. SkVertices::Builder builder(SkVertices::kTriangleFan_VertexMode, 10, 2,
  109. SkVertices::kHasColors_BuilderFlag);
  110. REPORTER_ASSERT(reporter, !builder.isValid());
  111. }
  112. }
  113. static void fill_triangle(SkCanvas* canvas, const SkPoint pts[], SkColor c) {
  114. SkColor colors[] = { c, c, c };
  115. auto verts = SkVertices::MakeCopy(SkVertices::kTriangles_VertexMode, 3, pts, nullptr, colors);
  116. canvas->drawVertices(verts, SkBlendMode::kSrc, SkPaint());
  117. }
  118. DEF_TEST(Vertices_clipping, reporter) {
  119. // A very large triangle has to be geometrically clipped (since its "fast" clipping is
  120. // normally done in after building SkFixed coordinates). Check that we handle this.
  121. // (and don't assert).
  122. auto surf = SkSurface::MakeRasterN32Premul(3, 3);
  123. SkPoint pts[] = { { -10, 1 }, { -10, 2 }, { 1e9f, 1.5f } };
  124. fill_triangle(surf->getCanvas(), pts, SK_ColorBLACK);
  125. ToolUtils::PixelIter iter(surf.get());
  126. SkIPoint loc;
  127. while (void* addr = iter.next(&loc)) {
  128. SkPMColor c = *(SkPMColor*)addr;
  129. if (loc.fY == 1) {
  130. REPORTER_ASSERT(reporter, c == 0xFF000000);
  131. } else {
  132. REPORTER_ASSERT(reporter, c == 0);
  133. }
  134. }
  135. }