| // Copyright 2023 The Chromium Authors | |
| // Use of this source code is governed by a BSD-style license that can be | |
| // found in the LICENSE file. | |
| #ifndef GPU_COMMAND_BUFFER_SERVICE_SHARED_IMAGE_GL_TEXTURE_HOLDER_H_ | |
| #define GPU_COMMAND_BUFFER_SERVICE_SHARED_IMAGE_GL_TEXTURE_HOLDER_H_ | |
| #include "base/memory/ref_counted.h" | |
| #include "gpu/command_buffer/service/shared_image/gl_common_image_backing_factory.h" | |
| #include "gpu/command_buffer/service/shared_image/shared_image_format_service_utils.h" | |
| #include "gpu/gpu_gles2_export.h" | |
| #include "ui/gl/progress_reporter.h" | |
| class GrPromiseImageTexture; | |
| namespace gpu { | |
| class SharedContextState; | |
| // Helper class that holds a single GL texture, that works with either | |
| // validating or passthrough command decoder. | |
| class GPU_GLES2_EXPORT GLTextureHolder | |
| : public base::RefCounted<GLTextureHolder> { | |
| public: | |
| // Returns the equivalent SharedImageFormat for plane specified by | |
| // `plane_index`. | |
| static viz::SharedImageFormat GetPlaneFormat(viz::SharedImageFormat format, | |
| int plane_index); | |
| // `format` must be single-planar format. | |
| GLTextureHolder(viz::SharedImageFormat format, | |
| const gfx::Size& size, | |
| bool is_passthrough, | |
| gl::ProgressReporter* progress_reporter); | |
| gles2::Texture* texture() { return texture_; } | |
| const scoped_refptr<gles2::TexturePassthrough>& passthrough_texture() { | |
| return passthrough_texture_; | |
| } | |
| // Returns the service GL texture id. | |
| GLuint GetServiceId() const; | |
| // Initialize by creating a new GL texture. | |
| void Initialize(const GLCommonImageBackingFactory::FormatInfo& format_info, | |
| bool framebuffer_attachment_angle, | |
| base::span<const uint8_t> pixel_data, | |
| const std::string& debug_label); | |
| // Initialize with a GL texture created elsewhere. | |
| void InitializeWithTexture(const GLFormatDesc& format_desc, | |
| scoped_refptr<gles2::TexturePassthrough> texture); | |
| // Initialize with a GL texture created elsewhere. Takes ownership of | |
| // lightweight ref on `texture`. | |
| void InitializeWithTexture(const GLFormatDesc& format_desc, | |
| gles2::Texture* texture); | |
| // Uploads pixels from `pixmap` to GL texture. | |
| bool UploadFromMemory(const SkPixmap& pixmap); | |
| // Readback pixels from GL texture to `pixmap`. | |
| bool ReadbackToMemory(const SkPixmap& pixmap); | |
| // Returns a promise image for the GL texture. | |
| sk_sp<GrPromiseImageTexture> GetPromiseImage( | |
| SharedContextState* context_state); | |
| // Gets/sets cleared rect from gles2::Texture. Only valid to call with | |
| // validating command decoder. | |
| gfx::Rect GetClearedRect() const; | |
| void SetClearedRect(const gfx::Rect& cleared_rect); | |
| void SetContextLost(); | |
| private: | |
| friend class base::RefCounted<GLTextureHolder>; | |
| ~GLTextureHolder(); | |
| viz::SharedImageFormat format_; | |
| gfx::Size size_; | |
| bool is_passthrough_; | |
| bool context_lost_ = false; | |
| raw_ptr<gles2::Texture> texture_ = nullptr; | |
| scoped_refptr<gles2::TexturePassthrough> passthrough_texture_; | |
| // The GL context that was current when this holder was | |
| // initialized. This is used to ensure the correct GL context is current | |
| // during GL operations (e.g. UploadFromMemory, ReadbackToMemory). | |
| scoped_refptr<gl::GLContext> context_; | |
| GLFormatDesc format_desc_; | |
| raw_ptr<gl::ProgressReporter> progress_reporter_ = nullptr; | |
| }; | |
| } // namespace gpu | |
| #endif // GPU_COMMAND_BUFFER_SERVICE_SHARED_IMAGE_GL_TEXTURE_HOLDER_H_ |