Overte C++ Documentation
Sysmem.h
1 //
2 // Created by Sam Gateau on 10/8/2014.
3 // Split from Resource.h/Resource.cpp by Bradley Austin Davis on 2016/08/07
4 // Copyright 2014 High Fidelity, Inc.
5 //
6 // Distributed under the Apache License, Version 2.0.
7 // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
8 //
9 #ifndef hifi_gpu_Sysmem_h
10 #define hifi_gpu_Sysmem_h
11 
12 
13 #include "Forward.h"
14 
15 namespace gpu {
16 
17 // Sysmem is the underneath cache for the data in ram of a resource.
18 class Sysmem {
19 public:
20  static const Size NOT_ALLOCATED = INVALID_SIZE;
21 
22  Sysmem();
23  Sysmem(Size size, const Byte* bytes);
24  Sysmem(const Sysmem& sysmem); // deep copy of the sysmem buffer
25  Sysmem& operator=(const Sysmem& sysmem); // deep copy of the sysmem buffer
26  ~Sysmem();
27 
28  Size getSize() const { return _size; }
29 
30  // Allocate the byte array
31  // \param pSize The nb of bytes to allocate, if already exist, content is lost.
32  // \return The nb of bytes allocated, nothing if allready the appropriate size.
33  Size allocate(Size pSize);
34 
35  // Resize the byte array
36  // Keep previous data [0 to min(pSize, mSize)]
37  Size resize(Size pSize);
38 
39  // Assign data bytes and size (allocate for size, then copy bytes if exists)
40  Size setData(Size size, const Byte* bytes);
41 
42  // Update Sub data,
43  // doesn't allocate and only copy size * bytes at the offset location
44  // only if all fits in the existing allocated buffer
45  Size setSubData(Size offset, Size size, const Byte* bytes);
46 
47  // Append new data at the end of the current buffer
48  // do a resize( size + getSIze) and copy the new data
49  // \return the number of bytes copied
50  Size append(Size size, const Byte* data);
51 
52  // Access the byte array.
53  // The edit version allow to map data.
54  const Byte* readData() const { return _data; }
55  Byte* editData() { return _data; }
56 
57  template< typename T > const T* read() const { return reinterpret_cast< T* > (_data); }
58  template< typename T > T* edit() { return reinterpret_cast< T* > (_data); }
59 
60  // Access the current version of the sysmem, used to compare if copies are in sync
61  Stamp getStamp() const { return _stamp; }
62 
63  static Size allocateMemory(Byte** memAllocated, Size size);
64  static void deallocateMemory(Byte* memDeallocated, Size size);
65 
66  bool isAvailable() const { return (_data != 0); }
67 
68 private:
69  Stamp _stamp{ 0 };
70  Size _size{ 0 };
71  Byte* _data{ nullptr };
72 }; // Sysmem
73 
74 }
75 
76 #endif