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
  211
  212
  213
  214
  215
  216
  217
  218
  219
  220
  221
  222
  223
  224
  225
  226
  227
  228
  229
  230
  231
  232
  233
  234
  235
  236
  237
  238
  239
  240
  241
  242
  243
  244
  245
  246
  247
  248
  249
  250
  251
  252
  253
  254
  255
  256
  257
  258
  259
  260
  261
  262
  263
  264
  265
  266
  267
  268
  269
  270
  271
  272
  273
  274
  275
  276
  277
  278
  279
  280
  281
  282
  283
  284
  285
  286
  287
  288
  289
  290
  291
  292
  293
  294
  295
  296
  297
  298
  299
  300
  301
  302
  303
  304
  305
  306
  307
  308
  309
  310
  311
  312
  313
  314
  315
  316
  317
  318
  319
  320
  321
  322
  323
  324
  325
  326
  327
  328
  329
  330
  331
  332
  333
  334
  335
  336
  337
  338
  339
  340
  341
  342
  343
  344
  345
  346
  347
  348
  349
  350
  351
  352
  353
  354
  355
  356
  357
  358
  359
  360
  361
  362
  363
  364
  365
  366
  367
  368
  369
  370
  371
  372
  373
  374
  375
  376
  377
  378
  379
  380
  381

cc / test / pixel_test.cc [blame]

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

#include "cc/test/pixel_test.h"

#include <memory>
#include <utility>

#include "base/command_line.h"
#include "base/feature_list.h"
#include "base/functional/bind.h"
#include "base/functional/callback_helpers.h"
#include "base/memory/read_only_shared_memory_region.h"
#include "base/memory/shared_memory_mapping.h"
#include "base/path_service.h"
#include "base/run_loop.h"
#include "base/test/test_switches.h"
#include "build/build_config.h"
#include "cc/test/fake_output_surface_client.h"
#include "cc/test/pixel_test_output_surface.h"
#include "cc/test/pixel_test_utils.h"
#include "components/viz/client/client_resource_provider.h"
#include "components/viz/common/frame_sinks/copy_output_request.h"
#include "components/viz/common/frame_sinks/copy_output_result.h"
#include "components/viz/common/resources/shared_bitmap.h"
#include "components/viz/service/display/display_resource_provider_skia.h"
#include "components/viz/service/display/display_resource_provider_software.h"
#include "components/viz/service/display/software_output_device.h"
#include "components/viz/service/display/software_renderer.h"
#include "components/viz/service/display_embedder/skia_output_surface_dependency_impl.h"
#include "components/viz/service/display_embedder/skia_output_surface_impl.h"
#include "components/viz/service/gl/gpu_service_impl.h"
#include "components/viz/test/paths.h"
#include "components/viz/test/test_in_process_context_provider.h"
#include "gpu/command_buffer/service/gpu_switches.h"
#include "gpu/config/gpu_finch_features.h"
#include "skia/buildflags.h"
#include "testing/gtest/include/gtest/gtest.h"

#if BUILDFLAG(SKIA_USE_DAWN)
#include "third_party/dawn/include/dawn/dawn_proc.h"
#include "third_party/dawn/include/dawn/native/DawnNative.h"  // nogncheck
#endif

namespace cc {
namespace {
void DeleteSharedImage(scoped_refptr<gpu::ClientSharedImage> shared_image,
                       const gpu::SyncToken& sync_token,
                       bool is_lost) {
  shared_image->UpdateDestructionSyncToken(sync_token);
}
}  // namespace

PixelTest::PixelTest(GraphicsBackend backend)
    : device_viewport_size_(gfx::Size(200, 200)),
      output_surface_client_(std::make_unique<FakeOutputSurfaceClient>()),
      graphics_backend_(backend) {
  // Keep texture sizes exactly matching the bounds of the RenderPass to avoid
  // floating point badness in texcoords.
  renderer_settings_.dont_round_texture_sizes_for_pixel_tests = true;

  // Copy requests force full damage, but OutputSurface-based readback can test
  // incremental damage cases.
  renderer_settings_.partial_swap_enabled = true;

  // Check if the graphics backend needs to initialize Vulkan, Dawn.
  bool init_vulkan = false;
  bool init_dawn = false;
  if (backend == kSkiaVulkan) {
    scoped_feature_list_.InitAndEnableFeature(features::kVulkan);
    init_vulkan = true;
  } else if (backend == kSkiaGraphiteDawn) {
    scoped_feature_list_.InitAndEnableFeature(features::kSkiaGraphite);
    auto* command_line = base::CommandLine::ForCurrentProcess();
    bool use_gpu = command_line->HasSwitch(::switches::kUseGpuInTests);
    // Force the use of Graphite even if disallowed for other reasons e.g.
    // ANGLE Metal is not enabled on Mac. Use dawn-swiftshader backend if
    // kUseGpuInTests is not set.
    command_line->AppendSwitch(::switches::kEnableSkiaGraphite);
    command_line->AppendSwitchASCII(
        ::switches::kSkiaGraphiteBackend,
        use_gpu ? ::switches::kSkiaGraphiteBackendDawn
                : ::switches::kSkiaGraphiteBackendDawnSwiftshader);
    init_dawn = true;
#if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)
    init_vulkan = true;
#endif
  } else if (backend == kSkiaGraphiteMetal) {
    scoped_feature_list_.InitAndEnableFeature(features::kSkiaGraphite);
    auto* command_line = base::CommandLine::ForCurrentProcess();
    // Force the use of Graphite even if disallowed for other reasons.
    command_line->AppendSwitch(::switches::kEnableSkiaGraphite);
    command_line->AppendSwitchASCII(::switches::kSkiaGraphiteBackend,
                                    ::switches::kSkiaGraphiteBackendMetal);
  } else {
    // Ensure that we don't accidentally have vulkan or graphite enabled.
    scoped_feature_list_.InitWithFeatures(
        /*enabled_features=*/{},
        /*disabled_features=*/{features::kVulkan, features::kSkiaGraphite});
  }

  if (init_vulkan) {
    auto* command_line = base::CommandLine::ForCurrentProcess();
    bool use_gpu = command_line->HasSwitch(::switches::kUseGpuInTests);
    command_line->AppendSwitchASCII(
        ::switches::kUseVulkan,
        use_gpu ? ::switches::kVulkanImplementationNameNative
                : ::switches::kVulkanImplementationNameSwiftshader);
  }

  if (init_dawn) {
#if BUILDFLAG(SKIA_USE_DAWN)
    dawnProcSetProcs(&dawn::native::GetProcs());
#endif
  }
}

PixelTest::~PixelTest() = default;

void PixelTest::RenderReadbackTargetAndAreaToResultBitmap(
    viz::AggregatedRenderPassList* pass_list,
    viz::AggregatedRenderPass* target,
    const gfx::Rect* copy_rect) {
  base::RunLoop run_loop;

  const bool use_copy_request = target != pass_list->back().get();
  if (use_copy_request) {
    std::unique_ptr<viz::CopyOutputRequest> request =
        std::make_unique<viz::CopyOutputRequest>(
            viz::CopyOutputRequest::ResultFormat::RGBA,
            viz::CopyOutputRequest::ResultDestination::kSystemMemory,
            base::BindOnce(&PixelTest::ReadbackResult, base::Unretained(this),
                           run_loop.QuitClosure()));
    if (copy_rect) {
      request->set_area(*copy_rect);
    }
    target->copy_requests.push_back(std::move(request));
  }

  float device_scale_factor = 1.f;
  renderer_->DrawFrame(pass_list, device_scale_factor, device_viewport_size_,
                       display_color_spaces_,
                       std::move(surface_damage_rect_list_));

  if (use_copy_request) {
    // Call SwapBuffersSkipped(), so the renderer can have a chance to release
    // resources.
    renderer_->SwapBuffersSkipped();
  } else {
    renderer_->SwapBuffers(viz::DirectRenderer::SwapFrameData());
    output_surface_->ReadbackForTesting(
        base::BindOnce(&PixelTest::ReadbackResult, base::Unretained(this),
                       run_loop.QuitClosure()));
  }

  // Wait for the readback to complete.
  run_loop.Run();
}

bool PixelTest::RunPixelTest(viz::AggregatedRenderPassList* pass_list,
                             const base::FilePath& ref_file,
                             const PixelComparator& comparator) {
  return RunPixelTestWithCopyOutputRequest(pass_list, pass_list->back().get(),
                                           ref_file, comparator);
}

bool PixelTest::RunPixelTestWithCopyOutputRequest(
    viz::AggregatedRenderPassList* pass_list,
    viz::AggregatedRenderPass* target,
    const base::FilePath& ref_file,
    const PixelComparator& comparator) {
  return RunPixelTestWithCopyOutputRequestAndArea(pass_list, target, ref_file,
                                                  comparator, nullptr);
}

bool PixelTest::RunPixelTestWithCopyOutputRequestAndArea(
    viz::AggregatedRenderPassList* pass_list,
    viz::AggregatedRenderPass* target,
    const base::FilePath& ref_file,
    const PixelComparator& comparator,
    const gfx::Rect* copy_rect) {
  RenderReadbackTargetAndAreaToResultBitmap(pass_list, target, copy_rect);

  base::FilePath test_data_dir;
  if (!base::PathService::Get(viz::Paths::DIR_TEST_DATA, &test_data_dir)) {
    return false;
  }

  // If this is false, we didn't set up a readback on a render pass.
  if (!result_bitmap_) {
    return false;
  }

  base::CommandLine* cmd = base::CommandLine::ForCurrentProcess();
  if (cmd->HasSwitch(switches::kRebaselinePixelTests)) {
    return WritePNGFile(*result_bitmap_, test_data_dir.Append(ref_file), true);
  }

  return MatchesPNGFile(*result_bitmap_, test_data_dir.Append(ref_file),
                        comparator);
}

bool PixelTest::RunPixelTest(viz::AggregatedRenderPassList* pass_list,
                             std::vector<SkColor>* ref_pixels,
                             const PixelComparator& comparator) {
  RenderReadbackTargetAndAreaToResultBitmap(pass_list, pass_list->back().get(),
                                            nullptr);

  // Need to wrap |ref_pixels| in a SkBitmap.
  DCHECK_EQ(ref_pixels->size(), static_cast<size_t>(result_bitmap_->width() *
                                                    result_bitmap_->height()));
  SkBitmap ref_pixels_bitmap;
  ref_pixels_bitmap.installPixels(
      SkImageInfo::MakeN32Premul(result_bitmap_->width(),
                                 result_bitmap_->height()),
      ref_pixels->data(), result_bitmap_->width() * sizeof(SkColor));
  bool result = comparator.Compare(*result_bitmap_, ref_pixels_bitmap);
  if (!result) {
    std::string res_bmp_data_url = GetPNGDataUrl(*result_bitmap_);
    std::string ref_bmp_data_url = GetPNGDataUrl(ref_pixels_bitmap);
    LOG(ERROR) << "Pixels do not match!";
    LOG(ERROR) << "Actual: " << res_bmp_data_url;
    LOG(ERROR) << "Expected: " << ref_bmp_data_url;
  }
  return result;
}

bool PixelTest::RunPixelTest(viz::AggregatedRenderPassList* pass_list,
                             SkBitmap ref_bitmap,
                             const PixelComparator& comparator) {
  RenderReadbackTargetAndAreaToResultBitmap(pass_list, pass_list->back().get(),
                                            nullptr);

  bool result = comparator.Compare(*result_bitmap_, ref_bitmap);
  if (!result) {
    std::string res_bmp_data_url = GetPNGDataUrl(*result_bitmap_);
    std::string ref_bmp_data_url = GetPNGDataUrl(ref_bitmap);
    LOG(ERROR) << "Pixels do not match!";
    LOG(ERROR) << "Actual: " << res_bmp_data_url;
    LOG(ERROR) << "Expected: " << ref_bmp_data_url;
  }
  return result;
}

void PixelTest::ReadbackResult(base::OnceClosure quit_run_loop,
                               std::unique_ptr<viz::CopyOutputResult> result) {
  ASSERT_FALSE(result->IsEmpty());
  EXPECT_EQ(result->format(), viz::CopyOutputResult::Format::RGBA);
  EXPECT_EQ(result->destination(),
            viz::CopyOutputResult::Destination::kSystemMemory);
  auto scoped_sk_bitmap = result->ScopedAccessSkBitmap();
  result_bitmap_ =
      std::make_unique<SkBitmap>(scoped_sk_bitmap.GetOutScopedBitmap());
  EXPECT_TRUE(result_bitmap_->readyToDraw());
  std::move(quit_run_loop).Run();
}

void PixelTest::AllocateSharedBitmapMemory(
    scoped_refptr<viz::RasterContextProvider> context_provider,
    const gfx::Size& size,
    scoped_refptr<gpu::ClientSharedImage>& shared_image,
    base::WritableSharedMemoryMapping& mapping,
    gpu::SyncToken& sync_token) {
  DCHECK(context_provider);
  auto* shared_image_interface = context_provider->SharedImageInterface();
  DCHECK(shared_image_interface);
  auto shared_image_mapping = shared_image_interface->CreateSharedImage(
      {viz::SinglePlaneFormat::kBGRA_8888, size, gfx::ColorSpace(),
       gpu::SHARED_IMAGE_USAGE_CPU_WRITE_ONLY, "PixelTestSharedBitmap"});

  shared_image = std::move(shared_image_mapping.shared_image);
  mapping = std::move(shared_image_mapping.mapping);
  sync_token = shared_image_interface->GenVerifiedSyncToken();
  CHECK(shared_image);
}

viz::ResourceId PixelTest::AllocateAndFillSoftwareResource(
    scoped_refptr<viz::RasterContextProvider> context_provider,
    const gfx::Size& size,
    const SkBitmap& source) {
  scoped_refptr<gpu::ClientSharedImage> shared_image;
  base::WritableSharedMemoryMapping mapping;
  gpu::SyncToken sync_token;
  AllocateSharedBitmapMemory(context_provider, size, shared_image, mapping,
                             sync_token);

  SkImageInfo info = SkImageInfo::MakeN32Premul(size.width(), size.height());
  const size_t row_bytes = info.minRowBytes();
  base::span<uint8_t> mem(mapping);
  CHECK_GE(mem.size(), info.computeByteSize(row_bytes));
  source.readPixels(info, mem.data(), row_bytes, 0, 0);

  auto transferable_resource =
      viz::TransferableResource::MakeSoftwareSharedImage(
          shared_image, sync_token, size, viz::SinglePlaneFormat::kBGRA_8888,
          viz::TransferableResource::ResourceSource::kTileRasterTask);
  auto release_callback =
      base::BindOnce(&DeleteSharedImage, std::move(shared_image));

  return child_resource_provider_->ImportResource(
      std::move(transferable_resource), std::move(release_callback));
}

void PixelTest::SetUpSkiaRenderer(gfx::SurfaceOrigin output_surface_origin) {
  enable_pixel_output_ = std::make_unique<gl::DisableNullDrawGLBindings>();
  // Set up the GPU service.
  gpu_service_holder_ = viz::TestGpuServiceHolder::GetInstance();

  auto skia_deps = std::make_unique<viz::SkiaOutputSurfaceDependencyImpl>(
      gpu_service(), gpu::kNullSurfaceHandle);
  display_controller_ =
      std::make_unique<viz::DisplayCompositorMemoryAndTaskController>(
          std::move(skia_deps));
  output_surface_ = viz::SkiaOutputSurfaceImpl::Create(
      display_controller_.get(), renderer_settings_, &debug_settings_);
  output_surface_->BindToClient(output_surface_client_.get());
  static_cast<viz::SkiaOutputSurfaceImpl*>(output_surface_.get())
      ->SetCapabilitiesForTesting(output_surface_origin);
  auto resource_provider = std::make_unique<viz::DisplayResourceProviderSkia>();
  renderer_ = std::make_unique<viz::SkiaRenderer>(
      &renderer_settings_, &debug_settings_, output_surface_.get(),
      resource_provider.get(), nullptr,
      static_cast<viz::SkiaOutputSurface*>(output_surface_.get()));
  resource_provider_ = std::move(resource_provider);

  FinishSetup();
}

void PixelTest::TearDown() {
  // Tear down the client side context provider, etc.
  child_resource_provider_->ShutdownAndReleaseAllResources();
  child_resource_provider_.reset();
  child_context_provider_.reset();

  // Tear down the skia renderer.
  software_renderer_ = nullptr;
  renderer_.reset();
  resource_provider_.reset();
  output_surface_.reset();
}

void PixelTest::SetUpSoftwareRenderer() {
  // Set up the GPU service. It's only used for shared bitmaps but it's still
  // needed.
  gpu_service_holder_ = viz::TestGpuServiceHolder::GetInstance();

  output_surface_ = std::make_unique<PixelTestOutputSurface>(
      std::make_unique<viz::SoftwareOutputDevice>());
  output_surface_->BindToClient(output_surface_client_.get());

  auto* gpu_service = gpu_service_holder_->gpu_service();
  auto resource_provider =
      std::make_unique<viz::DisplayResourceProviderSoftware>(
          /*shared_bitmap_manager=*/nullptr,
          gpu_service->shared_image_manager(), gpu_service->gpu_scheduler());

  auto renderer = std::make_unique<viz::SoftwareRenderer>(
      &renderer_settings_, &debug_settings_, output_surface_.get(),
      resource_provider.get(), nullptr);
  resource_provider_ = std::move(resource_provider);
  software_renderer_ = renderer.get();
  renderer_ = std::move(renderer);

  FinishSetup();
}

void PixelTest::FinishSetup() {
  CHECK(renderer_);
  renderer_->Initialize();
  renderer_->SetVisible(true);

  child_context_provider_ =
      base::MakeRefCounted<viz::TestInProcessContextProvider>(
          viz::TestContextType::kSoftwareRaster, /*support_locking=*/false);
  gpu::ContextResult result = child_context_provider_->BindToCurrentSequence();
  CHECK_EQ(result, gpu::ContextResult::kSuccess);
  child_resource_provider_ = std::make_unique<viz::ClientResourceProvider>();
}

}  // namespace cc