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
  108
  109
  110
  111
  112
  113
  114
  115
  116
  117
  118
  119
  120
  121
  122
  123
  124
  125
  126
  127
  128
  129
  130
  131
  132
  133
  134
  135
  136
  137
  138
  139
  140
  141
  142
  143
  144
  145
  146
  147
  148
  149
  150
  151
  152
  153
  154
  155
  156
  157
  158
  159
  160
  161
  162
  163
  164
  165
  166
  167
  168
  169
  170
  171
  172
  173
  174
  175
  176
  177
  178

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

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

// Helper classes for implementing gpu client side unit tests.

#ifndef GPU_COMMAND_BUFFER_CLIENT_CLIENT_TEST_HELPER_H_
#define GPU_COMMAND_BUFFER_CLIENT_CLIENT_TEST_HELPER_H_

#include <stddef.h>
#include <stdint.h>

#include <memory>

#include "base/compiler_specific.h"
#include "gpu/command_buffer/client/gpu_control.h"
#include "gpu/command_buffer/common/cmd_buffer_common.h"
#include "gpu/command_buffer/common/gpu_memory_allocation.h"
#include "gpu/command_buffer/common/sync_token.h"
#include "gpu/command_buffer/service/command_buffer_service.h"
#include "gpu/command_buffer/service/decoder_client.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace gpu {

class FakeCommandBufferServiceBase : public CommandBufferServiceBase {
 public:
  static const int32_t kTransferBufferBaseId = 0x123;
  static const int32_t kMaxTransferBuffers = 32;

  FakeCommandBufferServiceBase();
  ~FakeCommandBufferServiceBase() override;

  CommandBuffer::State GetState() override;
  void SetReleaseCount(uint64_t release_count) override;
  scoped_refptr<gpu::Buffer> GetTransferBuffer(int32_t id) override;
  void SetToken(int32_t token) override;
  void SetParseError(error::Error error) override;
  void SetContextLostReason(error::ContextLostReason reason) override;

  // Get's the Id of the next transfer buffer that will be returned
  // by CreateTransferBuffer. This is useful for testing expected ids.
  int32_t GetNextFreeTransferBufferId();

  void FlushHelper(int32_t put_offset);
  void SetGetBufferHelper(int transfer_buffer_id, int32_t token);
  scoped_refptr<gpu::Buffer> CreateTransferBufferHelper(uint32_t size,
                                                        int32_t* id);
  void DestroyTransferBufferHelper(int32_t id);

 private:
  scoped_refptr<Buffer> transfer_buffer_buffers_[kMaxTransferBuffers];
  CommandBuffer::State state_;
};

class MockClientCommandBuffer : public CommandBuffer,
                                public FakeCommandBufferServiceBase {
 public:
  MockClientCommandBuffer();
  ~MockClientCommandBuffer() override;

  State GetLastState() override;
  State WaitForTokenInRange(int32_t start, int32_t end) override;
  State WaitForGetOffsetInRange(uint32_t set_get_buffer_count,
                                int32_t start,
                                int32_t end) override;
  void SetGetBuffer(int transfer_buffer_id) override;
  scoped_refptr<gpu::Buffer> CreateTransferBuffer(
      uint32_t size,
      int32_t* id,
      uint32_t alignment = 0,
      TransferBufferAllocationOption option =
          TransferBufferAllocationOption::kLoseContextOnOOM) override;

  // This is so we can use all the gmock functions when Flush is called.
  MOCK_METHOD0(OnFlush, void());
  MOCK_METHOD1(DestroyTransferBuffer, void(int32_t id));

  void Flush(int32_t put_offset) override;
  void OrderingBarrier(int32_t put_offset) override;

  void DelegateToFake();

  int32_t GetServicePutOffset() { return put_offset_; }

  void SetTokenForSetGetBuffer(int32_t token) { token_ = token; }

  void ForceLostContext(error::ContextLostReason reason) override;

 private:
  int32_t put_offset_ = 0;
  int32_t token_ = 10000;  // All token checks in the tests should pass.
};

class MockClientCommandBufferMockFlush : public MockClientCommandBuffer {
 public:
  MockClientCommandBufferMockFlush();
  ~MockClientCommandBufferMockFlush() override;

  MOCK_METHOD1(Flush, void(int32_t put_offset));
  MOCK_METHOD1(OrderingBarrier, void(int32_t put_offset));

  void DelegateToFake();
  void DoFlush(int32_t put_offset);
};

class MockClientGpuControl : public GpuControl {
 public:
  MockClientGpuControl();

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

  ~MockClientGpuControl() override;

  MOCK_METHOD1(SetGpuControlClient, void(GpuControlClient*));
  MOCK_CONST_METHOD0(GetCapabilities, const Capabilities&());
  MOCK_CONST_METHOD0(GetGLCapabilities, const GLCapabilities&());
  MOCK_METHOD3(CreateImage,
               int32_t(ClientBuffer buffer, size_t width, size_t height));
  MOCK_METHOD1(DestroyImage, void(int32_t id));

  // Workaround for move-only args in GMock.
  MOCK_METHOD2(DoSignalQuery,
               void(uint32_t query, base::OnceClosure* callback));
  void SignalQuery(uint32_t query, base::OnceClosure callback) override {
    DoSignalQuery(query, &callback);
  }
  MOCK_METHOD0(CancelAllQueries, void());

  MOCK_METHOD1(CreateStreamTexture, uint32_t(uint32_t));
  MOCK_METHOD1(SetLock, void(base::Lock*));
  MOCK_METHOD0(EnsureWorkVisible, void());
  MOCK_CONST_METHOD0(GetNamespaceID, CommandBufferNamespace());
  MOCK_CONST_METHOD0(GetCommandBufferID, CommandBufferId());
  MOCK_METHOD0(FlushPendingWork, void());
  MOCK_METHOD0(GenerateFenceSyncRelease, uint64_t());
  MOCK_METHOD1(IsFenceSyncReleased, bool(uint64_t release));

  // Workaround for move-only args in GMock.
  MOCK_METHOD2(DoSignalSyncToken,
               void(const SyncToken& sync_token, base::OnceClosure* callback));
  void SignalSyncToken(const SyncToken& sync_token,
                       base::OnceClosure callback) override {
    DoSignalSyncToken(sync_token, &callback);
  }

  MOCK_METHOD1(WaitSyncToken, void(const SyncToken&));
  MOCK_METHOD1(CanWaitUnverifiedSyncToken, bool(const SyncToken&));
  MOCK_METHOD2(CreateGpuFence,
               void(uint32_t gpu_fence_id, ClientGpuFence source));
  // OnceCallback isn't mockable?
  void GetGpuFence(uint32_t gpu_fence_id,
                   base::OnceCallback<void(std::unique_ptr<gfx::GpuFence>)>
                       callback) override {}
  MOCK_METHOD1(SetDisplayTransform, void(gfx::OverlayTransform));
};

class FakeDecoderClient : public DecoderClient {
 public:
  ~FakeDecoderClient() override;
  void OnConsoleMessage(int32_t id, const std::string& message) override;
  void CacheBlob(gpu::GpuDiskCacheType type,
                 const std::string& key,
                 const std::string& shader) override;
  void OnFenceSyncRelease(uint64_t release) override;
  void OnDescheduleUntilFinished() override;
  void OnRescheduleAfterFinished() override;
  void ScheduleGrContextCleanup() override;
  void SetActiveURL(GURL url) override;
  void HandleReturnData(base::span<const uint8_t> data) override;
  bool ShouldYield() override;
};

}  // namespace gpu

#endif  // GPU_COMMAND_BUFFER_CLIENT_CLIENT_TEST_HELPER_H_