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

loader-utils: Copy utility functions from core to loader-utils #233

Merged
merged 2 commits into from
May 28, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {copyStringToDataView} from '@loaders.gl/loader-utils';
import {MAGIC_ARRAY} from '../constants';
import {encode3DTileHeader, encode3DTileByteLength} from './helpers/encode-3d-tile-header';
import {copyStringToDataView} from './helpers/encode-utils';

// Procedurally encode the tile array dataView for testing purposes
export function encodeBatchedModel3DTile(tile, dataView, byteOffset, options) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {copyStringToDataView} from '@loaders.gl/loader-utils';
import {MAGIC_ARRAY} from '../constants';
import {encode3DTileHeader, encode3DTileByteLength} from './helpers/encode-3d-tile-header';
import {copyStringToDataView} from './helpers/encode-utils';

// Procedurally encode the tile array buffer for testing purposes
// eslint-disable-next-line max-statements
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
padStringToByteAlignment,
copyStringToDataView,
copyBinaryToDataView
} from './helpers/encode-utils';
} from '@loaders.gl/loader-utils';

const DEFAULT_FEATURE_TABLE_JSON = {
POINTS_LENGTH: 1,
Expand Down
3 changes: 1 addition & 2 deletions modules/gltf/src/lib/deprecated/glb-builder.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
/* eslint-disable camelcase, max-statements */
import {padTo4Bytes, copyToArray} from '@loaders.gl/loader-utils';
import {isImage} from '@loaders.gl/images';
import {getAccessorTypeFromSize, getComponentTypeFromArray} from '../gltf-utils/gltf-utils';
import {padTo4Bytes} from '../utils/encode-utils';
import {copyToArray} from '../utils/encode-utils';
import encodeGLBSync from '../encode-glb';
import packBinaryJson from './packed-json/pack-binary-json';

Expand Down
5 changes: 4 additions & 1 deletion modules/gltf/src/lib/encode-glb.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
/* eslint-disable camelcase, max-statements */
import {copyPaddedStringToDataView, copyPaddedArrayBufferToDataView} from './utils/encode-utils';
import {
copyPaddedStringToDataView,
copyPaddedArrayBufferToDataView
} from '@loaders.gl/loader-utils';

const MAGIC_glTF = 0x46546c67; // glTF in ASCII
const MAGIC_JSON = 0x4e4f534a; // JSON in ASCII
Expand Down
2 changes: 1 addition & 1 deletion modules/gltf/src/lib/gltf-scenegraph.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {getImageMIMEType} from '@loaders.gl/images';
import {padTo4Bytes, copyToArray} from '@loaders.gl/loader-utils';
import assert from './utils/assert';
import {padTo4Bytes, copyToArray} from './utils/encode-utils';
import {
getAccessorArrayTypeAndLength,
getAccessorTypeFromSize,
Expand Down
3 changes: 2 additions & 1 deletion modules/gltf/src/lib/parse-glb.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/* eslint-disable camelcase, max-statements */
import {TextDecoder, padTo4Bytes, assert} from '@loaders.gl/core';
/* global TextDecoder */
import {padTo4Bytes, assert} from '@loaders.gl/loader-utils';

const MAGIC_glTF = 0x676c5446; // glTF in Big-Endian ASCII

Expand Down
2 changes: 0 additions & 2 deletions modules/gltf/test/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
// import './lib/utils/encode-utils.spec';

import './lib/glb/glb-encode-parse.spec';
import './lib/glb/glb-encoder-decoder.spec';
import './lib/glb/glb-custom-payload.spec';
Expand Down
11 changes: 11 additions & 0 deletions modules/loader-utils/src/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
// WORKER UTILS
export {default as createWorker} from './worker-utils/create-worker';

// MEMORY COPY UTILS
export {padTo4Bytes, copyToArray, copyArrayBuffer} from './lib/memory-copy-utils';
export {copyPaddedArrayBufferToDataView, copyPaddedStringToDataView} from './lib/binary-copy-utils';
export {
padStringToByteAlignment,
copyStringToDataView,
copyBinaryToDataView
} from './lib/encode-utils';

// MESH CATEGORY UTILS
export {getMeshSize as _getMeshSize} from './categories/mesh/mesh-utils';

export {default as assert} from './lib/utils/assert';
37 changes: 37 additions & 0 deletions modules/loader-utils/src/lib/binary-copy-utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/* global TextEncoder */
import {padTo4Bytes} from './memory-copy-utils';

export function copyPaddedArrayBufferToDataView(dataView, byteOffset, sourceBuffer, padding) {
const paddedLength = padTo4Bytes(sourceBuffer.byteLength);
const padLength = paddedLength - sourceBuffer.byteLength;

if (dataView) {
// Copy array
const targetArray = new Uint8Array(
dataView.buffer,
dataView.byteOffset + byteOffset,
sourceBuffer.byteLength
);
const sourceArray = new Uint8Array(sourceBuffer);
targetArray.set(sourceArray);

// Add PADDING
for (let i = 0; i < padLength; ++i) {
// json chunk is padded with spaces (ASCII 0x20)
dataView.setUint8(byteOffset + sourceBuffer.byteLength + i, 0x20);
}
}
byteOffset += paddedLength;
return byteOffset;
}

export function copyPaddedStringToDataView(dataView, byteOffset, string, padding) {
const textEncoder = new TextEncoder('utf8');
// PERFORMANCE IDEA: We encode twice, once to get size and once to store
// PERFORMANCE IDEA: Use TextEncoder.encodeInto() to avoid temporary copy
const stringBuffer = textEncoder.encode(string);

byteOffset = copyPaddedArrayBufferToDataView(dataView, byteOffset, stringBuffer, padding);

return byteOffset;
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import {TextEncoder} from '@loaders.gl/core';
export function padTo4Bytes(byteLength) {
return (byteLength + 3) & ~3;
}

/* Copies two different ArrayBuffers
/* Creates a new Uint8Array based on two different ArrayBuffers
* @private
* @param {ArrayBuffers} buffer1 The first buffer.
* @param {ArrayBuffers} buffer2 The second buffer.
* @return {ArrayBuffers} The new ArrayBuffer created out of the two.
Expand All @@ -17,45 +20,6 @@ export function copyArrayBuffer(
return targetBuffer;
}

export function copyPaddedArrayBufferToDataView(dataView, byteOffset, sourceBuffer, padding) {
const paddedLength = padTo4Bytes(sourceBuffer.byteLength);
const padLength = paddedLength - sourceBuffer.byteLength;

if (dataView) {
// Copy array
const targetArray = new Uint8Array(
dataView.buffer,
dataView.byteOffset + byteOffset,
sourceBuffer.byteLength
);
const sourceArray = new Uint8Array(sourceBuffer);
targetArray.set(sourceArray);

// Add PADDING
for (let i = 0; i < padLength; ++i) {
// json chunk is padded with spaces (ASCII 0x20)
dataView.setUint8(byteOffset + sourceBuffer.byteLength + i, 0x20);
}
}
byteOffset += paddedLength;
return byteOffset;
}

export function copyPaddedStringToDataView(dataView, byteOffset, string, padding) {
const textEncoder = new TextEncoder('utf8');
// PERFORMANCE IDEA: We encode twice, once to get size and once to store
// PERFORMANCE IDEA: Use TextEncoder.encodeInto() to avoid temporary copy
const stringBuffer = textEncoder.encode(string);

byteOffset = copyPaddedArrayBufferToDataView(dataView, byteOffset, stringBuffer, padding);

return byteOffset;
}

export function padTo4Bytes(byteLength) {
return (byteLength + 3) & ~3;
}

/**
* Copy from source to target at the targetOffset
*
Expand Down
8 changes: 8 additions & 0 deletions modules/loader-utils/src/lib/utils/assert.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// Replacement for the external assert method to reduce bundle size
// Note: We don't use the second "message" argument in calling code,
// so no need to support it here
export default function assert(condition, message) {
if (!condition) {
throw new Error(message || 'loader assertion failed.');
}
}
3 changes: 3 additions & 0 deletions modules/loader-utils/test/index.js
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
import './lib/binary-copy-utils.spec';
import './lib/memory-copy-utils.spec';

import './categories/mesh/mesh-utils.spec';
15 changes: 15 additions & 0 deletions modules/loader-utils/test/lib/binary-copy-utils.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/* eslint-disable max-len */
import test from 'tape-promise/tape';

import {copyPaddedStringToDataView} from '@loaders.gl/loader-utils';

test('copyPaddedStringToDataView', t => {
const STRING = 'abcdef';
const byteLength = copyPaddedStringToDataView(null, 0, STRING);
t.equals(byteLength, 8); // padded
const arrayBuffer = new ArrayBuffer(byteLength);
const dataView = new DataView(arrayBuffer);
const finalLength = copyPaddedStringToDataView(dataView, 0, STRING);
t.equals(finalLength, 8);
t.end();
});
17 changes: 17 additions & 0 deletions modules/loader-utils/test/lib/memory-copy-utils.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import test from 'tape-promise/tape';
import {padTo4Bytes, copyArrayBuffer, copyToArray} from '@loaders.gl/loader-utils';

test('padTo4Bytes', t => {
t.ok(padTo4Bytes, 'padTo4Bytes defined');
t.end();
});

test('toBuffer', t => {
t.ok(copyArrayBuffer, 'copyArrayBuffer defined');
t.end();
});

test('copyToArray', t => {
t.ok(copyToArray, 'copyToArray defined');
t.end();
});