Commit Graph

76 Commits

Author SHA1 Message Date
Sebastien Binet e71bf13c9a gpu,op/clip: implement dashed stroked paths
Signed-off-by: Sebastien Binet <s@sbinet.org>
2020-12-09 11:23:13 +01:00
Sebastien Binet be89f8b945 all: introduce Outline and Stroke builders
This CL introduces 2 new path builders:
- Outline which takes a PathSpec to be outlined
- Stroke which takes a PathSpec and a stroke style, to stroke a path.

typically, code like this:

  var p clip.Path
  ...
  p.Outline().Add(o)

should be replaced with:

  var p clip.Path
  ...
  clip.Outline{Path: p.End()}.Op().Add(o)

similarly, stroking should be modified from:

  var p clip.Path
  ...
  p.Stroke(width, clip.StrokeStyle{...}).Add(o)

to:

  var p clip.Path
  ...
  clip.Stroke{Path: p.End(), Style: clip.StrokeStyle{Width:...}}.Op().Add(o)

here are tentative 'rf' scripts (see rsc.io/rf for more details):

  ```
  ex {
  	import "gioui.org/op";
  	import "gioui.org/op/clip";

  	var p clip.Path;
  	var o *op.Ops;

  	p.Outline().Add(o) -> clip.Outline{Path:p.End()}.Op().Add(o);
  }

  ex {
  	import "gioui.org/op";
  	import "gioui.org/op/clip";

  	var o *op.Ops;
  	var p clip.Path;
  	var sty clip.StrokeStyle;
  	var width float32;

  	p.Stroke(width, sty).Add(o) ->   \
	    clip.Stroke{                 \
		Path:p.End(),            \
		Style: clip.StrokeStyle{ \
		    Width: width,        \
	    }}.Op().Add(o);
  }
  ```

Signed-off-by: Sebastien Binet <s@sbinet.org>
2020-12-09 09:44:15 +01:00
Elias Naur 951d45a2c6 op/clip: fix incirrent mention of even-odd fill rule
It's the non-zero winding rule.

Noticed by Richard Wilkes.

Signed-off-by: Elias Naur <mail@eliasnaur.com>
2020-12-03 10:03:51 +01:00
Egon Elbre 7bad76ad75 op/clip: handle zero Cube
Currently this comes up with RRect/Border that has zero corners. It
improves them from ~250ns to ~170ns. While it's possible to check this
in RRect implementation, however it'll slow down calls with non-zero
corners.

Signed-off-by: Egon Elbre <egonelbre@gmail.com>
2020-11-24 14:38:14 +01:00
Egon Elbre 918a5da308 Many operations do not pass refs to Write. Similarly adding
...interface{} requires constructing a slice, which is slow.

This cuts about 100ns from RRect and Border benchmark.

Signed-off-by: Egon Elbre <egonelbre@gmail.com>
2020-11-23 23:43:17 +01:00
Egon Elbre 1899104536 op/clip: expose LineTo and QuadTo
Using delta position with Line and Quad can drift over successive calls.
Also, in some cases it's much more convenient to use absolute
coordinates rather than relative.

Signed-off-by: Egon Elbre <egonelbre@gmail.com>
2020-11-21 22:46:03 +01:00
Egon Elbre 21ef492cc9 all: use color.NRGBA in public API
color.RGBA has two problems with regards to using it.

First the color values need to be premultiplied, whereas most APIs
have non-premultiplied values. This is mainly to preserve color components
with low alpha values.

Second there are two ways to premultiply with sRGB. One is to premultiply
after sRGB conversion, the other is before. This makes using the API more
confusing.

Using color.NRGBA in sRGB makes it align with CSS.e

Signed-off-by: Egon Elbre <egonelbre@gmail.com>
2020-11-19 11:30:11 +01:00
Elias Naur 23fa3480c6 op/paint: update documentation to match the removal of PaintOp.Rect
Signed-off-by: Elias Naur <mail@eliasnaur.com>
2020-11-16 12:54:58 +01:00
Sebastien Binet 7eb32360e5 gpu,op/clip: implement stroked paths with miter joins
Signed-off-by: Sebastien Binet <s@sbinet.org>
2020-11-11 16:14:49 +01:00
Sebastien Binet 700cec440e gpu,op/clip: implement stroked paths with round joins
Signed-off-by: Sebastien Binet <s@sbinet.org>
2020-11-11 14:03:30 +01:00
Sebastien Binet 1106d90f11 gpu,op/clip: implement stroked paths with round caps
Signed-off-by: Sebastien Binet <s@sbinet.org>
2020-11-11 12:25:24 +01:00
Sebastien Binet ae256b5be8 op/clip: use stroked path to draw Border
Signed-off-by: Sebastien Binet <s@sbinet.org>
2020-11-10 16:41:14 +01:00
Sebastien Binet 33c5fb63db gpu,op/clip: implement stroked paths
Flat and Square caps are implemented.
Bevel joins are implemented.

Round caps, Round joins and Miter joins are left for another PR.

Signed-off-by: Sebastien Binet <s@sbinet.org>
2020-11-10 15:58:10 +01:00
Sebastien Binet 936eb52b7e all: rename clip.Path.End into clip.Path.Outline
Signed-off-by: Sebastien Binet <s@sbinet.org>
2020-11-10 15:58:07 +01:00
Elias Naur 8b5c0d8b0c op/paint: document that color.RGBA values are in the sRGB color space
Updates gio#169

Signed-off-by: Elias Naur <mail@eliasnaur.com>
2020-11-08 19:31:38 +01:00
Elias Naur 94d242d18c op/paint: remove support for PaintOp.Rect
PaintOp.Rect is the wrong abstraction; it implies a clip operation
better handled by package clip, and not all paints need it (colors).
Furthermore, it's awkward to specify a PaintOp that fills up the
current clip area, regardless of its size.

Redefine PathOp to mean "fill current clip area".

API change. Replace uses of PaintOp.Rect with a TransformOp applied
before the PaintOp.

Leave a TODO for the PathOp infinity area.

Fixes gio#167

Signed-off-by: Elias Naur <mail@eliasnaur.com>
2020-11-05 16:32:19 +01:00
Elias Naur afb52194d1 app/headless,internal/rendertest: replace PaintOps with Fill/FillShape
We're about to remove PaintOp.Rect. Replacing PaintOps with Fill or
FillShape where possible will ease the transition.

Using Fill in tests exposed a problem with the infinity in paint.Fill.
Adjust it for now; it will be removed later.

Updates gio#167

Signed-off-by: Elias Naur <mail@eliasnaur.com>
2020-11-05 16:12:39 +01:00
Elias Naur 852958f4b5 gpu,widget,op/paint,gpu: remove support for ImageOp.Rect
This is effectively a revert of commit gioui.org/commit/69dfd2e3a5541.

ImageOp.Rect is the wrong abstraction; it implies a clipping operation that is
better handled by package clip.

API change. Uses of ImageOp.Rect should apply a clip.Rect before the PaintOp,
or use image.RGBA.SubImage (or similar).

Signed-off-by: Elias Naur <mail@eliasnaur.com>
2020-11-05 13:36:36 +01:00
Elias Naur efdd5ae602 op/paint: make the shape argument last in FillShape
FillShape is a generalization of Fill, so it seems more natural to
have the extra argument last.

Signed-off-by: Elias Naur <mail@eliasnaur.com>
2020-11-03 18:57:25 +01:00
Elias Naur 3107c95757 op/clip: remove unused parameter from Rect.Op
Signed-off-by: Elias Naur <mail@eliasnaur.com>
2020-11-03 18:57:25 +01:00
Egon Elbre f00f3a3359 gpu: add linear gradient
Signed-off-by: Egon Elbre <egonelbre@gmail.com>
2020-11-03 15:39:06 +01:00
Chris Waldon fc307c1e71 op/paint: add Fill helpers for painting an area with a solid color
Signed-off-by: Chris Waldon <christopher.waldon.dev@gmail.com>
2020-10-22 18:57:47 +02:00
Chris Waldon 685e0772a3 op/clip: add UniformRRect to create rects with uniform corner radii
Signed-off-by: Chris Waldon <christopher.waldon.dev@gmail.com>
2020-10-22 18:57:43 +02:00
Chris Waldon 8bdca84c56 op/clip: export Op method of clip shapes
This allows passing the resulting clip.Op types into functions that
draw shapes.

Signed-off-by: Chris Waldon <christopher.waldon.dev@gmail.com>
2020-10-22 18:57:40 +02:00
Sebastien Binet f2cd504a9f op/clip: smoother implementation of Arc for large angles
Signed-off-by: Sebastien Binet <s@sbinet.org>
2020-08-29 17:36:37 +02:00
Sebastien Binet 3fe0f62fa3 op/clip: export Path's current pen position
Signed-off-by: Sebastien Binet <s@sbinet.org>
2020-08-27 14:46:05 +02:00
Sebastien Binet 7054ba622a op/clip: implement arc path
Signed-off-by: Sebastien Binet <s@sbinet.org>
2020-08-27 14:46:02 +02:00
Elias Naur c76b42e486 op/clip,widget: add clip.Border and widget.Border
Signed-off-by: Elias Naur <mail@eliasnaur.com>
2020-07-09 19:55:37 +02:00
Elias Naur d572aa23ac op/clip: split Rect into pixel-aligned Rect and rounded RRect
The pixel-aligned Rect is more efficient and easier to use in the common case
of layout clipping.

Signed-off-by: Elias Naur <mail@eliasnaur.com>
2020-07-09 18:33:00 +02:00
Elias Naur 4818538ef8 op/clip: unexport Rect.Op
It wasn't used anywhere outside Rect.Add.

Signed-off-by: Elias Naur <mail@eliasnaur.com>
2020-07-09 17:29:31 +02:00
Elias Naur 878131189b all: remove redundant op.TransformOp.Offset
Use op.Offset instead, or create and manipulate a f32.Affine2D.

API change. Update your code with a gofmt rule:

	gofmt -r 'op.TransformOp{}.Offset -> op.Offset'

Signed-off-by: Elias Naur <mail@eliasnaur.com>
2020-06-21 22:41:56 +02:00
Viktor 24951a7ee7 gpu, op, internal/ops: add affine transformations
Add support for affine transformations. The key changes are outlined
below.

- Painting/clipping with rectangles is handled by, for complex
  transforms, creating clipping paths representing the transformed
  rectangle and using a larger bounding box. Cover/Blit shaders updated
  correspondingly to correctly map texture cordinates from the new
  bounding boxes.
- Since path splitting must happen on CPU the transforms must happen CPU
  side as well - offsets removed from shaders.
- Complex transforms will lead to different path splitting which means
  that GPU arrays can no longer be cached if the transform has changed.
  Thus the current transform is added as a key to the cache.
- Add a public API to op for setting Affine transformations.

There are a number of optimizations that could be explored further but
which are left out now:
- Caching also of CPU operations (e.g path splitting & transforms) and
  not only caching the GPU arrays.
- Allow for re-use of cached GPU vertices if the transformation change
  is a pure offset / scaling since the splitting is then the same.

Signed-off-by: Viktor <viktor.ogeman@gmail.com>
2020-06-21 11:17:53 +02:00
Viktor b247395c62 gpu, io/router, op: use f32.Affine2D instead of op.TransformOp for transforms
Encode TransformOp as an Affince2D matrix instead and use that in gpu and io transform handling.
There are no changes to user facing API and so far only the offset part of the matrix is used.

This patch is a step towards full affine transformations.

Signed-off-by: Viktor <viktor.ogeman@gmail.com>
2020-06-21 11:17:42 +02:00
Viktor 5b277757cf op/clip, gpu: split complex curves in package gpu instead
This is a first step towards supporting affine drawing transforms.
The rendering algorithm relies on quadratic curves that do not cross
x = 0 more than once, thus curves must be split after any rotation/shear
transforms. Move this logic and the generation of vertices to package gpu.
Also close all curves and draw zero-width edges as preparation for
transform since the will no longer implicitly be vertical with no
effect.

This commit will severely affect performance since vertexes are now
transformed also for cached items, using cpu resources.

Signed-off-by: Viktor <viktor.ogeman@gmail.com>
2020-06-21 11:17:27 +02:00
Elias Naur 1603a6f3ee op: add note that Ops.Reset invalidates recorded macros.
Signed-off-by: Elias Naur <mail@eliasnaur.com>
2020-06-17 09:59:15 +02:00
Elias Naur c19ed05342 op: change CallOp to be a return value from MacroOp.Stop
Converting

	macro := op.Record(ops)
	...
	macro.Stop()

	macro.Add()

to

	macro := op.Record(ops)
	...
	call := macro.Stop()

	call.Add(ops)

Which is more general (call.Add can take a different ops than the op.Record
that started it), and enforced the order between Stop and the subsequent Add.

Signed-off-by: Elias Naur <mail@eliasnaur.com>
2020-06-02 12:07:20 +02:00
Elias Naur acc23a5b3e op: make MacroOp methods take value receivers
The only mutable field is "recording", which is used for a sanity
check. THat check is performed (less generally) by Ops.macroStack.

Signed-off-by: Elias Naur <mail@eliasnaur.com>
2020-06-02 11:12:37 +02:00
Elias Naur 3e8c502550 op: return value StackOp from Push and make Pop use a value receiver
To match Record, we'd like Push to return a value. To do that and
support the one-line

	defer op.Push(ops).Pop()

Pop needs to use a value receiver as well. Drop the active field
and make it so. The field was only a sanity check, a check which is
already done by Ops.stackStack, albeit with a less specific panic.

Signed-off-by: Elias Naur <mail@eliasnaur.com>
2020-06-02 10:52:46 +02:00
Thomas Bruyelle ae8a377cda op: add op.Push and op.Record funcs
The funcs replace stack.Push and macro.Record, which become private.
This makes stack and macro faster to write, in particular for stacks
where you can just write the following line to save and restore the
state :

  defer op.Push(ops).Pop()

This usage requires Push to return a pointer (since Pop has a pointer
receiver), or else the code doesn't compile.

For consistancy, I tried to do the same for op.Record, but this implied
to turn all the MacroOp fields into pointers, and this caused some
panics. As a result, op.Record doesn't return a pointer.

An other side effect pointed by Larry Clapp: StackOp and MacroOp are not
re-usable any more, you have to allocate a new one for each usage, using
the described funcs above.

Signed-off-by: Thomas Bruyelle <thomas.bruyelle@gmail.com>
2020-06-02 10:39:56 +02:00
Elias Naur ac62e3a7ab op/clip: add Rect.Add shorthand
Signed-off-by: Elias Naur <mail@eliasnaur.com>
2020-05-20 10:21:11 +02:00
Elias Naur 013ea395b4 all: use new rectangle and point convenience functions
Signed-off-by: Elias Naur <mail@eliasnaur.com>
2020-05-19 11:03:30 +02:00
Elias Naur 31acd5451e op/paint: further clarify PaintOp documentation
Signed-off-by: Elias Naur <mail@eliasnaur.com>
2020-05-14 13:44:36 +02:00
Elias Naur 29c9b06dab op/paint: document ImageOp gotcha
Signed-off-by: Elias Naur <mail@eliasnaur.com>
2020-05-14 13:31:21 +02:00
Elias Naur b7ba809517 op/paint: rename material to brush
The "material" name clashes with the theme of the same name.

Signed-off-by: Elias Naur <mail@eliasnaur.com>
2020-05-14 13:19:15 +02:00
Elias Naur 81f474f5d7 op/paint,io/system: document ImageOp lifetime
Signed-off-by: Elias Naur <mail@eliasnaur.com>
2020-05-14 13:15:05 +02:00
Elias Naur af68e17dd3 op/paint: set subimage Rect when converting image
Signed-off-by: Elias Naur <mail@eliasnaur.com>
2020-03-12 18:42:20 +01:00
Elias Naur bfb50cef5d all: remove unused fields, functions and add missing error handling
Credit to staticcheck.io.

Signed-off-by: Elias Naur <mail@eliasnaur.com>
2020-02-27 22:26:54 +01:00
Elias Naur 591c89ab0a gpu: drop use of integer shader inputs
They're a pain to support. Encode the single integer value we have
as a float instead.

Signed-off-by: Elias Naur <mail@eliasnaur.com>
2020-02-27 21:22:59 +01:00
Elias Naur 69dfd2e3a5 op/paint: add support for efficient ImageOp subimages
The new field ImageOp.Rect is initialized to cover the entire source
image, but can be modified to draw only a section of it.

Signed-off-by: Elias Naur <mail@eliasnaur.com>
2020-02-13 13:15:32 +01:00
axel paulander 2e66c90aee op/paint: correct image loading
Corrected the use of draw.Draw in paint.NewImageOp as the old use only works for images starting at the origin

Signed-off-by: Axel Paulander <axel.paulander@gmail.com>
2020-01-10 17:52:33 +01:00