1 // Copyright 2018 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/texture_owner.h"
6 
7 #include <memory>
8 
9 #include "base/bind.h"
10 #include "base/feature_list.h"
11 #include "base/threading/thread_task_runner_handle.h"
12 #include "gpu/command_buffer/service/abstract_texture.h"
13 #include "gpu/command_buffer/service/abstract_texture_impl_shared_context_state.h"
14 #include "gpu/command_buffer/service/decoder_context.h"
15 #include "gpu/command_buffer/service/feature_info.h"
16 #include "gpu/command_buffer/service/image_reader_gl_owner.h"
17 #include "gpu/command_buffer/service/surface_texture_gl_owner.h"
18 #include "gpu/command_buffer/service/texture_base.h"
19 #include "ui/gl/scoped_binders.h"
20 
21 namespace gpu {
22 
TextureOwner(bool binds_texture_on_update,std::unique_ptr<gles2::AbstractTexture> texture)23 TextureOwner::TextureOwner(bool binds_texture_on_update,
24                            std::unique_ptr<gles2::AbstractTexture> texture)
25     : base::RefCountedDeleteOnSequence<TextureOwner>(
26           base::ThreadTaskRunnerHandle::Get()),
27       binds_texture_on_update_(binds_texture_on_update),
28       texture_(std::move(texture)),
29       task_runner_(base::ThreadTaskRunnerHandle::Get()) {
30   // Notify the subclass when the texture is destroyed.
31   // Unretained is safe, since we insist that |texture_| is dropped before we're
32   // destroyed, which implies that the callback has run.
33   texture_->SetCleanupCallback(base::BindOnce(&TextureOwner::OnTextureDestroyed,
34                                               base::Unretained(this)));
35 }
36 
~TextureOwner()37 TextureOwner::~TextureOwner() {
38   // The subclass must delete the texture before now.
39   DCHECK(!texture_);
40 }
41 
42 // static
Create(std::unique_ptr<gles2::AbstractTexture> texture,Mode mode)43 scoped_refptr<TextureOwner> TextureOwner::Create(
44     std::unique_ptr<gles2::AbstractTexture> texture,
45     Mode mode) {
46   switch (mode) {
47     case Mode::kAImageReaderInsecure:
48     case Mode::kAImageReaderInsecureMultithreaded:
49     case Mode::kAImageReaderInsecureSurfaceControl:
50     case Mode::kAImageReaderSecureSurfaceControl:
51       return new ImageReaderGLOwner(std::move(texture), mode);
52     case Mode::kSurfaceTextureInsecure:
53       return new SurfaceTextureGLOwner(std::move(texture));
54   }
55 
56   NOTREACHED();
57   return nullptr;
58 }
59 
60 // static
CreateTexture(scoped_refptr<SharedContextState> context_state)61 std::unique_ptr<gles2::AbstractTexture> TextureOwner::CreateTexture(
62     scoped_refptr<SharedContextState> context_state) {
63   DCHECK(context_state);
64 
65   gles2::FeatureInfo* feature_info = context_state->feature_info();
66   if (feature_info && feature_info->is_passthrough_cmd_decoder()) {
67     return std::make_unique<
68         gles2::AbstractTextureImplOnSharedContextPassthrough>(
69         GL_TEXTURE_EXTERNAL_OES, std::move(context_state));
70   }
71 
72   return std::make_unique<gles2::AbstractTextureImplOnSharedContext>(
73       GL_TEXTURE_EXTERNAL_OES, GL_RGBA,
74       0,  // width
75       0,  // height
76       1,  // depth
77       0,  // border
78       GL_RGBA, GL_UNSIGNED_BYTE, std::move(context_state));
79 }
80 
GetTextureId() const81 GLuint TextureOwner::GetTextureId() const {
82   return texture_->service_id();
83 }
84 
GetTextureBase() const85 TextureBase* TextureOwner::GetTextureBase() const {
86   return texture_->GetTextureBase();
87 }
88 
ClearAbstractTexture()89 void TextureOwner::ClearAbstractTexture() {
90   texture_.reset();
91 }
92 
93 }  // namespace gpu
94