1 // Copyright 2022 The Android Open Source Project
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either expresso or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include "BufferGl.h"
16 
17 namespace gfxstream {
18 namespace gl {
19 
BufferGl(uint64_t size,HandleType handle,ContextHelper * helper)20 BufferGl::BufferGl(uint64_t size, HandleType handle, ContextHelper* helper)
21     : mSize(size), mHandle(handle), mContextHelper(helper) {}
22 
23 // static
create(uint64_t size,HandleType handle,ContextHelper * helper)24 std::unique_ptr<BufferGl> BufferGl::create(uint64_t size, HandleType handle, ContextHelper* helper) {
25     RecursiveScopedContextBind bind(helper);
26     if (!bind.isOk()) {
27         return NULL;
28     }
29 
30     std::unique_ptr<BufferGl> buffer(new BufferGl(size, handle, helper));
31 
32     /*
33     // TODO: GL_EXT_external_buffer
34     s_gles2.glGenBuffers(1, &buffer->m_buffer);
35     s_gles2.glBindBuffer(GL_ARRAY_BUFFER, buffer->m_buffer);
36     s_gles2.glBufferData(GL_ARRAY_BUFFER, size, nullptr, GL_DYNAMIC_DRAW);
37     */
38 
39     return buffer;
40 }
41 
read(uint64_t offset,uint64_t size,void * bytes)42 void BufferGl::read(uint64_t offset, uint64_t size, void* bytes) {
43     RecursiveScopedContextBind bind(mContextHelper);
44     if (!bind.isOk()) {
45         return;
46     }
47 
48     // Note: Gfxstream does not yet support GL_EXT_external_buffer so BufferGl reads are
49     // currently a no-op from the host point-of-view when the guest is not using ANGLE.
50     // Instead, the guest shadow buffer contains the source of truth of the buffer
51     // contents.
52     //
53     // For completeness, this is not fully correct as a guest that is not using ANGLE
54     // could still have native users of Vulkan. In such cases, the guest shadow buffer
55     // contents are not yet sync'ed with the Vulkan contents. However, this has not yet
56     // been observed to be an issue.
57 
58     /*
59     // TODO: GL_EXT_external_buffer
60     s_gles2.glBindBuffer(GL_ARRAY_BUFFER, m_buffer);
61     void* mapped = s_gles2.glMapBufferRange(GL_ARRAY_BUFFER, offset, size, GL_MAP_READ_BIT);
62     std::memcpy(bytes, mapped, size);
63     s_gles2.glUnmapBuffer(GL_ARRAY_BUFFER);
64     s_gles2.glBindBuffer(GL_ARRAY_BUFFER, 0);
65     */
66 }
67 
subUpdate(uint64_t offset,uint64_t size,const void * bytes)68 void BufferGl::subUpdate(uint64_t offset, uint64_t size, const void* bytes) {
69     RecursiveScopedContextBind bind(mContextHelper);
70     if (!bind.isOk()) {
71         return;
72     }
73 
74     // Note: Gfxstream does not yet support GL_EXT_external_buffer so BufferGl writes are
75     // currently a no-op from the host point-of-view when the guest is not using ANGLE.
76     // Instead, the guest shadow buffer contains the source of truth of the buffer
77     // contents.
78     //
79     // For completeness, this is not fully correct as a guest that is not using ANGLE
80     // could still have native users of Vulkan. In such cases, the guest shadow buffer
81     // contents are not yet sync'ed with the Vulkan contents. However, this has not yet
82     // been observed to be an issue.
83 
84     /*
85     // TODO: GL_EXT_external_buffer
86     s_gles2.glBindBuffer(GL_ARRAY_BUFFER, m_buffer);
87     s_gles2.glBufferSubData(GL_ARRAY_BUFFER, offset, size, bytes);
88     s_gles2.glBindBuffer(GL_ARRAY_BUFFER, 0);
89     */
90 }
91 
92 /*static*/
onLoad(android::base::Stream * stream,ContextHelper * helper)93 std::unique_ptr<BufferGl> BufferGl::onLoad(android::base::Stream* stream, ContextHelper* helper) {
94     const auto size = static_cast<uint64_t>(stream->getBe64());
95     const auto handle = static_cast<HandleType>(stream->getBe32());
96     return std::unique_ptr<BufferGl>(new BufferGl(size, handle, helper));
97 }
98 
onSave(android::base::Stream * stream)99 void BufferGl::onSave(android::base::Stream* stream) {
100     stream->putBe64(mSize);
101     stream->putBe32(mHandle);
102 }
103 
104 }  // namespace gl
105 }  // namespace gfxstream
106