| 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 void DeviceBufferGLES::Flush(std::optional<Range> range) const {
61 generation_++;
62 }
63
64 static GLenum ToTarget(DeviceBufferGLES::BindingType type) {
65 switch (type) {
66 case DeviceBufferGLES::BindingType::kArrayBuffer:
67 return GL_ARRAY_BUFFER;
68 case DeviceBufferGLES::BindingType::kElementArrayBuffer:
69 return GL_ELEMENT_ARRAY_BUFFER;
70 }
71 FML_UNREACHABLE();
72 }
73
74 bool DeviceBufferGLES::BindAndUploadDataIfNecessary(BindingType type) const {
75 if (!reactor_) {
76 return false;
77 }
78
79 auto buffer = reactor_->GetGLHandle(handle_);
80 if (!buffer.has_value()) {
81 return false;
82 }
83
84 const auto target_type = ToTarget(type);
85 const auto& gl = reactor_->GetProcTable();
86
87 gl.BindBuffer(target_type, buffer.value());
88
89 if (upload_generation_ != generation_) {
90 TRACE_EVENT1("impeller", "BufferData", "Bytes",
91 std::to_string(backing_store_->GetLength()).c_str());
92 gl.BufferData(target_type, backing_store_->GetLength(),
93 backing_store_->GetBuffer(), GL_STATIC_DRAW);
94 upload_generation_ = generation_;
95 }
96
97 return true;
98 }
99
100 // |DeviceBuffer|
101 bool DeviceBufferGLES::SetLabel(const std::string& label) {
102 reactor_->SetDebugLabel(handle_, label);
103 return true;
104 }
105
106 // |DeviceBuffer|
107 bool DeviceBufferGLES::SetLabel(const std::string& label, Range range) {
108 // Cannot support debug label on the range. Set the label for the entire
109 // range.
110 return SetLabel(label);
111 }
112
113 const uint8_t* DeviceBufferGLES::GetBufferData() const {
114 return backing_store_->GetBuffer();
115 }
116
117 void DeviceBufferGLES::UpdateBufferData(
118 const std::function<void(uint8_t* data, size_t length)>&
119 update_buffer_data) {
120 if (update_buffer_data) {
121 update_buffer_data(backing_store_->GetBuffer(),
122 backing_store_->GetLength());
123 ++generation_;
124 }
125 }
126
127 } // namespace impeller
impeller::DeviceBufferGLES::BindingType::kArrayBuffer
@ kArrayBuffer
impeller::DeviceBuffer
Definition: device_buffer.h:19
allocation.h
impeller::DeviceBufferDescriptor
Definition: device_buffer_descriptor.h:14
impeller::ReactorGLES::Ref
std::shared_ptr< ReactorGLES > Ref
Definition: reactor_gles.h:87
impeller::HandleType
HandleType
Definition: handle_gles.h:22
impeller::HandleGLES::IsDead
constexpr bool IsDead() const
Definition: handle_gles.h:43
validation.h
impeller::DeviceBufferGLES::GetBufferData
const uint8_t * GetBufferData() const
Definition: device_buffer_gles.cc:113
device_buffer_gles.h
impeller::DeviceBufferGLES::BindingType::kElementArrayBuffer
@ kElementArrayBuffer
impeller::DeviceBufferGLES::BindAndUploadDataIfNecessary
bool BindAndUploadDataIfNecessary(BindingType type) const
Definition: device_buffer_gles.cc:74
impeller::HandleGLES
Definition: handle_gles.h:35
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:117
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:34
impeller::ToTarget
static GLenum ToTarget(DeviceBufferGLES::BindingType type)
Definition: device_buffer_gles.cc:64
std
Definition: comparable.h:95
offset
Point offset
Definition: stroke_path_geometry.cc:300
impeller::HandleType::kBuffer
@ kBuffer
config.h
impeller
Definition: aiks_blur_unittests.cc:20
impeller::DeviceBufferGLES::Flush
void Flush(std::optional< Range > range=std::nullopt) const override
Definition: device_buffer_gles.cc:60