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 "core/mali_gralloc_reference.h"
21 #include "mali_gralloc_log.h"
22 #include "mali_gralloc_usages.h"
23 //#include <VendorVideoAPI.h>
24 
25 namespace arm
26 {
27 namespace mapper
28 {
29 namespace common
30 {
31 
shared_metadata_init(void * memory,std::string_view name)32 void shared_metadata_init(void *memory, std::string_view name)
33 {
34 	new(memory) shared_metadata(name);
35 }
36 
shared_metadata_size()37 size_t shared_metadata_size()
38 {
39 	return sizeof(shared_metadata);
40 }
41 
get_name(const private_handle_t * hnd,std::string * name)42 void get_name(const private_handle_t *hnd, std::string *name)
43 {
44 	auto *metadata = reinterpret_cast<shared_metadata *>(mali_gralloc_reference_get_metadata_addr(hnd).value());
45 	*name = metadata->get_name();
46 }
47 
get_crop_rect(const private_handle_t * hnd,std::optional<Rect> * crop)48 void get_crop_rect(const private_handle_t *hnd, std::optional<Rect> *crop)
49 {
50 	auto *metadata = reinterpret_cast<shared_metadata *>(mali_gralloc_reference_get_metadata_addr(hnd).value());
51 	*crop = metadata->crop.to_std_optional();
52 }
53 
set_crop_rect(const private_handle_t * hnd,const Rect & crop)54 android::status_t set_crop_rect(const private_handle_t *hnd, const Rect &crop)
55 {
56 	auto *metadata = reinterpret_cast<shared_metadata *>(mali_gralloc_reference_get_metadata_addr(hnd).value());
57 
58 	if (crop.top < 0 || crop.left < 0 ||
59 	    crop.left > crop.right || crop.right > hnd->plane_info[0].alloc_width ||
60 	    crop.top > crop.bottom || crop.bottom > hnd->plane_info[0].alloc_height ||
61 	    (crop.right - crop.left) != hnd->width ||
62 	    (crop.bottom - crop.top) != hnd->height)
63 	{
64 		MALI_GRALLOC_LOGE("Attempt to set invalid crop rectangle");
65 		return android::BAD_VALUE;
66 	}
67 
68 	metadata->crop = aligned_optional(crop);
69 	return android::OK;
70 }
71 
get_dataspace(const private_handle_t * hnd,std::optional<Dataspace> * dataspace)72 void get_dataspace(const private_handle_t *hnd, std::optional<Dataspace> *dataspace)
73 {
74 	auto *metadata = reinterpret_cast<shared_metadata *>(mali_gralloc_reference_get_metadata_addr(hnd).value());
75 	*dataspace = metadata->dataspace.to_std_optional();
76 }
77 
set_dataspace(const private_handle_t * hnd,const Dataspace & dataspace)78 void set_dataspace(const private_handle_t *hnd, const Dataspace &dataspace)
79 {
80 	auto *metadata = reinterpret_cast<shared_metadata *>(mali_gralloc_reference_get_metadata_addr(hnd).value());
81 	metadata->dataspace = aligned_optional(dataspace);
82 }
83 
get_blend_mode(const private_handle_t * hnd,std::optional<BlendMode> * blend_mode)84 void get_blend_mode(const private_handle_t *hnd, std::optional<BlendMode> *blend_mode)
85 {
86 	auto *metadata = reinterpret_cast<shared_metadata *>(mali_gralloc_reference_get_metadata_addr(hnd).value());
87 	*blend_mode = metadata->blend_mode.to_std_optional();
88 }
89 
set_blend_mode(const private_handle_t * hnd,const BlendMode & blend_mode)90 void set_blend_mode(const private_handle_t *hnd, const BlendMode &blend_mode)
91 {
92 	auto *metadata = reinterpret_cast<shared_metadata *>(mali_gralloc_reference_get_metadata_addr(hnd).value());
93 	metadata->blend_mode = aligned_optional(blend_mode);
94 }
95 
get_smpte2086(const private_handle_t * hnd,std::optional<Smpte2086> * smpte2086)96 void get_smpte2086(const private_handle_t *hnd, std::optional<Smpte2086> *smpte2086)
97 {
98 	auto *metadata = reinterpret_cast<shared_metadata *>(mali_gralloc_reference_get_metadata_addr(hnd).value());
99 	*smpte2086 = metadata->smpte2086.to_std_optional();
100 }
101 
set_smpte2086(const private_handle_t * hnd,const std::optional<Smpte2086> & smpte2086)102 android::status_t set_smpte2086(const private_handle_t *hnd, const std::optional<Smpte2086> &smpte2086)
103 {
104 	auto *metadata = reinterpret_cast<shared_metadata *>(mali_gralloc_reference_get_metadata_addr(hnd).value());
105 	metadata->smpte2086 = aligned_optional(smpte2086);
106 
107 	return android::OK;
108 }
109 
get_cta861_3(const private_handle_t * hnd,std::optional<Cta861_3> * cta861_3)110 void get_cta861_3(const private_handle_t *hnd, std::optional<Cta861_3> *cta861_3)
111 {
112 	auto *metadata = reinterpret_cast<shared_metadata *>(mali_gralloc_reference_get_metadata_addr(hnd).value());
113 	*cta861_3 = metadata->cta861_3.to_std_optional();
114 }
115 
set_cta861_3(const private_handle_t * hnd,const std::optional<Cta861_3> & cta861_3)116 android::status_t set_cta861_3(const private_handle_t *hnd, const std::optional<Cta861_3> &cta861_3)
117 {
118 	auto *metadata = reinterpret_cast<shared_metadata *>(mali_gralloc_reference_get_metadata_addr(hnd).value());
119 	metadata->cta861_3 = aligned_optional(cta861_3);
120 
121 	return android::OK;
122 }
123 
get_smpte2094_40(const private_handle_t * hnd,std::optional<std::vector<uint8_t>> * smpte2094_40)124 void get_smpte2094_40(const private_handle_t *hnd, std::optional<std::vector<uint8_t>> *smpte2094_40)
125 {
126 	auto *metadata = reinterpret_cast<shared_metadata *>(mali_gralloc_reference_get_metadata_addr(hnd).value());
127 	if (metadata->smpte2094_40.size > 0)
128 	{
129 		const uint8_t *begin = metadata->smpte2094_40.data();
130 		const uint8_t *end = begin + metadata->smpte2094_40.size;
131 		smpte2094_40->emplace(begin, end);
132 	}
133 	else
134 	{
135 		smpte2094_40->reset();
136 	}
137 }
138 
set_smpte2094_40(const private_handle_t * hnd,const std::optional<std::vector<uint8_t>> & smpte2094_40)139 android::status_t set_smpte2094_40(const private_handle_t *hnd, const std::optional<std::vector<uint8_t>> &smpte2094_40)
140 {
141 	auto *metadata = reinterpret_cast<shared_metadata *>(mali_gralloc_reference_get_metadata_addr(hnd).value());
142 	const size_t size = smpte2094_40.has_value() ? smpte2094_40->size() : 0;
143 	if (size > metadata->smpte2094_40.capacity())
144 	{
145 		MALI_GRALLOC_LOGE("SMPTE 2094-40 metadata too large to fit in shared metadata region");
146 		return android::BAD_VALUE;
147 	}
148 
149 	metadata->smpte2094_40.size = size;
150 	std::memcpy(metadata->smpte2094_40.data(), smpte2094_40->data(), size);
151 
152 	return android::OK;
153 }
154 
get_video_hdr(const private_handle_t * hnd)155 void* get_video_hdr(const private_handle_t *hnd) {
156 	auto *metadata = reinterpret_cast<shared_metadata *>(mali_gralloc_reference_get_metadata_addr(hnd).value());
157 	return &(metadata->video_private_data);
158 }
159 
get_video_roiinfo(const private_handle_t * hnd)160 void* get_video_roiinfo(const private_handle_t *hnd) {
161 	if (!(hnd->get_usage() & GRALLOC_USAGE_ROIINFO))
162 		return nullptr;
163 
164 	auto *metadata = static_cast<char*>(mali_gralloc_reference_get_metadata_addr(hnd).value());
165 	return metadata + sizeof(shared_metadata) + hnd->reserved_region_size;
166 }
167 
get_video_gmv(const private_handle_t * hnd)168 VideoGMV get_video_gmv(const private_handle_t *hnd) {
169 	auto *metadata = reinterpret_cast<shared_metadata *>(mali_gralloc_reference_get_metadata_addr(hnd).value());
170 	return metadata->video_gmv_data;
171 }
172 
set_video_gmv(const private_handle_t * hnd,const VideoGMV & data)173 android::status_t set_video_gmv(const private_handle_t *hnd, const VideoGMV &data) {
174 	auto *metadata = reinterpret_cast<shared_metadata *>(mali_gralloc_reference_get_metadata_addr(hnd).value());
175 	metadata->video_gmv_data = data;
176 	return android::OK;
177 }
178 
179 } // namespace common
180 } // namespace mapper
181 } // namespace arm
182