• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2 * Copyright (C) 2011 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 #include <GLcommon/GLESbuffer.h>
17 #include <GLcommon/GLEScontext.h>
18 #include <string.h>
19 #include <limits.h>
20 
setBuffer(GLuint size,GLuint usage,const GLvoid * data)21 bool  GLESbuffer::setBuffer(GLuint size,GLuint usage,const GLvoid* data) {
22     m_size = size;
23     m_usage = usage;
24     if(m_data) {
25         delete [] m_data;
26         m_data = NULL;
27     }
28     m_data = new unsigned char[size];
29     if(m_data) {
30         if(data) {
31             memcpy(m_data,data,size);
32         }
33         m_conversionManager.clear();
34         m_conversionManager.addRange(Range(0,m_size));
35         return true;
36     }
37     return false;
38 }
39 
setSubBuffer(GLuint offset,GLuint size,const GLvoid * data)40 bool  GLESbuffer::setSubBuffer(GLuint offset, GLuint size, const GLvoid* data) {
41     if (UINT_MAX - offset < size) return false;
42     if(offset + size > m_size) return false;
43     memcpy(m_data+offset,data,size);
44     m_conversionManager.addRange(Range(offset,size));
45     m_conversionManager.merge();
46     return true;
47 }
48 
getConversions(const RangeList & rIn,RangeList & rOut)49 void  GLESbuffer::getConversions(const RangeList& rIn,RangeList& rOut) {
50         m_conversionManager.delRanges(rIn,rOut);
51         rOut.merge();
52 }
53 
~GLESbuffer()54 GLESbuffer::~GLESbuffer() {
55     if(m_data) {
56         delete [] m_data;
57     }
58 }
59 
GLESbuffer(android::base::Stream * stream)60 GLESbuffer::GLESbuffer(android::base::Stream* stream) : ObjectData(stream) {
61     m_size = stream->getBe32();
62     m_usage = stream->getBe32();
63     if (m_size) {
64         m_data = new unsigned char[m_size];
65         stream->read(m_data, m_size);
66         // TODO: m_conversionManager loading
67         m_conversionManager.addRange(Range(0,m_size));
68     }
69     m_wasBound = stream->getByte();
70 }
71 
onSave(android::base::Stream * stream,unsigned int globalName) const72 void GLESbuffer::onSave(android::base::Stream* stream,
73         unsigned int globalName) const {
74     ObjectData::onSave(stream, globalName);
75     stream->putBe32(m_size);
76     stream->putBe32(m_usage);
77     GLDispatch& dispatcher = GLEScontext::dispatcher();
78     bool mapSuccess = false;
79     if (!needRestore() && dispatcher.glMapBufferRange && m_size != 0) {
80         // If glMapBufferRange is supported, m_data might be inconsistent
81         // with GPU memory (because we did not update m_data when glUnmapBuffer)
82         // Thus we directly load buffer data from GPU memory
83 
84         // + We do not handle the situation when snapshot happens between
85         // a map and an unmap, because the way we implement GLES decoder
86         // prevents such behavior.
87         int prevBuffer = 0;
88         dispatcher.glGetIntegerv(GL_ARRAY_BUFFER_BINDING, &prevBuffer);
89         dispatcher.glBindBuffer(GL_ARRAY_BUFFER, globalName);
90         void * data = dispatcher.glMapBufferRange(GL_ARRAY_BUFFER, 0,
91                     m_size, GL_MAP_READ_BIT);
92         assert(data);
93         // BUG: 68051848
94         // It is supposed to be fixed, but for safety we keep the fallback path
95         // here.
96         if (data) {
97             stream->write(data, m_size);
98             bool success = dispatcher.glUnmapBuffer(GL_ARRAY_BUFFER);
99             assert(success);
100             (void)success;
101             mapSuccess = true;
102         }
103         dispatcher.glBindBuffer(GL_ARRAY_BUFFER, prevBuffer);
104     }
105     if (!mapSuccess) {
106         stream->write(m_data, m_size);
107     }
108 
109     // TODO: m_conversionManager
110     //
111     // Treat it as a low priority issue for now because in GLES2 this is only
112     // used for fix point vertex buffers. We are very unlikely to hit it
113     // when snapshotting home screen.
114     stream->putByte(m_wasBound);
115 }
116 
restore(ObjectLocalName localName,const getGlobalName_t & getGlobalName)117 void GLESbuffer::restore(ObjectLocalName localName,
118         const getGlobalName_t& getGlobalName) {
119     ObjectData::restore(localName, getGlobalName);
120     GLDispatch& dispatcher = GLEScontext::dispatcher();
121     int globalName = getGlobalName(NamedObjectType::VERTEXBUFFER, localName);
122     // We bind to GL_ARRAY_BUFFER just for uploading buffer data
123     dispatcher.glBindBuffer(GL_ARRAY_BUFFER, globalName);
124     dispatcher.glBufferData(GL_ARRAY_BUFFER, m_size, m_data, m_usage);
125 }
126