blob: 56a6cae3fb08f32f8e3e7fcbbb276ba81620b858 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
bsalomon@google.com27847de2011-02-22 20:59:41 +00002/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00003 * Copyright 2011 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.
bsalomon@google.com27847de2011-02-22 20:59:41 +00007 */
8
epoger@google.comec3ed6a2011-07-28 14:26:00 +00009
bsalomon@google.com27847de2011-02-22 20:59:41 +000010#ifndef GrGeometryBuffer_DEFINED
11#define GrGeometryBuffer_DEFINED
12
bsalomon6d3fe022014-07-25 08:35:45 -070013#include "GrGpuResource.h"
bsalomon@google.com8fe72472011-03-30 21:26:44 +000014
15class GrGpu;
bsalomon@google.com27847de2011-02-22 20:59:41 +000016
17/**
18 * Parent class for vertex and index buffers
19 */
bsalomon6d3fe022014-07-25 08:35:45 -070020class GrGeometryBuffer : public GrGpuResource {
bsalomon@google.com27847de2011-02-22 20:59:41 +000021public:
mtklein2766c002015-06-26 11:45:03 -070022
bsalomon@google.com8fe72472011-03-30 21:26:44 +000023
bsalomon@google.com27847de2011-02-22 20:59:41 +000024 /**
25 *Retrieves whether the buffer was created with the dynamic flag
26 *
27 * @return true if the buffer was created with the dynamic flag
28 */
29 bool dynamic() const { return fDynamic; }
bsalomon@google.com8fe72472011-03-30 21:26:44 +000030
bsalomon@google.com27847de2011-02-22 20:59:41 +000031 /**
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +000032 * Returns true if the buffer is a wrapper around a CPU array. If true it
commit-bot@chromium.org8341eb72014-05-07 20:51:05 +000033 * indicates that map will always succeed and will be free.
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +000034 */
35 bool isCPUBacked() const { return fCPUBacked; }
36
37 /**
commit-bot@chromium.org8341eb72014-05-07 20:51:05 +000038 * Maps the buffer to be written by the CPU.
bsalomon@google.com8fe72472011-03-30 21:26:44 +000039 *
bsalomon@google.com27847de2011-02-22 20:59:41 +000040 * The previous content of the buffer is invalidated. It is an error
commit-bot@chromium.org8341eb72014-05-07 20:51:05 +000041 * to draw from the buffer while it is mapped. It is an error to call map
42 * on an already mapped buffer. It may fail if the backend doesn't support
43 * mapping the buffer. If the buffer is CPU backed then it will always
44 * succeed and is a free operation. Must be matched by an unmap() call.
45 * Currently only one map at a time is supported (no nesting of
46 * map/unmap).
bsalomon@google.com8fe72472011-03-30 21:26:44 +000047 *
commit-bot@chromium.orge529a612014-05-08 18:13:23 +000048 * Note that buffer mapping does not go through GrContext and therefore is
49 * not serialized with other operations.
50 *
halcanary96fcdcc2015-08-27 07:41:13 -070051 * @return a pointer to the data or nullptr if the map fails.
bsalomon@google.com27847de2011-02-22 20:59:41 +000052 */
commit-bot@chromium.orge529a612014-05-08 18:13:23 +000053 void* map() { return (fMapPtr = this->onMap()); }
bsalomon@google.com8fe72472011-03-30 21:26:44 +000054
55 /**
commit-bot@chromium.org8341eb72014-05-07 20:51:05 +000056 * Unmaps the buffer.
bsalomon@google.com8fe72472011-03-30 21:26:44 +000057 *
commit-bot@chromium.org8341eb72014-05-07 20:51:05 +000058 * The pointer returned by the previous map call will no longer be valid.
bsalomon@google.com27847de2011-02-22 20:59:41 +000059 */
commit-bot@chromium.orge529a612014-05-08 18:13:23 +000060 void unmap() {
bsalomon49f085d2014-09-05 13:34:00 -070061 SkASSERT(fMapPtr);
commit-bot@chromium.orge529a612014-05-08 18:13:23 +000062 this->onUnmap();
halcanary96fcdcc2015-08-27 07:41:13 -070063 fMapPtr = nullptr;
commit-bot@chromium.orge529a612014-05-08 18:13:23 +000064 }
65
66 /**
halcanary96fcdcc2015-08-27 07:41:13 -070067 * Returns the same ptr that map() returned at time of map or nullptr if the
commit-bot@chromium.orge529a612014-05-08 18:13:23 +000068 * is not mapped.
69 *
halcanary96fcdcc2015-08-27 07:41:13 -070070 * @return ptr to mapped buffer data or nullptr if buffer is not mapped.
commit-bot@chromium.orge529a612014-05-08 18:13:23 +000071 */
72 void* mapPtr() const { return fMapPtr; }
bsalomon@google.com8fe72472011-03-30 21:26:44 +000073
74 /**
commit-bot@chromium.org8341eb72014-05-07 20:51:05 +000075 Queries whether the buffer has been mapped.
bsalomon@google.com8fe72472011-03-30 21:26:44 +000076
commit-bot@chromium.org8341eb72014-05-07 20:51:05 +000077 @return true if the buffer is mapped, false otherwise.
bsalomon@google.com27847de2011-02-22 20:59:41 +000078 */
bsalomon49f085d2014-09-05 13:34:00 -070079 bool isMapped() const { return SkToBool(fMapPtr); }
bsalomon@google.com8fe72472011-03-30 21:26:44 +000080
bsalomon@google.com27847de2011-02-22 20:59:41 +000081 /**
bsalomon@google.com8fe72472011-03-30 21:26:44 +000082 * Updates the buffer data.
83 *
84 * The size of the buffer will be preserved. The src data will be
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +000085 * placed at the beginning of the buffer and any remaining contents will
commit-bot@chromium.orge529a612014-05-08 18:13:23 +000086 * be undefined. srcSizeInBytes must be <= to the buffer size.
87 *
88 * The buffer must not be mapped.
89 *
90 * Note that buffer updates do not go through GrContext and therefore are
91 * not serialized with other operations.
bsalomon@google.com8fe72472011-03-30 21:26:44 +000092 *
bsalomon@google.com27847de2011-02-22 20:59:41 +000093 * @return returns true if the update succeeds, false otherwise.
94 */
commit-bot@chromium.orge529a612014-05-08 18:13:23 +000095 bool updateData(const void* src, size_t srcSizeInBytes) {
96 SkASSERT(!this->isMapped());
97 SkASSERT(srcSizeInBytes <= fGpuMemorySize);
98 return this->onUpdateData(src, srcSizeInBytes);
99 }
bsalomon@google.com8fe72472011-03-30 21:26:44 +0000100
bsalomon@google.com27847de2011-02-22 20:59:41 +0000101protected:
bsalomon5236cf42015-01-14 10:42:08 -0800102 GrGeometryBuffer(GrGpu* gpu, size_t gpuMemorySize, bool dynamic, bool cpuBacked)
103 : INHERITED(gpu, kCached_LifeCycle)
halcanary96fcdcc2015-08-27 07:41:13 -0700104 , fMapPtr(nullptr)
commit-bot@chromium.org089a7802014-05-02 21:38:22 +0000105 , fGpuMemorySize(gpuMemorySize)
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +0000106 , fDynamic(dynamic)
107 , fCPUBacked(cpuBacked) {}
bsalomon@google.com27847de2011-02-22 20:59:41 +0000108
109private:
bsalomon69ed47f2014-11-12 11:13:39 -0800110 virtual size_t onGpuMemorySize() const { return fGpuMemorySize; }
111
commit-bot@chromium.orge529a612014-05-08 18:13:23 +0000112 virtual void* onMap() = 0;
113 virtual void onUnmap() = 0;
114 virtual bool onUpdateData(const void* src, size_t srcSizeInBytes) = 0;
115
116 void* fMapPtr;
commit-bot@chromium.org089a7802014-05-02 21:38:22 +0000117 size_t fGpuMemorySize;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000118 bool fDynamic;
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +0000119 bool fCPUBacked;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000120
bsalomon6d3fe022014-07-25 08:35:45 -0700121 typedef GrGpuResource INHERITED;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000122};
123
124#endif