Factored YOrder out of Renderer as it is only an optimization and not directly related to the rendering.

This commit is contained in:
Sascha L. Teichmann 2017-03-01 15:05:51 +01:00
parent 75aeb36c95
commit 956f7b5e8b
3 changed files with 122 additions and 93 deletions

View File

@ -74,24 +74,31 @@ func NewBaseTileCreator(
update BaseTileUpdateFunc) *BaseTileCreator {
renderer := NewRenderer(tileWidth, tileHeight, transparent)
yMin, yMax = Order16(yMin, yMax)
return &BaseTileCreator{
btc := &BaseTileCreator{
client: client,
colors: colors,
bg: bg,
renderer: renderer,
yOrder: NewYOrder(renderer, yOrderCapacity),
yMin: yMin,
yMax: yMax,
baseDir: baseDir,
update: update}
btc.yOrder = NewYOrder(btc.renderBlock, yOrderCapacity)
return btc
}
func (btc *BaseTileCreator) Close() error {
return btc.client.Close()
}
func (btc *BaseTileCreator) drawBlock(block *Block) {
if err := btc.yOrder.RenderBlock(block, btc.colors); err != nil {
// renderBlock is a callback to draw a block with a YOrder.
func (btc *BaseTileCreator) renderBlock(block *Block) error {
return btc.renderer.RenderBlock(block, btc.colors)
}
// blockLoaded is a callback for RedisClient.QueryCuboid.
func (btc *BaseTileCreator) blockLoaded(block *Block) {
if err := btc.yOrder.RenderBlock(block); err != nil {
log.Printf("WARN: rendering block failed: %s\n", err)
}
}
@ -126,10 +133,10 @@ func (btc *BaseTileCreator) CreateTile(x, z int16, i, j int) (bool, error) {
query := Cuboid{P1: c1, P2: c2}
var count int
var err error
if count, err = btc.client.QueryCuboid(query, btc.drawBlock); err != nil {
if count, err = btc.client.QueryCuboid(query, btc.blockLoaded); err != nil {
return false, err
}
if err = btc.yOrder.Drain(btc.colors); err != nil {
if err = btc.yOrder.Drain(); err != nil {
log.Printf("WARN: rendering block failed: %s\n", err)
}

View File

@ -5,7 +5,6 @@
package common
import (
"container/heap"
"image"
"image/color"
"image/draw"
@ -27,92 +26,6 @@ type Renderer struct {
tBuffer []*Span
}
type YOrder struct {
Renderer *Renderer
blocks []*Block
capacity int
}
func NewYOrder(renderer *Renderer, capacity int) *YOrder {
return &YOrder{
Renderer: renderer,
blocks: make([]*Block, 0, capacity),
capacity: capacity}
}
func (yo *YOrder) Reset() {
yo.blocks = yo.blocks[:0]
}
func copyData(data []byte) []byte {
l := len(data)
ndata := make([]byte, l, Max(l, 8*1024))
copy(ndata, data)
return ndata
}
func (yo *YOrder) RenderBlock(block *Block, colors *Colors) (err error) {
var nblock *Block
if len(yo.blocks) == yo.capacity {
oblock := yo.blocks[0]
if oblock.Coord.Y < block.Coord.Y {
// New one is above highest old. Directly render new.
err = yo.Renderer.RenderBlock(block, colors)
return
}
// Render old one. Store copy of new in heap.
heap.Pop(yo)
err = yo.Renderer.RenderBlock(oblock, colors)
l := len(block.Data)
if cap(oblock.Data) < l {
oblock.Data = make([]byte, l, Max(l, 8*1024))
} else {
oblock.Data = oblock.Data[:l]
}
copy(oblock.Data, block.Data)
oblock.Coord = block.Coord
nblock = oblock
} else {
nblock = &Block{Coord: block.Coord, Data: copyData(block.Data)}
}
heap.Push(yo, nblock)
return
}
func (yo *YOrder) Drain(colors *Colors) (err error) {
for len(yo.blocks) > 0 {
if err = yo.Renderer.RenderBlock(heap.Pop(yo).(*Block), colors); err != nil {
return
}
}
return
}
func (yo *YOrder) Len() int {
return len(yo.blocks)
}
func (yo *YOrder) Swap(i, j int) {
yo.blocks[i], yo.blocks[j] = yo.blocks[j], yo.blocks[i]
}
func (yo *YOrder) Less(i, j int) bool {
// Reverse order intented.
return yo.blocks[i].Coord.Y > yo.blocks[j].Coord.Y
}
func (yo *YOrder) Push(x interface{}) {
yo.blocks = append(yo.blocks, x.(*Block))
}
func (yo *YOrder) Pop() (x interface{}) {
l := len(yo.blocks)
x = yo.blocks[l-1]
yo.blocks = yo.blocks[0 : l-1]
return x
}
func NewRenderer(width, height int, transparent bool) (renderer *Renderer) {
dim := width * height
pixSize := dim * 16 * 16

109
common/yorder.go Normal file
View File

@ -0,0 +1,109 @@
// Copyright 2014, 2015, 2017 by Sascha L. Teichmann
// Use of this source code is governed by the MIT license
// that can be found in the LICENSE file.
package common
import "container/heap"
// YOrder is a "streaming" Y sorter. The blocks comming from the
// database are not sorted in Y order. To unpack only the
// relevant blocks (the one at the surface) it would be nice
// to have them sorted in inverse Y order so that blocks with
// lower Y value are shadowed by ones wither higher value.
//
// Sorting all blocks correctly would leadind to load all blocks
// before rendering. But a perfect order is not strictly necessary
// because the problem is (expensively) solved at per node level.
//
// The YOrder defines a "windowed" data structure in which all blocks
// are sorted correctly. So for small amounts of blocks the
// sorting is perfect. For larger amounts it is possible to
// have partial incorrect sortings but as stated above it doesn't
// matter. The window allows not to preload all blocks.
type YOrder struct {
RenderFn func(*Block) error
blocks []*Block
capacity int
}
func NewYOrder(renderFn func(*Block) error, capacity int) *YOrder {
return &YOrder{
RenderFn: renderFn,
blocks: make([]*Block, 0, capacity),
capacity: capacity}
}
func (yo *YOrder) Reset() {
yo.blocks = yo.blocks[:0]
}
func copyData(data []byte) []byte {
l := len(data)
ndata := make([]byte, l, Max(l, 8*1024))
copy(ndata, data)
return ndata
}
func (yo *YOrder) RenderBlock(block *Block) (err error) {
var nblock *Block
if len(yo.blocks) == yo.capacity {
oblock := yo.blocks[0]
if oblock.Coord.Y < block.Coord.Y {
// New one is above highest old. Directly render new.
err = yo.RenderFn(block)
return
}
// Render old one. Store copy of new in heap.
heap.Pop(yo)
err = yo.RenderFn(oblock)
l := len(block.Data)
if cap(oblock.Data) < l {
oblock.Data = make([]byte, l, Max(l, 8*1024))
} else {
oblock.Data = oblock.Data[:l]
}
copy(oblock.Data, block.Data)
oblock.Coord = block.Coord
nblock = oblock
} else {
nblock = &Block{Coord: block.Coord, Data: copyData(block.Data)}
}
heap.Push(yo, nblock)
return
}
func (yo *YOrder) Drain() error {
for len(yo.blocks) > 0 {
if err := yo.RenderFn(heap.Pop(yo).(*Block)); err != nil {
return err
}
}
return nil
}
func (yo *YOrder) Len() int {
return len(yo.blocks)
}
func (yo *YOrder) Swap(i, j int) {
yo.blocks[i], yo.blocks[j] = yo.blocks[j], yo.blocks[i]
}
func (yo *YOrder) Less(i, j int) bool {
// Reverse order intented.
return yo.blocks[i].Coord.Y > yo.blocks[j].Coord.Y
}
func (yo *YOrder) Push(x interface{}) {
yo.blocks = append(yo.blocks, x.(*Block))
}
func (yo *YOrder) Pop() (x interface{}) {
l := len(yo.blocks)
x = yo.blocks[l-1]
yo.blocks = yo.blocks[0 : l-1]
return x
}