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

gpu / command_buffer / service / shared_context_state_unittest.cc [blame]

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

#include "gpu/command_buffer/service/shared_context_state.h"

#include <limits>
#include <memory>
#include <string>
#include <utility>

#include "base/command_line.h"
#include "base/functional/callback_helpers.h"
#include "base/memory/ptr_util.h"
#include "gpu/command_buffer/common/gles2_cmd_utils.h"
#include "gpu/command_buffer/service/context_state_test_helpers.h"
#include "gpu/command_buffer/service/feature_info.h"
#include "gpu/command_buffer/service/test_helper.h"
#include "gpu/config/gpu_driver_bug_workarounds.h"
#include "gpu/config/gpu_feature_info.h"
#include "gpu/config/gpu_preferences.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/gl/gl_context_stub.h"
#include "ui/gl/gl_mock.h"
#include "ui/gl/gl_share_group.h"
#include "ui/gl/gl_surface_stub.h"
#include "ui/gl/init/gl_factory.h"
#include "ui/gl/test/gl_surface_test_support.h"

using ::testing::_;
using ::testing::InSequence;
using ::testing::NiceMock;
using ::testing::Return;
using ::testing::SetArgPointee;
using ::testing::StrictMock;

namespace gpu {

class SharedContextStateTest : public ::testing::Test {
 public:
  SharedContextStateTest() = default;
};

class MockGrContextOptionsProvider
    : public SharedContextState::GrContextOptionsProvider {
 public:
  MOCK_METHOD(void,
              SetCustomGrContextOptions,
              (GrContextOptions & options),
              (const, override));
};

TEST_F(SharedContextStateTest, InitFailsIfLostContext) {
  const ContextType context_type = CONTEXT_TYPE_OPENGLES2;

  // For easier substring/extension matching
  gl::SetGLGetProcAddressProc(gl::MockGLInterface::GetGLProcAddress);
  auto* display = gl::GLSurfaceTestSupport::InitializeOneOffWithMockBindings();
  ASSERT_TRUE(display);
  {
    StrictMock<gl::MockGLInterface> gl_interface;
    gl::MockGLInterface::SetGLInterface(&gl_interface);

    InSequence sequence;

    auto surface = base::MakeRefCounted<gl::GLSurfaceStub>();
    auto context = base::MakeRefCounted<gl::GLContextStub>();
    const char gl_version[] = "OpenGL ES 2.0";
    context->SetGLVersionString(gl_version);
    const char gl_extensions[] = "GL_KHR_robustness";
    context->SetExtensionsString(gl_extensions);
    // The stub ctx needs to be initialized so that the gl::GLContext can
    // store the offscreen stub |surface|.
    context->Initialize(surface.get(), {});

    context->MakeCurrent(surface.get());

    GpuFeatureInfo gpu_feature_info;
    GpuDriverBugWorkarounds workarounds;
    auto feature_info =
        base::MakeRefCounted<gles2::FeatureInfo>(workarounds, gpu_feature_info);
    gles2::TestHelper::SetupFeatureInfoInitExpectationsWithGLVersion(
        &gl_interface, gl_extensions, "ANGLE", gl_version, context_type);
    feature_info->Initialize(gpu::CONTEXT_TYPE_OPENGLES2,
                             false /* passthrough */,
                             gles2::DisallowedFeatures());

    // Setup expectations for SharedContextState::InitializeGL().
    EXPECT_CALL(gl_interface, GetIntegerv(GL_MAX_VERTEX_ATTRIBS, _))
        .WillOnce(SetArgPointee<1>(8u))
        .RetiresOnSaturation();
    EXPECT_CALL(gl_interface,
                GetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, _))
        .WillOnce(SetArgPointee<1>(8u))
        .RetiresOnSaturation();
    ContextStateTestHelpers::SetupInitState(&gl_interface, feature_info.get(),
                                            gfx::Size(1, 1));

    EXPECT_CALL(gl_interface, GetGraphicsResetStatusARB())
        .WillOnce(Return(GL_GUILTY_CONTEXT_RESET_ARB));

    auto shared_context_state = base::MakeRefCounted<SharedContextState>(
        new gl::GLShareGroup(), surface, context,
        false /* use_virtualized_gl_contexts */, base::DoNothing(),
        GrContextType::kGL);

    bool result =
        shared_context_state->InitializeGL(GpuPreferences(), feature_info);
    EXPECT_FALSE(result);
  }
  gl::GLSurfaceTestSupport::ShutdownGL(display);
}

TEST_F(SharedContextStateTest, GLOptionsProviderSetsCustomOptions) {
  gl::SetGLGetProcAddressProc(gl::MockGLInterface::GetGLProcAddress);
  auto* display = gl::GLSurfaceTestSupport::InitializeOneOffWithMockBindings();
  ASSERT_TRUE(display);
  {
    NiceMock<gl::MockGLInterface> gl_interface;
    gl::MockGLInterface::SetGLInterface(&gl_interface);

    InSequence sequence;

    auto share_group = base::MakeRefCounted<gl::GLShareGroup>();
    auto surface = base::MakeRefCounted<gl::GLSurfaceStub>();
    auto context = base::MakeRefCounted<gl::GLContextStub>(share_group.get());
    const char gl_version[] = "OpenGL ES 2.0";
    context->SetGLVersionString(gl_version);
    const char gl_extensions[] = "GL_KHR_robustness";
    context->SetExtensionsString(gl_extensions);

    // The stub ctx needs to be initialized so that the gl::GLContext can
    // store the offscreen stub |surface|.
    context->Initialize(surface.get(), {});
    context->MakeCurrent(surface.get());
    GpuDriverBugWorkarounds workarounds;

    auto shared_context_state = base::MakeRefCounted<SharedContextState>(
        share_group.get(), surface, context,
        false /* use_virtualized_gl_contexts */, base::DoNothing(),
        GrContextType::kGL);
    StrictMock<MockGrContextOptionsProvider> provider;
    shared_context_state->gr_context_options_provider_ = &provider;
    EXPECT_CALL(provider, SetCustomGrContextOptions(_))
        .Times(1)
        .RetiresOnSaturation();
    shared_context_state->InitializeGanesh(
        GpuPreferences(), workarounds, /*cache=*/nullptr,
        /*use_shader_cache_shm_count=*/nullptr, /*progress_reporter=*/nullptr);
  }
  gl::GLSurfaceTestSupport::ShutdownGL(display);
}

TEST_F(SharedContextStateTest, VulkanOptionsProviderSetsCustomOptions) {
  gl::SetGLGetProcAddressProc(gl::MockGLInterface::GetGLProcAddress);
  auto* display = gl::GLSurfaceTestSupport::InitializeOneOffWithMockBindings();
  ASSERT_TRUE(display);
  {
    NiceMock<gl::MockGLInterface> gl_interface;
    gl::MockGLInterface::SetGLInterface(&gl_interface);
    // This line and passing kVulkan into InitializeGanesh should ensure we go
    // down the Vulkan code path.
    gl::SetGLImplementationParts(
        gl::GLImplementationParts(gl::ANGLEImplementation::kVulkan));

    InSequence sequence;

    auto share_group = base::MakeRefCounted<gl::GLShareGroup>();
    auto surface = base::MakeRefCounted<gl::GLSurfaceStub>();
    auto context = base::MakeRefCounted<gl::GLContextStub>(share_group.get());
    const char gl_version[] = "OpenGL ES 2.0";
    context->SetGLVersionString(gl_version);
    const char gl_extensions[] = "GL_KHR_robustness";
    context->SetExtensionsString(gl_extensions);

    // The stub ctx needs to be initialized so that the gl::GLContext can
    // store the offscreen stub |surface|.
    context->Initialize(surface.get(), {});
    context->MakeCurrent(surface.get());
    GpuDriverBugWorkarounds workarounds;

    auto shared_context_state = base::MakeRefCounted<SharedContextState>(
        share_group.get(), surface, context,
        false /* use_virtualized_gl_contexts */, base::DoNothing(),
        GrContextType::kVulkan);
    StrictMock<MockGrContextOptionsProvider> provider;
    shared_context_state->gr_context_options_provider_ = &provider;
    EXPECT_CALL(provider, SetCustomGrContextOptions(_))
        .Times(1)
        .RetiresOnSaturation();
    shared_context_state->InitializeGanesh(
        GpuPreferences(), workarounds, /*cache=*/nullptr,
        /*use_shader_cache_shm_count=*/nullptr, /*progress_reporter=*/nullptr);
  }
  gl::GLSurfaceTestSupport::ShutdownGL(display);
}

}  // namespace gpu