xref: /aosp_15_r20/external/skia/tests/graphite/DeviceTest.cpp (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
1 /*
2  * Copyright 2022 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 
8 #include "tests/Test.h"
9 
10 #include "include/core/SkBitmap.h"
11 #include "include/core/SkCanvas.h"
12 #include "include/core/SkPixmap.h"
13 #include "include/core/SkVertices.h"
14 #include "include/gpu/graphite/Context.h"
15 #include "include/gpu/graphite/Recorder.h"
16 #include "include/gpu/graphite/Surface.h"
17 
18 namespace skgpu::graphite {
19 
20 // Tests that vertex transparency will affect draw order.
DEF_GRAPHITE_TEST_FOR_ALL_CONTEXTS(DeviceTestVertexTransparency,reporter,context,CtsEnforcement::kApiLevel_V)21 DEF_GRAPHITE_TEST_FOR_ALL_CONTEXTS(DeviceTestVertexTransparency, reporter, context,
22                                    CtsEnforcement::kApiLevel_V) {
23     // Set up transparent vertices, in a 5px wide by 10px tall rectangle.
24     static constexpr int kVertexCount = 5;
25     SkPoint positions[kVertexCount];
26     positions[0].set(2.5, 5);
27     positions[1].set(0, 0);
28     positions[2].set(5, 0);
29     positions[3].set(5, 10);
30     positions[4].set(0, 10);
31 
32     static constexpr int kIndexCount = 6;
33     static constexpr uint16_t kIndices[kIndexCount] = {0, 1, 2, 3, 4, 1};
34 
35     SkColor colors[kVertexCount];
36     for (size_t i = 0; i < kVertexCount; ++i) {
37         colors[i] = 0x7F00FF00;
38     }
39 
40     auto v = SkVertices::MakeCopy(SkVertices::kTriangleFan_VertexMode,
41                                   kVertexCount,
42                                   positions,
43                                   nullptr,
44                                   colors,
45                                   kIndexCount,
46                                   kIndices);
47 
48     // Draw vertices at x = 0.
49     std::unique_ptr<Recorder> recorder = context->makeRecorder();
50     SkImageInfo ii = SkImageInfo::Make(SkISize::Make(10, 10),
51                                        SkColorType::kRGBA_8888_SkColorType,
52                                        SkAlphaType::kPremul_SkAlphaType);
53     sk_sp<SkSurface> surface = SkSurfaces::RenderTarget(recorder.get(), ii);
54     SkCanvas* canvas = surface->getCanvas();
55     canvas->drawVertices(v, SkBlendMode::kDst, SkPaint());
56 
57     // Draw a square that will overlap both vertex draws.
58     SkPaint redPaint;
59     redPaint.setColor(SK_ColorRED);
60     canvas->drawRect(SkRect::MakeXYWH(0, 0, 10, 10), redPaint);
61 
62     // Draw vertices at x = 5.
63     canvas->translate(5, 0);
64     canvas->drawVertices(v, SkBlendMode::kDst, SkPaint());
65 
66     // Read pixels.
67     SkBitmap bitmap;
68     SkPixmap pixmap;
69     bitmap.allocPixels(ii);
70     SkAssertResult(bitmap.peekPixels(&pixmap));
71     if (!surface->readPixels(pixmap, 0, 0)) {
72         ERRORF(reporter, "readPixels failed");
73         return;
74     }
75 
76     // Check that draws weren't reordered to put vertex draws together.
77     // The second vertex draw should have been 50% green on top of red.
78     SkColor color = pixmap.getColor(9, 5);
79     SkColor expected = 0xFF807F00;
80     REPORTER_ASSERT(
81             reporter, color == expected, "Wrong color, expected %08x, found %08x", expected, color);
82 }
83 
84 }  // namespace skgpu::graphite
85