Python GPU: Add reference of PyObject GPU object to the GPU object itself

Instead of creating different python wrappers for the same GPU object,
return the same `PyObject` created earlier.

This also allows for more secure access to existing GPU objects.

Reviewed By: brecht

Differential Revision: https://developer.blender.org/D11044
This commit is contained in:
Germano Cavalcante
2021-04-29 14:48:59 -03:00
committed by Germano Cavalcante
parent c96506d54a
commit 04b6296e81
12 changed files with 143 additions and 25 deletions

View File

@@ -247,7 +247,7 @@ static PyObject *pygpu_texture__tp_new(PyTypeObject *UNUSED(self), PyObject *arg
return NULL;
}
return BPyGPUTexture_CreatePyObject(tex);
return BPyGPUTexture_CreatePyObject(tex, false);
}
PyDoc_STRVAR(pygpu_texture_width_doc, "Width of the texture.\n\n:type: `int`");
@@ -412,6 +412,9 @@ static PyObject *pygpu_texture_free(BPyGPUTexture *self)
static void BPyGPUTexture__tp_dealloc(BPyGPUTexture *self)
{
if (self->tex) {
#if GPU_USE_PY_REFERENCES
GPU_texture_py_reference_set(self->tex, NULL);
#endif
GPU_texture_free(self->tex);
}
Py_TYPE(self)->tp_free((PyObject *)self);
@@ -535,10 +538,7 @@ static PyObject *pygpu_texture_from_image(PyObject *UNUSED(self), PyObject *arg)
BKE_imageuser_default(&iuser);
GPUTexture *tex = BKE_image_get_gpu_texture(ima, &iuser, NULL);
/* Increase the texture reference count. */
GPU_texture_ref(tex);
return BPyGPUTexture_CreatePyObject(tex);
return BPyGPUTexture_CreatePyObject(tex, true);
}
static struct PyMethodDef pygpu_texture__m_methods[] = {
@@ -595,13 +595,33 @@ PyObject *bpygpu_texture_init(void)
/** \name Public API
* \{ */
PyObject *BPyGPUTexture_CreatePyObject(GPUTexture *tex)
PyObject *BPyGPUTexture_CreatePyObject(GPUTexture *tex, bool shared_reference)
{
BPyGPUTexture *self;
if (shared_reference) {
#if GPU_USE_PY_REFERENCES
void **ref = GPU_texture_py_reference_get(tex);
if (ref) {
/* Retrieve BPyGPUTexture reference. */
self = POINTER_OFFSET(ref, -offsetof(BPyGPUTexture, tex));
BLI_assert(self->tex == tex);
Py_INCREF(self);
return (PyObject *)self;
}
#endif
GPU_texture_ref(tex);
}
self = PyObject_New(BPyGPUTexture, &BPyGPUTexture_Type);
self->tex = tex;
#if GPU_USE_PY_REFERENCES
BLI_assert(GPU_texture_py_reference_get(tex) == NULL);
GPU_texture_py_reference_set(tex, &self->tex);
#endif
return (PyObject *)self;
}