Skip to content

Commit 59e032c

Browse files
committed
[css-paint-api] Removes invalidation based on size, and allows user-agents to cache.
Fixes #326. I realized that explicitly placing an invalidation based on size here was wrong, e.g. if a box changes size it has to run the "object size negotiation" algorithm, which makes invalidating the paint() function off size redundant. Additionally allows user-agents to cache results from previous invocations, and re-use. This allows UAs to re-use images between instances if they have the same input style, concrete object size, and input arguments.
1 parent 2a8e4f8 commit 59e032c

File tree

1 file changed

+52
-7
lines changed

1 file changed

+52
-7
lines changed

css-paint-api/Overview.bs

Lines changed: 52 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -84,10 +84,9 @@ Paint Invalidation {#paint-invalidation}
8484
Each <<paint()>> function for a box has an associated <dfn>paint valid flag</dfn>. It may be either
8585
<dfn>paint-valid</dfn> or <dfn>paint-invalid</dfn>. It is initially set to <a>paint-invalid</a>.
8686

87-
When the size (as determined by layout) of a |box| changes, each <<paint()>> function's <a>paint
88-
valid flag</a> should be set to <a>paint-invalid</a>.
89-
90-
When the computed style for a |box| changes, the user agent <em>must</em> run the following steps:
87+
<div algorithm>
88+
When the user agent wants to <dfn>invalidate paint functions</dfn> given |box|, the user agent
89+
<em>must</em> run the following steps:
9190
1. For each <<paint()>> function on the |box|, perform the following steps:
9291
1. Let |paintFunction| be the current <<paint()>> function on the |box|.
9392

@@ -104,10 +103,14 @@ When the computed style for a |box| changes, the user agent <em>must</em> run th
104103

105104
6. For each |property| in |inputProperties|, if the |property|'s <a>computed value</a> has
106105
changed, set the <a>paint valid flag</a> on the |paintFunction| to <a>paint-invalid</a>.
106+
</div>
107107

108108
Performing <a>draw a paint image</a> results in the <a>paint valid flag</a> for a <<paint()>>
109109
function on a box to be set to <a>paint-valid</a>.
110110

111+
<a>Invalidate paint functions</a> is expected to be run when the user agent recalculates computed
112+
style for a box.
113+
111114
Note: In a future version of the spec, support could be added for partial invalidation. The user
112115
agent will be able to specify a region of the rendering context which needs to be re-painted by
113116
the paint class.
@@ -614,12 +617,54 @@ When the user agent wants to <dfn>invoke a paint callback</dfn> given |name|, |i
614617
9. Let |paintSize| be a new {{PaintSize}} initialized to the width and height defined by
615618
|concreteObjectSize|.
616619

617-
10. Let |paintFunctionCallback| be |definition|'s <a>paint function</a>.
620+
10. At this stage the user agent may re-use an image from a previous invocation if |paintSize|,
621+
|styleMap|, |inputArguments| are equivalent to that previous invocation. If so let the image
622+
output be that cached image and abort all these steps.
623+
624+
<div class=note>
625+
In the example below, both <code>div-1</code> and <code>div-2</code> have paint
626+
functions which have equivalent javascript arguments. A user-agent can cache the result
627+
of one invocation and use it for both elements.
628+
629+
<pre class=lang-javascript>
630+
// paint.js
631+
registerPaint('simple', class {
632+
paint(ctx, size) {
633+
ctx.fillStyle = 'green';
634+
ctx.fillRect(0, 0, size.width, size.height);
635+
}
636+
});
637+
</pre>
638+
639+
<pre class=lang-markup>
640+
&lt;style>
641+
.div-1 {
642+
width: 50px;
643+
height: 50px;
644+
background-image: paint(simple);
645+
}
646+
.div-2 {
647+
width: 100px;
648+
height: 100px;
649+
650+
background-size: 50% 50%;
651+
background-image: paint(simple);
652+
}
653+
&lt;/style>
654+
&lt;div class=div-1>&lt;/div>
655+
&lt;div class=div-2>&lt;/div>
656+
&lt;script>
657+
CSS.paintWorklet.import('paint.js');
658+
&lt;/script>
659+
</pre>
660+
</div>
661+
662+
11. Let |paintFunctionCallback| be |definition|'s <a>paint function</a>.
618663

619-
11. <a>Invoke</a> |paintFunctionCallback| with arguments «|renderingContext|, |paintSize|,
664+
12. <a>Invoke</a> |paintFunctionCallback| with arguments «|renderingContext|, |paintSize|,
620665
|styleMap|, |inputArguments|», and with |paintInstance| as the <a>callback this value</a>.
621666

622-
12. The image output is to be produced from the |renderingContext| given to the method.
667+
13. The image output is to be produced from the |renderingContext| given to the method.
623668

624669
If an exception is <a>thrown</a> the let the image output be an <a>invalid image</a>.
625670

0 commit comments

Comments
 (0)