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