| 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 // A class to emulate GLES2 over command buffers. | 5 // A class to emulate GLES2 over command buffers. |
| 6 | 6 |
| 7 #include "gpu/command_buffer/client/gles2_implementation.h" | 7 #include "gpu/command_buffer/client/gles2_implementation.h" |
| 8 | 8 |
| 9 #include <GLES2/gl2.h> | 9 #include <GLES2/gl2.h> |
| 10 #include <GLES2/gl2ext.h> | 10 #include <GLES2/gl2ext.h> |
| 11 #include <GLES2/gl2extchromium.h> | 11 #include <GLES2/gl2extchromium.h> |
| 12 #include <GLES3/gl3.h> | 12 #include <GLES3/gl3.h> |
| 13 #include <stddef.h> | 13 #include <stddef.h> |
| 14 #include <stdint.h> | 14 #include <stdint.h> |
| 15 #include <algorithm> | 15 #include <algorithm> |
| 16 #include <map> | 16 #include <map> |
| 17 #include <set> | 17 #include <set> |
| 18 #include <sstream> | 18 #include <sstream> |
| 19 #include <string> | 19 #include <string> |
| 20 #include "base/atomic_sequence_num.h" | 20 #include "base/atomic_sequence_num.h" |
| 21 #include "base/compiler_specific.h" | 21 #include "base/compiler_specific.h" |
| 22 #include "base/memory/ptr_util.h" |
| 22 #include "base/strings/string_split.h" | 23 #include "base/strings/string_split.h" |
| 23 #include "base/strings/stringprintf.h" | 24 #include "base/strings/stringprintf.h" |
| 24 #include "base/sys_info.h" | 25 #include "base/sys_info.h" |
| 25 #include "base/threading/thread_task_runner_handle.h" | 26 #include "base/threading/thread_task_runner_handle.h" |
| 26 #include "base/trace_event/memory_allocator_dump.h" | 27 #include "base/trace_event/memory_allocator_dump.h" |
| 27 #include "base/trace_event/memory_dump_manager.h" | 28 #include "base/trace_event/memory_dump_manager.h" |
| 28 #include "base/trace_event/process_memory_dump.h" | 29 #include "base/trace_event/process_memory_dump.h" |
| 29 #include "base/trace_event/trace_event.h" | 30 #include "base/trace_event/trace_event.h" |
| 30 #include "gpu/command_buffer/client/buffer_tracker.h" | 31 #include "gpu/command_buffer/client/buffer_tracker.h" |
| 31 #include "gpu/command_buffer/client/gles2_cmd_helper.h" | 32 #include "gpu/command_buffer/client/gles2_cmd_helper.h" |
| (...skipping 12 matching lines...) Expand all Loading... |
| 44 #if defined(GPU_CLIENT_DEBUG) | 45 #if defined(GPU_CLIENT_DEBUG) |
| 45 #include "base/command_line.h" | 46 #include "base/command_line.h" |
| 46 #include "gpu/command_buffer/client/gpu_switches.h" | 47 #include "gpu/command_buffer/client/gpu_switches.h" |
| 47 #endif | 48 #endif |
| 48 | 49 |
| 49 namespace gpu { | 50 namespace gpu { |
| 50 namespace gles2 { | 51 namespace gles2 { |
| 51 | 52 |
| 52 namespace { | 53 namespace { |
| 53 | 54 |
| 55 // Class that DCHECKs if it is destructed without first having Release called. |
| 56 class ScopedVisibilityImpl : public ContextSupport::ScopedVisibility { |
| 57 public: |
| 58 explicit ScopedVisibilityImpl(ContextSupport* context_support) |
| 59 : initial_context_support_(context_support) {} |
| 60 ~ScopedVisibilityImpl() { DCHECK(!initial_context_support_); } |
| 61 |
| 62 void Release(ContextSupport* context_support) { |
| 63 DCHECK_EQ(initial_context_support_, context_support); |
| 64 initial_context_support_ = nullptr; |
| 65 } |
| 66 |
| 67 private: |
| 68 const ContextSupport* initial_context_support_; |
| 69 }; |
| 70 |
| 54 void CopyRectToBuffer(const void* pixels, | 71 void CopyRectToBuffer(const void* pixels, |
| 55 uint32_t height, | 72 uint32_t height, |
| 56 uint32_t unpadded_row_size, | 73 uint32_t unpadded_row_size, |
| 57 uint32_t pixels_padded_row_size, | 74 uint32_t pixels_padded_row_size, |
| 58 void* buffer, | 75 void* buffer, |
| 59 uint32_t buffer_padded_row_size) { | 76 uint32_t buffer_padded_row_size) { |
| 60 if (height == 0) | 77 if (height == 0) |
| 61 return; | 78 return; |
| 62 const int8_t* source = static_cast<const int8_t*>(pixels); | 79 const int8_t* source = static_cast<const int8_t*>(pixels); |
| 63 int8_t* dest = static_cast<int8_t*>(buffer); | 80 int8_t* dest = static_cast<int8_t*>(buffer); |
| (...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 157 // Do not use more than 5% of extra shared memory, and do not | 174 // Do not use more than 5% of extra shared memory, and do not |
| 158 // use any extra for memory contrained devices (<=1GB). | 175 // use any extra for memory contrained devices (<=1GB). |
| 159 base::SysInfo::AmountOfPhysicalMemory() > 1024 * 1024 * 1024 | 176 base::SysInfo::AmountOfPhysicalMemory() > 1024 * 1024 * 1024 |
| 160 ? base::saturated_cast<uint32_t>( | 177 ? base::saturated_cast<uint32_t>( |
| 161 base::SysInfo::AmountOfPhysicalMemory() / 20) | 178 base::SysInfo::AmountOfPhysicalMemory() / 20) |
| 162 : 0), | 179 : 0), |
| 163 #endif | 180 #endif |
| 164 current_trace_stack_(0), | 181 current_trace_stack_(0), |
| 165 gpu_control_(gpu_control), | 182 gpu_control_(gpu_control), |
| 166 capabilities_(gpu_control->GetCapabilities()), | 183 capabilities_(gpu_control->GetCapabilities()), |
| 167 aggressively_free_resources_(false), | |
| 168 cached_extension_string_(nullptr), | 184 cached_extension_string_(nullptr), |
| 169 weak_ptr_factory_(this) { | 185 weak_ptr_factory_(this) { |
| 170 DCHECK(helper); | 186 DCHECK(helper); |
| 171 DCHECK(transfer_buffer); | 187 DCHECK(transfer_buffer); |
| 172 DCHECK(gpu_control); | 188 DCHECK(gpu_control); |
| 173 | 189 |
| 174 std::stringstream ss; | 190 std::stringstream ss; |
| 175 ss << std::hex << this; | 191 ss << std::hex << this; |
| 176 this_in_hex_ = ss.str(); | 192 this_in_hex_ = ss.str(); |
| 177 | 193 |
| (...skipping 227 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 405 // Flush previously entered commands to ensure ordering with any | 421 // Flush previously entered commands to ensure ordering with any |
| 406 // glBeginQueryEXT() calls that may have been put into the context. | 422 // glBeginQueryEXT() calls that may have been put into the context. |
| 407 ShallowFlushCHROMIUM(); | 423 ShallowFlushCHROMIUM(); |
| 408 gpu_control_->SignalQuery( | 424 gpu_control_->SignalQuery( |
| 409 query, | 425 query, |
| 410 base::Bind(&GLES2Implementation::RunIfContextNotLost, | 426 base::Bind(&GLES2Implementation::RunIfContextNotLost, |
| 411 weak_ptr_factory_.GetWeakPtr(), | 427 weak_ptr_factory_.GetWeakPtr(), |
| 412 callback)); | 428 callback)); |
| 413 } | 429 } |
| 414 | 430 |
| 415 void GLES2Implementation::SetAggressivelyFreeResources( | 431 void GLES2Implementation::TrimResources() { |
| 416 bool aggressively_free_resources) { | 432 TRACE_EVENT0("gpu", "GLES2Implementation::TrimResources"); |
| 417 TRACE_EVENT1("gpu", "GLES2Implementation::SetAggressivelyFreeResources", | 433 |
| 418 "aggressively_free_resources", aggressively_free_resources); | 434 // Set aggressively_free_resources_ to true, which will cause us to free |
| 419 aggressively_free_resources_ = aggressively_free_resources; | 435 // resources when we call Flush or ShallowFlushCHROMIUM below. |
| 436 aggressively_free_resources_ = true; |
| 420 | 437 |
| 421 if (aggressively_free_resources_ && helper_->HaveRingBuffer()) { | 438 if (aggressively_free_resources_ && helper_->HaveRingBuffer()) { |
| 422 // Ensure that we clean up as much cache memory as possible and fully flush. | 439 // Ensure that we clean up as much cache memory as possible and fully flush. |
| 423 FlushDriverCachesCHROMIUM(); | 440 FlushDriverCachesCHROMIUM(); |
| 424 | 441 |
| 425 // Flush will delete transfer buffer resources if | 442 // Flush will delete transfer buffer resources if |
| 426 // |aggressively_free_resources_| is true. | 443 // |aggressively_free_resources_| is true. |
| 427 Flush(); | 444 Flush(); |
| 428 } else { | 445 } else { |
| 429 ShallowFlushCHROMIUM(); | 446 ShallowFlushCHROMIUM(); |
| 430 } | 447 } |
| 448 |
| 449 // If we are not visible, continue freeing resources on subsequent flushes. |
| 450 // This will be set to false once we become visible. |
| 451 aggressively_free_resources_ = !AnyClientsVisible(); |
| 431 } | 452 } |
| 432 | 453 |
| 433 bool GLES2Implementation::OnMemoryDump( | 454 bool GLES2Implementation::OnMemoryDump( |
| 434 const base::trace_event::MemoryDumpArgs& args, | 455 const base::trace_event::MemoryDumpArgs& args, |
| 435 base::trace_event::ProcessMemoryDump* pmd) { | 456 base::trace_event::ProcessMemoryDump* pmd) { |
| 436 if (!transfer_buffer_->HaveBuffer()) | 457 if (!transfer_buffer_->HaveBuffer()) |
| 437 return true; | 458 return true; |
| 438 | 459 |
| 439 const uint64_t tracing_process_id = | 460 const uint64_t tracing_process_id = |
| 440 base::trace_event::MemoryDumpManager::GetInstance() | 461 base::trace_event::MemoryDumpManager::GetInstance() |
| (...skipping 5413 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 5854 | 5875 |
| 5855 uint64_t GLES2Implementation::ShareGroupTracingGUID() const { | 5876 uint64_t GLES2Implementation::ShareGroupTracingGUID() const { |
| 5856 return share_group_->TracingGUID(); | 5877 return share_group_->TracingGUID(); |
| 5857 } | 5878 } |
| 5858 | 5879 |
| 5859 void GLES2Implementation::SetErrorMessageCallback( | 5880 void GLES2Implementation::SetErrorMessageCallback( |
| 5860 const base::Callback<void(const char*, int32_t)>& callback) { | 5881 const base::Callback<void(const char*, int32_t)>& callback) { |
| 5861 error_message_callback_ = callback; | 5882 error_message_callback_ = callback; |
| 5862 } | 5883 } |
| 5863 | 5884 |
| 5864 void GLES2Implementation::SetClientVisible(int client_id, bool is_visible) { | 5885 std::unique_ptr<ContextSupport::ScopedVisibility> |
| 5865 if (is_visible) { | 5886 GLES2Implementation::ClientBecameVisible() { |
| 5866 visible_clients_.insert(client_id); | 5887 ++num_visible_clients_; |
| 5867 } else { | 5888 // We are visible, ensure that we are no longer freeing resources on flush. |
| 5868 auto found = visible_clients_.find(client_id); | 5889 aggressively_free_resources_ = false; |
| 5869 if (found != visible_clients_.end()) | 5890 return base::MakeUnique<ScopedVisibilityImpl>(this); |
| 5870 visible_clients_.erase(found); | 5891 } |
| 5871 } | 5892 |
| 5893 void GLES2Implementation::ClientBecameNotVisible( |
| 5894 std::unique_ptr<ScopedVisibility> visibility) { |
| 5895 DCHECK(visibility); |
| 5896 DCHECK_GT(num_visible_clients_, 0u); |
| 5897 --num_visible_clients_; |
| 5898 static_cast<ScopedVisibilityImpl*>(visibility.get())->Release(this); |
| 5872 } | 5899 } |
| 5873 | 5900 |
| 5874 bool GLES2Implementation::AnyClientsVisible() const { | 5901 bool GLES2Implementation::AnyClientsVisible() const { |
| 5875 return !visible_clients_.empty(); | 5902 return num_visible_clients_ > 0; |
| 5876 } | 5903 } |
| 5877 | 5904 |
| 5878 void GLES2Implementation::SetLostContextCallback( | 5905 void GLES2Implementation::SetLostContextCallback( |
| 5879 const base::Closure& callback) { | 5906 const base::Closure& callback) { |
| 5880 lost_context_callback_ = callback; | 5907 lost_context_callback_ = callback; |
| 5881 } | 5908 } |
| 5882 | 5909 |
| 5883 GLuint64 GLES2Implementation::InsertFenceSyncCHROMIUM() { | 5910 GLuint64 GLES2Implementation::InsertFenceSyncCHROMIUM() { |
| 5884 const uint64_t release = gpu_control_->GenerateFenceSyncRelease(); | 5911 const uint64_t release = gpu_control_->GenerateFenceSyncRelease(); |
| 5885 helper_->InsertFenceSyncCHROMIUM(release); | 5912 helper_->InsertFenceSyncCHROMIUM(release); |
| (...skipping 1023 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 6909 cached_extensions_.clear(); | 6936 cached_extensions_.clear(); |
| 6910 } | 6937 } |
| 6911 | 6938 |
| 6912 // Include the auto-generated part of this file. We split this because it means | 6939 // Include the auto-generated part of this file. We split this because it means |
| 6913 // we can easily edit the non-auto generated parts right here in this file | 6940 // we can easily edit the non-auto generated parts right here in this file |
| 6914 // instead of having to edit some template or the code generator. | 6941 // instead of having to edit some template or the code generator. |
| 6915 #include "gpu/command_buffer/client/gles2_implementation_impl_autogen.h" | 6942 #include "gpu/command_buffer/client/gles2_implementation_impl_autogen.h" |
| 6916 | 6943 |
| 6917 } // namespace gles2 | 6944 } // namespace gles2 |
| 6918 } // namespace gpu | 6945 } // namespace gpu |
| OLD | NEW |