blob: dca2bb1d2cd7804fbb8aa15a4b8e87aaf8889cb8 [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"
Jason Samsf9e077a2013-03-20 19:04:19 -070021#include "rsCppUtils.h"
Tim Murray89daad62013-07-29 14:30:02 -070022#include "util/RefBase.h"
Tim Murraya4230962013-07-17 16:50:10 -070023#include "rsDispatch.h"
24
Tim Murray89daad62013-07-29 14:30:02 -070025#include <vector>
Tim Murrayab716362013-08-12 12:37:18 -070026#include <string>
Tim Murray89daad62013-07-29 14:30:02 -070027
Tim Murray96267c22013-02-12 11:25:12 -080028// Every row in an RS allocation is guaranteed to be aligned by this amount
29// Every row in a user-backed allocation must be aligned by this amount
30#define RS_CPU_ALLOCATION_ALIGNMENT 16
31
Tim Murray84bf2b82012-10-31 16:03:16 -070032namespace android {
Tim Murray9eb7f4b2012-11-16 14:02:18 -080033namespace RSC {
Tim Murray84bf2b82012-10-31 16:03:16 -070034
35typedef void (*ErrorHandlerFunc_t)(uint32_t errorNum, const char *errorText);
36typedef void (*MessageHandlerFunc_t)(uint32_t msgNum, const void *msgData, size_t msgLen);
37
38class RS;
39class BaseObj;
40class Element;
41class Type;
42class Allocation;
43class Script;
44class ScriptC;
Tim Murray729b6fe2013-07-23 16:20:42 -070045class Sampler;
Tim Murray84bf2b82012-10-31 16:03:16 -070046
Tim Murray21fa7a02013-08-15 16:25:03 -070047 enum RSError {
48 RS_SUCCESS = 0,
49 RS_ERROR_INVALID_PARAMETER = 1,
50 RS_ERROR_RUNTIME_ERROR = 2,
Tim Murray10913a52013-08-20 17:19:47 -070051 RS_ERROR_INVALID_ELEMENT = 3,
Tim Murray21fa7a02013-08-15 16:25:03 -070052 RS_ERROR_MAX = 9999
53
54 };
55
Tim Murray89daad62013-07-29 14:30:02 -070056 class RS : public android::RSC::LightRefBase<RS> {
Tim Murray84bf2b82012-10-31 16:03:16 -070057
58 public:
59 RS();
60 virtual ~RS();
61
Tim Murray4d252d62012-11-29 14:37:59 -080062 bool init(bool forceCpu = false, bool synchronous = false);
Tim Murray84bf2b82012-10-31 16:03:16 -070063
64 void setErrorHandler(ErrorHandlerFunc_t func);
65 ErrorHandlerFunc_t getErrorHandler() { return mErrorFunc; }
66
67 void setMessageHandler(MessageHandlerFunc_t func);
68 MessageHandlerFunc_t getMessageHandler() { return mMessageFunc; }
69
Tim Murray21fa7a02013-08-15 16:25:03 -070070 void throwError(RSError error, const char *errMsg);
Tim Murray10913a52013-08-20 17:19:47 -070071 RSError getError();
Tim Murray84bf2b82012-10-31 16:03:16 -070072
73 RsContext getContext() { return mContext; }
74
Tim Murraybaca6c32012-11-14 16:51:46 -080075 void finish();
76
Tim Murraya4230962013-07-17 16:50:10 -070077 static dispatchTable* dispatch;
78
Tim Murray84bf2b82012-10-31 16:03:16 -070079 private:
Tim Murray4a92d122013-07-22 10:56:18 -070080 static bool usingNative;
Tim Murraya4230962013-07-17 16:50:10 -070081 static bool initDispatch(int targetApi);
82
Tim Murray4d252d62012-11-29 14:37:59 -080083 bool init(int targetApi, bool forceCpu, bool synchronous);
Tim Murray84bf2b82012-10-31 16:03:16 -070084 static void * threadProc(void *);
85
86 static bool gInitialized;
87 static pthread_mutex_t gInitMutex;
88
89 pthread_t mMessageThreadId;
90 pid_t mNativeMessageThreadId;
91 bool mMessageRun;
92
93 RsDevice mDev;
94 RsContext mContext;
Tim Murray21fa7a02013-08-15 16:25:03 -070095 RSError mCurrentError;
Tim Murray84bf2b82012-10-31 16:03:16 -070096
97 ErrorHandlerFunc_t mErrorFunc;
98 MessageHandlerFunc_t mMessageFunc;
Tim Murraya4230962013-07-17 16:50:10 -070099 bool mInit;
Tim Murray84bf2b82012-10-31 16:03:16 -0700100
101 struct {
Tim Murray89daad62013-07-29 14:30:02 -0700102 sp<const Element> U8;
103 sp<const Element> I8;
104 sp<const Element> U16;
105 sp<const Element> I16;
106 sp<const Element> U32;
107 sp<const Element> I32;
108 sp<const Element> U64;
109 sp<const Element> I64;
110 sp<const Element> F32;
111 sp<const Element> F64;
112 sp<const Element> BOOLEAN;
Tim Murray84bf2b82012-10-31 16:03:16 -0700113
Tim Murray89daad62013-07-29 14:30:02 -0700114 sp<const Element> ELEMENT;
115 sp<const Element> TYPE;
116 sp<const Element> ALLOCATION;
117 sp<const Element> SAMPLER;
118 sp<const Element> SCRIPT;
119 sp<const Element> MESH;
120 sp<const Element> PROGRAM_FRAGMENT;
121 sp<const Element> PROGRAM_VERTEX;
122 sp<const Element> PROGRAM_RASTER;
123 sp<const Element> PROGRAM_STORE;
Tim Murray84bf2b82012-10-31 16:03:16 -0700124
Tim Murray89daad62013-07-29 14:30:02 -0700125 sp<const Element> A_8;
126 sp<const Element> RGB_565;
127 sp<const Element> RGB_888;
128 sp<const Element> RGBA_5551;
129 sp<const Element> RGBA_4444;
130 sp<const Element> RGBA_8888;
Tim Murray84bf2b82012-10-31 16:03:16 -0700131
Tim Murray89daad62013-07-29 14:30:02 -0700132 sp<const Element> FLOAT_2;
133 sp<const Element> FLOAT_3;
134 sp<const Element> FLOAT_4;
Tim Murray84bf2b82012-10-31 16:03:16 -0700135
Tim Murray89daad62013-07-29 14:30:02 -0700136 sp<const Element> DOUBLE_2;
137 sp<const Element> DOUBLE_3;
138 sp<const Element> DOUBLE_4;
Tim Murray84bf2b82012-10-31 16:03:16 -0700139
Tim Murray89daad62013-07-29 14:30:02 -0700140 sp<const Element> UCHAR_2;
141 sp<const Element> UCHAR_3;
142 sp<const Element> UCHAR_4;
Tim Murray84bf2b82012-10-31 16:03:16 -0700143
Tim Murray89daad62013-07-29 14:30:02 -0700144 sp<const Element> CHAR_2;
145 sp<const Element> CHAR_3;
146 sp<const Element> CHAR_4;
Tim Murray84bf2b82012-10-31 16:03:16 -0700147
Tim Murray89daad62013-07-29 14:30:02 -0700148 sp<const Element> USHORT_2;
149 sp<const Element> USHORT_3;
150 sp<const Element> USHORT_4;
Tim Murray84bf2b82012-10-31 16:03:16 -0700151
Tim Murray89daad62013-07-29 14:30:02 -0700152 sp<const Element> SHORT_2;
153 sp<const Element> SHORT_3;
154 sp<const Element> SHORT_4;
Tim Murray84bf2b82012-10-31 16:03:16 -0700155
Tim Murray89daad62013-07-29 14:30:02 -0700156 sp<const Element> UINT_2;
157 sp<const Element> UINT_3;
158 sp<const Element> UINT_4;
Tim Murray84bf2b82012-10-31 16:03:16 -0700159
Tim Murray89daad62013-07-29 14:30:02 -0700160 sp<const Element> INT_2;
161 sp<const Element> INT_3;
162 sp<const Element> INT_4;
Tim Murray84bf2b82012-10-31 16:03:16 -0700163
Tim Murray89daad62013-07-29 14:30:02 -0700164 sp<const Element> ULONG_2;
165 sp<const Element> ULONG_3;
166 sp<const Element> ULONG_4;
Tim Murray84bf2b82012-10-31 16:03:16 -0700167
Tim Murray89daad62013-07-29 14:30:02 -0700168 sp<const Element> LONG_2;
169 sp<const Element> LONG_3;
170 sp<const Element> LONG_4;
Tim Murray84bf2b82012-10-31 16:03:16 -0700171
Tim Murray89daad62013-07-29 14:30:02 -0700172 sp<const Element> MATRIX_4X4;
173 sp<const Element> MATRIX_3X3;
174 sp<const Element> MATRIX_2X2;
Tim Murray84bf2b82012-10-31 16:03:16 -0700175 } mElements;
176
Tim Murray729b6fe2013-07-23 16:20:42 -0700177 struct {
Tim Murray89daad62013-07-29 14:30:02 -0700178 sp<const Sampler> CLAMP_NEAREST;
179 sp<const Sampler> CLAMP_LINEAR;
180 sp<const Sampler> CLAMP_LINEAR_MIP_LINEAR;
181 sp<const Sampler> WRAP_NEAREST;
182 sp<const Sampler> WRAP_LINEAR;
183 sp<const Sampler> WRAP_LINEAR_MIP_LINEAR;
184 sp<const Sampler> MIRRORED_REPEAT_NEAREST;
185 sp<const Sampler> MIRRORED_REPEAT_LINEAR;
186 sp<const Sampler> MIRRORED_REPEAT_LINEAR_MIP_LINEAR;
Tim Murray729b6fe2013-07-23 16:20:42 -0700187 } mSamplers;
188 friend class Sampler;
189 friend class Element;
Tim Murray84bf2b82012-10-31 16:03:16 -0700190};
191
Tim Murray89daad62013-07-29 14:30:02 -0700192class BaseObj : public android::RSC::LightRefBase<BaseObj> {
193public:
194 void * getID() const;
195 virtual ~BaseObj();
196 virtual void updateFromNative();
197 virtual bool equals(sp<const BaseObj> obj);
198
Tim Murray84bf2b82012-10-31 16:03:16 -0700199protected:
200 void *mID;
201 sp<RS> mRS;
Tim Murrayab716362013-08-12 12:37:18 -0700202 std::string mName;
Tim Murray84bf2b82012-10-31 16:03:16 -0700203
204 BaseObj(void *id, sp<RS> rs);
205 void checkValid();
206
207 static void * getObjID(sp<const BaseObj> o);
208
Tim Murray84bf2b82012-10-31 16:03:16 -0700209};
210
211
212class Allocation : public BaseObj {
213protected:
Tim Murray89daad62013-07-29 14:30:02 -0700214 sp<const Type> mType;
Tim Murray84bf2b82012-10-31 16:03:16 -0700215 uint32_t mUsage;
Tim Murray89daad62013-07-29 14:30:02 -0700216 sp<Allocation> mAdaptedAllocation;
Tim Murray84bf2b82012-10-31 16:03:16 -0700217
218 bool mConstrainedLOD;
219 bool mConstrainedFace;
220 bool mConstrainedY;
221 bool mConstrainedZ;
222 bool mReadAllowed;
223 bool mWriteAllowed;
224 uint32_t mSelectedY;
225 uint32_t mSelectedZ;
226 uint32_t mSelectedLOD;
227 RsAllocationCubemapFace mSelectedFace;
228
229 uint32_t mCurrentDimX;
230 uint32_t mCurrentDimY;
231 uint32_t mCurrentDimZ;
232 uint32_t mCurrentCount;
233
234 void * getIDSafe() const;
235 void updateCacheInfo(sp<const Type> t);
236
237 Allocation(void *id, sp<RS> rs, sp<const Type> t, uint32_t usage);
238
239 void validateIsInt32();
240 void validateIsInt16();
241 void validateIsInt8();
242 void validateIsFloat32();
243 void validateIsObject();
244
245 virtual void updateFromNative();
246
247 void validate2DRange(uint32_t xoff, uint32_t yoff, uint32_t w, uint32_t h);
248
249public:
Stephen Hinesa180b7d2013-08-21 12:42:13 -0700250 sp<const Type> getType() const {
Tim Murray84bf2b82012-10-31 16:03:16 -0700251 return mType;
252 }
253
254 void syncAll(RsAllocationUsageType srcLocation);
255 void ioSendOutput();
256 void ioGetInput();
257
258 void generateMipmaps();
Tim Murray509ea5c2012-11-13 11:56:40 -0800259
Tim Murray0b93e302012-11-15 14:56:54 -0800260 void copy1DRangeFrom(uint32_t off, size_t count, const void *data);
Tim Murraya4cbc2b2012-11-14 17:18:08 -0800261 void copy1DRangeFrom(uint32_t off, size_t count, sp<const Allocation> data, uint32_t dataOff);
Tim Murray84bf2b82012-10-31 16:03:16 -0700262
Tim Murray0b93e302012-11-15 14:56:54 -0800263 void copy1DRangeTo(uint32_t off, size_t count, void *data);
264
265 void copy1DFrom(const void* data);
266 void copy1DTo(void* data);
Tim Murraya4cbc2b2012-11-14 17:18:08 -0800267
Tim Murray84bf2b82012-10-31 16:03:16 -0700268 void copy2DRangeFrom(uint32_t xoff, uint32_t yoff, uint32_t w, uint32_t h,
Tim Murray0b93e302012-11-15 14:56:54 -0800269 const void *data);
Tim Murray7b3e3092012-11-16 13:32:24 -0800270
271 void copy2DRangeTo(uint32_t xoff, uint32_t yoff, uint32_t w, uint32_t h,
272 void *data);
273
Tim Murray0b93e302012-11-15 14:56:54 -0800274 void copy2DRangeFrom(uint32_t xoff, uint32_t yoff, uint32_t w, uint32_t h,
275 sp<const Allocation> data, uint32_t dataXoff, uint32_t dataYoff);
Tim Murray84bf2b82012-10-31 16:03:16 -0700276
Tim Murray358747a2012-11-26 13:52:04 -0800277 void copy2DStridedFrom(uint32_t xoff, uint32_t yoff, uint32_t w, uint32_t h,
278 const void *data, size_t stride);
279 void copy2DStridedFrom(const void *data, size_t stride);
280
281 void copy2DStridedTo(uint32_t xoff, uint32_t yoff, uint32_t w, uint32_t h,
282 void *data, size_t stride);
283 void copy2DStridedTo(void *data, size_t stride);
284
Tim Murray84bf2b82012-10-31 16:03:16 -0700285 void resize(int dimX);
286 void resize(int dimX, int dimY);
287
288 static sp<Allocation> createTyped(sp<RS> rs, sp<const Type> type,
289 RsAllocationMipmapControl mips, uint32_t usage);
290 static sp<Allocation> createTyped(sp<RS> rs, sp<const Type> type,
291 RsAllocationMipmapControl mips, uint32_t usage, void * pointer);
292
293 static sp<Allocation> createTyped(sp<RS> rs, sp<const Type> type,
294 uint32_t usage = RS_ALLOCATION_USAGE_SCRIPT);
295 static sp<Allocation> createSized(sp<RS> rs, sp<const Element> e, size_t count,
296 uint32_t usage = RS_ALLOCATION_USAGE_SCRIPT);
Tim Murray684726c2012-11-14 11:57:42 -0800297 static sp<Allocation> createSized2D(sp<RS> rs, sp<const Element> e,
298 size_t x, size_t y,
299 uint32_t usage = RS_ALLOCATION_USAGE_SCRIPT);
300
Tim Murray84bf2b82012-10-31 16:03:16 -0700301
302};
303
304class Element : public BaseObj {
305public:
306 bool isComplex();
307 size_t getSubElementCount() {
308 return mVisibleElementMap.size();
309 }
310
311 sp<const Element> getSubElement(uint32_t index);
312 const char * getSubElementName(uint32_t index);
313 size_t getSubElementArraySize(uint32_t index);
314 uint32_t getSubElementOffsetBytes(uint32_t index);
315 RsDataType getDataType() const {
316 return mType;
317 }
318
319 RsDataKind getDataKind() const {
320 return mKind;
321 }
322
323 size_t getSizeBytes() const {
324 return mSizeBytes;
325 }
326
Tim Murray10913a52013-08-20 17:19:47 -0700327 uint32_t getVectorSize() const {
328 return mVectorSize;
329 }
330
Tim Murray84bf2b82012-10-31 16:03:16 -0700331 static sp<const Element> BOOLEAN(sp<RS> rs);
332 static sp<const Element> U8(sp<RS> rs);
333 static sp<const Element> I8(sp<RS> rs);
334 static sp<const Element> U16(sp<RS> rs);
335 static sp<const Element> I16(sp<RS> rs);
336 static sp<const Element> U32(sp<RS> rs);
337 static sp<const Element> I32(sp<RS> rs);
338 static sp<const Element> U64(sp<RS> rs);
339 static sp<const Element> I64(sp<RS> rs);
340 static sp<const Element> F32(sp<RS> rs);
341 static sp<const Element> F64(sp<RS> rs);
342 static sp<const Element> ELEMENT(sp<RS> rs);
343 static sp<const Element> TYPE(sp<RS> rs);
344 static sp<const Element> ALLOCATION(sp<RS> rs);
345 static sp<const Element> SAMPLER(sp<RS> rs);
346 static sp<const Element> SCRIPT(sp<RS> rs);
347 static sp<const Element> MESH(sp<RS> rs);
348 static sp<const Element> PROGRAM_FRAGMENT(sp<RS> rs);
349 static sp<const Element> PROGRAM_VERTEX(sp<RS> rs);
350 static sp<const Element> PROGRAM_RASTER(sp<RS> rs);
351 static sp<const Element> PROGRAM_STORE(sp<RS> rs);
352
353 static sp<const Element> A_8(sp<RS> rs);
354 static sp<const Element> RGB_565(sp<RS> rs);
355 static sp<const Element> RGB_888(sp<RS> rs);
356 static sp<const Element> RGBA_5551(sp<RS> rs);
357 static sp<const Element> RGBA_4444(sp<RS> rs);
358 static sp<const Element> RGBA_8888(sp<RS> rs);
359
360 static sp<const Element> F32_2(sp<RS> rs);
361 static sp<const Element> F32_3(sp<RS> rs);
362 static sp<const Element> F32_4(sp<RS> rs);
363 static sp<const Element> F64_2(sp<RS> rs);
364 static sp<const Element> F64_3(sp<RS> rs);
365 static sp<const Element> F64_4(sp<RS> rs);
366 static sp<const Element> U8_2(sp<RS> rs);
367 static sp<const Element> U8_3(sp<RS> rs);
368 static sp<const Element> U8_4(sp<RS> rs);
369 static sp<const Element> I8_2(sp<RS> rs);
370 static sp<const Element> I8_3(sp<RS> rs);
371 static sp<const Element> I8_4(sp<RS> rs);
372 static sp<const Element> U16_2(sp<RS> rs);
373 static sp<const Element> U16_3(sp<RS> rs);
374 static sp<const Element> U16_4(sp<RS> rs);
375 static sp<const Element> I16_2(sp<RS> rs);
376 static sp<const Element> I16_3(sp<RS> rs);
377 static sp<const Element> I16_4(sp<RS> rs);
378 static sp<const Element> U32_2(sp<RS> rs);
379 static sp<const Element> U32_3(sp<RS> rs);
380 static sp<const Element> U32_4(sp<RS> rs);
381 static sp<const Element> I32_2(sp<RS> rs);
382 static sp<const Element> I32_3(sp<RS> rs);
383 static sp<const Element> I32_4(sp<RS> rs);
384 static sp<const Element> U64_2(sp<RS> rs);
385 static sp<const Element> U64_3(sp<RS> rs);
386 static sp<const Element> U64_4(sp<RS> rs);
387 static sp<const Element> I64_2(sp<RS> rs);
388 static sp<const Element> I64_3(sp<RS> rs);
389 static sp<const Element> I64_4(sp<RS> rs);
390 static sp<const Element> MATRIX_4X4(sp<RS> rs);
391 static sp<const Element> MATRIX_3X3(sp<RS> rs);
392 static sp<const Element> MATRIX_2X2(sp<RS> rs);
393
Tim Murray84bf2b82012-10-31 16:03:16 -0700394 void updateFromNative();
395 static sp<const Element> createUser(sp<RS> rs, RsDataType dt);
396 static sp<const Element> createVector(sp<RS> rs, RsDataType dt, uint32_t size);
397 static sp<const Element> createPixel(sp<RS> rs, RsDataType dt, RsDataKind dk);
Tim Murray10913a52013-08-20 17:19:47 -0700398 bool isCompatible(sp<const Element>e) const;
Tim Murray84bf2b82012-10-31 16:03:16 -0700399
Tim Murray89daad62013-07-29 14:30:02 -0700400 Element(void *id, sp<RS> rs,
401 std::vector<sp<Element> > &elements,
Tim Murrayab716362013-08-12 12:37:18 -0700402 std::vector<std::string> &elementNames,
Tim Murray89daad62013-07-29 14:30:02 -0700403 std::vector<uint32_t> &arraySizes);
404 Element(void *id, sp<RS> rs, RsDataType dt, RsDataKind dk, bool norm, uint32_t size);
405 Element(sp<RS> rs);
406 virtual ~Element();
407
408 protected:
Tim Murray84bf2b82012-10-31 16:03:16 -0700409 class Builder {
410 private:
411 sp<RS> mRS;
Tim Murray89daad62013-07-29 14:30:02 -0700412 std::vector<sp<Element> > mElements;
Tim Murrayab716362013-08-12 12:37:18 -0700413 std::vector<std::string> mElementNames;
Tim Murray89daad62013-07-29 14:30:02 -0700414 std::vector<uint32_t> mArraySizes;
Tim Murray84bf2b82012-10-31 16:03:16 -0700415 bool mSkipPadding;
416
417 public:
418 Builder(sp<RS> rs);
419 ~Builder();
Tim Murrayab716362013-08-12 12:37:18 -0700420 void add(sp<Element> e, std::string &name, uint32_t arraySize = 1);
Tim Murray84bf2b82012-10-31 16:03:16 -0700421 sp<const Element> create();
422 };
423
424private:
425 void updateVisibleSubElements();
426
Tim Murray89daad62013-07-29 14:30:02 -0700427 std::vector<sp<Element> > mElements;
Tim Murrayab716362013-08-12 12:37:18 -0700428 std::vector<std::string> mElementNames;
Tim Murray89daad62013-07-29 14:30:02 -0700429 std::vector<uint32_t> mArraySizes;
430 std::vector<uint32_t> mVisibleElementMap;
431 std::vector<uint32_t> mOffsetInBytes;
Tim Murray84bf2b82012-10-31 16:03:16 -0700432
433 RsDataType mType;
434 RsDataKind mKind;
435 bool mNormalized;
436 size_t mSizeBytes;
437 size_t mVectorSize;
438};
439
Stephen Hines2c7206e2012-11-14 19:47:01 -0800440class FieldPacker {
441protected:
442 unsigned char* mData;
443 size_t mPos;
444 size_t mLen;
445
446public:
447 FieldPacker(size_t len)
Tim Murray89daad62013-07-29 14:30:02 -0700448 : mPos(0), mLen(len) {
449 mData = new unsigned char[len];
450 }
Stephen Hines2c7206e2012-11-14 19:47:01 -0800451
452 virtual ~FieldPacker() {
453 delete [] mData;
454 }
455
456 void align(size_t v) {
457 if ((v & (v - 1)) != 0) {
Tim Murrayab716362013-08-12 12:37:18 -0700458 // ALOGE("Non-power-of-two alignment: %zu", v);
Stephen Hines2c7206e2012-11-14 19:47:01 -0800459 return;
460 }
461
462 while ((mPos & (v - 1)) != 0) {
463 mData[mPos++] = 0;
464 }
465 }
466
467 void reset() {
468 mPos = 0;
469 }
470
471 void reset(size_t i) {
472 if (i >= mLen) {
Tim Murrayab716362013-08-12 12:37:18 -0700473 // ALOGE("Out of bounds: i (%zu) >= len (%zu)", i, mLen);
Stephen Hines2c7206e2012-11-14 19:47:01 -0800474 return;
475 }
476 mPos = i;
477 }
478
479 void skip(size_t i) {
480 size_t res = mPos + i;
481 if (res > mLen) {
Tim Murrayab716362013-08-12 12:37:18 -0700482 // ALOGE("Exceeded buffer length: i (%zu) > len (%zu)", i, mLen);
Stephen Hines2c7206e2012-11-14 19:47:01 -0800483 return;
484 }
485 mPos = res;
486 }
487
488 void* getData() const {
489 return mData;
490 }
491
492 size_t getLength() const {
493 return mLen;
494 }
495
496 template <typename T>
Tim Murray89daad62013-07-29 14:30:02 -0700497 void add(T t) {
Stephen Hines2c7206e2012-11-14 19:47:01 -0800498 align(sizeof(t));
499 if (mPos + sizeof(t) <= mLen) {
500 memcpy(&mData[mPos], &t, sizeof(t));
501 mPos += sizeof(t);
502 }
503 }
Stephen Hines43514cd2012-11-16 14:33:47 -0800504
505 /*
Tim Murray89daad62013-07-29 14:30:02 -0700506 void add(rs_matrix4x4 m) {
507 for (size_t i = 0; i < 16; i++) {
508 add(m.m[i]);
509 }
510 }
Stephen Hines43514cd2012-11-16 14:33:47 -0800511
Tim Murray89daad62013-07-29 14:30:02 -0700512 void add(rs_matrix3x3 m) {
513 for (size_t i = 0; i < 9; i++) {
514 add(m.m[i]);
515 }
516 }
Stephen Hines43514cd2012-11-16 14:33:47 -0800517
Tim Murray89daad62013-07-29 14:30:02 -0700518 void add(rs_matrix2x2 m) {
519 for (size_t i = 0; i < 4; i++) {
520 add(m.m[i]);
521 }
522 }
Stephen Hines43514cd2012-11-16 14:33:47 -0800523 */
524
Tim Murray89daad62013-07-29 14:30:02 -0700525 void add(sp<BaseObj> obj) {
Stephen Hines43514cd2012-11-16 14:33:47 -0800526 if (obj != NULL) {
527 add((uint32_t) (uintptr_t) obj->getID());
528 } else {
529 add((uint32_t) 0);
530 }
531 }
Stephen Hines2c7206e2012-11-14 19:47:01 -0800532};
533
Tim Murray89daad62013-07-29 14:30:02 -0700534
Tim Murray84bf2b82012-10-31 16:03:16 -0700535class Type : public BaseObj {
536protected:
537 friend class Allocation;
538
539 uint32_t mDimX;
540 uint32_t mDimY;
541 uint32_t mDimZ;
542 bool mDimMipmaps;
543 bool mDimFaces;
544 size_t mElementCount;
545 sp<const Element> mElement;
546
547 void calcElementCount();
548 virtual void updateFromNative();
549
550public:
551
552 sp<const Element> getElement() const {
553 return mElement;
554 }
555
556 uint32_t getX() const {
557 return mDimX;
558 }
559
560 uint32_t getY() const {
561 return mDimY;
562 }
563
564 uint32_t getZ() const {
565 return mDimZ;
566 }
567
568 bool hasMipmaps() const {
569 return mDimMipmaps;
570 }
571
572 bool hasFaces() const {
573 return mDimFaces;
574 }
575
576 size_t getCount() const {
577 return mElementCount;
578 }
579
580 size_t getSizeBytes() const {
581 return mElementCount * mElement->getSizeBytes();
582 }
583
584 Type(void *id, sp<RS> rs);
585
Tim Murray96267c22013-02-12 11:25:12 -0800586 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 -0700587
588 class Builder {
589 protected:
590 sp<RS> mRS;
591 uint32_t mDimX;
592 uint32_t mDimY;
593 uint32_t mDimZ;
594 bool mDimMipmaps;
595 bool mDimFaces;
596 sp<const Element> mElement;
597
598 public:
599 Builder(sp<RS> rs, sp<const Element> e);
600
601 void setX(uint32_t value);
602 void setY(int value);
603 void setMipmaps(bool value);
604 void setFaces(bool value);
605 sp<const Type> create();
606 };
607
608};
609
610class Script : public BaseObj {
611private:
612
613protected:
614 Script(void *id, sp<RS> rs);
615 void forEach(uint32_t slot, sp<const Allocation> in, sp<const Allocation> out,
616 const void *v, size_t) const;
617 void bindAllocation(sp<Allocation> va, uint32_t slot) const;
618 void setVar(uint32_t index, const void *, size_t len) const;
619 void setVar(uint32_t index, sp<const BaseObj> o) const;
620 void invoke(uint32_t slot, const void *v, size_t len) const;
621
622
623 void invoke(uint32_t slot) const {
624 invoke(slot, NULL, 0);
625 }
626 void setVar(uint32_t index, float v) const {
627 setVar(index, &v, sizeof(v));
628 }
629 void setVar(uint32_t index, double v) const {
630 setVar(index, &v, sizeof(v));
631 }
632 void setVar(uint32_t index, int32_t v) const {
633 setVar(index, &v, sizeof(v));
634 }
635 void setVar(uint32_t index, int64_t v) const {
636 setVar(index, &v, sizeof(v));
637 }
638 void setVar(uint32_t index, bool v) const {
639 setVar(index, &v, sizeof(v));
640 }
641
642public:
643 class FieldBase {
644 protected:
645 sp<const Element> mElement;
646 sp<Allocation> mAllocation;
647
648 void init(sp<RS> rs, uint32_t dimx, uint32_t usages = 0);
649
650 public:
651 sp<const Element> getElement() {
652 return mElement;
653 }
654
655 sp<const Type> getType() {
656 return mAllocation->getType();
657 }
658
659 sp<const Allocation> getAllocation() {
660 return mAllocation;
661 }
662
663 //void updateAllocation();
664 };
665};
666
667class ScriptC : public Script {
668protected:
669 ScriptC(sp<RS> rs,
670 const void *codeTxt, size_t codeLength,
671 const char *cachedName, size_t cachedNameLength,
672 const char *cacheDir, size_t cacheDirLength);
673
674};
675
Tim Murray7f0d5682012-11-08 16:35:24 -0800676class ScriptIntrinsic : public Script {
677 protected:
Tim Murray10913a52013-08-20 17:19:47 -0700678 sp<const Element> mElement;
Tim Murray3cd44af2012-11-14 11:25:27 -0800679 ScriptIntrinsic(sp<RS> rs, int id, sp<const Element> e);
Tim Murrayb27b1812013-08-05 14:00:40 -0700680 virtual ~ScriptIntrinsic();
Tim Murray7f0d5682012-11-08 16:35:24 -0800681};
682
Tim Murray89daad62013-07-29 14:30:02 -0700683class ScriptIntrinsic3DLUT : public ScriptIntrinsic {
Tim Murray21fa7a02013-08-15 16:25:03 -0700684 private:
Tim Murray89daad62013-07-29 14:30:02 -0700685 ScriptIntrinsic3DLUT(sp<RS> rs, sp<const Element> e);
Tim Murray21fa7a02013-08-15 16:25:03 -0700686 public:
687 static sp<ScriptIntrinsic3DLUT> create(sp<RS> rs, sp<const Element> e);
Tim Murray89daad62013-07-29 14:30:02 -0700688 void forEach(sp<Allocation> ain, sp<Allocation> aout);
689 void setLUT(sp<Allocation> lut);
690};
691
Tim Murray7f0d5682012-11-08 16:35:24 -0800692class ScriptIntrinsicBlend : public ScriptIntrinsic {
Tim Murray21fa7a02013-08-15 16:25:03 -0700693 private:
Tim Murray89daad62013-07-29 14:30:02 -0700694 ScriptIntrinsicBlend(sp<RS> rs, sp<const Element> e);
Tim Murray21fa7a02013-08-15 16:25:03 -0700695 public:
696 static sp<ScriptIntrinsicBlend> create(sp<RS> rs, sp<const Element> e);
Tim Murray7f0d5682012-11-08 16:35:24 -0800697 void blendClear(sp<Allocation> in, sp<Allocation> out);
698 void blendSrc(sp<Allocation> in, sp<Allocation> out);
699 void blendDst(sp<Allocation> in, sp<Allocation> out);
700 void blendSrcOver(sp<Allocation> in, sp<Allocation> out);
701 void blendDstOver(sp<Allocation> in, sp<Allocation> out);
702 void blendSrcIn(sp<Allocation> in, sp<Allocation> out);
703 void blendDstIn(sp<Allocation> in, sp<Allocation> out);
704 void blendSrcOut(sp<Allocation> in, sp<Allocation> out);
705 void blendDstOut(sp<Allocation> in, sp<Allocation> out);
706 void blendSrcAtop(sp<Allocation> in, sp<Allocation> out);
707 void blendDstAtop(sp<Allocation> in, sp<Allocation> out);
708 void blendXor(sp<Allocation> in, sp<Allocation> out);
709 void blendMultiply(sp<Allocation> in, sp<Allocation> out);
710 void blendAdd(sp<Allocation> in, sp<Allocation> out);
711 void blendSubtract(sp<Allocation> in, sp<Allocation> out);
712};
Tim Murray84bf2b82012-10-31 16:03:16 -0700713
Tim Murray8f1e60c2012-11-13 12:25:11 -0800714class ScriptIntrinsicBlur : public ScriptIntrinsic {
Tim Murray21fa7a02013-08-15 16:25:03 -0700715 private:
Tim Murray89daad62013-07-29 14:30:02 -0700716 ScriptIntrinsicBlur(sp<RS> rs, sp<const Element> e);
Tim Murray21fa7a02013-08-15 16:25:03 -0700717 public:
718 static sp<ScriptIntrinsicBlur> create(sp<RS> rs, sp<const Element> e);
719 void setInput(sp<Allocation> in);
720 void forEach(sp<Allocation> out);
Tim Murray8f1e60c2012-11-13 12:25:11 -0800721 void setRadius(float radius);
722};
723
Tim Murray89daad62013-07-29 14:30:02 -0700724class ScriptIntrinsicColorMatrix : public ScriptIntrinsic {
Tim Murray21fa7a02013-08-15 16:25:03 -0700725 private:
Tim Murray89daad62013-07-29 14:30:02 -0700726 ScriptIntrinsicColorMatrix(sp<RS> rs, sp<const Element> e);
Tim Murray21fa7a02013-08-15 16:25:03 -0700727 public:
728 static sp<ScriptIntrinsicColorMatrix> create(sp<RS> rs, sp<const Element> e);
Tim Murray89daad62013-07-29 14:30:02 -0700729 void forEach(sp<Allocation> in, sp<Allocation> out);
Tim Murray10913a52013-08-20 17:19:47 -0700730 void setAdd(float* add);
Tim Murray89daad62013-07-29 14:30:02 -0700731 void setColorMatrix3(float* m);
732 void setColorMatrix4(float* m);
733 void setGreyscale();
734 void setRGBtoYUV();
735 void setYUVtoRGB();
736};
737
738class ScriptIntrinsicConvolve3x3 : public ScriptIntrinsic {
Tim Murray21fa7a02013-08-15 16:25:03 -0700739 private:
Tim Murray89daad62013-07-29 14:30:02 -0700740 ScriptIntrinsicConvolve3x3(sp<RS> rs, sp<const Element> e);
Tim Murray21fa7a02013-08-15 16:25:03 -0700741 public:
742 static sp<ScriptIntrinsicConvolve3x3> create(sp<RS> rs, sp<const Element> e);
Tim Murray89daad62013-07-29 14:30:02 -0700743 void setInput(sp<Allocation> in);
744 void forEach(sp<Allocation> out);
745 void setCoefficients(float* v);
746};
747
748class ScriptIntrinsicConvolve5x5 : public ScriptIntrinsic {
Tim Murray21fa7a02013-08-15 16:25:03 -0700749 private:
Tim Murray89daad62013-07-29 14:30:02 -0700750 ScriptIntrinsicConvolve5x5(sp<RS> rs, sp<const Element> e);
Tim Murray21fa7a02013-08-15 16:25:03 -0700751 public:
752 static sp<ScriptIntrinsicConvolve5x5> create(sp<RS> rs, sp<const Element> e);
Tim Murray89daad62013-07-29 14:30:02 -0700753 void setInput(sp<Allocation> in);
754 void forEach(sp<Allocation> out);
755 void setCoefficients(float* v);
756};
757
Tim Murrayb27b1812013-08-05 14:00:40 -0700758class ScriptIntrinsicHistogram : public ScriptIntrinsic {
Tim Murray21fa7a02013-08-15 16:25:03 -0700759 private:
Tim Murrayb27b1812013-08-05 14:00:40 -0700760 ScriptIntrinsicHistogram(sp<RS> rs, sp<const Element> e);
Tim Murray10913a52013-08-20 17:19:47 -0700761 sp<Allocation> mOut;
Tim Murray21fa7a02013-08-15 16:25:03 -0700762 public:
Tim Murray10913a52013-08-20 17:19:47 -0700763 static sp<ScriptIntrinsicHistogram> create(sp<RS> rs);
Tim Murrayb27b1812013-08-05 14:00:40 -0700764 void setOutput(sp<Allocation> aout);
765 void setDotCoefficients(float r, float g, float b, float a);
766 void forEach(sp<Allocation> ain);
767 void forEach_dot(sp<Allocation> ain);
768};
769
770class ScriptIntrinsicLUT : public ScriptIntrinsic {
771 private:
772 sp<Allocation> LUT;
773 bool mDirty;
774 unsigned char mCache[1024];
775 void setTable(unsigned int offset, unsigned char base, unsigned char length, unsigned char* lutValues);
Tim Murray21fa7a02013-08-15 16:25:03 -0700776 ScriptIntrinsicLUT(sp<RS> rs, sp<const Element> e);
Tim Murrayb27b1812013-08-05 14:00:40 -0700777
Tim Murray89daad62013-07-29 14:30:02 -0700778 public:
Tim Murray21fa7a02013-08-15 16:25:03 -0700779 static sp<ScriptIntrinsicLUT> create(sp<RS> rs, sp<const Element> e);
Tim Murray89daad62013-07-29 14:30:02 -0700780 void forEach(sp<Allocation> ain, sp<Allocation> aout);
Tim Murrayb27b1812013-08-05 14:00:40 -0700781 void setRed(unsigned char base, unsigned char length, unsigned char* lutValues);
782 void setGreen(unsigned char base, unsigned char length, unsigned char* lutValues);
783 void setBlue(unsigned char base, unsigned char length, unsigned char* lutValues);
784 void setAlpha(unsigned char base, unsigned char length, unsigned char* lutValues);
785 virtual ~ScriptIntrinsicLUT();
786};
787
Tim Murray89daad62013-07-29 14:30:02 -0700788class ScriptIntrinsicYuvToRGB : public ScriptIntrinsic {
Tim Murray21fa7a02013-08-15 16:25:03 -0700789 private:
Tim Murrayb27b1812013-08-05 14:00:40 -0700790 ScriptIntrinsicYuvToRGB(sp<RS> rs, sp<const Element> e);
Tim Murray21fa7a02013-08-15 16:25:03 -0700791 public:
792 static sp<ScriptIntrinsicYuvToRGB> create(sp<RS> rs, sp<const Element> e);
Tim Murrayb27b1812013-08-05 14:00:40 -0700793 void setInput(sp<Allocation> in);
794 void forEach(sp<Allocation> out);
Tim Murray89daad62013-07-29 14:30:02 -0700795
796};
Tim Murrayb27b1812013-08-05 14:00:40 -0700797
Tim Murray89daad62013-07-29 14:30:02 -0700798
Tim Murray729b6fe2013-07-23 16:20:42 -0700799 class Sampler : public BaseObj {
800 private:
801 Sampler(sp<RS> rs, void* id);
802 RsSamplerValue mMin;
803 RsSamplerValue mMag;
804 RsSamplerValue mWrapS;
805 RsSamplerValue mWrapT;
806 RsSamplerValue mWrapR;
807 float mAniso;
808
809 public:
810 static sp<Sampler> create(sp<RS> rs, RsSamplerValue min, RsSamplerValue mag, RsSamplerValue wrapS, RsSamplerValue wrapT, float anisotropy);
811
812 RsSamplerValue getMinification();
813 RsSamplerValue getMagnification();
814 RsSamplerValue getWrapS();
815 RsSamplerValue getWrapT();
816 float getAnisotropy();
817
818 sp<const Sampler> CLAMP_NEAREST(sp<RS> rs);
819 sp<const Sampler> CLAMP_LINEAR(sp<RS> rs);
820 sp<const Sampler> CLAMP_LINEAR_MIP_LINEAR(sp<RS> rs);
821 sp<const Sampler> WRAP_NEAREST(sp<RS> rs);
822 sp<const Sampler> WRAP_LINEAR(sp<RS> rs);
823 sp<const Sampler> WRAP_LINEAR_MIP_LINEAR(sp<RS> rs);
824 sp<const Sampler> MIRRORED_REPEAT_NEAREST(sp<RS> rs);
825 sp<const Sampler> MIRRORED_REPEAT_LINEAR(sp<RS> rs);
826 sp<const Sampler> MIRRORED_REPEAT_LINEAR_MIP_LINEAR(sp<RS> rs);
827
828};
829
Tim Murray84bf2b82012-10-31 16:03:16 -0700830}
Tim Murray7f0d5682012-11-08 16:35:24 -0800831
Tim Murray84bf2b82012-10-31 16:03:16 -0700832}
833
834#endif