Debian Codegraph @

src/python/PyImath/PyImathBufferProtocol.cpp

raw
1 //
2 // SPDX-License-Identifier: BSD-3-Clause
3 // Copyright Contributors to the OpenEXR Project.
4 //
5
6 // clang-format off
7
8 #include <ImathVec.h>
9 #include "PyImathBufferProtocol.h"
10 #include "PyImathExport.h"
11 #include "PyImathFixedArray.h"
12 #include "PyImathFixedArrayTraits.h"
13
14 #include <type_traits>
15
16 namespace PyImath {
17
18 namespace {
19
20 // A wrapper API for the buffer protocol functions to access and traverse
21 // the memory of a FixedArray.
22 //
23 template <class ArrayT>
24 class BufferAPI
25 {
26 using T = typename ArrayT::BaseType;
27 static_assert (std::is_same<ArrayT, FixedArray<T> >::value,
28 "BufferAPI is only valid for FixedArray classes");
29
30 public:
31
32 virtual ~BufferAPI()
33 { delete[] shape; delete[] stride; }
34
35 // The size, in bytes, of the smallest individual element of a buffer
36 // element. For example, a V3fArray is a 2D array of 4-byte floats.
37 Py_ssize_t atomicSize() const
38 { return FixedArrayAtomicSize<T>::value; }
39
40 // NonCopyable
41 BufferAPI (const BufferAPI &rhs) = delete;
42 BufferAPI &operator= (const BufferAPI &rhs) = delete;
43
44 // API
45 virtual bool sharedBuffer() const = 0;
46 virtual Py_ssize_t numBytes() const = 0;
47 virtual bool readOnly() const = 0;
48 virtual void * buffer() = 0;
49
50 protected:
51
52 BufferAPI (const unsigned int length, const unsigned int interleave)
53 : dimensions (FixedArrayDimension<T>::value),
54 shape (new Py_ssize_t[dimensions]),
55 stride (new Py_ssize_t[dimensions])
56 {
57 shape[0] = Py_ssize_t (length);
58 stride[0] = atomicSize() * FixedArrayWidth<T>::value * interleave;
59 for (int d=1; d<dimensions; d++)
60 {
61 shape[d] = FixedArrayWidth<T>::value * interleave;
62 stride[d] = atomicSize();
63 }
64 }
65
66 public:
67
68 // The number of dimensions in the data buffer (e.g. a one dimensional
69 // FixedArray of V3fs would have a data buffer dimension of 2).
70 int dimensions;
71
72 Py_ssize_t *shape;
73 Py_ssize_t *stride;
74 };
75
76
77 // The SharedBuffer class is used for buffers that will be shared between
78 // two different Python objects, and so the FixedArray is stored internally
79 // as a reference.
80 //
81 template <class ArrayT>
82 class SharedBufferAPI : public BufferAPI<ArrayT>
83 {
84 public:
85
86 using BufferAPI<ArrayT>::atomicSize;
87
88 explicit
89 SharedBufferAPI (ArrayT &a)
90 : BufferAPI<ArrayT> (a.len(), a.stride()),
91 _orig (a)
92 {}
93
94 // NonCopyable
95 SharedBufferAPI (const SharedBufferAPI &rhs) = delete;
96 SharedBufferAPI &operator= (const SharedBufferAPI &rhs) = delete;
97
98 virtual ~SharedBufferAPI() = default;
99
100 bool sharedBuffer() const override
101 { return true; }
102
103 Py_ssize_t numBytes() const override
104 { return _orig.len() * atomicSize() * _orig.stride(); }
105
106 bool readOnly() const override
107 { return !_orig.writable(); }
108
109 void *buffer() override
110 { return static_cast<void *> (&_orig.direct_index(0)); }
111
112 private:
113
114 ArrayT &_orig;
115 };
116
117
118 // This class exists for the case in which the Python array view
119 // object is writable but the source FixedArray is not.
120 //
121 template <class ArrayT>
122 class CopyBufferAPI : public BufferAPI<ArrayT>
123 {
124 public:
125
126 using BufferAPI<ArrayT>::atomicSize;
127
128 explicit
129 CopyBufferAPI (ArrayT &a)
130 : BufferAPI<ArrayT> (a.len(), a.stride()),
131 _copy (a)
132 {}
133
134 virtual ~CopyBufferAPI() = default;
135
136 // NonCopyable
137 CopyBufferAPI (const CopyBufferAPI &rhs) = delete;
138 CopyBufferAPI &operator= (const CopyBufferAPI &rhs) = delete;
139
140 bool sharedBuffer() const override
141 { return false; }
142
143 Py_ssize_t numBytes() const override
144 { return _copy.len() * atomicSize() * _copy.stride(); }
145
146 bool readOnly() const override
147 { return false; }
148
149 void *buffer() override
150 { return static_cast<void *> (&_copy.direct_index(0)); }
151
152 private:
153
154 ArrayT _copy;
155 };
156
157
158 template <class ArrayT>
159 Py_ssize_t
160 bufferInfo (ArrayT &array, void **buf)
161 {
162 *buf = static_cast<void *> (&array.direct_index(0));
163 return array.len() * sizeof(typename ArrayT::BaseType);
164 }
165
166
167 template <class ArrayT>
168 Py_ssize_t
169 getreadbuf (PyObject *obj, Py_ssize_t segment, void **buf)
170 {
171 if (segment != 0)
172 {
173 PyErr_SetString (PyExc_ValueError, "FixedArrays are not segmented");
174 return -1;
175 }
176
177 boost::python::extract<ArrayT> eObj (obj);
178 if (!eObj.check())
179 {
180 PyErr_SetString (PyExc_ValueError, "Cannot extract FixedArray");
181 return -1;
182 }
183
184 ArrayT array = eObj();
185 return bufferInfo<ArrayT> (array, buf);
186 }
187
188
189 template <class ArrayT>
190 Py_ssize_t
191 getwritebuf (PyObject *obj, Py_ssize_t segment, void **buf)
192 {
193 PyErr_SetString
194 (PyExc_ValueError,
195 "writable buffers supported only with new-style buffer protocol.");
196
197 return -1;
198 }
199
200
201 template <class ArrayT>
202 Py_ssize_t
203 getsegcount (PyObject *obj, Py_ssize_t *lenp)
204 {
205 // FixedArrays are always in one, fixed-sized block
206 return 1;
207 }
208
209
210 template <class ArrayT>
211 Py_ssize_t
212 getcharbuf (PyObject *obj, Py_ssize_t segment, const char **charBuf)
213 {
214 return getreadbuf<ArrayT> (obj, segment, (void **)charBuf);
215 }
216
217
218 template <class ArrayT>
219 void
220 releasebuffer (PyObject *obj, Py_buffer *view)
221 {
222 delete static_cast<BufferAPI<ArrayT> *>(view->internal);
223 }
224
225
226 template <class ArrayT>
227 int
228 getbuffer (PyObject *obj, Py_buffer *view, int flags)
229 {
230 if (view == nullptr)
231 {
232 PyErr_SetString (PyExc_ValueError, "Buffer view is NULL");
233 return -1;
234 }
235
236 if ((flags & PyBUF_F_CONTIGUOUS) == PyBUF_F_CONTIGUOUS)
237 {
238 PyErr_SetString (PyExc_ValueError, "FORTRAN order not supported");
239 return -1;
240 }
241
242 boost::python::extract<ArrayT> eObj (obj);
243 if (!eObj.check())
244 {
245 PyErr_SetString (PyExc_ValueError, "Cannot extract FixedArray");
246 return -1;
247 }
248 ArrayT array = eObj();
249
250 if (array.isMaskedReference())
251 {
252 PyErr_SetString (PyExc_ValueError, "Buffer protocol does not support masked references");
253 return -1;
254 }
255
256 BufferAPI<ArrayT> *api = nullptr;
257 bool writableBuffer = ((flags & PyBUF_WRITABLE) == PyBUF_WRITABLE);
258 if (writableBuffer && !array.writable())
259 api = new CopyBufferAPI<ArrayT> (array);
260 else
261 api = new SharedBufferAPI<ArrayT> (array);
262
263 view->internal = api;
264 view->buf = api->buffer();
265 view->len = api->numBytes();
266 view->readonly = api->readOnly();
267 view->itemsize = api->atomicSize();
268 view->suboffsets = nullptr;
269
270 view->format = ((flags & PyBUF_FORMAT) == PyBUF_FORMAT)
271 ? PyFormat<typename ArrayT::BaseType>()
272 : nullptr;
273
274 view->strides = ((flags & PyBUF_STRIDES) == PyBUF_STRIDES)
275 ? api->stride
276 : nullptr;
277
278 if ((flags & PyBUF_ND) == PyBUF_ND)
279 {
280 view->ndim = api->dimensions;
281 view->shape = api->shape;
282 }
283 else
284 {
285 view->ndim = 0;
286 view->shape = nullptr;
287 }
288
289 view->obj = obj;
290 Py_INCREF (obj);
291
292 return 0;
293 }
294
295
296 // This structure serves to instantiate a PyBufferProcs instance with pointers
297 // to the right buffer protocol functions.
298 template <class ArrayT>
299 struct FixedArrayBufferProcs
300 {
301 static PyBufferProcs procs;
302 };
303
304
305 template <class ArrayT>
306 PyBufferProcs FixedArrayBufferProcs<ArrayT>::procs =
307 {
308 #if PY_MAJOR_VERSION == 2
309 (readbufferproc) getreadbuf<ArrayT>,
310 (writebufferproc) getwritebuf<ArrayT>,
311 (segcountproc) getsegcount<ArrayT>,
312 (charbufferproc) getcharbuf<ArrayT>,
313 (getbufferproc) getbuffer<ArrayT>,
314 (releasebufferproc) releasebuffer<ArrayT>
315 #else // Note deprecation of support for the older style
316 (getbufferproc) getbuffer<ArrayT>,
317 (releasebufferproc) releasebuffer<ArrayT>
318 #endif
319 };
320
321 } // anonymous
322
323
324 template <class ArrayT>
325 void
326 add_buffer_protocol (boost::python::class_<ArrayT> &classObj)
327 {
328 auto *typeObj = reinterpret_cast<PyTypeObject *> (classObj.ptr());
329 typeObj->tp_as_buffer = &FixedArrayBufferProcs<ArrayT>::procs;
330 #if PY_MAJOR_VERSION == 2
331 typeObj->tp_flags |= (Py_TPFLAGS_HAVE_NEWBUFFER | Py_TPFLAGS_HAVE_GETCHARBUFFER);
332 #endif
333 }
334
335
336 template <class ArrayT>
337 ArrayT *
338 fixedArrayFromBuffer (PyObject *obj)
339 {
340 if (!PyObject_CheckBuffer (obj))
341 throw std::invalid_argument ("Python object does not support the buffer protocol");
342
343 // Request a strided buffer with type & dimensions.
344 Py_buffer view;
345 memset(&view, 0, sizeof(view));
346 if (PyObject_GetBuffer (obj, &view, PyBUF_FORMAT | PyBUF_STRIDES) != 0)
347 {
348 throw std::logic_error ("Failed to get dimensioned, typed buffer");
349 }
350
351 // We have a buffer. Check that the type matches.
352 if (!view.format ||
353 view.format[0] == '>' ||
354 view.format[0] == '!' ||
355 view.format[0] == '=' ||
356 view.format[0] == '^')
357 {
358 PyBuffer_Release(&view);
359 throw std::invalid_argument ("Unsupported buffer type");
360 }
361
362 ArrayT *array = new ArrayT (view.shape[0], PyImath::UNINITIALIZED);
363 memcpy (reinterpret_cast<void*>(&array->direct_index(0)), view.buf, view.len);
364 PyBuffer_Release(&view);
365
366 return array;
367 }
368
369 ///////////////////////////////////////////////////////////////////////////////
370
371 template PYIMATH_EXPORT void add_buffer_protocol (boost::python::class_<FixedArray<short> > &classObj);
372 template PYIMATH_EXPORT void add_buffer_protocol (boost::python::class_<FixedArray<int> > &classObj);
373 template PYIMATH_EXPORT void add_buffer_protocol (boost::python::class_<FixedArray<int64_t> > &classObj);
374 template PYIMATH_EXPORT void add_buffer_protocol (boost::python::class_<FixedArray<float> > &classObj);
375 template PYIMATH_EXPORT void add_buffer_protocol (boost::python::class_<FixedArray<double> > &classObj);
376 template PYIMATH_EXPORT void add_buffer_protocol (boost::python::class_<FixedArray<unsigned char> > &classObj);
377
378 template PYIMATH_EXPORT void add_buffer_protocol (boost::python::class_<FixedArray<Imath::Vec2<short> > > &classObj);
379 template PYIMATH_EXPORT void add_buffer_protocol (boost::python::class_<FixedArray<Imath::Vec2<int> > > &classObj);
380 template PYIMATH_EXPORT void add_buffer_protocol (boost::python::class_<FixedArray<Imath::Vec2<int64_t> > > &classObj);
381 template PYIMATH_EXPORT void add_buffer_protocol (boost::python::class_<FixedArray<Imath::Vec2<float> > > &classObj);
382 template PYIMATH_EXPORT void add_buffer_protocol (boost::python::class_<FixedArray<Imath::Vec2<double> > > &classObj);
383 template PYIMATH_EXPORT void add_buffer_protocol (boost::python::class_<FixedArray<Imath::Vec3<short> > > &classObj);
384 template PYIMATH_EXPORT void add_buffer_protocol (boost::python::class_<FixedArray<Imath::Vec3<int> > > &classObj);
385 template PYIMATH_EXPORT void add_buffer_protocol (boost::python::class_<FixedArray<Imath::Vec3<int64_t> > > &classObj);
386 template PYIMATH_EXPORT void add_buffer_protocol (boost::python::class_<FixedArray<Imath::Vec3<float> > > &classObj);
387 template PYIMATH_EXPORT void add_buffer_protocol (boost::python::class_<FixedArray<Imath::Vec3<double> > > &classObj);
388 template PYIMATH_EXPORT void add_buffer_protocol (boost::python::class_<FixedArray<Imath::Vec4<short> > > &classObj);
389 template PYIMATH_EXPORT void add_buffer_protocol (boost::python::class_<FixedArray<Imath::Vec4<int> > > &classObj);
390 template PYIMATH_EXPORT void add_buffer_protocol (boost::python::class_<FixedArray<Imath::Vec4<int64_t> > > &classObj);
391 template PYIMATH_EXPORT void add_buffer_protocol (boost::python::class_<FixedArray<Imath::Vec4<float> > > &classObj);
392 template PYIMATH_EXPORT void add_buffer_protocol (boost::python::class_<FixedArray<Imath::Vec4<double> > > &classObj);
393
394 template PYIMATH_EXPORT FixedArray<Imath::Vec2<short> >* fixedArrayFromBuffer (PyObject *obj);
395 template PYIMATH_EXPORT FixedArray<Imath::Vec2<int> >* fixedArrayFromBuffer (PyObject *obj);
396 template PYIMATH_EXPORT FixedArray<Imath::Vec2<int64_t> >* fixedArrayFromBuffer (PyObject *obj);
397 template PYIMATH_EXPORT FixedArray<Imath::Vec2<float> >* fixedArrayFromBuffer (PyObject *obj);
398 template PYIMATH_EXPORT FixedArray<Imath::Vec2<double> >* fixedArrayFromBuffer (PyObject *obj);
399 template PYIMATH_EXPORT FixedArray<Imath::Vec3<short> >* fixedArrayFromBuffer (PyObject *obj);
400 template PYIMATH_EXPORT FixedArray<Imath::Vec3<int> >* fixedArrayFromBuffer (PyObject *obj);
401 template PYIMATH_EXPORT FixedArray<Imath::Vec3<int64_t> >* fixedArrayFromBuffer (PyObject *obj);
402 template PYIMATH_EXPORT FixedArray<Imath::Vec3<float> >* fixedArrayFromBuffer (PyObject *obj);
403 template PYIMATH_EXPORT FixedArray<Imath::Vec3<double> >* fixedArrayFromBuffer (PyObject *obj);
404 template PYIMATH_EXPORT FixedArray<Imath::Vec4<short> >* fixedArrayFromBuffer (PyObject *obj);
405 template PYIMATH_EXPORT FixedArray<Imath::Vec4<int> >* fixedArrayFromBuffer (PyObject *obj);
406 template PYIMATH_EXPORT FixedArray<Imath::Vec4<int64_t> >* fixedArrayFromBuffer (PyObject *obj);
407 template PYIMATH_EXPORT FixedArray<Imath::Vec4<float> >* fixedArrayFromBuffer (PyObject *obj);
408 template PYIMATH_EXPORT FixedArray<Imath::Vec4<double> >* fixedArrayFromBuffer (PyObject *obj);
409 template PYIMATH_EXPORT FixedArray<short>* fixedArrayFromBuffer (PyObject *obj);
410 template PYIMATH_EXPORT FixedArray<int>* fixedArrayFromBuffer (PyObject *obj);
411 template PYIMATH_EXPORT FixedArray<int64_t>* fixedArrayFromBuffer (PyObject *obj);
412 template PYIMATH_EXPORT FixedArray<float>* fixedArrayFromBuffer (PyObject *obj);
413 template PYIMATH_EXPORT FixedArray<double>* fixedArrayFromBuffer (PyObject *obj);
414
415 }