// Copyright 2025 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef THIRD_PARTY_BLINK_RENDERER_MODULES_XR_XR_WEBGL_SWAP_CHAIN_H_
#define THIRD_PARTY_BLINK_RENDERER_MODULES_XR_XR_WEBGL_SWAP_CHAIN_H_

#include "gpu/command_buffer/common/sync_token.h"
#include "third_party/blink/renderer/modules/webgl/webgl_rendering_context_base.h"
#include "third_party/blink/renderer/modules/webgl/webgl_unowned_texture.h"
#include "third_party/blink/renderer/modules/xr/xr_swap_chain.h"
#include "third_party/blink/renderer/platform/graphics/gpu/xr_webgl_drawing_buffer.h"
#include "third_party/blink/renderer/platform/graphics/static_bitmap_image.h"
#include "third_party/blink/renderer/platform/heap/garbage_collected.h"
#include "third_party/blink/renderer/platform/heap/member.h"
#include "third_party/blink/renderer/platform/heap/prefinalizer.h"

namespace blink {

class WebGLRenderingContextBase;
class WebGLUnownedTexture;

class ScopedXRWebGLStateRestorer {
  STACK_ALLOCATED();

 public:
  ScopedXRWebGLStateRestorer(WebGLRenderingContextBase* context,
                             GLenum source_texture_target);
  ~ScopedXRWebGLStateRestorer();

  ScopedXRWebGLStateRestorer(const ScopedXRWebGLStateRestorer&) = delete;
  ScopedXRWebGLStateRestorer& operator=(const ScopedXRWebGLStateRestorer&) =
      delete;

 private:
  WebGLRenderingContextBase* context_;
  gpu::gles2::GLES2Interface* gl_;
  GLenum source_texture_target_;

  std::array<GLint, 4> viewport_ = {0, 0, 0, 0};
  bool depth_test_enabled_ = false;
  bool stencil_test_enabled_ = false;
  bool culling_enabled_ = false;
  bool blend_enabled_ = false;
  bool dither_enabled_ = false;

  GLenum polygon_mode_ = GL_FILL_ANGLE;
  bool polygon_mode_extension_enabled_ = false;
};

class XRWebGLSwapChain : public XRSwapChain<WebGLUnownedTexture> {
 public:
  struct Descriptor {
    GLenum format;
    GLenum internal_format;
    GLenum type;
    GLenum attachment_target;
    uint16_t width;
    uint16_t height;
    uint16_t layers;
    bool is_texture_array;
    bool clear_on_access;
  };

  XRWebGLSwapChain(WebGLRenderingContextBase*,
                   const XRWebGLSwapChain::Descriptor&,
                   bool webgl_2);
  ~XRWebGLSwapChain() override = default;

  WebGLRenderingContextBase* context() { return webgl_context_.Get(); }
  const XRWebGLSwapChain::Descriptor& descriptor() const { return descriptor_; }
  bool webgl2() const { return webgl2_; }

  void Trace(Visitor* visitor) const override;

  void ClearCurrentTexture();

  virtual std::unique_ptr<SharedImageHolder> TransferToSharedImageHolder() {
    return nullptr;
  }
  virtual bool IsCube() const { return false; }
  virtual gpu::SyncToken GetSyncToken() const { return gpu::SyncToken(); }

 protected:
  void OnTextureQueried() override;
  WebGLFramebuffer* GetFramebuffer();

 private:
  Member<WebGLRenderingContextBase> webgl_context_;
  Member<WebGLFramebuffer> framebuffer_;

  XRWebGLSwapChain::Descriptor descriptor_;
  bool webgl2_;
};

// A texture swap chain that is not communicated back to the compositor, used
// for things like depth/stencil attachments that don't assist reprojection.
class XRWebGLStaticSwapChain final : public XRWebGLSwapChain {
  USING_PRE_FINALIZER(XRWebGLStaticSwapChain, Dispose);

 public:
  XRWebGLStaticSwapChain(WebGLRenderingContextBase*,
                         const XRWebGLSwapChain::Descriptor&,
                         bool webgl2);
  ~XRWebGLStaticSwapChain() override = default;

  void Dispose();

  WebGLUnownedTexture* ProduceTexture() override;

  void OnFrameEnd() override;

 private:
  GLuint owned_texture_;
};

// A swap chain backed by SharedImages
class XRWebGLSharedImageSwapChain final : public XRWebGLSwapChain {
 public:
  XRWebGLSharedImageSwapChain(WebGLRenderingContextBase*,
                              const XRWebGLSwapChain::Descriptor&,
                              bool webgl2);
  ~XRWebGLSharedImageSwapChain() override = default;

  WebGLUnownedTexture* ProduceTexture() override;

  void OnFrameEnd() override;

  gpu::SyncToken GetSyncToken() const override { return sync_token_; }

 private:
  std::unique_ptr<gpu::SharedImageTexture> shared_image_texture_;
  std::unique_ptr<gpu::SharedImageTexture::ScopedAccess>
      shared_image_scoped_access_;
  gpu::SyncToken sync_token_;
};

}  // namespace blink

#endif  // THIRD_PARTY_BLINK_RENDERER_MODULES_XR_XR_WEBGL_SWAP_CHAIN_H_
