blob: c3e1c65252c4f7376a7abd016704308173abdf38 [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:
robertphillips@google.com7fa18762012-09-11 13:02:31 +000022 SK_DECLARE_INST_COUNT(GrGeometryBuffer);
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 *
commit-bot@chromium.org8341eb72014-05-07 20:51:05 +000051 * @return a pointer to the data or NULL 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();
63 fMapPtr = NULL;
64 }
65
66 /**
67 * Returns the same ptr that map() returned at time of map or NULL if the
68 * is not mapped.
69 *
70 * @return ptr to mapped buffer data or NULL if buffer is not mapped.
71 */
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
bsalomon6d3fe022014-07-25 08:35:45 -0700101 // GrGpuResource overrides
commit-bot@chromium.org089a7802014-05-02 21:38:22 +0000102 virtual size_t gpuMemorySize() const { return fGpuMemorySize; }
bsalomon@google.comcee661a2011-07-26 12:32:36 +0000103
bsalomon@google.com27847de2011-02-22 20:59:41 +0000104protected:
commit-bot@chromium.org089a7802014-05-02 21:38:22 +0000105 GrGeometryBuffer(GrGpu* gpu, bool isWrapped, size_t gpuMemorySize, bool dynamic, bool cpuBacked)
bsalomon@google.com72830222013-01-23 20:25:22 +0000106 : INHERITED(gpu, isWrapped)
commit-bot@chromium.orge529a612014-05-08 18:13:23 +0000107 , fMapPtr(NULL)
commit-bot@chromium.org089a7802014-05-02 21:38:22 +0000108 , fGpuMemorySize(gpuMemorySize)
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +0000109 , fDynamic(dynamic)
110 , fCPUBacked(cpuBacked) {}
bsalomon@google.com27847de2011-02-22 20:59:41 +0000111
112private:
commit-bot@chromium.orge529a612014-05-08 18:13:23 +0000113 virtual void* onMap() = 0;
114 virtual void onUnmap() = 0;
115 virtual bool onUpdateData(const void* src, size_t srcSizeInBytes) = 0;
116
117 void* fMapPtr;
commit-bot@chromium.org089a7802014-05-02 21:38:22 +0000118 size_t fGpuMemorySize;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000119 bool fDynamic;
bsalomon@google.comee3bc3b2013-02-21 14:33:46 +0000120 bool fCPUBacked;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000121
bsalomon6d3fe022014-07-25 08:35:45 -0700122 typedef GrGpuResource INHERITED;
bsalomon@google.com27847de2011-02-22 20:59:41 +0000123};
124
125#endif