Mercurial > hg > touchkeys
changeset 27:eef567a60146
Changed how OpenGL updates are handled so the canvas is only re-rendered when something changes. This should reduce CPU usage in certain circumstances.
author | Andrew McPherson <andrewm@eecs.qmul.ac.uk> |
---|---|
date | Sun, 02 Mar 2014 19:25:50 +0000 |
parents | 90069a503079 |
children | cfbcd31a54e7 |
files | Source/Display/KeyPositionGraphDisplay.cpp Source/Display/KeyPositionGraphDisplay.h Source/Display/KeyboardDisplay.cpp Source/Display/KeyboardDisplay.h Source/Display/OpenGLDisplayBase.h Source/Display/OpenGLJuceCanvas.h Source/Display/RawSensorDisplay.cpp Source/Display/RawSensorDisplay.h Source/MainApplicationController.cpp |
diffstat | 9 files changed, 112 insertions(+), 97 deletions(-) [+] |
line wrap: on
line diff
--- a/Source/Display/KeyPositionGraphDisplay.cpp Sat Feb 22 21:59:05 2014 +0000 +++ b/Source/Display/KeyPositionGraphDisplay.cpp Sun Mar 02 19:25:50 2014 +0000 @@ -23,6 +23,7 @@ */ #include "KeyPositionGraphDisplay.h" +#include "OpenGLJuceCanvas.h" #include <iostream> #include <cmath> @@ -36,9 +37,9 @@ const float KeyPositionGraphDisplay::kDisplayGraphWidth = 20.0; const float KeyPositionGraphDisplay::kDisplayGraphHeight = 10.0; -KeyPositionGraphDisplay::KeyPositionGraphDisplay() : +KeyPositionGraphDisplay::KeyPositionGraphDisplay() : canvas_(0), displayPixelWidth_(1.0), displayPixelHeight_(1.0), totalDisplayWidth_(1.0), totalDisplayHeight_(1.0), -xMin_(0.0), xMax_(1.0), yMin_(-0.2), yMax_(1.2), needsUpdate_(true) +xMin_(0.0), xMax_(1.0), yMin_(-0.2), yMax_(1.2) { // Initialize OpenGL settings: 2D only @@ -52,6 +53,12 @@ totalDisplayHeight_ = kDisplayTopMargin + kDisplayBottomMargin + kDisplayGraphHeight; } +// Tell the underlying canvas to repaint itself +void KeyPositionGraphDisplay::tellCanvasToRepaint() { + if(canvas_ != 0) + canvas_->triggerRepaint(); +} + void KeyPositionGraphDisplay::setDisplaySize(float width, float height) { ScopedLock sl(displayMutex_); displayPixelWidth_ = width; @@ -149,8 +156,6 @@ glVertex2f(graphToDisplayX(releaseFinishTimestamp_), kDisplayGraphHeight); glEnd(); } - - needsUpdate_ = false; glFlush(); } @@ -160,29 +165,21 @@ void KeyPositionGraphDisplay::mouseDown(float x, float y) { //Point mousePoint = {x, y}; //Point scaledPoint = screenToInternal(mousePoint); - - //needsUpdate_ = true; } void KeyPositionGraphDisplay::mouseDragged(float x, float y) { //Point mousePoint = {x, y}; //Point scaledPoint = screenToInternal(mousePoint); - - //needsUpdate_ = true; } void KeyPositionGraphDisplay::mouseUp(float x, float y) { //Point mousePoint = {x, y}; //Point scaledPoint = screenToInternal(mousePoint); - - //needsUpdate_ = true; } void KeyPositionGraphDisplay::rightMouseDown(float x, float y) { //Point mousePoint = {x, y}; //Point scaledPoint = screenToInternal(mousePoint); - - //needsUpdate_ = true; } void KeyPositionGraphDisplay::rightMouseDragged(float x, float y) { @@ -232,7 +229,7 @@ xMax_ = keyTimestamps_.back(); yMin_ = -0.2; yMax_ = 1.2; - needsUpdate_ = true; + tellCanvasToRepaint(); } // Conversion from internal coordinate space to external pixel values and back
--- a/Source/Display/KeyPositionGraphDisplay.h Sat Feb 22 21:59:05 2014 +0000 +++ b/Source/Display/KeyPositionGraphDisplay.h Sun Mar 02 19:25:50 2014 +0000 @@ -57,11 +57,14 @@ public: KeyPositionGraphDisplay(); + // Set canvas for triggering rendering; + void setCanvas(OpenGLJuceCanvas *canvas) { canvas_ = canvas; } + void tellCanvasToRepaint(); + // Setup methods for display size and keyboard range void setDisplaySize(float width, float height); // Drawing methods - bool needsRender() { return needsUpdate_; } void render(); // Interaction methods @@ -78,22 +81,22 @@ void setKeyPressStart(key_position position, timestamp_type timestamp) { pressStartTimestamp_ = timestamp; pressStartPosition_ = position; - needsUpdate_ = true; + tellCanvasToRepaint(); } void setKeyPressFinish(key_position position, timestamp_type timestamp) { pressFinishTimestamp_ = timestamp; pressFinishPosition_ = position; - needsUpdate_ = true; + tellCanvasToRepaint(); } void setKeyReleaseStart(key_position position, timestamp_type timestamp) { releaseStartTimestamp_ = timestamp; releaseStartPosition_ = position; - needsUpdate_ = true; + tellCanvasToRepaint(); } void setKeyReleaseFinish(key_position position, timestamp_type timestamp) { releaseFinishTimestamp_ = timestamp; releaseFinishPosition_ = position; - needsUpdate_ = true; + tellCanvasToRepaint(); } private: @@ -107,13 +110,13 @@ Point screenToInternal(Point& inPoint); Point internalToScreen(Point& inPoint); +private: + OpenGLJuceCanvas *canvas_; // Reference to canvas that renders OpenGL -private: float displayPixelWidth_, displayPixelHeight_; // Pixel resolution of the surrounding window float totalDisplayWidth_, totalDisplayHeight_; // Size of the internal view (centered around origin) float xMin_, xMax_, yMin_, yMax_; // Coordinates for the graph axes - bool needsUpdate_; // Whether the keyboard should be redrawn CriticalSection displayMutex_; // Synchronize access between data and display threads std::vector<key_position> keyPositions_; // Positions (0-1 normalized) of the key
--- a/Source/Display/KeyboardDisplay.cpp Sat Feb 22 21:59:05 2014 +0000 +++ b/Source/Display/KeyboardDisplay.cpp Sun Mar 02 19:25:50 2014 +0000 @@ -22,6 +22,7 @@ */ #include "KeyboardDisplay.h" +#include "OpenGLJuceCanvas.h" #include <iostream> #include <cmath> @@ -62,9 +63,9 @@ const float KeyboardDisplay::kDisplayMinTouchSize = 0.1; const float KeyboardDisplay::kDisplayTouchSizeScaler = 0.5; -KeyboardDisplay::KeyboardDisplay() : lowestMidiNote_(0), highestMidiNote_(0), +KeyboardDisplay::KeyboardDisplay() : canvas_(0), lowestMidiNote_(0), highestMidiNote_(0), totalDisplayWidth_(1.0), totalDisplayHeight_(1.0), displayPixelWidth_(1.0), displayPixelHeight_(1.0), -needsUpdate_(true), currentHighlightedKey_(-1), touchSensingEnabled_(false), analogSensorsPresent_(false) { +currentHighlightedKey_(-1), touchSensingEnabled_(false), analogSensorsPresent_(false) { // Initialize OpenGL settings: 2D only //glMatrixMode(GL_PROJECTION); @@ -75,6 +76,12 @@ midiActiveForKey_[i] = false; } +// Tell the underlying canvas to repaint itself +void KeyboardDisplay::tellCanvasToRepaint() { + if(canvas_ != 0) + canvas_->triggerRepaint(); +} + void KeyboardDisplay::setKeyboardRange(int lowest, int highest) { if(lowest < 0 || highest < 0) return; @@ -228,8 +235,6 @@ glTranslatef(-offsetH, -offsetV, 0.0); } } - - needsUpdate_ = false; glFlush(); } @@ -241,7 +246,7 @@ Point scaledPoint = screenToInternal(mousePoint); currentHighlightedKey_ = keyForLocation(scaledPoint); - needsUpdate_ = true; + tellCanvasToRepaint(); } void KeyboardDisplay::mouseDragged(float x, float y) { @@ -249,7 +254,7 @@ Point scaledPoint = screenToInternal(mousePoint); currentHighlightedKey_ = keyForLocation(scaledPoint); - needsUpdate_ = true; + tellCanvasToRepaint(); } void KeyboardDisplay::mouseUp(float x, float y) { @@ -263,7 +268,7 @@ keyClicked(currentHighlightedKey_); currentHighlightedKey_ = -1; - needsUpdate_ = true; + tellCanvasToRepaint(); } void KeyboardDisplay::rightMouseDown(float x, float y) { @@ -274,7 +279,7 @@ if(key != -1) keyRightClicked(key); - needsUpdate_ = true; + tellCanvasToRepaint(); } void KeyboardDisplay::rightMouseDragged(float x, float y) { @@ -315,7 +320,7 @@ currentTouches_[key].size2 = touch.sizes[1]; currentTouches_[key].size3 = touch.sizes[2]; - needsUpdate_ = true; + tellCanvasToRepaint(); } // Clear touch information for this key @@ -326,7 +331,7 @@ //currentTouches_.erase(key); currentTouches_[key].active = 0; - needsUpdate_ = true; + tellCanvasToRepaint(); } // Clear all current touch information @@ -338,7 +343,7 @@ for(int i = 0; i < 128; i++) currentTouches_[i].active = false; - needsUpdate_ = true; + tellCanvasToRepaint(); } // Indicate whether the given key is calibrated or not @@ -347,7 +352,7 @@ if(key < 0 || key > 127) return; analogValueIsCalibratedForKey_[key] = isCalibrated; - needsUpdate_ = true; + tellCanvasToRepaint(); } // Set the current value of the analog sensor for the given key. @@ -359,7 +364,7 @@ if(key < 0 || key > 127) return; analogValueForKey_[key] = value; - needsUpdate_ = true; + tellCanvasToRepaint(); } // Clear all the analog data for all keys @@ -367,20 +372,20 @@ for(int key = 0; key < 128; key++) { analogValueForKey_[key] = 0.0; } - needsUpdate_ = true; + tellCanvasToRepaint(); } void KeyboardDisplay::setMidiActive(int key, bool active) { if(key < 0 || key > 127) return; midiActiveForKey_[key] = active; - needsUpdate_ = true; + tellCanvasToRepaint(); } void KeyboardDisplay::clearMidiData() { for(int key = 0; key < 128; key++) midiActiveForKey_[key] = false; - needsUpdate_ = true; + tellCanvasToRepaint(); } // Indicate whether a given key has touch sensing capability @@ -619,7 +624,7 @@ // in, otherwise return -1 if no key matches. int KeyboardDisplay::keyForLocation(Point& internalPoint) { - std::cout << "(" << internalPoint.x << "," << internalPoint.y << ")\n"; + // std::cout << "(" << internalPoint.x << "," << internalPoint.y << ")\n"; // First, check that the point is within the overall bounding box of the keyboard if(internalPoint.y < -totalDisplayHeight_*0.5 + kDisplayBottomMargin || @@ -652,7 +657,7 @@ // Check if we're on the front area of the white keys, and if so, ignore points located in the gaps // between the keys - std::cout << "norm " << (-internalPoint.y + totalDisplayHeight_*0.5) << std::endl; + // std::cout << "norm " << (-internalPoint.y + totalDisplayHeight_*0.5) << std::endl; if(-internalPoint.y + totalDisplayHeight_*0.5 - kDisplayBottomMargin <= kWhiteKeyFrontLength) { if(normalizedHLoc - floorf(normalizedHLoc) > kWhiteKeyFrontWidth / (kWhiteKeyFrontWidth + kInterKeySpacing))
--- a/Source/Display/KeyboardDisplay.h Sat Feb 22 21:59:05 2014 +0000 +++ b/Source/Display/KeyboardDisplay.h Sun Mar 02 19:25:50 2014 +0000 @@ -95,6 +95,10 @@ public: KeyboardDisplay(); virtual ~KeyboardDisplay() {} + + // Set canvas for triggering rendering; + void setCanvas(OpenGLJuceCanvas *canvas) { canvas_ = canvas; } + void tellCanvasToRepaint(); // Setup methods for display size and keyboard range void setKeyboardRange(int lowest, int highest); @@ -102,7 +106,6 @@ virtual void setDisplaySize(float width, float height); // Drawing methods - virtual bool needsRender() { return needsUpdate_; } virtual void render(); // Interaction methods @@ -159,11 +162,11 @@ int keyForLocation(Point& internalPoint); protected: - + OpenGLJuceCanvas *canvas_; // Reference to object which handles rendering + int lowestMidiNote_, highestMidiNote_; // Which keys should be displayed (use MIDI note numbers) float totalDisplayWidth_, totalDisplayHeight_; // Size of the internal view (centered around origin) float displayPixelWidth_, displayPixelHeight_; // Pixel resolution of the surrounding window - bool needsUpdate_; // Whether the keyboard should be redrawn int currentHighlightedKey_; // What key is being clicked on at the moment bool touchSensingEnabled_; // Whether touch-sensitive keys are being used bool touchSensingPresentOnKey_[128]; // Whether the key with this MIDI note has a touch sensor
--- a/Source/Display/OpenGLDisplayBase.h Sat Feb 22 21:59:05 2014 +0000 +++ b/Source/Display/OpenGLDisplayBase.h Sun Mar 02 19:25:50 2014 +0000 @@ -24,6 +24,8 @@ #ifndef touchkeys_OpenGLDisplayBase_h #define touchkeys_OpenGLDisplayBase_h +class OpenGLJuceCanvas; + // Virtual base class that implements some basic methods that the OS-specific // GUI can attach to. Specific displays are subclasses of this @@ -32,12 +34,14 @@ OpenGLDisplayBase() {} virtual ~OpenGLDisplayBase() {} + + // Canvas reference method + virtual void setCanvas(OpenGLJuceCanvas *canvas) = 0; // Setup method for display size virtual void setDisplaySize(float width, float height) = 0; // Drawing methods - virtual bool needsRender() = 0; virtual void render() = 0; // Interaction methods
--- a/Source/Display/OpenGLJuceCanvas.h Sat Feb 22 21:59:05 2014 +0000 +++ b/Source/Display/OpenGLJuceCanvas.h Sun Mar 02 19:25:50 2014 +0000 @@ -38,9 +38,10 @@ // *** Constructor / Destructor *** OpenGLJuceCanvas(OpenGLDisplayBase& display) : display_(display) { openGLContext_.setRenderer (this); - openGLContext_.setComponentPaintingEnabled (true); - openGLContext_.setContinuousRepainting (true); + openGLContext_.setComponentPaintingEnabled (false); + openGLContext_.setContinuousRepainting (false); openGLContext_.attachTo (*this); + display_.setCanvas(this); } ~OpenGLJuceCanvas() { openGLContext_.detach(); @@ -50,24 +51,21 @@ void newOpenGLContextCreated() {} void openGLContextClosing() {} - void mouseDown (const MouseEvent& e) - { + void mouseDown (const MouseEvent& e) { if(e.mods.isLeftButtonDown()) display_.mouseDown(e.x, e.y); else if(e.mods.isRightButtonDown()) display_.rightMouseDown(e.x, e.y); } - void mouseDrag (const MouseEvent& e) - { + void mouseDrag (const MouseEvent& e) { if(e.mods.isLeftButtonDown()) display_.mouseDragged(e.x, e.y); else if(e.mods.isRightButtonDown()) display_.rightMouseDragged(e.x, e.y); } - void mouseUp (const MouseEvent& e) - { + void mouseUp (const MouseEvent& e) { if(e.mods.isLeftButtonDown()) display_.mouseUp(e.x, e.y); else if(e.mods.isRightButtonDown()) @@ -81,11 +79,15 @@ void paint (Graphics&) {} - void renderOpenGL() - { + void renderOpenGL() { display_.render(); } + // Method to tell the OpenGLContext to repaint + void triggerRepaint() { + openGLContext_.triggerRepaint(); + } + private: OpenGLDisplayBase& display_; // Reference to the TouchKeys-specific OpenGL Display OpenGLContext openGLContext_;
--- a/Source/Display/RawSensorDisplay.cpp Sat Feb 22 21:59:05 2014 +0000 +++ b/Source/Display/RawSensorDisplay.cpp Sun Mar 02 19:25:50 2014 +0000 @@ -21,6 +21,7 @@ */ #include "RawSensorDisplay.h" +#include "OpenGLJuceCanvas.h" #include <iostream> #include <cmath> @@ -36,9 +37,9 @@ const float RawSensorDisplay::kDisplayBarHeight = 10.0; -RawSensorDisplay::RawSensorDisplay() : +RawSensorDisplay::RawSensorDisplay() : canvas_(0), displayPixelWidth_(1.0), displayPixelHeight_(1.0), totalDisplayWidth_(1.0), totalDisplayHeight_(1.0), -yMin_(-10), yMax_(256), needsUpdate_(true) { +yMin_(-10), yMax_(256) { // Initialize OpenGL settings: 2D only //glMatrixMode(GL_PROJECTION); @@ -48,6 +49,12 @@ totalDisplayHeight_ = kDisplayTopMargin + kDisplayBottomMargin + kDisplayBarHeight; } +// Tell the underlying canvas to repaint itself +void RawSensorDisplay::tellCanvasToRepaint() { + if(canvas_ != 0) + canvas_->triggerRepaint(); +} + void RawSensorDisplay::setDisplaySize(float width, float height) { ScopedLock sl(displayMutex_); @@ -97,8 +104,6 @@ glTranslatef(kDisplayBarWidth + kDisplayBarSpacing, 0.0, 0.0); } - - needsUpdate_ = false; glFlush(); } @@ -110,7 +115,7 @@ // Update display width according to number of data points totalDisplayWidth_ = kDisplaySideMargin*2 + (kDisplayBarWidth + kDisplayBarSpacing) * displayValues_.size(); - needsUpdate_ = true; + tellCanvasToRepaint(); } // Mouse interaction methods @@ -118,29 +123,21 @@ void RawSensorDisplay::mouseDown(float x, float y) { //Point mousePoint = {x, y}; //Point scaledPoint = screenToInternal(mousePoint); - - //needsUpdate_ = true; } void RawSensorDisplay::mouseDragged(float x, float y) { //Point mousePoint = {x, y}; //Point scaledPoint = screenToInternal(mousePoint); - - //needsUpdate_ = true; } void RawSensorDisplay::mouseUp(float x, float y) { //Point mousePoint = {x, y}; //Point scaledPoint = screenToInternal(mousePoint); - - //needsUpdate_ = true; } void RawSensorDisplay::rightMouseDown(float x, float y) { //Point mousePoint = {x, y}; //Point scaledPoint = screenToInternal(mousePoint); - - //needsUpdate_ = true; } void RawSensorDisplay::rightMouseDragged(float x, float y) {
--- a/Source/Display/RawSensorDisplay.h Sat Feb 22 21:59:05 2014 +0000 +++ b/Source/Display/RawSensorDisplay.h Sun Mar 02 19:25:50 2014 +0000 @@ -56,12 +56,15 @@ public: RawSensorDisplay(); + // Set canvas for triggering rendering; + void setCanvas(OpenGLJuceCanvas *canvas) { canvas_ = canvas; } + void tellCanvasToRepaint(); + // Setup methods for display size and keyboard range void setDisplaySize(float width, float height); float aspectRatio() { return totalDisplayWidth_ / totalDisplayHeight_; } // Drawing methods - bool needsRender() { return needsUpdate_; } void render(); // Interaction methods @@ -88,11 +91,12 @@ private: + OpenGLJuceCanvas *canvas_; // Reference to object which handles rendering + float displayPixelWidth_, displayPixelHeight_; // Pixel resolution of the surrounding window float totalDisplayWidth_, totalDisplayHeight_; // Size of the internal view (centered around origin) float yMin_, yMax_; // Range of the graph axes - bool needsUpdate_; // Whether the keyboard should be redrawn CriticalSection displayMutex_; // Synchronize access between data and display threads std::vector<int> displayValues_; // Values to display as a bar graph };
--- a/Source/MainApplicationController.cpp Sat Feb 22 21:59:05 2014 +0000 +++ b/Source/MainApplicationController.cpp Sun Mar 02 19:25:50 2014 +0000 @@ -156,36 +156,36 @@ std::vector<std::string> MainApplicationController::availableTouchkeyDevices() { std::vector<std::string> devices; -#ifdef _MSC_VER - for(int i = 1; i <= 128; i++) { - String comPortName("COM"); - comPortName += i; - - DWORD dwSize = 0; - LPCOMMCONFIG lpCC = (LPCOMMCONFIG) new BYTE[1]; - BOOL ret = GetDefaultCommConfig(comPortName.toUTF8(), lpCC, &dwSize); - delete [] lpCC; - - if(ret) - devices.push_back(comPortName.toStdString()); - else { - if(GetLastError() == ERROR_INSUFFICIENT_BUFFER) { - //Logger::writeToLog(String::formatted("Found " + comPortName)); - lpCC = (LPCOMMCONFIG) new BYTE[dwSize]; - ret = GetDefaultCommConfig(comPortName.toUTF8(), lpCC, &dwSize); - if(ret) - devices.push_back(comPortName.toStdString()); - else { - int error = GetLastError(); - //Logger::writeToLog(String("2Didn't find " + comPortName + "; error " + String(error))); - } - delete [] lpCC; - } - else { - int error = GetLastError(); - //Logger::writeToLog(String("Didn't find " + comPortName + "; error " + String(error))); - } - } +#ifdef _MSC_VER + for(int i = 1; i <= 128; i++) { + String comPortName("COM"); + comPortName += i; + + DWORD dwSize = 0; + LPCOMMCONFIG lpCC = (LPCOMMCONFIG) new BYTE[1]; + BOOL ret = GetDefaultCommConfig(comPortName.toUTF8(), lpCC, &dwSize); + delete [] lpCC; + + if(ret) + devices.push_back(comPortName.toStdString()); + else { + if(GetLastError() == ERROR_INSUFFICIENT_BUFFER) { + //Logger::writeToLog(String::formatted("Found " + comPortName)); + lpCC = (LPCOMMCONFIG) new BYTE[dwSize]; + ret = GetDefaultCommConfig(comPortName.toUTF8(), lpCC, &dwSize); + if(ret) + devices.push_back(comPortName.toStdString()); + else { + int error = GetLastError(); + //Logger::writeToLog(String("2Didn't find " + comPortName + "; error " + String(error))); + } + delete [] lpCC; + } + else { + int error = GetLastError(); + //Logger::writeToLog(String("Didn't find " + comPortName + "; error " + String(error))); + } + } } #else if(SystemStats::getOperatingSystemType() == SystemStats::Linux) {