github.com/icexin/eggos@v0.4.2-0.20220216025428-78b167e4f349/drivers/vbe/view.go (about) 1 // Package vbe provides access to the VESA VBE framebuffer. 2 package vbe 3 4 import ( 5 "image" 6 "image/draw" 7 ) 8 9 // View provides a canvas for offscreen rendering which may be commited to the 10 // framebuffer. 11 type View struct { 12 // underlying offscreen rendering buffer. 13 buffer *image.RGBA 14 } 15 16 // NewView returns a new view for offscreen rendering which may be commited to 17 // the framebuffer. 18 func NewView() *View { 19 return &View{ 20 buffer: image.NewRGBA(image.Rect(0, 0, int(info.Width), int(info.Height))), 21 } 22 } 23 24 // Canvas returns a canvas for rendering into the given view. 25 func (v *View) Canvas() draw.Image { 26 return v.buffer 27 } 28 29 // Clear clears the given view, setting every pixel to black colour. 30 func (v *View) Clear() { 31 for i := range v.buffer.Pix { 32 v.buffer.Pix[i] = 0 33 } 34 } 35 36 // CommitRect commits the given rectangle of the view to the framebuffer, 37 // copying the pixels of the view within rect to the framebuffer. 38 func (v *View) CommitRect(rect image.Rectangle) { 39 if fbbuf == nil { 40 return 41 } 42 if v != currentView { 43 return 44 } 45 46 // let rect in the range of view rect 47 rect = v.buffer.Rect.Intersect(rect).Canon() 48 49 bufcopy(buffer, v.buffer.Pix, v.buffer.Stride, rect, func(dst, src []uint8) { 50 for i := 0; i < len(dst); i += 4 { 51 _ = dst[i+3] 52 _ = src[i+3] 53 dst[i] = src[i+2] 54 dst[i+1] = src[i+1] 55 dst[i+2] = src[i] 56 dst[i+3] = src[i+3] 57 } 58 }) 59 60 bufcopy(fbbuf, buffer, v.buffer.Stride, rect, func(dst, src []uint8) { 61 copy(dst, src) 62 }) 63 } 64 65 // Commit commits the view to the framebuffer, copying every pixel of the view 66 // to the framebuffer. 67 func (v *View) Commit() { 68 if fbbuf == nil { 69 return 70 } 71 if v != currentView { 72 return 73 } 74 75 pix := v.buffer.Pix 76 for i, j := 0, 0; i < len(pix); i, j = i+4, j+4 { 77 _ = buffer[j+3] 78 _ = pix[i+3] 79 buffer[j] = pix[i+2] 80 buffer[j+1] = pix[i+1] 81 buffer[j+2] = pix[i] 82 buffer[j+3] = pix[i+3] 83 } 84 copy(fbbuf, buffer) 85 }