blob: 3d104ebdd5345412cdbf2ac2027063e373c907fb [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);
Tim Murray9d24ae62013-08-30 12:17:14 -0700247 void validate3DRange(uint32_t xoff, uint32_t yoff, uint32_t zoff,
248 uint32_t w, uint32_t h, uint32_t d);
Tim Murray84bf2b82012-10-31 16:03:16 -0700249
250public:
Stephen Hinesa180b7d2013-08-21 12:42:13 -0700251 sp<const Type> getType() const {
Tim Murray84bf2b82012-10-31 16:03:16 -0700252 return mType;
253 }
254
255 void syncAll(RsAllocationUsageType srcLocation);
256 void ioSendOutput();
257 void ioGetInput();
258
259 void generateMipmaps();
Tim Murray509ea5c2012-11-13 11:56:40 -0800260
Tim Murray0b93e302012-11-15 14:56:54 -0800261 void copy1DRangeFrom(uint32_t off, size_t count, const void *data);
Tim Murraya4cbc2b2012-11-14 17:18:08 -0800262 void copy1DRangeFrom(uint32_t off, size_t count, sp<const Allocation> data, uint32_t dataOff);
Tim Murray84bf2b82012-10-31 16:03:16 -0700263
Tim Murray0b93e302012-11-15 14:56:54 -0800264 void copy1DRangeTo(uint32_t off, size_t count, void *data);
265
266 void copy1DFrom(const void* data);
267 void copy1DTo(void* data);
Tim Murraya4cbc2b2012-11-14 17:18:08 -0800268
Tim Murray84bf2b82012-10-31 16:03:16 -0700269 void copy2DRangeFrom(uint32_t xoff, uint32_t yoff, uint32_t w, uint32_t h,
Tim Murray0b93e302012-11-15 14:56:54 -0800270 const void *data);
Tim Murray7b3e3092012-11-16 13:32:24 -0800271
272 void copy2DRangeTo(uint32_t xoff, uint32_t yoff, uint32_t w, uint32_t h,
273 void *data);
274
Tim Murray0b93e302012-11-15 14:56:54 -0800275 void copy2DRangeFrom(uint32_t xoff, uint32_t yoff, uint32_t w, uint32_t h,
276 sp<const Allocation> data, uint32_t dataXoff, uint32_t dataYoff);
Tim Murray84bf2b82012-10-31 16:03:16 -0700277
Tim Murray358747a2012-11-26 13:52:04 -0800278 void copy2DStridedFrom(uint32_t xoff, uint32_t yoff, uint32_t w, uint32_t h,
279 const void *data, size_t stride);
280 void copy2DStridedFrom(const void *data, size_t stride);
281
282 void copy2DStridedTo(uint32_t xoff, uint32_t yoff, uint32_t w, uint32_t h,
283 void *data, size_t stride);
284 void copy2DStridedTo(void *data, size_t stride);
285
Tim Murray9d24ae62013-08-30 12:17:14 -0700286 void copy3DRangeFrom(uint32_t xoff, uint32_t yoff, uint32_t zoff, uint32_t w,
287 uint32_t h, uint32_t d, const void* data);
288
289 void copy3DRangeFrom(uint32_t xoff, uint32_t yoff, uint32_t zoff,
290 uint32_t w, uint32_t h, uint32_t d,
291 sp<const Allocation> data,
292 uint32_t dataXoff, uint32_t dataYoff, uint32_t dataZoff);
Tim Murray84bf2b82012-10-31 16:03:16 -0700293
294 static sp<Allocation> createTyped(sp<RS> rs, sp<const Type> type,
295 RsAllocationMipmapControl mips, uint32_t usage);
296 static sp<Allocation> createTyped(sp<RS> rs, sp<const Type> type,
297 RsAllocationMipmapControl mips, uint32_t usage, void * pointer);
298
299 static sp<Allocation> createTyped(sp<RS> rs, sp<const Type> type,
300 uint32_t usage = RS_ALLOCATION_USAGE_SCRIPT);
301 static sp<Allocation> createSized(sp<RS> rs, sp<const Element> e, size_t count,
302 uint32_t usage = RS_ALLOCATION_USAGE_SCRIPT);
Tim Murray684726c2012-11-14 11:57:42 -0800303 static sp<Allocation> createSized2D(sp<RS> rs, sp<const Element> e,
304 size_t x, size_t y,
305 uint32_t usage = RS_ALLOCATION_USAGE_SCRIPT);
306
Tim Murray84bf2b82012-10-31 16:03:16 -0700307
308};
309
310class Element : public BaseObj {
311public:
312 bool isComplex();
313 size_t getSubElementCount() {
314 return mVisibleElementMap.size();
315 }
316
317 sp<const Element> getSubElement(uint32_t index);
318 const char * getSubElementName(uint32_t index);
319 size_t getSubElementArraySize(uint32_t index);
320 uint32_t getSubElementOffsetBytes(uint32_t index);
321 RsDataType getDataType() const {
322 return mType;
323 }
324
325 RsDataKind getDataKind() const {
326 return mKind;
327 }
328
329 size_t getSizeBytes() const {
330 return mSizeBytes;
331 }
332
Tim Murray10913a52013-08-20 17:19:47 -0700333 uint32_t getVectorSize() const {
334 return mVectorSize;
335 }
336
Tim Murray84bf2b82012-10-31 16:03:16 -0700337 static sp<const Element> BOOLEAN(sp<RS> rs);
338 static sp<const Element> U8(sp<RS> rs);
339 static sp<const Element> I8(sp<RS> rs);
340 static sp<const Element> U16(sp<RS> rs);
341 static sp<const Element> I16(sp<RS> rs);
342 static sp<const Element> U32(sp<RS> rs);
343 static sp<const Element> I32(sp<RS> rs);
344 static sp<const Element> U64(sp<RS> rs);
345 static sp<const Element> I64(sp<RS> rs);
346 static sp<const Element> F32(sp<RS> rs);
347 static sp<const Element> F64(sp<RS> rs);
348 static sp<const Element> ELEMENT(sp<RS> rs);
349 static sp<const Element> TYPE(sp<RS> rs);
350 static sp<const Element> ALLOCATION(sp<RS> rs);
351 static sp<const Element> SAMPLER(sp<RS> rs);
352 static sp<const Element> SCRIPT(sp<RS> rs);
353 static sp<const Element> MESH(sp<RS> rs);
354 static sp<const Element> PROGRAM_FRAGMENT(sp<RS> rs);
355 static sp<const Element> PROGRAM_VERTEX(sp<RS> rs);
356 static sp<const Element> PROGRAM_RASTER(sp<RS> rs);
357 static sp<const Element> PROGRAM_STORE(sp<RS> rs);
358
359 static sp<const Element> A_8(sp<RS> rs);
360 static sp<const Element> RGB_565(sp<RS> rs);
361 static sp<const Element> RGB_888(sp<RS> rs);
362 static sp<const Element> RGBA_5551(sp<RS> rs);
363 static sp<const Element> RGBA_4444(sp<RS> rs);
364 static sp<const Element> RGBA_8888(sp<RS> rs);
365
366 static sp<const Element> F32_2(sp<RS> rs);
367 static sp<const Element> F32_3(sp<RS> rs);
368 static sp<const Element> F32_4(sp<RS> rs);
369 static sp<const Element> F64_2(sp<RS> rs);
370 static sp<const Element> F64_3(sp<RS> rs);
371 static sp<const Element> F64_4(sp<RS> rs);
372 static sp<const Element> U8_2(sp<RS> rs);
373 static sp<const Element> U8_3(sp<RS> rs);
374 static sp<const Element> U8_4(sp<RS> rs);
375 static sp<const Element> I8_2(sp<RS> rs);
376 static sp<const Element> I8_3(sp<RS> rs);
377 static sp<const Element> I8_4(sp<RS> rs);
378 static sp<const Element> U16_2(sp<RS> rs);
379 static sp<const Element> U16_3(sp<RS> rs);
380 static sp<const Element> U16_4(sp<RS> rs);
381 static sp<const Element> I16_2(sp<RS> rs);
382 static sp<const Element> I16_3(sp<RS> rs);
383 static sp<const Element> I16_4(sp<RS> rs);
384 static sp<const Element> U32_2(sp<RS> rs);
385 static sp<const Element> U32_3(sp<RS> rs);
386 static sp<const Element> U32_4(sp<RS> rs);
387 static sp<const Element> I32_2(sp<RS> rs);
388 static sp<const Element> I32_3(sp<RS> rs);
389 static sp<const Element> I32_4(sp<RS> rs);
390 static sp<const Element> U64_2(sp<RS> rs);
391 static sp<const Element> U64_3(sp<RS> rs);
392 static sp<const Element> U64_4(sp<RS> rs);
393 static sp<const Element> I64_2(sp<RS> rs);
394 static sp<const Element> I64_3(sp<RS> rs);
395 static sp<const Element> I64_4(sp<RS> rs);
Tim Murrayeb4426d2013-08-27 15:30:16 -0700396 static sp<const Element> YUV(sp<RS> rs);
Tim Murray84bf2b82012-10-31 16:03:16 -0700397 static sp<const Element> MATRIX_4X4(sp<RS> rs);
398 static sp<const Element> MATRIX_3X3(sp<RS> rs);
399 static sp<const Element> MATRIX_2X2(sp<RS> rs);
400
Tim Murray84bf2b82012-10-31 16:03:16 -0700401 void updateFromNative();
402 static sp<const Element> createUser(sp<RS> rs, RsDataType dt);
403 static sp<const Element> createVector(sp<RS> rs, RsDataType dt, uint32_t size);
404 static sp<const Element> createPixel(sp<RS> rs, RsDataType dt, RsDataKind dk);
Tim Murray10913a52013-08-20 17:19:47 -0700405 bool isCompatible(sp<const Element>e) const;
Tim Murray84bf2b82012-10-31 16:03:16 -0700406
407 class Builder {
408 private:
409 sp<RS> mRS;
Tim Murray89daad62013-07-29 14:30:02 -0700410 std::vector<sp<Element> > mElements;
Tim Murrayab716362013-08-12 12:37:18 -0700411 std::vector<std::string> mElementNames;
Tim Murray89daad62013-07-29 14:30:02 -0700412 std::vector<uint32_t> mArraySizes;
Tim Murray84bf2b82012-10-31 16:03:16 -0700413 bool mSkipPadding;
414
415 public:
416 Builder(sp<RS> rs);
417 ~Builder();
Tim Murrayab716362013-08-12 12:37:18 -0700418 void add(sp<Element> e, std::string &name, uint32_t arraySize = 1);
Tim Murray84bf2b82012-10-31 16:03:16 -0700419 sp<const Element> create();
420 };
421
Stephen Hines7d1b7572013-08-22 01:24:06 -0700422protected:
423 Element(void *id, sp<RS> rs,
424 std::vector<sp<Element> > &elements,
425 std::vector<std::string> &elementNames,
426 std::vector<uint32_t> &arraySizes);
427 Element(void *id, sp<RS> rs, RsDataType dt, RsDataKind dk, bool norm, uint32_t size);
428 Element(sp<RS> rs);
429 virtual ~Element();
430
Tim Murray84bf2b82012-10-31 16:03:16 -0700431private:
432 void updateVisibleSubElements();
433
Tim Murray89daad62013-07-29 14:30:02 -0700434 std::vector<sp<Element> > mElements;
Tim Murrayab716362013-08-12 12:37:18 -0700435 std::vector<std::string> mElementNames;
Tim Murray89daad62013-07-29 14:30:02 -0700436 std::vector<uint32_t> mArraySizes;
437 std::vector<uint32_t> mVisibleElementMap;
438 std::vector<uint32_t> mOffsetInBytes;
Tim Murray84bf2b82012-10-31 16:03:16 -0700439
440 RsDataType mType;
441 RsDataKind mKind;
442 bool mNormalized;
443 size_t mSizeBytes;
444 size_t mVectorSize;
445};
446
Stephen Hines2c7206e2012-11-14 19:47:01 -0800447class FieldPacker {
448protected:
449 unsigned char* mData;
450 size_t mPos;
451 size_t mLen;
452
453public:
454 FieldPacker(size_t len)
Tim Murray89daad62013-07-29 14:30:02 -0700455 : mPos(0), mLen(len) {
456 mData = new unsigned char[len];
457 }
Stephen Hines2c7206e2012-11-14 19:47:01 -0800458
459 virtual ~FieldPacker() {
460 delete [] mData;
461 }
462
463 void align(size_t v) {
464 if ((v & (v - 1)) != 0) {
Tim Murrayab716362013-08-12 12:37:18 -0700465 // ALOGE("Non-power-of-two alignment: %zu", v);
Stephen Hines2c7206e2012-11-14 19:47:01 -0800466 return;
467 }
468
469 while ((mPos & (v - 1)) != 0) {
470 mData[mPos++] = 0;
471 }
472 }
473
474 void reset() {
475 mPos = 0;
476 }
477
478 void reset(size_t i) {
479 if (i >= mLen) {
Tim Murrayab716362013-08-12 12:37:18 -0700480 // ALOGE("Out of bounds: i (%zu) >= len (%zu)", i, mLen);
Stephen Hines2c7206e2012-11-14 19:47:01 -0800481 return;
482 }
483 mPos = i;
484 }
485
486 void skip(size_t i) {
487 size_t res = mPos + i;
488 if (res > mLen) {
Tim Murrayab716362013-08-12 12:37:18 -0700489 // ALOGE("Exceeded buffer length: i (%zu) > len (%zu)", i, mLen);
Stephen Hines2c7206e2012-11-14 19:47:01 -0800490 return;
491 }
492 mPos = res;
493 }
494
495 void* getData() const {
496 return mData;
497 }
498
499 size_t getLength() const {
500 return mLen;
501 }
502
503 template <typename T>
Tim Murray89daad62013-07-29 14:30:02 -0700504 void add(T t) {
Stephen Hines2c7206e2012-11-14 19:47:01 -0800505 align(sizeof(t));
506 if (mPos + sizeof(t) <= mLen) {
507 memcpy(&mData[mPos], &t, sizeof(t));
508 mPos += sizeof(t);
509 }
510 }
Stephen Hines43514cd2012-11-16 14:33:47 -0800511
512 /*
Tim Murray89daad62013-07-29 14:30:02 -0700513 void add(rs_matrix4x4 m) {
514 for (size_t i = 0; i < 16; i++) {
515 add(m.m[i]);
516 }
517 }
Stephen Hines43514cd2012-11-16 14:33:47 -0800518
Tim Murray89daad62013-07-29 14:30:02 -0700519 void add(rs_matrix3x3 m) {
520 for (size_t i = 0; i < 9; i++) {
521 add(m.m[i]);
522 }
523 }
Stephen Hines43514cd2012-11-16 14:33:47 -0800524
Tim Murray89daad62013-07-29 14:30:02 -0700525 void add(rs_matrix2x2 m) {
526 for (size_t i = 0; i < 4; i++) {
527 add(m.m[i]);
528 }
529 }
Stephen Hines43514cd2012-11-16 14:33:47 -0800530 */
531
Tim Murray89daad62013-07-29 14:30:02 -0700532 void add(sp<BaseObj> obj) {
Stephen Hines43514cd2012-11-16 14:33:47 -0800533 if (obj != NULL) {
534 add((uint32_t) (uintptr_t) obj->getID());
535 } else {
536 add((uint32_t) 0);
537 }
538 }
Stephen Hines2c7206e2012-11-14 19:47:01 -0800539};
540
Tim Murray89daad62013-07-29 14:30:02 -0700541
Tim Murray84bf2b82012-10-31 16:03:16 -0700542class Type : public BaseObj {
543protected:
544 friend class Allocation;
545
546 uint32_t mDimX;
547 uint32_t mDimY;
548 uint32_t mDimZ;
Tim Murrayeb4426d2013-08-27 15:30:16 -0700549 RSYuvFormat mYuvFormat;
Tim Murray84bf2b82012-10-31 16:03:16 -0700550 bool mDimMipmaps;
551 bool mDimFaces;
552 size_t mElementCount;
553 sp<const Element> mElement;
554
Stephen Hines7d1b7572013-08-22 01:24:06 -0700555 Type(void *id, sp<RS> rs);
556
Tim Murray84bf2b82012-10-31 16:03:16 -0700557 void calcElementCount();
558 virtual void updateFromNative();
559
560public:
561
Tim Murrayeb4426d2013-08-27 15:30:16 -0700562 RSYuvFormat getYuvFormat() const {
563 return mYuvFormat;
564 }
565
Tim Murray84bf2b82012-10-31 16:03:16 -0700566 sp<const Element> getElement() const {
567 return mElement;
568 }
569
570 uint32_t getX() const {
571 return mDimX;
572 }
573
574 uint32_t getY() const {
575 return mDimY;
576 }
577
578 uint32_t getZ() const {
579 return mDimZ;
580 }
581
582 bool hasMipmaps() const {
583 return mDimMipmaps;
584 }
585
586 bool hasFaces() const {
587 return mDimFaces;
588 }
589
590 size_t getCount() const {
591 return mElementCount;
592 }
593
594 size_t getSizeBytes() const {
595 return mElementCount * mElement->getSizeBytes();
596 }
597
Tim Murray96267c22013-02-12 11:25:12 -0800598 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 -0700599
600 class Builder {
601 protected:
602 sp<RS> mRS;
603 uint32_t mDimX;
604 uint32_t mDimY;
605 uint32_t mDimZ;
Tim Murrayeb4426d2013-08-27 15:30:16 -0700606 RSYuvFormat mYuvFormat;
Tim Murray84bf2b82012-10-31 16:03:16 -0700607 bool mDimMipmaps;
608 bool mDimFaces;
609 sp<const Element> mElement;
610
611 public:
612 Builder(sp<RS> rs, sp<const Element> e);
613
614 void setX(uint32_t value);
Stephen Hines7d1b7572013-08-22 01:24:06 -0700615 void setY(uint32_t value);
Tim Murrayeb4426d2013-08-27 15:30:16 -0700616 void setZ(uint32_t value);
617 void setYuvFormat(RSYuvFormat format);
Tim Murray84bf2b82012-10-31 16:03:16 -0700618 void setMipmaps(bool value);
619 void setFaces(bool value);
620 sp<const Type> create();
621 };
622
623};
624
625class Script : public BaseObj {
626private:
627
628protected:
629 Script(void *id, sp<RS> rs);
630 void forEach(uint32_t slot, sp<const Allocation> in, sp<const Allocation> out,
631 const void *v, size_t) const;
632 void bindAllocation(sp<Allocation> va, uint32_t slot) const;
633 void setVar(uint32_t index, const void *, size_t len) const;
634 void setVar(uint32_t index, sp<const BaseObj> o) const;
635 void invoke(uint32_t slot, const void *v, size_t len) const;
636
637
638 void invoke(uint32_t slot) const {
639 invoke(slot, NULL, 0);
640 }
641 void setVar(uint32_t index, float v) const {
642 setVar(index, &v, sizeof(v));
643 }
644 void setVar(uint32_t index, double v) const {
645 setVar(index, &v, sizeof(v));
646 }
647 void setVar(uint32_t index, int32_t v) const {
648 setVar(index, &v, sizeof(v));
649 }
650 void setVar(uint32_t index, int64_t v) const {
651 setVar(index, &v, sizeof(v));
652 }
653 void setVar(uint32_t index, bool v) const {
654 setVar(index, &v, sizeof(v));
655 }
656
657public:
658 class FieldBase {
659 protected:
660 sp<const Element> mElement;
661 sp<Allocation> mAllocation;
662
663 void init(sp<RS> rs, uint32_t dimx, uint32_t usages = 0);
664
665 public:
666 sp<const Element> getElement() {
667 return mElement;
668 }
669
670 sp<const Type> getType() {
671 return mAllocation->getType();
672 }
673
674 sp<const Allocation> getAllocation() {
675 return mAllocation;
676 }
677
678 //void updateAllocation();
679 };
680};
681
682class ScriptC : public Script {
683protected:
684 ScriptC(sp<RS> rs,
685 const void *codeTxt, size_t codeLength,
686 const char *cachedName, size_t cachedNameLength,
687 const char *cacheDir, size_t cacheDirLength);
688
689};
690
Tim Murray7f0d5682012-11-08 16:35:24 -0800691class ScriptIntrinsic : public Script {
692 protected:
Tim Murray10913a52013-08-20 17:19:47 -0700693 sp<const Element> mElement;
Tim Murray3cd44af2012-11-14 11:25:27 -0800694 ScriptIntrinsic(sp<RS> rs, int id, sp<const Element> e);
Tim Murrayb27b1812013-08-05 14:00:40 -0700695 virtual ~ScriptIntrinsic();
Tim Murray7f0d5682012-11-08 16:35:24 -0800696};
697
Tim Murray89daad62013-07-29 14:30:02 -0700698class ScriptIntrinsic3DLUT : public ScriptIntrinsic {
Tim Murray21fa7a02013-08-15 16:25:03 -0700699 private:
Tim Murray89daad62013-07-29 14:30:02 -0700700 ScriptIntrinsic3DLUT(sp<RS> rs, sp<const Element> e);
Tim Murray21fa7a02013-08-15 16:25:03 -0700701 public:
702 static sp<ScriptIntrinsic3DLUT> create(sp<RS> rs, sp<const Element> e);
Tim Murray89daad62013-07-29 14:30:02 -0700703 void forEach(sp<Allocation> ain, sp<Allocation> aout);
704 void setLUT(sp<Allocation> lut);
705};
706
Tim Murray7f0d5682012-11-08 16:35:24 -0800707class ScriptIntrinsicBlend : public ScriptIntrinsic {
Tim Murray21fa7a02013-08-15 16:25:03 -0700708 private:
Tim Murray89daad62013-07-29 14:30:02 -0700709 ScriptIntrinsicBlend(sp<RS> rs, sp<const Element> e);
Tim Murray21fa7a02013-08-15 16:25:03 -0700710 public:
711 static sp<ScriptIntrinsicBlend> create(sp<RS> rs, sp<const Element> e);
Tim Murray7f0d5682012-11-08 16:35:24 -0800712 void blendClear(sp<Allocation> in, sp<Allocation> out);
713 void blendSrc(sp<Allocation> in, sp<Allocation> out);
714 void blendDst(sp<Allocation> in, sp<Allocation> out);
715 void blendSrcOver(sp<Allocation> in, sp<Allocation> out);
716 void blendDstOver(sp<Allocation> in, sp<Allocation> out);
717 void blendSrcIn(sp<Allocation> in, sp<Allocation> out);
718 void blendDstIn(sp<Allocation> in, sp<Allocation> out);
719 void blendSrcOut(sp<Allocation> in, sp<Allocation> out);
720 void blendDstOut(sp<Allocation> in, sp<Allocation> out);
721 void blendSrcAtop(sp<Allocation> in, sp<Allocation> out);
722 void blendDstAtop(sp<Allocation> in, sp<Allocation> out);
723 void blendXor(sp<Allocation> in, sp<Allocation> out);
724 void blendMultiply(sp<Allocation> in, sp<Allocation> out);
725 void blendAdd(sp<Allocation> in, sp<Allocation> out);
726 void blendSubtract(sp<Allocation> in, sp<Allocation> out);
727};
Tim Murray84bf2b82012-10-31 16:03:16 -0700728
Tim Murray8f1e60c2012-11-13 12:25:11 -0800729class ScriptIntrinsicBlur : public ScriptIntrinsic {
Tim Murray21fa7a02013-08-15 16:25:03 -0700730 private:
Tim Murray89daad62013-07-29 14:30:02 -0700731 ScriptIntrinsicBlur(sp<RS> rs, sp<const Element> e);
Tim Murray21fa7a02013-08-15 16:25:03 -0700732 public:
733 static sp<ScriptIntrinsicBlur> create(sp<RS> rs, sp<const Element> e);
734 void setInput(sp<Allocation> in);
735 void forEach(sp<Allocation> out);
Tim Murray8f1e60c2012-11-13 12:25:11 -0800736 void setRadius(float radius);
737};
738
Tim Murray89daad62013-07-29 14:30:02 -0700739class ScriptIntrinsicColorMatrix : public ScriptIntrinsic {
Tim Murray21fa7a02013-08-15 16:25:03 -0700740 private:
Tim Murray89daad62013-07-29 14:30:02 -0700741 ScriptIntrinsicColorMatrix(sp<RS> rs, sp<const Element> e);
Tim Murray21fa7a02013-08-15 16:25:03 -0700742 public:
Tim Murrayaae73c92013-09-03 17:05:46 -0700743 static sp<ScriptIntrinsicColorMatrix> create(sp<RS> rs);
Tim Murray89daad62013-07-29 14:30:02 -0700744 void forEach(sp<Allocation> in, sp<Allocation> out);
Tim Murray10913a52013-08-20 17:19:47 -0700745 void setAdd(float* add);
Tim Murray89daad62013-07-29 14:30:02 -0700746 void setColorMatrix3(float* m);
747 void setColorMatrix4(float* m);
748 void setGreyscale();
749 void setRGBtoYUV();
750 void setYUVtoRGB();
751};
752
753class ScriptIntrinsicConvolve3x3 : public ScriptIntrinsic {
Tim Murray21fa7a02013-08-15 16:25:03 -0700754 private:
Tim Murray89daad62013-07-29 14:30:02 -0700755 ScriptIntrinsicConvolve3x3(sp<RS> rs, sp<const Element> e);
Tim Murray21fa7a02013-08-15 16:25:03 -0700756 public:
757 static sp<ScriptIntrinsicConvolve3x3> create(sp<RS> rs, sp<const Element> e);
Tim Murray89daad62013-07-29 14:30:02 -0700758 void setInput(sp<Allocation> in);
759 void forEach(sp<Allocation> out);
760 void setCoefficients(float* v);
761};
762
763class ScriptIntrinsicConvolve5x5 : public ScriptIntrinsic {
Tim Murray21fa7a02013-08-15 16:25:03 -0700764 private:
Tim Murray89daad62013-07-29 14:30:02 -0700765 ScriptIntrinsicConvolve5x5(sp<RS> rs, sp<const Element> e);
Tim Murray21fa7a02013-08-15 16:25:03 -0700766 public:
767 static sp<ScriptIntrinsicConvolve5x5> create(sp<RS> rs, sp<const Element> e);
Tim Murray89daad62013-07-29 14:30:02 -0700768 void setInput(sp<Allocation> in);
769 void forEach(sp<Allocation> out);
770 void setCoefficients(float* v);
771};
772
Tim Murrayb27b1812013-08-05 14:00:40 -0700773class ScriptIntrinsicHistogram : public ScriptIntrinsic {
Tim Murray21fa7a02013-08-15 16:25:03 -0700774 private:
Tim Murrayb27b1812013-08-05 14:00:40 -0700775 ScriptIntrinsicHistogram(sp<RS> rs, sp<const Element> e);
Tim Murray10913a52013-08-20 17:19:47 -0700776 sp<Allocation> mOut;
Tim Murray21fa7a02013-08-15 16:25:03 -0700777 public:
Tim Murray10913a52013-08-20 17:19:47 -0700778 static sp<ScriptIntrinsicHistogram> create(sp<RS> rs);
Tim Murrayb27b1812013-08-05 14:00:40 -0700779 void setOutput(sp<Allocation> aout);
780 void setDotCoefficients(float r, float g, float b, float a);
781 void forEach(sp<Allocation> ain);
782 void forEach_dot(sp<Allocation> ain);
783};
784
785class ScriptIntrinsicLUT : public ScriptIntrinsic {
786 private:
787 sp<Allocation> LUT;
788 bool mDirty;
789 unsigned char mCache[1024];
Tim Murray2acce992013-08-28 14:23:31 -0700790 void setTable(unsigned int offset, unsigned char base, unsigned int length, unsigned char* lutValues);
Tim Murray21fa7a02013-08-15 16:25:03 -0700791 ScriptIntrinsicLUT(sp<RS> rs, sp<const Element> e);
Tim Murrayb27b1812013-08-05 14:00:40 -0700792
Tim Murray89daad62013-07-29 14:30:02 -0700793 public:
Tim Murray21fa7a02013-08-15 16:25:03 -0700794 static sp<ScriptIntrinsicLUT> create(sp<RS> rs, sp<const Element> e);
Tim Murray89daad62013-07-29 14:30:02 -0700795 void forEach(sp<Allocation> ain, sp<Allocation> aout);
Tim Murray2acce992013-08-28 14:23:31 -0700796 void setRed(unsigned char base, unsigned int length, unsigned char* lutValues);
797 void setGreen(unsigned char base, unsigned int length, unsigned char* lutValues);
798 void setBlue(unsigned char base, unsigned int length, unsigned char* lutValues);
799 void setAlpha(unsigned char base, unsigned int length, unsigned char* lutValues);
Tim Murrayb27b1812013-08-05 14:00:40 -0700800 virtual ~ScriptIntrinsicLUT();
801};
802
Tim Murray89daad62013-07-29 14:30:02 -0700803class ScriptIntrinsicYuvToRGB : public ScriptIntrinsic {
Tim Murray21fa7a02013-08-15 16:25:03 -0700804 private:
Tim Murrayb27b1812013-08-05 14:00:40 -0700805 ScriptIntrinsicYuvToRGB(sp<RS> rs, sp<const Element> e);
Tim Murray21fa7a02013-08-15 16:25:03 -0700806 public:
807 static sp<ScriptIntrinsicYuvToRGB> create(sp<RS> rs, sp<const Element> e);
Tim Murrayb27b1812013-08-05 14:00:40 -0700808 void setInput(sp<Allocation> in);
809 void forEach(sp<Allocation> out);
Tim Murray89daad62013-07-29 14:30:02 -0700810
811};
Tim Murrayb27b1812013-08-05 14:00:40 -0700812
Tim Murray89daad62013-07-29 14:30:02 -0700813
Tim Murray729b6fe2013-07-23 16:20:42 -0700814 class Sampler : public BaseObj {
815 private:
816 Sampler(sp<RS> rs, void* id);
817 RsSamplerValue mMin;
818 RsSamplerValue mMag;
819 RsSamplerValue mWrapS;
820 RsSamplerValue mWrapT;
821 RsSamplerValue mWrapR;
822 float mAniso;
823
824 public:
825 static sp<Sampler> create(sp<RS> rs, RsSamplerValue min, RsSamplerValue mag, RsSamplerValue wrapS, RsSamplerValue wrapT, float anisotropy);
826
827 RsSamplerValue getMinification();
828 RsSamplerValue getMagnification();
829 RsSamplerValue getWrapS();
830 RsSamplerValue getWrapT();
831 float getAnisotropy();
832
833 sp<const Sampler> CLAMP_NEAREST(sp<RS> rs);
834 sp<const Sampler> CLAMP_LINEAR(sp<RS> rs);
835 sp<const Sampler> CLAMP_LINEAR_MIP_LINEAR(sp<RS> rs);
836 sp<const Sampler> WRAP_NEAREST(sp<RS> rs);
837 sp<const Sampler> WRAP_LINEAR(sp<RS> rs);
838 sp<const Sampler> WRAP_LINEAR_MIP_LINEAR(sp<RS> rs);
839 sp<const Sampler> MIRRORED_REPEAT_NEAREST(sp<RS> rs);
840 sp<const Sampler> MIRRORED_REPEAT_LINEAR(sp<RS> rs);
841 sp<const Sampler> MIRRORED_REPEAT_LINEAR_MIP_LINEAR(sp<RS> rs);
842
843};
844
Tim Murray84bf2b82012-10-31 16:03:16 -0700845}
Tim Murray7f0d5682012-11-08 16:35:24 -0800846
Tim Murray84bf2b82012-10-31 16:03:16 -0700847}
848
849#endif