1// Copyright (c) 2014 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "content/browser/gpu/gpu_memory_buffer_factory_host_impl.h"
6
7#include "base/bind.h"
8#include "content/browser/gpu/gpu_process_host.h"
9#include "content/public/browser/browser_thread.h"
10#include "ui/gfx/gpu_memory_buffer.h"
11
12namespace content {
13
14GpuMemoryBufferFactoryHostImpl::GpuMemoryBufferFactoryHostImpl()
15    : gpu_host_id_(0), next_create_gpu_memory_buffer_request_id_(0) {
16}
17
18GpuMemoryBufferFactoryHostImpl::~GpuMemoryBufferFactoryHostImpl() {
19}
20
21void GpuMemoryBufferFactoryHostImpl::CreateGpuMemoryBuffer(
22    const gfx::GpuMemoryBufferHandle& handle,
23    const gfx::Size& size,
24    unsigned internalformat,
25    unsigned usage,
26    const CreateGpuMemoryBufferCallback& callback) {
27  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
28
29  GpuProcessHost* host = GpuProcessHost::FromID(gpu_host_id_);
30  if (!host) {
31    callback.Run(gfx::GpuMemoryBufferHandle());
32    return;
33  }
34
35  uint32 request_id = next_create_gpu_memory_buffer_request_id_++;
36  create_gpu_memory_buffer_requests_[request_id] = callback;
37
38  host->CreateGpuMemoryBuffer(
39      handle,
40      size,
41      internalformat,
42      usage,
43      base::Bind(&GpuMemoryBufferFactoryHostImpl::OnGpuMemoryBufferCreated,
44                 base::Unretained(this),
45                 request_id));
46}
47
48void GpuMemoryBufferFactoryHostImpl::DestroyGpuMemoryBuffer(
49    const gfx::GpuMemoryBufferHandle& handle,
50    int32 sync_point) {
51  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
52
53  GpuProcessHost* host = GpuProcessHost::FromID(gpu_host_id_);
54  if (!host)
55    return;
56
57  host->DestroyGpuMemoryBuffer(handle, sync_point);
58}
59
60void GpuMemoryBufferFactoryHostImpl::OnGpuMemoryBufferCreated(
61    uint32 request_id,
62    const gfx::GpuMemoryBufferHandle& handle) {
63  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
64
65  CreateGpuMemoryBufferCallbackMap::iterator iter =
66      create_gpu_memory_buffer_requests_.find(request_id);
67  DCHECK(iter != create_gpu_memory_buffer_requests_.end());
68  iter->second.Run(handle);
69  create_gpu_memory_buffer_requests_.erase(iter);
70}
71
72}  // namespace content
73