blob: 49f3f5740b27dba0e2236ac3cb6dfee41453084b [file] [log] [blame]
Avi Drissman05dfbc822022-09-13 21:25:341// Copyright 2018 The Chromium Authors
liberato2744a142017-03-07 19:50:192// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Eric Karl0ec495062019-09-10 18:40:155#ifndef GPU_COMMAND_BUFFER_SERVICE_TEXTURE_OWNER_H_
6#define GPU_COMMAND_BUFFER_SERVICE_TEXTURE_OWNER_H_
liberato2744a142017-03-07 19:50:197
Khushala55414a2018-10-09 17:13:538#include <android/hardware_buffer.h>
9
Chris Watkins3285ffe22017-05-25 17:18:1810#include "base/memory/ref_counted.h"
Chris Watkins65dc2a52017-06-21 17:18:0711#include "base/memory/ref_counted_delete_on_sequence.h"
Patrick Monette643cdf62021-10-15 19:13:4212#include "base/task/single_thread_task_runner.h"
Vasiliy Telezhnikovc2b79e12024-01-18 14:54:4613#include "base/trace_event/memory_dump_manager.h"
Vikas Soni6f31ac832022-03-22 22:46:3214#include "gpu/command_buffer/service/ref_counted_lock.h"
Vikas Soni5dda0892021-05-10 20:01:5215#include "gpu/command_buffer/service/shared_context_state.h"
Eric Karl0ec495062019-09-10 18:40:1516#include "gpu/gpu_gles2_export.h"
Chris Watkins05681d392017-06-01 18:53:0717#include "ui/gl/android/scoped_java_surface.h"
liberato2744a142017-03-07 19:50:1918#include "ui/gl/gl_bindings.h"
19#include "ui/gl/gl_context.h"
20#include "ui/gl/gl_surface.h"
21
Vikas Sonib729ade2019-01-24 21:40:1122namespace base {
23namespace android {
24class ScopedHardwareBufferFenceSync;
25} // namespace android
26} // namespace base
27
liberato@chromium.orgc3214782018-12-19 22:27:1028namespace gpu {
Vasiliy Telezhnikov14068e712023-02-02 22:31:3629class AbstractTextureAndroid;
Vikas Soni25c125d2019-04-04 01:18:2530class TextureBase;
liberato2744a142017-03-07 19:50:1931
Vasiliy Telezhnikov5b1563b2024-03-18 23:27:3532// 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.
36enum TextureOwnerCodecType {
37 kMediaCodec = 0,
38 kStreamTexture = 1,
39 kMaxValue = kStreamTexture
40};
41
Vikas Soni0ba01b62018-05-09 21:01:5542// 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 Watkins65dc2a52017-06-21 17:18:0744// It should only be accessed on the thread it was created on, with the
Vikas Soni0cd28d92019-08-01 18:57:5945// 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 Soni5dda0892021-05-10 20:01:5249// TextureOwner also is a shared context lost observer to get notified if the
50// TextureOwner's shared context is lost.
Eric Karl0ec495062019-09-10 18:40:1551class GPU_GLES2_EXPORT TextureOwner
Vikas Soni5dda0892021-05-10 20:01:5252 : public base::RefCountedDeleteOnSequence<TextureOwner>,
Vasiliy Telezhnikovc2b79e12024-01-18 14:54:4653 public SharedContextState::ContextLostObserver,
54 public base::trace_event::MemoryDumpProvider {
liberato2744a142017-03-07 19:50:1955 public:
Vikas Soni158fa75a2018-05-25 20:11:2556 // 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.orgc3214782018-12-19 22:27:1058 // |texture| should be either from CreateAbstractTexture() or a mock. The
59 // corresponding GL context must be current.
Khushalb39ecde2019-03-06 00:28:5660 // Mode indicates which framework API to use and whether the video textures
vikassoni1e5ed632019-06-28 01:02:4161 // created using this owner should be hardware protected. It also indicates
62 // whether SurfaceControl is being used or not.
Khushalb39ecde2019-03-06 00:28:5663 enum class Mode {
Khushalb39ecde2019-03-06 00:28:5664 kAImageReaderInsecure,
vikassoni1e5ed632019-06-28 01:02:4165 kAImageReaderInsecureSurfaceControl,
Vasiliy Telezhnikovee16b702025-07-09 18:27:1566 kAImageReaderSecureSurfaceControl
Khushalb39ecde2019-03-06 00:28:5667 };
Vasiliy Telezhnikov14068e712023-02-02 22:31:3668
liberato@chromium.orgc3214782018-12-19 22:27:1069 static scoped_refptr<TextureOwner> Create(
Vikas Soni5dda0892021-05-10 20:01:5270 Mode mode,
Vikas Soni6f31ac832022-03-22 22:46:3271 scoped_refptr<SharedContextState> context_state,
Vasiliy Telezhnikov5b1563b2024-03-18 23:27:3572 scoped_refptr<RefCountedLock> drdc_lock,
73 TextureOwnerCodecType type_for_metrics);
Vikas Soni158fa75a2018-05-25 20:11:2574
Peter BostrÃļm1ab741d2021-10-05 22:49:4175 TextureOwner(const TextureOwner&) = delete;
76 TextureOwner& operator=(const TextureOwner&) = delete;
77
Chris Watkinsdc9901342017-07-05 22:00:4778 scoped_refptr<base::SingleThreadTaskRunner> task_runner() {
79 return task_runner_;
80 }
81
Vikas Sonie5eda882018-05-08 18:30:5882 // Returns the GL texture id that the TextureOwner is attached to.
liberato@chromium.orgc3214782018-12-19 22:27:1083 GLuint GetTextureId() const;
Vikas Soniaa04e982019-08-16 20:57:0884 TextureBase* GetTextureBase() const;
Chris Watkins05681d392017-06-01 18:53:0785 virtual gl::GLContext* GetContext() const = 0;
86 virtual gl::GLSurface* GetSurface() const = 0;
liberato2744a142017-03-07 19:50:1987
Vikas Sonie5eda882018-05-08 18:30:5888 // Create a java surface for the TextureOwner.
Chris Watkins05681d392017-06-01 18:53:0789 virtual gl::ScopedJavaSurface CreateJavaSurface() const = 0;
liberato2744a142017-03-07 19:50:1990
Vikas Soni0ba01b62018-05-09 21:01:5591 // Update the texture image using the latest available image data.
Vasiliy Telezhnikov81e25a92025-01-31 18:18:5192 virtual bool UpdateTexImage(bool discard) = 0;
Khushale2d9fa32019-03-08 00:02:1493
Vikas Soni0ba01b62018-05-09 21:01:5594 // Transformation matrix if any associated with the texture image.
Chris Watkins05681d392017-06-01 18:53:0795 virtual void ReleaseBackBuffers() = 0;
96
Khushala55414a2018-10-09 17:13:5397 // 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 Sonib729ade2019-01-24 21:40:11100 virtual std::unique_ptr<base::android::ScopedHardwareBufferFenceSync>
Khushala55414a2018-10-09 17:13:53101 GetAHardwareBuffer() = 0;
102
Vasiliy Telezhnikovf3b4d4e2020-05-08 23:33:34103 // 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 Telezhnikov605f7fe32020-07-31 22:40:09110 // Returns whether call was successful or not.
111 virtual bool GetCodedSizeAndVisibleRect(gfx::Size rotated_visible_size,
Vasiliy Telezhnikovf3b4d4e2020-05-08 23:33:34112 gfx::Size* coded_size,
113 gfx::Rect* visible_rect) = 0;
114
Vikas Soniaa04e982019-08-16 20:57:08115 // 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 Telezhnikovc9e34c62020-12-01 14:40:01122 // Runs callback when the free buffer is available to render to front buffer.
Vasiliy Telezhnikovf2f50aaa2021-10-07 03:19:44123 // Can be run before returning from the function. Callback is run on a caller
124 // thread.
Vasiliy Telezhnikovc9e34c62020-12-01 14:40:01125 virtual void RunWhenBufferIsAvailable(base::OnceClosure callback) = 0;
126
Khushale2d9fa32019-03-08 00:02:14127 bool binds_texture_on_update() const { return binds_texture_on_update_; }
128
Vikas Soni5dda0892021-05-10 20:01:52129 // SharedContextState::ContextLostObserver implementation.
130 void OnContextLost() override;
131
Chris Watkins05681d392017-06-01 18:53:07132 protected:
Vikas Sonie5eda882018-05-08 18:30:58133 friend class base::RefCountedDeleteOnSequence<TextureOwner>;
134 friend class base::DeleteHelper<TextureOwner>;
liberato@chromium.orgc3214782018-12-19 22:27:10135
136 // |texture| is the texture that we'll own.
Khushale2d9fa32019-03-08 00:02:14137 TextureOwner(bool binds_texture_on_update,
Vasiliy Telezhnikov14068e712023-02-02 22:31:36138 std::unique_ptr<AbstractTextureAndroid> texture,
Vikas Soni5dda0892021-05-10 20:01:52139 scoped_refptr<SharedContextState> context_state);
140 ~TextureOwner() override;
liberato@chromium.orgc3214782018-12-19 22:27:10141
142 // Called when |texture_| signals that the platform texture will be destroyed.
Vikas Soni5dda0892021-05-10 20:01:52143 virtual void ReleaseResources() = 0;
liberato@chromium.orgc3214782018-12-19 22:27:10144
Vasiliy Telezhnikov14068e712023-02-02 22:31:36145 AbstractTextureAndroid* texture() const { return texture_.get(); }
liberato@chromium.orgc3214782018-12-19 22:27:10146
Vasiliy Telezhnikovc2b79e12024-01-18 14:54:46147 int tracing_id() const { return tracing_id_; }
148
Saifuddin Hitawala02e0b422025-03-26 21:35:55149 static constexpr char kMemoryDumpPrefix[] = "gpu/media_texture_owner_0x%x";
150
liberato2744a142017-03-07 19:50:19151 private:
Vikas Soni5dda0892021-05-10 20:01:52152 friend class MockTextureOwner;
153
154 // To be used by MockTextureOwner.
155 TextureOwner(bool binds_texture_on_update,
Vasiliy Telezhnikov14068e712023-02-02 22:31:36156 std::unique_ptr<AbstractTextureAndroid> texture);
Vikas Soni5dda0892021-05-10 20:01:52157
Khushale2d9fa32019-03-08 00:02:14158 // 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 Soni5dda0892021-05-10 20:01:52162 scoped_refptr<SharedContextState> context_state_;
Vasiliy Telezhnikov14068e712023-02-02 22:31:36163 std::unique_ptr<AbstractTextureAndroid> texture_;
Chris Watkinsdc9901342017-07-05 22:00:47164 scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
Vasiliy Telezhnikovc2b79e12024-01-18 14:54:46165 const int tracing_id_;
Chris Watkins05681d392017-06-01 18:53:07166};
liberato2744a142017-03-07 19:50:19167
Vikas Soniaa04e982019-08-16 20:57:08168} // namespace gpu
liberato2744a142017-03-07 19:50:19169
Eric Karl0ec495062019-09-10 18:40:15170#endif // GPU_COMMAND_BUFFER_SERVICE_TEXTURE_OWNER_H_