summaryrefslogtreecommitdiff
path: root/cros_gralloc/cros_gralloc_buffer.cc
blob: b2c4dc86f03e19ba98a08a5a291f76d38bb8643e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
/*
 * Copyright 2017 The Chromium OS Authors. All rights reserved.
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#include "cros_gralloc_buffer.h"

#include <assert.h>
#include <sys/mman.h>

#include <cutils/native_handle.h>

#include "cros_gralloc_buffer_metadata.h"

using aidl::android::hardware::graphics::common::BlendMode;
using aidl::android::hardware::graphics::common::Cta861_3;
using aidl::android::hardware::graphics::common::Dataspace;
using aidl::android::hardware::graphics::common::Smpte2086;

/*static*/
std::unique_ptr<cros_gralloc_buffer>
cros_gralloc_buffer::create(struct bo *acquire_bo,
			    const struct cros_gralloc_handle *borrowed_handle)
{
	auto acquire_hnd =
	    reinterpret_cast<struct cros_gralloc_handle *>(native_handle_clone(borrowed_handle));
	if (!acquire_hnd) {
		ALOGE("Failed to create cros_gralloc_buffer: failed to clone handle.");
		return {};
	}

	std::unique_ptr<cros_gralloc_buffer> buffer(
	    new cros_gralloc_buffer(acquire_bo, acquire_hnd));
	if (!buffer) {
		ALOGE("Failed to create cros_gralloc_buffer: failed to allocate.");
		native_handle_close(acquire_hnd);
		native_handle_delete(acquire_hnd);
		return {};
	}

	return buffer;
}

int32_t
cros_gralloc_buffer::initialize_metadata(const struct cros_gralloc_buffer_descriptor *descriptor)
{
	struct cros_gralloc_buffer_metadata *metadata;

	int ret = get_metadata(&metadata);
	if (ret) {
		ALOGE("Failed to initialize metadata: failed to get metadata region.");
		return ret;
	}

	if (metadata == nullptr) {
		ALOGE("Failed to initialize metadata: invalid metadata address.");
		return -1;
	}

	new (metadata) cros_gralloc_buffer_metadata();

	snprintf(metadata->name, CROS_GRALLOC_BUFFER_METADATA_MAX_NAME_SIZE, "%s",
		 descriptor->name.c_str());
	metadata->dataspace = descriptor->dataspace;
	metadata->blend_mode = descriptor->blend;
	return 0;
}

cros_gralloc_buffer::cros_gralloc_buffer(struct bo *acquire_bo,
					 struct cros_gralloc_handle *acquire_handle)
    : bo_(acquire_bo), hnd_(acquire_handle)
{
	assert(bo_);
	assert(hnd_);
	for (uint32_t plane = 0; plane < DRV_MAX_PLANES; plane++)
		lock_data_[plane] = nullptr;
}

cros_gralloc_buffer::~cros_gralloc_buffer()
{
	drv_bo_destroy(bo_);
	if (reserved_region_addr_) {
		munmap(reserved_region_addr_, hnd_->reserved_region_size);
	}
	native_handle_close(hnd_);
	native_handle_delete(hnd_);
}

uint32_t cros_gralloc_buffer::get_id() const
{
	return hnd_->id;
}

uint32_t cros_gralloc_buffer::get_width() const
{
	return hnd_->width;
}

uint32_t cros_gralloc_buffer::get_pixel_stride() const
{
	return hnd_->pixel_stride;
}

uint32_t cros_gralloc_buffer::get_height() const
{
	return hnd_->height;
}

uint32_t cros_gralloc_buffer::get_format() const
{
	return hnd_->format;
}

uint64_t cros_gralloc_buffer::get_format_modifier() const
{
	return hnd_->format_modifier;
}

uint64_t cros_gralloc_buffer::get_total_size() const
{
	return hnd_->total_size;
}

uint32_t cros_gralloc_buffer::get_num_planes() const
{
	return hnd_->num_planes;
}

uint32_t cros_gralloc_buffer::get_plane_offset(uint32_t plane) const
{
	return hnd_->offsets[plane];
}

uint32_t cros_gralloc_buffer::get_plane_stride(uint32_t plane) const
{
	return hnd_->strides[plane];
}

uint32_t cros_gralloc_buffer::get_plane_size(uint32_t plane) const
{
	return hnd_->sizes[plane];
}

int32_t cros_gralloc_buffer::get_android_format() const
{
	return hnd_->droid_format;
}

int64_t cros_gralloc_buffer::get_android_usage() const
{
	return hnd_->usage;
}

int32_t cros_gralloc_buffer::get_name(std::optional<std::string> *name) const
{
	const struct cros_gralloc_buffer_metadata *metadata;

	int ret = get_metadata(&metadata);
	if (ret) {
		ALOGE("Failed to get_name: failed to get metadata.");
		return ret;
	}

	*name = metadata->name;
	return 0;
}

int32_t cros_gralloc_buffer::get_blend_mode(std::optional<BlendMode> *blend_mode) const
{
	const struct cros_gralloc_buffer_metadata *metadata;

	int ret = get_metadata(&metadata);
	if (ret) {
		ALOGE("Failed to get_blend_mode: failed to get metadata.");
		return ret;
	}

	*blend_mode = metadata->blend_mode;
	return 0;
}

int32_t cros_gralloc_buffer::set_blend_mode(BlendMode blend_mode)
{
	struct cros_gralloc_buffer_metadata *metadata;

	int ret = get_metadata(&metadata);
	if (ret) {
		ALOGE("Failed to set_blend_mode: failed to get metadata.");
		return ret;
	}

	metadata->blend_mode = blend_mode;
	return 0;
}

int32_t cros_gralloc_buffer::get_dataspace(std::optional<Dataspace> *dataspace) const
{
	const struct cros_gralloc_buffer_metadata *metadata;

	int ret = get_metadata(&metadata);
	if (ret) {
		ALOGE("Failed to get_dataspace: failed to get metadata.");
		return ret;
	}

	*dataspace = metadata->dataspace;
	return 0;
}

int32_t cros_gralloc_buffer::set_dataspace(Dataspace dataspace)
{
	struct cros_gralloc_buffer_metadata *metadata;

	int ret = get_metadata(&metadata);
	if (ret) {
		ALOGE("Failed to set_dataspace: failed to get metadata.");
		return ret;
	}

	metadata->dataspace = dataspace;
	return 0;
}

int32_t cros_gralloc_buffer::get_cta861_3(std::optional<Cta861_3> *cta) const
{
	const struct cros_gralloc_buffer_metadata *metadata;

	int ret = get_metadata(&metadata);
	if (ret) {
		ALOGE("Failed to get_cta861_3: failed to get metadata.");
		return ret;
	}

	*cta = metadata->cta861_3.to_std_optional();
	return 0;
}

int32_t cros_gralloc_buffer::set_cta861_3(std::optional<Cta861_3> cta)
{
	struct cros_gralloc_buffer_metadata *metadata;

	int ret = get_metadata(&metadata);
	if (ret) {
		ALOGE("Failed to set_cta861_3: failed to get metadata.");
		return ret;
	}

	metadata->cta861_3 = cta;
	return 0;
}

int32_t cros_gralloc_buffer::get_smpte2086(std::optional<Smpte2086> *smpte) const
{
	const struct cros_gralloc_buffer_metadata *metadata;

	int ret = get_metadata(&metadata);
	if (ret) {
		ALOGE("Failed to get_smpte2086: failed to get metadata.");
		return ret;
	}

	*smpte = metadata->smpte2086.to_std_optional();
	return 0;
}

int32_t cros_gralloc_buffer::set_smpte2086(std::optional<Smpte2086> smpte)
{
	struct cros_gralloc_buffer_metadata *metadata;

	int ret = get_metadata(&metadata);
	if (ret) {
		ALOGE("Failed to set_cta861_3: failed to get metadata.");
		return ret;
	}

	metadata->smpte2086 = smpte;
	return 0;
}

int32_t cros_gralloc_buffer::increase_refcount()
{
	return ++refcount_;
}

int32_t cros_gralloc_buffer::decrease_refcount()
{
	assert(refcount_ > 0);
	return --refcount_;
}

int32_t cros_gralloc_buffer::lock(const struct rectangle *rect, uint32_t map_flags,
				  uint8_t *addr[DRV_MAX_PLANES])
{
	void *vaddr = nullptr;

	memset(addr, 0, DRV_MAX_PLANES * sizeof(*addr));

	if (map_flags) {
		if (lock_data_[0]) {
			drv_bo_invalidate(bo_, lock_data_[0]);
			vaddr = lock_data_[0]->vma->addr;
		} else {
			struct rectangle r = *rect;

			if (!r.width && !r.height && !r.x && !r.y) {
				/*
				 * Android IMapper.hal: An accessRegion of all-zeros means the
				 * entire buffer.
				 */
				r.width = drv_bo_get_width(bo_);
				r.height = drv_bo_get_height(bo_);
			}

			vaddr = drv_bo_map(bo_, &r, map_flags, &lock_data_[0], 0);
		}

		if (vaddr == MAP_FAILED) {
			ALOGE("Mapping failed.");
			return -EFAULT;
		}
	}

	for (uint32_t plane = 0; plane < hnd_->num_planes; plane++)
		addr[plane] = static_cast<uint8_t *>(vaddr) + drv_bo_get_plane_offset(bo_, plane);

	lockcount_++;
	return 0;
}

int32_t cros_gralloc_buffer::unlock()
{
	if (lockcount_ <= 0) {
		ALOGE("Buffer was not locked.");
		return -EINVAL;
	}

	if (!--lockcount_) {
		if (lock_data_[0]) {
			drv_bo_flush_or_unmap(bo_, lock_data_[0]);
			lock_data_[0] = nullptr;
		}
	}

	return 0;
}

int32_t cros_gralloc_buffer::resource_info(uint32_t strides[DRV_MAX_PLANES],
					   uint32_t offsets[DRV_MAX_PLANES],
					   uint64_t *format_modifier)
{
	return drv_resource_info(bo_, strides, offsets, format_modifier);
}

int32_t cros_gralloc_buffer::invalidate()
{
	if (lockcount_ <= 0) {
		ALOGE("Buffer was not locked.");
		return -EINVAL;
	}

	if (lock_data_[0])
		return drv_bo_invalidate(bo_, lock_data_[0]);

	return 0;
}

int32_t cros_gralloc_buffer::flush()
{
	if (lockcount_ <= 0) {
		ALOGE("Buffer was not locked.");
		return -EINVAL;
	}

	if (lock_data_[0])
		return drv_bo_flush(bo_, lock_data_[0]);

	return 0;
}

int32_t cros_gralloc_buffer::get_reserved_region(void **addr, uint64_t *size) const
{
	int32_t reserved_region_fd = hnd_->fds[hnd_->num_planes];
	if (reserved_region_fd < 0) {
		ALOGE("Buffer does not have reserved region.");
		return -EINVAL;
	}

	if (!reserved_region_addr_) {
		reserved_region_addr_ =
		    mmap(nullptr, hnd_->reserved_region_size, PROT_WRITE | PROT_READ, MAP_SHARED,
			 reserved_region_fd, 0);
		if (reserved_region_addr_ == MAP_FAILED) {
			ALOGE("Failed to mmap reserved region: %s.", strerror(errno));
			return -errno;
		}
	}

	*addr = reserved_region_addr_;
	*size = hnd_->reserved_region_size;
	return 0;
}

int32_t cros_gralloc_buffer::get_client_reserved_region(void **client_reserved_region_addr,
							uint64_t *client_reserved_region_size) const
{
	int32_t ret = get_reserved_region(client_reserved_region_addr, client_reserved_region_size);
	if (ret) {
		return ret;
	}

	*client_reserved_region_addr =
	    reinterpret_cast<void *>(reinterpret_cast<char *>(*client_reserved_region_addr) +
				     sizeof(struct cros_gralloc_buffer_metadata));
	*client_reserved_region_size =
	    *client_reserved_region_size - sizeof(struct cros_gralloc_buffer_metadata);
	return 0;
}

int32_t cros_gralloc_buffer::get_metadata(struct cros_gralloc_buffer_metadata **metadata)
{
	void *metadata_addr;
	uint64_t metadata_region_size;
	int32_t ret = get_reserved_region(&metadata_addr, &metadata_region_size);
	if (ret) {
		return ret;
	}

	if (metadata_addr == nullptr) {
		return -1;
	}

	*metadata = reinterpret_cast<struct cros_gralloc_buffer_metadata *>(metadata_addr);
	return 0;
}

int32_t
cros_gralloc_buffer::get_metadata(const struct cros_gralloc_buffer_metadata **metadata) const
{
	void *metadata_addr;
	uint64_t metadata_region_size;
	int32_t ret = get_reserved_region(&metadata_addr, &metadata_region_size);
	if (ret) {
		return ret;
	}

	if (metadata_addr == nullptr) {
		return -1;
	}

	*metadata = reinterpret_cast<const struct cros_gralloc_buffer_metadata *>(metadata_addr);
	return 0;
}