1 // Copyright 2017 The Dawn Authors 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 express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 #include "utils/TerribleCommandBuffer.h" 16 17 #include "common/Assert.h" 18 19 namespace utils { 20 TerribleCommandBuffer()21 TerribleCommandBuffer::TerribleCommandBuffer() { 22 } 23 TerribleCommandBuffer(dawn_wire::CommandHandler * handler)24 TerribleCommandBuffer::TerribleCommandBuffer(dawn_wire::CommandHandler* handler) 25 : mHandler(handler) { 26 } 27 SetHandler(dawn_wire::CommandHandler * handler)28 void TerribleCommandBuffer::SetHandler(dawn_wire::CommandHandler* handler) { 29 mHandler = handler; 30 } 31 GetMaximumAllocationSize() const32 size_t TerribleCommandBuffer::GetMaximumAllocationSize() const { 33 return sizeof(mBuffer); 34 } 35 GetCmdSpace(size_t size)36 void* TerribleCommandBuffer::GetCmdSpace(size_t size) { 37 // Note: This returns non-null even if size is zero. 38 if (size > sizeof(mBuffer)) { 39 return nullptr; 40 } 41 char* result = &mBuffer[mOffset]; 42 if (sizeof(mBuffer) - size < mOffset) { 43 if (!Flush()) { 44 return nullptr; 45 } 46 return GetCmdSpace(size); 47 } 48 49 mOffset += size; 50 return result; 51 } 52 Flush()53 bool TerribleCommandBuffer::Flush() { 54 bool success = mHandler->HandleCommands(mBuffer, mOffset) != nullptr; 55 mOffset = 0; 56 return success; 57 } 58 59 } // namespace utils 60