Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(627)

Side by Side Diff: content/common/gpu/client/gpu_memory_buffer_impl_dma_buf.cc

Issue 288343004: working copy of "zero_copy" Base URL: http://src.chromium.org/svn/trunk/src/
Patch Set: Created 6 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright 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/common/gpu/client/gpu_memory_buffer_impl_dma_buf.h"
6
7 #define USE_MAP_GBM
8 #if defined(USE_MAP_GBM)
9 #include <gbm.h>
10 #endif
11 #include <sys/mman.h>
12 #include "base/logging.h"
13 #include "ui/gl/gl_bindings.h"
14 namespace content {
15
16 #if defined(USE_MAP_GBM)
17 class GBMWrapper {
18 public:
19 explicit GBMWrapper(int handle) {
20 gbm_ = gbm_create_device(handle);
21 if (!gbm_) {
22 LOG(ERROR) << "GBMWrapper: Failed to create GBM device.";
23 }
24 else {
25 fprintf(stderr, "GBM ready!\n");
26 }
27 }
28
29 ~GBMWrapper() {
30 if (gbm_)
31 gbm_device_destroy(gbm_);
32 }
33
34 struct gbm_device* GBMDevice() const { return gbm_; }
35 private:
36 struct gbm_device* gbm_;
37 };
38
39 GBMWrapper* GpuMemoryBufferImplDMABuf::wrapper_ = NULL;
40 #endif
41
42 GpuMemoryBufferImplDMABuf::GpuMemoryBufferImplDMABuf(
43 gfx::Size size, unsigned internalformat)
44 : GpuMemoryBufferImpl(size, internalformat),
45 dma_buf_id_(0),
46 mapped_addr_(NULL),
47 stride_(0),
48 tex_bo_(NULL) {
49 }
50
51 GpuMemoryBufferImplDMABuf::~GpuMemoryBufferImplDMABuf() {
52 #if defined(USE_MAP_GBM)
53 if (tex_bo_) {
54 gbm_bo_destroy(tex_bo_);
55 }
56 #endif
57 if (dma_buf_id_)
58 close(dma_buf_id_);
59 }
60
61 // static
62 bool GpuMemoryBufferImplDMABuf::IsFormatSupported(unsigned internalformat) {
63 switch (internalformat) {
64 case GL_BGRA8_EXT:
65 return true;
66 default:
67 return false;
68 }
69 }
70
71 bool GpuMemoryBufferImplDMABuf::Initialize(
72 gfx::GpuMemoryBufferHandle handle) {
73 dma_buf_id_ = handle.handle.fd;
74 stride_ = handle.stride;
75 #if defined(USE_MAP_GBM)
76 if (!wrapper_)
77 wrapper_ = new GBMWrapper(handle.card_handle.fd);
78 DCHECK(!tex_bo_);
79 struct gbm_device* gbm = wrapper_->GBMDevice();
80
81 gbm_import_fd_data data;
82 data.fd = dma_buf_id_;
83 data.width = size_.width();
84 data.height = size_.height();
85 data.stride = stride_;
86 data.format = GBM_FORMAT_ARGB8888;
87 tex_bo_ = gbm_bo_import(gbm,
88 GBM_BO_IMPORT_FD,
89 reinterpret_cast<void**>(&data),
90 GBM_BO_USE_MAP);
91 if (!tex_bo_) {
92 fprintf(stderr, "FAIL IMPORT!\n");
93 fflush(stderr);
94 return false;
95 }
96 #endif
97 return true;
98 }
99
100 void* GpuMemoryBufferImplDMABuf::Map() {
101 #if defined(USE_MAP_GBM)
102 DCHECK(!mapped_ && tex_bo_);
103 mapped_addr_ = gbm_bo_map(tex_bo_);
104 #else
105 DCHECK(!mapped_ && dma_buf_id_);
106 mapped_addr_ = mmap(NULL,
107 stride_,
108 PROT_WRITE,
109 MAP_SHARED,
110 dma_buf_id_,
111 0);
112 #endif
113
114 if (mapped_addr_ == MAP_FAILED || mapped_addr_ == NULL) {
115 LOG(ERROR) << "GpuMemoryBufferImplDMABuf: Failed to map GBM buffer.";
116 fprintf(stderr, "FAIL MAP!\n");
117 fflush(stderr);
118 mapped_addr_ = NULL;
119 return NULL;
120 }
121
122 #ifdef __DEBUG__
123 fprintf(stderr, "MAP to %p!\n", mapped_addr_);
124 fflush(stderr);
125 #endif
126
127 mapped_ = true;
128 return mapped_addr_;
129 }
130
131 void GpuMemoryBufferImplDMABuf::Unmap() {
132 DCHECK(mapped_ && mapped_addr_);
133 #if defined(USE_MAP_GBM)
134 DCHECK(tex_bo_);
135 gbm_bo_unmap(tex_bo_);
136 #else
137 if (dma_buf_id_) {
138 munmap(mapped_addr_, stride_);
139 mapped_addr_ = NULL;
140 }
141 #endif
142 mapped_ = false;
143 }
144
145 gfx::GpuMemoryBufferHandle GpuMemoryBufferImplDMABuf::GetHandle() const {
146 gfx::GpuMemoryBufferHandle handle;
147 handle.type = gfx::DMA_BUFFER;
148 handle.handle.fd = dma_buf_id_;
149 handle.stride = stride_;
150 return handle;
151 }
152
153 uint32 GpuMemoryBufferImplDMABuf::GetStride() const {
154 return stride_;
155 }
156
157 } // namespace content
OLDNEW
« no previous file with comments | « content/common/gpu/client/gpu_memory_buffer_impl_dma_buf.h ('k') | content/common/gpu/client/gpu_memory_buffer_impl_linux.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698