github.com/jspc/eggos@v0.5.1-0.20221028160421-556c75c878a5/drivers/vbe/view.go (about) 1 package vbe 2 3 import ( 4 "image" 5 "image/draw" 6 ) 7 8 type View struct { 9 buffer *image.RGBA 10 } 11 12 func NewView() *View { 13 return &View{ 14 buffer: image.NewRGBA(image.Rect(0, 0, int(info.Width), int(info.Height))), 15 } 16 } 17 18 func (v *View) Canvas() draw.Image { 19 return v.buffer 20 } 21 22 func (v *View) Clear() { 23 for i := range v.buffer.Pix { 24 v.buffer.Pix[i] = 0 25 } 26 } 27 28 func (v *View) CommitRect(rect image.Rectangle) { 29 if fbbuf == nil { 30 return 31 } 32 if v != currentView { 33 return 34 } 35 36 // let rect in the range of view rect 37 rect = v.buffer.Rect.Intersect(rect).Canon() 38 39 bufcopy(buffer, v.buffer.Pix, v.buffer.Stride, rect, func(dst, src []uint8) { 40 for i := 0; i < len(dst); i += 4 { 41 _ = dst[i+3] 42 _ = src[i+3] 43 dst[i] = src[i+2] 44 dst[i+1] = src[i+1] 45 dst[i+2] = src[i] 46 dst[i+3] = src[i+3] 47 } 48 }) 49 50 bufcopy(fbbuf, buffer, v.buffer.Stride, rect, func(dst, src []uint8) { 51 copy(dst, src) 52 }) 53 } 54 55 func (v *View) Commit() { 56 if fbbuf == nil { 57 return 58 } 59 if v != currentView { 60 return 61 } 62 63 pix := v.buffer.Pix 64 for i, j := 0, 0; i < len(pix); i, j = i+4, j+4 { 65 _ = buffer[j+3] 66 _ = pix[i+3] 67 buffer[j] = pix[i+2] 68 buffer[j+1] = pix[i+1] 69 buffer[j+2] = pix[i] 70 buffer[j+3] = pix[i+3] 71 } 72 copy(fbbuf, buffer) 73 }