Skip to content

Commit

Permalink
Revert "Revert "render: cache SKPaint, and drawing contexts""
Browse files Browse the repository at this point in the history
This reverts commit 3540ecc.
  • Loading branch information
Yatao Li committed Mar 21, 2020
1 parent b154733 commit 6743716
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 19 deletions.
11 changes: 6 additions & 5 deletions Views/Cursor.xaml.fs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ type Cursor() as this =
let mutable fgbrush: SolidColorBrush = SolidColorBrush(Colors.White)
let mutable spbrush: SolidColorBrush = SolidColorBrush(Colors.Red)
let mutable cursor_fb = AllocateFramebuffer (20.0) (20.0) 1.0
let mutable cursor_dc: Avalonia.Platform.IDrawingContextImpl = null
let mutable cursor_fb_vm = CursorViewModel(Some -1)
let mutable cursor_fb_s = 1.0

Expand All @@ -42,7 +43,9 @@ type Cursor() as this =
cursor_fb_vm <- this.ViewModel.Clone()
cursor_fb_s <- s
cursor_fb.Dispose()
cursor_dc.Dispose()
cursor_fb <- AllocateFramebuffer (cursor_fb_vm.Width + 50.0) (cursor_fb_vm.Height + 50.0) s
cursor_dc <- cursor_fb.CreateDrawingContext(null)
true
elif cursor_fb_vm.text <> this.ViewModel.text then
cursor_fb_vm.text <- this.ViewModel.text
Expand Down Expand Up @@ -162,11 +165,9 @@ type Cursor() as this =
// deferred
let redraw = ensure_fb()
if redraw then
let dc = cursor_fb.CreateDrawingContext(null)
dc.PushClip(bounds)
render_block dc
dc.PopClip()
dc.Dispose()
cursor_dc.PushClip(bounds)
render_block cursor_dc
cursor_dc.PopClip()
ctx.DrawImage(cursor_fb, 1.0, Rect(0.0, 0.0, bounds.Width * scale, bounds.Height * scale), bounds)
with
| ex -> trace "cursor" "render exception: %s" <| ex.ToString()
Expand Down
27 changes: 13 additions & 14 deletions Views/Editor.xaml.fs
Original file line number Diff line number Diff line change
Expand Up @@ -38,19 +38,20 @@ type Editor() as this =
static let RenderTickProperty = AvaloniaProperty.Register<Editor, int>("RenderTick")

let mutable grid_fb: RenderTargetBitmap = null
let mutable grid_dc: IDrawingContextImpl = null
let mutable grid_scale: float = 1.0
let mutable grid_vm: EditorViewModel = Unchecked.defaultof<_>

let mutable m_debug = States.ui_multigrid
let mutable m_alphaLUT: byte [] = Array.create 256 0uy
let mutable m_normalLUT: byte [] = Array.create 256 0uy
let mutable m_nullLUT: byte [] = Array.create 256 0uy

let resizeFrameBuffer() =
trace grid_vm "resizeFrameBuffer bufw=%A bufh=%A" grid_vm.BufferWidth grid_vm.BufferHeight
grid_scale <- this.GetVisualRoot().RenderScaling
if grid_fb <> null then grid_fb.Dispose()
if grid_fb <> null then
grid_fb.Dispose()
grid_dc.Dispose()
grid_fb <- AllocateFramebuffer (grid_vm.BufferWidth) (grid_vm.BufferHeight) grid_scale
grid_dc <- grid_fb.CreateDrawingContext(null)

//-------------------------------------------------------------------------
// = The rounding error of the rendering system =
Expand All @@ -77,6 +78,10 @@ type Editor() as this =
let px = pt * grid_scale
Point(Math.Ceiling px.X, Math.Ceiling px.Y) / grid_scale

let mutable fgpaint = null
let mutable bgpaint = null
let mutable sppaint = null

let drawBuffer (ctx: IDrawingContextImpl) row col colend hlid (str: string list) (issym: bool) =

let x =
Expand All @@ -88,9 +93,10 @@ type Editor() as this =
let fg, bg, sp, attrs = theme.GetDrawAttrs hlid
let shaper, typeface = GetTypeface(x, attrs.italic, attrs.bold, font, fontwide)

use fgpaint = new SKPaint()
use bgpaint = new SKPaint()
use sppaint = new SKPaint()
if fgpaint = null then
fgpaint <- new SKPaint()
bgpaint <- new SKPaint()
sppaint <- new SKPaint()
SetForegroundBrush(fgpaint, fg, typeface, fontsize)

let nr_col =
Expand Down Expand Up @@ -234,12 +240,6 @@ type Editor() as this =

do

// setup color filter LUTs
for i = 0 to 255 do
m_alphaLUT.[i] <- 0uy
m_normalLUT.[i] <- byte i
m_alphaLUT.[255] <- 255uy

this.Watch
[ this.GetObservable(Editor.DataContextProperty)
|> Observable.ofType
Expand Down Expand Up @@ -270,7 +270,6 @@ type Editor() as this =
let regions = dirty.Regions()
trace grid_vm "drawing %d regions" regions.Count
let timer = System.Diagnostics.Stopwatch.StartNew()
use grid_dc = grid_fb.CreateDrawingContext(null)
grid_dc.PushClip(Rect this.Bounds.Size)
for r in regions do
for row = r.row to r.row_end - 1 do
Expand Down

0 comments on commit 6743716

Please sign in to comment.