aboutsummaryrefslogtreecommitdiff
path: root/guest/hals/hwcomposer/common/gralloc_utils.cpp
blob: bb2350f21ec9b8c55555f001d0166941ab5708c0 (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
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
/*
 * Copyright (C) 2020 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "guest/hals/hwcomposer/common/gralloc_utils.h"

#include <aidl/android/hardware/graphics/common/PlaneLayoutComponent.h>
#include <aidl/android/hardware/graphics/common/PlaneLayoutComponentType.h>
#include <drm_fourcc.h>
#include <gralloctypes/Gralloc4.h>
#include <hidl/ServiceManagement.h>
#include <log/log.h>

// TODO(b/146515640): remove this.
#include "guest/hals/gralloc/legacy/gralloc_vsoc_priv.h"
#include "guest/hals/hwcomposer/common/drm_utils.h"

using aidl::android::hardware::graphics::common::PlaneLayout;
using aidl::android::hardware::graphics::common::PlaneLayoutComponent;
using aidl::android::hardware::graphics::common::PlaneLayoutComponentType;
using android::hardware::graphics::common::V1_2::BufferUsage;
using android::hardware::graphics::mapper::V4_0::Error;
using android::hardware::graphics::mapper::V4_0::IMapper;
using android::hardware::hidl_handle;
using android::hardware::hidl_vec;
using MetadataType =
  android::hardware::graphics::mapper::V4_0::IMapper::MetadataType;

// TODO(b/146515640): remove this.
using cuttlefish_gralloc0_buffer_handle_t = private_handle_t;

namespace cvd {

Gralloc::Gralloc() {
  android::hardware::preloadPassthroughService<IMapper>();

  gralloc4_ = IMapper::getService();
  if (gralloc4_ != nullptr) {
    ALOGE("%s using Gralloc4.", __FUNCTION__);
    return;
  }
  ALOGE("%s Gralloc4 not available.", __FUNCTION__);


  hw_get_module(GRALLOC_HARDWARE_MODULE_ID,
                reinterpret_cast<const hw_module_t**>(&gralloc0_));
  if (gralloc0_ != nullptr) {
    ALOGE("%s using Gralloc0.", __FUNCTION__);
    return;
  }
  ALOGE("%s Gralloc0 not available.", __FUNCTION__);

  ALOGE("%s No Grallocs available!", __FUNCTION__);
}

Error Gralloc::GetMetadata(buffer_handle_t buffer, MetadataType type,
                           hidl_vec<uint8_t>* metadata) {
  if (gralloc4_ == nullptr) {
    ALOGE("%s Gralloc4 not available.", __FUNCTION__);
    return Error::NO_RESOURCES;
  }

  if (metadata == nullptr) {
    return Error::BAD_VALUE;
  }

  Error error = Error::NONE;

  auto native_handle = const_cast<native_handle_t*>(buffer);

  auto ret = gralloc4_->get(native_handle, type,
                            [&](const auto& get_error, const auto& get_metadata) {
                              error = get_error;
                              *metadata = get_metadata;
                            });

  if (!ret.isOk()) {
    error = Error::NO_RESOURCES;
  }

  if (error != Error::NONE) {
    ALOGE("%s failed to get metadata %s", __FUNCTION__, type.name.c_str());
  }
  return error;
}

std::optional<uint32_t> Gralloc::GetWidth(buffer_handle_t buffer) {
  if (gralloc4_ != nullptr) {
    hidl_vec<uint8_t> encoded_width;

    Error error = GetMetadata(buffer, android::gralloc4::MetadataType_Width,
                              &encoded_width);
    if (error != Error::NONE) {
      return std::nullopt;
    }

    uint64_t width = 0;
    android::gralloc4::decodeWidth(encoded_width, &width);
    return static_cast<uint32_t>(width);
  }
  if (gralloc0_ != nullptr) {
    const cuttlefish_gralloc0_buffer_handle_t* gralloc0_buffer =
      reinterpret_cast<const cuttlefish_gralloc0_buffer_handle_t*>(buffer);

    return gralloc0_buffer->x_res;
  }
  return std::nullopt;
}

std::optional<uint32_t> Gralloc::GetHeight(buffer_handle_t buffer) {
  if (gralloc4_ != nullptr) {
    hidl_vec<uint8_t> encoded_height;

    Error error = GetMetadata(buffer, android::gralloc4::MetadataType_Height,
                              &encoded_height);
    if (error != Error::NONE) {
      return std::nullopt;
    }

    uint64_t height = 0;
    android::gralloc4::decodeHeight(encoded_height, &height);
    return static_cast<uint32_t>(height);
  }
  if (gralloc0_ != nullptr) {
    const cuttlefish_gralloc0_buffer_handle_t* gralloc0_buffer =
      reinterpret_cast<const cuttlefish_gralloc0_buffer_handle_t*>(buffer);

    return gralloc0_buffer->y_res;
  }
  return std::nullopt;
}

std::optional<uint32_t> Gralloc::GetDrmFormat(buffer_handle_t buffer) {
  if (gralloc4_ != nullptr) {
    hidl_vec<uint8_t> encoded_format;

    Error error = GetMetadata(buffer,
                              android::gralloc4::MetadataType_PixelFormatFourCC,
                              &encoded_format);
    if (error != Error::NONE) {
      return std::nullopt;
    }

    uint32_t format = 0;
    android::gralloc4::decodePixelFormatFourCC(encoded_format, &format);
    return static_cast<uint32_t>(format);
  }
  if (gralloc0_ != nullptr) {
    const cuttlefish_gralloc0_buffer_handle_t* gralloc0_buffer =
      reinterpret_cast<const cuttlefish_gralloc0_buffer_handle_t*>(buffer);

    return GetDrmFormatFromHalFormat(gralloc0_buffer->format);
  }
  return std::nullopt;
}

std::optional<std::vector<PlaneLayout>> Gralloc::GetPlaneLayouts(
    buffer_handle_t buffer) {
  if (gralloc4_ != nullptr) {
    hidl_vec<uint8_t> encoded_layouts;

    Error error = GetMetadata(buffer,
                              android::gralloc4::MetadataType_PlaneLayouts,
                              &encoded_layouts);
    if (error != Error::NONE) {
      return std::nullopt;
    }

    std::vector<PlaneLayout> plane_layouts;
    android::gralloc4::decodePlaneLayouts(encoded_layouts, &plane_layouts);
    return plane_layouts;
  }
  return std::nullopt;
}

std::optional<uint32_t> Gralloc::GetMonoPlanarStrideBytes(
    buffer_handle_t buffer) {
  if (gralloc4_ != nullptr) {
    auto plane_layouts_opt = GetPlaneLayouts(buffer);
    if (!plane_layouts_opt) {
      return std::nullopt;
    }

    std::vector<PlaneLayout>& plane_layouts = *plane_layouts_opt;
    if (plane_layouts.size() != 1) {
      return std::nullopt;
    }

    return static_cast<uint32_t>(plane_layouts[0].strideInBytes);
  }
  if (gralloc0_ != nullptr) {
    const cuttlefish_gralloc0_buffer_handle_t* gralloc0_buffer =
      reinterpret_cast<const cuttlefish_gralloc0_buffer_handle_t*>(buffer);

    int bytes_per_pixel = formatToBytesPerPixel(gralloc0_buffer->format);
    return gralloc0_buffer->stride_in_pixels * bytes_per_pixel;
  }
  return std::nullopt;
}

std::optional<GrallocBuffer> Gralloc::Import(buffer_handle_t buffer) {
  if (gralloc4_ != nullptr) {
    buffer_handle_t imported_buffer;

    Error error;
    auto ret = gralloc4_->importBuffer(buffer,
                                       [&](const auto& err, const auto& buf) {
                                         error = err;
                                         if (err == Error::NONE) {
                                           imported_buffer =
                                             static_cast<buffer_handle_t>(buf);
                                         }
                                        });

    if (!ret.isOk() || error != Error::NONE) {
      ALOGE("%s failed to import buffer", __FUNCTION__);
      return std::nullopt;
    }
    return GrallocBuffer(this, imported_buffer);
  }
  if (gralloc0_ != nullptr) {
    return GrallocBuffer(this, buffer);
  }
  return std::nullopt;
}

void Gralloc::Release(buffer_handle_t buffer) {
  if (gralloc4_ != nullptr) {
    auto native_buffer = const_cast<native_handle_t*>(buffer);
    auto ret = gralloc4_->freeBuffer(native_buffer);

    if (!ret.isOk()) {
      ALOGE("%s failed to release buffer", __FUNCTION__);
    }
    return;
  }
  if (gralloc0_) {
    // no-opt
  }
}

std::optional<void*> Gralloc::Lock(buffer_handle_t buffer) {
  if (gralloc4_ != nullptr) {
    auto native_buffer = const_cast<native_handle_t*>(buffer);

    const auto buffer_usage = static_cast<uint64_t>(BufferUsage::CPU_READ_OFTEN);

    auto width_opt = GetWidth(buffer);
    if (!width_opt) {
      return std::nullopt;
    }

    auto height_opt = GetHeight(buffer);
    if (!height_opt) {
      return std::nullopt;
    }

    IMapper::Rect buffer_region;
    buffer_region.left = 0;
    buffer_region.top = 0;
    buffer_region.width = *width_opt;
    buffer_region.height = *height_opt;

    // Empty fence, lock immedietly.
    hidl_handle fence;

    Error error = Error::NONE;
    void* data = nullptr;

    auto ret = gralloc4_->lock(native_buffer, buffer_usage, buffer_region,
                               fence,
                               [&](const auto& lock_error,
                                   const auto& lock_data) {
                                 error = lock_error;
                                 if (lock_error == Error::NONE) {
                                  data = lock_data;
                                }
                               });

    if (!ret.isOk()) {
      error = Error::NO_RESOURCES;
    }

    if (error != Error::NONE) {
      ALOGE("%s failed to lock buffer", __FUNCTION__);
      return std::nullopt;
    }

    return data;
  }
  if (gralloc0_ != nullptr) {
    const cuttlefish_gralloc0_buffer_handle_t* gralloc0_buffer =
      reinterpret_cast<const cuttlefish_gralloc0_buffer_handle_t*>(buffer);

    void* data = nullptr;
    int ret = gralloc0_->lock(gralloc0_,
                              gralloc0_buffer,
                              GRALLOC_USAGE_SW_READ_OFTEN,
                              0,
                              0,
                              gralloc0_buffer->x_res,
                              gralloc0_buffer->y_res,
                              &data);

    if (ret) {
      ALOGE("%s failed to lock buffer", __FUNCTION__);
      return std::nullopt;
    }
    return data;
  }
  return std::nullopt;
}

std::optional<android_ycbcr> Gralloc::LockYCbCr(buffer_handle_t buffer) {
  auto format_opt = GetDrmFormat(buffer);
  if (!format_opt) {
    ALOGE("%s failed to check format of buffer", __FUNCTION__);
    return std::nullopt;
  }

  if (*format_opt != DRM_FORMAT_NV12 &&
      *format_opt != DRM_FORMAT_NV21 &&
      *format_opt != DRM_FORMAT_YVU420) {
    ALOGE("%s called on non-ycbcr buffer", __FUNCTION__);
    return std::nullopt;
  }

  if (gralloc4_ != nullptr) {
    auto lock_opt = Lock(buffer);
    if (!lock_opt) {
      ALOGE("%s failed to lock buffer", __FUNCTION__);
      return std::nullopt;
    }

    auto plane_layouts_opt = GetPlaneLayouts(buffer);
    if (!plane_layouts_opt) {
      ALOGE("%s failed to get plane layouts", __FUNCTION__);
      return std::nullopt;
    }

    android_ycbcr buffer_ycbcr;
    buffer_ycbcr.y = nullptr;
    buffer_ycbcr.cb = nullptr;
    buffer_ycbcr.cr = nullptr;
    buffer_ycbcr.ystride = 0;
    buffer_ycbcr.cstride = 0;
    buffer_ycbcr.chroma_step = 0;

    for (const auto& plane_layout : *plane_layouts_opt) {
      for (const auto& plane_layout_component : plane_layout.components) {
        const auto& type = plane_layout_component.type;

        if (!android::gralloc4::isStandardPlaneLayoutComponentType(type)) {
          continue;
        }

        auto* component_data =
          reinterpret_cast<uint8_t*>(*lock_opt) +
          plane_layout.offsetInBytes +
          plane_layout_component.offsetInBits / 8;

        switch (static_cast<PlaneLayoutComponentType>(type.value)) {
          case PlaneLayoutComponentType::Y:
            buffer_ycbcr.y = component_data;
            buffer_ycbcr.ystride = plane_layout.strideInBytes;
            break;
          case PlaneLayoutComponentType::CB:
            buffer_ycbcr.cb = component_data;
            buffer_ycbcr.cstride = plane_layout.strideInBytes;
            buffer_ycbcr.chroma_step = plane_layout.sampleIncrementInBits / 8;
            break;
          case PlaneLayoutComponentType::CR:
            buffer_ycbcr.cr = component_data;
            buffer_ycbcr.cstride = plane_layout.strideInBytes;
            buffer_ycbcr.chroma_step = plane_layout.sampleIncrementInBits / 8;
            break;
          default:
            break;
        }
      }
    }

    return buffer_ycbcr;
  }
  if (gralloc0_ != nullptr) {
    auto lock_opt = Lock(buffer);
    if (!lock_opt) {
      ALOGE("%s failed to lock buffer", __FUNCTION__);
      return std::nullopt;
    }
    void* data = *lock_opt;

    const cuttlefish_gralloc0_buffer_handle_t* gralloc0_buffer =
      reinterpret_cast<const cuttlefish_gralloc0_buffer_handle_t*>(buffer);

    android_ycbcr buffer_ycbcr;
    formatToYcbcr(gralloc0_buffer->format,
                  gralloc0_buffer->x_res,
                  gralloc0_buffer->y_res,
                  data,
                  &buffer_ycbcr);
    return buffer_ycbcr;
  }
  return std::nullopt;
}

void Gralloc::Unlock(buffer_handle_t buffer) {
  if (gralloc4_ != nullptr) {
    auto native_handle = const_cast<native_handle_t*>(buffer);

    Error error = Error::NONE;
    auto ret = gralloc4_->unlock(native_handle,
                               [&](const auto& unlock_error, const auto&) {
                                 error = unlock_error;
                               });

    if (!ret.isOk()) {
      error = Error::NO_RESOURCES;
    }

    if (error != Error::NONE) {
      ALOGE("%s failed to unlock buffer", __FUNCTION__);
    }
    return;
  }
  if (gralloc0_ != nullptr) {
    const cuttlefish_gralloc0_buffer_handle_t* gralloc0_buffer =
      reinterpret_cast<const cuttlefish_gralloc0_buffer_handle_t*>(buffer);

    gralloc0_->unlock(gralloc0_, gralloc0_buffer);
    return;
  }
}


GrallocBuffer::GrallocBuffer(Gralloc* gralloc, buffer_handle_t buffer) :
  gralloc_(gralloc), buffer_(buffer) {}

GrallocBuffer::~GrallocBuffer() {  Release(); }

GrallocBuffer::GrallocBuffer(GrallocBuffer&& rhs) {
  *this = std::move(rhs);
}

GrallocBuffer& GrallocBuffer::operator=(GrallocBuffer&& rhs) {
  gralloc_ = rhs.gralloc_;
  buffer_ = rhs.buffer_;
  rhs.gralloc_ = nullptr;
  rhs.buffer_ = nullptr;
  return *this;
}

void GrallocBuffer::Release() {
  if (gralloc_ && buffer_) {
    gralloc_->Release(buffer_);
    gralloc_ = nullptr;
    buffer_ = nullptr;
  }
}

std::optional<void*> GrallocBuffer::Lock() {
  if (gralloc_ && buffer_) {
    return gralloc_->Lock(buffer_);
  }
  return std::nullopt;
}

 std::optional<android_ycbcr> GrallocBuffer::LockYCbCr() {
  if (gralloc_ && buffer_) {
    return gralloc_->LockYCbCr(buffer_);
  }
  return std::nullopt;
 }

void GrallocBuffer::Unlock() {
  if (gralloc_ && buffer_) {
    gralloc_->Unlock(buffer_);
  }
}

std::optional<uint32_t> GrallocBuffer::GetWidth() {
  if (gralloc_ && buffer_) {
    return gralloc_->GetWidth(buffer_);
  }
  return std::nullopt;
}

std::optional<uint32_t> GrallocBuffer::GetHeight() {
  if (gralloc_ && buffer_) {
    return gralloc_->GetHeight(buffer_);
  }
  return std::nullopt;
}

std::optional<uint32_t> GrallocBuffer::GetDrmFormat() {
  if (gralloc_ && buffer_) {
    return gralloc_->GetDrmFormat(buffer_);
  }
  return std::nullopt;
}

std::optional<std::vector<PlaneLayout>>
GrallocBuffer::GetPlaneLayouts() {
  if (gralloc_ && buffer_) {
    return gralloc_->GetPlaneLayouts(buffer_);
  }
  return std::nullopt;
}

std::optional<uint32_t> GrallocBuffer::GetMonoPlanarStrideBytes() {
  if (gralloc_ && buffer_) {
    return gralloc_->GetMonoPlanarStrideBytes(buffer_);
  }
  return std::nullopt;
}

}  // namespace cvd