1 
2 /*
3  * Copyright 2015 Google Inc.
4  *
5  * Use of this source code is governed by a BSD-style license that can be
6  * found in the LICENSE file.
7  */
8 
9 
10 #ifndef GrTransferBuffer_DEFINED
11 #define GrTransferBuffer_DEFINED
12 
13 #include "GrGpuResource.h"
14 
15 class GrTransferBuffer : public GrGpuResource {
16 public:
17     /**
18     * Maps the buffer to be written by the CPU.
19     *
20     * The previous content of the buffer is invalidated. It is an error
21     * to transfer to or from the buffer while it is mapped. It is an error to
22     * call map on an already mapped buffer. Must be matched by an unmap() call.
23     * Currently only one map at a time is supported (no nesting of map/unmap).
24     *
25     * Note that buffer mapping does not go through GrContext and therefore is
26     * not serialized with other operations.
27     *
28     * @return a pointer to the data or nullptr if the map fails.
29     */
map()30     void* map() { return (fMapPtr = this->onMap()); }
31 
32     /**
33     * Unmaps the buffer.
34     *
35     * The pointer returned by the previous map call will no longer be valid.
36     */
unmap()37     void unmap() {
38         SkASSERT(fMapPtr);
39         this->onUnmap();
40         fMapPtr = nullptr;
41     }
42 
43     /**
44     * Returns the same ptr that map() returned at time of map or nullptr if the
45     * is not mapped.
46     *
47     * @return ptr to mapped buffer data or nullptr if buffer is not mapped.
48     */
mapPtr()49     void* mapPtr() const { return fMapPtr; }
50 
51     /**
52     Queries whether the buffer has been mapped.
53 
54     @return true if the buffer is mapped, false otherwise.
55     */
isMapped()56     bool isMapped() const { return SkToBool(fMapPtr); }
57 
58 protected:
GrTransferBuffer(GrGpu * gpu,size_t gpuMemorySize)59     GrTransferBuffer(GrGpu* gpu, size_t gpuMemorySize)
60         : INHERITED(gpu, kUncached_LifeCycle)
61         , fGpuMemorySize(gpuMemorySize) {
62     }
63 
64 private:
onGpuMemorySize()65     virtual size_t onGpuMemorySize() const { return fGpuMemorySize; }
66 
67     virtual void* onMap() = 0;
68     virtual void  onUnmap() = 0;
69 
70     void*    fMapPtr;
71     size_t   fGpuMemorySize;
72 
73     typedef GrGpuResource INHERITED;
74 };
75 
76 #endif
77