1 /*
2 * Copyright 2021 Google LLC
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 "experimental/graphite/include/BackendTexture.h"
11 #include "experimental/graphite/include/Context.h"
12 #include "experimental/graphite/include/Recorder.h"
13 #include "experimental/graphite/include/SkStuff.h"
14 #include "experimental/graphite/src/Caps.h"
15 #include "experimental/graphite/src/ContextPriv.h"
16 #include "experimental/graphite/src/Gpu.h"
17 #include "experimental/graphite/src/ResourceTypes.h"
18
19 #include "include/core/SkSurface.h"
20
21 using namespace skgpu;
22
23 namespace {
24 const SkISize kSize = {16, 16};
25 }
26
DEF_GRAPHITE_TEST_FOR_CONTEXTS(BackendTextureTest,reporter,context)27 DEF_GRAPHITE_TEST_FOR_CONTEXTS(BackendTextureTest, reporter, context) {
28 auto caps = context->priv().gpu()->caps();
29
30 TextureInfo info = caps->getDefaultSampledTextureInfo(kRGBA_8888_SkColorType,
31 /*levelCount=*/1,
32 Protected::kNo,
33 Renderable::kNo);
34 REPORTER_ASSERT(reporter, info.isValid());
35
36 auto texture1 = context->createBackendTexture(kSize, info);
37 REPORTER_ASSERT(reporter, texture1.isValid());
38
39 // We make a copy to do the remaining tests so we still have texture1 to safely delete the
40 // backend object.
41 auto texture1Copy = texture1;
42 REPORTER_ASSERT(reporter, texture1Copy.isValid());
43 REPORTER_ASSERT(reporter, texture1 == texture1Copy);
44
45 auto texture2 = context->createBackendTexture(kSize, info);
46 REPORTER_ASSERT(reporter, texture2.isValid());
47
48 REPORTER_ASSERT(reporter, texture1Copy != texture2);
49
50 // Test state after assignment
51 texture1Copy = texture2;
52 REPORTER_ASSERT(reporter, texture1Copy.isValid());
53 REPORTER_ASSERT(reporter, texture1Copy == texture2);
54
55 BackendTexture invalidTexture;
56 REPORTER_ASSERT(reporter, !invalidTexture.isValid());
57
58 texture1Copy = invalidTexture;
59 REPORTER_ASSERT(reporter, !texture1Copy.isValid());
60
61 texture1Copy = texture1;
62 REPORTER_ASSERT(reporter, texture1Copy.isValid());
63 REPORTER_ASSERT(reporter, texture1 == texture1Copy);
64
65 context->deleteBackendTexture(texture1);
66 context->deleteBackendTexture(texture2);
67 }
68
69 // Tests the wrapping of a BackendTexture in an SkSurface
DEF_GRAPHITE_TEST_FOR_CONTEXTS(SurfaceBackendTextureTest,reporter,context)70 DEF_GRAPHITE_TEST_FOR_CONTEXTS(SurfaceBackendTextureTest, reporter, context) {
71 // TODO: Right now this just tests very basic combinations of surfaces. This should be expanded
72 // to conver a much broader set of things once we add more support in Graphite for different
73 // formats, color types, etc.
74
75 auto caps = context->priv().gpu()->caps();
76 std::unique_ptr<Recorder> recorder = context->makeRecorder();
77
78 TextureInfo info = caps->getDefaultSampledTextureInfo(kRGBA_8888_SkColorType,
79 /*levelCount=*/1,
80 Protected::kNo,
81 Renderable::kYes);
82
83 auto texture = context->createBackendTexture(kSize, info);
84 REPORTER_ASSERT(reporter, texture.isValid());
85
86 sk_sp<SkSurface> surface = MakeGraphiteFromBackendTexture(recorder.get(),
87 texture,
88 kRGBA_8888_SkColorType,
89 /*colorSpace=*/nullptr,
90 /*props=*/nullptr);
91 REPORTER_ASSERT(reporter, surface);
92
93 surface.reset();
94
95 // We should fail when trying to wrap the same texture in a surface with a non compatible
96 // color type.
97 surface = MakeGraphiteFromBackendTexture(recorder.get(),
98 texture,
99 kAlpha_8_SkColorType,
100 /*colorSpace=*/nullptr,
101 /*props=*/nullptr);
102 REPORTER_ASSERT(reporter, !surface);
103
104 context->deleteBackendTexture(texture);
105
106 // We should fail to make a wrap non renderable texture in a surface.
107 info = caps->getDefaultSampledTextureInfo(kRGBA_8888_SkColorType,
108 /*levelCount=*/1,
109 Protected::kNo,
110 Renderable::kNo);
111 texture = context->createBackendTexture(kSize, info);
112 REPORTER_ASSERT(reporter, texture.isValid());
113
114 surface = MakeGraphiteFromBackendTexture(recorder.get(),
115 texture,
116 kRGBA_8888_SkColorType,
117 /*colorSpace=*/nullptr,
118 /*props=*/nullptr);
119
120 REPORTER_ASSERT(reporter, !surface);
121 context->deleteBackendTexture(texture);
122 }
123
124