1 /*
2  * Copyright (C) 2020 Arm Limited.
3  *
4  * Copyright 2016 The Android Open Source Project
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18 
19 #include "SharedMetadata.h"
20 #include "mali_gralloc_log.h"
21 //#include <VendorVideoAPI.h>
22 
23 namespace arm
24 {
25 namespace mapper
26 {
27 namespace common
28 {
29 
shared_metadata_init(void * memory,std::string_view name)30 void shared_metadata_init(void *memory, std::string_view name)
31 {
32 	new(memory) shared_metadata(name);
33 }
34 
shared_metadata_size()35 size_t shared_metadata_size()
36 {
37 	return sizeof(shared_metadata);
38 }
39 
get_name(const private_handle_t * hnd,std::string * name)40 void get_name(const private_handle_t *hnd, std::string *name)
41 {
42 	auto *metadata = reinterpret_cast<const shared_metadata *>(hnd->attr_base);
43 	*name = metadata->get_name();
44 }
45 
get_crop_rect(const private_handle_t * hnd,std::optional<Rect> * crop)46 void get_crop_rect(const private_handle_t *hnd, std::optional<Rect> *crop)
47 {
48 	auto *metadata = reinterpret_cast<const shared_metadata *>(hnd->attr_base);
49 	*crop = metadata->crop.to_std_optional();
50 }
51 
set_crop_rect(const private_handle_t * hnd,const Rect & crop)52 android::status_t set_crop_rect(const private_handle_t *hnd, const Rect &crop)
53 {
54 	auto *metadata = reinterpret_cast<shared_metadata *>(hnd->attr_base);
55 
56 	if (crop.top < 0 || crop.left < 0 ||
57 	    crop.left > crop.right || crop.right > hnd->plane_info[0].alloc_width ||
58 	    crop.top > crop.bottom || crop.bottom > hnd->plane_info[0].alloc_height ||
59 	    (crop.right - crop.left) != hnd->width ||
60 	    (crop.bottom - crop.top) != hnd->height)
61 	{
62 		MALI_GRALLOC_LOGE("Attempt to set invalid crop rectangle");
63 		return android::BAD_VALUE;
64 	}
65 
66 	metadata->crop = aligned_optional(crop);
67 	return android::OK;
68 }
69 
get_dataspace(const private_handle_t * hnd,std::optional<Dataspace> * dataspace)70 void get_dataspace(const private_handle_t *hnd, std::optional<Dataspace> *dataspace)
71 {
72 	auto *metadata = reinterpret_cast<const shared_metadata *>(hnd->attr_base);
73 	*dataspace = metadata->dataspace.to_std_optional();
74 }
75 
set_dataspace(const private_handle_t * hnd,const Dataspace & dataspace)76 void set_dataspace(const private_handle_t *hnd, const Dataspace &dataspace)
77 {
78 	auto *metadata = reinterpret_cast<shared_metadata *>(hnd->attr_base);
79 	metadata->dataspace = aligned_optional(dataspace);
80 }
81 
get_blend_mode(const private_handle_t * hnd,std::optional<BlendMode> * blend_mode)82 void get_blend_mode(const private_handle_t *hnd, std::optional<BlendMode> *blend_mode)
83 {
84 	auto *metadata = reinterpret_cast<const shared_metadata *>(hnd->attr_base);
85 	*blend_mode = metadata->blend_mode.to_std_optional();
86 }
87 
set_blend_mode(const private_handle_t * hnd,const BlendMode & blend_mode)88 void set_blend_mode(const private_handle_t *hnd, const BlendMode &blend_mode)
89 {
90 	auto *metadata = reinterpret_cast<shared_metadata *>(hnd->attr_base);
91 	metadata->blend_mode = aligned_optional(blend_mode);
92 }
93 
get_smpte2086(const private_handle_t * hnd,std::optional<Smpte2086> * smpte2086)94 void get_smpte2086(const private_handle_t *hnd, std::optional<Smpte2086> *smpte2086)
95 {
96 	auto *metadata = reinterpret_cast<const shared_metadata *>(hnd->attr_base);
97 	*smpte2086 = metadata->smpte2086.to_std_optional();
98 }
99 
set_smpte2086(const private_handle_t * hnd,const std::optional<Smpte2086> & smpte2086)100 android::status_t set_smpte2086(const private_handle_t *hnd, const std::optional<Smpte2086> &smpte2086)
101 {
102 	if (!smpte2086.has_value())
103 	{
104 		return android::BAD_VALUE;
105 	}
106 
107 	auto *metadata = reinterpret_cast<shared_metadata *>(hnd->attr_base);
108 	metadata->smpte2086 = aligned_optional(smpte2086);
109 
110 	return android::OK;
111 }
112 
get_cta861_3(const private_handle_t * hnd,std::optional<Cta861_3> * cta861_3)113 void get_cta861_3(const private_handle_t *hnd, std::optional<Cta861_3> *cta861_3)
114 {
115 	auto *metadata = reinterpret_cast<const shared_metadata *>(hnd->attr_base);
116 	*cta861_3 = metadata->cta861_3.to_std_optional();
117 }
118 
set_cta861_3(const private_handle_t * hnd,const std::optional<Cta861_3> & cta861_3)119 android::status_t set_cta861_3(const private_handle_t *hnd, const std::optional<Cta861_3> &cta861_3)
120 {
121 	if (!cta861_3.has_value())
122 	{
123 		return android::BAD_VALUE;
124 	}
125 
126 	auto *metadata = reinterpret_cast<shared_metadata *>(hnd->attr_base);
127 	metadata->cta861_3 = aligned_optional(cta861_3);
128 
129 	return android::OK;
130 }
131 
get_smpte2094_40(const private_handle_t * hnd,std::optional<std::vector<uint8_t>> * smpte2094_40)132 void get_smpte2094_40(const private_handle_t *hnd, std::optional<std::vector<uint8_t>> *smpte2094_40)
133 {
134 	auto *metadata = reinterpret_cast<const shared_metadata *>(hnd->attr_base);
135 	if (metadata->smpte2094_40.size > 0)
136 	{
137 		const uint8_t *begin = metadata->smpte2094_40.data();
138 		const uint8_t *end = begin + metadata->smpte2094_40.size;
139 		smpte2094_40->emplace(begin, end);
140 	}
141 	else
142 	{
143 		smpte2094_40->reset();
144 	}
145 }
146 
set_smpte2094_40(const private_handle_t * hnd,const std::optional<std::vector<uint8_t>> & smpte2094_40)147 android::status_t set_smpte2094_40(const private_handle_t *hnd, const std::optional<std::vector<uint8_t>> &smpte2094_40)
148 {
149 	if (!smpte2094_40.has_value() || smpte2094_40->size() == 0)
150 	{
151 		MALI_GRALLOC_LOGE("Empty SMPTE 2094-40 data");
152 		return android::BAD_VALUE;
153 	}
154 
155 	auto *metadata = reinterpret_cast<shared_metadata *>(hnd->attr_base);
156 	const size_t size = smpte2094_40.has_value() ? smpte2094_40->size() : 0;
157 	if (size > metadata->smpte2094_40.capacity())
158 	{
159 		MALI_GRALLOC_LOGE("SMPTE 2094-40 metadata too large to fit in shared metadata region");
160 		return android::BAD_VALUE;
161 	}
162 
163 	metadata->smpte2094_40.size = size;
164 	std::memcpy(metadata->smpte2094_40.data(), smpte2094_40->data(), size);
165 
166 	return android::OK;
167 }
168 
169 } // namespace common
170 } // namespace mapper
171 } // namespace arm
172