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/resources/resource_provider.h" | 5 #include "cc/resources/resource_provider.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 #include <stdint.h> | 8 #include <stdint.h> |
9 | 9 |
10 #include <algorithm> | 10 #include <algorithm> |
(...skipping 30 matching lines...) Expand all Loading... |
41 #include "ui/gfx/geometry/rect.h" | 41 #include "ui/gfx/geometry/rect.h" |
42 #include "ui/gfx/geometry/vector2d.h" | 42 #include "ui/gfx/geometry/vector2d.h" |
43 #include "ui/gfx/gpu_memory_buffer_tracing.h" | 43 #include "ui/gfx/gpu_memory_buffer_tracing.h" |
44 #include "ui/gfx/icc_profile.h" | 44 #include "ui/gfx/icc_profile.h" |
45 #include "ui/gl/trace_util.h" | 45 #include "ui/gl/trace_util.h" |
46 | 46 |
47 using gpu::gles2::GLES2Interface; | 47 using gpu::gles2::GLES2Interface; |
48 | 48 |
49 namespace cc { | 49 namespace cc { |
50 | 50 |
51 class IdAllocator { | 51 class TextureIdAllocator { |
52 public: | 52 public: |
53 virtual ~IdAllocator() {} | 53 TextureIdAllocator(GLES2Interface* gl, |
54 | 54 size_t texture_id_allocation_chunk_size) |
55 virtual GLuint NextId() = 0; | |
56 | |
57 protected: | |
58 IdAllocator(GLES2Interface* gl, size_t id_allocation_chunk_size) | |
59 : gl_(gl), | 55 : gl_(gl), |
60 id_allocation_chunk_size_(id_allocation_chunk_size), | 56 id_allocation_chunk_size_(texture_id_allocation_chunk_size), |
61 ids_(new GLuint[id_allocation_chunk_size]), | 57 ids_(new GLuint[texture_id_allocation_chunk_size]), |
62 next_id_index_(id_allocation_chunk_size) { | 58 next_id_index_(texture_id_allocation_chunk_size) { |
63 DCHECK(id_allocation_chunk_size_); | 59 DCHECK(id_allocation_chunk_size_); |
64 DCHECK_LE(id_allocation_chunk_size_, | 60 DCHECK_LE(id_allocation_chunk_size_, |
65 static_cast<size_t>(std::numeric_limits<int>::max())); | 61 static_cast<size_t>(std::numeric_limits<int>::max())); |
66 } | 62 } |
67 | 63 |
| 64 ~TextureIdAllocator() { |
| 65 gl_->DeleteTextures( |
| 66 static_cast<int>(id_allocation_chunk_size_ - next_id_index_), |
| 67 ids_.get() + next_id_index_); |
| 68 } |
| 69 |
| 70 GLuint NextId() { |
| 71 if (next_id_index_ == id_allocation_chunk_size_) { |
| 72 gl_->GenTextures(static_cast<int>(id_allocation_chunk_size_), ids_.get()); |
| 73 next_id_index_ = 0; |
| 74 } |
| 75 |
| 76 return ids_[next_id_index_++]; |
| 77 } |
| 78 |
| 79 private: |
68 GLES2Interface* gl_; | 80 GLES2Interface* gl_; |
69 const size_t id_allocation_chunk_size_; | 81 const size_t id_allocation_chunk_size_; |
70 std::unique_ptr<GLuint[]> ids_; | 82 std::unique_ptr<GLuint[]> ids_; |
71 size_t next_id_index_; | 83 size_t next_id_index_; |
| 84 |
| 85 DISALLOW_COPY_AND_ASSIGN(TextureIdAllocator); |
72 }; | 86 }; |
73 | 87 |
74 namespace { | 88 namespace { |
75 | 89 |
76 bool IsGpuResourceType(ResourceProvider::ResourceType type) { | 90 bool IsGpuResourceType(ResourceProvider::ResourceType type) { |
77 return type != ResourceProvider::RESOURCE_TYPE_BITMAP; | 91 return type != ResourceProvider::RESOURCE_TYPE_BITMAP; |
78 } | 92 } |
79 | 93 |
80 GLenum TextureToStorageFormat(ResourceFormat format) { | 94 GLenum TextureToStorageFormat(ResourceFormat format) { |
81 GLenum storage_format = GL_RGBA8_OES; | 95 GLenum storage_format = GL_RGBA8_OES; |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
152 // Active unit being GL_TEXTURE0 is effectively the ground state. | 166 // Active unit being GL_TEXTURE0 is effectively the ground state. |
153 if (unit_ != GL_TEXTURE0) | 167 if (unit_ != GL_TEXTURE0) |
154 gl_->ActiveTexture(GL_TEXTURE0); | 168 gl_->ActiveTexture(GL_TEXTURE0); |
155 } | 169 } |
156 | 170 |
157 private: | 171 private: |
158 GLES2Interface* gl_; | 172 GLES2Interface* gl_; |
159 GLenum unit_; | 173 GLenum unit_; |
160 }; | 174 }; |
161 | 175 |
162 class TextureIdAllocator : public IdAllocator { | |
163 public: | |
164 TextureIdAllocator(GLES2Interface* gl, | |
165 size_t texture_id_allocation_chunk_size) | |
166 : IdAllocator(gl, texture_id_allocation_chunk_size) {} | |
167 ~TextureIdAllocator() override { | |
168 gl_->DeleteTextures( | |
169 static_cast<int>(id_allocation_chunk_size_ - next_id_index_), | |
170 ids_.get() + next_id_index_); | |
171 } | |
172 | |
173 // Overridden from IdAllocator: | |
174 GLuint NextId() override { | |
175 if (next_id_index_ == id_allocation_chunk_size_) { | |
176 gl_->GenTextures(static_cast<int>(id_allocation_chunk_size_), ids_.get()); | |
177 next_id_index_ = 0; | |
178 } | |
179 | |
180 return ids_[next_id_index_++]; | |
181 } | |
182 | |
183 private: | |
184 DISALLOW_COPY_AND_ASSIGN(TextureIdAllocator); | |
185 }; | |
186 | |
187 class BufferIdAllocator : public IdAllocator { | |
188 public: | |
189 BufferIdAllocator(GLES2Interface* gl, size_t buffer_id_allocation_chunk_size) | |
190 : IdAllocator(gl, buffer_id_allocation_chunk_size) {} | |
191 ~BufferIdAllocator() override { | |
192 gl_->DeleteBuffers( | |
193 static_cast<int>(id_allocation_chunk_size_ - next_id_index_), | |
194 ids_.get() + next_id_index_); | |
195 } | |
196 | |
197 // Overridden from IdAllocator: | |
198 GLuint NextId() override { | |
199 if (next_id_index_ == id_allocation_chunk_size_) { | |
200 gl_->GenBuffers(static_cast<int>(id_allocation_chunk_size_), ids_.get()); | |
201 next_id_index_ = 0; | |
202 } | |
203 | |
204 return ids_[next_id_index_++]; | |
205 } | |
206 | |
207 private: | |
208 DISALLOW_COPY_AND_ASSIGN(BufferIdAllocator); | |
209 }; | |
210 | |
211 // Generates process-unique IDs to use for tracing a ResourceProvider's | 176 // Generates process-unique IDs to use for tracing a ResourceProvider's |
212 // resources. | 177 // resources. |
213 base::StaticAtomicSequenceNumber g_next_resource_provider_tracing_id; | 178 base::StaticAtomicSequenceNumber g_next_resource_provider_tracing_id; |
214 | 179 |
215 } // namespace | 180 } // namespace |
216 | 181 |
217 ResourceProvider::Resource::~Resource() {} | 182 ResourceProvider::Resource::~Resource() {} |
218 | 183 |
219 ResourceProvider::Resource::Resource(GLuint texture_id, | 184 ResourceProvider::Resource::Resource(GLuint texture_id, |
220 const gfx::Size& size, | 185 const gfx::Size& size, |
(...skipping 259 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
480 // TODO(ericrk): Get this working in Android Webview. crbug.com/517156 | 445 // TODO(ericrk): Get this working in Android Webview. crbug.com/517156 |
481 if (base::ThreadTaskRunnerHandle::IsSet()) { | 446 if (base::ThreadTaskRunnerHandle::IsSet()) { |
482 base::trace_event::MemoryDumpManager::GetInstance()->RegisterDumpProvider( | 447 base::trace_event::MemoryDumpManager::GetInstance()->RegisterDumpProvider( |
483 this, "cc::ResourceProvider", base::ThreadTaskRunnerHandle::Get()); | 448 this, "cc::ResourceProvider", base::ThreadTaskRunnerHandle::Get()); |
484 } | 449 } |
485 | 450 |
486 if (!compositor_context_provider) | 451 if (!compositor_context_provider) |
487 return; | 452 return; |
488 | 453 |
489 DCHECK(!texture_id_allocator_); | 454 DCHECK(!texture_id_allocator_); |
490 DCHECK(!buffer_id_allocator_); | |
491 GLES2Interface* gl = ContextGL(); | 455 GLES2Interface* gl = ContextGL(); |
492 texture_id_allocator_.reset( | 456 texture_id_allocator_.reset( |
493 new TextureIdAllocator(gl, id_allocation_chunk_size)); | 457 new TextureIdAllocator(gl, id_allocation_chunk_size)); |
494 buffer_id_allocator_.reset( | |
495 new BufferIdAllocator(gl, id_allocation_chunk_size)); | |
496 } | 458 } |
497 | 459 |
498 ResourceProvider::~ResourceProvider() { | 460 ResourceProvider::~ResourceProvider() { |
499 base::trace_event::MemoryDumpManager::GetInstance()->UnregisterDumpProvider( | 461 base::trace_event::MemoryDumpManager::GetInstance()->UnregisterDumpProvider( |
500 this); | 462 this); |
501 | 463 |
502 while (!children_.empty()) | 464 while (!children_.empty()) |
503 DestroyChildInternal(children_.begin(), FOR_SHUTDOWN); | 465 DestroyChildInternal(children_.begin(), FOR_SHUTDOWN); |
504 while (!resources_.empty()) | 466 while (!resources_.empty()) |
505 DeleteResourceInternal(resources_.begin(), FOR_SHUTDOWN); | 467 DeleteResourceInternal(resources_.begin(), FOR_SHUTDOWN); |
506 | 468 |
507 GLES2Interface* gl = ContextGL(); | 469 GLES2Interface* gl = ContextGL(); |
508 if (!IsGpuResourceType(settings_.default_resource_type)) { | 470 if (!IsGpuResourceType(settings_.default_resource_type)) { |
509 // We are not in GL mode, but double check before returning. | 471 // We are not in GL mode, but double check before returning. |
510 DCHECK(!gl); | 472 DCHECK(!gl); |
511 return; | 473 return; |
512 } | 474 } |
513 | 475 |
514 DCHECK(gl); | 476 DCHECK(gl); |
515 #if DCHECK_IS_ON() | 477 #if DCHECK_IS_ON() |
516 // Check that all GL resources has been deleted. | 478 // Check that all GL resources has been deleted. |
517 for (ResourceMap::const_iterator itr = resources_.begin(); | 479 for (ResourceMap::const_iterator itr = resources_.begin(); |
518 itr != resources_.end(); ++itr) { | 480 itr != resources_.end(); ++itr) { |
519 DCHECK(!IsGpuResourceType(itr->second.type)); | 481 DCHECK(!IsGpuResourceType(itr->second.type)); |
520 } | 482 } |
521 #endif // DCHECK_IS_ON() | 483 #endif // DCHECK_IS_ON() |
522 | 484 |
523 texture_id_allocator_ = nullptr; | 485 texture_id_allocator_ = nullptr; |
524 buffer_id_allocator_ = nullptr; | |
525 gl->Finish(); | 486 gl->Finish(); |
526 } | 487 } |
527 | 488 |
528 bool ResourceProvider::IsResourceFormatSupported(ResourceFormat format) const { | 489 bool ResourceProvider::IsResourceFormatSupported(ResourceFormat format) const { |
529 gpu::Capabilities caps; | 490 gpu::Capabilities caps; |
530 if (compositor_context_provider_) | 491 if (compositor_context_provider_) |
531 caps = compositor_context_provider_->ContextCapabilities(); | 492 caps = compositor_context_provider_->ContextCapabilities(); |
532 | 493 |
533 switch (format) { | 494 switch (format) { |
534 case ALPHA_8: | 495 case ALPHA_8: |
(...skipping 1652 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2187 | 2148 |
2188 const int kImportance = 2; | 2149 const int kImportance = 2; |
2189 pmd->CreateSharedGlobalAllocatorDump(guid); | 2150 pmd->CreateSharedGlobalAllocatorDump(guid); |
2190 pmd->AddOwnershipEdge(dump->guid(), guid, kImportance); | 2151 pmd->AddOwnershipEdge(dump->guid(), guid, kImportance); |
2191 } | 2152 } |
2192 | 2153 |
2193 return true; | 2154 return true; |
2194 } | 2155 } |
2195 | 2156 |
2196 } // namespace cc | 2157 } // namespace cc |
OLD | NEW |