blob: 0b7e613d14c5e7faf3bf73463888a56bb6a19b8b [file] [log] [blame]
Tim Murray84bf2b82012-10-31 16:03:16 -07001/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
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
20#include <utils/String8.h>
21#include <utils/Vector.h>
22#include "utils/RefBase.h"
23
24#include <rs.h>
25
26namespace android {
27namespace renderscriptCpp {
28
29typedef void (*ErrorHandlerFunc_t)(uint32_t errorNum, const char *errorText);
30typedef void (*MessageHandlerFunc_t)(uint32_t msgNum, const void *msgData, size_t msgLen);
31
32class RS;
33class BaseObj;
34class Element;
35class Type;
36class Allocation;
37class Script;
38class ScriptC;
39
40class RS : public android::LightRefBase<RS> {
41
42 public:
43 RS();
44 virtual ~RS();
45
Tim Murray7e0acab2012-11-06 14:37:19 -080046 bool init() { return init(false); }
47 bool init(bool forceCpu);
Tim Murray84bf2b82012-10-31 16:03:16 -070048
49 void setErrorHandler(ErrorHandlerFunc_t func);
50 ErrorHandlerFunc_t getErrorHandler() { return mErrorFunc; }
51
52 void setMessageHandler(MessageHandlerFunc_t func);
53 MessageHandlerFunc_t getMessageHandler() { return mMessageFunc; }
54
55 void throwError(const char *err) const;
56
57 RsContext getContext() { return mContext; }
58
59 private:
Tim Murray7e0acab2012-11-06 14:37:19 -080060 bool init(int targetApi, bool forceCpu);
Tim Murray84bf2b82012-10-31 16:03:16 -070061 static void * threadProc(void *);
62
63 static bool gInitialized;
64 static pthread_mutex_t gInitMutex;
65
66 pthread_t mMessageThreadId;
67 pid_t mNativeMessageThreadId;
68 bool mMessageRun;
69
70 RsDevice mDev;
71 RsContext mContext;
72
73 ErrorHandlerFunc_t mErrorFunc;
74 MessageHandlerFunc_t mMessageFunc;
75
76 struct {
77 Element *U8;
78 Element *I8;
79 Element *U16;
80 Element *I16;
81 Element *U32;
82 Element *I32;
83 Element *U64;
84 Element *I64;
85 Element *F32;
86 Element *F64;
87 Element *BOOLEAN;
88
89 Element *ELEMENT;
90 Element *TYPE;
91 Element *ALLOCATION;
92 Element *SAMPLER;
93 Element *SCRIPT;
94 Element *MESH;
95 Element *PROGRAM_FRAGMENT;
96 Element *PROGRAM_VERTEX;
97 Element *PROGRAM_RASTER;
98 Element *PROGRAM_STORE;
99
100 Element *A_8;
101 Element *RGB_565;
102 Element *RGB_888;
103 Element *RGBA_5551;
104 Element *RGBA_4444;
105 Element *RGBA_8888;
106
107 Element *FLOAT_2;
108 Element *FLOAT_3;
109 Element *FLOAT_4;
110
111 Element *DOUBLE_2;
112 Element *DOUBLE_3;
113 Element *DOUBLE_4;
114
115 Element *UCHAR_2;
116 Element *UCHAR_3;
117 Element *UCHAR_4;
118
119 Element *CHAR_2;
120 Element *CHAR_3;
121 Element *CHAR_4;
122
123 Element *USHORT_2;
124 Element *USHORT_3;
125 Element *USHORT_4;
126
127 Element *SHORT_2;
128 Element *SHORT_3;
129 Element *SHORT_4;
130
131 Element *UINT_2;
132 Element *UINT_3;
133 Element *UINT_4;
134
135 Element *INT_2;
136 Element *INT_3;
137 Element *INT_4;
138
139 Element *ULONG_2;
140 Element *ULONG_3;
141 Element *ULONG_4;
142
143 Element *LONG_2;
144 Element *LONG_3;
145 Element *LONG_4;
146
147 Element *MATRIX_4X4;
148 Element *MATRIX_3X3;
149 Element *MATRIX_2X2;
150 } mElements;
151
152};
153
154class BaseObj : public android::LightRefBase<BaseObj> {
155protected:
156 void *mID;
157 sp<RS> mRS;
158 String8 mName;
159
160 BaseObj(void *id, sp<RS> rs);
161 void checkValid();
162
163 static void * getObjID(sp<const BaseObj> o);
164
165public:
166
167 void * getID() const;
168 virtual ~BaseObj();
169 virtual void updateFromNative();
170 virtual bool equals(const BaseObj *obj);
171};
172
173
174class Allocation : public BaseObj {
175protected:
176 android::sp<const Type> mType;
177 uint32_t mUsage;
178 android::sp<Allocation> mAdaptedAllocation;
179
180 bool mConstrainedLOD;
181 bool mConstrainedFace;
182 bool mConstrainedY;
183 bool mConstrainedZ;
184 bool mReadAllowed;
185 bool mWriteAllowed;
186 uint32_t mSelectedY;
187 uint32_t mSelectedZ;
188 uint32_t mSelectedLOD;
189 RsAllocationCubemapFace mSelectedFace;
190
191 uint32_t mCurrentDimX;
192 uint32_t mCurrentDimY;
193 uint32_t mCurrentDimZ;
194 uint32_t mCurrentCount;
195
196 void * getIDSafe() const;
197 void updateCacheInfo(sp<const Type> t);
198
199 Allocation(void *id, sp<RS> rs, sp<const Type> t, uint32_t usage);
200
201 void validateIsInt32();
202 void validateIsInt16();
203 void validateIsInt8();
204 void validateIsFloat32();
205 void validateIsObject();
206
207 virtual void updateFromNative();
208
209 void validate2DRange(uint32_t xoff, uint32_t yoff, uint32_t w, uint32_t h);
210
211public:
212 android::sp<const Type> getType() {
213 return mType;
214 }
215
216 void syncAll(RsAllocationUsageType srcLocation);
217 void ioSendOutput();
218 void ioGetInput();
219
220 void generateMipmaps();
221 void copy1DRangeFromUnchecked(uint32_t off, size_t count, const void *data, size_t dataLen);
222 void copy1DRangeFrom(uint32_t off, size_t count, const int32_t* d, size_t dataLen);
223 void copy1DRangeFrom(uint32_t off, size_t count, const int16_t* d, size_t dataLen);
224 void copy1DRangeFrom(uint32_t off, size_t count, const int8_t* d, size_t dataLen);
225 void copy1DRangeFrom(uint32_t off, size_t count, const float* d, size_t dataLen);
226 void copy1DRangeFrom(uint32_t off, size_t count, const Allocation *data, uint32_t dataOff);
227
228 void copy2DRangeFrom(uint32_t xoff, uint32_t yoff, uint32_t w, uint32_t h,
229 const int32_t *data, size_t dataLen);
230 void copy2DRangeFrom(uint32_t xoff, uint32_t yoff, uint32_t w, uint32_t h,
231 const int16_t *data, size_t dataLen);
232 void copy2DRangeFrom(uint32_t xoff, uint32_t yoff, uint32_t w, uint32_t h,
233 const int8_t *data, size_t dataLen);
234 void copy2DRangeFrom(uint32_t xoff, uint32_t yoff, uint32_t w, uint32_t h,
235 const float *data, size_t dataLen);
236 void copy2DRangeFrom(uint32_t xoff, uint32_t yoff, uint32_t w, uint32_t h,
237 const Allocation *data, size_t dataLen,
238 uint32_t dataXoff, uint32_t dataYoff);
239
240 void resize(int dimX);
241 void resize(int dimX, int dimY);
242
243 static sp<Allocation> createTyped(sp<RS> rs, sp<const Type> type,
244 RsAllocationMipmapControl mips, uint32_t usage);
245 static sp<Allocation> createTyped(sp<RS> rs, sp<const Type> type,
246 RsAllocationMipmapControl mips, uint32_t usage, void * pointer);
247
248 static sp<Allocation> createTyped(sp<RS> rs, sp<const Type> type,
249 uint32_t usage = RS_ALLOCATION_USAGE_SCRIPT);
250 static sp<Allocation> createSized(sp<RS> rs, sp<const Element> e, size_t count,
251 uint32_t usage = RS_ALLOCATION_USAGE_SCRIPT);
252
253};
254
255class Element : public BaseObj {
256public:
257 bool isComplex();
258 size_t getSubElementCount() {
259 return mVisibleElementMap.size();
260 }
261
262 sp<const Element> getSubElement(uint32_t index);
263 const char * getSubElementName(uint32_t index);
264 size_t getSubElementArraySize(uint32_t index);
265 uint32_t getSubElementOffsetBytes(uint32_t index);
266 RsDataType getDataType() const {
267 return mType;
268 }
269
270 RsDataKind getDataKind() const {
271 return mKind;
272 }
273
274 size_t getSizeBytes() const {
275 return mSizeBytes;
276 }
277
278 static sp<const Element> BOOLEAN(sp<RS> rs);
279 static sp<const Element> U8(sp<RS> rs);
280 static sp<const Element> I8(sp<RS> rs);
281 static sp<const Element> U16(sp<RS> rs);
282 static sp<const Element> I16(sp<RS> rs);
283 static sp<const Element> U32(sp<RS> rs);
284 static sp<const Element> I32(sp<RS> rs);
285 static sp<const Element> U64(sp<RS> rs);
286 static sp<const Element> I64(sp<RS> rs);
287 static sp<const Element> F32(sp<RS> rs);
288 static sp<const Element> F64(sp<RS> rs);
289 static sp<const Element> ELEMENT(sp<RS> rs);
290 static sp<const Element> TYPE(sp<RS> rs);
291 static sp<const Element> ALLOCATION(sp<RS> rs);
292 static sp<const Element> SAMPLER(sp<RS> rs);
293 static sp<const Element> SCRIPT(sp<RS> rs);
294 static sp<const Element> MESH(sp<RS> rs);
295 static sp<const Element> PROGRAM_FRAGMENT(sp<RS> rs);
296 static sp<const Element> PROGRAM_VERTEX(sp<RS> rs);
297 static sp<const Element> PROGRAM_RASTER(sp<RS> rs);
298 static sp<const Element> PROGRAM_STORE(sp<RS> rs);
299
300 static sp<const Element> A_8(sp<RS> rs);
301 static sp<const Element> RGB_565(sp<RS> rs);
302 static sp<const Element> RGB_888(sp<RS> rs);
303 static sp<const Element> RGBA_5551(sp<RS> rs);
304 static sp<const Element> RGBA_4444(sp<RS> rs);
305 static sp<const Element> RGBA_8888(sp<RS> rs);
306
307 static sp<const Element> F32_2(sp<RS> rs);
308 static sp<const Element> F32_3(sp<RS> rs);
309 static sp<const Element> F32_4(sp<RS> rs);
310 static sp<const Element> F64_2(sp<RS> rs);
311 static sp<const Element> F64_3(sp<RS> rs);
312 static sp<const Element> F64_4(sp<RS> rs);
313 static sp<const Element> U8_2(sp<RS> rs);
314 static sp<const Element> U8_3(sp<RS> rs);
315 static sp<const Element> U8_4(sp<RS> rs);
316 static sp<const Element> I8_2(sp<RS> rs);
317 static sp<const Element> I8_3(sp<RS> rs);
318 static sp<const Element> I8_4(sp<RS> rs);
319 static sp<const Element> U16_2(sp<RS> rs);
320 static sp<const Element> U16_3(sp<RS> rs);
321 static sp<const Element> U16_4(sp<RS> rs);
322 static sp<const Element> I16_2(sp<RS> rs);
323 static sp<const Element> I16_3(sp<RS> rs);
324 static sp<const Element> I16_4(sp<RS> rs);
325 static sp<const Element> U32_2(sp<RS> rs);
326 static sp<const Element> U32_3(sp<RS> rs);
327 static sp<const Element> U32_4(sp<RS> rs);
328 static sp<const Element> I32_2(sp<RS> rs);
329 static sp<const Element> I32_3(sp<RS> rs);
330 static sp<const Element> I32_4(sp<RS> rs);
331 static sp<const Element> U64_2(sp<RS> rs);
332 static sp<const Element> U64_3(sp<RS> rs);
333 static sp<const Element> U64_4(sp<RS> rs);
334 static sp<const Element> I64_2(sp<RS> rs);
335 static sp<const Element> I64_3(sp<RS> rs);
336 static sp<const Element> I64_4(sp<RS> rs);
337 static sp<const Element> MATRIX_4X4(sp<RS> rs);
338 static sp<const Element> MATRIX_3X3(sp<RS> rs);
339 static sp<const Element> MATRIX_2X2(sp<RS> rs);
340
341 Element(void *id, sp<RS> rs,
342 android::Vector<sp<Element> > &elements,
343 android::Vector<android::String8> &elementNames,
344 android::Vector<uint32_t> &arraySizes);
345 Element(void *id, sp<RS> rs, RsDataType dt, RsDataKind dk, bool norm, uint32_t size);
346 Element(sp<RS> rs);
347 virtual ~Element();
348
349 void updateFromNative();
350 static sp<const Element> createUser(sp<RS> rs, RsDataType dt);
351 static sp<const Element> createVector(sp<RS> rs, RsDataType dt, uint32_t size);
352 static sp<const Element> createPixel(sp<RS> rs, RsDataType dt, RsDataKind dk);
353 bool isCompatible(sp<const Element>e);
354
355 class Builder {
356 private:
357 sp<RS> mRS;
358 android::Vector<sp<Element> > mElements;
359 android::Vector<android::String8> mElementNames;
360 android::Vector<uint32_t> mArraySizes;
361 bool mSkipPadding;
362
363 public:
364 Builder(sp<RS> rs);
365 ~Builder();
366 void add(sp<Element>, android::String8 &name, uint32_t arraySize = 1);
367 sp<const Element> create();
368 };
369
370private:
371 void updateVisibleSubElements();
372
373 android::Vector<sp</*const*/ Element> > mElements;
374 android::Vector<android::String8> mElementNames;
375 android::Vector<uint32_t> mArraySizes;
376 android::Vector<uint32_t> mVisibleElementMap;
377 android::Vector<uint32_t> mOffsetInBytes;
378
379 RsDataType mType;
380 RsDataKind mKind;
381 bool mNormalized;
382 size_t mSizeBytes;
383 size_t mVectorSize;
384};
385
386class Type : public BaseObj {
387protected:
388 friend class Allocation;
389
390 uint32_t mDimX;
391 uint32_t mDimY;
392 uint32_t mDimZ;
393 bool mDimMipmaps;
394 bool mDimFaces;
395 size_t mElementCount;
396 sp<const Element> mElement;
397
398 void calcElementCount();
399 virtual void updateFromNative();
400
401public:
402
403 sp<const Element> getElement() const {
404 return mElement;
405 }
406
407 uint32_t getX() const {
408 return mDimX;
409 }
410
411 uint32_t getY() const {
412 return mDimY;
413 }
414
415 uint32_t getZ() const {
416 return mDimZ;
417 }
418
419 bool hasMipmaps() const {
420 return mDimMipmaps;
421 }
422
423 bool hasFaces() const {
424 return mDimFaces;
425 }
426
427 size_t getCount() const {
428 return mElementCount;
429 }
430
431 size_t getSizeBytes() const {
432 return mElementCount * mElement->getSizeBytes();
433 }
434
435 Type(void *id, sp<RS> rs);
436
437
438 class Builder {
439 protected:
440 sp<RS> mRS;
441 uint32_t mDimX;
442 uint32_t mDimY;
443 uint32_t mDimZ;
444 bool mDimMipmaps;
445 bool mDimFaces;
446 sp<const Element> mElement;
447
448 public:
449 Builder(sp<RS> rs, sp<const Element> e);
450
451 void setX(uint32_t value);
452 void setY(int value);
453 void setMipmaps(bool value);
454 void setFaces(bool value);
455 sp<const Type> create();
456 };
457
458};
459
460class Script : public BaseObj {
461private:
462
463protected:
464 Script(void *id, sp<RS> rs);
465 void forEach(uint32_t slot, sp<const Allocation> in, sp<const Allocation> out,
466 const void *v, size_t) const;
467 void bindAllocation(sp<Allocation> va, uint32_t slot) const;
468 void setVar(uint32_t index, const void *, size_t len) const;
469 void setVar(uint32_t index, sp<const BaseObj> o) const;
470 void invoke(uint32_t slot, const void *v, size_t len) const;
471
472
473 void invoke(uint32_t slot) const {
474 invoke(slot, NULL, 0);
475 }
476 void setVar(uint32_t index, float v) const {
477 setVar(index, &v, sizeof(v));
478 }
479 void setVar(uint32_t index, double v) const {
480 setVar(index, &v, sizeof(v));
481 }
482 void setVar(uint32_t index, int32_t v) const {
483 setVar(index, &v, sizeof(v));
484 }
485 void setVar(uint32_t index, int64_t v) const {
486 setVar(index, &v, sizeof(v));
487 }
488 void setVar(uint32_t index, bool v) const {
489 setVar(index, &v, sizeof(v));
490 }
491
492public:
493 class FieldBase {
494 protected:
495 sp<const Element> mElement;
496 sp<Allocation> mAllocation;
497
498 void init(sp<RS> rs, uint32_t dimx, uint32_t usages = 0);
499
500 public:
501 sp<const Element> getElement() {
502 return mElement;
503 }
504
505 sp<const Type> getType() {
506 return mAllocation->getType();
507 }
508
509 sp<const Allocation> getAllocation() {
510 return mAllocation;
511 }
512
513 //void updateAllocation();
514 };
515};
516
517class ScriptC : public Script {
518protected:
519 ScriptC(sp<RS> rs,
520 const void *codeTxt, size_t codeLength,
521 const char *cachedName, size_t cachedNameLength,
522 const char *cacheDir, size_t cacheDirLength);
523
524};
525
Tim Murray7f0d5682012-11-08 16:35:24 -0800526class ScriptIntrinsic : public Script {
527 protected:
528 ScriptIntrinsic(sp<RS> rs, int id, Element *e);
529};
530
531class ScriptIntrinsicBlend : public ScriptIntrinsic {
532 public:
533 ScriptIntrinsicBlend(sp<RS> rs, Element *e);
534 void blendClear(sp<Allocation> in, sp<Allocation> out);
535 void blendSrc(sp<Allocation> in, sp<Allocation> out);
536 void blendDst(sp<Allocation> in, sp<Allocation> out);
537 void blendSrcOver(sp<Allocation> in, sp<Allocation> out);
538 void blendDstOver(sp<Allocation> in, sp<Allocation> out);
539 void blendSrcIn(sp<Allocation> in, sp<Allocation> out);
540 void blendDstIn(sp<Allocation> in, sp<Allocation> out);
541 void blendSrcOut(sp<Allocation> in, sp<Allocation> out);
542 void blendDstOut(sp<Allocation> in, sp<Allocation> out);
543 void blendSrcAtop(sp<Allocation> in, sp<Allocation> out);
544 void blendDstAtop(sp<Allocation> in, sp<Allocation> out);
545 void blendXor(sp<Allocation> in, sp<Allocation> out);
546 void blendMultiply(sp<Allocation> in, sp<Allocation> out);
547 void blendAdd(sp<Allocation> in, sp<Allocation> out);
548 void blendSubtract(sp<Allocation> in, sp<Allocation> out);
549};
Tim Murray84bf2b82012-10-31 16:03:16 -0700550
551}
Tim Murray7f0d5682012-11-08 16:35:24 -0800552
Tim Murray84bf2b82012-10-31 16:03:16 -0700553}
554
555#endif