Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use createImageBitmap when supported #650

Merged
merged 1 commit into from
Nov 23, 2021

Conversation

clementgayvallet
Copy link
Contributor

@clementgayvallet clementgayvallet commented Nov 19, 2021

When converting a byte buffer to a CanvasImageSource, maplibre uses either an ImageBitmap if supported or an HTMLImageElement as fallback.
HTMLImageElement has many flaws, such as the transparent pixel hack to avoid memory leaks. Therefore the ImageBitmap seems more reliable and is the preferred image source.

In order to load the byte buffer to an ImageBitmap the library checks if the feature is available on the browser. Currently the function checks if the function createImageBitmap is defined and if the OffscreenCanvas feature is available.
However the OffscreenCanvas is not required to create an ImageBitmap and on browsers like Firefox where createImageBitmap is supported but not OffscreenCanvas (which is still experimental), the image source falls back on HTMLImageElement.

This PR simply checks for the availability of the createImageBitmap function instead of the unneeded OffscreenCanvas feature.

Launch Checklist

  • Confirm your changes do not include backports from Mapbox projects (unless with compliant license) - if you are not sure about this, please ask!
  • Briefly describe the changes in this PR.
  • Include before/after visuals or gifs if this PR includes visual changes.
  • Write tests for all new functionality.
  • Document any changes to public APIs.
  • Post benchmark scores.
  • Manually test the debug page.
  • Suggest a changelog category: "feature".
  • Add an entry inside this element for inclusion in the maplibre-gl-js changelog: <changelog>getImage uses createImageBitmap when supported (#650)</changelog>.

@github-actions
Copy link
Contributor

github-actions bot commented Nov 19, 2021

Bundle size report:

Size Change: -32 B
Total Size Before: 194 kB
Total Size After: 194 kB

Output file Before After Change
maplibre-gl.js 185 kB 185 kB -32 B
maplibre-gl.css 9.49 kB 9.49 kB 0 B
ℹ️ View Details
Source file Before After Change
rollup/build/tsc/src/source/raster_dem_tile_source.js 875 B 955 B +80 B
rollup/build/tsc/src/util/ajax.js 2.31 kB 2.32 kB +12 B
rollup/build/tsc/src/data/feature_index.js 1.7 kB 1.7 kB +6 B
node_modules/@mapbox/vector-tile/lib/vectortilelayer.js 425 B 429 B +4 B
rollup/build/tsc/src/style/style.js 6.55 kB 6.55 kB +4 B
node_modules/gl-matrix/esm/mat4.js 2.12 kB 2.12 kB +3 B
rollup/build/tsc/src/style-spec/validate/validate_constants.js 114 B 116 B +2 B
rollup/build/tsc/src/style/properties.js 1.87 kB 1.87 kB +2 B
rollup/build/tsc/src/data/segment.js 449 B 451 B +2 B
rollup/build/tsc/src/style/style_layer/circle_style_layer_properties.js 230 B 232 B +2 B
node_modules/gl-matrix/esm/vec4.js 447 B 449 B +2 B
rollup/build/tsc/src/style/style_layer/circle_style_layer.js 545 B 547 B +2 B
rollup/build/tsc/src/symbol/anchor.js 168 B 170 B +2 B
rollup/build/tsc/src/util/performance.js 467 B 469 B +2 B
rollup/build/tsc/src/util/global_worker_pool.js 336 B 338 B +2 B
rollup/build/tsc/src/symbol/projection.js 1.77 kB 1.78 kB +2 B
rollup/build/tsc/src/symbol/cross_tile_symbol_index.js 1.13 kB 1.14 kB +2 B
rollup/build/tsc/src/render/program/raster_program.js 563 B 565 B +2 B
rollup/build/tsc/src/gl/vertex_buffer.js 537 B 539 B +2 B
rollup/build/tsc/src/render/draw_line.js 1.01 kB 1.01 kB +2 B
rollup/build/tsc/src/ui/handler_manager.js 2.32 kB 2.33 kB +2 B
rollup/build/tsc/src/style-spec/error/validation_error.js 125 B 126 B +1 B
rollup/build/tsc/src/style-spec/validate/validate_object.js 392 B 393 B +1 B
rollup/build/tsc/src/style-spec/validate/validate_array.js 356 B 357 B +1 B
rollup/build/tsc/src/render/uniform_binding.js 637 B 638 B +1 B
rollup/build/tsc/src/data/bucket/pattern_bucket_features.js 327 B 328 B +1 B
rollup/build/tsc/src/style/style_layer/fill_style_layer_properties.js 196 B 197 B +1 B
node_modules/@mapbox/vector-tile/lib/vectortile.js 163 B 164 B +1 B
rollup/build/tsc/src/style/style_layer/fill_extrusion_style_layer.js 928 B 929 B +1 B
rollup/build/tsc/src/data/bucket/symbol_attributes.js 767 B 768 B +1 B
node_modules/pbf/index.js 2.81 kB 2.81 kB +1 B
rollup/build/tsc/src/symbol/quads.js 1.89 kB 1.89 kB +1 B
rollup/build/tsc/src/util/find_pole_of_inaccessibility.js 685 B 686 B +1 B
rollup/build/tsc/src/style/style_layer/symbol_style_layer_properties.js 645 B 646 B +1 B
rollup/build/tsc/src/style/style_layer/raster_style_layer_properties.js 167 B 168 B +1 B
rollup/build/tsc/src/style/style_layer/custom_style_layer.js 485 B 486 B +1 B
rollup/build/tsc/src/util/throttled_invoker.js 212 B 213 B +1 B
rollup/build/tsc/src/util/actor.js 1.11 kB 1.11 kB +1 B
node_modules/gl-matrix/esm/vec3.js 830 B 831 B +1 B
rollup/build/tsc/src/render/texture.js 680 B 681 B +1 B
rollup/build/tsc/src/render/image_manager.js 1.4 kB 1.4 kB +1 B
rollup/build/tsc/src/source/vector_tile_source.js 1.11 kB 1.11 kB +1 B
rollup/build/tsc/src/source/tile.js 1.92 kB 1.92 kB +1 B
rollup/build/tsc/src/style-spec/deref.js 207 B 208 B +1 B
rollup/build/tsc/src/symbol/grid_index.js 1.28 kB 1.28 kB +1 B
rollup/build/tsc/src/symbol/placement.js 4.64 kB 4.65 kB +1 B
rollup/build/tsc/src/render/program.js 911 B 912 B +1 B
rollup/build/tsc/src/render/program/pattern.js 614 B 615 B +1 B
rollup/build/tsc/src/render/draw_collision_debug.js 1.07 kB 1.07 kB +1 B
rollup/build/tsc/src/render/draw_fill.js 976 B 977 B +1 B
rollup/build/tsc/src/render/draw_hillshade.js 1.09 kB 1.09 kB +1 B
rollup/build/tsc/src/render/draw_background.js 528 B 529 B +1 B
rollup/build/tsc/src/util/primitives.js 976 B 977 B +1 B
rollup/build/tsc/src/ui/hash.js 939 B 940 B +1 B
rollup/build/tsc/src/style-spec/util/unbundle_jsonlint.js 181 B 180 B -1 B
rollup/build/tsc/src/style-spec/validate/validate_number.js 223 B 222 B -1 B
rollup/build/tsc/src/style-spec/feature_filter/index.js 884 B 883 B -1 B
rollup/build/tsc/src/style-spec/validate/validate_light.js 291 B 290 B -1 B
rollup/build/tsc/src/style-spec/validate_style.min.js 293 B 292 B -1 B
rollup/build/tsc/src/style/validate_style.js 155 B 154 B -1 B
rollup/build/tsc/src/style/zoom_history.js 183 B 182 B -1 B
rollup/build/tsc/src/data/array_types.js 2.35 kB 2.35 kB -1 B
rollup/build/tsc/src/data/feature_position_map.js 554 B 553 B -1 B
rollup/build/tsc/src/data/program_configuration.js 2.61 kB 2.61 kB -1 B
rollup/build/tsc/src/util/intersection_tests.js 944 B 943 B -1 B
rollup/build/tsc/src/style/query_utils.js 267 B 266 B -1 B
node_modules/earcut/src/earcut.js 2.62 kB 2.62 kB -1 B
node_modules/quickselect/index.js 354 B 353 B -1 B
rollup/build/tsc/src/data/bucket/fill_extrusion_bucket.js 1.3 kB 1.3 kB -1 B
rollup/build/tsc/src/style/style_layer/fill_extrusion_style_layer_properties.js 189 B 188 B -1 B
rollup/build/tsc/src/data/bucket/line_bucket.js 2.41 kB 2.41 kB -1 B
rollup/build/tsc/src/style/style_layer/line_style_layer.js 971 B 970 B -1 B
rollup/build/tsc/src/util/verticalize_punctuation.js 589 B 588 B -1 B
rollup/build/tsc/src/style/parse_glyph_pbf.js 365 B 364 B -1 B
rollup/build/tsc/src/symbol/symbol_size.js 541 B 540 B -1 B
rollup/build/tsc/src/symbol/get_anchors.js 581 B 580 B -1 B
rollup/build/tsc/src/data/bucket/symbol_bucket.js 4.24 kB 4.24 kB -1 B
rollup/build/tsc/src/style/format_section_override.js 307 B 306 B -1 B
rollup/build/tsc/src/geo/mercator_coordinate.js 348 B 347 B -1 B
rollup/build/tsc/src/source/tile_cache.js 556 B 555 B -1 B
rollup/build/tsc/src/symbol/collision_index.js 1.57 kB 1.57 kB -1 B
rollup/build/tsc/src/data/pos_attributes.js 7.07 kB 7.07 kB -1 B
rollup/build/tsc/src/shaders/shaders.js 444 B 443 B -1 B
rollup/build/tsc/src/render/program/circle_program.js 459 B 458 B -1 B
rollup/build/tsc/src/gl/value.js 1.09 kB 1.09 kB -1 B
rollup/build/tsc/src/gl/framebuffer.js 221 B 220 B -1 B
rollup/build/tsc/src/gl/context.js 1.28 kB 1.28 kB -1 B
rollup/build/tsc/src/render/draw_raster.js 1 kB 1 kB -1 B
rollup/build/tsc/src/geo/transform.js 3.76 kB 3.76 kB -1 B
rollup/build/tsc/src/ui/handler/map_event.js 431 B 430 B -1 B
rollup/build/tsc/src/ui/handler/tap_zoom.js 366 B 365 B -1 B
rollup/build/tsc/src/ui/handler/touch_pan.js 430 B 429 B -1 B
rollup/build/tsc/src/util/task_queue.js 268 B 267 B -1 B
rollup/build/tsc/src/ui/default_locale.js 284 B 283 B -1 B
rollup/build/tsc/src/ui/control/navigation_control.js 1.62 kB 1.61 kB -1 B
rollup/build/tsc/src/ui/marker.js 2.72 kB 2.72 kB -1 B
rollup/build/tsc/src/util/tile_request_cache.js 1.02 kB 1.02 kB -2 B
rollup/build/tsc/src/style-spec/validate/validate_filter.js 627 B 625 B -2 B
rollup/build/tsc/src/symbol/transform_text.js 204 B 202 B -2 B
rollup/build/tsc/src/source/query_features.js 1.21 kB 1.21 kB -2 B
rollup/build/tsc/src/render/draw_symbol.js 2.48 kB 2.48 kB -2 B
rollup/build/tsc/src/ui/handler_inertia.js 822 B 820 B -2 B
rollup/build/tsc/src/ui/handler/shim/dblclick_zoom.js 151 B 149 B -2 B
rollup/build/tsc/src/ui/camera.js 2.96 kB 2.96 kB -2 B
rollup/build/tsc/src/ui/control/attribution_control.js 1 kB 1 kB -2 B
rollup/build/tsc/src/ui/anchor.js 219 B 217 B -2 B
rollup/build/tsc/src/symbol/shaping.js 3.63 kB 3.63 kB -3 B
rollup/build/tsc/src/symbol/symbol_layout.js 3.61 kB 3.61 kB -3 B
rollup/build/tsc/src/source/tile_id.js 1.13 kB 1.12 kB -3 B
rollup/build/tsc/src/util/offscreen_canvas_supported.js 165 B 154 B -11 B
rollup/build/tsc/src/util/util.js 2.01 kB 1.98 kB -27 B
rollup/build/tsc/src/util/webp_supported.js 492 B 384 B -108 B

@HarelM
Copy link
Member

HarelM commented Nov 19, 2021

Is this the only place that uses the offscreen camvas check? Can you think why offscreen was checked here?
Also, I think I wrote a function to check for imagebitmap support somewhere...

@clementgayvallet
Copy link
Contributor Author

No, the OffscreenCanvas check is also used in the raster_dem_tile_source but in this case the check is relevant since the feature is used in the worker.

For the getImage function however, I don't see any reason to check the OffscreenCanvas support here and to be reassured mapbox-gl-js relaxed the check too in this commit.

Regarding the ImageBitmap support check that you wrote, you may be referring to this commit. I did not find any other occurrence of createImageBitmap in the code.

@HarelM
Copy link
Member

HarelM commented Nov 22, 2021

Thanks for the clarification, in that case my comments are the following:

  1. Move offscreenCanvasSupported into raster_dem_tile_source since it is only used there, unless this doesn't feel right to you
  2. Remove offscreen_canvas_supported file (since it should be empty after 1)
  3. do not use window. to test for createImageBitmap as it might be used from worker where window is not defined.

I got confused with isImageBitmap I wrote which is not related to this issue.

Thanks!

@clementgayvallet
Copy link
Contributor Author

clementgayvallet commented Nov 23, 2021

  1. I'd rather let the offscreenCanvasSupported function in a separate file since the function uses a private variable as cache to prevent the costly context creation I suppose. I find it's cleaner that way.
  2. done :)

@HarelM
Copy link
Member

HarelM commented Nov 23, 2021

Thanks!! Can we add some tests to this behavior?

@clementgayvallet
Copy link
Contributor Author

I just tried but it wasn't very convincing. The problem is that it's browser dependent and during unit test the createImageBitmap is not defined. We can mock it with something like window.createImageBitmap = () => Promise.resolve(); but we have to use window.createImageBitmap in the code too. But even with that I don't think the tests add a real value.

Do you have any idea on how to test this?

@HarelM
Copy link
Member

HarelM commented Nov 23, 2021

You can override global.create... in order to mock this, you don't have to use window.
Writing a unit test to make sure the right branch is used in the if is a good unit test I believe.

@HarelM
Copy link
Member

HarelM commented Nov 23, 2021

Change log entry? Please also tic the relevant checkboxes in the pr template to make sure this is not copied etc...

@HarelM HarelM merged commit c0f06f2 into maplibre:main Nov 23, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants