Class: Heapviz::Renderer

Inherits:
Object
  • Object
show all
Defined in:
lib/heapviz/renderer.rb

Constant Summary collapse

SLOT_BASE_SIZE =
4
COLORS =
{
  40 => ChunkyPNG::Color.rgba(104, 195, 163, 255),
  80 => ChunkyPNG::Color.rgba(27, 163, 156, 255),
  160 => ChunkyPNG::Color.rgba(200, 247, 197, 255),
  320 => ChunkyPNG::Color.rgba(22, 160, 133, 255),
  640 => ChunkyPNG::Color.rgba(22, 160, 255, 255),
}
WHITE =
ChunkyPNG::Color.rgba(255, 255, 255, 255)
BLACK =
ChunkyPNG::Color.rgba(0, 0, 0, 255)

Instance Method Summary collapse

Constructor Details

#initialize(op_path, heap, logger = nil) ⇒ Renderer

Returns a new instance of Renderer.



17
18
19
20
21
22
23
24
25
26
# File 'lib/heapviz/renderer.rb', line 17

def initialize(op_path, heap, logger = nil)
  @op_path = op_path
  @logger = logger || Logger.new(op_path + ".log")
  @logger.datetime_format = "%s.%L"
  @heap = heap
  @img = ChunkyPNG::Image.new(
    heap.page_count * SLOT_BASE_SIZE,
    heap.max_page_size * SLOT_BASE_SIZE
  )
end

Instance Method Details

#renderObject



28
29
30
31
32
33
34
35
# File 'lib/heapviz/renderer.rb', line 28

def render
  @heap.pages.each_with_index do |page, i|
    start_i = i * SLOT_BASE_SIZE
    render_page(page, start_i)
  end

  @img.save(@op_path, interlace: true)
end

#render_page(page, x) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/heapviz/renderer.rb', line 49

def render_page(page, x)
  # The height of each slot will be scaled up depending on the
  # slot size of the page. Slot size is a multiple of rvalue size
  # right now
  adjusted_height = SLOT_BASE_SIZE * (page.slot_size / Heap::SIZEOF_RVALUE)

  @logger.debug "#{page}"
  page.each_slot.with_index do |slot, y|
    @logger.debug "\t=> #{slot}"
    y = y * adjusted_height

    adjusted_height.times do |y_offset|
      SLOT_BASE_SIZE.times do |x_offset|
        @img[x + x_offset, y + y_offset] = slot_colour(slot)
      end
    end
  end
end

#slot_colour(slot) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
# File 'lib/heapviz/renderer.rb', line 37

def slot_colour(slot)
  return WHITE unless slot

  if slot.pinned?
    BLACK
  elsif slot.type == "NONE"
    WHITE
  else
    COLORS[slot.size]
  end
end