Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add RGBMatrix::DeleteFrameCanvas(FrameCanvas*) #1456

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3,375 changes: 2,314 additions & 1,061 deletions bindings/python/rgbmatrix/core.cpp

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions bindings/python/rgbmatrix/core.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ cimport cppinc
cdef class Canvas:
cdef cppinc.Canvas *__getCanvas(self) except +

cdef class FrameData:
cdef const char *__data
cdef size_t __length
cdef const char *__getData(self) except +

cdef class FrameCanvas(Canvas):
cdef cppinc.FrameCanvas *__canvas

Expand Down
36 changes: 36 additions & 0 deletions bindings/python/rgbmatrix/core.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@ cdef class Canvas:
b = (pixel >> 16) & 0xFF
my_canvas.SetPixel(xstart+col, ystart+row, r, g, b)

cdef class FrameData:
cdef const char *__getData(self) except +:
if <void*>self.__data != NULL:
return self.__data
raise Exception("FrameData ptr was destroyed or not initialized, you cannot use this object anymore")

cdef class FrameCanvas(Canvas):
def __dealloc__(self):
if <void*>self.__canvas != NULL:
Expand All @@ -72,6 +78,17 @@ cdef class FrameCanvas(Canvas):
def SetPixel(self, int x, int y, uint8_t red, uint8_t green, uint8_t blue):
(<cppinc.FrameCanvas*>self.__getCanvas()).SetPixel(x, y, red, green, blue)

def Serialize(self):
cdef const char* data
cdef size_t length
(<cppinc.FrameCanvas*>self.__getCanvas()).Serialize(&data, &length)
framedata = FrameData()
framedata.__length = length
framedata.__data = data
return framedata

def Deserialize(self, FrameData framedata):
return (<cppinc.FrameCanvas*>self.__getCanvas()).Deserialize(framedata.__getData(), framedata.__length)

property width:
def __get__(self): return (<cppinc.FrameCanvas*>self.__getCanvas()).width()
Expand Down Expand Up @@ -230,9 +247,28 @@ cdef class RGBMatrix(Canvas):
def Clear(self):
self.__matrix.Clear()

# Create a new buffer to be used for multi-buffering. The returned new
# Buffer implements a Canvas with the same size of thie RGBMatrix.
# You can use it to draw off-screen on it, then swap it with the active
# buffer using SwapOnVSync(). That would be classic double-buffering.

# You can also create as many FrameCanvas as you like and for instance use
# them to pre-fill scenes of an animation for fast playback later.

# The ownership of the created Canvases remains with the RGBMatrix. You
# you don't want to create more than needed as this will fill up your
# memory as they are only deleted when the RGBMatrix is deleted or by
# calling DeleteFrameCanvas().
def CreateFrameCanvas(self):
return __createFrameCanvas(self.__matrix.CreateFrameCanvas())

# Delete a FrameCanvas associated with the RGBMatrix
# Otherwise FrameCanvas objects created by CreateFrameCanvas() are never deleted
# until the RGBMatrix is deleted
def DeleteFrameCanvas(self, FrameCanvas frame):
self.__matrix.DeleteFrameCanvas(frame.__canvas)
del frame

# The optional "framerate_fraction" parameter allows to choose which
# multiple of the global frame-count to use. So it slows down your animation
# to an exact integer fraction of the refresh rate.
Expand Down
3 changes: 3 additions & 0 deletions bindings/python/rgbmatrix/cppinc.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,15 @@ cdef extern from "led-matrix.h" namespace "rgb_matrix":
uint8_t brightness()
FrameCanvas *CreateFrameCanvas()
FrameCanvas *SwapOnVSync(FrameCanvas*, uint8_t)
void DeleteFrameCanvas(FrameCanvas*)

cdef cppclass FrameCanvas(Canvas):
bool SetPWMBits(uint8_t)
uint8_t pwmbits()
void SetBrightness(uint8_t)
uint8_t brightness()
void Serialize(const char **data, size_t *len)
bool Deserialize(const char *data, size_t len)

struct RuntimeOptions:
RuntimeOptions() except +
Expand Down
50 changes: 42 additions & 8 deletions bindings/python/rgbmatrix/graphics.cpp

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 9 additions & 4 deletions include/led-matrix.h
Original file line number Diff line number Diff line change
Expand Up @@ -209,12 +209,17 @@ class RGBMatrix : public Canvas {
// You can also create as many FrameCanvas as you like and for instance use
// them to pre-fill scenes of an animation for fast playback later.
//
// The ownership of the created Canvases remains with the RGBMatrix, so you
// don't have to worry about deleting them (but you also don't want to create
// more than needed as this will fill up your memory as they are only deleted
// when the RGBMatrix is deleted).
// The ownership of the created Canvases remains with the RGBMatrix. You
// you don't want to create more than needed as this will fill up your
// memory as they are only deleted when the RGBMatrix is deleted or by
// calling DeleteFrameCanvas().
FrameCanvas *CreateFrameCanvas();

// Delete a FrameCanvas associated with the RGBMatrix
// Otherwise FrameCanvas objects created by CreateFrameCanvas() are never deleted
// until the RGBMatrix is deleted
void DeleteFrameCanvas(FrameCanvas* frame);

// This method waits to the next VSync and swaps the active buffer with the
// supplied buffer. The formerly active buffer is returned.
//
Expand Down
23 changes: 22 additions & 1 deletion lib/led-matrix.cc
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ class RGBMatrix::Impl {
bool StartRefresh();

FrameCanvas *CreateFrameCanvas();
void DeleteFrameCanvas(FrameCanvas *frame);
FrameCanvas *SwapOnVSync(FrameCanvas *other, unsigned framerate_fraction);
bool ApplyPixelMapper(const PixelMapper *mapper);

Expand Down Expand Up @@ -504,6 +505,21 @@ FrameCanvas *RGBMatrix::Impl::CreateFrameCanvas() {
return result;
}

void RGBMatrix::Impl::DeleteFrameCanvas(FrameCanvas* frame) {
if (frame == nullptr){
return;
}

for(auto it = created_frames_.begin(); it != created_frames_.end(); ++it){
if (*it == frame){
created_frames_.erase(it);
}

}
delete frame;
frame = nullptr;
}

FrameCanvas *RGBMatrix::Impl::SwapOnVSync(FrameCanvas *other,
unsigned frame_fraction) {
if (frame_fraction == 0) frame_fraction = 1; // correct user error.
Expand Down Expand Up @@ -546,7 +562,6 @@ void RGBMatrix::Impl::SetBrightness(uint8_t brightness) {
uint8_t RGBMatrix::Impl::brightness() {
return params_.brightness;
}

bool RGBMatrix::Impl::ApplyPixelMapper(const PixelMapper *mapper) {
if (mapper == NULL) return true;
using internal::PixelDesignatorMap;
Expand Down Expand Up @@ -672,10 +687,16 @@ RGBMatrix *RGBMatrix::CreateFromFlags(int *argc, char ***argv,
FrameCanvas *RGBMatrix::CreateFrameCanvas() {
return impl_->CreateFrameCanvas();
}

void RGBMatrix::DeleteFrameCanvas(FrameCanvas *frame){
impl_->DeleteFrameCanvas(frame);
}

FrameCanvas *RGBMatrix::SwapOnVSync(FrameCanvas *other,
unsigned framerate_fraction) {
return impl_->SwapOnVSync(other, framerate_fraction);
}

bool RGBMatrix::ApplyPixelMapper(const PixelMapper *mapper) {
return impl_->ApplyPixelMapper(mapper);
}
Expand Down