1 // Copyright 2019 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "gpu/command_buffer/service/test_shared_image_backing.h"
6 #include "build/build_config.h"
7 #include "components/viz/common/resources/resource_format_utils.h"
8 #include "gpu/command_buffer/service/shared_context_state.h"
9 #include "third_party/skia/include/core/SkPromiseImageTexture.h"
10 #include "third_party/skia/include/gpu/GrBackendSurface.h"
11 #include "third_party/skia/include/gpu/mock/GrMockTypes.h"
12
13 namespace gpu {
14 namespace {
15 class TestSharedImageRepresentationGLTexture
16 : public SharedImageRepresentationGLTexture {
17 public:
TestSharedImageRepresentationGLTexture(SharedImageManager * manager,SharedImageBacking * backing,MemoryTypeTracker * tracker,gles2::Texture * texture)18 TestSharedImageRepresentationGLTexture(SharedImageManager* manager,
19 SharedImageBacking* backing,
20 MemoryTypeTracker* tracker,
21 gles2::Texture* texture)
22 : SharedImageRepresentationGLTexture(manager, backing, tracker),
23 texture_(texture) {}
24
GetTexture()25 gles2::Texture* GetTexture() override { return texture_; }
BeginAccess(GLenum mode)26 bool BeginAccess(GLenum mode) override {
27 return static_cast<TestSharedImageBacking*>(backing())->can_access();
28 }
29
30 private:
31 gles2::Texture* const texture_;
32 };
33
34 class TestSharedImageRepresentationGLTexturePassthrough
35 : public SharedImageRepresentationGLTexturePassthrough {
36 public:
TestSharedImageRepresentationGLTexturePassthrough(SharedImageManager * manager,SharedImageBacking * backing,MemoryTypeTracker * tracker,scoped_refptr<gles2::TexturePassthrough> texture)37 TestSharedImageRepresentationGLTexturePassthrough(
38 SharedImageManager* manager,
39 SharedImageBacking* backing,
40 MemoryTypeTracker* tracker,
41 scoped_refptr<gles2::TexturePassthrough> texture)
42 : SharedImageRepresentationGLTexturePassthrough(manager,
43 backing,
44 tracker),
45 texture_(std::move(texture)) {}
46
GetTexturePassthrough()47 const scoped_refptr<gles2::TexturePassthrough>& GetTexturePassthrough()
48 override {
49 return texture_;
50 }
BeginAccess(GLenum mode)51 bool BeginAccess(GLenum mode) override {
52 return static_cast<TestSharedImageBacking*>(backing())->can_access();
53 }
54
55 private:
56 const scoped_refptr<gles2::TexturePassthrough> texture_;
57 };
58
59 class TestSharedImageRepresentationSkia : public SharedImageRepresentationSkia {
60 public:
TestSharedImageRepresentationSkia(SharedImageManager * manager,SharedImageBacking * backing,MemoryTypeTracker * tracker)61 TestSharedImageRepresentationSkia(SharedImageManager* manager,
62 SharedImageBacking* backing,
63 MemoryTypeTracker* tracker)
64 : SharedImageRepresentationSkia(manager, backing, tracker) {}
65
66 protected:
BeginWriteAccess(int final_msaa_count,const SkSurfaceProps & surface_props,std::vector<GrBackendSemaphore> * begin_semaphores,std::vector<GrBackendSemaphore> * end_semaphores)67 sk_sp<SkSurface> BeginWriteAccess(
68 int final_msaa_count,
69 const SkSurfaceProps& surface_props,
70 std::vector<GrBackendSemaphore>* begin_semaphores,
71 std::vector<GrBackendSemaphore>* end_semaphores) override {
72 if (!static_cast<TestSharedImageBacking*>(backing())->can_access()) {
73 return nullptr;
74 }
75 return SkSurface::MakeRasterN32Premul(size().width(), size().height());
76 }
EndWriteAccess(sk_sp<SkSurface> surface)77 void EndWriteAccess(sk_sp<SkSurface> surface) override {}
BeginReadAccess(std::vector<GrBackendSemaphore> * begin_semaphores,std::vector<GrBackendSemaphore> * end_semaphores)78 sk_sp<SkPromiseImageTexture> BeginReadAccess(
79 std::vector<GrBackendSemaphore>* begin_semaphores,
80 std::vector<GrBackendSemaphore>* end_semaphores) override {
81 if (!static_cast<TestSharedImageBacking*>(backing())->can_access()) {
82 return nullptr;
83 }
84 GrBackendTexture backend_tex(size().width(), size().height(),
85 GrMipMapped::kNo, GrMockTextureInfo());
86 return SkPromiseImageTexture::Make(backend_tex);
87 }
EndReadAccess()88 void EndReadAccess() override {}
89 };
90
91 class TestSharedImageRepresentationDawn : public SharedImageRepresentationDawn {
92 public:
TestSharedImageRepresentationDawn(SharedImageManager * manager,SharedImageBacking * backing,MemoryTypeTracker * tracker)93 TestSharedImageRepresentationDawn(SharedImageManager* manager,
94 SharedImageBacking* backing,
95 MemoryTypeTracker* tracker)
96 : SharedImageRepresentationDawn(manager, backing, tracker) {}
97
BeginAccess(WGPUTextureUsage usage)98 WGPUTexture BeginAccess(WGPUTextureUsage usage) override {
99 if (!static_cast<TestSharedImageBacking*>(backing())->can_access()) {
100 return nullptr;
101 }
102
103 // Return a dummy value.
104 return reinterpret_cast<WGPUTexture>(203);
105 }
106
EndAccess()107 void EndAccess() override {}
108 };
109
110 class TestSharedImageRepresentationOverlay
111 : public SharedImageRepresentationOverlay {
112 public:
TestSharedImageRepresentationOverlay(SharedImageManager * manager,SharedImageBacking * backing,MemoryTypeTracker * tracker)113 TestSharedImageRepresentationOverlay(SharedImageManager* manager,
114 SharedImageBacking* backing,
115 MemoryTypeTracker* tracker)
116 : SharedImageRepresentationOverlay(manager, backing, tracker) {}
117
BeginReadAccess()118 bool BeginReadAccess() override { return true; }
EndReadAccess()119 void EndReadAccess() override {}
GetGLImage()120 gl::GLImage* GetGLImage() override { return nullptr; }
121
122 #if defined(OS_ANDROID)
NotifyOverlayPromotion(bool promotion,const gfx::Rect & bounds)123 void NotifyOverlayPromotion(bool promotion,
124 const gfx::Rect& bounds) override {}
125 #endif
126 };
127
128 } // namespace
129
TestSharedImageBacking(const Mailbox & mailbox,viz::ResourceFormat format,const gfx::Size & size,const gfx::ColorSpace & color_space,uint32_t usage,size_t estimated_size,GLuint texture_id)130 TestSharedImageBacking::TestSharedImageBacking(
131 const Mailbox& mailbox,
132 viz::ResourceFormat format,
133 const gfx::Size& size,
134 const gfx::ColorSpace& color_space,
135 uint32_t usage,
136 size_t estimated_size,
137 GLuint texture_id)
138 : SharedImageBacking(mailbox,
139 format,
140 size,
141 color_space,
142 usage,
143 estimated_size,
144 false /* is_thread_safe */),
145 service_id_(texture_id) {
146 texture_ = new gles2::Texture(service_id_);
147 texture_->SetLightweightRef();
148 texture_->SetTarget(GL_TEXTURE_2D, 1);
149 texture_->sampler_state_.min_filter = GL_LINEAR;
150 texture_->sampler_state_.mag_filter = GL_LINEAR;
151 texture_->sampler_state_.wrap_s = GL_CLAMP_TO_EDGE;
152 texture_->sampler_state_.wrap_t = GL_CLAMP_TO_EDGE;
153 texture_->SetLevelInfo(GL_TEXTURE_2D, 0, GLInternalFormat(format),
154 size.width(), size.height(), 1, 0,
155 GLDataFormat(format), GLDataType(format), gfx::Rect());
156 texture_->SetImmutable(true, true);
157 texture_passthrough_ = base::MakeRefCounted<gles2::TexturePassthrough>(
158 service_id_, GL_TEXTURE_2D);
159 }
160
TestSharedImageBacking(const Mailbox & mailbox,viz::ResourceFormat format,const gfx::Size & size,const gfx::ColorSpace & color_space,uint32_t usage,size_t estimated_size)161 TestSharedImageBacking::TestSharedImageBacking(
162 const Mailbox& mailbox,
163 viz::ResourceFormat format,
164 const gfx::Size& size,
165 const gfx::ColorSpace& color_space,
166 uint32_t usage,
167 size_t estimated_size)
168 : TestSharedImageBacking(mailbox,
169 format,
170 size,
171 color_space,
172 usage,
173 estimated_size,
174 203 /* texture_id */) {
175 // Using a dummy |texture_id|, so lose our context so we don't do anything
176 // real with it.
177 OnContextLost();
178 }
179
~TestSharedImageBacking()180 TestSharedImageBacking::~TestSharedImageBacking() {
181 // Pretend our context is lost to avoid actual cleanup in |texture_| or
182 // |passthrough_texture_|.
183 texture_->RemoveLightweightRef(false /* have_context */);
184 texture_passthrough_->MarkContextLost();
185 texture_passthrough_.reset();
186
187 if (have_context())
188 glDeleteTextures(1, &service_id_);
189 }
190
ClearedRect() const191 gfx::Rect TestSharedImageBacking::ClearedRect() const {
192 return texture_->GetLevelClearedRect(texture_->target(), 0);
193 }
194
SetClearedRect(const gfx::Rect & cleared_rect)195 void TestSharedImageBacking::SetClearedRect(const gfx::Rect& cleared_rect) {
196 texture_->SetLevelClearedRect(texture_->target(), 0, cleared_rect);
197 }
198
ProduceLegacyMailbox(MailboxManager * mailbox_manager)199 bool TestSharedImageBacking::ProduceLegacyMailbox(
200 MailboxManager* mailbox_manager) {
201 return false;
202 }
203
204 std::unique_ptr<SharedImageRepresentationGLTexture>
ProduceGLTexture(SharedImageManager * manager,MemoryTypeTracker * tracker)205 TestSharedImageBacking::ProduceGLTexture(SharedImageManager* manager,
206 MemoryTypeTracker* tracker) {
207 return std::make_unique<TestSharedImageRepresentationGLTexture>(
208 manager, this, tracker, texture_);
209 }
210
211 std::unique_ptr<SharedImageRepresentationGLTexturePassthrough>
ProduceGLTexturePassthrough(SharedImageManager * manager,MemoryTypeTracker * tracker)212 TestSharedImageBacking::ProduceGLTexturePassthrough(
213 SharedImageManager* manager,
214 MemoryTypeTracker* tracker) {
215 return std::make_unique<TestSharedImageRepresentationGLTexturePassthrough>(
216 manager, this, tracker, texture_passthrough_);
217 }
218
219 std::unique_ptr<SharedImageRepresentationSkia>
ProduceSkia(SharedImageManager * manager,MemoryTypeTracker * tracker,scoped_refptr<SharedContextState> context_state)220 TestSharedImageBacking::ProduceSkia(
221 SharedImageManager* manager,
222 MemoryTypeTracker* tracker,
223 scoped_refptr<SharedContextState> context_state) {
224 return std::make_unique<TestSharedImageRepresentationSkia>(manager, this,
225 tracker);
226 }
227
228 std::unique_ptr<SharedImageRepresentationDawn>
ProduceDawn(SharedImageManager * manager,MemoryTypeTracker * tracker,WGPUDevice device)229 TestSharedImageBacking::ProduceDawn(SharedImageManager* manager,
230 MemoryTypeTracker* tracker,
231 WGPUDevice device) {
232 return std::make_unique<TestSharedImageRepresentationDawn>(manager, this,
233 tracker);
234 }
235
236 std::unique_ptr<SharedImageRepresentationOverlay>
ProduceOverlay(SharedImageManager * manager,MemoryTypeTracker * tracker)237 TestSharedImageBacking::ProduceOverlay(SharedImageManager* manager,
238 MemoryTypeTracker* tracker) {
239 return std::make_unique<TestSharedImageRepresentationOverlay>(manager, this,
240 tracker);
241 }
242
243 } // namespace gpu
244