-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
platform_react_native.ts
287 lines (257 loc) · 9.39 KB
/
platform_react_native.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
/**
* @license
* Copyright 2019 Google LLC. All Rights Reserved.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* =============================================================================
*/
import "@tensorflow/tfjs-backend-cpu";
import {
GPGPUContext,
MathBackendWebGL,
setWebGLContext,
} from "@tensorflow/tfjs-backend-webgl";
import * as tf from "@tensorflow/tfjs-core";
import { Platform } from "@tensorflow/tfjs-core";
import { Buffer } from "buffer";
import { GLView } from "expo-gl";
import { Platform as RNPlatform } from "react-native";
// See implementation note on fetch
// tslint:disable-next-line:max-line-length
// https://github.com/facebook/react-native/blob/0ee5f68929610106ee6864baa04ea90be0fc5160/Libraries/vendor/core/whatwg-fetch.js#L421
function parseHeaders(rawHeaders: string) {
const headers = new Headers();
// Replace instances of \r\n and \n followed by at least one space or
// horizontal tab with a space https://tools.ietf.org/html/rfc7230#section-3.2
const preProcessedHeaders = rawHeaders.replace(/\r?\n[\t ]+/g, " ");
preProcessedHeaders.split(/\r?\n/).forEach((line) => {
const parts = line.split(":");
const key = parts.shift().trim();
if (key) {
const value = parts.join(":").trim();
headers.append(key, value);
}
});
return headers;
}
// Implementation note: This is a patch of react-native's fetch implementation
// tslint:disable-next-line:max-line-length
// https://github.com/facebook/react-native/blob/0ee5f68929610106ee6864baa04ea90be0fc5160/Libraries/vendor/core/whatwg-fetch.js#L484
//
// The response object supplied by fetch does not implement arrayBuffer()
// FileReader.readAsArrayBuffer is not implemented.
// tslint:disable-next-line:max-line-length
// https://github.com/facebook/react-native/blob/d7a5e3e215eedb7377a86f172e0619403e20c2b8/Libraries/Blob/FileReader.js#L83
//
// However if one uses XMLHttpRequest directly and set the responseType
// correctly before making the request. The returned response object will have
// a working arrayBuffer method that can be used downstraeam.
/**
* Makes an HTTP request.
* @param path The URL path to make a request to
* @param init The request init. See init here:
* https://developer.mozilla.org/en-US/docs/Web/API/Request/Request
* @param options A RequestDetails object.
* - __options.isBinary__ boolean indicating whether this request is for a
* binary file.
*
* @doc {heading: 'Platform helpers', subheading: 'http'}
*/
export async function fetch(
path: string,
init?: RequestInit,
options?: tf.io.RequestDetails
): Promise<Response> {
return new Promise((resolve, reject) => {
const request = new Request(path, init);
const xhr = new XMLHttpRequest();
xhr.onload = () => {
const reqOptions = {
status: xhr.status,
statusText: xhr.statusText,
headers: parseHeaders(xhr.getAllResponseHeaders() || ""),
url: "",
};
reqOptions.url =
"responseURL" in xhr
? xhr.responseURL
: reqOptions.headers.get("X-Request-URL");
//@ts-ignore — ts believes the latter case will never occur.
const body = "response" in xhr ? xhr.response : xhr.responseText;
resolve(new Response(body, reqOptions));
};
xhr.onerror = () => reject(new TypeError("Network request failed"));
xhr.ontimeout = () => reject(new TypeError("Network request failed"));
xhr.open(request.method, request.url, true);
if (request.credentials === "include") {
xhr.withCredentials = true;
} else if (request.credentials === "omit") {
xhr.withCredentials = false;
}
if (options != null && options.isBinary) {
// In react native We need to set the response type to arraybuffer when
// fetching binary resources in order for `.arrayBuffer` to work correctly
// on the response.
xhr.responseType = "arraybuffer";
}
request.headers.forEach((value: string, name: string) => {
xhr.setRequestHeader(name, value);
});
xhr.send(
//@ts-ignore
typeof request._bodyInit === "undefined" ? null : request._bodyInit
);
});
}
export class PlatformReactNative implements Platform {
/**
* Makes an HTTP request.
*
* see @fetch docs above.
*/
async fetch(
path: string,
init?: RequestInit,
options?: tf.io.RequestDetails
) {
return fetch(path, init, options);
}
/**
* Encode the provided string into an array of bytes using the provided
* encoding.
*/
encode(text: string, encoding: string): Uint8Array {
// See https://www.w3.org/TR/encoding/#utf-16le
if (encoding === "utf-16") {
encoding = "utf16le";
}
return new Uint8Array(Buffer.from(text, encoding as BufferEncoding));
}
/** Decode the provided bytes into a string using the provided encoding. */
decode(bytes: Uint8Array, encoding: string): string {
// See https://www.w3.org/TR/encoding/#utf-16le
if (encoding === "utf-16") {
encoding = "utf16le";
}
return Buffer.from(bytes).toString(encoding as BufferEncoding);
}
now(): number {
//@ts-ignore
if (global.nativePerformanceNow) {
//@ts-ignore
return global.nativePerformanceNow();
}
return Date.now();
}
setTimeoutCustom() {
throw new Error("react native does not support setTimeoutCustom");
}
isTypedArray(
a: unknown
): a is Uint8Array | Float32Array | Int32Array | Uint8ClampedArray {
return (
a instanceof Float32Array ||
a instanceof Int32Array ||
a instanceof Uint8Array ||
a instanceof Uint8ClampedArray
);
}
}
function setupGlobals() {
global.Buffer = Buffer;
}
function registerWebGLBackend() {
try {
const PRIORITY = 5;
tf.registerBackend(
"rn-webgl",
async () => {
const glContext = await GLView.createContextAsync();
// ExpoGl getBufferSubData is not implemented yet (throws an exception).
tf.env().set("WEBGL_BUFFER_SUPPORTED", false);
//
// Mock extension support for EXT_color_buffer_float and
// EXT_color_buffer_half_float on the expo-gl context object.
// In react native we do not have to get a handle to the extension
// in order to use the functionality of that extension on the device.
//
// This code block makes iOS and Android devices pass the extension checks
// used in core. After those are done core will actually test whether
// we can render/download float or half float textures.
//
// We can remove this block once we upstream checking for these
// extensions in expo.
//
// TODO look into adding support for checking these extensions in expo-gl
//
//@ts-ignore
const getExt = glContext.getExtension.bind(glContext);
const shimGetExt = (name: string) => {
if (name === "EXT_color_buffer_float") {
if (RNPlatform.OS === "ios") {
// iOS does not support EXT_color_buffer_float
return null;
} else {
return {};
}
}
if (name === "EXT_color_buffer_half_float") {
return {};
}
return getExt(name);
};
//
// Manually make 'read' synchronous. glContext has a defined gl.fenceSync
// function that throws a "Not implemented yet" exception so core
// cannot properly detect that it is not supported. We mock
// implementations of gl.fenceSync and gl.clientWaitSync
// TODO remove once fenceSync and clientWaitSync is implemented upstream.
//
const shimFenceSync = () => {
return {};
};
const shimClientWaitSync = () => glContext.CONDITION_SATISFIED;
// @ts-ignore
glContext.getExtension = shimGetExt.bind(glContext);
glContext.fenceSync = shimFenceSync.bind(glContext);
glContext.clientWaitSync = shimClientWaitSync.bind(glContext);
// Set the WebGLContext before flag evaluation
setWebGLContext(2, glContext);
const context = new GPGPUContext();
const backend = new MathBackendWebGL(context);
return backend;
},
PRIORITY
);
// Register all the webgl kernels on the rn-webgl backend
// TODO: Use tf.copyRegisteredKernels once synced to tfjs-core 2.5.0.
// tf.copyRegisteredKernels('webgl', 'rn-webgl');
const kernels = tf.getKernelsForBackend("webgl");
kernels.forEach((kernelConfig) => {
const newKernelConfig = Object.assign({}, kernelConfig, {
backendName: "rn-webgl",
});
tf.registerKernel(newKernelConfig);
});
} catch (e) {
throw new Error(`Failed to register Webgl backend: ${e.message}`);
}
}
tf.env().registerFlag(
"IS_REACT_NATIVE",
() => navigator && navigator.product === "ReactNative"
);
if (tf.env().getBool("IS_REACT_NATIVE")) {
setupGlobals();
registerWebGLBackend();
tf.setPlatform("react-native", new PlatformReactNative());
}