Skip to content

Commit cf529a3

Browse files
yell0wd0gannevk
authored andcommitted
Add an optional desynchronized mode to 2D canvas
Developers of stylus-based drawing applications (e.g., Google Keep) have found this hint to be critical in order to be competitive with native applications. It has been prototyped in Chromium and yields significant latency improvements there. Tests: will be added as part of https://bugs.chromium.org/p/chromium/issues/detail?id=788439. Supersedes #4234. Partial fix for #4087. Co-authored-by: Miguel Casas-Sanchez <mcasas@chromium.org>
1 parent e32a6f8 commit cf529a3

File tree

1 file changed

+64
-32
lines changed

1 file changed

+64
-32
lines changed

source

Lines changed: 64 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -60200,6 +60200,7 @@ enum <dfn>CanvasFillRule</dfn> { "<span data-x="dom-context-2d-fillRule-nonzero"
6020060200

6020160201
dictionary <dfn>CanvasRenderingContext2DSettings</dfn> {
6020260202
boolean <span data-x="dom-CanvasRenderingContext2DSettings-alpha">alpha</span> = true;
60203+
boolean <span data-x="dom-CanvasRenderingContext2DSettings-desynchronized">desynchronized</span> = false;
6020360204
};
6020460205

6020560206
enum <dfn>ImageSmoothingQuality</dfn> { "<span data-x="dom-context-2d-imageSmoothingQuality-low">low</span>", "<span data-x="dom-context-2d-imageSmoothingQuality-medium">medium</span>", "<span data-x="dom-context-2d-imageSmoothingQuality-high">high</span>" };
@@ -60452,36 +60453,38 @@ interface <dfn>Path2D</dfn> {
6045260453
objects.</p>
6045360454

6045460455
<dl class="domintro">
60455-
60456-
<dt><var>context</var> = <var>canvas</var> . <code data-x="dom-canvas-getContext">getContext</code>('2d' [, { [ <code data-x="dom-CanvasRenderingContext2DSettings-alpha">alpha</code>: false ] } ] )</dt>
60457-
60456+
<dt><var>context</var> = <var>canvas</var> . <code data-x="dom-canvas-getContext">getContext</code>('2d' [, { [ <code data-x="dom-CanvasRenderingContext2DSettings-alpha">alpha</code>: true ] [, <code data-x="dom-CanvasRenderingContext2DSettings-desynchronized">desynchronized</code>: false ] } ] )</dt>
6045860457
<dd>
60459-
6046060458
<p>Returns a <code>CanvasRenderingContext2D</code> object that is permanently bound to a
6046160459
particular <code>canvas</code> element.</p>
6046260460

60463-
<p>If the <code data-x="dom-CanvasRenderingContext2DSettings-alpha">alpha</code> setting is
60464-
provided and set to false, then the canvas is forced to always be opaque.</p>
60461+
<p>If the <code data-x="dom-CanvasRenderingContext2DSettings-alpha">alpha</code> member is
60462+
false, then the context is forced to always be opaque.</p>
6046560463

60464+
<p>If the <code
60465+
data-x="dom-CanvasRenderingContext2DSettings-desynchronized">desynchronized</code> member is
60466+
true, then the context might be <span
60467+
data-x="concept-canvas-desynchronized">desynchronized</span>.</p>
6046660468
</dd>
6046760469

6046860470
<dt><var>context</var> . <code subdfn data-x="dom-context-2d-canvas">canvas</code></dt>
60469-
6047060471
<dd>
60471-
6047260472
<p>Returns the <code>canvas</code> element.</p>
60473-
6047460473
</dd>
6047560474

6047660475
<dt><var>attributes</var> = <var>canvas</var> . <code data-x="dom-context-2d-canvas-getcontextattributes">getContextAttributes</code>()</dt>
60477-
6047860476
<dd>
60477+
<p>Returns an object whose:</p>
6047960478

60480-
<p>Returns an object whose <code data-x="concept-canvas-alpha">alpha</code> member is true if
60481-
the context has an alpha channel, or false if it was forced to be opaque.</p>
60479+
<ul>
60480+
<li><code data-x="concept-canvas-alpha">alpha</code> member is true if the context has an alpha
60481+
channel, or false if it was forced to be opaque.</li>
6048260482

60483+
<li><code data-x="dom-CanvasRenderingContext2DSettings-desynchronized">desynchronized</code>
60484+
member is true if the context can be <span
60485+
data-x="concept-canvas-desynchronized">desynchronized</span>.</li>
60486+
</ul>
6048360487
</dd>
60484-
6048560488
</dl>
6048660489

6048760490
<div w-nodev>
@@ -60497,12 +60500,11 @@ interface <dfn>Path2D</dfn> {
6049760500
<!--REMOVE-TOPIC:Security-->
6049860501

6049960502
<p>The <code>CanvasRenderingContext2D</code> object also has an <dfn
60500-
data-x="concept-canvas-alpha">alpha</dfn> flag, which can be set to true or false. Initially,
60501-
when the context is created, its <span data-x="concept-canvas-alpha">alpha</span> flag must be
60502-
set to true. When a <code>CanvasRenderingContext2D</code> object has its <span
60503-
data-x="concept-canvas-alpha">alpha</span> flag set to false, then its alpha channel must be
60504-
fixed to 1.0 (fully opaque) for all pixels, and attempts to change the alpha component of any
60505-
pixel must be silently ignored.</p>
60503+
data-x="concept-canvas-alpha">alpha</dfn> boolean. When a
60504+
<code>CanvasRenderingContext2D</code> object's <span
60505+
data-x="concept-canvas-alpha">alpha</span> is false, then its alpha channel must be fixed to 1.0
60506+
(fully opaque) for all pixels, and attempts to change the alpha component of any pixel must be
60507+
silently ignored.</p>
6050660508

6050760509
<p class="note">Thus, the bitmap of such a context starts off as <span>opaque black</span> instead
6050860510
of <span>transparent black</span>; <code data-x="dom-context-2d-clearRect">clearRect()</code>
@@ -60512,14 +60514,43 @@ interface <dfn>Path2D</dfn> {
6051260514
fourth byte in its input, and so on. However, the alpha component of styles and images drawn
6051360515
onto the canvas are still honoured up to the point where they would impact the <span>output
6051460516
bitmap</span>'s alpha channel; for instance, drawing a 50% transparent white square on a freshly
60515-
created <span>output bitmap</span> with its <span data-x="concept-canvas-alpha">alpha</span>
60516-
flag set to false will result in a fully-opaque gray square.</p>
60517+
created <span>output bitmap</span> with its <span data-x="concept-canvas-alpha">alpha</span> set
60518+
to false will result in a fully-opaque gray square.</p>
60519+
60520+
<p>The <code>CanvasRenderingContext2D</code> object also has a <dfn
60521+
data-x="concept-canvas-desynchronized">desynchronized</dfn> boolean. When a
60522+
<code>CanvasRenderingContext2D</code> object's <span
60523+
data-x="concept-canvas-desynchronized">desynchronized</span> is true, then the user agent may
60524+
optimize the rendering of the canvas to reduce the latency, as measured from input events to
60525+
rasterization, by desynchronizing the canvas paint cycle from the event loop, bypassing the
60526+
ordinary user agent rendering algorithm, or both. Insofar as this mode involves bypassing the
60527+
usual paint mechanisms, rasterization, or both, it might introduce visible tearing artifacts.</p>
60528+
60529+
<p class="note">The user agent usually renders on a buffer which is not being displayed, quickly
60530+
swapping it and the one being scanned out for presentation; the former buffer is called
60531+
back buffer and the latter <i>front buffer</i>. A popular technique for reducing latency is called
60532+
front buffer rendering, also known as <i>single buffer</i> rendering, where rendering happens in
60533+
parallel and racily with the scanning out process. This technique reduces the latency at the price
60534+
of potentially introducing tearing artifacts and can be used to implement in total or part of the
60535+
<span data-x="concept-canvas-desynchronized">desynchronized</span> boolean. <ref
60536+
spec=MULTIPLEBUFFERING></p>
60537+
60538+
<p class="note">The <span data-x="concept-canvas-desynchronized">desynchronized</span> boolean
60539+
can be useful when implementing certain kinds of applications, such as drawing applications,
60540+
where the latency between input and rasterization is critical.</p>
6051760541

6051860542
<p>The <dfn><code
6051960543
data-x="dom-context-2d-canvas-getcontextattributes">getContextAttributes()</code></dfn> method,
60520-
when invoked, must return a <code>CanvasRenderingContext2DSettings</code> dictionary whose <code
60521-
data-x="concept-canvas-alpha">alpha</code> member is set to true if this context's <span
60522-
data-x="concept-canvas-alpha">alpha</span> flag is set, and false otherwise.</p>
60544+
when invoked, must return a <code>CanvasRenderingContext2DSettings</code> dictionary containing
60545+
the following members:</p>
60546+
60547+
<ul>
60548+
<li><p><code data-x="concept-canvas-alpha">alpha</code>, set to this context's <span
60549+
data-x="concept-canvas-alpha">alpha</span>.</p></li>
60550+
60551+
<li><p><code data-x="concept-canvas-desynchronized">desynchronized</code>, set to this context's
60552+
<span data-x="concept-canvas-desynchronized">desynchronized</span>.</p></li>
60553+
</ul>
6052360554

6052460555
<hr>
6052560556

@@ -60575,15 +60606,13 @@ interface <dfn>Path2D</dfn> {
6057560606
data-x="attr-canvas-width">width</code> and <code data-x="attr-canvas-height">height</code>
6057660607
content attributes.</p></li>
6057760608

60578-
<li>
60579-
<p>Process each of the members of <var>settings</var> as follows:</p>
60609+
<li><p>Set <var>context</var>'s <span data-x="concept-canvas-alpha">alpha</span> to
60610+
<var>settings</var>'s <dfn><code
60611+
data-x="dom-CanvasRenderingContext2DSettings-alpha">alpha</code></dfn>.</p></li>
6058060612

60581-
<dl>
60582-
<dt><dfn><code data-x="dom-CanvasRenderingContext2DSettings-alpha">alpha</code></dfn></dt>
60583-
<dd>If false, then set <var>context</var>'s <span data-x="concept-canvas-alpha">alpha</span>
60584-
flag to false.</dd>
60585-
</dl>
60586-
</li>
60613+
<li><p>Set <var>context</var>'s <span
60614+
data-x="concept-canvas-desynchronized">desynchronized</span> to <var>settings</var>'s <dfn><code
60615+
data-x="dom-CanvasRenderingContext2DSettings-desynchronized">desynchronized</code></dfn>.</p></li>
6058760616

6058860617
<li><p>Return <var>context</var>.</p></li>
6058960618
</ol>
@@ -121910,6 +121939,9 @@ INSERT INTERFACES HERE
121910121939
<dt id="refsMQ">[MQ]</dt>
121911121940
<dd><cite><a href="https://drafts.csswg.org/mediaqueries/">Media Queries</a></cite>, H. Lie, T. &Ccedil;elik, D. Glazman, A. van Kesteren. W3C.</dd>
121912121941

121942+
<dt id="refsMULTIPLEBUFFERING">[MULTIPLEBUFFERING]</dt>
121943+
<dd>(Non-normative) <cite><a href="https://en.wikipedia.org/wiki/Multiple_buffering">Multiple buffering</a></cite>. Wikipedia.</dd>
121944+
121913121945
<dt id="refsNAVMODEL">[NAVMODEL]</dt>
121914121946
<dd><cite><a href="https://arxiv.org/abs/1608.05444">A Model of Navigation History</a></cite>. C. Brewster, A. Jeffrey.</dd>
121915121947

0 commit comments

Comments
 (0)