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
   91
   92
   93
   94
   95
   96
   97
   98
   99
  100
  101
  102
  103
  104
  105
  106
  107

gpu / command_buffer / client / mock_transfer_buffer.h [blame]

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

#ifdef UNSAFE_BUFFERS_BUILD
// TODO(crbug.com/40285824): Remove this and convert code to safer constructs.
#pragma allow_unsafe_buffers
#endif

#ifndef GPU_COMMAND_BUFFER_CLIENT_MOCK_TRANSFER_BUFFER_H_
#define GPU_COMMAND_BUFFER_CLIENT_MOCK_TRANSFER_BUFFER_H_

#include "base/memory/raw_ptr.h"
#include "base/memory/stack_allocated.h"
#include "gpu/command_buffer/client/ring_buffer.h"
#include "gpu/command_buffer/client/transfer_buffer.h"

namespace gpu {

class CommandBuffer;

class MockTransferBuffer : public TransferBufferInterface {
 public:
  struct ExpectedMemoryInfo {
    STACK_ALLOCATED();

   public:
    uint32_t offset;
    int32_t id;
    uint8_t* ptr;
  };

  MockTransferBuffer(CommandBuffer* command_buffer,
                     unsigned int size,
                     unsigned int result_size,
                     unsigned int alignment,
                     bool initialize_fail);

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

  ~MockTransferBuffer() override;

  base::UnguessableToken shared_memory_guid() const override;
  bool Initialize(unsigned int starting_buffer_size,
                  unsigned int result_size,
                  unsigned int /* min_buffer_size */,
                  unsigned int /* max_buffer_size */,
                  unsigned int alignment) override;
  int GetShmId() override;
  void* AcquireResultBuffer() override;
  void ReleaseResultBuffer() override;
  int GetResultOffset() override;
  void Free() override;
  bool HaveBuffer() const override;
  void* AllocUpTo(unsigned int size, unsigned int* size_allocated) override;
  void* Alloc(unsigned int size) override;
  RingBuffer::Offset GetOffset(void* pointer) const override;
  void DiscardBlock(void* p) override;
  void FreePendingToken(void* p, unsigned int /* token */) override;
  unsigned int GetSize() const override;
  unsigned int GetFreeSize() const override;
  unsigned int GetFragmentedFreeSize() const override;
  void ShrinkLastBlock(unsigned int new_size) override;
  unsigned int GetMaxSize() const override;

  uint32_t MaxTransferBufferSize();
  unsigned int RoundToAlignment(unsigned int size);
  bool InSync();
  ExpectedMemoryInfo GetExpectedMemory(uint32_t size);
  ExpectedMemoryInfo GetExpectedResultMemory(uint32_t size);

 private:
  static const int kNumBuffers = 2;

  uint8_t* actual_buffer() const {
    return static_cast<uint8_t*>(buffers_[actual_buffer_index_]->memory());
  }

  uint8_t* expected_buffer() const {
    return static_cast<uint8_t*>(buffers_[expected_buffer_index_]->memory());
  }

  uint32_t AllocateExpectedTransferBuffer(uint32_t size);
  void* GetExpectedTransferAddressFromOffset(uint32_t offset, uint32_t size);
  int GetExpectedResultBufferId();
  uint32_t GetExpectedResultBufferOffset();
  int GetExpectedTransferBufferId();

  raw_ptr<CommandBuffer> command_buffer_;
  uint32_t size_;
  uint32_t result_size_;
  uint32_t alignment_;
  int buffer_ids_[kNumBuffers];
  scoped_refptr<Buffer> buffers_[kNumBuffers];
  int actual_buffer_index_;
  int expected_buffer_index_;
  raw_ptr<void> last_alloc_;
  uint32_t expected_offset_;
  uint32_t actual_offset_;
  bool initialize_fail_;
  bool outstanding_result_pointer_ = false;
};

}  // namespace gpu

#endif  // GPU_COMMAND_BUFFER_CLIENT_MOCK_TRANSFER_BUFFER_H_