clover: Switch memory objects to the new model.
[mesa.git] / src / gallium / state_trackers / clover / core / memory.hpp
1 //
2 // Copyright 2012 Francisco Jerez
3 //
4 // Permission is hereby granted, free of charge, to any person obtaining a
5 // copy of this software and associated documentation files (the "Software"),
6 // to deal in the Software without restriction, including without limitation
7 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 // and/or sell copies of the Software, and to permit persons to whom the
9 // Software is furnished to do so, subject to the following conditions:
10 //
11 // The above copyright notice and this permission notice shall be included in
12 // all copies or substantial portions of the Software.
13 //
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
18 // OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20 // OTHER DEALINGS IN THE SOFTWARE.
21 //
22
23 #ifndef CLOVER_CORE_MEMORY_HPP
24 #define CLOVER_CORE_MEMORY_HPP
25
26 #include <functional>
27 #include <map>
28 #include <memory>
29
30 #include "core/object.hpp"
31 #include "core/queue.hpp"
32
33 namespace clover {
34 class resource;
35 class sub_resource;
36
37 class memory_obj : public ref_counter, public _cl_mem {
38 protected:
39 memory_obj(context &ctx, cl_mem_flags flags,
40 size_t size, void *host_ptr);
41 memory_obj(const memory_obj &obj) = delete;
42
43 public:
44 virtual ~memory_obj();
45
46 virtual cl_mem_object_type type() const = 0;
47 virtual clover::resource &resource(command_queue &q) = 0;
48
49 void destroy_notify(std::function<void ()> f);
50 cl_mem_flags flags() const;
51 size_t size() const;
52 void *host_ptr() const;
53
54 context &ctx;
55
56 private:
57 cl_mem_flags _flags;
58 size_t _size;
59 void *_host_ptr;
60 std::function<void ()> _destroy_notify;
61
62 protected:
63 std::string data;
64 };
65
66 class buffer : public memory_obj {
67 protected:
68 buffer(context &ctx, cl_mem_flags flags,
69 size_t size, void *host_ptr);
70
71 public:
72 virtual cl_mem_object_type type() const;
73 };
74
75 class root_buffer : public buffer {
76 public:
77 root_buffer(context &ctx, cl_mem_flags flags,
78 size_t size, void *host_ptr);
79
80 virtual clover::resource &resource(command_queue &q);
81
82 private:
83 std::map<device *,
84 std::unique_ptr<root_resource>> resources;
85 };
86
87 class sub_buffer : public buffer {
88 public:
89 sub_buffer(root_buffer &parent, cl_mem_flags flags,
90 size_t offset, size_t size);
91
92 virtual clover::resource &resource(command_queue &q);
93 size_t offset() const;
94
95 root_buffer &parent;
96
97 private:
98 size_t _offset;
99 std::map<device *,
100 std::unique_ptr<sub_resource>> resources;
101 };
102
103 class image : public memory_obj {
104 protected:
105 image(context &ctx, cl_mem_flags flags,
106 const cl_image_format *format,
107 size_t width, size_t height, size_t depth,
108 size_t row_pitch, size_t slice_pitch, size_t size,
109 void *host_ptr);
110
111 public:
112 virtual clover::resource &resource(command_queue &q);
113 cl_image_format format() const;
114 size_t width() const;
115 size_t height() const;
116 size_t depth() const;
117 size_t row_pitch() const;
118 size_t slice_pitch() const;
119
120 private:
121 cl_image_format _format;
122 size_t _width;
123 size_t _height;
124 size_t _depth;
125 size_t _row_pitch;
126 size_t _slice_pitch;
127 std::map<device *,
128 std::unique_ptr<root_resource>> resources;
129 };
130
131 class image2d : public image {
132 public:
133 image2d(context &ctx, cl_mem_flags flags,
134 const cl_image_format *format, size_t width,
135 size_t height, size_t row_pitch,
136 void *host_ptr);
137
138 virtual cl_mem_object_type type() const;
139 };
140
141 class image3d : public image {
142 public:
143 image3d(context &ctx, cl_mem_flags flags,
144 const cl_image_format *format,
145 size_t width, size_t height, size_t depth,
146 size_t row_pitch, size_t slice_pitch,
147 void *host_ptr);
148
149 virtual cl_mem_object_type type() const;
150 };
151 }
152
153 #endif