Fix T59773: Raise exception if the gpu module is used in backgound mode.
authormano-wii <germano.costa@ig.com.br>
Wed, 2 Jan 2019 12:01:46 +0000 (10:01 -0200)
committermano-wii <germano.costa@ig.com.br>
Wed, 2 Jan 2019 12:01:46 +0000 (10:01 -0200)
Instead of crashing, an error message is displayed if a function of the gpu module is called without a GPU context.

Reviewers: brecht, campbellbarton, JacquesLucke, mont29

Subscribers: abdelmatinboulbayam, amir.shehata

Differential Revision: https://developer.blender.org/D4143

12 files changed:
source/blender/gpu/GPU_init_exit.h
source/blender/gpu/intern/gpu_init_exit.c
source/blender/python/gpu/CMakeLists.txt
source/blender/python/gpu/gpu_py.c [moved from source/blender/python/gpu/gpu_py_api.c with 61% similarity]
source/blender/python/gpu/gpu_py.h [moved from source/blender/python/gpu/gpu_py_api.h with 85% similarity]
source/blender/python/gpu/gpu_py_batch.c
source/blender/python/gpu/gpu_py_element.c
source/blender/python/gpu/gpu_py_offscreen.c
source/blender/python/gpu/gpu_py_primitive.c [deleted file]
source/blender/python/gpu/gpu_py_primitive.h [deleted file]
source/blender/python/gpu/gpu_py_shader.c
source/blender/python/intern/bpy_interface.c

index e89c970b7d9ef7d46063fe1c5f474c0f29954633..8f1a42c87955c169a2f82671d0646eca8f67216c 100644 (file)
@@ -38,6 +38,7 @@ extern "C" {
 
 void GPU_init(void);
 void GPU_exit(void);
+bool GPU_is_initialized(void);
 
 #ifdef __cplusplus
 }
index 55d0466c92970cd207b14155b546d0d498707f28..d21acb188d9027cda91f04c0b6a4c69ed3bae088 100644 (file)
@@ -73,7 +73,6 @@ void GPU_init(void)
 }
 
 
-
 void GPU_exit(void)
 {
        if (!G.background) {
@@ -92,3 +91,9 @@ void GPU_exit(void)
 
        initialized = false;
 }
+
+
+bool GPU_is_initialized(void)
+{
+       return initialized;
+}
index 0d4dedf7c763f9d37f03991fcf0a3547d5655b67..fb5eb07a7d8d468f355c09247b6e5886a02c58b9 100644 (file)
@@ -34,24 +34,22 @@ set(INC_SYS
 )
 
 set(SRC
-       gpu_py_api.c
+       gpu_py.c
        gpu_py_batch.c
        gpu_py_element.c
        gpu_py_matrix.c
        gpu_py_offscreen.c
-       gpu_py_primitive.c
        gpu_py_select.c
        gpu_py_shader.c
        gpu_py_types.c
        gpu_py_vertex_buffer.c
        gpu_py_vertex_format.c
 
-       gpu_py_api.h
+       gpu_py.h
        gpu_py_batch.h
        gpu_py_element.h
        gpu_py_matrix.h
        gpu_py_offscreen.h
-       gpu_py_primitive.h
        gpu_py_select.h
        gpu_py_shader.h
        gpu_py_types.h
similarity index 61%
rename from source/blender/python/gpu/gpu_py_api.c
rename to source/blender/python/gpu/gpu_py.c
index 373bce160b757247ffa8bd9531b1c5eb4b3dfffa..5470e791011a77197cb2f2b57e47bfc7f574ece0 100644 (file)
 
 #include "../generic/python_utildefines.h"
 
+#include "GPU_init_exit.h"
+#include "GPU_primitive.h"
+
 #include "gpu_py_matrix.h"
 #include "gpu_py_select.h"
 #include "gpu_py_types.h"
 
-#include "gpu_py_api.h" /* own include */
+#include "gpu_py.h" /* own include */
+
+
+/* -------------------------------------------------------------------- */
+
+/** \name Utils to invalidate functions
+ * \{ */
+
+bool bpygpu_is_initialized(void)
+{
+       if (!GPU_is_initialized()) {
+               PyErr_SetString(
+                       PyExc_SystemError,
+                       "GPU functions for drawing are not available in background mode");
+
+               return false;
+       }
+
+       return true;
+}
+
+/** \} */
+
+
+/* -------------------------------------------------------------------- */
+
+/** \name Primitive Type Utils
+ * \{ */
+
+int bpygpu_ParsePrimType(PyObject *o, void *p)
+{
+       Py_ssize_t mode_id_len;
+       const char *mode_id = _PyUnicode_AsStringAndSize(o, &mode_id_len);
+       if (mode_id == NULL) {
+               PyErr_Format(PyExc_ValueError,
+                            "expected a string, got %s",
+                            Py_TYPE(o)->tp_name);
+               return 0;
+       }
+#define MATCH_ID(id) \
+       if (mode_id_len == strlen(STRINGIFY(id))) { \
+               if (STREQ(mode_id, STRINGIFY(id))) { \
+                       mode = GPU_PRIM_##id; \
+                       goto success; \
+               } \
+       } ((void)0)
+
+       GPUPrimType mode;
+       MATCH_ID(POINTS);
+       MATCH_ID(LINES);
+       MATCH_ID(TRIS);
+       MATCH_ID(LINE_STRIP);
+       MATCH_ID(LINE_LOOP);
+       MATCH_ID(TRI_STRIP);
+       MATCH_ID(TRI_FAN);
+       MATCH_ID(LINE_STRIP_ADJ);
+
+#undef MATCH_ID
+       PyErr_Format(PyExc_ValueError,
+                    "unknown type literal: '%s'",
+                    mode_id);
+       return 0;
+
+success:
+       (*(GPUPrimType *)p) = mode;
+       return 1;
+}
+
+/** \} */
+
+
+/* -------------------------------------------------------------------- */
+
+/** \name GPU Module
+ * \{ */
+
 
 PyDoc_STRVAR(GPU_doc,
 "This module provides Python wrappers for the GPU implementation in Blender. "
@@ -83,3 +161,5 @@ PyObject *BPyInit_gpu(void)
 
        return mod;
 }
+
+/** \} */
similarity index 85%
rename from source/blender/python/gpu/gpu_py_api.h
rename to source/blender/python/gpu/gpu_py.h
index 20eafb3d1401c21e483ef2cddc2cadddc5f4d061..d3ee7b56def0053b0b11b7a7c76ca59b5ae8282b 100644 (file)
  *  \ingroup bpygpu
  */
 
-#ifndef __GPU_PY_API_H__
-#define __GPU_PY_API_H__
+#ifndef __GPU_PY_H__
+#define __GPU_PY_H__
+
+bool bpygpu_is_initialized(void);
+
+int bpygpu_ParsePrimType(PyObject *o, void *p);
 
 PyObject *BPyInit_gpu(void);
 
-#endif  /* __GPU_PY_API_H__ */
+#endif  /* __GPU_PY_H__ */
index ffbd2a17e344f5e9aef58ebc32fcd5c4fe88713c..aa687ecaff50da13b0f06a2facc600c142bdb908 100644 (file)
@@ -44,7 +44,7 @@
 
 #include "../generic/py_capi_utils.h"
 
-#include "gpu_py_primitive.h"
+#include "gpu_py.h"
 #include "gpu_py_shader.h"
 #include "gpu_py_vertex_buffer.h"
 #include "gpu_py_element.h"
@@ -85,7 +85,8 @@ static PyObject *bpygpu_Batch_new(PyTypeObject *UNUSED(type), PyObject *args, Py
 
        static const char *_keywords[] = {"type", "buf", "elem", NULL};
        static _PyArg_Parser _parser = {"|$O&O!O!:GPUBatch.__new__", _keywords, 0};
-       if (!_PyArg_ParseTupleAndKeywordsFast(
+       if (!bpygpu_is_initialized() ||
+           !_PyArg_ParseTupleAndKeywordsFast(
                args, kwds, &_parser,
                bpygpu_ParsePrimType, &params.type_id,
                &BPyGPUVertBuf_Type, &params.py_vertbuf,
index 12027d2e2b80c2ed2f1dfcb7c17e237d6ff7d142..b996e859bc7f3ed5d3ee5bbaaa403b268d3e6616 100644 (file)
@@ -36,7 +36,7 @@
 #include "../generic/py_capi_utils.h"
 #include "../generic/python_utildefines.h"
 
-#include "gpu_py_primitive.h"
+#include "gpu_py.h"
 #include "gpu_py_element.h" /* own include */
 
 
@@ -61,7 +61,8 @@ static PyObject *bpygpu_IndexBuf_new(PyTypeObject *UNUSED(type), PyObject *args,
 
        static const char *_keywords[] = {"type", "seq", NULL};
        static _PyArg_Parser _parser = {"$O&O:IndexBuf.__new__", _keywords, 0};
-       if (!_PyArg_ParseTupleAndKeywordsFast(
+       if (!bpygpu_is_initialized() ||
+           !_PyArg_ParseTupleAndKeywordsFast(
                args, kwds, &_parser,
                bpygpu_ParsePrimType, &params.type_id,
                &params.seq))
index c51a356d9004ee37e830a6ae101693d7d8afb3d9..2725b55a5da00da9605d2ba8c6dc8aa9ea25121d 100644 (file)
@@ -53,6 +53,7 @@
 
 #include "../generic/py_capi_utils.h"
 
+#include "gpu_py.h"
 #include "gpu_py_offscreen.h" /* own include */
 
 
@@ -93,7 +94,8 @@ static PyObject *bpygpu_offscreen_new(PyTypeObject *UNUSED(self), PyObject *args
 
        static const char *_keywords[] = {"width", "height", "samples", NULL};
        static _PyArg_Parser _parser = {"ii|i:GPUOffScreen.__new__", _keywords, 0};
-       if (!_PyArg_ParseTupleAndKeywordsFast(
+       if (!bpygpu_is_initialized() ||
+           !_PyArg_ParseTupleAndKeywordsFast(
                args, kwds, &_parser,
                &width, &height, &samples))
        {
diff --git a/source/blender/python/gpu/gpu_py_primitive.c b/source/blender/python/gpu/gpu_py_primitive.c
deleted file mode 100644 (file)
index 798dfc0..0000000
+++ /dev/null
@@ -1,81 +0,0 @@
-/*
- * ***** BEGIN GPL LICENSE BLOCK *****
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public License
- * as published by the Free Software Foundation; either version 2
- * of the License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- *
- * Copyright 2015, Blender Foundation.
- *
- * ***** END GPL LICENSE BLOCK *****
- */
-
-/** \file blender/python/gpu/gpu_py_primitive.c
- *  \ingroup bpygpu
- *
- * - Use ``bpygpu_`` for local API.
- * - Use ``BPyGPU`` for public API.
- */
-
-#include <Python.h>
-
-#include "BLI_utildefines.h"
-
-#include "GPU_primitive.h"
-
-#include "gpu_py_primitive.h" /* own include */
-
-
-/* -------------------------------------------------------------------- */
-
-/** \name Primitive Utils
- * \{ */
-
-int bpygpu_ParsePrimType(PyObject *o, void *p)
-{
-       Py_ssize_t mode_id_len;
-       const char *mode_id = _PyUnicode_AsStringAndSize(o, &mode_id_len);
-       if (mode_id == NULL) {
-               PyErr_Format(PyExc_ValueError,
-                            "expected a string, got %s",
-                            Py_TYPE(o)->tp_name);
-               return 0;
-       }
-#define MATCH_ID(id) \
-       if (mode_id_len == strlen(STRINGIFY(id))) { \
-               if (STREQ(mode_id, STRINGIFY(id))) { \
-                       mode = GPU_PRIM_##id; \
-                       goto success; \
-               } \
-       } ((void)0)
-
-       GPUPrimType mode;
-       MATCH_ID(POINTS);
-       MATCH_ID(LINES);
-       MATCH_ID(TRIS);
-       MATCH_ID(LINE_STRIP);
-       MATCH_ID(LINE_LOOP);
-       MATCH_ID(TRI_STRIP);
-       MATCH_ID(TRI_FAN);
-       MATCH_ID(LINE_STRIP_ADJ);
-
-#undef MATCH_ID
-       PyErr_Format(PyExc_ValueError,
-                    "unknown type literal: '%s'",
-                    mode_id);
-       return 0;
-
-success:
-       (*(GPUPrimType *)p) = mode;
-       return 1;
-}
diff --git a/source/blender/python/gpu/gpu_py_primitive.h b/source/blender/python/gpu/gpu_py_primitive.h
deleted file mode 100644 (file)
index d10ee01..0000000
+++ /dev/null
@@ -1,30 +0,0 @@
-/*
- * ***** BEGIN GPL LICENSE BLOCK *****
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public License
- * as published by the Free Software Foundation; either version 2
- * of the License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- *
- * ***** END GPL LICENSE BLOCK *****
- */
-
-/** \file blender/python/gpu/gpu_py_primitive.h
- *  \ingroup bpygpu
- */
-
-#ifndef __GPU_PY_PRIMITIVE_H__
-#define __GPU_PY_PRIMITIVE_H__
-
-int bpygpu_ParsePrimType(PyObject *o, void *p);
-
-#endif /* __GPU_PY_PRIMITIVE_H__ */
index 5f1ea7a33cea0a85aad473c2788a9bbc3ebc63e4..3c50900c6886becb828b33a0ae6c07c5399348f2 100644 (file)
@@ -36,6 +36,7 @@
 #include "../generic/python_utildefines.h"
 #include "../mathutils/mathutils.h"
 
+#include "gpu_py.h"
 #include "gpu_py_shader.h" /* own include */
 #include "gpu_py_vertex_format.h"
 
@@ -117,7 +118,8 @@ static PyObject *bpygpu_shader_new(PyTypeObject *UNUSED(type), PyObject *args, P
                "libcode", "defines", NULL};
 
        static _PyArg_Parser _parser = {"ss|$sss:GPUShader.__new__", _keywords, 0};
-       if (!_PyArg_ParseTupleAndKeywordsFast(
+       if (!bpygpu_is_initialized() ||
+           !_PyArg_ParseTupleAndKeywordsFast(
                args, kwds, &_parser,
                &params.vertexcode, &params.fragcode, &params.geocode,
                &params.libcode, &params.defines))
@@ -717,7 +719,9 @@ static PyObject *bpygpu_shader_from_builtin(PyObject *UNUSED(self), PyObject *ar
 {
        GPUBuiltinShader shader_id;
 
-       if (!bpygpu_ParseBultinShaderEnum(arg, &shader_id)) {
+       if (!bpygpu_is_initialized() ||
+           !bpygpu_ParseBultinShaderEnum(arg, &shader_id))
+       {
                return NULL;
        }
 
index e17e7562f2afe056bb407539e63b6e36604c86e6..3b4e8c0904c448b906b0283f2f452c5b72c6ff52 100644 (file)
@@ -74,7 +74,7 @@
 #include "../generic/blf_py_api.h"
 #include "../generic/idprop_py_api.h"
 #include "../generic/imbuf_py_api.h"
-#include "../gpu/gpu_py_api.h"
+#include "../gpu/gpu_py.h"
 #include "../bmesh/bmesh_py_api.h"
 #include "../mathutils/mathutils.h"