summaryrefslogtreecommitdiffstats
path: root/content/common/gpu/client/gpu_memory_buffer_impl_unittest.cc
blob: 1ab1d99d4b9e168e134e381705fd24104444fa76 (plain)
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
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "content/common/gpu/client/gpu_memory_buffer_impl.h"

#include "base/bind.h"
#include "content/common/gpu/gpu_memory_buffer_factory.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/gfx/buffer_format_util.h"

namespace content {
namespace {

const int kClientId = 1;

class GpuMemoryBufferImplTest
    : public testing::TestWithParam<gfx::GpuMemoryBufferType> {
 public:
  GpuMemoryBufferImplTest() : buffer_count_(0), factory_(nullptr) {}

  // Overridden from testing::Test:
  void SetUp() override {
    factory_ = GpuMemoryBufferFactory::Create(GetParam());
    factory_->GetSupportedGpuMemoryBufferConfigurations(
        &supported_configurations_);
  }
  void TearDown() override { factory_.reset(); }

  gfx::GpuMemoryBufferHandle CreateGpuMemoryBuffer(gfx::GpuMemoryBufferId id,
                                                   const gfx::Size& size,
                                                   gfx::BufferFormat format,
                                                   gfx::BufferUsage usage) {
    ++buffer_count_;
    return factory_->CreateGpuMemoryBuffer(id, size, format, usage, kClientId,
                                           gfx::kNullPluginWindow);
  }

  void DestroyGpuMemoryBuffer(gfx::GpuMemoryBufferId id, uint32 sync_point) {
    factory_->DestroyGpuMemoryBuffer(id, kClientId);
    DCHECK_GT(buffer_count_, 0);
    --buffer_count_;
  }

  std::vector<GpuMemoryBufferFactory::Configuration> supported_configurations_;
  int buffer_count_;

 private:
  scoped_ptr<GpuMemoryBufferFactory> factory_;
};

TEST_P(GpuMemoryBufferImplTest, CreateFromHandle) {
  const int kBufferId = 1;

  gfx::Size buffer_size(8, 8);

  for (auto configuration : supported_configurations_) {
    scoped_ptr<GpuMemoryBufferImpl> buffer(
        GpuMemoryBufferImpl::CreateFromHandle(
            CreateGpuMemoryBuffer(kBufferId, buffer_size, configuration.format,
                                  configuration.usage),
            buffer_size, configuration.format, configuration.usage,
            base::Bind(&GpuMemoryBufferImplTest::DestroyGpuMemoryBuffer,
                       base::Unretained(this), kBufferId)));
    EXPECT_EQ(1, buffer_count_);
    ASSERT_TRUE(buffer);
    EXPECT_EQ(buffer->GetFormat(), configuration.format);

    // Check if destruction callback is executed when deleting the buffer.
    buffer.reset();
    EXPECT_EQ(0, buffer_count_);
  }
}

TEST_P(GpuMemoryBufferImplTest, Map) {
  const int kBufferId = 1;

  // Use a multiple of 4 for both dimensions to support compressed formats.
  gfx::Size buffer_size(4, 4);

  for (auto configuration : supported_configurations_) {
    if (configuration.usage != gfx::BufferUsage::MAP)
      continue;

    scoped_ptr<GpuMemoryBufferImpl> buffer(
        GpuMemoryBufferImpl::CreateFromHandle(
            CreateGpuMemoryBuffer(kBufferId, buffer_size, configuration.format,
                                  configuration.usage),
            buffer_size, configuration.format, configuration.usage,
            base::Bind(&GpuMemoryBufferImplTest::DestroyGpuMemoryBuffer,
                       base::Unretained(this), kBufferId)));
    ASSERT_TRUE(buffer);
    EXPECT_FALSE(buffer->IsMapped());

    size_t num_planes =
        gfx::NumberOfPlanesForBufferFormat(configuration.format);

    // Map buffer into user space.
    scoped_ptr<void*[]> mapped_buffers(new void*[num_planes]);
    bool rv = buffer->Map(mapped_buffers.get());
    ASSERT_TRUE(rv);
    EXPECT_TRUE(buffer->IsMapped());

    // Get strides.
    scoped_ptr<int[]> strides(new int[num_planes]);
    buffer->GetStride(strides.get());

    // Copy and compare mapped buffers.
    for (size_t plane = 0; plane < num_planes; ++plane) {
      size_t row_size_in_bytes = 0;
      EXPECT_TRUE(GpuMemoryBufferImpl::RowSizeInBytes(
          buffer_size.width(), configuration.format, plane,
          &row_size_in_bytes));
      EXPECT_GT(row_size_in_bytes, 0u);

      scoped_ptr<char[]> data(new char[row_size_in_bytes]);
      memset(data.get(), 0x2a + plane, row_size_in_bytes);

      size_t height =
          buffer_size.height() /
          GpuMemoryBufferImpl::SubsamplingFactor(configuration.format, plane);
      for (size_t y = 0; y < height; ++y) {
        memcpy(static_cast<char*>(mapped_buffers[plane]) + y * strides[plane],
               data.get(), row_size_in_bytes);
        EXPECT_EQ(memcmp(static_cast<char*>(mapped_buffers[plane]) +
                             y * strides[plane],
                         data.get(), row_size_in_bytes),
                  0);
      }
    }

    buffer->Unmap();
    EXPECT_FALSE(buffer->IsMapped());
  }
}

TEST_P(GpuMemoryBufferImplTest, PersistentMap) {
  const int kBufferId = 1;

  // Use a multiple of 4 for both dimensions to support compressed formats.
  gfx::Size buffer_size(4, 4);

  for (auto configuration : supported_configurations_) {
    if (configuration.usage != gfx::BufferUsage::PERSISTENT_MAP)
      continue;

    scoped_ptr<GpuMemoryBufferImpl> buffer(
        GpuMemoryBufferImpl::CreateFromHandle(
            CreateGpuMemoryBuffer(kBufferId, buffer_size, configuration.format,
                                  configuration.usage),
            buffer_size, configuration.format, configuration.usage,
            base::Bind(&GpuMemoryBufferImplTest::DestroyGpuMemoryBuffer,
                       base::Unretained(this), kBufferId)));
    ASSERT_TRUE(buffer);
    EXPECT_FALSE(buffer->IsMapped());

    size_t num_planes =
        gfx::NumberOfPlanesForBufferFormat(configuration.format);

    // Map buffer into user space.
    scoped_ptr<void* []> mapped_buffers(new void* [num_planes]);
    bool rv = buffer->Map(mapped_buffers.get());
    ASSERT_TRUE(rv);
    EXPECT_TRUE(buffer->IsMapped());

    // Get strides.
    scoped_ptr<int[]> strides(new int[num_planes]);
    buffer->GetStride(strides.get());

    // Copy and compare mapped buffers.
    for (size_t plane = 0; plane < num_planes; ++plane) {
      size_t row_size_in_bytes;
      EXPECT_TRUE(GpuMemoryBufferImpl::RowSizeInBytes(
          buffer_size.width(), configuration.format, plane,
          &row_size_in_bytes));

      scoped_ptr<char[]> data(new char[row_size_in_bytes]);
      memset(data.get(), 0x2a + plane, row_size_in_bytes);

      size_t height =
          buffer_size.height() /
          GpuMemoryBufferImpl::SubsamplingFactor(configuration.format, plane);
      for (size_t y = 0; y < height; ++y) {
        memcpy(static_cast<char*>(mapped_buffers[plane]) + y * strides[plane],
               data.get(), row_size_in_bytes);
        EXPECT_EQ(memcmp(static_cast<char*>(mapped_buffers[plane]) +
                             y * strides[plane],
                         data.get(), row_size_in_bytes),
                  0);
      }
    }

    buffer->Unmap();
    EXPECT_FALSE(buffer->IsMapped());

    // Remap the buffer, and compare again. It should contain the same data.
    rv = buffer->Map(mapped_buffers.get());
    ASSERT_TRUE(rv);
    EXPECT_TRUE(buffer->IsMapped());

    buffer->GetStride(strides.get());

    for (size_t plane = 0; plane < num_planes; ++plane) {
      size_t row_size_in_bytes;
      EXPECT_TRUE(GpuMemoryBufferImpl::RowSizeInBytes(
          buffer_size.width(), configuration.format, plane,
          &row_size_in_bytes));

      scoped_ptr<char[]> data(new char[row_size_in_bytes]);
      memset(data.get(), 0x2a + plane, row_size_in_bytes);

      size_t height =
          buffer_size.height() /
          GpuMemoryBufferImpl::SubsamplingFactor(configuration.format, plane);
      for (size_t y = 0; y < height; ++y) {
        EXPECT_EQ(memcmp(static_cast<char*>(mapped_buffers[plane]) +
                             y * strides[plane],
                         data.get(), row_size_in_bytes),
                  0);
      }
    }

    buffer->Unmap();
    EXPECT_FALSE(buffer->IsMapped());
  }
}

std::vector<gfx::GpuMemoryBufferType> GetSupportedGpuMemoryBufferTypes() {
  std::vector<gfx::GpuMemoryBufferType> supported_types;
  GpuMemoryBufferFactory::GetSupportedTypes(&supported_types);
  return supported_types;
}

INSTANTIATE_TEST_CASE_P(
    GpuMemoryBufferImplTests,
    GpuMemoryBufferImplTest,
    ::testing::ValuesIn(GetSupportedGpuMemoryBufferTypes()));

}  // namespace
}  // namespace content