summaryrefslogtreecommitdiff
path: root/chromium/media/gpu/vaapi/vaapi_image_decoder.cc
blob: 91b26f9ebd27fb3b46754692a255d31ddd620e95 (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
// Copyright 2019 The Chromium 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 "media/gpu/vaapi/vaapi_image_decoder.h"

#include <utility>

#include "base/logging.h"
#include "media/gpu/macros.h"
#include "media/gpu/vaapi/va_surface.h"
#include "media/gpu/vaapi/vaapi_utils.h"
#include "media/gpu/vaapi/vaapi_wrapper.h"
#include "ui/gfx/geometry/size.h"
#include "ui/gfx/linux/native_pixmap_dmabuf.h"

namespace media {

void VAContextAndScopedVASurfaceDeleter::operator()(
    ScopedVASurface* scoped_va_surface) const {
  scoped_va_surface->vaapi_wrapper_->DestroyContext();
  delete scoped_va_surface;
}

VaapiImageDecoder::VaapiImageDecoder(VAProfile va_profile)
    : va_profile_(va_profile) {}

VaapiImageDecoder::~VaapiImageDecoder() = default;

bool VaapiImageDecoder::Initialize(const ReportErrorToUMACB& error_uma_cb) {
  vaapi_wrapper_ =
      VaapiWrapper::Create(VaapiWrapper::kDecode, va_profile_,
                           EncryptionScheme::kUnencrypted, error_uma_cb);
  return !!vaapi_wrapper_;
}

VaapiImageDecodeStatus VaapiImageDecoder::Decode(
    base::span<const uint8_t> encoded_image) {
  if (!vaapi_wrapper_) {
    VLOGF(1) << "VaapiImageDecoder has not been initialized";
    scoped_va_context_and_surface_.reset();
    return VaapiImageDecodeStatus::kInvalidState;
  }

  const VaapiImageDecodeStatus status =
      AllocateVASurfaceAndSubmitVABuffers(encoded_image);
  if (status != VaapiImageDecodeStatus::kSuccess) {
    scoped_va_context_and_surface_.reset();
    return status;
  }

  if (!vaapi_wrapper_->ExecuteAndDestroyPendingBuffers(
          scoped_va_context_and_surface_->id())) {
    VLOGF(1) << "ExecuteAndDestroyPendingBuffers() failed";
    scoped_va_context_and_surface_.reset();
    return VaapiImageDecodeStatus::kExecuteDecodeFailed;
  }
  return VaapiImageDecodeStatus::kSuccess;
}

const ScopedVASurface* VaapiImageDecoder::GetScopedVASurface() const {
  return scoped_va_context_and_surface_.get();
}

gpu::ImageDecodeAcceleratorSupportedProfile
VaapiImageDecoder::GetSupportedProfile() const {
  if (!vaapi_wrapper_) {
    DVLOGF(1) << "The VAAPI has not been initialized";
    return gpu::ImageDecodeAcceleratorSupportedProfile();
  }

  gpu::ImageDecodeAcceleratorSupportedProfile profile;
  profile.image_type = GetType();
  DCHECK_NE(gpu::ImageDecodeAcceleratorType::kUnknown, profile.image_type);

  // Note that since |vaapi_wrapper_| was created successfully, we expect the
  // following call to be successful. Hence the DCHECK.
  const bool got_supported_resolutions = VaapiWrapper::GetSupportedResolutions(
      va_profile_, VaapiWrapper::CodecMode::kDecode,
      profile.min_encoded_dimensions, profile.max_encoded_dimensions);
  DCHECK(got_supported_resolutions);

  // TODO(andrescj): Ideally, we would advertise support for all the formats
  // supported by the driver. However, for now, we will only support exposing
  // YUV 4:2:0 surfaces as DmaBufs.
  DCHECK(VaapiWrapper::GetDecodeSupportedInternalFormats(va_profile_).yuv420);
  profile.subsamplings.push_back(gpu::ImageDecodeAcceleratorSubsampling::k420);
  return profile;
}

std::unique_ptr<NativePixmapAndSizeInfo>
VaapiImageDecoder::ExportAsNativePixmapDmaBuf(VaapiImageDecodeStatus* status) {
  DCHECK(status);

  // We need to take ownership of the ScopedVASurface so that the next Decode()
  // doesn't attempt to use the same ScopedVASurface. Otherwise, it could
  // overwrite the result of the current decode before it's used by the caller.
  // This ScopedVASurface will self-clean at the end of this scope, but the
  // underlying buffer should stay alive because of the exported FDs.
  ScopedVAContextAndSurface temp_scoped_va_surface =
      std::move(scoped_va_context_and_surface_);

  if (!temp_scoped_va_surface) {
    DVLOGF(1) << "No decoded image available";
    *status = VaapiImageDecodeStatus::kInvalidState;
    return nullptr;
  }
  DCHECK(temp_scoped_va_surface->IsValid());

  std::unique_ptr<NativePixmapAndSizeInfo> exported_pixmap =
      vaapi_wrapper_->ExportVASurfaceAsNativePixmapDmaBuf(
          *temp_scoped_va_surface);
  if (!exported_pixmap) {
    *status = VaapiImageDecodeStatus::kCannotExportSurface;
    return nullptr;
  }

  DCHECK_EQ(temp_scoped_va_surface->size(),
            exported_pixmap->pixmap->GetBufferSize());
  *status = VaapiImageDecodeStatus::kSuccess;
  return exported_pixmap;
}

}  // namespace media