gpu_memory_buffer.h revision 5d1f7b1de12d16ceb2c938c56701a3e8bfa558f7
1// Copyright 2013 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#ifndef UI_GFX_GPU_MEMORY_BUFFER_H_
6#define UI_GFX_GPU_MEMORY_BUFFER_H_
7
8#include "base/memory/shared_memory.h"
9#include "build/build_config.h"
10#include "ui/gfx/gfx_export.h"
11
12#if defined(OS_ANDROID)
13#include <third_party/khronos/EGL/egl.h>
14#endif
15
16namespace gfx {
17
18enum GpuMemoryBufferType {
19  EMPTY_BUFFER,
20  SHARED_MEMORY_BUFFER,
21  EGL_CLIENT_BUFFER,
22  IO_SURFACE_BUFFER,
23  GPU_MEMORY_BUFFER_TYPE_LAST = IO_SURFACE_BUFFER
24};
25
26struct GpuMemoryBufferHandle {
27  GpuMemoryBufferHandle()
28      : type(EMPTY_BUFFER),
29        handle(base::SharedMemory::NULLHandle())
30#if defined(OS_ANDROID)
31        , native_buffer(NULL)
32#endif
33#if defined(OS_MACOSX)
34        , io_surface_id(0)
35#endif
36  {
37  }
38  bool is_null() const { return type == EMPTY_BUFFER; }
39  GpuMemoryBufferType type;
40  base::SharedMemoryHandle handle;
41#if defined(OS_ANDROID)
42  EGLClientBuffer native_buffer;
43#endif
44#if defined(OS_MACOSX)
45  uint32 io_surface_id;
46#endif
47
48};
49
50// Interface for creating and accessing a zero-copy GPU memory buffer.
51// This design evolved from the generalization of GraphicBuffer API
52// of Android framework.
53//
54// THREADING CONSIDERATIONS:
55//
56// This interface is thread-safe. However, multiple threads mapping
57// a buffer for Write or ReadOrWrite simultaneously may result in undefined
58// behavior and is not allowed.
59class GFX_EXPORT GpuMemoryBuffer {
60 public:
61  enum AccessMode {
62    READ_ONLY,
63    WRITE_ONLY,
64    READ_WRITE,
65  };
66
67  GpuMemoryBuffer();
68  virtual ~GpuMemoryBuffer();
69
70  // Maps the buffer so the client can write the bitmap data in |*vaddr|
71  // subsequently. This call may block, for instance if the hardware needs
72  // to finish rendering or if CPU caches need to be synchronized.
73  virtual void Map(AccessMode mode, void** vaddr) = 0;
74
75  // Unmaps the buffer. Called after all changes to the buffer are
76  // completed.
77  virtual void Unmap() = 0;
78
79  // Returns true iff the buffer is mapped.
80  virtual bool IsMapped() const = 0;
81
82  // Returns the stride in bytes for the buffer.
83  virtual uint32 GetStride() const = 0;
84
85  // Returns a platform specific handle for this buffer.
86  virtual GpuMemoryBufferHandle GetHandle() const = 0;
87};
88
89}  // namespace gfx
90
91#endif  // UI_GFX_GPU_MEMORY_BUFFER_H_
92