blob: 913fd91e8965bb9e7ca6ff080cefe772c8e81f9a [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,
Tim Murray10913a52013-08-20 17:19:47 -070051 RS_ERROR_INVALID_ELEMENT = 3,
Tim Murray21fa7a02013-08-15 16:25:03 -070052 RS_ERROR_MAX = 9999
53
54 };
55
Tim Murrayeb4426d2013-08-27 15:30:16 -070056 enum RSYuvFormat {
57 RS_YUV_NONE = 0,
58 RS_YUV_YV12 = 1,
59 RS_YUV_NV21 = 2,
60 RS_YUV_MAX = 3
61 };
62
Tim Murray89daad62013-07-29 14:30:02 -070063 class RS : public android::RSC::LightRefBase<RS> {
Tim Murray84bf2b82012-10-31 16:03:16 -070064
65 public:
66 RS();
67 virtual ~RS();
68
Tim Murray4d252d62012-11-29 14:37:59 -080069 bool init(bool forceCpu = false, bool synchronous = false);
Tim Murray84bf2b82012-10-31 16:03:16 -070070
71 void setErrorHandler(ErrorHandlerFunc_t func);
72 ErrorHandlerFunc_t getErrorHandler() { return mErrorFunc; }
73
74 void setMessageHandler(MessageHandlerFunc_t func);
75 MessageHandlerFunc_t getMessageHandler() { return mMessageFunc; }
76
Tim Murray21fa7a02013-08-15 16:25:03 -070077 void throwError(RSError error, const char *errMsg);
Tim Murray10913a52013-08-20 17:19:47 -070078 RSError getError();
Tim Murray84bf2b82012-10-31 16:03:16 -070079
80 RsContext getContext() { return mContext; }
81
Tim Murraybaca6c32012-11-14 16:51:46 -080082 void finish();
83
Tim Murraya4230962013-07-17 16:50:10 -070084 static dispatchTable* dispatch;
85
Tim Murray84bf2b82012-10-31 16:03:16 -070086 private:
Tim Murray4a92d122013-07-22 10:56:18 -070087 static bool usingNative;
Tim Murraya4230962013-07-17 16:50:10 -070088 static bool initDispatch(int targetApi);
89
Tim Murray4d252d62012-11-29 14:37:59 -080090 bool init(int targetApi, bool forceCpu, bool synchronous);
Tim Murray84bf2b82012-10-31 16:03:16 -070091 static void * threadProc(void *);
92
93 static bool gInitialized;
94 static pthread_mutex_t gInitMutex;
95
96 pthread_t mMessageThreadId;
97 pid_t mNativeMessageThreadId;
98 bool mMessageRun;
99
100 RsDevice mDev;
101 RsContext mContext;
Tim Murray21fa7a02013-08-15 16:25:03 -0700102 RSError mCurrentError;
Tim Murray84bf2b82012-10-31 16:03:16 -0700103
104 ErrorHandlerFunc_t mErrorFunc;
105 MessageHandlerFunc_t mMessageFunc;
Tim Murraya4230962013-07-17 16:50:10 -0700106 bool mInit;
Tim Murray84bf2b82012-10-31 16:03:16 -0700107
108 struct {
Tim Murray89daad62013-07-29 14:30:02 -0700109 sp<const Element> U8;
Tim Murrayeb4426d2013-08-27 15:30:16 -0700110 sp<const Element> U8_2;
111 sp<const Element> U8_3;
112 sp<const Element> U8_4;
Tim Murray89daad62013-07-29 14:30:02 -0700113 sp<const Element> I8;
Tim Murrayeb4426d2013-08-27 15:30:16 -0700114 sp<const Element> I8_2;
115 sp<const Element> I8_3;
116 sp<const Element> I8_4;
Tim Murray89daad62013-07-29 14:30:02 -0700117 sp<const Element> U16;
Tim Murrayeb4426d2013-08-27 15:30:16 -0700118 sp<const Element> U16_2;
119 sp<const Element> U16_3;
120 sp<const Element> U16_4;
Tim Murray89daad62013-07-29 14:30:02 -0700121 sp<const Element> I16;
Tim Murrayeb4426d2013-08-27 15:30:16 -0700122 sp<const Element> I16_2;
123 sp<const Element> I16_3;
124 sp<const Element> I16_4;
Tim Murray89daad62013-07-29 14:30:02 -0700125 sp<const Element> U32;
Tim Murrayeb4426d2013-08-27 15:30:16 -0700126 sp<const Element> U32_2;
127 sp<const Element> U32_3;
128 sp<const Element> U32_4;
Tim Murray89daad62013-07-29 14:30:02 -0700129 sp<const Element> I32;
Tim Murrayeb4426d2013-08-27 15:30:16 -0700130 sp<const Element> I32_2;
131 sp<const Element> I32_3;
132 sp<const Element> I32_4;
Tim Murray89daad62013-07-29 14:30:02 -0700133 sp<const Element> U64;
Tim Murrayeb4426d2013-08-27 15:30:16 -0700134 sp<const Element> U64_2;
135 sp<const Element> U64_3;
136 sp<const Element> U64_4;
Tim Murray89daad62013-07-29 14:30:02 -0700137 sp<const Element> I64;
Tim Murrayeb4426d2013-08-27 15:30:16 -0700138 sp<const Element> I64_2;
139 sp<const Element> I64_3;
140 sp<const Element> I64_4;
Tim Murray89daad62013-07-29 14:30:02 -0700141 sp<const Element> F32;
Tim Murrayeb4426d2013-08-27 15:30:16 -0700142 sp<const Element> F32_2;
143 sp<const Element> F32_3;
144 sp<const Element> F32_4;
Tim Murray89daad62013-07-29 14:30:02 -0700145 sp<const Element> F64;
Tim Murrayeb4426d2013-08-27 15:30:16 -0700146 sp<const Element> F64_2;
147 sp<const Element> F64_3;
148 sp<const Element> F64_4;
Tim Murray89daad62013-07-29 14:30:02 -0700149 sp<const Element> BOOLEAN;
Tim Murray84bf2b82012-10-31 16:03:16 -0700150
Tim Murray89daad62013-07-29 14:30:02 -0700151 sp<const Element> ELEMENT;
152 sp<const Element> TYPE;
153 sp<const Element> ALLOCATION;
154 sp<const Element> SAMPLER;
155 sp<const Element> SCRIPT;
156 sp<const Element> MESH;
157 sp<const Element> PROGRAM_FRAGMENT;
158 sp<const Element> PROGRAM_VERTEX;
159 sp<const Element> PROGRAM_RASTER;
160 sp<const Element> PROGRAM_STORE;
Tim Murray84bf2b82012-10-31 16:03:16 -0700161
Tim Murray89daad62013-07-29 14:30:02 -0700162 sp<const Element> A_8;
163 sp<const Element> RGB_565;
164 sp<const Element> RGB_888;
165 sp<const Element> RGBA_5551;
166 sp<const Element> RGBA_4444;
167 sp<const Element> RGBA_8888;
Tim Murray84bf2b82012-10-31 16:03:16 -0700168
Tim Murrayeb4426d2013-08-27 15:30:16 -0700169 sp<const Element> YUV;
Tim Murray84bf2b82012-10-31 16:03:16 -0700170
Tim Murray89daad62013-07-29 14:30:02 -0700171 sp<const Element> MATRIX_4X4;
172 sp<const Element> MATRIX_3X3;
173 sp<const Element> MATRIX_2X2;
Tim Murray84bf2b82012-10-31 16:03:16 -0700174 } mElements;
175
Tim Murray729b6fe2013-07-23 16:20:42 -0700176 struct {
Tim Murray89daad62013-07-29 14:30:02 -0700177 sp<const Sampler> CLAMP_NEAREST;
178 sp<const Sampler> CLAMP_LINEAR;
179 sp<const Sampler> CLAMP_LINEAR_MIP_LINEAR;
180 sp<const Sampler> WRAP_NEAREST;
181 sp<const Sampler> WRAP_LINEAR;
182 sp<const Sampler> WRAP_LINEAR_MIP_LINEAR;
183 sp<const Sampler> MIRRORED_REPEAT_NEAREST;
184 sp<const Sampler> MIRRORED_REPEAT_LINEAR;
185 sp<const Sampler> MIRRORED_REPEAT_LINEAR_MIP_LINEAR;
Tim Murray729b6fe2013-07-23 16:20:42 -0700186 } mSamplers;
187 friend class Sampler;
188 friend class Element;
Tim Murray84bf2b82012-10-31 16:03:16 -0700189};
190
Tim Murray89daad62013-07-29 14:30:02 -0700191class BaseObj : public android::RSC::LightRefBase<BaseObj> {
192public:
193 void * getID() const;
194 virtual ~BaseObj();
195 virtual void updateFromNative();
196 virtual bool equals(sp<const BaseObj> obj);
197
Tim Murray84bf2b82012-10-31 16:03:16 -0700198protected:
199 void *mID;
200 sp<RS> mRS;
Tim Murrayab716362013-08-12 12:37:18 -0700201 std::string mName;
Tim Murray84bf2b82012-10-31 16:03:16 -0700202
203 BaseObj(void *id, sp<RS> rs);
204 void checkValid();
205
206 static void * getObjID(sp<const BaseObj> o);
207
Tim Murray84bf2b82012-10-31 16:03:16 -0700208};
209
210
211class Allocation : public BaseObj {
212protected:
Tim Murray89daad62013-07-29 14:30:02 -0700213 sp<const Type> mType;
Tim Murray84bf2b82012-10-31 16:03:16 -0700214 uint32_t mUsage;
Tim Murray89daad62013-07-29 14:30:02 -0700215 sp<Allocation> mAdaptedAllocation;
Tim Murray84bf2b82012-10-31 16:03:16 -0700216
217 bool mConstrainedLOD;
218 bool mConstrainedFace;
219 bool mConstrainedY;
220 bool mConstrainedZ;
221 bool mReadAllowed;
222 bool mWriteAllowed;
223 uint32_t mSelectedY;
224 uint32_t mSelectedZ;
225 uint32_t mSelectedLOD;
226 RsAllocationCubemapFace mSelectedFace;
227
228 uint32_t mCurrentDimX;
229 uint32_t mCurrentDimY;
230 uint32_t mCurrentDimZ;
231 uint32_t mCurrentCount;
232
233 void * getIDSafe() const;
234 void updateCacheInfo(sp<const Type> t);
235
236 Allocation(void *id, sp<RS> rs, sp<const Type> t, uint32_t usage);
237
238 void validateIsInt32();
239 void validateIsInt16();
240 void validateIsInt8();
241 void validateIsFloat32();
242 void validateIsObject();
243
244 virtual void updateFromNative();
245
246 void validate2DRange(uint32_t xoff, uint32_t yoff, uint32_t w, uint32_t h);
247
248public:
Stephen Hinesa180b7d2013-08-21 12:42:13 -0700249 sp<const Type> getType() const {
Tim Murray84bf2b82012-10-31 16:03:16 -0700250 return mType;
251 }
252
253 void syncAll(RsAllocationUsageType srcLocation);
254 void ioSendOutput();
255 void ioGetInput();
256
257 void generateMipmaps();
Tim Murray509ea5c2012-11-13 11:56:40 -0800258
Tim Murray0b93e302012-11-15 14:56:54 -0800259 void copy1DRangeFrom(uint32_t off, size_t count, const void *data);
Tim Murraya4cbc2b2012-11-14 17:18:08 -0800260 void copy1DRangeFrom(uint32_t off, size_t count, sp<const Allocation> data, uint32_t dataOff);
Tim Murray84bf2b82012-10-31 16:03:16 -0700261
Tim Murray0b93e302012-11-15 14:56:54 -0800262 void copy1DRangeTo(uint32_t off, size_t count, void *data);
263
264 void copy1DFrom(const void* data);
265 void copy1DTo(void* data);
Tim Murraya4cbc2b2012-11-14 17:18:08 -0800266
Tim Murray84bf2b82012-10-31 16:03:16 -0700267 void copy2DRangeFrom(uint32_t xoff, uint32_t yoff, uint32_t w, uint32_t h,
Tim Murray0b93e302012-11-15 14:56:54 -0800268 const void *data);
Tim Murray7b3e3092012-11-16 13:32:24 -0800269
270 void copy2DRangeTo(uint32_t xoff, uint32_t yoff, uint32_t w, uint32_t h,
271 void *data);
272
Tim Murray0b93e302012-11-15 14:56:54 -0800273 void copy2DRangeFrom(uint32_t xoff, uint32_t yoff, uint32_t w, uint32_t h,
274 sp<const Allocation> data, uint32_t dataXoff, uint32_t dataYoff);
Tim Murray84bf2b82012-10-31 16:03:16 -0700275
Tim Murray358747a2012-11-26 13:52:04 -0800276 void copy2DStridedFrom(uint32_t xoff, uint32_t yoff, uint32_t w, uint32_t h,
277 const void *data, size_t stride);
278 void copy2DStridedFrom(const void *data, size_t stride);
279
280 void copy2DStridedTo(uint32_t xoff, uint32_t yoff, uint32_t w, uint32_t h,
281 void *data, size_t stride);
282 void copy2DStridedTo(void *data, size_t stride);
283
Tim Murray84bf2b82012-10-31 16:03:16 -0700284 void resize(int dimX);
285 void resize(int dimX, int dimY);
286
287 static sp<Allocation> createTyped(sp<RS> rs, sp<const Type> type,
288 RsAllocationMipmapControl mips, uint32_t usage);
289 static sp<Allocation> createTyped(sp<RS> rs, sp<const Type> type,
290 RsAllocationMipmapControl mips, uint32_t usage, void * pointer);
291
292 static sp<Allocation> createTyped(sp<RS> rs, sp<const Type> type,
293 uint32_t usage = RS_ALLOCATION_USAGE_SCRIPT);
294 static sp<Allocation> createSized(sp<RS> rs, sp<const Element> e, size_t count,
295 uint32_t usage = RS_ALLOCATION_USAGE_SCRIPT);
Tim Murray684726c2012-11-14 11:57:42 -0800296 static sp<Allocation> createSized2D(sp<RS> rs, sp<const Element> e,
297 size_t x, size_t y,
298 uint32_t usage = RS_ALLOCATION_USAGE_SCRIPT);
299
Tim Murray84bf2b82012-10-31 16:03:16 -0700300
301};
302
303class Element : public BaseObj {
304public:
305 bool isComplex();
306 size_t getSubElementCount() {
307 return mVisibleElementMap.size();
308 }
309
310 sp<const Element> getSubElement(uint32_t index);
311 const char * getSubElementName(uint32_t index);
312 size_t getSubElementArraySize(uint32_t index);
313 uint32_t getSubElementOffsetBytes(uint32_t index);
314 RsDataType getDataType() const {
315 return mType;
316 }
317
318 RsDataKind getDataKind() const {
319 return mKind;
320 }
321
322 size_t getSizeBytes() const {
323 return mSizeBytes;
324 }
325
Tim Murray10913a52013-08-20 17:19:47 -0700326 uint32_t getVectorSize() const {
327 return mVectorSize;
328 }
329
Tim Murray84bf2b82012-10-31 16:03:16 -0700330 static sp<const Element> BOOLEAN(sp<RS> rs);
331 static sp<const Element> U8(sp<RS> rs);
332 static sp<const Element> I8(sp<RS> rs);
333 static sp<const Element> U16(sp<RS> rs);
334 static sp<const Element> I16(sp<RS> rs);
335 static sp<const Element> U32(sp<RS> rs);
336 static sp<const Element> I32(sp<RS> rs);
337 static sp<const Element> U64(sp<RS> rs);
338 static sp<const Element> I64(sp<RS> rs);
339 static sp<const Element> F32(sp<RS> rs);
340 static sp<const Element> F64(sp<RS> rs);
341 static sp<const Element> ELEMENT(sp<RS> rs);
342 static sp<const Element> TYPE(sp<RS> rs);
343 static sp<const Element> ALLOCATION(sp<RS> rs);
344 static sp<const Element> SAMPLER(sp<RS> rs);
345 static sp<const Element> SCRIPT(sp<RS> rs);
346 static sp<const Element> MESH(sp<RS> rs);
347 static sp<const Element> PROGRAM_FRAGMENT(sp<RS> rs);
348 static sp<const Element> PROGRAM_VERTEX(sp<RS> rs);
349 static sp<const Element> PROGRAM_RASTER(sp<RS> rs);
350 static sp<const Element> PROGRAM_STORE(sp<RS> rs);
351
352 static sp<const Element> A_8(sp<RS> rs);
353 static sp<const Element> RGB_565(sp<RS> rs);
354 static sp<const Element> RGB_888(sp<RS> rs);
355 static sp<const Element> RGBA_5551(sp<RS> rs);
356 static sp<const Element> RGBA_4444(sp<RS> rs);
357 static sp<const Element> RGBA_8888(sp<RS> rs);
358
359 static sp<const Element> F32_2(sp<RS> rs);
360 static sp<const Element> F32_3(sp<RS> rs);
361 static sp<const Element> F32_4(sp<RS> rs);
362 static sp<const Element> F64_2(sp<RS> rs);
363 static sp<const Element> F64_3(sp<RS> rs);
364 static sp<const Element> F64_4(sp<RS> rs);
365 static sp<const Element> U8_2(sp<RS> rs);
366 static sp<const Element> U8_3(sp<RS> rs);
367 static sp<const Element> U8_4(sp<RS> rs);
368 static sp<const Element> I8_2(sp<RS> rs);
369 static sp<const Element> I8_3(sp<RS> rs);
370 static sp<const Element> I8_4(sp<RS> rs);
371 static sp<const Element> U16_2(sp<RS> rs);
372 static sp<const Element> U16_3(sp<RS> rs);
373 static sp<const Element> U16_4(sp<RS> rs);
374 static sp<const Element> I16_2(sp<RS> rs);
375 static sp<const Element> I16_3(sp<RS> rs);
376 static sp<const Element> I16_4(sp<RS> rs);
377 static sp<const Element> U32_2(sp<RS> rs);
378 static sp<const Element> U32_3(sp<RS> rs);
379 static sp<const Element> U32_4(sp<RS> rs);
380 static sp<const Element> I32_2(sp<RS> rs);
381 static sp<const Element> I32_3(sp<RS> rs);
382 static sp<const Element> I32_4(sp<RS> rs);
383 static sp<const Element> U64_2(sp<RS> rs);
384 static sp<const Element> U64_3(sp<RS> rs);
385 static sp<const Element> U64_4(sp<RS> rs);
386 static sp<const Element> I64_2(sp<RS> rs);
387 static sp<const Element> I64_3(sp<RS> rs);
388 static sp<const Element> I64_4(sp<RS> rs);
Tim Murrayeb4426d2013-08-27 15:30:16 -0700389 static sp<const Element> YUV(sp<RS> rs);
Tim Murray84bf2b82012-10-31 16:03:16 -0700390 static sp<const Element> MATRIX_4X4(sp<RS> rs);
391 static sp<const Element> MATRIX_3X3(sp<RS> rs);
392 static sp<const Element> MATRIX_2X2(sp<RS> rs);
393
Tim Murray84bf2b82012-10-31 16:03:16 -0700394 void updateFromNative();
395 static sp<const Element> createUser(sp<RS> rs, RsDataType dt);
396 static sp<const Element> createVector(sp<RS> rs, RsDataType dt, uint32_t size);
397 static sp<const Element> createPixel(sp<RS> rs, RsDataType dt, RsDataKind dk);
Tim Murray10913a52013-08-20 17:19:47 -0700398 bool isCompatible(sp<const Element>e) const;
Tim Murray84bf2b82012-10-31 16:03:16 -0700399
400 class Builder {
401 private:
402 sp<RS> mRS;
Tim Murray89daad62013-07-29 14:30:02 -0700403 std::vector<sp<Element> > mElements;
Tim Murrayab716362013-08-12 12:37:18 -0700404 std::vector<std::string> mElementNames;
Tim Murray89daad62013-07-29 14:30:02 -0700405 std::vector<uint32_t> mArraySizes;
Tim Murray84bf2b82012-10-31 16:03:16 -0700406 bool mSkipPadding;
407
408 public:
409 Builder(sp<RS> rs);
410 ~Builder();
Tim Murrayab716362013-08-12 12:37:18 -0700411 void add(sp<Element> e, std::string &name, uint32_t arraySize = 1);
Tim Murray84bf2b82012-10-31 16:03:16 -0700412 sp<const Element> create();
413 };
414
Stephen Hines7d1b7572013-08-22 01:24:06 -0700415protected:
416 Element(void *id, sp<RS> rs,
417 std::vector<sp<Element> > &elements,
418 std::vector<std::string> &elementNames,
419 std::vector<uint32_t> &arraySizes);
420 Element(void *id, sp<RS> rs, RsDataType dt, RsDataKind dk, bool norm, uint32_t size);
421 Element(sp<RS> rs);
422 virtual ~Element();
423
Tim Murray84bf2b82012-10-31 16:03:16 -0700424private:
425 void updateVisibleSubElements();
426
Tim Murray89daad62013-07-29 14:30:02 -0700427 std::vector<sp<Element> > mElements;
Tim Murrayab716362013-08-12 12:37:18 -0700428 std::vector<std::string> mElementNames;
Tim Murray89daad62013-07-29 14:30:02 -0700429 std::vector<uint32_t> mArraySizes;
430 std::vector<uint32_t> mVisibleElementMap;
431 std::vector<uint32_t> mOffsetInBytes;
Tim Murray84bf2b82012-10-31 16:03:16 -0700432
433 RsDataType mType;
434 RsDataKind mKind;
435 bool mNormalized;
436 size_t mSizeBytes;
437 size_t mVectorSize;
438};
439
Stephen Hines2c7206e2012-11-14 19:47:01 -0800440class FieldPacker {
441protected:
442 unsigned char* mData;
443 size_t mPos;
444 size_t mLen;
445
446public:
447 FieldPacker(size_t len)
Tim Murray89daad62013-07-29 14:30:02 -0700448 : mPos(0), mLen(len) {
449 mData = new unsigned char[len];
450 }
Stephen Hines2c7206e2012-11-14 19:47:01 -0800451
452 virtual ~FieldPacker() {
453 delete [] mData;
454 }
455
456 void align(size_t v) {
457 if ((v & (v - 1)) != 0) {
Tim Murrayab716362013-08-12 12:37:18 -0700458 // ALOGE("Non-power-of-two alignment: %zu", v);
Stephen Hines2c7206e2012-11-14 19:47:01 -0800459 return;
460 }
461
462 while ((mPos & (v - 1)) != 0) {
463 mData[mPos++] = 0;
464 }
465 }
466
467 void reset() {
468 mPos = 0;
469 }
470
471 void reset(size_t i) {
472 if (i >= mLen) {
Tim Murrayab716362013-08-12 12:37:18 -0700473 // ALOGE("Out of bounds: i (%zu) >= len (%zu)", i, mLen);
Stephen Hines2c7206e2012-11-14 19:47:01 -0800474 return;
475 }
476 mPos = i;
477 }
478
479 void skip(size_t i) {
480 size_t res = mPos + i;
481 if (res > mLen) {
Tim Murrayab716362013-08-12 12:37:18 -0700482 // ALOGE("Exceeded buffer length: i (%zu) > len (%zu)", i, mLen);
Stephen Hines2c7206e2012-11-14 19:47:01 -0800483 return;
484 }
485 mPos = res;
486 }
487
488 void* getData() const {
489 return mData;
490 }
491
492 size_t getLength() const {
493 return mLen;
494 }
495
496 template <typename T>
Tim Murray89daad62013-07-29 14:30:02 -0700497 void add(T t) {
Stephen Hines2c7206e2012-11-14 19:47:01 -0800498 align(sizeof(t));
499 if (mPos + sizeof(t) <= mLen) {
500 memcpy(&mData[mPos], &t, sizeof(t));
501 mPos += sizeof(t);
502 }
503 }
Stephen Hines43514cd2012-11-16 14:33:47 -0800504
505 /*
Tim Murray89daad62013-07-29 14:30:02 -0700506 void add(rs_matrix4x4 m) {
507 for (size_t i = 0; i < 16; 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_matrix3x3 m) {
513 for (size_t i = 0; i < 9; i++) {
514 add(m.m[i]);
515 }
516 }
Stephen Hines43514cd2012-11-16 14:33:47 -0800517
Tim Murray89daad62013-07-29 14:30:02 -0700518 void add(rs_matrix2x2 m) {
519 for (size_t i = 0; i < 4; i++) {
520 add(m.m[i]);
521 }
522 }
Stephen Hines43514cd2012-11-16 14:33:47 -0800523 */
524
Tim Murray89daad62013-07-29 14:30:02 -0700525 void add(sp<BaseObj> obj) {
Stephen Hines43514cd2012-11-16 14:33:47 -0800526 if (obj != NULL) {
527 add((uint32_t) (uintptr_t) obj->getID());
528 } else {
529 add((uint32_t) 0);
530 }
531 }
Stephen Hines2c7206e2012-11-14 19:47:01 -0800532};
533
Tim Murray89daad62013-07-29 14:30:02 -0700534
Tim Murray84bf2b82012-10-31 16:03:16 -0700535class Type : public BaseObj {
536protected:
537 friend class Allocation;
538
539 uint32_t mDimX;
540 uint32_t mDimY;
541 uint32_t mDimZ;
Tim Murrayeb4426d2013-08-27 15:30:16 -0700542 RSYuvFormat mYuvFormat;
Tim Murray84bf2b82012-10-31 16:03:16 -0700543 bool mDimMipmaps;
544 bool mDimFaces;
545 size_t mElementCount;
546 sp<const Element> mElement;
547
Stephen Hines7d1b7572013-08-22 01:24:06 -0700548 Type(void *id, sp<RS> rs);
549
Tim Murray84bf2b82012-10-31 16:03:16 -0700550 void calcElementCount();
551 virtual void updateFromNative();
552
553public:
554
Tim Murrayeb4426d2013-08-27 15:30:16 -0700555 RSYuvFormat getYuvFormat() const {
556 return mYuvFormat;
557 }
558
Tim Murray84bf2b82012-10-31 16:03:16 -0700559 sp<const Element> getElement() const {
560 return mElement;
561 }
562
563 uint32_t getX() const {
564 return mDimX;
565 }
566
567 uint32_t getY() const {
568 return mDimY;
569 }
570
571 uint32_t getZ() const {
572 return mDimZ;
573 }
574
575 bool hasMipmaps() const {
576 return mDimMipmaps;
577 }
578
579 bool hasFaces() const {
580 return mDimFaces;
581 }
582
583 size_t getCount() const {
584 return mElementCount;
585 }
586
587 size_t getSizeBytes() const {
588 return mElementCount * mElement->getSizeBytes();
589 }
590
Tim Murray96267c22013-02-12 11:25:12 -0800591 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 -0700592
593 class Builder {
594 protected:
595 sp<RS> mRS;
596 uint32_t mDimX;
597 uint32_t mDimY;
598 uint32_t mDimZ;
Tim Murrayeb4426d2013-08-27 15:30:16 -0700599 RSYuvFormat mYuvFormat;
Tim Murray84bf2b82012-10-31 16:03:16 -0700600 bool mDimMipmaps;
601 bool mDimFaces;
602 sp<const Element> mElement;
603
604 public:
605 Builder(sp<RS> rs, sp<const Element> e);
606
607 void setX(uint32_t value);
Stephen Hines7d1b7572013-08-22 01:24:06 -0700608 void setY(uint32_t value);
Tim Murrayeb4426d2013-08-27 15:30:16 -0700609 void setZ(uint32_t value);
610 void setYuvFormat(RSYuvFormat format);
Tim Murray84bf2b82012-10-31 16:03:16 -0700611 void setMipmaps(bool value);
612 void setFaces(bool value);
613 sp<const Type> create();
614 };
615
616};
617
618class Script : public BaseObj {
619private:
620
621protected:
622 Script(void *id, sp<RS> rs);
623 void forEach(uint32_t slot, sp<const Allocation> in, sp<const Allocation> out,
624 const void *v, size_t) const;
625 void bindAllocation(sp<Allocation> va, uint32_t slot) const;
626 void setVar(uint32_t index, const void *, size_t len) const;
627 void setVar(uint32_t index, sp<const BaseObj> o) const;
628 void invoke(uint32_t slot, const void *v, size_t len) const;
629
630
631 void invoke(uint32_t slot) const {
632 invoke(slot, NULL, 0);
633 }
634 void setVar(uint32_t index, float v) const {
635 setVar(index, &v, sizeof(v));
636 }
637 void setVar(uint32_t index, double v) const {
638 setVar(index, &v, sizeof(v));
639 }
640 void setVar(uint32_t index, int32_t v) const {
641 setVar(index, &v, sizeof(v));
642 }
643 void setVar(uint32_t index, int64_t v) const {
644 setVar(index, &v, sizeof(v));
645 }
646 void setVar(uint32_t index, bool v) const {
647 setVar(index, &v, sizeof(v));
648 }
649
650public:
651 class FieldBase {
652 protected:
653 sp<const Element> mElement;
654 sp<Allocation> mAllocation;
655
656 void init(sp<RS> rs, uint32_t dimx, uint32_t usages = 0);
657
658 public:
659 sp<const Element> getElement() {
660 return mElement;
661 }
662
663 sp<const Type> getType() {
664 return mAllocation->getType();
665 }
666
667 sp<const Allocation> getAllocation() {
668 return mAllocation;
669 }
670
671 //void updateAllocation();
672 };
673};
674
675class ScriptC : public Script {
676protected:
677 ScriptC(sp<RS> rs,
678 const void *codeTxt, size_t codeLength,
679 const char *cachedName, size_t cachedNameLength,
680 const char *cacheDir, size_t cacheDirLength);
681
682};
683
Tim Murray7f0d5682012-11-08 16:35:24 -0800684class ScriptIntrinsic : public Script {
685 protected:
Tim Murray10913a52013-08-20 17:19:47 -0700686 sp<const Element> mElement;
Tim Murray3cd44af2012-11-14 11:25:27 -0800687 ScriptIntrinsic(sp<RS> rs, int id, sp<const Element> e);
Tim Murrayb27b1812013-08-05 14:00:40 -0700688 virtual ~ScriptIntrinsic();
Tim Murray7f0d5682012-11-08 16:35:24 -0800689};
690
Tim Murray89daad62013-07-29 14:30:02 -0700691class ScriptIntrinsic3DLUT : public ScriptIntrinsic {
Tim Murray21fa7a02013-08-15 16:25:03 -0700692 private:
Tim Murray89daad62013-07-29 14:30:02 -0700693 ScriptIntrinsic3DLUT(sp<RS> rs, sp<const Element> e);
Tim Murray21fa7a02013-08-15 16:25:03 -0700694 public:
695 static sp<ScriptIntrinsic3DLUT> create(sp<RS> rs, sp<const Element> e);
Tim Murray89daad62013-07-29 14:30:02 -0700696 void forEach(sp<Allocation> ain, sp<Allocation> aout);
697 void setLUT(sp<Allocation> lut);
698};
699
Tim Murray7f0d5682012-11-08 16:35:24 -0800700class ScriptIntrinsicBlend : public ScriptIntrinsic {
Tim Murray21fa7a02013-08-15 16:25:03 -0700701 private:
Tim Murray89daad62013-07-29 14:30:02 -0700702 ScriptIntrinsicBlend(sp<RS> rs, sp<const Element> e);
Tim Murray21fa7a02013-08-15 16:25:03 -0700703 public:
704 static sp<ScriptIntrinsicBlend> create(sp<RS> rs, sp<const Element> e);
Tim Murray7f0d5682012-11-08 16:35:24 -0800705 void blendClear(sp<Allocation> in, sp<Allocation> out);
706 void blendSrc(sp<Allocation> in, sp<Allocation> out);
707 void blendDst(sp<Allocation> in, sp<Allocation> out);
708 void blendSrcOver(sp<Allocation> in, sp<Allocation> out);
709 void blendDstOver(sp<Allocation> in, sp<Allocation> out);
710 void blendSrcIn(sp<Allocation> in, sp<Allocation> out);
711 void blendDstIn(sp<Allocation> in, sp<Allocation> out);
712 void blendSrcOut(sp<Allocation> in, sp<Allocation> out);
713 void blendDstOut(sp<Allocation> in, sp<Allocation> out);
714 void blendSrcAtop(sp<Allocation> in, sp<Allocation> out);
715 void blendDstAtop(sp<Allocation> in, sp<Allocation> out);
716 void blendXor(sp<Allocation> in, sp<Allocation> out);
717 void blendMultiply(sp<Allocation> in, sp<Allocation> out);
718 void blendAdd(sp<Allocation> in, sp<Allocation> out);
719 void blendSubtract(sp<Allocation> in, sp<Allocation> out);
720};
Tim Murray84bf2b82012-10-31 16:03:16 -0700721
Tim Murray8f1e60c2012-11-13 12:25:11 -0800722class ScriptIntrinsicBlur : public ScriptIntrinsic {
Tim Murray21fa7a02013-08-15 16:25:03 -0700723 private:
Tim Murray89daad62013-07-29 14:30:02 -0700724 ScriptIntrinsicBlur(sp<RS> rs, sp<const Element> e);
Tim Murray21fa7a02013-08-15 16:25:03 -0700725 public:
726 static sp<ScriptIntrinsicBlur> create(sp<RS> rs, sp<const Element> e);
727 void setInput(sp<Allocation> in);
728 void forEach(sp<Allocation> out);
Tim Murray8f1e60c2012-11-13 12:25:11 -0800729 void setRadius(float radius);
730};
731
Tim Murray89daad62013-07-29 14:30:02 -0700732class ScriptIntrinsicColorMatrix : public ScriptIntrinsic {
Tim Murray21fa7a02013-08-15 16:25:03 -0700733 private:
Tim Murray89daad62013-07-29 14:30:02 -0700734 ScriptIntrinsicColorMatrix(sp<RS> rs, sp<const Element> e);
Tim Murray21fa7a02013-08-15 16:25:03 -0700735 public:
736 static sp<ScriptIntrinsicColorMatrix> create(sp<RS> rs, sp<const Element> e);
Tim Murray89daad62013-07-29 14:30:02 -0700737 void forEach(sp<Allocation> in, sp<Allocation> out);
Tim Murray10913a52013-08-20 17:19:47 -0700738 void setAdd(float* add);
Tim Murray89daad62013-07-29 14:30:02 -0700739 void setColorMatrix3(float* m);
740 void setColorMatrix4(float* m);
741 void setGreyscale();
742 void setRGBtoYUV();
743 void setYUVtoRGB();
744};
745
746class ScriptIntrinsicConvolve3x3 : public ScriptIntrinsic {
Tim Murray21fa7a02013-08-15 16:25:03 -0700747 private:
Tim Murray89daad62013-07-29 14:30:02 -0700748 ScriptIntrinsicConvolve3x3(sp<RS> rs, sp<const Element> e);
Tim Murray21fa7a02013-08-15 16:25:03 -0700749 public:
750 static sp<ScriptIntrinsicConvolve3x3> create(sp<RS> rs, sp<const Element> e);
Tim Murray89daad62013-07-29 14:30:02 -0700751 void setInput(sp<Allocation> in);
752 void forEach(sp<Allocation> out);
753 void setCoefficients(float* v);
754};
755
756class ScriptIntrinsicConvolve5x5 : public ScriptIntrinsic {
Tim Murray21fa7a02013-08-15 16:25:03 -0700757 private:
Tim Murray89daad62013-07-29 14:30:02 -0700758 ScriptIntrinsicConvolve5x5(sp<RS> rs, sp<const Element> e);
Tim Murray21fa7a02013-08-15 16:25:03 -0700759 public:
760 static sp<ScriptIntrinsicConvolve5x5> create(sp<RS> rs, sp<const Element> e);
Tim Murray89daad62013-07-29 14:30:02 -0700761 void setInput(sp<Allocation> in);
762 void forEach(sp<Allocation> out);
763 void setCoefficients(float* v);
764};
765
Tim Murrayb27b1812013-08-05 14:00:40 -0700766class ScriptIntrinsicHistogram : public ScriptIntrinsic {
Tim Murray21fa7a02013-08-15 16:25:03 -0700767 private:
Tim Murrayb27b1812013-08-05 14:00:40 -0700768 ScriptIntrinsicHistogram(sp<RS> rs, sp<const Element> e);
Tim Murray10913a52013-08-20 17:19:47 -0700769 sp<Allocation> mOut;
Tim Murray21fa7a02013-08-15 16:25:03 -0700770 public:
Tim Murray10913a52013-08-20 17:19:47 -0700771 static sp<ScriptIntrinsicHistogram> create(sp<RS> rs);
Tim Murrayb27b1812013-08-05 14:00:40 -0700772 void setOutput(sp<Allocation> aout);
773 void setDotCoefficients(float r, float g, float b, float a);
774 void forEach(sp<Allocation> ain);
775 void forEach_dot(sp<Allocation> ain);
776};
777
778class ScriptIntrinsicLUT : public ScriptIntrinsic {
779 private:
780 sp<Allocation> LUT;
781 bool mDirty;
782 unsigned char mCache[1024];
Tim Murray2acce992013-08-28 14:23:31 -0700783 void setTable(unsigned int offset, unsigned char base, unsigned int length, unsigned char* lutValues);
Tim Murray21fa7a02013-08-15 16:25:03 -0700784 ScriptIntrinsicLUT(sp<RS> rs, sp<const Element> e);
Tim Murrayb27b1812013-08-05 14:00:40 -0700785
Tim Murray89daad62013-07-29 14:30:02 -0700786 public:
Tim Murray21fa7a02013-08-15 16:25:03 -0700787 static sp<ScriptIntrinsicLUT> create(sp<RS> rs, sp<const Element> e);
Tim Murray89daad62013-07-29 14:30:02 -0700788 void forEach(sp<Allocation> ain, sp<Allocation> aout);
Tim Murray2acce992013-08-28 14:23:31 -0700789 void setRed(unsigned char base, unsigned int length, unsigned char* lutValues);
790 void setGreen(unsigned char base, unsigned int length, unsigned char* lutValues);
791 void setBlue(unsigned char base, unsigned int length, unsigned char* lutValues);
792 void setAlpha(unsigned char base, unsigned int length, unsigned char* lutValues);
Tim Murrayb27b1812013-08-05 14:00:40 -0700793 virtual ~ScriptIntrinsicLUT();
794};
795
Tim Murray89daad62013-07-29 14:30:02 -0700796class ScriptIntrinsicYuvToRGB : public ScriptIntrinsic {
Tim Murray21fa7a02013-08-15 16:25:03 -0700797 private:
Tim Murrayb27b1812013-08-05 14:00:40 -0700798 ScriptIntrinsicYuvToRGB(sp<RS> rs, sp<const Element> e);
Tim Murray21fa7a02013-08-15 16:25:03 -0700799 public:
800 static sp<ScriptIntrinsicYuvToRGB> create(sp<RS> rs, sp<const Element> e);
Tim Murrayb27b1812013-08-05 14:00:40 -0700801 void setInput(sp<Allocation> in);
802 void forEach(sp<Allocation> out);
Tim Murray89daad62013-07-29 14:30:02 -0700803
804};
Tim Murrayb27b1812013-08-05 14:00:40 -0700805
Tim Murray89daad62013-07-29 14:30:02 -0700806
Tim Murray729b6fe2013-07-23 16:20:42 -0700807 class Sampler : public BaseObj {
808 private:
809 Sampler(sp<RS> rs, void* id);
810 RsSamplerValue mMin;
811 RsSamplerValue mMag;
812 RsSamplerValue mWrapS;
813 RsSamplerValue mWrapT;
814 RsSamplerValue mWrapR;
815 float mAniso;
816
817 public:
818 static sp<Sampler> create(sp<RS> rs, RsSamplerValue min, RsSamplerValue mag, RsSamplerValue wrapS, RsSamplerValue wrapT, float anisotropy);
819
820 RsSamplerValue getMinification();
821 RsSamplerValue getMagnification();
822 RsSamplerValue getWrapS();
823 RsSamplerValue getWrapT();
824 float getAnisotropy();
825
826 sp<const Sampler> CLAMP_NEAREST(sp<RS> rs);
827 sp<const Sampler> CLAMP_LINEAR(sp<RS> rs);
828 sp<const Sampler> CLAMP_LINEAR_MIP_LINEAR(sp<RS> rs);
829 sp<const Sampler> WRAP_NEAREST(sp<RS> rs);
830 sp<const Sampler> WRAP_LINEAR(sp<RS> rs);
831 sp<const Sampler> WRAP_LINEAR_MIP_LINEAR(sp<RS> rs);
832 sp<const Sampler> MIRRORED_REPEAT_NEAREST(sp<RS> rs);
833 sp<const Sampler> MIRRORED_REPEAT_LINEAR(sp<RS> rs);
834 sp<const Sampler> MIRRORED_REPEAT_LINEAR_MIP_LINEAR(sp<RS> rs);
835
836};
837
Tim Murray84bf2b82012-10-31 16:03:16 -0700838}
Tim Murray7f0d5682012-11-08 16:35:24 -0800839
Tim Murray84bf2b82012-10-31 16:03:16 -0700840}
841
842#endif