1
    2
    3
    4
    5
    6
    7
    8
    9
   10
   11
   12
   13
   14
   15
   16
   17
   18
   19
   20
   21
   22
   23
   24
   25
   26
   27
   28
   29
   30
   31
   32
   33
   34
   35
   36
   37
   38
   39
   40
   41
   42
   43
   44
   45
   46
   47
   48
   49
   50
   51
   52
   53
   54
   55
   56
   57
   58
   59
   60
   61
   62
   63
   64
   65
   66
   67
   68
   69
   70
   71
   72
   73
   74
   75
   76
   77
   78
   79
   80
   81
   82
   83
   84
   85
   86
   87
   88
   89
   90

gpu / command_buffer / service / shared_image / gl_texture_holder.h [blame]

// 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 "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 "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 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);
  GLTextureHolder(GLTextureHolder&& other);
  GLTextureHolder& operator=(GLTextureHolder&& other);
  ~GLTextureHolder();

  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:
  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_;
  GLFormatDesc format_desc_;
  raw_ptr<gl::ProgressReporter> progress_reporter_ = nullptr;
};

}  // namespace gpu

#endif  // GPU_COMMAND_BUFFER_SERVICE_SHARED_IMAGE_GL_TEXTURE_HOLDER_H_