Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 1 | |
| 2 | /* |
| 3 | * Copyright (C) 2009 The Android Open Source Project |
| 4 | * |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at |
| 8 | * |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | */ |
| 17 | |
| 18 | #include "rsContext.h" |
Alex Sakhartchouk | 4edf030 | 2012-03-09 10:47:27 -0800 | [diff] [blame] | 19 | #include "rsAdapter.h" |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 20 | |
| 21 | using namespace android; |
| 22 | using namespace android::renderscript; |
| 23 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 24 | Adapter1D::Adapter1D(Context *rsc) : ObjectBase(rsc) { |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 25 | reset(); |
| 26 | } |
| 27 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 28 | Adapter1D::Adapter1D(Context *rsc, Allocation *a) : ObjectBase(rsc) { |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 29 | reset(); |
| 30 | setAllocation(a); |
| 31 | } |
| 32 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 33 | void Adapter1D::reset() { |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 34 | mY = 0; |
| 35 | mZ = 0; |
| 36 | mLOD = 0; |
| 37 | mFace = 0; |
| 38 | } |
| 39 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 40 | void * Adapter1D::getElement(uint32_t x) { |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 41 | rsAssert(mAllocation.get()); |
| 42 | rsAssert(mAllocation->getPtr()); |
| 43 | rsAssert(mAllocation->getType()); |
| 44 | uint8_t * ptr = static_cast<uint8_t *>(mAllocation->getPtr()); |
| 45 | ptr += mAllocation->getType()->getLODOffset(mLOD, x, mY); |
| 46 | return ptr; |
| 47 | } |
| 48 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 49 | void Adapter1D::subData(uint32_t xoff, uint32_t count, const void *data) { |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 50 | if (mAllocation.get() && mAllocation.get()->getType()) { |
| 51 | void *ptr = getElement(xoff); |
| 52 | count *= mAllocation.get()->getType()->getElementSizeBytes(); |
| 53 | memcpy(ptr, data, count); |
| 54 | } |
| 55 | } |
| 56 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 57 | void Adapter1D::data(const void *data) { |
Jason Sams | d3c8de2 | 2009-08-03 16:03:08 -0700 | [diff] [blame] | 58 | memcpy(getElement(0), |
| 59 | data, |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 60 | mAllocation.get()->getType()->getSizeBytes()); |
| 61 | } |
| 62 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 63 | void Adapter1D::serialize(OStream *stream) const { |
Alex Sakhartchouk | fb6b614 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 64 | } |
| 65 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 66 | Adapter1D *Adapter1D::createFromStream(Context *rsc, IStream *stream) { |
Alex Sakhartchouk | fb6b614 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 67 | return NULL; |
| 68 | } |
| 69 | |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 70 | namespace android { |
| 71 | namespace renderscript { |
| 72 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 73 | RsAdapter1D rsi_Adapter1DCreate(Context *rsc) { |
Jason Sams | e514b45 | 2009-09-25 14:51:22 -0700 | [diff] [blame] | 74 | Adapter1D *a = new Adapter1D(rsc); |
Jason Sams | 9397e30 | 2009-08-27 20:23:34 -0700 | [diff] [blame] | 75 | a->incUserRef(); |
Jason Sams | d3c8de2 | 2009-08-03 16:03:08 -0700 | [diff] [blame] | 76 | return a; |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 77 | } |
| 78 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 79 | void rsi_Adapter1DBindAllocation(Context *rsc, RsAdapter1D va, RsAllocation valloc) { |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 80 | Adapter1D * a = static_cast<Adapter1D *>(va); |
| 81 | Allocation * alloc = static_cast<Allocation *>(valloc); |
| 82 | a->setAllocation(alloc); |
| 83 | } |
| 84 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 85 | void rsi_Adapter1DSetConstraint(Context *rsc, RsAdapter1D va, RsDimension dim, uint32_t value) { |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 86 | Adapter1D * a = static_cast<Adapter1D *>(va); |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 87 | switch (dim) { |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 88 | case RS_DIMENSION_X: |
| 89 | rsAssert(!"Cannot contrain X in an 1D adapter"); |
| 90 | return; |
| 91 | case RS_DIMENSION_Y: |
| 92 | a->setY(value); |
| 93 | break; |
| 94 | case RS_DIMENSION_Z: |
| 95 | a->setZ(value); |
| 96 | break; |
| 97 | case RS_DIMENSION_LOD: |
| 98 | a->setLOD(value); |
| 99 | break; |
| 100 | case RS_DIMENSION_FACE: |
| 101 | a->setFace(value); |
| 102 | break; |
| 103 | default: |
| 104 | rsAssert(!"Unimplemented constraint"); |
| 105 | return; |
| 106 | } |
| 107 | } |
| 108 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 109 | void rsi_Adapter1DSubData(Context *rsc, RsAdapter1D va, uint32_t xoff, uint32_t count, const void *data) { |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 110 | Adapter1D * a = static_cast<Adapter1D *>(va); |
| 111 | a->subData(xoff, count, data); |
| 112 | } |
| 113 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 114 | void rsi_Adapter1DData(Context *rsc, RsAdapter1D va, const void *data) { |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 115 | Adapter1D * a = static_cast<Adapter1D *>(va); |
| 116 | a->data(data); |
| 117 | } |
| 118 | |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | ////////////////////////// |
| 123 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 124 | Adapter2D::Adapter2D(Context *rsc) : ObjectBase(rsc) { |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 125 | reset(); |
| 126 | } |
| 127 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 128 | Adapter2D::Adapter2D(Context *rsc, Allocation *a) : ObjectBase(rsc) { |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 129 | reset(); |
| 130 | setAllocation(a); |
| 131 | } |
| 132 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 133 | void Adapter2D::reset() { |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 134 | mZ = 0; |
| 135 | mLOD = 0; |
| 136 | mFace = 0; |
| 137 | } |
| 138 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 139 | void * Adapter2D::getElement(uint32_t x, uint32_t y) const { |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 140 | rsAssert(mAllocation.get()); |
| 141 | rsAssert(mAllocation->getPtr()); |
| 142 | rsAssert(mAllocation->getType()); |
Alex Sakhartchouk | 84e4027 | 2010-11-18 15:22:43 -0800 | [diff] [blame] | 143 | if (mFace != 0 && !mAllocation->getType()->getDimFaces()) { |
Steve Block | af12ac6 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 144 | ALOGE("Adapter wants cubemap face, but allocation has none"); |
Alex Sakhartchouk | 84e4027 | 2010-11-18 15:22:43 -0800 | [diff] [blame] | 145 | return NULL; |
| 146 | } |
| 147 | |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 148 | uint8_t * ptr = static_cast<uint8_t *>(mAllocation->getPtr()); |
| 149 | ptr += mAllocation->getType()->getLODOffset(mLOD, x, y); |
Alex Sakhartchouk | 84e4027 | 2010-11-18 15:22:43 -0800 | [diff] [blame] | 150 | |
| 151 | if (mFace != 0) { |
| 152 | uint32_t totalSizeBytes = mAllocation->getType()->getSizeBytes(); |
| 153 | uint32_t faceOffset = totalSizeBytes / 6; |
| 154 | ptr += faceOffset * mFace; |
| 155 | } |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 156 | return ptr; |
| 157 | } |
| 158 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 159 | void Adapter2D::subData(uint32_t xoff, uint32_t yoff, uint32_t w, uint32_t h, const void *data) { |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 160 | rsAssert(mAllocation.get()); |
| 161 | rsAssert(mAllocation->getPtr()); |
| 162 | rsAssert(mAllocation->getType()); |
| 163 | |
| 164 | uint32_t eSize = mAllocation.get()->getType()->getElementSizeBytes(); |
| 165 | uint32_t lineSize = eSize * w; |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 166 | |
| 167 | const uint8_t *src = static_cast<const uint8_t *>(data); |
| 168 | for (uint32_t line=yoff; line < (yoff+h); line++) { |
| 169 | memcpy(getElement(xoff, line), src, lineSize); |
| 170 | src += lineSize; |
| 171 | } |
| 172 | } |
| 173 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 174 | void Adapter2D::data(const void *data) { |
Jason Sams | d3c8de2 | 2009-08-03 16:03:08 -0700 | [diff] [blame] | 175 | memcpy(getElement(0,0), |
| 176 | data, |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 177 | mAllocation.get()->getType()->getSizeBytes()); |
| 178 | } |
| 179 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 180 | void Adapter2D::serialize(OStream *stream) const { |
Alex Sakhartchouk | fb6b614 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 181 | } |
| 182 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 183 | Adapter2D *Adapter2D::createFromStream(Context *rsc, IStream *stream) { |
Alex Sakhartchouk | fb6b614 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 184 | return NULL; |
| 185 | } |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 186 | |
| 187 | |
| 188 | namespace android { |
| 189 | namespace renderscript { |
| 190 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 191 | RsAdapter2D rsi_Adapter2DCreate(Context *rsc) { |
Jason Sams | e514b45 | 2009-09-25 14:51:22 -0700 | [diff] [blame] | 192 | Adapter2D *a = new Adapter2D(rsc); |
Jason Sams | 9397e30 | 2009-08-27 20:23:34 -0700 | [diff] [blame] | 193 | a->incUserRef(); |
Jason Sams | d3c8de2 | 2009-08-03 16:03:08 -0700 | [diff] [blame] | 194 | return a; |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 195 | } |
| 196 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 197 | void rsi_Adapter2DBindAllocation(Context *rsc, RsAdapter2D va, RsAllocation valloc) { |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 198 | Adapter2D * a = static_cast<Adapter2D *>(va); |
| 199 | Allocation * alloc = static_cast<Allocation *>(valloc); |
| 200 | a->setAllocation(alloc); |
| 201 | } |
| 202 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 203 | void rsi_Adapter2DSetConstraint(Context *rsc, RsAdapter2D va, RsDimension dim, uint32_t value) { |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 204 | Adapter2D * a = static_cast<Adapter2D *>(va); |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 205 | switch (dim) { |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 206 | case RS_DIMENSION_X: |
| 207 | rsAssert(!"Cannot contrain X in an 2D adapter"); |
| 208 | return; |
| 209 | case RS_DIMENSION_Y: |
| 210 | rsAssert(!"Cannot contrain Y in an 2D adapter"); |
| 211 | break; |
| 212 | case RS_DIMENSION_Z: |
| 213 | a->setZ(value); |
| 214 | break; |
| 215 | case RS_DIMENSION_LOD: |
| 216 | a->setLOD(value); |
| 217 | break; |
| 218 | case RS_DIMENSION_FACE: |
| 219 | a->setFace(value); |
| 220 | break; |
| 221 | default: |
| 222 | rsAssert(!"Unimplemented constraint"); |
| 223 | return; |
| 224 | } |
| 225 | } |
| 226 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 227 | void rsi_Adapter2DData(Context *rsc, RsAdapter2D va, const void *data) { |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 228 | Adapter2D * a = static_cast<Adapter2D *>(va); |
| 229 | a->data(data); |
| 230 | } |
| 231 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 232 | void rsi_Adapter2DSubData(Context *rsc, RsAdapter2D va, uint32_t xoff, uint32_t yoff, uint32_t w, uint32_t h, const void *data) { |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 233 | Adapter2D * a = static_cast<Adapter2D *>(va); |
| 234 | a->subData(xoff, yoff, w, h, data); |
| 235 | } |
| 236 | |
| 237 | } |
| 238 | } |