| Flutter Impeller |

device_buffer_gles.cc
Go to the documentation of this file.
1 // Copyright 2013 The Flutter 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 "impeller/renderer/backend/gles/device_buffer_gles.h"
6
7 #include <cstring>
8 #include <memory>
9
10 #include "flutter/fml/trace_event.h"
11 #include "impeller/base/allocation.h"
12 #include "impeller/base/config.h"
13 #include "impeller/base/validation.h"
14
15 namespace impeller {
16
17 DeviceBufferGLES::DeviceBufferGLES(DeviceBufferDescriptor desc,
18 ReactorGLES::Ref reactor,
19 std::shared_ptr<Allocation> backing_store)
20 : DeviceBuffer(desc),
21 reactor_(std::move(reactor)),
22 handle_(reactor_ ? reactor_->CreateHandle(HandleType::kBuffer)
23 : HandleGLES::DeadHandle()),
24 backing_store_(std::move(backing_store)) {}
25
26 // |DeviceBuffer|
27 DeviceBufferGLES::~DeviceBufferGLES() {
28 if (!handle_.IsDead()) {
29 reactor_->CollectHandle(handle_);
30 }
31 }
32
33 // |DeviceBuffer|
34 uint8_t* DeviceBufferGLES::OnGetContents() const {
35 if (!reactor_) {
36 return nullptr;
37 }
38 return backing_store_->GetBuffer();
39 }
40
41 // |DeviceBuffer|
42 bool DeviceBufferGLES::OnCopyHostBuffer(const uint8_t* source,
43 Range source_range,
44 size_t offset) {
45 if (!reactor_) {
46 return false;
47 }
48
49 if (offset + source_range.length > backing_store_->GetLength()) {
50 return false;
51 }
52
53 std::memmove(backing_store_->GetBuffer() + offset,
54 source + source_range.offset, source_range.length);
55 ++generation_;
56
57 return true;
58 }
59
60 static GLenum ToTarget(DeviceBufferGLES::BindingType type) {
61 switch (type) {
62 case DeviceBufferGLES::BindingType::kArrayBuffer:
63 return GL_ARRAY_BUFFER;
64 case DeviceBufferGLES::BindingType::kElementArrayBuffer:
65 return GL_ELEMENT_ARRAY_BUFFER;
66 }
67 FML_UNREACHABLE();
68 }
69
70 bool DeviceBufferGLES::BindAndUploadDataIfNecessary(BindingType type) const {
71 if (!reactor_) {
72 return false;
73 }
74
75 auto buffer = reactor_->GetGLHandle(handle_);
76 if (!buffer.has_value()) {
77 return false;
78 }
79
80 const auto target_type = ToTarget(type);
81 const auto& gl = reactor_->GetProcTable();
82
83 gl.BindBuffer(target_type, buffer.value());
84
85 if (upload_generation_ != generation_) {
86 TRACE_EVENT1("impeller", "BufferData", "Bytes",
87 std::to_string(backing_store_->GetLength()).c_str());
88 gl.BufferData(target_type, backing_store_->GetLength(),
89 backing_store_->GetBuffer(), GL_STATIC_DRAW);
90 upload_generation_ = generation_;
91 }
92
93 return true;
94 }
95
96 // |DeviceBuffer|
97 bool DeviceBufferGLES::SetLabel(const std::string& label) {
98 reactor_->SetDebugLabel(handle_, label);
99 return true;
100 }
101
102 // |DeviceBuffer|
103 bool DeviceBufferGLES::SetLabel(const std::string& label, Range range) {
104 // Cannot support debug label on the range. Set the label for the entire
105 // range.
106 return SetLabel(label);
107 }
108
109 const uint8_t* DeviceBufferGLES::GetBufferData() const {
110 return backing_store_->GetBuffer();
111 }
112
113 void DeviceBufferGLES::UpdateBufferData(
114 const std::function<void(uint8_t* data, size_t length)>&
115 update_buffer_data) {
116 if (update_buffer_data) {
117 update_buffer_data(backing_store_->GetBuffer(),
118 backing_store_->GetLength());
119 ++generation_;
120 }
121 }
122
123 } // namespace impeller
impeller::DeviceBufferGLES::BindingType::kArrayBuffer
@ kArrayBuffer
impeller::DeviceBuffer
Definition: device_buffer.h:20
allocation.h
impeller::DeviceBufferDescriptor
Definition: device_buffer_descriptor.h:13
impeller::ReactorGLES::Ref
std::shared_ptr< ReactorGLES > Ref
Definition: reactor_gles.h:31
impeller::HandleType
HandleType
Definition: handle_gles.h:21
impeller::HandleGLES::IsDead
constexpr bool IsDead() const
Definition: handle_gles.h:42
validation.h
impeller::DeviceBufferGLES::GetBufferData
const uint8_t * GetBufferData() const
Definition: device_buffer_gles.cc:109
device_buffer_gles.h
impeller::DeviceBufferGLES::BindingType::kElementArrayBuffer
@ kElementArrayBuffer
impeller::DeviceBufferGLES::BindAndUploadDataIfNecessary
bool BindAndUploadDataIfNecessary(BindingType type) const
Definition: device_buffer_gles.cc:70
impeller::HandleGLES
Definition: handle_gles.h:34
impeller::DeviceBufferGLES::~DeviceBufferGLES
~DeviceBufferGLES() override
Definition: device_buffer_gles.cc:27
impeller::DeviceBufferGLES::UpdateBufferData
void UpdateBufferData(const std::function< void(uint8_t *, size_t length)> &update_buffer_data)
Definition: device_buffer_gles.cc:113
impeller::DeviceBufferGLES::DeviceBufferGLES
DeviceBufferGLES(DeviceBufferDescriptor desc, ReactorGLES::Ref reactor, std::shared_ptr< Allocation > backing_store)
Definition: device_buffer_gles.cc:17
impeller::DeviceBufferGLES::BindingType
BindingType
Definition: device_buffer_gles.h:33
impeller::ToTarget
static GLenum ToTarget(DeviceBufferGLES::BindingType type)
Definition: device_buffer_gles.cc:60
std
Definition: comparable.h:98
impeller::HandleType::kBuffer
@ kBuffer
config.h
impeller
Definition: aiks_context.cc:10