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 | #ifndef GrCpuBuffer_DEFINED |
9 | #define GrCpuBuffer_DEFINED |
10 | |
11 | #include "src/core/SkSafeMath.h" |
12 | #include "src/gpu/GrBuffer.h" |
13 | #include "src/gpu/GrNonAtomicRef.h" |
14 | |
15 | class GrCpuBuffer final : public GrNonAtomicRef<GrCpuBuffer>, public GrBuffer { |
16 | public: |
17 | static sk_sp<GrCpuBuffer> Make(size_t size) { |
18 | SkASSERT(size > 0); |
19 | SkSafeMath sm; |
20 | size_t combinedSize = sm.add(sizeof(GrCpuBuffer), size); |
21 | if (!sm.ok()) { |
22 | SK_ABORT("Buffer size is too big."); |
23 | } |
24 | auto mem = ::operator new(combinedSize); |
25 | return sk_sp<GrCpuBuffer>(new (mem) GrCpuBuffer((char*)mem + sizeof(GrCpuBuffer), size)); |
26 | } |
27 | |
28 | // TODO(b/30449950): use sized delete once P0722R3 is available |
29 | static void operator delete(void* p) { ::operator delete(p); } |
30 | |
31 | void ref() const override { GrNonAtomicRef<GrCpuBuffer>::ref(); } |
32 | void unref() const override { GrNonAtomicRef<GrCpuBuffer>::unref(); } |
33 | size_t size() const override { return fSize; } |
34 | bool isCpuBuffer() const override { return true; } |
35 | |
36 | char* data() { return reinterpret_cast<char*>(fData); } |
37 | const char* data() const { return reinterpret_cast<const char*>(fData); } |
38 | |
39 | private: |
40 | GrCpuBuffer(void* data, size_t size) : fData(data), fSize(size) {} |
41 | void* fData; |
42 | size_t fSize; |
43 | }; |
44 | |
45 | #endif |
46 |