blob: 7e3561eb2687c87696341a308f79f28abd4788c0 [file] [log] [blame]
Brian Salomondbf70722019-02-07 11:31:24 -05001/*
2 * Copyright 2019 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 GrGpuBuffer_DEFINED
9#define GrGpuBuffer_DEFINED
10
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "include/gpu/GrGpuResource.h"
12#include "src/gpu/GrBuffer.h"
Brian Salomondbf70722019-02-07 11:31:24 -050013
14class GrGpu;
15
16class GrGpuBuffer : public GrGpuResource, public GrBuffer {
17public:
18 /**
19 * Computes a scratch key for a GPU-side buffer with a "dynamic" access pattern. (Buffers with
20 * "static" and "stream" patterns are disqualified by nature from being cached and reused.)
21 */
22 static void ComputeScratchKeyForDynamicVBO(size_t size, GrGpuBufferType, GrScratchKey*);
23
24 GrAccessPattern accessPattern() const { return fAccessPattern; }
25
26 size_t size() const final { return fSizeInBytes; }
27
28 void ref() const final { GrGpuResource::ref(); }
29
30 void unref() const final { GrGpuResource::unref(); }
31
32 /**
Brian Salomon68aeec02019-04-16 11:01:13 -040033 * Maps the buffer to be read or written by the CPU.
Brian Salomondbf70722019-02-07 11:31:24 -050034 *
Brian Salomon68aeec02019-04-16 11:01:13 -040035 * It is an error to draw from the buffer while it is mapped or transfer to/from the buffer. It
36 * may fail if the backend doesn't support mapping the buffer. Once a buffer is mapped,
37 * subsequent calls to map() trivially succeed. No matter how many times map() is called,
38 * umap() will unmap the buffer on the first call if it is mapped.
Brian Salomondbf70722019-02-07 11:31:24 -050039 *
Brian Salomon68aeec02019-04-16 11:01:13 -040040 * If the buffer is of type GrGpuBufferType::kXferGpuToCpu then it is mapped for reading only.
41 * Otherwise it is mapped writing only. Writing to a buffer that is mapped for reading or vice
42 * versa produces undefined results. If the buffer is mapped for writing then then the buffer's
43 * previous contents are invalidated.
Brian Salomondbf70722019-02-07 11:31:24 -050044 *
45 * @return a pointer to the data or nullptr if the map fails.
46 */
Brian Salomon68aeec02019-04-16 11:01:13 -040047 void* map();
Brian Salomondbf70722019-02-07 11:31:24 -050048
49 /**
Brian Salomon68aeec02019-04-16 11:01:13 -040050 * Unmaps the buffer if it is mapped.
Brian Salomondbf70722019-02-07 11:31:24 -050051 *
52 * The pointer returned by the previous map call will no longer be valid.
53 */
Brian Salomon68aeec02019-04-16 11:01:13 -040054 void unmap();
Brian Salomondbf70722019-02-07 11:31:24 -050055
56 /**
Brian Salomon68aeec02019-04-16 11:01:13 -040057 * Queries whether the buffer has been mapped.
58 *
59 * @return true if the buffer is mapped, false otherwise.
Brian Salomondbf70722019-02-07 11:31:24 -050060 */
Brian Salomon68aeec02019-04-16 11:01:13 -040061 bool isMapped() const;
Brian Salomondbf70722019-02-07 11:31:24 -050062
63 bool isCpuBuffer() const final { return false; }
64
65 /**
66 * Updates the buffer data.
67 *
68 * The size of the buffer will be preserved. The src data will be
69 * placed at the beginning of the buffer and any remaining contents will
70 * be undefined. srcSizeInBytes must be <= to the buffer size.
71 *
72 * The buffer must not be mapped.
73 *
Brian Salomon68aeec02019-04-16 11:01:13 -040074 * Fails for GrGpuBufferType::kXferGpuToCpu.
75 *
Brian Salomondbf70722019-02-07 11:31:24 -050076 * Note that buffer updates do not go through GrContext and therefore are
77 * not serialized with other operations.
78 *
79 * @return returns true if the update succeeds, false otherwise.
80 */
Brian Salomon68aeec02019-04-16 11:01:13 -040081 bool updateData(const void* src, size_t srcSizeInBytes);
Brian Salomondbf70722019-02-07 11:31:24 -050082
83protected:
84 GrGpuBuffer(GrGpu*, size_t sizeInBytes, GrGpuBufferType, GrAccessPattern);
85 GrGpuBufferType intendedType() const { return fIntendedType; }
86
87 void* fMapPtr;
88
89private:
90 virtual void onMap() = 0;
91 virtual void onUnmap() = 0;
92 virtual bool onUpdateData(const void* src, size_t srcSizeInBytes) = 0;
93
94 size_t onGpuMemorySize() const override { return fSizeInBytes; }
95 const char* getResourceType() const override { return "Buffer Object"; }
96 void computeScratchKey(GrScratchKey* key) const override;
97
98 size_t fSizeInBytes;
99 GrAccessPattern fAccessPattern;
100 GrGpuBufferType fIntendedType;
101};
102
103#endif