summaryrefslogtreecommitdiffstats
path: root/content/browser/gpu/browser_gpu_memory_buffer_manager.cc
blob: 777f508ad091e3f423433149698e84eaeeb1bc31 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "content/browser/gpu/browser_gpu_memory_buffer_manager.h"

#include "base/atomic_sequence_num.h"
#include "base/bind.h"
#include "base/command_line.h"
#include "base/strings/stringprintf.h"
#include "base/synchronization/waitable_event.h"
#include "base/threading/thread_restrictions.h"
#include "base/trace_event/process_memory_dump.h"
#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"
#include "content/public/browser/browser_thread.h"
#include "content/public/common/content_switches.h"
#include "gpu/GLES2/gl2extchromium.h"
#include "ui/gfx/buffer_format_util.h"
#include "ui/gl/gl_switches.h"

#if defined(OS_MACOSX)
#include "content/common/gpu/gpu_memory_buffer_factory_io_surface.h"
#endif

#if defined(OS_ANDROID)
#include "content/common/gpu/gpu_memory_buffer_factory_surface_texture.h"
#endif

#if defined(USE_OZONE)
#include "content/common/gpu/gpu_memory_buffer_factory_ozone_native_pixmap.h"
#endif

namespace content {
namespace {

void GpuMemoryBufferDeleted(
    scoped_refptr<base::SingleThreadTaskRunner> destruction_task_runner,
    const GpuMemoryBufferImpl::DestructionCallback& destruction_callback,
    uint32 sync_point) {
  destruction_task_runner->PostTask(
      FROM_HERE, base::Bind(destruction_callback, sync_point));
}

bool IsGpuMemoryBufferFactoryConfigurationSupported(
    gfx::GpuMemoryBufferType type,
    const GpuMemoryBufferFactory::Configuration& configuration) {
  switch (type) {
    case gfx::SHARED_MEMORY_BUFFER:
      return GpuMemoryBufferFactorySharedMemory::
          IsGpuMemoryBufferConfigurationSupported(configuration.format,
                                                  configuration.usage);
#if defined(OS_MACOSX)
    case gfx::IO_SURFACE_BUFFER:
      return GpuMemoryBufferFactoryIOSurface::
          IsGpuMemoryBufferConfigurationSupported(configuration.format,
                                                  configuration.usage);
#endif
#if defined(OS_ANDROID)
    case gfx::SURFACE_TEXTURE_BUFFER:
      return GpuMemoryBufferFactorySurfaceTexture::
          IsGpuMemoryBufferConfigurationSupported(configuration.format,
                                                  configuration.usage);
#endif
#if defined(USE_OZONE)
    case gfx::OZONE_NATIVE_PIXMAP:
      return GpuMemoryBufferFactoryOzoneNativePixmap::
          IsGpuMemoryBufferConfigurationSupported(configuration.format,
                                                  configuration.usage);
#endif
    default:
      NOTREACHED();
      return false;
  }
}

gfx::GpuMemoryBufferType GetGpuMemoryBufferFactoryType() {
  std::vector<gfx::GpuMemoryBufferType> supported_types;
  GpuMemoryBufferFactory::GetSupportedTypes(&supported_types);
  DCHECK(!supported_types.empty());

  // The GPU service will always use the preferred type.
  return supported_types[0];
}

std::vector<GpuMemoryBufferFactory::Configuration>
GetSupportedGpuMemoryBufferConfigurations(gfx::GpuMemoryBufferType type) {
  std::vector<GpuMemoryBufferFactory::Configuration> configurations;
#if defined(OS_MACOSX)
  bool enable_native_gpu_memory_buffers =
      !base::CommandLine::ForCurrentProcess()->HasSwitch(
          switches::kDisableNativeGpuMemoryBuffers);
#else
  bool enable_native_gpu_memory_buffers =
      base::CommandLine::ForCurrentProcess()->HasSwitch(
          switches::kEnableNativeGpuMemoryBuffers);
#endif

  // Disable native buffers when using Mesa.
  if (base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
          switches::kUseGL) == gfx::kGLImplementationOSMesaName) {
    enable_native_gpu_memory_buffers = false;
  }

  if (enable_native_gpu_memory_buffers) {
    const GpuMemoryBufferFactory::Configuration kNativeConfigurations[] = {
        {gfx::BufferFormat::R_8, gfx::BufferUsage::MAP},
        {gfx::BufferFormat::R_8, gfx::BufferUsage::PERSISTENT_MAP},
        {gfx::BufferFormat::RGBA_4444, gfx::BufferUsage::MAP},
        {gfx::BufferFormat::RGBA_4444, gfx::BufferUsage::PERSISTENT_MAP},
        {gfx::BufferFormat::RGBA_8888, gfx::BufferUsage::MAP},
        {gfx::BufferFormat::RGBA_8888, gfx::BufferUsage::PERSISTENT_MAP},
        {gfx::BufferFormat::BGRA_8888, gfx::BufferUsage::MAP},
        {gfx::BufferFormat::BGRA_8888, gfx::BufferUsage::PERSISTENT_MAP},
        {gfx::BufferFormat::UYVY_422, gfx::BufferUsage::MAP},
        {gfx::BufferFormat::UYVY_422, gfx::BufferUsage::PERSISTENT_MAP},
        {gfx::BufferFormat::YUV_420_BIPLANAR, gfx::BufferUsage::MAP},
        {gfx::BufferFormat::YUV_420_BIPLANAR, gfx::BufferUsage::PERSISTENT_MAP},
    };
    for (auto& configuration : kNativeConfigurations) {
      if (IsGpuMemoryBufferFactoryConfigurationSupported(type, configuration))
        configurations.push_back(configuration);
    }
  }

#if defined(USE_OZONE) || defined(OS_MACOSX)
  const GpuMemoryBufferFactory::Configuration kScanoutConfigurations[] = {
      {gfx::BufferFormat::BGRA_8888, gfx::BufferUsage::SCANOUT},
      {gfx::BufferFormat::BGRX_8888, gfx::BufferUsage::SCANOUT},
      {gfx::BufferFormat::UYVY_422, gfx::BufferUsage::SCANOUT},
      {gfx::BufferFormat::YUV_420_BIPLANAR, gfx::BufferUsage::SCANOUT},
  };
  for (auto& configuration : kScanoutConfigurations) {
    if (IsGpuMemoryBufferFactoryConfigurationSupported(type, configuration))
      configurations.push_back(configuration);
  }
#endif

  return configurations;
}

BrowserGpuMemoryBufferManager* g_gpu_memory_buffer_manager = nullptr;

}  // namespace

struct BrowserGpuMemoryBufferManager::AllocateGpuMemoryBufferRequest {
  AllocateGpuMemoryBufferRequest(const gfx::Size& size,
                                 gfx::BufferFormat format,
                                 gfx::BufferUsage usage,
                                 int client_id,
                                 int surface_id)
      : event(true, false),
        size(size),
        format(format),
        usage(usage),
        client_id(client_id),
        surface_id(surface_id) {}
  ~AllocateGpuMemoryBufferRequest() {}
  base::WaitableEvent event;
  gfx::Size size;
  gfx::BufferFormat format;
  gfx::BufferUsage usage;
  int client_id;
  int surface_id;
  scoped_ptr<gfx::GpuMemoryBuffer> result;
};

BrowserGpuMemoryBufferManager::BrowserGpuMemoryBufferManager(
    int gpu_client_id,
    uint64_t gpu_client_tracing_id)
    : factory_type_(GetGpuMemoryBufferFactoryType()),
      supported_configurations_(
          GetSupportedGpuMemoryBufferConfigurations(factory_type_)),
      gpu_client_id_(gpu_client_id),
      gpu_client_tracing_id_(gpu_client_tracing_id),
      gpu_host_id_(0) {
  DCHECK(!g_gpu_memory_buffer_manager);
  g_gpu_memory_buffer_manager = this;
}

BrowserGpuMemoryBufferManager::~BrowserGpuMemoryBufferManager() {
  g_gpu_memory_buffer_manager = nullptr;
}

// static
BrowserGpuMemoryBufferManager* BrowserGpuMemoryBufferManager::current() {
  return g_gpu_memory_buffer_manager;
}

// static
uint32 BrowserGpuMemoryBufferManager::GetImageTextureTarget(
    gfx::BufferFormat format,
    gfx::BufferUsage usage) {
  gfx::GpuMemoryBufferType type = GetGpuMemoryBufferFactoryType();
  for (auto& configuration : GetSupportedGpuMemoryBufferConfigurations(type)) {
    if (configuration.format != format || configuration.usage != usage)
      continue;

    switch (type) {
      case gfx::SURFACE_TEXTURE_BUFFER:
      case gfx::OZONE_NATIVE_PIXMAP:
        // GPU memory buffers that are shared with the GL using EGLImages
        // require TEXTURE_EXTERNAL_OES.
        return GL_TEXTURE_EXTERNAL_OES;
      case gfx::IO_SURFACE_BUFFER:
        // IOSurface backed images require GL_TEXTURE_RECTANGLE_ARB.
        return GL_TEXTURE_RECTANGLE_ARB;
      default:
        return GL_TEXTURE_2D;
    }
  }

  return GL_TEXTURE_2D;
}

scoped_ptr<gfx::GpuMemoryBuffer>
BrowserGpuMemoryBufferManager::AllocateGpuMemoryBuffer(const gfx::Size& size,
                                                       gfx::BufferFormat format,
                                                       gfx::BufferUsage usage) {
  return AllocateGpuMemoryBufferForSurface(size, format, usage, 0);
}

scoped_ptr<gfx::GpuMemoryBuffer>
BrowserGpuMemoryBufferManager::AllocateGpuMemoryBufferForScanout(
    const gfx::Size& size,
    gfx::BufferFormat format,
    int32 surface_id) {
  DCHECK_GT(surface_id, 0);
  return AllocateGpuMemoryBufferForSurface(
      size, format, gfx::BufferUsage::SCANOUT, surface_id);
}

void BrowserGpuMemoryBufferManager::AllocateGpuMemoryBufferForChildProcess(
    gfx::GpuMemoryBufferId id,
    const gfx::Size& size,
    gfx::BufferFormat format,
    gfx::BufferUsage usage,
    base::ProcessHandle child_process_handle,
    int child_client_id,
    const AllocationCallback& callback) {
  DCHECK_CURRENTLY_ON(BrowserThread::IO);

  // Use service side allocation if this is a supported configuration.
  if (IsGpuMemoryBufferConfigurationSupported(format, usage)) {
    AllocateGpuMemoryBufferOnIO(id, size, format, usage, child_client_id, 0,
                                false, callback);
    return;
  }

  // Early out if we cannot fallback to shared memory buffer.
  if (!GpuMemoryBufferImplSharedMemory::IsFormatSupported(format) ||
      !GpuMemoryBufferImplSharedMemory::IsUsageSupported(usage) ||
      !GpuMemoryBufferImplSharedMemory::IsSizeValidForFormat(size, format)) {
    callback.Run(gfx::GpuMemoryBufferHandle());
    return;
  }

  BufferMap& buffers = clients_[child_client_id];

  // Allocate shared memory buffer as fallback.
  auto insert_result = buffers.insert(std::make_pair(
      id, BufferInfo(size, gfx::SHARED_MEMORY_BUFFER, format, usage, 0)));
  if (!insert_result.second) {
    DLOG(ERROR) << "Child process attempted to allocate a GpuMemoryBuffer with "
                   "an existing ID.";
    callback.Run(gfx::GpuMemoryBufferHandle());
    return;
  }

  callback.Run(GpuMemoryBufferImplSharedMemory::AllocateForChildProcess(
      id, size, format, child_process_handle));
}

gfx::GpuMemoryBuffer*
BrowserGpuMemoryBufferManager::GpuMemoryBufferFromClientBuffer(
    ClientBuffer buffer) {
  return GpuMemoryBufferImpl::FromClientBuffer(buffer);
}

void BrowserGpuMemoryBufferManager::SetDestructionSyncPoint(
    gfx::GpuMemoryBuffer* buffer,
    uint32 sync_point) {
  static_cast<GpuMemoryBufferImpl*>(buffer)
      ->set_destruction_sync_point(sync_point);
}

bool BrowserGpuMemoryBufferManager::OnMemoryDump(
    const base::trace_event::MemoryDumpArgs& args,
    base::trace_event::ProcessMemoryDump* pmd) {
  DCHECK_CURRENTLY_ON(BrowserThread::IO);

  for (const auto& client : clients_) {
    int client_id = client.first;

    for (const auto& buffer : client.second) {
      if (buffer.second.type == gfx::EMPTY_BUFFER)
        continue;

      gfx::GpuMemoryBufferId buffer_id = buffer.first;
      base::trace_event::MemoryAllocatorDump* dump =
          pmd->CreateAllocatorDump(base::StringPrintf(
              "gpumemorybuffer/client_%d/buffer_%d", client_id, buffer_id.id));
      if (!dump)
        return false;

      size_t buffer_size_in_bytes = gfx::BufferSizeForBufferFormat(
          buffer.second.size, buffer.second.format);
      dump->AddScalar(base::trace_event::MemoryAllocatorDump::kNameSize,
                      base::trace_event::MemoryAllocatorDump::kUnitsBytes,
                      buffer_size_in_bytes);

      // Create the cross-process ownership edge. If the client creates a
      // corresponding dump for the same buffer, this will avoid to
      // double-count them in tracing. If, instead, no other process will emit a
      // dump with the same guid, the segment will be accounted to the browser.
      uint64 client_tracing_process_id = ClientIdToTracingProcessId(client_id);

      base::trace_event::MemoryAllocatorDumpGuid shared_buffer_guid =
          gfx::GetGpuMemoryBufferGUIDForTracing(client_tracing_process_id,
                                                buffer_id);
      pmd->CreateSharedGlobalAllocatorDump(shared_buffer_guid);
      pmd->AddOwnershipEdge(dump->guid(), shared_buffer_guid);
    }
  }

  return true;
}

void BrowserGpuMemoryBufferManager::ChildProcessDeletedGpuMemoryBuffer(
    gfx::GpuMemoryBufferId id,
    base::ProcessHandle child_process_handle,
    int child_client_id,
    uint32 sync_point) {
  DCHECK_CURRENTLY_ON(BrowserThread::IO);

  DestroyGpuMemoryBufferOnIO(id, child_client_id, sync_point);
}

void BrowserGpuMemoryBufferManager::ProcessRemoved(
    base::ProcessHandle process_handle,
    int client_id) {
  DCHECK_CURRENTLY_ON(BrowserThread::IO);

  ClientMap::iterator client_it = clients_.find(client_id);
  if (client_it == clients_.end())
    return;

  for (const auto& buffer : client_it->second) {
    // This might happen if buffer is currenlty in the process of being
    // allocated. The buffer will in that case be cleaned up when allocation
    // completes.
    if (buffer.second.type == gfx::EMPTY_BUFFER)
      continue;

    GpuProcessHost* host = GpuProcessHost::FromID(buffer.second.gpu_host_id);
    if (host)
      host->DestroyGpuMemoryBuffer(buffer.first, client_id, 0);
  }

  clients_.erase(client_it);
}

scoped_ptr<gfx::GpuMemoryBuffer>
BrowserGpuMemoryBufferManager::AllocateGpuMemoryBufferForSurface(
    const gfx::Size& size,
    gfx::BufferFormat format,
    gfx::BufferUsage usage,
    int32 surface_id) {
  DCHECK(!BrowserThread::CurrentlyOn(BrowserThread::IO));

  AllocateGpuMemoryBufferRequest request(size, format, usage, gpu_client_id_,
                                         surface_id);
  BrowserThread::PostTask(
      BrowserThread::IO, FROM_HERE,
      base::Bind(
          &BrowserGpuMemoryBufferManager::AllocateGpuMemoryBufferForSurfaceOnIO,
          base::Unretained(this),  // Safe as we wait for result below.
          base::Unretained(&request)));

  // We're blocking the UI thread, which is generally undesirable.
  TRACE_EVENT0(
      "browser",
      "BrowserGpuMemoryBufferManager::AllocateGpuMemoryBufferForSurface");
  base::ThreadRestrictions::ScopedAllowWait allow_wait;
  request.event.Wait();
  return request.result.Pass();
}

bool BrowserGpuMemoryBufferManager::IsGpuMemoryBufferConfigurationSupported(
    gfx::BufferFormat format,
    gfx::BufferUsage usage) const {
  for (auto& configuration : supported_configurations_) {
    if (configuration.format == format && configuration.usage == usage)
      return true;
  }
  return false;
}

void BrowserGpuMemoryBufferManager::AllocateGpuMemoryBufferForSurfaceOnIO(
    AllocateGpuMemoryBufferRequest* request) {
  DCHECK_CURRENTLY_ON(BrowserThread::IO);

  gfx::GpuMemoryBufferId new_id = content::GetNextGenericSharedMemoryId();

  // Use service side allocation if this is a supported configuration.
  if (IsGpuMemoryBufferConfigurationSupported(request->format,
                                              request->usage)) {
    // Note: Unretained is safe as this is only used for synchronous allocation
    // from a non-IO thread.
    AllocateGpuMemoryBufferOnIO(
        new_id, request->size, request->format, request->usage,
        request->client_id, request->surface_id, false,
        base::Bind(&BrowserGpuMemoryBufferManager::
                       GpuMemoryBufferAllocatedForSurfaceOnIO,
                   base::Unretained(this), base::Unretained(request)));
    return;
  }

  DCHECK(GpuMemoryBufferImplSharedMemory::IsFormatSupported(request->format))
      << static_cast<int>(request->format);
  DCHECK(GpuMemoryBufferImplSharedMemory::IsUsageSupported(request->usage))
      << static_cast<int>(request->usage);

  BufferMap& buffers = clients_[request->client_id];

  // Allocate shared memory buffer as fallback.
  auto insert_result = buffers.insert(std::make_pair(
      new_id, BufferInfo(request->size, gfx::SHARED_MEMORY_BUFFER,
                         request->format, request->usage, 0)));
  DCHECK(insert_result.second);

  // Note: Unretained is safe as IO thread is stopped before manager is
  // destroyed.
  request->result = GpuMemoryBufferImplSharedMemory::Create(
      new_id, request->size, request->format,
      base::Bind(
          &GpuMemoryBufferDeleted,
          BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO),
          base::Bind(&BrowserGpuMemoryBufferManager::DestroyGpuMemoryBufferOnIO,
                     base::Unretained(this), new_id, request->client_id)));
  request->event.Signal();
}

void BrowserGpuMemoryBufferManager::GpuMemoryBufferAllocatedForSurfaceOnIO(
    AllocateGpuMemoryBufferRequest* request,
    const gfx::GpuMemoryBufferHandle& handle) {
  DCHECK_CURRENTLY_ON(BrowserThread::IO);

  // Early out if factory failed to allocate the buffer.
  if (handle.is_null()) {
    request->event.Signal();
    return;
  }

  // Note: Unretained is safe as IO thread is stopped before manager is
  // destroyed.
  request->result = GpuMemoryBufferImpl::CreateFromHandle(
      handle, request->size, request->format, request->usage,
      base::Bind(
          &GpuMemoryBufferDeleted,
          BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO),
          base::Bind(&BrowserGpuMemoryBufferManager::DestroyGpuMemoryBufferOnIO,
                     base::Unretained(this), handle.id, request->client_id)));
  request->event.Signal();
}

void BrowserGpuMemoryBufferManager::AllocateGpuMemoryBufferOnIO(
    gfx::GpuMemoryBufferId id,
    const gfx::Size& size,
    gfx::BufferFormat format,
    gfx::BufferUsage usage,
    int client_id,
    int surface_id,
    bool reused_gpu_process,
    const AllocationCallback& callback) {
  DCHECK_CURRENTLY_ON(BrowserThread::IO);

  GpuProcessHost* host = GpuProcessHost::FromID(gpu_host_id_);
  if (!host) {
    host = GpuProcessHost::Get(GpuProcessHost::GPU_PROCESS_KIND_SANDBOXED,
                               CAUSE_FOR_GPU_LAUNCH_GPU_MEMORY_BUFFER_ALLOCATE);
    if (!host) {
      LOG(ERROR) << "Failed to launch GPU process.";
      callback.Run(gfx::GpuMemoryBufferHandle());
      return;
    }
    gpu_host_id_ = host->host_id();
    reused_gpu_process = false;
  } else {
    if (reused_gpu_process) {
      // We come here if we retried to allocate the buffer because of a
      // failure in GpuMemoryBufferAllocatedOnIO, but we ended up with the
      // same process ID, meaning the failure was not because of a channel
      // error, but another reason. So fail now.
      LOG(ERROR) << "Failed to allocate GpuMemoryBuffer.";
      callback.Run(gfx::GpuMemoryBufferHandle());
      return;
    }
    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.
  auto insert_result = buffers.insert(std::make_pair(
      id, BufferInfo(size, gfx::EMPTY_BUFFER, format, usage, 0)));
  if (!insert_result.second) {
    DLOG(ERROR) << "Child process attempted to allocate a GpuMemoryBuffer with "
                   "an existing ID.";
    callback.Run(gfx::GpuMemoryBufferHandle());
    return;
  }

  // Note: Unretained is safe as IO thread is stopped before manager is
  // destroyed.
  host->CreateGpuMemoryBuffer(
      id, size, format, usage, client_id, surface_id,
      base::Bind(&BrowserGpuMemoryBufferManager::GpuMemoryBufferAllocatedOnIO,
                 base::Unretained(this), id, client_id, surface_id,
                 gpu_host_id_, reused_gpu_process, callback));
}

void BrowserGpuMemoryBufferManager::GpuMemoryBufferAllocatedOnIO(
    gfx::GpuMemoryBufferId id,
    int client_id,
    int surface_id,
    int gpu_host_id,
    bool reused_gpu_process,
    const AllocationCallback& callback,
    const gfx::GpuMemoryBufferHandle& handle) {
  DCHECK_CURRENTLY_ON(BrowserThread::IO);

  ClientMap::iterator client_it = clients_.find(client_id);

  // This can happen if client is removed while the buffer is being allocated.
  if (client_it == clients_.end()) {
    if (!handle.is_null()) {
      GpuProcessHost* host = GpuProcessHost::FromID(gpu_host_id);
      if (host)
        host->DestroyGpuMemoryBuffer(handle.id, client_id, 0);
    }
    callback.Run(gfx::GpuMemoryBufferHandle());
    return;
  }

  BufferMap& buffers = client_it->second;

  BufferMap::iterator buffer_it = buffers.find(id);
  DCHECK(buffer_it != buffers.end());
  DCHECK_EQ(buffer_it->second.type, gfx::EMPTY_BUFFER);

  // If the handle isn't valid, that means that the GPU process crashed or is
  // misbehaving.
  bool valid_handle = !handle.is_null() && handle.id == id;
  if (!valid_handle) {
    // If we failed after re-using the GPU process, it may have died in the
    // mean time. Retry to have a chance to create a fresh GPU process.
    if (handle.is_null() && reused_gpu_process) {
      DVLOG(1) << "Failed to create buffer through existing GPU process. "
                  "Trying to restart GPU process.";
      // If the GPU process has already been restarted, retry without failure
      // when GPU process host ID already exists.
      if (gpu_host_id != gpu_host_id_)
        reused_gpu_process = false;
      gfx::Size size = buffer_it->second.size;
      gfx::BufferFormat format = buffer_it->second.format;
      gfx::BufferUsage usage = buffer_it->second.usage;
      // Remove the buffer entry and call AllocateGpuMemoryBufferOnIO again.
      buffers.erase(buffer_it);
      AllocateGpuMemoryBufferOnIO(id, size, format, usage, client_id,
                                  surface_id, reused_gpu_process, callback);
    } else {
      // Remove the buffer entry and run the allocation callback with an empty
      // handle to indicate failure.
      buffers.erase(buffer_it);
      callback.Run(gfx::GpuMemoryBufferHandle());
    }
    return;
  }

  // Store the type and host id of this buffer so it can be cleaned up if the
  // client is removed.
  buffer_it->second.type = handle.type;
  buffer_it->second.gpu_host_id = gpu_host_id;

  callback.Run(handle);
}

void BrowserGpuMemoryBufferManager::DestroyGpuMemoryBufferOnIO(
    gfx::GpuMemoryBufferId id,
    int client_id,
    uint32 sync_point) {
  DCHECK_CURRENTLY_ON(BrowserThread::IO);
  DCHECK(clients_.find(client_id) != clients_.end());

  BufferMap& buffers = clients_[client_id];

  BufferMap::iterator buffer_it = buffers.find(id);
  if (buffer_it == buffers.end()) {
    LOG(ERROR) << "Invalid GpuMemoryBuffer ID for client.";
    return;
  }

  // This can happen if a client managed to call this while a buffer is in the
  // process of being allocated.
  if (buffer_it->second.type == gfx::EMPTY_BUFFER) {
    LOG(ERROR) << "Invalid GpuMemoryBuffer type.";
    return;
  }

  GpuProcessHost* host = GpuProcessHost::FromID(buffer_it->second.gpu_host_id);
  if (host)
    host->DestroyGpuMemoryBuffer(id, client_id, sync_point);

  buffers.erase(buffer_it);
}

uint64_t BrowserGpuMemoryBufferManager::ClientIdToTracingProcessId(
    int client_id) const {
  if (client_id == gpu_client_id_) {
    // The gpu_client uses a fixed tracing ID.
    return gpu_client_tracing_id_;
  }

  // In normal cases, |client_id| is a child process id, so we can perform
  // the standard conversion.
  return ChildProcessHostImpl::ChildProcessUniqueIdToTracingProcessId(
      client_id);
}

}  // namespace content