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