File: TestMacIOSurfaceHelpers.cpp

package info (click to toggle)
firefox 149.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 4,767,760 kB
  • sloc: cpp: 7,416,064; javascript: 6,752,859; ansic: 3,774,850; python: 1,250,473; xml: 641,578; asm: 439,191; java: 186,617; sh: 56,634; makefile: 18,856; objc: 13,092; perl: 12,763; pascal: 5,960; yacc: 4,583; cs: 3,846; lex: 1,720; ruby: 1,002; php: 436; lisp: 258; awk: 105; sql: 66; sed: 53; csh: 10; exp: 6
file content (93 lines) | stat: -rw-r--r-- 3,687 bytes parent folder | download
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
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

// Tests for MacIOSurfaceHelpers: verifies that CPU readback of MacIOSurfaces
// via CreateSourceSurfaceFromMacIOSurface produces correct BGRA pixel values
// for 10-bit biplanar formats (P010, NV16).

#include "gtest/gtest.h"
#include "mozilla/gfx/2D.h"
#include "mozilla/gfx/MacIOSurface.h"
#include "MacIOSurfaceHelpers.h"

using namespace mozilla;
using namespace mozilla::gfx;
using namespace mozilla::layers;

static void FillBiPlanarSurface(MacIOSurface* aSurface, uint16_t aYVal,
                                uint16_t aCbCrVal) {
  uint16_t* yPlane =
      reinterpret_cast<uint16_t*>(aSurface->GetBaseAddressOfPlane(0));
  size_t yStride = aSurface->GetBytesPerRow(0) / sizeof(uint16_t);
  for (size_t y = 0; y < aSurface->GetDevicePixelHeight(0); y++) {
    for (size_t x = 0; x < aSurface->GetDevicePixelWidth(0); x++) {
      yPlane[y * yStride + x] = aYVal;
    }
  }

  uint16_t* cbcrPlane =
      reinterpret_cast<uint16_t*>(aSurface->GetBaseAddressOfPlane(1));
  size_t cbcrStride = aSurface->GetBytesPerRow(1) / sizeof(uint16_t);
  for (size_t y = 0; y < aSurface->GetDevicePixelHeight(1); y++) {
    for (size_t x = 0; x < aSurface->GetDevicePixelWidth(1); x++) {
      cbcrPlane[y * cbcrStride + x * 2] = aCbCrVal;
      cbcrPlane[y * cbcrStride + x * 2 + 1] = aCbCrVal;
    }
  }
}

static void TestBiPlanarMidGrayReadback(const IntSize& aYSize,
                                        const IntSize& aCbCrSize,
                                        ChromaSubsampling aSubsampling) {
  RefPtr<MacIOSurface> surface = MacIOSurface::CreateBiPlanarSurface(
      aYSize, aCbCrSize, aSubsampling, YUVColorSpace::BT709,
      TransferFunction::BT709, ColorRange::FULL, ColorDepth::COLOR_10);
  ASSERT_TRUE(surface);

  // Mid-gray in 10-bit full range: Y=Cb=Cr=512, stored in bits 15:6.
  ASSERT_TRUE(surface->Lock(false));
  FillBiPlanarSurface(surface, uint16_t(512) << 6, uint16_t(512) << 6);
  surface->Unlock();

  RefPtr<SourceSurface> sourceSurface =
      CreateSourceSurfaceFromMacIOSurface(surface);
  ASSERT_TRUE(sourceSurface);
  ASSERT_EQ(sourceSurface->GetSize(), aYSize);

  RefPtr<DataSourceSurface> dataSurface = sourceSurface->GetDataSurface();
  ASSERT_TRUE(dataSurface);

  DataSourceSurface::ScopedMap map(dataSurface, DataSourceSurface::READ);
  ASSERT_TRUE(map.IsMapped());

  uint8_t* pixels = map.GetData();
  const int32_t stride = map.GetStride();
  for (int y = 0; y < aYSize.height; y++) {
    for (int x = 0; x < aYSize.width; x++) {
      EXPECT_NEAR(pixels[y * stride + x * 4 + 0], 128, 3)
          << "Pixel (" << x << "," << y << ")";
      EXPECT_NEAR(pixels[y * stride + x * 4 + 1], 128, 3)
          << "Pixel (" << x << "," << y << ")";
      EXPECT_NEAR(pixels[y * stride + x * 4 + 2], 128, 3)
          << "Pixel (" << x << "," << y << ")";
    }
  }
}

// Tests that a P010 (10-bit 4:2:0 biplanar) IOSurface filled with mid-gray
// produces the correct BGRA output via CreateSourceSurfaceFromMacIOSurface.
TEST(MacIOSurfaceHelpers, P010Readback)
{
  ASSERT_NO_FATAL_FAILURE(TestBiPlanarMidGrayReadback(
      IntSize(16, 16), IntSize(8, 8),
      ChromaSubsampling::HALF_WIDTH_AND_HEIGHT));
}

// Tests that an NV16 (10-bit 4:2:2 biplanar) IOSurface filled with mid-gray
// produces the correct BGRA output via CreateSourceSurfaceFromMacIOSurface.
TEST(MacIOSurfaceHelpers, NV16Readback)
{
  ASSERT_NO_FATAL_FAILURE(TestBiPlanarMidGrayReadback(
      IntSize(16, 16), IntSize(8, 16), ChromaSubsampling::HALF_WIDTH));
}