1 // Copyright 2018 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 GPU_COMMAND_BUFFER_SERVICE_TEXTURE_OWNER_H_
6 #define GPU_COMMAND_BUFFER_SERVICE_TEXTURE_OWNER_H_
7 
8 #include <android/hardware_buffer.h>
9 
10 #include "base/memory/ref_counted.h"
11 #include "base/memory/ref_counted_delete_on_sequence.h"
12 #include "base/single_thread_task_runner.h"
13 #include "gpu/gpu_gles2_export.h"
14 #include "ui/gl/android/scoped_java_surface.h"
15 #include "ui/gl/gl_bindings.h"
16 #include "ui/gl/gl_context.h"
17 #include "ui/gl/gl_image.h"
18 #include "ui/gl/gl_surface.h"
19 
20 namespace base {
21 namespace android {
22 class ScopedHardwareBufferFenceSync;
23 }  // namespace android
24 }  // namespace base
25 
26 namespace gpu {
27 class SharedContextState;
28 class TextureBase;
29 namespace gles2 {
30 class AbstractTexture;
31 }  // namespace gles2
32 
33 // A Texture wrapper interface that creates and maintains ownership of the
34 // attached GL or Vulkan texture. The texture is destroyed with the object.
35 // It should only be accessed on the thread it was created on, with the
36 // exception of CreateJavaSurface() and SetFrameAvailableCallback(), which can
37 // be called on any thread. It's safe to keep and drop refptrs to it on any
38 // thread; it will be automatically destructed on the thread it was constructed
39 // on.
40 class GPU_GLES2_EXPORT TextureOwner
41     : public base::RefCountedDeleteOnSequence<TextureOwner> {
42  public:
43   // Creates a GL texture using the current platform GL context and returns a
44   // new TextureOwner attached to it. Returns null on failure.
45   // |texture| should be either from CreateAbstractTexture() or a mock.  The
46   // corresponding GL context must be current.
47   // Mode indicates which framework API to use and whether the video textures
48   // created using this owner should be hardware protected. It also indicates
49   // whether SurfaceControl is being used or not.
50   enum class Mode {
51     kAImageReaderInsecure,
52 
53     // This mode indicates that the frame is going to be used in multi-threaded
54     // compositor where compositor is running on a different gpu thread and
55     // context than chrome's gpu main thread/context.
56     kAImageReaderInsecureMultithreaded,
57     kAImageReaderInsecureSurfaceControl,
58     kAImageReaderSecureSurfaceControl,
59     kSurfaceTextureInsecure
60   };
61   static scoped_refptr<TextureOwner> Create(
62       std::unique_ptr<gles2::AbstractTexture> texture,
63       Mode mode);
64 
65   // Create a texture that's appropriate for a TextureOwner.
66   static std::unique_ptr<gles2::AbstractTexture> CreateTexture(
67       scoped_refptr<SharedContextState> context_state);
68 
task_runner()69   scoped_refptr<base::SingleThreadTaskRunner> task_runner() {
70     return task_runner_;
71   }
72 
73   // Returns the GL texture id that the TextureOwner is attached to.
74   GLuint GetTextureId() const;
75   TextureBase* GetTextureBase() const;
76   virtual gl::GLContext* GetContext() const = 0;
77   virtual gl::GLSurface* GetSurface() const = 0;
78 
79   // Create a java surface for the TextureOwner.
80   virtual gl::ScopedJavaSurface CreateJavaSurface() const = 0;
81 
82   // Update the texture image using the latest available image data.
83   virtual void UpdateTexImage() = 0;
84 
85   // Ensures that the latest texture image is bound to the texture target.
86   // Should only be used if the TextureOwner requires explicit binding of the
87   // image after an update.
88   virtual void EnsureTexImageBound() = 0;
89 
90   // Transformation matrix if any associated with the texture image.
91   virtual void ReleaseBackBuffers() = 0;
92 
93   // Retrieves the AHardwareBuffer from the latest available image data.
94   // Note that the object must be used and destroyed on the same thread the
95   // TextureOwner is bound to.
96   virtual std::unique_ptr<base::android::ScopedHardwareBufferFenceSync>
97   GetAHardwareBuffer() = 0;
98 
99   // Retrieves backing size and visible rect associated with the most recent
100   // image. |rotated_visible_size| is the size of the visible region
101   // post-transform in pixels and is used for SurfaceTexture case. Transform
102   // here means transform that we get from SurfaceTexture. For MediaPlayer we
103   // expect to have rotation and MediaPlayer reports rotated size. For
104   // MediaCodec we don't expect rotation in ST so visible_size (i.e crop rect
105   // from codec) can be used.
106   // Returns whether call was successful or not.
107   virtual bool GetCodedSizeAndVisibleRect(gfx::Size rotated_visible_size,
108                                           gfx::Size* coded_size,
109                                           gfx::Rect* visible_rect) = 0;
110 
111   // Set the callback function to run when a new frame is available.
112   // |frame_available_cb| is thread safe and can be called on any thread. This
113   // method should be called only once, i.e., once a callback is provided, it
114   // should not be changed.
115   virtual void SetFrameAvailableCallback(
116       const base::RepeatingClosure& frame_available_cb) = 0;
117 
binds_texture_on_update()118   bool binds_texture_on_update() const { return binds_texture_on_update_; }
119 
120  protected:
121   friend class base::RefCountedDeleteOnSequence<TextureOwner>;
122   friend class base::DeleteHelper<TextureOwner>;
123 
124   // |texture| is the texture that we'll own.
125   TextureOwner(bool binds_texture_on_update,
126                std::unique_ptr<gles2::AbstractTexture> texture);
127   virtual ~TextureOwner();
128 
129   // Drop |texture_| immediately.  Will call OnTextureDestroyed immediately if
130   // it hasn't been called before (e.g., due to lost context).
131   // Subclasses must call this before they complete destruction, else
132   // OnTextureDestroyed might be called when we drop |texture_|, which is not
133   // defined once subclass destruction has completed.
134   void ClearAbstractTexture();
135 
136   // Called when |texture_| signals that the platform texture will be destroyed.
137   // See AbstractTexture::SetCleanupCallback.
138   virtual void OnTextureDestroyed(gles2::AbstractTexture*) = 0;
139 
texture()140   gles2::AbstractTexture* texture() const { return texture_.get(); }
141 
142  private:
143   // Set to true if the updating the image for this owner will automatically
144   // bind it to the texture target.
145   const bool binds_texture_on_update_;
146 
147   std::unique_ptr<gles2::AbstractTexture> texture_;
148   scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
149 
150   DISALLOW_COPY_AND_ASSIGN(TextureOwner);
151 };
152 
153 }  // namespace gpu
154 
155 #endif  // GPU_COMMAND_BUFFER_SERVICE_TEXTURE_OWNER_H_
156