Jason Sams | eb4fe18 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 1 | /* |
Jason Sams | 3522f40 | 2012-03-23 11:47:26 -0700 | [diff] [blame] | 2 | * Copyright (C) 2011-2012 The Android Open Source Project |
Jason Sams | eb4fe18 | 2011-05-26 16:33:01 -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 | */ |
| 16 | |
| 17 | |
| 18 | #include "rsdCore.h" |
Jason Sams | eb4fe18 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 19 | #include "rsdAllocation.h" |
Alex Sakhartchouk | a949524 | 2011-06-16 11:05:13 -0700 | [diff] [blame] | 20 | #include "rsdFrameBufferObj.h" |
Jason Sams | eb4fe18 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 21 | |
| 22 | #include "rsAllocation.h" |
| 23 | |
Jason Sams | 7ac2a4d | 2012-02-15 12:04:24 -0800 | [diff] [blame] | 24 | #include "system/window.h" |
| 25 | #include "hardware/gralloc.h" |
| 26 | #include "ui/Rect.h" |
| 27 | #include "ui/GraphicBufferMapper.h" |
Jason Sams | 3522f40 | 2012-03-23 11:47:26 -0700 | [diff] [blame] | 28 | #include "gui/SurfaceTexture.h" |
Jason Sams | 7ac2a4d | 2012-02-15 12:04:24 -0800 | [diff] [blame] | 29 | |
Jason Sams | eb4fe18 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 30 | #include <GLES/gl.h> |
| 31 | #include <GLES2/gl2.h> |
| 32 | #include <GLES/glext.h> |
| 33 | |
| 34 | using namespace android; |
| 35 | using namespace android::renderscript; |
| 36 | |
| 37 | |
| 38 | |
| 39 | const static GLenum gFaceOrder[] = { |
| 40 | GL_TEXTURE_CUBE_MAP_POSITIVE_X, |
| 41 | GL_TEXTURE_CUBE_MAP_NEGATIVE_X, |
| 42 | GL_TEXTURE_CUBE_MAP_POSITIVE_Y, |
| 43 | GL_TEXTURE_CUBE_MAP_NEGATIVE_Y, |
| 44 | GL_TEXTURE_CUBE_MAP_POSITIVE_Z, |
| 45 | GL_TEXTURE_CUBE_MAP_NEGATIVE_Z |
| 46 | }; |
| 47 | |
| 48 | |
Jason Sams | a614ae1 | 2011-05-26 17:05:51 -0700 | [diff] [blame] | 49 | GLenum rsdTypeToGLType(RsDataType t) { |
| 50 | switch (t) { |
| 51 | case RS_TYPE_UNSIGNED_5_6_5: return GL_UNSIGNED_SHORT_5_6_5; |
| 52 | case RS_TYPE_UNSIGNED_5_5_5_1: return GL_UNSIGNED_SHORT_5_5_5_1; |
| 53 | case RS_TYPE_UNSIGNED_4_4_4_4: return GL_UNSIGNED_SHORT_4_4_4_4; |
| 54 | |
| 55 | //case RS_TYPE_FLOAT_16: return GL_HALF_FLOAT; |
| 56 | case RS_TYPE_FLOAT_32: return GL_FLOAT; |
| 57 | case RS_TYPE_UNSIGNED_8: return GL_UNSIGNED_BYTE; |
| 58 | case RS_TYPE_UNSIGNED_16: return GL_UNSIGNED_SHORT; |
| 59 | case RS_TYPE_SIGNED_8: return GL_BYTE; |
| 60 | case RS_TYPE_SIGNED_16: return GL_SHORT; |
| 61 | default: break; |
| 62 | } |
| 63 | return 0; |
| 64 | } |
| 65 | |
| 66 | GLenum rsdKindToGLFormat(RsDataKind k) { |
| 67 | switch (k) { |
| 68 | case RS_KIND_PIXEL_L: return GL_LUMINANCE; |
| 69 | case RS_KIND_PIXEL_A: return GL_ALPHA; |
| 70 | case RS_KIND_PIXEL_LA: return GL_LUMINANCE_ALPHA; |
| 71 | case RS_KIND_PIXEL_RGB: return GL_RGB; |
| 72 | case RS_KIND_PIXEL_RGBA: return GL_RGBA; |
| 73 | case RS_KIND_PIXEL_DEPTH: return GL_DEPTH_COMPONENT16; |
| 74 | default: break; |
| 75 | } |
| 76 | return 0; |
| 77 | } |
| 78 | |
Jason Sams | 807fdc4 | 2012-07-25 17:55:39 -0700 | [diff] [blame] | 79 | uint8_t *GetOffsetPtr(const android::renderscript::Allocation *alloc, |
| 80 | uint32_t xoff, uint32_t yoff, uint32_t lod, |
| 81 | RsAllocationCubemapFace face) { |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 82 | uint8_t *ptr = (uint8_t *)alloc->mHal.drvState.lod[lod].mallocPtr; |
| 83 | ptr += face * alloc->mHal.drvState.faceOffset; |
| 84 | ptr += yoff * alloc->mHal.drvState.lod[lod].stride; |
Jason Sams | 807fdc4 | 2012-07-25 17:55:39 -0700 | [diff] [blame] | 85 | ptr += xoff * alloc->mHal.state.elementSizeBytes; |
| 86 | return ptr; |
| 87 | } |
| 88 | |
Jason Sams | a614ae1 | 2011-05-26 17:05:51 -0700 | [diff] [blame] | 89 | |
Jason Sams | 2382aba | 2011-09-13 15:41:01 -0700 | [diff] [blame] | 90 | static void Update2DTexture(const Context *rsc, const Allocation *alloc, const void *ptr, |
| 91 | uint32_t xoff, uint32_t yoff, uint32_t lod, |
| 92 | RsAllocationCubemapFace face, uint32_t w, uint32_t h) { |
Jason Sams | eb4fe18 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 93 | DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv; |
| 94 | |
Jason Sams | eb4fe18 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 95 | rsAssert(drv->textureID); |
Jason Sams | 2382aba | 2011-09-13 15:41:01 -0700 | [diff] [blame] | 96 | RSD_CALL_GL(glBindTexture, drv->glTarget, drv->textureID); |
| 97 | RSD_CALL_GL(glPixelStorei, GL_UNPACK_ALIGNMENT, 1); |
Jason Sams | eb4fe18 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 98 | GLenum t = GL_TEXTURE_2D; |
| 99 | if (alloc->mHal.state.hasFaces) { |
| 100 | t = gFaceOrder[face]; |
| 101 | } |
Jason Sams | 2382aba | 2011-09-13 15:41:01 -0700 | [diff] [blame] | 102 | RSD_CALL_GL(glTexSubImage2D, t, lod, xoff, yoff, w, h, drv->glFormat, drv->glType, ptr); |
Jason Sams | eb4fe18 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | |
| 106 | static void Upload2DTexture(const Context *rsc, const Allocation *alloc, bool isFirstUpload) { |
| 107 | DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv; |
| 108 | |
Jason Sams | 2382aba | 2011-09-13 15:41:01 -0700 | [diff] [blame] | 109 | RSD_CALL_GL(glBindTexture, drv->glTarget, drv->textureID); |
| 110 | RSD_CALL_GL(glPixelStorei, GL_UNPACK_ALIGNMENT, 1); |
Jason Sams | eb4fe18 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 111 | |
| 112 | uint32_t faceCount = 1; |
| 113 | if (alloc->mHal.state.hasFaces) { |
| 114 | faceCount = 6; |
| 115 | } |
| 116 | |
| 117 | rsdGLCheckError(rsc, "Upload2DTexture 1 "); |
| 118 | for (uint32_t face = 0; face < faceCount; face ++) { |
| 119 | for (uint32_t lod = 0; lod < alloc->mHal.state.type->getLODCount(); lod++) { |
Jason Sams | 807fdc4 | 2012-07-25 17:55:39 -0700 | [diff] [blame] | 120 | const uint8_t *p = GetOffsetPtr(alloc, 0, 0, lod, (RsAllocationCubemapFace)face); |
Jason Sams | eb4fe18 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 121 | |
| 122 | GLenum t = GL_TEXTURE_2D; |
| 123 | if (alloc->mHal.state.hasFaces) { |
| 124 | t = gFaceOrder[face]; |
| 125 | } |
| 126 | |
| 127 | if (isFirstUpload) { |
Jason Sams | 2382aba | 2011-09-13 15:41:01 -0700 | [diff] [blame] | 128 | RSD_CALL_GL(glTexImage2D, t, lod, drv->glFormat, |
Jason Sams | eb4fe18 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 129 | alloc->mHal.state.type->getLODDimX(lod), |
| 130 | alloc->mHal.state.type->getLODDimY(lod), |
Jason Sams | a614ae1 | 2011-05-26 17:05:51 -0700 | [diff] [blame] | 131 | 0, drv->glFormat, drv->glType, p); |
Jason Sams | eb4fe18 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 132 | } else { |
Jason Sams | 2382aba | 2011-09-13 15:41:01 -0700 | [diff] [blame] | 133 | RSD_CALL_GL(glTexSubImage2D, t, lod, 0, 0, |
Jason Sams | eb4fe18 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 134 | alloc->mHal.state.type->getLODDimX(lod), |
| 135 | alloc->mHal.state.type->getLODDimY(lod), |
Jason Sams | a614ae1 | 2011-05-26 17:05:51 -0700 | [diff] [blame] | 136 | drv->glFormat, drv->glType, p); |
Jason Sams | eb4fe18 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 137 | } |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | if (alloc->mHal.state.mipmapControl == RS_ALLOCATION_MIPMAP_ON_SYNC_TO_TEXTURE) { |
Jason Sams | 2382aba | 2011-09-13 15:41:01 -0700 | [diff] [blame] | 142 | RSD_CALL_GL(glGenerateMipmap, drv->glTarget); |
Jason Sams | eb4fe18 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 143 | } |
| 144 | rsdGLCheckError(rsc, "Upload2DTexture"); |
| 145 | } |
| 146 | |
| 147 | static void UploadToTexture(const Context *rsc, const Allocation *alloc) { |
| 148 | DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv; |
| 149 | |
Jason Sams | 3522f40 | 2012-03-23 11:47:26 -0700 | [diff] [blame] | 150 | if (alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_IO_INPUT) { |
Jason Sams | 41e373d | 2012-01-13 14:01:20 -0800 | [diff] [blame] | 151 | if (!drv->textureID) { |
| 152 | RSD_CALL_GL(glGenTextures, 1, &drv->textureID); |
| 153 | } |
| 154 | return; |
| 155 | } |
| 156 | |
Jason Sams | a614ae1 | 2011-05-26 17:05:51 -0700 | [diff] [blame] | 157 | if (!drv->glType || !drv->glFormat) { |
Jason Sams | eb4fe18 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 158 | return; |
| 159 | } |
| 160 | |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 161 | if (!alloc->mHal.drvState.lod[0].mallocPtr) { |
Jason Sams | eb4fe18 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 162 | return; |
| 163 | } |
| 164 | |
| 165 | bool isFirstUpload = false; |
| 166 | |
| 167 | if (!drv->textureID) { |
Jason Sams | 2382aba | 2011-09-13 15:41:01 -0700 | [diff] [blame] | 168 | RSD_CALL_GL(glGenTextures, 1, &drv->textureID); |
Jason Sams | eb4fe18 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 169 | isFirstUpload = true; |
| 170 | } |
| 171 | |
| 172 | Upload2DTexture(rsc, alloc, isFirstUpload); |
| 173 | |
| 174 | if (!(alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_SCRIPT)) { |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 175 | if (alloc->mHal.drvState.lod[0].mallocPtr) { |
| 176 | free(alloc->mHal.drvState.lod[0].mallocPtr); |
| 177 | alloc->mHal.drvState.lod[0].mallocPtr = NULL; |
Jason Sams | eb4fe18 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 178 | } |
| 179 | } |
| 180 | rsdGLCheckError(rsc, "UploadToTexture"); |
| 181 | } |
| 182 | |
| 183 | static void AllocateRenderTarget(const Context *rsc, const Allocation *alloc) { |
| 184 | DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv; |
| 185 | |
Jason Sams | a614ae1 | 2011-05-26 17:05:51 -0700 | [diff] [blame] | 186 | if (!drv->glFormat) { |
Jason Sams | eb4fe18 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 187 | return; |
| 188 | } |
| 189 | |
| 190 | if (!drv->renderTargetID) { |
Jason Sams | 2382aba | 2011-09-13 15:41:01 -0700 | [diff] [blame] | 191 | RSD_CALL_GL(glGenRenderbuffers, 1, &drv->renderTargetID); |
Jason Sams | eb4fe18 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 192 | |
| 193 | if (!drv->renderTargetID) { |
| 194 | // This should generally not happen |
Steve Block | af12ac6 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 195 | ALOGE("allocateRenderTarget failed to gen mRenderTargetID"); |
Jason Sams | eb4fe18 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 196 | rsc->dumpDebug(); |
| 197 | return; |
| 198 | } |
Jason Sams | 2382aba | 2011-09-13 15:41:01 -0700 | [diff] [blame] | 199 | RSD_CALL_GL(glBindRenderbuffer, GL_RENDERBUFFER, drv->renderTargetID); |
| 200 | RSD_CALL_GL(glRenderbufferStorage, GL_RENDERBUFFER, drv->glFormat, |
Jason Sams | eb4fe18 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 201 | alloc->mHal.state.dimensionX, alloc->mHal.state.dimensionY); |
| 202 | } |
| 203 | rsdGLCheckError(rsc, "AllocateRenderTarget"); |
| 204 | } |
| 205 | |
| 206 | static void UploadToBufferObject(const Context *rsc, const Allocation *alloc) { |
| 207 | DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv; |
| 208 | |
| 209 | rsAssert(!alloc->mHal.state.type->getDimY()); |
| 210 | rsAssert(!alloc->mHal.state.type->getDimZ()); |
| 211 | |
| 212 | //alloc->mHal.state.usageFlags |= RS_ALLOCATION_USAGE_GRAPHICS_VERTEX; |
| 213 | |
| 214 | if (!drv->bufferID) { |
Jason Sams | 2382aba | 2011-09-13 15:41:01 -0700 | [diff] [blame] | 215 | RSD_CALL_GL(glGenBuffers, 1, &drv->bufferID); |
Jason Sams | eb4fe18 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 216 | } |
| 217 | if (!drv->bufferID) { |
Steve Block | af12ac6 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 218 | ALOGE("Upload to buffer object failed"); |
Jason Sams | eb4fe18 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 219 | drv->uploadDeferred = true; |
| 220 | return; |
| 221 | } |
Jason Sams | 2382aba | 2011-09-13 15:41:01 -0700 | [diff] [blame] | 222 | RSD_CALL_GL(glBindBuffer, drv->glTarget, drv->bufferID); |
| 223 | RSD_CALL_GL(glBufferData, drv->glTarget, alloc->mHal.state.type->getSizeBytes(), |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 224 | alloc->mHal.drvState.lod[0].mallocPtr, GL_DYNAMIC_DRAW); |
Jason Sams | 2382aba | 2011-09-13 15:41:01 -0700 | [diff] [blame] | 225 | RSD_CALL_GL(glBindBuffer, drv->glTarget, 0); |
Jason Sams | eb4fe18 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 226 | rsdGLCheckError(rsc, "UploadToBufferObject"); |
| 227 | } |
| 228 | |
Jason Sams | 463bfce | 2012-08-21 13:54:42 -0700 | [diff] [blame] | 229 | static size_t AllocationBuildPointerTable(const Context *rsc, const Allocation *alloc, |
| 230 | const Type *type, uint8_t *ptr) { |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 231 | alloc->mHal.drvState.lod[0].dimX = type->getDimX(); |
| 232 | alloc->mHal.drvState.lod[0].dimY = type->getDimY(); |
Jason Sams | f2b611e | 2012-12-27 19:05:22 -0800 | [diff] [blame^] | 233 | alloc->mHal.drvState.lod[0].dimZ = type->getDimZ(); |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 234 | alloc->mHal.drvState.lod[0].mallocPtr = 0; |
| 235 | alloc->mHal.drvState.lod[0].stride = alloc->mHal.drvState.lod[0].dimX * type->getElementSizeBytes(); |
| 236 | alloc->mHal.drvState.lodCount = type->getLODCount(); |
| 237 | alloc->mHal.drvState.faceCount = type->getDimFaces(); |
Jason Sams | 807fdc4 | 2012-07-25 17:55:39 -0700 | [diff] [blame] | 238 | |
| 239 | size_t offsets[Allocation::MAX_LOD]; |
| 240 | memset(offsets, 0, sizeof(offsets)); |
| 241 | |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 242 | size_t o = alloc->mHal.drvState.lod[0].stride * rsMax(alloc->mHal.drvState.lod[0].dimY, 1u) * |
| 243 | rsMax(alloc->mHal.drvState.lod[0].dimZ, 1u); |
| 244 | if(alloc->mHal.drvState.lodCount > 1) { |
| 245 | uint32_t tx = alloc->mHal.drvState.lod[0].dimX; |
| 246 | uint32_t ty = alloc->mHal.drvState.lod[0].dimY; |
| 247 | uint32_t tz = alloc->mHal.drvState.lod[0].dimZ; |
| 248 | for (uint32_t lod=1; lod < alloc->mHal.drvState.lodCount; lod++) { |
| 249 | alloc->mHal.drvState.lod[lod].dimX = tx; |
| 250 | alloc->mHal.drvState.lod[lod].dimY = ty; |
| 251 | alloc->mHal.drvState.lod[lod].dimZ = tz; |
| 252 | alloc->mHal.drvState.lod[lod].stride = tx * type->getElementSizeBytes(); |
Jason Sams | 807fdc4 | 2012-07-25 17:55:39 -0700 | [diff] [blame] | 253 | offsets[lod] = o; |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 254 | o += alloc->mHal.drvState.lod[lod].stride * rsMax(ty, 1u) * rsMax(tz, 1u); |
Jason Sams | 807fdc4 | 2012-07-25 17:55:39 -0700 | [diff] [blame] | 255 | if (tx > 1) tx >>= 1; |
| 256 | if (ty > 1) ty >>= 1; |
| 257 | if (tz > 1) tz >>= 1; |
| 258 | } |
| 259 | } |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 260 | alloc->mHal.drvState.faceOffset = o; |
Jason Sams | 807fdc4 | 2012-07-25 17:55:39 -0700 | [diff] [blame] | 261 | |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 262 | alloc->mHal.drvState.lod[0].mallocPtr = ptr; |
| 263 | for (uint32_t lod=1; lod < alloc->mHal.drvState.lodCount; lod++) { |
| 264 | alloc->mHal.drvState.lod[lod].mallocPtr = ptr + offsets[lod]; |
Jason Sams | 463bfce | 2012-08-21 13:54:42 -0700 | [diff] [blame] | 265 | } |
Jason Sams | 463bfce | 2012-08-21 13:54:42 -0700 | [diff] [blame] | 266 | |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 267 | size_t allocSize = alloc->mHal.drvState.faceOffset; |
| 268 | if(alloc->mHal.drvState.faceCount) { |
Jason Sams | 463bfce | 2012-08-21 13:54:42 -0700 | [diff] [blame] | 269 | allocSize *= 6; |
| 270 | } |
| 271 | |
| 272 | return allocSize; |
| 273 | } |
| 274 | |
| 275 | bool rsdAllocationInit(const Context *rsc, Allocation *alloc, bool forceZero) { |
| 276 | DrvAllocation *drv = (DrvAllocation *)calloc(1, sizeof(DrvAllocation)); |
| 277 | if (!drv) { |
| 278 | return false; |
| 279 | } |
| 280 | alloc->mHal.drv = drv; |
| 281 | |
| 282 | // Calculate the object size. |
| 283 | size_t allocSize = AllocationBuildPointerTable(rsc, alloc, alloc->getType(), NULL); |
| 284 | |
Jason Sams | 807fdc4 | 2012-07-25 17:55:39 -0700 | [diff] [blame] | 285 | uint8_t * ptr = NULL; |
Jason Sams | 7ac2a4d | 2012-02-15 12:04:24 -0800 | [diff] [blame] | 286 | if (alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_IO_OUTPUT) { |
Tim Murray | 2e1a94d | 2012-11-29 13:12:25 -0800 | [diff] [blame] | 287 | } else if (alloc->mHal.state.userProvidedPtr != NULL) { |
| 288 | // user-provided allocation |
| 289 | // limitations: no faces, no LOD, USAGE_SCRIPT only |
| 290 | if (alloc->mHal.state.usageFlags != RS_ALLOCATION_USAGE_SCRIPT) { |
| 291 | ALOGE("Can't use user-allocated buffers if usage is not USAGE_SCRIPT"); |
| 292 | return false; |
| 293 | } |
| 294 | if (alloc->getType()->getDimLOD() || alloc->getType()->getDimFaces()) { |
| 295 | ALOGE("User-allocated buffers must not have multiple faces or LODs"); |
| 296 | return false; |
| 297 | } |
| 298 | ptr = (uint8_t*)alloc->mHal.state.userProvidedPtr; |
Jason Sams | 7ac2a4d | 2012-02-15 12:04:24 -0800 | [diff] [blame] | 299 | } else { |
Tim Murray | 2e1a94d | 2012-11-29 13:12:25 -0800 | [diff] [blame] | 300 | if (forceZero) { |
| 301 | ptr = (uint8_t *)calloc(1, allocSize); |
| 302 | } else { |
| 303 | ptr = (uint8_t *)malloc(allocSize); |
| 304 | } |
Jason Sams | 179e9a4 | 2011-11-23 15:02:15 -0800 | [diff] [blame] | 305 | if (!ptr) { |
You Kim | ed419f3 | 2012-12-17 03:42:57 +0900 | [diff] [blame] | 306 | alloc->mHal.drv = NULL; |
Jason Sams | 179e9a4 | 2011-11-23 15:02:15 -0800 | [diff] [blame] | 307 | free(drv); |
| 308 | return false; |
| 309 | } |
Jason Sams | eb4fe18 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 310 | } |
Jason Sams | 463bfce | 2012-08-21 13:54:42 -0700 | [diff] [blame] | 311 | // Build the pointer tables |
| 312 | size_t verifySize = AllocationBuildPointerTable(rsc, alloc, alloc->getType(), ptr); |
| 313 | if(allocSize != verifySize) { |
| 314 | rsAssert(!"Size mismatch"); |
Jason Sams | 807fdc4 | 2012-07-25 17:55:39 -0700 | [diff] [blame] | 315 | } |
Jason Sams | eb4fe18 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 316 | |
| 317 | drv->glTarget = GL_NONE; |
| 318 | if (alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_GRAPHICS_TEXTURE) { |
| 319 | if (alloc->mHal.state.hasFaces) { |
| 320 | drv->glTarget = GL_TEXTURE_CUBE_MAP; |
| 321 | } else { |
| 322 | drv->glTarget = GL_TEXTURE_2D; |
| 323 | } |
| 324 | } else { |
| 325 | if (alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_GRAPHICS_VERTEX) { |
| 326 | drv->glTarget = GL_ARRAY_BUFFER; |
| 327 | } |
| 328 | } |
| 329 | |
Jason Sams | a614ae1 | 2011-05-26 17:05:51 -0700 | [diff] [blame] | 330 | drv->glType = rsdTypeToGLType(alloc->mHal.state.type->getElement()->getComponent().getType()); |
| 331 | drv->glFormat = rsdKindToGLFormat(alloc->mHal.state.type->getElement()->getComponent().getKind()); |
| 332 | |
Jason Sams | eb4fe18 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 333 | if (alloc->mHal.state.usageFlags & ~RS_ALLOCATION_USAGE_SCRIPT) { |
| 334 | drv->uploadDeferred = true; |
| 335 | } |
Alex Sakhartchouk | a949524 | 2011-06-16 11:05:13 -0700 | [diff] [blame] | 336 | |
Jason Sams | b322033 | 2012-04-02 14:41:54 -0700 | [diff] [blame] | 337 | |
Alex Sakhartchouk | a949524 | 2011-06-16 11:05:13 -0700 | [diff] [blame] | 338 | drv->readBackFBO = NULL; |
| 339 | |
Jason Sams | eb4fe18 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 340 | return true; |
| 341 | } |
| 342 | |
| 343 | void rsdAllocationDestroy(const Context *rsc, Allocation *alloc) { |
| 344 | DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv; |
| 345 | |
| 346 | if (drv->bufferID) { |
| 347 | // Causes a SW crash.... |
Steve Block | 6598201 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 348 | //ALOGV(" mBufferID %i", mBufferID); |
Jason Sams | eb4fe18 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 349 | //glDeleteBuffers(1, &mBufferID); |
| 350 | //mBufferID = 0; |
| 351 | } |
| 352 | if (drv->textureID) { |
Jason Sams | 2382aba | 2011-09-13 15:41:01 -0700 | [diff] [blame] | 353 | RSD_CALL_GL(glDeleteTextures, 1, &drv->textureID); |
Jason Sams | eb4fe18 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 354 | drv->textureID = 0; |
| 355 | } |
| 356 | if (drv->renderTargetID) { |
Jason Sams | 2382aba | 2011-09-13 15:41:01 -0700 | [diff] [blame] | 357 | RSD_CALL_GL(glDeleteRenderbuffers, 1, &drv->renderTargetID); |
Jason Sams | eb4fe18 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 358 | drv->renderTargetID = 0; |
| 359 | } |
| 360 | |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 361 | if (alloc->mHal.drvState.lod[0].mallocPtr) { |
Tim Murray | 2e1a94d | 2012-11-29 13:12:25 -0800 | [diff] [blame] | 362 | // don't free user-allocated ptrs |
| 363 | if (!alloc->mHal.state.userProvidedPtr) { |
| 364 | free(alloc->mHal.drvState.lod[0].mallocPtr); |
| 365 | } |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 366 | alloc->mHal.drvState.lod[0].mallocPtr = NULL; |
Jason Sams | eb4fe18 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 367 | } |
Alex Sakhartchouk | a949524 | 2011-06-16 11:05:13 -0700 | [diff] [blame] | 368 | if (drv->readBackFBO != NULL) { |
| 369 | delete drv->readBackFBO; |
| 370 | drv->readBackFBO = NULL; |
| 371 | } |
Jason Sams | eb4fe18 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 372 | free(drv); |
| 373 | alloc->mHal.drv = NULL; |
| 374 | } |
| 375 | |
| 376 | void rsdAllocationResize(const Context *rsc, const Allocation *alloc, |
| 377 | const Type *newType, bool zeroNew) { |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 378 | void * oldPtr = alloc->mHal.drvState.lod[0].mallocPtr; |
Jason Sams | 463bfce | 2012-08-21 13:54:42 -0700 | [diff] [blame] | 379 | // Calculate the object size |
| 380 | size_t s = AllocationBuildPointerTable(rsc, alloc, newType, NULL); |
| 381 | uint8_t *ptr = (uint8_t *)realloc(oldPtr, s); |
| 382 | // Build the relative pointer tables. |
| 383 | size_t verifySize = AllocationBuildPointerTable(rsc, alloc, newType, ptr); |
| 384 | if(s != verifySize) { |
| 385 | rsAssert(!"Size mismatch"); |
| 386 | } |
Jason Sams | eb4fe18 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 387 | |
| 388 | const uint32_t oldDimX = alloc->mHal.state.dimensionX; |
| 389 | const uint32_t dimX = newType->getDimX(); |
| 390 | |
| 391 | if (dimX > oldDimX) { |
Jason Sams | 463bfce | 2012-08-21 13:54:42 -0700 | [diff] [blame] | 392 | uint32_t stride = alloc->mHal.state.elementSizeBytes; |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 393 | memset(((uint8_t *)alloc->mHal.drvState.lod[0].mallocPtr) + stride * oldDimX, |
Jason Sams | 2275e9c | 2012-04-16 17:01:26 -0700 | [diff] [blame] | 394 | 0, stride * (dimX - oldDimX)); |
Jason Sams | eb4fe18 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 395 | } |
| 396 | } |
| 397 | |
Alex Sakhartchouk | a949524 | 2011-06-16 11:05:13 -0700 | [diff] [blame] | 398 | static void rsdAllocationSyncFromFBO(const Context *rsc, const Allocation *alloc) { |
| 399 | if (!alloc->getIsScript()) { |
| 400 | return; // nothing to sync |
| 401 | } |
| 402 | |
| 403 | RsdHal *dc = (RsdHal *)rsc->mHal.drv; |
| 404 | RsdFrameBufferObj *lastFbo = dc->gl.currentFrameBuffer; |
| 405 | |
| 406 | DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv; |
| 407 | if (!drv->textureID && !drv->renderTargetID) { |
| 408 | return; // nothing was rendered here yet, so nothing to sync |
| 409 | } |
| 410 | if (drv->readBackFBO == NULL) { |
| 411 | drv->readBackFBO = new RsdFrameBufferObj(); |
| 412 | drv->readBackFBO->setColorTarget(drv, 0); |
| 413 | drv->readBackFBO->setDimensions(alloc->getType()->getDimX(), |
| 414 | alloc->getType()->getDimY()); |
| 415 | } |
| 416 | |
| 417 | // Bind the framebuffer object so we can read back from it |
| 418 | drv->readBackFBO->setActive(rsc); |
| 419 | |
| 420 | // Do the readback |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 421 | RSD_CALL_GL(glReadPixels, 0, 0, alloc->mHal.drvState.lod[0].dimX, |
| 422 | alloc->mHal.drvState.lod[0].dimY, |
| 423 | drv->glFormat, drv->glType, alloc->mHal.drvState.lod[0].mallocPtr); |
Alex Sakhartchouk | a949524 | 2011-06-16 11:05:13 -0700 | [diff] [blame] | 424 | |
| 425 | // Revert framebuffer to its original |
| 426 | lastFbo->setActive(rsc); |
| 427 | } |
Jason Sams | eb4fe18 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 428 | |
| 429 | |
| 430 | void rsdAllocationSyncAll(const Context *rsc, const Allocation *alloc, |
| 431 | RsAllocationUsageType src) { |
| 432 | DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv; |
| 433 | |
Alex Sakhartchouk | a949524 | 2011-06-16 11:05:13 -0700 | [diff] [blame] | 434 | if (src == RS_ALLOCATION_USAGE_GRAPHICS_RENDER_TARGET) { |
| 435 | if(!alloc->getIsRenderTarget()) { |
| 436 | rsc->setError(RS_ERROR_FATAL_DRIVER, |
| 437 | "Attempting to sync allocation from render target, " |
| 438 | "for non-render target allocation"); |
| 439 | } else if (alloc->getType()->getElement()->getKind() != RS_KIND_PIXEL_RGBA) { |
| 440 | rsc->setError(RS_ERROR_FATAL_DRIVER, "Cannot only sync from RGBA" |
| 441 | "render target"); |
| 442 | } else { |
| 443 | rsdAllocationSyncFromFBO(rsc, alloc); |
| 444 | } |
Jason Sams | eb4fe18 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 445 | return; |
| 446 | } |
| 447 | |
| 448 | rsAssert(src == RS_ALLOCATION_USAGE_SCRIPT); |
| 449 | |
| 450 | if (alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_GRAPHICS_TEXTURE) { |
| 451 | UploadToTexture(rsc, alloc); |
| 452 | } else { |
Jason Sams | b322033 | 2012-04-02 14:41:54 -0700 | [diff] [blame] | 453 | if ((alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_GRAPHICS_RENDER_TARGET) && |
Jason Sams | 0dc6693 | 2012-04-10 17:05:49 -0700 | [diff] [blame] | 454 | !(alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_IO_OUTPUT)) { |
Jason Sams | eb4fe18 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 455 | AllocateRenderTarget(rsc, alloc); |
| 456 | } |
| 457 | } |
| 458 | if (alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_GRAPHICS_VERTEX) { |
| 459 | UploadToBufferObject(rsc, alloc); |
| 460 | } |
| 461 | |
| 462 | drv->uploadDeferred = false; |
| 463 | } |
| 464 | |
| 465 | void rsdAllocationMarkDirty(const Context *rsc, const Allocation *alloc) { |
| 466 | DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv; |
| 467 | drv->uploadDeferred = true; |
| 468 | } |
| 469 | |
Jason Sams | 41e373d | 2012-01-13 14:01:20 -0800 | [diff] [blame] | 470 | int32_t rsdAllocationInitSurfaceTexture(const Context *rsc, const Allocation *alloc) { |
| 471 | DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv; |
| 472 | UploadToTexture(rsc, alloc); |
| 473 | return drv->textureID; |
| 474 | } |
| 475 | |
Jason Sams | 7ac2a4d | 2012-02-15 12:04:24 -0800 | [diff] [blame] | 476 | static bool IoGetBuffer(const Context *rsc, Allocation *alloc, ANativeWindow *nw) { |
| 477 | DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv; |
| 478 | |
Jamie Gennis | cd919a1 | 2012-06-13 16:34:11 -0700 | [diff] [blame] | 479 | int32_t r = native_window_dequeue_buffer_and_wait(nw, &drv->wndBuffer); |
Jason Sams | 7ac2a4d | 2012-02-15 12:04:24 -0800 | [diff] [blame] | 480 | if (r) { |
| 481 | rsc->setError(RS_ERROR_DRIVER, "Error getting next IO output buffer."); |
| 482 | return false; |
| 483 | } |
| 484 | |
Jason Sams | 7ac2a4d | 2012-02-15 12:04:24 -0800 | [diff] [blame] | 485 | // Must lock the whole surface |
| 486 | GraphicBufferMapper &mapper = GraphicBufferMapper::get(); |
| 487 | Rect bounds(drv->wndBuffer->width, drv->wndBuffer->height); |
| 488 | |
| 489 | void *dst = NULL; |
| 490 | mapper.lock(drv->wndBuffer->handle, |
| 491 | GRALLOC_USAGE_SW_READ_NEVER | GRALLOC_USAGE_SW_WRITE_OFTEN, |
| 492 | bounds, &dst); |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 493 | alloc->mHal.drvState.lod[0].mallocPtr = dst; |
| 494 | alloc->mHal.drvState.lod[0].stride = drv->wndBuffer->stride * alloc->mHal.state.elementSizeBytes; |
Jason Sams | b322033 | 2012-04-02 14:41:54 -0700 | [diff] [blame] | 495 | |
Jason Sams | 7ac2a4d | 2012-02-15 12:04:24 -0800 | [diff] [blame] | 496 | return true; |
| 497 | } |
| 498 | |
| 499 | void rsdAllocationSetSurfaceTexture(const Context *rsc, Allocation *alloc, ANativeWindow *nw) { |
| 500 | DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv; |
| 501 | |
| 502 | //ALOGE("rsdAllocationSetSurfaceTexture %p %p", alloc, nw); |
| 503 | |
Jason Sams | b322033 | 2012-04-02 14:41:54 -0700 | [diff] [blame] | 504 | if (alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_GRAPHICS_RENDER_TARGET) { |
| 505 | //TODO finish support for render target + script |
| 506 | drv->wnd = nw; |
| 507 | return; |
| 508 | } |
| 509 | |
| 510 | |
Jason Sams | 7ac2a4d | 2012-02-15 12:04:24 -0800 | [diff] [blame] | 511 | // Cleanup old surface if there is one. |
| 512 | if (alloc->mHal.state.wndSurface) { |
| 513 | ANativeWindow *old = alloc->mHal.state.wndSurface; |
| 514 | GraphicBufferMapper &mapper = GraphicBufferMapper::get(); |
| 515 | mapper.unlock(drv->wndBuffer->handle); |
Jamie Gennis | cd919a1 | 2012-06-13 16:34:11 -0700 | [diff] [blame] | 516 | old->queueBuffer(old, drv->wndBuffer, -1); |
Jason Sams | 7ac2a4d | 2012-02-15 12:04:24 -0800 | [diff] [blame] | 517 | } |
| 518 | |
| 519 | if (nw != NULL) { |
| 520 | int32_t r; |
Jason Sams | b322033 | 2012-04-02 14:41:54 -0700 | [diff] [blame] | 521 | uint32_t flags = 0; |
| 522 | if (alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_SCRIPT) { |
| 523 | flags |= GRALLOC_USAGE_SW_READ_RARELY | GRALLOC_USAGE_SW_WRITE_OFTEN; |
| 524 | } |
| 525 | if (alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_GRAPHICS_RENDER_TARGET) { |
| 526 | flags |= GRALLOC_USAGE_HW_RENDER; |
| 527 | } |
| 528 | |
| 529 | r = native_window_set_usage(nw, flags); |
Jason Sams | 7ac2a4d | 2012-02-15 12:04:24 -0800 | [diff] [blame] | 530 | if (r) { |
| 531 | rsc->setError(RS_ERROR_DRIVER, "Error setting IO output buffer usage."); |
| 532 | return; |
| 533 | } |
| 534 | |
| 535 | r = native_window_set_buffers_dimensions(nw, alloc->mHal.state.dimensionX, |
| 536 | alloc->mHal.state.dimensionY); |
| 537 | if (r) { |
| 538 | rsc->setError(RS_ERROR_DRIVER, "Error setting IO output buffer dimensions."); |
| 539 | return; |
| 540 | } |
| 541 | |
| 542 | r = native_window_set_buffer_count(nw, 3); |
| 543 | if (r) { |
| 544 | rsc->setError(RS_ERROR_DRIVER, "Error setting IO output buffer count."); |
| 545 | return; |
| 546 | } |
| 547 | |
| 548 | IoGetBuffer(rsc, alloc, nw); |
| 549 | } |
| 550 | } |
| 551 | |
| 552 | void rsdAllocationIoSend(const Context *rsc, Allocation *alloc) { |
| 553 | DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv; |
| 554 | ANativeWindow *nw = alloc->mHal.state.wndSurface; |
| 555 | |
Jason Sams | b322033 | 2012-04-02 14:41:54 -0700 | [diff] [blame] | 556 | if (alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_GRAPHICS_RENDER_TARGET) { |
| 557 | RsdHal *dc = (RsdHal *)rsc->mHal.drv; |
| 558 | RSD_CALL_GL(eglSwapBuffers, dc->gl.egl.display, dc->gl.egl.surface); |
Jason Sams | 7ac2a4d | 2012-02-15 12:04:24 -0800 | [diff] [blame] | 559 | return; |
| 560 | } |
| 561 | |
Jason Sams | b322033 | 2012-04-02 14:41:54 -0700 | [diff] [blame] | 562 | if (alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_SCRIPT) { |
| 563 | GraphicBufferMapper &mapper = GraphicBufferMapper::get(); |
| 564 | mapper.unlock(drv->wndBuffer->handle); |
Jamie Gennis | cd919a1 | 2012-06-13 16:34:11 -0700 | [diff] [blame] | 565 | int32_t r = nw->queueBuffer(nw, drv->wndBuffer, -1); |
Jason Sams | b322033 | 2012-04-02 14:41:54 -0700 | [diff] [blame] | 566 | if (r) { |
| 567 | rsc->setError(RS_ERROR_DRIVER, "Error sending IO output buffer."); |
| 568 | return; |
| 569 | } |
| 570 | |
| 571 | IoGetBuffer(rsc, alloc, nw); |
| 572 | } |
Jason Sams | 7ac2a4d | 2012-02-15 12:04:24 -0800 | [diff] [blame] | 573 | } |
| 574 | |
| 575 | void rsdAllocationIoReceive(const Context *rsc, Allocation *alloc) { |
Jason Sams | 3522f40 | 2012-03-23 11:47:26 -0700 | [diff] [blame] | 576 | DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv; |
| 577 | alloc->mHal.state.surfaceTexture->updateTexImage(); |
Jason Sams | 7ac2a4d | 2012-02-15 12:04:24 -0800 | [diff] [blame] | 578 | } |
| 579 | |
| 580 | |
Jason Sams | eb4fe18 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 581 | void rsdAllocationData1D(const Context *rsc, const Allocation *alloc, |
| 582 | uint32_t xoff, uint32_t lod, uint32_t count, |
Alex Sakhartchouk | c794cd5 | 2012-02-13 11:57:32 -0800 | [diff] [blame] | 583 | const void *data, size_t sizeBytes) { |
Jason Sams | eb4fe18 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 584 | DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv; |
| 585 | |
| 586 | const uint32_t eSize = alloc->mHal.state.type->getElementSizeBytes(); |
Jason Sams | 807fdc4 | 2012-07-25 17:55:39 -0700 | [diff] [blame] | 587 | uint8_t * ptr = GetOffsetPtr(alloc, xoff, 0, 0, RS_ALLOCATION_CUBEMAP_FACE_POSITIVE_X); |
Jason Sams | eb4fe18 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 588 | uint32_t size = count * eSize; |
| 589 | |
Stephen Hines | 20c535f | 2012-12-06 19:04:38 -0800 | [diff] [blame] | 590 | if (ptr != data) { |
| 591 | // Skip the copy if we are the same allocation. This can arise from |
| 592 | // our Bitmap optimization, where we share the same storage. |
| 593 | if (alloc->mHal.state.hasReferences) { |
| 594 | alloc->incRefs(data, count); |
| 595 | alloc->decRefs(ptr, count); |
| 596 | } |
| 597 | memcpy(ptr, data, size); |
Jason Sams | eb4fe18 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 598 | } |
Jason Sams | eb4fe18 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 599 | drv->uploadDeferred = true; |
| 600 | } |
| 601 | |
| 602 | void rsdAllocationData2D(const Context *rsc, const Allocation *alloc, |
| 603 | uint32_t xoff, uint32_t yoff, uint32_t lod, RsAllocationCubemapFace face, |
Tim Murray | 358747a | 2012-11-26 13:52:04 -0800 | [diff] [blame] | 604 | uint32_t w, uint32_t h, const void *data, size_t sizeBytes, size_t stride) { |
Jason Sams | eb4fe18 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 605 | DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv; |
| 606 | |
| 607 | uint32_t eSize = alloc->mHal.state.elementSizeBytes; |
| 608 | uint32_t lineSize = eSize * w; |
Tim Murray | 358747a | 2012-11-26 13:52:04 -0800 | [diff] [blame] | 609 | if (!stride) { |
| 610 | stride = lineSize; |
| 611 | } |
Jason Sams | eb4fe18 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 612 | |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 613 | if (alloc->mHal.drvState.lod[0].mallocPtr) { |
Jason Sams | eb4fe18 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 614 | const uint8_t *src = static_cast<const uint8_t *>(data); |
Jason Sams | 807fdc4 | 2012-07-25 17:55:39 -0700 | [diff] [blame] | 615 | uint8_t *dst = GetOffsetPtr(alloc, xoff, yoff, lod, face); |
Stephen Hines | 20c535f | 2012-12-06 19:04:38 -0800 | [diff] [blame] | 616 | if (dst == src) { |
| 617 | // Skip the copy if we are the same allocation. This can arise from |
| 618 | // our Bitmap optimization, where we share the same storage. |
| 619 | drv->uploadDeferred = true; |
| 620 | return; |
| 621 | } |
Jason Sams | eb4fe18 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 622 | |
| 623 | for (uint32_t line=yoff; line < (yoff+h); line++) { |
| 624 | if (alloc->mHal.state.hasReferences) { |
| 625 | alloc->incRefs(src, w); |
| 626 | alloc->decRefs(dst, w); |
| 627 | } |
| 628 | memcpy(dst, src, lineSize); |
Tim Murray | 358747a | 2012-11-26 13:52:04 -0800 | [diff] [blame] | 629 | src += stride; |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 630 | dst += alloc->mHal.drvState.lod[lod].stride; |
Jason Sams | eb4fe18 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 631 | } |
| 632 | drv->uploadDeferred = true; |
| 633 | } else { |
Jason Sams | 2382aba | 2011-09-13 15:41:01 -0700 | [diff] [blame] | 634 | Update2DTexture(rsc, alloc, data, xoff, yoff, lod, face, w, h); |
Jason Sams | eb4fe18 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 635 | } |
| 636 | } |
| 637 | |
| 638 | void rsdAllocationData3D(const Context *rsc, const Allocation *alloc, |
| 639 | uint32_t xoff, uint32_t yoff, uint32_t zoff, |
| 640 | uint32_t lod, RsAllocationCubemapFace face, |
| 641 | uint32_t w, uint32_t h, uint32_t d, const void *data, uint32_t sizeBytes) { |
| 642 | |
| 643 | } |
| 644 | |
Jason Sams | 807fdc4 | 2012-07-25 17:55:39 -0700 | [diff] [blame] | 645 | void rsdAllocationRead1D(const Context *rsc, const Allocation *alloc, |
| 646 | uint32_t xoff, uint32_t lod, uint32_t count, |
| 647 | void *data, size_t sizeBytes) { |
Jason Sams | 807fdc4 | 2012-07-25 17:55:39 -0700 | [diff] [blame] | 648 | const uint32_t eSize = alloc->mHal.state.type->getElementSizeBytes(); |
| 649 | const uint8_t * ptr = GetOffsetPtr(alloc, xoff, 0, 0, RS_ALLOCATION_CUBEMAP_FACE_POSITIVE_X); |
Stephen Hines | 20c535f | 2012-12-06 19:04:38 -0800 | [diff] [blame] | 650 | if (data != ptr) { |
| 651 | // Skip the copy if we are the same allocation. This can arise from |
| 652 | // our Bitmap optimization, where we share the same storage. |
| 653 | memcpy(data, ptr, count * eSize); |
| 654 | } |
Jason Sams | 807fdc4 | 2012-07-25 17:55:39 -0700 | [diff] [blame] | 655 | } |
| 656 | |
| 657 | void rsdAllocationRead2D(const Context *rsc, const Allocation *alloc, |
Tim Murray | 358747a | 2012-11-26 13:52:04 -0800 | [diff] [blame] | 658 | uint32_t xoff, uint32_t yoff, uint32_t lod, RsAllocationCubemapFace face, |
| 659 | uint32_t w, uint32_t h, void *data, size_t sizeBytes, size_t stride) { |
Jason Sams | 807fdc4 | 2012-07-25 17:55:39 -0700 | [diff] [blame] | 660 | uint32_t eSize = alloc->mHal.state.elementSizeBytes; |
| 661 | uint32_t lineSize = eSize * w; |
Tim Murray | 358747a | 2012-11-26 13:52:04 -0800 | [diff] [blame] | 662 | if (!stride) { |
| 663 | stride = lineSize; |
| 664 | } |
Jason Sams | 807fdc4 | 2012-07-25 17:55:39 -0700 | [diff] [blame] | 665 | |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 666 | if (alloc->mHal.drvState.lod[0].mallocPtr) { |
Jason Sams | 807fdc4 | 2012-07-25 17:55:39 -0700 | [diff] [blame] | 667 | uint8_t *dst = static_cast<uint8_t *>(data); |
| 668 | const uint8_t *src = GetOffsetPtr(alloc, xoff, yoff, lod, face); |
Stephen Hines | 20c535f | 2012-12-06 19:04:38 -0800 | [diff] [blame] | 669 | if (dst == src) { |
| 670 | // Skip the copy if we are the same allocation. This can arise from |
| 671 | // our Bitmap optimization, where we share the same storage. |
| 672 | return; |
| 673 | } |
Jason Sams | 807fdc4 | 2012-07-25 17:55:39 -0700 | [diff] [blame] | 674 | |
| 675 | for (uint32_t line=yoff; line < (yoff+h); line++) { |
| 676 | memcpy(dst, src, lineSize); |
Tim Murray | 358747a | 2012-11-26 13:52:04 -0800 | [diff] [blame] | 677 | dst += stride; |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 678 | src += alloc->mHal.drvState.lod[lod].stride; |
Jason Sams | 807fdc4 | 2012-07-25 17:55:39 -0700 | [diff] [blame] | 679 | } |
| 680 | } else { |
| 681 | ALOGE("Add code to readback from non-script memory"); |
| 682 | } |
| 683 | } |
| 684 | |
Tim Murray | 358747a | 2012-11-26 13:52:04 -0800 | [diff] [blame] | 685 | |
Jason Sams | 807fdc4 | 2012-07-25 17:55:39 -0700 | [diff] [blame] | 686 | void rsdAllocationRead3D(const Context *rsc, const Allocation *alloc, |
| 687 | uint32_t xoff, uint32_t yoff, uint32_t zoff, |
| 688 | uint32_t lod, RsAllocationCubemapFace face, |
| 689 | uint32_t w, uint32_t h, uint32_t d, void *data, uint32_t sizeBytes) { |
| 690 | |
| 691 | } |
| 692 | |
| 693 | void * rsdAllocationLock1D(const android::renderscript::Context *rsc, |
| 694 | const android::renderscript::Allocation *alloc) { |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 695 | return alloc->mHal.drvState.lod[0].mallocPtr; |
Jason Sams | 807fdc4 | 2012-07-25 17:55:39 -0700 | [diff] [blame] | 696 | } |
| 697 | |
| 698 | void rsdAllocationUnlock1D(const android::renderscript::Context *rsc, |
| 699 | const android::renderscript::Allocation *alloc) { |
| 700 | |
| 701 | } |
| 702 | |
Alex Sakhartchouk | 74a8279 | 2011-06-14 11:13:19 -0700 | [diff] [blame] | 703 | void rsdAllocationData1D_alloc(const android::renderscript::Context *rsc, |
| 704 | const android::renderscript::Allocation *dstAlloc, |
| 705 | uint32_t dstXoff, uint32_t dstLod, uint32_t count, |
| 706 | const android::renderscript::Allocation *srcAlloc, |
Alex Sakhartchouk | a949524 | 2011-06-16 11:05:13 -0700 | [diff] [blame] | 707 | uint32_t srcXoff, uint32_t srcLod) { |
| 708 | } |
| 709 | |
Alex Sakhartchouk | a949524 | 2011-06-16 11:05:13 -0700 | [diff] [blame] | 710 | |
| 711 | void rsdAllocationData2D_alloc_script(const android::renderscript::Context *rsc, |
| 712 | const android::renderscript::Allocation *dstAlloc, |
| 713 | uint32_t dstXoff, uint32_t dstYoff, uint32_t dstLod, |
| 714 | RsAllocationCubemapFace dstFace, uint32_t w, uint32_t h, |
| 715 | const android::renderscript::Allocation *srcAlloc, |
| 716 | uint32_t srcXoff, uint32_t srcYoff, uint32_t srcLod, |
| 717 | RsAllocationCubemapFace srcFace) { |
| 718 | uint32_t elementSize = dstAlloc->getType()->getElementSizeBytes(); |
| 719 | for (uint32_t i = 0; i < h; i ++) { |
Jason Sams | 807fdc4 | 2012-07-25 17:55:39 -0700 | [diff] [blame] | 720 | uint8_t *dstPtr = GetOffsetPtr(dstAlloc, dstXoff, dstYoff + i, dstLod, dstFace); |
| 721 | uint8_t *srcPtr = GetOffsetPtr(srcAlloc, srcXoff, srcYoff + i, srcLod, srcFace); |
Alex Sakhartchouk | a949524 | 2011-06-16 11:05:13 -0700 | [diff] [blame] | 722 | memcpy(dstPtr, srcPtr, w * elementSize); |
| 723 | |
Steve Block | af12ac6 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 724 | //ALOGE("COPIED dstXoff(%u), dstYoff(%u), dstLod(%u), dstFace(%u), w(%u), h(%u), srcXoff(%u), srcYoff(%u), srcLod(%u), srcFace(%u)", |
Stephen Hines | 0a44ab4 | 2011-06-23 16:18:28 -0700 | [diff] [blame] | 725 | // dstXoff, dstYoff, dstLod, dstFace, w, h, srcXoff, srcYoff, srcLod, srcFace); |
Alex Sakhartchouk | a949524 | 2011-06-16 11:05:13 -0700 | [diff] [blame] | 726 | } |
Alex Sakhartchouk | 74a8279 | 2011-06-14 11:13:19 -0700 | [diff] [blame] | 727 | } |
| 728 | |
| 729 | void rsdAllocationData2D_alloc(const android::renderscript::Context *rsc, |
| 730 | const android::renderscript::Allocation *dstAlloc, |
| 731 | uint32_t dstXoff, uint32_t dstYoff, uint32_t dstLod, |
| 732 | RsAllocationCubemapFace dstFace, uint32_t w, uint32_t h, |
| 733 | const android::renderscript::Allocation *srcAlloc, |
| 734 | uint32_t srcXoff, uint32_t srcYoff, uint32_t srcLod, |
| 735 | RsAllocationCubemapFace srcFace) { |
Alex Sakhartchouk | a949524 | 2011-06-16 11:05:13 -0700 | [diff] [blame] | 736 | if (!dstAlloc->getIsScript() && !srcAlloc->getIsScript()) { |
| 737 | rsc->setError(RS_ERROR_FATAL_DRIVER, "Non-script allocation copies not " |
| 738 | "yet implemented."); |
| 739 | return; |
| 740 | } |
| 741 | rsdAllocationData2D_alloc_script(rsc, dstAlloc, dstXoff, dstYoff, |
| 742 | dstLod, dstFace, w, h, srcAlloc, |
| 743 | srcXoff, srcYoff, srcLod, srcFace); |
Alex Sakhartchouk | 74a8279 | 2011-06-14 11:13:19 -0700 | [diff] [blame] | 744 | } |
| 745 | |
| 746 | void rsdAllocationData3D_alloc(const android::renderscript::Context *rsc, |
| 747 | const android::renderscript::Allocation *dstAlloc, |
| 748 | uint32_t dstXoff, uint32_t dstYoff, uint32_t dstZoff, |
| 749 | uint32_t dstLod, RsAllocationCubemapFace dstFace, |
| 750 | uint32_t w, uint32_t h, uint32_t d, |
| 751 | const android::renderscript::Allocation *srcAlloc, |
| 752 | uint32_t srcXoff, uint32_t srcYoff, uint32_t srcZoff, |
| 753 | uint32_t srcLod, RsAllocationCubemapFace srcFace) { |
| 754 | } |
| 755 | |
Jason Sams | eb4fe18 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 756 | void rsdAllocationElementData1D(const Context *rsc, const Allocation *alloc, |
| 757 | uint32_t x, |
| 758 | const void *data, uint32_t cIdx, uint32_t sizeBytes) { |
| 759 | DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv; |
| 760 | |
| 761 | uint32_t eSize = alloc->mHal.state.elementSizeBytes; |
Jason Sams | 807fdc4 | 2012-07-25 17:55:39 -0700 | [diff] [blame] | 762 | uint8_t * ptr = GetOffsetPtr(alloc, x, 0, 0, RS_ALLOCATION_CUBEMAP_FACE_POSITIVE_X); |
Jason Sams | eb4fe18 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 763 | |
| 764 | const Element * e = alloc->mHal.state.type->getElement()->getField(cIdx); |
| 765 | ptr += alloc->mHal.state.type->getElement()->getFieldOffsetBytes(cIdx); |
| 766 | |
| 767 | if (alloc->mHal.state.hasReferences) { |
| 768 | e->incRefs(data); |
| 769 | e->decRefs(ptr); |
| 770 | } |
| 771 | |
| 772 | memcpy(ptr, data, sizeBytes); |
| 773 | drv->uploadDeferred = true; |
| 774 | } |
| 775 | |
| 776 | void rsdAllocationElementData2D(const Context *rsc, const Allocation *alloc, |
| 777 | uint32_t x, uint32_t y, |
| 778 | const void *data, uint32_t cIdx, uint32_t sizeBytes) { |
| 779 | DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv; |
| 780 | |
| 781 | uint32_t eSize = alloc->mHal.state.elementSizeBytes; |
Jason Sams | 807fdc4 | 2012-07-25 17:55:39 -0700 | [diff] [blame] | 782 | uint8_t * ptr = GetOffsetPtr(alloc, x, y, 0, RS_ALLOCATION_CUBEMAP_FACE_POSITIVE_X); |
Jason Sams | eb4fe18 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 783 | |
| 784 | const Element * e = alloc->mHal.state.type->getElement()->getField(cIdx); |
| 785 | ptr += alloc->mHal.state.type->getElement()->getFieldOffsetBytes(cIdx); |
| 786 | |
| 787 | if (alloc->mHal.state.hasReferences) { |
| 788 | e->incRefs(data); |
| 789 | e->decRefs(ptr); |
| 790 | } |
| 791 | |
| 792 | memcpy(ptr, data, sizeBytes); |
| 793 | drv->uploadDeferred = true; |
| 794 | } |
| 795 | |
Jason Sams | 61a4bb7 | 2012-07-25 19:33:43 -0700 | [diff] [blame] | 796 | static void mip565(const Allocation *alloc, int lod, RsAllocationCubemapFace face) { |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 797 | uint32_t w = alloc->mHal.drvState.lod[lod + 1].dimX; |
| 798 | uint32_t h = alloc->mHal.drvState.lod[lod + 1].dimY; |
Jason Sams | 61a4bb7 | 2012-07-25 19:33:43 -0700 | [diff] [blame] | 799 | |
| 800 | for (uint32_t y=0; y < h; y++) { |
| 801 | uint16_t *oPtr = (uint16_t *)GetOffsetPtr(alloc, 0, y, lod + 1, face); |
| 802 | const uint16_t *i1 = (uint16_t *)GetOffsetPtr(alloc, 0, y*2, lod, face); |
| 803 | const uint16_t *i2 = (uint16_t *)GetOffsetPtr(alloc, 0, y*2+1, lod, face); |
| 804 | |
| 805 | for (uint32_t x=0; x < w; x++) { |
| 806 | *oPtr = rsBoxFilter565(i1[0], i1[1], i2[0], i2[1]); |
| 807 | oPtr ++; |
| 808 | i1 += 2; |
| 809 | i2 += 2; |
| 810 | } |
| 811 | } |
| 812 | } |
| 813 | |
| 814 | static void mip8888(const Allocation *alloc, int lod, RsAllocationCubemapFace face) { |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 815 | uint32_t w = alloc->mHal.drvState.lod[lod + 1].dimX; |
| 816 | uint32_t h = alloc->mHal.drvState.lod[lod + 1].dimY; |
Jason Sams | 61a4bb7 | 2012-07-25 19:33:43 -0700 | [diff] [blame] | 817 | |
| 818 | for (uint32_t y=0; y < h; y++) { |
| 819 | uint32_t *oPtr = (uint32_t *)GetOffsetPtr(alloc, 0, y, lod + 1, face); |
| 820 | const uint32_t *i1 = (uint32_t *)GetOffsetPtr(alloc, 0, y*2, lod, face); |
| 821 | const uint32_t *i2 = (uint32_t *)GetOffsetPtr(alloc, 0, y*2+1, lod, face); |
| 822 | |
| 823 | for (uint32_t x=0; x < w; x++) { |
| 824 | *oPtr = rsBoxFilter8888(i1[0], i1[1], i2[0], i2[1]); |
| 825 | oPtr ++; |
| 826 | i1 += 2; |
| 827 | i2 += 2; |
| 828 | } |
| 829 | } |
| 830 | } |
| 831 | |
| 832 | static void mip8(const Allocation *alloc, int lod, RsAllocationCubemapFace face) { |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 833 | uint32_t w = alloc->mHal.drvState.lod[lod + 1].dimX; |
| 834 | uint32_t h = alloc->mHal.drvState.lod[lod + 1].dimY; |
Jason Sams | 61a4bb7 | 2012-07-25 19:33:43 -0700 | [diff] [blame] | 835 | |
| 836 | for (uint32_t y=0; y < h; y++) { |
| 837 | uint8_t *oPtr = GetOffsetPtr(alloc, 0, y, lod + 1, face); |
| 838 | const uint8_t *i1 = GetOffsetPtr(alloc, 0, y*2, lod, face); |
| 839 | const uint8_t *i2 = GetOffsetPtr(alloc, 0, y*2+1, lod, face); |
| 840 | |
| 841 | for (uint32_t x=0; x < w; x++) { |
| 842 | *oPtr = (uint8_t)(((uint32_t)i1[0] + i1[1] + i2[0] + i2[1]) * 0.25f); |
| 843 | oPtr ++; |
| 844 | i1 += 2; |
| 845 | i2 += 2; |
| 846 | } |
| 847 | } |
| 848 | } |
| 849 | |
| 850 | void rsdAllocationGenerateMipmaps(const Context *rsc, const Allocation *alloc) { |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 851 | if(!alloc->mHal.drvState.lod[0].mallocPtr) { |
Jason Sams | 61a4bb7 | 2012-07-25 19:33:43 -0700 | [diff] [blame] | 852 | return; |
| 853 | } |
| 854 | uint32_t numFaces = alloc->getType()->getDimFaces() ? 6 : 1; |
| 855 | for (uint32_t face = 0; face < numFaces; face ++) { |
| 856 | for (uint32_t lod=0; lod < (alloc->getType()->getLODCount() -1); lod++) { |
| 857 | switch (alloc->getType()->getElement()->getSizeBits()) { |
| 858 | case 32: |
| 859 | mip8888(alloc, lod, (RsAllocationCubemapFace)face); |
| 860 | break; |
| 861 | case 16: |
| 862 | mip565(alloc, lod, (RsAllocationCubemapFace)face); |
| 863 | break; |
| 864 | case 8: |
| 865 | mip8(alloc, lod, (RsAllocationCubemapFace)face); |
| 866 | break; |
| 867 | } |
| 868 | } |
| 869 | } |
| 870 | } |
| 871 | |
Jason Sams | eb4fe18 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 872 | |