blob: 386da1a6d74d36117a25c1b06fa55b78159f881b [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();
Tim Murray509ea5c2012-11-13 11:56:40 -0800221
Tim Murray84bf2b82012-10-31 16:03:16 -0700222 void copy1DRangeFromUnchecked(uint32_t off, size_t count, const void *data, size_t dataLen);
Tim Murray509ea5c2012-11-13 11:56:40 -0800223 void copy1DRangeToUnchecked(uint32_t off, size_t count, void *data, size_t dataLen);
224
Tim Murray84bf2b82012-10-31 16:03:16 -0700225 void copy1DRangeFrom(uint32_t off, size_t count, const int32_t* d, size_t dataLen);
226 void copy1DRangeFrom(uint32_t off, size_t count, const int16_t* d, size_t dataLen);
227 void copy1DRangeFrom(uint32_t off, size_t count, const int8_t* d, size_t dataLen);
228 void copy1DRangeFrom(uint32_t off, size_t count, const float* d, size_t dataLen);
229 void copy1DRangeFrom(uint32_t off, size_t count, const Allocation *data, uint32_t dataOff);
230
231 void copy2DRangeFrom(uint32_t xoff, uint32_t yoff, uint32_t w, uint32_t h,
232 const int32_t *data, size_t dataLen);
233 void copy2DRangeFrom(uint32_t xoff, uint32_t yoff, uint32_t w, uint32_t h,
234 const int16_t *data, size_t dataLen);
235 void copy2DRangeFrom(uint32_t xoff, uint32_t yoff, uint32_t w, uint32_t h,
236 const int8_t *data, size_t dataLen);
237 void copy2DRangeFrom(uint32_t xoff, uint32_t yoff, uint32_t w, uint32_t h,
238 const float *data, size_t dataLen);
239 void copy2DRangeFrom(uint32_t xoff, uint32_t yoff, uint32_t w, uint32_t h,
240 const Allocation *data, size_t dataLen,
241 uint32_t dataXoff, uint32_t dataYoff);
242
243 void resize(int dimX);
244 void resize(int dimX, int dimY);
245
246 static sp<Allocation> createTyped(sp<RS> rs, sp<const Type> type,
247 RsAllocationMipmapControl mips, uint32_t usage);
248 static sp<Allocation> createTyped(sp<RS> rs, sp<const Type> type,
249 RsAllocationMipmapControl mips, uint32_t usage, void * pointer);
250
251 static sp<Allocation> createTyped(sp<RS> rs, sp<const Type> type,
252 uint32_t usage = RS_ALLOCATION_USAGE_SCRIPT);
253 static sp<Allocation> createSized(sp<RS> rs, sp<const Element> e, size_t count,
254 uint32_t usage = RS_ALLOCATION_USAGE_SCRIPT);
255
256};
257
258class Element : public BaseObj {
259public:
260 bool isComplex();
261 size_t getSubElementCount() {
262 return mVisibleElementMap.size();
263 }
264
265 sp<const Element> getSubElement(uint32_t index);
266 const char * getSubElementName(uint32_t index);
267 size_t getSubElementArraySize(uint32_t index);
268 uint32_t getSubElementOffsetBytes(uint32_t index);
269 RsDataType getDataType() const {
270 return mType;
271 }
272
273 RsDataKind getDataKind() const {
274 return mKind;
275 }
276
277 size_t getSizeBytes() const {
278 return mSizeBytes;
279 }
280
281 static sp<const Element> BOOLEAN(sp<RS> rs);
282 static sp<const Element> U8(sp<RS> rs);
283 static sp<const Element> I8(sp<RS> rs);
284 static sp<const Element> U16(sp<RS> rs);
285 static sp<const Element> I16(sp<RS> rs);
286 static sp<const Element> U32(sp<RS> rs);
287 static sp<const Element> I32(sp<RS> rs);
288 static sp<const Element> U64(sp<RS> rs);
289 static sp<const Element> I64(sp<RS> rs);
290 static sp<const Element> F32(sp<RS> rs);
291 static sp<const Element> F64(sp<RS> rs);
292 static sp<const Element> ELEMENT(sp<RS> rs);
293 static sp<const Element> TYPE(sp<RS> rs);
294 static sp<const Element> ALLOCATION(sp<RS> rs);
295 static sp<const Element> SAMPLER(sp<RS> rs);
296 static sp<const Element> SCRIPT(sp<RS> rs);
297 static sp<const Element> MESH(sp<RS> rs);
298 static sp<const Element> PROGRAM_FRAGMENT(sp<RS> rs);
299 static sp<const Element> PROGRAM_VERTEX(sp<RS> rs);
300 static sp<const Element> PROGRAM_RASTER(sp<RS> rs);
301 static sp<const Element> PROGRAM_STORE(sp<RS> rs);
302
303 static sp<const Element> A_8(sp<RS> rs);
304 static sp<const Element> RGB_565(sp<RS> rs);
305 static sp<const Element> RGB_888(sp<RS> rs);
306 static sp<const Element> RGBA_5551(sp<RS> rs);
307 static sp<const Element> RGBA_4444(sp<RS> rs);
308 static sp<const Element> RGBA_8888(sp<RS> rs);
309
310 static sp<const Element> F32_2(sp<RS> rs);
311 static sp<const Element> F32_3(sp<RS> rs);
312 static sp<const Element> F32_4(sp<RS> rs);
313 static sp<const Element> F64_2(sp<RS> rs);
314 static sp<const Element> F64_3(sp<RS> rs);
315 static sp<const Element> F64_4(sp<RS> rs);
316 static sp<const Element> U8_2(sp<RS> rs);
317 static sp<const Element> U8_3(sp<RS> rs);
318 static sp<const Element> U8_4(sp<RS> rs);
319 static sp<const Element> I8_2(sp<RS> rs);
320 static sp<const Element> I8_3(sp<RS> rs);
321 static sp<const Element> I8_4(sp<RS> rs);
322 static sp<const Element> U16_2(sp<RS> rs);
323 static sp<const Element> U16_3(sp<RS> rs);
324 static sp<const Element> U16_4(sp<RS> rs);
325 static sp<const Element> I16_2(sp<RS> rs);
326 static sp<const Element> I16_3(sp<RS> rs);
327 static sp<const Element> I16_4(sp<RS> rs);
328 static sp<const Element> U32_2(sp<RS> rs);
329 static sp<const Element> U32_3(sp<RS> rs);
330 static sp<const Element> U32_4(sp<RS> rs);
331 static sp<const Element> I32_2(sp<RS> rs);
332 static sp<const Element> I32_3(sp<RS> rs);
333 static sp<const Element> I32_4(sp<RS> rs);
334 static sp<const Element> U64_2(sp<RS> rs);
335 static sp<const Element> U64_3(sp<RS> rs);
336 static sp<const Element> U64_4(sp<RS> rs);
337 static sp<const Element> I64_2(sp<RS> rs);
338 static sp<const Element> I64_3(sp<RS> rs);
339 static sp<const Element> I64_4(sp<RS> rs);
340 static sp<const Element> MATRIX_4X4(sp<RS> rs);
341 static sp<const Element> MATRIX_3X3(sp<RS> rs);
342 static sp<const Element> MATRIX_2X2(sp<RS> rs);
343
344 Element(void *id, sp<RS> rs,
345 android::Vector<sp<Element> > &elements,
346 android::Vector<android::String8> &elementNames,
347 android::Vector<uint32_t> &arraySizes);
348 Element(void *id, sp<RS> rs, RsDataType dt, RsDataKind dk, bool norm, uint32_t size);
349 Element(sp<RS> rs);
350 virtual ~Element();
351
352 void updateFromNative();
353 static sp<const Element> createUser(sp<RS> rs, RsDataType dt);
354 static sp<const Element> createVector(sp<RS> rs, RsDataType dt, uint32_t size);
355 static sp<const Element> createPixel(sp<RS> rs, RsDataType dt, RsDataKind dk);
356 bool isCompatible(sp<const Element>e);
357
358 class Builder {
359 private:
360 sp<RS> mRS;
361 android::Vector<sp<Element> > mElements;
362 android::Vector<android::String8> mElementNames;
363 android::Vector<uint32_t> mArraySizes;
364 bool mSkipPadding;
365
366 public:
367 Builder(sp<RS> rs);
368 ~Builder();
369 void add(sp<Element>, android::String8 &name, uint32_t arraySize = 1);
370 sp<const Element> create();
371 };
372
373private:
374 void updateVisibleSubElements();
375
376 android::Vector<sp</*const*/ Element> > mElements;
377 android::Vector<android::String8> mElementNames;
378 android::Vector<uint32_t> mArraySizes;
379 android::Vector<uint32_t> mVisibleElementMap;
380 android::Vector<uint32_t> mOffsetInBytes;
381
382 RsDataType mType;
383 RsDataKind mKind;
384 bool mNormalized;
385 size_t mSizeBytes;
386 size_t mVectorSize;
387};
388
Stephen Hines2c7206e2012-11-14 19:47:01 -0800389class FieldPacker {
390protected:
391 unsigned char* mData;
392 size_t mPos;
393 size_t mLen;
394
395public:
396 FieldPacker(size_t len)
397 : mPos(0),
398 mLen(len) {
399 mData = new unsigned char[len];
400 }
401
402 virtual ~FieldPacker() {
403 delete [] mData;
404 }
405
406 void align(size_t v) {
407 if ((v & (v - 1)) != 0) {
408 ALOGE("Non-power-of-two alignment: %zu", v);
409 return;
410 }
411
412 while ((mPos & (v - 1)) != 0) {
413 mData[mPos++] = 0;
414 }
415 }
416
417 void reset() {
418 mPos = 0;
419 }
420
421 void reset(size_t i) {
422 if (i >= mLen) {
423 ALOGE("Out of bounds: i (%zu) >= len (%zu)", i, mLen);
424 return;
425 }
426 mPos = i;
427 }
428
429 void skip(size_t i) {
430 size_t res = mPos + i;
431 if (res > mLen) {
432 ALOGE("Exceeded buffer length: i (%zu) > len (%zu)", i, mLen);
433 return;
434 }
435 mPos = res;
436 }
437
438 void* getData() const {
439 return mData;
440 }
441
442 size_t getLength() const {
443 return mLen;
444 }
445
446 template <typename T>
447 void add(T t) {
448 align(sizeof(t));
449 if (mPos + sizeof(t) <= mLen) {
450 memcpy(&mData[mPos], &t, sizeof(t));
451 mPos += sizeof(t);
452 }
453 }
454};
455
Tim Murray84bf2b82012-10-31 16:03:16 -0700456class Type : public BaseObj {
457protected:
458 friend class Allocation;
459
460 uint32_t mDimX;
461 uint32_t mDimY;
462 uint32_t mDimZ;
463 bool mDimMipmaps;
464 bool mDimFaces;
465 size_t mElementCount;
466 sp<const Element> mElement;
467
468 void calcElementCount();
469 virtual void updateFromNative();
470
471public:
472
473 sp<const Element> getElement() const {
474 return mElement;
475 }
476
477 uint32_t getX() const {
478 return mDimX;
479 }
480
481 uint32_t getY() const {
482 return mDimY;
483 }
484
485 uint32_t getZ() const {
486 return mDimZ;
487 }
488
489 bool hasMipmaps() const {
490 return mDimMipmaps;
491 }
492
493 bool hasFaces() const {
494 return mDimFaces;
495 }
496
497 size_t getCount() const {
498 return mElementCount;
499 }
500
501 size_t getSizeBytes() const {
502 return mElementCount * mElement->getSizeBytes();
503 }
504
505 Type(void *id, sp<RS> rs);
506
507
508 class Builder {
509 protected:
510 sp<RS> mRS;
511 uint32_t mDimX;
512 uint32_t mDimY;
513 uint32_t mDimZ;
514 bool mDimMipmaps;
515 bool mDimFaces;
516 sp<const Element> mElement;
517
518 public:
519 Builder(sp<RS> rs, sp<const Element> e);
520
521 void setX(uint32_t value);
522 void setY(int value);
523 void setMipmaps(bool value);
524 void setFaces(bool value);
525 sp<const Type> create();
526 };
527
528};
529
530class Script : public BaseObj {
531private:
532
533protected:
534 Script(void *id, sp<RS> rs);
535 void forEach(uint32_t slot, sp<const Allocation> in, sp<const Allocation> out,
536 const void *v, size_t) const;
537 void bindAllocation(sp<Allocation> va, uint32_t slot) const;
538 void setVar(uint32_t index, const void *, size_t len) const;
539 void setVar(uint32_t index, sp<const BaseObj> o) const;
540 void invoke(uint32_t slot, const void *v, size_t len) const;
541
542
543 void invoke(uint32_t slot) const {
544 invoke(slot, NULL, 0);
545 }
546 void setVar(uint32_t index, float v) const {
547 setVar(index, &v, sizeof(v));
548 }
549 void setVar(uint32_t index, double v) const {
550 setVar(index, &v, sizeof(v));
551 }
552 void setVar(uint32_t index, int32_t v) const {
553 setVar(index, &v, sizeof(v));
554 }
555 void setVar(uint32_t index, int64_t v) const {
556 setVar(index, &v, sizeof(v));
557 }
558 void setVar(uint32_t index, bool v) const {
559 setVar(index, &v, sizeof(v));
560 }
561
562public:
563 class FieldBase {
564 protected:
565 sp<const Element> mElement;
566 sp<Allocation> mAllocation;
567
568 void init(sp<RS> rs, uint32_t dimx, uint32_t usages = 0);
569
570 public:
571 sp<const Element> getElement() {
572 return mElement;
573 }
574
575 sp<const Type> getType() {
576 return mAllocation->getType();
577 }
578
579 sp<const Allocation> getAllocation() {
580 return mAllocation;
581 }
582
583 //void updateAllocation();
584 };
585};
586
587class ScriptC : public Script {
588protected:
589 ScriptC(sp<RS> rs,
590 const void *codeTxt, size_t codeLength,
591 const char *cachedName, size_t cachedNameLength,
592 const char *cacheDir, size_t cacheDirLength);
593
594};
595
Tim Murray7f0d5682012-11-08 16:35:24 -0800596class ScriptIntrinsic : public Script {
597 protected:
598 ScriptIntrinsic(sp<RS> rs, int id, Element *e);
599};
600
601class ScriptIntrinsicBlend : public ScriptIntrinsic {
602 public:
603 ScriptIntrinsicBlend(sp<RS> rs, Element *e);
604 void blendClear(sp<Allocation> in, sp<Allocation> out);
605 void blendSrc(sp<Allocation> in, sp<Allocation> out);
606 void blendDst(sp<Allocation> in, sp<Allocation> out);
607 void blendSrcOver(sp<Allocation> in, sp<Allocation> out);
608 void blendDstOver(sp<Allocation> in, sp<Allocation> out);
609 void blendSrcIn(sp<Allocation> in, sp<Allocation> out);
610 void blendDstIn(sp<Allocation> in, sp<Allocation> out);
611 void blendSrcOut(sp<Allocation> in, sp<Allocation> out);
612 void blendDstOut(sp<Allocation> in, sp<Allocation> out);
613 void blendSrcAtop(sp<Allocation> in, sp<Allocation> out);
614 void blendDstAtop(sp<Allocation> in, sp<Allocation> out);
615 void blendXor(sp<Allocation> in, sp<Allocation> out);
616 void blendMultiply(sp<Allocation> in, sp<Allocation> out);
617 void blendAdd(sp<Allocation> in, sp<Allocation> out);
618 void blendSubtract(sp<Allocation> in, sp<Allocation> out);
619};
Tim Murray84bf2b82012-10-31 16:03:16 -0700620
Tim Murray8f1e60c2012-11-13 12:25:11 -0800621class ScriptIntrinsicBlur : public ScriptIntrinsic {
622 public:
623 ScriptIntrinsicBlur(sp<RS> rs, Element *e);
624 void blur(sp<Allocation> in, sp<Allocation> out);
625 void setRadius(float radius);
626};
627
Tim Murray84bf2b82012-10-31 16:03:16 -0700628}
Tim Murray7f0d5682012-11-08 16:35:24 -0800629
Tim Murray84bf2b82012-10-31 16:03:16 -0700630}
631
632#endif