Nux

Merge lp:~3v1n0/nux/fallback-texture into lp:nux

Proposed by Marco Trevisan (Treviño)
Status: Merged
Approved by: Andrea Azzarone
Approved revision: 808
Merged at revision: 799
Proposed branch: lp:~3v1n0/nux/fallback-texture
Merge into: lp:nux
Diff against target: 239 lines (+86/-18)
4 files modified
NuxGraphics/GLTextureResourceManager.cpp (+33/-13)
NuxGraphics/ImageSurface.cpp (+1/-0)
tests/Makefile.am (+1/-4)
tests/gtest-nuxgraphics-texture.cpp (+51/-1)
To merge this branch: bzr merge lp:~3v1n0/nux/fallback-texture
Reviewer Review Type Date Requested Status
PS Jenkins bot (community) continuous-integration Approve
Andrea Azzarone (community) Approve
Review via email: mp+172927@code.launchpad.net

Commit message

GLTextureResourceManager: always return an empty (but non-NULL) texture if env var is set

Specifically, if NUX_FALLBACK_TEXTURE env variable is set, then we always try to
generate a valid texture. This is nice for testing purposes

Description of the change

When generating new textures from a local file or a pixbuf, Nux returns a new (not-NULL) BaseTexture only if the pixbuf is valid or the file exists. This is generally ok, but for testing purposes we sometimes (such as in tests) we need to ignore this and try to return a non-NULL (but empty) texture.
So, now setting the NUX_FALLBACK_TEXTURE to any value, makes Nux to be less strict on this, and always return an empty BaseTexture.

New tests added. Improved logging as well.

To post a comment you must log in.
Revision history for this message
Andrea Azzarone (azzar1) :
review: Approve
Revision history for this message
PS Jenkins bot (ps-jenkins) :
review: Approve (continuous-integration)

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'NuxGraphics/GLTextureResourceManager.cpp'
2--- NuxGraphics/GLTextureResourceManager.cpp 2013-04-07 18:03:50 +0000
3+++ NuxGraphics/GLTextureResourceManager.cpp 2013-07-03 23:17:28 +0000
4@@ -25,9 +25,11 @@
5 #include "GpuDevice.h"
6 #include "GraphicsEngine.h"
7 #include "GLTextureResourceManager.h"
8+#include "NuxCore/Logger.h"
9
10 namespace nux
11 {
12+ DECLARE_LOGGER(logger, "nux.gltexture.resource.manager");
13
14 NUX_IMPLEMENT_OBJECT_TYPE(BaseTexture);
15 NUX_IMPLEMENT_OBJECT_TYPE(Texture2D);
16@@ -43,6 +45,24 @@
17 NUX_IMPLEMENT_OBJECT_TYPE(CachedTextureVolume);
18 NUX_IMPLEMENT_OBJECT_TYPE(CachedTextureFrameAnimation);
19
20+namespace
21+{
22+ nux::BaseTexture* get_null_texture(std::string const& extra_msg = "")
23+ {
24+ if (!g_getenv("NUX_FALLBACK_TEXTURE"))
25+ {
26+ LOG_ERROR(logger) << "Invalid target, impossible to generate a new texture."
27+ << " " << extra_msg;
28+ return nullptr;
29+ }
30+
31+ LOG_WARN(logger) << "Invalid target, impossible to generate a new valid texture."
32+ << " " << extra_msg << ". Using fallback mode...";
33+
34+ return GetGraphicsDisplay()->GetGpuDevice()->CreateSystemCapableTexture();
35+ }
36+}
37+
38 /*! Up cast a Resource.
39 The source must be derived from the destination type
40 @param T Destination type.
41@@ -60,6 +80,7 @@
42
43 BaseTexture* CreateTexture2DFromPixbuf(GdkPixbuf* pixbuf, bool premultiply)
44 {
45+ NUX_RETURN_VALUE_IF_NULL(pixbuf, get_null_texture("Null Pixbuf"));
46 const unsigned int rowstride = gdk_pixbuf_get_rowstride(pixbuf);
47 const unsigned int width = gdk_pixbuf_get_width(pixbuf);
48 const unsigned int height = gdk_pixbuf_get_height(pixbuf);
49@@ -158,8 +179,10 @@
50 }
51
52 BaseTexture* CreateTexture2DFromFile(const char* filename, int max_size,
53- bool premultiply)
54+ bool premultiply)
55 {
56+ NUX_RETURN_VALUE_IF_NULL(filename, get_null_texture("Empty filename"));
57+
58 GError* error = NULL;
59 GdkPixbuf* pixbuf = gdk_pixbuf_new_from_file_at_size(filename, max_size, max_size, &error);
60 if (error == NULL)
61@@ -170,16 +193,16 @@
62 }
63 else
64 {
65- nuxDebugMsg("%s", error->message);
66+ std::string error_msg = (error->message ? error->message : "Unknown error");
67 g_error_free(error);
68- return NULL;
69+ return get_null_texture("Impossible to generate a pixbuf: "+error_msg);
70 }
71 }
72
73 BaseTexture* CreateTextureFromPixbuf(GdkPixbuf* pixbuf)
74 {
75 NBitmapData* BitmapData = LoadGdkPixbuf(pixbuf);
76- NUX_RETURN_VALUE_IF_NULL(BitmapData, 0);
77+ NUX_RETURN_VALUE_IF_NULL(BitmapData, get_null_texture("Invalid Pixbuf"));
78
79 if (BitmapData->IsTextureData())
80 {
81@@ -188,15 +211,14 @@
82 return texture;
83 }
84 delete BitmapData;
85- return 0;
86+ return get_null_texture("Invalid Pixbuf");
87 }
88
89 BaseTexture* CreateTextureFromFile(const char* TextureFilename)
90 {
91 BaseTexture* texture = NULL;
92-
93 NBitmapData* BitmapData = LoadImageFile(TextureFilename);
94- NUX_RETURN_VALUE_IF_NULL(BitmapData, 0);
95+ NUX_RETURN_VALUE_IF_NULL(BitmapData, get_null_texture("No data for '"+(TextureFilename ? std::string(TextureFilename) : "<null>")+"'"));
96
97 if (BitmapData->IsTextureData())
98 {
99@@ -222,6 +244,7 @@
100 else
101 {
102 nuxDebugMsg("[CreateTextureFromFile] Invalid texture format type for file(%s)", TextureFilename);
103+ texture = get_null_texture();
104 }
105
106 delete BitmapData;
107@@ -230,8 +253,7 @@
108
109 BaseTexture* CreateTextureFromBitmapData(const NBitmapData* BitmapData)
110 {
111- if (BitmapData == 0)
112- return 0;
113+ NUX_RETURN_VALUE_IF_NULL(BitmapData, get_null_texture("Invalid BitmapData"));
114
115 if (BitmapData->IsTextureData())
116 {
117@@ -257,15 +279,13 @@
118 texture->Update(BitmapData);
119 return texture;
120 }
121- return 0;
122+ return get_null_texture();
123 }
124
125 BaseTexture* LoadTextureFromFile(const std::string& filename)
126 {
127 NBitmapData* bitmap = LoadImageFile(filename.c_str());
128-
129- if (bitmap == NULL)
130- return NULL;
131+ NUX_RETURN_VALUE_IF_NULL(bitmap, get_null_texture("No Data for '"+filename+"'"));
132
133 BaseTexture* texture = CreateTextureFromBitmapData(bitmap);
134 delete bitmap;
135
136=== modified file 'NuxGraphics/ImageSurface.cpp'
137--- NuxGraphics/ImageSurface.cpp 2013-04-08 00:04:18 +0000
138+++ NuxGraphics/ImageSurface.cpp 2013-07-03 23:17:28 +0000
139@@ -41,6 +41,7 @@
140
141 NBitmapData *LoadGdkPixbuf(GdkPixbuf *pixbuf)
142 {
143+ NUX_RETURN_VALUE_IF_NULL(pixbuf, nullptr);
144 unsigned int width = gdk_pixbuf_get_width(pixbuf);
145 unsigned int height = gdk_pixbuf_get_height(pixbuf);
146 unsigned int row_bytes = gdk_pixbuf_get_rowstride(pixbuf);
147
148=== modified file 'tests/Makefile.am'
149--- tests/Makefile.am 2013-04-18 13:28:09 +0000
150+++ tests/Makefile.am 2013-07-03 23:17:28 +0000
151@@ -189,10 +189,7 @@
152 libgtest.a
153
154 gtest_nuxgraphics_LDFLAGS = \
155- -lpthread -lgmock \
156- -lboost_filesystem -lboost_system
157-
158-
159+ -lgmock
160
161 TestFlags = -I$(srcdir) \
162 -I$(top_srcdir) \
163
164=== modified file 'tests/gtest-nuxgraphics-texture.cpp'
165--- tests/gtest-nuxgraphics-texture.cpp 2012-12-03 11:44:26 +0000
166+++ tests/gtest-nuxgraphics-texture.cpp 2013-07-03 23:17:28 +0000
167@@ -15,15 +15,23 @@
168
169 namespace {
170
171+const char *FALLBACK_ENV = "NUX_FALLBACK_TEXTURE";
172+
173 class TestTextures : public ::testing::Test
174 {
175 public:
176 virtual void SetUp()
177- {
178+ {
179+ g_unsetenv(FALLBACK_ENV);
180 nux::NuxInitialize(0);
181 wnd_thread.reset(nux::CreateNuxWindow("nux::TestTextures", 300, 200, nux::WINDOWSTYLE_NORMAL, NULL, false, NULL, NULL));
182 }
183
184+ void EnableFallbackMode()
185+ {
186+ g_setenv(FALLBACK_ENV, "TRUE", TRUE);
187+ }
188+
189 std::unique_ptr<nux::WindowThread> wnd_thread;
190 };
191
192@@ -52,4 +60,46 @@
193 }
194 }
195
196+TEST_F(TestTextures, FallbackTexture2DFromFile)
197+{
198+ ASSERT_THAT(CreateTexture2DFromFile(nullptr, -1, false), IsNull());
199+ EnableFallbackMode();
200+ ASSERT_THAT(CreateTexture2DFromFile(nullptr, -1, false), NotNull());
201+}
202+
203+TEST_F(TestTextures, FallbackTexture2DFromPixbuf)
204+{
205+ ASSERT_THAT(CreateTexture2DFromPixbuf(nullptr, false), IsNull());
206+ EnableFallbackMode();
207+ ASSERT_THAT(CreateTexture2DFromPixbuf(nullptr, false), NotNull());
208+}
209+
210+TEST_F(TestTextures, FallbackTextureFromPixbuf)
211+{
212+ ASSERT_THAT(CreateTextureFromPixbuf(nullptr), IsNull());
213+ EnableFallbackMode();
214+ ASSERT_THAT(CreateTextureFromPixbuf(nullptr), NotNull());
215+}
216+
217+TEST_F(TestTextures, FallbackTextureFromFile)
218+{
219+ ASSERT_THAT(CreateTextureFromFile(nullptr), IsNull());
220+ EnableFallbackMode();
221+ ASSERT_THAT(CreateTextureFromFile(nullptr), NotNull());
222+}
223+
224+TEST_F(TestTextures, FallbackTextureFromBitmapData)
225+{
226+ ASSERT_THAT(CreateTextureFromBitmapData(nullptr), IsNull());
227+ EnableFallbackMode();
228+ ASSERT_THAT(CreateTextureFromBitmapData(nullptr), NotNull());
229+}
230+
231+TEST_F(TestTextures, FallbackTextureLoadFromFile)
232+{
233+ ASSERT_THAT(LoadTextureFromFile(std::string()), IsNull());
234+ EnableFallbackMode();
235+ ASSERT_THAT(LoadTextureFromFile(std::string()), NotNull());
236+}
237+
238 }
239\ No newline at end of file

Subscribers

People subscribed via source and target branches