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(); |
| 233 | alloc->mHal.drvState.lod[0].mallocPtr = 0; |
| 234 | alloc->mHal.drvState.lod[0].stride = alloc->mHal.drvState.lod[0].dimX * type->getElementSizeBytes(); |
| 235 | alloc->mHal.drvState.lodCount = type->getLODCount(); |
| 236 | alloc->mHal.drvState.faceCount = type->getDimFaces(); |
Jason Sams | 807fdc4 | 2012-07-25 17:55:39 -0700 | [diff] [blame] | 237 | |
| 238 | size_t offsets[Allocation::MAX_LOD]; |
| 239 | memset(offsets, 0, sizeof(offsets)); |
| 240 | |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 241 | size_t o = alloc->mHal.drvState.lod[0].stride * rsMax(alloc->mHal.drvState.lod[0].dimY, 1u) * |
| 242 | rsMax(alloc->mHal.drvState.lod[0].dimZ, 1u); |
| 243 | if(alloc->mHal.drvState.lodCount > 1) { |
| 244 | uint32_t tx = alloc->mHal.drvState.lod[0].dimX; |
| 245 | uint32_t ty = alloc->mHal.drvState.lod[0].dimY; |
| 246 | uint32_t tz = alloc->mHal.drvState.lod[0].dimZ; |
| 247 | for (uint32_t lod=1; lod < alloc->mHal.drvState.lodCount; lod++) { |
| 248 | alloc->mHal.drvState.lod[lod].dimX = tx; |
| 249 | alloc->mHal.drvState.lod[lod].dimY = ty; |
| 250 | alloc->mHal.drvState.lod[lod].dimZ = tz; |
| 251 | alloc->mHal.drvState.lod[lod].stride = tx * type->getElementSizeBytes(); |
Jason Sams | 807fdc4 | 2012-07-25 17:55:39 -0700 | [diff] [blame] | 252 | offsets[lod] = o; |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 253 | 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] | 254 | if (tx > 1) tx >>= 1; |
| 255 | if (ty > 1) ty >>= 1; |
| 256 | if (tz > 1) tz >>= 1; |
| 257 | } |
| 258 | } |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 259 | alloc->mHal.drvState.faceOffset = o; |
Jason Sams | 807fdc4 | 2012-07-25 17:55:39 -0700 | [diff] [blame] | 260 | |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 261 | alloc->mHal.drvState.lod[0].mallocPtr = ptr; |
| 262 | for (uint32_t lod=1; lod < alloc->mHal.drvState.lodCount; lod++) { |
| 263 | alloc->mHal.drvState.lod[lod].mallocPtr = ptr + offsets[lod]; |
Jason Sams | 463bfce | 2012-08-21 13:54:42 -0700 | [diff] [blame] | 264 | } |
Jason Sams | 463bfce | 2012-08-21 13:54:42 -0700 | [diff] [blame] | 265 | |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 266 | size_t allocSize = alloc->mHal.drvState.faceOffset; |
| 267 | if(alloc->mHal.drvState.faceCount) { |
Jason Sams | 463bfce | 2012-08-21 13:54:42 -0700 | [diff] [blame] | 268 | allocSize *= 6; |
| 269 | } |
| 270 | |
| 271 | return allocSize; |
| 272 | } |
| 273 | |
| 274 | bool rsdAllocationInit(const Context *rsc, Allocation *alloc, bool forceZero) { |
| 275 | DrvAllocation *drv = (DrvAllocation *)calloc(1, sizeof(DrvAllocation)); |
| 276 | if (!drv) { |
| 277 | return false; |
| 278 | } |
| 279 | alloc->mHal.drv = drv; |
| 280 | |
| 281 | // Calculate the object size. |
| 282 | size_t allocSize = AllocationBuildPointerTable(rsc, alloc, alloc->getType(), NULL); |
| 283 | |
Jason Sams | 807fdc4 | 2012-07-25 17:55:39 -0700 | [diff] [blame] | 284 | uint8_t * ptr = NULL; |
Jason Sams | 7ac2a4d | 2012-02-15 12:04:24 -0800 | [diff] [blame] | 285 | if (alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_IO_OUTPUT) { |
| 286 | } else { |
Jason Sams | 807fdc4 | 2012-07-25 17:55:39 -0700 | [diff] [blame] | 287 | |
| 288 | ptr = (uint8_t *)malloc(allocSize); |
Jason Sams | 179e9a4 | 2011-11-23 15:02:15 -0800 | [diff] [blame] | 289 | if (!ptr) { |
| 290 | free(drv); |
| 291 | return false; |
| 292 | } |
Jason Sams | eb4fe18 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 293 | } |
Jason Sams | 463bfce | 2012-08-21 13:54:42 -0700 | [diff] [blame] | 294 | // Build the pointer tables |
| 295 | size_t verifySize = AllocationBuildPointerTable(rsc, alloc, alloc->getType(), ptr); |
| 296 | if(allocSize != verifySize) { |
| 297 | rsAssert(!"Size mismatch"); |
Jason Sams | 807fdc4 | 2012-07-25 17:55:39 -0700 | [diff] [blame] | 298 | } |
Jason Sams | eb4fe18 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 299 | |
| 300 | drv->glTarget = GL_NONE; |
| 301 | if (alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_GRAPHICS_TEXTURE) { |
| 302 | if (alloc->mHal.state.hasFaces) { |
| 303 | drv->glTarget = GL_TEXTURE_CUBE_MAP; |
| 304 | } else { |
| 305 | drv->glTarget = GL_TEXTURE_2D; |
| 306 | } |
| 307 | } else { |
| 308 | if (alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_GRAPHICS_VERTEX) { |
| 309 | drv->glTarget = GL_ARRAY_BUFFER; |
| 310 | } |
| 311 | } |
| 312 | |
Jason Sams | a614ae1 | 2011-05-26 17:05:51 -0700 | [diff] [blame] | 313 | drv->glType = rsdTypeToGLType(alloc->mHal.state.type->getElement()->getComponent().getType()); |
| 314 | drv->glFormat = rsdKindToGLFormat(alloc->mHal.state.type->getElement()->getComponent().getKind()); |
| 315 | |
Jason Sams | 7ac2a4d | 2012-02-15 12:04:24 -0800 | [diff] [blame] | 316 | if (forceZero && ptr) { |
Jason Sams | eb4fe18 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 317 | memset(ptr, 0, alloc->mHal.state.type->getSizeBytes()); |
| 318 | } |
| 319 | |
| 320 | if (alloc->mHal.state.usageFlags & ~RS_ALLOCATION_USAGE_SCRIPT) { |
| 321 | drv->uploadDeferred = true; |
| 322 | } |
Alex Sakhartchouk | a949524 | 2011-06-16 11:05:13 -0700 | [diff] [blame] | 323 | |
Jason Sams | b322033 | 2012-04-02 14:41:54 -0700 | [diff] [blame] | 324 | |
Alex Sakhartchouk | a949524 | 2011-06-16 11:05:13 -0700 | [diff] [blame] | 325 | drv->readBackFBO = NULL; |
| 326 | |
Jason Sams | eb4fe18 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 327 | return true; |
| 328 | } |
| 329 | |
| 330 | void rsdAllocationDestroy(const Context *rsc, Allocation *alloc) { |
| 331 | DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv; |
| 332 | |
| 333 | if (drv->bufferID) { |
| 334 | // Causes a SW crash.... |
Steve Block | 6598201 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 335 | //ALOGV(" mBufferID %i", mBufferID); |
Jason Sams | eb4fe18 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 336 | //glDeleteBuffers(1, &mBufferID); |
| 337 | //mBufferID = 0; |
| 338 | } |
| 339 | if (drv->textureID) { |
Jason Sams | 2382aba | 2011-09-13 15:41:01 -0700 | [diff] [blame] | 340 | RSD_CALL_GL(glDeleteTextures, 1, &drv->textureID); |
Jason Sams | eb4fe18 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 341 | drv->textureID = 0; |
| 342 | } |
| 343 | if (drv->renderTargetID) { |
Jason Sams | 2382aba | 2011-09-13 15:41:01 -0700 | [diff] [blame] | 344 | RSD_CALL_GL(glDeleteRenderbuffers, 1, &drv->renderTargetID); |
Jason Sams | eb4fe18 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 345 | drv->renderTargetID = 0; |
| 346 | } |
| 347 | |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 348 | if (alloc->mHal.drvState.lod[0].mallocPtr) { |
| 349 | free(alloc->mHal.drvState.lod[0].mallocPtr); |
| 350 | alloc->mHal.drvState.lod[0].mallocPtr = NULL; |
Jason Sams | eb4fe18 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 351 | } |
Alex Sakhartchouk | a949524 | 2011-06-16 11:05:13 -0700 | [diff] [blame] | 352 | if (drv->readBackFBO != NULL) { |
| 353 | delete drv->readBackFBO; |
| 354 | drv->readBackFBO = NULL; |
| 355 | } |
Jason Sams | eb4fe18 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 356 | free(drv); |
| 357 | alloc->mHal.drv = NULL; |
| 358 | } |
| 359 | |
| 360 | void rsdAllocationResize(const Context *rsc, const Allocation *alloc, |
| 361 | const Type *newType, bool zeroNew) { |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 362 | void * oldPtr = alloc->mHal.drvState.lod[0].mallocPtr; |
Jason Sams | 463bfce | 2012-08-21 13:54:42 -0700 | [diff] [blame] | 363 | // Calculate the object size |
| 364 | size_t s = AllocationBuildPointerTable(rsc, alloc, newType, NULL); |
| 365 | uint8_t *ptr = (uint8_t *)realloc(oldPtr, s); |
| 366 | // Build the relative pointer tables. |
| 367 | size_t verifySize = AllocationBuildPointerTable(rsc, alloc, newType, ptr); |
| 368 | if(s != verifySize) { |
| 369 | rsAssert(!"Size mismatch"); |
| 370 | } |
Jason Sams | eb4fe18 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 371 | |
| 372 | const uint32_t oldDimX = alloc->mHal.state.dimensionX; |
| 373 | const uint32_t dimX = newType->getDimX(); |
| 374 | |
| 375 | if (dimX > oldDimX) { |
Jason Sams | 463bfce | 2012-08-21 13:54:42 -0700 | [diff] [blame] | 376 | uint32_t stride = alloc->mHal.state.elementSizeBytes; |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 377 | memset(((uint8_t *)alloc->mHal.drvState.lod[0].mallocPtr) + stride * oldDimX, |
Jason Sams | 2275e9c | 2012-04-16 17:01:26 -0700 | [diff] [blame] | 378 | 0, stride * (dimX - oldDimX)); |
Jason Sams | eb4fe18 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 379 | } |
| 380 | } |
| 381 | |
Alex Sakhartchouk | a949524 | 2011-06-16 11:05:13 -0700 | [diff] [blame] | 382 | static void rsdAllocationSyncFromFBO(const Context *rsc, const Allocation *alloc) { |
| 383 | if (!alloc->getIsScript()) { |
| 384 | return; // nothing to sync |
| 385 | } |
| 386 | |
| 387 | RsdHal *dc = (RsdHal *)rsc->mHal.drv; |
| 388 | RsdFrameBufferObj *lastFbo = dc->gl.currentFrameBuffer; |
| 389 | |
| 390 | DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv; |
| 391 | if (!drv->textureID && !drv->renderTargetID) { |
| 392 | return; // nothing was rendered here yet, so nothing to sync |
| 393 | } |
| 394 | if (drv->readBackFBO == NULL) { |
| 395 | drv->readBackFBO = new RsdFrameBufferObj(); |
| 396 | drv->readBackFBO->setColorTarget(drv, 0); |
| 397 | drv->readBackFBO->setDimensions(alloc->getType()->getDimX(), |
| 398 | alloc->getType()->getDimY()); |
| 399 | } |
| 400 | |
| 401 | // Bind the framebuffer object so we can read back from it |
| 402 | drv->readBackFBO->setActive(rsc); |
| 403 | |
| 404 | // Do the readback |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 405 | RSD_CALL_GL(glReadPixels, 0, 0, alloc->mHal.drvState.lod[0].dimX, |
| 406 | alloc->mHal.drvState.lod[0].dimY, |
| 407 | drv->glFormat, drv->glType, alloc->mHal.drvState.lod[0].mallocPtr); |
Alex Sakhartchouk | a949524 | 2011-06-16 11:05:13 -0700 | [diff] [blame] | 408 | |
| 409 | // Revert framebuffer to its original |
| 410 | lastFbo->setActive(rsc); |
| 411 | } |
Jason Sams | eb4fe18 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 412 | |
| 413 | |
| 414 | void rsdAllocationSyncAll(const Context *rsc, const Allocation *alloc, |
| 415 | RsAllocationUsageType src) { |
| 416 | DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv; |
| 417 | |
Alex Sakhartchouk | a949524 | 2011-06-16 11:05:13 -0700 | [diff] [blame] | 418 | if (src == RS_ALLOCATION_USAGE_GRAPHICS_RENDER_TARGET) { |
| 419 | if(!alloc->getIsRenderTarget()) { |
| 420 | rsc->setError(RS_ERROR_FATAL_DRIVER, |
| 421 | "Attempting to sync allocation from render target, " |
| 422 | "for non-render target allocation"); |
| 423 | } else if (alloc->getType()->getElement()->getKind() != RS_KIND_PIXEL_RGBA) { |
| 424 | rsc->setError(RS_ERROR_FATAL_DRIVER, "Cannot only sync from RGBA" |
| 425 | "render target"); |
| 426 | } else { |
| 427 | rsdAllocationSyncFromFBO(rsc, alloc); |
| 428 | } |
Jason Sams | eb4fe18 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 429 | return; |
| 430 | } |
| 431 | |
| 432 | rsAssert(src == RS_ALLOCATION_USAGE_SCRIPT); |
| 433 | |
| 434 | if (alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_GRAPHICS_TEXTURE) { |
| 435 | UploadToTexture(rsc, alloc); |
| 436 | } else { |
Jason Sams | b322033 | 2012-04-02 14:41:54 -0700 | [diff] [blame] | 437 | if ((alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_GRAPHICS_RENDER_TARGET) && |
Jason Sams | 0dc6693 | 2012-04-10 17:05:49 -0700 | [diff] [blame] | 438 | !(alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_IO_OUTPUT)) { |
Jason Sams | eb4fe18 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 439 | AllocateRenderTarget(rsc, alloc); |
| 440 | } |
| 441 | } |
| 442 | if (alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_GRAPHICS_VERTEX) { |
| 443 | UploadToBufferObject(rsc, alloc); |
| 444 | } |
| 445 | |
| 446 | drv->uploadDeferred = false; |
| 447 | } |
| 448 | |
| 449 | void rsdAllocationMarkDirty(const Context *rsc, const Allocation *alloc) { |
| 450 | DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv; |
| 451 | drv->uploadDeferred = true; |
| 452 | } |
| 453 | |
Jason Sams | 41e373d | 2012-01-13 14:01:20 -0800 | [diff] [blame] | 454 | int32_t rsdAllocationInitSurfaceTexture(const Context *rsc, const Allocation *alloc) { |
| 455 | DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv; |
| 456 | UploadToTexture(rsc, alloc); |
| 457 | return drv->textureID; |
| 458 | } |
| 459 | |
Jason Sams | 7ac2a4d | 2012-02-15 12:04:24 -0800 | [diff] [blame] | 460 | static bool IoGetBuffer(const Context *rsc, Allocation *alloc, ANativeWindow *nw) { |
| 461 | DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv; |
| 462 | |
Jamie Gennis | cd919a1 | 2012-06-13 16:34:11 -0700 | [diff] [blame] | 463 | int32_t r = native_window_dequeue_buffer_and_wait(nw, &drv->wndBuffer); |
Jason Sams | 7ac2a4d | 2012-02-15 12:04:24 -0800 | [diff] [blame] | 464 | if (r) { |
| 465 | rsc->setError(RS_ERROR_DRIVER, "Error getting next IO output buffer."); |
| 466 | return false; |
| 467 | } |
| 468 | |
Jason Sams | 7ac2a4d | 2012-02-15 12:04:24 -0800 | [diff] [blame] | 469 | // Must lock the whole surface |
| 470 | GraphicBufferMapper &mapper = GraphicBufferMapper::get(); |
| 471 | Rect bounds(drv->wndBuffer->width, drv->wndBuffer->height); |
| 472 | |
| 473 | void *dst = NULL; |
| 474 | mapper.lock(drv->wndBuffer->handle, |
| 475 | GRALLOC_USAGE_SW_READ_NEVER | GRALLOC_USAGE_SW_WRITE_OFTEN, |
| 476 | bounds, &dst); |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 477 | alloc->mHal.drvState.lod[0].mallocPtr = dst; |
| 478 | 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] | 479 | |
Jason Sams | 7ac2a4d | 2012-02-15 12:04:24 -0800 | [diff] [blame] | 480 | return true; |
| 481 | } |
| 482 | |
| 483 | void rsdAllocationSetSurfaceTexture(const Context *rsc, Allocation *alloc, ANativeWindow *nw) { |
| 484 | DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv; |
| 485 | |
| 486 | //ALOGE("rsdAllocationSetSurfaceTexture %p %p", alloc, nw); |
| 487 | |
Jason Sams | b322033 | 2012-04-02 14:41:54 -0700 | [diff] [blame] | 488 | if (alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_GRAPHICS_RENDER_TARGET) { |
| 489 | //TODO finish support for render target + script |
| 490 | drv->wnd = nw; |
| 491 | return; |
| 492 | } |
| 493 | |
| 494 | |
Jason Sams | 7ac2a4d | 2012-02-15 12:04:24 -0800 | [diff] [blame] | 495 | // Cleanup old surface if there is one. |
| 496 | if (alloc->mHal.state.wndSurface) { |
| 497 | ANativeWindow *old = alloc->mHal.state.wndSurface; |
| 498 | GraphicBufferMapper &mapper = GraphicBufferMapper::get(); |
| 499 | mapper.unlock(drv->wndBuffer->handle); |
Jamie Gennis | cd919a1 | 2012-06-13 16:34:11 -0700 | [diff] [blame] | 500 | old->queueBuffer(old, drv->wndBuffer, -1); |
Jason Sams | 7ac2a4d | 2012-02-15 12:04:24 -0800 | [diff] [blame] | 501 | } |
| 502 | |
| 503 | if (nw != NULL) { |
| 504 | int32_t r; |
Jason Sams | b322033 | 2012-04-02 14:41:54 -0700 | [diff] [blame] | 505 | uint32_t flags = 0; |
| 506 | if (alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_SCRIPT) { |
| 507 | flags |= GRALLOC_USAGE_SW_READ_RARELY | GRALLOC_USAGE_SW_WRITE_OFTEN; |
| 508 | } |
| 509 | if (alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_GRAPHICS_RENDER_TARGET) { |
| 510 | flags |= GRALLOC_USAGE_HW_RENDER; |
| 511 | } |
| 512 | |
| 513 | r = native_window_set_usage(nw, flags); |
Jason Sams | 7ac2a4d | 2012-02-15 12:04:24 -0800 | [diff] [blame] | 514 | if (r) { |
| 515 | rsc->setError(RS_ERROR_DRIVER, "Error setting IO output buffer usage."); |
| 516 | return; |
| 517 | } |
| 518 | |
| 519 | r = native_window_set_buffers_dimensions(nw, alloc->mHal.state.dimensionX, |
| 520 | alloc->mHal.state.dimensionY); |
| 521 | if (r) { |
| 522 | rsc->setError(RS_ERROR_DRIVER, "Error setting IO output buffer dimensions."); |
| 523 | return; |
| 524 | } |
| 525 | |
| 526 | r = native_window_set_buffer_count(nw, 3); |
| 527 | if (r) { |
| 528 | rsc->setError(RS_ERROR_DRIVER, "Error setting IO output buffer count."); |
| 529 | return; |
| 530 | } |
| 531 | |
| 532 | IoGetBuffer(rsc, alloc, nw); |
| 533 | } |
| 534 | } |
| 535 | |
| 536 | void rsdAllocationIoSend(const Context *rsc, Allocation *alloc) { |
| 537 | DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv; |
| 538 | ANativeWindow *nw = alloc->mHal.state.wndSurface; |
| 539 | |
Jason Sams | b322033 | 2012-04-02 14:41:54 -0700 | [diff] [blame] | 540 | if (alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_GRAPHICS_RENDER_TARGET) { |
| 541 | RsdHal *dc = (RsdHal *)rsc->mHal.drv; |
| 542 | RSD_CALL_GL(eglSwapBuffers, dc->gl.egl.display, dc->gl.egl.surface); |
Jason Sams | 7ac2a4d | 2012-02-15 12:04:24 -0800 | [diff] [blame] | 543 | return; |
| 544 | } |
| 545 | |
Jason Sams | b322033 | 2012-04-02 14:41:54 -0700 | [diff] [blame] | 546 | if (alloc->mHal.state.usageFlags & RS_ALLOCATION_USAGE_SCRIPT) { |
| 547 | GraphicBufferMapper &mapper = GraphicBufferMapper::get(); |
| 548 | mapper.unlock(drv->wndBuffer->handle); |
Jamie Gennis | cd919a1 | 2012-06-13 16:34:11 -0700 | [diff] [blame] | 549 | int32_t r = nw->queueBuffer(nw, drv->wndBuffer, -1); |
Jason Sams | b322033 | 2012-04-02 14:41:54 -0700 | [diff] [blame] | 550 | if (r) { |
| 551 | rsc->setError(RS_ERROR_DRIVER, "Error sending IO output buffer."); |
| 552 | return; |
| 553 | } |
| 554 | |
| 555 | IoGetBuffer(rsc, alloc, nw); |
| 556 | } |
Jason Sams | 7ac2a4d | 2012-02-15 12:04:24 -0800 | [diff] [blame] | 557 | } |
| 558 | |
| 559 | void rsdAllocationIoReceive(const Context *rsc, Allocation *alloc) { |
Jason Sams | 3522f40 | 2012-03-23 11:47:26 -0700 | [diff] [blame] | 560 | DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv; |
| 561 | alloc->mHal.state.surfaceTexture->updateTexImage(); |
Jason Sams | 7ac2a4d | 2012-02-15 12:04:24 -0800 | [diff] [blame] | 562 | } |
| 563 | |
| 564 | |
Jason Sams | eb4fe18 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 565 | void rsdAllocationData1D(const Context *rsc, const Allocation *alloc, |
| 566 | uint32_t xoff, uint32_t lod, uint32_t count, |
Alex Sakhartchouk | c794cd5 | 2012-02-13 11:57:32 -0800 | [diff] [blame] | 567 | const void *data, size_t sizeBytes) { |
Jason Sams | eb4fe18 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 568 | DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv; |
| 569 | |
| 570 | const uint32_t eSize = alloc->mHal.state.type->getElementSizeBytes(); |
Jason Sams | 807fdc4 | 2012-07-25 17:55:39 -0700 | [diff] [blame] | 571 | 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] | 572 | uint32_t size = count * eSize; |
| 573 | |
| 574 | if (alloc->mHal.state.hasReferences) { |
| 575 | alloc->incRefs(data, count); |
| 576 | alloc->decRefs(ptr, count); |
| 577 | } |
| 578 | |
| 579 | memcpy(ptr, data, size); |
| 580 | drv->uploadDeferred = true; |
| 581 | } |
| 582 | |
| 583 | void rsdAllocationData2D(const Context *rsc, const Allocation *alloc, |
| 584 | uint32_t xoff, uint32_t yoff, uint32_t lod, RsAllocationCubemapFace face, |
Alex Sakhartchouk | c794cd5 | 2012-02-13 11:57:32 -0800 | [diff] [blame] | 585 | uint32_t w, uint32_t h, const void *data, size_t sizeBytes) { |
Jason Sams | eb4fe18 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 586 | DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv; |
| 587 | |
| 588 | uint32_t eSize = alloc->mHal.state.elementSizeBytes; |
| 589 | uint32_t lineSize = eSize * w; |
Jason Sams | eb4fe18 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 590 | |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 591 | if (alloc->mHal.drvState.lod[0].mallocPtr) { |
Jason Sams | eb4fe18 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 592 | const uint8_t *src = static_cast<const uint8_t *>(data); |
Jason Sams | 807fdc4 | 2012-07-25 17:55:39 -0700 | [diff] [blame] | 593 | uint8_t *dst = GetOffsetPtr(alloc, xoff, yoff, lod, face); |
Jason Sams | eb4fe18 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 594 | |
| 595 | for (uint32_t line=yoff; line < (yoff+h); line++) { |
| 596 | if (alloc->mHal.state.hasReferences) { |
| 597 | alloc->incRefs(src, w); |
| 598 | alloc->decRefs(dst, w); |
| 599 | } |
| 600 | memcpy(dst, src, lineSize); |
| 601 | src += lineSize; |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 602 | dst += alloc->mHal.drvState.lod[lod].stride; |
Jason Sams | eb4fe18 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 603 | } |
| 604 | drv->uploadDeferred = true; |
| 605 | } else { |
Jason Sams | 2382aba | 2011-09-13 15:41:01 -0700 | [diff] [blame] | 606 | Update2DTexture(rsc, alloc, data, xoff, yoff, lod, face, w, h); |
Jason Sams | eb4fe18 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 607 | } |
| 608 | } |
| 609 | |
| 610 | void rsdAllocationData3D(const Context *rsc, const Allocation *alloc, |
| 611 | uint32_t xoff, uint32_t yoff, uint32_t zoff, |
| 612 | uint32_t lod, RsAllocationCubemapFace face, |
| 613 | uint32_t w, uint32_t h, uint32_t d, const void *data, uint32_t sizeBytes) { |
| 614 | |
| 615 | } |
| 616 | |
Jason Sams | 807fdc4 | 2012-07-25 17:55:39 -0700 | [diff] [blame] | 617 | void rsdAllocationRead1D(const Context *rsc, const Allocation *alloc, |
| 618 | uint32_t xoff, uint32_t lod, uint32_t count, |
| 619 | void *data, size_t sizeBytes) { |
Jason Sams | 807fdc4 | 2012-07-25 17:55:39 -0700 | [diff] [blame] | 620 | const uint32_t eSize = alloc->mHal.state.type->getElementSizeBytes(); |
| 621 | const uint8_t * ptr = GetOffsetPtr(alloc, xoff, 0, 0, RS_ALLOCATION_CUBEMAP_FACE_POSITIVE_X); |
| 622 | memcpy(data, ptr, count * eSize); |
| 623 | } |
| 624 | |
| 625 | void rsdAllocationRead2D(const Context *rsc, const Allocation *alloc, |
| 626 | uint32_t xoff, uint32_t yoff, uint32_t lod, RsAllocationCubemapFace face, |
| 627 | uint32_t w, uint32_t h, void *data, size_t sizeBytes) { |
Jason Sams | 807fdc4 | 2012-07-25 17:55:39 -0700 | [diff] [blame] | 628 | uint32_t eSize = alloc->mHal.state.elementSizeBytes; |
| 629 | uint32_t lineSize = eSize * w; |
| 630 | |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 631 | if (alloc->mHal.drvState.lod[0].mallocPtr) { |
Jason Sams | 807fdc4 | 2012-07-25 17:55:39 -0700 | [diff] [blame] | 632 | uint8_t *dst = static_cast<uint8_t *>(data); |
| 633 | const uint8_t *src = GetOffsetPtr(alloc, xoff, yoff, lod, face); |
| 634 | |
| 635 | for (uint32_t line=yoff; line < (yoff+h); line++) { |
| 636 | memcpy(dst, src, lineSize); |
| 637 | dst += lineSize; |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 638 | src += alloc->mHal.drvState.lod[lod].stride; |
Jason Sams | 807fdc4 | 2012-07-25 17:55:39 -0700 | [diff] [blame] | 639 | } |
| 640 | } else { |
| 641 | ALOGE("Add code to readback from non-script memory"); |
| 642 | } |
| 643 | } |
| 644 | |
| 645 | void rsdAllocationRead3D(const Context *rsc, const Allocation *alloc, |
| 646 | uint32_t xoff, uint32_t yoff, uint32_t zoff, |
| 647 | uint32_t lod, RsAllocationCubemapFace face, |
| 648 | uint32_t w, uint32_t h, uint32_t d, void *data, uint32_t sizeBytes) { |
| 649 | |
| 650 | } |
| 651 | |
| 652 | void * rsdAllocationLock1D(const android::renderscript::Context *rsc, |
| 653 | const android::renderscript::Allocation *alloc) { |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 654 | return alloc->mHal.drvState.lod[0].mallocPtr; |
Jason Sams | 807fdc4 | 2012-07-25 17:55:39 -0700 | [diff] [blame] | 655 | } |
| 656 | |
| 657 | void rsdAllocationUnlock1D(const android::renderscript::Context *rsc, |
| 658 | const android::renderscript::Allocation *alloc) { |
| 659 | |
| 660 | } |
| 661 | |
Alex Sakhartchouk | 74a8279 | 2011-06-14 11:13:19 -0700 | [diff] [blame] | 662 | void rsdAllocationData1D_alloc(const android::renderscript::Context *rsc, |
| 663 | const android::renderscript::Allocation *dstAlloc, |
| 664 | uint32_t dstXoff, uint32_t dstLod, uint32_t count, |
| 665 | const android::renderscript::Allocation *srcAlloc, |
Alex Sakhartchouk | a949524 | 2011-06-16 11:05:13 -0700 | [diff] [blame] | 666 | uint32_t srcXoff, uint32_t srcLod) { |
| 667 | } |
| 668 | |
Alex Sakhartchouk | a949524 | 2011-06-16 11:05:13 -0700 | [diff] [blame] | 669 | |
| 670 | void rsdAllocationData2D_alloc_script(const android::renderscript::Context *rsc, |
| 671 | const android::renderscript::Allocation *dstAlloc, |
| 672 | uint32_t dstXoff, uint32_t dstYoff, uint32_t dstLod, |
| 673 | RsAllocationCubemapFace dstFace, uint32_t w, uint32_t h, |
| 674 | const android::renderscript::Allocation *srcAlloc, |
| 675 | uint32_t srcXoff, uint32_t srcYoff, uint32_t srcLod, |
| 676 | RsAllocationCubemapFace srcFace) { |
| 677 | uint32_t elementSize = dstAlloc->getType()->getElementSizeBytes(); |
| 678 | for (uint32_t i = 0; i < h; i ++) { |
Jason Sams | 807fdc4 | 2012-07-25 17:55:39 -0700 | [diff] [blame] | 679 | uint8_t *dstPtr = GetOffsetPtr(dstAlloc, dstXoff, dstYoff + i, dstLod, dstFace); |
| 680 | uint8_t *srcPtr = GetOffsetPtr(srcAlloc, srcXoff, srcYoff + i, srcLod, srcFace); |
Alex Sakhartchouk | a949524 | 2011-06-16 11:05:13 -0700 | [diff] [blame] | 681 | memcpy(dstPtr, srcPtr, w * elementSize); |
| 682 | |
Steve Block | af12ac6 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 683 | //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] | 684 | // dstXoff, dstYoff, dstLod, dstFace, w, h, srcXoff, srcYoff, srcLod, srcFace); |
Alex Sakhartchouk | a949524 | 2011-06-16 11:05:13 -0700 | [diff] [blame] | 685 | } |
Alex Sakhartchouk | 74a8279 | 2011-06-14 11:13:19 -0700 | [diff] [blame] | 686 | } |
| 687 | |
| 688 | void rsdAllocationData2D_alloc(const android::renderscript::Context *rsc, |
| 689 | const android::renderscript::Allocation *dstAlloc, |
| 690 | uint32_t dstXoff, uint32_t dstYoff, uint32_t dstLod, |
| 691 | RsAllocationCubemapFace dstFace, uint32_t w, uint32_t h, |
| 692 | const android::renderscript::Allocation *srcAlloc, |
| 693 | uint32_t srcXoff, uint32_t srcYoff, uint32_t srcLod, |
| 694 | RsAllocationCubemapFace srcFace) { |
Alex Sakhartchouk | a949524 | 2011-06-16 11:05:13 -0700 | [diff] [blame] | 695 | if (!dstAlloc->getIsScript() && !srcAlloc->getIsScript()) { |
| 696 | rsc->setError(RS_ERROR_FATAL_DRIVER, "Non-script allocation copies not " |
| 697 | "yet implemented."); |
| 698 | return; |
| 699 | } |
| 700 | rsdAllocationData2D_alloc_script(rsc, dstAlloc, dstXoff, dstYoff, |
| 701 | dstLod, dstFace, w, h, srcAlloc, |
| 702 | srcXoff, srcYoff, srcLod, srcFace); |
Alex Sakhartchouk | 74a8279 | 2011-06-14 11:13:19 -0700 | [diff] [blame] | 703 | } |
| 704 | |
| 705 | void rsdAllocationData3D_alloc(const android::renderscript::Context *rsc, |
| 706 | const android::renderscript::Allocation *dstAlloc, |
| 707 | uint32_t dstXoff, uint32_t dstYoff, uint32_t dstZoff, |
| 708 | uint32_t dstLod, RsAllocationCubemapFace dstFace, |
| 709 | uint32_t w, uint32_t h, uint32_t d, |
| 710 | const android::renderscript::Allocation *srcAlloc, |
| 711 | uint32_t srcXoff, uint32_t srcYoff, uint32_t srcZoff, |
| 712 | uint32_t srcLod, RsAllocationCubemapFace srcFace) { |
| 713 | } |
| 714 | |
Jason Sams | eb4fe18 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 715 | void rsdAllocationElementData1D(const Context *rsc, const Allocation *alloc, |
| 716 | uint32_t x, |
| 717 | const void *data, uint32_t cIdx, uint32_t sizeBytes) { |
| 718 | DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv; |
| 719 | |
| 720 | uint32_t eSize = alloc->mHal.state.elementSizeBytes; |
Jason Sams | 807fdc4 | 2012-07-25 17:55:39 -0700 | [diff] [blame] | 721 | 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] | 722 | |
| 723 | const Element * e = alloc->mHal.state.type->getElement()->getField(cIdx); |
| 724 | ptr += alloc->mHal.state.type->getElement()->getFieldOffsetBytes(cIdx); |
| 725 | |
| 726 | if (alloc->mHal.state.hasReferences) { |
| 727 | e->incRefs(data); |
| 728 | e->decRefs(ptr); |
| 729 | } |
| 730 | |
| 731 | memcpy(ptr, data, sizeBytes); |
| 732 | drv->uploadDeferred = true; |
| 733 | } |
| 734 | |
| 735 | void rsdAllocationElementData2D(const Context *rsc, const Allocation *alloc, |
| 736 | uint32_t x, uint32_t y, |
| 737 | const void *data, uint32_t cIdx, uint32_t sizeBytes) { |
| 738 | DrvAllocation *drv = (DrvAllocation *)alloc->mHal.drv; |
| 739 | |
| 740 | uint32_t eSize = alloc->mHal.state.elementSizeBytes; |
Jason Sams | 807fdc4 | 2012-07-25 17:55:39 -0700 | [diff] [blame] | 741 | 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] | 742 | |
| 743 | const Element * e = alloc->mHal.state.type->getElement()->getField(cIdx); |
| 744 | ptr += alloc->mHal.state.type->getElement()->getFieldOffsetBytes(cIdx); |
| 745 | |
| 746 | if (alloc->mHal.state.hasReferences) { |
| 747 | e->incRefs(data); |
| 748 | e->decRefs(ptr); |
| 749 | } |
| 750 | |
| 751 | memcpy(ptr, data, sizeBytes); |
| 752 | drv->uploadDeferred = true; |
| 753 | } |
| 754 | |
Jason Sams | 61a4bb7 | 2012-07-25 19:33:43 -0700 | [diff] [blame] | 755 | static void mip565(const Allocation *alloc, int lod, RsAllocationCubemapFace face) { |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 756 | uint32_t w = alloc->mHal.drvState.lod[lod + 1].dimX; |
| 757 | uint32_t h = alloc->mHal.drvState.lod[lod + 1].dimY; |
Jason Sams | 61a4bb7 | 2012-07-25 19:33:43 -0700 | [diff] [blame] | 758 | |
| 759 | for (uint32_t y=0; y < h; y++) { |
| 760 | uint16_t *oPtr = (uint16_t *)GetOffsetPtr(alloc, 0, y, lod + 1, face); |
| 761 | const uint16_t *i1 = (uint16_t *)GetOffsetPtr(alloc, 0, y*2, lod, face); |
| 762 | const uint16_t *i2 = (uint16_t *)GetOffsetPtr(alloc, 0, y*2+1, lod, face); |
| 763 | |
| 764 | for (uint32_t x=0; x < w; x++) { |
| 765 | *oPtr = rsBoxFilter565(i1[0], i1[1], i2[0], i2[1]); |
| 766 | oPtr ++; |
| 767 | i1 += 2; |
| 768 | i2 += 2; |
| 769 | } |
| 770 | } |
| 771 | } |
| 772 | |
| 773 | static void mip8888(const Allocation *alloc, int lod, RsAllocationCubemapFace face) { |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 774 | uint32_t w = alloc->mHal.drvState.lod[lod + 1].dimX; |
| 775 | uint32_t h = alloc->mHal.drvState.lod[lod + 1].dimY; |
Jason Sams | 61a4bb7 | 2012-07-25 19:33:43 -0700 | [diff] [blame] | 776 | |
| 777 | for (uint32_t y=0; y < h; y++) { |
| 778 | uint32_t *oPtr = (uint32_t *)GetOffsetPtr(alloc, 0, y, lod + 1, face); |
| 779 | const uint32_t *i1 = (uint32_t *)GetOffsetPtr(alloc, 0, y*2, lod, face); |
| 780 | const uint32_t *i2 = (uint32_t *)GetOffsetPtr(alloc, 0, y*2+1, lod, face); |
| 781 | |
| 782 | for (uint32_t x=0; x < w; x++) { |
| 783 | *oPtr = rsBoxFilter8888(i1[0], i1[1], i2[0], i2[1]); |
| 784 | oPtr ++; |
| 785 | i1 += 2; |
| 786 | i2 += 2; |
| 787 | } |
| 788 | } |
| 789 | } |
| 790 | |
| 791 | static void mip8(const Allocation *alloc, int lod, RsAllocationCubemapFace face) { |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 792 | uint32_t w = alloc->mHal.drvState.lod[lod + 1].dimX; |
| 793 | uint32_t h = alloc->mHal.drvState.lod[lod + 1].dimY; |
Jason Sams | 61a4bb7 | 2012-07-25 19:33:43 -0700 | [diff] [blame] | 794 | |
| 795 | for (uint32_t y=0; y < h; y++) { |
| 796 | uint8_t *oPtr = GetOffsetPtr(alloc, 0, y, lod + 1, face); |
| 797 | const uint8_t *i1 = GetOffsetPtr(alloc, 0, y*2, lod, face); |
| 798 | const uint8_t *i2 = GetOffsetPtr(alloc, 0, y*2+1, lod, face); |
| 799 | |
| 800 | for (uint32_t x=0; x < w; x++) { |
| 801 | *oPtr = (uint8_t)(((uint32_t)i1[0] + i1[1] + i2[0] + i2[1]) * 0.25f); |
| 802 | oPtr ++; |
| 803 | i1 += 2; |
| 804 | i2 += 2; |
| 805 | } |
| 806 | } |
| 807 | } |
| 808 | |
| 809 | void rsdAllocationGenerateMipmaps(const Context *rsc, const Allocation *alloc) { |
Jason Sams | 709a097 | 2012-11-15 18:18:04 -0800 | [diff] [blame] | 810 | if(!alloc->mHal.drvState.lod[0].mallocPtr) { |
Jason Sams | 61a4bb7 | 2012-07-25 19:33:43 -0700 | [diff] [blame] | 811 | return; |
| 812 | } |
| 813 | uint32_t numFaces = alloc->getType()->getDimFaces() ? 6 : 1; |
| 814 | for (uint32_t face = 0; face < numFaces; face ++) { |
| 815 | for (uint32_t lod=0; lod < (alloc->getType()->getLODCount() -1); lod++) { |
| 816 | switch (alloc->getType()->getElement()->getSizeBits()) { |
| 817 | case 32: |
| 818 | mip8888(alloc, lod, (RsAllocationCubemapFace)face); |
| 819 | break; |
| 820 | case 16: |
| 821 | mip565(alloc, lod, (RsAllocationCubemapFace)face); |
| 822 | break; |
| 823 | case 8: |
| 824 | mip8(alloc, lod, (RsAllocationCubemapFace)face); |
| 825 | break; |
| 826 | } |
| 827 | } |
| 828 | } |
| 829 | } |
| 830 | |
Jason Sams | eb4fe18 | 2011-05-26 16:33:01 -0700 | [diff] [blame] | 831 | |