|
| 1 | +part of '_gpu.dart'; |
| 2 | + |
| 3 | +/// Decodes [encoded] (any format the browser reads) and uploads it as an |
| 4 | +/// RGBA8 texture, straight into this backend's own WebGL context. |
| 5 | +/// |
| 6 | +/// The image never becomes a `ui.Image`, so nothing is read back through the |
| 7 | +/// framework's renderer and no pixel crosses Dart memory. An image larger than |
| 8 | +/// [maxSize], or than the context's `MAX_TEXTURE_SIZE`, is scaled down by the |
| 9 | +/// browser, keeping its aspect ratio. |
| 10 | +/// |
| 11 | +/// Returns null on a backend with no such path; the caller decodes through |
| 12 | +/// `dart:ui` instead. |
| 13 | +Future<Texture?> createTextureFromEncodedImage( |
| 14 | + Uint8List encoded, { |
| 15 | + bool mipmaps = true, |
| 16 | + int? maxSize, |
| 17 | +}) async { |
| 18 | + final context = gpuContext; |
| 19 | + final gl = context._gl; |
| 20 | + |
| 21 | + web.ImageBitmapOptions options({int? width, int? height}) { |
| 22 | + final result = web.ImageBitmapOptions( |
| 23 | + premultiplyAlpha: 'none', |
| 24 | + colorSpaceConversion: 'none', |
| 25 | + ); |
| 26 | + if (width != null && height != null) { |
| 27 | + result |
| 28 | + ..resizeWidth = width |
| 29 | + ..resizeHeight = height |
| 30 | + ..resizeQuality = 'high'; |
| 31 | + } |
| 32 | + return result; |
| 33 | + } |
| 34 | + |
| 35 | + var bitmap = await web.window |
| 36 | + .createImageBitmap(web.Blob(<JSAny>[encoded.toJS].toJS), options()) |
| 37 | + .toDart; |
| 38 | + try { |
| 39 | + // Read as a JS number: under dart2wasm the value is not a Dart `num`. |
| 40 | + final JSAny? reported = gl.getParameter( |
| 41 | + web.WebGL2RenderingContext.MAX_TEXTURE_SIZE, |
| 42 | + ); |
| 43 | + var limit = reported.isA<JSNumber>() |
| 44 | + ? (reported as JSNumber).toDartInt |
| 45 | + : 2048; |
| 46 | + if (maxSize != null && maxSize < limit) limit = maxSize; |
| 47 | + |
| 48 | + final longest = bitmap.width > bitmap.height ? bitmap.width : bitmap.height; |
| 49 | + if (longest > limit) { |
| 50 | + final scaled = await web.window |
| 51 | + .createImageBitmap( |
| 52 | + bitmap, |
| 53 | + options( |
| 54 | + width: (bitmap.width * limit ~/ longest).clamp(1, limit), |
| 55 | + height: (bitmap.height * limit ~/ longest).clamp(1, limit), |
| 56 | + ), |
| 57 | + ) |
| 58 | + .toDart; |
| 59 | + bitmap.close(); |
| 60 | + bitmap = scaled; |
| 61 | + } |
| 62 | + |
| 63 | + final width = bitmap.width; |
| 64 | + final height = bitmap.height; |
| 65 | + final texture = context.createTexture( |
| 66 | + StorageMode.hostVisible, |
| 67 | + width, |
| 68 | + height, |
| 69 | + mipLevelCount: mipmaps ? Texture.fullMipCount(width, height) : 1, |
| 70 | + ); |
| 71 | + context._bindTextureForSetup(texture.glTarget, texture._texture); |
| 72 | + gl.texSubImage2D( |
| 73 | + texture.glTarget, |
| 74 | + 0, |
| 75 | + 0, |
| 76 | + 0, |
| 77 | + width.toJS, |
| 78 | + height.toJS, |
| 79 | + web.WebGL2RenderingContext.RGBA.toJS, |
| 80 | + web.WebGL2RenderingContext.UNSIGNED_BYTE, |
| 81 | + bitmap, |
| 82 | + ); |
| 83 | + if (texture.mipLevelCount > 1) gl.generateMipmap(texture.glTarget); |
| 84 | + return texture; |
| 85 | + } finally { |
| 86 | + bitmap.close(); |
| 87 | + } |
| 88 | +} |
0 commit comments