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