blob: 51497666763173382f9f030afe0e24a9f3fec37c [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
8#ifndef GrCpuBuffer_DEFINED
9#define GrCpuBuffer_DEFINED
10
Brian Salomon9136a042019-08-14 11:54:55 -040011#include "src/core/SkSafeMath.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050012#include "src/gpu/GrBuffer.h"
13#include "src/gpu/GrNonAtomicRef.h"
Brian Salomondbf70722019-02-07 11:31:24 -050014
15class GrCpuBuffer final : public GrNonAtomicRef<GrCpuBuffer>, public GrBuffer {
16public:
17 static sk_sp<GrCpuBuffer> Make(size_t size) {
18 SkASSERT(size > 0);
Brian Salomon9136a042019-08-14 11:54:55 -040019 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 Salomondbf70722019-02-07 11:31:24 -050025 return sk_sp<GrCpuBuffer>(new (mem) GrCpuBuffer((char*)mem + sizeof(GrCpuBuffer), size));
26 }
27
Andy Weissecd03db2019-08-28 08:46:43 -070028 // TODO(b/30449950): use sized delete once P0722R3 is available
29 static void operator delete(void* p) { ::operator delete(p); }
30
Brian Salomondbf70722019-02-07 11:31:24 -050031 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
39private:
40 GrCpuBuffer(void* data, size_t size) : fData(data), fSize(size) {}
41 void* fData;
42 size_t fSize;
43};
44
45#endif