1From ede4260fcee7a7b022604ef303285c128c078609 Mon Sep 17 00:00:00 2001
2From: Jeffy Chen <jeffy.chen@rock-chips.com>
3Date: Mon, 21 Mar 2022 18:30:22 +0800
4Subject: [PATCH 15/15] media/gpu/v4l2: Non-blocking initialize
5
6See:
7https://bugs.chromium.org/p/chromium/issues/detail?id=1308345
8
9Signed-off-by: Jeffy Chen <jeffy.chen@rock-chips.com>
10---
11 .../gpu/v4l2/v4l2_video_encode_accelerator.cc | 35 +++++++++----------
12 .../gpu/v4l2/v4l2_video_encode_accelerator.h  |  4 +--
13 2 files changed, 17 insertions(+), 22 deletions(-)
14
15diff --git a/media/gpu/v4l2/v4l2_video_encode_accelerator.cc b/media/gpu/v4l2/v4l2_video_encode_accelerator.cc
16index 0edf56412..488e05828 100644
17--- a/media/gpu/v4l2/v4l2_video_encode_accelerator.cc
18+++ b/media/gpu/v4l2/v4l2_video_encode_accelerator.cc
19@@ -272,25 +272,15 @@ bool V4L2VideoEncodeAccelerator::Initialize(
20     return false;
21   }
22
23-  bool result = false;
24-  base::WaitableEvent done;
25   encoder_task_runner_->PostTask(
26       FROM_HERE, base::BindOnce(&V4L2VideoEncodeAccelerator::InitializeTask,
27-                                weak_this_, config, &result, &done));
28-  done.Wait();
29-  return result;
30+                                weak_this_, config));
31+  return true;
32 }
33
34-void V4L2VideoEncodeAccelerator::InitializeTask(const Config& config,
35-                                                bool* result,
36-                                                base::WaitableEvent* done) {
37+void V4L2VideoEncodeAccelerator::InitializeTask(const Config& config) {
38   DCHECK_CALLED_ON_VALID_SEQUENCE(encoder_sequence_checker_);
39
40-  // Signal the event when leaving the method.
41-  base::ScopedClosureRunner signal_event(
42-      base::BindOnce(&base::WaitableEvent::Signal, base::Unretained(done)));
43-  *result = false;
44-
45   native_input_mode_ =
46       config.storage_type.value_or(Config::StorageType::kShmem) ==
47       Config::StorageType::kGpuMemoryBuffer;
48@@ -305,6 +295,7 @@ void V4L2VideoEncodeAccelerator::InitializeTask(const Config& config,
49
50   if (!SetFormats(config.input_format, config.output_profile)) {
51     VLOGF(1) << "Failed setting up formats";
52+    NOTIFY_ERROR(kPlatformFailureError);
53     return;
54   }
55
56@@ -318,6 +309,7 @@ void V4L2VideoEncodeAccelerator::InitializeTask(const Config& config,
57             VideoFrame::NumPlanes(config.input_format)));
58     if (!input_layout) {
59       VLOGF(1) << "Invalid image processor input layout";
60+      NOTIFY_ERROR(kPlatformFailureError);
61       return;
62     }
63
64@@ -327,6 +319,7 @@ void V4L2VideoEncodeAccelerator::InitializeTask(const Config& config,
65                               encoder_input_visible_rect_,
66                               encoder_input_visible_rect_)) {
67       VLOGF(1) << "Failed to create image processor";
68+      NOTIFY_ERROR(kPlatformFailureError);
69       return;
70     }
71
72@@ -338,16 +331,23 @@ void V4L2VideoEncodeAccelerator::InitializeTask(const Config& config,
73       VLOGF(1) << "Failed to reconfigure v4l2 encoder driver with the "
74                << "ImageProcessor output buffer: "
75                << ip_output_buffer_size.ToString();
76+      NOTIFY_ERROR(kPlatformFailureError);
77       return;
78     }
79   }
80
81-  if (!InitInputMemoryType(config))
82+  if (!InitInputMemoryType(config)) {
83+    NOTIFY_ERROR(kPlatformFailureError);
84     return;
85-  if (!InitControls(config))
86+  }
87+  if (!InitControls(config)) {
88+    NOTIFY_ERROR(kPlatformFailureError);
89     return;
90-  if (!CreateOutputBuffers())
91+  }
92+  if (!CreateOutputBuffers()) {
93+    NOTIFY_ERROR(kPlatformFailureError);
94     return;
95+  }
96
97   encoder_state_ = kInitialized;
98   RequestEncodingParametersChangeTask(
99@@ -380,9 +380,6 @@ void V4L2VideoEncodeAccelerator::InitializeTask(const Config& config,
100   child_task_runner_->PostTask(
101       FROM_HERE,
102       base::BindOnce(&Client::NotifyEncoderInfoChange, client_, encoder_info));
103-
104-  // Finish initialization.
105-  *result = true;
106 }
107
108 bool V4L2VideoEncodeAccelerator::CreateImageProcessor(
109diff --git a/media/gpu/v4l2/v4l2_video_encode_accelerator.h b/media/gpu/v4l2/v4l2_video_encode_accelerator.h
110index d414a327d..ce6cad5ab 100644
111--- a/media/gpu/v4l2/v4l2_video_encode_accelerator.h
112+++ b/media/gpu/v4l2/v4l2_video_encode_accelerator.h
113@@ -200,9 +200,7 @@ class MEDIA_GPU_EXPORT V4L2VideoEncodeAccelerator
114                                            uint32_t framerate);
115
116   // Do several initializations (e.g. set up format) on |encoder_task_runner_|.
117-  void InitializeTask(const Config& config,
118-                      bool* result,
119-                      base::WaitableEvent* done);
120+  void InitializeTask(const Config& config);
121
122   // Set up formats and initialize the device for them.
123   bool SetFormats(VideoPixelFormat input_format,
124--
1252.20.1
126
127