From bfe7256714a7b6e881907e0788a8ff3063899bee Mon Sep 17 00:00:00 2001 From: Anshul Singhvi Date: Wed, 8 Jun 2022 23:28:08 +0200 Subject: [PATCH 1/7] First shot at GLMakie plots in CairoMakie --- CairoMakie/src/glmakie_integration.jl | 106 ++++++++++++++++++++++++++ 1 file changed, 106 insertions(+) create mode 100644 CairoMakie/src/glmakie_integration.jl diff --git a/CairoMakie/src/glmakie_integration.jl b/CairoMakie/src/glmakie_integration.jl new file mode 100644 index 00000000000..8c7acf745ce --- /dev/null +++ b/CairoMakie/src/glmakie_integration.jl @@ -0,0 +1,106 @@ + +function gl_alpha_colorbuffer(scene::Scene) + GLMakie.activate!() + # Makie.inline!(true) + bg = scene.backgroundcolor[] + scene.backgroundcolor[] = RGBf(0, 0, 0) + screen = display(scene) + println("Screen") + @show size(scene) GeometryBasics.widths(screen) + b1 = Makie.colorbuffer(screen) + # b1, screen1 = GLMakie.scene2image(scene) + save("hi-white.png", b1) + b1 = deepcopy(b1) + println("White") + @show size(scene) GeometryBasics.widths(screen) size(b1) + scene.backgroundcolor[] = RGBf(1, 1, 1) + screen = display(scene) + b2 = Makie.colorbuffer(screen) + # b2, screen2 = GLMakie.scene2image(scene) + save("hi-black.png", b2) + println("Black") + @show size(scene) GeometryBasics.widths(screen) size(b2) + scene.backgroundcolor[] = bg + CairoMakie.activate!() + # Makie.inline!(!Makie.use_display[]) + c= infer_alphacolor.(b1, b2) + @show typeof(c) + c +end + +function alpha_colorbuffer(scene) + GLMakie.activate!() + display(scene) + bg = scene.backgroundcolor[] + scene.backgroundcolor[] = RGBAf(0, 0, 0, 1) + b1 = copy(Makie.colorbuffer(scene)) + FileIO.save("hi-white.png", b1) + scene.backgroundcolor[] = RGBAf(1, 1, 1, 1) + b2 = Makie.colorbuffer(scene) + FileIO.save("hi-black.png", b2) + CairoMakie.activate!() + scene.backgroundcolor[] = bg + return map(infer_alphacolor, b1, b2) +end + + +function infer_alphacolor(rgb1, rgb2) + rgb1 == rgb2 && return RGBAf(rgb1.r, rgb1.g, rgb1.b, 1) + c1 = Float64.((rgb1.r, rgb1.g, rgb1.b)) + c2 = Float64.((rgb2.r, rgb2.g, rgb2.b)) + alpha = @. 1 - (c1 - c2) * -1 # ( / (0 - 1)) + meanalpha = clamp(sum(alpha) / 3, 0, 1) + meanalpha == 0 && return RGBAf(0, 0, 0, 0) + c = @. clamp((c1 / meanalpha), 0, 1) + return RGBAf(c..., meanalpha) +end + +function plot2img(plot::Combined, scale::Real = 1; use_backgroundcolor = false) + scene = Makie.parent_scene(plot) + w, h = Int.(scene.px_area[].widths) + + # We create a dummy scene to render to, which inherits its parent's + # transformation and camera + + render_scene = Makie.Scene(camera = Makie.camera(scene), lights = scene.lights, ssao = scene.ssao, show_axis = false, backgroundcolor = :transparent,) + Makie.resize!(render_scene, (pixelarea(scene)[].widths .* scale)...) + # copy over the transofrmation and camera attributes + render_scene.transformation.transform_func[] = scene.transformation.transform_func[] + render_scene.transformation.rotation[] = scene.transformation.rotation[] + render_scene.transformation.scale[] = scene.transformation.scale[] + render_scene.transformation.translation[] = scene.transformation.translation[] + + # push only the relevant plot to the scene + push!(render_scene, plot) + + img = if use_backgroundcolor + Makie.colorbuffer(render_scene) + else # render with transparency, using the alpha-colorbuffer hack + alpha_colorbuffer(render_scene) + end + + return img +end + +function CairoMakie.draw_atomic(scene::Scene, screen::CairoMakie.CairoScreen, primitive::Volume, scale::Real = 10) + + w, h = Int.(scene.px_area[].widths) + + img = plot2img(primitive, scale; use_backgroundcolor = false) + Makie.save("hi.png", img) # cache for debugging + + surf = Cairo.CairoARGBSurface(CairoMakie.to_uint32_color.(img)) + + Cairo.rectangle(screen.context, 0, 0, w, h) + Cairo.save(screen.context) + Cairo.scale(screen.context, w / surf.width, h / surf.height) + Cairo.set_source_surface(screen.context, surf, 0, 0) + p = Cairo.get_source(screen.context) + Cairo.pattern_set_extend(p, Cairo.EXTEND_PAD) # avoid blurry edges + Cairo.pattern_set_filter(p, Cairo.FILTER_NEAREST) + Cairo.fill(screen.context) + Cairo.restore(screen.context) + + return + +end From 6e728be25c4976287e5ae7d049a3dd8506c7afb7 Mon Sep 17 00:00:00 2001 From: Anshul Singhvi Date: Wed, 8 Jun 2022 23:29:24 +0200 Subject: [PATCH 2/7] Remove debug function --- CairoMakie/src/glmakie_integration.jl | 30 --------------------------- 1 file changed, 30 deletions(-) diff --git a/CairoMakie/src/glmakie_integration.jl b/CairoMakie/src/glmakie_integration.jl index 8c7acf745ce..f1af9d4e68e 100644 --- a/CairoMakie/src/glmakie_integration.jl +++ b/CairoMakie/src/glmakie_integration.jl @@ -1,33 +1,3 @@ - -function gl_alpha_colorbuffer(scene::Scene) - GLMakie.activate!() - # Makie.inline!(true) - bg = scene.backgroundcolor[] - scene.backgroundcolor[] = RGBf(0, 0, 0) - screen = display(scene) - println("Screen") - @show size(scene) GeometryBasics.widths(screen) - b1 = Makie.colorbuffer(screen) - # b1, screen1 = GLMakie.scene2image(scene) - save("hi-white.png", b1) - b1 = deepcopy(b1) - println("White") - @show size(scene) GeometryBasics.widths(screen) size(b1) - scene.backgroundcolor[] = RGBf(1, 1, 1) - screen = display(scene) - b2 = Makie.colorbuffer(screen) - # b2, screen2 = GLMakie.scene2image(scene) - save("hi-black.png", b2) - println("Black") - @show size(scene) GeometryBasics.widths(screen) size(b2) - scene.backgroundcolor[] = bg - CairoMakie.activate!() - # Makie.inline!(!Makie.use_display[]) - c= infer_alphacolor.(b1, b2) - @show typeof(c) - c -end - function alpha_colorbuffer(scene) GLMakie.activate!() display(scene) From 4380e2e3b51e2de3bd01fc10c4a7e28761190b3b Mon Sep 17 00:00:00 2001 From: Anshul Singhvi Date: Wed, 8 Jun 2022 23:45:22 +0200 Subject: [PATCH 3/7] Remove white/black debug statements --- CairoMakie/src/glmakie_integration.jl | 2 -- 1 file changed, 2 deletions(-) diff --git a/CairoMakie/src/glmakie_integration.jl b/CairoMakie/src/glmakie_integration.jl index f1af9d4e68e..33bb4edb580 100644 --- a/CairoMakie/src/glmakie_integration.jl +++ b/CairoMakie/src/glmakie_integration.jl @@ -4,10 +4,8 @@ function alpha_colorbuffer(scene) bg = scene.backgroundcolor[] scene.backgroundcolor[] = RGBAf(0, 0, 0, 1) b1 = copy(Makie.colorbuffer(scene)) - FileIO.save("hi-white.png", b1) scene.backgroundcolor[] = RGBAf(1, 1, 1, 1) b2 = Makie.colorbuffer(scene) - FileIO.save("hi-black.png", b2) CairoMakie.activate!() scene.backgroundcolor[] = bg return map(infer_alphacolor, b1, b2) From ffd05c1c590ee5cc771c2a0062ce9a024ac1479c Mon Sep 17 00:00:00 2001 From: Anshul Singhvi Date: Fri, 10 Jun 2022 08:07:33 +0200 Subject: [PATCH 4/7] Cache render scene, reorganize code --- CairoMakie/src/glmakie_integration.jl | 92 +++++++++++++++++++++------ 1 file changed, 72 insertions(+), 20 deletions(-) diff --git a/CairoMakie/src/glmakie_integration.jl b/CairoMakie/src/glmakie_integration.jl index 33bb4edb580..fca6ac94136 100644 --- a/CairoMakie/src/glmakie_integration.jl +++ b/CairoMakie/src/glmakie_integration.jl @@ -1,3 +1,6 @@ +# Upstreamable code + +# TODO: make this function more efficient! function alpha_colorbuffer(scene) GLMakie.activate!() display(scene) @@ -23,24 +26,55 @@ function infer_alphacolor(rgb1, rgb2) return RGBAf(c..., meanalpha) end -function plot2img(plot::Combined, scale::Real = 1; use_backgroundcolor = false) +function create_render_scene(plot::Combined; scale::Real = 1) scene = Makie.parent_scene(plot) w, h = Int.(scene.px_area[].widths) # We create a dummy scene to render to, which inherits its parent's # transformation and camera - render_scene = Makie.Scene(camera = Makie.camera(scene), lights = scene.lights, ssao = scene.ssao, show_axis = false, backgroundcolor = :transparent,) - Makie.resize!(render_scene, (pixelarea(scene)[].widths .* scale)...) - # copy over the transofrmation and camera attributes - render_scene.transformation.transform_func[] = scene.transformation.transform_func[] - render_scene.transformation.rotation[] = scene.transformation.rotation[] - render_scene.transformation.scale[] = scene.transformation.scale[] - render_scene.transformation.translation[] = scene.transformation.translation[] + render_scene = Makie.Scene( + camera = Makie.camera(scene), + lights = scene.lights, + ssao = scene.ssao, + show_axis = false, + backgroundcolor = :transparent + ) + + # continually keep the pixel area updated + on(pixelarea(scene); update = true) do px_area + Makie.resize!(render_scene, (px_area.widths .* scale)...) + end + + # link the transofrmation attributes + Makie.Observables.connect!(render_scene.transformation.transform_func, scene.transformation.transform_func) + Makie.Observables.connect!(render_scene.transformation.rotation , scene.transformation.rotation) + Makie.Observables.connect!(render_scene.transformation.scale , scene.transformation.scale) + Makie.Observables.connect!(render_scene.transformation.translation , scene.transformation.translation) + Makie.Observables.connect!(render_scene.transformation.model , scene.transformation.model) # push only the relevant plot to the scene push!(render_scene, plot) + return render_scene + +end +function plot2img(plot::Combined; scale::Real = 1, use_backgroundcolor = false) + parent = Makie.parent_scene(plot) + render_scene = if haskey(parent.theme, :_render_scenes) && haskey(parent.theme._render_scenes.val, plot) + parent.theme._render_scenes.val[plot] + else # we have to create a render scene + println("Rerendering") + scene = create_render_scene(plot; scale = scale) + + # set up cache + rs_dict = get!(parent.theme.attributes, :_render_scenes, Dict{Union{Makie.AbstractPlot, Makie.Combined}, Makie.Scene}()) + rs_dict.val[plot] = scene + + scene + end + + img = if use_backgroundcolor Makie.colorbuffer(render_scene) else # render with transparency, using the alpha-colorbuffer hack @@ -50,24 +84,42 @@ function plot2img(plot::Combined, scale::Real = 1; use_backgroundcolor = false) return img end +# Utility function to remove rendercaches +function purge_render_cache!(sc::Scene) + haskey(scene.attributes, :_render_scenes) && delete!(scene.attributes, :_render_scenes) + purge_render_cache!.(scene.children) +end +purge_render_cache!(fig::Figure) = purge_render_cache!(fig.scene) + +# Rendering pipeline + +# This goes as follows: +# The first time a plot is encountered which has to be rasterized, +# we create the rasterization scene, and connect it to the original Scene's +# attributes. +# Then, we retrieve this scene from `plot._render_scene`, an attribute of the plot +# which we set in the previous step to contain the scene. +# This retrieval + + function CairoMakie.draw_atomic(scene::Scene, screen::CairoMakie.CairoScreen, primitive::Volume, scale::Real = 10) w, h = Int.(scene.px_area[].widths) - img = plot2img(primitive, scale; use_backgroundcolor = false) + img = plot2img(primitive; scale = scale, use_backgroundcolor = false) Makie.save("hi.png", img) # cache for debugging - surf = Cairo.CairoARGBSurface(CairoMakie.to_uint32_color.(img)) - - Cairo.rectangle(screen.context, 0, 0, w, h) - Cairo.save(screen.context) - Cairo.scale(screen.context, w / surf.width, h / surf.height) - Cairo.set_source_surface(screen.context, surf, 0, 0) - p = Cairo.get_source(screen.context) - Cairo.pattern_set_extend(p, Cairo.EXTEND_PAD) # avoid blurry edges - Cairo.pattern_set_filter(p, Cairo.FILTER_NEAREST) - Cairo.fill(screen.context) - Cairo.restore(screen.context) + surf = CairoMakie.Cairo.CairoARGBSurface(CairoMakie.to_uint32_color.(img)) + + CairoMakie.Cairo.rectangle(screen.context, 0, 0, w, h) + CairoMakie.Cairo.save(screen.context) + CairoMakie.Cairo.scale(screen.context, w / surf.width, h / surf.height) + CairoMakie.Cairo.set_source_surface(screen.context, surf, 0, 0) + p = CairoMakie.Cairo.get_source(screen.context) + CairoMakie.Cairo.pattern_set_extend(p, CairoMakie.Cairo.EXTEND_PAD) # avoid blurry edges + CairoMakie.Cairo.pattern_set_filter(p, CairoMakie.Cairo.FILTER_NEAREST) + CairoMakie.Cairo.fill(screen.context) + CairoMakie.Cairo.restore(screen.context) return From 138c4d79ef7671ce997a9440a22f387f4020bbd3 Mon Sep 17 00:00:00 2001 From: Anshul Singhvi Date: Fri, 10 Jun 2022 11:45:52 +0200 Subject: [PATCH 5/7] Implement refactored rasterization pipeline --- CairoMakie/src/CairoMakie.jl | 1 + CairoMakie/src/glmakie_integration.jl | 116 +++++++++++++++++++------- CairoMakie/src/infrastructure.jl | 19 ++++- 3 files changed, 100 insertions(+), 36 deletions(-) diff --git a/CairoMakie/src/CairoMakie.jl b/CairoMakie/src/CairoMakie.jl index 742e1f1219f..88a3996f312 100644 --- a/CairoMakie/src/CairoMakie.jl +++ b/CairoMakie/src/CairoMakie.jl @@ -34,6 +34,7 @@ include("utils.jl") include("fonts.jl") include("primitives.jl") include("overrides.jl") +include("glmakie_integration.jl") function __init__() activate!() diff --git a/CairoMakie/src/glmakie_integration.jl b/CairoMakie/src/glmakie_integration.jl index fca6ac94136..116338be21a 100644 --- a/CairoMakie/src/glmakie_integration.jl +++ b/CairoMakie/src/glmakie_integration.jl @@ -1,19 +1,29 @@ # Upstreamable code # TODO: make this function more efficient! -function alpha_colorbuffer(scene) - GLMakie.activate!() - display(scene) - bg = scene.backgroundcolor[] - scene.backgroundcolor[] = RGBAf(0, 0, 0, 1) - b1 = copy(Makie.colorbuffer(scene)) - scene.backgroundcolor[] = RGBAf(1, 1, 1, 1) - b2 = Makie.colorbuffer(scene) - CairoMakie.activate!() - scene.backgroundcolor[] = bg - return map(infer_alphacolor, b1, b2) +function alpha_colorbuffer(Backend, scene::Scene) + img = try + Backend.activate!() + display(scene) + bg = scene.backgroundcolor[] + scene.backgroundcolor[] = RGBAf(0, 0, 0, 1) + b1 = copy(Makie.colorbuffer(scene)) + scene.backgroundcolor[] = RGBAf(1, 1, 1, 1) + b2 = Makie.colorbuffer(scene) + scene.backgroundcolor[] = bg + map(infer_alphacolor, b1, b2) + catch e + println("Error: something failed in alpha colorbuffer!") + rethrow(e) + finally + CairoMakie.activate!() + end + + return img end +alpha_colorbuffer(scene::Scene) + function infer_alphacolor(rgb1, rgb2) rgb1 == rgb2 && return RGBAf(rgb1.r, rgb1.g, rgb1.b, 1) @@ -31,8 +41,10 @@ function create_render_scene(plot::Combined; scale::Real = 1) w, h = Int.(scene.px_area[].widths) # We create a dummy scene to render to, which inherits its parent's - # transformation and camera - + # transformation and camera. + # WARNING: lights, SSAO and axis do not update when the original Scene's + # attributes do. This is because they are stored as fields in the Scene + # struct, not as attributes. render_scene = Makie.Scene( camera = Makie.camera(scene), lights = scene.lights, @@ -59,26 +71,31 @@ function create_render_scene(plot::Combined; scale::Real = 1) return render_scene end -function plot2img(plot::Combined; scale::Real = 1, use_backgroundcolor = false) + +""" + plot2img(backend::Makie.AbstractBackend, plot::Combined; scale::Real = 1, use_backgroundcolor = false) +""" +function plot2img(Backend, plot::Combined; scale::Real = 1, use_backgroundcolor = false) parent = Makie.parent_scene(plot) + # obtain or create the render scene render_scene = if haskey(parent.theme, :_render_scenes) && haskey(parent.theme._render_scenes.val, plot) - parent.theme._render_scenes.val[plot] + @show keys(parent.theme._render_scenes[]) + parent.theme._render_scenes[][plot] else # we have to create a render scene println("Rerendering") scene = create_render_scene(plot; scale = scale) # set up cache rs_dict = get!(parent.theme.attributes, :_render_scenes, Dict{Union{Makie.AbstractPlot, Makie.Combined}, Makie.Scene}()) - rs_dict.val[plot] = scene + rs_dict[][plot] = scene scene end - img = if use_backgroundcolor Makie.colorbuffer(render_scene) else # render with transparency, using the alpha-colorbuffer hack - alpha_colorbuffer(render_scene) + alpha_colorbuffer(Backend, render_scene) end return img @@ -102,25 +119,60 @@ purge_render_cache!(fig::Figure) = purge_render_cache!(fig.scene) # This retrieval -function CairoMakie.draw_atomic(scene::Scene, screen::CairoMakie.CairoScreen, primitive::Volume, scale::Real = 10) +function CairoMakie.draw_atomic(scene::Scene, screen::CairoMakie.CairoScreen, primitive::Volume) + draw_plot_as_image_with_backend(GLMakie, scene, screen, primitive) +end + + +function draw_plot_as_image_with_backend(Backend, scene::Scene, screen::CairoScreen, plot) + + w, h = Int.(scene.px_area[].widths) + img = plot2img(Backend, primitive; scale = scale, use_backgroundcolor = false) + Makie.save("hi.png", img) # cache for debugging + + surf = Cairo.CairoARGBSurface(to_uint32_color.(img)) + + Cairo.rectangle(screen.context, 0, 0, w, h) + Cairo.save(screen.context) + Cairo.scale(screen.context, w / surf.width, h / surf.height) + Cairo.set_source_surface(screen.context, surf, 0, 0) + p = Cairo.get_source(screen.context) + Cairo.pattern_set_extend(p, Cairo.EXTEND_PAD) # avoid blurry edges + Cairo.pattern_set_filter(p, Cairo.FILTER_NEAREST) + Cairo.fill(screen.context) + Cairo.restore(screen.context) + + return + +end + +function draw_scene_as_image(Backend, scene::Scene, screen::CairoScreen; scale = 1) w, h = Int.(scene.px_area[].widths) - img = plot2img(primitive; scale = scale, use_backgroundcolor = false) - Makie.save("hi.png", img) # cache for debugging + render_scene = create_render_scene(scene.plots[begin]; scale = scale) + if length(scene.plots) > 1 + push!.(Ref(render_scene), scene.plots[(begin+1):end]) + end - surf = CairoMakie.Cairo.CairoARGBSurface(CairoMakie.to_uint32_color.(img)) + img = if Makie.to_color(scene.backgroundcolor[]) == RGBAf(0,0,0,0) + alpha_colorbuffer(Backend, scene) + else + Makie.colorbuffer(scene) + end - CairoMakie.Cairo.rectangle(screen.context, 0, 0, w, h) - CairoMakie.Cairo.save(screen.context) - CairoMakie.Cairo.scale(screen.context, w / surf.width, h / surf.height) - CairoMakie.Cairo.set_source_surface(screen.context, surf, 0, 0) - p = CairoMakie.Cairo.get_source(screen.context) - CairoMakie.Cairo.pattern_set_extend(p, CairoMakie.Cairo.EXTEND_PAD) # avoid blurry edges - CairoMakie.Cairo.pattern_set_filter(p, CairoMakie.Cairo.FILTER_NEAREST) - CairoMakie.Cairo.fill(screen.context) - CairoMakie.Cairo.restore(screen.context) + Makie.save("hi.png", img) # cache for debugging - return + surf = Cairo.CairoARGBSurface(to_uint32_color.(img)) + + Cairo.rectangle(screen.context, 0, 0, w, h) + Cairo.save(screen.context) + Cairo.scale(screen.context, w / surf.width, h / surf.height) + Cairo.set_source_surface(screen.context, surf, 0, 0) + p = Cairo.get_source(screen.context) + Cairo.pattern_set_extend(p, Cairo.EXTEND_PAD) # avoid blurry edges + Cairo.pattern_set_filter(p, Cairo.FILTER_NEAREST) + Cairo.fill(screen.context) + Cairo.restore(screen.context) end diff --git a/CairoMakie/src/infrastructure.jl b/CairoMakie/src/infrastructure.jl index 40bcb33847f..adb6d7ca480 100644 --- a/CairoMakie/src/infrastructure.jl +++ b/CairoMakie/src/infrastructure.jl @@ -186,9 +186,20 @@ function cairo_draw(screen::CairoScreen, scene::Scene) # rasterize it when plotting to vector backends, by using the `rasterize` # keyword argument. This can be set to a Bool or an Int which describes # the density of rasterization (in terms of a direct scaling factor.) - if to_value(get(p, :rasterize, false)) != false && should_rasterize - draw_plot_as_image(pparent, screen, p, p[:rasterize][]) - else # draw vector + # This can also be set to a `Module` (i.e., ) + if to_value(get(p, :rasterize, false)) != false + if p.rasterize[] isa Union{Bool, Int} && should_rasterize + draw_plot_as_image(pparent, screen, p, p[:rasterize][]) + elseif p.rasterize[] isa Union{<: Module, Tuple{<: Module, Int}} + backend = p.rasterize[] isa Module ? p.rasterize[] : p.rasterize[][1] + scale = p.rasterize[] isa Module ? 1 : p.rasterize[][2] + draw_plot_as_image_with_backend(backend, pparent, screen, p; scale = scale, use_backgroundcolor = false) + else # rasterization option was not recognized, or should_rasterize + # was false and backend was not selected. + draw_plot(pparent, screen, p) + end + else # draw vector, only if a parent plot has not been rasterized + draw_plot(pparent, screen, p) end Cairo.restore(screen.context) @@ -308,7 +319,7 @@ function clear(screen::CairoScreen) end ######################################### -# Backend interface to Makie # +# Backend interface to Makie # ######################################### function Makie.backend_display(x::CairoBackend, scene::Scene; kw...) From e2bd7f1a149372128fe71174125e749fc7e157ed Mon Sep 17 00:00:00 2001 From: Anshul Singhvi Date: Fri, 10 Jun 2022 11:46:02 +0200 Subject: [PATCH 6/7] Special overloads for GLMakie conversion --- GLMakie/src/GLAbstraction/GLUniforms.jl | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/GLMakie/src/GLAbstraction/GLUniforms.jl b/GLMakie/src/GLAbstraction/GLUniforms.jl index 487d2f268b6..36b0c921132 100644 --- a/GLMakie/src/GLAbstraction/GLUniforms.jl +++ b/GLMakie/src/GLAbstraction/GLUniforms.jl @@ -198,6 +198,11 @@ gl_convert(s::Vector{Matrix{T}}) where {T<:Colorant} = Texture(s) gl_convert(s::Nothing) = s +# special overloads for rasterization +gl_convert(s::Module) = nothing +gl_convert(s::Tuple{Module, Any}) = nothing + + isa_gl_struct(x::AbstractArray) = false isa_gl_struct(x::NATIVE_TYPES) = false isa_gl_struct(x::Colorant) = false From 8c8441cdd2df4aa20fe2d25b28568e01fc3fc869 Mon Sep 17 00:00:00 2001 From: Anshul Singhvi Date: Fri, 10 Jun 2022 18:07:43 +0200 Subject: [PATCH 7/7] Fix the methods and get them working --- CairoMakie/src/glmakie_integration.jl | 20 +++++++++----------- CairoMakie/src/infrastructure.jl | 4 ++-- 2 files changed, 11 insertions(+), 13 deletions(-) diff --git a/CairoMakie/src/glmakie_integration.jl b/CairoMakie/src/glmakie_integration.jl index 116338be21a..21df65f149b 100644 --- a/CairoMakie/src/glmakie_integration.jl +++ b/CairoMakie/src/glmakie_integration.jl @@ -22,9 +22,6 @@ function alpha_colorbuffer(Backend, scene::Scene) return img end -alpha_colorbuffer(scene::Scene) - - function infer_alphacolor(rgb1, rgb2) rgb1 == rgb2 && return RGBAf(rgb1.r, rgb1.g, rgb1.b, 1) c1 = Float64.((rgb1.r, rgb1.g, rgb1.b)) @@ -118,17 +115,18 @@ purge_render_cache!(fig::Figure) = purge_render_cache!(fig.scene) # which we set in the previous step to contain the scene. # This retrieval +function draw_plot_as_image_with_backend(Backend, scene::Scene, screen::CairoScreen, plot; scale = 1) -function CairoMakie.draw_atomic(scene::Scene, screen::CairoMakie.CairoScreen, primitive::Volume) - draw_plot_as_image_with_backend(GLMakie, scene, screen, primitive) -end - - -function draw_plot_as_image_with_backend(Backend, scene::Scene, screen::CairoScreen, plot) + # special case if CairoMakie is the backend, since + # we don't want an infinite loop. + if Backend == @__MODULE__ + draw_plot_as_image(scene, screen, plot, scale) + return + end w, h = Int.(scene.px_area[].widths) - img = plot2img(Backend, primitive; scale = scale, use_backgroundcolor = false) + img = plot2img(Backend, plot; scale = scale, use_backgroundcolor = false) Makie.save("hi.png", img) # cache for debugging surf = Cairo.CairoARGBSurface(to_uint32_color.(img)) @@ -155,7 +153,7 @@ function draw_scene_as_image(Backend, scene::Scene, screen::CairoScreen; scale = push!.(Ref(render_scene), scene.plots[(begin+1):end]) end - img = if Makie.to_color(scene.backgroundcolor[]) == RGBAf(0,0,0,0) + img = if RGBAf(Makie.to_color(scene.backgroundcolor[])) == RGBAf(0,0,0,0) alpha_colorbuffer(Backend, scene) else Makie.colorbuffer(scene) diff --git a/CairoMakie/src/infrastructure.jl b/CairoMakie/src/infrastructure.jl index adb6d7ca480..db54aee4ad5 100644 --- a/CairoMakie/src/infrastructure.jl +++ b/CairoMakie/src/infrastructure.jl @@ -193,7 +193,7 @@ function cairo_draw(screen::CairoScreen, scene::Scene) elseif p.rasterize[] isa Union{<: Module, Tuple{<: Module, Int}} backend = p.rasterize[] isa Module ? p.rasterize[] : p.rasterize[][1] scale = p.rasterize[] isa Module ? 1 : p.rasterize[][2] - draw_plot_as_image_with_backend(backend, pparent, screen, p; scale = scale, use_backgroundcolor = false) + draw_plot_as_image_with_backend(backend, pparent, screen, p; scale = scale) else # rasterization option was not recognized, or should_rasterize # was false and backend was not selected. draw_plot(pparent, screen, p) @@ -401,7 +401,7 @@ function Makie.backend_show(x::CairoBackend, io::IO, ::MIME"image/png", scene::S # while relative line and font sizes are unaffected px_per_unit = get(io, :px_per_unit, x.px_per_unit) antialias = get(io, :antialias, x.antialias) - + # create an ARGB surface, to speed up drawing ops. screen = CairoScreen(scene; device_scaling_factor = px_per_unit, antialias = antialias) cairo_draw(screen, scene)