Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
GrGpuBuffer.cpp
Go to the documentation of this file.
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
9
13#include "src/gpu/ResourceKey.h"
16
17#include <cstdint>
18
20 GrAccessPattern pattern,
21 std::string_view label)
22 : GrGpuResource(gpu, label)
23 , fMapPtr(nullptr)
24 , fSizeInBytes(sizeInBytes)
25 , fAccessPattern(pattern)
26 , fIntendedType(type) {}
27
29 if (this->wasDestroyed()) {
30 return nullptr;
31 }
32 if (!fMapPtr) {
33 this->onMap(this->mapType());
34 }
35 return fMapPtr;
36}
37
39 if (this->wasDestroyed()) {
40 return;
41 }
43 this->onUnmap(this->mapType());
44 fMapPtr = nullptr;
45}
46
47bool GrGpuBuffer::isMapped() const { return SkToBool(fMapPtr); }
48
50 SkASSERT(!this->isMapped());
51
52 if (this->wasDestroyed()) {
53 return false;
54 }
55
57 return false;
58 }
59
60 return this->onClearToZero();
61}
62
63bool GrGpuBuffer::updateData(const void* src, size_t offset, size_t size, bool preserve) {
64 SkASSERT(!this->isMapped());
65 SkASSERT(size > 0 && offset + size <= fSizeInBytes);
66 SkASSERT(src);
67
68 if (this->wasDestroyed()) {
69 return false;
70 }
71
72 if (preserve) {
73 size_t a = this->getGpu()->caps()->bufferUpdateDataPreserveAlignment();
74 if (SkAlignTo(offset, a) != offset || SkAlignTo(size, a) != size) {
75 return false;
76 }
77 }
78
80 return false;
81 }
82
83 return this->onUpdateData(src, offset, size, preserve);
84}
85
87 GrGpuBufferType intendedType,
90 skgpu::ScratchKey::Builder builder(key, kType, 1 + (sizeof(size_t) + 3) / 4);
91 builder[0] = SkToU32(intendedType);
92 builder[1] = (uint32_t)size;
93 if (sizeof(size_t) > 4) {
94 builder[2] = (uint32_t)((uint64_t)size >> 32);
95 }
96}
97
99 if (kDynamic_GrAccessPattern == fAccessPattern) {
100 ComputeScratchKeyForDynamicBuffer(fSizeInBytes, fIntendedType, key);
101 }
102}
GrGpuBufferType
GrAccessPattern
@ kDynamic_GrAccessPattern
static constexpr size_t SkAlignTo(size_t x, size_t alignment)
Definition SkAlign.h:33
#define SkASSERT(cond)
Definition SkAssert.h:116
static constexpr bool SkToBool(const T &x)
Definition SkTo.h:35
constexpr uint32_t SkToU32(S x)
Definition SkTo.h:26
size_t bufferUpdateDataPreserveAlignment() const
Definition GrCaps.h:250
static void ComputeScratchKeyForDynamicBuffer(size_t size, GrGpuBufferType, skgpu::ScratchKey *)
size_t size() const final
Definition GrGpuBuffer.h:34
bool clearToZero()
GrGpuBuffer(GrGpu *, size_t sizeInBytes, GrGpuBufferType, GrAccessPattern, std::string_view label)
void * map()
GrGpuBufferType intendedType() const
Definition GrGpuBuffer.h:99
virtual bool onUpdateData(const void *src, size_t offset, size_t size, bool preserve)=0
virtual void onUnmap(MapType)=0
virtual bool onClearToZero()=0
bool isMapped() const
bool updateData(const void *src, size_t offset, size_t size, bool preserve)
virtual void onMap(MapType)=0
void computeScratchKey(skgpu::ScratchKey *key) const override
void * fMapPtr
GrGpu * getGpu() const
bool wasDestroyed() const
Definition GrGpu.h:62
const GrCaps * caps() const
Definition GrGpu.h:73
static ResourceType GenerateResourceType()
uint32_t ResourceType
struct MyStruct a[10]
Point offset