OLD | NEW |
1 // Copyright 2012 The Chromium Authors. All rights reserved. | 1 // Copyright 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 #include "cc/resource_provider.h" | 5 #include "cc/resource_provider.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "cc/output_surface.h" | 8 #include "cc/output_surface.h" |
9 #include "cc/scoped_ptr_deque.h" | 9 #include "cc/scoped_ptr_deque.h" |
10 #include "cc/scoped_ptr_hash_map.h" | 10 #include "cc/scoped_ptr_hash_map.h" |
11 #include "cc/test/fake_output_surface.h" | 11 #include "cc/test/fake_output_surface.h" |
12 #include "cc/test/fake_web_graphics_context_3d.h" | 12 #include "cc/test/fake_web_graphics_context_3d.h" |
13 #include "testing/gmock/include/gmock/gmock.h" | 13 #include "testing/gmock/include/gmock/gmock.h" |
14 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
15 #include "third_party/WebKit/Source/Platform/chromium/public/WebGraphicsContext3
D.h" | 15 #include "third_party/WebKit/Source/Platform/chromium/public/WebGraphicsContext3
D.h" |
16 #include "third_party/khronos/GLES2/gl2.h" | 16 #include "third_party/khronos/GLES2/gl2.h" |
17 #include "third_party/khronos/GLES2/gl2ext.h" | 17 #include "third_party/khronos/GLES2/gl2ext.h" |
18 #include "ui/gfx/rect.h" | 18 #include "ui/gfx/rect.h" |
19 | 19 |
20 using namespace WebKit; | 20 using namespace WebKit; |
21 | 21 |
22 using testing::Mock; | 22 using testing::Mock; |
| 23 using testing::NiceMock; |
| 24 using testing::Return; |
| 25 using testing::StrictMock; |
| 26 using testing::_; |
23 | 27 |
24 namespace cc { | 28 namespace cc { |
25 namespace { | 29 namespace { |
26 | 30 |
27 size_t textureSize(const gfx::Size& size, WGC3Denum format) | 31 size_t textureSize(const gfx::Size& size, WGC3Denum format) |
28 { | 32 { |
29 unsigned int componentsPerPixel = 4; | 33 unsigned int componentsPerPixel = 4; |
30 unsigned int bytesPerComponent = 1; | 34 unsigned int bytesPerComponent = 1; |
31 return size.width() * size.height() * componentsPerPixel * bytesPerComponent
; | 35 return size.width() * size.height() * componentsPerPixel * bytesPerComponent
; |
32 } | 36 } |
(...skipping 543 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
576 | 580 |
577 scoped_ptr<OutputSurface> outputSurface(FakeOutputSurface::Create3d(scoped_p
tr<WebKit::WebGraphicsContext3D>(new TextureStateTrackingContext))); | 581 scoped_ptr<OutputSurface> outputSurface(FakeOutputSurface::Create3d(scoped_p
tr<WebKit::WebGraphicsContext3D>(new TextureStateTrackingContext))); |
578 TextureStateTrackingContext* context = static_cast<TextureStateTrackingConte
xt*>(outputSurface->Context3D()); | 582 TextureStateTrackingContext* context = static_cast<TextureStateTrackingConte
xt*>(outputSurface->Context3D()); |
579 scoped_ptr<ResourceProvider> resourceProvider(ResourceProvider::create(outpu
tSurface.get())); | 583 scoped_ptr<ResourceProvider> resourceProvider(ResourceProvider::create(outpu
tSurface.get())); |
580 | 584 |
581 gfx::Size size(1, 1); | 585 gfx::Size size(1, 1); |
582 WGC3Denum format = GL_RGBA; | 586 WGC3Denum format = GL_RGBA; |
583 int textureId = 1; | 587 int textureId = 1; |
584 | 588 |
585 // Check that the texture gets created with the right sampler settings. | 589 // Check that the texture gets created with the right sampler settings. |
586 EXPECT_CALL(*context, bindTexture(GL_TEXTURE_2D, textureId)); | 590 EXPECT_CALL(*context, bindTexture(GL_TEXTURE_2D, textureId)) |
| 591 .Times(2); // Once to create and once to allocate. |
587 EXPECT_CALL(*context, texParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL
_LINEAR)); | 592 EXPECT_CALL(*context, texParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL
_LINEAR)); |
588 EXPECT_CALL(*context, texParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL
_LINEAR)); | 593 EXPECT_CALL(*context, texParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL
_LINEAR)); |
589 EXPECT_CALL(*context, texParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLA
MP_TO_EDGE)); | 594 EXPECT_CALL(*context, texParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLA
MP_TO_EDGE)); |
590 EXPECT_CALL(*context, texParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLA
MP_TO_EDGE)); | 595 EXPECT_CALL(*context, texParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLA
MP_TO_EDGE)); |
591 EXPECT_CALL(*context, texParameteri(GL_TEXTURE_2D, GL_TEXTURE_POOL_CHROMIUM,
GL_TEXTURE_POOL_UNMANAGED_CHROMIUM)); | 596 EXPECT_CALL(*context, texParameteri(GL_TEXTURE_2D, GL_TEXTURE_POOL_CHROMIUM,
GL_TEXTURE_POOL_UNMANAGED_CHROMIUM)); |
592 ResourceProvider::ResourceId id = resourceProvider->createResource(size, for
mat, ResourceProvider::TextureUsageAny); | 597 ResourceProvider::ResourceId id = resourceProvider->createResource(size, for
mat, ResourceProvider::TextureUsageAny); |
| 598 resourceProvider->allocateForTesting(id); |
593 | 599 |
594 // Creating a sampler with the default filter should not change any texture | 600 // Creating a sampler with the default filter should not change any texture |
595 // parameters. | 601 // parameters. |
596 { | 602 { |
597 EXPECT_CALL(*context, bindTexture(GL_TEXTURE_2D, textureId)); | 603 EXPECT_CALL(*context, bindTexture(GL_TEXTURE_2D, textureId)); |
598 ResourceProvider::ScopedSamplerGL sampler(resourceProvider.get(), id, GL
_TEXTURE_2D, GL_LINEAR); | 604 ResourceProvider::ScopedSamplerGL sampler(resourceProvider.get(), id, GL
_TEXTURE_2D, GL_LINEAR); |
599 } | 605 } |
600 | 606 |
601 // Using a different filter should be reflected in the texture parameters. | 607 // Using a different filter should be reflected in the texture parameters. |
602 { | 608 { |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
636 EXPECT_CALL(*context, texParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL
_LINEAR)); | 642 EXPECT_CALL(*context, texParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL
_LINEAR)); |
637 EXPECT_CALL(*context, texParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL
_LINEAR)); | 643 EXPECT_CALL(*context, texParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL
_LINEAR)); |
638 EXPECT_CALL(*context, texParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLA
MP_TO_EDGE)); | 644 EXPECT_CALL(*context, texParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLA
MP_TO_EDGE)); |
639 EXPECT_CALL(*context, texParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLA
MP_TO_EDGE)); | 645 EXPECT_CALL(*context, texParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLA
MP_TO_EDGE)); |
640 EXPECT_CALL(*context, texParameteri(GL_TEXTURE_2D, GL_TEXTURE_POOL_CHROMIUM,
GL_TEXTURE_POOL_MANAGED_CHROMIUM)); | 646 EXPECT_CALL(*context, texParameteri(GL_TEXTURE_2D, GL_TEXTURE_POOL_CHROMIUM,
GL_TEXTURE_POOL_MANAGED_CHROMIUM)); |
641 ResourceProvider::ResourceId id = resourceProvider->createManagedResource(si
ze, format, ResourceProvider::TextureUsageAny); | 647 ResourceProvider::ResourceId id = resourceProvider->createManagedResource(si
ze, format, ResourceProvider::TextureUsageAny); |
642 | 648 |
643 Mock::VerifyAndClearExpectations(context); | 649 Mock::VerifyAndClearExpectations(context); |
644 } | 650 } |
645 | 651 |
| 652 class AllocationTrackingContext3D : public FakeWebGraphicsContext3D { |
| 653 public: |
| 654 MOCK_METHOD0(createTexture, WebGLId()); |
| 655 MOCK_METHOD1(deleteTexture, void(WebGLId texture_id)); |
| 656 MOCK_METHOD2(bindTexture, void(WGC3Denum target, WebGLId texture)); |
| 657 MOCK_METHOD9(texImage2D, void(WGC3Denum target, WGC3Dint level, WGC3Denum in
ternalformat, |
| 658 WGC3Dsizei width, WGC3Dsizei height, WGC3Dint
border, WGC3Denum format, |
| 659 WGC3Denum type, const void* pixels)); |
| 660 MOCK_METHOD9(texSubImage2D, void(WGC3Denum target, WGC3Dint level, WGC3Dint
xoffset, WGC3Dint yoffset, |
| 661 WGC3Dsizei width, WGC3Dsizei height, WGC3De
num format, |
| 662 WGC3Denum type, const void* pixels)); |
| 663 MOCK_METHOD9(asyncTexImage2DCHROMIUM, void(WGC3Denum target, WGC3Dint level,
WGC3Denum internalformat, |
| 664 WGC3Dsizei width, WGC3Dsizei heigh
t, WGC3Dint border, WGC3Denum format, |
| 665 WGC3Denum type, const void* pixels
)); |
| 666 MOCK_METHOD9(asyncTexSubImage2DCHROMIUM, void(WGC3Denum target, WGC3Dint lev
el, WGC3Dint xoffset, WGC3Dint yoffset, |
| 667 WGC3Dsizei width, WGC3Dsizei h
eight, WGC3Denum format, |
| 668 WGC3Denum type, const void* pi
xels)); |
| 669 }; |
| 670 |
| 671 TEST_P(ResourceProviderTest, TextureAllocation) |
| 672 { |
| 673 // Only for GL textures. |
| 674 if (GetParam() != ResourceProvider::GLTexture) |
| 675 return; |
| 676 scoped_ptr<WebKit::WebGraphicsContext3D> mock_context( |
| 677 static_cast<WebKit::WebGraphicsContext3D*>(new NiceMock<AllocationTracki
ngContext3D>)); |
| 678 scoped_ptr<OutputSurface> outputSurface(FakeOutputSurface::Create3d(mock_con
text.Pass())); |
| 679 |
| 680 gfx::Size size(2, 2); |
| 681 gfx::Vector2d offset(0, 0); |
| 682 gfx::Rect rect(0, 0, 2, 2); |
| 683 WGC3Denum format = GL_RGBA; |
| 684 ResourceProvider::ResourceId id = 0; |
| 685 uint8_t pixels[16] = {0}; |
| 686 int textureId = 123; |
| 687 |
| 688 AllocationTrackingContext3D* context = static_cast<AllocationTrackingContext
3D*>(outputSurface->Context3D()); |
| 689 scoped_ptr<ResourceProvider> resourceProvider(ResourceProvider::create(outpu
tSurface.get())); |
| 690 |
| 691 // Lazy allocation. Don't allocate when creating the resource. |
| 692 EXPECT_CALL(*context, createTexture()).WillOnce(Return(textureId)); |
| 693 EXPECT_CALL(*context, deleteTexture(textureId)).Times(1); |
| 694 EXPECT_CALL(*context, bindTexture(GL_TEXTURE_2D, textureId)).Times(1); |
| 695 EXPECT_CALL(*context, texImage2D(_,_,_,_,_,_,_,_,_)).Times(0); |
| 696 EXPECT_CALL(*context, asyncTexImage2DCHROMIUM(_,_,_,_,_,_,_,_,_)).Times(0); |
| 697 id = resourceProvider->createResource(size, format, ResourceProvider::Textur
eUsageAny); |
| 698 resourceProvider->deleteResource(id); |
| 699 Mock::VerifyAndClearExpectations(context); |
| 700 |
| 701 // Do allocate when we set the pixels. |
| 702 EXPECT_CALL(*context, createTexture()).WillOnce(Return(textureId)); |
| 703 EXPECT_CALL(*context, deleteTexture(textureId)).Times(1); |
| 704 EXPECT_CALL(*context, bindTexture(GL_TEXTURE_2D, textureId)).Times(3); |
| 705 EXPECT_CALL(*context, texImage2D(_,_,_,2,2,_,_,_,_)).Times(1); |
| 706 EXPECT_CALL(*context, texSubImage2D(_,_,_,_,2,2,_,_,_)).Times(1); |
| 707 id = resourceProvider->createResource(size, format, ResourceProvider::Textur
eUsageAny); |
| 708 resourceProvider->setPixels(id, pixels, rect, rect, offset); |
| 709 resourceProvider->deleteResource(id); |
| 710 Mock::VerifyAndClearExpectations(context); |
| 711 |
| 712 // Same for setPixelsFromBuffer |
| 713 EXPECT_CALL(*context, createTexture()).WillOnce(Return(textureId)); |
| 714 EXPECT_CALL(*context, deleteTexture(textureId)).Times(1); |
| 715 EXPECT_CALL(*context, bindTexture(GL_TEXTURE_2D, textureId)).Times(3); |
| 716 EXPECT_CALL(*context, texImage2D(_,_,_,2,2,_,_,_,_)).Times(1); |
| 717 EXPECT_CALL(*context, texSubImage2D(_,_,_,_,2,2,_,_,_)).Times(1); |
| 718 id = resourceProvider->createResource(size, format, ResourceProvider::Textur
eUsageAny); |
| 719 resourceProvider->acquirePixelBuffer(id); |
| 720 resourceProvider->setPixelsFromBuffer(id); |
| 721 resourceProvider->releasePixelBuffer(id); |
| 722 resourceProvider->deleteResource(id); |
| 723 Mock::VerifyAndClearExpectations(context); |
| 724 |
| 725 // Same for async version. |
| 726 EXPECT_CALL(*context, createTexture()).WillOnce(Return(textureId)); |
| 727 EXPECT_CALL(*context, deleteTexture(textureId)).Times(1); |
| 728 EXPECT_CALL(*context, bindTexture(GL_TEXTURE_2D, textureId)).Times(2); |
| 729 EXPECT_CALL(*context, asyncTexImage2DCHROMIUM(_,_,_,2,2,_,_,_,_)).Times(1); |
| 730 id = resourceProvider->createResource(size, format, ResourceProvider::Textur
eUsageAny); |
| 731 resourceProvider->acquirePixelBuffer(id); |
| 732 resourceProvider->beginSetPixels(id); |
| 733 resourceProvider->releasePixelBuffer(id); |
| 734 resourceProvider->deleteResource(id); |
| 735 Mock::VerifyAndClearExpectations(context); |
| 736 } |
| 737 |
646 INSTANTIATE_TEST_CASE_P(ResourceProviderTests, | 738 INSTANTIATE_TEST_CASE_P(ResourceProviderTests, |
647 ResourceProviderTest, | 739 ResourceProviderTest, |
648 ::testing::Values(ResourceProvider::GLTexture, | 740 ::testing::Values(ResourceProvider::GLTexture, |
649 ResourceProvider::Bitmap)); | 741 ResourceProvider::Bitmap)); |
650 | 742 |
651 } // namespace | 743 } // namespace |
652 } // namespace cc | 744 } // namespace cc |
OLD | NEW |