chromium/ui/gfx/blit_unittest.cc

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

#include <stdint.h>

#include "base/memory/platform_shared_memory_region.h"
#include "build/build_config.h"
#include "skia/ext/platform_canvas.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/gfx/blit.h"
#include "ui/gfx/geometry/point.h"
#include "ui/gfx/geometry/rect.h"

namespace {

// A TestPixelMap is a thin shim layer that allows writing a square canvas of
// 32-bit pixels as a flat array of 8-bit value, which makes the tests a bit
// less fiddly to read and maintain. At construction time, each 8-bit value is
// used to fill in each of the ARGB channels of the 32-bit pixels.
struct TestPixelMap {};

// Fills the given canvas with the values by duplicating the values into each
// color channel for the corresponding pixel.
//
// Example values = {{0x0, 0x01}, {0x12, 0xFF}} would give a canvas with:
//   0x00000000 0x01010101
//   0x12121212 0xFFFFFFFF
void SetToCanvas(SkCanvas* canvas, const TestPixelMap& values) {}

// Checks each pixel in the given canvas and see if it is made up of the given
// values, where each value has been duplicated into each channel of the given
// bitmap (see SetToCanvas above).
void VerifyCanvasValues(SkCanvas* canvas, const TestPixelMap& values) {}

}  // namespace

TEST(Blit, ScrollCanvas) {}

#if BUILDFLAG(IS_WIN)

TEST(Blit, WithSharedMemory) {
  const int kCanvasWidth = 5;
  const int kCanvasHeight = 5;
  base::subtle::PlatformSharedMemoryRegion section =
      base::subtle::PlatformSharedMemoryRegion::CreateWritable(kCanvasWidth *
                                                               kCanvasHeight);
  ASSERT_TRUE(section.IsValid());
  std::unique_ptr<SkCanvas> canvas =
      skia::CreatePlatformCanvasWithSharedSection(
          kCanvasWidth, kCanvasHeight, false, section.GetPlatformHandle(),
          skia::RETURN_NULL_ON_FAILURE);
  ASSERT_TRUE(canvas);
  // Closes a HANDLE associated with |section|, |canvas| must remain valid.
  section = base::subtle::PlatformSharedMemoryRegion();

  const TestPixelMap initial_values({0x00, 0x01, 0x02, 0x03, 0x04, 0x10, 0x11,
                                     0x12, 0x13, 0x14, 0x20, 0x21, 0x22, 0x23,
                                     0x24, 0x30, 0x31, 0x32, 0x33, 0x34, 0x40,
                                     0x41, 0x42, 0x43, 0x44});
  SetToCanvas(canvas.get(), initial_values);
  VerifyCanvasValues(canvas.get(), initial_values);
}

#endif