Avi Drissman | 05dfbc82 | 2022-09-13 21:25:34 | [diff] [blame] | 1 | // Copyright 2018 The Chromium Authors |
liberato | 2744a14 | 2017-03-07 19:50:19 | [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 | |
Eric Karl | 0ec49506 | 2019-09-10 18:40:15 | [diff] [blame] | 5 | #ifndef GPU_COMMAND_BUFFER_SERVICE_TEXTURE_OWNER_H_ |
| 6 | #define GPU_COMMAND_BUFFER_SERVICE_TEXTURE_OWNER_H_ |
liberato | 2744a14 | 2017-03-07 19:50:19 | [diff] [blame] | 7 | |
Khushal | a55414a | 2018-10-09 17:13:53 | [diff] [blame] | 8 | #include <android/hardware_buffer.h> |
| 9 | |
Chris Watkins | 3285ffe2 | 2017-05-25 17:18:18 | [diff] [blame] | 10 | #include "base/memory/ref_counted.h" |
Chris Watkins | 65dc2a5 | 2017-06-21 17:18:07 | [diff] [blame] | 11 | #include "base/memory/ref_counted_delete_on_sequence.h" |
Patrick Monette | 643cdf6 | 2021-10-15 19:13:42 | [diff] [blame] | 12 | #include "base/task/single_thread_task_runner.h" |
Vasiliy Telezhnikov | c2b79e1 | 2024-01-18 14:54:46 | [diff] [blame] | 13 | #include "base/trace_event/memory_dump_manager.h" |
Vikas Soni | 6f31ac83 | 2022-03-22 22:46:32 | [diff] [blame] | 14 | #include "gpu/command_buffer/service/ref_counted_lock.h" |
Vikas Soni | 5dda089 | 2021-05-10 20:01:52 | [diff] [blame] | 15 | #include "gpu/command_buffer/service/shared_context_state.h" |
Eric Karl | 0ec49506 | 2019-09-10 18:40:15 | [diff] [blame] | 16 | #include "gpu/gpu_gles2_export.h" |
Chris Watkins | 05681d39 | 2017-06-01 18:53:07 | [diff] [blame] | 17 | #include "ui/gl/android/scoped_java_surface.h" |
liberato | 2744a14 | 2017-03-07 19:50:19 | [diff] [blame] | 18 | #include "ui/gl/gl_bindings.h" |
| 19 | #include "ui/gl/gl_context.h" |
| 20 | #include "ui/gl/gl_surface.h" |
| 21 | |
Vikas Soni | b729ade | 2019-01-24 21:40:11 | [diff] [blame] | 22 | namespace base { |
| 23 | namespace android { |
| 24 | class ScopedHardwareBufferFenceSync; |
| 25 | } // namespace android |
| 26 | } // namespace base |
| 27 | |
liberato@chromium.org | c321478 | 2018-12-19 22:27:10 | [diff] [blame] | 28 | namespace gpu { |
Vasiliy Telezhnikov | 14068e71 | 2023-02-02 22:31:36 | [diff] [blame] | 29 | class AbstractTextureAndroid; |
Vikas Soni | 25c125d | 2019-04-04 01:18:25 | [diff] [blame] | 30 | class TextureBase; |
liberato | 2744a14 | 2017-03-07 19:50:19 | [diff] [blame] | 31 | |
Vasiliy Telezhnikov | 5b1563b | 2024-03-18 23:27:35 | [diff] [blame] | 32 | // Used for diagnosting metrics. Do not use for anything else. |
| 33 | // TODO(crbug.com/329821776): Remove once we get enough data. |
| 34 | // These values are persisted to logs. Entries should not be renumbered and |
| 35 | // numeric values should never be reused. |
| 36 | enum TextureOwnerCodecType { |
| 37 | kMediaCodec = 0, |
| 38 | kStreamTexture = 1, |
| 39 | kMaxValue = kStreamTexture |
| 40 | }; |
| 41 | |
Vikas Soni | 0ba01b6 | 2018-05-09 21:01:55 | [diff] [blame] | 42 | // A Texture wrapper interface that creates and maintains ownership of the |
| 43 | // attached GL or Vulkan texture. The texture is destroyed with the object. |
Chris Watkins | 65dc2a5 | 2017-06-21 17:18:07 | [diff] [blame] | 44 | // It should only be accessed on the thread it was created on, with the |
Vikas Soni | 0cd28d9 | 2019-08-01 18:57:59 | [diff] [blame] | 45 | // exception of CreateJavaSurface() and SetFrameAvailableCallback(), which can |
| 46 | // be called on any thread. It's safe to keep and drop refptrs to it on any |
| 47 | // thread; it will be automatically destructed on the thread it was constructed |
| 48 | // on. |
Vikas Soni | 5dda089 | 2021-05-10 20:01:52 | [diff] [blame] | 49 | // TextureOwner also is a shared context lost observer to get notified if the |
| 50 | // TextureOwner's shared context is lost. |
Eric Karl | 0ec49506 | 2019-09-10 18:40:15 | [diff] [blame] | 51 | class GPU_GLES2_EXPORT TextureOwner |
Vikas Soni | 5dda089 | 2021-05-10 20:01:52 | [diff] [blame] | 52 | : public base::RefCountedDeleteOnSequence<TextureOwner>, |
Vasiliy Telezhnikov | c2b79e1 | 2024-01-18 14:54:46 | [diff] [blame] | 53 | public SharedContextState::ContextLostObserver, |
| 54 | public base::trace_event::MemoryDumpProvider { |
liberato | 2744a14 | 2017-03-07 19:50:19 | [diff] [blame] | 55 | public: |
Vikas Soni | 158fa75a | 2018-05-25 20:11:25 | [diff] [blame] | 56 | // Creates a GL texture using the current platform GL context and returns a |
| 57 | // new TextureOwner attached to it. Returns null on failure. |
liberato@chromium.org | c321478 | 2018-12-19 22:27:10 | [diff] [blame] | 58 | // |texture| should be either from CreateAbstractTexture() or a mock. The |
| 59 | // corresponding GL context must be current. |
Khushal | b39ecde | 2019-03-06 00:28:56 | [diff] [blame] | 60 | // Mode indicates which framework API to use and whether the video textures |
vikassoni | 1e5ed63 | 2019-06-28 01:02:41 | [diff] [blame] | 61 | // created using this owner should be hardware protected. It also indicates |
| 62 | // whether SurfaceControl is being used or not. |
Khushal | b39ecde | 2019-03-06 00:28:56 | [diff] [blame] | 63 | enum class Mode { |
Khushal | b39ecde | 2019-03-06 00:28:56 | [diff] [blame] | 64 | kAImageReaderInsecure, |
vikassoni | 1e5ed63 | 2019-06-28 01:02:41 | [diff] [blame] | 65 | kAImageReaderInsecureSurfaceControl, |
Vasiliy Telezhnikov | ee16b70 | 2025-07-09 18:27:15 | [diff] [blame] | 66 | kAImageReaderSecureSurfaceControl |
Khushal | b39ecde | 2019-03-06 00:28:56 | [diff] [blame] | 67 | }; |
Vasiliy Telezhnikov | 14068e71 | 2023-02-02 22:31:36 | [diff] [blame] | 68 | |
liberato@chromium.org | c321478 | 2018-12-19 22:27:10 | [diff] [blame] | 69 | static scoped_refptr<TextureOwner> Create( |
Vikas Soni | 5dda089 | 2021-05-10 20:01:52 | [diff] [blame] | 70 | Mode mode, |
Vikas Soni | 6f31ac83 | 2022-03-22 22:46:32 | [diff] [blame] | 71 | scoped_refptr<SharedContextState> context_state, |
Vasiliy Telezhnikov | 5b1563b | 2024-03-18 23:27:35 | [diff] [blame] | 72 | scoped_refptr<RefCountedLock> drdc_lock, |
| 73 | TextureOwnerCodecType type_for_metrics); |
Vikas Soni | 158fa75a | 2018-05-25 20:11:25 | [diff] [blame] | 74 | |
Peter BostrÃļm | 1ab741d | 2021-10-05 22:49:41 | [diff] [blame] | 75 | TextureOwner(const TextureOwner&) = delete; |
| 76 | TextureOwner& operator=(const TextureOwner&) = delete; |
| 77 | |
Chris Watkins | dc990134 | 2017-07-05 22:00:47 | [diff] [blame] | 78 | scoped_refptr<base::SingleThreadTaskRunner> task_runner() { |
| 79 | return task_runner_; |
| 80 | } |
| 81 | |
Vikas Soni | e5eda88 | 2018-05-08 18:30:58 | [diff] [blame] | 82 | // Returns the GL texture id that the TextureOwner is attached to. |
liberato@chromium.org | c321478 | 2018-12-19 22:27:10 | [diff] [blame] | 83 | GLuint GetTextureId() const; |
Vikas Soni | aa04e98 | 2019-08-16 20:57:08 | [diff] [blame] | 84 | TextureBase* GetTextureBase() const; |
Chris Watkins | 05681d39 | 2017-06-01 18:53:07 | [diff] [blame] | 85 | virtual gl::GLContext* GetContext() const = 0; |
| 86 | virtual gl::GLSurface* GetSurface() const = 0; |
liberato | 2744a14 | 2017-03-07 19:50:19 | [diff] [blame] | 87 | |
Vikas Soni | e5eda88 | 2018-05-08 18:30:58 | [diff] [blame] | 88 | // Create a java surface for the TextureOwner. |
Chris Watkins | 05681d39 | 2017-06-01 18:53:07 | [diff] [blame] | 89 | virtual gl::ScopedJavaSurface CreateJavaSurface() const = 0; |
liberato | 2744a14 | 2017-03-07 19:50:19 | [diff] [blame] | 90 | |
Vikas Soni | 0ba01b6 | 2018-05-09 21:01:55 | [diff] [blame] | 91 | // Update the texture image using the latest available image data. |
Vasiliy Telezhnikov | 81e25a9 | 2025-01-31 18:18:51 | [diff] [blame] | 92 | virtual bool UpdateTexImage(bool discard) = 0; |
Khushal | e2d9fa3 | 2019-03-08 00:02:14 | [diff] [blame] | 93 | |
Vikas Soni | 0ba01b6 | 2018-05-09 21:01:55 | [diff] [blame] | 94 | // Transformation matrix if any associated with the texture image. |
Chris Watkins | 05681d39 | 2017-06-01 18:53:07 | [diff] [blame] | 95 | virtual void ReleaseBackBuffers() = 0; |
| 96 | |
Khushal | a55414a | 2018-10-09 17:13:53 | [diff] [blame] | 97 | // Retrieves the AHardwareBuffer from the latest available image data. |
| 98 | // Note that the object must be used and destroyed on the same thread the |
| 99 | // TextureOwner is bound to. |
Vikas Soni | b729ade | 2019-01-24 21:40:11 | [diff] [blame] | 100 | virtual std::unique_ptr<base::android::ScopedHardwareBufferFenceSync> |
Khushal | a55414a | 2018-10-09 17:13:53 | [diff] [blame] | 101 | GetAHardwareBuffer() = 0; |
| 102 | |
Vasiliy Telezhnikov | f3b4d4e | 2020-05-08 23:33:34 | [diff] [blame] | 103 | // Retrieves backing size and visible rect associated with the most recent |
| 104 | // image. |rotated_visible_size| is the size of the visible region |
| 105 | // post-transform in pixels and is used for SurfaceTexture case. Transform |
| 106 | // here means transform that we get from SurfaceTexture. For MediaPlayer we |
| 107 | // expect to have rotation and MediaPlayer reports rotated size. For |
| 108 | // MediaCodec we don't expect rotation in ST so visible_size (i.e crop rect |
| 109 | // from codec) can be used. |
Vasiliy Telezhnikov | 605f7fe3 | 2020-07-31 22:40:09 | [diff] [blame] | 110 | // Returns whether call was successful or not. |
| 111 | virtual bool GetCodedSizeAndVisibleRect(gfx::Size rotated_visible_size, |
Vasiliy Telezhnikov | f3b4d4e | 2020-05-08 23:33:34 | [diff] [blame] | 112 | gfx::Size* coded_size, |
| 113 | gfx::Rect* visible_rect) = 0; |
| 114 | |
Vikas Soni | aa04e98 | 2019-08-16 20:57:08 | [diff] [blame] | 115 | // Set the callback function to run when a new frame is available. |
| 116 | // |frame_available_cb| is thread safe and can be called on any thread. This |
| 117 | // method should be called only once, i.e., once a callback is provided, it |
| 118 | // should not be changed. |
| 119 | virtual void SetFrameAvailableCallback( |
| 120 | const base::RepeatingClosure& frame_available_cb) = 0; |
| 121 | |
Vasiliy Telezhnikov | c9e34c6 | 2020-12-01 14:40:01 | [diff] [blame] | 122 | // Runs callback when the free buffer is available to render to front buffer. |
Vasiliy Telezhnikov | f2f50aaa | 2021-10-07 03:19:44 | [diff] [blame] | 123 | // Can be run before returning from the function. Callback is run on a caller |
| 124 | // thread. |
Vasiliy Telezhnikov | c9e34c6 | 2020-12-01 14:40:01 | [diff] [blame] | 125 | virtual void RunWhenBufferIsAvailable(base::OnceClosure callback) = 0; |
| 126 | |
Khushal | e2d9fa3 | 2019-03-08 00:02:14 | [diff] [blame] | 127 | bool binds_texture_on_update() const { return binds_texture_on_update_; } |
| 128 | |
Vikas Soni | 5dda089 | 2021-05-10 20:01:52 | [diff] [blame] | 129 | // SharedContextState::ContextLostObserver implementation. |
| 130 | void OnContextLost() override; |
| 131 | |
Chris Watkins | 05681d39 | 2017-06-01 18:53:07 | [diff] [blame] | 132 | protected: |
Vikas Soni | e5eda88 | 2018-05-08 18:30:58 | [diff] [blame] | 133 | friend class base::RefCountedDeleteOnSequence<TextureOwner>; |
| 134 | friend class base::DeleteHelper<TextureOwner>; |
liberato@chromium.org | c321478 | 2018-12-19 22:27:10 | [diff] [blame] | 135 | |
| 136 | // |texture| is the texture that we'll own. |
Khushal | e2d9fa3 | 2019-03-08 00:02:14 | [diff] [blame] | 137 | TextureOwner(bool binds_texture_on_update, |
Vasiliy Telezhnikov | 14068e71 | 2023-02-02 22:31:36 | [diff] [blame] | 138 | std::unique_ptr<AbstractTextureAndroid> texture, |
Vikas Soni | 5dda089 | 2021-05-10 20:01:52 | [diff] [blame] | 139 | scoped_refptr<SharedContextState> context_state); |
| 140 | ~TextureOwner() override; |
liberato@chromium.org | c321478 | 2018-12-19 22:27:10 | [diff] [blame] | 141 | |
| 142 | // Called when |texture_| signals that the platform texture will be destroyed. |
Vikas Soni | 5dda089 | 2021-05-10 20:01:52 | [diff] [blame] | 143 | virtual void ReleaseResources() = 0; |
liberato@chromium.org | c321478 | 2018-12-19 22:27:10 | [diff] [blame] | 144 | |
Vasiliy Telezhnikov | 14068e71 | 2023-02-02 22:31:36 | [diff] [blame] | 145 | AbstractTextureAndroid* texture() const { return texture_.get(); } |
liberato@chromium.org | c321478 | 2018-12-19 22:27:10 | [diff] [blame] | 146 | |
Vasiliy Telezhnikov | c2b79e1 | 2024-01-18 14:54:46 | [diff] [blame] | 147 | int tracing_id() const { return tracing_id_; } |
| 148 | |
Saifuddin Hitawala | 02e0b42 | 2025-03-26 21:35:55 | [diff] [blame] | 149 | static constexpr char kMemoryDumpPrefix[] = "gpu/media_texture_owner_0x%x"; |
| 150 | |
liberato | 2744a14 | 2017-03-07 19:50:19 | [diff] [blame] | 151 | private: |
Vikas Soni | 5dda089 | 2021-05-10 20:01:52 | [diff] [blame] | 152 | friend class MockTextureOwner; |
| 153 | |
| 154 | // To be used by MockTextureOwner. |
| 155 | TextureOwner(bool binds_texture_on_update, |
Vasiliy Telezhnikov | 14068e71 | 2023-02-02 22:31:36 | [diff] [blame] | 156 | std::unique_ptr<AbstractTextureAndroid> texture); |
Vikas Soni | 5dda089 | 2021-05-10 20:01:52 | [diff] [blame] | 157 | |
Khushal | e2d9fa3 | 2019-03-08 00:02:14 | [diff] [blame] | 158 | // Set to true if the updating the image for this owner will automatically |
| 159 | // bind it to the texture target. |
| 160 | const bool binds_texture_on_update_; |
| 161 | |
Vikas Soni | 5dda089 | 2021-05-10 20:01:52 | [diff] [blame] | 162 | scoped_refptr<SharedContextState> context_state_; |
Vasiliy Telezhnikov | 14068e71 | 2023-02-02 22:31:36 | [diff] [blame] | 163 | std::unique_ptr<AbstractTextureAndroid> texture_; |
Chris Watkins | dc990134 | 2017-07-05 22:00:47 | [diff] [blame] | 164 | scoped_refptr<base::SingleThreadTaskRunner> task_runner_; |
Vasiliy Telezhnikov | c2b79e1 | 2024-01-18 14:54:46 | [diff] [blame] | 165 | const int tracing_id_; |
Chris Watkins | 05681d39 | 2017-06-01 18:53:07 | [diff] [blame] | 166 | }; |
liberato | 2744a14 | 2017-03-07 19:50:19 | [diff] [blame] | 167 | |
Vikas Soni | aa04e98 | 2019-08-16 20:57:08 | [diff] [blame] | 168 | } // namespace gpu |
liberato | 2744a14 | 2017-03-07 19:50:19 | [diff] [blame] | 169 | |
Eric Karl | 0ec49506 | 2019-09-10 18:40:15 | [diff] [blame] | 170 | #endif // GPU_COMMAND_BUFFER_SERVICE_TEXTURE_OWNER_H_ |