Jason Sams | d19f10d | 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" |
| 19 | |
| 20 | using namespace android; |
| 21 | using namespace android::renderscript; |
| 22 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 23 | Adapter1D::Adapter1D(Context *rsc) : ObjectBase(rsc) { |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 24 | reset(); |
| 25 | } |
| 26 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 27 | Adapter1D::Adapter1D(Context *rsc, Allocation *a) : ObjectBase(rsc) { |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 28 | reset(); |
| 29 | setAllocation(a); |
| 30 | } |
| 31 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 32 | void Adapter1D::reset() { |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 33 | mY = 0; |
| 34 | mZ = 0; |
| 35 | mLOD = 0; |
| 36 | mFace = 0; |
| 37 | } |
| 38 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 39 | void * Adapter1D::getElement(uint32_t x) { |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 40 | rsAssert(mAllocation.get()); |
| 41 | rsAssert(mAllocation->getPtr()); |
| 42 | rsAssert(mAllocation->getType()); |
| 43 | uint8_t * ptr = static_cast<uint8_t *>(mAllocation->getPtr()); |
| 44 | ptr += mAllocation->getType()->getLODOffset(mLOD, x, mY); |
| 45 | return ptr; |
| 46 | } |
| 47 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 48 | void Adapter1D::subData(uint32_t xoff, uint32_t count, const void *data) { |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 49 | if (mAllocation.get() && mAllocation.get()->getType()) { |
| 50 | void *ptr = getElement(xoff); |
| 51 | count *= mAllocation.get()->getType()->getElementSizeBytes(); |
| 52 | memcpy(ptr, data, count); |
| 53 | } |
| 54 | } |
| 55 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 56 | void Adapter1D::data(const void *data) { |
Jason Sams | bd1c3ad | 2009-08-03 16:03:08 -0700 | [diff] [blame] | 57 | memcpy(getElement(0), |
| 58 | data, |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 59 | mAllocation.get()->getType()->getSizeBytes()); |
| 60 | } |
| 61 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 62 | void Adapter1D::serialize(OStream *stream) const { |
Alex Sakhartchouk | aa7d288 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 63 | } |
| 64 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 65 | Adapter1D *Adapter1D::createFromStream(Context *rsc, IStream *stream) { |
Alex Sakhartchouk | aa7d288 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 66 | return NULL; |
| 67 | } |
| 68 | |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 69 | namespace android { |
| 70 | namespace renderscript { |
| 71 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 72 | RsAdapter1D rsi_Adapter1DCreate(Context *rsc) { |
Jason Sams | a9e7a05 | 2009-09-25 14:51:22 -0700 | [diff] [blame] | 73 | Adapter1D *a = new Adapter1D(rsc); |
Jason Sams | 07ae406 | 2009-08-27 20:23:34 -0700 | [diff] [blame] | 74 | a->incUserRef(); |
Jason Sams | bd1c3ad | 2009-08-03 16:03:08 -0700 | [diff] [blame] | 75 | return a; |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 76 | } |
| 77 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 78 | void rsi_Adapter1DBindAllocation(Context *rsc, RsAdapter1D va, RsAllocation valloc) { |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 79 | Adapter1D * a = static_cast<Adapter1D *>(va); |
| 80 | Allocation * alloc = static_cast<Allocation *>(valloc); |
| 81 | a->setAllocation(alloc); |
| 82 | } |
| 83 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 84 | void rsi_Adapter1DSetConstraint(Context *rsc, RsAdapter1D va, RsDimension dim, uint32_t value) { |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 85 | Adapter1D * a = static_cast<Adapter1D *>(va); |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 86 | switch (dim) { |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 87 | case RS_DIMENSION_X: |
| 88 | rsAssert(!"Cannot contrain X in an 1D adapter"); |
| 89 | return; |
| 90 | case RS_DIMENSION_Y: |
| 91 | a->setY(value); |
| 92 | break; |
| 93 | case RS_DIMENSION_Z: |
| 94 | a->setZ(value); |
| 95 | break; |
| 96 | case RS_DIMENSION_LOD: |
| 97 | a->setLOD(value); |
| 98 | break; |
| 99 | case RS_DIMENSION_FACE: |
| 100 | a->setFace(value); |
| 101 | break; |
| 102 | default: |
| 103 | rsAssert(!"Unimplemented constraint"); |
| 104 | return; |
| 105 | } |
| 106 | } |
| 107 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 108 | void rsi_Adapter1DSubData(Context *rsc, RsAdapter1D va, uint32_t xoff, uint32_t count, const void *data) { |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 109 | Adapter1D * a = static_cast<Adapter1D *>(va); |
| 110 | a->subData(xoff, count, data); |
| 111 | } |
| 112 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 113 | void rsi_Adapter1DData(Context *rsc, RsAdapter1D va, const void *data) { |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 114 | Adapter1D * a = static_cast<Adapter1D *>(va); |
| 115 | a->data(data); |
| 116 | } |
| 117 | |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | ////////////////////////// |
| 122 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 123 | Adapter2D::Adapter2D(Context *rsc) : ObjectBase(rsc) { |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 124 | reset(); |
| 125 | } |
| 126 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 127 | Adapter2D::Adapter2D(Context *rsc, Allocation *a) : ObjectBase(rsc) { |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 128 | reset(); |
| 129 | setAllocation(a); |
| 130 | } |
| 131 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 132 | void Adapter2D::reset() { |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 133 | mZ = 0; |
| 134 | mLOD = 0; |
| 135 | mFace = 0; |
| 136 | } |
| 137 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 138 | void * Adapter2D::getElement(uint32_t x, uint32_t y) const { |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 139 | rsAssert(mAllocation.get()); |
| 140 | rsAssert(mAllocation->getPtr()); |
| 141 | rsAssert(mAllocation->getType()); |
Alex Sakhartchouk | 67f2e44 | 2010-11-18 15:22:43 -0800 | [diff] [blame] | 142 | if (mFace != 0 && !mAllocation->getType()->getDimFaces()) { |
| 143 | LOGE("Adapter wants cubemap face, but allocation has none"); |
| 144 | return NULL; |
| 145 | } |
| 146 | |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 147 | uint8_t * ptr = static_cast<uint8_t *>(mAllocation->getPtr()); |
| 148 | ptr += mAllocation->getType()->getLODOffset(mLOD, x, y); |
Alex Sakhartchouk | 67f2e44 | 2010-11-18 15:22:43 -0800 | [diff] [blame] | 149 | |
| 150 | if (mFace != 0) { |
| 151 | uint32_t totalSizeBytes = mAllocation->getType()->getSizeBytes(); |
| 152 | uint32_t faceOffset = totalSizeBytes / 6; |
| 153 | ptr += faceOffset * mFace; |
| 154 | } |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 155 | return ptr; |
| 156 | } |
| 157 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 158 | void Adapter2D::subData(uint32_t xoff, uint32_t yoff, uint32_t w, uint32_t h, const void *data) { |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 159 | rsAssert(mAllocation.get()); |
| 160 | rsAssert(mAllocation->getPtr()); |
| 161 | rsAssert(mAllocation->getType()); |
| 162 | |
| 163 | uint32_t eSize = mAllocation.get()->getType()->getElementSizeBytes(); |
| 164 | uint32_t lineSize = eSize * w; |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 165 | |
| 166 | const uint8_t *src = static_cast<const uint8_t *>(data); |
| 167 | for (uint32_t line=yoff; line < (yoff+h); line++) { |
| 168 | memcpy(getElement(xoff, line), src, lineSize); |
| 169 | src += lineSize; |
| 170 | } |
| 171 | } |
| 172 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 173 | void Adapter2D::data(const void *data) { |
Jason Sams | bd1c3ad | 2009-08-03 16:03:08 -0700 | [diff] [blame] | 174 | memcpy(getElement(0,0), |
| 175 | data, |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 176 | mAllocation.get()->getType()->getSizeBytes()); |
| 177 | } |
| 178 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 179 | void Adapter2D::serialize(OStream *stream) const { |
Alex Sakhartchouk | aa7d288 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 180 | } |
| 181 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 182 | Adapter2D *Adapter2D::createFromStream(Context *rsc, IStream *stream) { |
Alex Sakhartchouk | aa7d288 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 183 | return NULL; |
| 184 | } |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 185 | |
| 186 | |
| 187 | namespace android { |
| 188 | namespace renderscript { |
| 189 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 190 | RsAdapter2D rsi_Adapter2DCreate(Context *rsc) { |
Jason Sams | a9e7a05 | 2009-09-25 14:51:22 -0700 | [diff] [blame] | 191 | Adapter2D *a = new Adapter2D(rsc); |
Jason Sams | 07ae406 | 2009-08-27 20:23:34 -0700 | [diff] [blame] | 192 | a->incUserRef(); |
Jason Sams | bd1c3ad | 2009-08-03 16:03:08 -0700 | [diff] [blame] | 193 | return a; |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 194 | } |
| 195 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 196 | void rsi_Adapter2DBindAllocation(Context *rsc, RsAdapter2D va, RsAllocation valloc) { |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 197 | Adapter2D * a = static_cast<Adapter2D *>(va); |
| 198 | Allocation * alloc = static_cast<Allocation *>(valloc); |
| 199 | a->setAllocation(alloc); |
| 200 | } |
| 201 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 202 | void rsi_Adapter2DSetConstraint(Context *rsc, RsAdapter2D va, RsDimension dim, uint32_t value) { |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 203 | Adapter2D * a = static_cast<Adapter2D *>(va); |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 204 | switch (dim) { |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 205 | case RS_DIMENSION_X: |
| 206 | rsAssert(!"Cannot contrain X in an 2D adapter"); |
| 207 | return; |
| 208 | case RS_DIMENSION_Y: |
| 209 | rsAssert(!"Cannot contrain Y in an 2D adapter"); |
| 210 | break; |
| 211 | case RS_DIMENSION_Z: |
| 212 | a->setZ(value); |
| 213 | break; |
| 214 | case RS_DIMENSION_LOD: |
| 215 | a->setLOD(value); |
| 216 | break; |
| 217 | case RS_DIMENSION_FACE: |
| 218 | a->setFace(value); |
| 219 | break; |
| 220 | default: |
| 221 | rsAssert(!"Unimplemented constraint"); |
| 222 | return; |
| 223 | } |
| 224 | } |
| 225 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 226 | void rsi_Adapter2DData(Context *rsc, RsAdapter2D va, const void *data) { |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 227 | Adapter2D * a = static_cast<Adapter2D *>(va); |
| 228 | a->data(data); |
| 229 | } |
| 230 | |
Alex Sakhartchouk | ed9f210 | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 231 | void rsi_Adapter2DSubData(Context *rsc, RsAdapter2D va, uint32_t xoff, uint32_t yoff, uint32_t w, uint32_t h, const void *data) { |
Jason Sams | d19f10d | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 232 | Adapter2D * a = static_cast<Adapter2D *>(va); |
| 233 | a->subData(xoff, yoff, w, h, data); |
| 234 | } |
| 235 | |
| 236 | } |
| 237 | } |