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 | #ifndef GrCpuBuffer_DEFINED |
| 9 | #define GrCpuBuffer_DEFINED |
| 10 | |
Brian Salomon | 9136a04 | 2019-08-14 11:54:55 -0400 | [diff] [blame] | 11 | #include "src/core/SkSafeMath.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 12 | #include "src/gpu/GrBuffer.h" |
| 13 | #include "src/gpu/GrNonAtomicRef.h" |
Brian Salomon | dbf7072 | 2019-02-07 11:31:24 -0500 | [diff] [blame] | 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); |
Brian Salomon | 9136a04 | 2019-08-14 11:54:55 -0400 | [diff] [blame] | 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); |
Brian Salomon | dbf7072 | 2019-02-07 11:31:24 -0500 | [diff] [blame] | 25 | return sk_sp<GrCpuBuffer>(new (mem) GrCpuBuffer((char*)mem + sizeof(GrCpuBuffer), size)); |
| 26 | } |
| 27 | |
Andy Weiss | ecd03db | 2019-08-28 08:46:43 -0700 | [diff] [blame] | 28 | // TODO(b/30449950): use sized delete once P0722R3 is available |
| 29 | static void operator delete(void* p) { ::operator delete(p); } |
| 30 | |
Brian Salomon | dbf7072 | 2019-02-07 11:31:24 -0500 | [diff] [blame] | 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 |