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 "recording/cmd_list.h"
17
18 #include <algorithm>
19
20 #ifdef SUPPORT_OHOS_PIXMAP
21 #include "pixel_map.h"
22 #endif
23 #include "utils/log.h"
24
25 namespace OHOS {
26 namespace Rosen {
27 namespace Drawing {
28 static constexpr uint32_t OPITEM_HEAD = 0;
29
CmdList(const CmdListData & cmdListData)30 CmdList::CmdList(const CmdListData& cmdListData)
31 {
32 opAllocator_.BuildFromDataWithCopy(cmdListData.first, cmdListData.second);
33 }
34
~CmdList()35 CmdList::~CmdList()
36 {
37 #ifdef SUPPORT_OHOS_PIXMAP
38 pixelMapVec_.clear();
39 #endif
40 }
41
AddCmdListData(const CmdListData & data)42 uint32_t CmdList::AddCmdListData(const CmdListData& data)
43 {
44 std::lock_guard<std::mutex> lock(mutex_);
45 if (!lastOpItemOffset_.has_value()) {
46 void* op = opAllocator_.Allocate<OpItem>(OPITEM_HEAD);
47 if (op == nullptr) {
48 LOGE("add OpItem head failed!");
49 return 0;
50 }
51 lastOpItemOffset_.emplace(opAllocator_.AddrToOffset(op));
52 }
53 void* addr = opAllocator_.Add(data.first, data.second);
54 if (addr == nullptr) {
55 LOGE("CmdList AddCmdListData failed!");
56 return 0;
57 }
58 return opAllocator_.AddrToOffset(addr);
59 }
60
GetCmdListData(uint32_t offset) const61 const void* CmdList::GetCmdListData(uint32_t offset) const
62 {
63 return opAllocator_.OffsetToAddr(offset);
64 }
65
GetData() const66 CmdListData CmdList::GetData() const
67 {
68 return std::make_pair(opAllocator_.GetData(), opAllocator_.GetSize());
69 }
70
SetUpImageData(const void * data,size_t size)71 bool CmdList::SetUpImageData(const void* data, size_t size)
72 {
73 return imageAllocator_.BuildFromDataWithCopy(data, size);
74 }
75
AddImageData(const void * data,size_t size)76 uint32_t CmdList::AddImageData(const void* data, size_t size)
77 {
78 std::lock_guard<std::mutex> lock(mutex_);
79 void* addr = imageAllocator_.Add(data, size);
80 if (addr == nullptr) {
81 LOGE("CmdList AddImageData failed!");
82 return 0;
83 }
84 return imageAllocator_.AddrToOffset(addr);
85 }
86
GetImageData(uint32_t offset) const87 const void* CmdList::GetImageData(uint32_t offset) const
88 {
89 return imageAllocator_.OffsetToAddr(offset);
90 }
91
GetAllImageData() const92 CmdListData CmdList::GetAllImageData() const
93 {
94 return std::make_pair(imageAllocator_.GetData(), imageAllocator_.GetSize());
95 }
96
AddPixelMap(const std::shared_ptr<Media::PixelMap> & pixelMap)97 uint32_t CmdList::AddPixelMap(const std::shared_ptr<Media::PixelMap>& pixelMap)
98 {
99 #ifdef SUPPORT_OHOS_PIXMAP
100 std::lock_guard<std::mutex> lock(pixelMapMutex_);
101 pixelMapVec_.emplace_back(pixelMap);
102 return static_cast<uint32_t>(pixelMapVec_.size()) - 1;
103 #else
104 return 0;
105 #endif
106 }
107
GetPixelMap(uint32_t id)108 std::shared_ptr<Media::PixelMap> CmdList::GetPixelMap(uint32_t id)
109 {
110 #ifdef SUPPORT_OHOS_PIXMAP
111 std::lock_guard<std::mutex> lock(pixelMapMutex_);
112 if (id >= pixelMapVec_.size()) {
113 return nullptr;
114 }
115 return pixelMapVec_[id];
116 #else
117 return nullptr;
118 #endif
119 }
120
GetAllPixelMap(std::vector<std::shared_ptr<Media::PixelMap>> & pixelMapList)121 uint32_t CmdList::GetAllPixelMap(std::vector<std::shared_ptr<Media::PixelMap>>& pixelMapList)
122 {
123 #ifdef SUPPORT_OHOS_PIXMAP
124 std::lock_guard<std::mutex> lock(pixelMapMutex_);
125 for (const auto &pixelMap : pixelMapVec_) {
126 pixelMapList.emplace_back(pixelMap);
127 }
128 return pixelMapList.size();
129 #else
130 return 0;
131 #endif
132 }
133
SetupPixelMap(const std::vector<std::shared_ptr<Media::PixelMap>> & pixelMapList)134 uint32_t CmdList::SetupPixelMap(const std::vector<std::shared_ptr<Media::PixelMap>>& pixelMapList)
135 {
136 #ifdef SUPPORT_OHOS_PIXMAP
137 std::lock_guard<std::mutex> lock(pixelMapMutex_);
138 for (const auto &pixelMap : pixelMapList) {
139 pixelMapVec_.emplace_back(pixelMap);
140 }
141 return pixelMapVec_.size();
142 #else
143 return 0;
144 #endif
145 }
146 } // namespace Drawing
147 } // namespace Rosen
148 } // namespace OHOS
149