| 1 |
/* |
| 2 |
* Copyright 2021 Google Inc. |
| 3 |
* |
| 4 |
* Use of this source code is governed by a BSD-style license that can be |
| 5 |
* found in the LICENSE file. |
| 6 |
*/ |
| 7 |
|
| 8 |
#ifndef skgpu_graphite_Buffer_DEFINED |
| 9 |
#define skgpu_graphite_Buffer_DEFINED |
| 10 |
|
| 11 |
#include "include/gpu/graphite/GraphiteTypes.h" |
| 12 |
#include "src/gpu/graphite/Resource.h" |
| 13 |
#include "src/gpu/graphite/ResourceTypes.h" |
| 14 |
|
| 15 |
#include <cstddef> |
| 16 |
|
| 17 |
namespace skgpu { |
| 18 |
enum class Protected : bool; |
| 19 |
} |
| 20 |
|
| 21 |
namespace skgpu::graphite { |
| 22 |
class SharedContext; |
| 23 |
|
| 24 |
class Buffer : public Resource { |
| 25 |
public: |
| 26 |
size_t size() const { return fSize; } |
| 27 |
Protected isProtected() const { return fIsProtected; } |
| 28 |
|
| 29 |
// TODO(b/262249983): Separate into mapRead(), mapWrite() methods. |
| 30 |
// If the buffer is already mapped then pointer is returned. If an asyncMap() was started then |
| 31 |
// it is waited on. Otherwise, a synchronous map is performed. |
| 32 |
void* map(); |
| 33 |
// Starts a new asynchronous map. |
| 34 |
void asyncMap(GpuFinishedProc = nullptr, GpuFinishedContext = nullptr); |
| 35 |
// If the buffer is mapped then unmaps. If an async map is pending then it is cancelled. |
| 36 |
void unmap(); |
| 37 |
|
| 38 |
bool isMapped() const { return fMapPtr; } |
| 39 |
|
| 40 |
// Returns true if mapped or an asyncMap was started and hasn't been completed or canceled. |
| 41 |
virtual bool isUnmappable() const; |
| 42 |
|
| 43 |
const char* getResourceType() const override { return "Buffer"; } |
| 44 |
|
| 45 |
protected: |
| 46 |
Buffer(const SharedContext* sharedContext, |
| 47 |
size_t size, |
| 48 |
Protected isProtected, |
| 49 |
bool reusableRequiresPurgeable = false, |
| 50 |
bool requiresPrepareForReturnToCache = false) |
| 51 |
: Resource(sharedContext, |
| 52 |
Ownership::kOwned, |
| 53 |
size, |
| 54 |
reusableRequiresPurgeable, |
| 55 |
requiresPrepareForReturnToCache) |
| 56 |
, fSize(size) |
| 57 |
, fIsProtected(isProtected) {} |
| 58 |
|
| 59 |
void* fMapPtr = nullptr; |
| 60 |
|
| 61 |
private: |
| 62 |
virtual void onMap() = 0; |
| 63 |
virtual void onAsyncMap(GpuFinishedProc, GpuFinishedContext); |
| 64 |
virtual void onUnmap() = 0; |
| 65 |
|
| 66 |
size_t fSize; |
| 67 |
Protected fIsProtected; |
| 68 |
}; |
| 69 |
|
| 70 |
} // namespace skgpu::graphite |
| 71 |
|
| 72 |
#endif // skgpu_graphite_Buffer_DEFINED |