362 Commits

Author SHA1 Message Date
Elias Naur 526db27c75 gpu: fix opacity layer rendering on OpenGL
Fixes: https://todo.sr.ht/~eliasnaur/gio/536
Signed-off-by: Elias Naur <mail@eliasnaur.com>
2023-10-02 13:07:15 -05:00
Elias Naur ae3bd2a1e1 op/paint: add opacity operation
The new paint.PushOpacity allows for adjusting the opacity of a group
of drawing operations.

Signed-off-by: Elias Naur <mail@eliasnaur.com>
2023-09-08 11:46:17 -05:00
Chris Waldon 43c47f0883 go.*,text,font{,/opentype},app,gpu,widget{,/material}: [API] load system fonts
This commit updates the text package to be able to load system fonts. As a consequence,
application authors may choose to provide no fonts manually, and it's
also possible that the system provides none (WASM, for instance, currently provides no
system fonts). As such, the text stack needed some minor tweaks to handle this case by
displaying blank spaces where text should be rather than crashing when no faces are
available.

Internally, we are dropping the old method of choosing faces and instead relying solely
on the new font matching logic in go-text. I chose to do this because maintaining two
different sets of logic with a hierarchical relationship proved to be really complex,
and also the go-text logic seems to produce higher-quality choices.

The breaking API change from this commit is the new way of constructing a text shaper
using text.ShaperOptions. Providing no options will result in a shaper that uses solely
system fonts. The various options can be used to disable system font loading and to
provide an already-parsed collection of fonts as per Gio's old API.

The material.NewTheme function now accepts no arguments instead of a font collection.
Users wanting to provide a collection can simply provide a new shaper configured how
they would like:

    theme := material.NewTheme()
	theme.Shaper = text.NewShaper(text.NoSystemFonts(), text.WithCollection(gofont.Regular()))

This commit touches many packages to fix up their construction of text shapers, mostly in
test code. The changes to the tests in package widget deserve special note:
Changing our font resolution logic caused the tofu characters within the
test strings to use a different font's tofu. This isn't a problem, but shifted
the layout of the shaped text a little bit. I've updated the numbers to expect
the new glyph positions.

Fixes: https://todo.sr.ht/~eliasnaur/gio/309
Fixes: https://todo.sr.ht/~eliasnaur/gio/184
Signed-off-by: Chris Waldon <christopher.waldon.dev@gmail.com>
2023-07-19 10:01:51 +02:00
Dominik Honnef b183774063 internal/stroke, gpu: reuse slice when splitting cubics
When building GPU vertices from paths, we call stroke.SplitCubic once
per OpCubic. Before this change, each call to stroke.SplitCubic would
allocate a slice, which we would only use to iterate over.

This allocation can be easily avoided by reusing the slice. We can
conveniently store it in gpu.quadSplitter.

In a real application that renders hundreds of paths with tens of
rounded rectangles per path, this saved roughly 4500 allocations (or 1
MB worth) per frame.

Signed-off-by: Dominik Honnef <dominik@honnef.co>
2023-06-19 16:19:07 +02:00
Serhat Sevki Dincer 39b1158410 app,gpu{,/headless,/internal/rendertest}: replace io/ioutil with io & os
Signed-off-by: Serhat Sevki Dincer <jfcgauss@gmail.com>
2023-03-23 16:37:46 -06:00
Elias Naur 32c6a9b10d gpu/internal/rendertest: add issue references to broken tests
References: https://todo.sr.ht/~eliasnaur/gio/479
Signed-off-by: Elias Naur <mail@eliasnaur.com>
2023-02-06 12:08:04 -06:00
Egon Elbre bce4153640 internal/stroke: fix line overlap
When the line overlaps itself backtracking exactly, e.g.

   path.MoveTo(0, 100)
   path.LineTo(100, 0)
   path.LineTo(0, 100)

then acos calculation is relatively unstable. By using atan2 it avoids
some of such problems in the calculation. Additionally, it simpliflies
the round join calculation.

Fixes: https://todo.sr.ht/~eliasnaur/gio/474
Signed-off-by: Egon Elbre <egonelbre@gmail.com>
2023-02-06 12:01:52 -06:00
Egon Elbre 14a33f3cb7 gpu/internal/rendertest: fix alphaClose check
Apparently, alphaClose has been overflowing and giving the wrong answer
for a while and hence some of the tests are broken. I currently disabled
those tests, because I'm not quite sure where and how they broke.

Also, bumped alpha tolerance to 8, to ignore false positives.

Signed-off-by: Egon Elbre <egonelbre@gmail.com>
2023-02-06 11:51:02 -06:00
Elias Naur af7afea5a3 gpu: don't allocate null materials buffer when running on the CPU
References: https://todo.sr.ht/~eliasnaur/gio/469
Signed-off-by: Elias Naur <mail@eliasnaur.com>
2023-01-14 09:59:55 -06:00
Elias Naur f8221bb2ab gpu/internal/opengl: don't query FRAMEBUFFER_ATTACHMENT_COLOR_ENCODING on GLES2
It's not supported in OpenGL ES 2.

References: https://todo.sr.ht/~eliasnaur/gio/469
Signed-off-by: Elias Naur <mail@eliasnaur.com>
2023-01-06 18:51:20 -06:00
Elias Naur 1a84517b12 gpu/internal/opengl: avoid UNPACK_ROW_LENGTH/PACK_ROW_LENGTH on GLES2
Similarly to WebGL1, they're not supported in OpenGL ES 2.0.

References: https://todo.sr.ht/~eliasnaur/gio/469
Signed-off-by: Elias Naur <mail@eliasnaur.com>
2023-01-06 18:40:22 -06:00
Egon Elbre 827e20d84d gpu: optimize pack.tryAdd
name       old time/op  new time/op  delta
Packer-32   559µs ± 2%   295µs ± 1%  -47.18%  (p=0.008 n=5+5)

Signed-off-by: Egon Elbre <egonelbre@gmail.com>
2023-01-06 18:26:42 -06:00
Egon Elbre 8bc6737dea gpu: optimize encodeQuadTo
name             old time/op  new time/op  delta
EncodeQuadTo-32  35.4ns ± 1%  11.9ns ± 3%  -66.34%  (p=0.008 n=5+5)

Signed-off-by: Egon Elbre <egonelbre@gmail.com>
2023-01-06 18:26:38 -06:00
Elias Naur bebc73db37 gpu: implement automatic mipmaps for images
All GPU APIs except OpenGL ES 2 can generate mipmaps for textures.
This trades 33% more GPU memory use for improved rendering quality
and speed for downscaled images.

Signed-off-by: Elias Naur <mail@eliasnaur.com>
2022-10-22 16:47:00 -06:00
Chris Waldon dbf6429026 app,gpu/headless: [linux] make EGL the default backend
This commit switches the priority of EGL and Vulkan so that EGL is always
tried first. This is because our EGL backend performs significantly better
than the Vulkan one, and we want the most performant experience to be the
default.

Our hypothesis is that the EGL backend is benefitting from lots of optimization
within the OpenGL driver that Vulkan drivers expect applications to perform
themselves. Rather than invest a bunch of time optimizing the Vulkan backend
right now, this change lets us focus on other priorities.

Signed-off-by: Chris Waldon <christopher.waldon.dev@gmail.com>
2022-09-08 11:41:35 +02:00
Elias Naur 61b2e37691 all: format comments with go fmt ./... using Go 1.19
Signed-off-by: Elias Naur <mail@eliasnaur.com>
2022-08-06 12:26:03 +02:00
Elias Naur 5326ca5fbe all: add support for macOS to flake.nix
The Nix version of the macOS toolchain has difficulties compiling
Objective-C modules; disable modules instead of figuring out why.
It also doesn't include any frameworks automatically; add them explicitly.

While here, move suppression of OpenGL deprecation to a GL-specific
file.

Signed-off-by: Elias Naur <mail@eliasnaur.com>
2022-08-01 01:33:21 +02:00
Egon Elbre 0777afb85f gpu: avoid bounds checks in decode
Signed-off-by: Egon Elbre <egonelbre@gmail.com>
2022-07-07 09:36:26 +02:00
Egon Elbre 6bf5d4dc2d gpu: optimize resourceCache
By keeping all the information in a single map, we avoid multiple
lookups and can switch between frames more easily.

Before ~35us, after ~20us for adding 50 new+old and switching
the frame.

Signed-off-by: Egon Elbre <egonelbre@gmail.com>
2022-07-02 20:12:41 +02:00
Egon Elbre 9de13e37e9 f32: add Affine2D.Split
splitTransform func was creating multiple copies of f32.Affine2D
due to not having access to the internal and passing around non-pointer.

This makes splitTransform from ~8ns to .Split ~2ns.

It seems that the non-pointer receiver was ~0.6ns slower in this case.

Signed-off-by: Egon Elbre <egonelbre@gmail.com>
2022-07-02 07:04:47 +02:00
Elias Naur 3ca4c98596 gpu/headless: tweak test to pass on MacBook Pro M1
Apparently, there is a rounding error somewhere in the pipeline
from clearing a FBO to downloading its contents on at least one Apple
M1 machine. Tweak the test colors a bit to make it pass.

Signed-off-by: Elias Naur <mail@eliasnaur.com>
2022-06-23 19:14:53 +02:00
Elias Naur 5ff316ed6d gpu: re-align coverUniforms struct
Direct3D requires GPU vertex attribute structs sizes be a multiple
16. A cleanup commit removed an unusued field, and broke that
assumption.

Fixes: https://todo.sr.ht/~eliasnaur/gio/422
Signed-off-by: Elias Naur <mail@eliasnaur.com>
2022-06-15 11:30:05 +02:00
Elias Naur 916efb4612 all: apply suggestions from staticcheck.io
Signed-off-by: Elias Naur <mail@eliasnaur.com>
2022-06-07 12:28:28 +02:00
Elias Naur b5f12c5f26 f32: [API] unexport Rectangle
There are no public API that uses f32.Rectangle anymore. Move Rectangle
to an internal package for internal use.

Signed-off-by: Elias Naur <mail@eliasnaur.com>
2022-05-31 10:24:09 +02:00
Elias Naur fc79ec5c94 layout: [API] remove FRect
We're about to unexport f32.Rectangle, this change removes the only
public API for it.

Signed-off-by: Elias Naur <mail@eliasnaur.com>
2022-05-31 10:24:09 +02:00
Elias Naur 48a8540a68 all: [API] change clip.RRect and UniformRRect to take integer coordinates
Like the change to op.Offset before this, clip.RRect and UniformRRect
is usually used with integer coordinates. Change to integer coordinates
to eliminate many useless conversions to float32.

Signed-off-by: Elias Naur <mail@eliasnaur.com>
2022-05-31 10:24:09 +02:00
Elias Naur a63e0cb44a all: [API] change op.Offset to take integer coordinates
op.Offset is a convenience function most often used by layouts. Layouts
usually operate in integer coordinates, and the float32 version of op.Offset
needlessly force conversions from int to float32. This change makes op.Offset
take integer coordinates, to better match its intended use.

Signed-off-by: Elias Naur <mail@eliasnaur.com>
2022-05-31 10:24:09 +02:00
Elias Naur 6a14269682 gpu/internal/opengl: add fallback for sparse OpenGL ES 2.0 ReadPixels
OpenGL ES 2.0 doesn't support GL_PACK_ROW_LENGTH, so this change implements
a fallback using a temporary buffer.

Signed-off-by: Elias Naur <mail@eliasnaur.com>
2022-04-13 17:28:36 +02:00
Elias Naur 6e66203881 gpu/headless: return error if NewTexture fails
Signed-off-by: Elias Naur <mail@eliasnaur.com>
2022-04-13 12:58:33 +02:00
Elias Naur 1f11a5a17b f32,gpu,op/clip: add f32.Rectangle method for converting to image.Rectangle
Creating an image.Rectangle from a f32.Rectangle is used by two packages in Gio
and about to be used for a third. Add a Round method to f32.Rectangle to avoid
duplicating the implementation.

Signed-off-by: Elias Naur <mail@eliasnaur.com>
2022-03-30 22:20:27 +02:00
Inkeliz eec78223d1 gpu: prevent texture to be larger than MaxTextureSize
Change eeb2febfea added extra space to FBO
sizes to avoid re-creating them often. However, the size could end up higher
than the GPU supports. This change caps the size.

Fixes: https://todo.sr.ht/~eliasnaur/gio/380
Signed-off-by: Inkeliz <inkeliz@inkeliz.com>
2022-03-16 15:52:35 +01:00
Egon Elbre 3fd231367f internal/stroke: optimize arc drawing
Arc with a small angle doesn't need many segments.

Updates: https://todo.sr.ht/~eliasnaur/gio/313
Signed-off-by: Egon Elbre <egonelbre@gmail.com>
2022-03-14 11:42:59 +01:00
Elias Naur eeb2febfea gpu: minimize FBO resizes in current renderer
References: https://todo.sr.ht/~eliasnaur/gio/375
Signed-off-by: Elias Naur <mail@eliasnaur.com>
2022-03-11 20:34:49 +01:00
Elias Naur 7785310e9d gpu/internal/vulkan,internal/vk: allocate descriptor sets in batches
We know exactly the configuration and number of sets in each pool, so
we may as well allocate them al up front.

References: https://todo.sr.ht/~eliasnaur/gio/375
Signed-off-by: Elias Naur <mail@eliasnaur.com>
2022-03-11 20:34:49 +01:00
Elias Naur e8aa881d40 gpu/internal/vulkan: [Vulkan] replace Device/QueueWaitIdle with fences
vkDeviceWaitIdle and vkQueueWaitIdle are expensive; a vkFence is cheaper
and the usual way to ensure a previous frame has completed before starting
another.

References: https://todo.sr.ht/~eliasnaur/gio/375
Signed-off-by: Elias Naur <mail@eliasnaur.com>
2022-03-11 20:34:49 +01:00
Elias Naur c244b7c349 gpu/internal/vulkan: resize descriptor set pool correctly
Before this change, the resized descriptor set pool would never
increase, defeating the purpose of re-using pools.

References: https://todo.sr.ht/~eliasnaur/gio/375
Signed-off-by: Elias Naur <mail@eliasnaur.com>
2022-03-11 20:34:49 +01:00
Elias Naur f19b16fecc gpu,app: don't call time.Now when not profiling
runtime.nanotime1 shows up in profiles on Android, so avoid calling
time.Now when we can.

Signed-off-by: Elias Naur <mail@eliasnaur.com>
2022-03-11 20:34:49 +01:00
Elias Naur fea2f888bb all: replace unsafe slice operations with unsafe.Slice
Signed-off-by: Elias Naur <mail@eliasnaur.com>
2022-03-02 16:53:02 +01:00
Elias Naur ce116abded op/clip: specify path for stroked Rects
Fixes: https://todo.sr.ht/~eliasnaur/gio/358
Signed-off-by: Elias Naur <mail@eliasnaur.com>
2022-02-10 19:01:08 +01:00
Elias Naur 242045f662 gpu/internal/opengl: ensure Backend.queryState is idempotent
Backend.queryState sets GL_ACTIVE_TEXTURE to access texture bindings; this change
makes sure GL_ACTIVE_TEXTURE is restored.

Also, make sure GL state changes made in Backend.restoreState is reflected
in Backend.glstate. Backend.glstate is not used after restoreState, so this
is just for clarity.

This is an alternative to https://github.com/gioui/gio/pull/77.

Signed-off-by: Elias Naur <mail@eliasnaur.com>
2022-02-10 17:03:13 +01:00
Andy Balholm 44c9fb7448 internal/stroke: don't draw round join if angle is NaN
References: https://todo.sr.ht/~eliasnaur/gio/331
Signed-off-by: Andy Balholm <andy@balholm.com>
2022-02-06 11:07:16 +01:00
Pierre Curto 2ce9ad36af clip: check Path ops are not mixed with others
Multiple operations Op, such as clip.Path, cannot
be interleaved with other ops. This patch adds a
mechanism to ensure that is the case by starting
multi ops with ops.BeginMulti and ending them with
ops.EndMulti while operations are written to op.Ops
with ops.WriteMulti.
This mechanism is applied to clip.Path.

Fixes: https://todo.sr.ht/~eliasnaur/gio/336
Signed-off-by: Pierre Curto <pierre.curto@gmail.com>
2022-01-15 19:16:07 +01:00
Andy Balholm 456f22c986 gpu/internal/rendertest: add regression test for issue 331
References: https://todo.sr.ht/~eliasnaur/gio/331
Signed-off-by: Andy Balholm <andy@balholm.com>
2022-01-12 11:34:37 +01:00
Elias Naur 86330faca6 gpu/internal/opengl: restore Android emulator OpenGL workaround
For some reason, the Android emulator OpenGL implementation needs the output
framebuffer current when eglSwapBuffers is called; otherwise sRGB emulation
breaks. We used to restore the default FBO explicitly, 9b5e9ae607
restored it implicitly through automatic state restoring, but 30ecf75a0f
broke that by only saving and restoring shared context state. This change
restores the explicit behaviour for non-shared contexts.

Signed-off-by: Elias Naur <mail@eliasnaur.com>
2022-01-08 14:50:14 +01:00
Elias Naur 8d8aeef66b app,gpu,internal/egl: lock OS thread when making OpenGL context current
OpenGL stores the current context in thread-local memory, but commit
4f5baa9a51 removed a runtime.LockOSThread from app.Window that ensured
the goroutine that drives the context stays on the operating thread that
has the context current. This change restores the thread lock.

As a bonus, this change makes the OpenGL contexts responsible for locking
the thread at MakeCurrent, thereby removing LockOSThread calls from GPU
backend-agnostic code.

Fixes: https://todo.sr.ht/~eliasnaur/gio/334
Signed-off-by: Elias Naur <mail@eliasnaur.com>
2022-01-05 11:49:29 +01:00
Pierre Curto 11bb86166a op/clip: automatically close Path in Outlines
Unclosed path segments in Path will be automatically
closed by a line.

Fixes: https://todo.sr.ht/~eliasnaur/gio/320
Signed-off-by: Pierre Curto <pierre.curto@gmail.com>
2021-12-21 10:13:20 +01:00
Elias Naur 533f85cf8e gpu/internal/vulkan: don't destroy nil vkDescriptorPool
Most drivers seem to tolerate vkDestroyDescriptorPool with a nil
pool, but NVIDIA's (rightly) doesn't. Fix that.

References: https://todo.sr.ht/~eliasnaur/gio/323
References: https://todo.sr.ht/~eliasnaur/gio/314
Signed-off-by: Elias Naur <mail@eliasnaur.com>
2021-12-19 14:19:37 +01:00
Pierre Curto af63c089f6 gpu/headless: make Screenshot take an input image to tranfer into
When extracting headless.Window's content via screenshots,
it can be useful to keep reusing the same image for output,
as well as specify which area of the Window is to be
extracted.
The updated Screenshot method does this by using the supplied
image.

API change: users must pass an existing image to Window.Screenshot.

Signed-off-by: Pierre Curto <pierre.curto@gmail.com>
2021-12-15 16:05:19 +01:00
Elias Naur 0c7b0b1d0b gpu,gpu/headless: plug a resource leak when taking screenshots
Ever since commit 8ff654628, the headless implementation has used two
GPU backend (not renderer) instances, one for the renderer and one for
creating the offscreen texture to render into. This arrangment leaks
resources because the backends only clear temporary storage at
BeginFrame, which is not called when reading pixel data from renders.

This change adds an internal constructor, gpu.NewWithDevice, to allow
headless.Window to share its device with the renderer, fixing the leak.
It also makes the code simpler (took me a while to debug this issue); in
fact I'm surprised it even works.

This is not a great fix: it adds an exported yet internal constructor,
and the ownership transfer of the device is surprising enough to warrant
two comments.

Fixes gio#322

Signed-off-by: Elias Naur <mail@eliasnaur.com>
2021-12-14 20:53:00 +01:00
Elias Naur cf9832ffcb gpu/internal/vulkan: support novulkan tag, FreeBSD
The novulkan tag didn't completely disable the Vulkan backend, and
the backend wouldn't register on FreeBSD. This change fixes both
issues.

Signed-off-by: Elias Naur <mail@eliasnaur.com>
2021-11-26 22:02:41 +01:00