• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 	if (!smpte2086.has_value())
105 	{
106 		return android::BAD_VALUE;
107 	}
108 
109 	auto *metadata = reinterpret_cast<shared_metadata *>(mali_gralloc_reference_get_metadata_addr(hnd).value());
110 	metadata->smpte2086 = aligned_optional(smpte2086);
111 
112 	return android::OK;
113 }
114 
get_cta861_3(const private_handle_t * hnd,std::optional<Cta861_3> * cta861_3)115 void get_cta861_3(const private_handle_t *hnd, std::optional<Cta861_3> *cta861_3)
116 {
117 	auto *metadata = reinterpret_cast<shared_metadata *>(mali_gralloc_reference_get_metadata_addr(hnd).value());
118 	*cta861_3 = metadata->cta861_3.to_std_optional();
119 }
120 
set_cta861_3(const private_handle_t * hnd,const std::optional<Cta861_3> & cta861_3)121 android::status_t set_cta861_3(const private_handle_t *hnd, const std::optional<Cta861_3> &cta861_3)
122 {
123 	if (!cta861_3.has_value())
124 	{
125 		return android::BAD_VALUE;
126 	}
127 
128 	auto *metadata = reinterpret_cast<shared_metadata *>(mali_gralloc_reference_get_metadata_addr(hnd).value());
129 	metadata->cta861_3 = aligned_optional(cta861_3);
130 
131 	return android::OK;
132 }
133 
get_smpte2094_40(const private_handle_t * hnd,std::optional<std::vector<uint8_t>> * smpte2094_40)134 void get_smpte2094_40(const private_handle_t *hnd, std::optional<std::vector<uint8_t>> *smpte2094_40)
135 {
136 	auto *metadata = reinterpret_cast<shared_metadata *>(mali_gralloc_reference_get_metadata_addr(hnd).value());
137 	if (metadata->smpte2094_40.size > 0)
138 	{
139 		const uint8_t *begin = metadata->smpte2094_40.data();
140 		const uint8_t *end = begin + metadata->smpte2094_40.size;
141 		smpte2094_40->emplace(begin, end);
142 	}
143 	else
144 	{
145 		smpte2094_40->reset();
146 	}
147 }
148 
set_smpte2094_40(const private_handle_t * hnd,const std::optional<std::vector<uint8_t>> & smpte2094_40)149 android::status_t set_smpte2094_40(const private_handle_t *hnd, const std::optional<std::vector<uint8_t>> &smpte2094_40)
150 {
151 	auto *metadata = reinterpret_cast<shared_metadata *>(mali_gralloc_reference_get_metadata_addr(hnd).value());
152 	const size_t size = smpte2094_40.has_value() ? smpte2094_40->size() : 0;
153 	if (size > metadata->smpte2094_40.capacity())
154 	{
155 		MALI_GRALLOC_LOGE("SMPTE 2094-40 metadata too large to fit in shared metadata region");
156 		return android::BAD_VALUE;
157 	}
158 
159 	metadata->smpte2094_40.size = size;
160 	std::memcpy(metadata->smpte2094_40.data(), smpte2094_40->data(), size);
161 
162 	return android::OK;
163 }
164 
get_video_hdr(const private_handle_t * hnd)165 void* get_video_hdr(const private_handle_t *hnd) {
166 	auto *metadata = reinterpret_cast<shared_metadata *>(mali_gralloc_reference_get_metadata_addr(hnd).value());
167 	return &(metadata->video_private_data);
168 }
169 
get_video_roiinfo(const private_handle_t * hnd)170 void* get_video_roiinfo(const private_handle_t *hnd) {
171 	if (!(hnd->get_usage() & GRALLOC_USAGE_ROIINFO))
172 		return nullptr;
173 
174 	auto *metadata = static_cast<char*>(mali_gralloc_reference_get_metadata_addr(hnd).value());
175 	return metadata + sizeof(shared_metadata) + hnd->reserved_region_size;
176 }
177 } // namespace common
178 } // namespace mapper
179 } // namespace arm
180