Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / content / browser / gpu / gpu_memory_buffer_factory_host_impl.cc
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
12 namespace content {
13
14 GpuMemoryBufferFactoryHostImpl::GpuMemoryBufferFactoryHostImpl()
15     : gpu_host_id_(0), next_create_gpu_memory_buffer_request_id_(0) {
16 }
17
18 GpuMemoryBufferFactoryHostImpl::~GpuMemoryBufferFactoryHostImpl() {
19 }
20
21 void GpuMemoryBufferFactoryHostImpl::CreateGpuMemoryBuffer(
22     gfx::GpuMemoryBufferType type,
23     gfx::GpuMemoryBufferId id,
24     const gfx::Size& size,
25     gfx::GpuMemoryBuffer::Format format,
26     gfx::GpuMemoryBuffer::Usage usage,
27     int client_id,
28     const CreateGpuMemoryBufferCallback& callback) {
29   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
30
31   GpuProcessHost* host = GpuProcessHost::FromID(gpu_host_id_);
32   if (!host) {
33     callback.Run(gfx::GpuMemoryBufferHandle());
34     return;
35   }
36
37   uint32 request_id = next_create_gpu_memory_buffer_request_id_++;
38   create_gpu_memory_buffer_requests_[request_id] = callback;
39
40   host->CreateGpuMemoryBuffer(
41       type,
42       id,
43       size,
44       format,
45       usage,
46       client_id,
47       base::Bind(&GpuMemoryBufferFactoryHostImpl::OnGpuMemoryBufferCreated,
48                  base::Unretained(this),
49                  request_id));
50 }
51
52 void GpuMemoryBufferFactoryHostImpl::DestroyGpuMemoryBuffer(
53     gfx::GpuMemoryBufferType type,
54     gfx::GpuMemoryBufferId id,
55     int client_id,
56     int32 sync_point) {
57   BrowserThread::PostTask(
58       BrowserThread::IO,
59       FROM_HERE,
60       base::Bind(&GpuMemoryBufferFactoryHostImpl::DestroyGpuMemoryBufferOnIO,
61                  base::Unretained(this),
62                  type,
63                  id,
64                  client_id,
65                  sync_point));
66 }
67
68 void GpuMemoryBufferFactoryHostImpl::DestroyGpuMemoryBufferOnIO(
69     gfx::GpuMemoryBufferType type,
70     gfx::GpuMemoryBufferId id,
71     int client_id,
72     int32 sync_point) {
73   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
74
75   GpuProcessHost* host = GpuProcessHost::FromID(gpu_host_id_);
76   if (!host)
77     return;
78
79   host->DestroyGpuMemoryBuffer(type, id, client_id, sync_point);
80 }
81
82 void GpuMemoryBufferFactoryHostImpl::OnGpuMemoryBufferCreated(
83     uint32 request_id,
84     const gfx::GpuMemoryBufferHandle& handle) {
85   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
86
87   CreateGpuMemoryBufferCallbackMap::iterator iter =
88       create_gpu_memory_buffer_requests_.find(request_id);
89   DCHECK(iter != create_gpu_memory_buffer_requests_.end());
90   iter->second.Run(handle);
91   create_gpu_memory_buffer_requests_.erase(iter);
92 }
93
94 }  // namespace content