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.
5 #ifndef CONTENT_COMMON_GPU_CLIENT_GPU_MEMORY_BUFFER_IMPL_H_
6 #define CONTENT_COMMON_GPU_CLIENT_GPU_MEMORY_BUFFER_IMPL_H_
8 #include "base/callback.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "content/common/content_export.h"
11 #include "ui/gfx/geometry/size.h"
12 #include "ui/gfx/gpu_memory_buffer.h"
16 // Provides common implementation of a GPU memory buffer.
17 class CONTENT_EXPORT GpuMemoryBufferImpl
: public gfx::GpuMemoryBuffer
{
19 typedef base::Callback
<void(uint32 sync_point
)> DestructionCallback
;
21 ~GpuMemoryBufferImpl() override
;
23 // Creates an instance from the given |handle|. |size| and |internalformat|
24 // should match what was used to allocate the |handle|. |callback| is
25 // called when instance is deleted, which is not necessarily on the same
26 // thread as this function was called on and instance was created on.
27 static scoped_ptr
<GpuMemoryBufferImpl
> CreateFromHandle(
28 const gfx::GpuMemoryBufferHandle
& handle
,
29 const gfx::Size
& size
,
30 gfx::BufferFormat format
,
31 gfx::BufferUsage usage
,
32 const DestructionCallback
& callback
);
34 // Type-checking upcast routine. Returns an NULL on failure.
35 static GpuMemoryBufferImpl
* FromClientBuffer(ClientBuffer buffer
);
37 // Returns the number of planes based on the format of the buffer.
38 static size_t NumberOfPlanesForGpuMemoryBufferFormat(
39 gfx::BufferFormat format
);
41 // Returns the subsampling factor applied to the given zero-indexed |plane| of
42 // the |format| both horizontally and vertically.
43 static size_t SubsamplingFactor(gfx::BufferFormat format
, int plane
);
45 // Returns the number of bytes used to store a row of the given zero-indexed
46 // |plane| of |format|.
47 // Note: This is an approximation and the exact size used by an implementation
48 // might be different.
49 static bool RowSizeInBytes(size_t width
,
50 gfx::BufferFormat format
,
52 size_t* size_in_bytes
);
54 // Returns the number of bytes used to store all the planes of a given
56 // Note: This is an approximation and the exact size used by an implementation
57 // might be different.
58 static bool BufferSizeInBytes(const gfx::Size
& size
,
59 gfx::BufferFormat format
,
60 size_t* size_in_bytes
);
62 // Overridden from gfx::GpuMemoryBuffer:
63 bool IsMapped() const override
;
64 gfx::BufferFormat
GetFormat() const override
;
65 gfx::GpuMemoryBufferId
GetId() const override
;
66 ClientBuffer
AsClientBuffer() override
;
68 void set_destruction_sync_point(uint32 sync_point
) {
69 destruction_sync_point_
= sync_point
;
73 GpuMemoryBufferImpl(gfx::GpuMemoryBufferId id
,
74 const gfx::Size
& size
,
75 gfx::BufferFormat format
,
76 const DestructionCallback
& callback
);
78 const gfx::GpuMemoryBufferId id_
;
79 const gfx::Size size_
;
80 const gfx::BufferFormat format_
;
81 const DestructionCallback callback_
;
83 uint32 destruction_sync_point_
;
86 DISALLOW_COPY_AND_ASSIGN(GpuMemoryBufferImpl
);
89 } // namespace content
91 #endif // CONTENT_COMMON_GPU_CLIENT_GPU_MEMORY_BUFFER_IMPL_H_