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 #include "gpu/command_buffer/client/program_info_manager.h" | 5 #include "gpu/command_buffer/client/program_info_manager.h" |
6 | 6 |
7 #include "base/numerics/safe_math.h" | |
8 | |
9 namespace { | 7 namespace { |
10 | 8 |
11 template<typename T> static T LocalGetAs( | 9 template<typename T> static T LocalGetAs( |
12 const std::vector<int8>& data, uint32 offset, size_t size) { | 10 const std::vector<int8>& data, uint32 offset, size_t size) { |
13 const int8* p = &data[0] + offset; | 11 const int8* p = &data[0] + offset; |
14 if (offset + size > data.size()) { | 12 if (offset + size > data.size()) { |
15 NOTREACHED(); | 13 NOTREACHED(); |
16 return NULL; | 14 return NULL; |
17 } | 15 } |
18 return static_cast<T>(static_cast<const void*>(p)); | 16 return static_cast<T>(static_cast<const void*>(p)); |
(...skipping 223 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
242 if (cached_es3_uniform_blocks_) { | 240 if (cached_es3_uniform_blocks_) { |
243 return; | 241 return; |
244 } | 242 } |
245 if (result.empty()) { | 243 if (result.empty()) { |
246 // This should only happen on a lost context. | 244 // This should only happen on a lost context. |
247 return; | 245 return; |
248 } | 246 } |
249 uniform_blocks_.clear(); | 247 uniform_blocks_.clear(); |
250 active_uniform_block_max_name_length_ = 0; | 248 active_uniform_block_max_name_length_ = 0; |
251 | 249 |
| 250 // |result| comes from GPU process. We consider it trusted data. Therefore, |
| 251 // no need to check for overflows as the GPU side did the checks already. |
252 uint32_t header_size = sizeof(UniformBlocksHeader); | 252 uint32_t header_size = sizeof(UniformBlocksHeader); |
253 DCHECK_GE(result.size(), header_size); | 253 DCHECK_GE(result.size(), header_size); |
254 const UniformBlocksHeader* header = LocalGetAs<const UniformBlocksHeader*>( | 254 const UniformBlocksHeader* header = LocalGetAs<const UniformBlocksHeader*>( |
255 result, 0, header_size); | 255 result, 0, header_size); |
256 DCHECK(header); | 256 DCHECK(header); |
257 if (header->num_uniform_blocks == 0) { | 257 if (header->num_uniform_blocks == 0) { |
258 DCHECK_EQ(result.size(), header_size); | 258 DCHECK_EQ(result.size(), header_size); |
259 // TODO(zmo): Here we can't tell if no uniform blocks are defined, or | 259 // TODO(zmo): Here we can't tell if no uniform blocks are defined, or |
260 // the previous link failed. | 260 // the previous link failed. |
261 return; | 261 return; |
(...skipping 267 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
529 | 529 |
530 GLuint ProgramInfoManager::GetUniformBlockIndex( | 530 GLuint ProgramInfoManager::GetUniformBlockIndex( |
531 GLES2Implementation* gl, GLuint program, const char* name) { | 531 GLES2Implementation* gl, GLuint program, const char* name) { |
532 { | 532 { |
533 base::AutoLock auto_lock(lock_); | 533 base::AutoLock auto_lock(lock_); |
534 Program* info = GetProgramInfo(gl, program, kES3UniformBlocks); | 534 Program* info = GetProgramInfo(gl, program, kES3UniformBlocks); |
535 if (info) { | 535 if (info) { |
536 return info->GetUniformBlockIndex(name); | 536 return info->GetUniformBlockIndex(name); |
537 } | 537 } |
538 } | 538 } |
539 return false; | 539 return gl->GetUniformBlockIndexHelper(program, name); |
540 // TODO(zmo): return gl->GetUniformBlockIndexHelper(program, name); | |
541 } | 540 } |
542 | 541 |
543 bool ProgramInfoManager::GetActiveUniformBlockName( | 542 bool ProgramInfoManager::GetActiveUniformBlockName( |
544 GLES2Implementation* gl, GLuint program, GLuint index, | 543 GLES2Implementation* gl, GLuint program, GLuint index, |
545 GLsizei buf_size, GLsizei* length, char* name) { | 544 GLsizei buf_size, GLsizei* length, char* name) { |
| 545 DCHECK_LE(0, buf_size); |
| 546 if (!name) { |
| 547 buf_size = 0; |
| 548 } |
546 { | 549 { |
547 base::AutoLock auto_lock(lock_); | 550 base::AutoLock auto_lock(lock_); |
548 Program* info = GetProgramInfo(gl, program, kES3UniformBlocks); | 551 Program* info = GetProgramInfo(gl, program, kES3UniformBlocks); |
549 if (info) { | 552 if (info) { |
550 const Program::UniformBlock* uniform_block = info->GetUniformBlock(index); | 553 const Program::UniformBlock* uniform_block = info->GetUniformBlock(index); |
551 if (uniform_block && buf_size >= 1) { | 554 if (uniform_block) { |
552 GLsizei written_size = std::min( | 555 if (buf_size == 0) { |
553 buf_size, static_cast<GLsizei>(uniform_block->name.size()) + 1); | 556 if (length) { |
554 if (length) { | 557 *length = 0; |
555 *length = written_size - 1; | 558 } |
| 559 } else if (length || name) { |
| 560 GLsizei max_size = std::min( |
| 561 buf_size - 1, static_cast<GLsizei>(uniform_block->name.size())); |
| 562 if (length) { |
| 563 *length = max_size; |
| 564 } |
| 565 if (name) { |
| 566 memcpy(name, uniform_block->name.data(), max_size); |
| 567 name[max_size] = '\0'; |
| 568 } |
556 } | 569 } |
557 memcpy(name, uniform_block->name.c_str(), written_size); | |
558 return true; | 570 return true; |
559 } | 571 } |
560 } | 572 } |
561 } | 573 } |
562 return false; | 574 return gl->GetActiveUniformBlockNameHelper( |
563 // TODO(zmo): return gl->GetActiveUniformBlockNameHelper( | 575 program, index, buf_size, length, name); |
564 // program, index, buf_size, length, name); | |
565 } | 576 } |
566 | 577 |
567 bool ProgramInfoManager::GetActiveUniformBlockiv( | 578 bool ProgramInfoManager::GetActiveUniformBlockiv( |
568 GLES2Implementation* gl, GLuint program, GLuint index, | 579 GLES2Implementation* gl, GLuint program, GLuint index, |
569 GLenum pname, GLint* params) { | 580 GLenum pname, GLint* params) { |
570 { | 581 { |
571 base::AutoLock auto_lock(lock_); | 582 base::AutoLock auto_lock(lock_); |
572 Program* info = GetProgramInfo(gl, program, kES3UniformBlocks); | 583 Program* info = GetProgramInfo(gl, program, kES3UniformBlocks); |
573 if (info) { | 584 if (info) { |
574 const Program::UniformBlock* uniform_block = info->GetUniformBlock(index); | 585 const Program::UniformBlock* uniform_block = info->GetUniformBlock(index); |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
625 } | 636 } |
626 } | 637 } |
627 return false; | 638 return false; |
628 // TODO(zmo): return gl->GetActiveUniformBlockivHelper( | 639 // TODO(zmo): return gl->GetActiveUniformBlockivHelper( |
629 // program, index, pname, params); | 640 // program, index, pname, params); |
630 } | 641 } |
631 | 642 |
632 } // namespace gles2 | 643 } // namespace gles2 |
633 } // namespace gpu | 644 } // namespace gpu |
634 | 645 |
OLD | NEW |