diff --git a/DisplayManX.lua b/DisplayManX.lua index 4ba6f4c..8c650e9 100644 --- a/DisplayManX.lua +++ b/DisplayManX.lua @@ -350,7 +350,7 @@ DMXDisplay_mt = { Snapshot = function(self, resource, transform) transform = transform or ffi.C.VC_IMAGE_ROT0; - return DisplayManX.snapshot(self.Handle, resource, transform); + return DisplayManX.snapshot(self.Handle, resource.Handle, transform); end, }, } @@ -367,15 +367,7 @@ DMXElement = ffi.typeof("struct DMXElement"); DMXElement_mt = { __gc = function(self) print("GC: DMXElement"); - if self.Handle == DISPMANX_NO_HANDLE then - return true - end - - local update = DMXUpdate(10); - if update then - DisplayManX.element_remove(update.Handle, self.Handle); - return update:SubmitSync(); - end + self:Free(); end, __new = function(ct, handle) @@ -386,6 +378,19 @@ DMXElement_mt = { end, __index = { + Free = function(self) + + if self.Handle == DISPMANX_NO_HANDLE then + return true + end + + local update = DMXUpdate(10); + if update then + DisplayManX.element_remove(update.Handle, self.Handle); + update:SubmitSync(); + self.Handle = DISPMANX_NO_HANDLE; + end + end, }, } ffi.metatype(DMXElement, DMXElement_mt); @@ -400,6 +405,7 @@ DMXResource_mt = { end, __new = function(ct, width, height, imgtype) + imgtype = imgtype or ffi.C.VC_IMAGE_RGB565; local handle, imgptr = DisplayManX.resource_create(imgtype, width, height); if not handle then return nil, imgptr @@ -509,18 +515,19 @@ local DMXView_mt = { __index = DMXView, } -DMXView.new = function(display, x, y, width, height, pformat, layer) +DMXView.new = function(display, x, y, width, height, layer, pformat, resource) x = x or 0 y = y or 0 layer = layer or 0 pformat = pformat or ffi.C.VC_IMAGE_RGB565 - local resource = DMXResource(width, height, pformat); + resource = resource or DMXResource(width, height, pformat); + local obj = { X = x; Y = y; Width = width; Height = height; - Resource = DMXResource(width, height, pformat); + Resource = resource; Layer = layer; Display = display; } @@ -535,6 +542,16 @@ DMXView.CopyPixelBuffer = function(self, pbuff, x, y, width, height) self.Resource:CopyPixelBuffer(pbuff, x, y, width, height) end +DMXView.Hide = function(self) + if (self.Surface) then + self.Surface:Free(); + end + + self.Surface = nil; + + return true; +end + DMXView.Show = function(self) local dst_rect = VC_RECT_T(self.X, self.Y, self.Width, self.Height); local src_rect = VC_RECT_T( 0, 0, lshift(self.Width, 16), lshift(self.Height, 16) ); diff --git a/PixelBuffer.lua b/PixelBuffer.lua new file mode 100644 index 0000000..f266c32 --- /dev/null +++ b/PixelBuffer.lua @@ -0,0 +1,38 @@ + +local ffi = require "ffi" +local bit = require "bit" +local bnot = bit.bnot +local band = bit.band +local bor = bit.bor +local rshift = bit.rshift +local lshift = bit.lshift + +local DMX = require "DisplayManX" + +local ALIGN_UP = function(x,y) + return band((x + y-1), bnot(y-1)) +end + +local PixelBuffer = {} +local PixelBuffer_mt = { + __index = PixelBuffer +} + +PixelBuffer.new = function(width, height, imgtype) + imgtype = imgtype or ffi.C.VC_IMAGE_RGB565 + + local pitch = ALIGN_UP(width*sizeofpixel, 32); + local aligned_height = ALIGN_UP(height, 16); + + local obj = { + PixelFormat = imgtype; + Width = width; + Height = height; + Pitch = pitch; + Data = ffi.new("uint8_t[?]", pitch * height); + } + + setmetatable(obj, PixelBuffer_mt); + + return obj; +end diff --git a/test_dispmanx.lua b/test_dispmanx.lua index 3250a31..4c62801 100644 --- a/test_dispmanx.lua +++ b/test_dispmanx.lua @@ -17,7 +17,6 @@ local DMX = require "DisplayManX" -- It will fill in a rectangle, and that's it. function FillRect( pbuff, x, y, w, h, val) local dataPtr = ffi.cast("uint8_t *", pbuff.Data); - --local pitch = pbuff.Pitch; local row; local col; @@ -29,12 +28,13 @@ function FillRect( pbuff, x, y, w, h, val) end end +-- Setup the display width = 400 height = 200 -- Get a connection to the display local Display = DMXDisplay(); -Display:SetBackground(5, 65, 65); +Display:SetBackground(125, 65, 65); local info = Display:GetInfo(); diff --git a/test_snapshot.lua b/test_snapshot.lua new file mode 100644 index 0000000..c2bea83 --- /dev/null +++ b/test_snapshot.lua @@ -0,0 +1,27 @@ + +local ffi = require "ffi" +local DMX = require "DisplayManX" + +local Display = DMXDisplay(); + +local width = 640; +local height = 480; +local layer = 0; -- keep the snapshot view on top + +-- Create a resource to copy image into +local pixmap = DMX.DMXResource(width,height); + +-- create a view with the snapshot as +-- the backing store +local mainView = DMX.DMXView.new(Display, 200, 200, width, height, layer, pformat, pixmap); + +for i=1,20 do + -- Do the snapshot + mainView:Hide(); + Display:Snapshot(pixmap); + mainView:Show(); + + ffi.C.sleep(1); + + +end \ No newline at end of file diff --git a/tests/test_upvalue.lua b/tests/test_upvalue.lua new file mode 100644 index 0000000..d0de273 --- /dev/null +++ b/tests/test_upvalue.lua @@ -0,0 +1,15 @@ +local upvalue=nil + +function test() + local last + for j=1,20 do + last = upvalue + end + print(last, upvalue) +end + +for i=1,8 do + upvalue = i + test() +end +