• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2 * Copyright (C) 2021 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 "CommandBufferStagingStream.h"
17 
18 #if PLATFORM_SDK_VERSION < 26
19 #include <cutils/log.h>
20 #else
21 #include <log/log.h>
22 #endif
23 #include <cutils/properties.h>
24 #include <errno.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <unistd.h>
28 #include <string.h>
29 
30 static const size_t kReadSize = 512 * 1024;
31 static const size_t kWriteOffset = kReadSize;
32 
CommandBufferStagingStream()33 CommandBufferStagingStream::CommandBufferStagingStream() :
34     IOStream(1048576), m_buf(nullptr), m_size(1048576), m_writePos(0) { }
35 
~CommandBufferStagingStream()36 CommandBufferStagingStream::~CommandBufferStagingStream() { flush(); if (m_buf) free(m_buf); }
37 
idealAllocSize(size_t len)38 size_t CommandBufferStagingStream::idealAllocSize(size_t len) {
39     if (len > 1048576) return len;
40     return 1048576;
41 }
42 
allocBuffer(size_t minSize)43 void *CommandBufferStagingStream::allocBuffer(size_t minSize) {
44     size_t allocSize =
45         (1048576 < minSize ? minSize : 1048576);
46 
47     // Initial case: blank
48     if (!m_buf) {
49         m_buf = (unsigned char *)malloc(allocSize);
50         m_size = allocSize;
51         return (void*)m_buf;
52     }
53 
54     // Calculate remaining
55     size_t remaining = m_size - m_writePos;
56 
57     if (remaining < allocSize) {
58         size_t newAllocSize = m_size * 2 + allocSize;
59         unsigned char *p = (unsigned char *)realloc(m_buf, newAllocSize);
60         m_buf = p;
61         m_size = newAllocSize;
62         return (void*)(m_buf + m_writePos);
63 
64     }
65 
66     return (void*)(m_buf + m_writePos);
67 }
68 
commitBuffer(size_t size)69 int CommandBufferStagingStream::commitBuffer(size_t size)
70 {
71     m_writePos += size;
72     return 0;
73 }
74 
readFully(void *,size_t)75 const unsigned char *CommandBufferStagingStream::readFully(void*, size_t) {
76     // Not supported
77     ALOGE("CommandBufferStagingStream::%s: Fatal: not supported\n", __func__);
78     abort();
79     return nullptr;
80 }
81 
read(void *,size_t *)82 const unsigned char *CommandBufferStagingStream::read(void*, size_t*) {
83     // Not supported
84     ALOGE("CommandBufferStagingStream::%s: Fatal: not supported\n", __func__);
85     abort();
86     return nullptr;
87 }
88 
writeFully(const void *,size_t)89 int CommandBufferStagingStream::writeFully(const void*, size_t)
90 {
91     // Not supported
92     ALOGE("CommandBufferStagingStream::%s: Fatal: not supported\n", __func__);
93     abort();
94     return 0;
95 }
96 
commitBufferAndReadFully(size_t,void *,size_t)97 const unsigned char *CommandBufferStagingStream::commitBufferAndReadFully(
98     size_t, void *, size_t) {
99 
100     // Not supported
101     ALOGE("CommandBufferStagingStream::%s: Fatal: not supported\n", __func__);
102     abort();
103     return nullptr;
104 }
105 
getWritten(unsigned char ** bufOut,size_t * sizeOut)106 void CommandBufferStagingStream::getWritten(unsigned char** bufOut, size_t* sizeOut) {
107     *bufOut = m_buf;
108     *sizeOut = m_writePos;
109 }
110 
reset()111 void CommandBufferStagingStream::reset() {
112     m_writePos = 0;
113     IOStream::rewind();
114 }
115