1 // Copyright 2017 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 GPU_IPC_COMMON_GPU_MEMORY_BUFFER_IMPL_ANDROID_HARDWARE_BUFFER_H_
6 #define GPU_IPC_COMMON_GPU_MEMORY_BUFFER_IMPL_ANDROID_HARDWARE_BUFFER_H_
7 
8 #include "base/android/scoped_hardware_buffer_handle.h"
9 #include "gpu/gpu_export.h"
10 #include "gpu/ipc/common/gpu_memory_buffer_impl.h"
11 
12 namespace gpu {
13 
14 // Implementation of GPU memory buffer based on AHardwareBuffer.
15 class GPU_EXPORT GpuMemoryBufferImplAndroidHardwareBuffer
16     : public GpuMemoryBufferImpl {
17  public:
18   ~GpuMemoryBufferImplAndroidHardwareBuffer() override;
19 
20   static constexpr gfx::GpuMemoryBufferType kBufferType =
21       gfx::ANDROID_HARDWARE_BUFFER;
22 
23   static std::unique_ptr<GpuMemoryBufferImplAndroidHardwareBuffer> Create(
24       gfx::GpuMemoryBufferId id,
25       const gfx::Size& size,
26       gfx::BufferFormat format,
27       gfx::BufferUsage usage,
28       DestructionCallback callback);
29 
30   static std::unique_ptr<GpuMemoryBufferImplAndroidHardwareBuffer>
31   CreateFromHandle(gfx::GpuMemoryBufferHandle handle,
32                    const gfx::Size& size,
33                    gfx::BufferFormat format,
34                    gfx::BufferUsage usage,
35                    DestructionCallback callback);
36 
37   static base::OnceClosure AllocateForTesting(
38       const gfx::Size& size,
39       gfx::BufferFormat format,
40       gfx::BufferUsage usage,
41       gfx::GpuMemoryBufferHandle* handle);
42 
43   // Overridden from gfx::GpuMemoryBuffer:
44   bool Map() override;
45   void* memory(size_t plane) override;
46   void Unmap() override;
47   int stride(size_t plane) const override;
48   gfx::GpuMemoryBufferType GetType() const override;
49   gfx::GpuMemoryBufferHandle CloneHandle() const override;
50 
51  private:
52   GpuMemoryBufferImplAndroidHardwareBuffer(
53       gfx::GpuMemoryBufferId id,
54       const gfx::Size& size,
55       gfx::BufferFormat format,
56       DestructionCallback callback,
57       base::android::ScopedHardwareBufferHandle handle);
58 
59   base::android::ScopedHardwareBufferHandle hardware_buffer_handle_;
60 
61   DISALLOW_COPY_AND_ASSIGN(GpuMemoryBufferImplAndroidHardwareBuffer);
62 };
63 
64 }  // namespace gpu
65 
66 #endif  // GPU_IPC_COMMON_GPU_MEMORY_BUFFER_IMPL_ANDROID_HARDWARE_BUFFER_H_
67