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