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 "gpu/command_buffer/service/mailbox_manager.h" | 5 #include "gpu/command_buffer/service/mailbox_manager.h" |
6 | 6 |
| 7 #include "gpu/command_buffer/service/feature_info.h" |
| 8 #include "gpu/command_buffer/service/mailbox_synchronizer.h" |
7 #include "gpu/command_buffer/service/texture_manager.h" | 9 #include "gpu/command_buffer/service/texture_manager.h" |
8 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
| 11 #include "ui/gl/gl_mock.h" |
9 | 12 |
10 namespace gpu { | 13 namespace gpu { |
11 namespace gles2 { | 14 namespace gles2 { |
12 | 15 |
| 16 using namespace ::testing; |
| 17 |
13 class MailboxManagerTest : public testing::Test { | 18 class MailboxManagerTest : public testing::Test { |
14 public: | 19 public: |
15 MailboxManagerTest() : manager_(new MailboxManager()) {} | 20 MailboxManagerTest() {} |
16 virtual ~MailboxManagerTest() {} | 21 virtual ~MailboxManagerTest() {} |
17 | 22 |
18 protected: | 23 protected: |
| 24 virtual void SetUp() { |
| 25 testing::Test::SetUp(); |
| 26 feature_info_ = new FeatureInfo; |
| 27 manager_ = new MailboxManager; |
| 28 } |
| 29 |
19 Texture* CreateTexture() { | 30 Texture* CreateTexture() { |
20 return new Texture(0); | 31 return new Texture(1); |
| 32 } |
| 33 |
| 34 void SetTarget(Texture* texture, GLenum target, GLuint max_level) { |
| 35 texture->SetTarget(NULL, target, max_level); |
| 36 } |
| 37 |
| 38 void SetLevelInfo( |
| 39 Texture* texture, |
| 40 GLenum target, |
| 41 GLint level, |
| 42 GLenum internal_format, |
| 43 GLsizei width, |
| 44 GLsizei height, |
| 45 GLsizei depth, |
| 46 GLint border, |
| 47 GLenum format, |
| 48 GLenum type, |
| 49 bool cleared) { |
| 50 texture->SetLevelInfo(NULL, |
| 51 target, |
| 52 level, |
| 53 internal_format, |
| 54 width, |
| 55 height, |
| 56 depth, |
| 57 border, |
| 58 format, |
| 59 type, |
| 60 cleared); |
| 61 } |
| 62 |
| 63 GLenum SetParameter(Texture* texture, GLenum pname, GLint param) { |
| 64 return texture->SetParameter(feature_info_, pname, param); |
21 } | 65 } |
22 | 66 |
23 void DestroyTexture(Texture* texture) { | 67 void DestroyTexture(Texture* texture) { |
24 delete texture; | 68 delete texture; |
25 } | 69 } |
26 | 70 |
27 scoped_refptr<MailboxManager> manager_; | 71 scoped_refptr<MailboxManager> manager_; |
28 | 72 |
29 private: | 73 private: |
| 74 scoped_refptr<FeatureInfo> feature_info_; |
| 75 |
30 DISALLOW_COPY_AND_ASSIGN(MailboxManagerTest); | 76 DISALLOW_COPY_AND_ASSIGN(MailboxManagerTest); |
31 }; | 77 }; |
32 | 78 |
33 // Tests basic produce/consume behavior. | 79 // Tests basic produce/consume behavior. |
34 TEST_F(MailboxManagerTest, Basic) { | 80 TEST_F(MailboxManagerTest, Basic) { |
35 Texture* texture = CreateTexture(); | 81 Texture* texture = CreateTexture(); |
36 | 82 |
37 Mailbox name = Mailbox::Generate(); | 83 Mailbox name = Mailbox::Generate(); |
38 manager_->ProduceTexture(0, name, texture); | 84 manager_->ProduceTexture(0, name, texture); |
39 EXPECT_EQ(texture, manager_->ConsumeTexture(0, name)); | 85 EXPECT_EQ(texture, manager_->ConsumeTexture(0, name)); |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
119 | 165 |
120 // Destroy texture1, shouldn't affect name2. | 166 // Destroy texture1, shouldn't affect name2. |
121 DestroyTexture(texture1); | 167 DestroyTexture(texture1); |
122 EXPECT_EQ(NULL, manager_->ConsumeTexture(0, name1)); | 168 EXPECT_EQ(NULL, manager_->ConsumeTexture(0, name1)); |
123 EXPECT_EQ(texture2, manager_->ConsumeTexture(0, name2)); | 169 EXPECT_EQ(texture2, manager_->ConsumeTexture(0, name2)); |
124 | 170 |
125 DestroyTexture(texture2); | 171 DestroyTexture(texture2); |
126 EXPECT_EQ(NULL, manager_->ConsumeTexture(0, name2)); | 172 EXPECT_EQ(NULL, manager_->ConsumeTexture(0, name2)); |
127 } | 173 } |
128 | 174 |
| 175 const GLsizei kMaxTextureWidth = 64; |
| 176 const GLsizei kMaxTextureHeight = 64; |
| 177 const GLsizei kMaxTextureDepth = 1; |
| 178 |
| 179 class MailboxManagerSyncTest : public MailboxManagerTest { |
| 180 public: |
| 181 MailboxManagerSyncTest() {} |
| 182 virtual ~MailboxManagerSyncTest() {} |
| 183 |
| 184 protected: |
| 185 virtual void SetUp() { |
| 186 MailboxSynchronizer::Initialize(); |
| 187 MailboxManagerTest::SetUp(); |
| 188 manager2_ = new MailboxManager; |
| 189 gl_.reset(new ::testing::StrictMock< ::gfx::MockGLInterface>()); |
| 190 ::gfx::MockGLInterface::SetGLInterface(gl_.get()); |
| 191 } |
| 192 |
| 193 Texture* DefineTexture() { |
| 194 Texture* texture = CreateTexture(); |
| 195 const GLsizei levels_needed = TextureManager::ComputeMipMapCount( |
| 196 GL_TEXTURE_2D, kMaxTextureWidth, kMaxTextureHeight, kMaxTextureDepth); |
| 197 SetTarget(texture, GL_TEXTURE_2D, levels_needed); |
| 198 SetLevelInfo(texture, |
| 199 GL_TEXTURE_2D, |
| 200 0, |
| 201 GL_RGBA, |
| 202 1, |
| 203 1, |
| 204 1, |
| 205 0, |
| 206 GL_RGBA, |
| 207 GL_UNSIGNED_BYTE, |
| 208 true); |
| 209 SetParameter(texture, GL_TEXTURE_MIN_FILTER, GL_LINEAR); |
| 210 SetParameter(texture, GL_TEXTURE_MAG_FILTER, GL_LINEAR); |
| 211 return texture; |
| 212 } |
| 213 |
| 214 void SetupUpdateTexParamExpectations(GLuint texture_id, |
| 215 GLenum min, |
| 216 GLenum mag, |
| 217 GLenum wrap_s, |
| 218 GLenum wrap_t) { |
| 219 DCHECK(texture_id); |
| 220 const GLuint kCurrentTexture = 0; |
| 221 EXPECT_CALL(*gl_, GetIntegerv(GL_TEXTURE_BINDING_2D, _)) |
| 222 .WillOnce(SetArgPointee<1>(kCurrentTexture)) |
| 223 .RetiresOnSaturation(); |
| 224 EXPECT_CALL(*gl_, BindTexture(GL_TEXTURE_2D, texture_id)) |
| 225 .Times(1) |
| 226 .RetiresOnSaturation(); |
| 227 EXPECT_CALL(*gl_, |
| 228 TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, min)) |
| 229 .Times(1) |
| 230 .RetiresOnSaturation(); |
| 231 EXPECT_CALL(*gl_, |
| 232 TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, mag)) |
| 233 .Times(1) |
| 234 .RetiresOnSaturation(); |
| 235 EXPECT_CALL(*gl_, TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, wrap_s)) |
| 236 .Times(1) |
| 237 .RetiresOnSaturation(); |
| 238 EXPECT_CALL(*gl_, TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, wrap_t)) |
| 239 .Times(1) |
| 240 .RetiresOnSaturation(); |
| 241 EXPECT_CALL(*gl_, Flush()) |
| 242 .Times(1) |
| 243 .RetiresOnSaturation(); |
| 244 EXPECT_CALL(*gl_, BindTexture(GL_TEXTURE_2D, kCurrentTexture)) |
| 245 .Times(1) |
| 246 .RetiresOnSaturation(); |
| 247 } |
| 248 |
| 249 virtual void TearDown() { |
| 250 MailboxManagerTest::TearDown(); |
| 251 MailboxSynchronizer::Terminate(); |
| 252 ::gfx::MockGLInterface::SetGLInterface(NULL); |
| 253 gl_.reset(); |
| 254 } |
| 255 |
| 256 scoped_ptr< ::testing::StrictMock< ::gfx::MockGLInterface> > gl_; |
| 257 scoped_refptr<MailboxManager> manager2_; |
| 258 |
| 259 private: |
| 260 DISALLOW_COPY_AND_ASSIGN(MailboxManagerSyncTest); |
| 261 }; |
| 262 |
| 263 TEST_F(MailboxManagerSyncTest, ProduceDestroy) { |
| 264 Texture* texture = DefineTexture(); |
| 265 Mailbox name = Mailbox::Generate(); |
| 266 |
| 267 InSequence sequence; |
| 268 manager_->ProduceTexture(GL_TEXTURE_2D, name, texture); |
| 269 EXPECT_EQ(texture, manager_->ConsumeTexture(GL_TEXTURE_2D, name)); |
| 270 |
| 271 DestroyTexture(texture); |
| 272 EXPECT_EQ(NULL, manager_->ConsumeTexture(GL_TEXTURE_2D, name)); |
| 273 EXPECT_EQ(NULL, manager2_->ConsumeTexture(GL_TEXTURE_2D, name)); |
| 274 } |
| 275 |
| 276 TEST_F(MailboxManagerSyncTest, ProduceSyncDestroy) { |
| 277 InSequence sequence; |
| 278 |
| 279 Texture* texture = DefineTexture(); |
| 280 Mailbox name = Mailbox::Generate(); |
| 281 |
| 282 manager_->ProduceTexture(GL_TEXTURE_2D, name, texture); |
| 283 EXPECT_EQ(texture, manager_->ConsumeTexture(GL_TEXTURE_2D, name)); |
| 284 |
| 285 // Synchronize |
| 286 EXPECT_CALL(*gl_, Flush()).Times(1); |
| 287 manager_->PushTextureUpdates(); |
| 288 manager2_->PullTextureUpdates(); |
| 289 |
| 290 DestroyTexture(texture); |
| 291 EXPECT_EQ(NULL, manager_->ConsumeTexture(GL_TEXTURE_2D, name)); |
| 292 EXPECT_EQ(NULL, manager2_->ConsumeTexture(GL_TEXTURE_2D, name)); |
| 293 } |
| 294 |
| 295 // Duplicates a texture into a second manager instance, and then |
| 296 // makes sure a redefinition becomes visible there too. |
| 297 TEST_F(MailboxManagerSyncTest, ProduceConsumeResize) { |
| 298 const GLuint kNewTextureId = 1234; |
| 299 InSequence sequence; |
| 300 |
| 301 Texture* texture = DefineTexture(); |
| 302 Mailbox name = Mailbox::Generate(); |
| 303 |
| 304 manager_->ProduceTexture(GL_TEXTURE_2D, name, texture); |
| 305 EXPECT_EQ(texture, manager_->ConsumeTexture(GL_TEXTURE_2D, name)); |
| 306 |
| 307 // Synchronize |
| 308 EXPECT_CALL(*gl_, Flush()).Times(1); |
| 309 manager_->PushTextureUpdates(); |
| 310 manager2_->PullTextureUpdates(); |
| 311 |
| 312 EXPECT_CALL(*gl_, GenTextures(1, _)) |
| 313 .WillOnce(SetArgPointee<1>(kNewTextureId)); |
| 314 SetupUpdateTexParamExpectations( |
| 315 kNewTextureId, GL_LINEAR, GL_LINEAR, GL_REPEAT, GL_REPEAT); |
| 316 Texture* new_texture = manager2_->ConsumeTexture(GL_TEXTURE_2D, name); |
| 317 EXPECT_FALSE(new_texture == NULL); |
| 318 EXPECT_NE(texture, new_texture); |
| 319 EXPECT_EQ(kNewTextureId, new_texture->service_id()); |
| 320 |
| 321 // Resize original texture |
| 322 SetLevelInfo(texture, |
| 323 GL_TEXTURE_2D, |
| 324 0, |
| 325 GL_RGBA, |
| 326 16, |
| 327 32, |
| 328 1, |
| 329 0, |
| 330 GL_RGBA, |
| 331 GL_UNSIGNED_BYTE, |
| 332 true); |
| 333 // Should have been orphaned |
| 334 EXPECT_TRUE(texture->GetLevelImage(GL_TEXTURE_2D, 0) == NULL); |
| 335 |
| 336 // Synchronize again |
| 337 EXPECT_CALL(*gl_, Flush()).Times(1); |
| 338 manager_->PushTextureUpdates(); |
| 339 SetupUpdateTexParamExpectations( |
| 340 kNewTextureId, GL_LINEAR, GL_LINEAR, GL_REPEAT, GL_REPEAT); |
| 341 manager2_->PullTextureUpdates(); |
| 342 GLsizei width, height; |
| 343 new_texture->GetLevelSize(GL_TEXTURE_2D, 0, &width, &height); |
| 344 EXPECT_EQ(16, width); |
| 345 EXPECT_EQ(32, height); |
| 346 |
| 347 // Should have gotten a new attachment |
| 348 EXPECT_TRUE(texture->GetLevelImage(GL_TEXTURE_2D, 0) != NULL); |
| 349 // Resize original texture again.... |
| 350 SetLevelInfo(texture, |
| 351 GL_TEXTURE_2D, |
| 352 0, |
| 353 GL_RGBA, |
| 354 64, |
| 355 64, |
| 356 1, |
| 357 0, |
| 358 GL_RGBA, |
| 359 GL_UNSIGNED_BYTE, |
| 360 true); |
| 361 // ...and immediately delete the texture which should save the changes. |
| 362 SetupUpdateTexParamExpectations( |
| 363 kNewTextureId, GL_LINEAR, GL_LINEAR, GL_REPEAT, GL_REPEAT); |
| 364 DestroyTexture(texture); |
| 365 |
| 366 // Should be still around since there is a ref from manager2 |
| 367 EXPECT_EQ(new_texture, manager2_->ConsumeTexture(GL_TEXTURE_2D, name)); |
| 368 |
| 369 // The last change to the texture should be visible without a sync point (i.e. |
| 370 // push). |
| 371 manager2_->PullTextureUpdates(); |
| 372 new_texture->GetLevelSize(GL_TEXTURE_2D, 0, &width, &height); |
| 373 EXPECT_EQ(64, width); |
| 374 EXPECT_EQ(64, height); |
| 375 |
| 376 DestroyTexture(new_texture); |
| 377 EXPECT_EQ(NULL, manager_->ConsumeTexture(GL_TEXTURE_2D, name)); |
| 378 EXPECT_EQ(NULL, manager2_->ConsumeTexture(GL_TEXTURE_2D, name)); |
| 379 } |
| 380 |
| 381 // Makes sure changes are correctly published even when updates are |
| 382 // pushed in both directions, i.e. makes sure we don't clobber a shared |
| 383 // texture definition with an older version. |
| 384 TEST_F(MailboxManagerSyncTest, ProduceConsumeBidirectional) { |
| 385 const GLuint kNewTextureId1 = 1234; |
| 386 const GLuint kNewTextureId2 = 4321; |
| 387 |
| 388 Texture* texture1 = DefineTexture(); |
| 389 Mailbox name1 = Mailbox::Generate(); |
| 390 Texture* texture2 = DefineTexture(); |
| 391 Mailbox name2 = Mailbox::Generate(); |
| 392 Texture* new_texture1 = NULL; |
| 393 Texture* new_texture2 = NULL; |
| 394 |
| 395 manager_->ProduceTexture(GL_TEXTURE_2D, name1, texture1); |
| 396 manager2_->ProduceTexture(GL_TEXTURE_2D, name2, texture2); |
| 397 |
| 398 // Make visible. |
| 399 EXPECT_CALL(*gl_, Flush()).Times(2); |
| 400 manager_->PushTextureUpdates(); |
| 401 manager2_->PushTextureUpdates(); |
| 402 |
| 403 // Create textures in the other manager instances for texture1 and texture2, |
| 404 // respectively to create a real sharing scenario. Otherwise, there would |
| 405 // never be conflicting updates/pushes. |
| 406 { |
| 407 InSequence sequence; |
| 408 EXPECT_CALL(*gl_, GenTextures(1, _)) |
| 409 .WillOnce(SetArgPointee<1>(kNewTextureId1)); |
| 410 SetupUpdateTexParamExpectations( |
| 411 kNewTextureId1, GL_LINEAR, GL_LINEAR, GL_REPEAT, GL_REPEAT); |
| 412 new_texture1 = manager2_->ConsumeTexture(GL_TEXTURE_2D, name1); |
| 413 EXPECT_CALL(*gl_, GenTextures(1, _)) |
| 414 .WillOnce(SetArgPointee<1>(kNewTextureId2)); |
| 415 SetupUpdateTexParamExpectations( |
| 416 kNewTextureId2, GL_LINEAR, GL_LINEAR, GL_REPEAT, GL_REPEAT); |
| 417 new_texture2 = manager_->ConsumeTexture(GL_TEXTURE_2D, name2); |
| 418 } |
| 419 EXPECT_EQ(kNewTextureId1, new_texture1->service_id()); |
| 420 EXPECT_EQ(kNewTextureId2, new_texture2->service_id()); |
| 421 |
| 422 // Make a change to texture1 |
| 423 DCHECK_EQ(static_cast<GLuint>(GL_LINEAR), texture1->min_filter()); |
| 424 EXPECT_EQ(static_cast<GLenum>(GL_NO_ERROR), |
| 425 SetParameter(texture1, GL_TEXTURE_MIN_FILTER, GL_NEAREST)); |
| 426 |
| 427 // Make sure this does not clobber it with the previous version we pushed. |
| 428 manager_->PullTextureUpdates(); |
| 429 |
| 430 // Make a change to texture2 |
| 431 DCHECK_EQ(static_cast<GLuint>(GL_LINEAR), texture2->mag_filter()); |
| 432 EXPECT_EQ(static_cast<GLenum>(GL_NO_ERROR), |
| 433 SetParameter(texture2, GL_TEXTURE_MAG_FILTER, GL_NEAREST)); |
| 434 |
| 435 Mock::VerifyAndClearExpectations(gl_.get()); |
| 436 |
| 437 // Synchronize in both directions |
| 438 EXPECT_CALL(*gl_, Flush()).Times(2); |
| 439 manager_->PushTextureUpdates(); |
| 440 manager2_->PushTextureUpdates(); |
| 441 // manager1 should see the change to texture2 mag_filter being applied. |
| 442 SetupUpdateTexParamExpectations( |
| 443 new_texture2->service_id(), GL_LINEAR, GL_NEAREST, GL_REPEAT, GL_REPEAT); |
| 444 manager_->PullTextureUpdates(); |
| 445 // manager2 should see the change to texture1 min_filter being applied. |
| 446 SetupUpdateTexParamExpectations( |
| 447 new_texture1->service_id(), GL_NEAREST, GL_LINEAR, GL_REPEAT, GL_REPEAT); |
| 448 manager2_->PullTextureUpdates(); |
| 449 |
| 450 DestroyTexture(texture1); |
| 451 DestroyTexture(texture2); |
| 452 DestroyTexture(new_texture1); |
| 453 DestroyTexture(new_texture2); |
| 454 } |
| 455 |
| 456 // TODO: different texture into same mailbox |
| 457 |
| 458 // TODO: same texture, multiple mailboxes |
| 459 |
| 460 // TODO: Produce incomplete texture |
| 461 |
| 462 // TODO: Texture::level_infos_[][].size() |
| 463 |
| 464 // TODO: unsupported targets and formats |
| 465 |
129 } // namespace gles2 | 466 } // namespace gles2 |
130 } // namespace gpu | 467 } // namespace gpu |
OLD | NEW |