blob: 0c06ab1036480e2635f50b8cbbd041ec39e8a1d7 [file] [log] [blame]
Corentin Wallez4a9ef4e2018-07-18 11:40:26 +02001// Copyright 2017 The Dawn Authors
Corentin Wallezb38ff682017-05-30 18:06:38 -04002//
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 Wallezbdc86772018-07-26 15:07:57 +020015#include "utils/TerribleCommandBuffer.h"
Corentin Wallezb38ff682017-05-30 18:06:38 -040016
Kai Ninomiya21006bb2018-06-20 18:54:18 -070017#include "common/Assert.h"
18
Corentin Wallezbdc86772018-07-26 15:07:57 +020019namespace utils {
Corentin Wallezb38ff682017-05-30 18:06:38 -040020
21 TerribleCommandBuffer::TerribleCommandBuffer() {
22 }
23
Corentin Wallezbdc86772018-07-26 15:07:57 +020024 TerribleCommandBuffer::TerribleCommandBuffer(dawn_wire::CommandHandler* handler)
25 : mHandler(handler) {
Corentin Wallezb38ff682017-05-30 18:06:38 -040026 }
27
Corentin Wallezbdc86772018-07-26 15:07:57 +020028 void TerribleCommandBuffer::SetHandler(dawn_wire::CommandHandler* handler) {
Corentin Wallezad647042017-11-23 10:49:58 -080029 mHandler = handler;
Corentin Wallezb38ff682017-05-30 18:06:38 -040030 }
31
Austin Engcac04422020-10-13 22:35:34 +000032 size_t TerribleCommandBuffer::GetMaximumAllocationSize() const {
33 return sizeof(mBuffer);
34 }
35
Corentin Wallezb38ff682017-05-30 18:06:38 -040036 void* TerribleCommandBuffer::GetCmdSpace(size_t size) {
Austin Engca41b002021-06-07 18:23:52 +000037 // Note: This returns non-null even if size is zero.
Corentin Wallezad647042017-11-23 10:49:58 -080038 if (size > sizeof(mBuffer)) {
Austin Engcac04422020-10-13 22:35:34 +000039 return nullptr;
Corentin Wallezb38ff682017-05-30 18:06:38 -040040 }
Yanb639e682019-11-15 09:32:56 +000041 char* result = &mBuffer[mOffset];
Yanb639e682019-11-15 09:32:56 +000042 if (sizeof(mBuffer) - size < mOffset) {
43 if (!Flush()) {
44 return nullptr;
45 }
46 return GetCmdSpace(size);
47 }
48
49 mOffset += size;
Corentin Wallezb38ff682017-05-30 18:06:38 -040050 return result;
51 }
52
Kai Ninomiya21006bb2018-06-20 18:54:18 -070053 bool TerribleCommandBuffer::Flush() {
Austin Engcac04422020-10-13 22:35:34 +000054 bool success = mHandler->HandleCommands(mBuffer, mOffset) != nullptr;
Corentin Wallezad647042017-11-23 10:49:58 -080055 mOffset = 0;
Kai Ninomiya21006bb2018-06-20 18:54:18 -070056 return success;
Corentin Wallezb38ff682017-05-30 18:06:38 -040057 }
58
Corentin Wallezbdc86772018-07-26 15:07:57 +020059} // namespace utils