1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef SERVICES_VIZ_PUBLIC_CPP_GPU_GPU_H_
6 #define SERVICES_VIZ_PUBLIC_CPP_GPU_GPU_H_
7 
8 #include <stdint.h>
9 #include <vector>
10 
11 #include "base/macros.h"
12 #include "base/memory/ref_counted.h"
13 #include "base/single_thread_task_runner.h"
14 #include "build/chromeos_buildflags.h"
15 #include "components/viz/common/gpu/context_provider.h"
16 #include "gpu/ipc/client/gpu_channel_host.h"
17 #include "mojo/public/cpp/bindings/pending_receiver.h"
18 #include "mojo/public/cpp/bindings/pending_remote.h"
19 #include "services/viz/public/cpp/gpu/client_gpu_memory_buffer_manager.h"
20 #include "services/viz/public/mojom/gpu.mojom.h"
21 
22 namespace service_manager {
23 class Connector;
24 }
25 
26 namespace viz {
27 
28 class Gpu : public gpu::GpuChannelEstablishFactory {
29  public:
30   // The Gpu has to be initialized in the main thread before establishing
31   // the gpu channel.
32   static std::unique_ptr<Gpu> Create(
33       service_manager::Connector* connector,
34       const std::string& service_name,
35       scoped_refptr<base::SingleThreadTaskRunner> io_task_runner);
36   static std::unique_ptr<Gpu> Create(
37       mojo::PendingRemote<mojom::Gpu> remote,
38       scoped_refptr<base::SingleThreadTaskRunner> io_task_runner);
39 
40   ~Gpu() override;
41 
gpu_memory_buffer_manager()42   gpu::GpuMemoryBufferManager* gpu_memory_buffer_manager() const {
43     return gpu_memory_buffer_manager_.get();
44   }
45 
46 #if BUILDFLAG(IS_ASH)
47   void CreateJpegDecodeAccelerator(
48       mojo::PendingReceiver<chromeos_camera::mojom::MjpegDecodeAccelerator>
49           jda_receiver);
50 #endif  // BUILDFLAG(IS_ASH)
51   void CreateVideoEncodeAcceleratorProvider(
52       mojo::PendingReceiver<media::mojom::VideoEncodeAcceleratorProvider>
53           vea_provider_receiver);
54 
55   // gpu::GpuChannelEstablishFactory:
56   void EstablishGpuChannel(
57       gpu::GpuChannelEstablishedCallback callback) override;
58   scoped_refptr<gpu::GpuChannelHost> EstablishGpuChannelSync() override;
59   gpu::GpuMemoryBufferManager* GetGpuMemoryBufferManager() override;
60 
61   void LoseChannel();
62   scoped_refptr<gpu::GpuChannelHost> GetGpuChannel();
63 
64  private:
65   friend class GpuTest;
66 
67   class GpuPtrIO;
68   class EstablishRequest;
69 
70   Gpu(mojo::PendingRemote<mojom::Gpu> gpu_remote,
71       scoped_refptr<base::SingleThreadTaskRunner> task_runner);
72 
73   // Sends a request to establish a gpu channel. If a request is currently
74   // pending this will do nothing.
75   void SendEstablishGpuChannelRequest();
76 
77   // Handles results of request to establish a gpu channel in
78   // |pending_request_|.
79   void OnEstablishedGpuChannel();
80 
81   scoped_refptr<base::SingleThreadTaskRunner> main_task_runner_;
82   scoped_refptr<base::SingleThreadTaskRunner> io_task_runner_;
83   std::unique_ptr<ClientGpuMemoryBufferManager> gpu_memory_buffer_manager_;
84 
85   std::unique_ptr<GpuPtrIO, base::OnTaskRunnerDeleter> gpu_;
86   scoped_refptr<EstablishRequest> pending_request_;
87   scoped_refptr<gpu::GpuChannelHost> gpu_channel_;
88   std::vector<gpu::GpuChannelEstablishedCallback> establish_callbacks_;
89 
90   DISALLOW_COPY_AND_ASSIGN(Gpu);
91 };
92 
93 }  // namespace viz
94 
95 #endif  // SERVICES_VIZ_PUBLIC_CPP_GPU_GPU_H_
96