Repository: mozilla/pdf.js
File: web/pdf_page_view.js (JavaScript)

Code before:
        annotationEditorUIManager,
        onAppend: annotationLayerDiv => {
          this.#addLayer(annotationLayerDiv, "annotationLayer");
        },
      });
    }

    const renderContinueCallback = cont => {
      showCanvas?.(false);
      if (this.renderingQueue && !this.renderingQueue.isHighestPriority(this)) {
        this.renderingState = RenderingStates.PAUSED;
        this.resume = () => {
          this.renderingState = RenderingStates.RUNNING;
          cont();
        };
        return;
      }
      cont();
    };

    const { width, height } = viewport;
    const canvas = document.createElement("canvas");
    canvas.setAttribute("role", "presentation");

    // Keep the canvas hidden until the first draw callback, or until drawing
    // is complete when `!this.renderingQueue`, to prevent black flickering.
    const hasHCM = !!(pageColors?.background && pageColors?.foreground);
    const prevCanvas = this.canvas;
    if (!prevCanvas && !hasHCM) {
      canvasWrapper.append(canvas);
    }
    this.canvas = canvas;

    let showCanvas = isLastShow => {
      // In HCM, a final filter is applied on the canvas which means that
      // before it's applied we've normal colors. Consequently, to avoid to have
      // a final flash we just display it once all the drawing is done.
      if ((!prevCanvas && !hasHCM) || isLastShow) {
        if (prevCanvas) {
          prevCanvas.replaceWith(canvas);
          prevCanvas.width = prevCanvas.height = 0;
        } else if (hasHCM) {
          canvasWrapper.append(canvas);
        }
        showCanvas = null; // Only invoke the function once.
      }
    };

    const ctx = canvas.getContext("2d", {
      alpha: false,
      willReadFrequently: !this.#enableHWA,

Review comment (suggestion):
Unfortunately it seems that the new implementation doesn't prevent the "black flickering" that this comment talks about since scrolling *very quickly* through the viewer, using the default `tracemonkey.pdf`, will now trigger this effect intermittently.

Code after:
    }

    const renderContinueCallback = cont => {
      showCanvas?.(false);
      if (this.renderingQueue && !this.renderingQueue.isHighestPriority(this)) {
        this.renderingState = RenderingStates.PAUSED;
        this.resume = () => {
          this.renderingState = RenderingStates.RUNNING;
          cont();
        };
        return;
      }
      cont();
    };

    const { width, height } = viewport;
    const canvas = document.createElement("canvas");
    canvas.setAttribute("role", "presentation");

    const hasHCM = !!(pageColors?.background && pageColors?.foreground);
    const prevCanvas = this.canvas;
    const updateOnFirstShow = !prevCanvas && !hasHCM;
    this.canvas = canvas;
    this.#originalViewport = viewport;

    let showCanvas = isLastShow => {
      if (updateOnFirstShow) {
        // Don't add the canvas until the first draw callback, or until
        // drawing is complete when `!this.renderingQueue`, to prevent black
        // flickering.
        canvasWrapper.append(canvas);
        showCanvas = null;
        return;
      }
      if (!isLastShow) {
        return;
      }

      if (prevCanvas) {
        prevCanvas.replaceWith(canvas);
        prevCanvas.width = prevCanvas.height = 0;
      } else {
        // In HCM, a final filter is applied on the canvas which means that
        // before it's applied we've normal colors. Consequently, to avoid to
        // have a final flash we just display it once all the drawing is done.
        canvasWrapper.append(canvas);
      }

      showCanvas = null;
    };