summaryrefslogtreecommitdiff
path: root/chromium/media/gpu/vaapi/vaapi_picture_tfp.cc
blob: 87c9b918e65331c102fbd68f762e6fef71730568 (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
// Copyright 2014 The Chromium Authors
// 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_picture_tfp.h"

#include "media/gpu/vaapi/va_surface.h"
#include "media/gpu/vaapi/vaapi_status.h"
#include "media/gpu/vaapi/vaapi_wrapper.h"
#include "ui/gfx/x/connection.h"
#include "ui/gfx/x/future.h"
#include "ui/gl/gl_bindings.h"
#include "ui/gl/gl_image_glx.h"
#include "ui/gl/scoped_binders.h"

namespace media {

VaapiTFPPicture::VaapiTFPPicture(
    scoped_refptr<VaapiWrapper> vaapi_wrapper,
    const MakeGLContextCurrentCallback& make_context_current_cb,
    const BindGLImageCallback& bind_image_cb,
    int32_t picture_buffer_id,
    const gfx::Size& size,
    const gfx::Size& visible_size,
    uint32_t texture_id,
    uint32_t client_texture_id,
    uint32_t texture_target)
    : VaapiPicture(std::move(vaapi_wrapper),
                   make_context_current_cb,
                   bind_image_cb,
                   picture_buffer_id,
                   size,
                   visible_size,
                   texture_id,
                   client_texture_id,
                   texture_target),
      connection_(x11::Connection::Get()),
      x_pixmap_(x11::Pixmap::None) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  DCHECK(texture_id);
  DCHECK(client_texture_id);
}

VaapiTFPPicture::~VaapiTFPPicture() {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  if (glx_image_.get() && make_context_current_cb_.Run()) {
    glx_image_->ReleaseTexImage(texture_target_);
    DCHECK_EQ(glGetError(), static_cast<GLenum>(GL_NO_ERROR));
  }

  if (x_pixmap_ != x11::Pixmap::None)
    connection_->FreePixmap({x_pixmap_});
}

VaapiStatus VaapiTFPPicture::Initialize() {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  DCHECK_NE(x_pixmap_, x11::Pixmap::None);

  if (make_context_current_cb_ && !make_context_current_cb_.Run())
    return VaapiStatus::Codes::kBadContext;

  glx_image_ = new gl::GLImageGLX(size_, gfx::BufferFormat::BGRX_8888);
  if (!glx_image_->Initialize(x_pixmap_)) {
    // x_pixmap_ will be freed in the destructor.
    DLOG(ERROR) << "Failed creating a GLX Pixmap for TFP";
    return VaapiStatus::Codes::kFailedToInitializeImage;
  }

  gl::ScopedTextureBinder texture_binder(texture_target_, texture_id_);
  if (!glx_image_->BindTexImage(texture_target_)) {
    DLOG(ERROR) << "Failed to bind texture to glx image";
    return VaapiStatus::Codes::kFailedToBindTexture;
  }

  return VaapiStatus::Codes::kOk;
}

VaapiStatus VaapiTFPPicture::Allocate(gfx::BufferFormat format) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  if (format != gfx::BufferFormat::BGRX_8888 &&
      format != gfx::BufferFormat::BGRA_8888 &&
      format != gfx::BufferFormat::RGBX_8888) {
    DLOG(ERROR) << "Unsupported format";
    return VaapiStatus::Codes::kUnsupportedFormat;
  }

  if (!connection_->Ready())
    return VaapiStatus::Codes::kNoPixmap;

  auto root = connection_->default_root();

  uint8_t depth = 0;
  if (auto reply = connection_->GetGeometry(root).Sync())
    depth = reply->depth;
  else
    return VaapiStatus::Codes::kNoPixmap;

  // TODO(posciak): pass the depth required by libva, not the RootWindow's
  // depth
  auto pixmap = connection_->GenerateId<x11::Pixmap>();
  uint16_t pixmap_width, pixmap_height;
  if (!base::CheckedNumeric<int>(size_.width()).AssignIfValid(&pixmap_width) ||
      !base::CheckedNumeric<int>(size_.height())
           .AssignIfValid(&pixmap_height)) {
    return VaapiStatus::Codes::kNoPixmap;
  }
  auto req = connection_->CreatePixmap(
      {depth, pixmap, root, pixmap_width, pixmap_height});
  if (req.Sync().error) {
    DLOG(ERROR) << "Failed creating an X Pixmap for TFP";
    return VaapiStatus::Codes::kNoPixmap;
  } else {
    x_pixmap_ = pixmap;
  }

  return Initialize();
}

bool VaapiTFPPicture::ImportGpuMemoryBufferHandle(
    gfx::BufferFormat format,
    gfx::GpuMemoryBufferHandle gpu_memory_buffer_handle) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  NOTIMPLEMENTED() << "GpuMemoryBufferHandle import not implemented";
  return false;
}

bool VaapiTFPPicture::DownloadFromSurface(scoped_refptr<VASurface> va_surface) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  return vaapi_wrapper_->PutSurfaceIntoPixmap(va_surface->id(), x_pixmap_,
                                              va_surface->size());
}

}  // namespace media