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/service/common_decoder.h" | 5 #include "gpu/command_buffer/service/common_decoder.h" |
| 6 |
| 7 #include "base/numerics/safe_math.h" |
6 #include "gpu/command_buffer/service/cmd_buffer_engine.h" | 8 #include "gpu/command_buffer/service/cmd_buffer_engine.h" |
7 | 9 |
8 namespace gpu { | 10 namespace gpu { |
9 | 11 |
10 const CommonDecoder::CommandInfo CommonDecoder::command_info[] = { | 12 const CommonDecoder::CommandInfo CommonDecoder::command_info[] = { |
11 #define COMMON_COMMAND_BUFFER_CMD_OP(name) \ | 13 #define COMMON_COMMAND_BUFFER_CMD_OP(name) \ |
12 { \ | 14 { \ |
13 &CommonDecoder::Handle##name, cmd::name::kArgFlags, \ | 15 &CommonDecoder::Handle##name, cmd::name::kArgFlags, \ |
14 cmd::name::cmd_flags, \ | 16 cmd::name::cmd_flags, \ |
15 sizeof(cmd::name) / sizeof(CommandBufferEntry) - 1, \ | 17 sizeof(cmd::name) / sizeof(CommandBufferEntry) - 1, \ |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
62 | 64 |
63 bool CommonDecoder::Bucket::GetAsString(std::string* str) { | 65 bool CommonDecoder::Bucket::GetAsString(std::string* str) { |
64 DCHECK(str); | 66 DCHECK(str); |
65 if (size_ == 0) { | 67 if (size_ == 0) { |
66 return false; | 68 return false; |
67 } | 69 } |
68 str->assign(GetDataAs<const char*>(0, size_ - 1), size_ - 1); | 70 str->assign(GetDataAs<const char*>(0, size_ - 1), size_ - 1); |
69 return true; | 71 return true; |
70 } | 72 } |
71 | 73 |
| 74 bool CommonDecoder::Bucket::GetAsStrings( |
| 75 GLsizei* _count, std::vector<char*>* _string, std::vector<GLint>* _length) { |
| 76 const size_t kMinBucketSize = sizeof(GLint); |
| 77 // Each string has at least |length| in the header and a NUL character. |
| 78 const size_t kMinStringSize = sizeof(GLint) + 1; |
| 79 const size_t bucket_size = this->size(); |
| 80 if (bucket_size < kMinBucketSize) { |
| 81 return false; |
| 82 } |
| 83 char* bucket_data = this->GetDataAs<char*>(0, bucket_size); |
| 84 GLint* header = reinterpret_cast<GLint*>(bucket_data); |
| 85 GLsizei count = static_cast<GLsizei>(header[0]); |
| 86 if (count < 0) { |
| 87 return false; |
| 88 } |
| 89 const size_t max_count = (bucket_size - kMinBucketSize) / kMinStringSize; |
| 90 if (max_count < static_cast<size_t>(count)) { |
| 91 return false; |
| 92 } |
| 93 GLint* length = header + 1; |
| 94 std::vector<char*> strs(count); |
| 95 base::CheckedNumeric<size_t> total_size = sizeof(GLint); |
| 96 total_size *= count + 1; // Header size. |
| 97 if (!total_size.IsValid()) |
| 98 return false; |
| 99 for (GLsizei ii = 0; ii < count; ++ii) { |
| 100 strs[ii] = bucket_data + total_size.ValueOrDefault(0); |
| 101 total_size += length[ii]; |
| 102 total_size += 1; // NUL char at the end of each char array. |
| 103 if (!total_size.IsValid() || total_size.ValueOrDefault(0) > bucket_size || |
| 104 strs[ii][length[ii]] != 0) { |
| 105 return false; |
| 106 } |
| 107 } |
| 108 if (total_size.ValueOrDefault(0) != bucket_size) { |
| 109 return false; |
| 110 } |
| 111 DCHECK(_count && _string && _length); |
| 112 *_count = count; |
| 113 *_string = strs; |
| 114 _length->resize(count); |
| 115 for (GLsizei ii = 0; ii < count; ++ii) { |
| 116 (*_length)[ii] = length[ii]; |
| 117 } |
| 118 return true; |
| 119 } |
| 120 |
72 CommonDecoder::CommonDecoder() : engine_(NULL) {} | 121 CommonDecoder::CommonDecoder() : engine_(NULL) {} |
73 | 122 |
74 CommonDecoder::~CommonDecoder() {} | 123 CommonDecoder::~CommonDecoder() {} |
75 | 124 |
76 void* CommonDecoder::GetAddressAndCheckSize(unsigned int shm_id, | 125 void* CommonDecoder::GetAddressAndCheckSize(unsigned int shm_id, |
77 unsigned int data_offset, | 126 unsigned int data_offset, |
78 unsigned int data_size) { | 127 unsigned int data_size) { |
79 CHECK(engine_); | 128 CHECK(engine_); |
80 scoped_refptr<gpu::Buffer> buffer = engine_->GetSharedMemoryBuffer(shm_id); | 129 scoped_refptr<gpu::Buffer> buffer = engine_->GetSharedMemoryBuffer(shm_id); |
81 if (!buffer.get()) | 130 if (!buffer.get()) |
(...skipping 195 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
277 } | 326 } |
278 const void* src = bucket->GetData(offset, size); | 327 const void* src = bucket->GetData(offset, size); |
279 if (!src) { | 328 if (!src) { |
280 return error::kInvalidArguments; | 329 return error::kInvalidArguments; |
281 } | 330 } |
282 memcpy(data, src, size); | 331 memcpy(data, src, size); |
283 return error::kNoError; | 332 return error::kNoError; |
284 } | 333 } |
285 | 334 |
286 } // namespace gpu | 335 } // namespace gpu |
OLD | NEW |