Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 1 | /* |
Stephen Hines | 6ae039b | 2012-01-18 18:46:27 -0800 | [diff] [blame] | 2 | * Copyright (C) 2009-2012 The Android Open Source Project |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 3 | * |
| 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 | */ |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 16 | |
Alex Sakhartchouk | 77d9f4b | 2011-01-31 14:53:24 -0800 | [diff] [blame] | 17 | #include "rsContext.h" |
Alex Sakhartchouk | 4edf030 | 2012-03-09 10:47:27 -0800 | [diff] [blame^] | 18 | #include "rsAllocation.h" |
| 19 | #include "rsAdapter.h" |
Jason Sams | eb4fe18 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 20 | #include "rs_hal.h" |
| 21 | |
Jason Sams | 7ac2a4d | 2012-02-15 12:04:24 -0800 | [diff] [blame] | 22 | #include "system/window.h" |
Alex Sakhartchouk | 39f2ef6 | 2010-10-11 12:35:15 -0700 | [diff] [blame] | 23 | |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 24 | using namespace android; |
| 25 | using namespace android::renderscript; |
| 26 | |
Alex Sakhartchouk | a2aab8b | 2010-12-15 09:59:58 -0800 | [diff] [blame] | 27 | Allocation::Allocation(Context *rsc, const Type *type, uint32_t usages, |
Jason Sams | 179e9a4 | 2011-11-23 15:02:15 -0800 | [diff] [blame] | 28 | RsAllocationMipmapControl mc, void * ptr) |
Alex Sakhartchouk | a2aab8b | 2010-12-15 09:59:58 -0800 | [diff] [blame] | 29 | : ObjectBase(rsc) { |
Jason Sams | fa84da2 | 2010-03-01 15:31:04 -0800 | [diff] [blame] | 30 | |
Jason Sams | eb4fe18 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 31 | memset(&mHal, 0, sizeof(mHal)); |
| 32 | mHal.state.mipmapControl = RS_ALLOCATION_MIPMAP_NONE; |
Jason Sams | bad8074 | 2011-03-16 16:29:28 -0700 | [diff] [blame] | 33 | mHal.state.usageFlags = usages; |
| 34 | mHal.state.mipmapControl = mc; |
Jason Sams | 179e9a4 | 2011-11-23 15:02:15 -0800 | [diff] [blame] | 35 | mHal.state.usrPtr = ptr; |
Jason Sams | 366c9c8 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 36 | |
Alex Sakhartchouk | 064aa7e | 2011-10-18 10:54:29 -0700 | [diff] [blame] | 37 | setType(type); |
Jason Sams | bad8074 | 2011-03-16 16:29:28 -0700 | [diff] [blame] | 38 | updateCache(); |
| 39 | } |
Jason Sams | fa84da2 | 2010-03-01 15:31:04 -0800 | [diff] [blame] | 40 | |
Jason Sams | eb4fe18 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 41 | Allocation * Allocation::createAllocation(Context *rsc, const Type *type, uint32_t usages, |
Jason Sams | 179e9a4 | 2011-11-23 15:02:15 -0800 | [diff] [blame] | 42 | RsAllocationMipmapControl mc, void * ptr) { |
| 43 | Allocation *a = new Allocation(rsc, type, usages, mc, ptr); |
Jason Sams | eb4fe18 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 44 | |
| 45 | if (!rsc->mHal.funcs.allocation.init(rsc, a, type->getElement()->getHasReferences())) { |
| 46 | rsc->setError(RS_ERROR_FATAL_DRIVER, "Allocation::Allocation, alloc failure"); |
| 47 | delete a; |
| 48 | return NULL; |
| 49 | } |
Jason Sams | 7ac2a4d | 2012-02-15 12:04:24 -0800 | [diff] [blame] | 50 | |
Jason Sams | eb4fe18 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 51 | return a; |
| 52 | } |
| 53 | |
Jason Sams | bad8074 | 2011-03-16 16:29:28 -0700 | [diff] [blame] | 54 | void Allocation::updateCache() { |
Alex Sakhartchouk | 064aa7e | 2011-10-18 10:54:29 -0700 | [diff] [blame] | 55 | const Type *type = mHal.state.type; |
Jason Sams | bad8074 | 2011-03-16 16:29:28 -0700 | [diff] [blame] | 56 | mHal.state.dimensionX = type->getDimX(); |
| 57 | mHal.state.dimensionY = type->getDimY(); |
| 58 | mHal.state.dimensionZ = type->getDimZ(); |
| 59 | mHal.state.hasFaces = type->getDimFaces(); |
| 60 | mHal.state.hasMipmaps = type->getDimLOD(); |
| 61 | mHal.state.elementSizeBytes = type->getElementSizeBytes(); |
| 62 | mHal.state.hasReferences = mHal.state.type->getElement()->getHasReferences(); |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 63 | } |
| 64 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 65 | Allocation::~Allocation() { |
Jason Sams | c7cec1e | 2011-08-18 18:01:33 -0700 | [diff] [blame] | 66 | freeChildrenUnlocked(); |
Jason Sams | eb4fe18 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 67 | mRSC->mHal.funcs.allocation.destroy(mRSC, this); |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 68 | } |
| 69 | |
Jason Sams | 366c9c8 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 70 | void Allocation::syncAll(Context *rsc, RsAllocationUsageType src) { |
Jason Sams | eb4fe18 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 71 | rsc->mHal.funcs.allocation.syncAll(rsc, this, src); |
Jason Sams | cf4c7c9 | 2009-12-14 12:57:40 -0800 | [diff] [blame] | 72 | } |
| 73 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 74 | void Allocation::read(void *data) { |
Jason Sams | eb4fe18 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 75 | memcpy(data, getPtr(), mHal.state.type->getSizeBytes()); |
Jason Sams | e579df4 | 2009-08-10 14:55:26 -0700 | [diff] [blame] | 76 | } |
| 77 | |
Jason Sams | 4b45b89 | 2010-12-29 14:31:29 -0800 | [diff] [blame] | 78 | void Allocation::data(Context *rsc, uint32_t xoff, uint32_t lod, |
Stephen Hines | 6ae039b | 2012-01-18 18:46:27 -0800 | [diff] [blame] | 79 | uint32_t count, const void *data, size_t sizeBytes) { |
| 80 | const size_t eSize = mHal.state.type->getElementSizeBytes(); |
Jason Sams | 9397e30 | 2009-08-27 20:23:34 -0700 | [diff] [blame] | 81 | |
Jason Sams | eb4fe18 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 82 | if ((count * eSize) != sizeBytes) { |
Stephen Hines | 6ae039b | 2012-01-18 18:46:27 -0800 | [diff] [blame] | 83 | ALOGE("Allocation::subData called with mismatched size expected %zu, got %zu", |
Jason Sams | eb4fe18 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 84 | (count * eSize), sizeBytes); |
Jason Sams | bad8074 | 2011-03-16 16:29:28 -0700 | [diff] [blame] | 85 | mHal.state.type->dumpLOGV("type info"); |
Jason Sams | 9397e30 | 2009-08-27 20:23:34 -0700 | [diff] [blame] | 86 | return; |
| 87 | } |
Jason Sams | e3929c9 | 2010-08-09 18:13:33 -0700 | [diff] [blame] | 88 | |
Jason Sams | eb4fe18 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 89 | rsc->mHal.funcs.allocation.data1D(rsc, this, xoff, lod, count, data, sizeBytes); |
| 90 | sendDirty(rsc); |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 91 | } |
| 92 | |
Jason Sams | 4b45b89 | 2010-12-29 14:31:29 -0800 | [diff] [blame] | 93 | void Allocation::data(Context *rsc, uint32_t xoff, uint32_t yoff, uint32_t lod, RsAllocationCubemapFace face, |
Stephen Hines | 6ae039b | 2012-01-18 18:46:27 -0800 | [diff] [blame] | 94 | uint32_t w, uint32_t h, const void *data, size_t sizeBytes) { |
| 95 | const size_t eSize = mHal.state.elementSizeBytes; |
| 96 | const size_t lineSize = eSize * w; |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 97 | |
Steve Block | af12ac6 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 98 | //ALOGE("data2d %p, %i %i %i %i %i %i %p %i", this, xoff, yoff, lod, face, w, h, data, sizeBytes); |
Jason Sams | 9397e30 | 2009-08-27 20:23:34 -0700 | [diff] [blame] | 99 | |
Jason Sams | a237151 | 2011-01-12 13:28:37 -0800 | [diff] [blame] | 100 | if ((lineSize * h) != sizeBytes) { |
Stephen Hines | 6ae039b | 2012-01-18 18:46:27 -0800 | [diff] [blame] | 101 | ALOGE("Allocation size mismatch, expected %zu, got %zu", (lineSize * h), sizeBytes); |
Jason Sams | 9397e30 | 2009-08-27 20:23:34 -0700 | [diff] [blame] | 102 | rsAssert(!"Allocation::subData called with mismatched size"); |
| 103 | return; |
| 104 | } |
| 105 | |
Jason Sams | eb4fe18 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 106 | rsc->mHal.funcs.allocation.data2D(rsc, this, xoff, yoff, lod, face, w, h, data, sizeBytes); |
| 107 | sendDirty(rsc); |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 108 | } |
| 109 | |
Jason Sams | 236385b | 2011-01-12 14:53:25 -0800 | [diff] [blame] | 110 | void Allocation::data(Context *rsc, uint32_t xoff, uint32_t yoff, uint32_t zoff, |
| 111 | uint32_t lod, RsAllocationCubemapFace face, |
Stephen Hines | 6ae039b | 2012-01-18 18:46:27 -0800 | [diff] [blame] | 112 | uint32_t w, uint32_t h, uint32_t d, const void *data, size_t sizeBytes) { |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 113 | } |
| 114 | |
Jason Sams | 4b45b89 | 2010-12-29 14:31:29 -0800 | [diff] [blame] | 115 | void Allocation::elementData(Context *rsc, uint32_t x, const void *data, |
Stephen Hines | 6ae039b | 2012-01-18 18:46:27 -0800 | [diff] [blame] | 116 | uint32_t cIdx, size_t sizeBytes) { |
| 117 | size_t eSize = mHal.state.elementSizeBytes; |
Jason Sams | 5f0c84c | 2010-08-31 13:50:42 -0700 | [diff] [blame] | 118 | |
Jason Sams | bad8074 | 2011-03-16 16:29:28 -0700 | [diff] [blame] | 119 | if (cIdx >= mHal.state.type->getElement()->getFieldCount()) { |
Steve Block | af12ac6 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 120 | ALOGE("Error Allocation::subElementData component %i out of range.", cIdx); |
Jason Sams | 5f0c84c | 2010-08-31 13:50:42 -0700 | [diff] [blame] | 121 | rsc->setError(RS_ERROR_BAD_VALUE, "subElementData component out of range."); |
| 122 | return; |
| 123 | } |
| 124 | |
Jason Sams | bad8074 | 2011-03-16 16:29:28 -0700 | [diff] [blame] | 125 | if (x >= mHal.state.dimensionX) { |
Steve Block | af12ac6 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 126 | ALOGE("Error Allocation::subElementData X offset %i out of range.", x); |
Jason Sams | 5f0c84c | 2010-08-31 13:50:42 -0700 | [diff] [blame] | 127 | rsc->setError(RS_ERROR_BAD_VALUE, "subElementData X offset out of range."); |
| 128 | return; |
| 129 | } |
| 130 | |
Jason Sams | bad8074 | 2011-03-16 16:29:28 -0700 | [diff] [blame] | 131 | const Element * e = mHal.state.type->getElement()->getField(cIdx); |
Alex Sakhartchouk | 7694632 | 2012-02-02 09:47:26 -0800 | [diff] [blame] | 132 | uint32_t elemArraySize = mHal.state.type->getElement()->getFieldArraySize(cIdx); |
| 133 | if (sizeBytes != e->getSizeBytes() * elemArraySize) { |
Stephen Hines | 6ae039b | 2012-01-18 18:46:27 -0800 | [diff] [blame] | 134 | ALOGE("Error Allocation::subElementData data size %zu does not match field size %zu.", sizeBytes, e->getSizeBytes()); |
Jason Sams | 5f0c84c | 2010-08-31 13:50:42 -0700 | [diff] [blame] | 135 | rsc->setError(RS_ERROR_BAD_VALUE, "subElementData bad size."); |
| 136 | return; |
| 137 | } |
| 138 | |
Jason Sams | eb4fe18 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 139 | rsc->mHal.funcs.allocation.elementData1D(rsc, this, x, data, cIdx, sizeBytes); |
| 140 | sendDirty(rsc); |
Jason Sams | 5f0c84c | 2010-08-31 13:50:42 -0700 | [diff] [blame] | 141 | } |
| 142 | |
Jason Sams | 4b45b89 | 2010-12-29 14:31:29 -0800 | [diff] [blame] | 143 | void Allocation::elementData(Context *rsc, uint32_t x, uint32_t y, |
Stephen Hines | 6ae039b | 2012-01-18 18:46:27 -0800 | [diff] [blame] | 144 | const void *data, uint32_t cIdx, size_t sizeBytes) { |
| 145 | size_t eSize = mHal.state.elementSizeBytes; |
Jason Sams | 5f0c84c | 2010-08-31 13:50:42 -0700 | [diff] [blame] | 146 | |
Jason Sams | bad8074 | 2011-03-16 16:29:28 -0700 | [diff] [blame] | 147 | if (x >= mHal.state.dimensionX) { |
Steve Block | af12ac6 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 148 | ALOGE("Error Allocation::subElementData X offset %i out of range.", x); |
Jason Sams | 5f0c84c | 2010-08-31 13:50:42 -0700 | [diff] [blame] | 149 | rsc->setError(RS_ERROR_BAD_VALUE, "subElementData X offset out of range."); |
| 150 | return; |
| 151 | } |
| 152 | |
Jason Sams | bad8074 | 2011-03-16 16:29:28 -0700 | [diff] [blame] | 153 | if (y >= mHal.state.dimensionY) { |
Steve Block | af12ac6 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 154 | ALOGE("Error Allocation::subElementData X offset %i out of range.", x); |
Jason Sams | 5f0c84c | 2010-08-31 13:50:42 -0700 | [diff] [blame] | 155 | rsc->setError(RS_ERROR_BAD_VALUE, "subElementData X offset out of range."); |
| 156 | return; |
| 157 | } |
| 158 | |
Jason Sams | bad8074 | 2011-03-16 16:29:28 -0700 | [diff] [blame] | 159 | if (cIdx >= mHal.state.type->getElement()->getFieldCount()) { |
Steve Block | af12ac6 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 160 | ALOGE("Error Allocation::subElementData component %i out of range.", cIdx); |
Jason Sams | 5f0c84c | 2010-08-31 13:50:42 -0700 | [diff] [blame] | 161 | rsc->setError(RS_ERROR_BAD_VALUE, "subElementData component out of range."); |
| 162 | return; |
| 163 | } |
| 164 | |
Jason Sams | bad8074 | 2011-03-16 16:29:28 -0700 | [diff] [blame] | 165 | const Element * e = mHal.state.type->getElement()->getField(cIdx); |
Alex Sakhartchouk | 7694632 | 2012-02-02 09:47:26 -0800 | [diff] [blame] | 166 | uint32_t elemArraySize = mHal.state.type->getElement()->getFieldArraySize(cIdx); |
| 167 | if (sizeBytes != e->getSizeBytes() * elemArraySize) { |
Stephen Hines | 6ae039b | 2012-01-18 18:46:27 -0800 | [diff] [blame] | 168 | ALOGE("Error Allocation::subElementData data size %zu does not match field size %zu.", sizeBytes, e->getSizeBytes()); |
Jason Sams | 5f0c84c | 2010-08-31 13:50:42 -0700 | [diff] [blame] | 169 | rsc->setError(RS_ERROR_BAD_VALUE, "subElementData bad size."); |
| 170 | return; |
| 171 | } |
| 172 | |
Jason Sams | eb4fe18 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 173 | rsc->mHal.funcs.allocation.elementData2D(rsc, this, x, y, data, cIdx, sizeBytes); |
| 174 | sendDirty(rsc); |
Jason Sams | 5f0c84c | 2010-08-31 13:50:42 -0700 | [diff] [blame] | 175 | } |
| 176 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 177 | void Allocation::addProgramToDirty(const Program *p) { |
Alex Sakhartchouk | fb6b614 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 178 | mToDirtyList.push(p); |
Jason Sams | 5c3e3bc | 2009-10-26 15:19:28 -0700 | [diff] [blame] | 179 | } |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 180 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 181 | void Allocation::removeProgramToDirty(const Program *p) { |
Jason Sams | 5c3e3bc | 2009-10-26 15:19:28 -0700 | [diff] [blame] | 182 | for (size_t ct=0; ct < mToDirtyList.size(); ct++) { |
| 183 | if (mToDirtyList[ct] == p) { |
| 184 | mToDirtyList.removeAt(ct); |
| 185 | return; |
| 186 | } |
| 187 | } |
| 188 | rsAssert(0); |
| 189 | } |
| 190 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 191 | void Allocation::dumpLOGV(const char *prefix) const { |
Jason Sams | c21cf40 | 2009-11-17 17:26:46 -0800 | [diff] [blame] | 192 | ObjectBase::dumpLOGV(prefix); |
| 193 | |
| 194 | String8 s(prefix); |
| 195 | s.append(" type "); |
Alex Sakhartchouk | 064aa7e | 2011-10-18 10:54:29 -0700 | [diff] [blame] | 196 | if (mHal.state.type) { |
Jason Sams | bad8074 | 2011-03-16 16:29:28 -0700 | [diff] [blame] | 197 | mHal.state.type->dumpLOGV(s.string()); |
Jason Sams | c21cf40 | 2009-11-17 17:26:46 -0800 | [diff] [blame] | 198 | } |
| 199 | |
Steve Block | 6598201 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 200 | ALOGV("%s allocation ptr=%p mUsageFlags=0x04%x, mMipmapControl=0x%04x", |
Jason Sams | eb4fe18 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 201 | prefix, getPtr(), mHal.state.usageFlags, mHal.state.mipmapControl); |
Jason Sams | c21cf40 | 2009-11-17 17:26:46 -0800 | [diff] [blame] | 202 | } |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 203 | |
Alex Sakhartchouk | 2d1220c | 2011-11-15 15:15:21 -0800 | [diff] [blame] | 204 | uint32_t Allocation::getPackedSize() const { |
| 205 | uint32_t numItems = mHal.state.type->getSizeBytes() / mHal.state.type->getElementSizeBytes(); |
| 206 | return numItems * mHal.state.type->getElement()->getSizeBytesUnpadded(); |
| 207 | } |
| 208 | |
| 209 | void Allocation::writePackedData(const Type *type, |
| 210 | uint8_t *dst, const uint8_t *src, bool dstPadded) { |
| 211 | const Element *elem = type->getElement(); |
| 212 | uint32_t unpaddedBytes = elem->getSizeBytesUnpadded(); |
| 213 | uint32_t paddedBytes = elem->getSizeBytes(); |
| 214 | uint32_t numItems = type->getSizeBytes() / paddedBytes; |
| 215 | |
| 216 | uint32_t srcInc = !dstPadded ? paddedBytes : unpaddedBytes; |
| 217 | uint32_t dstInc = dstPadded ? paddedBytes : unpaddedBytes; |
| 218 | |
| 219 | // no sub-elements |
| 220 | uint32_t fieldCount = elem->getFieldCount(); |
| 221 | if (fieldCount == 0) { |
| 222 | for (uint32_t i = 0; i < numItems; i ++) { |
| 223 | memcpy(dst, src, unpaddedBytes); |
| 224 | src += srcInc; |
| 225 | dst += dstInc; |
| 226 | } |
| 227 | return; |
| 228 | } |
| 229 | |
| 230 | // Cache offsets |
| 231 | uint32_t *offsetsPadded = new uint32_t[fieldCount]; |
| 232 | uint32_t *offsetsUnpadded = new uint32_t[fieldCount]; |
| 233 | uint32_t *sizeUnpadded = new uint32_t[fieldCount]; |
| 234 | |
| 235 | for (uint32_t i = 0; i < fieldCount; i++) { |
| 236 | offsetsPadded[i] = elem->getFieldOffsetBytes(i); |
| 237 | offsetsUnpadded[i] = elem->getFieldOffsetBytesUnpadded(i); |
| 238 | sizeUnpadded[i] = elem->getField(i)->getSizeBytesUnpadded(); |
| 239 | } |
| 240 | |
| 241 | uint32_t *srcOffsets = !dstPadded ? offsetsPadded : offsetsUnpadded; |
| 242 | uint32_t *dstOffsets = dstPadded ? offsetsPadded : offsetsUnpadded; |
| 243 | |
| 244 | // complex elements, need to copy subelem after subelem |
| 245 | for (uint32_t i = 0; i < numItems; i ++) { |
| 246 | for (uint32_t fI = 0; fI < fieldCount; fI++) { |
| 247 | memcpy(dst + dstOffsets[fI], src + srcOffsets[fI], sizeUnpadded[fI]); |
| 248 | } |
| 249 | src += srcInc; |
| 250 | dst += dstInc; |
| 251 | } |
| 252 | |
| 253 | delete[] offsetsPadded; |
| 254 | delete[] offsetsUnpadded; |
| 255 | delete[] sizeUnpadded; |
| 256 | } |
| 257 | |
Stephen Hines | 6ae039b | 2012-01-18 18:46:27 -0800 | [diff] [blame] | 258 | void Allocation::unpackVec3Allocation(const void *data, size_t dataSize) { |
Alex Sakhartchouk | 2d1220c | 2011-11-15 15:15:21 -0800 | [diff] [blame] | 259 | const uint8_t *src = (const uint8_t*)data; |
| 260 | uint8_t *dst = (uint8_t*)getPtr(); |
| 261 | |
| 262 | writePackedData(getType(), dst, src, true); |
| 263 | } |
| 264 | |
| 265 | void Allocation::packVec3Allocation(OStream *stream) const { |
| 266 | uint32_t paddedBytes = getType()->getElement()->getSizeBytes(); |
| 267 | uint32_t unpaddedBytes = getType()->getElement()->getSizeBytesUnpadded(); |
| 268 | uint32_t numItems = mHal.state.type->getSizeBytes() / paddedBytes; |
| 269 | |
| 270 | const uint8_t *src = (const uint8_t*)getPtr(); |
| 271 | uint8_t *dst = new uint8_t[numItems * unpaddedBytes]; |
| 272 | |
| 273 | writePackedData(getType(), dst, src, false); |
| 274 | stream->addByteArray(dst, getPackedSize()); |
| 275 | |
| 276 | delete[] dst; |
| 277 | } |
| 278 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 279 | void Allocation::serialize(OStream *stream) const { |
Alex Sakhartchouk | fb6b614 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 280 | // Need to identify ourselves |
| 281 | stream->addU32((uint32_t)getClassId()); |
| 282 | |
| 283 | String8 name(getName()); |
| 284 | stream->addString(&name); |
| 285 | |
| 286 | // First thing we need to serialize is the type object since it will be needed |
| 287 | // to initialize the class |
Jason Sams | bad8074 | 2011-03-16 16:29:28 -0700 | [diff] [blame] | 288 | mHal.state.type->serialize(stream); |
Alex Sakhartchouk | fb6b614 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 289 | |
Jason Sams | bad8074 | 2011-03-16 16:29:28 -0700 | [diff] [blame] | 290 | uint32_t dataSize = mHal.state.type->getSizeBytes(); |
Alex Sakhartchouk | 2d1220c | 2011-11-15 15:15:21 -0800 | [diff] [blame] | 291 | // 3 element vectors are padded to 4 in memory, but padding isn't serialized |
| 292 | uint32_t packedSize = getPackedSize(); |
Alex Sakhartchouk | fb6b614 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 293 | // Write how much data we are storing |
Alex Sakhartchouk | 2d1220c | 2011-11-15 15:15:21 -0800 | [diff] [blame] | 294 | stream->addU32(packedSize); |
| 295 | if (dataSize == packedSize) { |
| 296 | // Now write the data |
| 297 | stream->addByteArray(getPtr(), dataSize); |
| 298 | } else { |
| 299 | // Now write the data |
| 300 | packVec3Allocation(stream); |
| 301 | } |
Alex Sakhartchouk | fb6b614 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 302 | } |
| 303 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 304 | Allocation *Allocation::createFromStream(Context *rsc, IStream *stream) { |
Alex Sakhartchouk | fb6b614 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 305 | // First make sure we are reading the correct object |
Alex Sakhartchouk | b825f67 | 2010-06-04 10:06:50 -0700 | [diff] [blame] | 306 | RsA3DClassID classID = (RsA3DClassID)stream->loadU32(); |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 307 | if (classID != RS_A3D_CLASS_ID_ALLOCATION) { |
Steve Block | af12ac6 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 308 | ALOGE("allocation loading skipped due to invalid class id\n"); |
Alex Sakhartchouk | fb6b614 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 309 | return NULL; |
| 310 | } |
| 311 | |
| 312 | String8 name; |
| 313 | stream->loadString(&name); |
| 314 | |
| 315 | Type *type = Type::createFromStream(rsc, stream); |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 316 | if (!type) { |
Alex Sakhartchouk | fb6b614 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 317 | return NULL; |
| 318 | } |
| 319 | type->compute(); |
| 320 | |
Alex Sakhartchouk | 2d1220c | 2011-11-15 15:15:21 -0800 | [diff] [blame] | 321 | Allocation *alloc = Allocation::createAllocation(rsc, type, RS_ALLOCATION_USAGE_SCRIPT); |
| 322 | type->decUserRef(); |
| 323 | |
Alex Sakhartchouk | fb6b614 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 324 | // Number of bytes we wrote out for this allocation |
| 325 | uint32_t dataSize = stream->loadU32(); |
Alex Sakhartchouk | 2d1220c | 2011-11-15 15:15:21 -0800 | [diff] [blame] | 326 | // 3 element vectors are padded to 4 in memory, but padding isn't serialized |
| 327 | uint32_t packedSize = alloc->getPackedSize(); |
| 328 | if (dataSize != type->getSizeBytes() && |
| 329 | dataSize != packedSize) { |
Steve Block | af12ac6 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 330 | ALOGE("failed to read allocation because numbytes written is not the same loaded type wants\n"); |
Alex Sakhartchouk | 2d1220c | 2011-11-15 15:15:21 -0800 | [diff] [blame] | 331 | ObjectBase::checkDelete(alloc); |
Jason Sams | 225afd3 | 2010-10-21 14:06:55 -0700 | [diff] [blame] | 332 | ObjectBase::checkDelete(type); |
Alex Sakhartchouk | fb6b614 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 333 | return NULL; |
| 334 | } |
| 335 | |
Alex Sakhartchouk | fb6b614 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 336 | alloc->setName(name.string(), name.size()); |
| 337 | |
Alex Sakhartchouk | 2d1220c | 2011-11-15 15:15:21 -0800 | [diff] [blame] | 338 | if (dataSize == type->getSizeBytes()) { |
| 339 | uint32_t count = dataSize / type->getElementSizeBytes(); |
| 340 | // Read in all of our allocation data |
| 341 | alloc->data(rsc, 0, 0, count, stream->getPtr() + stream->getPos(), dataSize); |
| 342 | } else { |
| 343 | alloc->unpackVec3Allocation(stream->getPtr() + stream->getPos(), dataSize); |
| 344 | } |
Alex Sakhartchouk | e6d9fbc | 2010-08-11 10:30:44 -0700 | [diff] [blame] | 345 | stream->reset(stream->getPos() + dataSize); |
Alex Sakhartchouk | fb6b614 | 2010-05-21 12:53:13 -0700 | [diff] [blame] | 346 | |
| 347 | return alloc; |
| 348 | } |
| 349 | |
Jason Sams | eb4fe18 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 350 | void Allocation::sendDirty(const Context *rsc) const { |
Jason Sams | 5c3e3bc | 2009-10-26 15:19:28 -0700 | [diff] [blame] | 351 | for (size_t ct=0; ct < mToDirtyList.size(); ct++) { |
| 352 | mToDirtyList[ct]->forceDirty(); |
| 353 | } |
Jason Sams | eb4fe18 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 354 | mRSC->mHal.funcs.allocation.markDirty(rsc, this); |
Jason Sams | 5c3e3bc | 2009-10-26 15:19:28 -0700 | [diff] [blame] | 355 | } |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 356 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 357 | void Allocation::incRefs(const void *ptr, size_t ct, size_t startOff) const { |
Jason Sams | e3929c9 | 2010-08-09 18:13:33 -0700 | [diff] [blame] | 358 | const uint8_t *p = static_cast<const uint8_t *>(ptr); |
Jason Sams | bad8074 | 2011-03-16 16:29:28 -0700 | [diff] [blame] | 359 | const Element *e = mHal.state.type->getElement(); |
Jason Sams | e3929c9 | 2010-08-09 18:13:33 -0700 | [diff] [blame] | 360 | uint32_t stride = e->getSizeBytes(); |
| 361 | |
Jason Sams | 96abf81 | 2010-10-05 13:32:49 -0700 | [diff] [blame] | 362 | p += stride * startOff; |
Jason Sams | e3929c9 | 2010-08-09 18:13:33 -0700 | [diff] [blame] | 363 | while (ct > 0) { |
| 364 | e->incRefs(p); |
| 365 | ct --; |
| 366 | p += stride; |
| 367 | } |
| 368 | } |
| 369 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 370 | void Allocation::decRefs(const void *ptr, size_t ct, size_t startOff) const { |
Alex Sakhartchouk | 5c4369a | 2011-08-12 11:30:30 -0700 | [diff] [blame] | 371 | if (!mHal.state.hasReferences || !getIsScript()) { |
| 372 | return; |
| 373 | } |
Jason Sams | e3929c9 | 2010-08-09 18:13:33 -0700 | [diff] [blame] | 374 | const uint8_t *p = static_cast<const uint8_t *>(ptr); |
Jason Sams | bad8074 | 2011-03-16 16:29:28 -0700 | [diff] [blame] | 375 | const Element *e = mHal.state.type->getElement(); |
Jason Sams | e3929c9 | 2010-08-09 18:13:33 -0700 | [diff] [blame] | 376 | uint32_t stride = e->getSizeBytes(); |
| 377 | |
Jason Sams | 96abf81 | 2010-10-05 13:32:49 -0700 | [diff] [blame] | 378 | p += stride * startOff; |
Jason Sams | e3929c9 | 2010-08-09 18:13:33 -0700 | [diff] [blame] | 379 | while (ct > 0) { |
| 380 | e->decRefs(p); |
| 381 | ct --; |
| 382 | p += stride; |
| 383 | } |
| 384 | } |
| 385 | |
Jason Sams | c7cec1e | 2011-08-18 18:01:33 -0700 | [diff] [blame] | 386 | void Allocation::freeChildrenUnlocked () { |
| 387 | decRefs(getPtr(), mHal.state.type->getSizeBytes() / mHal.state.type->getElementSizeBytes(), 0); |
| 388 | } |
| 389 | |
| 390 | bool Allocation::freeChildren() { |
| 391 | if (mHal.state.hasReferences) { |
| 392 | incSysRef(); |
| 393 | freeChildrenUnlocked(); |
| 394 | return decSysRef(); |
| 395 | } |
| 396 | return false; |
| 397 | } |
| 398 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 399 | void Allocation::copyRange1D(Context *rsc, const Allocation *src, int32_t srcOff, int32_t destOff, int32_t len) { |
Jason Sams | 96abf81 | 2010-10-05 13:32:49 -0700 | [diff] [blame] | 400 | } |
| 401 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 402 | void Allocation::resize1D(Context *rsc, uint32_t dimX) { |
Jason Sams | bad8074 | 2011-03-16 16:29:28 -0700 | [diff] [blame] | 403 | uint32_t oldDimX = mHal.state.dimensionX; |
Jason Sams | 96abf81 | 2010-10-05 13:32:49 -0700 | [diff] [blame] | 404 | if (dimX == oldDimX) { |
| 405 | return; |
| 406 | } |
| 407 | |
Alex Sakhartchouk | c700e64 | 2011-08-16 13:09:46 -0700 | [diff] [blame] | 408 | ObjectBaseRef<Type> t = mHal.state.type->cloneAndResize1D(rsc, dimX); |
Jason Sams | 96abf81 | 2010-10-05 13:32:49 -0700 | [diff] [blame] | 409 | if (dimX < oldDimX) { |
Jason Sams | eb4fe18 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 410 | decRefs(getPtr(), oldDimX - dimX, dimX); |
Jason Sams | 96abf81 | 2010-10-05 13:32:49 -0700 | [diff] [blame] | 411 | } |
Alex Sakhartchouk | c700e64 | 2011-08-16 13:09:46 -0700 | [diff] [blame] | 412 | rsc->mHal.funcs.allocation.resize(rsc, this, t.get(), mHal.state.hasReferences); |
Alex Sakhartchouk | 064aa7e | 2011-10-18 10:54:29 -0700 | [diff] [blame] | 413 | setType(t.get()); |
Jason Sams | bad8074 | 2011-03-16 16:29:28 -0700 | [diff] [blame] | 414 | updateCache(); |
Jason Sams | 96abf81 | 2010-10-05 13:32:49 -0700 | [diff] [blame] | 415 | } |
| 416 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 417 | void Allocation::resize2D(Context *rsc, uint32_t dimX, uint32_t dimY) { |
Steve Block | af12ac6 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 418 | ALOGE("not implemented"); |
Jason Sams | 96abf81 | 2010-10-05 13:32:49 -0700 | [diff] [blame] | 419 | } |
| 420 | |
Jason Sams | 41e373d | 2012-01-13 14:01:20 -0800 | [diff] [blame] | 421 | int32_t Allocation::getSurfaceTextureID(const Context *rsc) { |
| 422 | int32_t id = rsc->mHal.funcs.allocation.initSurfaceTexture(rsc, this); |
| 423 | mHal.state.surfaceTextureID = id; |
| 424 | return id; |
| 425 | } |
| 426 | |
Jason Sams | 7ac2a4d | 2012-02-15 12:04:24 -0800 | [diff] [blame] | 427 | void Allocation::setSurface(const Context *rsc, RsNativeWindow sur) { |
| 428 | ANativeWindow *nw = (ANativeWindow *)sur; |
| 429 | ANativeWindow *old = mHal.state.wndSurface; |
| 430 | if (nw) { |
| 431 | nw->incStrong(NULL); |
| 432 | } |
| 433 | rsc->mHal.funcs.allocation.setSurfaceTexture(rsc, this, nw); |
| 434 | mHal.state.wndSurface = nw; |
| 435 | if (old) { |
| 436 | old->decStrong(NULL); |
| 437 | } |
| 438 | } |
| 439 | |
| 440 | void Allocation::ioSend(const Context *rsc) { |
| 441 | rsc->mHal.funcs.allocation.ioSend(rsc, this); |
| 442 | } |
| 443 | |
| 444 | void Allocation::ioReceive(const Context *rsc) { |
| 445 | rsc->mHal.funcs.allocation.ioReceive(rsc, this); |
| 446 | } |
| 447 | |
| 448 | |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 449 | ///////////////// |
Jason Sams | 565ac36 | 2009-06-03 16:04:54 -0700 | [diff] [blame] | 450 | // |
Stephen Hines | 6a12181 | 2011-03-01 17:34:59 -0800 | [diff] [blame] | 451 | |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 452 | namespace android { |
| 453 | namespace renderscript { |
| 454 | |
Jason Sams | c975cf4 | 2011-04-28 18:26:48 -0700 | [diff] [blame] | 455 | static void AllocationGenerateScriptMips(RsContext con, RsAllocation va); |
| 456 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 457 | static void mip565(const Adapter2D &out, const Adapter2D &in) { |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 458 | uint32_t w = out.getDimX(); |
| 459 | uint32_t h = out.getDimY(); |
| 460 | |
Jason Sams | e9f5c53 | 2009-07-28 17:20:11 -0700 | [diff] [blame] | 461 | for (uint32_t y=0; y < h; y++) { |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 462 | uint16_t *oPtr = static_cast<uint16_t *>(out.getElement(0, y)); |
| 463 | const uint16_t *i1 = static_cast<uint16_t *>(in.getElement(0, y*2)); |
| 464 | const uint16_t *i2 = static_cast<uint16_t *>(in.getElement(0, y*2+1)); |
| 465 | |
Jason Sams | e9f5c53 | 2009-07-28 17:20:11 -0700 | [diff] [blame] | 466 | for (uint32_t x=0; x < w; x++) { |
Jason Sams | 565ac36 | 2009-06-03 16:04:54 -0700 | [diff] [blame] | 467 | *oPtr = rsBoxFilter565(i1[0], i1[1], i2[0], i2[1]); |
| 468 | oPtr ++; |
| 469 | i1 += 2; |
| 470 | i2 += 2; |
| 471 | } |
| 472 | } |
| 473 | } |
| 474 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 475 | static void mip8888(const Adapter2D &out, const Adapter2D &in) { |
Jason Sams | 565ac36 | 2009-06-03 16:04:54 -0700 | [diff] [blame] | 476 | uint32_t w = out.getDimX(); |
| 477 | uint32_t h = out.getDimY(); |
| 478 | |
Jason Sams | e9f5c53 | 2009-07-28 17:20:11 -0700 | [diff] [blame] | 479 | for (uint32_t y=0; y < h; y++) { |
Jason Sams | 565ac36 | 2009-06-03 16:04:54 -0700 | [diff] [blame] | 480 | uint32_t *oPtr = static_cast<uint32_t *>(out.getElement(0, y)); |
| 481 | const uint32_t *i1 = static_cast<uint32_t *>(in.getElement(0, y*2)); |
| 482 | const uint32_t *i2 = static_cast<uint32_t *>(in.getElement(0, y*2+1)); |
| 483 | |
Jason Sams | e9f5c53 | 2009-07-28 17:20:11 -0700 | [diff] [blame] | 484 | for (uint32_t x=0; x < w; x++) { |
Jason Sams | 565ac36 | 2009-06-03 16:04:54 -0700 | [diff] [blame] | 485 | *oPtr = rsBoxFilter8888(i1[0], i1[1], i2[0], i2[1]); |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 486 | oPtr ++; |
| 487 | i1 += 2; |
| 488 | i2 += 2; |
| 489 | } |
| 490 | } |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 491 | } |
| 492 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 493 | static void mip8(const Adapter2D &out, const Adapter2D &in) { |
Jason Sams | 2f6d861 | 2010-01-19 17:53:54 -0800 | [diff] [blame] | 494 | uint32_t w = out.getDimX(); |
| 495 | uint32_t h = out.getDimY(); |
| 496 | |
| 497 | for (uint32_t y=0; y < h; y++) { |
| 498 | uint8_t *oPtr = static_cast<uint8_t *>(out.getElement(0, y)); |
| 499 | const uint8_t *i1 = static_cast<uint8_t *>(in.getElement(0, y*2)); |
| 500 | const uint8_t *i2 = static_cast<uint8_t *>(in.getElement(0, y*2+1)); |
| 501 | |
| 502 | for (uint32_t x=0; x < w; x++) { |
| 503 | *oPtr = (uint8_t)(((uint32_t)i1[0] + i1[1] + i2[0] + i2[1]) * 0.25f); |
| 504 | oPtr ++; |
| 505 | i1 += 2; |
| 506 | i2 += 2; |
| 507 | } |
| 508 | } |
| 509 | } |
| 510 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 511 | static void mip(const Adapter2D &out, const Adapter2D &in) { |
| 512 | switch (out.getBaseType()->getElement()->getSizeBits()) { |
Jason Sams | e9f5c53 | 2009-07-28 17:20:11 -0700 | [diff] [blame] | 513 | case 32: |
| 514 | mip8888(out, in); |
| 515 | break; |
| 516 | case 16: |
| 517 | mip565(out, in); |
| 518 | break; |
Jason Sams | 2f6d861 | 2010-01-19 17:53:54 -0800 | [diff] [blame] | 519 | case 8: |
| 520 | mip8(out, in); |
| 521 | break; |
Jason Sams | e9f5c53 | 2009-07-28 17:20:11 -0700 | [diff] [blame] | 522 | } |
Jason Sams | e9f5c53 | 2009-07-28 17:20:11 -0700 | [diff] [blame] | 523 | } |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 524 | |
Jason Sams | 366c9c8 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 525 | void rsi_AllocationSyncAll(Context *rsc, RsAllocation va, RsAllocationUsageType src) { |
| 526 | Allocation *a = static_cast<Allocation *>(va); |
Jason Sams | eb4fe18 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 527 | a->sendDirty(rsc); |
Jason Sams | 366c9c8 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 528 | a->syncAll(rsc, src); |
| 529 | } |
| 530 | |
Jason Sams | a237151 | 2011-01-12 13:28:37 -0800 | [diff] [blame] | 531 | void rsi_AllocationGenerateMipmaps(Context *rsc, RsAllocation va) { |
Alex Sakhartchouk | 39f2ef6 | 2010-10-11 12:35:15 -0700 | [diff] [blame] | 532 | Allocation *texAlloc = static_cast<Allocation *>(va); |
Jason Sams | c975cf4 | 2011-04-28 18:26:48 -0700 | [diff] [blame] | 533 | AllocationGenerateScriptMips(rsc, texAlloc); |
Jason Sams | 837e388 | 2010-12-10 16:03:15 -0800 | [diff] [blame] | 534 | } |
| 535 | |
| 536 | void rsi_AllocationCopyToBitmap(Context *rsc, RsAllocation va, void *data, size_t dataLen) { |
| 537 | Allocation *texAlloc = static_cast<Allocation *>(va); |
| 538 | const Type * t = texAlloc->getType(); |
| 539 | |
| 540 | size_t s = t->getDimX() * t->getDimY() * t->getElementSizeBytes(); |
| 541 | if (s != dataLen) { |
| 542 | rsc->setError(RS_ERROR_BAD_VALUE, "Bitmap size didn't match allocation size"); |
| 543 | return; |
| 544 | } |
| 545 | |
| 546 | memcpy(data, texAlloc->getPtr(), s); |
Alex Sakhartchouk | 39f2ef6 | 2010-10-11 12:35:15 -0700 | [diff] [blame] | 547 | } |
| 548 | |
Jason Sams | 4b45b89 | 2010-12-29 14:31:29 -0800 | [diff] [blame] | 549 | void rsi_Allocation1DData(Context *rsc, RsAllocation va, uint32_t xoff, uint32_t lod, |
Alex Sakhartchouk | b81a0eb | 2011-06-03 10:18:01 -0700 | [diff] [blame] | 550 | uint32_t count, const void *data, size_t sizeBytes) { |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 551 | Allocation *a = static_cast<Allocation *>(va); |
Jason Sams | 4b45b89 | 2010-12-29 14:31:29 -0800 | [diff] [blame] | 552 | a->data(rsc, xoff, lod, count, data, sizeBytes); |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 553 | } |
| 554 | |
Jason Sams | 4b45b89 | 2010-12-29 14:31:29 -0800 | [diff] [blame] | 555 | void rsi_Allocation2DElementData(Context *rsc, RsAllocation va, uint32_t x, uint32_t y, uint32_t lod, RsAllocationCubemapFace face, |
Stephen Hines | 6ae039b | 2012-01-18 18:46:27 -0800 | [diff] [blame] | 556 | const void *data, size_t sizeBytes, size_t eoff) { |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 557 | Allocation *a = static_cast<Allocation *>(va); |
Jason Sams | 4b45b89 | 2010-12-29 14:31:29 -0800 | [diff] [blame] | 558 | a->elementData(rsc, x, y, data, eoff, sizeBytes); |
Jason Sams | 5f0c84c | 2010-08-31 13:50:42 -0700 | [diff] [blame] | 559 | } |
| 560 | |
Jason Sams | 4b45b89 | 2010-12-29 14:31:29 -0800 | [diff] [blame] | 561 | void rsi_Allocation1DElementData(Context *rsc, RsAllocation va, uint32_t x, uint32_t lod, |
Stephen Hines | 6ae039b | 2012-01-18 18:46:27 -0800 | [diff] [blame] | 562 | const void *data, size_t sizeBytes, size_t eoff) { |
Jason Sams | 5f0c84c | 2010-08-31 13:50:42 -0700 | [diff] [blame] | 563 | Allocation *a = static_cast<Allocation *>(va); |
Jason Sams | 4b45b89 | 2010-12-29 14:31:29 -0800 | [diff] [blame] | 564 | a->elementData(rsc, x, data, eoff, sizeBytes); |
Jason Sams | 5f0c84c | 2010-08-31 13:50:42 -0700 | [diff] [blame] | 565 | } |
| 566 | |
Jason Sams | 4b45b89 | 2010-12-29 14:31:29 -0800 | [diff] [blame] | 567 | void rsi_Allocation2DData(Context *rsc, RsAllocation va, uint32_t xoff, uint32_t yoff, uint32_t lod, RsAllocationCubemapFace face, |
Alex Sakhartchouk | b81a0eb | 2011-06-03 10:18:01 -0700 | [diff] [blame] | 568 | uint32_t w, uint32_t h, const void *data, size_t sizeBytes) { |
Jason Sams | 5f0c84c | 2010-08-31 13:50:42 -0700 | [diff] [blame] | 569 | Allocation *a = static_cast<Allocation *>(va); |
Jason Sams | 4b45b89 | 2010-12-29 14:31:29 -0800 | [diff] [blame] | 570 | a->data(rsc, xoff, yoff, lod, face, w, h, data, sizeBytes); |
Jason Sams | 326e0dd | 2009-05-22 14:03:28 -0700 | [diff] [blame] | 571 | } |
| 572 | |
Alex Sakhartchouk | 70b83c1 | 2011-04-06 10:57:51 -0700 | [diff] [blame] | 573 | void rsi_AllocationRead(Context *rsc, RsAllocation va, void *data, size_t data_length) { |
Jason Sams | e579df4 | 2009-08-10 14:55:26 -0700 | [diff] [blame] | 574 | Allocation *a = static_cast<Allocation *>(va); |
| 575 | a->read(data); |
| 576 | } |
| 577 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 578 | void rsi_AllocationResize1D(Context *rsc, RsAllocation va, uint32_t dimX) { |
Jason Sams | 96abf81 | 2010-10-05 13:32:49 -0700 | [diff] [blame] | 579 | Allocation *a = static_cast<Allocation *>(va); |
| 580 | a->resize1D(rsc, dimX); |
| 581 | } |
| 582 | |
Alex Sakhartchouk | afb743a | 2010-11-09 17:00:54 -0800 | [diff] [blame] | 583 | void rsi_AllocationResize2D(Context *rsc, RsAllocation va, uint32_t dimX, uint32_t dimY) { |
Jason Sams | 96abf81 | 2010-10-05 13:32:49 -0700 | [diff] [blame] | 584 | Allocation *a = static_cast<Allocation *>(va); |
| 585 | a->resize2D(rsc, dimX, dimY); |
| 586 | } |
| 587 | |
Jason Sams | c975cf4 | 2011-04-28 18:26:48 -0700 | [diff] [blame] | 588 | static void AllocationGenerateScriptMips(RsContext con, RsAllocation va) { |
Alex Sakhartchouk | f8aafcf | 2011-01-11 14:47:44 -0800 | [diff] [blame] | 589 | Context *rsc = static_cast<Context *>(con); |
| 590 | Allocation *texAlloc = static_cast<Allocation *>(va); |
| 591 | uint32_t numFaces = texAlloc->getType()->getDimFaces() ? 6 : 1; |
| 592 | for (uint32_t face = 0; face < numFaces; face ++) { |
| 593 | Adapter2D adapt(rsc, texAlloc); |
| 594 | Adapter2D adapt2(rsc, texAlloc); |
| 595 | adapt.setFace(face); |
| 596 | adapt2.setFace(face); |
| 597 | for (uint32_t lod=0; lod < (texAlloc->getType()->getLODCount() -1); lod++) { |
| 598 | adapt.setLOD(lod); |
| 599 | adapt2.setLOD(lod + 1); |
| 600 | mip(adapt2, adapt); |
| 601 | } |
| 602 | } |
| 603 | } |
| 604 | |
Jason Sams | c975cf4 | 2011-04-28 18:26:48 -0700 | [diff] [blame] | 605 | RsAllocation rsi_AllocationCreateTyped(Context *rsc, RsType vtype, |
| 606 | RsAllocationMipmapControl mips, |
Jason Sams | 179e9a4 | 2011-11-23 15:02:15 -0800 | [diff] [blame] | 607 | uint32_t usages, uint32_t ptr) { |
| 608 | Allocation * alloc = Allocation::createAllocation(rsc, static_cast<Type *>(vtype), usages, mips, (void *)ptr); |
Jason Sams | eb4fe18 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 609 | if (!alloc) { |
| 610 | return NULL; |
| 611 | } |
Jason Sams | f0c1df4 | 2010-10-26 13:09:17 -0700 | [diff] [blame] | 612 | alloc->incUserRef(); |
| 613 | return alloc; |
| 614 | } |
| 615 | |
Jason Sams | c975cf4 | 2011-04-28 18:26:48 -0700 | [diff] [blame] | 616 | RsAllocation rsi_AllocationCreateFromBitmap(Context *rsc, RsType vtype, |
| 617 | RsAllocationMipmapControl mips, |
| 618 | const void *data, size_t data_length, uint32_t usages) { |
Jason Sams | 366c9c8 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 619 | Type *t = static_cast<Type *>(vtype); |
Jason Sams | f0c1df4 | 2010-10-26 13:09:17 -0700 | [diff] [blame] | 620 | |
Jason Sams | 179e9a4 | 2011-11-23 15:02:15 -0800 | [diff] [blame] | 621 | RsAllocation vTexAlloc = rsi_AllocationCreateTyped(rsc, vtype, mips, usages, 0); |
Jason Sams | f0c1df4 | 2010-10-26 13:09:17 -0700 | [diff] [blame] | 622 | Allocation *texAlloc = static_cast<Allocation *>(vTexAlloc); |
| 623 | if (texAlloc == NULL) { |
Steve Block | af12ac6 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 624 | ALOGE("Memory allocation failure"); |
Jason Sams | f0c1df4 | 2010-10-26 13:09:17 -0700 | [diff] [blame] | 625 | return NULL; |
| 626 | } |
| 627 | |
Jason Sams | 366c9c8 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 628 | memcpy(texAlloc->getPtr(), data, t->getDimX() * t->getDimY() * t->getElementSizeBytes()); |
Jason Sams | ebc5019 | 2010-12-13 15:32:35 -0800 | [diff] [blame] | 629 | if (mips == RS_ALLOCATION_MIPMAP_FULL) { |
Jason Sams | c975cf4 | 2011-04-28 18:26:48 -0700 | [diff] [blame] | 630 | AllocationGenerateScriptMips(rsc, texAlloc); |
Jason Sams | f0c1df4 | 2010-10-26 13:09:17 -0700 | [diff] [blame] | 631 | } |
| 632 | |
Jason Sams | eb4fe18 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 633 | texAlloc->sendDirty(rsc); |
Jason Sams | f0c1df4 | 2010-10-26 13:09:17 -0700 | [diff] [blame] | 634 | return texAlloc; |
| 635 | } |
Alex Sakhartchouk | 84e4027 | 2010-11-18 15:22:43 -0800 | [diff] [blame] | 636 | |
Jason Sams | c975cf4 | 2011-04-28 18:26:48 -0700 | [diff] [blame] | 637 | RsAllocation rsi_AllocationCubeCreateFromBitmap(Context *rsc, RsType vtype, |
| 638 | RsAllocationMipmapControl mips, |
| 639 | const void *data, size_t data_length, uint32_t usages) { |
Jason Sams | 366c9c8 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 640 | Type *t = static_cast<Type *>(vtype); |
Alex Sakhartchouk | 84e4027 | 2010-11-18 15:22:43 -0800 | [diff] [blame] | 641 | |
| 642 | // Cubemap allocation's faces should be Width by Width each. |
| 643 | // Source data should have 6 * Width by Width pixels |
| 644 | // Error checking is done in the java layer |
Jason Sams | 179e9a4 | 2011-11-23 15:02:15 -0800 | [diff] [blame] | 645 | RsAllocation vTexAlloc = rsi_AllocationCreateTyped(rsc, vtype, mips, usages, 0); |
Alex Sakhartchouk | 84e4027 | 2010-11-18 15:22:43 -0800 | [diff] [blame] | 646 | Allocation *texAlloc = static_cast<Allocation *>(vTexAlloc); |
| 647 | if (texAlloc == NULL) { |
Steve Block | af12ac6 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 648 | ALOGE("Memory allocation failure"); |
Alex Sakhartchouk | 84e4027 | 2010-11-18 15:22:43 -0800 | [diff] [blame] | 649 | return NULL; |
| 650 | } |
| 651 | |
Alex Sakhartchouk | 9f8bc4f | 2011-01-10 15:57:57 -0800 | [diff] [blame] | 652 | uint32_t faceSize = t->getDimX(); |
| 653 | uint32_t strideBytes = faceSize * 6 * t->getElementSizeBytes(); |
| 654 | uint32_t copySize = faceSize * t->getElementSizeBytes(); |
| 655 | |
Alex Sakhartchouk | 84e4027 | 2010-11-18 15:22:43 -0800 | [diff] [blame] | 656 | uint8_t *sourcePtr = (uint8_t*)data; |
Jason Sams | 366c9c8 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 657 | for (uint32_t face = 0; face < 6; face ++) { |
| 658 | Adapter2D faceAdapter(rsc, texAlloc); |
| 659 | faceAdapter.setFace(face); |
Alex Sakhartchouk | 84e4027 | 2010-11-18 15:22:43 -0800 | [diff] [blame] | 660 | |
Alex Sakhartchouk | 9f8bc4f | 2011-01-10 15:57:57 -0800 | [diff] [blame] | 661 | for (uint32_t dI = 0; dI < faceSize; dI ++) { |
| 662 | memcpy(faceAdapter.getElement(0, dI), sourcePtr + strideBytes * dI, copySize); |
| 663 | } |
Alex Sakhartchouk | 84e4027 | 2010-11-18 15:22:43 -0800 | [diff] [blame] | 664 | |
Jason Sams | 366c9c8 | 2010-12-08 16:14:36 -0800 | [diff] [blame] | 665 | // Move the data pointer to the next cube face |
Alex Sakhartchouk | 9f8bc4f | 2011-01-10 15:57:57 -0800 | [diff] [blame] | 666 | sourcePtr += copySize; |
Alex Sakhartchouk | f8aafcf | 2011-01-11 14:47:44 -0800 | [diff] [blame] | 667 | } |
Alex Sakhartchouk | 84e4027 | 2010-11-18 15:22:43 -0800 | [diff] [blame] | 668 | |
Alex Sakhartchouk | f8aafcf | 2011-01-11 14:47:44 -0800 | [diff] [blame] | 669 | if (mips == RS_ALLOCATION_MIPMAP_FULL) { |
Jason Sams | c975cf4 | 2011-04-28 18:26:48 -0700 | [diff] [blame] | 670 | AllocationGenerateScriptMips(rsc, texAlloc); |
Alex Sakhartchouk | 84e4027 | 2010-11-18 15:22:43 -0800 | [diff] [blame] | 671 | } |
| 672 | |
Jason Sams | eb4fe18 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 673 | texAlloc->sendDirty(rsc); |
Alex Sakhartchouk | 84e4027 | 2010-11-18 15:22:43 -0800 | [diff] [blame] | 674 | return texAlloc; |
| 675 | } |
Alex Sakhartchouk | 099d7d3 | 2011-01-28 09:31:47 -0800 | [diff] [blame] | 676 | |
Alex Sakhartchouk | 74a8279 | 2011-06-14 11:13:19 -0700 | [diff] [blame] | 677 | void rsi_AllocationCopy2DRange(Context *rsc, |
| 678 | RsAllocation dstAlloc, |
| 679 | uint32_t dstXoff, uint32_t dstYoff, |
| 680 | uint32_t dstMip, uint32_t dstFace, |
| 681 | uint32_t width, uint32_t height, |
| 682 | RsAllocation srcAlloc, |
| 683 | uint32_t srcXoff, uint32_t srcYoff, |
| 684 | uint32_t srcMip, uint32_t srcFace) { |
| 685 | Allocation *dst = static_cast<Allocation *>(dstAlloc); |
| 686 | Allocation *src= static_cast<Allocation *>(srcAlloc); |
| 687 | rsc->mHal.funcs.allocation.allocData2D(rsc, dst, dstXoff, dstYoff, dstMip, |
| 688 | (RsAllocationCubemapFace)dstFace, |
| 689 | width, height, |
| 690 | src, srcXoff, srcYoff,srcMip, |
| 691 | (RsAllocationCubemapFace)srcFace); |
| 692 | } |
| 693 | |
Jason Sams | 41e373d | 2012-01-13 14:01:20 -0800 | [diff] [blame] | 694 | int32_t rsi_AllocationGetSurfaceTextureID(Context *rsc, RsAllocation valloc) { |
| 695 | Allocation *alloc = static_cast<Allocation *>(valloc); |
| 696 | return alloc->getSurfaceTextureID(rsc); |
| 697 | } |
| 698 | |
Jason Sams | 7ac2a4d | 2012-02-15 12:04:24 -0800 | [diff] [blame] | 699 | void rsi_AllocationSetSurface(Context *rsc, RsAllocation valloc, RsNativeWindow sur) { |
| 700 | Allocation *alloc = static_cast<Allocation *>(valloc); |
| 701 | alloc->setSurface(rsc, sur); |
| 702 | } |
| 703 | |
| 704 | void rsi_AllocationIoSend(Context *rsc, RsAllocation valloc) { |
| 705 | Allocation *alloc = static_cast<Allocation *>(valloc); |
| 706 | alloc->ioSend(rsc); |
| 707 | } |
| 708 | |
| 709 | void rsi_AllocationIoReceive(Context *rsc, RsAllocation valloc) { |
| 710 | Allocation *alloc = static_cast<Allocation *>(valloc); |
| 711 | alloc->ioReceive(rsc); |
| 712 | } |
| 713 | |
Jason Sams | c975cf4 | 2011-04-28 18:26:48 -0700 | [diff] [blame] | 714 | } |
| 715 | } |
| 716 | |
| 717 | const void * rsaAllocationGetType(RsContext con, RsAllocation va) { |
| 718 | Allocation *a = static_cast<Allocation *>(va); |
| 719 | a->getType()->incUserRef(); |
| 720 | |
| 721 | return a->getType(); |
| 722 | } |