1/*
2 * Copyright (C) 2023 Huawei Device Co., Ltd.
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
16#include "buffer/avbuffer_common.h"
17#include "surface_type.h" // foundation/graphic/graphic_2d/interfaces/surface/surface_type.h
18#include "log.h"
19
20namespace {
21constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { LOG_CORE, LOG_DOMAIN_FOUNDATION, "AVBufferCommon" };
22}
23
24namespace OHOS {
25namespace Media {
26AVBufferConfig::AVBufferConfig()
27{
28    this->surfaceBufferConfig = std::make_unique<BufferRequestConfig>();
29}
30
31AVBufferConfig::AVBufferConfig(const AVBufferConfig &rhs)
32{
33    FALSE_RETURN_W(&rhs != this);
34    this->surfaceBufferConfig = std::make_unique<BufferRequestConfig>();
35    *(this->surfaceBufferConfig) = *(rhs.surfaceBufferConfig);
36    this->size = rhs.size;
37    this->align = rhs.align;
38    this->dmaFd = rhs.dmaFd;
39    this->capacity = rhs.capacity;
40    this->memoryFlag = rhs.memoryFlag;
41    this->memoryType = rhs.memoryType;
42}
43
44AVBufferConfig::AVBufferConfig(AVBufferConfig &&rhs) noexcept
45{
46    this->surfaceBufferConfig = std::move(rhs.surfaceBufferConfig);
47    this->size = rhs.size;
48    this->align = rhs.align;
49    this->dmaFd = rhs.dmaFd;
50    this->capacity = rhs.capacity;
51    this->memoryFlag = rhs.memoryFlag;
52    this->memoryType = rhs.memoryType;
53}
54
55AVBufferConfig &AVBufferConfig::operator=(const AVBufferConfig &rhs)
56{
57    if (&rhs == this) {
58        return *this;
59    }
60    *(this->surfaceBufferConfig) = *(rhs.surfaceBufferConfig);
61    this->size = rhs.size;
62    this->align = rhs.align;
63    this->dmaFd = rhs.dmaFd;
64    this->capacity = rhs.capacity;
65    this->memoryFlag = rhs.memoryFlag;
66    this->memoryType = rhs.memoryType;
67    return *this;
68}
69
70AVBufferConfig &AVBufferConfig::operator=(AVBufferConfig &&rhs) noexcept
71{
72    FALSE_RETURN_V(&rhs != this, *this);
73    this->surfaceBufferConfig = std::move(rhs.surfaceBufferConfig);
74    this->size = rhs.size;
75    this->align = rhs.align;
76    this->dmaFd = rhs.dmaFd;
77    this->capacity = rhs.capacity;
78    this->memoryFlag = rhs.memoryFlag;
79    this->memoryType = rhs.memoryType;
80    return *this;
81}
82
83bool AVBufferConfig::operator<=(const struct AVBufferConfig &rhs) const
84{
85    FALSE_RETURN_V(memoryType == rhs.memoryType, false);
86    int32_t configAllocSize = rhs.align ? (rhs.capacity + rhs.align - 1) : rhs.capacity;
87    switch (memoryType) {
88        case MemoryType::VIRTUAL_MEMORY:
89            return size <= configAllocSize;
90        case MemoryType::SHARED_MEMORY:
91            return size <= configAllocSize &&
92                   (memoryFlag == rhs.memoryFlag || rhs.memoryFlag == MemoryFlag::MEMORY_READ_WRITE);
93        case MemoryType::HARDWARE_MEMORY:
94            return size <= configAllocSize &&
95                   (memoryFlag == rhs.memoryFlag || rhs.memoryFlag == MemoryFlag::MEMORY_READ_WRITE);
96        case MemoryType::SURFACE_MEMORY:
97            return (surfaceBufferConfig->width == rhs.surfaceBufferConfig->width) &&
98                   (surfaceBufferConfig->height == rhs.surfaceBufferConfig->height) &&
99                   (surfaceBufferConfig->strideAlignment == rhs.surfaceBufferConfig->strideAlignment) &&
100                   (surfaceBufferConfig->format == rhs.surfaceBufferConfig->format) &&
101                   (surfaceBufferConfig->usage == rhs.surfaceBufferConfig->usage) &&
102                   (surfaceBufferConfig->transform == rhs.surfaceBufferConfig->transform) &&
103                   (surfaceBufferConfig->colorGamut == rhs.surfaceBufferConfig->colorGamut); // ignore timeout
104        default:
105            return false;
106    }
107}
108
109} // namespace Media
110} // namespace OHOS