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

Unified 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 side-by-side diff with in-line comments
Download patch
Index: content/common/gpu/client/gpu_memory_buffer_impl_dma_buf.cc
===================================================================
--- content/common/gpu/client/gpu_memory_buffer_impl_dma_buf.cc (revision 0)
+++ content/common/gpu/client/gpu_memory_buffer_impl_dma_buf.cc (revision 0)
@@ -0,0 +1,157 @@
+// 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/common/gpu/client/gpu_memory_buffer_impl_dma_buf.h"
+
+#define USE_MAP_GBM
+#if defined(USE_MAP_GBM)
+#include <gbm.h>
+#endif
+#include <sys/mman.h>
+#include "base/logging.h"
+#include "ui/gl/gl_bindings.h"
+namespace content {
+
+#if defined(USE_MAP_GBM)
+class GBMWrapper {
+ public:
+ explicit GBMWrapper(int handle) {
+ gbm_ = gbm_create_device(handle);
+ if (!gbm_) {
+ LOG(ERROR) << "GBMWrapper: Failed to create GBM device.";
+ }
+ else {
+ fprintf(stderr, "GBM ready!\n");
+ }
+ }
+
+ ~GBMWrapper() {
+ if (gbm_)
+ gbm_device_destroy(gbm_);
+ }
+
+ struct gbm_device* GBMDevice() const { return gbm_; }
+ private:
+ struct gbm_device* gbm_;
+};
+
+GBMWrapper* GpuMemoryBufferImplDMABuf::wrapper_ = NULL;
+#endif
+
+GpuMemoryBufferImplDMABuf::GpuMemoryBufferImplDMABuf(
+ gfx::Size size, unsigned internalformat)
+ : GpuMemoryBufferImpl(size, internalformat),
+ dma_buf_id_(0),
+ mapped_addr_(NULL),
+ stride_(0),
+ tex_bo_(NULL) {
+}
+
+GpuMemoryBufferImplDMABuf::~GpuMemoryBufferImplDMABuf() {
+#if defined(USE_MAP_GBM)
+ if (tex_bo_) {
+ gbm_bo_destroy(tex_bo_);
+ }
+#endif
+ if (dma_buf_id_)
+ close(dma_buf_id_);
+}
+
+// static
+bool GpuMemoryBufferImplDMABuf::IsFormatSupported(unsigned internalformat) {
+ switch (internalformat) {
+ case GL_BGRA8_EXT:
+ return true;
+ default:
+ return false;
+ }
+}
+
+bool GpuMemoryBufferImplDMABuf::Initialize(
+ gfx::GpuMemoryBufferHandle handle) {
+ dma_buf_id_ = handle.handle.fd;
+ stride_ = handle.stride;
+#if defined(USE_MAP_GBM)
+ if (!wrapper_)
+ wrapper_ = new GBMWrapper(handle.card_handle.fd);
+ DCHECK(!tex_bo_);
+ struct gbm_device* gbm = wrapper_->GBMDevice();
+
+ gbm_import_fd_data data;
+ data.fd = dma_buf_id_;
+ data.width = size_.width();
+ data.height = size_.height();
+ data.stride = stride_;
+ data.format = GBM_FORMAT_ARGB8888;
+ tex_bo_ = gbm_bo_import(gbm,
+ GBM_BO_IMPORT_FD,
+ reinterpret_cast<void**>(&data),
+ GBM_BO_USE_MAP);
+ if (!tex_bo_) {
+ fprintf(stderr, "FAIL IMPORT!\n");
+ fflush(stderr);
+ return false;
+ }
+#endif
+ return true;
+}
+
+void* GpuMemoryBufferImplDMABuf::Map() {
+#if defined(USE_MAP_GBM)
+ DCHECK(!mapped_ && tex_bo_);
+ mapped_addr_ = gbm_bo_map(tex_bo_);
+#else
+ DCHECK(!mapped_ && dma_buf_id_);
+ mapped_addr_ = mmap(NULL,
+ stride_,
+ PROT_WRITE,
+ MAP_SHARED,
+ dma_buf_id_,
+ 0);
+#endif
+
+ if (mapped_addr_ == MAP_FAILED || mapped_addr_ == NULL) {
+ LOG(ERROR) << "GpuMemoryBufferImplDMABuf: Failed to map GBM buffer.";
+ fprintf(stderr, "FAIL MAP!\n");
+ fflush(stderr);
+ mapped_addr_ = NULL;
+ return NULL;
+ }
+
+#ifdef __DEBUG__
+ fprintf(stderr, "MAP to %p!\n", mapped_addr_);
+ fflush(stderr);
+#endif
+
+ mapped_ = true;
+ return mapped_addr_;
+}
+
+void GpuMemoryBufferImplDMABuf::Unmap() {
+ DCHECK(mapped_ && mapped_addr_);
+#if defined(USE_MAP_GBM)
+ DCHECK(tex_bo_);
+ gbm_bo_unmap(tex_bo_);
+#else
+ if (dma_buf_id_) {
+ munmap(mapped_addr_, stride_);
+ mapped_addr_ = NULL;
+ }
+#endif
+ mapped_ = false;
+}
+
+gfx::GpuMemoryBufferHandle GpuMemoryBufferImplDMABuf::GetHandle() const {
+ gfx::GpuMemoryBufferHandle handle;
+ handle.type = gfx::DMA_BUFFER;
+ handle.handle.fd = dma_buf_id_;
+ handle.stride = stride_;
+ return handle;
+}
+
+uint32 GpuMemoryBufferImplDMABuf::GetStride() const {
+ return stride_;
+}
+
+} // namespace content
« 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