Index: content/browser/gpu/browser_gpu_memory_buffer_manager.cc |
diff --git a/content/browser/gpu/browser_gpu_memory_buffer_manager.cc b/content/browser/gpu/browser_gpu_memory_buffer_manager.cc |
index 7df495c816d2a2933aa3d712c49e9df6eb7dc14c..2b4a75c5f3aee69bf57aa690713d1d7d7f95cee7 100644 |
--- a/content/browser/gpu/browser_gpu_memory_buffer_manager.cc |
+++ b/content/browser/gpu/browser_gpu_memory_buffer_manager.cc |
@@ -14,6 +14,7 @@ |
#include "base/trace_event/trace_event.h" |
#include "content/browser/gpu/gpu_process_host.h" |
#include "content/common/child_process_host_impl.h" |
+#include "content/common/generic_shared_memory_id_generator.h" |
#include "content/common/gpu/client/gpu_memory_buffer_impl.h" |
#include "content/common/gpu/client/gpu_memory_buffer_impl_shared_memory.h" |
#include "content/common/gpu/gpu_memory_buffer_factory_shared_memory.h" |
@@ -136,9 +137,6 @@ GetSupportedGpuMemoryBufferConfigurations(gfx::GpuMemoryBufferType type) { |
BrowserGpuMemoryBufferManager* g_gpu_memory_buffer_manager = nullptr; |
-// Global atomic to generate gpu memory buffer unique IDs. |
-base::StaticAtomicSequenceNumber g_next_gpu_memory_buffer_id; |
- |
} // namespace |
struct BrowserGpuMemoryBufferManager::AllocateGpuMemoryBufferRequest { |
@@ -229,6 +227,7 @@ BrowserGpuMemoryBufferManager::AllocateGpuMemoryBufferForScanout( |
} |
void BrowserGpuMemoryBufferManager::AllocateGpuMemoryBufferForChildProcess( |
+ gfx::GpuMemoryBufferId id, |
const gfx::Size& size, |
gfx::BufferFormat format, |
gfx::BufferUsage usage, |
@@ -237,11 +236,9 @@ void BrowserGpuMemoryBufferManager::AllocateGpuMemoryBufferForChildProcess( |
const AllocationCallback& callback) { |
DCHECK_CURRENTLY_ON(BrowserThread::IO); |
- gfx::GpuMemoryBufferId new_id = g_next_gpu_memory_buffer_id.GetNext(); |
- |
// Use service side allocation if this is a supported configuration. |
if (IsGpuMemoryBufferConfigurationSupported(format, usage)) { |
- AllocateGpuMemoryBufferOnIO(new_id, size, format, usage, child_client_id, 0, |
+ AllocateGpuMemoryBufferOnIO(id, size, format, usage, child_client_id, 0, |
false, callback); |
return; |
} |
@@ -255,13 +252,19 @@ void BrowserGpuMemoryBufferManager::AllocateGpuMemoryBufferForChildProcess( |
} |
BufferMap& buffers = clients_[child_client_id]; |
- DCHECK(buffers.find(new_id) == buffers.end()); |
+ |
+ if (!TryAddNewBufferToMap( |
+ buffers, id, |
+ BufferInfo(size, gfx::SHARED_MEMORY_BUFFER, format, usage, 0))) { |
+ LOG(ERROR) << "Client process attempted to allocate a GpuMemoryBuffer with " |
reveman
2015/08/10 22:16:14
nit: "Child process..." to be consistent with func
ericrk
2015/08/11 01:40:07
Done.
|
+ "an existing ID. Failing allocation."; |
reveman
2015/08/10 22:16:14
nit: I think "Failing allocation." is implicit fro
ericrk
2015/08/11 01:40:07
Done.
|
+ callback.Run(gfx::GpuMemoryBufferHandle()); |
+ return; |
+ } |
// Allocate shared memory buffer as fallback. |
- buffers[new_id] = |
- BufferInfo(size, gfx::SHARED_MEMORY_BUFFER, format, usage, 0); |
callback.Run(GpuMemoryBufferImplSharedMemory::AllocateForChildProcess( |
- new_id, size, format, child_process_handle)); |
+ id, size, format, child_process_handle)); |
} |
gfx::GpuMemoryBuffer* |
@@ -315,8 +318,8 @@ bool BrowserGpuMemoryBufferManager::OnMemoryDump( |
uint64 client_tracing_process_id = ClientIdToTracingProcessId(client_id); |
base::trace_event::MemoryAllocatorDumpGuid shared_buffer_guid = |
- gfx::GetGpuMemoryBufferGUIDForTracing(client_tracing_process_id, |
- buffer_id); |
+ base::GetGenericSharedMemoryGUIDForTracing(client_tracing_process_id, |
+ buffer_id); |
pmd->CreateSharedGlobalAllocatorDump(shared_buffer_guid); |
pmd->AddOwnershipEdge(dump->guid(), shared_buffer_guid); |
} |
@@ -399,7 +402,7 @@ void BrowserGpuMemoryBufferManager::AllocateGpuMemoryBufferForSurfaceOnIO( |
AllocateGpuMemoryBufferRequest* request) { |
DCHECK_CURRENTLY_ON(BrowserThread::IO); |
- gfx::GpuMemoryBufferId new_id = g_next_gpu_memory_buffer_id.GetNext(); |
+ gfx::GpuMemoryBufferId new_id = content::GetNextGenericSharedMemoryId(); |
// Use service side allocation if this is a supported configuration. |
if (IsGpuMemoryBufferConfigurationSupported(request->format, |
@@ -421,11 +424,11 @@ void BrowserGpuMemoryBufferManager::AllocateGpuMemoryBufferForSurfaceOnIO( |
<< static_cast<int>(request->usage); |
BufferMap& buffers = clients_[request->client_id]; |
- DCHECK(buffers.find(new_id) == buffers.end()); |
// Allocate shared memory buffer as fallback. |
- buffers[new_id] = BufferInfo(request->size, gfx::SHARED_MEMORY_BUFFER, |
- request->format, request->usage, 0); |
+ DCHECK(TryAddNewBufferToMap( |
+ buffers, new_id, BufferInfo(request->size, gfx::SHARED_MEMORY_BUFFER, |
+ request->format, request->usage, 0))); |
reveman
2015/08/10 22:16:14
Is this DCHECK safe? Is it not possible for a chil
ericrk
2015/08/11 01:40:07
The ID here is safe, as it's allocated and control
|
// Note: Unretained is safe as IO thread is stopped before manager is |
// destroyed. |
request->result = GpuMemoryBufferImplSharedMemory::Create( |
@@ -472,9 +475,6 @@ void BrowserGpuMemoryBufferManager::AllocateGpuMemoryBufferOnIO( |
const AllocationCallback& callback) { |
DCHECK_CURRENTLY_ON(BrowserThread::IO); |
- BufferMap& buffers = clients_[client_id]; |
- DCHECK(buffers.find(id) == buffers.end()); |
- |
GpuProcessHost* host = GpuProcessHost::FromID(gpu_host_id_); |
if (!host) { |
host = GpuProcessHost::Get(GpuProcessHost::GPU_PROCESS_KIND_SANDBOXED, |
@@ -499,10 +499,18 @@ void BrowserGpuMemoryBufferManager::AllocateGpuMemoryBufferOnIO( |
reused_gpu_process = true; |
} |
+ BufferMap& buffers = clients_[client_id]; |
+ |
// Note: Handling of cases where the client is removed before the allocation |
// completes is less subtle if we set the buffer type to EMPTY_BUFFER here |
// and verify that this has not changed when allocation completes. |
- buffers[id] = BufferInfo(size, gfx::EMPTY_BUFFER, format, usage, 0); |
+ if (!TryAddNewBufferToMap( |
+ buffers, id, BufferInfo(size, gfx::EMPTY_BUFFER, format, usage, 0))) { |
+ LOG(ERROR) << "Client process attempted to allocate a GpuMemoryBuffer with " |
+ "an existing ID. Failing allocation."; |
+ callback.Run(gfx::GpuMemoryBufferHandle()); |
+ return; |
+ } |
// Note: Unretained is safe as IO thread is stopped before manager is |
// destroyed. |
@@ -621,4 +629,15 @@ uint64_t BrowserGpuMemoryBufferManager::ClientIdToTracingProcessId( |
client_id); |
} |
+bool BrowserGpuMemoryBufferManager::TryAddNewBufferToMap( |
+ BufferMap& buffers, |
+ int id, |
+ const BufferInfo& buffer_info) { |
+ auto insert_result = buffers.insert(std::make_pair( |
+ id, buffer_info)); |
+ |
+ // Pair is returned. Second value is true if a new element was inserted. |
+ return insert_result.second; |
+} |
reveman
2015/08/10 22:16:14
I think "if (!buffers_.insert(make_pair(key, value
ericrk
2015/08/11 01:40:07
removed this... probably not worth the function...
|
+ |
} // namespace content |