| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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/resources/video_resource_updater.h" | 5 #include "cc/resources/video_resource_updater.h" |
| 6 | 6 |
| 7 #include "base/memory/shared_memory.h" | 7 #include "base/memory/shared_memory.h" |
| 8 #include "cc/resources/resource_provider.h" | 8 #include "cc/resources/resource_provider.h" |
| 9 #include "cc/test/fake_output_surface.h" | 9 #include "cc/test/fake_output_surface.h" |
| 10 #include "cc/test/fake_output_surface_client.h" | 10 #include "cc/test/fake_output_surface_client.h" |
| (...skipping 20 matching lines...) Expand all Loading... |
| 31 ++upload_count_; | 31 ++upload_count_; |
| 32 } | 32 } |
| 33 | 33 |
| 34 int UploadCount() { return upload_count_; } | 34 int UploadCount() { return upload_count_; } |
| 35 void ResetUploadCount() { upload_count_ = 0; } | 35 void ResetUploadCount() { upload_count_ = 0; } |
| 36 | 36 |
| 37 private: | 37 private: |
| 38 int upload_count_; | 38 int upload_count_; |
| 39 }; | 39 }; |
| 40 | 40 |
| 41 class SharedBitmapManagerAllocationCounter : public TestSharedBitmapManager { |
| 42 public: |
| 43 scoped_ptr<SharedBitmap> AllocateSharedBitmap( |
| 44 const gfx::Size& size) override { |
| 45 ++allocation_count_; |
| 46 return TestSharedBitmapManager::AllocateSharedBitmap(size); |
| 47 } |
| 48 |
| 49 int AllocationCount() { return allocation_count_; } |
| 50 void ResetAllocationCount() { allocation_count_ = 0; } |
| 51 |
| 52 private: |
| 53 int allocation_count_; |
| 54 }; |
| 55 |
| 41 class VideoResourceUpdaterTest : public testing::Test { | 56 class VideoResourceUpdaterTest : public testing::Test { |
| 42 protected: | 57 protected: |
| 43 VideoResourceUpdaterTest() { | 58 VideoResourceUpdaterTest() { |
| 44 scoped_ptr<WebGraphicsContext3DUploadCounter> context3d( | 59 scoped_ptr<WebGraphicsContext3DUploadCounter> context3d( |
| 45 new WebGraphicsContext3DUploadCounter()); | 60 new WebGraphicsContext3DUploadCounter()); |
| 46 | 61 |
| 47 context3d_ = context3d.get(); | 62 context3d_ = context3d.get(); |
| 48 | 63 |
| 49 output_surface3d_ = | 64 output_surface3d_ = |
| 50 FakeOutputSurface::Create3d(context3d.Pass()); | 65 FakeOutputSurface::Create3d(context3d.Pass()); |
| 51 CHECK(output_surface3d_->BindToClient(&client_)); | 66 CHECK(output_surface3d_->BindToClient(&client_)); |
| 52 shared_bitmap_manager_.reset(new TestSharedBitmapManager()); | 67 |
| 68 output_surface_software_ = FakeOutputSurface::CreateSoftware( |
| 69 make_scoped_ptr(new SoftwareOutputDevice)); |
| 70 CHECK(output_surface_software_->BindToClient(&client_)); |
| 71 |
| 72 shared_bitmap_manager_.reset(new SharedBitmapManagerAllocationCounter()); |
| 53 resource_provider3d_ = | 73 resource_provider3d_ = |
| 54 ResourceProvider::Create(output_surface3d_.get(), | 74 ResourceProvider::Create(output_surface3d_.get(), |
| 55 shared_bitmap_manager_.get(), | 75 shared_bitmap_manager_.get(), |
| 56 NULL, | 76 NULL, |
| 57 NULL, | 77 NULL, |
| 58 0, | 78 0, |
| 59 false, | 79 false, |
| 60 1); | 80 1); |
| 81 |
| 82 resource_provider_software_ = ResourceProvider::Create( |
| 83 output_surface_software_.get(), shared_bitmap_manager_.get(), NULL, |
| 84 NULL, 0, false, 1); |
| 61 } | 85 } |
| 62 | 86 |
| 63 scoped_refptr<media::VideoFrame> CreateTestYUVVideoFrame() { | 87 scoped_refptr<media::VideoFrame> CreateTestYUVVideoFrame() { |
| 64 const int kDimension = 10; | 88 const int kDimension = 10; |
| 65 gfx::Size size(kDimension, kDimension); | 89 gfx::Size size(kDimension, kDimension); |
| 66 static uint8 y_data[kDimension * kDimension] = { 0 }; | 90 static uint8 y_data[kDimension * kDimension] = { 0 }; |
| 67 static uint8 u_data[kDimension * kDimension / 2] = { 0 }; | 91 static uint8 u_data[kDimension * kDimension / 2] = { 0 }; |
| 68 static uint8 v_data[kDimension * kDimension / 2] = { 0 }; | 92 static uint8 v_data[kDimension * kDimension / 2] = { 0 }; |
| 69 | 93 |
| 70 return media::VideoFrame::WrapExternalYuvData( | 94 return media::VideoFrame::WrapExternalYuvData( |
| 71 media::VideoFrame::YV16, // format | 95 media::VideoFrame::YV16, // format |
| 72 size, // coded_size | 96 size, // coded_size |
| 73 gfx::Rect(size), // visible_rect | 97 gfx::Rect(size), // visible_rect |
| 74 size, // natural_size | 98 size, // natural_size |
| 75 size.width(), // y_stride | 99 size.width(), // y_stride |
| 76 size.width() / 2, // u_stride | 100 size.width() / 2, // u_stride |
| 77 size.width() / 2, // v_stride | 101 size.width() / 2, // v_stride |
| 78 y_data, // y_data | 102 y_data, // y_data |
| 79 u_data, // u_data | 103 u_data, // u_data |
| 80 v_data, // v_data | 104 v_data, // v_data |
| 81 base::TimeDelta(), // timestamp, | 105 base::TimeDelta(), // timestamp, |
| 82 base::Closure()); // no_longer_needed_cb | 106 base::Closure()); // no_longer_needed_cb |
| 83 } | 107 } |
| 84 | 108 |
| 85 WebGraphicsContext3DUploadCounter* context3d_; | 109 WebGraphicsContext3DUploadCounter* context3d_; |
| 86 FakeOutputSurfaceClient client_; | 110 FakeOutputSurfaceClient client_; |
| 87 scoped_ptr<FakeOutputSurface> output_surface3d_; | 111 scoped_ptr<FakeOutputSurface> output_surface3d_; |
| 88 scoped_ptr<TestSharedBitmapManager> shared_bitmap_manager_; | 112 scoped_ptr<FakeOutputSurface> output_surface_software_; |
| 113 scoped_ptr<SharedBitmapManagerAllocationCounter> shared_bitmap_manager_; |
| 89 scoped_ptr<ResourceProvider> resource_provider3d_; | 114 scoped_ptr<ResourceProvider> resource_provider3d_; |
| 115 scoped_ptr<ResourceProvider> resource_provider_software_; |
| 90 }; | 116 }; |
| 91 | 117 |
| 92 TEST_F(VideoResourceUpdaterTest, SoftwareFrame) { | 118 TEST_F(VideoResourceUpdaterTest, SoftwareFrame) { |
| 93 VideoResourceUpdater updater(output_surface3d_->context_provider(), | 119 VideoResourceUpdater updater(output_surface3d_->context_provider(), |
| 94 resource_provider3d_.get()); | 120 resource_provider3d_.get()); |
| 95 scoped_refptr<media::VideoFrame> video_frame = CreateTestYUVVideoFrame(); | 121 scoped_refptr<media::VideoFrame> video_frame = CreateTestYUVVideoFrame(); |
| 96 | 122 |
| 97 VideoFrameExternalResources resources = | 123 VideoFrameExternalResources resources = |
| 98 updater.CreateExternalResourcesFromVideoFrame(video_frame); | 124 updater.CreateExternalResourcesFromVideoFrame(video_frame); |
| 99 EXPECT_EQ(VideoFrameExternalResources::YUV_RESOURCE, resources.type); | 125 EXPECT_EQ(VideoFrameExternalResources::YUV_RESOURCE, resources.type); |
| 100 } | 126 } |
| 101 | 127 |
| 102 TEST_F(VideoResourceUpdaterTest, ReuseResource) { | 128 TEST_F(VideoResourceUpdaterTest, ReuseResource) { |
| 103 VideoResourceUpdater updater(output_surface3d_->context_provider(), | 129 VideoResourceUpdater updater(output_surface3d_->context_provider(), |
| 104 resource_provider3d_.get()); | 130 resource_provider3d_.get()); |
| 105 scoped_refptr<media::VideoFrame> video_frame = CreateTestYUVVideoFrame(); | 131 scoped_refptr<media::VideoFrame> video_frame = CreateTestYUVVideoFrame(); |
| 106 video_frame->set_timestamp(base::TimeDelta::FromSeconds(1234)); | 132 video_frame->set_timestamp(base::TimeDelta::FromSeconds(1234)); |
| 107 | 133 |
| 108 // Allocate the resources for a YUV video frame. | 134 // Allocate the resources for a YUV video frame. |
| 109 context3d_->ResetUploadCount(); | 135 context3d_->ResetUploadCount(); |
| 110 VideoFrameExternalResources resources = | 136 VideoFrameExternalResources resources = |
| 111 updater.CreateExternalResourcesFromVideoFrame(video_frame); | 137 updater.CreateExternalResourcesFromVideoFrame(video_frame); |
| 112 EXPECT_EQ(VideoFrameExternalResources::YUV_RESOURCE, resources.type); | 138 EXPECT_EQ(VideoFrameExternalResources::YUV_RESOURCE, resources.type); |
| 113 EXPECT_EQ(size_t(3), resources.mailboxes.size()); | 139 EXPECT_EQ(size_t(3), resources.mailboxes.size()); |
| 114 EXPECT_EQ(size_t(3), resources.release_callbacks.size()); | 140 EXPECT_EQ(size_t(3), resources.release_callbacks.size()); |
| 141 EXPECT_EQ(size_t(0), resources.software_resources.size()); |
| 115 // Expect exactly three texture uploads, one for each plane. | 142 // Expect exactly three texture uploads, one for each plane. |
| 116 EXPECT_EQ(3, context3d_->UploadCount()); | 143 EXPECT_EQ(3, context3d_->UploadCount()); |
| 117 | 144 |
| 118 // Simulate the ResourceProvider releasing the resources back to the video | 145 // Simulate the ResourceProvider releasing the resources back to the video |
| 119 // updater. | 146 // updater. |
| 120 for (ReleaseCallbackImpl& release_callback : resources.release_callbacks) | 147 for (ReleaseCallbackImpl& release_callback : resources.release_callbacks) |
| 121 release_callback.Run(0, false, nullptr); | 148 release_callback.Run(0, false, nullptr); |
| 122 | 149 |
| 123 // Allocate resources for the same frame. | 150 // Allocate resources for the same frame. |
| 124 context3d_->ResetUploadCount(); | 151 context3d_->ResetUploadCount(); |
| (...skipping 11 matching lines...) Expand all Loading... |
| 136 scoped_refptr<media::VideoFrame> video_frame = CreateTestYUVVideoFrame(); | 163 scoped_refptr<media::VideoFrame> video_frame = CreateTestYUVVideoFrame(); |
| 137 video_frame->set_timestamp(base::TimeDelta::FromSeconds(1234)); | 164 video_frame->set_timestamp(base::TimeDelta::FromSeconds(1234)); |
| 138 | 165 |
| 139 // Allocate the resources for a YUV video frame. | 166 // Allocate the resources for a YUV video frame. |
| 140 context3d_->ResetUploadCount(); | 167 context3d_->ResetUploadCount(); |
| 141 VideoFrameExternalResources resources = | 168 VideoFrameExternalResources resources = |
| 142 updater.CreateExternalResourcesFromVideoFrame(video_frame); | 169 updater.CreateExternalResourcesFromVideoFrame(video_frame); |
| 143 EXPECT_EQ(VideoFrameExternalResources::YUV_RESOURCE, resources.type); | 170 EXPECT_EQ(VideoFrameExternalResources::YUV_RESOURCE, resources.type); |
| 144 EXPECT_EQ(size_t(3), resources.mailboxes.size()); | 171 EXPECT_EQ(size_t(3), resources.mailboxes.size()); |
| 145 EXPECT_EQ(size_t(3), resources.release_callbacks.size()); | 172 EXPECT_EQ(size_t(3), resources.release_callbacks.size()); |
| 173 EXPECT_EQ(size_t(0), resources.software_resources.size()); |
| 146 // Expect exactly three texture uploads, one for each plane. | 174 // Expect exactly three texture uploads, one for each plane. |
| 147 EXPECT_EQ(3, context3d_->UploadCount()); | 175 EXPECT_EQ(3, context3d_->UploadCount()); |
| 148 | 176 |
| 149 // Allocate resources for the same frame. | 177 // Allocate resources for the same frame. |
| 150 context3d_->ResetUploadCount(); | 178 context3d_->ResetUploadCount(); |
| 151 resources = updater.CreateExternalResourcesFromVideoFrame(video_frame); | 179 resources = updater.CreateExternalResourcesFromVideoFrame(video_frame); |
| 152 EXPECT_EQ(VideoFrameExternalResources::YUV_RESOURCE, resources.type); | 180 EXPECT_EQ(VideoFrameExternalResources::YUV_RESOURCE, resources.type); |
| 153 EXPECT_EQ(size_t(3), resources.mailboxes.size()); | 181 EXPECT_EQ(size_t(3), resources.mailboxes.size()); |
| 154 EXPECT_EQ(size_t(3), resources.release_callbacks.size()); | 182 EXPECT_EQ(size_t(3), resources.release_callbacks.size()); |
| 155 // The data should be reused so expect no texture uploads. | 183 // The data should be reused so expect no texture uploads. |
| 156 EXPECT_EQ(0, context3d_->UploadCount()); | 184 EXPECT_EQ(0, context3d_->UploadCount()); |
| 157 } | 185 } |
| 158 | 186 |
| 187 TEST_F(VideoResourceUpdaterTest, SoftwareFrameSoftwareCompositor) { |
| 188 VideoResourceUpdater updater(nullptr, resource_provider_software_.get()); |
| 189 scoped_refptr<media::VideoFrame> video_frame = CreateTestYUVVideoFrame(); |
| 190 |
| 191 VideoFrameExternalResources resources = |
| 192 updater.CreateExternalResourcesFromVideoFrame(video_frame); |
| 193 EXPECT_EQ(VideoFrameExternalResources::SOFTWARE_RESOURCE, resources.type); |
| 194 } |
| 195 |
| 196 TEST_F(VideoResourceUpdaterTest, ReuseResourceSoftwareCompositor) { |
| 197 VideoResourceUpdater updater(nullptr, resource_provider_software_.get()); |
| 198 scoped_refptr<media::VideoFrame> video_frame = CreateTestYUVVideoFrame(); |
| 199 video_frame->set_timestamp(base::TimeDelta::FromSeconds(1234)); |
| 200 |
| 201 // Allocate the resources for a software video frame. |
| 202 shared_bitmap_manager_->ResetAllocationCount(); |
| 203 VideoFrameExternalResources resources = |
| 204 updater.CreateExternalResourcesFromVideoFrame(video_frame); |
| 205 EXPECT_EQ(VideoFrameExternalResources::SOFTWARE_RESOURCE, resources.type); |
| 206 EXPECT_EQ(size_t(0), resources.mailboxes.size()); |
| 207 EXPECT_EQ(size_t(0), resources.release_callbacks.size()); |
| 208 EXPECT_EQ(size_t(1), resources.software_resources.size()); |
| 209 // Expect exactly one allocated shared bitmap. |
| 210 EXPECT_EQ(1, shared_bitmap_manager_->AllocationCount()); |
| 211 |
| 212 // Simulate the ResourceProvider releasing the resource back to the video |
| 213 // updater. |
| 214 resources.software_release_callback.Run(0, false, nullptr); |
| 215 |
| 216 // Allocate resources for the same frame. |
| 217 shared_bitmap_manager_->ResetAllocationCount(); |
| 218 resources = updater.CreateExternalResourcesFromVideoFrame(video_frame); |
| 219 EXPECT_EQ(VideoFrameExternalResources::SOFTWARE_RESOURCE, resources.type); |
| 220 EXPECT_EQ(size_t(0), resources.mailboxes.size()); |
| 221 EXPECT_EQ(size_t(0), resources.release_callbacks.size()); |
| 222 EXPECT_EQ(size_t(1), resources.software_resources.size()); |
| 223 // The data should be reused so expect no new allocations. |
| 224 EXPECT_EQ(0, shared_bitmap_manager_->AllocationCount()); |
| 225 } |
| 226 |
| 227 TEST_F(VideoResourceUpdaterTest, ReuseResourceNoDeleteSoftwareCompositor) { |
| 228 VideoResourceUpdater updater(nullptr, resource_provider_software_.get()); |
| 229 scoped_refptr<media::VideoFrame> video_frame = CreateTestYUVVideoFrame(); |
| 230 video_frame->set_timestamp(base::TimeDelta::FromSeconds(1234)); |
| 231 |
| 232 // Allocate the resources for a software video frame. |
| 233 shared_bitmap_manager_->ResetAllocationCount(); |
| 234 VideoFrameExternalResources resources = |
| 235 updater.CreateExternalResourcesFromVideoFrame(video_frame); |
| 236 EXPECT_EQ(VideoFrameExternalResources::SOFTWARE_RESOURCE, resources.type); |
| 237 EXPECT_EQ(size_t(0), resources.mailboxes.size()); |
| 238 EXPECT_EQ(size_t(0), resources.release_callbacks.size()); |
| 239 EXPECT_EQ(size_t(1), resources.software_resources.size()); |
| 240 // Expect exactly one allocated shared bitmap. |
| 241 EXPECT_EQ(1, shared_bitmap_manager_->AllocationCount()); |
| 242 |
| 243 // Allocate resources for the same frame. |
| 244 shared_bitmap_manager_->ResetAllocationCount(); |
| 245 resources = updater.CreateExternalResourcesFromVideoFrame(video_frame); |
| 246 EXPECT_EQ(VideoFrameExternalResources::SOFTWARE_RESOURCE, resources.type); |
| 247 EXPECT_EQ(size_t(0), resources.mailboxes.size()); |
| 248 EXPECT_EQ(size_t(0), resources.release_callbacks.size()); |
| 249 EXPECT_EQ(size_t(1), resources.software_resources.size()); |
| 250 // The data should be reused so expect no new allocations. |
| 251 EXPECT_EQ(0, shared_bitmap_manager_->AllocationCount()); |
| 252 } |
| 253 |
| 159 } // namespace | 254 } // namespace |
| 160 } // namespace cc | 255 } // namespace cc |
| OLD | NEW |