Brian Salomon | dbf7072 | 2019-02-07 11:31:24 -0500 | [diff] [blame] | 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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "src/gpu/GrCaps.h" |
| 9 | #include "src/gpu/GrGpu.h" |
| 10 | #include "src/gpu/GrGpuBuffer.h" |
Brian Salomon | dbf7072 | 2019-02-07 11:31:24 -0500 | [diff] [blame] | 11 | |
| 12 | GrGpuBuffer::GrGpuBuffer(GrGpu* gpu, size_t sizeInBytes, GrGpuBufferType type, |
| 13 | GrAccessPattern pattern) |
| 14 | : GrGpuResource(gpu) |
| 15 | , fMapPtr(nullptr) |
| 16 | , fSizeInBytes(sizeInBytes) |
| 17 | , fAccessPattern(pattern) |
| 18 | , fIntendedType(type) {} |
| 19 | |
Brian Salomon | 68aeec0 | 2019-04-16 11:01:13 -0400 | [diff] [blame] | 20 | void* GrGpuBuffer::map() { |
| 21 | if (this->wasDestroyed()) { |
| 22 | return nullptr; |
| 23 | } |
Greg Daniel | c1ea3d9 | 2021-01-27 10:21:21 -0500 | [diff] [blame] | 24 | SkASSERT(!fHasWrittenToBuffer || fAccessPattern == kDynamic_GrAccessPattern); |
Brian Salomon | 68aeec0 | 2019-04-16 11:01:13 -0400 | [diff] [blame] | 25 | if (!fMapPtr) { |
| 26 | this->onMap(); |
| 27 | } |
| 28 | return fMapPtr; |
| 29 | } |
| 30 | |
| 31 | void GrGpuBuffer::unmap() { |
| 32 | if (this->wasDestroyed()) { |
| 33 | return; |
| 34 | } |
| 35 | SkASSERT(fMapPtr); |
| 36 | this->onUnmap(); |
| 37 | fMapPtr = nullptr; |
Greg Daniel | c1ea3d9 | 2021-01-27 10:21:21 -0500 | [diff] [blame] | 38 | #ifdef SK_DEBUG |
| 39 | fHasWrittenToBuffer = true; |
| 40 | #endif |
Brian Salomon | 68aeec0 | 2019-04-16 11:01:13 -0400 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | bool GrGpuBuffer::isMapped() const { return SkToBool(fMapPtr); } |
| 44 | |
| 45 | bool GrGpuBuffer::updateData(const void* src, size_t srcSizeInBytes) { |
Greg Daniel | c1ea3d9 | 2021-01-27 10:21:21 -0500 | [diff] [blame] | 46 | SkASSERT(!fHasWrittenToBuffer || fAccessPattern == kDynamic_GrAccessPattern); |
Brian Salomon | 68aeec0 | 2019-04-16 11:01:13 -0400 | [diff] [blame] | 47 | SkASSERT(!this->isMapped()); |
| 48 | SkASSERT(srcSizeInBytes <= fSizeInBytes); |
| 49 | if (this->intendedType() == GrGpuBufferType::kXferGpuToCpu) { |
| 50 | return false; |
| 51 | } |
Greg Daniel | c1ea3d9 | 2021-01-27 10:21:21 -0500 | [diff] [blame] | 52 | bool result = this->onUpdateData(src, srcSizeInBytes); |
| 53 | #ifdef SK_DEBUG |
| 54 | if (result) { |
| 55 | fHasWrittenToBuffer = true; |
| 56 | } |
| 57 | #endif |
| 58 | return result; |
Brian Salomon | 68aeec0 | 2019-04-16 11:01:13 -0400 | [diff] [blame] | 59 | } |
| 60 | |
Greg Daniel | 8eb119a | 2021-02-04 09:41:19 -0500 | [diff] [blame] | 61 | void GrGpuBuffer::ComputeScratchKeyForDynamicBuffer(size_t size, GrGpuBufferType intendedType, |
Brian Salomon | dbf7072 | 2019-02-07 11:31:24 -0500 | [diff] [blame] | 62 | GrScratchKey* key) { |
| 63 | static const GrScratchKey::ResourceType kType = GrScratchKey::GenerateResourceType(); |
| 64 | GrScratchKey::Builder builder(key, kType, 1 + (sizeof(size_t) + 3) / 4); |
Brian Salomon | dbf7072 | 2019-02-07 11:31:24 -0500 | [diff] [blame] | 65 | builder[0] = SkToU32(intendedType); |
| 66 | builder[1] = (uint32_t)size; |
| 67 | if (sizeof(size_t) > 4) { |
| 68 | builder[2] = (uint32_t)((uint64_t)size >> 32); |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | void GrGpuBuffer::computeScratchKey(GrScratchKey* key) const { |
| 73 | if (SkIsPow2(fSizeInBytes) && kDynamic_GrAccessPattern == fAccessPattern) { |
Greg Daniel | 8eb119a | 2021-02-04 09:41:19 -0500 | [diff] [blame] | 74 | ComputeScratchKeyForDynamicBuffer(fSizeInBytes, fIntendedType, key); |
Brian Salomon | dbf7072 | 2019-02-07 11:31:24 -0500 | [diff] [blame] | 75 | } |
| 76 | } |