Avi Drissman | 4e1b7bc3 | 2022-09-15 14:03:50 | [diff] [blame] | 1 | // Copyright 2012 The Chromium Authors |
[email protected] | 5bba4dc2 | 2010-08-17 19:45:28 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | 20790a22 | 2013-07-25 02:23:05 | [diff] [blame] | 5 | #ifndef CONTENT_RENDERER_PEPPER_PPB_GRAPHICS_3D_IMPL_H_ |
| 6 | #define CONTENT_RENDERER_PEPPER_PPB_GRAPHICS_3D_IMPL_H_ |
[email protected] | 5bba4dc2 | 2010-08-17 19:45:28 | [diff] [blame] | 7 | |
avi | 1023d01 | 2015-12-25 02:39:14 | [diff] [blame] | 8 | #include <stdint.h> |
| 9 | |
mostynb | 6682b1c4 | 2016-04-19 10:17:30 | [diff] [blame] | 10 | #include <memory> |
| 11 | |
Vasiliy Telezhnikov | 2e6fab1 | 2022-12-01 17:08:04 | [diff] [blame] | 12 | #include "base/containers/flat_map.h" |
Alexandr Ilin | 15bb703 | 2018-07-13 10:09:06 | [diff] [blame] | 13 | #include "base/memory/unsafe_shared_memory_region.h" |
[email protected] | fa48a62b | 2011-10-07 21:45:32 | [diff] [blame] | 14 | #include "base/memory/weak_ptr.h" |
danakj | 1005777 | 2016-04-12 19:35:44 | [diff] [blame] | 15 | #include "gpu/command_buffer/client/gpu_control_client.h" |
lukasza | 2573ce7d | 2016-02-16 19:17:22 | [diff] [blame] | 16 | #include "gpu/command_buffer/common/command_buffer_id.h" |
[email protected] | 2206942 | 2014-05-02 04:29:00 | [diff] [blame] | 17 | #include "gpu/command_buffer/common/mailbox.h" |
dyen | ddfdbbb | 2016-01-14 22:21:38 | [diff] [blame] | 18 | #include "gpu/command_buffer/common/sync_token.h" |
[email protected] | 9a57839 | 2011-12-07 18:59:27 | [diff] [blame] | 19 | #include "ppapi/shared_impl/ppb_graphics_3d_shared.h" |
[email protected] | bbf076f1 | 2011-08-24 15:19:37 | [diff] [blame] | 20 | #include "ppapi/shared_impl/resource.h" |
[email protected] | 5bba4dc2 | 2010-08-17 19:45:28 | [diff] [blame] | 21 | |
piman | 360175c | 2014-11-07 02:30:01 | [diff] [blame] | 22 | namespace gpu { |
piman | 360175c | 2014-11-07 02:30:01 | [diff] [blame] | 23 | struct Capabilities; |
Saifuddin Hitawala | b2da4f0 | 2023-10-04 15:36:46 | [diff] [blame] | 24 | struct GLCapabilities; |
penghuang | 346a46f9 | 2016-03-31 21:37:52 | [diff] [blame] | 25 | class CommandBufferProxyImpl; |
Vasiliy Telezhnikov | 2e6fab1 | 2022-12-01 17:08:04 | [diff] [blame] | 26 | class ClientSharedImageInterface; |
piman | 360175c | 2014-11-07 02:30:01 | [diff] [blame] | 27 | } |
| 28 | |
[email protected] | adab233 | 2013-07-25 18:04:32 | [diff] [blame] | 29 | namespace content { |
[email protected] | 5bba4dc2 | 2010-08-17 19:45:28 | [diff] [blame] | 30 | |
danakj | 1005777 | 2016-04-12 19:35:44 | [diff] [blame] | 31 | class PPB_Graphics3D_Impl : public ppapi::PPB_Graphics3D_Shared, |
| 32 | public gpu::GpuControlClient { |
[email protected] | 5bba4dc2 | 2010-08-17 19:45:28 | [diff] [blame] | 33 | public: |
Alexandr Ilin | 15bb703 | 2018-07-13 10:09:06 | [diff] [blame] | 34 | static PP_Resource CreateRaw( |
| 35 | PP_Instance instance, |
| 36 | PP_Resource share_context, |
Vasiliy Telezhnikov | 2741f48 | 2023-05-29 17:23:26 | [diff] [blame] | 37 | const ppapi::Graphics3DContextAttribs& context_attribs, |
Alexandr Ilin | 15bb703 | 2018-07-13 10:09:06 | [diff] [blame] | 38 | gpu::Capabilities* capabilities, |
Saifuddin Hitawala | b2da4f0 | 2023-10-04 15:36:46 | [diff] [blame] | 39 | gpu::GLCapabilities* gl_capabilities, |
Alexandr Ilin | 15bb703 | 2018-07-13 10:09:06 | [diff] [blame] | 40 | const base::UnsafeSharedMemoryRegion** shared_state_region, |
| 41 | gpu::CommandBufferId* command_buffer_id); |
[email protected] | 6678946 | 2011-04-07 20:07:18 | [diff] [blame] | 42 | |
Peter Boström | 9b03653 | 2021-10-28 23:37:28 | [diff] [blame] | 43 | PPB_Graphics3D_Impl(const PPB_Graphics3D_Impl&) = delete; |
| 44 | PPB_Graphics3D_Impl& operator=(const PPB_Graphics3D_Impl&) = delete; |
| 45 | |
[email protected] | eeb4e4a | 2011-07-19 16:22:06 | [diff] [blame] | 46 | // PPB_Graphics3D_API trusted implementation. |
dcheng | 6d18e40 | 2014-10-21 12:32:52 | [diff] [blame] | 47 | PP_Bool SetGetBuffer(int32_t transfer_buffer_id) override; |
| 48 | scoped_refptr<gpu::Buffer> CreateTransferBuffer(uint32_t size, |
avi | 1023d01 | 2015-12-25 02:39:14 | [diff] [blame] | 49 | int32_t* id) override; |
dcheng | 6d18e40 | 2014-10-21 12:32:52 | [diff] [blame] | 50 | PP_Bool DestroyTransferBuffer(int32_t id) override; |
Yuzhu Shen | b152e7d4 | 2024-10-08 04:39:02 | [diff] [blame] | 51 | PP_Bool Flush(int32_t put_offset, uint64_t release_count) override; |
dcheng | 6d18e40 | 2014-10-21 12:32:52 | [diff] [blame] | 52 | gpu::CommandBuffer::State WaitForTokenInRange(int32_t start, |
| 53 | int32_t end) override; |
Antoine Labour | d346994 | 2017-05-16 21:23:42 | [diff] [blame] | 54 | gpu::CommandBuffer::State WaitForGetOffsetInRange( |
| 55 | uint32_t set_get_buffer_count, |
| 56 | int32_t start, |
| 57 | int32_t end) override; |
dyen | 4de3d345f | 2016-01-12 18:30:42 | [diff] [blame] | 58 | void EnsureWorkVisible() override; |
erikchen | 438b044 | 2016-05-11 18:33:30 | [diff] [blame] | 59 | void ReturnFrontBuffer(const gpu::Mailbox& mailbox, |
| 60 | const gpu::SyncToken& sync_token, |
| 61 | bool is_lost); |
Vasiliy Telezhnikov | 2e6fab1 | 2022-12-01 17:08:04 | [diff] [blame] | 62 | void ResolveAndDetachFramebuffer() override; |
| 63 | void DoResize(gfx::Size size) override; |
[email protected] | eeb4e4a | 2011-07-19 16:22:06 | [diff] [blame] | 64 | |
| 65 | // Binds/unbinds the graphics of this context with the associated instance. |
| 66 | // Returns true if binding/unbinding is successful. |
| 67 | bool BindToInstance(bool bind); |
| 68 | |
[email protected] | 3e4d8de | 2012-03-06 03:59:17 | [diff] [blame] | 69 | // Returns true if the backing texture is always opaque. |
| 70 | bool IsOpaque(); |
| 71 | |
[email protected] | 29ed96a | 2012-02-04 18:12:16 | [diff] [blame] | 72 | // Notifications about the view's progress painting. See PluginInstance. |
| 73 | // These messages are used to send Flush callbacks to the plugin. |
[email protected] | eeb4e4a | 2011-07-19 16:22:06 | [diff] [blame] | 74 | void ViewInitiatedPaint(); |
[email protected] | eeb4e4a | 2011-07-19 16:22:06 | [diff] [blame] | 75 | |
penghuang | 346a46f9 | 2016-03-31 21:37:52 | [diff] [blame] | 76 | gpu::CommandBufferProxyImpl* GetCommandBufferProxy(); |
[email protected] | 2206942 | 2014-05-02 04:29:00 | [diff] [blame] | 77 | |
[email protected] | eeb4e4a | 2011-07-19 16:22:06 | [diff] [blame] | 78 | protected: |
dcheng | 6d18e40 | 2014-10-21 12:32:52 | [diff] [blame] | 79 | ~PPB_Graphics3D_Impl() override; |
[email protected] | 9a57839 | 2011-12-07 18:59:27 | [diff] [blame] | 80 | // ppapi::PPB_Graphics3D_Shared overrides. |
dcheng | 6d18e40 | 2014-10-21 12:32:52 | [diff] [blame] | 81 | gpu::CommandBuffer* GetCommandBuffer() override; |
| 82 | gpu::GpuControl* GetGpuControl() override; |
erikchen | b13637b | 2016-07-08 09:38:56 | [diff] [blame] | 83 | int32_t DoSwapBuffers(const gpu::SyncToken& sync_token, |
piman | b36392c2 | 2016-07-13 02:11:36 | [diff] [blame] | 84 | const gfx::Size& size) override; |
[email protected] | e18e639 | 2011-06-22 20:57:00 | [diff] [blame] | 85 | |
| 86 | private: |
Vasiliy Telezhnikov | 2e6fab1 | 2022-12-01 17:08:04 | [diff] [blame] | 87 | class ColorBuffer; |
| 88 | |
[email protected] | bbf076f1 | 2011-08-24 15:19:37 | [diff] [blame] | 89 | explicit PPB_Graphics3D_Impl(PP_Instance instance); |
[email protected] | 6678946 | 2011-04-07 20:07:18 | [diff] [blame] | 90 | |
penghuang | a206fb49 | 2014-09-09 21:27:32 | [diff] [blame] | 91 | bool InitRaw(PPB_Graphics3D_API* share_context, |
Vasiliy Telezhnikov | 2741f48 | 2023-05-29 17:23:26 | [diff] [blame] | 92 | const ppapi::Graphics3DContextAttribs& requested_attribs, |
piman | 360175c | 2014-11-07 02:30:01 | [diff] [blame] | 93 | gpu::Capabilities* capabilities, |
Saifuddin Hitawala | b2da4f0 | 2023-10-04 15:36:46 | [diff] [blame] | 94 | gpu::GLCapabilities* gl_capabilities, |
Alexandr Ilin | 15bb703 | 2018-07-13 10:09:06 | [diff] [blame] | 95 | const base::UnsafeSharedMemoryRegion** shared_state_region, |
lukasza | 2573ce7d | 2016-02-16 19:17:22 | [diff] [blame] | 96 | gpu::CommandBufferId* command_buffer_id); |
[email protected] | eeb4e4a | 2011-07-19 16:22:06 | [diff] [blame] | 97 | |
danakj | 1005777 | 2016-04-12 19:35:44 | [diff] [blame] | 98 | // GpuControlClient implementation. |
| 99 | void OnGpuControlLostContext() final; |
danakj | 90fcc5e | 2016-04-29 04:21:00 | [diff] [blame] | 100 | void OnGpuControlLostContextMaybeReentrant() final; |
danakj | 1005777 | 2016-04-12 19:35:44 | [diff] [blame] | 101 | void OnGpuControlErrorMessage(const char* msg, int id) final; |
Austin Eng | 6ef198c5 | 2019-02-28 19:19:04 | [diff] [blame] | 102 | void OnGpuControlReturnData(base::span<const uint8_t> data) final; |
danakj | 1005777 | 2016-04-12 19:35:44 | [diff] [blame] | 103 | |
| 104 | // Other notifications from the GPU process. |
[email protected] | eeb4e4a | 2011-07-19 16:22:06 | [diff] [blame] | 105 | void OnSwapBuffers(); |
[email protected] | eeb4e4a | 2011-07-19 16:22:06 | [diff] [blame] | 106 | // Notifications sent to plugin. |
| 107 | void SendContextLost(); |
| 108 | |
Vasiliy Telezhnikov | 2e6fab1 | 2022-12-01 17:08:04 | [diff] [blame] | 109 | // This is called by NaCL process when it wants to present next frame |
| 110 | // (SwapBuffers call from the plugin). Note that |
| 111 | // `ResolveAndDetachFramebuffer()` must be called before and `sync_token` must |
| 112 | // be submitted after that call. |
| 113 | int32_t DoPresent(const gpu::SyncToken& sync_token, const gfx::Size& size); |
| 114 | |
| 115 | // Returns ColorBuffer for the next frame. It will try to re-use one of |
| 116 | // `available_color_buffers_` first and create new one if there is none. |
| 117 | std::unique_ptr<ColorBuffer> GetOrCreateColorBuffer(); |
| 118 | |
| 119 | // This returns ColorBuffer from the display compositor. If it's not lost and |
| 120 | // have the same size, it will be put in `available_color_buffers_` or |
| 121 | // Destroyed otherwise. |
| 122 | void RecycleColorBuffer(std::unique_ptr<ColorBuffer> buffer, |
| 123 | const gpu::SyncToken& sync_token, |
| 124 | bool is_lost); |
| 125 | |
| 126 | gfx::Size swapchain_size_; |
| 127 | std::vector<std::unique_ptr<ColorBuffer>> available_color_buffers_; |
| 128 | std::unique_ptr<ColorBuffer> current_color_buffer_; |
| 129 | base::flat_map<gpu::Mailbox, std::unique_ptr<ColorBuffer>> |
| 130 | inflight_color_buffers_; |
| 131 | |
[email protected] | eeb4e4a | 2011-07-19 16:22:06 | [diff] [blame] | 132 | // True if context is bound to instance. |
| 133 | bool bound_to_instance_; |
| 134 | // True when waiting for compositor to commit our backing texture. |
| 135 | bool commit_pending_; |
[email protected] | 2206942 | 2014-05-02 04:29:00 | [diff] [blame] | 136 | |
danakj | 1005777 | 2016-04-12 19:35:44 | [diff] [blame] | 137 | #if DCHECK_IS_ON() |
| 138 | bool lost_context_ = false; |
| 139 | #endif |
| 140 | |
Vasiliy Telezhnikov | 2e6fab1 | 2022-12-01 17:08:04 | [diff] [blame] | 141 | bool has_alpha_ = false; |
| 142 | bool is_single_buffered_ = false; |
| 143 | int samples_count_ = 0; |
| 144 | bool preserve_ = false; |
| 145 | bool needs_depth_ = false; |
| 146 | bool needs_stencil_ = false; |
| 147 | |
dcheng | cedca561 | 2016-04-09 01:40:15 | [diff] [blame] | 148 | std::unique_ptr<gpu::CommandBufferProxyImpl> command_buffer_; |
Mingjing Zhang | 506608b | 2024-01-23 03:10:00 | [diff] [blame] | 149 | scoped_refptr<gpu::ClientSharedImageInterface> shared_image_interface_; |
[email protected] | 2206942 | 2014-05-02 04:29:00 | [diff] [blame] | 150 | |
Jeremy Roman | 3bca4bf | 2019-07-11 03:41:25 | [diff] [blame] | 151 | base::WeakPtrFactory<PPB_Graphics3D_Impl> weak_ptr_factory_{this}; |
[email protected] | 5bba4dc2 | 2010-08-17 19:45:28 | [diff] [blame] | 152 | }; |
| 153 | |
[email protected] | adab233 | 2013-07-25 18:04:32 | [diff] [blame] | 154 | } // namespace content |
[email protected] | 5bba4dc2 | 2010-08-17 19:45:28 | [diff] [blame] | 155 | |
[email protected] | 20790a22 | 2013-07-25 02:23:05 | [diff] [blame] | 156 | #endif // CONTENT_RENDERER_PEPPER_PPB_GRAPHICS_3D_IMPL_H_ |