GrGpuBuffer.cpp 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. #include "src/gpu/GrCaps.h"
  8. #include "src/gpu/GrGpu.h"
  9. #include "src/gpu/GrGpuBuffer.h"
  10. GrGpuBuffer::GrGpuBuffer(GrGpu* gpu, size_t sizeInBytes, GrGpuBufferType type,
  11. GrAccessPattern pattern)
  12. : GrGpuResource(gpu)
  13. , fMapPtr(nullptr)
  14. , fSizeInBytes(sizeInBytes)
  15. , fAccessPattern(pattern)
  16. , fIntendedType(type) {}
  17. void* GrGpuBuffer::map() {
  18. if (this->wasDestroyed()) {
  19. return nullptr;
  20. }
  21. if (!fMapPtr) {
  22. this->onMap();
  23. }
  24. return fMapPtr;
  25. }
  26. void GrGpuBuffer::unmap() {
  27. if (this->wasDestroyed()) {
  28. return;
  29. }
  30. SkASSERT(fMapPtr);
  31. this->onUnmap();
  32. fMapPtr = nullptr;
  33. }
  34. bool GrGpuBuffer::isMapped() const { return SkToBool(fMapPtr); }
  35. bool GrGpuBuffer::updateData(const void* src, size_t srcSizeInBytes) {
  36. SkASSERT(!this->isMapped());
  37. SkASSERT(srcSizeInBytes <= fSizeInBytes);
  38. if (this->intendedType() == GrGpuBufferType::kXferGpuToCpu) {
  39. return false;
  40. }
  41. return this->onUpdateData(src, srcSizeInBytes);
  42. }
  43. void GrGpuBuffer::ComputeScratchKeyForDynamicVBO(size_t size, GrGpuBufferType intendedType,
  44. GrScratchKey* key) {
  45. static const GrScratchKey::ResourceType kType = GrScratchKey::GenerateResourceType();
  46. GrScratchKey::Builder builder(key, kType, 1 + (sizeof(size_t) + 3) / 4);
  47. // TODO: There's not always reason to cache a buffer by type. In some (all?) APIs it's just
  48. // a chunk of memory we can use/reuse for any type of data. We really only need to
  49. // differentiate between the "read" types (e.g. kGpuToCpu_BufferType) and "draw" types.
  50. builder[0] = SkToU32(intendedType);
  51. builder[1] = (uint32_t)size;
  52. if (sizeof(size_t) > 4) {
  53. builder[2] = (uint32_t)((uint64_t)size >> 32);
  54. }
  55. }
  56. void GrGpuBuffer::computeScratchKey(GrScratchKey* key) const {
  57. if (SkIsPow2(fSizeInBytes) && kDynamic_GrAccessPattern == fAccessPattern) {
  58. ComputeScratchKeyForDynamicVBO(fSizeInBytes, fIntendedType, key);
  59. }
  60. }