mike-jumper commented on a change in pull request #446: GUACAMOLE-884: Leverage 
createImageBitmap() for reading image data where supported.
URL: https://github.com/apache/guacamole-client/pull/446#discussion_r333141831
 
 

 ##########
 File path: guacamole-common-js/src/main/webapp/modules/Display.js
 ##########
 @@ -543,26 +549,97 @@ Guacamole.Display = function() {
      */
     this.drawBlob = function(layer, x, y, blob) {
 
-        // Create URL for blob
-        var url = URL.createObjectURL(blob);
+        var task;
 
-        // Draw and free blob URL when ready
-        var task = scheduleTask(function __display_drawBlob() {
+        // Prefer createImageBitmap() over blob URLs if available
+        if (window.createImageBitmap) {
 
-            // Draw the image only if it loaded without errors
-            if (image.width && image.height)
-                layer.drawImage(x, y, image);
+            var bitmap;
 
-            // Blob URL no longer needed
-            URL.revokeObjectURL(url);
+            // Draw image once loaded
+            task = scheduleTask(function drawImageBitmap() {
+                layer.drawImage(x, y, bitmap);
+            }, true);
 
-        }, true);
+            // Load image from provided blob
+            window.createImageBitmap(blob).then(function bitmapLoaded(decoded) 
{
+                bitmap = decoded;
+                task.unblock();
+            });
 
-        // Load image from URL
-        var image = new Image();
-        image.onload = task.unblock;
-        image.onerror = task.unblock;
-        image.src = url;
+        }
+
+        // Use blob URLs and the Image object if createImageBitmap() is
+        // unavailable
+        else {
+
+            // Create URL for blob
+            var url = URL.createObjectURL(blob);
+
+            // Draw and free blob URL when ready
+            task = scheduleTask(function __display_drawBlob() {
+
+                // Draw the image only if it loaded without errors
+                if (image.width && image.height)
+                    layer.drawImage(x, y, image);
+
+                // Blob URL no longer needed
+                URL.revokeObjectURL(url);
+
+            }, true);
+
+            // Load image from URL
+            var image = new Image();
+            image.onload = task.unblock;
+            image.onerror = task.unblock;
+            image.src = url;
+
+        }
+
+    };
+
+    /**
+     * Draws the image within the given stream at the given coordinates. The
+     * image will be loaded automatically, and this and any future operations
+     * will wait for the image to finish loading. This function will
+     * automatically choose an approriate method for reading and decoding the
+     * given image stream, and should be preferred for received streams except
+     * where manual decoding of the stream is unavoidable.
+     *
+     * @param {Guacamole.Layer} layer
+     *     The layer to draw upon.
+     *
+     * @param {Number} x
+     *     The destination X coordinate.
+     *
+     * @param {Number} y
+     *     The destination Y coordinate.
+     *
+     * @param {Guacamole.InputStream} stream
+     *     The stream along which image data will be received.
+     *
+     * @param {String} mimetype
+     *     The mimetype of the image within the stream.
+     */
+    this.drawStream = function drawStream(layer, x, y, stream, mimetype) {
+
+        // If createImageBitmap() is available, load the image as a blob so
+        // that function can be used
+        if (window.createImageBitmap) {
+            var reader = new Guacamole.BlobReader(stream, mimetype);
+            reader.onend = function drawImageBlob() {
+                guac_display.drawBlob(layer, x, y, reader.getBlob());
+            };
+        }
+
+        // Lacking createImageBitmap(), fall back to data URIs and the Image
+        // object
+        else {
+            var reader = new Guacamole.DataURIReader(stream, mimetype);
+            reader.onend = function drawImageDataURI() {
+                guac_display.draw(layer, x, y, reader.getURI());
 
 Review comment:
   Yep, `draw()` is correct here as we are using a data URI.
   
   `drawImage()` takes an `Image` (though the documentation is now updated to 
acknowledge it can take anything considered a `CanvasImageSource`). The 
`draw()` function is the one that accepts a URI.
   
   See:
   
   * 
http://guacamole.apache.org/doc/guacamole-common-js/Guacamole.Display.html#draw
   * 
http://guacamole.apache.org/doc/guacamole-common-js/Guacamole.Display.html#drawImage

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

Reply via email to