blob: f6cb1dd8a1728b1d50863136b2425a5d91184fb7 [file] [log] [blame]
Tim Murray84bf2b82012-10-31 16:03:16 -07001/*
Tim Murray89daad62013-07-29 14:30:02 -07002 * Copyright (C) 2013 The Android Open Source Project
Tim Murray84bf2b82012-10-31 16:03:16 -07003 *
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
Tim Murray89daad62013-07-29 14:30:02 -070020#include "rsDefines.h"
Jason Samsf9e077a2013-03-20 19:04:19 -070021#include "rsCppUtils.h"
Tim Murray89daad62013-07-29 14:30:02 -070022#include "util/RefBase.h"
Tim Murraya4230962013-07-17 16:50:10 -070023#include "rsDispatch.h"
24
Tim Murray89daad62013-07-29 14:30:02 -070025#include <vector>
Tim Murrayab716362013-08-12 12:37:18 -070026#include <string>
Tim Murray89daad62013-07-29 14:30:02 -070027
Tim Murray96267c22013-02-12 11:25:12 -080028// Every row in an RS allocation is guaranteed to be aligned by this amount
29// Every row in a user-backed allocation must be aligned by this amount
30#define RS_CPU_ALLOCATION_ALIGNMENT 16
31
Tim Murray84bf2b82012-10-31 16:03:16 -070032namespace android {
Tim Murray9eb7f4b2012-11-16 14:02:18 -080033namespace RSC {
Tim Murray84bf2b82012-10-31 16:03:16 -070034
35typedef void (*ErrorHandlerFunc_t)(uint32_t errorNum, const char *errorText);
36typedef void (*MessageHandlerFunc_t)(uint32_t msgNum, const void *msgData, size_t msgLen);
37
38class RS;
39class BaseObj;
40class Element;
41class Type;
42class Allocation;
43class Script;
44class ScriptC;
Tim Murray729b6fe2013-07-23 16:20:42 -070045class Sampler;
Tim Murray84bf2b82012-10-31 16:03:16 -070046
Tim Murray21fa7a02013-08-15 16:25:03 -070047 enum RSError {
48 RS_SUCCESS = 0,
49 RS_ERROR_INVALID_PARAMETER = 1,
50 RS_ERROR_RUNTIME_ERROR = 2,
51 RS_ERROR_MAX = 9999
52
53 };
54
Tim Murray89daad62013-07-29 14:30:02 -070055 class RS : public android::RSC::LightRefBase<RS> {
Tim Murray84bf2b82012-10-31 16:03:16 -070056
57 public:
58 RS();
59 virtual ~RS();
60
Tim Murray4d252d62012-11-29 14:37:59 -080061 bool init(bool forceCpu = false, bool synchronous = false);
Tim Murray84bf2b82012-10-31 16:03:16 -070062
63 void setErrorHandler(ErrorHandlerFunc_t func);
64 ErrorHandlerFunc_t getErrorHandler() { return mErrorFunc; }
65
66 void setMessageHandler(MessageHandlerFunc_t func);
67 MessageHandlerFunc_t getMessageHandler() { return mMessageFunc; }
68
Tim Murray21fa7a02013-08-15 16:25:03 -070069 void throwError(RSError error, const char *errMsg);
Tim Murray84bf2b82012-10-31 16:03:16 -070070
71 RsContext getContext() { return mContext; }
72
Tim Murraybaca6c32012-11-14 16:51:46 -080073 void finish();
74
Tim Murraya4230962013-07-17 16:50:10 -070075 static dispatchTable* dispatch;
76
Tim Murray84bf2b82012-10-31 16:03:16 -070077 private:
Tim Murray4a92d122013-07-22 10:56:18 -070078 static bool usingNative;
Tim Murraya4230962013-07-17 16:50:10 -070079 static bool initDispatch(int targetApi);
80
Tim Murray4d252d62012-11-29 14:37:59 -080081 bool init(int targetApi, bool forceCpu, bool synchronous);
Tim Murray84bf2b82012-10-31 16:03:16 -070082 static void * threadProc(void *);
83
84 static bool gInitialized;
85 static pthread_mutex_t gInitMutex;
86
87 pthread_t mMessageThreadId;
88 pid_t mNativeMessageThreadId;
89 bool mMessageRun;
90
91 RsDevice mDev;
92 RsContext mContext;
Tim Murray21fa7a02013-08-15 16:25:03 -070093 RSError mCurrentError;
Tim Murray84bf2b82012-10-31 16:03:16 -070094
95 ErrorHandlerFunc_t mErrorFunc;
96 MessageHandlerFunc_t mMessageFunc;
Tim Murraya4230962013-07-17 16:50:10 -070097 bool mInit;
Tim Murray84bf2b82012-10-31 16:03:16 -070098
99 struct {
Tim Murray89daad62013-07-29 14:30:02 -0700100 sp<const Element> U8;
101 sp<const Element> I8;
102 sp<const Element> U16;
103 sp<const Element> I16;
104 sp<const Element> U32;
105 sp<const Element> I32;
106 sp<const Element> U64;
107 sp<const Element> I64;
108 sp<const Element> F32;
109 sp<const Element> F64;
110 sp<const Element> BOOLEAN;
Tim Murray84bf2b82012-10-31 16:03:16 -0700111
Tim Murray89daad62013-07-29 14:30:02 -0700112 sp<const Element> ELEMENT;
113 sp<const Element> TYPE;
114 sp<const Element> ALLOCATION;
115 sp<const Element> SAMPLER;
116 sp<const Element> SCRIPT;
117 sp<const Element> MESH;
118 sp<const Element> PROGRAM_FRAGMENT;
119 sp<const Element> PROGRAM_VERTEX;
120 sp<const Element> PROGRAM_RASTER;
121 sp<const Element> PROGRAM_STORE;
Tim Murray84bf2b82012-10-31 16:03:16 -0700122
Tim Murray89daad62013-07-29 14:30:02 -0700123 sp<const Element> A_8;
124 sp<const Element> RGB_565;
125 sp<const Element> RGB_888;
126 sp<const Element> RGBA_5551;
127 sp<const Element> RGBA_4444;
128 sp<const Element> RGBA_8888;
Tim Murray84bf2b82012-10-31 16:03:16 -0700129
Tim Murray89daad62013-07-29 14:30:02 -0700130 sp<const Element> FLOAT_2;
131 sp<const Element> FLOAT_3;
132 sp<const Element> FLOAT_4;
Tim Murray84bf2b82012-10-31 16:03:16 -0700133
Tim Murray89daad62013-07-29 14:30:02 -0700134 sp<const Element> DOUBLE_2;
135 sp<const Element> DOUBLE_3;
136 sp<const Element> DOUBLE_4;
Tim Murray84bf2b82012-10-31 16:03:16 -0700137
Tim Murray89daad62013-07-29 14:30:02 -0700138 sp<const Element> UCHAR_2;
139 sp<const Element> UCHAR_3;
140 sp<const Element> UCHAR_4;
Tim Murray84bf2b82012-10-31 16:03:16 -0700141
Tim Murray89daad62013-07-29 14:30:02 -0700142 sp<const Element> CHAR_2;
143 sp<const Element> CHAR_3;
144 sp<const Element> CHAR_4;
Tim Murray84bf2b82012-10-31 16:03:16 -0700145
Tim Murray89daad62013-07-29 14:30:02 -0700146 sp<const Element> USHORT_2;
147 sp<const Element> USHORT_3;
148 sp<const Element> USHORT_4;
Tim Murray84bf2b82012-10-31 16:03:16 -0700149
Tim Murray89daad62013-07-29 14:30:02 -0700150 sp<const Element> SHORT_2;
151 sp<const Element> SHORT_3;
152 sp<const Element> SHORT_4;
Tim Murray84bf2b82012-10-31 16:03:16 -0700153
Tim Murray89daad62013-07-29 14:30:02 -0700154 sp<const Element> UINT_2;
155 sp<const Element> UINT_3;
156 sp<const Element> UINT_4;
Tim Murray84bf2b82012-10-31 16:03:16 -0700157
Tim Murray89daad62013-07-29 14:30:02 -0700158 sp<const Element> INT_2;
159 sp<const Element> INT_3;
160 sp<const Element> INT_4;
Tim Murray84bf2b82012-10-31 16:03:16 -0700161
Tim Murray89daad62013-07-29 14:30:02 -0700162 sp<const Element> ULONG_2;
163 sp<const Element> ULONG_3;
164 sp<const Element> ULONG_4;
Tim Murray84bf2b82012-10-31 16:03:16 -0700165
Tim Murray89daad62013-07-29 14:30:02 -0700166 sp<const Element> LONG_2;
167 sp<const Element> LONG_3;
168 sp<const Element> LONG_4;
Tim Murray84bf2b82012-10-31 16:03:16 -0700169
Tim Murray89daad62013-07-29 14:30:02 -0700170 sp<const Element> MATRIX_4X4;
171 sp<const Element> MATRIX_3X3;
172 sp<const Element> MATRIX_2X2;
Tim Murray84bf2b82012-10-31 16:03:16 -0700173 } mElements;
174
Tim Murray729b6fe2013-07-23 16:20:42 -0700175 struct {
Tim Murray89daad62013-07-29 14:30:02 -0700176 sp<const Sampler> CLAMP_NEAREST;
177 sp<const Sampler> CLAMP_LINEAR;
178 sp<const Sampler> CLAMP_LINEAR_MIP_LINEAR;
179 sp<const Sampler> WRAP_NEAREST;
180 sp<const Sampler> WRAP_LINEAR;
181 sp<const Sampler> WRAP_LINEAR_MIP_LINEAR;
182 sp<const Sampler> MIRRORED_REPEAT_NEAREST;
183 sp<const Sampler> MIRRORED_REPEAT_LINEAR;
184 sp<const Sampler> MIRRORED_REPEAT_LINEAR_MIP_LINEAR;
Tim Murray729b6fe2013-07-23 16:20:42 -0700185 } mSamplers;
186 friend class Sampler;
187 friend class Element;
Tim Murray84bf2b82012-10-31 16:03:16 -0700188};
189
Tim Murray89daad62013-07-29 14:30:02 -0700190class BaseObj : public android::RSC::LightRefBase<BaseObj> {
191public:
192 void * getID() const;
193 virtual ~BaseObj();
194 virtual void updateFromNative();
195 virtual bool equals(sp<const BaseObj> obj);
196
Tim Murray84bf2b82012-10-31 16:03:16 -0700197protected:
198 void *mID;
199 sp<RS> mRS;
Tim Murrayab716362013-08-12 12:37:18 -0700200 std::string mName;
Tim Murray84bf2b82012-10-31 16:03:16 -0700201
202 BaseObj(void *id, sp<RS> rs);
203 void checkValid();
204
205 static void * getObjID(sp<const BaseObj> o);
206
Tim Murray84bf2b82012-10-31 16:03:16 -0700207};
208
209
210class Allocation : public BaseObj {
211protected:
Tim Murray89daad62013-07-29 14:30:02 -0700212 sp<const Type> mType;
Tim Murray84bf2b82012-10-31 16:03:16 -0700213 uint32_t mUsage;
Tim Murray89daad62013-07-29 14:30:02 -0700214 sp<Allocation> mAdaptedAllocation;
Tim Murray84bf2b82012-10-31 16:03:16 -0700215
216 bool mConstrainedLOD;
217 bool mConstrainedFace;
218 bool mConstrainedY;
219 bool mConstrainedZ;
220 bool mReadAllowed;
221 bool mWriteAllowed;
222 uint32_t mSelectedY;
223 uint32_t mSelectedZ;
224 uint32_t mSelectedLOD;
225 RsAllocationCubemapFace mSelectedFace;
226
227 uint32_t mCurrentDimX;
228 uint32_t mCurrentDimY;
229 uint32_t mCurrentDimZ;
230 uint32_t mCurrentCount;
231
232 void * getIDSafe() const;
233 void updateCacheInfo(sp<const Type> t);
234
235 Allocation(void *id, sp<RS> rs, sp<const Type> t, uint32_t usage);
236
237 void validateIsInt32();
238 void validateIsInt16();
239 void validateIsInt8();
240 void validateIsFloat32();
241 void validateIsObject();
242
243 virtual void updateFromNative();
244
245 void validate2DRange(uint32_t xoff, uint32_t yoff, uint32_t w, uint32_t h);
246
247public:
Tim Murray89daad62013-07-29 14:30:02 -0700248 sp<const Type> getType() {
Tim Murray84bf2b82012-10-31 16:03:16 -0700249 return mType;
250 }
251
252 void syncAll(RsAllocationUsageType srcLocation);
253 void ioSendOutput();
254 void ioGetInput();
255
256 void generateMipmaps();
Tim Murray509ea5c2012-11-13 11:56:40 -0800257
Tim Murray0b93e302012-11-15 14:56:54 -0800258 void copy1DRangeFrom(uint32_t off, size_t count, const void *data);
Tim Murraya4cbc2b2012-11-14 17:18:08 -0800259 void copy1DRangeFrom(uint32_t off, size_t count, sp<const Allocation> data, uint32_t dataOff);
Tim Murray84bf2b82012-10-31 16:03:16 -0700260
Tim Murray0b93e302012-11-15 14:56:54 -0800261 void copy1DRangeTo(uint32_t off, size_t count, void *data);
262
263 void copy1DFrom(const void* data);
264 void copy1DTo(void* data);
Tim Murraya4cbc2b2012-11-14 17:18:08 -0800265
Tim Murray84bf2b82012-10-31 16:03:16 -0700266 void copy2DRangeFrom(uint32_t xoff, uint32_t yoff, uint32_t w, uint32_t h,
Tim Murray0b93e302012-11-15 14:56:54 -0800267 const void *data);
Tim Murray7b3e3092012-11-16 13:32:24 -0800268
269 void copy2DRangeTo(uint32_t xoff, uint32_t yoff, uint32_t w, uint32_t h,
270 void *data);
271
Tim Murray0b93e302012-11-15 14:56:54 -0800272 void copy2DRangeFrom(uint32_t xoff, uint32_t yoff, uint32_t w, uint32_t h,
273 sp<const Allocation> data, uint32_t dataXoff, uint32_t dataYoff);
Tim Murray84bf2b82012-10-31 16:03:16 -0700274
Tim Murray358747a2012-11-26 13:52:04 -0800275 void copy2DStridedFrom(uint32_t xoff, uint32_t yoff, uint32_t w, uint32_t h,
276 const void *data, size_t stride);
277 void copy2DStridedFrom(const void *data, size_t stride);
278
279 void copy2DStridedTo(uint32_t xoff, uint32_t yoff, uint32_t w, uint32_t h,
280 void *data, size_t stride);
281 void copy2DStridedTo(void *data, size_t stride);
282
Tim Murray84bf2b82012-10-31 16:03:16 -0700283 void resize(int dimX);
284 void resize(int dimX, int dimY);
285
286 static sp<Allocation> createTyped(sp<RS> rs, sp<const Type> type,
287 RsAllocationMipmapControl mips, uint32_t usage);
288 static sp<Allocation> createTyped(sp<RS> rs, sp<const Type> type,
289 RsAllocationMipmapControl mips, uint32_t usage, void * pointer);
290
291 static sp<Allocation> createTyped(sp<RS> rs, sp<const Type> type,
292 uint32_t usage = RS_ALLOCATION_USAGE_SCRIPT);
293 static sp<Allocation> createSized(sp<RS> rs, sp<const Element> e, size_t count,
294 uint32_t usage = RS_ALLOCATION_USAGE_SCRIPT);
Tim Murray684726c2012-11-14 11:57:42 -0800295 static sp<Allocation> createSized2D(sp<RS> rs, sp<const Element> e,
296 size_t x, size_t y,
297 uint32_t usage = RS_ALLOCATION_USAGE_SCRIPT);
298
Tim Murray84bf2b82012-10-31 16:03:16 -0700299
300};
301
302class Element : public BaseObj {
303public:
304 bool isComplex();
305 size_t getSubElementCount() {
306 return mVisibleElementMap.size();
307 }
308
309 sp<const Element> getSubElement(uint32_t index);
310 const char * getSubElementName(uint32_t index);
311 size_t getSubElementArraySize(uint32_t index);
312 uint32_t getSubElementOffsetBytes(uint32_t index);
313 RsDataType getDataType() const {
314 return mType;
315 }
316
317 RsDataKind getDataKind() const {
318 return mKind;
319 }
320
321 size_t getSizeBytes() const {
322 return mSizeBytes;
323 }
324
325 static sp<const Element> BOOLEAN(sp<RS> rs);
326 static sp<const Element> U8(sp<RS> rs);
327 static sp<const Element> I8(sp<RS> rs);
328 static sp<const Element> U16(sp<RS> rs);
329 static sp<const Element> I16(sp<RS> rs);
330 static sp<const Element> U32(sp<RS> rs);
331 static sp<const Element> I32(sp<RS> rs);
332 static sp<const Element> U64(sp<RS> rs);
333 static sp<const Element> I64(sp<RS> rs);
334 static sp<const Element> F32(sp<RS> rs);
335 static sp<const Element> F64(sp<RS> rs);
336 static sp<const Element> ELEMENT(sp<RS> rs);
337 static sp<const Element> TYPE(sp<RS> rs);
338 static sp<const Element> ALLOCATION(sp<RS> rs);
339 static sp<const Element> SAMPLER(sp<RS> rs);
340 static sp<const Element> SCRIPT(sp<RS> rs);
341 static sp<const Element> MESH(sp<RS> rs);
342 static sp<const Element> PROGRAM_FRAGMENT(sp<RS> rs);
343 static sp<const Element> PROGRAM_VERTEX(sp<RS> rs);
344 static sp<const Element> PROGRAM_RASTER(sp<RS> rs);
345 static sp<const Element> PROGRAM_STORE(sp<RS> rs);
346
347 static sp<const Element> A_8(sp<RS> rs);
348 static sp<const Element> RGB_565(sp<RS> rs);
349 static sp<const Element> RGB_888(sp<RS> rs);
350 static sp<const Element> RGBA_5551(sp<RS> rs);
351 static sp<const Element> RGBA_4444(sp<RS> rs);
352 static sp<const Element> RGBA_8888(sp<RS> rs);
353
354 static sp<const Element> F32_2(sp<RS> rs);
355 static sp<const Element> F32_3(sp<RS> rs);
356 static sp<const Element> F32_4(sp<RS> rs);
357 static sp<const Element> F64_2(sp<RS> rs);
358 static sp<const Element> F64_3(sp<RS> rs);
359 static sp<const Element> F64_4(sp<RS> rs);
360 static sp<const Element> U8_2(sp<RS> rs);
361 static sp<const Element> U8_3(sp<RS> rs);
362 static sp<const Element> U8_4(sp<RS> rs);
363 static sp<const Element> I8_2(sp<RS> rs);
364 static sp<const Element> I8_3(sp<RS> rs);
365 static sp<const Element> I8_4(sp<RS> rs);
366 static sp<const Element> U16_2(sp<RS> rs);
367 static sp<const Element> U16_3(sp<RS> rs);
368 static sp<const Element> U16_4(sp<RS> rs);
369 static sp<const Element> I16_2(sp<RS> rs);
370 static sp<const Element> I16_3(sp<RS> rs);
371 static sp<const Element> I16_4(sp<RS> rs);
372 static sp<const Element> U32_2(sp<RS> rs);
373 static sp<const Element> U32_3(sp<RS> rs);
374 static sp<const Element> U32_4(sp<RS> rs);
375 static sp<const Element> I32_2(sp<RS> rs);
376 static sp<const Element> I32_3(sp<RS> rs);
377 static sp<const Element> I32_4(sp<RS> rs);
378 static sp<const Element> U64_2(sp<RS> rs);
379 static sp<const Element> U64_3(sp<RS> rs);
380 static sp<const Element> U64_4(sp<RS> rs);
381 static sp<const Element> I64_2(sp<RS> rs);
382 static sp<const Element> I64_3(sp<RS> rs);
383 static sp<const Element> I64_4(sp<RS> rs);
384 static sp<const Element> MATRIX_4X4(sp<RS> rs);
385 static sp<const Element> MATRIX_3X3(sp<RS> rs);
386 static sp<const Element> MATRIX_2X2(sp<RS> rs);
387
Tim Murray84bf2b82012-10-31 16:03:16 -0700388 void updateFromNative();
389 static sp<const Element> createUser(sp<RS> rs, RsDataType dt);
390 static sp<const Element> createVector(sp<RS> rs, RsDataType dt, uint32_t size);
391 static sp<const Element> createPixel(sp<RS> rs, RsDataType dt, RsDataKind dk);
392 bool isCompatible(sp<const Element>e);
393
Tim Murray89daad62013-07-29 14:30:02 -0700394 Element(void *id, sp<RS> rs,
395 std::vector<sp<Element> > &elements,
Tim Murrayab716362013-08-12 12:37:18 -0700396 std::vector<std::string> &elementNames,
Tim Murray89daad62013-07-29 14:30:02 -0700397 std::vector<uint32_t> &arraySizes);
398 Element(void *id, sp<RS> rs, RsDataType dt, RsDataKind dk, bool norm, uint32_t size);
399 Element(sp<RS> rs);
400 virtual ~Element();
401
402 protected:
Tim Murray84bf2b82012-10-31 16:03:16 -0700403 class Builder {
404 private:
405 sp<RS> mRS;
Tim Murray89daad62013-07-29 14:30:02 -0700406 std::vector<sp<Element> > mElements;
Tim Murrayab716362013-08-12 12:37:18 -0700407 std::vector<std::string> mElementNames;
Tim Murray89daad62013-07-29 14:30:02 -0700408 std::vector<uint32_t> mArraySizes;
Tim Murray84bf2b82012-10-31 16:03:16 -0700409 bool mSkipPadding;
410
411 public:
412 Builder(sp<RS> rs);
413 ~Builder();
Tim Murrayab716362013-08-12 12:37:18 -0700414 void add(sp<Element> e, std::string &name, uint32_t arraySize = 1);
Tim Murray84bf2b82012-10-31 16:03:16 -0700415 sp<const Element> create();
416 };
417
418private:
419 void updateVisibleSubElements();
420
Tim Murray89daad62013-07-29 14:30:02 -0700421 std::vector<sp<Element> > mElements;
Tim Murrayab716362013-08-12 12:37:18 -0700422 std::vector<std::string> mElementNames;
Tim Murray89daad62013-07-29 14:30:02 -0700423 std::vector<uint32_t> mArraySizes;
424 std::vector<uint32_t> mVisibleElementMap;
425 std::vector<uint32_t> mOffsetInBytes;
Tim Murray84bf2b82012-10-31 16:03:16 -0700426
427 RsDataType mType;
428 RsDataKind mKind;
429 bool mNormalized;
430 size_t mSizeBytes;
431 size_t mVectorSize;
432};
433
Stephen Hines2c7206e2012-11-14 19:47:01 -0800434class FieldPacker {
435protected:
436 unsigned char* mData;
437 size_t mPos;
438 size_t mLen;
439
440public:
441 FieldPacker(size_t len)
Tim Murray89daad62013-07-29 14:30:02 -0700442 : mPos(0), mLen(len) {
443 mData = new unsigned char[len];
444 }
Stephen Hines2c7206e2012-11-14 19:47:01 -0800445
446 virtual ~FieldPacker() {
447 delete [] mData;
448 }
449
450 void align(size_t v) {
451 if ((v & (v - 1)) != 0) {
Tim Murrayab716362013-08-12 12:37:18 -0700452 // ALOGE("Non-power-of-two alignment: %zu", v);
Stephen Hines2c7206e2012-11-14 19:47:01 -0800453 return;
454 }
455
456 while ((mPos & (v - 1)) != 0) {
457 mData[mPos++] = 0;
458 }
459 }
460
461 void reset() {
462 mPos = 0;
463 }
464
465 void reset(size_t i) {
466 if (i >= mLen) {
Tim Murrayab716362013-08-12 12:37:18 -0700467 // ALOGE("Out of bounds: i (%zu) >= len (%zu)", i, mLen);
Stephen Hines2c7206e2012-11-14 19:47:01 -0800468 return;
469 }
470 mPos = i;
471 }
472
473 void skip(size_t i) {
474 size_t res = mPos + i;
475 if (res > mLen) {
Tim Murrayab716362013-08-12 12:37:18 -0700476 // ALOGE("Exceeded buffer length: i (%zu) > len (%zu)", i, mLen);
Stephen Hines2c7206e2012-11-14 19:47:01 -0800477 return;
478 }
479 mPos = res;
480 }
481
482 void* getData() const {
483 return mData;
484 }
485
486 size_t getLength() const {
487 return mLen;
488 }
489
490 template <typename T>
Tim Murray89daad62013-07-29 14:30:02 -0700491 void add(T t) {
Stephen Hines2c7206e2012-11-14 19:47:01 -0800492 align(sizeof(t));
493 if (mPos + sizeof(t) <= mLen) {
494 memcpy(&mData[mPos], &t, sizeof(t));
495 mPos += sizeof(t);
496 }
497 }
Stephen Hines43514cd2012-11-16 14:33:47 -0800498
499 /*
Tim Murray89daad62013-07-29 14:30:02 -0700500 void add(rs_matrix4x4 m) {
501 for (size_t i = 0; i < 16; i++) {
502 add(m.m[i]);
503 }
504 }
Stephen Hines43514cd2012-11-16 14:33:47 -0800505
Tim Murray89daad62013-07-29 14:30:02 -0700506 void add(rs_matrix3x3 m) {
507 for (size_t i = 0; i < 9; i++) {
508 add(m.m[i]);
509 }
510 }
Stephen Hines43514cd2012-11-16 14:33:47 -0800511
Tim Murray89daad62013-07-29 14:30:02 -0700512 void add(rs_matrix2x2 m) {
513 for (size_t i = 0; i < 4; i++) {
514 add(m.m[i]);
515 }
516 }
Stephen Hines43514cd2012-11-16 14:33:47 -0800517 */
518
Tim Murray89daad62013-07-29 14:30:02 -0700519 void add(sp<BaseObj> obj) {
Stephen Hines43514cd2012-11-16 14:33:47 -0800520 if (obj != NULL) {
521 add((uint32_t) (uintptr_t) obj->getID());
522 } else {
523 add((uint32_t) 0);
524 }
525 }
Stephen Hines2c7206e2012-11-14 19:47:01 -0800526};
527
Tim Murray89daad62013-07-29 14:30:02 -0700528
Tim Murray84bf2b82012-10-31 16:03:16 -0700529class Type : public BaseObj {
530protected:
531 friend class Allocation;
532
533 uint32_t mDimX;
534 uint32_t mDimY;
535 uint32_t mDimZ;
536 bool mDimMipmaps;
537 bool mDimFaces;
538 size_t mElementCount;
539 sp<const Element> mElement;
540
541 void calcElementCount();
542 virtual void updateFromNative();
543
544public:
545
546 sp<const Element> getElement() const {
547 return mElement;
548 }
549
550 uint32_t getX() const {
551 return mDimX;
552 }
553
554 uint32_t getY() const {
555 return mDimY;
556 }
557
558 uint32_t getZ() const {
559 return mDimZ;
560 }
561
562 bool hasMipmaps() const {
563 return mDimMipmaps;
564 }
565
566 bool hasFaces() const {
567 return mDimFaces;
568 }
569
570 size_t getCount() const {
571 return mElementCount;
572 }
573
574 size_t getSizeBytes() const {
575 return mElementCount * mElement->getSizeBytes();
576 }
577
578 Type(void *id, sp<RS> rs);
579
Tim Murray96267c22013-02-12 11:25:12 -0800580 static sp<const Type> create(sp<RS> rs, sp<const Element> e, uint32_t dimX, uint32_t dimY, uint32_t dimZ);
Tim Murray84bf2b82012-10-31 16:03:16 -0700581
582 class Builder {
583 protected:
584 sp<RS> mRS;
585 uint32_t mDimX;
586 uint32_t mDimY;
587 uint32_t mDimZ;
588 bool mDimMipmaps;
589 bool mDimFaces;
590 sp<const Element> mElement;
591
592 public:
593 Builder(sp<RS> rs, sp<const Element> e);
594
595 void setX(uint32_t value);
596 void setY(int value);
597 void setMipmaps(bool value);
598 void setFaces(bool value);
599 sp<const Type> create();
600 };
601
602};
603
604class Script : public BaseObj {
605private:
606
607protected:
608 Script(void *id, sp<RS> rs);
609 void forEach(uint32_t slot, sp<const Allocation> in, sp<const Allocation> out,
610 const void *v, size_t) const;
611 void bindAllocation(sp<Allocation> va, uint32_t slot) const;
612 void setVar(uint32_t index, const void *, size_t len) const;
613 void setVar(uint32_t index, sp<const BaseObj> o) const;
614 void invoke(uint32_t slot, const void *v, size_t len) const;
615
616
617 void invoke(uint32_t slot) const {
618 invoke(slot, NULL, 0);
619 }
620 void setVar(uint32_t index, float v) const {
621 setVar(index, &v, sizeof(v));
622 }
623 void setVar(uint32_t index, double v) const {
624 setVar(index, &v, sizeof(v));
625 }
626 void setVar(uint32_t index, int32_t v) const {
627 setVar(index, &v, sizeof(v));
628 }
629 void setVar(uint32_t index, int64_t v) const {
630 setVar(index, &v, sizeof(v));
631 }
632 void setVar(uint32_t index, bool v) const {
633 setVar(index, &v, sizeof(v));
634 }
635
636public:
637 class FieldBase {
638 protected:
639 sp<const Element> mElement;
640 sp<Allocation> mAllocation;
641
642 void init(sp<RS> rs, uint32_t dimx, uint32_t usages = 0);
643
644 public:
645 sp<const Element> getElement() {
646 return mElement;
647 }
648
649 sp<const Type> getType() {
650 return mAllocation->getType();
651 }
652
653 sp<const Allocation> getAllocation() {
654 return mAllocation;
655 }
656
657 //void updateAllocation();
658 };
659};
660
661class ScriptC : public Script {
662protected:
663 ScriptC(sp<RS> rs,
664 const void *codeTxt, size_t codeLength,
665 const char *cachedName, size_t cachedNameLength,
666 const char *cacheDir, size_t cacheDirLength);
667
668};
669
Tim Murray7f0d5682012-11-08 16:35:24 -0800670class ScriptIntrinsic : public Script {
671 protected:
Tim Murray3cd44af2012-11-14 11:25:27 -0800672 ScriptIntrinsic(sp<RS> rs, int id, sp<const Element> e);
Tim Murrayb27b1812013-08-05 14:00:40 -0700673 virtual ~ScriptIntrinsic();
Tim Murray7f0d5682012-11-08 16:35:24 -0800674};
675
Tim Murray89daad62013-07-29 14:30:02 -0700676class ScriptIntrinsic3DLUT : public ScriptIntrinsic {
Tim Murray21fa7a02013-08-15 16:25:03 -0700677 private:
Tim Murray89daad62013-07-29 14:30:02 -0700678 ScriptIntrinsic3DLUT(sp<RS> rs, sp<const Element> e);
Tim Murray21fa7a02013-08-15 16:25:03 -0700679 public:
680 static sp<ScriptIntrinsic3DLUT> create(sp<RS> rs, sp<const Element> e);
Tim Murray89daad62013-07-29 14:30:02 -0700681 void forEach(sp<Allocation> ain, sp<Allocation> aout);
682 void setLUT(sp<Allocation> lut);
683};
684
Tim Murray7f0d5682012-11-08 16:35:24 -0800685class ScriptIntrinsicBlend : public ScriptIntrinsic {
Tim Murray21fa7a02013-08-15 16:25:03 -0700686 private:
Tim Murray89daad62013-07-29 14:30:02 -0700687 ScriptIntrinsicBlend(sp<RS> rs, sp<const Element> e);
Tim Murray21fa7a02013-08-15 16:25:03 -0700688 public:
689 static sp<ScriptIntrinsicBlend> create(sp<RS> rs, sp<const Element> e);
Tim Murray7f0d5682012-11-08 16:35:24 -0800690 void blendClear(sp<Allocation> in, sp<Allocation> out);
691 void blendSrc(sp<Allocation> in, sp<Allocation> out);
692 void blendDst(sp<Allocation> in, sp<Allocation> out);
693 void blendSrcOver(sp<Allocation> in, sp<Allocation> out);
694 void blendDstOver(sp<Allocation> in, sp<Allocation> out);
695 void blendSrcIn(sp<Allocation> in, sp<Allocation> out);
696 void blendDstIn(sp<Allocation> in, sp<Allocation> out);
697 void blendSrcOut(sp<Allocation> in, sp<Allocation> out);
698 void blendDstOut(sp<Allocation> in, sp<Allocation> out);
699 void blendSrcAtop(sp<Allocation> in, sp<Allocation> out);
700 void blendDstAtop(sp<Allocation> in, sp<Allocation> out);
701 void blendXor(sp<Allocation> in, sp<Allocation> out);
702 void blendMultiply(sp<Allocation> in, sp<Allocation> out);
703 void blendAdd(sp<Allocation> in, sp<Allocation> out);
704 void blendSubtract(sp<Allocation> in, sp<Allocation> out);
705};
Tim Murray84bf2b82012-10-31 16:03:16 -0700706
Tim Murray8f1e60c2012-11-13 12:25:11 -0800707class ScriptIntrinsicBlur : public ScriptIntrinsic {
Tim Murray21fa7a02013-08-15 16:25:03 -0700708 private:
Tim Murray89daad62013-07-29 14:30:02 -0700709 ScriptIntrinsicBlur(sp<RS> rs, sp<const Element> e);
Tim Murray21fa7a02013-08-15 16:25:03 -0700710 public:
711 static sp<ScriptIntrinsicBlur> create(sp<RS> rs, sp<const Element> e);
712 void setInput(sp<Allocation> in);
713 void forEach(sp<Allocation> out);
Tim Murray8f1e60c2012-11-13 12:25:11 -0800714 void setRadius(float radius);
715};
716
Tim Murray89daad62013-07-29 14:30:02 -0700717class ScriptIntrinsicColorMatrix : public ScriptIntrinsic {
Tim Murray21fa7a02013-08-15 16:25:03 -0700718 private:
Tim Murray89daad62013-07-29 14:30:02 -0700719 ScriptIntrinsicColorMatrix(sp<RS> rs, sp<const Element> e);
Tim Murray21fa7a02013-08-15 16:25:03 -0700720 public:
721 static sp<ScriptIntrinsicColorMatrix> create(sp<RS> rs, sp<const Element> e);
Tim Murray89daad62013-07-29 14:30:02 -0700722 void forEach(sp<Allocation> in, sp<Allocation> out);
723 void setColorMatrix3(float* m);
724 void setColorMatrix4(float* m);
725 void setGreyscale();
726 void setRGBtoYUV();
727 void setYUVtoRGB();
728};
729
730class ScriptIntrinsicConvolve3x3 : public ScriptIntrinsic {
Tim Murray21fa7a02013-08-15 16:25:03 -0700731 private:
Tim Murray89daad62013-07-29 14:30:02 -0700732 ScriptIntrinsicConvolve3x3(sp<RS> rs, sp<const Element> e);
Tim Murray21fa7a02013-08-15 16:25:03 -0700733 public:
734 static sp<ScriptIntrinsicConvolve3x3> create(sp<RS> rs, sp<const Element> e);
Tim Murray89daad62013-07-29 14:30:02 -0700735 void setInput(sp<Allocation> in);
736 void forEach(sp<Allocation> out);
737 void setCoefficients(float* v);
738};
739
740class ScriptIntrinsicConvolve5x5 : public ScriptIntrinsic {
Tim Murray21fa7a02013-08-15 16:25:03 -0700741 private:
Tim Murray89daad62013-07-29 14:30:02 -0700742 ScriptIntrinsicConvolve5x5(sp<RS> rs, sp<const Element> e);
Tim Murray21fa7a02013-08-15 16:25:03 -0700743 public:
744 static sp<ScriptIntrinsicConvolve5x5> create(sp<RS> rs, sp<const Element> e);
Tim Murray89daad62013-07-29 14:30:02 -0700745 void setInput(sp<Allocation> in);
746 void forEach(sp<Allocation> out);
747 void setCoefficients(float* v);
748};
749
Tim Murrayb27b1812013-08-05 14:00:40 -0700750class ScriptIntrinsicHistogram : public ScriptIntrinsic {
Tim Murray21fa7a02013-08-15 16:25:03 -0700751 private:
Tim Murrayb27b1812013-08-05 14:00:40 -0700752 ScriptIntrinsicHistogram(sp<RS> rs, sp<const Element> e);
Tim Murray21fa7a02013-08-15 16:25:03 -0700753 public:
754 static sp<ScriptIntrinsicHistogram> create(sp<RS> rs, sp<const Element> e);
Tim Murrayb27b1812013-08-05 14:00:40 -0700755 void setOutput(sp<Allocation> aout);
756 void setDotCoefficients(float r, float g, float b, float a);
757 void forEach(sp<Allocation> ain);
758 void forEach_dot(sp<Allocation> ain);
759};
760
761class ScriptIntrinsicLUT : public ScriptIntrinsic {
762 private:
763 sp<Allocation> LUT;
764 bool mDirty;
765 unsigned char mCache[1024];
766 void setTable(unsigned int offset, unsigned char base, unsigned char length, unsigned char* lutValues);
Tim Murray21fa7a02013-08-15 16:25:03 -0700767 ScriptIntrinsicLUT(sp<RS> rs, sp<const Element> e);
Tim Murrayb27b1812013-08-05 14:00:40 -0700768
Tim Murray89daad62013-07-29 14:30:02 -0700769 public:
Tim Murray21fa7a02013-08-15 16:25:03 -0700770 static sp<ScriptIntrinsicLUT> create(sp<RS> rs, sp<const Element> e);
Tim Murray89daad62013-07-29 14:30:02 -0700771 void forEach(sp<Allocation> ain, sp<Allocation> aout);
Tim Murrayb27b1812013-08-05 14:00:40 -0700772 void setRed(unsigned char base, unsigned char length, unsigned char* lutValues);
773 void setGreen(unsigned char base, unsigned char length, unsigned char* lutValues);
774 void setBlue(unsigned char base, unsigned char length, unsigned char* lutValues);
775 void setAlpha(unsigned char base, unsigned char length, unsigned char* lutValues);
776 virtual ~ScriptIntrinsicLUT();
777};
778
Tim Murray89daad62013-07-29 14:30:02 -0700779class ScriptIntrinsicYuvToRGB : public ScriptIntrinsic {
Tim Murray21fa7a02013-08-15 16:25:03 -0700780 private:
Tim Murrayb27b1812013-08-05 14:00:40 -0700781 ScriptIntrinsicYuvToRGB(sp<RS> rs, sp<const Element> e);
Tim Murray21fa7a02013-08-15 16:25:03 -0700782 public:
783 static sp<ScriptIntrinsicYuvToRGB> create(sp<RS> rs, sp<const Element> e);
Tim Murrayb27b1812013-08-05 14:00:40 -0700784 void setInput(sp<Allocation> in);
785 void forEach(sp<Allocation> out);
Tim Murray89daad62013-07-29 14:30:02 -0700786
787};
Tim Murrayb27b1812013-08-05 14:00:40 -0700788
Tim Murray89daad62013-07-29 14:30:02 -0700789
Tim Murray729b6fe2013-07-23 16:20:42 -0700790 class Sampler : public BaseObj {
791 private:
792 Sampler(sp<RS> rs, void* id);
793 RsSamplerValue mMin;
794 RsSamplerValue mMag;
795 RsSamplerValue mWrapS;
796 RsSamplerValue mWrapT;
797 RsSamplerValue mWrapR;
798 float mAniso;
799
800 public:
801 static sp<Sampler> create(sp<RS> rs, RsSamplerValue min, RsSamplerValue mag, RsSamplerValue wrapS, RsSamplerValue wrapT, float anisotropy);
802
803 RsSamplerValue getMinification();
804 RsSamplerValue getMagnification();
805 RsSamplerValue getWrapS();
806 RsSamplerValue getWrapT();
807 float getAnisotropy();
808
809 sp<const Sampler> CLAMP_NEAREST(sp<RS> rs);
810 sp<const Sampler> CLAMP_LINEAR(sp<RS> rs);
811 sp<const Sampler> CLAMP_LINEAR_MIP_LINEAR(sp<RS> rs);
812 sp<const Sampler> WRAP_NEAREST(sp<RS> rs);
813 sp<const Sampler> WRAP_LINEAR(sp<RS> rs);
814 sp<const Sampler> WRAP_LINEAR_MIP_LINEAR(sp<RS> rs);
815 sp<const Sampler> MIRRORED_REPEAT_NEAREST(sp<RS> rs);
816 sp<const Sampler> MIRRORED_REPEAT_LINEAR(sp<RS> rs);
817 sp<const Sampler> MIRRORED_REPEAT_LINEAR_MIP_LINEAR(sp<RS> rs);
818
819};
820
Tim Murray84bf2b82012-10-31 16:03:16 -0700821}
Tim Murray7f0d5682012-11-08 16:35:24 -0800822
Tim Murray84bf2b82012-10-31 16:03:16 -0700823}
824
825#endif