summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorMattias Andrée <maandree@operamail.com>2014-03-14 02:19:56 +0100
committerMattias Andrée <maandree@operamail.com>2014-03-14 02:19:56 +0100
commit09569c7798ea46da9fbc997bb4357e39534c7b64 (patch)
tree203bbfbd98d55e555988feaee884b2fb1262e9c2 /src
parentfix sigusr2 handling (diff)
downloadblueshift-09569c7798ea46da9fbc997bb4357e39534c7b64.tar.gz
blueshift-09569c7798ea46da9fbc997bb4357e39534c7b64.tar.bz2
blueshift-09569c7798ea46da9fbc997bb4357e39534c7b64.tar.xz
reduce allocations in vidmode
Signed-off-by: Mattias Andrée <maandree@operamail.com>
Diffstat (limited to 'src')
-rw-r--r--src/blueshift_vidmode.pyx72
-rw-r--r--src/blueshift_vidmode_c.c68
-rw-r--r--src/monitor.py4
3 files changed, 56 insertions, 88 deletions
diff --git a/src/blueshift_vidmode.pyx b/src/blueshift_vidmode.pyx
index d3ab99f..7f1810d 100644
--- a/src/blueshift_vidmode.pyx
+++ b/src/blueshift_vidmode.pyx
@@ -20,22 +20,42 @@ from libc.stdlib cimport malloc, free
cdef extern int blueshift_vidmode_open(int use_screen)
-cdef extern unsigned short int* blueshift_vidmode_read(int use_crtc)
+cdef extern int blueshift_vidmode_read(int use_crtc,
+ unsigned short int* r_curve,
+ unsigned short int* g_curve,
+ unsigned short int* b_curve)
cdef extern int blueshift_vidmode_apply(unsigned long long int use_crtcs,
- unsigned short int* r_curve,
- unsigned short int* g_curve,
- unsigned short int* b_curve)
+ unsigned short int* r_curve,
+ unsigned short int* g_curve,
+ unsigned short int* b_curve)
cdef extern void blueshift_vidmode_close()
+
+cdef int vidmode_gamma_size
+vidmode_gamma_size = 0
+
+cdef unsigned short int* r_c
+cdef unsigned short int* g_c
+cdef unsigned short int* b_c
+r_c = <unsigned short int*>malloc(256 * 2)
+g_c = <unsigned short int*>malloc(256 * 2)
+b_c = <unsigned short int*>malloc(256 * 2)
+if (r_c is NULL) or (g_c is NULL) or (b_c is NULL):
+ raise MemoryError()
+
+
+
def vidmode_open(int use_screen):
'''
Start stage of colour curve control
@param use_screen The screen to use
- @return Zero on success
+ @return :bool Whether call was successful
'''
- return blueshift_vidmode_open(use_screen)
+ global vidmode_gamma_size
+ vidmode_gamma_size = blueshift_vidmode_open(use_screen)
+ return vidmode_gamma_size > 1
def vidmode_read(int use_crtc):
@@ -45,19 +65,14 @@ def vidmode_read(int use_crtc):
@param use_crtc The CRTC to use
@return :(r:list<int>, g:list<int>, b:list<int>) The current red, green and blue colour curves
'''
- cdef unsigned short int* got
- got = blueshift_vidmode_read(use_crtc)
- if got is NULL:
+ if not blueshift_vidmode_read(use_crtc, r_c, g_c, b_c) == 0:
raise Exception()
- r, g, b, i = [], [], [], 0
- for c in (r, g, b):
- s = got[i]
- i += 1
- for j in range(s):
- c.append(got[i + j])
- i += s
+ r, g, b = [], [], []
+ for i in range(vidmode_gamma_size):
+ r.append(r_c[i])
+ g.append(g_c[i])
+ b.append(b_c[i])
return (r, g, b)
-
def vidmode_apply(unsigned long long use_crtcs, r_curve, g_curve, b_curve):
@@ -70,28 +85,19 @@ def vidmode_apply(unsigned long long use_crtcs, r_curve, g_curve, b_curve):
@param b_curve:list<unsigned short int> The blue colour curve
@return Zero on success
'''
- cdef unsigned short int* r
- cdef unsigned short int* g
- cdef unsigned short int* b
- r = <unsigned short int*>malloc(256 * 2)
- g = <unsigned short int*>malloc(256 * 2)
- b = <unsigned short int*>malloc(256 * 2)
- if (r is NULL) or (g is NULL) or (b is NULL):
- raise MemoryError()
for i in range(256):
- r[i] = r_curve[i] & 0xFFFF
- g[i] = g_curve[i] & 0xFFFF
- b[i] = b_curve[i] & 0xFFFF
- rc = blueshift_vidmode_apply(use_crtcs, r, g, b)
- free(r)
- free(g)
- free(b)
- return rc
+ r_c[i] = r_curve[i] & 0xFFFF
+ g_c[i] = g_curve[i] & 0xFFFF
+ b_c[i] = b_curve[i] & 0xFFFF
+ return blueshift_vidmode_apply(use_crtcs, r_c, g_c, b_c)
def vidmode_close():
'''
Resource freeing stage of colour curve control
'''
+ free(r_c)
+ free(g_c)
+ free(b_c)
blueshift_vidmode_close()
diff --git a/src/blueshift_vidmode_c.c b/src/blueshift_vidmode_c.c
index fdc73ae..72c8053 100644
--- a/src/blueshift_vidmode_c.c
+++ b/src/blueshift_vidmode_c.c
@@ -44,14 +44,11 @@ static int curve_size;
* Start stage of colour curve control
*
* @param use_screen The screen to use
- * @return Zero on success
+ * @return Zero on error, otherwise the size of colours curves
*/
int blueshift_vidmode_open(int use_screen)
{
int _major, _minor;
- uint16_t* r_gamma;
- uint16_t* g_gamma;
- uint16_t* b_gamma;
/* Get X display */
@@ -59,7 +56,7 @@ int blueshift_vidmode_open(int use_screen)
if ((display = XOpenDisplay(NULL)) == NULL)
{
fprintf(stderr, "Cannot open X display\n");
- return 1;
+ return 0;
}
@@ -69,7 +66,7 @@ int blueshift_vidmode_open(int use_screen)
{
fprintf(stderr, "VidMode version query failed\n");
XCloseDisplay(display);
- return 1;
+ return 0;
}
@@ -80,37 +77,17 @@ int blueshift_vidmode_open(int use_screen)
{
fprintf(stderr, "VidMode gamma size query failed\n");
XCloseDisplay(display);
- return 1;
+ return 0;
}
- if (curve_size < 1)
+ if (curve_size <= 1)
{
- fprintf(stderr, "VidMode gamma size query failed\n");
+ fprintf(stderr, "VidMode gamma size query failed, impossible dimension\n");
XCloseDisplay(display);
- return 1;
+ return 0;
}
-
- /* Acquire curve control */
-
- r_gamma = malloc(3 * curve_size * sizeof(uint16_t));
- if (r_gamma == NULL)
- {
- fprintf(stderr, "Out of memory\n");
- return 1;
- }
- g_gamma = r_gamma + curve_size;
- b_gamma = g_gamma + curve_size;
- if (XF86VidModeGetGammaRamp(display, screen, curve_size, r_gamma, g_gamma, b_gamma) == 0)
- {
- fprintf(stderr, "VidMode gamma query failed\n");
- free(r_gamma);
- XCloseDisplay(display);
- return 1;
- }
- free(r_gamma);
-
- return 0;
+ return curve_size;
}
@@ -118,40 +95,25 @@ int blueshift_vidmode_open(int use_screen)
* Gets the current colour curves
*
* @param use_crtc The CRTC to use
- * @return {the size of the red curve, *the red curve,
- * the size of the green curve, *the green curve,
- * the size of the blue curve, *the blue curve},
- * needs to be free:d. `NULL` on error.
+ * @param r_gamma Storage location for the red colour curve
+ * @param g_gamma Storage location for the green colour curve
+ * @param b_gamma Storage location for the blue colour curve
+ * @return Zero on success
*/
-uint16_t* blueshift_vidmode_read(int use_crtc)
+int blueshift_vidmode_read(int use_crtc, uint16_t* r_gamma, uint16_t* g_gamma, uint16_t* b_gamma)
{
(void) use_crtc;
/* Read curves */
- uint16_t* r_gamma = malloc((3 + 3 * curve_size) * sizeof(uint16_t));
- uint16_t* g_gamma = r_gamma + curve_size + 1;
- uint16_t* b_gamma = g_gamma + curve_size + 1;
- if (r_gamma == NULL)
- {
- fprintf(stderr, "Out of memory\n");
- XCloseDisplay(display);
- return NULL;
- }
-
- *r_gamma++ = curve_size;
- *g_gamma++ = curve_size;
- *b_gamma++ = curve_size;
-
if (XF86VidModeGetGammaRamp(display, screen, curve_size, r_gamma, g_gamma, b_gamma) == 0)
{
fprintf(stderr, "VidMode gamma query failed\n");
- free(r_gamma);
XCloseDisplay(display);
- return NULL;
+ return 1;
}
- return r_gamma - 1;
+ return 0;
}
diff --git a/src/monitor.py b/src/monitor.py
index f375156..90ae603 100644
--- a/src/monitor.py
+++ b/src/monitor.py
@@ -122,7 +122,7 @@ def vidmode_get(crtc = 0, screen = 0):
if (vidmode_opened is None) or not (vidmode_opened == screen):
if vidmode_opened is not None:
vidmode_close()
- if vidmode_open(screen) == 0:
+ if vidmode_open(screen):
vidmode_opened = screen
else:
sys.exit(1)
@@ -186,7 +186,7 @@ def vidmode(*crtcs, screen = 0):
if (vidmode_opened is None) or not (vidmode_opened == screen):
if vidmode_opened is not None:
vidmode_close()
- if vidmode_open(screen) == 0:
+ if vidmode_open(screen):
vidmode_opened = screen
else:
sys.exit(1)