Flutter Engine
The Flutter Engine
GrCpuBuffer.h
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
8#ifndef GrCpuBuffer_DEFINED
9#define GrCpuBuffer_DEFINED
10
11#include "src/base/SkSafeMath.h"
14
15class GrCpuBuffer final : public GrNonAtomicRef<GrCpuBuffer>, public GrBuffer {
16public:
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
39private:
40 GrCpuBuffer(void* data, size_t size) : fData(data), fSize(size) {}
41 void* fData;
42 size_t fSize;
43};
44
45#endif
#define SK_ABORT(message,...)
Definition: SkAssert.h:70
#define SkASSERT(cond)
Definition: SkAssert.h:116
void ref() const override
Definition: GrCpuBuffer.h:31
const char * data() const
Definition: GrCpuBuffer.h:37
void unref() const override
Definition: GrCpuBuffer.h:32
bool isCpuBuffer() const override
Definition: GrCpuBuffer.h:34
char * data()
Definition: GrCpuBuffer.h:36
static sk_sp< GrCpuBuffer > Make(size_t size)
Definition: GrCpuBuffer.h:17
size_t size() const override
Definition: GrCpuBuffer.h:33
void ref() const
void unref() const
size_t add(size_t x, size_t y)
Definition: SkSafeMath.h:33
bool ok() const
Definition: SkSafeMath.h:26