1 /*
2  * Copyright (C) 2019 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "TransformFeedbackData.h"
18 
19 #include "GLcommon/GLEScontext.h"
20 #include "GLcommon/GLSnapshotSerializers.h"
21 
TransformFeedbackData(android::base::Stream * stream)22 TransformFeedbackData::TransformFeedbackData(android::base::Stream* stream)
23     : ObjectData(stream) {
24     if (stream) {
25         loadContainer(stream, m_indexedTransformFeedbackBuffers);
26     }
27 }
28 
setMaxSize(int maxSize)29 void TransformFeedbackData::setMaxSize(int maxSize) {
30     m_indexedTransformFeedbackBuffers.resize(maxSize);
31 }
32 
onSave(android::base::Stream * stream,unsigned int globalName) const33 void TransformFeedbackData::onSave(android::base::Stream* stream,
34                                    unsigned int globalName) const {
35     ObjectData::onSave(stream, globalName);
36     saveContainer(stream, m_indexedTransformFeedbackBuffers);
37 }
38 
restore(ObjectLocalName localName,const getGlobalName_t & getGlobalName)39 void TransformFeedbackData::restore(ObjectLocalName localName,
40                                     const getGlobalName_t& getGlobalName) {
41     ObjectData::restore(localName, getGlobalName);
42     int globalName =
43             getGlobalName(NamedObjectType::TRANSFORM_FEEDBACK, localName);
44     GLDispatch& dispatcher = GLEScontext::dispatcher();
45     dispatcher.glBindTransformFeedback(GL_TRANSFORM_FEEDBACK, globalName);
46     for (unsigned int i = 0; i < m_indexedTransformFeedbackBuffers.size();
47          i++) {
48         auto& bufferBinding = m_indexedTransformFeedbackBuffers[i];
49         if (!bufferBinding.buffer)
50             continue;
51         auto bufferName = getGlobalName(NamedObjectType::VERTEXBUFFER,
52                                         bufferBinding.buffer);
53         if (bufferBinding.isBindBase) {
54             dispatcher.glBindBufferBase(GL_TRANSFORM_FEEDBACK, i, bufferName);
55         } else {
56             dispatcher.glBindBufferRange(GL_TRANSFORM_FEEDBACK, i, bufferName,
57                                          bufferBinding.offset,
58                                          bufferBinding.size);
59         }
60     }
61 }
62 
bindIndexedBuffer(GLuint index,GLuint buffer,GLintptr offset,GLsizeiptr size,GLintptr stride,bool isBindBase)63 void TransformFeedbackData::bindIndexedBuffer(GLuint index,
64                                               GLuint buffer,
65                                               GLintptr offset,
66                                               GLsizeiptr size,
67                                               GLintptr stride,
68                                               bool isBindBase) {
69     if (index >= m_indexedTransformFeedbackBuffers.size()) {
70         return;
71     }
72     auto& bufferBinding = m_indexedTransformFeedbackBuffers[index];
73     bufferBinding.buffer = buffer;
74     bufferBinding.offset = offset;
75     bufferBinding.size = size;
76     bufferBinding.stride = stride;
77     bufferBinding.isBindBase = isBindBase;
78 }
79 
unbindBuffer(GLuint buffer)80 void TransformFeedbackData::unbindBuffer(GLuint buffer) {
81     for (auto& bufferBinding : m_indexedTransformFeedbackBuffers) {
82         if (bufferBinding.buffer == buffer) {
83             bufferBinding = {};
84         }
85     }
86 }
87 
getIndexedBuffer(GLuint index)88 GLuint TransformFeedbackData::getIndexedBuffer(GLuint index) {
89     return m_indexedTransformFeedbackBuffers[index].buffer;
90 }
91