blob: d136ece76d3e579c147f5f9898691ae1f006c723 [file] [log] [blame]
Tim Murray84bf2b82012-10-31 16:03:16 -07001/*
Tim Murray89daad62013-07-29 14:30:02 -07002 * Copyright (C) 2013 The Android Open Source Project
Tim Murray84bf2b82012-10-31 16:03:16 -07003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef ANDROID_RSCPPSTRUCTS_H
18#define ANDROID_RSCPPSTRUCTS_H
19
Tim Murray89daad62013-07-29 14:30:02 -070020#include "rsDefines.h"
Tim Murray89daad62013-07-29 14:30:02 -070021#include "util/RefBase.h"
Tim Murraya4230962013-07-17 16:50:10 -070022
Tim Murray8c24cd62014-04-10 18:04:39 -070023#include <pthread.h>
Tim Murray89daad62013-07-29 14:30:02 -070024
Miao Wang09d2dd22015-03-18 20:09:20 -070025
Tim Murray75e877d2013-09-11 14:45:20 -070026/**
27 * Every row in an RS allocation is guaranteed to be aligned by this amount, and
28 * every row in a user-backed allocation must be aligned by this amount.
29 */
Tim Murray96267c22013-02-12 11:25:12 -080030#define RS_CPU_ALLOCATION_ALIGNMENT 16
31
Jason Sams66f0a162014-11-11 13:46:38 -080032struct dispatchTable;
33
Tim Murray84bf2b82012-10-31 16:03:16 -070034namespace android {
Miao Wang09d2dd22015-03-18 20:09:20 -070035class Surface;
36
Tim Murray9eb7f4b2012-11-16 14:02:18 -080037namespace RSC {
Tim Murray84bf2b82012-10-31 16:03:16 -070038
Jason Sams66f0a162014-11-11 13:46:38 -080039
Tim Murray84bf2b82012-10-31 16:03:16 -070040typedef void (*ErrorHandlerFunc_t)(uint32_t errorNum, const char *errorText);
41typedef void (*MessageHandlerFunc_t)(uint32_t msgNum, const void *msgData, size_t msgLen);
42
43class RS;
44class BaseObj;
45class Element;
46class Type;
47class Allocation;
48class Script;
49class ScriptC;
Tim Murray729b6fe2013-07-23 16:20:42 -070050class Sampler;
Tim Murray84bf2b82012-10-31 16:03:16 -070051
Tim Murray75e877d2013-09-11 14:45:20 -070052/**
53 * Possible error codes used by RenderScript. Once a status other than RS_SUCCESS
54 * is returned, the RenderScript context is considered dead and cannot perform any
55 * additional work.
56 */
Tim Murray21fa7a02013-08-15 16:25:03 -070057 enum RSError {
Tim Murray75e877d2013-09-11 14:45:20 -070058 RS_SUCCESS = 0, ///< No error
59 RS_ERROR_INVALID_PARAMETER = 1, ///< An invalid parameter was passed to a function
60 RS_ERROR_RUNTIME_ERROR = 2, ///< The RenderScript driver returned an error; this is
61 ///< often indicative of a kernel that crashed
62 RS_ERROR_INVALID_ELEMENT = 3, ///< An invalid Element was passed to a function
Tim Murray21fa7a02013-08-15 16:25:03 -070063 RS_ERROR_MAX = 9999
64
65 };
66
Tim Murray75e877d2013-09-11 14:45:20 -070067 /**
68 * YUV formats supported by the RenderScript API.
69 */
Tim Murrayeb4426d2013-08-27 15:30:16 -070070 enum RSYuvFormat {
Tim Murray75e877d2013-09-11 14:45:20 -070071 RS_YUV_NONE = 0, ///< No YUV data
72 RS_YUV_YV12 = 1, ///< YUV data in YV12 format
73 RS_YUV_NV21 = 2, ///< YUV data in NV21 format
Tim Murrayeb4426d2013-08-27 15:30:16 -070074 RS_YUV_MAX = 3
75 };
76
Tim Murray75e877d2013-09-11 14:45:20 -070077 /**
78 * Flags that can control RenderScript behavior on a per-context level.
79 */
Tim Murray84e3dea2013-09-09 16:12:51 -070080 enum RSInitFlags {
Tim Murray75e877d2013-09-11 14:45:20 -070081 RS_INIT_SYNCHRONOUS = 1, ///< All RenderScript calls will be synchronous. May reduce latency.
82 RS_INIT_LOW_LATENCY = 2, ///< Prefer low latency devices over potentially higher throughput devices.
Stephen McGroartyd5164d52015-05-08 15:31:49 +010083 // Bitflag 4 is reserved for the context flag low power
84 RS_INIT_WAIT_FOR_ATTACH = 8, ///< Kernel execution will hold to give time for a debugger to be attached
Stephen McGroarty15c1d062015-09-02 16:03:38 +010085 RS_INIT_MAX = 16
Tim Murray84e3dea2013-09-09 16:12:51 -070086 };
87
Tim Murray75e877d2013-09-11 14:45:20 -070088 /**
89 * The RenderScript context. This class controls initialization, resource management, and teardown.
90 */
Tim Murray89daad62013-07-29 14:30:02 -070091 class RS : public android::RSC::LightRefBase<RS> {
Tim Murray84bf2b82012-10-31 16:03:16 -070092
93 public:
94 RS();
95 virtual ~RS();
96
Tim Murray75e877d2013-09-11 14:45:20 -070097 /**
98 * Initializes a RenderScript context. A context must be initialized before it can be used.
Tim Murraycaf41262013-12-13 12:54:37 -080099 * @param[in] name Directory name to be used by this context. This should be equivalent to
100 * Context.getCacheDir().
Tim Murray75e877d2013-09-11 14:45:20 -0700101 * @param[in] flags Optional flags for this context.
102 * @return true on success
103 */
Miao Wangbc10dff2015-04-03 17:44:55 -0700104 bool init(const char * name, uint32_t flags = 0);
Tim Murray84bf2b82012-10-31 16:03:16 -0700105
Tim Murray75e877d2013-09-11 14:45:20 -0700106 /**
107 * Sets the error handler function for this context. This error handler is
108 * called whenever an error is set.
109 *
110 * @param[in] func Error handler function
111 */
Tim Murray84bf2b82012-10-31 16:03:16 -0700112 void setErrorHandler(ErrorHandlerFunc_t func);
Tim Murray75e877d2013-09-11 14:45:20 -0700113
114 /**
115 * Returns the current error handler function for this context.
116 *
117 * @return pointer to current error handler function or NULL if not set
118 */
Tim Murray84bf2b82012-10-31 16:03:16 -0700119 ErrorHandlerFunc_t getErrorHandler() { return mErrorFunc; }
120
Tim Murray75e877d2013-09-11 14:45:20 -0700121 /**
122 * Sets the message handler function for this context. This message handler
123 * is called whenever a message is sent from a RenderScript kernel.
124 *
125 * @param[in] func Message handler function
126 */
Tim Murray84bf2b82012-10-31 16:03:16 -0700127 void setMessageHandler(MessageHandlerFunc_t func);
Tim Murray75e877d2013-09-11 14:45:20 -0700128
129 /**
130 * Returns the current message handler function for this context.
131 *
132 * @return pointer to current message handler function or NULL if not set
133 */
Tim Murray84bf2b82012-10-31 16:03:16 -0700134 MessageHandlerFunc_t getMessageHandler() { return mMessageFunc; }
135
Tim Murray75e877d2013-09-11 14:45:20 -0700136 /**
137 * Returns current status for the context.
138 *
139 * @return current error
140 */
Tim Murray10913a52013-08-20 17:19:47 -0700141 RSError getError();
Tim Murray84bf2b82012-10-31 16:03:16 -0700142
Tim Murray75e877d2013-09-11 14:45:20 -0700143 /**
144 * Waits for any currently running asynchronous operations to finish. This
145 * should only be used for performance testing and timing.
146 */
Tim Murraybaca6c32012-11-14 16:51:46 -0800147 void finish();
148
Tim Murray75e877d2013-09-11 14:45:20 -0700149 RsContext getContext() { return mContext; }
150 void throwError(RSError error, const char *errMsg);
151
Tim Murraya4230962013-07-17 16:50:10 -0700152 static dispatchTable* dispatch;
153
Tim Murray84bf2b82012-10-31 16:03:16 -0700154 private:
Tim Murray4a92d122013-07-22 10:56:18 -0700155 static bool usingNative;
Tim Murraya4230962013-07-17 16:50:10 -0700156 static bool initDispatch(int targetApi);
157
Miao Wangbc10dff2015-04-03 17:44:55 -0700158 bool init(const char * name, int targetApi, uint32_t flags);
Tim Murray84bf2b82012-10-31 16:03:16 -0700159 static void * threadProc(void *);
160
161 static bool gInitialized;
162 static pthread_mutex_t gInitMutex;
163
164 pthread_t mMessageThreadId;
165 pid_t mNativeMessageThreadId;
166 bool mMessageRun;
167
168 RsDevice mDev;
169 RsContext mContext;
Tim Murray21fa7a02013-08-15 16:25:03 -0700170 RSError mCurrentError;
Tim Murray84bf2b82012-10-31 16:03:16 -0700171
172 ErrorHandlerFunc_t mErrorFunc;
173 MessageHandlerFunc_t mMessageFunc;
Tim Murraya4230962013-07-17 16:50:10 -0700174 bool mInit;
Tim Murray84bf2b82012-10-31 16:03:16 -0700175
Miao Wangbc10dff2015-04-03 17:44:55 -0700176 char mCacheDir[PATH_MAX+1];
177 uint32_t mCacheDirLen;
Tim Murraycaf41262013-12-13 12:54:37 -0800178
Tim Murray84bf2b82012-10-31 16:03:16 -0700179 struct {
Tim Murray89daad62013-07-29 14:30:02 -0700180 sp<const Element> U8;
Tim Murrayeb4426d2013-08-27 15:30:16 -0700181 sp<const Element> U8_2;
182 sp<const Element> U8_3;
183 sp<const Element> U8_4;
Tim Murray89daad62013-07-29 14:30:02 -0700184 sp<const Element> I8;
Tim Murrayeb4426d2013-08-27 15:30:16 -0700185 sp<const Element> I8_2;
186 sp<const Element> I8_3;
187 sp<const Element> I8_4;
Tim Murray89daad62013-07-29 14:30:02 -0700188 sp<const Element> U16;
Tim Murrayeb4426d2013-08-27 15:30:16 -0700189 sp<const Element> U16_2;
190 sp<const Element> U16_3;
191 sp<const Element> U16_4;
Tim Murray89daad62013-07-29 14:30:02 -0700192 sp<const Element> I16;
Tim Murrayeb4426d2013-08-27 15:30:16 -0700193 sp<const Element> I16_2;
194 sp<const Element> I16_3;
195 sp<const Element> I16_4;
Tim Murray89daad62013-07-29 14:30:02 -0700196 sp<const Element> U32;
Tim Murrayeb4426d2013-08-27 15:30:16 -0700197 sp<const Element> U32_2;
198 sp<const Element> U32_3;
199 sp<const Element> U32_4;
Tim Murray89daad62013-07-29 14:30:02 -0700200 sp<const Element> I32;
Tim Murrayeb4426d2013-08-27 15:30:16 -0700201 sp<const Element> I32_2;
202 sp<const Element> I32_3;
203 sp<const Element> I32_4;
Tim Murray89daad62013-07-29 14:30:02 -0700204 sp<const Element> U64;
Tim Murrayeb4426d2013-08-27 15:30:16 -0700205 sp<const Element> U64_2;
206 sp<const Element> U64_3;
207 sp<const Element> U64_4;
Tim Murray89daad62013-07-29 14:30:02 -0700208 sp<const Element> I64;
Tim Murrayeb4426d2013-08-27 15:30:16 -0700209 sp<const Element> I64_2;
210 sp<const Element> I64_3;
211 sp<const Element> I64_4;
Tim Murray89daad62013-07-29 14:30:02 -0700212 sp<const Element> F32;
Tim Murrayeb4426d2013-08-27 15:30:16 -0700213 sp<const Element> F32_2;
214 sp<const Element> F32_3;
215 sp<const Element> F32_4;
Tim Murray89daad62013-07-29 14:30:02 -0700216 sp<const Element> F64;
Tim Murrayeb4426d2013-08-27 15:30:16 -0700217 sp<const Element> F64_2;
218 sp<const Element> F64_3;
219 sp<const Element> F64_4;
Tim Murray89daad62013-07-29 14:30:02 -0700220 sp<const Element> BOOLEAN;
Tim Murray84bf2b82012-10-31 16:03:16 -0700221
Tim Murray89daad62013-07-29 14:30:02 -0700222 sp<const Element> ELEMENT;
223 sp<const Element> TYPE;
224 sp<const Element> ALLOCATION;
225 sp<const Element> SAMPLER;
226 sp<const Element> SCRIPT;
227 sp<const Element> MESH;
228 sp<const Element> PROGRAM_FRAGMENT;
229 sp<const Element> PROGRAM_VERTEX;
230 sp<const Element> PROGRAM_RASTER;
231 sp<const Element> PROGRAM_STORE;
Tim Murray84bf2b82012-10-31 16:03:16 -0700232
Tim Murray89daad62013-07-29 14:30:02 -0700233 sp<const Element> A_8;
234 sp<const Element> RGB_565;
235 sp<const Element> RGB_888;
236 sp<const Element> RGBA_5551;
237 sp<const Element> RGBA_4444;
238 sp<const Element> RGBA_8888;
Tim Murray84bf2b82012-10-31 16:03:16 -0700239
Tim Murrayeb4426d2013-08-27 15:30:16 -0700240 sp<const Element> YUV;
Tim Murray84bf2b82012-10-31 16:03:16 -0700241
Tim Murray89daad62013-07-29 14:30:02 -0700242 sp<const Element> MATRIX_4X4;
243 sp<const Element> MATRIX_3X3;
244 sp<const Element> MATRIX_2X2;
Tim Murray84bf2b82012-10-31 16:03:16 -0700245 } mElements;
246
Tim Murray729b6fe2013-07-23 16:20:42 -0700247 struct {
Tim Murray89daad62013-07-29 14:30:02 -0700248 sp<const Sampler> CLAMP_NEAREST;
249 sp<const Sampler> CLAMP_LINEAR;
250 sp<const Sampler> CLAMP_LINEAR_MIP_LINEAR;
251 sp<const Sampler> WRAP_NEAREST;
252 sp<const Sampler> WRAP_LINEAR;
253 sp<const Sampler> WRAP_LINEAR_MIP_LINEAR;
254 sp<const Sampler> MIRRORED_REPEAT_NEAREST;
255 sp<const Sampler> MIRRORED_REPEAT_LINEAR;
256 sp<const Sampler> MIRRORED_REPEAT_LINEAR_MIP_LINEAR;
Tim Murray729b6fe2013-07-23 16:20:42 -0700257 } mSamplers;
258 friend class Sampler;
259 friend class Element;
Tim Murraycaf41262013-12-13 12:54:37 -0800260 friend class ScriptC;
Tim Murray84bf2b82012-10-31 16:03:16 -0700261};
262
Tim Murray75e877d2013-09-11 14:45:20 -0700263 /**
264 * Base class for all RenderScript objects. Not for direct use by developers.
265 */
Tim Murray89daad62013-07-29 14:30:02 -0700266class BaseObj : public android::RSC::LightRefBase<BaseObj> {
267public:
268 void * getID() const;
269 virtual ~BaseObj();
270 virtual void updateFromNative();
271 virtual bool equals(sp<const BaseObj> obj);
272
Tim Murray84bf2b82012-10-31 16:03:16 -0700273protected:
274 void *mID;
Tim Murray35609072013-12-03 11:36:03 -0800275 RS* mRS;
Miao Wangbc10dff2015-04-03 17:44:55 -0700276 const char * mName;
Tim Murray84bf2b82012-10-31 16:03:16 -0700277
278 BaseObj(void *id, sp<RS> rs);
279 void checkValid();
280
281 static void * getObjID(sp<const BaseObj> o);
282
Tim Murray84bf2b82012-10-31 16:03:16 -0700283};
284
Tim Murray75e877d2013-09-11 14:45:20 -0700285 /**
286 * This class provides the primary method through which data is passed to and
287 * from RenderScript kernels. An Allocation provides the backing store for a
288 * given Type.
289 *
290 * An Allocation also contains a set of usage flags that denote how the
291 * Allocation could be used. For example, an Allocation may have usage flags
292 * specifying that it can be used from a script as well as input to a
293 * Sampler. A developer must synchronize across these different usages using
294 * syncAll(int) in order to ensure that different users of the Allocation have
295 * a consistent view of memory. For example, in the case where an Allocation is
296 * used as the output of one kernel and as Sampler input in a later kernel, a
297 * developer must call syncAll(RS_ALLOCATION_USAGE_SCRIPT) prior to launching the
298 * second kernel to ensure correctness.
299 */
Tim Murray84bf2b82012-10-31 16:03:16 -0700300class Allocation : public BaseObj {
301protected:
Tim Murray89daad62013-07-29 14:30:02 -0700302 sp<const Type> mType;
Tim Murray84bf2b82012-10-31 16:03:16 -0700303 uint32_t mUsage;
Tim Murray89daad62013-07-29 14:30:02 -0700304 sp<Allocation> mAdaptedAllocation;
Tim Murray84bf2b82012-10-31 16:03:16 -0700305
306 bool mConstrainedLOD;
307 bool mConstrainedFace;
308 bool mConstrainedY;
309 bool mConstrainedZ;
310 bool mReadAllowed;
311 bool mWriteAllowed;
Miao Wange5428e62015-03-10 15:29:40 -0700312 bool mAutoPadding;
Tim Murray84bf2b82012-10-31 16:03:16 -0700313 uint32_t mSelectedY;
314 uint32_t mSelectedZ;
315 uint32_t mSelectedLOD;
316 RsAllocationCubemapFace mSelectedFace;
317
318 uint32_t mCurrentDimX;
319 uint32_t mCurrentDimY;
320 uint32_t mCurrentDimZ;
321 uint32_t mCurrentCount;
322
323 void * getIDSafe() const;
324 void updateCacheInfo(sp<const Type> t);
325
326 Allocation(void *id, sp<RS> rs, sp<const Type> t, uint32_t usage);
327
Miao Wange5428e62015-03-10 15:29:40 -0700328 void validateIsInt64();
Tim Murray84bf2b82012-10-31 16:03:16 -0700329 void validateIsInt32();
330 void validateIsInt16();
331 void validateIsInt8();
332 void validateIsFloat32();
Miao Wange5428e62015-03-10 15:29:40 -0700333 void validateIsFloat64();
Tim Murray84bf2b82012-10-31 16:03:16 -0700334 void validateIsObject();
335
336 virtual void updateFromNative();
337
338 void validate2DRange(uint32_t xoff, uint32_t yoff, uint32_t w, uint32_t h);
Tim Murray9d24ae62013-08-30 12:17:14 -0700339 void validate3DRange(uint32_t xoff, uint32_t yoff, uint32_t zoff,
340 uint32_t w, uint32_t h, uint32_t d);
Tim Murray84bf2b82012-10-31 16:03:16 -0700341
342public:
Tim Murray75e877d2013-09-11 14:45:20 -0700343
344 /**
345 * Return Type for the allocation.
346 * @return pointer to underlying Type
347 */
Stephen Hinesa180b7d2013-08-21 12:42:13 -0700348 sp<const Type> getType() const {
Tim Murray84bf2b82012-10-31 16:03:16 -0700349 return mType;
350 }
351
Tim Murray75e877d2013-09-11 14:45:20 -0700352 /**
Miao Wange5428e62015-03-10 15:29:40 -0700353 * Enable/Disable AutoPadding for Vec3 elements.
354 *
355 * @param useAutoPadding True: enable AutoPadding; flase: disable AutoPadding
356 *
357 */
358 void setAutoPadding(bool useAutoPadding) {
359 mAutoPadding = useAutoPadding;
360 }
361
362 /**
Tim Murray75e877d2013-09-11 14:45:20 -0700363 * Propagate changes from one usage of the Allocation to other usages of the Allocation.
364 * @param[in] srcLocation source location with changes to propagate elsewhere
365 */
Tim Murray84bf2b82012-10-31 16:03:16 -0700366 void syncAll(RsAllocationUsageType srcLocation);
Miao Wang09d2dd22015-03-18 20:09:20 -0700367
368 /**
369 * Send a buffer to the output stream. The contents of the Allocation will
370 * be undefined after this operation. This operation is only valid if
371 * USAGE_IO_OUTPUT is set on the Allocation.
372 */
Tim Murray84bf2b82012-10-31 16:03:16 -0700373 void ioSendOutput();
Miao Wang09d2dd22015-03-18 20:09:20 -0700374
375 /**
376 * Receive the latest input into the Allocation. This operation
377 * is only valid if USAGE_IO_INPUT is set on the Allocation.
378 */
Tim Murray84bf2b82012-10-31 16:03:16 -0700379 void ioGetInput();
380
Miao Wang09d2dd22015-03-18 20:09:20 -0700381#if !defined(RS_SERVER) && !defined(RS_COMPATIBILITY_LIB)
382 /**
383 * Returns the handle to a raw buffer that is being managed by the screen
384 * compositor. This operation is only valid for Allocations with USAGE_IO_INPUT.
385 * @return Surface associated with allocation
386 */
387 sp<Surface> getSurface();
388
389 /**
390 * Associate a Surface with this Allocation. This
391 * operation is only valid for Allocations with USAGE_IO_OUTPUT.
392 * @param[in] s Surface to associate with allocation
393 */
394 void setSurface(sp<Surface> s);
395#endif
396
Tim Murray75e877d2013-09-11 14:45:20 -0700397 /**
398 * Generate a mipmap chain. This is only valid if the Type of the Allocation
399 * includes mipmaps. This function will generate a complete set of mipmaps
400 * from the top level LOD and place them into the script memory space. If
401 * the Allocation is also using other memory spaces, a call to
402 * syncAll(Allocation.USAGE_SCRIPT) is required.
403 */
Tim Murray84bf2b82012-10-31 16:03:16 -0700404 void generateMipmaps();
Tim Murray509ea5c2012-11-13 11:56:40 -0800405
Tim Murray75e877d2013-09-11 14:45:20 -0700406 /**
407 * Copy an array into part of this Allocation.
408 * @param[in] off offset of first Element to be overwritten
409 * @param[in] count number of Elements to copy
410 * @param[in] data array from which to copy
411 */
Tim Murray0b93e302012-11-15 14:56:54 -0800412 void copy1DRangeFrom(uint32_t off, size_t count, const void *data);
Tim Murray75e877d2013-09-11 14:45:20 -0700413
414 /**
415 * Copy part of an Allocation into part of this Allocation.
416 * @param[in] off offset of first Element to be overwritten
417 * @param[in] count number of Elements to copy
418 * @param[in] data Allocation from which to copy
419 * @param[in] dataOff offset of first Element in data to copy
420 */
Tim Murraya4cbc2b2012-11-14 17:18:08 -0800421 void copy1DRangeFrom(uint32_t off, size_t count, sp<const Allocation> data, uint32_t dataOff);
Tim Murray84bf2b82012-10-31 16:03:16 -0700422
Tim Murray75e877d2013-09-11 14:45:20 -0700423 /**
424 * Copy an array into part of this Allocation.
425 * @param[in] off offset of first Element to be overwritten
426 * @param[in] count number of Elements to copy
427 * @param[in] data array from which to copy
428 */
Tim Murray0b93e302012-11-15 14:56:54 -0800429 void copy1DRangeTo(uint32_t off, size_t count, void *data);
430
Tim Murray75e877d2013-09-11 14:45:20 -0700431 /**
432 * Copy entire array to an Allocation.
433 * @param[in] data array from which to copy
434 */
Tim Murray0b93e302012-11-15 14:56:54 -0800435 void copy1DFrom(const void* data);
Tim Murray75e877d2013-09-11 14:45:20 -0700436
437 /**
438 * Copy entire Allocation to an array.
439 * @param[in] data destination array
440 */
Tim Murray0b93e302012-11-15 14:56:54 -0800441 void copy1DTo(void* data);
Tim Murraya4cbc2b2012-11-14 17:18:08 -0800442
Tim Murray75e877d2013-09-11 14:45:20 -0700443 /**
444 * Copy from an array into a rectangular region in this Allocation. The
445 * array is assumed to be tightly packed.
446 * @param[in] xoff X offset of region to update in this Allocation
447 * @param[in] yoff Y offset of region to update in this Allocation
448 * @param[in] w Width of region to update
449 * @param[in] h Height of region to update
450 * @param[in] data Array from which to copy
451 */
Tim Murray84bf2b82012-10-31 16:03:16 -0700452 void copy2DRangeFrom(uint32_t xoff, uint32_t yoff, uint32_t w, uint32_t h,
Tim Murray0b93e302012-11-15 14:56:54 -0800453 const void *data);
Tim Murray7b3e3092012-11-16 13:32:24 -0800454
Tim Murray75e877d2013-09-11 14:45:20 -0700455 /**
456 * Copy from this Allocation into a rectangular region in an array. The
457 * array is assumed to be tightly packed.
458 * @param[in] xoff X offset of region to copy from this Allocation
459 * @param[in] yoff Y offset of region to copy from this Allocation
460 * @param[in] w Width of region to update
461 * @param[in] h Height of region to update
462 * @param[in] data destination array
463 */
Tim Murray7b3e3092012-11-16 13:32:24 -0800464 void copy2DRangeTo(uint32_t xoff, uint32_t yoff, uint32_t w, uint32_t h,
465 void *data);
466
Tim Murray75e877d2013-09-11 14:45:20 -0700467 /**
468 * Copy from an Allocation into a rectangular region in this Allocation.
469 * @param[in] xoff X offset of region to update in this Allocation
470 * @param[in] yoff Y offset of region to update in this Allocation
471 * @param[in] w Width of region to update
472 * @param[in] h Height of region to update
473 * @param[in] data Allocation from which to copy
474 * @param[in] dataXoff X offset of region to copy from in data
475 * @param[in] dataYoff Y offset of region to copy from in data
476 */
Tim Murray0b93e302012-11-15 14:56:54 -0800477 void copy2DRangeFrom(uint32_t xoff, uint32_t yoff, uint32_t w, uint32_t h,
478 sp<const Allocation> data, uint32_t dataXoff, uint32_t dataYoff);
Tim Murray84bf2b82012-10-31 16:03:16 -0700479
Tim Murray75e877d2013-09-11 14:45:20 -0700480 /**
481 * Copy from a strided array into a rectangular region in this Allocation.
482 * @param[in] xoff X offset of region to update in this Allocation
483 * @param[in] yoff Y offset of region to update in this Allocation
484 * @param[in] w Width of region to update
485 * @param[in] h Height of region to update
486 * @param[in] data array from which to copy
487 * @param[in] stride stride of data in bytes
488 */
Tim Murray358747a2012-11-26 13:52:04 -0800489 void copy2DStridedFrom(uint32_t xoff, uint32_t yoff, uint32_t w, uint32_t h,
490 const void *data, size_t stride);
Tim Murray75e877d2013-09-11 14:45:20 -0700491
492 /**
493 * Copy from a strided array into this Allocation.
494 * @param[in] data array from which to copy
495 * @param[in] stride stride of data in bytes
496 */
Tim Murray358747a2012-11-26 13:52:04 -0800497 void copy2DStridedFrom(const void *data, size_t stride);
498
Tim Murray75e877d2013-09-11 14:45:20 -0700499 /**
500 * Copy from a rectangular region in this Allocation into a strided array.
501 * @param[in] xoff X offset of region to update in this Allocation
502 * @param[in] yoff Y offset of region to update in this Allocation
503 * @param[in] w Width of region to update
504 * @param[in] h Height of region to update
505 * @param[in] data destination array
506 * @param[in] stride stride of data in bytes
507 */
Tim Murray358747a2012-11-26 13:52:04 -0800508 void copy2DStridedTo(uint32_t xoff, uint32_t yoff, uint32_t w, uint32_t h,
509 void *data, size_t stride);
Tim Murray75e877d2013-09-11 14:45:20 -0700510
511 /**
512 * Copy this Allocation into a strided array.
513 * @param[in] data destination array
514 * @param[in] stride stride of data in bytes
515 */
Tim Murray358747a2012-11-26 13:52:04 -0800516 void copy2DStridedTo(void *data, size_t stride);
517
Tim Murray75e877d2013-09-11 14:45:20 -0700518
519 /**
520 * Copy from an array into a 3D region in this Allocation. The
521 * array is assumed to be tightly packed.
522 * @param[in] xoff X offset of region to update in this Allocation
523 * @param[in] yoff Y offset of region to update in this Allocation
524 * @param[in] zoff Z offset of region to update in this Allocation
525 * @param[in] w Width of region to update
526 * @param[in] h Height of region to update
527 * @param[in] d Depth of region to update
528 * @param[in] data Array from which to copy
529 */
Tim Murray9d24ae62013-08-30 12:17:14 -0700530 void copy3DRangeFrom(uint32_t xoff, uint32_t yoff, uint32_t zoff, uint32_t w,
531 uint32_t h, uint32_t d, const void* data);
532
Tim Murray75e877d2013-09-11 14:45:20 -0700533 /**
534 * Copy from an Allocation into a 3D region in this Allocation.
535 * @param[in] xoff X offset of region to update in this Allocation
536 * @param[in] yoff Y offset of region to update in this Allocation
537 * @param[in] zoff Z offset of region to update in this Allocation
538 * @param[in] w Width of region to update
539 * @param[in] h Height of region to update
540 * @param[in] d Depth of region to update
541 * @param[in] data Allocation from which to copy
542 * @param[in] dataXoff X offset of region in data to copy from
543 * @param[in] dataYoff Y offset of region in data to copy from
544 * @param[in] dataZoff Z offset of region in data to copy from
545 */
Tim Murray9d24ae62013-08-30 12:17:14 -0700546 void copy3DRangeFrom(uint32_t xoff, uint32_t yoff, uint32_t zoff,
547 uint32_t w, uint32_t h, uint32_t d,
548 sp<const Allocation> data,
549 uint32_t dataXoff, uint32_t dataYoff, uint32_t dataZoff);
Tim Murray84bf2b82012-10-31 16:03:16 -0700550
Tim Murray75e877d2013-09-11 14:45:20 -0700551 /**
Miao Wange5428e62015-03-10 15:29:40 -0700552 * Copy a 3D region in this Allocation into an array. The
553 * array is assumed to be tightly packed.
554 * @param[in] xoff X offset of region to update in this Allocation
555 * @param[in] yoff Y offset of region to update in this Allocation
556 * @param[in] zoff Z offset of region to update in this Allocation
557 * @param[in] w Width of region to update
558 * @param[in] h Height of region to update
559 * @param[in] d Depth of region to update
560 * @param[in] data Array from which to copy
561 */
562 void copy3DRangeTo(uint32_t xoff, uint32_t yoff, uint32_t zoff, uint32_t w,
563 uint32_t h, uint32_t d, void* data);
564
565 /**
Tim Murray75e877d2013-09-11 14:45:20 -0700566 * Creates an Allocation for use by scripts with a given Type.
567 * @param[in] rs Context to which the Allocation will belong
568 * @param[in] type Type of the Allocation
Stephen Hines8f615d62013-12-20 12:23:32 -0800569 * @param[in] mipmaps desired mipmap behavior for the Allocation
Tim Murray75e877d2013-09-11 14:45:20 -0700570 * @param[in] usage usage for the Allocation
571 * @return new Allocation
572 */
Tim Murray84bf2b82012-10-31 16:03:16 -0700573 static sp<Allocation> createTyped(sp<RS> rs, sp<const Type> type,
Stephen Hines8f615d62013-12-20 12:23:32 -0800574 RsAllocationMipmapControl mipmaps, uint32_t usage);
Tim Murray75e877d2013-09-11 14:45:20 -0700575
576 /**
577 * Creates an Allocation for use by scripts with a given Type and a backing pointer. For use
578 * with RS_ALLOCATION_USAGE_SHARED.
579 * @param[in] rs Context to which the Allocation will belong
580 * @param[in] type Type of the Allocation
Stephen Hines8f615d62013-12-20 12:23:32 -0800581 * @param[in] mipmaps desired mipmap behavior for the Allocation
Tim Murray75e877d2013-09-11 14:45:20 -0700582 * @param[in] usage usage for the Allocation
583 * @param[in] pointer existing backing store to use for this Allocation if possible
584 * @return new Allocation
585 */
Tim Murray84bf2b82012-10-31 16:03:16 -0700586 static sp<Allocation> createTyped(sp<RS> rs, sp<const Type> type,
Stephen Hines8f615d62013-12-20 12:23:32 -0800587 RsAllocationMipmapControl mipmaps, uint32_t usage, void * pointer);
Tim Murray84bf2b82012-10-31 16:03:16 -0700588
Tim Murray75e877d2013-09-11 14:45:20 -0700589 /**
590 * Creates an Allocation for use by scripts with a given Type with no mipmaps.
591 * @param[in] rs Context to which the Allocation will belong
592 * @param[in] type Type of the Allocation
593 * @param[in] usage usage for the Allocation
594 * @return new Allocation
595 */
Tim Murray84bf2b82012-10-31 16:03:16 -0700596 static sp<Allocation> createTyped(sp<RS> rs, sp<const Type> type,
597 uint32_t usage = RS_ALLOCATION_USAGE_SCRIPT);
Tim Murray75e877d2013-09-11 14:45:20 -0700598 /**
599 * Creates an Allocation with a specified number of given elements.
600 * @param[in] rs Context to which the Allocation will belong
601 * @param[in] e Element used in the Allocation
602 * @param[in] count Number of elements of the Allocation
603 * @param[in] usage usage for the Allocation
604 * @return new Allocation
605 */
Tim Murray84bf2b82012-10-31 16:03:16 -0700606 static sp<Allocation> createSized(sp<RS> rs, sp<const Element> e, size_t count,
607 uint32_t usage = RS_ALLOCATION_USAGE_SCRIPT);
Tim Murray75e877d2013-09-11 14:45:20 -0700608
609 /**
610 * Creates a 2D Allocation with a specified number of given elements.
611 * @param[in] rs Context to which the Allocation will belong
612 * @param[in] e Element used in the Allocation
613 * @param[in] x Width in Elements of the Allocation
614 * @param[in] y Height of the Allocation
615 * @param[in] usage usage for the Allocation
616 * @return new Allocation
617 */
Tim Murray684726c2012-11-14 11:57:42 -0800618 static sp<Allocation> createSized2D(sp<RS> rs, sp<const Element> e,
619 size_t x, size_t y,
620 uint32_t usage = RS_ALLOCATION_USAGE_SCRIPT);
621
Tim Murray84bf2b82012-10-31 16:03:16 -0700622
Jason Samsb8a94e22014-02-24 17:52:32 -0800623 /**
624 * Get the backing pointer for a USAGE_SHARED allocation.
625 * @param[in] stride optional parameter. when non-NULL, will contain
626 * stride in bytes of a 2D Allocation
627 * @return pointer to data
628 */
629 void * getPointer(size_t *stride = NULL);
Tim Murray84bf2b82012-10-31 16:03:16 -0700630};
631
Tim Murray75e877d2013-09-11 14:45:20 -0700632 /**
633 * An Element represents one item within an Allocation. An Element is roughly
634 * equivalent to a C type in a RenderScript kernel. Elements may be basic
635 * or complex. Some basic elements are:
636
637 * - A single float value (equivalent to a float in a kernel)
638 * - A four-element float vector (equivalent to a float4 in a kernel)
639 * - An unsigned 32-bit integer (equivalent to an unsigned int in a kernel)
640 * - A single signed 8-bit integer (equivalent to a char in a kernel)
641
642 * Basic Elements are comprised of a Element.DataType and a
643 * Element.DataKind. The DataType encodes C type information of an Element,
644 * while the DataKind encodes how that Element should be interpreted by a
645 * Sampler. Note that Allocation objects with DataKind USER cannot be used as
646 * input for a Sampler. In general, Allocation objects that are intended for
647 * use with a Sampler should use bitmap-derived Elements such as
648 * Element::RGBA_8888.
649 */
650
651
Tim Murray84bf2b82012-10-31 16:03:16 -0700652class Element : public BaseObj {
653public:
654 bool isComplex();
Tim Murray75e877d2013-09-11 14:45:20 -0700655
656 /**
657 * Elements could be simple, such as an int or a float, or a structure with
658 * multiple sub-elements, such as a collection of floats, float2,
659 * float4. This function returns zero for simple elements or the number of
660 * sub-elements otherwise.
661 * @return number of sub-elements
662 */
Tim Murray84bf2b82012-10-31 16:03:16 -0700663 size_t getSubElementCount() {
Miao Wangbc10dff2015-04-03 17:44:55 -0700664 return mVisibleElementMapSize;
Tim Murray84bf2b82012-10-31 16:03:16 -0700665 }
666
Tim Murray75e877d2013-09-11 14:45:20 -0700667 /**
668 * For complex Elements, this returns the sub-element at a given index.
669 * @param[in] index index of sub-element
670 * @return sub-element
671 */
Tim Murray84bf2b82012-10-31 16:03:16 -0700672 sp<const Element> getSubElement(uint32_t index);
Tim Murray75e877d2013-09-11 14:45:20 -0700673
674 /**
675 * For complex Elements, this returns the name of the sub-element at a given
676 * index.
677 * @param[in] index index of sub-element
678 * @return name of sub-element
679 */
Tim Murray84bf2b82012-10-31 16:03:16 -0700680 const char * getSubElementName(uint32_t index);
Tim Murray75e877d2013-09-11 14:45:20 -0700681
682 /**
683 * For complex Elements, this returns the size of the sub-element at a given
684 * index.
685 * @param[in] index index of sub-element
686 * @return size of sub-element
687 */
Tim Murray84bf2b82012-10-31 16:03:16 -0700688 size_t getSubElementArraySize(uint32_t index);
Tim Murray75e877d2013-09-11 14:45:20 -0700689
690 /**
691 * Returns the location of a sub-element within a complex Element.
692 * @param[in] index index of sub-element
693 * @return offset in bytes
694 */
Tim Murray84bf2b82012-10-31 16:03:16 -0700695 uint32_t getSubElementOffsetBytes(uint32_t index);
Tim Murray75e877d2013-09-11 14:45:20 -0700696
697 /**
698 * Returns the data type used for the Element.
699 * @return data type
700 */
Tim Murray84bf2b82012-10-31 16:03:16 -0700701 RsDataType getDataType() const {
702 return mType;
703 }
704
Tim Murray75e877d2013-09-11 14:45:20 -0700705 /**
706 * Returns the data kind used for the Element.
707 * @return data kind
708 */
Tim Murray84bf2b82012-10-31 16:03:16 -0700709 RsDataKind getDataKind() const {
710 return mKind;
711 }
712
Tim Murray75e877d2013-09-11 14:45:20 -0700713 /**
714 * Returns the size in bytes of the Element.
715 * @return size in bytes
716 */
Tim Murray84bf2b82012-10-31 16:03:16 -0700717 size_t getSizeBytes() const {
718 return mSizeBytes;
719 }
720
Tim Murray75e877d2013-09-11 14:45:20 -0700721 /**
722 * Returns the number of vector components for this Element.
723 * @return number of vector components
724 */
Tim Murray10913a52013-08-20 17:19:47 -0700725 uint32_t getVectorSize() const {
726 return mVectorSize;
727 }
728
Tim Murray75e877d2013-09-11 14:45:20 -0700729 /**
730 * Utility function for returning an Element containing a single bool.
731 * @param[in] rs RenderScript context
732 * @return Element
733 */
Tim Murray84bf2b82012-10-31 16:03:16 -0700734 static sp<const Element> BOOLEAN(sp<RS> rs);
Tim Murray75e877d2013-09-11 14:45:20 -0700735 /**
736 * Utility function for returning an Element containing a single unsigned char.
737 * @param[in] rs RenderScript context
738 * @return Element
739 */
Tim Murray84bf2b82012-10-31 16:03:16 -0700740 static sp<const Element> U8(sp<RS> rs);
Tim Murray75e877d2013-09-11 14:45:20 -0700741 /**
742 * Utility function for returning an Element containing a single signed char.
743 * @param[in] rs RenderScript context
744 * @return Element
745 */
Tim Murray84bf2b82012-10-31 16:03:16 -0700746 static sp<const Element> I8(sp<RS> rs);
Tim Murray75e877d2013-09-11 14:45:20 -0700747 /**
748 * Utility function for returning an Element containing a single unsigned short.
749 * @param[in] rs RenderScript context
750 * @return Element
751 */
Tim Murray84bf2b82012-10-31 16:03:16 -0700752 static sp<const Element> U16(sp<RS> rs);
Tim Murray75e877d2013-09-11 14:45:20 -0700753 /**
754 * Utility function for returning an Element containing a single signed short.
755 * @param[in] rs RenderScript context
756 * @return Element
757 */
Tim Murray84bf2b82012-10-31 16:03:16 -0700758 static sp<const Element> I16(sp<RS> rs);
Tim Murray75e877d2013-09-11 14:45:20 -0700759 /**
760 * Utility function for returning an Element containing a single unsigned int.
761 * @param[in] rs RenderScript context
762 * @return Element
763 */
Tim Murray84bf2b82012-10-31 16:03:16 -0700764 static sp<const Element> U32(sp<RS> rs);
Tim Murray75e877d2013-09-11 14:45:20 -0700765 /**
766 * Utility function for returning an Element containing a single signed int.
767 * @param[in] rs RenderScript context
768 * @return Element
769 */
Tim Murray84bf2b82012-10-31 16:03:16 -0700770 static sp<const Element> I32(sp<RS> rs);
Tim Murray75e877d2013-09-11 14:45:20 -0700771 /**
772 * Utility function for returning an Element containing a single unsigned long long.
773 * @param[in] rs RenderScript context
774 * @return Element
775 */
Tim Murray84bf2b82012-10-31 16:03:16 -0700776 static sp<const Element> U64(sp<RS> rs);
Tim Murray75e877d2013-09-11 14:45:20 -0700777 /**
778 * Utility function for returning an Element containing a single signed long long.
779 * @param[in] rs RenderScript context
780 * @return Element
781 */
Tim Murray84bf2b82012-10-31 16:03:16 -0700782 static sp<const Element> I64(sp<RS> rs);
Tim Murray75e877d2013-09-11 14:45:20 -0700783 /**
784 * Utility function for returning an Element containing a single float.
785 * @param[in] rs RenderScript context
786 * @return Element
787 */
Tim Murray84bf2b82012-10-31 16:03:16 -0700788 static sp<const Element> F32(sp<RS> rs);
Tim Murray75e877d2013-09-11 14:45:20 -0700789 /**
790 * Utility function for returning an Element containing a single double.
791 * @param[in] rs RenderScript context
792 * @return Element
793 */
Tim Murray84bf2b82012-10-31 16:03:16 -0700794 static sp<const Element> F64(sp<RS> rs);
Tim Murray75e877d2013-09-11 14:45:20 -0700795 /**
796 * Utility function for returning an Element containing a single Element.
797 * @param[in] rs RenderScript context
798 * @return Element
799 */
Tim Murray84bf2b82012-10-31 16:03:16 -0700800 static sp<const Element> ELEMENT(sp<RS> rs);
Tim Murray75e877d2013-09-11 14:45:20 -0700801 /**
802 * Utility function for returning an Element containing a single Type.
803 * @param[in] rs RenderScript context
804 * @return Element
805 */
Tim Murray84bf2b82012-10-31 16:03:16 -0700806 static sp<const Element> TYPE(sp<RS> rs);
Tim Murray75e877d2013-09-11 14:45:20 -0700807 /**
808 * Utility function for returning an Element containing a single Allocation.
809 * @param[in] rs RenderScript context
810 * @return Element
811 */
Tim Murray84bf2b82012-10-31 16:03:16 -0700812 static sp<const Element> ALLOCATION(sp<RS> rs);
Tim Murray75e877d2013-09-11 14:45:20 -0700813 /**
814 * Utility function for returning an Element containing a single Sampler.
815 * @param[in] rs RenderScript context
816 * @return Element
817 */
Tim Murray84bf2b82012-10-31 16:03:16 -0700818 static sp<const Element> SAMPLER(sp<RS> rs);
Tim Murray75e877d2013-09-11 14:45:20 -0700819 /**
820 * Utility function for returning an Element containing a single Script.
821 * @param[in] rs RenderScript context
822 * @return Element
823 */
Tim Murray84bf2b82012-10-31 16:03:16 -0700824 static sp<const Element> SCRIPT(sp<RS> rs);
Tim Murray75e877d2013-09-11 14:45:20 -0700825 /**
826 * Utility function for returning an Element containing an ALPHA_8 pixel.
827 * @param[in] rs RenderScript context
828 * @return Element
829 */
Tim Murray84bf2b82012-10-31 16:03:16 -0700830 static sp<const Element> A_8(sp<RS> rs);
Tim Murray75e877d2013-09-11 14:45:20 -0700831 /**
832 * Utility function for returning an Element containing an RGB_565 pixel.
833 * @param[in] rs RenderScript context
834 * @return Element
835 */
Tim Murray84bf2b82012-10-31 16:03:16 -0700836 static sp<const Element> RGB_565(sp<RS> rs);
Tim Murray75e877d2013-09-11 14:45:20 -0700837 /**
838 * Utility function for returning an Element containing an RGB_888 pixel.
839 * @param[in] rs RenderScript context
840 * @return Element
841 */
Tim Murray84bf2b82012-10-31 16:03:16 -0700842 static sp<const Element> RGB_888(sp<RS> rs);
Tim Murray75e877d2013-09-11 14:45:20 -0700843 /**
844 * Utility function for returning an Element containing an RGBA_5551 pixel.
845 * @param[in] rs RenderScript context
846 * @return Element
847 */
Tim Murray84bf2b82012-10-31 16:03:16 -0700848 static sp<const Element> RGBA_5551(sp<RS> rs);
Tim Murray75e877d2013-09-11 14:45:20 -0700849 /**
850 * Utility function for returning an Element containing an RGBA_4444 pixel.
851 * @param[in] rs RenderScript context
852 * @return Element
853 */
Tim Murray84bf2b82012-10-31 16:03:16 -0700854 static sp<const Element> RGBA_4444(sp<RS> rs);
Tim Murray75e877d2013-09-11 14:45:20 -0700855 /**
856 * Utility function for returning an Element containing an RGBA_8888 pixel.
857 * @param[in] rs RenderScript context
858 * @return Element
859 */
Tim Murray84bf2b82012-10-31 16:03:16 -0700860 static sp<const Element> RGBA_8888(sp<RS> rs);
861
Tim Murray75e877d2013-09-11 14:45:20 -0700862 /**
863 * Utility function for returning an Element containing a float2.
864 * @param[in] rs RenderScript context
865 * @return Element
866 */
Tim Murray84bf2b82012-10-31 16:03:16 -0700867 static sp<const Element> F32_2(sp<RS> rs);
Tim Murray75e877d2013-09-11 14:45:20 -0700868 /**
869 * Utility function for returning an Element containing a float3.
870 * @param[in] rs RenderScript context
871 * @return Element
872 */
Tim Murray84bf2b82012-10-31 16:03:16 -0700873 static sp<const Element> F32_3(sp<RS> rs);
Tim Murray75e877d2013-09-11 14:45:20 -0700874 /**
875 * Utility function for returning an Element containing a float4.
876 * @param[in] rs RenderScript context
877 * @return Element
878 */
Tim Murray84bf2b82012-10-31 16:03:16 -0700879 static sp<const Element> F32_4(sp<RS> rs);
Tim Murray75e877d2013-09-11 14:45:20 -0700880 /**
881 * Utility function for returning an Element containing a double2.
882 * @param[in] rs RenderScript context
883 * @return Element
884 */
Tim Murray84bf2b82012-10-31 16:03:16 -0700885 static sp<const Element> F64_2(sp<RS> rs);
Tim Murray75e877d2013-09-11 14:45:20 -0700886 /**
887 * Utility function for returning an Element containing a double3.
888 * @param[in] rs RenderScript context
889 * @return Element
890 */
Tim Murray84bf2b82012-10-31 16:03:16 -0700891 static sp<const Element> F64_3(sp<RS> rs);
Tim Murray75e877d2013-09-11 14:45:20 -0700892 /**
893 * Utility function for returning an Element containing a double4.
894 * @param[in] rs RenderScript context
895 * @return Element
896 */
Tim Murray84bf2b82012-10-31 16:03:16 -0700897 static sp<const Element> F64_4(sp<RS> rs);
Tim Murray75e877d2013-09-11 14:45:20 -0700898 /**
899 * Utility function for returning an Element containing a uchar2.
900 * @param[in] rs RenderScript context
901 * @return Element
902 */
Tim Murray84bf2b82012-10-31 16:03:16 -0700903 static sp<const Element> U8_2(sp<RS> rs);
Tim Murray75e877d2013-09-11 14:45:20 -0700904 /**
905 * Utility function for returning an Element containing a uchar3.
906 * @param[in] rs RenderScript context
907 * @return Element
908 */
Tim Murray84bf2b82012-10-31 16:03:16 -0700909 static sp<const Element> U8_3(sp<RS> rs);
Tim Murray75e877d2013-09-11 14:45:20 -0700910 /**
911 * Utility function for returning an Element containing a uchar4.
912 * @param[in] rs RenderScript context
913 * @return Element
914 */
Tim Murray84bf2b82012-10-31 16:03:16 -0700915 static sp<const Element> U8_4(sp<RS> rs);
Tim Murray75e877d2013-09-11 14:45:20 -0700916 /**
917 * Utility function for returning an Element containing a char2.
918 * @param[in] rs RenderScript context
919 * @return Element
920 */
Tim Murray84bf2b82012-10-31 16:03:16 -0700921 static sp<const Element> I8_2(sp<RS> rs);
Tim Murray75e877d2013-09-11 14:45:20 -0700922 /**
923 * Utility function for returning an Element containing a char3.
924 * @param[in] rs RenderScript context
925 * @return Element
926 */
Tim Murray84bf2b82012-10-31 16:03:16 -0700927 static sp<const Element> I8_3(sp<RS> rs);
Tim Murray75e877d2013-09-11 14:45:20 -0700928 /**
929 * Utility function for returning an Element containing a char4.
930 * @param[in] rs RenderScript context
931 * @return Element
932 */
Tim Murray84bf2b82012-10-31 16:03:16 -0700933 static sp<const Element> I8_4(sp<RS> rs);
Tim Murray75e877d2013-09-11 14:45:20 -0700934 /**
935 * Utility function for returning an Element containing a ushort2.
936 * @param[in] rs RenderScript context
937 * @return Element
938 */
Tim Murray84bf2b82012-10-31 16:03:16 -0700939 static sp<const Element> U16_2(sp<RS> rs);
Tim Murray75e877d2013-09-11 14:45:20 -0700940 /**
941 * Utility function for returning an Element containing a ushort3.
942 * @param[in] rs RenderScript context
943 * @return Element
944 */
Tim Murray84bf2b82012-10-31 16:03:16 -0700945 static sp<const Element> U16_3(sp<RS> rs);
Tim Murray75e877d2013-09-11 14:45:20 -0700946 /**
947 * Utility function for returning an Element containing a ushort4.
948 * @param[in] rs RenderScript context
949 * @return Element
950 */
Tim Murray84bf2b82012-10-31 16:03:16 -0700951 static sp<const Element> U16_4(sp<RS> rs);
Tim Murray75e877d2013-09-11 14:45:20 -0700952 /**
953 * Utility function for returning an Element containing a short2.
954 * @param[in] rs RenderScript context
955 * @return Element
956 */
Tim Murray84bf2b82012-10-31 16:03:16 -0700957 static sp<const Element> I16_2(sp<RS> rs);
Tim Murray75e877d2013-09-11 14:45:20 -0700958 /**
959 * Utility function for returning an Element containing a short3.
960 * @param[in] rs RenderScript context
961 * @return Element
962 */
Tim Murray84bf2b82012-10-31 16:03:16 -0700963 static sp<const Element> I16_3(sp<RS> rs);
Tim Murray75e877d2013-09-11 14:45:20 -0700964 /**
965 * Utility function for returning an Element containing a short4.
966 * @param[in] rs RenderScript context
967 * @return Element
968 */
Tim Murray84bf2b82012-10-31 16:03:16 -0700969 static sp<const Element> I16_4(sp<RS> rs);
Tim Murray75e877d2013-09-11 14:45:20 -0700970 /**
971 * Utility function for returning an Element containing a uint2.
972 * @param[in] rs RenderScript context
973 * @return Element
974 */
Tim Murray84bf2b82012-10-31 16:03:16 -0700975 static sp<const Element> U32_2(sp<RS> rs);
Tim Murray75e877d2013-09-11 14:45:20 -0700976 /**
977 * Utility function for returning an Element containing a uint3.
978 * @param[in] rs RenderScript context
979 * @return Element
980 */
Tim Murray84bf2b82012-10-31 16:03:16 -0700981 static sp<const Element> U32_3(sp<RS> rs);
Tim Murray75e877d2013-09-11 14:45:20 -0700982 /**
983 * Utility function for returning an Element containing a uint4.
984 * @param[in] rs RenderScript context
985 * @return Element
986 */
Tim Murray84bf2b82012-10-31 16:03:16 -0700987 static sp<const Element> U32_4(sp<RS> rs);
Tim Murray75e877d2013-09-11 14:45:20 -0700988 /**
989 * Utility function for returning an Element containing an int2.
990 * @param[in] rs RenderScript context
991 * @return Element
992 */
Tim Murray84bf2b82012-10-31 16:03:16 -0700993 static sp<const Element> I32_2(sp<RS> rs);
Tim Murray75e877d2013-09-11 14:45:20 -0700994 /**
995 * Utility function for returning an Element containing an int3.
996 * @param[in] rs RenderScript context
997 * @return Element
998 */
Tim Murray84bf2b82012-10-31 16:03:16 -0700999 static sp<const Element> I32_3(sp<RS> rs);
Tim Murray75e877d2013-09-11 14:45:20 -07001000 /**
1001 * Utility function for returning an Element containing an int4.
1002 * @param[in] rs RenderScript context
1003 * @return Element
1004 */
Tim Murray84bf2b82012-10-31 16:03:16 -07001005 static sp<const Element> I32_4(sp<RS> rs);
Tim Murray75e877d2013-09-11 14:45:20 -07001006 /**
1007 * Utility function for returning an Element containing a ulong2.
1008 * @param[in] rs RenderScript context
1009 * @return Element
1010 */
Tim Murray84bf2b82012-10-31 16:03:16 -07001011 static sp<const Element> U64_2(sp<RS> rs);
Tim Murray75e877d2013-09-11 14:45:20 -07001012 /**
1013 * Utility function for returning an Element containing a ulong3.
1014 * @param[in] rs RenderScript context
1015 * @return Element
1016 */
Tim Murray84bf2b82012-10-31 16:03:16 -07001017 static sp<const Element> U64_3(sp<RS> rs);
Tim Murray75e877d2013-09-11 14:45:20 -07001018 /**
1019 * Utility function for returning an Element containing a ulong4.
1020 * @param[in] rs RenderScript context
1021 * @return Element
1022 */
Tim Murray84bf2b82012-10-31 16:03:16 -07001023 static sp<const Element> U64_4(sp<RS> rs);
Tim Murray75e877d2013-09-11 14:45:20 -07001024 /**
1025 * Utility function for returning an Element containing a long2.
1026 * @param[in] rs RenderScript context
1027 * @return Element
1028 */
Tim Murray84bf2b82012-10-31 16:03:16 -07001029 static sp<const Element> I64_2(sp<RS> rs);
Tim Murray75e877d2013-09-11 14:45:20 -07001030 /**
1031 * Utility function for returning an Element containing a long3.
1032 * @param[in] rs RenderScript context
1033 * @return Element
1034 */
Tim Murray84bf2b82012-10-31 16:03:16 -07001035 static sp<const Element> I64_3(sp<RS> rs);
Tim Murray75e877d2013-09-11 14:45:20 -07001036 /**
1037 * Utility function for returning an Element containing a long4.
1038 * @param[in] rs RenderScript context
1039 * @return Element
1040 */
Tim Murray84bf2b82012-10-31 16:03:16 -07001041 static sp<const Element> I64_4(sp<RS> rs);
Tim Murray75e877d2013-09-11 14:45:20 -07001042 /**
1043 * Utility function for returning an Element containing a YUV pixel.
1044 * @param[in] rs RenderScript context
1045 * @return Element
1046 */
Tim Murrayeb4426d2013-08-27 15:30:16 -07001047 static sp<const Element> YUV(sp<RS> rs);
Tim Murray75e877d2013-09-11 14:45:20 -07001048 /**
1049 * Utility function for returning an Element containing an rs_matrix_4x4.
1050 * @param[in] rs RenderScript context
1051 * @return Element
1052 */
Tim Murray84bf2b82012-10-31 16:03:16 -07001053 static sp<const Element> MATRIX_4X4(sp<RS> rs);
Tim Murray75e877d2013-09-11 14:45:20 -07001054 /**
1055 * Utility function for returning an Element containing an rs_matrix_3x3.
1056 * @param[in] rs RenderScript context
1057 * @return Element
1058 */
Tim Murray84bf2b82012-10-31 16:03:16 -07001059 static sp<const Element> MATRIX_3X3(sp<RS> rs);
Tim Murray75e877d2013-09-11 14:45:20 -07001060 /**
1061 * Utility function for returning an Element containing an rs_matrix_2x2.
1062 * @param[in] rs RenderScript context
1063 * @return Element
1064 */
Tim Murray84bf2b82012-10-31 16:03:16 -07001065 static sp<const Element> MATRIX_2X2(sp<RS> rs);
1066
Tim Murray84bf2b82012-10-31 16:03:16 -07001067 void updateFromNative();
Tim Murray75e877d2013-09-11 14:45:20 -07001068
1069 /**
1070 * Create an Element with a given DataType.
1071 * @param[in] rs RenderScript context
1072 * @param[in] dt data type
1073 * @return Element
1074 */
Tim Murray84bf2b82012-10-31 16:03:16 -07001075 static sp<const Element> createUser(sp<RS> rs, RsDataType dt);
Tim Murray75e877d2013-09-11 14:45:20 -07001076 /**
1077 * Create a vector Element with the given DataType
1078 * @param[in] rs RenderScript
1079 * @param[in] dt DataType
1080 * @param[in] size vector size
1081 * @return Element
1082 */
Tim Murray84bf2b82012-10-31 16:03:16 -07001083 static sp<const Element> createVector(sp<RS> rs, RsDataType dt, uint32_t size);
Tim Murray75e877d2013-09-11 14:45:20 -07001084 /**
1085 * Create an Element with a given DataType and DataKind.
1086 * @param[in] rs RenderScript context
1087 * @param[in] dt DataType
1088 * @param[in] dk DataKind
1089 * @return Element
1090 */
Tim Murray84bf2b82012-10-31 16:03:16 -07001091 static sp<const Element> createPixel(sp<RS> rs, RsDataType dt, RsDataKind dk);
Tim Murray75e877d2013-09-11 14:45:20 -07001092
1093 /**
1094 * Returns true if the Element can interoperate with this Element.
1095 * @param[in] e Element to compare
1096 * @return true if Elements can interoperate
1097 */
Tim Murray10913a52013-08-20 17:19:47 -07001098 bool isCompatible(sp<const Element>e) const;
Tim Murray84bf2b82012-10-31 16:03:16 -07001099
Tim Murray75e877d2013-09-11 14:45:20 -07001100 /**
1101 * Builder class for producing complex elements with matching field and name
1102 * pairs. The builder starts empty. The order in which elements are added is
1103 * retained for the layout in memory.
1104 */
Tim Murray84bf2b82012-10-31 16:03:16 -07001105 class Builder {
1106 private:
Tim Murray35609072013-12-03 11:36:03 -08001107 RS* mRS;
Miao Wangbc10dff2015-04-03 17:44:55 -07001108 size_t mElementsCount;
1109 size_t mElementsVecSize;
1110 sp<const Element> * mElements;
1111 char ** mElementNames;
1112 size_t * mElementNameLengths;
1113 uint32_t * mArraySizes;
Tim Murray84bf2b82012-10-31 16:03:16 -07001114 bool mSkipPadding;
1115
1116 public:
1117 Builder(sp<RS> rs);
1118 ~Builder();
Miao Wangbc10dff2015-04-03 17:44:55 -07001119 void add(sp<const Element> e, const char * name, uint32_t arraySize = 1);
Tim Murray84bf2b82012-10-31 16:03:16 -07001120 sp<const Element> create();
1121 };
1122
Stephen Hines7d1b7572013-08-22 01:24:06 -07001123protected:
1124 Element(void *id, sp<RS> rs,
Miao Wangbc10dff2015-04-03 17:44:55 -07001125 sp<const Element> * elements,
1126 size_t elementCount,
1127 const char ** elementNames,
1128 size_t * elementNameLengths,
1129 uint32_t * arraySizes);
Stephen Hines7d1b7572013-08-22 01:24:06 -07001130 Element(void *id, sp<RS> rs, RsDataType dt, RsDataKind dk, bool norm, uint32_t size);
1131 Element(sp<RS> rs);
1132 virtual ~Element();
1133
Tim Murray84bf2b82012-10-31 16:03:16 -07001134private:
1135 void updateVisibleSubElements();
1136
Miao Wangbc10dff2015-04-03 17:44:55 -07001137 size_t mElementsCount;
1138 size_t mVisibleElementMapSize;
1139
1140 sp<const Element> * mElements;
1141 char ** mElementNames;
1142 size_t * mElementNameLengths;
1143 uint32_t * mArraySizes;
1144 uint32_t * mVisibleElementMap;
1145 uint32_t * mOffsetInBytes;
Tim Murray84bf2b82012-10-31 16:03:16 -07001146
1147 RsDataType mType;
1148 RsDataKind mKind;
1149 bool mNormalized;
1150 size_t mSizeBytes;
1151 size_t mVectorSize;
1152};
1153
Stephen Hines2c7206e2012-11-14 19:47:01 -08001154class FieldPacker {
1155protected:
1156 unsigned char* mData;
1157 size_t mPos;
1158 size_t mLen;
1159
1160public:
1161 FieldPacker(size_t len)
Tim Murray89daad62013-07-29 14:30:02 -07001162 : mPos(0), mLen(len) {
1163 mData = new unsigned char[len];
1164 }
Stephen Hines2c7206e2012-11-14 19:47:01 -08001165
1166 virtual ~FieldPacker() {
1167 delete [] mData;
1168 }
1169
1170 void align(size_t v) {
1171 if ((v & (v - 1)) != 0) {
Tim Murrayab716362013-08-12 12:37:18 -07001172 // ALOGE("Non-power-of-two alignment: %zu", v);
Stephen Hines2c7206e2012-11-14 19:47:01 -08001173 return;
1174 }
1175
1176 while ((mPos & (v - 1)) != 0) {
1177 mData[mPos++] = 0;
1178 }
1179 }
1180
1181 void reset() {
1182 mPos = 0;
1183 }
1184
1185 void reset(size_t i) {
1186 if (i >= mLen) {
Tim Murrayab716362013-08-12 12:37:18 -07001187 // ALOGE("Out of bounds: i (%zu) >= len (%zu)", i, mLen);
Stephen Hines2c7206e2012-11-14 19:47:01 -08001188 return;
1189 }
1190 mPos = i;
1191 }
1192
1193 void skip(size_t i) {
1194 size_t res = mPos + i;
1195 if (res > mLen) {
Tim Murrayab716362013-08-12 12:37:18 -07001196 // ALOGE("Exceeded buffer length: i (%zu) > len (%zu)", i, mLen);
Stephen Hines2c7206e2012-11-14 19:47:01 -08001197 return;
1198 }
1199 mPos = res;
1200 }
1201
1202 void* getData() const {
1203 return mData;
1204 }
1205
1206 size_t getLength() const {
1207 return mLen;
1208 }
1209
1210 template <typename T>
Tim Murray89daad62013-07-29 14:30:02 -07001211 void add(T t) {
Stephen Hines2c7206e2012-11-14 19:47:01 -08001212 align(sizeof(t));
1213 if (mPos + sizeof(t) <= mLen) {
1214 memcpy(&mData[mPos], &t, sizeof(t));
1215 mPos += sizeof(t);
1216 }
1217 }
Stephen Hines43514cd2012-11-16 14:33:47 -08001218
1219 /*
Tim Murray89daad62013-07-29 14:30:02 -07001220 void add(rs_matrix4x4 m) {
1221 for (size_t i = 0; i < 16; i++) {
1222 add(m.m[i]);
1223 }
1224 }
Stephen Hines43514cd2012-11-16 14:33:47 -08001225
Tim Murray89daad62013-07-29 14:30:02 -07001226 void add(rs_matrix3x3 m) {
1227 for (size_t i = 0; i < 9; i++) {
1228 add(m.m[i]);
1229 }
1230 }
Stephen Hines43514cd2012-11-16 14:33:47 -08001231
Tim Murray89daad62013-07-29 14:30:02 -07001232 void add(rs_matrix2x2 m) {
1233 for (size_t i = 0; i < 4; i++) {
1234 add(m.m[i]);
1235 }
1236 }
Stephen Hines43514cd2012-11-16 14:33:47 -08001237 */
1238
Tim Murray89daad62013-07-29 14:30:02 -07001239 void add(sp<BaseObj> obj) {
Stephen Hines43514cd2012-11-16 14:33:47 -08001240 if (obj != NULL) {
1241 add((uint32_t) (uintptr_t) obj->getID());
1242 } else {
1243 add((uint32_t) 0);
1244 }
1245 }
Stephen Hines2c7206e2012-11-14 19:47:01 -08001246};
1247
Tim Murray75e877d2013-09-11 14:45:20 -07001248/**
1249 * A Type describes the Element and dimensions used for an Allocation or a
1250 * parallel operation.
1251 *
1252 * A Type always includes an Element and an X dimension. A Type may be
1253 * multidimensional, up to three dimensions. A nonzero value in the Y or Z
1254 * dimensions indicates that the dimension is present. Note that a Type with
1255 * only a given X dimension and a Type with the same X dimension but Y = 1 are
1256 * not equivalent.
1257 *
1258 * A Type also supports inclusion of level of detail (LOD) or cube map
1259 * faces. LOD and cube map faces are booleans to indicate present or not
1260 * present.
1261 *
1262 * A Type also supports YUV format information to support an Allocation in a YUV
1263 * format. The YUV formats supported are YV12 and NV21.
1264 */
Tim Murray84bf2b82012-10-31 16:03:16 -07001265class Type : public BaseObj {
1266protected:
1267 friend class Allocation;
1268
1269 uint32_t mDimX;
1270 uint32_t mDimY;
1271 uint32_t mDimZ;
Tim Murrayeb4426d2013-08-27 15:30:16 -07001272 RSYuvFormat mYuvFormat;
Tim Murray84bf2b82012-10-31 16:03:16 -07001273 bool mDimMipmaps;
1274 bool mDimFaces;
1275 size_t mElementCount;
1276 sp<const Element> mElement;
1277
Stephen Hines7d1b7572013-08-22 01:24:06 -07001278 Type(void *id, sp<RS> rs);
1279
Tim Murray84bf2b82012-10-31 16:03:16 -07001280 void calcElementCount();
1281 virtual void updateFromNative();
1282
1283public:
1284
Tim Murray75e877d2013-09-11 14:45:20 -07001285 /**
1286 * Returns the YUV format.
1287 * @return YUV format of the Allocation
1288 */
Tim Murrayeb4426d2013-08-27 15:30:16 -07001289 RSYuvFormat getYuvFormat() const {
1290 return mYuvFormat;
1291 }
1292
Tim Murray75e877d2013-09-11 14:45:20 -07001293 /**
1294 * Returns the Element of the Allocation.
1295 * @return YUV format of the Allocation
1296 */
Tim Murray84bf2b82012-10-31 16:03:16 -07001297 sp<const Element> getElement() const {
1298 return mElement;
1299 }
1300
Tim Murray75e877d2013-09-11 14:45:20 -07001301 /**
1302 * Returns the X dimension of the Allocation.
1303 * @return X dimension of the allocation
1304 */
Tim Murray84bf2b82012-10-31 16:03:16 -07001305 uint32_t getX() const {
1306 return mDimX;
1307 }
1308
Tim Murray75e877d2013-09-11 14:45:20 -07001309 /**
1310 * Returns the Y dimension of the Allocation.
1311 * @return Y dimension of the allocation
1312 */
Tim Murray84bf2b82012-10-31 16:03:16 -07001313 uint32_t getY() const {
1314 return mDimY;
1315 }
1316
Tim Murray75e877d2013-09-11 14:45:20 -07001317 /**
1318 * Returns the Z dimension of the Allocation.
1319 * @return Z dimension of the allocation
1320 */
Tim Murray84bf2b82012-10-31 16:03:16 -07001321 uint32_t getZ() const {
1322 return mDimZ;
1323 }
1324
Tim Murray75e877d2013-09-11 14:45:20 -07001325 /**
1326 * Returns true if the Allocation has mipmaps.
1327 * @return true if the Allocation has mipmaps
1328 */
Tim Murray84bf2b82012-10-31 16:03:16 -07001329 bool hasMipmaps() const {
1330 return mDimMipmaps;
1331 }
1332
Tim Murray75e877d2013-09-11 14:45:20 -07001333 /**
1334 * Returns true if the Allocation is a cube map
1335 * @return true if the Allocation is a cube map
1336 */
Tim Murray84bf2b82012-10-31 16:03:16 -07001337 bool hasFaces() const {
1338 return mDimFaces;
1339 }
1340
Tim Murray75e877d2013-09-11 14:45:20 -07001341 /**
1342 * Returns number of accessible Elements in the Allocation
1343 * @return number of accessible Elements in the Allocation
1344 */
Tim Murray84bf2b82012-10-31 16:03:16 -07001345 size_t getCount() const {
1346 return mElementCount;
1347 }
1348
Tim Murray75e877d2013-09-11 14:45:20 -07001349 /**
1350 * Returns size in bytes of all Elements in the Allocation
1351 * @return size in bytes of all Elements in the Allocation
1352 */
Tim Murray84bf2b82012-10-31 16:03:16 -07001353 size_t getSizeBytes() const {
1354 return mElementCount * mElement->getSizeBytes();
1355 }
1356
Tim Murray75e877d2013-09-11 14:45:20 -07001357 /**
1358 * Creates a new Type with the given Element and dimensions.
1359 * @param[in] rs RenderScript context
1360 * @param[in] e Element
1361 * @param[in] dimX X dimension
1362 * @param[in] dimY Y dimension
1363 * @param[in] dimZ Z dimension
1364 * @return new Type
1365 */
Tim Murray96267c22013-02-12 11:25:12 -08001366 static sp<const Type> create(sp<RS> rs, sp<const Element> e, uint32_t dimX, uint32_t dimY, uint32_t dimZ);
Tim Murray84bf2b82012-10-31 16:03:16 -07001367
1368 class Builder {
1369 protected:
Tim Murray35609072013-12-03 11:36:03 -08001370 RS* mRS;
Tim Murray84bf2b82012-10-31 16:03:16 -07001371 uint32_t mDimX;
1372 uint32_t mDimY;
1373 uint32_t mDimZ;
Tim Murrayeb4426d2013-08-27 15:30:16 -07001374 RSYuvFormat mYuvFormat;
Tim Murray84bf2b82012-10-31 16:03:16 -07001375 bool mDimMipmaps;
1376 bool mDimFaces;
1377 sp<const Element> mElement;
1378
1379 public:
1380 Builder(sp<RS> rs, sp<const Element> e);
1381
1382 void setX(uint32_t value);
Stephen Hines7d1b7572013-08-22 01:24:06 -07001383 void setY(uint32_t value);
Tim Murrayeb4426d2013-08-27 15:30:16 -07001384 void setZ(uint32_t value);
1385 void setYuvFormat(RSYuvFormat format);
Tim Murray84bf2b82012-10-31 16:03:16 -07001386 void setMipmaps(bool value);
1387 void setFaces(bool value);
1388 sp<const Type> create();
1389 };
1390
1391};
1392
Tim Murray75e877d2013-09-11 14:45:20 -07001393/**
1394 * The parent class for all executable Scripts. This should not be used by applications.
1395 */
Tim Murray84bf2b82012-10-31 16:03:16 -07001396class Script : public BaseObj {
1397private:
1398
1399protected:
1400 Script(void *id, sp<RS> rs);
1401 void forEach(uint32_t slot, sp<const Allocation> in, sp<const Allocation> out,
1402 const void *v, size_t) const;
Matt Wala394e9a62015-08-03 11:35:55 -07001403 void reduce(uint32_t slot, sp<const Allocation> in, sp<const Allocation> out,
1404 const RsScriptCall *sc) const;
Tim Murray84bf2b82012-10-31 16:03:16 -07001405 void bindAllocation(sp<Allocation> va, uint32_t slot) const;
1406 void setVar(uint32_t index, const void *, size_t len) const;
1407 void setVar(uint32_t index, sp<const BaseObj> o) const;
1408 void invoke(uint32_t slot, const void *v, size_t len) const;
1409
1410
1411 void invoke(uint32_t slot) const {
1412 invoke(slot, NULL, 0);
1413 }
1414 void setVar(uint32_t index, float v) const {
1415 setVar(index, &v, sizeof(v));
1416 }
1417 void setVar(uint32_t index, double v) const {
1418 setVar(index, &v, sizeof(v));
1419 }
1420 void setVar(uint32_t index, int32_t v) const {
1421 setVar(index, &v, sizeof(v));
1422 }
Jon Parrb05c8502015-03-13 14:41:58 +00001423 void setVar(uint32_t index, uint32_t v) const {
1424 setVar(index, &v, sizeof(v));
1425 }
Tim Murray84bf2b82012-10-31 16:03:16 -07001426 void setVar(uint32_t index, int64_t v) const {
1427 setVar(index, &v, sizeof(v));
1428 }
1429 void setVar(uint32_t index, bool v) const {
1430 setVar(index, &v, sizeof(v));
1431 }
1432
1433public:
1434 class FieldBase {
1435 protected:
1436 sp<const Element> mElement;
1437 sp<Allocation> mAllocation;
1438
1439 void init(sp<RS> rs, uint32_t dimx, uint32_t usages = 0);
1440
1441 public:
1442 sp<const Element> getElement() {
1443 return mElement;
1444 }
1445
1446 sp<const Type> getType() {
1447 return mAllocation->getType();
1448 }
1449
1450 sp<const Allocation> getAllocation() {
1451 return mAllocation;
1452 }
1453
1454 //void updateAllocation();
1455 };
1456};
1457
Tim Murray75e877d2013-09-11 14:45:20 -07001458/**
1459 * The parent class for all user-defined scripts. This is intended to be used by auto-generated code only.
1460 */
Tim Murray84bf2b82012-10-31 16:03:16 -07001461class ScriptC : public Script {
1462protected:
1463 ScriptC(sp<RS> rs,
1464 const void *codeTxt, size_t codeLength,
1465 const char *cachedName, size_t cachedNameLength,
1466 const char *cacheDir, size_t cacheDirLength);
1467
1468};
1469
Tim Murray75e877d2013-09-11 14:45:20 -07001470/**
1471 * The parent class for all script intrinsics. Intrinsics provide highly optimized implementations of
1472 * basic functions. This is not intended to be used directly.
1473 */
Tim Murray7f0d5682012-11-08 16:35:24 -08001474class ScriptIntrinsic : public Script {
1475 protected:
Tim Murray10913a52013-08-20 17:19:47 -07001476 sp<const Element> mElement;
Tim Murray3cd44af2012-11-14 11:25:27 -08001477 ScriptIntrinsic(sp<RS> rs, int id, sp<const Element> e);
Tim Murrayb27b1812013-08-05 14:00:40 -07001478 virtual ~ScriptIntrinsic();
Tim Murray7f0d5682012-11-08 16:35:24 -08001479};
1480
Tim Murray75e877d2013-09-11 14:45:20 -07001481/**
1482 * Intrinsic for converting RGB to RGBA by using a 3D lookup table. The incoming
1483 * r,g,b values are use as normalized x,y,z coordinates into a 3D
1484 * allocation. The 8 nearest values are sampled and linearly interpolated. The
1485 * result is placed in the output.
1486 */
Tim Murray89daad62013-07-29 14:30:02 -07001487class ScriptIntrinsic3DLUT : public ScriptIntrinsic {
Tim Murray21fa7a02013-08-15 16:25:03 -07001488 private:
Tim Murray89daad62013-07-29 14:30:02 -07001489 ScriptIntrinsic3DLUT(sp<RS> rs, sp<const Element> e);
Tim Murray21fa7a02013-08-15 16:25:03 -07001490 public:
Tim Murray75e877d2013-09-11 14:45:20 -07001491 /**
1492 * Supported Element types are U8_4. Default lookup table is identity.
1493 * @param[in] rs RenderScript context
1494 * @param[in] e Element
1495 * @return new ScriptIntrinsic
1496 */
Tim Murray21fa7a02013-08-15 16:25:03 -07001497 static sp<ScriptIntrinsic3DLUT> create(sp<RS> rs, sp<const Element> e);
Tim Murray75e877d2013-09-11 14:45:20 -07001498
1499 /**
1500 * Launch the intrinsic.
1501 * @param[in] ain input Allocation
1502 * @param[in] aout output Allocation
1503 */
Tim Murray89daad62013-07-29 14:30:02 -07001504 void forEach(sp<Allocation> ain, sp<Allocation> aout);
Tim Murray75e877d2013-09-11 14:45:20 -07001505
1506 /**
1507 * Sets the lookup table. The lookup table must use the same Element as the
1508 * intrinsic.
1509 * @param[in] lut new lookup table
1510 */
Tim Murray89daad62013-07-29 14:30:02 -07001511 void setLUT(sp<Allocation> lut);
1512};
1513
Tim Murray75e877d2013-09-11 14:45:20 -07001514/**
1515 * Intrinsic kernel for blending two Allocations.
1516 */
Tim Murray7f0d5682012-11-08 16:35:24 -08001517class ScriptIntrinsicBlend : public ScriptIntrinsic {
Tim Murray21fa7a02013-08-15 16:25:03 -07001518 private:
Tim Murray89daad62013-07-29 14:30:02 -07001519 ScriptIntrinsicBlend(sp<RS> rs, sp<const Element> e);
Tim Murray21fa7a02013-08-15 16:25:03 -07001520 public:
Tim Murray75e877d2013-09-11 14:45:20 -07001521 /**
1522 * Supported Element types are U8_4.
1523 * @param[in] rs RenderScript context
1524 * @param[in] e Element
1525 * @return new ScriptIntrinsicBlend
1526 */
Tim Murray21fa7a02013-08-15 16:25:03 -07001527 static sp<ScriptIntrinsicBlend> create(sp<RS> rs, sp<const Element> e);
Tim Murray75e877d2013-09-11 14:45:20 -07001528 /**
1529 * sets dst = {0, 0, 0, 0}
1530 * @param[in] in input Allocation
1531 * @param[in] out output Allocation
1532 */
1533 void forEachClear(sp<Allocation> in, sp<Allocation> out);
1534 /**
1535 * Sets dst = src
1536 * @param[in] in input Allocation
1537 * @param[in] out output Allocation
1538 */
1539 void forEachSrc(sp<Allocation> in, sp<Allocation> out);
1540 /**
1541 * Sets dst = dst (NOP)
1542 * @param[in] in input Allocation
1543 * @param[in] out output Allocation
1544 */
1545 void forEachDst(sp<Allocation> in, sp<Allocation> out);
1546 /**
1547 * Sets dst = src + dst * (1.0 - src.a)
1548 * @param[in] in input Allocation
1549 * @param[in] out output Allocation
1550 */
1551 void forEachSrcOver(sp<Allocation> in, sp<Allocation> out);
1552 /**
1553 * Sets dst = dst + src * (1.0 - dst.a)
1554 * @param[in] in input Allocation
1555 * @param[in] out output Allocation
1556 */
1557 void forEachDstOver(sp<Allocation> in, sp<Allocation> out);
1558 /**
1559 * Sets dst = src * dst.a
1560 * @param[in] in input Allocation
1561 * @param[in] out output Allocation
1562 */
1563 void forEachSrcIn(sp<Allocation> in, sp<Allocation> out);
1564 /**
1565 * Sets dst = dst * src.a
1566 * @param[in] in input Allocation
1567 * @param[in] out output Allocation
1568 */
1569 void forEachDstIn(sp<Allocation> in, sp<Allocation> out);
1570 /**
1571 * Sets dst = src * (1.0 - dst.a)
1572 * @param[in] in input Allocation
1573 * @param[in] out output Allocation
1574 */
1575 void forEachSrcOut(sp<Allocation> in, sp<Allocation> out);
1576 /**
1577 * Sets dst = dst * (1.0 - src.a)
1578 * @param[in] in input Allocation
1579 * @param[in] out output Allocation
1580 */
1581 void forEachDstOut(sp<Allocation> in, sp<Allocation> out);
1582 /**
1583 * Sets dst.rgb = src.rgb * dst.a + (1.0 - src.a) * dst.rgb
1584 * @param[in] in input Allocation
1585 * @param[in] out output Allocation
1586 */
1587 void forEachSrcAtop(sp<Allocation> in, sp<Allocation> out);
1588 /**
1589 * Sets dst.rgb = dst.rgb * src.a + (1.0 - dst.a) * src.rgb
1590 * @param[in] in input Allocation
1591 * @param[in] out output Allocation
1592 */
1593 void forEachDstAtop(sp<Allocation> in, sp<Allocation> out);
1594 /**
1595 * Sets dst = {src.r ^ dst.r, src.g ^ dst.g, src.b ^ dst.b, src.a ^ dst.a}
1596 * @param[in] in input Allocation
1597 * @param[in] out output Allocation
1598 */
1599 void forEachXor(sp<Allocation> in, sp<Allocation> out);
1600 /**
1601 * Sets dst = src * dst
1602 * @param[in] in input Allocation
1603 * @param[in] out output Allocation
1604 */
1605 void forEachMultiply(sp<Allocation> in, sp<Allocation> out);
1606 /**
1607 * Sets dst = min(src + dst, 1.0)
1608 * @param[in] in input Allocation
1609 * @param[in] out output Allocation
1610 */
1611 void forEachAdd(sp<Allocation> in, sp<Allocation> out);
1612 /**
1613 * Sets dst = max(dst - src, 0.0)
1614 * @param[in] in input Allocation
1615 * @param[in] out output Allocation
1616 */
1617 void forEachSubtract(sp<Allocation> in, sp<Allocation> out);
Tim Murray7f0d5682012-11-08 16:35:24 -08001618};
Tim Murray84bf2b82012-10-31 16:03:16 -07001619
Tim Murray75e877d2013-09-11 14:45:20 -07001620/**
1621 * Intrinsic Gausian blur filter. Applies a Gaussian blur of the specified
1622 * radius to all elements of an Allocation.
1623 */
Tim Murray8f1e60c2012-11-13 12:25:11 -08001624class ScriptIntrinsicBlur : public ScriptIntrinsic {
Tim Murray21fa7a02013-08-15 16:25:03 -07001625 private:
Tim Murray89daad62013-07-29 14:30:02 -07001626 ScriptIntrinsicBlur(sp<RS> rs, sp<const Element> e);
Tim Murray21fa7a02013-08-15 16:25:03 -07001627 public:
Tim Murray75e877d2013-09-11 14:45:20 -07001628 /**
1629 * Supported Element types are U8 and U8_4.
1630 * @param[in] rs RenderScript context
1631 * @param[in] e Element
1632 * @return new ScriptIntrinsicBlur
1633 */
Tim Murray21fa7a02013-08-15 16:25:03 -07001634 static sp<ScriptIntrinsicBlur> create(sp<RS> rs, sp<const Element> e);
Tim Murray75e877d2013-09-11 14:45:20 -07001635 /**
1636 * Sets the input of the blur.
1637 * @param[in] in input Allocation
1638 */
Tim Murray21fa7a02013-08-15 16:25:03 -07001639 void setInput(sp<Allocation> in);
Tim Murray75e877d2013-09-11 14:45:20 -07001640 /**
1641 * Runs the intrinsic.
1642 * @param[in] output Allocation
1643 */
Tim Murray21fa7a02013-08-15 16:25:03 -07001644 void forEach(sp<Allocation> out);
Tim Murray75e877d2013-09-11 14:45:20 -07001645 /**
1646 * Sets the radius of the blur. The supported range is 0 < radius <= 25.
1647 * @param[in] radius radius of the blur
1648 */
Tim Murray8f1e60c2012-11-13 12:25:11 -08001649 void setRadius(float radius);
1650};
1651
Tim Murray75e877d2013-09-11 14:45:20 -07001652/**
1653 * Intrinsic for applying a color matrix to allocations. This has the
1654 * same effect as loading each element and converting it to a
1655 * F32_N, multiplying the result by the 4x4 color matrix
1656 * as performed by rsMatrixMultiply() and writing it to the output
1657 * after conversion back to U8_N or F32_N.
1658 */
Tim Murray89daad62013-07-29 14:30:02 -07001659class ScriptIntrinsicColorMatrix : public ScriptIntrinsic {
Tim Murray21fa7a02013-08-15 16:25:03 -07001660 private:
Tim Murray89daad62013-07-29 14:30:02 -07001661 ScriptIntrinsicColorMatrix(sp<RS> rs, sp<const Element> e);
Tim Murray21fa7a02013-08-15 16:25:03 -07001662 public:
Tim Murray75e877d2013-09-11 14:45:20 -07001663 /**
1664 * Creates a new intrinsic.
1665 * @param[in] rs RenderScript context
1666 * @return new ScriptIntrinsicColorMatrix
1667 */
Tim Murrayaae73c92013-09-03 17:05:46 -07001668 static sp<ScriptIntrinsicColorMatrix> create(sp<RS> rs);
Tim Murray75e877d2013-09-11 14:45:20 -07001669 /**
1670 * Applies the color matrix. Supported types are U8 and F32 with
1671 * vector lengths between 1 and 4.
1672 * @param[in] in input Allocation
1673 * @param[out] out output Allocation
1674 */
Tim Murray89daad62013-07-29 14:30:02 -07001675 void forEach(sp<Allocation> in, sp<Allocation> out);
Tim Murray75e877d2013-09-11 14:45:20 -07001676 /**
1677 * Set the value to be added after the color matrix has been
1678 * applied. The default value is {0, 0, 0, 0}.
1679 * @param[in] add float[4] of values
1680 */
Tim Murray10913a52013-08-20 17:19:47 -07001681 void setAdd(float* add);
Tim Murray75e877d2013-09-11 14:45:20 -07001682
1683 /**
1684 * Set the color matrix which will be applied to each cell of the
1685 * image. The alpha channel will be copied.
1686 *
1687 * @param[in] m float[9] of values
1688 */
Tim Murray89daad62013-07-29 14:30:02 -07001689 void setColorMatrix3(float* m);
Tim Murray75e877d2013-09-11 14:45:20 -07001690 /**
1691 * Set the color matrix which will be applied to each cell of the
1692 * image.
1693 *
1694 * @param[in] m float[16] of values
1695 */
Tim Murray89daad62013-07-29 14:30:02 -07001696 void setColorMatrix4(float* m);
Tim Murray75e877d2013-09-11 14:45:20 -07001697 /**
1698 * Set a color matrix to convert from RGB to luminance. The alpha
1699 * channel will be a copy.
1700 */
Tim Murray89daad62013-07-29 14:30:02 -07001701 void setGreyscale();
Tim Murray75e877d2013-09-11 14:45:20 -07001702 /**
1703 * Set the matrix to convert from RGB to YUV with a direct copy of
1704 * the 4th channel.
1705 */
Tim Murray89daad62013-07-29 14:30:02 -07001706 void setRGBtoYUV();
Tim Murray75e877d2013-09-11 14:45:20 -07001707 /**
1708 * Set the matrix to convert from YUV to RGB with a direct copy of
1709 * the 4th channel.
1710 */
Tim Murray89daad62013-07-29 14:30:02 -07001711 void setYUVtoRGB();
1712};
1713
Tim Murray75e877d2013-09-11 14:45:20 -07001714/**
1715 * Intrinsic for applying a 3x3 convolve to an allocation.
1716 */
Tim Murray89daad62013-07-29 14:30:02 -07001717class ScriptIntrinsicConvolve3x3 : public ScriptIntrinsic {
Tim Murray21fa7a02013-08-15 16:25:03 -07001718 private:
Tim Murray89daad62013-07-29 14:30:02 -07001719 ScriptIntrinsicConvolve3x3(sp<RS> rs, sp<const Element> e);
Tim Murray21fa7a02013-08-15 16:25:03 -07001720 public:
Tim Murray75e877d2013-09-11 14:45:20 -07001721 /**
1722 * Supported types U8 and F32 with vector lengths between 1 and
1723 * 4. The default convolution kernel is the identity.
1724 * @param[in] rs RenderScript context
1725 * @param[in] e Element
1726 * @return new ScriptIntrinsicConvolve3x3
1727 */
Tim Murray21fa7a02013-08-15 16:25:03 -07001728 static sp<ScriptIntrinsicConvolve3x3> create(sp<RS> rs, sp<const Element> e);
Tim Murray75e877d2013-09-11 14:45:20 -07001729 /**
1730 * Sets input for intrinsic.
1731 * @param[in] in input Allocation
1732 */
Tim Murray89daad62013-07-29 14:30:02 -07001733 void setInput(sp<Allocation> in);
Tim Murray75e877d2013-09-11 14:45:20 -07001734 /**
1735 * Launches the intrinsic.
1736 * @param[in] out output Allocation
1737 */
Tim Murray89daad62013-07-29 14:30:02 -07001738 void forEach(sp<Allocation> out);
Tim Murray75e877d2013-09-11 14:45:20 -07001739 /**
1740 * Sets convolution kernel.
1741 * @param[in] v float[9] of values
1742 */
Tim Murray89daad62013-07-29 14:30:02 -07001743 void setCoefficients(float* v);
1744};
1745
Tim Murray75e877d2013-09-11 14:45:20 -07001746/**
1747 * Intrinsic for applying a 5x5 convolve to an allocation.
1748 */
Tim Murray89daad62013-07-29 14:30:02 -07001749class ScriptIntrinsicConvolve5x5 : public ScriptIntrinsic {
Tim Murray21fa7a02013-08-15 16:25:03 -07001750 private:
Tim Murray89daad62013-07-29 14:30:02 -07001751 ScriptIntrinsicConvolve5x5(sp<RS> rs, sp<const Element> e);
Tim Murray21fa7a02013-08-15 16:25:03 -07001752 public:
Tim Murray75e877d2013-09-11 14:45:20 -07001753 /**
1754 * Supported types U8 and F32 with vector lengths between 1 and
1755 * 4. The default convolution kernel is the identity.
1756 * @param[in] rs RenderScript context
1757 * @param[in] e Element
1758 * @return new ScriptIntrinsicConvolve5x5
1759 */
Tim Murray21fa7a02013-08-15 16:25:03 -07001760 static sp<ScriptIntrinsicConvolve5x5> create(sp<RS> rs, sp<const Element> e);
Tim Murray75e877d2013-09-11 14:45:20 -07001761 /**
1762 * Sets input for intrinsic.
1763 * @param[in] in input Allocation
1764 */
Tim Murray89daad62013-07-29 14:30:02 -07001765 void setInput(sp<Allocation> in);
Tim Murray75e877d2013-09-11 14:45:20 -07001766 /**
1767 * Launches the intrinsic.
1768 * @param[in] out output Allocation
1769 */
Tim Murray89daad62013-07-29 14:30:02 -07001770 void forEach(sp<Allocation> out);
Tim Murray75e877d2013-09-11 14:45:20 -07001771 /**
1772 * Sets convolution kernel.
1773 * @param[in] v float[25] of values
1774 */
Tim Murray89daad62013-07-29 14:30:02 -07001775 void setCoefficients(float* v);
1776};
1777
Tim Murray75e877d2013-09-11 14:45:20 -07001778/**
1779 * Intrinsic for computing a histogram.
1780 */
Tim Murrayb27b1812013-08-05 14:00:40 -07001781class ScriptIntrinsicHistogram : public ScriptIntrinsic {
Tim Murray21fa7a02013-08-15 16:25:03 -07001782 private:
Tim Murrayb27b1812013-08-05 14:00:40 -07001783 ScriptIntrinsicHistogram(sp<RS> rs, sp<const Element> e);
Tim Murray10913a52013-08-20 17:19:47 -07001784 sp<Allocation> mOut;
Tim Murray21fa7a02013-08-15 16:25:03 -07001785 public:
Tim Murray75e877d2013-09-11 14:45:20 -07001786 /**
1787 * Create an intrinsic for calculating the histogram of an uchar
1788 * or uchar4 image.
1789 *
1790 * Supported elements types are U8_4, U8_3, U8_2, and U8.
1791 *
1792 * @param[in] rs The RenderScript context
1793 * @param[in] e Element type for inputs
1794 *
1795 * @return ScriptIntrinsicHistogram
1796 */
Jon Parrb05c8502015-03-13 14:41:58 +00001797 static sp<ScriptIntrinsicHistogram> create(sp<RS> rs, sp<const Element> e);
Tim Murray75e877d2013-09-11 14:45:20 -07001798 /**
1799 * Set the output of the histogram. 32 bit integer types are
1800 * supported.
1801 *
1802 * @param[in] aout The output allocation
1803 */
Tim Murrayb27b1812013-08-05 14:00:40 -07001804 void setOutput(sp<Allocation> aout);
Tim Murray75e877d2013-09-11 14:45:20 -07001805 /**
1806 * Set the coefficients used for the dot product calculation. The
1807 * default is {0.299f, 0.587f, 0.114f, 0.f}.
1808 *
1809 * Coefficients must be >= 0 and sum to 1.0 or less.
1810 *
1811 * @param[in] r Red coefficient
1812 * @param[in] g Green coefficient
1813 * @param[in] b Blue coefficient
1814 * @param[in] a Alpha coefficient
1815 */
Tim Murrayb27b1812013-08-05 14:00:40 -07001816 void setDotCoefficients(float r, float g, float b, float a);
Tim Murray75e877d2013-09-11 14:45:20 -07001817 /**
1818 * Process an input buffer and place the histogram into the output
1819 * allocation. The output allocation may be a narrower vector size
1820 * than the input. In this case the vector size of the output is
1821 * used to determine how many of the input channels are used in
1822 * the computation. This is useful if you have an RGBA input
1823 * buffer but only want the histogram for RGB.
1824 *
1825 * 1D and 2D input allocations are supported.
1826 *
1827 * @param[in] ain The input image
1828 */
Tim Murrayb27b1812013-08-05 14:00:40 -07001829 void forEach(sp<Allocation> ain);
Tim Murray75e877d2013-09-11 14:45:20 -07001830 /**
1831 * Process an input buffer and place the histogram into the output
1832 * allocation. The dot product of the input channel and the
1833 * coefficients from 'setDotCoefficients' are used to calculate
1834 * the output values.
1835 *
1836 * 1D and 2D input allocations are supported.
1837 *
1838 * @param ain The input image
1839 */
Tim Murrayb27b1812013-08-05 14:00:40 -07001840 void forEach_dot(sp<Allocation> ain);
1841};
1842
Tim Murray75e877d2013-09-11 14:45:20 -07001843/**
1844 * Intrinsic for applying a per-channel lookup table. Each channel of
1845 * the input has an independant lookup table. The tables are 256
1846 * entries in size and can cover the full value range of U8_4.
1847 **/
Tim Murrayb27b1812013-08-05 14:00:40 -07001848class ScriptIntrinsicLUT : public ScriptIntrinsic {
1849 private:
1850 sp<Allocation> LUT;
1851 bool mDirty;
1852 unsigned char mCache[1024];
Tim Murray2acce992013-08-28 14:23:31 -07001853 void setTable(unsigned int offset, unsigned char base, unsigned int length, unsigned char* lutValues);
Tim Murray21fa7a02013-08-15 16:25:03 -07001854 ScriptIntrinsicLUT(sp<RS> rs, sp<const Element> e);
Tim Murrayb27b1812013-08-05 14:00:40 -07001855
Tim Murray89daad62013-07-29 14:30:02 -07001856 public:
Tim Murray75e877d2013-09-11 14:45:20 -07001857 /**
1858 * Supported elements types are U8_4.
1859 *
1860 * The defaults tables are identity.
1861 *
1862 * @param[in] rs The RenderScript context
1863 * @param[in] e Element type for intputs and outputs
1864 *
1865 * @return ScriptIntrinsicLUT
1866 */
Tim Murray21fa7a02013-08-15 16:25:03 -07001867 static sp<ScriptIntrinsicLUT> create(sp<RS> rs, sp<const Element> e);
Tim Murray75e877d2013-09-11 14:45:20 -07001868 /**
1869 * Invoke the kernel and apply the lookup to each cell of ain and
1870 * copy to aout.
1871 *
1872 * @param[in] ain Input allocation
1873 * @param[in] aout Output allocation
1874 */
Tim Murray89daad62013-07-29 14:30:02 -07001875 void forEach(sp<Allocation> ain, sp<Allocation> aout);
Tim Murray75e877d2013-09-11 14:45:20 -07001876 /**
1877 * Sets entries in LUT for the red channel.
1878 * @param[in] base base of region to update
1879 * @param[in] length length of region to update
1880 * @param[in] lutValues LUT values to use
1881 */
Tim Murray2acce992013-08-28 14:23:31 -07001882 void setRed(unsigned char base, unsigned int length, unsigned char* lutValues);
Tim Murray75e877d2013-09-11 14:45:20 -07001883 /**
1884 * Sets entries in LUT for the green channel.
1885 * @param[in] base base of region to update
1886 * @param[in] length length of region to update
1887 * @param[in] lutValues LUT values to use
1888 */
Tim Murray2acce992013-08-28 14:23:31 -07001889 void setGreen(unsigned char base, unsigned int length, unsigned char* lutValues);
Tim Murray75e877d2013-09-11 14:45:20 -07001890 /**
1891 * Sets entries in LUT for the blue channel.
1892 * @param[in] base base of region to update
1893 * @param[in] length length of region to update
1894 * @param[in] lutValues LUT values to use
1895 */
Tim Murray2acce992013-08-28 14:23:31 -07001896 void setBlue(unsigned char base, unsigned int length, unsigned char* lutValues);
Tim Murray75e877d2013-09-11 14:45:20 -07001897 /**
1898 * Sets entries in LUT for the alpha channel.
1899 * @param[in] base base of region to update
1900 * @param[in] length length of region to update
1901 * @param[in] lutValues LUT values to use
1902 */
Tim Murray2acce992013-08-28 14:23:31 -07001903 void setAlpha(unsigned char base, unsigned int length, unsigned char* lutValues);
Tim Murrayb27b1812013-08-05 14:00:40 -07001904 virtual ~ScriptIntrinsicLUT();
1905};
1906
Tim Murray75e877d2013-09-11 14:45:20 -07001907/**
Miao Wange5428e62015-03-10 15:29:40 -07001908 * Intrinsic for performing a resize of a 2D allocation.
1909 */
1910class ScriptIntrinsicResize : public ScriptIntrinsic {
1911 private:
1912 sp<Allocation> mInput;
1913 ScriptIntrinsicResize(sp<RS> rs, sp<const Element> e);
1914 public:
1915 /**
1916 * Supported Element types are U8_4. Default lookup table is identity.
1917 * @param[in] rs RenderScript context
1918 * @param[in] e Element
1919 * @return new ScriptIntrinsic
1920 */
1921 static sp<ScriptIntrinsicResize> create(sp<RS> rs);
1922
1923 /**
1924 * Resize copy the input allocation to the output specified. The
1925 * Allocation is rescaled if necessary using bi-cubic
1926 * interpolation.
1927 * @param[in] ain input Allocation
1928 * @param[in] aout output Allocation
1929 */
1930 void forEach_bicubic(sp<Allocation> aout);
1931
1932 /**
1933 * Set the input of the resize.
1934 * @param[in] lut new lookup table
1935 */
1936 void setInput(sp<Allocation> ain);
1937};
1938
1939/**
Tim Murray75e877d2013-09-11 14:45:20 -07001940 * Intrinsic for converting an Android YUV buffer to RGB.
1941 *
1942 * The input allocation should be supplied in a supported YUV format
1943 * as a YUV element Allocation. The output is RGBA; the alpha channel
1944 * will be set to 255.
1945 */
Tim Murray89daad62013-07-29 14:30:02 -07001946class ScriptIntrinsicYuvToRGB : public ScriptIntrinsic {
Tim Murray21fa7a02013-08-15 16:25:03 -07001947 private:
Tim Murrayb27b1812013-08-05 14:00:40 -07001948 ScriptIntrinsicYuvToRGB(sp<RS> rs, sp<const Element> e);
Tim Murray21fa7a02013-08-15 16:25:03 -07001949 public:
Tim Murray75e877d2013-09-11 14:45:20 -07001950 /**
1951 * Create an intrinsic for converting YUV to RGB.
1952 *
1953 * Supported elements types are U8_4.
1954 *
1955 * @param[in] rs The RenderScript context
1956 * @param[in] e Element type for output
1957 *
1958 * @return ScriptIntrinsicYuvToRGB
1959 */
Tim Murray21fa7a02013-08-15 16:25:03 -07001960 static sp<ScriptIntrinsicYuvToRGB> create(sp<RS> rs, sp<const Element> e);
Tim Murray75e877d2013-09-11 14:45:20 -07001961 /**
1962 * Set the input YUV allocation.
1963 *
1964 * @param[in] ain The input allocation.
1965 */
Tim Murrayb27b1812013-08-05 14:00:40 -07001966 void setInput(sp<Allocation> in);
Tim Murray75e877d2013-09-11 14:45:20 -07001967
1968 /**
1969 * Convert the image to RGB.
1970 *
1971 * @param[in] aout Output allocation. Must match creation element
1972 * type.
1973 */
Tim Murrayb27b1812013-08-05 14:00:40 -07001974 void forEach(sp<Allocation> out);
Tim Murray89daad62013-07-29 14:30:02 -07001975
1976};
Tim Murrayb27b1812013-08-05 14:00:40 -07001977
Tim Murray75e877d2013-09-11 14:45:20 -07001978/**
1979 * Sampler object that defines how Allocations can be read as textures
1980 * within a kernel. Samplers are used in conjunction with the rsSample
1981 * runtime function to return values from normalized coordinates.
1982 *
1983 * Any Allocation used with a Sampler must have been created with
1984 * RS_ALLOCATION_USAGE_GRAPHICS_TEXTURE; using a Sampler on an
1985 * Allocation that was not created with
1986 * RS_ALLOCATION_USAGE_GRAPHICS_TEXTURE is undefined.
1987 **/
Tim Murray729b6fe2013-07-23 16:20:42 -07001988 class Sampler : public BaseObj {
1989 private:
1990 Sampler(sp<RS> rs, void* id);
Miao Wange5428e62015-03-10 15:29:40 -07001991 Sampler(sp<RS> rs, void* id, RsSamplerValue min, RsSamplerValue mag,
1992 RsSamplerValue wrapS, RsSamplerValue wrapT, float anisotropy);
Tim Murray729b6fe2013-07-23 16:20:42 -07001993 RsSamplerValue mMin;
1994 RsSamplerValue mMag;
1995 RsSamplerValue mWrapS;
1996 RsSamplerValue mWrapT;
Tim Murray729b6fe2013-07-23 16:20:42 -07001997 float mAniso;
1998
1999 public:
Tim Murray75e877d2013-09-11 14:45:20 -07002000 /**
2001 * Creates a non-standard Sampler.
2002 * @param[in] rs RenderScript context
2003 * @param[in] min minification
2004 * @param[in] mag magnification
2005 * @param[in] wrapS S wrapping mode
2006 * @param[in] wrapT T wrapping mode
2007 * @param[in] anisotropy anisotropy setting
2008 */
Tim Murray729b6fe2013-07-23 16:20:42 -07002009 static sp<Sampler> create(sp<RS> rs, RsSamplerValue min, RsSamplerValue mag, RsSamplerValue wrapS, RsSamplerValue wrapT, float anisotropy);
2010
Tim Murray75e877d2013-09-11 14:45:20 -07002011 /**
2012 * @return minification setting for the sampler
2013 */
Tim Murray729b6fe2013-07-23 16:20:42 -07002014 RsSamplerValue getMinification();
Tim Murray75e877d2013-09-11 14:45:20 -07002015 /**
2016 * @return magnification setting for the sampler
2017 */
Tim Murray729b6fe2013-07-23 16:20:42 -07002018 RsSamplerValue getMagnification();
Tim Murray75e877d2013-09-11 14:45:20 -07002019 /**
2020 * @return S wrapping mode for the sampler
2021 */
Tim Murray729b6fe2013-07-23 16:20:42 -07002022 RsSamplerValue getWrapS();
Tim Murray75e877d2013-09-11 14:45:20 -07002023 /**
2024 * @return T wrapping mode for the sampler
2025 */
Tim Murray729b6fe2013-07-23 16:20:42 -07002026 RsSamplerValue getWrapT();
Tim Murray75e877d2013-09-11 14:45:20 -07002027 /**
2028 * @return anisotropy setting for the sampler
2029 */
Tim Murray729b6fe2013-07-23 16:20:42 -07002030 float getAnisotropy();
2031
Tim Murray75e877d2013-09-11 14:45:20 -07002032 /**
2033 * Retrieve a sampler with min and mag set to nearest and wrap modes set to
2034 * clamp.
2035 *
2036 * @param rs Context to which the sampler will belong.
2037 *
2038 * @return Sampler
2039 */
Stephen Hines8a588bd2013-11-26 15:38:31 -08002040 static sp<const Sampler> CLAMP_NEAREST(sp<RS> rs);
Tim Murray75e877d2013-09-11 14:45:20 -07002041 /**
2042 * Retrieve a sampler with min and mag set to linear and wrap modes set to
2043 * clamp.
2044 *
2045 * @param rs Context to which the sampler will belong.
2046 *
2047 * @return Sampler
2048 */
Stephen Hines8a588bd2013-11-26 15:38:31 -08002049 static sp<const Sampler> CLAMP_LINEAR(sp<RS> rs);
Tim Murray75e877d2013-09-11 14:45:20 -07002050 /**
2051 * Retrieve a sampler with mag set to linear, min linear mipmap linear, and
2052 * wrap modes set to clamp.
2053 *
2054 * @param rs Context to which the sampler will belong.
2055 *
2056 * @return Sampler
2057 */
Stephen Hines8a588bd2013-11-26 15:38:31 -08002058 static sp<const Sampler> CLAMP_LINEAR_MIP_LINEAR(sp<RS> rs);
Tim Murray75e877d2013-09-11 14:45:20 -07002059 /**
2060 * Retrieve a sampler with min and mag set to nearest and wrap modes set to
2061 * wrap.
2062 *
2063 * @param rs Context to which the sampler will belong.
2064 *
2065 * @return Sampler
2066 */
Stephen Hines8a588bd2013-11-26 15:38:31 -08002067 static sp<const Sampler> WRAP_NEAREST(sp<RS> rs);
Tim Murray75e877d2013-09-11 14:45:20 -07002068 /**
2069 * Retrieve a sampler with min and mag set to linear and wrap modes set to
2070 * wrap.
2071 *
2072 * @param rs Context to which the sampler will belong.
2073 *
2074 * @return Sampler
2075 */
Stephen Hines8a588bd2013-11-26 15:38:31 -08002076 static sp<const Sampler> WRAP_LINEAR(sp<RS> rs);
Tim Murray75e877d2013-09-11 14:45:20 -07002077 /**
2078 * Retrieve a sampler with mag set to linear, min linear mipmap linear, and
2079 * wrap modes set to wrap.
2080 *
2081 * @param rs Context to which the sampler will belong.
2082 *
2083 * @return Sampler
2084 */
Stephen Hines8a588bd2013-11-26 15:38:31 -08002085 static sp<const Sampler> WRAP_LINEAR_MIP_LINEAR(sp<RS> rs);
Tim Murray75e877d2013-09-11 14:45:20 -07002086 /**
2087 * Retrieve a sampler with min and mag set to nearest and wrap modes set to
2088 * mirrored repeat.
2089 *
2090 * @param rs Context to which the sampler will belong.
2091 *
2092 * @return Sampler
2093 */
Stephen Hines8a588bd2013-11-26 15:38:31 -08002094 static sp<const Sampler> MIRRORED_REPEAT_NEAREST(sp<RS> rs);
Tim Murray75e877d2013-09-11 14:45:20 -07002095 /**
2096 * Retrieve a sampler with min and mag set to linear and wrap modes set to
2097 * mirrored repeat.
2098 *
2099 * @param rs Context to which the sampler will belong.
2100 *
2101 * @return Sampler
2102 */
Stephen Hines8a588bd2013-11-26 15:38:31 -08002103 static sp<const Sampler> MIRRORED_REPEAT_LINEAR(sp<RS> rs);
Tim Murray75e877d2013-09-11 14:45:20 -07002104 /**
2105 * Retrieve a sampler with min and mag set to linear and wrap modes set to
2106 * mirrored repeat.
2107 *
2108 * @param rs Context to which the sampler will belong.
2109 *
2110 * @return Sampler
2111 */
Stephen Hines8a588bd2013-11-26 15:38:31 -08002112 static sp<const Sampler> MIRRORED_REPEAT_LINEAR_MIP_LINEAR(sp<RS> rs);
Tim Murray729b6fe2013-07-23 16:20:42 -07002113
2114};
2115
Stephen Hinesd10412f2013-08-29 18:27:21 -07002116class Byte2 {
2117 public:
2118 int8_t x, y;
2119
2120 Byte2(int8_t initX, int8_t initY)
2121 : x(initX), y(initY) {}
2122 Byte2() : x(0), y(0) {}
2123};
2124
2125class Byte3 {
2126 public:
2127 int8_t x, y, z;
2128
2129 Byte3(int8_t initX, int8_t initY, int8_t initZ)
2130 : x(initX), y(initY), z(initZ) {}
2131 Byte3() : x(0), y(0), z(0) {}
2132};
2133
2134class Byte4 {
2135 public:
2136 int8_t x, y, z, w;
2137
2138 Byte4(int8_t initX, int8_t initY, int8_t initZ, int8_t initW)
2139 : x(initX), y(initY), z(initZ), w(initW) {}
2140 Byte4() : x(0), y(0), z(0), w(0) {}
2141};
2142
2143class UByte2 {
2144 public:
2145 uint8_t x, y;
2146
2147 UByte2(uint8_t initX, uint8_t initY)
2148 : x(initX), y(initY) {}
2149 UByte2() : x(0), y(0) {}
2150};
2151
2152class UByte3 {
2153 public:
2154 uint8_t x, y, z;
2155
2156 UByte3(uint8_t initX, uint8_t initY, uint8_t initZ)
2157 : x(initX), y(initY), z(initZ) {}
2158 UByte3() : x(0), y(0), z(0) {}
2159};
2160
2161class UByte4 {
2162 public:
2163 uint8_t x, y, z, w;
2164
2165 UByte4(uint8_t initX, uint8_t initY, uint8_t initZ, uint8_t initW)
2166 : x(initX), y(initY), z(initZ), w(initW) {}
2167 UByte4() : x(0), y(0), z(0), w(0) {}
2168};
2169
2170class Short2 {
2171 public:
2172 short x, y;
2173
2174 Short2(short initX, short initY)
2175 : x(initX), y(initY) {}
2176 Short2() : x(0), y(0) {}
2177};
2178
2179class Short3 {
2180 public:
2181 short x, y, z;
2182
2183 Short3(short initX, short initY, short initZ)
2184 : x(initX), y(initY), z(initZ) {}
2185 Short3() : x(0), y(0), z(0) {}
2186};
2187
2188class Short4 {
2189 public:
2190 short x, y, z, w;
2191
2192 Short4(short initX, short initY, short initZ, short initW)
2193 : x(initX), y(initY), z(initZ), w(initW) {}
2194 Short4() : x(0), y(0), z(0), w(0) {}
2195};
2196
2197class UShort2 {
2198 public:
2199 uint16_t x, y;
2200
2201 UShort2(uint16_t initX, uint16_t initY)
2202 : x(initX), y(initY) {}
2203 UShort2() : x(0), y(0) {}
2204};
2205
2206class UShort3 {
2207 public:
2208 uint16_t x, y, z;
2209
2210 UShort3(uint16_t initX, uint16_t initY, uint16_t initZ)
2211 : x(initX), y(initY), z(initZ) {}
2212 UShort3() : x(0), y(0), z(0) {}
2213};
2214
2215class UShort4 {
2216 public:
2217 uint16_t x, y, z, w;
2218
2219 UShort4(uint16_t initX, uint16_t initY, uint16_t initZ, uint16_t initW)
2220 : x(initX), y(initY), z(initZ), w(initW) {}
2221 UShort4() : x(0), y(0), z(0), w(0) {}
2222};
2223
2224class Int2 {
2225 public:
2226 int x, y;
2227
2228 Int2(int initX, int initY)
2229 : x(initX), y(initY) {}
2230 Int2() : x(0), y(0) {}
2231};
2232
2233class Int3 {
2234 public:
2235 int x, y, z;
2236
2237 Int3(int initX, int initY, int initZ)
2238 : x(initX), y(initY), z(initZ) {}
2239 Int3() : x(0), y(0), z(0) {}
2240};
2241
2242class Int4 {
2243 public:
2244 int x, y, z, w;
2245
2246 Int4(int initX, int initY, int initZ, int initW)
2247 : x(initX), y(initY), z(initZ), w(initW) {}
2248 Int4() : x(0), y(0), z(0), w(0) {}
2249};
2250
2251class UInt2 {
2252 public:
2253 uint32_t x, y;
2254
2255 UInt2(uint32_t initX, uint32_t initY)
2256 : x(initX), y(initY) {}
2257 UInt2() : x(0), y(0) {}
2258};
2259
2260class UInt3 {
2261 public:
2262 uint32_t x, y, z;
2263
2264 UInt3(uint32_t initX, uint32_t initY, uint32_t initZ)
2265 : x(initX), y(initY), z(initZ) {}
2266 UInt3() : x(0), y(0), z(0) {}
2267};
2268
2269class UInt4 {
2270 public:
2271 uint32_t x, y, z, w;
2272
2273 UInt4(uint32_t initX, uint32_t initY, uint32_t initZ, uint32_t initW)
2274 : x(initX), y(initY), z(initZ), w(initW) {}
2275 UInt4() : x(0), y(0), z(0), w(0) {}
2276};
2277
2278class Long2 {
2279 public:
2280 int64_t x, y;
2281
2282 Long2(int64_t initX, int64_t initY)
2283 : x(initX), y(initY) {}
2284 Long2() : x(0), y(0) {}
2285};
2286
2287class Long3 {
2288 public:
2289 int64_t x, y, z;
2290
2291 Long3(int64_t initX, int64_t initY, int64_t initZ)
2292 : x(initX), y(initY), z(initZ) {}
2293 Long3() : x(0), y(0), z(0) {}
2294};
2295
2296class Long4 {
2297 public:
2298 int64_t x, y, z, w;
2299
2300 Long4(int64_t initX, int64_t initY, int64_t initZ, int64_t initW)
2301 : x(initX), y(initY), z(initZ), w(initW) {}
2302 Long4() : x(0), y(0), z(0), w(0) {}
2303};
2304
2305class ULong2 {
2306 public:
2307 uint64_t x, y;
2308
2309 ULong2(uint64_t initX, uint64_t initY)
2310 : x(initX), y(initY) {}
2311 ULong2() : x(0), y(0) {}
2312};
2313
2314class ULong3 {
2315 public:
2316 uint64_t x, y, z;
2317
2318 ULong3(uint64_t initX, uint64_t initY, uint64_t initZ)
2319 : x(initX), y(initY), z(initZ) {}
2320 ULong3() : x(0), y(0), z(0) {}
2321};
2322
2323class ULong4 {
2324 public:
2325 uint64_t x, y, z, w;
2326
2327 ULong4(uint64_t initX, uint64_t initY, uint64_t initZ, uint64_t initW)
2328 : x(initX), y(initY), z(initZ), w(initW) {}
2329 ULong4() : x(0), y(0), z(0), w(0) {}
2330};
2331
2332class Float2 {
2333 public:
2334 float x, y;
2335
2336 Float2(float initX, float initY)
2337 : x(initX), y(initY) {}
2338 Float2() : x(0), y(0) {}
2339};
2340
2341class Float3 {
2342 public:
2343 float x, y, z;
2344
2345 Float3(float initX, float initY, float initZ)
2346 : x(initX), y(initY), z(initZ) {}
2347 Float3() : x(0.f), y(0.f), z(0.f) {}
2348};
2349
2350class Float4 {
2351 public:
2352 float x, y, z, w;
2353
2354 Float4(float initX, float initY, float initZ, float initW)
2355 : x(initX), y(initY), z(initZ), w(initW) {}
2356 Float4() : x(0.f), y(0.f), z(0.f), w(0.f) {}
2357};
2358
2359class Double2 {
2360 public:
2361 double x, y;
2362
2363 Double2(double initX, double initY)
2364 : x(initX), y(initY) {}
2365 Double2() : x(0), y(0) {}
2366};
2367
2368class Double3 {
2369 public:
2370 double x, y, z;
2371
2372 Double3(double initX, double initY, double initZ)
2373 : x(initX), y(initY), z(initZ) {}
2374 Double3() : x(0), y(0), z(0) {}
2375};
2376
2377class Double4 {
2378 public:
2379 double x, y, z, w;
2380
2381 Double4(double initX, double initY, double initZ, double initW)
2382 : x(initX), y(initY), z(initZ), w(initW) {}
2383 Double4() : x(0), y(0), z(0), w(0) {}
2384};
2385
Tim Murray84bf2b82012-10-31 16:03:16 -07002386}
Tim Murray7f0d5682012-11-08 16:35:24 -08002387
Tim Murray84bf2b82012-10-31 16:03:16 -07002388}
2389
2390#endif