Skip to content

Commit

Permalink
Better memory management of gamma ramps
Browse files Browse the repository at this point in the history
  • Loading branch information
icy-arctic-fox committed Mar 2, 2022
1 parent 906f544 commit 38564da
Showing 1 changed file with 48 additions and 24 deletions.
72 changes: 48 additions & 24 deletions src/espresso/monitor/gamma_ramp.cr
Original file line number Diff line number Diff line change
@@ -1,46 +1,70 @@
module Espresso
# Gamma ramp of a monitor.
class GammaRamp
# Array of values describing the response of the red channel.
getter red : Slice(UInt16)

# Array of values describing the response of the green channel.
getter green : Slice(UInt16)
struct GammaRamp
# Number of elements in each channel's array.
getter size : Int32

# Array of values describing the response of the blue channel.
getter blue : Slice(UInt16)
@ramp : Pointer(UInt16)

# Creates a new gamma ramp with all values set to zero.
def initialize(size : Int32 = 256)
@red = Slice.new(size, 0_u16)
@green = Slice.new(size, 0_u16)
@blue = Slice.new(size, 0_u16)
def initialize(@size : Int32 = 256)
@ramp = Pointer(UInt16).malloc(@size * 3)
end

# Creates a new gamma ramp from a pointer to one from GLFW.
# Contents of the ramp are copied so that they are available
# even after the monitor is disconnected or GLFW is terminated.
protected def initialize(pointer : LibGLFW::GammaRamp*)
ramp = pointer.value
size = ramp.size.to_i
source = pointer.value
@size = source.size.to_i

# Copy contents of arrays since they may be invalidated when the monitor disconnects.
@red = Slice(UInt16).new(size).tap &.copy_from(ramp.red, size)
@green = Slice(UInt16).new(size).tap &.copy_from(ramp.green, size)
@blue = Slice(UInt16).new(size).tap &.copy_from(ramp.blue, size)
@ramp = Pointer(UInt16).malloc(@size * 3)
red_pointer.copy_from(source.red, @size)
green_pointer.copy_from(source.green, @size)
blue_pointer.copy_from(source.blue, @size)
end

# Number of elements in each channel's array.
def size : Int
@red.size
# Array of values describing the response of the red channel.
def red : Slice(UInt16)
red_pointer.to_slice(@size)
end

# Pointer to the start of the red channel data.
@[AlwaysInline]
private def red_pointer : Pointer(UInt16)
@ramp
end

# Array of values describing the response of the green channel.
def green : Slice(UInt16)
green_pointer.to_slice(@size)
end

# Pointer to the start of the green channel data.
@[AlwaysInline]
private def green_pointer : Pointer(UInt16)
@ramp + @size
end

# Array of values describing the response of the blue channel.
def blue : Slice(UInt16)
blue_pointer.to_slice(@size)
end

# Pointer to the start of the blue channel data.
@[AlwaysInline]
private def blue_pointer : Pointer(UInt16)
@ramp + @size + @size
end

# Converts to a GLFW compatible struct.
def to_unsafe
ramp = LibGLFW::GammaRamp.new
ramp.red = @red
ramp.green = @green
ramp.blue = @blue
ramp.size = size
ramp.red = red_pointer
ramp.green = green_pointer
ramp.blue = blue_pointer
ramp.size = @size
ramp
end
end
Expand Down

0 comments on commit 38564da

Please sign in to comment.