summaryrefslogtreecommitdiff
path: root/chromium/media/gpu/chromeos/image_processor_with_pool.cc
blob: 2c03db29ef3d9d1db38e1662dad636c51fd4d879 (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
// 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/chromeos/image_processor_with_pool.h"

#include "base/bind.h"
#include "base/memory/ptr_util.h"
#include "media/gpu/chromeos/gpu_buffer_layout.h"
#include "media/gpu/macros.h"

namespace media {

// static
std::unique_ptr<ImageProcessorWithPool> ImageProcessorWithPool::Create(
    std::unique_ptr<ImageProcessor> image_processor,
    DmabufVideoFramePool* const frame_pool,
    size_t num_frames,
    const scoped_refptr<base::SequencedTaskRunner> task_runner) {
  const ImageProcessor::PortConfig& config = image_processor->output_config();
  base::Optional<GpuBufferLayout> layout = frame_pool->Initialize(
      config.fourcc, config.size, config.visible_rect, config.size, num_frames,
      /*use_protected=*/false);
  if (!layout || layout->size() != config.size) {
    VLOGF(1) << "Failed to request frame with correct size. "
             << config.size.ToString() << " != "
             << (layout ? layout->size().ToString() : gfx::Size().ToString());
    return nullptr;
  }

  return base::WrapUnique<ImageProcessorWithPool>(new ImageProcessorWithPool(
      std::move(image_processor), frame_pool, std::move(task_runner)));
}

ImageProcessorWithPool::ImageProcessorWithPool(
    std::unique_ptr<ImageProcessor> image_processor,
    DmabufVideoFramePool* const frame_pool,
    const scoped_refptr<base::SequencedTaskRunner> task_runner)
    : image_processor_(std::move(image_processor)),
      frame_pool_(frame_pool),
      task_runner_(std::move(task_runner)) {
  DVLOGF(3);
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);

  weak_this_ = weak_this_factory_.GetWeakPtr();
}

ImageProcessorWithPool::~ImageProcessorWithPool() {
  DVLOGF(3);
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
}

void ImageProcessorWithPool::Reset() {
  DVLOGF(4);
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);

  image_processor_->Reset();
  pending_frames_ = {};
  num_frames_in_ip_ = 0;

  // Cancel pending tasks to avoid returning previous frames to the client.
  weak_this_factory_.InvalidateWeakPtrs();
  weak_this_ = weak_this_factory_.GetWeakPtr();
}

bool ImageProcessorWithPool::HasPendingFrames() const {
  DVLOGF(4);
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);

  return !pending_frames_.empty() || num_frames_in_ip_ > 0;
}

void ImageProcessorWithPool::Process(scoped_refptr<VideoFrame> frame,
                                     FrameReadyCB ready_cb) {
  DVLOGF(4);
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);

  pending_frames_.push(std::make_pair(std::move(frame), std::move(ready_cb)));
  PumpProcessFrames();
}

void ImageProcessorWithPool::PumpProcessFrames() {
  DVLOGF(4);
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);

  while (!pending_frames_.empty()) {
    scoped_refptr<VideoFrame> output_frame = frame_pool_->GetFrame();
    if (!output_frame) {
      // Notify when pool is available.
      frame_pool_->NotifyWhenFrameAvailable(base::BindOnce(
          base::IgnoreResult(&base::SequencedTaskRunner::PostTask),
          task_runner_, FROM_HERE,
          base::BindOnce(&ImageProcessorWithPool::PumpProcessFrames,
                         weak_this_)));
      break;
    }

    scoped_refptr<VideoFrame> input_frame =
        std::move(pending_frames_.front().first);
    FrameReadyCB ready_cb = std::move(pending_frames_.front().second);
    pending_frames_.pop();

    ++num_frames_in_ip_;
    image_processor_->Process(
        std::move(input_frame), std::move(output_frame),
        base::BindOnce(&ImageProcessorWithPool::OnFrameProcessed, weak_this_,
                       std::move(ready_cb)));
  }
}

void ImageProcessorWithPool::OnFrameProcessed(FrameReadyCB ready_cb,
                                              scoped_refptr<VideoFrame> frame) {
  DVLOGF(4);
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  DCHECK_NE(num_frames_in_ip_, 0u);

  --num_frames_in_ip_;
  std::move(ready_cb).Run(std::move(frame));
}

}  // namespace media