Corentin Wallez | 4a9ef4e | 2018-07-18 11:40:26 +0200 | [diff] [blame] | 1 | // Copyright 2017 The Dawn Authors |
Corentin Wallez | b38ff68 | 2017-05-30 18:06:38 -0400 | [diff] [blame] | 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 | |
Corentin Wallez | bdc8677 | 2018-07-26 15:07:57 +0200 | [diff] [blame] | 15 | #include "utils/TerribleCommandBuffer.h" |
Corentin Wallez | b38ff68 | 2017-05-30 18:06:38 -0400 | [diff] [blame] | 16 | |
Kai Ninomiya | 21006bb | 2018-06-20 18:54:18 -0700 | [diff] [blame] | 17 | #include "common/Assert.h" |
| 18 | |
Corentin Wallez | bdc8677 | 2018-07-26 15:07:57 +0200 | [diff] [blame] | 19 | namespace utils { |
Corentin Wallez | b38ff68 | 2017-05-30 18:06:38 -0400 | [diff] [blame] | 20 | |
| 21 | TerribleCommandBuffer::TerribleCommandBuffer() { |
| 22 | } |
| 23 | |
Corentin Wallez | bdc8677 | 2018-07-26 15:07:57 +0200 | [diff] [blame] | 24 | TerribleCommandBuffer::TerribleCommandBuffer(dawn_wire::CommandHandler* handler) |
| 25 | : mHandler(handler) { |
Corentin Wallez | b38ff68 | 2017-05-30 18:06:38 -0400 | [diff] [blame] | 26 | } |
| 27 | |
Corentin Wallez | bdc8677 | 2018-07-26 15:07:57 +0200 | [diff] [blame] | 28 | void TerribleCommandBuffer::SetHandler(dawn_wire::CommandHandler* handler) { |
Corentin Wallez | ad64704 | 2017-11-23 10:49:58 -0800 | [diff] [blame] | 29 | mHandler = handler; |
Corentin Wallez | b38ff68 | 2017-05-30 18:06:38 -0400 | [diff] [blame] | 30 | } |
| 31 | |
Austin Eng | cac0442 | 2020-10-13 22:35:34 +0000 | [diff] [blame] | 32 | size_t TerribleCommandBuffer::GetMaximumAllocationSize() const { |
| 33 | return sizeof(mBuffer); |
| 34 | } |
| 35 | |
Corentin Wallez | b38ff68 | 2017-05-30 18:06:38 -0400 | [diff] [blame] | 36 | void* TerribleCommandBuffer::GetCmdSpace(size_t size) { |
Austin Eng | ca41b00 | 2021-06-07 18:23:52 +0000 | [diff] [blame] | 37 | // Note: This returns non-null even if size is zero. |
Corentin Wallez | ad64704 | 2017-11-23 10:49:58 -0800 | [diff] [blame] | 38 | if (size > sizeof(mBuffer)) { |
Austin Eng | cac0442 | 2020-10-13 22:35:34 +0000 | [diff] [blame] | 39 | return nullptr; |
Corentin Wallez | b38ff68 | 2017-05-30 18:06:38 -0400 | [diff] [blame] | 40 | } |
Yan | b639e68 | 2019-11-15 09:32:56 +0000 | [diff] [blame] | 41 | char* result = &mBuffer[mOffset]; |
Yan | b639e68 | 2019-11-15 09:32:56 +0000 | [diff] [blame] | 42 | if (sizeof(mBuffer) - size < mOffset) { |
| 43 | if (!Flush()) { |
| 44 | return nullptr; |
| 45 | } |
| 46 | return GetCmdSpace(size); |
| 47 | } |
| 48 | |
| 49 | mOffset += size; |
Corentin Wallez | b38ff68 | 2017-05-30 18:06:38 -0400 | [diff] [blame] | 50 | return result; |
| 51 | } |
| 52 | |
Kai Ninomiya | 21006bb | 2018-06-20 18:54:18 -0700 | [diff] [blame] | 53 | bool TerribleCommandBuffer::Flush() { |
Austin Eng | cac0442 | 2020-10-13 22:35:34 +0000 | [diff] [blame] | 54 | bool success = mHandler->HandleCommands(mBuffer, mOffset) != nullptr; |
Corentin Wallez | ad64704 | 2017-11-23 10:49:58 -0800 | [diff] [blame] | 55 | mOffset = 0; |
Kai Ninomiya | 21006bb | 2018-06-20 18:54:18 -0700 | [diff] [blame] | 56 | return success; |
Corentin Wallez | b38ff68 | 2017-05-30 18:06:38 -0400 | [diff] [blame] | 57 | } |
| 58 | |
Corentin Wallez | bdc8677 | 2018-07-26 15:07:57 +0200 | [diff] [blame] | 59 | } // namespace utils |