• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2022 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 "include/gpu/graphite/BackendTexture.h"
11 #include "include/gpu/graphite/Context.h"
12 #include "include/gpu/graphite/Recorder.h"
13 #include "src/gpu/graphite/Caps.h"
14 #include "src/gpu/graphite/ContextPriv.h"
15 #include "src/gpu/graphite/RecorderPriv.h"
16 #include "src/gpu/graphite/ResourceProvider.h"
17 #include "src/gpu/graphite/Texture.h"
18 #include "src/gpu/graphite/TextureProxy.h"
19 
20 namespace skgpu::graphite {
21 
DEF_GRAPHITE_TEST_FOR_ALL_CONTEXTS(GraphiteTextureProxyTest,reporter,context)22 DEF_GRAPHITE_TEST_FOR_ALL_CONTEXTS(GraphiteTextureProxyTest, reporter, context) {
23     const Caps* caps = context->priv().caps();
24     constexpr SkISize kValidSize = SkISize::Make(1, 1);
25     constexpr SkISize kInvalidSize = SkISize::MakeEmpty();
26     constexpr SkColorType kValidColorType = kRGBA_8888_SkColorType;
27     constexpr SkColorType kInvalidColorType = kUnknown_SkColorType;
28 
29     std::unique_ptr<Recorder> recorder = context->makeRecorder();
30     ResourceProvider* resourceProvider = recorder->priv().resourceProvider();
31     const TextureInfo textureInfo = caps->getDefaultSampledTextureInfo(
32             kValidColorType, Mipmapped::kNo, Protected::kNo, Renderable::kNo);
33     const BackendTexture backendTexture = recorder->createBackendTexture(kValidSize, textureInfo);
34     sk_sp<Texture> texture = resourceProvider->createWrappedTexture(backendTexture);
35 
36     auto nullCallback = [](ResourceProvider*) -> sk_sp<Texture> { return nullptr; };
37     auto callback = [texture](ResourceProvider*) -> sk_sp<Texture> { return texture; };
38 
39     // Assign to assignableTexture before instantiating with this callback.
40     sk_sp<Texture> assignableTexture;
41     auto assignableCallback = [&assignableTexture](ResourceProvider*) -> sk_sp<Texture> {
42         return assignableTexture;
43     };
44 
45     // Invalid parameters.
46     sk_sp<TextureProxy> textureProxy;
47     textureProxy = TextureProxy::Make(caps,
48                                       kInvalidSize,
49                                       kValidColorType,
50                                       Mipmapped::kNo,
51                                       Protected::kNo,
52                                       Renderable::kNo,
53                                       skgpu::Budgeted::kNo);
54     REPORTER_ASSERT(reporter, textureProxy == nullptr);
55     textureProxy = TextureProxy::Make(caps,
56                                       kValidSize,
57                                       kInvalidColorType,
58                                       Mipmapped::kNo,
59                                       Protected::kNo,
60                                       Renderable::kNo,
61                                       skgpu::Budgeted::kNo);
62     REPORTER_ASSERT(reporter, textureProxy == nullptr);
63 
64     // Non-lazy TextureProxy, successful instantiation.
65     textureProxy = TextureProxy::Make(caps,
66                                       kValidSize,
67                                       kValidColorType,
68                                       Mipmapped::kNo,
69                                       Protected::kNo,
70                                       Renderable::kNo,
71                                       skgpu::Budgeted::kNo);
72     REPORTER_ASSERT(reporter, !textureProxy->isLazy());
73     REPORTER_ASSERT(reporter, !textureProxy->isFullyLazy());
74     REPORTER_ASSERT(reporter, !textureProxy->isVolatile());
75     REPORTER_ASSERT(reporter, !textureProxy->isInstantiated());
76     REPORTER_ASSERT(reporter, textureProxy->dimensions() == kValidSize);
77 
78     bool instantiateSuccess = textureProxy->instantiate(resourceProvider);
79     REPORTER_ASSERT(reporter, instantiateSuccess);
80     REPORTER_ASSERT(reporter, textureProxy->isInstantiated());
81     REPORTER_ASSERT(reporter, textureProxy->dimensions() == kValidSize);
82     const Texture* createdTexture = textureProxy->texture();
83 
84     instantiateSuccess = textureProxy->instantiate(resourceProvider);
85     REPORTER_ASSERT(reporter, instantiateSuccess);
86     REPORTER_ASSERT(reporter, textureProxy->texture() == createdTexture);
87 
88     // Lazy, non-volatile TextureProxy, unsuccessful instantiation.
89     textureProxy = TextureProxy::MakeLazy(
90             kValidSize, textureInfo, skgpu::Budgeted::kNo, Volatile::kNo, nullCallback);
91     REPORTER_ASSERT(reporter, textureProxy->isLazy());
92     REPORTER_ASSERT(reporter, !textureProxy->isFullyLazy());
93     REPORTER_ASSERT(reporter, !textureProxy->isVolatile());
94 
95     instantiateSuccess = textureProxy->lazyInstantiate(resourceProvider);
96     REPORTER_ASSERT(reporter, !instantiateSuccess);
97     REPORTER_ASSERT(reporter, !textureProxy->isInstantiated());
98 
99     // Lazy, non-volatile TextureProxy, successful instantiation.
100     textureProxy = TextureProxy::MakeLazy(
101             kValidSize, textureInfo, skgpu::Budgeted::kNo, Volatile::kNo, callback);
102 
103     instantiateSuccess = textureProxy->lazyInstantiate(resourceProvider);
104     REPORTER_ASSERT(reporter, instantiateSuccess);
105     REPORTER_ASSERT(reporter, textureProxy->texture() == texture.get());
106 
107     // Lazy, volatile TextureProxy, unsuccessful instantiation.
108     textureProxy = TextureProxy::MakeLazy(
109             kValidSize, textureInfo, skgpu::Budgeted::kNo, Volatile::kYes, nullCallback);
110     REPORTER_ASSERT(reporter, textureProxy->isLazy());
111     REPORTER_ASSERT(reporter, !textureProxy->isFullyLazy());
112     REPORTER_ASSERT(reporter, textureProxy->isVolatile());
113 
114     instantiateSuccess = textureProxy->lazyInstantiate(resourceProvider);
115     REPORTER_ASSERT(reporter, !instantiateSuccess);
116     REPORTER_ASSERT(reporter, !textureProxy->isInstantiated());
117 
118     // Lazy, volatile TextureProxy, successful instantiation.
119     textureProxy = TextureProxy::MakeLazy(
120             kValidSize, textureInfo, skgpu::Budgeted::kNo, Volatile::kYes, callback);
121 
122     instantiateSuccess = textureProxy->lazyInstantiate(resourceProvider);
123     REPORTER_ASSERT(reporter, instantiateSuccess);
124     REPORTER_ASSERT(reporter, textureProxy->texture() == texture.get());
125 
126     textureProxy->deinstantiate();
127     REPORTER_ASSERT(reporter, !textureProxy->isInstantiated());
128 
129     // Fully-lazy TextureProxy.
130     textureProxy = TextureProxy::MakeFullyLazy(
131             textureInfo, skgpu::Budgeted::kNo, Volatile::kYes, assignableCallback);
132     REPORTER_ASSERT(reporter, textureProxy->isLazy());
133     REPORTER_ASSERT(reporter, textureProxy->isFullyLazy());
134     REPORTER_ASSERT(reporter, textureProxy->isVolatile());
135 
136     assignableTexture = texture;
137     instantiateSuccess = textureProxy->lazyInstantiate(resourceProvider);
138     REPORTER_ASSERT(reporter, instantiateSuccess);
139     REPORTER_ASSERT(reporter, textureProxy->isInstantiated());
140     REPORTER_ASSERT(reporter, textureProxy->isFullyLazy());
141     REPORTER_ASSERT(reporter, textureProxy->dimensions() == kValidSize);
142 
143     textureProxy->deinstantiate();
144     REPORTER_ASSERT(reporter, !textureProxy->isInstantiated());
145     REPORTER_ASSERT(reporter, textureProxy->isFullyLazy());
146 
147     constexpr SkISize kLargerSize = SkISize::Make(2, 2);
148     const BackendTexture largerBackendTexture =
149             recorder->createBackendTexture(kLargerSize, textureInfo);
150     assignableTexture = resourceProvider->createWrappedTexture(largerBackendTexture);
151     instantiateSuccess = textureProxy->lazyInstantiate(resourceProvider);
152     REPORTER_ASSERT(reporter, instantiateSuccess);
153     REPORTER_ASSERT(reporter, textureProxy->dimensions() == kLargerSize);
154 
155     // InstantiateIfNotLazy tests.
156     textureProxy = TextureProxy::Make(caps,
157                                       kValidSize,
158                                       kValidColorType,
159                                       Mipmapped::kNo,
160                                       Protected::kNo,
161                                       Renderable::kNo,
162                                       skgpu::Budgeted::kNo);
163     instantiateSuccess = TextureProxy::InstantiateIfNotLazy(resourceProvider, textureProxy.get());
164     REPORTER_ASSERT(reporter, instantiateSuccess);
165 
166     textureProxy = TextureProxy::MakeLazy(
167             kValidSize, textureInfo, skgpu::Budgeted::kNo, Volatile::kNo, nullCallback);
168     instantiateSuccess = TextureProxy::InstantiateIfNotLazy(resourceProvider, textureProxy.get());
169     REPORTER_ASSERT(reporter, instantiateSuccess);
170 }
171 
172 }  // namespace skgpu::graphite
173