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
  179
  180
  181
  182
  183
  184
  185
  186
  187
  188
  189
  190
  191
  192
  193
  194
  195
  196
  197
  198
  199
  200
  201
  202
  203
  204
  205
  206
  207
  208
  209
  210

gpu / command_buffer / service / mocks.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.

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

// This file contains definitions for mock objects, used for testing.

// TODO(apatrick): This file "manually" defines some mock objects. Using gMock
// would be definitely preferable, unfortunately it doesn't work on Windows yet.

#ifndef GPU_COMMAND_BUFFER_SERVICE_MOCKS_H_
#define GPU_COMMAND_BUFFER_SERVICE_MOCKS_H_

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

#include <string>
#include <string_view>
#include <vector>

#include "base/memory/raw_ptr.h"
#include "gpu/command_buffer/common/cmd_buffer_common.h"
#include "gpu/command_buffer/service/async_api_interface.h"
#include "gpu/command_buffer/service/decoder_client.h"
#include "gpu/command_buffer/service/isolation_key_provider.h"
#include "gpu/command_buffer/service/memory_tracking.h"
#include "gpu/command_buffer/service/program_cache.h"
#include "gpu/command_buffer/service/shader_translator.h"
#include "testing/gmock/include/gmock/gmock.h"

namespace gpu {

class CommandBufferDirect;
class CommandBufferServiceBase;

// Mocks an AsyncAPIInterface, using GMock.
class AsyncAPIMock : public AsyncAPIInterface {
 public:
  explicit AsyncAPIMock(bool default_do_commands,
                        CommandBufferDirect* command_buffer,
                        CommandBufferServiceBase* command_buffer_service);
  ~AsyncAPIMock() override;

  error::Error FakeDoCommands(unsigned int num_commands,
                              const volatile void* buffer,
                              int num_entries,
                              int* entries_processed);

  // Predicate that matches args passed to DoCommand, by looking at the values.
  class IsArgs {
   public:
    IsArgs(unsigned int arg_count, const volatile void* args)
        : arg_count_(arg_count),
          args_(static_cast<volatile CommandBufferEntry*>(
              const_cast<volatile void*>(args))) {}

    bool operator()(const volatile void* _args) const {
      const volatile CommandBufferEntry* args =
          static_cast<const volatile CommandBufferEntry*>(_args) + 1;
      for (unsigned int i = 0; i < arg_count_; ++i) {
        if (args[i].value_uint32 != args_[i].value_uint32) return false;
      }
      return true;
    }

   private:
    unsigned int arg_count_;
    raw_ptr<volatile CommandBufferEntry, DanglingUntriaged | AllowPtrArithmetic>
        args_;
  };

  void BeginDecoding() override {}
  void EndDecoding() override {}

  MOCK_METHOD3(DoCommand,
               error::Error(unsigned int command,
                            unsigned int arg_count,
                            const volatile void* cmd_data));

  MOCK_METHOD4(DoCommands,
               error::Error(unsigned int num_commands,
                            const volatile void* buffer,
                            int num_entries,
                            int* entries_processed));

  std::string_view GetLogPrefix() override { return "None"; }

  // Forwards the SetToken commands to the engine.
  void SetToken(unsigned int command,
                unsigned int arg_count,
                const volatile void* _args);

 private:
  raw_ptr<CommandBufferDirect> command_buffer_;
  raw_ptr<CommandBufferServiceBase, DanglingUntriaged> command_buffer_service_;
};

class MockDecoderClient : public DecoderClient {
 public:
  MockDecoderClient();
  ~MockDecoderClient() override;

  MOCK_METHOD(void, OnConsoleMessage, (int32_t id, const std::string& message));
  MOCK_METHOD(void, OnGpuSwitched, (gl::GpuPreference active_gpu_heuristic));
  MOCK_METHOD(void,
              CacheBlob,
              (gpu::GpuDiskCacheType type,
               const std::string& key,
               const std::string& shader));
  MOCK_METHOD(void, OnFenceSyncRelease, (uint64_t release));
  MOCK_METHOD(void, OnDescheduleUntilFinished, ());
  MOCK_METHOD(void, OnRescheduleAfterFinished, ());
  MOCK_METHOD(void, ScheduleGrContextCleanup, ());
  MOCK_METHOD(void, SetActiveURL, (GURL url));
  MOCK_METHOD(void, HandleReturnData, (base::span<const uint8_t> data));
  MOCK_METHOD(bool, ShouldYield, ());
};

class MockIsolationKeyProvider : public IsolationKeyProvider {
 public:
  MockIsolationKeyProvider();
  ~MockIsolationKeyProvider() override;

  MOCK_METHOD(void,
              GetIsolationKey,
              (const blink::WebGPUExecutionContextToken& token,
               GetIsolationKeyCallback cb),
              (override));
};

namespace gles2 {

class MockShaderTranslator : public ShaderTranslatorInterface {
 public:
  MockShaderTranslator();

  MOCK_METHOD6(Init,
               bool(sh::GLenum shader_type,
                    ShShaderSpec shader_spec,
                    const ShBuiltInResources* resources,
                    ShShaderOutput shader_output_language,
                    const ShCompileOptions& driver_bug_workarounds,
                    bool gl_shader_interm_output));
  MOCK_CONST_METHOD9(Translate,
                     bool(const std::string& shader_source,
                          std::string* info_log,
                          std::string* translated_source,
                          int* shader_version,
                          AttributeMap* attrib_map,
                          UniformMap* uniform_map,
                          VaryingMap* varying_map,
                          InterfaceBlockMap* interface_block_map,
                          OutputVariableList* output_variable_list));
  MOCK_CONST_METHOD0(GetStringForOptionsThatWouldAffectCompilation,
                     OptionsAffectingCompilationString*());

 private:
  ~MockShaderTranslator() override;
};

class MockProgramCache : public ProgramCache {
 public:
  MockProgramCache();
  ~MockProgramCache() override;

  MOCK_METHOD7(LoadLinkedProgram,
               ProgramLoadResult(
                   GLuint program,
                   Shader* shader_a,
                   Shader* shader_b,
                   const LocationMap* bind_attrib_location_map,
                   const std::vector<std::string>& transform_feedback_varyings,
                   GLenum transform_feedback_buffer_mode,
                   DecoderClient* client));

  MOCK_METHOD7(SaveLinkedProgram,
               void(GLuint program,
                    const Shader* shader_a,
                    const Shader* shader_b,
                    const LocationMap* bind_attrib_location_map,
                    const std::vector<std::string>& transform_feedback_varyings,
                    GLenum transform_feedback_buffer_mode,
                    DecoderClient* client));
  MOCK_METHOD2(LoadProgram, void(const std::string&, const std::string&));
  MOCK_METHOD1(Trim, size_t(size_t));

 private:
  MOCK_METHOD0(ClearBackend, void());
};

class MockMemoryTracker : public MemoryTracker {
 public:
  MockMemoryTracker();
  ~MockMemoryTracker() override;

  MOCK_METHOD1(TrackMemoryAllocatedChange, void(int64_t delta));
  uint64_t GetSize() const override { return 0; }
  MOCK_CONST_METHOD0(ClientTracingId, uint64_t());
  MOCK_CONST_METHOD0(ClientId, int());
  MOCK_CONST_METHOD0(ContextGroupTracingId, uint64_t());
};

}  // namespace gles2
}  // namespace gpu

#endif  // GPU_COMMAND_BUFFER_SERVICE_MOCKS_H_