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