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 | |
| 8 | #include "GrGpuBuffer.h" |
| 9 | #include "GrCaps.h" |
| 10 | #include "GrGpu.h" |
| 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 | |
| 20 | void GrGpuBuffer::ComputeScratchKeyForDynamicVBO(size_t size, GrGpuBufferType intendedType, |
| 21 | GrScratchKey* key) { |
| 22 | static const GrScratchKey::ResourceType kType = GrScratchKey::GenerateResourceType(); |
| 23 | GrScratchKey::Builder builder(key, kType, 1 + (sizeof(size_t) + 3) / 4); |
| 24 | // TODO: There's not always reason to cache a buffer by type. In some (all?) APIs it's just |
| 25 | // a chunk of memory we can use/reuse for any type of data. We really only need to |
| 26 | // differentiate between the "read" types (e.g. kGpuToCpu_BufferType) and "draw" types. |
| 27 | builder[0] = SkToU32(intendedType); |
| 28 | builder[1] = (uint32_t)size; |
| 29 | if (sizeof(size_t) > 4) { |
| 30 | builder[2] = (uint32_t)((uint64_t)size >> 32); |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | void GrGpuBuffer::computeScratchKey(GrScratchKey* key) const { |
| 35 | if (SkIsPow2(fSizeInBytes) && kDynamic_GrAccessPattern == fAccessPattern) { |
| 36 | ComputeScratchKeyForDynamicVBO(fSizeInBytes, fIntendedType, key); |
| 37 | } |
| 38 | } |