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