blob: dc17bb69a0d004aa8c245da07944d491a887e74c [file] [log] [blame]
Brian Salomondbf70722019-02-07 11:31:24 -05001/*
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 Kleinc0bd9f92019-04-23 12:05:21 -05008#include "src/gpu/GrCaps.h"
9#include "src/gpu/GrGpu.h"
10#include "src/gpu/GrGpuBuffer.h"
Brian Salomondbf70722019-02-07 11:31:24 -050011
12GrGpuBuffer::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 Salomon68aeec02019-04-16 11:01:13 -040020void* GrGpuBuffer::map() {
21 if (this->wasDestroyed()) {
22 return nullptr;
23 }
Greg Danielc1ea3d92021-01-27 10:21:21 -050024 SkASSERT(!fHasWrittenToBuffer || fAccessPattern == kDynamic_GrAccessPattern);
Brian Salomon68aeec02019-04-16 11:01:13 -040025 if (!fMapPtr) {
26 this->onMap();
27 }
28 return fMapPtr;
29}
30
31void GrGpuBuffer::unmap() {
32 if (this->wasDestroyed()) {
33 return;
34 }
35 SkASSERT(fMapPtr);
36 this->onUnmap();
37 fMapPtr = nullptr;
Greg Danielc1ea3d92021-01-27 10:21:21 -050038#ifdef SK_DEBUG
39 fHasWrittenToBuffer = true;
40#endif
Brian Salomon68aeec02019-04-16 11:01:13 -040041}
42
43bool GrGpuBuffer::isMapped() const { return SkToBool(fMapPtr); }
44
45bool GrGpuBuffer::updateData(const void* src, size_t srcSizeInBytes) {
Greg Danielc1ea3d92021-01-27 10:21:21 -050046 SkASSERT(!fHasWrittenToBuffer || fAccessPattern == kDynamic_GrAccessPattern);
Brian Salomon68aeec02019-04-16 11:01:13 -040047 SkASSERT(!this->isMapped());
48 SkASSERT(srcSizeInBytes <= fSizeInBytes);
49 if (this->intendedType() == GrGpuBufferType::kXferGpuToCpu) {
50 return false;
51 }
Greg Danielc1ea3d92021-01-27 10:21:21 -050052 bool result = this->onUpdateData(src, srcSizeInBytes);
53#ifdef SK_DEBUG
54 if (result) {
55 fHasWrittenToBuffer = true;
56 }
57#endif
58 return result;
Brian Salomon68aeec02019-04-16 11:01:13 -040059}
60
Greg Daniel8eb119a2021-02-04 09:41:19 -050061void GrGpuBuffer::ComputeScratchKeyForDynamicBuffer(size_t size, GrGpuBufferType intendedType,
Brian Salomondbf70722019-02-07 11:31:24 -050062 GrScratchKey* key) {
63 static const GrScratchKey::ResourceType kType = GrScratchKey::GenerateResourceType();
64 GrScratchKey::Builder builder(key, kType, 1 + (sizeof(size_t) + 3) / 4);
Brian Salomondbf70722019-02-07 11:31:24 -050065 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
72void GrGpuBuffer::computeScratchKey(GrScratchKey* key) const {
73 if (SkIsPow2(fSizeInBytes) && kDynamic_GrAccessPattern == fAccessPattern) {
Greg Daniel8eb119a2021-02-04 09:41:19 -050074 ComputeScratchKeyForDynamicBuffer(fSizeInBytes, fIntendedType, key);
Brian Salomondbf70722019-02-07 11:31:24 -050075 }
76}