blob: a430c3512fe33518f121ac1351451f1b37193f82 [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 {
Tim Murray9eb7f4b2012-11-16 14:02:18 -080027namespace RSC {
Tim Murray84bf2b82012-10-31 16:03:16 -070028
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
Tim Murraybaca6c32012-11-14 16:51:46 -080059 void finish();
60
Tim Murray84bf2b82012-10-31 16:03:16 -070061 private:
Tim Murray7e0acab2012-11-06 14:37:19 -080062 bool init(int targetApi, bool forceCpu);
Tim Murray84bf2b82012-10-31 16:03:16 -070063 static void * threadProc(void *);
64
65 static bool gInitialized;
66 static pthread_mutex_t gInitMutex;
67
68 pthread_t mMessageThreadId;
69 pid_t mNativeMessageThreadId;
70 bool mMessageRun;
71
72 RsDevice mDev;
73 RsContext mContext;
74
75 ErrorHandlerFunc_t mErrorFunc;
76 MessageHandlerFunc_t mMessageFunc;
77
78 struct {
79 Element *U8;
80 Element *I8;
81 Element *U16;
82 Element *I16;
83 Element *U32;
84 Element *I32;
85 Element *U64;
86 Element *I64;
87 Element *F32;
88 Element *F64;
89 Element *BOOLEAN;
90
91 Element *ELEMENT;
92 Element *TYPE;
93 Element *ALLOCATION;
94 Element *SAMPLER;
95 Element *SCRIPT;
96 Element *MESH;
97 Element *PROGRAM_FRAGMENT;
98 Element *PROGRAM_VERTEX;
99 Element *PROGRAM_RASTER;
100 Element *PROGRAM_STORE;
101
102 Element *A_8;
103 Element *RGB_565;
104 Element *RGB_888;
105 Element *RGBA_5551;
106 Element *RGBA_4444;
107 Element *RGBA_8888;
108
109 Element *FLOAT_2;
110 Element *FLOAT_3;
111 Element *FLOAT_4;
112
113 Element *DOUBLE_2;
114 Element *DOUBLE_3;
115 Element *DOUBLE_4;
116
117 Element *UCHAR_2;
118 Element *UCHAR_3;
119 Element *UCHAR_4;
120
121 Element *CHAR_2;
122 Element *CHAR_3;
123 Element *CHAR_4;
124
125 Element *USHORT_2;
126 Element *USHORT_3;
127 Element *USHORT_4;
128
129 Element *SHORT_2;
130 Element *SHORT_3;
131 Element *SHORT_4;
132
133 Element *UINT_2;
134 Element *UINT_3;
135 Element *UINT_4;
136
137 Element *INT_2;
138 Element *INT_3;
139 Element *INT_4;
140
141 Element *ULONG_2;
142 Element *ULONG_3;
143 Element *ULONG_4;
144
145 Element *LONG_2;
146 Element *LONG_3;
147 Element *LONG_4;
148
149 Element *MATRIX_4X4;
150 Element *MATRIX_3X3;
151 Element *MATRIX_2X2;
152 } mElements;
153
154};
155
156class BaseObj : public android::LightRefBase<BaseObj> {
157protected:
158 void *mID;
159 sp<RS> mRS;
160 String8 mName;
161
162 BaseObj(void *id, sp<RS> rs);
163 void checkValid();
164
165 static void * getObjID(sp<const BaseObj> o);
166
167public:
168
169 void * getID() const;
170 virtual ~BaseObj();
171 virtual void updateFromNative();
172 virtual bool equals(const BaseObj *obj);
173};
174
175
176class Allocation : public BaseObj {
177protected:
178 android::sp<const Type> mType;
179 uint32_t mUsage;
180 android::sp<Allocation> mAdaptedAllocation;
181
182 bool mConstrainedLOD;
183 bool mConstrainedFace;
184 bool mConstrainedY;
185 bool mConstrainedZ;
186 bool mReadAllowed;
187 bool mWriteAllowed;
188 uint32_t mSelectedY;
189 uint32_t mSelectedZ;
190 uint32_t mSelectedLOD;
191 RsAllocationCubemapFace mSelectedFace;
192
193 uint32_t mCurrentDimX;
194 uint32_t mCurrentDimY;
195 uint32_t mCurrentDimZ;
196 uint32_t mCurrentCount;
197
198 void * getIDSafe() const;
199 void updateCacheInfo(sp<const Type> t);
200
201 Allocation(void *id, sp<RS> rs, sp<const Type> t, uint32_t usage);
202
203 void validateIsInt32();
204 void validateIsInt16();
205 void validateIsInt8();
206 void validateIsFloat32();
207 void validateIsObject();
208
209 virtual void updateFromNative();
210
211 void validate2DRange(uint32_t xoff, uint32_t yoff, uint32_t w, uint32_t h);
212
213public:
214 android::sp<const Type> getType() {
215 return mType;
216 }
217
218 void syncAll(RsAllocationUsageType srcLocation);
219 void ioSendOutput();
220 void ioGetInput();
221
222 void generateMipmaps();
Tim Murray509ea5c2012-11-13 11:56:40 -0800223
Tim Murray0b93e302012-11-15 14:56:54 -0800224 void copy1DRangeFrom(uint32_t off, size_t count, const void *data);
Tim Murraya4cbc2b2012-11-14 17:18:08 -0800225 void copy1DRangeFrom(uint32_t off, size_t count, sp<const Allocation> data, uint32_t dataOff);
Tim Murray84bf2b82012-10-31 16:03:16 -0700226
Tim Murray0b93e302012-11-15 14:56:54 -0800227 void copy1DRangeTo(uint32_t off, size_t count, void *data);
228
229 void copy1DFrom(const void* data);
230 void copy1DTo(void* data);
Tim Murraya4cbc2b2012-11-14 17:18:08 -0800231
Tim Murray84bf2b82012-10-31 16:03:16 -0700232 void copy2DRangeFrom(uint32_t xoff, uint32_t yoff, uint32_t w, uint32_t h,
Tim Murray0b93e302012-11-15 14:56:54 -0800233 const void *data);
Tim Murray7b3e3092012-11-16 13:32:24 -0800234
235 void copy2DRangeTo(uint32_t xoff, uint32_t yoff, uint32_t w, uint32_t h,
236 void *data);
237
Tim Murray0b93e302012-11-15 14:56:54 -0800238 void copy2DRangeFrom(uint32_t xoff, uint32_t yoff, uint32_t w, uint32_t h,
239 sp<const Allocation> data, uint32_t dataXoff, uint32_t dataYoff);
Tim Murray84bf2b82012-10-31 16:03:16 -0700240
Tim Murray358747a2012-11-26 13:52:04 -0800241 void copy2DStridedFrom(uint32_t xoff, uint32_t yoff, uint32_t w, uint32_t h,
242 const void *data, size_t stride);
243 void copy2DStridedFrom(const void *data, size_t stride);
244
245 void copy2DStridedTo(uint32_t xoff, uint32_t yoff, uint32_t w, uint32_t h,
246 void *data, size_t stride);
247 void copy2DStridedTo(void *data, size_t stride);
248
Tim Murray84bf2b82012-10-31 16:03:16 -0700249 void resize(int dimX);
250 void resize(int dimX, int dimY);
251
252 static sp<Allocation> createTyped(sp<RS> rs, sp<const Type> type,
253 RsAllocationMipmapControl mips, uint32_t usage);
254 static sp<Allocation> createTyped(sp<RS> rs, sp<const Type> type,
255 RsAllocationMipmapControl mips, uint32_t usage, void * pointer);
256
257 static sp<Allocation> createTyped(sp<RS> rs, sp<const Type> type,
258 uint32_t usage = RS_ALLOCATION_USAGE_SCRIPT);
259 static sp<Allocation> createSized(sp<RS> rs, sp<const Element> e, size_t count,
260 uint32_t usage = RS_ALLOCATION_USAGE_SCRIPT);
Tim Murray684726c2012-11-14 11:57:42 -0800261 static sp<Allocation> createSized2D(sp<RS> rs, sp<const Element> e,
262 size_t x, size_t y,
263 uint32_t usage = RS_ALLOCATION_USAGE_SCRIPT);
264
Tim Murray84bf2b82012-10-31 16:03:16 -0700265
266};
267
268class Element : public BaseObj {
269public:
270 bool isComplex();
271 size_t getSubElementCount() {
272 return mVisibleElementMap.size();
273 }
274
275 sp<const Element> getSubElement(uint32_t index);
276 const char * getSubElementName(uint32_t index);
277 size_t getSubElementArraySize(uint32_t index);
278 uint32_t getSubElementOffsetBytes(uint32_t index);
279 RsDataType getDataType() const {
280 return mType;
281 }
282
283 RsDataKind getDataKind() const {
284 return mKind;
285 }
286
287 size_t getSizeBytes() const {
288 return mSizeBytes;
289 }
290
291 static sp<const Element> BOOLEAN(sp<RS> rs);
292 static sp<const Element> U8(sp<RS> rs);
293 static sp<const Element> I8(sp<RS> rs);
294 static sp<const Element> U16(sp<RS> rs);
295 static sp<const Element> I16(sp<RS> rs);
296 static sp<const Element> U32(sp<RS> rs);
297 static sp<const Element> I32(sp<RS> rs);
298 static sp<const Element> U64(sp<RS> rs);
299 static sp<const Element> I64(sp<RS> rs);
300 static sp<const Element> F32(sp<RS> rs);
301 static sp<const Element> F64(sp<RS> rs);
302 static sp<const Element> ELEMENT(sp<RS> rs);
303 static sp<const Element> TYPE(sp<RS> rs);
304 static sp<const Element> ALLOCATION(sp<RS> rs);
305 static sp<const Element> SAMPLER(sp<RS> rs);
306 static sp<const Element> SCRIPT(sp<RS> rs);
307 static sp<const Element> MESH(sp<RS> rs);
308 static sp<const Element> PROGRAM_FRAGMENT(sp<RS> rs);
309 static sp<const Element> PROGRAM_VERTEX(sp<RS> rs);
310 static sp<const Element> PROGRAM_RASTER(sp<RS> rs);
311 static sp<const Element> PROGRAM_STORE(sp<RS> rs);
312
313 static sp<const Element> A_8(sp<RS> rs);
314 static sp<const Element> RGB_565(sp<RS> rs);
315 static sp<const Element> RGB_888(sp<RS> rs);
316 static sp<const Element> RGBA_5551(sp<RS> rs);
317 static sp<const Element> RGBA_4444(sp<RS> rs);
318 static sp<const Element> RGBA_8888(sp<RS> rs);
319
320 static sp<const Element> F32_2(sp<RS> rs);
321 static sp<const Element> F32_3(sp<RS> rs);
322 static sp<const Element> F32_4(sp<RS> rs);
323 static sp<const Element> F64_2(sp<RS> rs);
324 static sp<const Element> F64_3(sp<RS> rs);
325 static sp<const Element> F64_4(sp<RS> rs);
326 static sp<const Element> U8_2(sp<RS> rs);
327 static sp<const Element> U8_3(sp<RS> rs);
328 static sp<const Element> U8_4(sp<RS> rs);
329 static sp<const Element> I8_2(sp<RS> rs);
330 static sp<const Element> I8_3(sp<RS> rs);
331 static sp<const Element> I8_4(sp<RS> rs);
332 static sp<const Element> U16_2(sp<RS> rs);
333 static sp<const Element> U16_3(sp<RS> rs);
334 static sp<const Element> U16_4(sp<RS> rs);
335 static sp<const Element> I16_2(sp<RS> rs);
336 static sp<const Element> I16_3(sp<RS> rs);
337 static sp<const Element> I16_4(sp<RS> rs);
338 static sp<const Element> U32_2(sp<RS> rs);
339 static sp<const Element> U32_3(sp<RS> rs);
340 static sp<const Element> U32_4(sp<RS> rs);
341 static sp<const Element> I32_2(sp<RS> rs);
342 static sp<const Element> I32_3(sp<RS> rs);
343 static sp<const Element> I32_4(sp<RS> rs);
344 static sp<const Element> U64_2(sp<RS> rs);
345 static sp<const Element> U64_3(sp<RS> rs);
346 static sp<const Element> U64_4(sp<RS> rs);
347 static sp<const Element> I64_2(sp<RS> rs);
348 static sp<const Element> I64_3(sp<RS> rs);
349 static sp<const Element> I64_4(sp<RS> rs);
350 static sp<const Element> MATRIX_4X4(sp<RS> rs);
351 static sp<const Element> MATRIX_3X3(sp<RS> rs);
352 static sp<const Element> MATRIX_2X2(sp<RS> rs);
353
354 Element(void *id, sp<RS> rs,
355 android::Vector<sp<Element> > &elements,
356 android::Vector<android::String8> &elementNames,
357 android::Vector<uint32_t> &arraySizes);
358 Element(void *id, sp<RS> rs, RsDataType dt, RsDataKind dk, bool norm, uint32_t size);
359 Element(sp<RS> rs);
360 virtual ~Element();
361
362 void updateFromNative();
363 static sp<const Element> createUser(sp<RS> rs, RsDataType dt);
364 static sp<const Element> createVector(sp<RS> rs, RsDataType dt, uint32_t size);
365 static sp<const Element> createPixel(sp<RS> rs, RsDataType dt, RsDataKind dk);
366 bool isCompatible(sp<const Element>e);
367
368 class Builder {
369 private:
370 sp<RS> mRS;
371 android::Vector<sp<Element> > mElements;
372 android::Vector<android::String8> mElementNames;
373 android::Vector<uint32_t> mArraySizes;
374 bool mSkipPadding;
375
376 public:
377 Builder(sp<RS> rs);
378 ~Builder();
379 void add(sp<Element>, android::String8 &name, uint32_t arraySize = 1);
380 sp<const Element> create();
381 };
382
383private:
384 void updateVisibleSubElements();
385
386 android::Vector<sp</*const*/ Element> > mElements;
387 android::Vector<android::String8> mElementNames;
388 android::Vector<uint32_t> mArraySizes;
389 android::Vector<uint32_t> mVisibleElementMap;
390 android::Vector<uint32_t> mOffsetInBytes;
391
392 RsDataType mType;
393 RsDataKind mKind;
394 bool mNormalized;
395 size_t mSizeBytes;
396 size_t mVectorSize;
397};
398
Stephen Hines2c7206e2012-11-14 19:47:01 -0800399class FieldPacker {
400protected:
401 unsigned char* mData;
402 size_t mPos;
403 size_t mLen;
404
405public:
406 FieldPacker(size_t len)
407 : mPos(0),
408 mLen(len) {
409 mData = new unsigned char[len];
410 }
411
412 virtual ~FieldPacker() {
413 delete [] mData;
414 }
415
416 void align(size_t v) {
417 if ((v & (v - 1)) != 0) {
418 ALOGE("Non-power-of-two alignment: %zu", v);
419 return;
420 }
421
422 while ((mPos & (v - 1)) != 0) {
423 mData[mPos++] = 0;
424 }
425 }
426
427 void reset() {
428 mPos = 0;
429 }
430
431 void reset(size_t i) {
432 if (i >= mLen) {
433 ALOGE("Out of bounds: i (%zu) >= len (%zu)", i, mLen);
434 return;
435 }
436 mPos = i;
437 }
438
439 void skip(size_t i) {
440 size_t res = mPos + i;
441 if (res > mLen) {
442 ALOGE("Exceeded buffer length: i (%zu) > len (%zu)", i, mLen);
443 return;
444 }
445 mPos = res;
446 }
447
448 void* getData() const {
449 return mData;
450 }
451
452 size_t getLength() const {
453 return mLen;
454 }
455
456 template <typename T>
457 void add(T t) {
458 align(sizeof(t));
459 if (mPos + sizeof(t) <= mLen) {
460 memcpy(&mData[mPos], &t, sizeof(t));
461 mPos += sizeof(t);
462 }
463 }
464};
465
Tim Murray84bf2b82012-10-31 16:03:16 -0700466class Type : public BaseObj {
467protected:
468 friend class Allocation;
469
470 uint32_t mDimX;
471 uint32_t mDimY;
472 uint32_t mDimZ;
473 bool mDimMipmaps;
474 bool mDimFaces;
475 size_t mElementCount;
476 sp<const Element> mElement;
477
478 void calcElementCount();
479 virtual void updateFromNative();
480
481public:
482
483 sp<const Element> getElement() const {
484 return mElement;
485 }
486
487 uint32_t getX() const {
488 return mDimX;
489 }
490
491 uint32_t getY() const {
492 return mDimY;
493 }
494
495 uint32_t getZ() const {
496 return mDimZ;
497 }
498
499 bool hasMipmaps() const {
500 return mDimMipmaps;
501 }
502
503 bool hasFaces() const {
504 return mDimFaces;
505 }
506
507 size_t getCount() const {
508 return mElementCount;
509 }
510
511 size_t getSizeBytes() const {
512 return mElementCount * mElement->getSizeBytes();
513 }
514
515 Type(void *id, sp<RS> rs);
516
517
518 class Builder {
519 protected:
520 sp<RS> mRS;
521 uint32_t mDimX;
522 uint32_t mDimY;
523 uint32_t mDimZ;
524 bool mDimMipmaps;
525 bool mDimFaces;
526 sp<const Element> mElement;
527
528 public:
529 Builder(sp<RS> rs, sp<const Element> e);
530
531 void setX(uint32_t value);
532 void setY(int value);
533 void setMipmaps(bool value);
534 void setFaces(bool value);
535 sp<const Type> create();
536 };
537
538};
539
540class Script : public BaseObj {
541private:
542
543protected:
544 Script(void *id, sp<RS> rs);
545 void forEach(uint32_t slot, sp<const Allocation> in, sp<const Allocation> out,
546 const void *v, size_t) const;
547 void bindAllocation(sp<Allocation> va, uint32_t slot) const;
548 void setVar(uint32_t index, const void *, size_t len) const;
549 void setVar(uint32_t index, sp<const BaseObj> o) const;
550 void invoke(uint32_t slot, const void *v, size_t len) const;
551
552
553 void invoke(uint32_t slot) const {
554 invoke(slot, NULL, 0);
555 }
556 void setVar(uint32_t index, float v) const {
557 setVar(index, &v, sizeof(v));
558 }
559 void setVar(uint32_t index, double v) const {
560 setVar(index, &v, sizeof(v));
561 }
562 void setVar(uint32_t index, int32_t v) const {
563 setVar(index, &v, sizeof(v));
564 }
565 void setVar(uint32_t index, int64_t v) const {
566 setVar(index, &v, sizeof(v));
567 }
568 void setVar(uint32_t index, bool v) const {
569 setVar(index, &v, sizeof(v));
570 }
571
572public:
573 class FieldBase {
574 protected:
575 sp<const Element> mElement;
576 sp<Allocation> mAllocation;
577
578 void init(sp<RS> rs, uint32_t dimx, uint32_t usages = 0);
579
580 public:
581 sp<const Element> getElement() {
582 return mElement;
583 }
584
585 sp<const Type> getType() {
586 return mAllocation->getType();
587 }
588
589 sp<const Allocation> getAllocation() {
590 return mAllocation;
591 }
592
593 //void updateAllocation();
594 };
595};
596
597class ScriptC : public Script {
598protected:
599 ScriptC(sp<RS> rs,
600 const void *codeTxt, size_t codeLength,
601 const char *cachedName, size_t cachedNameLength,
602 const char *cacheDir, size_t cacheDirLength);
603
604};
605
Tim Murray7f0d5682012-11-08 16:35:24 -0800606class ScriptIntrinsic : public Script {
607 protected:
Tim Murray3cd44af2012-11-14 11:25:27 -0800608 ScriptIntrinsic(sp<RS> rs, int id, sp<const Element> e);
Tim Murray7f0d5682012-11-08 16:35:24 -0800609};
610
611class ScriptIntrinsicBlend : public ScriptIntrinsic {
612 public:
Tim Murray3cd44af2012-11-14 11:25:27 -0800613 ScriptIntrinsicBlend(sp<RS> rs, sp <const Element> e);
Tim Murray7f0d5682012-11-08 16:35:24 -0800614 void blendClear(sp<Allocation> in, sp<Allocation> out);
615 void blendSrc(sp<Allocation> in, sp<Allocation> out);
616 void blendDst(sp<Allocation> in, sp<Allocation> out);
617 void blendSrcOver(sp<Allocation> in, sp<Allocation> out);
618 void blendDstOver(sp<Allocation> in, sp<Allocation> out);
619 void blendSrcIn(sp<Allocation> in, sp<Allocation> out);
620 void blendDstIn(sp<Allocation> in, sp<Allocation> out);
621 void blendSrcOut(sp<Allocation> in, sp<Allocation> out);
622 void blendDstOut(sp<Allocation> in, sp<Allocation> out);
623 void blendSrcAtop(sp<Allocation> in, sp<Allocation> out);
624 void blendDstAtop(sp<Allocation> in, sp<Allocation> out);
625 void blendXor(sp<Allocation> in, sp<Allocation> out);
626 void blendMultiply(sp<Allocation> in, sp<Allocation> out);
627 void blendAdd(sp<Allocation> in, sp<Allocation> out);
628 void blendSubtract(sp<Allocation> in, sp<Allocation> out);
629};
Tim Murray84bf2b82012-10-31 16:03:16 -0700630
Tim Murray8f1e60c2012-11-13 12:25:11 -0800631class ScriptIntrinsicBlur : public ScriptIntrinsic {
632 public:
Tim Murray3cd44af2012-11-14 11:25:27 -0800633 ScriptIntrinsicBlur(sp<RS> rs, sp <const Element> e);
Tim Murray8f1e60c2012-11-13 12:25:11 -0800634 void blur(sp<Allocation> in, sp<Allocation> out);
635 void setRadius(float radius);
636};
637
Tim Murray84bf2b82012-10-31 16:03:16 -0700638}
Tim Murray7f0d5682012-11-08 16:35:24 -0800639
Tim Murray84bf2b82012-10-31 16:03:16 -0700640}
641
642#endif