GrGpuBuffer.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /*
  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. #ifndef GrGpuBuffer_DEFINED
  8. #define GrGpuBuffer_DEFINED
  9. #include "include/gpu/GrGpuResource.h"
  10. #include "src/gpu/GrBuffer.h"
  11. class GrGpu;
  12. class GrGpuBuffer : public GrGpuResource, public GrBuffer {
  13. public:
  14. /**
  15. * Computes a scratch key for a GPU-side buffer with a "dynamic" access pattern. (Buffers with
  16. * "static" and "stream" patterns are disqualified by nature from being cached and reused.)
  17. */
  18. static void ComputeScratchKeyForDynamicVBO(size_t size, GrGpuBufferType, GrScratchKey*);
  19. GrAccessPattern accessPattern() const { return fAccessPattern; }
  20. size_t size() const final { return fSizeInBytes; }
  21. void ref() const final { GrGpuResource::ref(); }
  22. void unref() const final { GrGpuResource::unref(); }
  23. /**
  24. * Maps the buffer to be read or written by the CPU.
  25. *
  26. * It is an error to draw from the buffer while it is mapped or transfer to/from the buffer. It
  27. * may fail if the backend doesn't support mapping the buffer. Once a buffer is mapped,
  28. * subsequent calls to map() trivially succeed. No matter how many times map() is called,
  29. * umap() will unmap the buffer on the first call if it is mapped.
  30. *
  31. * If the buffer is of type GrGpuBufferType::kXferGpuToCpu then it is mapped for reading only.
  32. * Otherwise it is mapped writing only. Writing to a buffer that is mapped for reading or vice
  33. * versa produces undefined results. If the buffer is mapped for writing then then the buffer's
  34. * previous contents are invalidated.
  35. *
  36. * @return a pointer to the data or nullptr if the map fails.
  37. */
  38. void* map();
  39. /**
  40. * Unmaps the buffer if it is mapped.
  41. *
  42. * The pointer returned by the previous map call will no longer be valid.
  43. */
  44. void unmap();
  45. /**
  46. * Queries whether the buffer has been mapped.
  47. *
  48. * @return true if the buffer is mapped, false otherwise.
  49. */
  50. bool isMapped() const;
  51. bool isCpuBuffer() const final { return false; }
  52. /**
  53. * Updates the buffer data.
  54. *
  55. * The size of the buffer will be preserved. The src data will be
  56. * placed at the beginning of the buffer and any remaining contents will
  57. * be undefined. srcSizeInBytes must be <= to the buffer size.
  58. *
  59. * The buffer must not be mapped.
  60. *
  61. * Fails for GrGpuBufferType::kXferGpuToCpu.
  62. *
  63. * Note that buffer updates do not go through GrContext and therefore are
  64. * not serialized with other operations.
  65. *
  66. * @return returns true if the update succeeds, false otherwise.
  67. */
  68. bool updateData(const void* src, size_t srcSizeInBytes);
  69. protected:
  70. GrGpuBuffer(GrGpu*, size_t sizeInBytes, GrGpuBufferType, GrAccessPattern);
  71. GrGpuBufferType intendedType() const { return fIntendedType; }
  72. void* fMapPtr;
  73. private:
  74. virtual void onMap() = 0;
  75. virtual void onUnmap() = 0;
  76. virtual bool onUpdateData(const void* src, size_t srcSizeInBytes) = 0;
  77. size_t onGpuMemorySize() const override { return fSizeInBytes; }
  78. const char* getResourceType() const override { return "Buffer Object"; }
  79. void computeScratchKey(GrScratchKey* key) const override;
  80. size_t fSizeInBytes;
  81. GrAccessPattern fAccessPattern;
  82. GrGpuBufferType fIntendedType;
  83. };
  84. #endif