Avi Drissman | 4a8573c | 2022-09-09 19:35:54 | [diff] [blame] | 1 | // Copyright 2018 The Chromium Authors |
Aldo Culquicondor | 9152d53 | 2018-09-25 14:55:14 | [diff] [blame] | 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 "chrome/browser/vr/cmd_buffer_surface_provider.h" |
| 6 | |
Hans Wennborg | 1790e6b | 2020-04-24 19:10:33 | [diff] [blame] | 7 | #include "base/check_op.h" |
Aldo Culquicondor | 9152d53 | 2018-09-25 14:55:14 | [diff] [blame] | 8 | #include "gpu/command_buffer/client/gles2_implementation.h" |
| 9 | #include "gpu/command_buffer/client/gles2_lib.h" |
| 10 | #include "gpu/skia_bindings/gl_bindings_skia_cmd_buffer.h" |
Robert Phillips | dbb0b7d | 2020-07-29 16:07:44 | [diff] [blame] | 11 | #include "third_party/skia/include/gpu/GrDirectContext.h" |
Aldo Culquicondor | 9152d53 | 2018-09-25 14:55:14 | [diff] [blame] | 12 | #include "third_party/skia/include/gpu/gl/GrGLInterface.h" |
| 13 | #include "ui/gfx/geometry/size.h" |
| 14 | |
| 15 | namespace vr { |
| 16 | |
| 17 | CmdBufferSurfaceProvider::CmdBufferSurfaceProvider() { |
| 18 | auto* gles2_implementation = |
| 19 | static_cast<gpu::gles2::GLES2Implementation*>(gles2::GetGLContext()); |
| 20 | DCHECK(gles2_implementation); |
| 21 | sk_sp<const GrGLInterface> gr_interface = |
| 22 | skia_bindings::CreateGLES2InterfaceBindings(gles2_implementation, |
| 23 | gles2_implementation); |
Robert Phillips | dbb0b7d | 2020-07-29 16:07:44 | [diff] [blame] | 24 | gr_context_ = GrDirectContext::MakeGL(std::move(gr_interface)); |
Aldo Culquicondor | 9152d53 | 2018-09-25 14:55:14 | [diff] [blame] | 25 | DCHECK(gr_context_); |
Aldo Culquicondor | 9152d53 | 2018-09-25 14:55:14 | [diff] [blame] | 26 | } |
| 27 | |
| 28 | CmdBufferSurfaceProvider::~CmdBufferSurfaceProvider() = default; |
| 29 | |
Vasiliy Telezhnikov | 2fa8baf | 2023-06-20 20:44:10 | [diff] [blame^] | 30 | std::unique_ptr<SkiaSurfaceProvider::Texture> |
| 31 | CmdBufferSurfaceProvider::CreateTextureWithSkia( |
| 32 | const gfx::Size& size, |
| 33 | base::FunctionRef<void(SkCanvas*)> paint) { |
| 34 | // We need to store and restore previous FBO after skia draw. |
| 35 | GLint prev_fbo = 0; |
| 36 | glGetIntegerv(GL_FRAMEBUFFER_BINDING, &prev_fbo); |
Aldo Culquicondor | 9152d53 | 2018-09-25 14:55:14 | [diff] [blame] | 37 | |
Vasiliy Telezhnikov | 2fa8baf | 2023-06-20 20:44:10 | [diff] [blame^] | 38 | auto texture = CreateTextureWithSkiaImpl(gr_context_.get(), size, paint); |
| 39 | |
Adlai Holler | abb78ea | 2020-09-16 11:27:57 | [diff] [blame] | 40 | gr_context_->resetContext(); |
Vasiliy Telezhnikov | 2fa8baf | 2023-06-20 20:44:10 | [diff] [blame^] | 41 | glBindFramebuffer(GL_FRAMEBUFFER, prev_fbo); |
Aldo Culquicondor | 9152d53 | 2018-09-25 14:55:14 | [diff] [blame] | 42 | |
Vasiliy Telezhnikov | 2fa8baf | 2023-06-20 20:44:10 | [diff] [blame^] | 43 | return texture; |
Aldo Culquicondor | 9152d53 | 2018-09-25 14:55:14 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | } // namespace vr |