Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor display code to be Screen centric #2306

Merged
merged 71 commits into from
Oct 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
71 commits
Select commit Hold shift + click to select a range
a5d8e84
Only rasterize if the backend is not itself raster
asinghvi17 May 31, 2022
88091a9
Define width of CairoScreen
asinghvi17 May 31, 2022
5467264
Make colorbuffer use the size of the screen, not the scene
asinghvi17 May 31, 2022
76265d3
Merge branch 'master' into as/resizable_colorbuffer
SimonDanisch Jun 3, 2022
40cb1f7
Merge branch 'master' into as/resizable_colorbuffer
asinghvi17 Jun 21, 2022
b561377
Add GeometryBasics.widths for ThreeDisplay
asinghvi17 Jun 21, 2022
241128e
Add GeometryBasics.widths for WebDisplay
asinghvi17 Jun 21, 2022
ff7edcf
Fix inadvertent bug
asinghvi17 Jul 10, 2022
3fd8f68
Merge branch 'master' into as/resizable_colorbuffer
asinghvi17 Jul 10, 2022
c2082fc
Add news entry
asinghvi17 Jul 11, 2022
713a8d6
Decrease the number of evaljs_value calls
asinghvi17 Jul 11, 2022
96ee55b
broadcast in WGLMakie method
asinghvi17 Jul 12, 2022
14e45a6
Pass kwargs along to backend_show in backend_display for CairoMakie
asinghvi17 Jul 12, 2022
16b7409
Pass arbitrary kwargs along
asinghvi17 Jul 12, 2022
521b4b0
convert Iterators.Pairs to namedtuple in backend_display
asinghvi17 Jul 13, 2022
7f4652f
Splat kwargs as a vector of pairs
asinghvi17 Jul 13, 2022
4331849
Merge branch 'master' into as/resizable_colorbuffer
SimonDanisch Sep 22, 2022
4fb61c2
clean up + merge master
SimonDanisch Sep 23, 2022
da036f4
refactor display code
SimonDanisch Sep 26, 2022
84efb78
new screen constructors
SimonDanisch Sep 26, 2022
6545947
get CairoMakie & GLMakie to work
SimonDanisch Sep 27, 2022
915a108
merge master
SimonDanisch Sep 27, 2022
a0b4955
fix tests
SimonDanisch Sep 27, 2022
ccbb4d6
more context switching
SimonDanisch Sep 28, 2022
d8fd213
try thiss
SimonDanisch Sep 28, 2022
0349b5d
hm
SimonDanisch Sep 28, 2022
88219bd
make sure we track context in shader compilation
SimonDanisch Sep 29, 2022
b8d17af
clean up ROBJ and context switching
SimonDanisch Sep 29, 2022
76594d1
fix RPRMakie
SimonDanisch Sep 29, 2022
0b0a148
fix thy backends
SimonDanisch Sep 29, 2022
5ab721a
fix stepper and docs
SimonDanisch Sep 29, 2022
b768ee6
remove all inline! use
SimonDanisch Sep 29, 2022
7110570
fix GLMakie screen deregistering
SimonDanisch Sep 29, 2022
e470780
Merge branch 'master' into sd/display-refactor
SimonDanisch Sep 29, 2022
5cb0e8e
fix offset in Stepper for CairoMakie
SimonDanisch Sep 29, 2022
543be4c
fix benchmarks + precompiles
SimonDanisch Sep 30, 2022
0f25366
implement switching of mimes
SimonDanisch Sep 30, 2022
7dfd3f2
Merge branch 'master' into sd/display-refactor
SimonDanisch Oct 4, 2022
bfb8c28
move display config to theme
SimonDanisch Oct 4, 2022
4dee3bb
Merge branch 'sd/display-refactor' of https:/MakieOrg/Mak…
SimonDanisch Oct 4, 2022
b32642d
fix parse error
SimonDanisch Oct 4, 2022
afa9525
bring back precompiles
SimonDanisch Oct 5, 2022
666c82b
small fixes/improvements
SimonDanisch Oct 5, 2022
5d8d419
incorperate changes from #2231 and refactor scene
SimonDanisch Oct 5, 2022
50110ea
fix CI ?
SimonDanisch Oct 5, 2022
fea07cf
improved screen clean up
SimonDanisch Oct 6, 2022
6a93b54
clean up and tests
SimonDanisch Oct 6, 2022
04d8131
add logging
SimonDanisch Oct 6, 2022
b2f7cef
fix makie unit tests
SimonDanisch Oct 6, 2022
1fc3da5
let CI fail gracefully
SimonDanisch Oct 6, 2022
162c8da
fix import
SimonDanisch Oct 6, 2022
87e0707
remove last GLMakie.Screen related globals
SimonDanisch Oct 7, 2022
3593f61
small improvements for Pluto + friends
SimonDanisch Oct 7, 2022
adabcab
add comment
SimonDanisch Oct 7, 2022
10a3c58
clean up docs
SimonDanisch Oct 10, 2022
cb66d3a
Merge branch 'master' into sd/display-refactor
SimonDanisch Oct 10, 2022
28f6605
move preferred mime machinery to CairoMakie, since its not needed any…
SimonDanisch Oct 10, 2022
bdd1cce
Merge branch 'sd/display-refactor' of https:/MakieOrg/Mak…
SimonDanisch Oct 10, 2022
0f37417
remove set_preferred_mime! from tests etc
SimonDanisch Oct 10, 2022
38065e4
Merge branch 'master' into sd/display-refactor
SimonDanisch Oct 10, 2022
80b857a
merge master
SimonDanisch Oct 11, 2022
4bbd47e
fix mime test
SimonDanisch Oct 11, 2022
5a494e0
last clean up & address review
SimonDanisch Oct 11, 2022
03dc6f9
update docs
SimonDanisch Oct 11, 2022
8642146
initialize array
SimonDanisch Oct 11, 2022
c7d6e5f
deprecate set_window_config! properly
SimonDanisch Oct 11, 2022
e164fe9
forgot another constructor
SimonDanisch Oct 11, 2022
5e92ee1
small clean up and doc fixes
SimonDanisch Oct 12, 2022
afe19ec
Merge branch 'master' into sd/display-refactor
SimonDanisch Oct 12, 2022
1269a41
Merge branch 'sd/display-refactor' of https:/MakieOrg/Mak…
SimonDanisch Oct 12, 2022
d5b1630
small doc fixes
SimonDanisch Oct 12, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/Docs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ jobs:
- name: Install Julia
uses: julia-actions/setup-julia@v1
with:
version: 1.6
version: '1'
- name: Build and deploy docs
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
Expand Down
1 change: 0 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ jobs:
with:
version: ${{ matrix.version }}
arch: ${{ matrix.arch }}
run: sudo apt-get -y install xclip
- uses: julia-actions/cache@v1
- name: Install Julia dependencies
shell: julia --project=monorepo {0}
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/compilation-benchmark.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ jobs:
run: sudo apt-get update && sudo apt-get install -y xorg-dev mesa-utils xvfb libgl1 freeglut3-dev libxrandr-dev libxinerama-dev libxcursor-dev libxi-dev libxext-dev
- uses: julia-actions/setup-julia@v1
with:
version: nightly
version: '1'
arch: x64
- uses: julia-actions/cache@v1
- name: Benchmark
Expand Down
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ Manifest.toml
/build

\.DS_Store
CairoMakie/src/display.*

WGLMakie/test/recorded_reference_images/
GLMakie/test/recorded_reference_images/
Expand Down
2 changes: 1 addition & 1 deletion CairoMakie/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ function drawonto(canvas, figure)
@guarded draw(canvas) do _
scene = figure.scene
resize!(scene, Gtk.width(canvas), Gtk.height(canvas))
screen = CairoMakie.CairoScreen(scene, Gtk.cairo_surface(canvas), getgc(canvas), nothing)
screen = CairoMakie.Screen(scene, Gtk.cairo_surface(canvas), getgc(canvas), nothing)
CairoMakie.cairo_draw(screen, scene)
end
end
Expand Down
35 changes: 4 additions & 31 deletions CairoMakie/src/CairoMakie.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@ import Cairo

using Makie: Scene, Lines, Text, Image, Heatmap, Scatter, @key_str, broadcast_foreach
using Makie: convert_attribute, @extractvalue, LineSegments, to_ndim, NativeFont
using Makie: @info, @get_attribute, Combined
using Makie: @info, @get_attribute, Combined, MakieScreen
using Makie: to_value, to_colormap, extrema_nan
using Makie: inline!
using Makie.Observables
using Makie: spaces, is_data_space, is_pixel_space, is_relative_space, is_clip_space
using Makie: numbers_to_colors
Expand All @@ -22,45 +21,19 @@ for name in names(Makie, all=true)
@eval export $(name)
end
end
export inline!

include("cairo-extension.jl")
include("screen.jl")
include("display.jl")
include("infrastructure.jl")
include("utils.jl")
include("fonts.jl")
include("primitives.jl")
include("overrides.jl")

function __init__()
activate!()
Makie.register_backend!(Makie.current_backend[])
end

function display_path(type::String)
if !(type in ("svg", "png", "pdf", "eps"))
error("Only \"svg\", \"png\", \"eps\" and \"pdf\" are allowed for `type`. Found: $(type)")
end
return joinpath(@__DIR__, "display." * type)
end

const _last_inline = Ref(true)
const _last_type = Ref("png")
const _last_px_per_unit = Ref(1.0)
const _last_pt_per_unit = Ref(0.75)
const _last_antialias = Ref(Cairo.ANTIALIAS_BEST)

function activate!(; inline = _last_inline[], type = _last_type[], px_per_unit=_last_px_per_unit[], pt_per_unit=_last_pt_per_unit[], antialias = _last_antialias[])
backend = CairoBackend(display_path(type); px_per_unit=px_per_unit, pt_per_unit=pt_per_unit, antialias = antialias)
Makie.current_backend[] = backend
Makie.use_display[] = !inline
_last_inline[] = inline
_last_type[] = type
_last_px_per_unit[] = px_per_unit
_last_pt_per_unit[] = pt_per_unit
_last_antialias[] = antialias
return
end

include("precompiles.jl")


end
75 changes: 75 additions & 0 deletions CairoMakie/src/cairo-extension.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
# TODO, move those to Cairo?

function set_font_matrix(ctx, matrix)
ccall((:cairo_set_font_matrix, Cairo.libcairo), Cvoid, (Ptr{Cvoid}, Ptr{Cvoid}), ctx.ptr, Ref(matrix))
end

function get_font_matrix(ctx)
matrix = Cairo.CairoMatrix()
ccall((:cairo_get_font_matrix, Cairo.libcairo), Cvoid, (Ptr{Cvoid}, Ptr{Cvoid}), ctx.ptr, Ref(matrix))
return matrix
end

function cairo_font_face_destroy(font_face)
ccall(
(:cairo_font_face_destroy, Cairo.libcairo),
Cvoid, (Ptr{Cvoid},),
font_face
)
end

function set_ft_font(ctx, font)

font_face = ccall(
(:cairo_ft_font_face_create_for_ft_face, Cairo.libcairo),
Ptr{Cvoid}, (Makie.FreeTypeAbstraction.FT_Face, Cint),
font, 0
)

ccall((:cairo_set_font_face, Cairo.libcairo), Cvoid, (Ptr{Cvoid}, Ptr{Cvoid}), ctx.ptr, font_face)

return font_face
end

struct CairoGlyph
index::Culong
x::Cdouble
y::Cdouble
end

function show_glyph(ctx, glyph, x, y)
cg = Ref(CairoGlyph(glyph, x, y))
ccall((:cairo_show_glyphs, Cairo.libcairo),
Nothing, (Ptr{Nothing}, Ptr{CairoGlyph}, Cint),
ctx.ptr, cg, 1)
end

function glyph_path(ctx, glyph::Culong, x, y)
cg = Ref(CairoGlyph(glyph, x, y))
ccall((:cairo_glyph_path, Cairo.libcairo),
Nothing, (Ptr{Nothing}, Ptr{CairoGlyph}, Cint),
ctx.ptr, cg, 1)
end

function surface_set_device_scale(surf, device_x_scale, device_y_scale=device_x_scale)
# this sets a scaling factor on the lowest level that is "hidden" so its even
# enabled when the drawing space is reset for strokes
# that means it can be used to increase or decrease the image resolution
ccall(
(:cairo_surface_set_device_scale, Cairo.libcairo),
Cvoid, (Ptr{Nothing}, Cdouble, Cdouble),
surf.ptr, device_x_scale, device_y_scale)
end

function set_miter_limit(ctx, limit)
ccall((:cairo_set_miter_limit, Cairo.libcairo), Cvoid, (Ptr{Nothing}, Cdouble), ctx.ptr, limit)
end

function get_render_type(surface::Cairo.CairoSurface)
typ = ccall((:cairo_surface_get_type, Cairo.libcairo), Cint, (Ptr{Nothing},), surface.ptr)
typ == Cairo.CAIRO_SURFACE_TYPE_PDF && return PDF
typ == Cairo.CAIRO_SURFACE_TYPE_PS && return EPS
typ == Cairo.CAIRO_SURFACE_TYPE_SVG && return SVG
typ == Cairo.CAIRO_SURFACE_TYPE_IMAGE && return IMAGE
error("Unsupported surface type: $(typ)")
end
171 changes: 171 additions & 0 deletions CairoMakie/src/display.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@

#########################################
# Backend interface to Makie #
#########################################

"""
tryrun(cmd::Cmd)
Try to run a command. Return `true` if `cmd` runs and is successful (exits with a code of `0`).
Return `false` otherwise.
"""
function tryrun(cmd::Cmd)
try
return success(cmd)
catch e
return false
end
end

function openurl(url::String)
if Sys.isapple()
tryrun(`open $url`) && return
elseif Sys.iswindows()
tryrun(`powershell.exe start $url`) && return
elseif Sys.isunix()
tryrun(`xdg-open $url`) && return
tryrun(`gnome-open $url`) && return
end
tryrun(`python -mwebbrowser $(url)`) && return
# our last hope
tryrun(`python3 -mwebbrowser $(url)`) && return
@warn("Can't find a way to open a browser, open $(url) manually!")
end

function display_path(type::String)
if !(type in ("svg", "png", "pdf", "eps"))
error("Only \"svg\", \"png\", \"eps\" and \"pdf\" are allowed for `type`. Found: $(type)")
end
return abspath(joinpath(@__DIR__, "display." * type))
end

function Base.display(screen::Screen{IMAGE}, scene::Scene; connect=false)
path = display_path("png")
cairo_draw(screen, scene)
Cairo.write_to_png(screen.surface, path)
if screen.visible
openurl("file:///" * path)
end
end

function Makie.backend_show(screen::Screen{SVG}, io::IO, ::MIME"image/svg+xml", scene::Scene)

cairo_draw(screen, scene)
Cairo.flush(screen.surface)
Cairo.finish(screen.surface)

svg = String(take!(Makie.raw_io(screen.surface.stream)))

# for some reason, in the svg, surfaceXXX ids keep counting up,
# even with the very same figure drawn again and again
# so we need to reset them to counting up from 1
# so that the same figure results in the same svg and in the same salt
surfaceids = sort(unique(collect(m.match for m in eachmatch(r"surface\d+", svg))))

for (i, id) in enumerate(surfaceids)
svg = replace(svg, id => "surface$i")
end

# salt svg ids with the first 8 characters of the base64 encoded
# sha512 hash to avoid collisions across svgs when embedding them on
# websites. the hash and therefore the salt will always be the same for the same file
# so the output is deterministic
salt = String(Base64.base64encode(SHA.sha512(svg)))[1:8]

ids = sort(unique(collect(m[1] for m in eachmatch(r"id\s*=\s*\"([^\"]*)\"", svg))))

for id in ids
svg = replace(svg, id => "$id-$salt")
end

print(io, svg)
return screen
end

function Makie.backend_show(screen::Screen{PDF}, io::IO, ::MIME"application/pdf", scene::Scene)
cairo_draw(screen, scene)
Cairo.finish(screen.surface)
return screen
end

function Makie.backend_show(screen::Screen{EPS}, io::IO, ::MIME"application/postscript", scene::Scene)
cairo_draw(screen, scene)
Cairo.finish(screen.surface)
return screen
end

function Makie.backend_show(screen::Screen{IMAGE}, io::IO, ::MIME"image/png", scene::Scene)
cairo_draw(screen, scene)
Cairo.write_to_png(screen.surface, io)
return screen
end

# Disabling mimes and showable

const DISABLED_MIMES = Set{String}()
const SUPPORTED_MIMES = Set([
"image/svg+xml",
"application/pdf",
"application/postscript",
"image/png"
])

function Makie.backend_showable(::Type{Screen}, ::MIME{SYM}) where SYM
supported_mimes = Base.setdiff(SUPPORTED_MIMES, DISABLED_MIMES)
return string(SYM) in supported_mimes
end

"""
to_mime_string(mime::Union{String, Symbol, MIME})

Converts anything like `"png", :png, "image/png", MIME"image/png"()` to `"image/png"`.
"""
function to_mime_string(mime::Union{String, Symbol, MIME})
if mime isa MIME
mime_str = string(mime)
if !(mime_str in SUPPORTED_MIMES)
error("Mime $(mime) not supported by CairoMakie")
end
return mime_str
else
mime_str = string(mime)
if !(mime_str in SUPPORTED_MIMES)
mime_str = string(to_mime(convert(RenderType, mime_str)))
end
return mime_str
end
end

"""
disable_mime!(mime::Union{String, Symbol, MIME}...)

The default is automatic, which lets the display system figure out the best mime.
If set to any other valid mime, will result in `showable(any_other_mime, figurelike)` to return false and only return true for `showable(preferred_mime, figurelike)`.
Depending on the display system used, this may result in nothing getting displayed.
"""
function disable_mime!(mimes::Union{String, Symbol, MIME}...)
empty!(DISABLED_MIMES) # always start from 0
if isempty(mimes)
# Reset disabled mimes when called with no arguments
return
end
mime_strings = Set{String}()
for mime in mimes
push!(mime_strings, to_mime_string(mime))
end
union!(DISABLED_MIMES, mime_strings)
return
end

function enable_only_mime!(mimes::Union{String, Symbol, MIME}...)
empty!(DISABLED_MIMES) # always start from 0
if isempty(mimes)
# Reset disabled mimes when called with no arguments
return
end
mime_strings = Set{String}()
for mime in mimes
push!(mime_strings, to_mime_string(mime))
end
union!(DISABLED_MIMES, setdiff(SUPPORTED_MIMES, mime_strings))
return
end
46 changes: 0 additions & 46 deletions CairoMakie/src/fonts.jl

This file was deleted.

Loading