2017-03-14 18:10:54 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2017 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkCanvas.h"
|
|
|
|
#include "include/core/SkSurface.h"
|
|
|
|
#include "include/core/SkVertices.h"
|
2020-06-01 20:33:28 +00:00
|
|
|
#include "src/core/SkAutoMalloc.h"
|
|
|
|
#include "src/core/SkReadBuffer.h"
|
2020-03-13 00:33:21 +00:00
|
|
|
#include "src/core/SkVerticesPriv.h"
|
2020-06-01 20:33:28 +00:00
|
|
|
#include "src/core/SkWriteBuffer.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "tests/Test.h"
|
|
|
|
#include "tools/ToolUtils.h"
|
2017-03-14 18:10:54 +00:00
|
|
|
|
2020-03-13 00:33:21 +00:00
|
|
|
static bool equal(const SkVertices* vert0, const SkVertices* vert1) {
|
2020-03-31 14:38:31 +00:00
|
|
|
SkVerticesPriv v0(vert0->priv()), v1(vert1->priv());
|
2020-03-13 00:33:21 +00:00
|
|
|
|
2020-03-31 14:38:31 +00:00
|
|
|
if (v0.mode() != v1.mode()) {
|
2017-03-14 18:10:54 +00:00
|
|
|
return false;
|
|
|
|
}
|
2020-03-31 14:38:31 +00:00
|
|
|
if (v0.vertexCount() != v1.vertexCount()) {
|
2017-03-14 18:10:54 +00:00
|
|
|
return false;
|
|
|
|
}
|
2020-03-31 14:38:31 +00:00
|
|
|
if (v0.indexCount() != v1.indexCount()) {
|
2017-03-14 18:10:54 +00:00
|
|
|
return false;
|
|
|
|
}
|
2020-03-30 13:57:53 +00:00
|
|
|
if (v0.attributeCount() != v1.attributeCount()) {
|
2020-03-05 18:01:00 +00:00
|
|
|
return false;
|
|
|
|
}
|
2020-03-30 13:57:53 +00:00
|
|
|
for (int i = 0; i < v0.attributeCount(); ++i) {
|
|
|
|
if (v0.attributes()[i] != v1.attributes()[i]) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2017-03-14 18:10:54 +00:00
|
|
|
|
2020-03-30 13:57:53 +00:00
|
|
|
if (!!v0.customData() != !!v1.customData()) {
|
2020-03-05 18:01:00 +00:00
|
|
|
return false;
|
|
|
|
}
|
2020-03-31 14:38:31 +00:00
|
|
|
if (!!v0.texCoords() != !!v1.texCoords()) {
|
2017-03-14 18:10:54 +00:00
|
|
|
return false;
|
|
|
|
}
|
2020-03-31 14:38:31 +00:00
|
|
|
if (!!v0.colors() != !!v1.colors()) {
|
2017-03-14 18:10:54 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-03-31 14:38:31 +00:00
|
|
|
for (int i = 0; i < v0.vertexCount(); ++i) {
|
|
|
|
if (v0.positions()[i] != v1.positions()[i]) {
|
2017-03-14 18:10:54 +00:00
|
|
|
return false;
|
|
|
|
}
|
2020-03-31 14:38:31 +00:00
|
|
|
if (v0.texCoords()) {
|
|
|
|
if (v0.texCoords()[i] != v1.texCoords()[i]) {
|
2017-03-14 18:10:54 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2020-03-31 14:38:31 +00:00
|
|
|
if (v0.colors()) {
|
|
|
|
if (v0.colors()[i] != v1.colors()[i]) {
|
2017-03-14 18:10:54 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-03-30 13:57:53 +00:00
|
|
|
size_t totalCustomDataSize = v0.vertexCount() * v0.customDataSize();
|
|
|
|
if (totalCustomDataSize) {
|
|
|
|
if (memcmp(v0.customData(), v1.customData(), totalCustomDataSize) != 0) {
|
2020-03-05 18:01:00 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2020-03-31 14:38:31 +00:00
|
|
|
for (int i = 0; i < v0.indexCount(); ++i) {
|
|
|
|
if (v0.indices()[i] != v1.indices()[i]) {
|
2017-03-14 18:10:54 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-03-05 18:01:00 +00:00
|
|
|
static void self_test(sk_sp<SkVertices> v0, skiatest::Reporter* reporter) {
|
2020-06-01 20:33:28 +00:00
|
|
|
SkBinaryWriteBuffer writer;
|
|
|
|
v0->priv().encode(writer);
|
2020-03-05 18:01:00 +00:00
|
|
|
|
2020-06-01 20:33:28 +00:00
|
|
|
SkAutoMalloc buf(writer.bytesWritten());
|
|
|
|
writer.writeToMemory(buf.get());
|
|
|
|
SkReadBuffer reader(buf.get(), writer.bytesWritten());
|
|
|
|
|
|
|
|
sk_sp<SkVertices> v1 = SkVerticesPriv::Decode(reader);
|
|
|
|
|
|
|
|
REPORTER_ASSERT(reporter, v1 != nullptr);
|
2020-03-05 18:01:00 +00:00
|
|
|
REPORTER_ASSERT(reporter, v0->uniqueID() != 0);
|
|
|
|
REPORTER_ASSERT(reporter, v1->uniqueID() != 0);
|
|
|
|
REPORTER_ASSERT(reporter, v0->uniqueID() != v1->uniqueID());
|
|
|
|
REPORTER_ASSERT(reporter, equal(v0.get(), v1.get()));
|
|
|
|
}
|
|
|
|
|
2017-03-14 18:10:54 +00:00
|
|
|
DEF_TEST(Vertices, reporter) {
|
2017-04-05 21:05:16 +00:00
|
|
|
int vCount = 5;
|
|
|
|
int iCount = 9; // odd value exercises padding logic in encode()
|
2017-03-14 18:10:54 +00:00
|
|
|
|
2020-03-05 18:01:00 +00:00
|
|
|
// color-tex tests
|
2017-03-16 18:38:48 +00:00
|
|
|
const uint32_t texFlags[] = { 0, SkVertices::kHasTexCoords_BuilderFlag };
|
|
|
|
const uint32_t colFlags[] = { 0, SkVertices::kHasColors_BuilderFlag };
|
2017-03-14 18:10:54 +00:00
|
|
|
for (auto texF : texFlags) {
|
|
|
|
for (auto colF : colFlags) {
|
|
|
|
uint32_t flags = texF | colF;
|
|
|
|
|
2017-04-03 15:11:09 +00:00
|
|
|
SkVertices::Builder builder(SkVertices::kTriangles_VertexMode, vCount, iCount, flags);
|
2017-03-14 18:10:54 +00:00
|
|
|
|
|
|
|
for (int i = 0; i < vCount; ++i) {
|
|
|
|
float x = (float)i;
|
|
|
|
builder.positions()[i].set(x, 1);
|
|
|
|
if (builder.texCoords()) {
|
|
|
|
builder.texCoords()[i].set(x, 2);
|
|
|
|
}
|
|
|
|
if (builder.colors()) {
|
|
|
|
builder.colors()[i] = SkColorSetARGB(0xFF, i, 0x80, 0);
|
|
|
|
}
|
|
|
|
}
|
2020-04-01 19:48:53 +00:00
|
|
|
for (int i = 0; i < iCount; ++i) {
|
2017-03-14 18:10:54 +00:00
|
|
|
builder.indices()[i] = i % vCount;
|
|
|
|
}
|
2020-03-05 18:01:00 +00:00
|
|
|
self_test(builder.detach(), reporter);
|
|
|
|
}
|
|
|
|
}
|
2020-03-30 13:57:53 +00:00
|
|
|
|
|
|
|
// custom data tests
|
|
|
|
using AttrType = SkVertices::Attribute::Type;
|
|
|
|
struct {
|
|
|
|
int count;
|
|
|
|
size_t expected_size;
|
|
|
|
SkVertices::Attribute attrs[4];
|
|
|
|
} attrTests[] = {
|
|
|
|
{ 1, 4, { AttrType::kFloat } },
|
|
|
|
{ 1, 8, { AttrType::kFloat2 } },
|
|
|
|
{ 1, 12, { AttrType::kFloat3 } },
|
|
|
|
{ 1, 16, { AttrType::kFloat4 } },
|
|
|
|
{ 1, 4, { AttrType::kByte4_unorm } },
|
|
|
|
{ 4, 16, { AttrType::kFloat, AttrType::kFloat, AttrType::kFloat, AttrType::kFloat } },
|
|
|
|
{ 2, 12, { AttrType::kFloat2, AttrType::kByte4_unorm } },
|
|
|
|
{ 2, 12, { AttrType::kByte4_unorm, AttrType::kFloat2 } },
|
|
|
|
};
|
|
|
|
|
|
|
|
for (const auto& test : attrTests) {
|
2020-03-05 18:01:00 +00:00
|
|
|
SkVertices::Builder builder(SkVertices::kTriangles_VertexMode, vCount, iCount,
|
2020-03-30 13:57:53 +00:00
|
|
|
test.attrs, test.count);
|
2017-03-14 18:10:54 +00:00
|
|
|
|
2020-03-30 13:57:53 +00:00
|
|
|
float* customData = (float*)builder.customData();
|
|
|
|
int customDataCount = test.expected_size / sizeof(float);
|
2020-04-01 19:48:53 +00:00
|
|
|
for (int i = 0; i < vCount; ++i) {
|
2020-03-05 18:01:00 +00:00
|
|
|
builder.positions()[i].set((float)i, 1);
|
2020-03-30 13:57:53 +00:00
|
|
|
for (int j = 0; j < customDataCount; ++j) {
|
|
|
|
customData[i * customDataCount + j] = (float)j;
|
2020-03-05 18:01:00 +00:00
|
|
|
}
|
|
|
|
}
|
2020-04-01 19:48:53 +00:00
|
|
|
for (int i = 0; i < iCount; ++i) {
|
2020-03-05 18:01:00 +00:00
|
|
|
builder.indices()[i] = i % vCount;
|
2017-03-14 18:10:54 +00:00
|
|
|
}
|
2020-03-05 18:01:00 +00:00
|
|
|
self_test(builder.detach(), reporter);
|
2017-03-14 18:10:54 +00:00
|
|
|
}
|
2020-03-30 13:57:53 +00:00
|
|
|
|
2018-04-28 20:13:08 +00:00
|
|
|
{
|
|
|
|
// This has the maximum number of vertices to be rewritten as indexed triangles without
|
|
|
|
// overflowing a 16bit index.
|
Reland "Remove SK_MaxSizeT, SK_M{in|ax}U{16|32}, #defines."
This reverts commit ab17347df32807cabd9f2a518d22c3bd420e482f.
Reason for revert: Chromium should now be ok.
Original change's description:
> Revert "Remove SK_MaxSizeT, SK_M{in|ax}U{16|32}, #defines."
>
> This reverts commit e1bc7de7c07686b28b00b850e44e0722189f3592.
>
> Reason for revert: chrome used it
>
> Original change's description:
> > Remove SK_MaxSizeT, SK_M{in|ax}U{16|32}, #defines.
> >
> > sed 's/SK_MaxSizeT/SIZE_MAX/g'
> > sed 's/SK_MaxU32/UINT32_MAX/g'
> > sed 's/SK_MaxU16/UINT16_MAX/g'
> >
> > SK_MinU32 and SK_MinU16 were unused
> >
> > Change-Id: I6b6c824df47b05bde7e73b13a58e851a5f63fe0e
> > Reviewed-on: https://skia-review.googlesource.com/134607
> > Commit-Queue: Hal Canary <halcanary@google.com>
> > Reviewed-by: Ben Wagner <bungeman@google.com>
>
> TBR=halcanary@google.com,bungeman@google.com,reed@google.com
>
> Change-Id: I1e2c440dcf9f59bf87c1fea113248cd5136f7519
> No-Presubmit: true
> No-Tree-Checks: true
> No-Try: true
> Reviewed-on: https://skia-review.googlesource.com/134921
> Reviewed-by: Hal Canary <halcanary@google.com>
> Commit-Queue: Hal Canary <halcanary@google.com>
CQ_INCLUDE_TRYBOTS=luci.chromium.try:linux-ozone-rel
TBR=halcanary@google.com,bungeman@google.com,reed@google.com
Change-Id: I7709f9715bea0463b85b5b0a89712ac1020fcddb
Reviewed-on: https://skia-review.googlesource.com/135180
Commit-Queue: Ben Wagner <bungeman@google.com>
Reviewed-by: Ben Wagner <bungeman@google.com>
2018-06-15 15:37:57 +00:00
|
|
|
SkVertices::Builder builder(SkVertices::kTriangleFan_VertexMode, UINT16_MAX + 1, 0,
|
2018-04-28 20:13:08 +00:00
|
|
|
SkVertices::kHasColors_BuilderFlag);
|
|
|
|
REPORTER_ASSERT(reporter, builder.isValid());
|
|
|
|
}
|
|
|
|
{
|
|
|
|
// This has too many to be rewritten.
|
Reland "Remove SK_MaxSizeT, SK_M{in|ax}U{16|32}, #defines."
This reverts commit ab17347df32807cabd9f2a518d22c3bd420e482f.
Reason for revert: Chromium should now be ok.
Original change's description:
> Revert "Remove SK_MaxSizeT, SK_M{in|ax}U{16|32}, #defines."
>
> This reverts commit e1bc7de7c07686b28b00b850e44e0722189f3592.
>
> Reason for revert: chrome used it
>
> Original change's description:
> > Remove SK_MaxSizeT, SK_M{in|ax}U{16|32}, #defines.
> >
> > sed 's/SK_MaxSizeT/SIZE_MAX/g'
> > sed 's/SK_MaxU32/UINT32_MAX/g'
> > sed 's/SK_MaxU16/UINT16_MAX/g'
> >
> > SK_MinU32 and SK_MinU16 were unused
> >
> > Change-Id: I6b6c824df47b05bde7e73b13a58e851a5f63fe0e
> > Reviewed-on: https://skia-review.googlesource.com/134607
> > Commit-Queue: Hal Canary <halcanary@google.com>
> > Reviewed-by: Ben Wagner <bungeman@google.com>
>
> TBR=halcanary@google.com,bungeman@google.com,reed@google.com
>
> Change-Id: I1e2c440dcf9f59bf87c1fea113248cd5136f7519
> No-Presubmit: true
> No-Tree-Checks: true
> No-Try: true
> Reviewed-on: https://skia-review.googlesource.com/134921
> Reviewed-by: Hal Canary <halcanary@google.com>
> Commit-Queue: Hal Canary <halcanary@google.com>
CQ_INCLUDE_TRYBOTS=luci.chromium.try:linux-ozone-rel
TBR=halcanary@google.com,bungeman@google.com,reed@google.com
Change-Id: I7709f9715bea0463b85b5b0a89712ac1020fcddb
Reviewed-on: https://skia-review.googlesource.com/135180
Commit-Queue: Ben Wagner <bungeman@google.com>
Reviewed-by: Ben Wagner <bungeman@google.com>
2018-06-15 15:37:57 +00:00
|
|
|
SkVertices::Builder builder(SkVertices::kTriangleFan_VertexMode, UINT16_MAX + 2, 0,
|
2018-04-28 20:13:08 +00:00
|
|
|
SkVertices::kHasColors_BuilderFlag);
|
|
|
|
REPORTER_ASSERT(reporter, !builder.isValid());
|
|
|
|
}
|
|
|
|
{
|
|
|
|
// Only two vertices - can't be rewritten.
|
|
|
|
SkVertices::Builder builder(SkVertices::kTriangleFan_VertexMode, 2, 0,
|
|
|
|
SkVertices::kHasColors_BuilderFlag);
|
|
|
|
REPORTER_ASSERT(reporter, !builder.isValid());
|
|
|
|
}
|
|
|
|
{
|
|
|
|
// Minimum number of indices to be rewritten.
|
|
|
|
SkVertices::Builder builder(SkVertices::kTriangleFan_VertexMode, 10, 3,
|
|
|
|
SkVertices::kHasColors_BuilderFlag);
|
|
|
|
REPORTER_ASSERT(reporter, builder.isValid());
|
|
|
|
}
|
|
|
|
{
|
|
|
|
// Too few indices to be rewritten.
|
|
|
|
SkVertices::Builder builder(SkVertices::kTriangleFan_VertexMode, 10, 2,
|
|
|
|
SkVertices::kHasColors_BuilderFlag);
|
|
|
|
REPORTER_ASSERT(reporter, !builder.isValid());
|
2020-03-05 18:01:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// validity tests for per-vertex-data
|
|
|
|
|
2020-03-30 13:57:53 +00:00
|
|
|
// Check that invalid counts fail to initialize the builder
|
|
|
|
for (int attrCount : {-1, 0, SkVertices::kMaxCustomAttributes + 1}) {
|
|
|
|
SkVertices::Attribute attrs[] = { AttrType::kFloat };
|
|
|
|
SkVertices::Builder builder(SkVertices::kTriangleFan_VertexMode, 10, 0, attrs, attrCount);
|
|
|
|
REPORTER_ASSERT(reporter, !builder.isValid());
|
|
|
|
}
|
|
|
|
{ // nullptr is definitely bad
|
|
|
|
SkVertices::Builder builder(SkVertices::kTriangleFan_VertexMode, 10, 0, nullptr, 4);
|
2020-03-05 18:01:00 +00:00
|
|
|
REPORTER_ASSERT(reporter, !builder.isValid());
|
|
|
|
}
|
2020-03-30 13:57:53 +00:00
|
|
|
{ // "normal" number of per-vertex-data (all floats)
|
|
|
|
SkVertices::Attribute attrs[] = {AttrType::kFloat2, AttrType::kFloat2};
|
|
|
|
SkVertices::Builder builder(SkVertices::kTriangleFan_VertexMode, 10, 0, attrs, 2);
|
2020-03-05 18:01:00 +00:00
|
|
|
REPORTER_ASSERT(reporter, builder.isValid());
|
2020-03-30 13:57:53 +00:00
|
|
|
REPORTER_ASSERT(reporter, builder.customData() != nullptr);
|
2020-03-05 18:01:00 +00:00
|
|
|
}
|
2020-03-30 13:57:53 +00:00
|
|
|
{ // "normal" number of per-vertex-data (with packed bytes)
|
|
|
|
SkVertices::Attribute attrs[] = {AttrType::kFloat2, AttrType::kByte4_unorm};
|
|
|
|
SkVertices::Builder builder(SkVertices::kTriangleFan_VertexMode, 10, 0, attrs, 2);
|
2020-03-05 18:01:00 +00:00
|
|
|
REPORTER_ASSERT(reporter, builder.isValid());
|
2020-03-30 13:57:53 +00:00
|
|
|
REPORTER_ASSERT(reporter, builder.customData() != nullptr);
|
2018-04-28 20:13:08 +00:00
|
|
|
}
|
2017-03-14 18:10:54 +00:00
|
|
|
}
|
2018-01-30 15:12:22 +00:00
|
|
|
|
|
|
|
static void fill_triangle(SkCanvas* canvas, const SkPoint pts[], SkColor c) {
|
|
|
|
SkColor colors[] = { c, c, c };
|
|
|
|
auto verts = SkVertices::MakeCopy(SkVertices::kTriangles_VertexMode, 3, pts, nullptr, colors);
|
|
|
|
canvas->drawVertices(verts, SkBlendMode::kSrc, SkPaint());
|
|
|
|
}
|
|
|
|
|
|
|
|
DEF_TEST(Vertices_clipping, reporter) {
|
|
|
|
// A very large triangle has to be geometrically clipped (since its "fast" clipping is
|
|
|
|
// normally done in after building SkFixed coordinates). Check that we handle this.
|
|
|
|
// (and don't assert).
|
|
|
|
auto surf = SkSurface::MakeRasterN32Premul(3, 3);
|
|
|
|
|
|
|
|
SkPoint pts[] = { { -10, 1 }, { -10, 2 }, { 1e9f, 1.5f } };
|
|
|
|
fill_triangle(surf->getCanvas(), pts, SK_ColorBLACK);
|
|
|
|
|
2019-03-20 16:12:10 +00:00
|
|
|
ToolUtils::PixelIter iter(surf.get());
|
2018-01-30 15:12:22 +00:00
|
|
|
SkIPoint loc;
|
|
|
|
while (void* addr = iter.next(&loc)) {
|
|
|
|
SkPMColor c = *(SkPMColor*)addr;
|
|
|
|
if (loc.fY == 1) {
|
|
|
|
REPORTER_ASSERT(reporter, c == 0xFF000000);
|
|
|
|
} else {
|
|
|
|
REPORTER_ASSERT(reporter, c == 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|