OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // This file contains the tests for the RingBuffer class. | 5 // This file contains the tests for the RingBuffer class. |
6 | 6 |
7 #include "gpu/command_buffer/client/ring_buffer.h" | 7 #include "gpu/command_buffer/client/ring_buffer.h" |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/bind_helpers.h" | 10 #include "base/bind_helpers.h" |
(...skipping 186 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
197 allocator_->FreePendingToken(pointer, helper_.get()->InsertToken()); | 197 allocator_->FreePendingToken(pointer, helper_.get()->InsertToken()); |
198 pointer = allocator_->Alloc(kAlloc2); | 198 pointer = allocator_->Alloc(kAlloc2); |
199 EXPECT_EQ(kBufferSize - kAlloc1 - kAlloc2, | 199 EXPECT_EQ(kBufferSize - kAlloc1 - kAlloc2, |
200 allocator_->GetLargestFreeSizeNoWaiting()); | 200 allocator_->GetLargestFreeSizeNoWaiting()); |
201 allocator_->FreePendingToken(pointer, helper_.get()->InsertToken()); | 201 allocator_->FreePendingToken(pointer, helper_.get()->InsertToken()); |
202 pointer = allocator_->Alloc(kBufferSize); | 202 pointer = allocator_->Alloc(kBufferSize); |
203 EXPECT_EQ(0u, allocator_->GetLargestFreeSizeNoWaiting()); | 203 EXPECT_EQ(0u, allocator_->GetLargestFreeSizeNoWaiting()); |
204 allocator_->FreePendingToken(pointer, helper_.get()->InsertToken()); | 204 allocator_->FreePendingToken(pointer, helper_.get()->InsertToken()); |
205 } | 205 } |
206 | 206 |
| 207 // Test that discarding a single allocation clears the block. |
| 208 TEST_F(RingBufferTest, DiscardTest) { |
| 209 const unsigned int kAlloc1 = 3*kAlignment; |
| 210 void* ptr = allocator_->Alloc(kAlloc1); |
| 211 EXPECT_EQ(kBufferSize - kAlloc1, allocator_->GetLargestFreeSizeNoWaiting()); |
| 212 allocator_->DiscardBlock(ptr); |
| 213 EXPECT_EQ(kBufferSize, allocator_->GetLargestFreeSizeNoWaiting()); |
| 214 } |
| 215 |
| 216 // Test that discarding front of the buffer effectively frees the block. |
| 217 TEST_F(RingBufferTest, DiscardFrontTest) { |
| 218 const unsigned int kAlloc1 = 3*kAlignment; |
| 219 const unsigned int kAlloc2 = 2*kAlignment; |
| 220 const unsigned int kAlloc3 = kBufferSize - kAlloc1 - kAlloc2; |
| 221 void* ptr1 = allocator_->Alloc(kAlloc1); |
| 222 EXPECT_EQ(kBufferSize - kAlloc1, allocator_->GetLargestFreeSizeNoWaiting()); |
| 223 allocator_->FreePendingToken(ptr1, helper_.get()->InsertToken()); |
| 224 |
| 225 void* ptr2 = allocator_->Alloc(kAlloc2); |
| 226 EXPECT_EQ(static_cast<uint8_t*>(ptr1) + kAlloc1, |
| 227 static_cast<uint8_t*>(ptr2)); |
| 228 EXPECT_EQ(kBufferSize - kAlloc1 - kAlloc2, |
| 229 allocator_->GetLargestFreeSizeNoWaiting()); |
| 230 allocator_->FreePendingToken(ptr2, helper_.get()->InsertToken()); |
| 231 |
| 232 void* ptr3 = allocator_->Alloc(kAlloc3); |
| 233 EXPECT_EQ(static_cast<uint8_t*>(ptr2) + kAlloc2, |
| 234 static_cast<uint8_t*>(ptr3)); |
| 235 EXPECT_EQ(0u, allocator_->GetLargestFreeSizeNoWaiting()); |
| 236 |
| 237 // Discard first block should free it up upon GetLargestFreeSizeNoWaiting(). |
| 238 allocator_->DiscardBlock(ptr1); |
| 239 EXPECT_EQ(kAlloc1, allocator_->GetLargestFreeSizeNoWaiting()); |
| 240 allocator_->FreePendingToken(ptr3, helper_.get()->InsertToken()); |
| 241 } |
| 242 |
| 243 // Test that discarding middle of the buffer merely marks it as padding. |
| 244 TEST_F(RingBufferTest, DiscardMiddleTest) { |
| 245 const unsigned int kAlloc1 = 3*kAlignment; |
| 246 const unsigned int kAlloc2 = 2*kAlignment; |
| 247 const unsigned int kAlloc3 = kBufferSize - kAlloc1 - kAlloc2; |
| 248 void* ptr1 = allocator_->Alloc(kAlloc1); |
| 249 EXPECT_EQ(kBufferSize - kAlloc1, allocator_->GetLargestFreeSizeNoWaiting()); |
| 250 allocator_->FreePendingToken(ptr1, helper_.get()->InsertToken()); |
| 251 |
| 252 void* ptr2 = allocator_->Alloc(kAlloc2); |
| 253 EXPECT_EQ(static_cast<uint8_t*>(ptr1) + kAlloc1, |
| 254 static_cast<uint8_t*>(ptr2)); |
| 255 EXPECT_EQ(kBufferSize - kAlloc1 - kAlloc2, |
| 256 allocator_->GetLargestFreeSizeNoWaiting()); |
| 257 allocator_->FreePendingToken(ptr2, helper_.get()->InsertToken()); |
| 258 |
| 259 void* ptr3 = allocator_->Alloc(kAlloc3); |
| 260 EXPECT_EQ(static_cast<uint8_t*>(ptr2) + kAlloc2, |
| 261 static_cast<uint8_t*>(ptr3)); |
| 262 EXPECT_EQ(0u, allocator_->GetLargestFreeSizeNoWaiting()); |
| 263 |
| 264 // Discard middle block should just set it as padding. |
| 265 allocator_->DiscardBlock(ptr2); |
| 266 EXPECT_EQ(0u, allocator_->GetLargestFreeSizeNoWaiting()); |
| 267 allocator_->FreePendingToken(ptr3, helper_.get()->InsertToken()); |
| 268 } |
| 269 |
| 270 // Test that discarding end of the buffer frees it for no waiting. |
| 271 TEST_F(RingBufferTest, DiscardEndTest) { |
| 272 const unsigned int kAlloc1 = 3*kAlignment; |
| 273 const unsigned int kAlloc2 = 2*kAlignment; |
| 274 const unsigned int kAlloc3 = kBufferSize - kAlloc1 - kAlloc2; |
| 275 void* ptr1 = allocator_->Alloc(kAlloc1); |
| 276 EXPECT_EQ(kBufferSize - kAlloc1, allocator_->GetLargestFreeSizeNoWaiting()); |
| 277 allocator_->FreePendingToken(ptr1, helper_.get()->InsertToken()); |
| 278 |
| 279 void* ptr2 = allocator_->Alloc(kAlloc2); |
| 280 EXPECT_EQ(static_cast<uint8_t*>(ptr1) + kAlloc1, |
| 281 static_cast<uint8_t*>(ptr2)); |
| 282 EXPECT_EQ(kBufferSize - kAlloc1 - kAlloc2, |
| 283 allocator_->GetLargestFreeSizeNoWaiting()); |
| 284 allocator_->FreePendingToken(ptr2, helper_.get()->InsertToken()); |
| 285 |
| 286 void* ptr3 = allocator_->Alloc(kAlloc3); |
| 287 EXPECT_EQ(static_cast<uint8_t*>(ptr2) + kAlloc2, |
| 288 static_cast<uint8_t*>(ptr3)); |
| 289 EXPECT_EQ(0u, allocator_->GetLargestFreeSizeNoWaiting()); |
| 290 |
| 291 // Discard end block should discard it. |
| 292 allocator_->DiscardBlock(ptr3); |
| 293 EXPECT_EQ(kAlloc3, allocator_->GetLargestFreeSizeNoWaiting()); |
| 294 } |
| 295 |
| 296 // Test discard end of the buffer that has looped around. |
| 297 TEST_F(RingBufferTest, DiscardLoopedEndTest) { |
| 298 const unsigned int kAlloc1 = 3*kAlignment; |
| 299 const unsigned int kAlloc2 = 2*kAlignment; |
| 300 const unsigned int kAlloc3 = kBufferSize - kAlloc1 - kAlloc2; |
| 301 void* ptr1 = allocator_->Alloc(kAlloc1); |
| 302 EXPECT_EQ(kBufferSize - kAlloc1, allocator_->GetLargestFreeSizeNoWaiting()); |
| 303 allocator_->FreePendingToken(ptr1, helper_.get()->InsertToken()); |
| 304 |
| 305 void* ptr2 = allocator_->Alloc(kAlloc2); |
| 306 EXPECT_EQ(static_cast<uint8_t*>(ptr1) + kAlloc1, |
| 307 static_cast<uint8_t*>(ptr2)); |
| 308 EXPECT_EQ(kBufferSize - kAlloc1 - kAlloc2, |
| 309 allocator_->GetLargestFreeSizeNoWaiting()); |
| 310 allocator_->FreePendingToken(ptr2, helper_.get()->InsertToken()); |
| 311 |
| 312 void* ptr3 = allocator_->Alloc(kAlloc3); |
| 313 EXPECT_EQ(static_cast<uint8_t*>(ptr2) + kAlloc2, |
| 314 static_cast<uint8_t*>(ptr3)); |
| 315 EXPECT_EQ(0u, allocator_->GetLargestFreeSizeNoWaiting()); |
| 316 allocator_->FreePendingToken(ptr3, helper_.get()->InsertToken()); |
| 317 |
| 318 // This allocation should be at the beginning again, we need to utilize |
| 319 // DiscardBlock here to discard the first item so that we can allocate |
| 320 // at the beginning without the FreeOldestBlock() getting called and freeing |
| 321 // the whole ring buffer. |
| 322 allocator_->DiscardBlock(ptr1); |
| 323 void* ptr4 = allocator_->Alloc(kAlloc1); |
| 324 EXPECT_EQ(ptr1, ptr4); |
| 325 EXPECT_EQ(0u, allocator_->GetLargestFreeSizeNoWaiting()); |
| 326 |
| 327 // Discard end block should work properly still. |
| 328 allocator_->DiscardBlock(ptr4); |
| 329 EXPECT_EQ(kAlloc1, allocator_->GetLargestFreeSizeNoWaiting()); |
| 330 } |
| 331 |
| 332 // Test discard end of the buffer that has looped around with padding. |
| 333 TEST_F(RingBufferTest, DiscardEndWithPaddingTest) { |
| 334 const unsigned int kAlloc1 = 3*kAlignment; |
| 335 const unsigned int kAlloc2 = 2*kAlignment; |
| 336 const unsigned int kPadding = kAlignment; |
| 337 const unsigned int kAlloc3 = kBufferSize - kAlloc1 - kAlloc2 - kPadding; |
| 338 void* ptr1 = allocator_->Alloc(kAlloc1); |
| 339 EXPECT_EQ(kBufferSize - kAlloc1, allocator_->GetLargestFreeSizeNoWaiting()); |
| 340 allocator_->FreePendingToken(ptr1, helper_.get()->InsertToken()); |
| 341 |
| 342 void* ptr2 = allocator_->Alloc(kAlloc2); |
| 343 EXPECT_EQ(static_cast<uint8_t*>(ptr1) + kAlloc1, |
| 344 static_cast<uint8_t*>(ptr2)); |
| 345 EXPECT_EQ(kBufferSize - kAlloc1 - kAlloc2, |
| 346 allocator_->GetLargestFreeSizeNoWaiting()); |
| 347 allocator_->FreePendingToken(ptr2, helper_.get()->InsertToken()); |
| 348 |
| 349 void* ptr3 = allocator_->Alloc(kAlloc3); |
| 350 EXPECT_EQ(static_cast<uint8_t*>(ptr2) + kAlloc2, |
| 351 static_cast<uint8_t*>(ptr3)); |
| 352 EXPECT_EQ(kPadding, allocator_->GetLargestFreeSizeNoWaiting()); |
| 353 allocator_->FreePendingToken(ptr3, helper_.get()->InsertToken()); |
| 354 |
| 355 // Cause it to loop around with padding at the end of ptr3. |
| 356 allocator_->DiscardBlock(ptr1); |
| 357 void* ptr4 = allocator_->Alloc(kAlloc1); |
| 358 EXPECT_EQ(ptr1, ptr4); |
| 359 EXPECT_EQ(0u, allocator_->GetLargestFreeSizeNoWaiting()); |
| 360 |
| 361 // Discard end block should also discard the padding. |
| 362 allocator_->DiscardBlock(ptr4); |
| 363 EXPECT_EQ(kAlloc1, allocator_->GetLargestFreeSizeNoWaiting()); |
| 364 |
| 365 // We can test that there is padding by attempting to allocate the padding. |
| 366 void* padding = allocator_->Alloc(kPadding); |
| 367 EXPECT_EQ(kAlloc1, allocator_->GetLargestFreeSizeNoWaiting()); |
| 368 allocator_->FreePendingToken(padding, helper_.get()->InsertToken()); |
| 369 } |
| 370 |
| 371 // Test that discard will effectively remove all padding at the end. |
| 372 TEST_F(RingBufferTest, DiscardAllPaddingFromEndTest) { |
| 373 const unsigned int kAlloc1 = 3*kAlignment; |
| 374 const unsigned int kAlloc2 = 2*kAlignment; |
| 375 const unsigned int kAlloc3 = kBufferSize - kAlloc1 - kAlloc2; |
| 376 void* ptr1 = allocator_->Alloc(kAlloc1); |
| 377 EXPECT_EQ(kBufferSize - kAlloc1, allocator_->GetLargestFreeSizeNoWaiting()); |
| 378 allocator_->FreePendingToken(ptr1, helper_.get()->InsertToken()); |
| 379 |
| 380 void* ptr2 = allocator_->Alloc(kAlloc2); |
| 381 EXPECT_EQ(static_cast<uint8_t*>(ptr1) + kAlloc1, |
| 382 static_cast<uint8_t*>(ptr2)); |
| 383 EXPECT_EQ(kBufferSize - kAlloc1 - kAlloc2, |
| 384 allocator_->GetLargestFreeSizeNoWaiting()); |
| 385 allocator_->FreePendingToken(ptr2, helper_.get()->InsertToken()); |
| 386 |
| 387 void* ptr3 = allocator_->Alloc(kAlloc3); |
| 388 EXPECT_EQ(static_cast<uint8_t*>(ptr2) + kAlloc2, |
| 389 static_cast<uint8_t*>(ptr3)); |
| 390 EXPECT_EQ(0u, allocator_->GetLargestFreeSizeNoWaiting()); |
| 391 |
| 392 // Discarding the middle allocation should turn it into padding. |
| 393 allocator_->DiscardBlock(ptr2); |
| 394 EXPECT_EQ(0u, allocator_->GetLargestFreeSizeNoWaiting()); |
| 395 |
| 396 // Discarding the last allocation should discard the middle padding as well. |
| 397 allocator_->DiscardBlock(ptr3); |
| 398 EXPECT_EQ(kAlloc2 + kAlloc3, allocator_->GetLargestFreeSizeNoWaiting()); |
| 399 } |
| 400 |
| 401 // Test that discard will effectively remove all padding from the beginning. |
| 402 TEST_F(RingBufferTest, DiscardAllPaddingFromBeginningTest) { |
| 403 const unsigned int kAlloc1 = 3*kAlignment; |
| 404 const unsigned int kAlloc2 = 2*kAlignment; |
| 405 const unsigned int kAlloc3 = kBufferSize - kAlloc1 - kAlloc2; |
| 406 void* ptr1 = allocator_->Alloc(kAlloc1); |
| 407 EXPECT_EQ(kBufferSize - kAlloc1, allocator_->GetLargestFreeSizeNoWaiting()); |
| 408 allocator_->FreePendingToken(ptr1, helper_.get()->InsertToken()); |
| 409 |
| 410 void* ptr2 = allocator_->Alloc(kAlloc2); |
| 411 EXPECT_EQ(static_cast<uint8_t*>(ptr1) + kAlloc1, |
| 412 static_cast<uint8_t*>(ptr2)); |
| 413 EXPECT_EQ(kBufferSize - kAlloc1 - kAlloc2, |
| 414 allocator_->GetLargestFreeSizeNoWaiting()); |
| 415 allocator_->FreePendingToken(ptr2, helper_.get()->InsertToken()); |
| 416 |
| 417 void* ptr3 = allocator_->Alloc(kAlloc3); |
| 418 EXPECT_EQ(static_cast<uint8_t*>(ptr2) + kAlloc2, |
| 419 static_cast<uint8_t*>(ptr3)); |
| 420 EXPECT_EQ(0u, allocator_->GetLargestFreeSizeNoWaiting()); |
| 421 allocator_->FreePendingToken(ptr3, helper_.get()->InsertToken()); |
| 422 |
| 423 // Discarding the middle allocation should turn it into padding. |
| 424 allocator_->DiscardBlock(ptr2); |
| 425 EXPECT_EQ(0u, allocator_->GetLargestFreeSizeNoWaiting()); |
| 426 |
| 427 // Discarding the first allocation should discard the middle padding as well. |
| 428 allocator_->DiscardBlock(ptr1); |
| 429 EXPECT_EQ(kAlloc1 + kAlloc2, allocator_->GetLargestFreeSizeNoWaiting()); |
| 430 } |
| 431 |
207 } // namespace gpu | 432 } // namespace gpu |
OLD | NEW |