1 /*
2  * Copyright (C) 2020 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 
17 #include "host/frontend/webrtc/cvd_video_frame_buffer.h"
18 
19 #include <map>
20 #include <mutex>
21 #include <vector>
22 #include "common/libs/utils/size_utils.h"
23 
24 namespace cuttlefish {
25 
26 namespace {
27 constexpr int kPlanePadding = 1024;
28 constexpr int kLogAlignment = 6;  // multiple of 2^6
29 
AlignStride(int width)30 inline int AlignStride(int width) {
31   return AlignToPowerOf2(width, kLogAlignment);
32 }
33 
34 std::multimap<int, std::vector<uint8_t>> pool;
35 std::mutex pool_mutex;
FromPool(int size)36 std::vector<uint8_t> FromPool(int size) {
37   {
38     std::lock_guard<std::mutex> lock(pool_mutex);
39     auto it = pool.find(size);
40     if (it != pool.end()) {
41       auto ret = std::move(it->second);
42       pool.erase(it);
43       return ret;
44     }
45   }
46   return std::vector<uint8_t>(size);
47 }
48 
BackToPool(std::vector<uint8_t> item)49 void BackToPool(std::vector<uint8_t> item) {
50   std::lock_guard<std::mutex> lock(pool_mutex);
51   pool.insert({item.size(), std::move(item)});
52 }
53 
54 }  // namespace
55 
CvdVideoFrameBuffer(int width,int height)56 CvdVideoFrameBuffer::CvdVideoFrameBuffer(int width, int height)
57     : width_(width),
58       height_(height),
59       y_(FromPool(AlignStride(width) * height + kPlanePadding)),
60       u_(FromPool(AlignStride((width + 1) / 2) * ((height + 1) / 2) +
61                   kPlanePadding)),
62       v_(FromPool(AlignStride((width + 1) / 2) * ((height + 1) / 2) +
63                   kPlanePadding)) {}
64 
~CvdVideoFrameBuffer()65 CvdVideoFrameBuffer::~CvdVideoFrameBuffer() {
66   BackToPool(std::move(y_));
67   BackToPool(std::move(u_));
68   BackToPool(std::move(v_));
69 }
70 
width() const71 int CvdVideoFrameBuffer::width() const { return width_; }
height() const72 int CvdVideoFrameBuffer::height() const { return height_; }
73 
StrideY() const74 int CvdVideoFrameBuffer::StrideY() const { return AlignStride(width_); }
StrideU() const75 int CvdVideoFrameBuffer::StrideU() const {
76   return AlignStride((width_ + 1) / 2);
77 }
StrideV() const78 int CvdVideoFrameBuffer::StrideV() const {
79   return AlignStride((width_ + 1) / 2);
80 }
81 
DataY() const82 const uint8_t *CvdVideoFrameBuffer::DataY() const { return y_.data(); }
DataU() const83 const uint8_t *CvdVideoFrameBuffer::DataU() const { return u_.data(); }
DataV() const84 const uint8_t *CvdVideoFrameBuffer::DataV() const { return v_.data(); }
85 
86 }  // namespace cuttlefish
87